1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
|
//===- Instruction.cpp - The Instructions of Sandbox IR -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/SandboxIR/Instruction.h"
#include "llvm/SandboxIR/Function.h"
namespace llvm::sandboxir {
const char *Instruction::getOpcodeName(Opcode Opc) {
switch (Opc) {
#define OP(OPC) \
case Opcode::OPC: \
return #OPC;
#define OPCODES(...) __VA_ARGS__
#define DEF_INSTR(ID, OPC, CLASS) OPC
#include "llvm/SandboxIR/Values.def"
}
llvm_unreachable("Unknown Opcode");
}
llvm::Instruction *Instruction::getTopmostLLVMInstruction() const {
Instruction *Prev = getPrevNode();
if (Prev == nullptr) {
// If at top of the BB, return the first BB instruction.
return &*cast<llvm::BasicBlock>(getParent()->Val)->begin();
}
// Else get the Previous sandbox IR instruction's bottom IR instruction and
// return its successor.
llvm::Instruction *PrevBotI = cast<llvm::Instruction>(Prev->Val);
return PrevBotI->getNextNode();
}
BBIterator Instruction::getIterator() const {
auto *I = cast<llvm::Instruction>(Val);
return BasicBlock::iterator(I->getParent(), I->getIterator(), &Ctx);
}
Instruction *Instruction::getNextNode() const {
assert(getParent() != nullptr && "Detached!");
assert(getIterator() != getParent()->end() && "Already at end!");
// `Val` is the bottom-most LLVM IR instruction. Get the next in the chain,
// and get the corresponding sandboxir Instruction that maps to it. This works
// even for SandboxIR Instructions that map to more than one LLVM Instruction.
auto *LLVMI = cast<llvm::Instruction>(Val);
assert(LLVMI->getParent() != nullptr && "LLVM IR instr is detached!");
auto *NextLLVMI = LLVMI->getNextNode();
auto *NextI = cast_or_null<Instruction>(Ctx.getValue(NextLLVMI));
if (NextI == nullptr)
return nullptr;
return NextI;
}
Instruction *Instruction::getPrevNode() const {
assert(getParent() != nullptr && "Detached!");
auto It = getIterator();
if (It != getParent()->begin())
return std::prev(getIterator()).get();
return nullptr;
}
void Instruction::removeFromParent() {
Ctx.getTracker().emplaceIfTracking<RemoveFromParent>(this);
// Detach all the LLVM IR instructions from their parent BB.
for (llvm::Instruction *I : getLLVMInstrs())
I->removeFromParent();
}
void Instruction::eraseFromParent() {
assert(users().empty() && "Still connected to users, can't erase!");
Ctx.runEraseInstrCallbacks(this);
std::unique_ptr<Value> Detached = Ctx.detach(this);
auto LLVMInstrs = getLLVMInstrs();
auto &Tracker = Ctx.getTracker();
if (Tracker.isTracking()) {
Tracker.track(std::make_unique<EraseFromParent>(std::move(Detached)));
// We don't actually delete the IR instruction, because then it would be
// impossible to bring it back from the dead at the same memory location.
// Instead we remove it from its BB and track its current location.
for (llvm::Instruction *I : LLVMInstrs)
I->removeFromParent();
// TODO: Multi-instructions need special treatment because some of the
// references are internal to the instruction.
for (llvm::Instruction *I : LLVMInstrs)
I->dropAllReferences();
} else {
// Erase in reverse to avoid erasing nstructions with attached uses.
for (llvm::Instruction *I : reverse(LLVMInstrs))
I->eraseFromParent();
}
}
void Instruction::moveBefore(BasicBlock &BB, const BBIterator &WhereIt) {
if (std::next(getIterator()) == WhereIt)
// Destination is same as origin, nothing to do.
return;
Ctx.runMoveInstrCallbacks(this, WhereIt);
Ctx.getTracker().emplaceIfTracking<MoveInstr>(this);
auto *LLVMBB = cast<llvm::BasicBlock>(BB.Val);
llvm::BasicBlock::iterator It;
if (WhereIt == BB.end()) {
It = LLVMBB->end();
} else {
Instruction *WhereI = &*WhereIt;
It = WhereI->getTopmostLLVMInstruction()->getIterator();
}
// TODO: Move this to the verifier of sandboxir::Instruction.
assert(is_sorted(getLLVMInstrs(),
[](auto *I1, auto *I2) { return I1->comesBefore(I2); }) &&
"Expected program order!");
// Do the actual move in LLVM IR.
for (auto *I : getLLVMInstrs())
I->moveBefore(*LLVMBB, It);
}
void Instruction::insertBefore(Instruction *BeforeI) {
llvm::Instruction *BeforeTopI = BeforeI->getTopmostLLVMInstruction();
Ctx.getTracker().emplaceIfTracking<InsertIntoBB>(this);
// Insert the LLVM IR Instructions in program order.
for (llvm::Instruction *I : getLLVMInstrs())
I->insertBefore(BeforeTopI->getIterator());
}
void Instruction::insertAfter(Instruction *AfterI) {
insertInto(AfterI->getParent(), std::next(AfterI->getIterator()));
}
void Instruction::insertInto(BasicBlock *BB, const BBIterator &WhereIt) {
llvm::BasicBlock *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
llvm::Instruction *LLVMBeforeI;
llvm::BasicBlock::iterator LLVMBeforeIt;
Instruction *BeforeI;
if (WhereIt != BB->end()) {
BeforeI = &*WhereIt;
LLVMBeforeI = BeforeI->getTopmostLLVMInstruction();
LLVMBeforeIt = LLVMBeforeI->getIterator();
} else {
BeforeI = nullptr;
LLVMBeforeI = nullptr;
LLVMBeforeIt = LLVMBB->end();
}
Ctx.getTracker().emplaceIfTracking<InsertIntoBB>(this);
// Insert the LLVM IR Instructions in program order.
for (llvm::Instruction *I : getLLVMInstrs())
I->insertInto(LLVMBB, LLVMBeforeIt);
}
BasicBlock *Instruction::getParent() const {
// Get the LLVM IR Instruction that this maps to, get its parent, and get the
// corresponding sandboxir::BasicBlock by looking it up in sandboxir::Context.
auto *BB = cast<llvm::Instruction>(Val)->getParent();
if (BB == nullptr)
return nullptr;
return cast<BasicBlock>(Ctx.getValue(BB));
}
bool Instruction::classof(const sandboxir::Value *From) {
switch (From->getSubclassID()) {
#define DEF_INSTR(ID, OPC, CLASS) \
case ClassID::ID: \
return true;
#include "llvm/SandboxIR/Values.def"
default:
return false;
}
}
void Instruction::setHasNoUnsignedWrap(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::hasNoUnsignedWrap,
&Instruction::setHasNoUnsignedWrap>>(
this);
cast<llvm::Instruction>(Val)->setHasNoUnsignedWrap(B);
}
void Instruction::setHasNoSignedWrap(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::hasNoSignedWrap,
&Instruction::setHasNoSignedWrap>>(this);
cast<llvm::Instruction>(Val)->setHasNoSignedWrap(B);
}
void Instruction::setFast(bool B) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&Instruction::isFast, &Instruction::setFast>>(this);
cast<llvm::Instruction>(Val)->setFast(B);
}
void Instruction::setIsExact(bool B) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&Instruction::isExact, &Instruction::setIsExact>>(this);
cast<llvm::Instruction>(Val)->setIsExact(B);
}
void Instruction::setHasAllowReassoc(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::hasAllowReassoc,
&Instruction::setHasAllowReassoc>>(this);
cast<llvm::Instruction>(Val)->setHasAllowReassoc(B);
}
void Instruction::setHasNoNaNs(bool B) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&Instruction::hasNoNaNs, &Instruction::setHasNoNaNs>>(
this);
cast<llvm::Instruction>(Val)->setHasNoNaNs(B);
}
void Instruction::setHasNoInfs(bool B) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&Instruction::hasNoInfs, &Instruction::setHasNoInfs>>(
this);
cast<llvm::Instruction>(Val)->setHasNoInfs(B);
}
void Instruction::setHasNoSignedZeros(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::hasNoSignedZeros,
&Instruction::setHasNoSignedZeros>>(
this);
cast<llvm::Instruction>(Val)->setHasNoSignedZeros(B);
}
void Instruction::setHasAllowReciprocal(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::hasAllowReciprocal,
&Instruction::setHasAllowReciprocal>>(
this);
cast<llvm::Instruction>(Val)->setHasAllowReciprocal(B);
}
void Instruction::setHasAllowContract(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::hasAllowContract,
&Instruction::setHasAllowContract>>(
this);
cast<llvm::Instruction>(Val)->setHasAllowContract(B);
}
void Instruction::setFastMathFlags(FastMathFlags FMF) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::getFastMathFlags,
&Instruction::copyFastMathFlags>>(this);
cast<llvm::Instruction>(Val)->setFastMathFlags(FMF);
}
void Instruction::copyFastMathFlags(FastMathFlags FMF) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::getFastMathFlags,
&Instruction::copyFastMathFlags>>(this);
cast<llvm::Instruction>(Val)->copyFastMathFlags(FMF);
}
Type *Instruction::getAccessType() const {
return Ctx.getType(cast<llvm::Instruction>(Val)->getAccessType());
}
void Instruction::setHasApproxFunc(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&Instruction::hasApproxFunc,
&Instruction::setHasApproxFunc>>(this);
cast<llvm::Instruction>(Val)->setHasApproxFunc(B);
}
#ifndef NDEBUG
void Instruction::dumpOS(raw_ostream &OS) const {
OS << "Unimplemented! Please override dump().";
}
#endif // NDEBUG
VAArgInst *VAArgInst::create(Value *List, Type *Ty, InsertPosition Pos,
Context &Ctx, const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *LLVMI =
cast<llvm::VAArgInst>(Builder.CreateVAArg(List->Val, Ty->LLVMTy, Name));
return Ctx.createVAArgInst(LLVMI);
}
Value *VAArgInst::getPointerOperand() {
return Ctx.getValue(cast<llvm::VAArgInst>(Val)->getPointerOperand());
}
FreezeInst *FreezeInst::create(Value *V, InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *LLVMI = cast<llvm::FreezeInst>(Builder.CreateFreeze(V->Val, Name));
return Ctx.createFreezeInst(LLVMI);
}
FenceInst *FenceInst::create(AtomicOrdering Ordering, InsertPosition Pos,
Context &Ctx, SyncScope::ID SSID) {
auto &Builder = Instruction::setInsertPos(Pos);
llvm::FenceInst *LLVMI = Builder.CreateFence(Ordering, SSID);
return Ctx.createFenceInst(LLVMI);
}
void FenceInst::setOrdering(AtomicOrdering Ordering) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&FenceInst::getOrdering, &FenceInst::setOrdering>>(
this);
cast<llvm::FenceInst>(Val)->setOrdering(Ordering);
}
void FenceInst::setSyncScopeID(SyncScope::ID SSID) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&FenceInst::getSyncScopeID,
&FenceInst::setSyncScopeID>>(this);
cast<llvm::FenceInst>(Val)->setSyncScopeID(SSID);
}
Value *SelectInst::create(Value *Cond, Value *True, Value *False,
InsertPosition Pos, Context &Ctx, const Twine &Name) {
auto &Builder = Instruction::setInsertPos(Pos);
llvm::Value *NewV =
Builder.CreateSelect(Cond->Val, True->Val, False->Val, Name);
if (auto *NewSI = dyn_cast<llvm::SelectInst>(NewV))
return Ctx.createSelectInst(NewSI);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
void SelectInst::swapValues() {
Ctx.getTracker().emplaceIfTracking<UseSwap>(getOperandUse(1),
getOperandUse(2));
cast<llvm::SelectInst>(Val)->swapValues();
}
bool SelectInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Select;
}
BranchInst *BranchInst::create(BasicBlock *IfTrue, InsertPosition Pos,
Context &Ctx) {
auto &Builder = setInsertPos(Pos);
llvm::BranchInst *NewBr =
Builder.CreateBr(cast<llvm::BasicBlock>(IfTrue->Val));
return Ctx.createBranchInst(NewBr);
}
BranchInst *BranchInst::create(BasicBlock *IfTrue, BasicBlock *IfFalse,
Value *Cond, InsertPosition Pos, Context &Ctx) {
auto &Builder = setInsertPos(Pos);
llvm::BranchInst *NewBr =
Builder.CreateCondBr(Cond->Val, cast<llvm::BasicBlock>(IfTrue->Val),
cast<llvm::BasicBlock>(IfFalse->Val));
return Ctx.createBranchInst(NewBr);
}
bool BranchInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Br;
}
Value *BranchInst::getCondition() const {
assert(isConditional() && "Cannot get condition of an uncond branch!");
return Ctx.getValue(cast<llvm::BranchInst>(Val)->getCondition());
}
BasicBlock *BranchInst::getSuccessor(unsigned SuccIdx) const {
assert(SuccIdx < getNumSuccessors() &&
"Successor # out of range for Branch!");
return cast_or_null<BasicBlock>(
Ctx.getValue(cast<llvm::BranchInst>(Val)->getSuccessor(SuccIdx)));
}
void BranchInst::setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
assert((Idx == 0 || Idx == 1) && "Out of bounds!");
setOperand(2u - Idx, NewSucc);
}
BasicBlock *BranchInst::LLVMBBToSBBB::operator()(llvm::BasicBlock *BB) const {
return cast<BasicBlock>(Ctx.getValue(BB));
}
const BasicBlock *
BranchInst::ConstLLVMBBToSBBB::operator()(const llvm::BasicBlock *BB) const {
return cast<BasicBlock>(Ctx.getValue(BB));
}
void LoadInst::setVolatile(bool V) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&LoadInst::isVolatile, &LoadInst::setVolatile>>(this);
cast<llvm::LoadInst>(Val)->setVolatile(V);
}
LoadInst *LoadInst::create(Type *Ty, Value *Ptr, MaybeAlign Align,
InsertPosition Pos, bool IsVolatile, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *NewLI =
Builder.CreateAlignedLoad(Ty->LLVMTy, Ptr->Val, Align, IsVolatile, Name);
auto *NewSBI = Ctx.createLoadInst(NewLI);
return NewSBI;
}
bool LoadInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Load;
}
Value *LoadInst::getPointerOperand() const {
return Ctx.getValue(cast<llvm::LoadInst>(Val)->getPointerOperand());
}
void StoreInst::setVolatile(bool V) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&StoreInst::isVolatile, &StoreInst::setVolatile>>(this);
cast<llvm::StoreInst>(Val)->setVolatile(V);
}
StoreInst *StoreInst::create(Value *V, Value *Ptr, MaybeAlign Align,
InsertPosition Pos, bool IsVolatile,
Context &Ctx) {
auto &Builder = setInsertPos(Pos);
auto *NewSI = Builder.CreateAlignedStore(V->Val, Ptr->Val, Align, IsVolatile);
auto *NewSBI = Ctx.createStoreInst(NewSI);
return NewSBI;
}
bool StoreInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Store;
}
Value *StoreInst::getValueOperand() const {
return Ctx.getValue(cast<llvm::StoreInst>(Val)->getValueOperand());
}
Value *StoreInst::getPointerOperand() const {
return Ctx.getValue(cast<llvm::StoreInst>(Val)->getPointerOperand());
}
UnreachableInst *UnreachableInst::create(InsertPosition Pos, Context &Ctx) {
auto &Builder = setInsertPos(Pos);
llvm::UnreachableInst *NewUI = Builder.CreateUnreachable();
return Ctx.createUnreachableInst(NewUI);
}
bool UnreachableInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Unreachable;
}
ReturnInst *ReturnInst::createCommon(Value *RetVal, IRBuilder<> &Builder,
Context &Ctx) {
llvm::ReturnInst *NewRI;
if (RetVal != nullptr)
NewRI = Builder.CreateRet(RetVal->Val);
else
NewRI = Builder.CreateRetVoid();
return Ctx.createReturnInst(NewRI);
}
ReturnInst *ReturnInst::create(Value *RetVal, InsertPosition Pos,
Context &Ctx) {
auto &Builder = setInsertPos(Pos);
return createCommon(RetVal, Builder, Ctx);
}
Value *ReturnInst::getReturnValue() const {
auto *LLVMRetVal = cast<llvm::ReturnInst>(Val)->getReturnValue();
return LLVMRetVal != nullptr ? Ctx.getValue(LLVMRetVal) : nullptr;
}
FunctionType *CallBase::getFunctionType() const {
return cast<FunctionType>(
Ctx.getType(cast<llvm::CallBase>(Val)->getFunctionType()));
}
Value *CallBase::getCalledOperand() const {
return Ctx.getValue(cast<llvm::CallBase>(Val)->getCalledOperand());
}
Use CallBase::getCalledOperandUse() const {
llvm::Use *LLVMUse = &cast<llvm::CallBase>(Val)->getCalledOperandUse();
return Use(LLVMUse, cast<User>(Ctx.getValue(LLVMUse->getUser())), Ctx);
}
Function *CallBase::getCalledFunction() const {
return cast_or_null<Function>(
Ctx.getValue(cast<llvm::CallBase>(Val)->getCalledFunction()));
}
Function *CallBase::getCaller() {
return cast<Function>(Ctx.getValue(cast<llvm::CallBase>(Val)->getCaller()));
}
void CallBase::setCalledFunction(Function *F) {
// F's function type is private, so we rely on `setCalledFunction()` to update
// it. But even though we are calling `setCalledFunction()` we also need to
// track this change at the SandboxIR level, which is why we call
// `setCalledOperand()` here.
// Note: This may break if `setCalledFunction()` early returns if `F`
// is already set, but we do have a unit test for it.
setCalledOperand(F);
cast<llvm::CallBase>(Val)->setCalledFunction(
cast<llvm::FunctionType>(F->getFunctionType()->LLVMTy),
cast<llvm::Function>(F->Val));
}
CallInst *CallInst::create(FunctionType *FTy, Value *Func,
ArrayRef<Value *> Args, InsertPosition Pos,
Context &Ctx, const Twine &NameStr) {
auto &Builder = setInsertPos(Pos);
SmallVector<llvm::Value *> LLVMArgs;
LLVMArgs.reserve(Args.size());
for (Value *Arg : Args)
LLVMArgs.push_back(Arg->Val);
llvm::CallInst *NewCI = Builder.CreateCall(
cast<llvm::FunctionType>(FTy->LLVMTy), Func->Val, LLVMArgs, NameStr);
return Ctx.createCallInst(NewCI);
}
InvokeInst *InvokeInst::create(FunctionType *FTy, Value *Func,
BasicBlock *IfNormal, BasicBlock *IfException,
ArrayRef<Value *> Args, InsertPosition Pos,
Context &Ctx, const Twine &NameStr) {
auto &Builder = setInsertPos(Pos);
SmallVector<llvm::Value *> LLVMArgs;
LLVMArgs.reserve(Args.size());
for (Value *Arg : Args)
LLVMArgs.push_back(Arg->Val);
llvm::InvokeInst *Invoke = Builder.CreateInvoke(
cast<llvm::FunctionType>(FTy->LLVMTy), Func->Val,
cast<llvm::BasicBlock>(IfNormal->Val),
cast<llvm::BasicBlock>(IfException->Val), LLVMArgs, NameStr);
return Ctx.createInvokeInst(Invoke);
}
BasicBlock *InvokeInst::getNormalDest() const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::InvokeInst>(Val)->getNormalDest()));
}
BasicBlock *InvokeInst::getUnwindDest() const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::InvokeInst>(Val)->getUnwindDest()));
}
void InvokeInst::setNormalDest(BasicBlock *BB) {
setOperand(1, BB);
assert(getNormalDest() == BB && "LLVM IR uses a different operan index!");
}
void InvokeInst::setUnwindDest(BasicBlock *BB) {
setOperand(2, BB);
assert(getUnwindDest() == BB && "LLVM IR uses a different operan index!");
}
LandingPadInst *InvokeInst::getLandingPadInst() const {
return cast<LandingPadInst>(
Ctx.getValue(cast<llvm::InvokeInst>(Val)->getLandingPadInst()));
;
}
BasicBlock *InvokeInst::getSuccessor(unsigned SuccIdx) const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::InvokeInst>(Val)->getSuccessor(SuccIdx)));
}
CallBrInst *CallBrInst::create(FunctionType *FTy, Value *Func,
BasicBlock *DefaultDest,
ArrayRef<BasicBlock *> IndirectDests,
ArrayRef<Value *> Args, InsertPosition Pos,
Context &Ctx, const Twine &NameStr) {
auto &Builder = setInsertPos(Pos);
SmallVector<llvm::BasicBlock *> LLVMIndirectDests;
LLVMIndirectDests.reserve(IndirectDests.size());
for (BasicBlock *IndDest : IndirectDests)
LLVMIndirectDests.push_back(cast<llvm::BasicBlock>(IndDest->Val));
SmallVector<llvm::Value *> LLVMArgs;
LLVMArgs.reserve(Args.size());
for (Value *Arg : Args)
LLVMArgs.push_back(Arg->Val);
llvm::CallBrInst *CallBr =
Builder.CreateCallBr(cast<llvm::FunctionType>(FTy->LLVMTy), Func->Val,
cast<llvm::BasicBlock>(DefaultDest->Val),
LLVMIndirectDests, LLVMArgs, NameStr);
return Ctx.createCallBrInst(CallBr);
}
Value *CallBrInst::getIndirectDestLabel(unsigned Idx) const {
return Ctx.getValue(cast<llvm::CallBrInst>(Val)->getIndirectDestLabel(Idx));
}
Value *CallBrInst::getIndirectDestLabelUse(unsigned Idx) const {
return Ctx.getValue(
cast<llvm::CallBrInst>(Val)->getIndirectDestLabelUse(Idx));
}
BasicBlock *CallBrInst::getDefaultDest() const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::CallBrInst>(Val)->getDefaultDest()));
}
BasicBlock *CallBrInst::getIndirectDest(unsigned Idx) const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::CallBrInst>(Val)->getIndirectDest(Idx)));
}
llvm::SmallVector<BasicBlock *, 16> CallBrInst::getIndirectDests() const {
SmallVector<BasicBlock *, 16> BBs;
for (llvm::BasicBlock *LLVMBB :
cast<llvm::CallBrInst>(Val)->getIndirectDests())
BBs.push_back(cast<BasicBlock>(Ctx.getValue(LLVMBB)));
return BBs;
}
void CallBrInst::setDefaultDest(BasicBlock *BB) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CallBrInst::getDefaultDest,
&CallBrInst::setDefaultDest>>(this);
cast<llvm::CallBrInst>(Val)->setDefaultDest(cast<llvm::BasicBlock>(BB->Val));
}
void CallBrInst::setIndirectDest(unsigned Idx, BasicBlock *BB) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetterWithIdx<&CallBrInst::getIndirectDest,
&CallBrInst::setIndirectDest>>(
this, Idx);
cast<llvm::CallBrInst>(Val)->setIndirectDest(Idx,
cast<llvm::BasicBlock>(BB->Val));
}
BasicBlock *CallBrInst::getSuccessor(unsigned Idx) const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::CallBrInst>(Val)->getSuccessor(Idx)));
}
LandingPadInst *LandingPadInst::create(Type *RetTy, unsigned NumReservedClauses,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::LandingPadInst *LLVMI =
Builder.CreateLandingPad(RetTy->LLVMTy, NumReservedClauses, Name);
return Ctx.createLandingPadInst(LLVMI);
}
void LandingPadInst::setCleanup(bool V) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&LandingPadInst::isCleanup,
&LandingPadInst::setCleanup>>(this);
cast<llvm::LandingPadInst>(Val)->setCleanup(V);
}
Constant *LandingPadInst::getClause(unsigned Idx) const {
return cast<Constant>(
Ctx.getValue(cast<llvm::LandingPadInst>(Val)->getClause(Idx)));
}
Value *FuncletPadInst::getParentPad() const {
return Ctx.getValue(cast<llvm::FuncletPadInst>(Val)->getParentPad());
}
void FuncletPadInst::setParentPad(Value *ParentPad) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&FuncletPadInst::getParentPad,
&FuncletPadInst::setParentPad>>(this);
cast<llvm::FuncletPadInst>(Val)->setParentPad(ParentPad->Val);
}
Value *FuncletPadInst::getArgOperand(unsigned Idx) const {
return Ctx.getValue(cast<llvm::FuncletPadInst>(Val)->getArgOperand(Idx));
}
void FuncletPadInst::setArgOperand(unsigned Idx, Value *V) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetterWithIdx<&FuncletPadInst::getArgOperand,
&FuncletPadInst::setArgOperand>>(
this, Idx);
cast<llvm::FuncletPadInst>(Val)->setArgOperand(Idx, V->Val);
}
CatchSwitchInst *CatchPadInst::getCatchSwitch() const {
return cast<CatchSwitchInst>(
Ctx.getValue(cast<llvm::CatchPadInst>(Val)->getCatchSwitch()));
}
CatchPadInst *CatchPadInst::create(Value *ParentPad, ArrayRef<Value *> Args,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
SmallVector<llvm::Value *> LLVMArgs;
LLVMArgs.reserve(Args.size());
for (auto *Arg : Args)
LLVMArgs.push_back(Arg->Val);
llvm::CatchPadInst *LLVMI =
Builder.CreateCatchPad(ParentPad->Val, LLVMArgs, Name);
return Ctx.createCatchPadInst(LLVMI);
}
CleanupPadInst *CleanupPadInst::create(Value *ParentPad, ArrayRef<Value *> Args,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
SmallVector<llvm::Value *> LLVMArgs;
LLVMArgs.reserve(Args.size());
for (auto *Arg : Args)
LLVMArgs.push_back(Arg->Val);
llvm::CleanupPadInst *LLVMI =
Builder.CreateCleanupPad(ParentPad->Val, LLVMArgs, Name);
return Ctx.createCleanupPadInst(LLVMI);
}
CatchReturnInst *CatchReturnInst::create(CatchPadInst *CatchPad, BasicBlock *BB,
InsertPosition Pos, Context &Ctx) {
auto &Builder = setInsertPos(Pos);
llvm::CatchReturnInst *LLVMI = Builder.CreateCatchRet(
cast<llvm::CatchPadInst>(CatchPad->Val), cast<llvm::BasicBlock>(BB->Val));
return Ctx.createCatchReturnInst(LLVMI);
}
CatchPadInst *CatchReturnInst::getCatchPad() const {
return cast<CatchPadInst>(
Ctx.getValue(cast<llvm::CatchReturnInst>(Val)->getCatchPad()));
}
void CatchReturnInst::setCatchPad(CatchPadInst *CatchPad) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CatchReturnInst::getCatchPad,
&CatchReturnInst::setCatchPad>>(this);
cast<llvm::CatchReturnInst>(Val)->setCatchPad(
cast<llvm::CatchPadInst>(CatchPad->Val));
}
BasicBlock *CatchReturnInst::getSuccessor() const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::CatchReturnInst>(Val)->getSuccessor()));
}
void CatchReturnInst::setSuccessor(BasicBlock *NewSucc) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CatchReturnInst::getSuccessor,
&CatchReturnInst::setSuccessor>>(this);
cast<llvm::CatchReturnInst>(Val)->setSuccessor(
cast<llvm::BasicBlock>(NewSucc->Val));
}
Value *CatchReturnInst::getCatchSwitchParentPad() const {
return Ctx.getValue(
cast<llvm::CatchReturnInst>(Val)->getCatchSwitchParentPad());
}
CleanupReturnInst *CleanupReturnInst::create(CleanupPadInst *CleanupPad,
BasicBlock *UnwindBB,
InsertPosition Pos, Context &Ctx) {
auto &Builder = setInsertPos(Pos);
auto *LLVMUnwindBB =
UnwindBB != nullptr ? cast<llvm::BasicBlock>(UnwindBB->Val) : nullptr;
llvm::CleanupReturnInst *LLVMI = Builder.CreateCleanupRet(
cast<llvm::CleanupPadInst>(CleanupPad->Val), LLVMUnwindBB);
return Ctx.createCleanupReturnInst(LLVMI);
}
CleanupPadInst *CleanupReturnInst::getCleanupPad() const {
return cast<CleanupPadInst>(
Ctx.getValue(cast<llvm::CleanupReturnInst>(Val)->getCleanupPad()));
}
void CleanupReturnInst::setCleanupPad(CleanupPadInst *CleanupPad) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CleanupReturnInst::getCleanupPad,
&CleanupReturnInst::setCleanupPad>>(
this);
cast<llvm::CleanupReturnInst>(Val)->setCleanupPad(
cast<llvm::CleanupPadInst>(CleanupPad->Val));
}
BasicBlock *CleanupReturnInst::getUnwindDest() const {
return cast_or_null<BasicBlock>(
Ctx.getValue(cast<llvm::CleanupReturnInst>(Val)->getUnwindDest()));
}
void CleanupReturnInst::setUnwindDest(BasicBlock *NewDest) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CleanupReturnInst::getUnwindDest,
&CleanupReturnInst::setUnwindDest>>(
this);
cast<llvm::CleanupReturnInst>(Val)->setUnwindDest(
cast<llvm::BasicBlock>(NewDest->Val));
}
Value *GetElementPtrInst::create(Type *Ty, Value *Ptr,
ArrayRef<Value *> IdxList, InsertPosition Pos,
Context &Ctx, const Twine &NameStr) {
auto &Builder = setInsertPos(Pos);
SmallVector<llvm::Value *> LLVMIdxList;
LLVMIdxList.reserve(IdxList.size());
for (Value *Idx : IdxList)
LLVMIdxList.push_back(Idx->Val);
llvm::Value *NewV =
Builder.CreateGEP(Ty->LLVMTy, Ptr->Val, LLVMIdxList, NameStr);
if (auto *NewGEP = dyn_cast<llvm::GetElementPtrInst>(NewV))
return Ctx.createGetElementPtrInst(NewGEP);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
Type *GetElementPtrInst::getSourceElementType() const {
return Ctx.getType(
cast<llvm::GetElementPtrInst>(Val)->getSourceElementType());
}
Type *GetElementPtrInst::getResultElementType() const {
return Ctx.getType(
cast<llvm::GetElementPtrInst>(Val)->getResultElementType());
}
Value *GetElementPtrInst::getPointerOperand() const {
return Ctx.getValue(cast<llvm::GetElementPtrInst>(Val)->getPointerOperand());
}
Type *GetElementPtrInst::getPointerOperandType() const {
return Ctx.getType(
cast<llvm::GetElementPtrInst>(Val)->getPointerOperandType());
}
BasicBlock *PHINode::LLVMBBToBB::operator()(llvm::BasicBlock *LLVMBB) const {
return cast<BasicBlock>(Ctx.getValue(LLVMBB));
}
PHINode *PHINode::create(Type *Ty, unsigned NumReservedValues,
InsertPosition Pos, Context &Ctx, const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::PHINode *NewPHI =
Builder.CreatePHI(Ty->LLVMTy, NumReservedValues, Name);
return Ctx.createPHINode(NewPHI);
}
bool PHINode::classof(const Value *From) {
return From->getSubclassID() == ClassID::PHI;
}
Value *PHINode::getIncomingValue(unsigned Idx) const {
return Ctx.getValue(cast<llvm::PHINode>(Val)->getIncomingValue(Idx));
}
void PHINode::setIncomingValue(unsigned Idx, Value *V) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetterWithIdx<&PHINode::getIncomingValue,
&PHINode::setIncomingValue>>(this,
Idx);
cast<llvm::PHINode>(Val)->setIncomingValue(Idx, V->Val);
}
BasicBlock *PHINode::getIncomingBlock(unsigned Idx) const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::PHINode>(Val)->getIncomingBlock(Idx)));
}
BasicBlock *PHINode::getIncomingBlock(const Use &U) const {
llvm::Use *LLVMUse = U.LLVMUse;
llvm::BasicBlock *BB = cast<llvm::PHINode>(Val)->getIncomingBlock(*LLVMUse);
return cast<BasicBlock>(Ctx.getValue(BB));
}
void PHINode::setIncomingBlock(unsigned Idx, BasicBlock *BB) {
// Helper to disambiguate PHINode::getIncomingBlock(unsigned).
constexpr BasicBlock *(PHINode::*GetIncomingBlockFn)(unsigned) const =
&PHINode::getIncomingBlock;
Ctx.getTracker()
.emplaceIfTracking<
GenericSetterWithIdx<GetIncomingBlockFn, &PHINode::setIncomingBlock>>(
this, Idx);
cast<llvm::PHINode>(Val)->setIncomingBlock(Idx,
cast<llvm::BasicBlock>(BB->Val));
}
void PHINode::addIncoming(Value *V, BasicBlock *BB) {
auto &Tracker = Ctx.getTracker();
Tracker.emplaceIfTracking<PHIAddIncoming>(this);
cast<llvm::PHINode>(Val)->addIncoming(V->Val,
cast<llvm::BasicBlock>(BB->Val));
}
Value *PHINode::removeIncomingValue(unsigned Idx) {
auto &Tracker = Ctx.getTracker();
Tracker.emplaceIfTracking<PHIRemoveIncoming>(this, Idx);
llvm::Value *LLVMV =
cast<llvm::PHINode>(Val)->removeIncomingValue(Idx,
/*DeletePHIIfEmpty=*/false);
return Ctx.getValue(LLVMV);
}
Value *PHINode::removeIncomingValue(BasicBlock *BB) {
auto &Tracker = Ctx.getTracker();
Tracker.emplaceIfTracking<PHIRemoveIncoming>(this, getBasicBlockIndex(BB));
auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
llvm::Value *LLVMV =
cast<llvm::PHINode>(Val)->removeIncomingValue(LLVMBB,
/*DeletePHIIfEmpty=*/false);
return Ctx.getValue(LLVMV);
}
int PHINode::getBasicBlockIndex(const BasicBlock *BB) const {
auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
return cast<llvm::PHINode>(Val)->getBasicBlockIndex(LLVMBB);
}
Value *PHINode::getIncomingValueForBlock(const BasicBlock *BB) const {
auto *LLVMBB = cast<llvm::BasicBlock>(BB->Val);
llvm::Value *LLVMV =
cast<llvm::PHINode>(Val)->getIncomingValueForBlock(LLVMBB);
return Ctx.getValue(LLVMV);
}
Value *PHINode::hasConstantValue() const {
llvm::Value *LLVMV = cast<llvm::PHINode>(Val)->hasConstantValue();
return LLVMV != nullptr ? Ctx.getValue(LLVMV) : nullptr;
}
void PHINode::replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) {
assert(New && Old && "Sandbox IR PHI node got a null basic block!");
for (unsigned Idx = 0, NumOps = cast<llvm::PHINode>(Val)->getNumOperands();
Idx != NumOps; ++Idx)
if (getIncomingBlock(Idx) == Old)
setIncomingBlock(Idx, New);
}
void PHINode::removeIncomingValueIf(function_ref<bool(unsigned)> Predicate) {
// Avoid duplicate tracking by going through this->removeIncomingValue here at
// the expense of some performance. Copy PHI::removeIncomingValueIf more
// directly if performance becomes an issue.
// Removing the element at index X, moves the element previously at X + 1
// to X. Working from the end avoids complications from that.
unsigned Idx = getNumIncomingValues();
while (Idx > 0) {
if (Predicate(Idx - 1))
removeIncomingValue(Idx - 1);
--Idx;
}
}
Value *CmpInst::create(Predicate P, Value *S1, Value *S2, InsertPosition Pos,
Context &Ctx, const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *LLVMV = Builder.CreateCmp(P, S1->Val, S2->Val, Name);
// It may have been folded into a constant.
if (auto *LLVMC = dyn_cast<llvm::Constant>(LLVMV))
return Ctx.getOrCreateConstant(LLVMC);
if (isa<llvm::ICmpInst>(LLVMV))
return Ctx.createICmpInst(cast<llvm::ICmpInst>(LLVMV));
return Ctx.createFCmpInst(cast<llvm::FCmpInst>(LLVMV));
}
Value *CmpInst::createWithCopiedFlags(Predicate P, Value *S1, Value *S2,
const Instruction *F, InsertPosition Pos,
Context &Ctx, const Twine &Name) {
Value *V = create(P, S1, S2, Pos, Ctx, Name);
if (auto *C = dyn_cast<Constant>(V))
return C;
cast<llvm::CmpInst>(V->Val)->copyIRFlags(F->Val);
return V;
}
Type *CmpInst::makeCmpResultType(Type *OpndType) {
if (auto *VT = dyn_cast<VectorType>(OpndType)) {
// TODO: Cleanup when we have more complete support for
// sandboxir::VectorType
return OpndType->getContext().getType(llvm::VectorType::get(
llvm::Type::getInt1Ty(OpndType->getContext().LLVMCtx),
cast<llvm::VectorType>(VT->LLVMTy)->getElementCount()));
}
return Type::getInt1Ty(OpndType->getContext());
}
void CmpInst::setPredicate(Predicate P) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&CmpInst::getPredicate, &CmpInst::setPredicate>>(this);
cast<llvm::CmpInst>(Val)->setPredicate(P);
}
void CmpInst::swapOperands() {
if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
IC->swapOperands();
else
cast<FCmpInst>(this)->swapOperands();
}
void ICmpInst::swapOperands() {
Ctx.getTracker().emplaceIfTracking<CmpSwapOperands>(this);
cast<llvm::ICmpInst>(Val)->swapOperands();
}
void FCmpInst::swapOperands() {
Ctx.getTracker().emplaceIfTracking<CmpSwapOperands>(this);
cast<llvm::FCmpInst>(Val)->swapOperands();
}
#ifndef NDEBUG
void CmpInst::dumpOS(raw_ostream &OS) const {
dumpCommonPrefix(OS);
dumpCommonSuffix(OS);
}
void CmpInst::dump() const {
dumpOS(dbgs());
dbgs() << "\n";
}
#endif // NDEBUG
static llvm::Instruction::CastOps getLLVMCastOp(Instruction::Opcode Opc) {
switch (Opc) {
case Instruction::Opcode::ZExt:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::ZExt);
case Instruction::Opcode::SExt:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::SExt);
case Instruction::Opcode::FPToUI:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPToUI);
case Instruction::Opcode::FPToSI:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPToSI);
case Instruction::Opcode::FPExt:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPExt);
case Instruction::Opcode::PtrToInt:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::PtrToInt);
case Instruction::Opcode::IntToPtr:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::IntToPtr);
case Instruction::Opcode::SIToFP:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::SIToFP);
case Instruction::Opcode::UIToFP:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::UIToFP);
case Instruction::Opcode::Trunc:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::Trunc);
case Instruction::Opcode::FPTrunc:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::FPTrunc);
case Instruction::Opcode::BitCast:
return static_cast<llvm::Instruction::CastOps>(llvm::Instruction::BitCast);
case Instruction::Opcode::AddrSpaceCast:
return static_cast<llvm::Instruction::CastOps>(
llvm::Instruction::AddrSpaceCast);
default:
llvm_unreachable("Opcode not suitable for CastInst!");
}
}
/// \Returns the LLVM opcode that corresponds to \p Opc.
static llvm::Instruction::UnaryOps getLLVMUnaryOp(Instruction::Opcode Opc) {
switch (Opc) {
case Instruction::Opcode::FNeg:
return static_cast<llvm::Instruction::UnaryOps>(llvm::Instruction::FNeg);
default:
llvm_unreachable("Not a unary op!");
}
}
CatchSwitchInst *CatchSwitchInst::create(Value *ParentPad, BasicBlock *UnwindBB,
unsigned NumHandlers,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::CatchSwitchInst *LLVMCSI = Builder.CreateCatchSwitch(
ParentPad->Val, cast<llvm::BasicBlock>(UnwindBB->Val), NumHandlers, Name);
return Ctx.createCatchSwitchInst(LLVMCSI);
}
Value *CatchSwitchInst::getParentPad() const {
return Ctx.getValue(cast<llvm::CatchSwitchInst>(Val)->getParentPad());
}
void CatchSwitchInst::setParentPad(Value *ParentPad) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CatchSwitchInst::getParentPad,
&CatchSwitchInst::setParentPad>>(this);
cast<llvm::CatchSwitchInst>(Val)->setParentPad(ParentPad->Val);
}
BasicBlock *CatchSwitchInst::getUnwindDest() const {
return cast_or_null<BasicBlock>(
Ctx.getValue(cast<llvm::CatchSwitchInst>(Val)->getUnwindDest()));
}
void CatchSwitchInst::setUnwindDest(BasicBlock *UnwindDest) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CatchSwitchInst::getUnwindDest,
&CatchSwitchInst::setUnwindDest>>(this);
cast<llvm::CatchSwitchInst>(Val)->setUnwindDest(
cast<llvm::BasicBlock>(UnwindDest->Val));
}
void CatchSwitchInst::addHandler(BasicBlock *Dest) {
Ctx.getTracker().emplaceIfTracking<CatchSwitchAddHandler>(this);
cast<llvm::CatchSwitchInst>(Val)->addHandler(
cast<llvm::BasicBlock>(Dest->Val));
}
ResumeInst *ResumeInst::create(Value *Exn, InsertPosition Pos, Context &Ctx) {
auto &Builder = setInsertPos(Pos);
auto *LLVMI = cast<llvm::ResumeInst>(Builder.CreateResume(Exn->Val));
return Ctx.createResumeInst(LLVMI);
}
Value *ResumeInst::getValue() const {
return Ctx.getValue(cast<llvm::ResumeInst>(Val)->getValue());
}
SwitchInst *SwitchInst::create(Value *V, BasicBlock *Dest, unsigned NumCases,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::SwitchInst *LLVMSwitch =
Builder.CreateSwitch(V->Val, cast<llvm::BasicBlock>(Dest->Val), NumCases);
return Ctx.createSwitchInst(LLVMSwitch);
}
Value *SwitchInst::getCondition() const {
return Ctx.getValue(cast<llvm::SwitchInst>(Val)->getCondition());
}
void SwitchInst::setCondition(Value *V) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&SwitchInst::getCondition, &SwitchInst::setCondition>>(
this);
cast<llvm::SwitchInst>(Val)->setCondition(V->Val);
}
BasicBlock *SwitchInst::getDefaultDest() const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::SwitchInst>(Val)->getDefaultDest()));
}
void SwitchInst::setDefaultDest(BasicBlock *DefaultCase) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&SwitchInst::getDefaultDest,
&SwitchInst::setDefaultDest>>(this);
cast<llvm::SwitchInst>(Val)->setDefaultDest(
cast<llvm::BasicBlock>(DefaultCase->Val));
}
ConstantInt *SwitchInst::findCaseDest(BasicBlock *BB) {
auto *LLVMC = cast<llvm::SwitchInst>(Val)->findCaseDest(
cast<llvm::BasicBlock>(BB->Val));
return LLVMC != nullptr ? cast<ConstantInt>(Ctx.getValue(LLVMC)) : nullptr;
}
void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Ctx.getTracker().emplaceIfTracking<SwitchAddCase>(this, OnVal);
// TODO: Track this!
cast<llvm::SwitchInst>(Val)->addCase(cast<llvm::ConstantInt>(OnVal->Val),
cast<llvm::BasicBlock>(Dest->Val));
}
SwitchInst::CaseIt SwitchInst::removeCase(CaseIt It) {
Ctx.getTracker().emplaceIfTracking<SwitchRemoveCase>(this);
auto *LLVMSwitch = cast<llvm::SwitchInst>(Val);
unsigned CaseNum = It - case_begin();
llvm::SwitchInst::CaseIt LLVMIt(LLVMSwitch, CaseNum);
auto LLVMCaseIt = LLVMSwitch->removeCase(LLVMIt);
unsigned Num = LLVMCaseIt - LLVMSwitch->case_begin();
return CaseIt(this, Num);
}
BasicBlock *SwitchInst::getSuccessor(unsigned Idx) const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::SwitchInst>(Val)->getSuccessor(Idx)));
}
void SwitchInst::setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetterWithIdx<&SwitchInst::getSuccessor,
&SwitchInst::setSuccessor>>(this,
Idx);
cast<llvm::SwitchInst>(Val)->setSuccessor(
Idx, cast<llvm::BasicBlock>(NewSucc->Val));
}
Value *UnaryOperator::create(Instruction::Opcode Op, Value *OpV,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *NewLLVMV = Builder.CreateUnOp(getLLVMUnaryOp(Op), OpV->Val, Name);
if (auto *NewUnOpV = dyn_cast<llvm::UnaryOperator>(NewLLVMV)) {
return Ctx.createUnaryOperator(NewUnOpV);
}
assert(isa<llvm::Constant>(NewLLVMV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewLLVMV));
}
Value *UnaryOperator::createWithCopiedFlags(Instruction::Opcode Op, Value *OpV,
Value *CopyFrom, InsertPosition Pos,
Context &Ctx, const Twine &Name) {
auto *NewV = create(Op, OpV, Pos, Ctx, Name);
if (auto *UnI = dyn_cast<llvm::UnaryOperator>(NewV->Val))
UnI->copyIRFlags(CopyFrom->Val);
return NewV;
}
/// \Returns the LLVM opcode that corresponds to \p Opc.
static llvm::Instruction::BinaryOps getLLVMBinaryOp(Instruction::Opcode Opc) {
switch (Opc) {
case Instruction::Opcode::Add:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Add);
case Instruction::Opcode::FAdd:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FAdd);
case Instruction::Opcode::Sub:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Sub);
case Instruction::Opcode::FSub:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FSub);
case Instruction::Opcode::Mul:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Mul);
case Instruction::Opcode::FMul:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FMul);
case Instruction::Opcode::UDiv:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::UDiv);
case Instruction::Opcode::SDiv:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::SDiv);
case Instruction::Opcode::FDiv:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FDiv);
case Instruction::Opcode::URem:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::URem);
case Instruction::Opcode::SRem:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::SRem);
case Instruction::Opcode::FRem:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::FRem);
case Instruction::Opcode::Shl:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Shl);
case Instruction::Opcode::LShr:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::LShr);
case Instruction::Opcode::AShr:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::AShr);
case Instruction::Opcode::And:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::And);
case Instruction::Opcode::Or:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Or);
case Instruction::Opcode::Xor:
return static_cast<llvm::Instruction::BinaryOps>(llvm::Instruction::Xor);
default:
llvm_unreachable("Not a binary op!");
}
}
Value *BinaryOperator::create(Instruction::Opcode Op, Value *LHS, Value *RHS,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::Value *NewV =
Builder.CreateBinOp(getLLVMBinaryOp(Op), LHS->Val, RHS->Val, Name);
if (auto *NewBinOp = dyn_cast<llvm::BinaryOperator>(NewV))
return Ctx.createBinaryOperator(NewBinOp);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
Value *BinaryOperator::createWithCopiedFlags(Instruction::Opcode Op, Value *LHS,
Value *RHS, Value *CopyFrom,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
Value *NewV = create(Op, LHS, RHS, Pos, Ctx, Name);
if (auto *NewBO = dyn_cast<BinaryOperator>(NewV))
cast<llvm::BinaryOperator>(NewBO->Val)->copyIRFlags(CopyFrom->Val);
return NewV;
}
void PossiblyDisjointInst::setIsDisjoint(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&PossiblyDisjointInst::isDisjoint,
&PossiblyDisjointInst::setIsDisjoint>>(
this);
cast<llvm::PossiblyDisjointInst>(Val)->setIsDisjoint(B);
}
void AtomicRMWInst::setAlignment(Align Align) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicRMWInst::getAlign,
&AtomicRMWInst::setAlignment>>(this);
cast<llvm::AtomicRMWInst>(Val)->setAlignment(Align);
}
void AtomicRMWInst::setVolatile(bool V) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicRMWInst::isVolatile,
&AtomicRMWInst::setVolatile>>(this);
cast<llvm::AtomicRMWInst>(Val)->setVolatile(V);
}
void AtomicRMWInst::setOrdering(AtomicOrdering Ordering) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicRMWInst::getOrdering,
&AtomicRMWInst::setOrdering>>(this);
cast<llvm::AtomicRMWInst>(Val)->setOrdering(Ordering);
}
void AtomicRMWInst::setSyncScopeID(SyncScope::ID SSID) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicRMWInst::getSyncScopeID,
&AtomicRMWInst::setSyncScopeID>>(this);
cast<llvm::AtomicRMWInst>(Val)->setSyncScopeID(SSID);
}
Value *AtomicRMWInst::getPointerOperand() {
return Ctx.getValue(cast<llvm::AtomicRMWInst>(Val)->getPointerOperand());
}
Value *AtomicRMWInst::getValOperand() {
return Ctx.getValue(cast<llvm::AtomicRMWInst>(Val)->getValOperand());
}
AtomicRMWInst *AtomicRMWInst::create(BinOp Op, Value *Ptr, Value *Val,
MaybeAlign Align, AtomicOrdering Ordering,
InsertPosition Pos, Context &Ctx,
SyncScope::ID SSID, const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *LLVMAtomicRMW =
Builder.CreateAtomicRMW(Op, Ptr->Val, Val->Val, Align, Ordering, SSID);
LLVMAtomicRMW->setName(Name);
return Ctx.createAtomicRMWInst(LLVMAtomicRMW);
}
void AtomicCmpXchgInst::setSyncScopeID(SyncScope::ID SSID) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicCmpXchgInst::getSyncScopeID,
&AtomicCmpXchgInst::setSyncScopeID>>(
this);
cast<llvm::AtomicCmpXchgInst>(Val)->setSyncScopeID(SSID);
}
Value *AtomicCmpXchgInst::getPointerOperand() {
return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getPointerOperand());
}
Value *AtomicCmpXchgInst::getCompareOperand() {
return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getCompareOperand());
}
Value *AtomicCmpXchgInst::getNewValOperand() {
return Ctx.getValue(cast<llvm::AtomicCmpXchgInst>(Val)->getNewValOperand());
}
AtomicCmpXchgInst *
AtomicCmpXchgInst::create(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
AtomicOrdering SuccessOrdering,
AtomicOrdering FailureOrdering, InsertPosition Pos,
Context &Ctx, SyncScope::ID SSID, const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *LLVMAtomicCmpXchg =
Builder.CreateAtomicCmpXchg(Ptr->Val, Cmp->Val, New->Val, Align,
SuccessOrdering, FailureOrdering, SSID);
LLVMAtomicCmpXchg->setName(Name);
return Ctx.createAtomicCmpXchgInst(LLVMAtomicCmpXchg);
}
void AtomicCmpXchgInst::setAlignment(Align Align) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicCmpXchgInst::getAlign,
&AtomicCmpXchgInst::setAlignment>>(this);
cast<llvm::AtomicCmpXchgInst>(Val)->setAlignment(Align);
}
void AtomicCmpXchgInst::setVolatile(bool V) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicCmpXchgInst::isVolatile,
&AtomicCmpXchgInst::setVolatile>>(this);
cast<llvm::AtomicCmpXchgInst>(Val)->setVolatile(V);
}
void AtomicCmpXchgInst::setWeak(bool IsWeak) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicCmpXchgInst::isWeak,
&AtomicCmpXchgInst::setWeak>>(this);
cast<llvm::AtomicCmpXchgInst>(Val)->setWeak(IsWeak);
}
void AtomicCmpXchgInst::setSuccessOrdering(AtomicOrdering Ordering) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicCmpXchgInst::getSuccessOrdering,
&AtomicCmpXchgInst::setSuccessOrdering>>(
this);
cast<llvm::AtomicCmpXchgInst>(Val)->setSuccessOrdering(Ordering);
}
void AtomicCmpXchgInst::setFailureOrdering(AtomicOrdering Ordering) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AtomicCmpXchgInst::getFailureOrdering,
&AtomicCmpXchgInst::setFailureOrdering>>(
this);
cast<llvm::AtomicCmpXchgInst>(Val)->setFailureOrdering(Ordering);
}
AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace, InsertPosition Pos,
Context &Ctx, Value *ArraySize,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
auto *NewAlloca =
Builder.CreateAlloca(Ty->LLVMTy, AddrSpace, ArraySize->Val, Name);
return Ctx.createAllocaInst(NewAlloca);
}
Type *AllocaInst::getAllocatedType() const {
return Ctx.getType(cast<llvm::AllocaInst>(Val)->getAllocatedType());
}
void AllocaInst::setAllocatedType(Type *Ty) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AllocaInst::getAllocatedType,
&AllocaInst::setAllocatedType>>(this);
cast<llvm::AllocaInst>(Val)->setAllocatedType(Ty->LLVMTy);
}
void AllocaInst::setAlignment(Align Align) {
Ctx.getTracker()
.emplaceIfTracking<
GenericSetter<&AllocaInst::getAlign, &AllocaInst::setAlignment>>(
this);
cast<llvm::AllocaInst>(Val)->setAlignment(Align);
}
void AllocaInst::setUsedWithInAlloca(bool V) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&AllocaInst::isUsedWithInAlloca,
&AllocaInst::setUsedWithInAlloca>>(this);
cast<llvm::AllocaInst>(Val)->setUsedWithInAlloca(V);
}
Value *AllocaInst::getArraySize() {
return Ctx.getValue(cast<llvm::AllocaInst>(Val)->getArraySize());
}
PointerType *AllocaInst::getType() const {
return cast<PointerType>(Ctx.getType(cast<llvm::AllocaInst>(Val)->getType()));
}
Value *CastInst::create(Type *DestTy, Opcode Op, Value *Operand,
InsertPosition Pos, Context &Ctx, const Twine &Name) {
assert(getLLVMCastOp(Op) && "Opcode not suitable for CastInst!");
auto &Builder = setInsertPos(Pos);
auto *NewV =
Builder.CreateCast(getLLVMCastOp(Op), Operand->Val, DestTy->LLVMTy, Name);
if (auto *NewCI = dyn_cast<llvm::CastInst>(NewV))
return Ctx.createCastInst(NewCI);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
bool CastInst::classof(const Value *From) {
return From->getSubclassID() == ClassID::Cast;
}
Type *CastInst::getSrcTy() const {
return Ctx.getType(cast<llvm::CastInst>(Val)->getSrcTy());
}
Type *CastInst::getDestTy() const {
return Ctx.getType(cast<llvm::CastInst>(Val)->getDestTy());
}
void PossiblyNonNegInst::setNonNeg(bool B) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&PossiblyNonNegInst::hasNonNeg,
&PossiblyNonNegInst::setNonNeg>>(this);
cast<llvm::PossiblyNonNegInst>(Val)->setNonNeg(B);
}
Value *InsertElementInst::create(Value *Vec, Value *NewElt, Value *Idx,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = Instruction::setInsertPos(Pos);
llvm::Value *NewV =
Builder.CreateInsertElement(Vec->Val, NewElt->Val, Idx->Val, Name);
if (auto *NewInsert = dyn_cast<llvm::InsertElementInst>(NewV))
return Ctx.createInsertElementInst(NewInsert);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
Value *ExtractElementInst::create(Value *Vec, Value *Idx, InsertPosition Pos,
Context &Ctx, const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::Value *NewV = Builder.CreateExtractElement(Vec->Val, Idx->Val, Name);
if (auto *NewExtract = dyn_cast<llvm::ExtractElementInst>(NewV))
return Ctx.createExtractElementInst(NewExtract);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
Value *ShuffleVectorInst::create(Value *V1, Value *V2, Value *Mask,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::Value *NewV =
Builder.CreateShuffleVector(V1->Val, V2->Val, Mask->Val, Name);
if (auto *NewShuffle = dyn_cast<llvm::ShuffleVectorInst>(NewV))
return Ctx.createShuffleVectorInst(NewShuffle);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
Value *ShuffleVectorInst::create(Value *V1, Value *V2, ArrayRef<int> Mask,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::Value *NewV = Builder.CreateShuffleVector(V1->Val, V2->Val, Mask, Name);
if (auto *NewShuffle = dyn_cast<llvm::ShuffleVectorInst>(NewV))
return Ctx.createShuffleVectorInst(NewShuffle);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
void ShuffleVectorInst::setShuffleMask(ArrayRef<int> Mask) {
Ctx.getTracker().emplaceIfTracking<ShuffleVectorSetMask>(this);
cast<llvm::ShuffleVectorInst>(Val)->setShuffleMask(Mask);
}
VectorType *ShuffleVectorInst::getType() const {
return cast<VectorType>(
Ctx.getType(cast<llvm::ShuffleVectorInst>(Val)->getType()));
}
void ShuffleVectorInst::commute() {
Ctx.getTracker().emplaceIfTracking<ShuffleVectorSetMask>(this);
Ctx.getTracker().emplaceIfTracking<UseSwap>(getOperandUse(0),
getOperandUse(1));
cast<llvm::ShuffleVectorInst>(Val)->commute();
}
Constant *ShuffleVectorInst::getShuffleMaskForBitcode() const {
return Ctx.getOrCreateConstant(
cast<llvm::ShuffleVectorInst>(Val)->getShuffleMaskForBitcode());
}
Constant *ShuffleVectorInst::convertShuffleMaskForBitcode(ArrayRef<int> Mask,
Type *ResultTy) {
return ResultTy->getContext().getOrCreateConstant(
llvm::ShuffleVectorInst::convertShuffleMaskForBitcode(Mask,
ResultTy->LLVMTy));
}
VectorType *ExtractElementInst::getVectorOperandType() const {
return cast<VectorType>(Ctx.getType(getVectorOperand()->getType()->LLVMTy));
}
Value *ExtractValueInst::create(Value *Agg, ArrayRef<unsigned> Idxs,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::Value *NewV = Builder.CreateExtractValue(Agg->Val, Idxs, Name);
if (auto *NewExtractValueInst = dyn_cast<llvm::ExtractValueInst>(NewV))
return Ctx.createExtractValueInst(NewExtractValueInst);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
Type *ExtractValueInst::getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs) {
auto *LLVMTy = llvm::ExtractValueInst::getIndexedType(Agg->LLVMTy, Idxs);
return Agg->getContext().getType(LLVMTy);
}
Value *InsertValueInst::create(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
InsertPosition Pos, Context &Ctx,
const Twine &Name) {
auto &Builder = setInsertPos(Pos);
llvm::Value *NewV = Builder.CreateInsertValue(Agg->Val, Val->Val, Idxs, Name);
if (auto *NewInsertValueInst = dyn_cast<llvm::InsertValueInst>(NewV))
return Ctx.createInsertValueInst(NewInsertValueInst);
assert(isa<llvm::Constant>(NewV) && "Expected constant");
return Ctx.getOrCreateConstant(cast<llvm::Constant>(NewV));
}
ConstantTokenNone *ConstantTokenNone::get(Context &Ctx) {
auto *LLVMC = llvm::ConstantTokenNone::get(Ctx.LLVMCtx);
return cast<ConstantTokenNone>(Ctx.getOrCreateConstant(LLVMC));
}
} // namespace llvm::sandboxir
|