LLVM: lib/Transforms/Utils/Local.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

59#include "llvm/IR/IntrinsicsWebAssembly.h"

80#include

81#include

82#include

83#include

84#include

85#include

86#include

87

88using namespace llvm;

90

92

93#define DEBUG_TYPE "local"

94

95STATISTIC(NumRemoved, "Number of unreachable basic blocks removed");

96STATISTIC(NumPHICSEs, "Number of PHI's that got CSE'd");

97

99 "phicse-debug-hash",

100#ifdef EXPENSIVE_CHECKS

102#else

104#endif

106 cl::desc("Perform extra assertion checking to verify that PHINodes's hash "

107 "function is well-behaved w.r.t. its isEqual predicate"));

108

112 "When the basic block contains not more than this number of PHI nodes, "

113 "perform a (faster!) exhaustive search instead of set-driven one."));

114

116 "max-phi-entries-increase-after-removing-empty-block", cl::init(1000),

118 cl::desc("Stop removing an empty block if removing it will introduce more "

119 "than this number of phi entries in its successor"));

120

121

122

124

125

126

127

128

129

130

131

132

133

134

135

141

142

143 if (auto *BI = dyn_cast(T)) {

144 if (BI->isUnconditional()) return false;

145

146 BasicBlock *Dest1 = BI->getSuccessor(0);

147 BasicBlock *Dest2 = BI->getSuccessor(1);

148

149 if (Dest2 == Dest1) {

150

151

152

153

154

155 assert(BI->getParent() && "Terminator not inserted in block!");

157

158

160

161

162 NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg,

163 LLVMContext::MD_annotation});

164

165 Value *Cond = BI->getCondition();

166 BI->eraseFromParent();

167 if (DeleteDeadConditions)

169 return true;

170 }

171

172 if (auto *Cond = dyn_cast(BI->getCondition())) {

173

174

175 BasicBlock *Destination = Cond->getZExtValue() ? Dest1 : Dest2;

176 BasicBlock *OldDest = Cond->getZExtValue() ? Dest2 : Dest1;

177

178

179

181

182

184

185

186 NewBI->copyMetadata(*BI, {LLVMContext::MD_loop, LLVMContext::MD_dbg,

187 LLVMContext::MD_annotation});

188

189 BI->eraseFromParent();

190 if (DTU)

191 DTU->applyUpdates({{DominatorTree::Delete, BB, OldDest}});

192 return true;

193 }

194

195 return false;

196 }

197

198 if (auto *SI = dyn_cast(T)) {

199

200

201 auto *CI = dyn_cast(SI->getCondition());

202 BasicBlock *DefaultDest = SI->getDefaultDest();

203 BasicBlock *TheOnlyDest = DefaultDest;

204

205

207 SI->getNumCases() > 0) {

208 TheOnlyDest = SI->case_begin()->getCaseSuccessor();

209 }

210

211 bool Changed = false;

212

213

214 for (auto It = SI->case_begin(), End = SI->case_end(); It != End;) {

215

216 if (It->getCaseValue() == CI) {

217 TheOnlyDest = It->getCaseSuccessor();

218 break;

219 }

220

221

222

223 if (It->getCaseSuccessor() == DefaultDest) {

225 unsigned NCases = SI->getNumCases();

226

227

228 if (NCases > 1 && MD) {

229

232

233

234 unsigned Idx = It->getCaseIndex();

235

236 Weights[0] += Weights[Idx + 1];

237

241 }

242

243 BasicBlock *ParentBB = SI->getParent();

245 It = SI->removeCase(It);

246 End = SI->case_end();

247

248

249

250 if (auto *NewCI = dyn_cast(SI->getCondition())) {

251 CI = NewCI;

252 It = SI->case_begin();

253 }

254

255 Changed = true;

256 continue;

257 }

258

259

260

261

262 if (It->getCaseSuccessor() != TheOnlyDest)

263 TheOnlyDest = nullptr;

264

265

266 ++It;

267 }

268

269 if (CI && !TheOnlyDest) {

270

271

272 TheOnlyDest = SI->getDefaultDest();

273 }

274

275

276

277 if (TheOnlyDest) {

278

279 Builder.CreateBr(TheOnlyDest);

281

283

284

285 BasicBlock *SuccToKeep = TheOnlyDest;

287 if (DTU && Succ != TheOnlyDest)

288 RemovedSuccessors.insert(Succ);

289

290 if (Succ == SuccToKeep) {

291 SuccToKeep = nullptr;

292 } else {

294 }

295 }

296

297

298 Value *Cond = SI->getCondition();

299 SI->eraseFromParent();

300 if (DeleteDeadConditions)

302 if (DTU) {

303 std::vectorDominatorTree::UpdateType Updates;

304 Updates.reserve(RemovedSuccessors.size());

305 for (auto *RemovedSuccessor : RemovedSuccessors)

306 Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});

308 }

309 return true;

310 }

311

312 if (SI->getNumCases() == 1) {

313

314

315 auto FirstCase = *SI->case_begin();

317 FirstCase.getCaseValue(), "cond");

318

319

321 FirstCase.getCaseSuccessor(),

322 SI->getDefaultDest());

325 uint32_t DefWeight = Weights[0];

326 uint32_t CaseWeight = Weights[1];

327

331 }

332

333

334 MDNode *MakeImplicitMD = SI->getMetadata(LLVMContext::MD_make_implicit);

335 if (MakeImplicitMD)

336 NewBr->setMetadata(LLVMContext::MD_make_implicit, MakeImplicitMD);

337

338

339 SI->eraseFromParent();

340 return true;

341 }

342 return Changed;

343 }

344

345 if (auto *IBI = dyn_cast(T)) {

346

347 if (auto *BA =

348 dyn_cast(IBI->getAddress()->stripPointerCasts())) {

349 BasicBlock *TheOnlyDest = BA->getBasicBlock();

351

352

353 Builder.CreateBr(TheOnlyDest);

354

355 BasicBlock *SuccToKeep = TheOnlyDest;

356 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {

357 BasicBlock *DestBB = IBI->getDestination(i);

358 if (DTU && DestBB != TheOnlyDest)

359 RemovedSuccessors.insert(DestBB);

360 if (IBI->getDestination(i) == SuccToKeep) {

361 SuccToKeep = nullptr;

362 } else {

364 }

365 }

367 IBI->eraseFromParent();

368 if (DeleteDeadConditions)

369

371

372

373

374 if (BA->use_empty())

375 BA->destroyConstant();

376

377

378

379

380 if (SuccToKeep) {

383 }

384

385 if (DTU) {

386 std::vectorDominatorTree::UpdateType Updates;

387 Updates.reserve(RemovedSuccessors.size());

388 for (auto *RemovedSuccessor : RemovedSuccessors)

389 Updates.push_back({DominatorTree::Delete, BB, RemovedSuccessor});

391 }

392 return true;

393 }

394 }

395

396 return false;

397}

398

399

400

401

402

403

404

405

408 if (I->use_empty())

409 return false;

411}

412

415

416

418 if (II->getIntrinsicID() == Intrinsic::stacksave ||

419 II->getIntrinsicID() == Intrinsic::launder_invariant_group ||

420 II->isLifetimeStartOrEnd())

421 return false;

423}

424

427 if (I->isTerminator())

428 return false;

429

430

431

432 if (I->isEHPad())

433 return false;

434

435

436 if (isa(I))

437 return false;

438

439 if (const DbgLabelInst *DLI = dyn_cast(I)) {

440 if (DLI->getLabel())

441 return false;

442 return true;

443 }

444

445 if (auto *CB = dyn_cast(I))

447 return true;

448

449 if (I->willReturn()) {

450 auto *II = dyn_cast(I);

451 if (II)

452 return false;

453

454 switch (II->getIntrinsicID()) {

455 case Intrinsic::experimental_guard: {

456

457

458

459 auto *Cond = dyn_cast(II->getArgOperand(0));

460 return Cond && Cond->isOne();

461 }

462

463

464 case Intrinsic::wasm_trunc_signed:

465 case Intrinsic::wasm_trunc_unsigned:

466 case Intrinsic::ptrauth_auth:

467 case Intrinsic::ptrauth_resign:

468 return true;

469 default:

470 return false;

471 }

472 }

473

474 if (I->mayHaveSideEffects())

475 return true;

476

477

478

480

481 if (II->getIntrinsicID() == Intrinsic::stacksave ||

482 II->getIntrinsicID() == Intrinsic::launder_invariant_group)

483 return true;

484

485

486

487 if (II->getIntrinsicID() == Intrinsic::allow_runtime_check ||

488 II->getIntrinsicID() == Intrinsic::allow_ubsan_check)

489 return true;

490

491 if (II->isLifetimeStartOrEnd()) {

492 auto *Arg = II->getArgOperand(1);

493

494 if (isa(Arg))

495 return true;

496

497

498 if (isa(Arg) || isa(Arg) || isa(Arg))

500 if (IntrinsicInst *IntrinsicUse =

501 dyn_cast(Use.getUser()))

502 return IntrinsicUse->isLifetimeStartOrEnd();

503 return false;

504 });

505 return false;

506 }

507

508

509 if (II->getIntrinsicID() == Intrinsic::assume &&

511 if (ConstantInt *Cond = dyn_cast(II->getArgOperand(0)))

512 return Cond->isZero();

513

514 return false;

515 }

516

517 if (auto *FPI = dyn_cast(I)) {

518 std::optionalfp::ExceptionBehavior ExBehavior =

519 FPI->getExceptionBehavior();

521 }

522 }

523

524 if (auto *Call = dyn_cast(I)) {

526 if (Constant *C = dyn_cast(FreedOp))

527 return C->isNullValue() || isa(C);

529 return true;

530 }

531

532

533 if (auto *LI = dyn_cast(I))

534 if (auto *GV = dyn_cast(

535 LI->getPointerOperand()->stripPointerCasts()))

536 if (!LI->isVolatile() && GV->isConstant())

537 return true;

538

539 return false;

540}

541

542

543

544

545

548 std::function<void(Value *)> AboutToDeleteCallback) {

551 return false;

552

556 AboutToDeleteCallback);

557

558 return true;

559}

560

564 std::function<void(Value *)> AboutToDeleteCallback) {

565 unsigned S = 0, E = DeadInsts.size(), Alive = 0;

566 for (; S != E; ++S) {

567 auto *I = dyn_cast_or_null(DeadInsts[S]);

569 DeadInsts[S] = nullptr;

570 ++Alive;

571 }

572 }

573 if (Alive == E)

574 return false;

576 AboutToDeleteCallback);

577 return true;

578}

579

583 std::function<void(Value *)> AboutToDeleteCallback) {

584

585 while (!DeadInsts.empty()) {

587 Instruction *I = cast_or_null(V);

588 if (I)

589 continue;

591 "Live instruction found in dead worklist!");

592 assert(I->use_empty() && "Instructions with uses are not dead.");

593

594

596

597 if (AboutToDeleteCallback)

598 AboutToDeleteCallback(I);

599

600

601

602 for (Use &OpU : I->operands()) {

603 Value *OpV = OpU.get();

604 OpU.set(nullptr);

605

607 continue;

608

609

610

611

612 if (Instruction *OpI = dyn_cast(OpV))

615 }

616 if (MSSAU)

618

619 I->eraseFromParent();

620 }

621}

622

627 for (auto *DII : DbgUsers)

628 DII->setKillLocation();

629 for (auto *DVR : DPUsers)

630 DVR->setKillLocation();

631 return !DbgUsers.empty() || !DPUsers.empty();

632}

633

634

635

636

637

641 if (UI == UE)

642 return true;

643

644 User *TheUse = *UI;

645 for (++UI; UI != UE; ++UI) {

646 if (*UI != TheUse)

647 return false;

648 }

649 return true;

650}

651

652

653

654

655

656

662 I = cast(*I->user_begin())) {

663 if (I->use_empty())

665

666

667

668 if (!Visited.insert(I).second) {

669

672 return true;

673 }

674 }

675 return false;

676}

677

678static bool

685

686

687

688 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {

689 Value *OpV = I->getOperand(i);

690 I->setOperand(i, nullptr);

691

693 continue;

694

695

696

697

698 if (Instruction *OpI = dyn_cast(OpV))

700 WorkList.insert(OpI);

701 }

702

703 I->eraseFromParent();

704

705 return true;

706 }

707

709

710

711 for (User *U : I->users()) {

712 if (U != I) {

713 WorkList.insert(cast(U));

714 }

715 }

716

717

718 bool Changed = false;

719 if (I->use_empty()) {

720 I->replaceAllUsesWith(SimpleV);

721 Changed = true;

722 }

724 I->eraseFromParent();

725 Changed = true;

726 }

727 return Changed;

728 }

729 return false;

730}

731

732

733

734

735

736

739 bool MadeChange = false;

741

742#ifndef NDEBUG

743

744

745

746

748#endif

749

751

752

753

755 BI != E;) {

756 assert(!BI->isTerminator());

758 ++BI;

759

760

761

762 if (!WorkList.count(I))

764 }

765

766 while (!WorkList.empty()) {

769 }

770 return MadeChange;

771}

772

773

774

775

776

779

780

781 while (PHINode *PN = dyn_cast(DestBB->begin())) {

782 Value *NewVal = PN->getIncomingValue(0);

783

785 PN->replaceAllUsesWith(NewVal);

786 PN->eraseFromParent();

787 }

788

790 assert(PredBB && "Block doesn't have a single predecessor!");

791

792 bool ReplaceEntryBB = PredBB->isEntryBlock();

793

794

795

797

798 if (DTU) {

799

803

804 if (PredOfPredBB != PredBB)

805 if (SeenPreds.insert(PredOfPredBB).second)

806 Updates.push_back({DominatorTree::Insert, PredOfPredBB, DestBB});

807 SeenPreds.clear();

809 if (SeenPreds.insert(PredOfPredBB).second)

810 Updates.push_back({DominatorTree::Delete, PredOfPredBB, PredBB});

811 Updates.push_back({DominatorTree::Delete, PredBB, DestBB});

812 }

813

814

815

823 }

824

825

827

828

832

833

834

835 if (ReplaceEntryBB)

837

838 if (DTU) {

841 "The successor list of PredBB isn't empty before "

842 "applying corresponding DTU updates.");

845

846

847 if (ReplaceEntryBB && DTU->hasDomTree()) {

848

849

850

852 }

853 }

854

855 else {

857 }

858}

859

860

861

863 return First == Second || isa(First) || isa(Second);

864}

865

866

867

868

869

870static bool

873 assert(*succ_begin(BB) == Succ && "Succ is not successor of BB!");

874

876 << Succ->getName() << "\n");

877

878

880 return true;

881

882

883

885 PHINode *PN = cast(I);

886

887

888

889

891 if (BBPN && BBPN->getParent() == BB) {

894 if (BBPreds.count(IBB) &&

898 << "Can't fold, phi node " << PN->getName() << " in "

899 << Succ->getName() << " is conflicting with "

900 << BBPN->getName() << " with regard to common predecessor "

901 << IBB->getName() << "\n");

902 return false;

903 }

904 }

905 } else {

908

909

910

912 if (BBPreds.count(IBB) &&

915 << " in " << Succ->getName()

916 << " is conflicting with regard to common "

917 << "predecessor " << IBB->getName() << "\n");

918 return false;

919 }

920 }

921 }

922 }

923

924 return true;

925}

926

929

930

931

932

933

934

935

936

937

938

939

940

941

944 if (!isa(OldVal)) {

946 IncomingValues.find(BB)->second == OldVal) &&

947 "Expected OldVal to match incoming value from BB!");

948

949 IncomingValues.insert(std::make_pair(BB, OldVal));

950 return OldVal;

951 }

952

954 if (It != IncomingValues.end()) return It->second;

955

956 return OldVal;

957}

958

959

960

961

962

963

964

965

966

972

973 if (!isa(V))

974 IncomingValues.insert(std::make_pair(BB, V));

975 }

976}

977

978

979

980

981

982

988

989 if (!isa(V)) continue;

990

993

994

995

996

997

998 if (It == IncomingValues.end()) {

1000 continue;

1001 }

1002

1003

1004

1006 }

1007

1008

1009

1010

1011 unsigned PoisonCount = count_if(TrueUndefOps, [&](unsigned i) {

1013 });

1014 if (PoisonCount != 0 && PoisonCount != TrueUndefOps.size()) {

1015 for (unsigned i : TrueUndefOps)

1017 }

1018}

1019

1020

1021

1022

1023static bool

1027

1028

1029 if (BB->phis().empty() || Succ->phis().empty())

1030 return false;

1031

1032

1034 return false;

1035

1037 return isa(Pred->getTerminator());

1038 }))

1039 return false;

1040

1041

1042

1044 if (BBPreds.count(SuccPred)) {

1045 if (CommonPred)

1046 return false;

1047 CommonPred = SuccPred;

1048 }

1049 }

1050

1051 return true;

1052}

1053

1054

1055

1056

1058

1059

1061 return false;

1062 unsigned NumPreds = pred_size(BB);

1063 unsigned NumChangedPhi = 0;

1064 for (auto &Phi : Succ->phis()) {

1065

1066

1067 if (auto *IncomingPhi = dyn_cast(Phi.getIncomingValueForBlock(BB)))

1068 if (IncomingPhi->getParent() == BB)

1069 continue;

1070

1071 NumChangedPhi++;

1072 }

1073

1074

1075

1076

1077 return (NumPreds - 1) * NumChangedPhi >

1079}

1080

1081

1082

1083

1084

1085

1086

1087

1088

1094 assert(OldVal && "No entry in PHI for Pred BB!");

1095

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1108

1109

1110

1111 if (isa(OldVal) && cast(OldVal)->getParent() == BB) {

1112 PHINode *OldValPN = cast(OldVal);

1114

1115

1116

1117

1118

1120

1121 if (PredBB == CommonPred)

1122 continue;

1123

1125 Value *Selected =

1127

1128

1129

1131 }

1132 if (CommonPred)

1134

1135 } else {

1136 for (BasicBlock *PredBB : BBPreds) {

1137

1138

1139 if (PredBB == CommonPred)

1140 continue;

1141

1142 Value *Selected =

1144

1145

1146

1148 }

1149 if (CommonPred)

1151 }

1152

1154}

1155

1159 "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");

1160

1161

1163 if (BB == Succ)

1164 return false;

1165

1167

1168

1170

1172

1173

1174

1176 BB, Succ, BBPreds, CommonPred);

1177

1179 return false;

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1198 while (isa(*BBI)) {

1199 for (Use &U : BBI->uses()) {

1200 if (PHINode* PN = dyn_cast(U.getUser())) {

1201 if (PN->getIncomingBlock(U) != BB)

1202 return false;

1203 } else {

1204 return false;

1205 }

1206 }

1207 ++BBI;

1208 }

1209 }

1210

1211 if (BBPhisMergeable && CommonPred)

1213 << " and " << Succ->getName() << " : "

1214 << CommonPred->getName() << "\n");

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

1282 if (TI->hasNonDebugLocLoopMetadata())

1284 if (Instruction *PredTI = Pred->getTerminator())

1285 if (PredTI->hasNonDebugLocLoopMetadata())

1286 return false;

1287

1288 if (BBKillable)

1289 LLVM_DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);

1290 else if (BBPhisMergeable)

1291 LLVM_DEBUG(dbgs() << "Merge Phis in Trivial BB: \n" << *BB);

1292

1294

1295 if (DTU) {

1296

1298

1299

1303

1304 if (!SuccPreds.contains(PredOfBB))

1305 if (SeenPreds.insert(PredOfBB).second)

1306 Updates.push_back({DominatorTree::Insert, PredOfBB, Succ});

1307 }

1308

1309 SeenPreds.clear();

1310

1312

1313

1314 if (SeenPreds.insert(PredOfBB).second && PredOfBB != CommonPred)

1315 Updates.push_back({DominatorTree::Delete, PredOfBB, BB});

1316

1317 if (BBKillable)

1318 Updates.push_back({DominatorTree::Delete, BB, Succ});

1319 }

1320

1321 if (isa(Succ->begin())) {

1322

1323

1324

1326

1327

1329 PHINode *PN = cast(I);

1331 }

1332 }

1333

1335

1336

1337

1340 } else {

1341 while (PHINode *PN = dyn_cast(&BB->front())) {

1342

1343 assert(PN->use_empty() && "There shouldn't be any uses here!");

1344 PN->eraseFromParent();

1345 }

1346 }

1347

1348

1349

1350

1352 if (TI->hasNonDebugLocLoopMetadata()) {

1353 MDNode *LoopMD = TI->getMetadata(LLVMContext::MD_loop);

1355 Pred->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopMD);

1356 }

1357

1358 if (BBKillable) {

1359

1361

1364

1365

1368

1370 assert(succ_empty(BB) && "The successor list of BB isn't empty before "

1371 "applying corresponding DTU updates.");

1372 } else if (BBPhisMergeable) {

1373

1375 if (Instruction *UseInst = dyn_cast(U.getUser()))

1376 return UseInst->getParent() != CommonPred &&

1377 BBPreds.contains(UseInst->getParent());

1378 return false;

1379 });

1380 }

1381

1382 if (DTU)

1384

1385 if (BBKillable)

1387

1388 return true;

1389}

1390

1391static bool

1394

1395

1396

1397

1398 bool Changed = false;

1399

1400

1401

1402

1403 for (auto I = BB->begin(); PHINode *PN = dyn_cast(I);) {

1404 ++I;

1405

1406

1407

1408 for (auto J = I; PHINode *DuplicatePN = dyn_cast(J); ++J) {

1409 if (ToRemove.contains(DuplicatePN))

1410 continue;

1412 continue;

1413

1414 ++NumPHICSEs;

1416 ToRemove.insert(DuplicatePN);

1417 Changed = true;

1418

1419

1421 break;

1422 }

1423 }

1424 return Changed;

1425}

1426

1427static bool

1430

1431

1432

1433

1434 struct PHIDenseMapInfo {

1435 static PHINode *getEmptyKey() {

1437 }

1438

1439 static PHINode *getTombstoneKey() {

1441 }

1442

1444 return PN == getEmptyKey() || PN == getTombstoneKey();

1445 }

1446

1447

1448

1450

1451

1452

1456 }

1457

1458 static unsigned getHashValue(PHINode *PN) {

1459#ifndef NDEBUG

1460

1461

1462

1463

1465 return 0;

1466#endif

1468 }

1469

1473 return LHS->isIdenticalTo(RHS);

1474 }

1475

1477

1478

1482 return Result;

1483 }

1484 };

1485

1486

1489

1490

1491 bool Changed = false;

1492 for (auto I = BB->begin(); PHINode *PN = dyn_cast(I++);) {

1494 continue;

1495 auto Inserted = PHISet.insert(PN);

1496 if (!Inserted.second) {

1497

1498 ++NumPHICSEs;

1499 PN->replaceAllUsesWith(*Inserted.first);

1501 Changed = true;

1502

1503

1504

1507 }

1508 }

1509

1510 return Changed;

1511}

1512

1515 if (

1518#endif

1522}

1523

1528 PN->eraseFromParent();

1529 return Changed;

1530}

1531

1534 V = V->stripPointerCasts();

1535

1536 if (AllocaInst *AI = dyn_cast(V)) {

1537

1538

1539

1540

1541

1542 Align CurrentAlign = AI->getAlign();

1543 if (PrefAlign <= CurrentAlign)

1544 return CurrentAlign;

1545

1546

1547

1548 MaybeAlign StackAlign = DL.getStackAlignment();

1549 if (StackAlign && PrefAlign > *StackAlign)

1550 return CurrentAlign;

1551 AI->setAlignment(PrefAlign);

1552 return PrefAlign;

1553 }

1554

1555 if (auto *GO = dyn_cast(V)) {

1556

1557 Align CurrentAlign = GO->getPointerAlignment(DL);

1558 if (PrefAlign <= CurrentAlign)

1559 return CurrentAlign;

1560

1561

1562

1563

1564

1565 if (!GO->canIncreaseAlignment())

1566 return CurrentAlign;

1567

1568 if (GO->isThreadLocal()) {

1569 unsigned MaxTLSAlign = GO->getParent()->getMaxTLSAlignment() / CHAR_BIT;

1570 if (MaxTLSAlign && PrefAlign > Align(MaxTLSAlign))

1571 PrefAlign = Align(MaxTLSAlign);

1572 }

1573

1574 GO->setAlignment(PrefAlign);

1575 return PrefAlign;

1576 }

1577

1578 return Align(1);

1579}

1580

1586 assert(V->getType()->isPointerTy() &&

1587 "getOrEnforceKnownAlignment expects a pointer!");

1588

1591

1592

1593

1594

1596

1598

1599 if (PrefAlign && *PrefAlign > Alignment)

1601

1602

1603 return Alignment;

1604}

1605

1606

1607

1608

1609

1610

1614

1615

1616

1619 findDbgValues(DbgValues, APN, &DbgVariableRecords);

1620 for (auto *DVI : DbgValues) {

1622 if ((DVI->getVariable() == DIVar) && (DVI->getExpression() == DIExpr))

1623 return true;

1624 }

1625 for (auto *DVR : DbgVariableRecords) {

1627 if ((DVR->getVariable() == DIVar) && (DVR->getExpression() == DIExpr))

1628 return true;

1629 }

1630 return false;

1631}

1632

1633

1634

1635

1636

1637

1638

1639

1640

1643 TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy);

1644 if (std::optional<uint64_t> FragmentSize =

1646 return TypeSize::isKnownGE(ValueSize, TypeSize::getFixed(*FragmentSize));

1647

1648

1649

1650

1652

1654 "address of variable must have exactly 1 location operand.");

1655 if (auto *AI =

1657 if (std::optional FragmentSize =

1658 AI->getAllocationSizeInBits(DL)) {

1659 return TypeSize::isKnownGE(ValueSize, *FragmentSize);

1660 }

1661 }

1662 }

1663

1664 return false;

1665}

1666

1667

1670 TypeSize ValueSize = DL.getTypeAllocSizeInBits(ValTy);

1671 if (std::optional<uint64_t> FragmentSize =

1673 return TypeSize::isKnownGE(ValueSize, TypeSize::getFixed(*FragmentSize));

1674

1675

1676

1677

1679

1681 "address of variable must have exactly 1 location operand.");

1682 if (auto *AI =

1684 if (std::optional FragmentSize = AI->getAllocationSizeInBits(DL)) {

1685 return TypeSize::isKnownGE(ValueSize, *FragmentSize);

1686 }

1687 }

1688 }

1689

1690 return false;

1691}

1692

1699 auto DbgVal = Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc,

1701 cast<Instruction *>(DbgVal)->insertBefore(Instr);

1702 } else {

1703

1704

1708 Instr->getParent()->insertDbgRecordBefore(DV, Instr);

1709 }

1710}

1711

1716 auto DbgVal = Builder.insertDbgValueIntrinsic(DV, DIVar, DIExpr, NewLoc,

1718 cast<Instruction *>(DbgVal)->insertAfter(&*Instr);

1719 } else {

1720

1721

1725 Instr->getParent()->insertDbgRecordAfter(DV, &*Instr);

1726 }

1727}

1728

1729

1730

1735 assert(DIVar && "Missing variable");

1737 Value *DV = SI->getValueOperand();

1738

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753 bool CanConvert =

1754 DIExpr->isDeref() || (!DIExpr->startsWithDeref() &&

1756 if (CanConvert) {

1758 SI->getIterator());

1759 return;

1760 }

1761

1762

1763

1764 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " << *DII

1765 << '\n');

1766

1767

1768

1771 SI->getIterator());

1772}

1773

1776 return DIExpression::get(DIExpr->getContext(),

1778}

1779

1783 assert(DIVar && "Missing variable");

1786 Value *DV = SI->getValueOperand();

1787

1789

1791 SI->getIterator());

1792}

1793

1794

1795

1800 assert(DIVar && "Missing variable");

1801

1803

1804

1805

1806 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "

1807 << *DII << '\n');

1808 return;

1809 }

1810

1812

1813

1814

1815

1816

1819}

1820

1825 assert(DIVar && "Missing variable");

1827 Value *DV = SI->getValueOperand();

1828

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843 bool CanConvert =

1844 DIExpr->isDeref() || (!DIExpr->startsWithDeref() &&

1846 if (CanConvert) {

1848 SI->getIterator());

1849 return;

1850 }

1851

1852

1853

1854 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: " << *DVR

1855 << '\n');

1857

1858

1859

1860

1865 SI->getParent()->insertDbgRecordBefore(NewDVR, SI->getIterator());

1866}

1867

1871 assert(DIVar && "Missing variable");

1874 Value *DV = SI->getValueOperand();

1875

1877

1879 SI->getIterator());

1880}

1881

1882

1883

1888 assert(DIVar && "Missing variable");

1889

1891 return;

1892

1894

1895

1896

1897 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to dbg.value: "

1898 << *DII << '\n');

1899 return;

1900 }

1901

1904

1906

1907

1908

1909

1910 if (InsertionPt != BB->end()) {

1912 InsertionPt);

1913 }

1914}

1915

1920 assert(DIVar && "Missing variable");

1921

1923

1924

1925

1926 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to DbgVariableRecord: "

1927 << *DVR << '\n');

1928 return;

1929 }

1930

1932

1933

1934

1935

1936

1938

1939

1943 LI->getParent()->insertDbgRecordAfter(DV, LI);

1944}

1945

1946

1950}

1951

1952

1955}

1960 assert(DIVar && "Missing variable");

1961

1963 return;

1964

1966

1967

1968

1969 LLVM_DEBUG(dbgs() << "Failed to convert dbg.declare to DbgVariableRecord: "

1970 << *DVR << '\n');

1971 return;

1972 }

1973

1976

1978

1979

1980

1981

1982 if (InsertionPt != BB->end()) {

1984 InsertionPt);

1985 }

1986}

1987

1988

1989

1991 bool Changed = false;

1992 DIBuilder DIB(*F.getParent(), false);

1995 for (auto &FI : F) {

1997 if (auto *DDI = dyn_cast(&BI))

2000 if (DVR.getType() == DbgVariableRecord::LocationType::Declare)

2002 }

2003 }

2004 }

2005

2007 return Changed;

2008

2009 auto LowerOne = [&](auto *DDI) {

2011 dyn_cast_or_null(DDI->getVariableLocationOp(0));

2012

2013

2014

2015

2016

2017

2019 return;

2020

2021

2023 if (LoadInst *LI = dyn_cast(U))

2024 return LI->isVolatile();

2025 if (StoreInst *SI = dyn_cast(U))

2026 return SI->isVolatile();

2027 return false;

2028 }))

2029 return;

2030

2033 while (!WorkList.empty()) {

2035 for (const auto &AIUse : V->uses()) {

2036 User *U = AIUse.getUser();

2037 if (StoreInst *SI = dyn_cast(U)) {

2038 if (AIUse.getOperandNo() == 1)

2040 } else if (LoadInst *LI = dyn_cast(U)) {

2042 } else if (CallInst *CI = dyn_cast(U)) {

2043

2044

2045

2046 if (!CI->isLifetimeStartOrEnd()) {

2048 auto *DerefExpr =

2051 DerefExpr, NewLoc,

2052 CI->getIterator());

2053 }

2054 } else if (BitCastInst *BI = dyn_cast(U)) {

2055 if (BI->getType()->isPointerTy())

2057 }

2058 }

2059 }

2060 DDI->eraseFromParent();

2061 Changed = true;

2062 };

2063

2066

2067 if (Changed)

2070

2071 return Changed;

2072}

2073

2074

2075

2076

2077static void

2080 assert(BB && "No BasicBlock to clone DbgVariableRecord(s) from.");

2081 if (InsertedPHIs.size() == 0)

2082 return;

2083

2084

2086 for (auto &I : *BB) {

2088 for (Value *V : DVR.location_ops())

2089 if (auto *Loc = dyn_cast_or_null(V))

2090 DbgValueMap.insert({Loc, &DVR});

2091 }

2092 }

2093 if (DbgValueMap.size() == 0)

2094 return;

2095

2096

2097

2098

2099

2100

2102 NewDbgValueMap;

2103

2104

2105

2106

2107

2108 for (auto PHI : InsertedPHIs) {

2110

2112 continue;

2113 for (auto VI : PHI->operand_values()) {

2114 auto V = DbgValueMap.find(VI);

2115 if (V != DbgValueMap.end()) {

2117 auto NewDI = NewDbgValueMap.find({Parent, DbgII});

2118 if (NewDI == NewDbgValueMap.end()) {

2120 NewDI = NewDbgValueMap.insert({{Parent, DbgII}, NewDbgII}).first;

2121 }

2123

2124

2127 }

2128 }

2129 }

2130

2131 for (auto DI : NewDbgValueMap) {

2132 BasicBlock *Parent = DI.first.first;

2135 assert(InsertionPt != Parent->end() && "Ill-formed basic block");

2136

2138 }

2139}

2140

2141

2144 assert(BB && "No BasicBlock to clone dbg.value(s) from.");

2145 if (InsertedPHIs.size() == 0)

2146 return;

2147

2149

2150

2152 for (auto &I : *BB) {

2153 if (auto DbgII = dyn_cast(&I)) {

2154 for (Value *V : DbgII->location_ops())

2155 if (auto *Loc = dyn_cast_or_null(V))

2156 DbgValueMap.insert({Loc, DbgII});

2157 }

2158 }

2159 if (DbgValueMap.size() == 0)

2160 return;

2161

2162

2163

2164

2165

2168 NewDbgValueMap;

2169

2170

2171

2172

2173

2174 for (auto *PHI : InsertedPHIs) {

2176

2178 continue;

2179 for (auto *VI : PHI->operand_values()) {

2180 auto V = DbgValueMap.find(VI);

2181 if (V != DbgValueMap.end()) {

2182 auto *DbgII = cast(V->second);

2183 auto [NewDI, Inserted] = NewDbgValueMap.try_emplace({Parent, DbgII});

2184 if (Inserted)

2185 NewDI->second = cast(DbgII->clone());

2187

2188

2191 }

2192 }

2193 }

2194

2195 for (auto DI : NewDbgValueMap) {

2196 BasicBlock *Parent = DI.first.first;

2197 auto *NewDbgII = DI.second;

2199 assert(InsertionPt != Parent->end() && "Ill-formed basic block");

2200 NewDbgII->insertBefore(&*InsertionPt);

2201 }

2202}

2203

2209

2210 auto ReplaceOne = [&](auto *DII) {

2211 assert(DII->getVariable() && "Missing variable");

2212 auto *DIExpr = DII->getExpression();

2214 DII->setExpression(DIExpr);

2215 DII->replaceVariableLocationOp(Address, NewAddress);

2216 };

2217

2218 for_each(DbgDeclares, ReplaceOne);

2219 for_each(DVRDeclares, ReplaceOne);

2220

2221 return !DbgDeclares.empty() || !DVRDeclares.empty();

2222}

2223

2230 assert(DIVar && "Missing variable");

2231

2232

2233

2234

2236 DIExpr->getElement(0) != dwarf::DW_OP_deref)

2237 return;

2238

2239

2242

2243 if (DVI) {

2246 } else {

2250 }

2251}

2252

2258

2259

2260 for (auto *DVI : DbgUsers)

2262 DVI->getExpression(), NewAllocaAddress, DVI,

2263 nullptr, Builder, Offset);

2264

2265

2268 DVR->getExpression(), NewAllocaAddress, nullptr,

2269 DVR, Builder, Offset);

2270}

2271

2272

2273

2279}

2280

2282 Instruction *I = dyn_cast(Assign->getAddress());

2283

2284 if (I)

2285 return;

2286

2287 assert(!Assign->getAddressExpression()->getFragmentInfo().has_value() &&

2288 "address-expression shouldn't have fragment info");

2289

2290

2295

2296

2297 if (!NewV)

2298 return;

2299

2301 Assign->getAddressExpression(), Ops, 0, false);

2303 "address-expression shouldn't have fragment info");

2304

2306

2307

2308 if (AdditionalValues.empty()) {

2309 Assign->setAddress(NewV);

2310 Assign->setAddressExpression(SalvagedExpr);

2311 } else {

2312 Assign->setKillAddress();

2313 }

2314}

2315

2319

2320

2321

2322 const unsigned MaxDebugArgs = 16;

2323 const unsigned MaxExpressionSize = 128;

2324 bool Salvaged = false;

2325

2326 for (auto *DII : DbgUsers) {

2327 if (auto *DAI = dyn_cast(DII)) {

2328 if (DAI->getAddress() == &I) {

2330 Salvaged = true;

2331 }

2332 if (DAI->getValue() != &I)

2333 continue;

2334 }

2335

2336

2337

2338 bool StackValue = isa(DII);

2339 auto DIILocation = DII->location_ops();

2342 "DbgVariableIntrinsic must use salvaged instruction as its location");

2344

2345

2346

2347

2348 Value *Op0 = nullptr;

2349 DIExpression *SalvagedExpr = DII->getExpression();

2350 auto LocItr = find(DIILocation, &I);

2351 while (SalvagedExpr && LocItr != DIILocation.end()) {

2353 unsigned LocNo = std::distance(DIILocation.begin(), LocItr);

2356 if (!Op0)

2357 break;

2358 SalvagedExpr =

2360 LocItr = std::find(++LocItr, DIILocation.end(), &I);

2361 }

2362

2363

2364 if (!Op0)

2365 break;

2366

2368 DII->replaceVariableLocationOp(&I, Op0);

2369 bool IsValidSalvageExpr = SalvagedExpr->getNumElements() <= MaxExpressionSize;

2370 if (AdditionalValues.empty() && IsValidSalvageExpr) {

2371 DII->setExpression(SalvagedExpr);

2372 } else if (isa(DII) && IsValidSalvageExpr &&

2373 DII->getNumVariableLocationOps() + AdditionalValues.size() <=

2374 MaxDebugArgs) {

2375 DII->addVariableLocationOps(AdditionalValues, SalvagedExpr);

2376 } else {

2377

2378

2379

2380 DII->setKillLocation();

2381 }

2383 Salvaged = true;

2384 }

2385

2386 for (auto *DVR : DPUsers) {

2387 if (DVR->isDbgAssign()) {

2388 if (DVR->getAddress() == &I) {

2390 Salvaged = true;

2391 }

2392 if (DVR->getValue() != &I)

2393 continue;

2394 }

2395

2396

2397

2398

2399 bool StackValue =

2400 DVR->getType() != DbgVariableRecord::LocationType::Declare;

2401 auto DVRLocation = DVR->location_ops();

2404 "DbgVariableIntrinsic must use salvaged instruction as its location");

2406

2407

2408

2409

2410 Value *Op0 = nullptr;

2411 DIExpression *SalvagedExpr = DVR->getExpression();

2412 auto LocItr = find(DVRLocation, &I);

2413 while (SalvagedExpr && LocItr != DVRLocation.end()) {

2415 unsigned LocNo = std::distance(DVRLocation.begin(), LocItr);

2418 if (!Op0)

2419 break;

2420 SalvagedExpr =

2422 LocItr = std::find(++LocItr, DVRLocation.end(), &I);

2423 }

2424

2425

2426 if (!Op0)

2427 break;

2428

2430 DVR->replaceVariableLocationOp(&I, Op0);

2431 bool IsValidSalvageExpr =

2432 SalvagedExpr->getNumElements() <= MaxExpressionSize;

2433 if (AdditionalValues.empty() && IsValidSalvageExpr) {

2434 DVR->setExpression(SalvagedExpr);

2435 } else if (DVR->getType() != DbgVariableRecord::LocationType::Declare &&

2436 IsValidSalvageExpr &&

2437 DVR->getNumVariableLocationOps() + AdditionalValues.size() <=

2438 MaxDebugArgs) {

2439 DVR->addVariableLocationOps(AdditionalValues, SalvagedExpr);

2440 } else {

2441

2442

2443

2444

2445 DVR->setKillLocation();

2446 }

2448 Salvaged = true;

2449 }

2450

2451 if (Salvaged)

2452 return;

2453

2454 for (auto *DII : DbgUsers)

2455 DII->setKillLocation();

2456

2457 for (auto *DVR : DPUsers)

2458 DVR->setKillLocation();

2459}

2460

2465 unsigned BitWidth = DL.getIndexSizeInBits(GEP->getPointerAddressSpace());

2466

2469 if (GEP->collectOffset(DL, BitWidth, VariableOffsets, ConstantOffset))

2470 return nullptr;

2471 if (!VariableOffsets.empty() && !CurrentLocOps) {

2472 Opcodes.insert(Opcodes.begin(), {dwarf::DW_OP_LLVM_arg, 0});

2473 CurrentLocOps = 1;

2474 }

2475 for (const auto &Offset : VariableOffsets) {

2477 assert(Offset.second.isStrictlyPositive() &&

2478 "Expected strictly positive multiplier for offset.");

2480 Offset.second.getZExtValue(), dwarf::DW_OP_mul,

2481 dwarf::DW_OP_plus});

2482 }

2484 return GEP->getOperand(0);

2485}

2486

2488 switch (Opcode) {

2489 case Instruction::Add:

2490 return dwarf::DW_OP_plus;

2491 case Instruction::Sub:

2492 return dwarf::DW_OP_minus;

2493 case Instruction::Mul:

2494 return dwarf::DW_OP_mul;

2495 case Instruction::SDiv:

2496 return dwarf::DW_OP_div;

2497 case Instruction::SRem:

2498 return dwarf::DW_OP_mod;

2499 case Instruction::Or:

2500 return dwarf::DW_OP_or;

2501 case Instruction::And:

2502 return dwarf::DW_OP_and;

2503 case Instruction::Xor:

2504 return dwarf::DW_OP_xor;

2505 case Instruction::Shl:

2506 return dwarf::DW_OP_shl;

2507 case Instruction::LShr:

2508 return dwarf::DW_OP_shr;

2509 case Instruction::AShr:

2510 return dwarf::DW_OP_shra;

2511 default:

2512

2513 return 0;

2514 }

2515}

2516

2521 if (!CurrentLocOps) {

2523 CurrentLocOps = 1;

2524 }

2526 AdditionalValues.push_back(I->getOperand(1));

2527}

2528

2532

2533 auto *ConstInt = dyn_cast(BI->getOperand(1));

2534

2535 if (ConstInt && ConstInt->getBitWidth() > 64)

2536 return nullptr;

2537

2539

2540 if (ConstInt) {

2541 uint64_t Val = ConstInt->getSExtValue();

2542

2543

2544 if (BinOpcode == Instruction::Add || BinOpcode == Instruction::Sub) {

2545 uint64_t Offset = BinOpcode == Instruction::Add ? Val : -int64_t(Val);

2548 }

2549 Opcodes.append({dwarf::DW_OP_constu, Val});

2550 } else {

2552 }

2553

2554

2555

2557 if (!DwarfBinOp)

2558 return nullptr;

2561}

2562

2564

2565

2566 switch (Pred) {

2568 return dwarf::DW_OP_eq;

2570 return dwarf::DW_OP_ne;

2573 return dwarf::DW_OP_gt;

2576 return dwarf::DW_OP_ge;

2579 return dwarf::DW_OP_lt;

2582 return dwarf::DW_OP_le;

2583 default:

2584 return 0;

2585 }

2586}

2587

2591

2592 auto *ConstInt = dyn_cast(Icmp->getOperand(1));

2593

2594 if (ConstInt && ConstInt->getBitWidth() > 64)

2595 return nullptr;

2596

2597 if (ConstInt) {

2599 Opcodes.push_back(dwarf::DW_OP_consts);

2600 else

2601 Opcodes.push_back(dwarf::DW_OP_constu);

2602 uint64_t Val = ConstInt->getSExtValue();

2604 } else {

2606 }

2607

2608

2609

2611 if (!DwarfIcmpOp)

2612 return nullptr;

2615}

2616

2620 auto &M = *I.getModule();

2621 auto &DL = M.getDataLayout();

2622

2623 if (auto *CI = dyn_cast(&I)) {

2624 Value *FromValue = CI->getOperand(0);

2625

2626 if (CI->isNoopCast(DL)) {

2627 return FromValue;

2628 }

2629

2633

2635 !(isa(&I) || isa(&I) || isa(&I) ||

2636 isa(&I) || isa(&I)))

2637 return nullptr;

2638

2640 if (FromType->isPointerTy())

2641 FromType = DL.getIntPtrType(FromType);

2642

2643 unsigned FromTypeBitSize = FromType->getScalarSizeInBits();

2645

2647 isa(&I));

2648 Ops.append(ExtOps.begin(), ExtOps.end());

2649 return FromValue;

2650 }

2651

2652 if (auto *GEP = dyn_cast(&I))

2654 if (auto *BI = dyn_cast(&I))

2656 if (auto *IC = dyn_cast(&I))

2658

2659

2660

2661

2662 return nullptr;

2663}

2664

2665

2667

2668

2669

2670

2675

2679 if (Users.empty() && DPUsers.empty())

2680 return false;

2681

2682

2683 bool Changed = false;

2684

2687 if (isa(&To)) {

2688 bool DomPointAfterFrom = From.getNextNonDebugInstruction() == &DomPoint;

2689

2690 for (auto *DII : Users) {

2691

2692

2696 Changed = true;

2697

2698

2699

2700 } else if (!DT.dominates(&DomPoint, DII)) {

2701 UndefOrSalvage.insert(DII);

2702 }

2703 }

2704

2705

2706 for (auto *DVR : DPUsers) {

2708 Instruction *NextNonDebug = MarkedInstr;

2709

2710 if (isa(NextNonDebug))

2712

2713 if (DomPointAfterFrom && NextNonDebug == &DomPoint) {

2716

2717 DomPoint.getParent()->insertDbgRecordAfter(DVR, &DomPoint);

2718 Changed = true;

2719 } else if (!DT.dominates(&DomPoint, MarkedInstr)) {

2720 UndefOrSalvageDVR.insert(DVR);

2721 }

2722 }

2723 }

2724

2725

2726 for (auto *DII : Users) {

2727 if (UndefOrSalvage.count(DII))

2728 continue;

2729

2731 if (!DVRepl)

2732 continue;

2733

2737 Changed = true;

2738 }

2739 for (auto *DVR : DPUsers) {

2740 if (UndefOrSalvageDVR.count(DVR))

2741 continue;

2742

2744 if (!DVRepl)

2745 continue;

2746

2750 Changed = true;

2751 }

2752

2753 if (!UndefOrSalvage.empty() || !UndefOrSalvageDVR.empty()) {

2754

2756 Changed = true;

2757 }

2758

2759 return Changed;

2760}

2761

2762

2763

2764

2765

2766

2767

2768

2770 Type *ToTy) {

2771

2772 if (FromTy == ToTy)

2773 return true;

2774

2775

2777 bool SameSize = DL.getTypeSizeInBits(FromTy) == DL.getTypeSizeInBits(ToTy);

2778 bool LosslessConversion = DL.isNonIntegralPointerType(FromTy) &&

2779 DL.isNonIntegralPointerType(ToTy);

2780 return SameSize && LosslessConversion;

2781 }

2782

2783

2784 return false;

2785}

2786

2789

2790 if (From.isUsedByMetadata())

2791 return false;

2792

2793 assert(&From != &To && "Can't replace something with itself");

2794

2795 Type *FromTy = From.getType();

2797

2800 };

2802 return DVR.getExpression();

2803 };

2804

2805

2810

2811

2812

2816 assert(FromBits != ToBits && "Unexpected no-op conversion");

2817

2818

2819

2820 if (FromBits < ToBits)

2822

2823

2824

2827

2828

2830 if (!Signedness)

2831 return std::nullopt;

2832

2833 bool Signed = *Signedness == DIBasicType::Signedness::Signed;

2836 };

2837

2838

2841

2842

2844 if (!Signedness)

2845 return std::nullopt;

2846

2847 bool Signed = *Signedness == DIBasicType::Signedness::Signed;

2850 };

2852 SignOrZeroExtDVR);

2853 }

2854

2855

2856 return false;

2857}

2858

2861 bool Changed = false;

2862

2863 I->dropDbgRecords();

2864 for (Use &U : I->operands()) {

2866 if (isa(Op) && Op->getType()->isTokenTy()) {

2869 Changed = true;

2870 }

2871 }

2872

2873 return Changed;

2874}

2875

2876std::pair<unsigned, unsigned>

2878 unsigned NumDeadInst = 0;

2879 unsigned NumDeadDbgInst = 0;

2880

2881

2885

2886 while (EndInst != &BB->front()) {

2887

2892

2893

2895 EndInst = Inst;

2896 continue;

2897 }

2898 if (isa(Inst))

2899 ++NumDeadDbgInst;

2900 else

2901 ++NumDeadInst;

2902

2905 }

2906 return {NumDeadInst, NumDeadDbgInst};

2907}

2908

2913

2914 if (MSSAU)

2916

2918

2919

2920

2922 Successor->removePredecessor(BB, PreserveLCSSA);

2923 if (DTU)

2925 }

2926 auto *UI = new UnreachableInst(I->getContext(), I->getIterator());

2927 UI->setDebugLoc(I->getDebugLoc());

2928

2929

2930 unsigned NumInstrsRemoved = 0;

2932 while (BBI != BBE) {

2933 if (!BBI->use_empty())

2935 BBI++->eraseFromParent();

2936 ++NumInstrsRemoved;

2937 }

2938 if (DTU) {

2940 Updates.reserve(UniqueSuccessors.size());

2941 for (BasicBlock *UniqueSuccessor : UniqueSuccessors)

2942 Updates.push_back({DominatorTree::Delete, BB, UniqueSuccessor});

2944 }

2946 return NumInstrsRemoved;

2947}

2948

2952 II->getOperandBundlesAsDefs(OpBundles);

2954 II->getCalledOperand(), Args, OpBundles);

2959

2960

2963

2965 auto NewWeights = uint32_t(TotalWeight) != TotalWeight

2966 ? nullptr

2968 NewCall->setMetadata(LLVMContext::MD_prof, NewWeights);

2969 }

2970

2971 return NewCall;

2972}

2973

2974

2979 II->replaceAllUsesWith(NewCall);

2980

2981

2982 BasicBlock *NormalDestBB = II->getNormalDest();

2984

2985

2987 BasicBlock *UnwindDestBB = II->getUnwindDest();

2989 II->eraseFromParent();

2990 if (DTU)

2991 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});

2992 return NewCall;

2993}

2994

2999

3000

3001

3002 BasicBlock *Split = SplitBlock(BB, CI, DTU, nullptr, nullptr,

3003 CI->getName() + ".noexc");

3004

3005

3007

3008

3011

3013

3014

3015

3016

3017

3020 UnwindEdge, InvokeArgs, OpBundles, CI->getName(), BB);

3024 II->setMetadata(LLVMContext::MD_prof, CI->getMetadata(LLVMContext::MD_prof));

3025

3026 if (DTU)

3027 DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}});

3028

3029

3030

3032

3033

3034 Split->front().eraseFromParent();

3035 return Split;

3036}

3037

3044 Reachable.insert(BB);

3045 bool Changed = false;

3046 do {

3048

3049

3050

3051

3053 if (auto *CI = dyn_cast(&I)) {

3054 Value *Callee = CI->getCalledOperand();

3055

3056 if (Function *F = dyn_cast(Callee)) {

3057 auto IntrinsicID = F->getIntrinsicID();

3058

3059

3060

3061

3062 if (IntrinsicID == Intrinsic::assume) {

3064

3066 Changed = true;

3067 break;

3068 }

3069 } else if (IntrinsicID == Intrinsic::experimental_guard) {

3070

3071

3072

3073

3074

3075

3076

3077

3078

3079 if (match(CI->getArgOperand(0), m_Zero()))

3080 if (!isa(CI->getNextNode())) {

3082 Changed = true;

3083 break;

3084 }

3085 }

3086 } else if ((isa(Callee) &&

3088 cast(Callee->getType())

3089 ->getAddressSpace())) ||

3090 isa(Callee)) {

3092 Changed = true;

3093 break;

3094 }

3095 if (CI->doesNotReturn() && !CI->isMustTailCall()) {

3096

3097

3098

3099 if (!isa(CI->getNextNonDebugInstruction())) {

3100

3102 Changed = true;

3103 }

3104 break;

3105 }

3106 } else if (auto *SI = dyn_cast(&I)) {

3107

3108

3109

3110

3111

3112 if (SI->isVolatile()) continue;

3113

3114 Value *Ptr = SI->getOperand(1);

3115

3116 if (isa(Ptr) ||

3117 (isa(Ptr) &&

3119 SI->getPointerAddressSpace()))) {

3121 Changed = true;

3122 break;

3123 }

3124 }

3125 }

3126

3127 Instruction *Terminator = BB->getTerminator();

3128 if (auto *II = dyn_cast(Terminator)) {

3129

3130 Value *Callee = II->getCalledOperand();

3131 if ((isa(Callee) &&

3133 isa(Callee)) {

3135 Changed = true;

3136 } else {

3137 if (II->doesNotReturn() &&

3138 !isa(II->getNormalDest()->front())) {

3139

3140

3141

3142

3143

3144 BasicBlock *OrigNormalDest = II->getNormalDest();

3148 Ctx, OrigNormalDest->getName() + ".unreachable",

3149 II->getFunction(), OrigNormalDest);

3151 II->setNormalDest(UnreachableNormalDest);

3152 if (DTU)

3153 DTU->applyUpdates(

3154 {{DominatorTree::Delete, BB, OrigNormalDest},

3155 {DominatorTree::Insert, BB, UnreachableNormalDest}});

3156 Changed = true;

3157 }

3159 if (II->use_empty() && II->mayHaveSideEffects()) {

3160

3161 BasicBlock *NormalDestBB = II->getNormalDest();

3162 BasicBlock *UnwindDestBB = II->getUnwindDest();

3165 II->eraseFromParent();

3166 if (DTU)

3167 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDestBB}});

3168 } else

3170 Changed = true;

3171 }

3172 }

3173 } else if (auto *CatchSwitch = dyn_cast(Terminator)) {

3174

3175 struct CatchPadDenseMapInfo {

3178 }

3179

3182 }

3183

3184 static unsigned getHashValue(CatchPadInst *CatchPad) {

3187 }

3188

3190 if (LHS == getEmptyKey() || LHS == getTombstoneKey() ||

3191 RHS == getEmptyKey() || RHS == getTombstoneKey())

3193 return LHS->isIdenticalTo(RHS);

3194 }

3195 };

3196

3198

3201 HandlerSet;

3204 E = CatchSwitch->handler_end();

3205 I != E; ++I) {

3207 if (DTU)

3208 ++NumPerSuccessorCases[HandlerBB];

3209 auto *CatchPad = cast(HandlerBB->getFirstNonPHI());

3210 if (!HandlerSet.insert({CatchPad, Empty}).second) {

3211 if (DTU)

3212 --NumPerSuccessorCases[HandlerBB];

3213 CatchSwitch->removeHandler(I);

3214 --I;

3215 --E;

3216 Changed = true;

3217 }

3218 }

3219 if (DTU) {

3220 std::vectorDominatorTree::UpdateType Updates;

3221 for (const std::pair<BasicBlock *, int> &I : NumPerSuccessorCases)

3222 if (I.second == 0)

3223 Updates.push_back({DominatorTree::Delete, BB, I.first});

3224 DTU->applyUpdates(Updates);

3225 }

3226 }

3227

3232 } while (!Worklist.empty());

3233 return Changed;

3234}

3235

3238

3239 if (auto *II = dyn_cast(TI))

3241

3244

3245 if (auto *CRI = dyn_cast(TI)) {

3247 UnwindDest = CRI->getUnwindDest();

3248 } else if (auto *CatchSwitch = dyn_cast(TI)) {

3250 CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(),

3251 CatchSwitch->getName(), CatchSwitch->getIterator());

3252 for (BasicBlock *PadBB : CatchSwitch->handlers())

3253 NewCatchSwitch->addHandler(PadBB);

3254

3255 NewTI = NewCatchSwitch;

3256 UnwindDest = CatchSwitch->getUnwindDest();

3257 } else {

3259 }

3260

3266 if (DTU)

3267 DTU->applyUpdates({{DominatorTree::Delete, BB, UnwindDest}});

3268 return NewTI;

3269}

3270

3271

3272

3273

3278

3279

3280 if (Reachable.size() == F.size())

3281 return Changed;

3282

3284

3285

3288

3289 if (Reachable.count(&BB))

3290 continue;

3291

3293 continue;

3294 BlocksToRemove.insert(&BB);

3295 }

3296

3297 if (BlocksToRemove.empty())

3298 return Changed;

3299

3300 Changed = true;

3301 NumRemoved += BlocksToRemove.size();

3302

3303 if (MSSAU)

3305

3307

3308 return Changed;

3309}

3310

3311

3312

3314 bool DoesKMove, bool AAOnly = false) {

3316 K->getAllMetadataOtherThanDebugLoc(Metadata);

3317 for (const auto &MD : Metadata) {

3318 unsigned Kind = MD.first;

3320 MDNode *KMD = MD.second;

3321

3322

3323 switch (Kind) {

3324 default:

3325 K->setMetadata(Kind, nullptr);

3326 break;

3327 case LLVMContext::MD_dbg:

3328 llvm_unreachable("getAllMetadataOtherThanDebugLoc returned a MD_dbg");

3329 case LLVMContext::MD_DIAssignID:

3330 if (!AAOnly)

3331 K->mergeDIAssignID(J);

3332 break;

3333 case LLVMContext::MD_tbaa:

3334 if (DoesKMove)

3336 break;

3337 case LLVMContext::MD_alias_scope:

3338 if (DoesKMove)

3340 break;

3341 case LLVMContext::MD_noalias:

3342 case LLVMContext::MD_mem_parallel_loop_access:

3343 if (DoesKMove)

3345 break;

3346 case LLVMContext::MD_access_group:

3347 if (DoesKMove)

3348 K->setMetadata(LLVMContext::MD_access_group,

3350 break;

3351 case LLVMContext::MD_range:

3352 if (!AAOnly && (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef)))

3354 break;

3355 case LLVMContext::MD_fpmath:

3356 if (!AAOnly)

3358 break;

3359 case LLVMContext::MD_invariant_load:

3360

3361

3362 if (DoesKMove)

3363 K->setMetadata(Kind, JMD);

3364 break;

3365 case LLVMContext::MD_nonnull:

3366 if (!AAOnly && (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef)))

3367 K->setMetadata(Kind, JMD);

3368 break;

3369 case LLVMContext::MD_invariant_group:

3370

3371 break;

3372 case LLVMContext::MD_mmra:

3373

3374 break;

3375 case LLVMContext::MD_align:

3376 if (!AAOnly && (DoesKMove || !K->hasMetadata(LLVMContext::MD_noundef)))

3377 K->setMetadata(

3379 break;

3380 case LLVMContext::MD_dereferenceable:

3381 case LLVMContext::MD_dereferenceable_or_null:

3382 if (!AAOnly && DoesKMove)

3383 K->setMetadata(Kind,

3385 break;

3386 case LLVMContext::MD_memprof:

3387 if (!AAOnly)

3389 break;

3390 case LLVMContext::MD_callsite:

3391 if (!AAOnly)

3393 break;

3394 case LLVMContext::MD_preserve_access_index:

3395

3396 break;

3397 case LLVMContext::MD_noundef:

3398

3399 if (!AAOnly && DoesKMove)

3400 K->setMetadata(Kind, JMD);

3401 break;

3402 case LLVMContext::MD_nontemporal:

3403

3404 if (!AAOnly)

3405 K->setMetadata(Kind, JMD);

3406 break;

3407 case LLVMContext::MD_prof:

3408 if (!AAOnly && DoesKMove)

3410 break;

3411 case LLVMContext::MD_noalias_addrspace:

3412 if (DoesKMove)

3413 K->setMetadata(Kind,

3415 break;

3416 }

3417 }

3418

3419

3420

3421

3422

3423

3424 if (auto *JMD = J->getMetadata(LLVMContext::MD_invariant_group))

3425 if (isa(K) || isa(K))

3426 K->setMetadata(LLVMContext::MD_invariant_group, JMD);

3427

3428

3429

3430

3431 auto JMMRA = J->getMetadata(LLVMContext::MD_mmra);

3432 auto KMMRA = K->getMetadata(LLVMContext::MD_mmra);

3433 if (JMMRA || KMMRA) {

3434 K->setMetadata(LLVMContext::MD_mmra,

3436 }

3437}

3438

3440 bool DoesKMove) {

3442}

3443

3445 combineMetadata(K, J, true, true);

3446}

3447

3450 Source.getAllMetadata(MD);

3453 const DataLayout &DL = Source.getDataLayout();

3454 for (const auto &MDPair : MD) {

3455 unsigned ID = MDPair.first;

3456 MDNode *N = MDPair.second;

3457

3458

3459

3460

3461

3462

3463

3464 switch (ID) {

3465 case LLVMContext::MD_dbg:

3466 case LLVMContext::MD_tbaa:

3467 case LLVMContext::MD_prof:

3468 case LLVMContext::MD_fpmath:

3469 case LLVMContext::MD_tbaa_struct:

3470 case LLVMContext::MD_invariant_load:

3471 case LLVMContext::MD_alias_scope:

3472 case LLVMContext::MD_noalias:

3473 case LLVMContext::MD_nontemporal:

3474 case LLVMContext::MD_mem_parallel_loop_access:

3475 case LLVMContext::MD_access_group:

3476 case LLVMContext::MD_noundef:

3477

3479 break;

3480

3481 case LLVMContext::MD_nonnull:

3483 break;

3484

3485 case LLVMContext::MD_align:

3486 case LLVMContext::MD_dereferenceable:

3487 case LLVMContext::MD_dereferenceable_or_null:

3488

3491 break;

3492

3493 case LLVMContext::MD_range:

3495 break;

3496 }

3497 }

3498}

3499

3501 auto *ReplInst = dyn_cast(Repl);

3502 if (!ReplInst)

3503 return;

3504

3505

3506

3508

3509

3510 if (isa(ReplInst) &&

3513

3514

3515

3516

3517 else if (!isa(I))

3518 ReplInst->andIRFlags(I);

3519

3520

3521 if (auto *CB1 = dyn_cast(ReplInst)) {

3522 if (auto *CB2 = dyn_cast(I)) {

3523 bool Success = CB1->tryIntersectAttributes(CB2);

3524 assert(Success && "We should not be trying to sink callbases "

3525 "with non-intersectable attributes");

3526

3528 }

3529 }

3530

3531

3532

3533

3534

3535

3536

3537

3538

3539

3541}

3542

3543template <typename RootType, typename ShouldReplaceFn>

3545 const RootType &Root,

3546 const ShouldReplaceFn &ShouldReplace) {

3548

3549 unsigned Count = 0;

3551 auto *II = dyn_cast(U.getUser());

3552 if (II && II->getIntrinsicID() == Intrinsic::fake_use)

3553 continue;

3554 if (!ShouldReplace(Root, U))

3555 continue;

3557 From->printAsOperand(dbgs());

3558 dbgs() << "' with " << *To << " in " << *U.getUser() << "\n");

3559 U.set(To);

3560 ++Count;

3561 }

3562 return Count;

3563}

3564

3567 auto *BB = From->getParent();

3568 unsigned Count = 0;

3569

3571 auto *I = cast(U.getUser());

3572 if (I->getParent() == BB)

3573 continue;

3574 U.set(To);

3575 ++Count;

3576 }

3577 return Count;

3578}

3579

3583 auto Dominates = [&DT](const BasicBlockEdge &Root, const Use &U) {

3585 };

3586 return ::replaceDominatedUsesWith(From, To, Root, Dominates);

3587}

3588

3592 auto Dominates = [&DT](const BasicBlock *BB, const Use &U) {

3594 };

3595 return ::replaceDominatedUsesWith(From, To, BB, Dominates);

3596}

3597

3601 auto DominatesAndShouldReplace =

3602 [&DT, &ShouldReplace, To](const BasicBlockEdge &Root, const Use &U) {

3603 return DT.dominates(Root, U) && ShouldReplace(U, To);

3604 };

3605 return ::replaceDominatedUsesWith(From, To, Root, DominatesAndShouldReplace);

3606}

3607

3611 auto DominatesAndShouldReplace = [&DT, &ShouldReplace,

3613 return DT.dominates(BB, U) && ShouldReplace(U, To);

3614 };

3615 return ::replaceDominatedUsesWith(From, To, BB, DominatesAndShouldReplace);

3616}

3617

3620

3621 if (Call->hasFnAttr("gc-leaf-function"))

3622 return true;

3623 if (const Function *F = Call->getCalledFunction()) {

3624 if (F->hasFnAttribute("gc-leaf-function"))

3625 return true;

3626

3627 if (auto IID = F->getIntrinsicID()) {

3628

3629 return IID != Intrinsic::experimental_gc_statepoint &&

3630 IID != Intrinsic::experimental_deoptimize &&

3631 IID != Intrinsic::memcpy_element_unordered_atomic &&

3632 IID != Intrinsic::memmove_element_unordered_atomic;

3633 }

3634 }

3635

3636

3637

3638

3641 return TLI.has(LF);

3642 }

3643

3644 return false;

3645}

3646

3649 auto *NewTy = NewLI.getType();

3650

3651

3652 if (NewTy->isPointerTy()) {

3653 NewLI.setMetadata(LLVMContext::MD_nonnull, N);

3654 return;

3655 }

3656

3657

3658

3659 if (!NewTy->isIntegerTy())

3660 return;

3661

3664 auto *ITy = cast(NewTy);

3668 NewLI.setMetadata(LLVMContext::MD_range,

3670}

3671

3674 auto *NewTy = NewLI.getType();

3675

3676 if (NewTy == OldLI.getType()) {

3677 NewLI.setMetadata(LLVMContext::MD_range, N);

3678 return;

3679 }

3680

3681

3682

3683

3684

3685 if (!NewTy->isPointerTy())

3686 return;

3687

3688 unsigned BitWidth = DL.getPointerTypeSizeInBits(NewTy);

3692 NewLI.setMetadata(LLVMContext::MD_nonnull, NN);

3693 }

3694}

3695

3700 for (auto *DII : DbgUsers)

3702 for (auto *DVR : DPUsers)

3703 DVR->eraseFromParent();

3704}

3705

3708

3709

3710

3711

3712

3713

3714

3715

3716

3717

3718

3719

3720

3721

3722

3723

3724

3725

3726

3727

3728

3729

3732 I->dropUBImplyingAttrsAndMetadata();

3733 if (I->isUsedByMetadata())

3735

3736 I->dropDbgRecords();

3737 if (I->isDebugOrPseudoInst()) {

3738

3739 II = I->eraseFromParent();

3740 continue;

3741 }

3743 ++II;

3744 }

3747}

3748

3751

3752 auto createIntegerExpression = [&DIB](const Constant &CV) -> DIExpression * {

3753 const APInt &API = cast(&CV)->getValue();

3754 std::optional<int64_t> InitIntOpt = API.trySExtValue();

3756 static_cast<uint64_t>(*InitIntOpt))

3757 : nullptr;

3758 };

3759

3760 if (isa(C))

3761 return createIntegerExpression(C);

3762

3763 auto *FP = dyn_cast(&C);

3765 const APFloat &APF = FP->getValueAPF();

3770 }

3771

3773 return nullptr;

3774

3775 if (isa(C))

3777

3778 if (const ConstantExpr *CE = dyn_cast(&C))

3779 if (CE->getOpcode() == Instruction::IntToPtr) {

3780 const Value *V = CE->getOperand(0);

3781 if (auto CI = dyn_cast_or_null(V))

3782 return createIntegerExpression(*CI);

3783 }

3784 return nullptr;

3785}

3786

3788 auto RemapDebugOperands = [&Mapping](auto *DV, auto Set) {

3789 for (auto *Op : Set) {

3790 auto I = Mapping.find(Op);

3791 if (I != Mapping.end())

3792 DV->replaceVariableLocationOp(Op, I->second, true);

3793 }

3794 };

3795 auto RemapAssignAddress = [&Mapping](auto *DA) {

3796 auto I = Mapping.find(DA->getAddress());

3797 if (I != Mapping.end())

3798 DA->setAddress(I->second);

3799 };

3800 if (auto DVI = dyn_cast(Inst))

3801 RemapDebugOperands(DVI, DVI->location_ops());

3802 if (auto DAI = dyn_cast(Inst))

3803 RemapAssignAddress(DAI);

3805 RemapDebugOperands(&DVR, DVR.location_ops());

3806 if (DVR.isDbgAssign())

3807 RemapAssignAddress(&DVR);

3808 }

3809}

3810

3811namespace {

3812

3813

3814

3815struct BitPart {

3816 BitPart(Value *P, unsigned BW) : Provider(P) {

3818 }

3819

3820

3821 Value *Provider;

3822

3823

3824

3826

3827 enum { Unset = -1 };

3828};

3829

3830}

3831

3832

3833

3834

3835

3836

3837

3838

3839

3840

3841

3842

3843

3844

3845

3846

3847

3848

3849

3850

3851

3852

3853

3854

3855

3856

3857

3858

3859static const std::optional &

3861 std::map<Value *, std::optional> &BPS, int Depth,

3862 bool &FoundRoot) {

3863 auto [I, Inserted] = BPS.try_emplace(V);

3864 if (!Inserted)

3865 return I->second;

3866

3867 auto &Result = I->second;

3868 auto BitWidth = V->getType()->getScalarSizeInBits();

3869

3870

3872 return Result;

3873

3874

3876 LLVM_DEBUG(dbgs() << "collectBitParts max recursion depth reached.\n");

3877 return Result;

3878 }

3879

3880 if (auto *I = dyn_cast(V)) {

3883

3884

3886

3887 const auto &A = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,

3888 Depth + 1, FoundRoot);

3889 if (A || A->Provider)

3890 return Result;

3891

3892 const auto &B = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS,

3893 Depth + 1, FoundRoot);

3894 if (B || A->Provider != B->Provider)

3895 return Result;

3896

3897

3898 Result = BitPart(A->Provider, BitWidth);

3899 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx) {

3900 if (A->Provenance[BitIdx] != BitPart::Unset &&

3901 B->Provenance[BitIdx] != BitPart::Unset &&

3902 A->Provenance[BitIdx] != B->Provenance[BitIdx])

3903 return Result = std::nullopt;

3904

3905 if (A->Provenance[BitIdx] == BitPart::Unset)

3906 Result->Provenance[BitIdx] = B->Provenance[BitIdx];

3907 else

3908 Result->Provenance[BitIdx] = A->Provenance[BitIdx];

3909 }

3910

3911 return Result;

3912 }

3913

3914

3916 const APInt &BitShift = *C;

3917

3918

3920 return Result;

3921

3922

3923 if (!MatchBitReversals && (BitShift.getZExtValue() % 8) != 0)

3924 return Result;

3925

3926 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,

3927 Depth + 1, FoundRoot);

3928 if (!Res)

3929 return Result;

3930 Result = Res;

3931

3932

3933 auto &P = Result->Provenance;

3934 if (I->getOpcode() == Instruction::Shl) {

3935 P.erase(std::prev(P.end(), BitShift.getZExtValue()), P.end());

3936 P.insert(P.begin(), BitShift.getZExtValue(), BitPart::Unset);

3937 } else {

3938 P.erase(P.begin(), std::next(P.begin(), BitShift.getZExtValue()));

3939 P.insert(P.end(), BitShift.getZExtValue(), BitPart::Unset);

3940 }

3941

3942 return Result;

3943 }

3944

3945

3946

3948 const APInt &AndMask = *C;

3949

3950

3951

3952 unsigned NumMaskedBits = AndMask.popcount();

3953 if (!MatchBitReversals && (NumMaskedBits % 8) != 0)

3954 return Result;

3955

3956 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,

3957 Depth + 1, FoundRoot);

3958 if (!Res)

3959 return Result;

3960 Result = Res;

3961

3962 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)

3963

3964 if (AndMask[BitIdx] == 0)

3965 Result->Provenance[BitIdx] = BitPart::Unset;

3966 return Result;

3967 }

3968

3969

3971 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,

3972 Depth + 1, FoundRoot);

3973 if (!Res)

3974 return Result;

3975

3976 Result = BitPart(Res->Provider, BitWidth);

3977 auto NarrowBitWidth = X->getType()->getScalarSizeInBits();

3978 for (unsigned BitIdx = 0; BitIdx < NarrowBitWidth; ++BitIdx)

3979 Result->Provenance[BitIdx] = Res->Provenance[BitIdx];

3980 for (unsigned BitIdx = NarrowBitWidth; BitIdx < BitWidth; ++BitIdx)

3981 Result->Provenance[BitIdx] = BitPart::Unset;

3982 return Result;

3983 }

3984

3985

3987 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,

3988 Depth + 1, FoundRoot);

3989 if (!Res)

3990 return Result;

3991

3992 Result = BitPart(Res->Provider, BitWidth);

3993 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)

3994 Result->Provenance[BitIdx] = Res->Provenance[BitIdx];

3995 return Result;

3996 }

3997

3998

3999

4001 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,

4002 Depth + 1, FoundRoot);

4003 if (!Res)

4004 return Result;

4005

4006 Result = BitPart(Res->Provider, BitWidth);

4007 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)

4008 Result->Provenance[(BitWidth - 1) - BitIdx] = Res->Provenance[BitIdx];

4009 return Result;

4010 }

4011

4012

4014 const auto &Res = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,

4015 Depth + 1, FoundRoot);

4016 if (!Res)

4017 return Result;

4018

4019 unsigned ByteWidth = BitWidth / 8;

4020 Result = BitPart(Res->Provider, BitWidth);

4021 for (unsigned ByteIdx = 0; ByteIdx < ByteWidth; ++ByteIdx) {

4022 unsigned ByteBitOfs = ByteIdx * 8;

4023 for (unsigned BitIdx = 0; BitIdx < 8; ++BitIdx)

4024 Result->Provenance[(BitWidth - 8 - ByteBitOfs) + BitIdx] =

4025 Res->Provenance[ByteBitOfs + BitIdx];

4026 }

4027 return Result;

4028 }

4029

4030

4031

4032

4033

4036

4037 unsigned ModAmt = C->urem(BitWidth);

4038 if (cast(I)->getIntrinsicID() == Intrinsic::fshr)

4040

4041

4042 if (!MatchBitReversals && (ModAmt % 8) != 0)

4043 return Result;

4044

4045

4046 const auto &LHS = collectBitParts(X, MatchBSwaps, MatchBitReversals, BPS,

4047 Depth + 1, FoundRoot);

4048 if (LHS || LHS->Provider)

4049 return Result;

4050

4051 const auto &RHS = collectBitParts(Y, MatchBSwaps, MatchBitReversals, BPS,

4052 Depth + 1, FoundRoot);

4053 if (RHS || LHS->Provider != RHS->Provider)

4054 return Result;

4055

4056 unsigned StartBitRHS = BitWidth - ModAmt;

4057 Result = BitPart(LHS->Provider, BitWidth);

4058 for (unsigned BitIdx = 0; BitIdx < StartBitRHS; ++BitIdx)

4059 Result->Provenance[BitIdx + ModAmt] = LHS->Provenance[BitIdx];

4060 for (unsigned BitIdx = 0; BitIdx < ModAmt; ++BitIdx)

4061 Result->Provenance[BitIdx] = RHS->Provenance[BitIdx + StartBitRHS];

4062 return Result;

4063 }

4064 }

4065

4066

4067

4068 if (FoundRoot)

4069 return Result;

4070

4071

4072

4073 FoundRoot = true;

4074 Result = BitPart(V, BitWidth);

4075 for (unsigned BitIdx = 0; BitIdx < BitWidth; ++BitIdx)

4076 Result->Provenance[BitIdx] = BitIdx;

4077 return Result;

4078}

4079

4082 if (From % 8 != To % 8)

4083 return false;

4084

4086 To >>= 3;

4089}

4090

4094}

4095

4097 Instruction *I, bool MatchBSwaps, bool MatchBitReversals,

4103 return false;

4104 if (!MatchBSwaps && !MatchBitReversals)

4105 return false;

4106 Type *ITy = I->getType();

4108 return false;

4109

4110

4111 bool FoundRoot = false;

4112 std::map<Value *, std::optional> BPS;

4113 const auto &Res =

4114 collectBitParts(I, MatchBSwaps, MatchBitReversals, BPS, 0, FoundRoot);

4115 if (!Res)

4116 return false;

4119 [](int8_t I) { return I == BitPart::Unset || 0 <= I; }) &&

4120 "Illegal bit provenance index");

4121

4122

4123 Type *DemandedTy = ITy;

4124 if (BitProvenance.back() == BitPart::Unset) {

4125 while (!BitProvenance.empty() && BitProvenance.back() == BitPart::Unset)

4126 BitProvenance = BitProvenance.drop_back();

4127 if (BitProvenance.empty())

4128 return false;

4130 if (auto *IVecTy = dyn_cast(ITy))

4131 DemandedTy = VectorType::get(DemandedTy, IVecTy);

4132 }

4133

4134

4137 return false;

4138

4139

4140

4142 bool OKForBSwap = MatchBSwaps && (DemandedBW % 16) == 0;

4143 bool OKForBitReverse = MatchBitReversals;

4144 for (unsigned BitIdx = 0;

4145 (BitIdx < DemandedBW) && (OKForBSwap || OKForBitReverse); ++BitIdx) {

4146 if (BitProvenance[BitIdx] == BitPart::Unset) {

4147 DemandedMask.clearBit(BitIdx);

4148 continue;

4149 }

4151 DemandedBW);

4153 BitIdx, DemandedBW);

4154 }

4155

4157 if (OKForBSwap)

4158 Intrin = Intrinsic::bswap;

4159 else if (OKForBitReverse)

4160 Intrin = Intrinsic::bitreverse;

4161 else

4162 return false;

4163

4166 Value *Provider = Res->Provider;

4167

4168

4169 if (DemandedTy != Provider->getType()) {

4170 auto *Trunc =

4173 Provider = Trunc;

4174 }

4175

4177 InsertedInsts.push_back(Result);

4178

4179 if (!DemandedMask.isAllOnes()) {

4180 auto *Mask = ConstantInt::get(DemandedTy, DemandedMask);

4181 Result = BinaryOperator::Create(Instruction::And, Result, Mask, "mask", I->getIterator());

4182 InsertedInsts.push_back(Result);

4183 }

4184

4185

4186 if (ITy != Result->getType()) {

4188 InsertedInsts.push_back(ExtInst);

4189 }

4190

4191 return true;

4192}

4193

4194

4195

4196

4197

4198

4203 if (F && F->hasLocalLinkage() && F->hasName() &&

4205 F->doesNotAccessMemory())

4206 CI->addFnAttr(Attribute::NoBuiltin);

4207}

4208

4210

4211 if (I->getOperand(OpIdx)->getType()->isMetadataTy())

4212 return false;

4213

4214

4215 if (!isa(I->getOperand(OpIdx)))

4216 return true;

4217

4218 switch (I->getOpcode()) {

4219 default:

4220 return true;

4221 case Instruction::Call:

4222 case Instruction::Invoke: {

4223 const auto &CB = cast(*I);

4224

4225

4226 if (CB.isInlineAsm())

4227 return false;

4228

4229

4230

4231 if (CB.isBundleOperand(OpIdx))

4232 return false;

4233

4234 if (OpIdx < CB.arg_size()) {

4235

4236

4237 if (isa(CB) &&

4238 OpIdx >= CB.getFunctionType()->getNumParams()) {

4239

4240 return CB.getIntrinsicID() == Intrinsic::experimental_stackmap;

4241 }

4242

4243

4244

4245 if (CB.getIntrinsicID() == Intrinsic::gcroot)

4246 return false;

4247

4248

4249 return !CB.paramHasAttr(OpIdx, Attribute::ImmArg);

4250 }

4251

4252

4253

4254 return !isa(CB);

4255 }

4256 case Instruction::ShuffleVector:

4257

4258 return OpIdx != 2;

4259 case Instruction::Switch:

4260 case Instruction::ExtractValue:

4261

4262 return OpIdx == 0;

4263 case Instruction::InsertValue:

4264

4265 return OpIdx < 2;

4266 case Instruction::Alloca:

4267

4268

4269

4270 return !cast(I)->isStaticAlloca();

4271 case Instruction::GetElementPtr:

4272 if (OpIdx == 0)

4273 return true;

4275 for (auto E = std::next(It, OpIdx); It != E; ++It)

4277 return false;

4278 return true;

4279 }

4280}

4281

4283

4284 if (Constant *C = dyn_cast(Condition))

4286

4287

4288 Value *NotCondition;

4290 return NotCondition;

4291

4293 Instruction *Inst = dyn_cast(Condition);

4294 if (Inst)

4296 else if (Argument *Arg = dyn_cast(Condition))

4298 assert(Parent && "Unsupported condition to invert");

4299

4300

4301 for (User *U : Condition->users())

4302 if (Instruction *I = dyn_cast(U))

4304 return I;

4305

4306

4307 auto *Inverted =

4309 if (Inst && !isa(Inst))

4310 Inverted->insertAfter(Inst);

4311 else

4313 return Inverted;

4314}

4315

4317

4318

4319

4320 bool Changed = false;

4321

4322 if (F.hasFnAttribute(Attribute::NoSync) &&

4323 F.doesNotAccessMemory() && F.isConvergent()) {

4324 F.setNoSync();

4325 Changed = true;

4326 }

4327

4328

4329 if (F.hasFnAttribute(Attribute::NoFree) && F.onlyReadsMemory()) {

4330 F.setDoesNotFreeMemory();

4331 Changed = true;

4332 }

4333

4334

4335 if (F.hasFnAttribute(Attribute::MustProgress) && F.willReturn()) {

4336 F.setMustProgress();

4337 Changed = true;

4338 }

4339

4340

4341

4342

4343 return Changed;

4344}

for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))

static unsigned getIntrinsicID(const SDNode *N)

This file implements a class to represent arbitrary precision integral constant values and operations...

ReachingDefAnalysis InstSet & ToRemove

MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL

static bool isEqual(const Function &Caller, const Function &Callee)

This file contains the simple types necessary to represent the attributes associated with functions a...

static const Function * getParent(const Value *V)

BlockVerifier::State From

static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")

static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")

This file contains the declarations for the subclasses of Constant, which represent the different fla...

static bool isSentinel(const DWARFDebugNames::AttributeEncoding &AE)

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

This file defines DenseMapInfo traits for DenseMap.

This file defines the DenseMap class.

This file defines the DenseSet and SmallDenseSet classes.

This file contains constants used for implementing Dwarf debug support.

static unsigned getHashValueImpl(SimpleValue Val)

static bool isEqualImpl(SimpleValue LHS, SimpleValue RHS)

static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")

This file provides various utilities for inspecting and working with the control flow graph in LLVM I...

Module.h This file contains the declarations for the Module class.

This defines the Use class.

iv Induction Variable Users

This file provides utility for Memory Model Relaxation Annotations (MMRAs).

uint64_t IntrinsicInst * II

static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")

This file contains the declarations for profiling metadata utility functions.

const SmallVectorImpl< MachineOperand > & Cond

Remove Loads Into Fake Uses

assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())

This file implements a set that has insertion order iteration characteristics.

This file defines the SmallPtrSet class.

This file defines the SmallVector class.

This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...

#define STATISTIC(VARNAME, DESC)

static bool markAliveBlocks(Function &F, SmallPtrSetImpl< BasicBlock * > &Reachable, DomTreeUpdater *DTU=nullptr)

static bool valueCoversEntireFragment(Type *ValTy, DbgVariableIntrinsic *DII)

Check if the alloc size of ValTy is large enough to cover the variable (or fragment of the variable) ...

static bool isBitCastSemanticsPreserving(const DataLayout &DL, Type *FromTy, Type *ToTy)

Check if a bitcast between a value of type FromTy to type ToTy would losslessly preserve the bits and...

static bool isStructure(AllocaInst *AI)

Determine whether this alloca is a structure.

uint64_t getDwarfOpForBinOp(Instruction::BinaryOps Opcode)

static bool PhiHasDebugValue(DILocalVariable *DIVar, DIExpression *DIExpr, PHINode *APN)

===------------------------------------------------------------------—===// Dbg Intrinsic utilities

static void gatherIncomingValuesToPhi(PHINode *PN, IncomingValueMap &IncomingValues)

Create a map from block to value for the operands of a given phi.

static void combineMetadata(Instruction *K, const Instruction *J, bool DoesKMove, bool AAOnly=false)

If AAOnly is set, only intersect alias analysis metadata and preserve other known metadata.

static void handleSSAValueOperands(uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Opcodes, SmallVectorImpl< Value * > &AdditionalValues, Instruction *I)

std::optional< DIExpression * > DbgValReplacement

A replacement for a dbg.value expression.

static void insertDbgValueOrDbgVariableRecordAfter(DIBuilder &Builder, Value *DV, DILocalVariable *DIVar, DIExpression *DIExpr, const DebugLoc &NewLoc, BasicBlock::iterator Instr)

Value * getSalvageOpsForBinOp(BinaryOperator *BI, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Opcodes, SmallVectorImpl< Value * > &AdditionalValues)

static DIExpression * dropInitialDeref(const DIExpression *DIExpr)

static void replaceUndefValuesInPhi(PHINode *PN, const IncomingValueMap &IncomingValues)

Replace the incoming undef values to a phi with the values from a block-to-value map.

Value * getSalvageOpsForGEP(GetElementPtrInst *GEP, const DataLayout &DL, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Opcodes, SmallVectorImpl< Value * > &AdditionalValues)

static bool CanRedirectPredsOfEmptyBBToSucc(BasicBlock *BB, BasicBlock *Succ, const SmallPtrSetImpl< BasicBlock * > &BBPreds, BasicBlock *&CommonPred)

Value * getSalvageOpsForIcmpOp(ICmpInst *Icmp, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Opcodes, SmallVectorImpl< Value * > &AdditionalValues)

static bool CanMergeValues(Value *First, Value *Second)

Return true if we can choose one of these values to use in place of the other.

static bool simplifyAndDCEInstruction(Instruction *I, SmallSetVector< Instruction *, 16 > &WorkList, const DataLayout &DL, const TargetLibraryInfo *TLI)

static bool isArray(AllocaInst *AI)

Determine whether this alloca is either a VLA or an array.

static bool areAllUsesEqual(Instruction *I)

areAllUsesEqual - Check whether the uses of a value are all the same.

static cl::opt< bool > PHICSEDebugHash("phicse-debug-hash", cl::init(false), cl::Hidden, cl::desc("Perform extra assertion checking to verify that PHINodes's hash " "function is well-behaved w.r.t. its isEqual predicate"))

static unsigned replaceDominatedUsesWith(Value *From, Value *To, const RootType &Root, const ShouldReplaceFn &ShouldReplace)

uint64_t getDwarfOpForIcmpPred(CmpInst::Predicate Pred)

static bool bitTransformIsCorrectForBSwap(unsigned From, unsigned To, unsigned BitWidth)

static const std::optional< BitPart > & collectBitParts(Value *V, bool MatchBSwaps, bool MatchBitReversals, std::map< Value *, std::optional< BitPart > > &BPS, int Depth, bool &FoundRoot)

Analyze the specified subexpression and see if it is capable of providing pieces of a bswap or bitrev...

static bool EliminateDuplicatePHINodesNaiveImpl(BasicBlock *BB, SmallPtrSetImpl< PHINode * > &ToRemove)

static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ, const SmallPtrSetImpl< BasicBlock * > &BBPreds)

Return true if we can fold BB, an almost-empty BB ending in an unconditional branch to Succ,...

static cl::opt< unsigned > PHICSENumPHISmallSize("phicse-num-phi-smallsize", cl::init(32), cl::Hidden, cl::desc("When the basic block contains not more than this number of PHI nodes, " "perform a (faster!) exhaustive search instead of set-driven one."))

static bool EliminateDuplicatePHINodesSetBasedImpl(BasicBlock *BB, SmallPtrSetImpl< PHINode * > &ToRemove)

static bool rewriteDebugUsers(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT, function_ref< DbgValReplacement(DbgVariableIntrinsic &DII)> RewriteExpr, function_ref< DbgValReplacement(DbgVariableRecord &DVR)> RewriteDVRExpr)

Point debug users of From to To using exprs given by RewriteExpr, possibly moving/undefing users to p...

static void insertDbgValueOrDbgVariableRecord(DIBuilder &Builder, Value *DV, DILocalVariable *DIVar, DIExpression *DIExpr, const DebugLoc &NewLoc, BasicBlock::iterator Instr)

static bool introduceTooManyPhiEntries(BasicBlock *BB, BasicBlock *Succ)

Check whether removing BB will make the phis in its Succ have too many incoming entries.

static Value * selectIncomingValueForBlock(Value *OldVal, BasicBlock *BB, IncomingValueMap &IncomingValues)

Determines the value to use as the phi node input for a block.

static void updateOneDbgValueForAlloca(const DebugLoc &Loc, DILocalVariable *DIVar, DIExpression *DIExpr, Value *NewAddress, DbgValueInst *DVI, DbgVariableRecord *DVR, DIBuilder &Builder, int Offset)

static void insertDbgVariableRecordsForPHIs(BasicBlock *BB, SmallVectorImpl< PHINode * > &InsertedPHIs)

static const unsigned BitPartRecursionMaxDepth

static void redirectValuesFromPredecessorsToPhi(BasicBlock *BB, const PredBlockVector &BBPreds, PHINode *PN, BasicBlock *CommonPred)

Replace a value flowing from a block to a phi with potentially multiple instances of that value flowi...

static cl::opt< unsigned > MaxPhiEntriesIncreaseAfterRemovingEmptyBlock("max-phi-entries-increase-after-removing-empty-block", cl::init(1000), cl::Hidden, cl::desc("Stop removing an empty block if removing it will introduce more " "than this number of phi entries in its successor"))

cl::opt< bool > UseNewDbgInfoFormat

static bool bitTransformIsCorrectForBitReverse(unsigned From, unsigned To, unsigned BitWidth)

static void salvageDbgAssignAddress(T *Assign)

APInt bitcastToAPInt() const

Class for arbitrary precision integers.

static APInt getAllOnes(unsigned numBits)

Return an APInt of a specified width with all bits set.

void clearBit(unsigned BitPosition)

Set a given bit to 0.

uint64_t getZExtValue() const

Get zero extended value.

unsigned popcount() const

Count the number of bits set.

bool isAllOnes() const

Determine if all bits are set. This is true for zero-width values.

const uint64_t * getRawData() const

This function returns a pointer to the internal storage of the APInt.

std::optional< int64_t > trySExtValue() const

Get sign extended value if possible.

int64_t getSExtValue() const

Get sign extended value.

bool uge(const APInt &RHS) const

Unsigned greater or equal comparison.

an instruction to allocate memory on the stack

Type * getAllocatedType() const

Return the type that is being allocated by the instruction.

bool isArrayAllocation() const

Return true if there is an allocation size parameter to the allocation instruction that is not 1.

This class represents an incoming formal argument to a Function.

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

const T & back() const

back - Get the last element.

ArrayRef< T > drop_front(size_t N=1) const

Drop the first N elements of the array.

size_t size() const

size - Get the array size.

ArrayRef< T > drop_back(size_t N=1) const

Drop the last N elements of the array.

bool empty() const

empty - Check if the array is empty.

Value handle that asserts if the Value is deleted.

A cache of @llvm.assume calls within a function.

LLVM Basic Block Representation.

iterator begin()

Instruction iterator methods.

iterator_range< const_phi_iterator > phis() const

Returns a range that iterates over the phis in the basic block.

const_iterator getFirstInsertionPt() const

Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...

bool hasAddressTaken() const

Returns true if there are any uses of this basic block other than direct branches,...

InstListType::const_iterator getFirstNonPHIIt() const

Iterator returning form of getFirstNonPHI.

void insertDbgRecordBefore(DbgRecord *DR, InstListType::iterator Here)

Insert a DbgRecord into a block at the position given by Here.

const Instruction * getFirstNonPHI() const

Returns a pointer to the first instruction in this block that is not a PHINode instruction.

const Instruction & front() const

static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)

Creates a new BasicBlock.

bool isEntryBlock() const

Return true if this is the entry block of the containing function.

void moveAfter(BasicBlock *MovePos)

Unlink this basic block from its current function and insert it right after MovePos in the function M...

bool hasNPredecessors(unsigned N) const

Return true if this block has exactly N predecessors.

const BasicBlock * getSinglePredecessor() const

Return the predecessor of this block if it has a single predecessor block.

void flushTerminatorDbgRecords()

Eject any debug-info trailing at the end of a block.

const Function * getParent() const

Return the enclosing method, or null if none.

const DataLayout & getDataLayout() const

Get the data layout of the module this basic block belongs to.

SymbolTableList< BasicBlock >::iterator eraseFromParent()

Unlink 'this' from the containing function and delete it.

const Instruction * getFirstNonPHIOrDbg(bool SkipPseudoOp=true) const

Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic,...

InstListType::iterator iterator

Instruction iterators...

LLVMContext & getContext() const

Get the context in which this basic block lives.

const Instruction * getTerminator() const LLVM_READONLY

Returns the terminator instruction if the block is well formed or null if the block is not well forme...

bool hasNPredecessorsOrMore(unsigned N) const

Return true if this block has N predecessors or more.

void splice(BasicBlock::iterator ToIt, BasicBlock *FromBB)

Transfer all instructions from FromBB to this basic block at ToIt.

const Instruction & back() const

void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)

Update PHI nodes in this BasicBlock before removal of predecessor Pred.

BinaryOps getOpcode() const

static BinaryOperator * CreateNot(Value *Op, const Twine &Name="", InsertPosition InsertBefore=nullptr)

static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)

Construct a binary instruction, given the opcode and the two operands.

This class represents a no-op cast from one type to another.

The address of a basic block.

static BlockAddress * get(Function *F, BasicBlock *BB)

Return a BlockAddress for the specified function and basic block.

Conditional or Unconditional Branch instruction.

static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)

Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...

void setCallingConv(CallingConv::ID CC)

void addFnAttr(Attribute::AttrKind Kind)

Adds the attribute to the function.

void getOperandBundlesAsDefs(SmallVectorImpl< OperandBundleDef > &Defs) const

Return the list of operand bundles attached to this instruction as a vector of OperandBundleDefs.

Function * getCalledFunction() const

Returns the function called, or null if this is an indirect function invocation or the function signa...

CallingConv::ID getCallingConv() const

Value * getCalledOperand() const

void setAttributes(AttributeList A)

Set the attributes for this call.

FunctionType * getFunctionType() const

iterator_range< User::op_iterator > args()

Iteration adapter for range-for loops.

AttributeList getAttributes() const

Return the attributes for this call.

This class represents a function call, abstracting a target machine's calling convention.

static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)

static CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", InsertPosition InsertBefore=nullptr)

Create a ZExt, BitCast, or Trunc for int -> int casts.

static CatchSwitchInst * Create(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumHandlers, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)

static CleanupReturnInst * Create(Value *CleanupPad, BasicBlock *UnwindBB=nullptr, InsertPosition InsertBefore=nullptr)

Predicate

This enumeration lists the possible predicates for CmpInst subclasses.

@ ICMP_SLT

signed less than

@ ICMP_SLE

signed less or equal

@ ICMP_UGE

unsigned greater or equal

@ ICMP_UGT

unsigned greater than

@ ICMP_SGT

signed greater than

@ ICMP_ULT

unsigned less than

@ ICMP_SGE

signed greater or equal

@ ICMP_ULE

unsigned less or equal

Predicate getPredicate() const

Return the predicate for this instruction.

A constant value that is initialized with an expression using other constant values.

static Constant * getIntToPtr(Constant *C, Type *Ty, bool OnlyIfReduced=false)

static Constant * getNot(Constant *C)

static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)

static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)

This is the shared class of boolean and integer constants.

static ConstantPointerNull * get(PointerType *T)

Static factory methods - Return objects of the specified value.

bool contains(const APInt &Val) const

Return true if the specified value is in the set.

This is an important base class in LLVM.

void destroyConstant()

Called if some element of this constant is no longer valid.

DIExpression * createConstantValueExpression(uint64_t Val)

Create an expression for a variable that does not have an address, but does have a constant value.

static DIExpression * append(const DIExpression *Expr, ArrayRef< uint64_t > Ops)

Append the opcodes Ops to DIExpr.

unsigned getNumElements() const

static ExtOps getExtOps(unsigned FromSize, unsigned ToSize, bool Signed)

Returns the ops for a zero- or sign-extension in a DIExpression.

static void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)

Append Ops with operations to apply the Offset.

static DIExpression * appendOpsToArg(const DIExpression *Expr, ArrayRef< uint64_t > Ops, unsigned ArgNo, bool StackValue=false)

Create a copy of Expr by appending the given list of Ops to each instance of the operand DW_OP_LLVM_a...

static std::optional< FragmentInfo > getFragmentInfo(expr_op_iterator Start, expr_op_iterator End)

Retrieve the details of this fragment expression.

DIExpression * foldConstantMath()

Try to shorten an expression with constant math operations that can be evaluated at compile time.

uint64_t getNumLocationOperands() const

Return the number of unique location operands referred to (via DW_OP_LLVM_arg) in this expression; th...

ArrayRef< uint64_t > getElements() const

std::optional< uint64_t > getActiveBits(DIVariable *Var)

Return the number of bits that have an active value, i.e.

uint64_t getElement(unsigned I) const

static DIExpression * prepend(const DIExpression *Expr, uint8_t Flags, int64_t Offset=0)

Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value or/and an ...

static DIExpression * appendExt(const DIExpression *Expr, unsigned FromSize, unsigned ToSize, bool Signed)

Append a zero- or sign-extension to Expr.

std::optional< DIBasicType::Signedness > getSignedness() const

Return the signedness of this variable's type, or std::nullopt if this type is neither signed nor uns...

This class represents an Operation in the Expression.

A parsed version of the target data layout string in and methods for querying it.

This represents the llvm.dbg.label instruction.

Instruction * MarkedInstr

Link back to the Instruction that owns this marker.

This represents the llvm.dbg.value instruction.

This is the common base class for debug info intrinsics for variables.

iterator_range< location_op_iterator > location_ops() const

Get the locations corresponding to the variable referenced by the debug info intrinsic.

void replaceVariableLocationOp(Value *OldValue, Value *NewValue, bool AllowEmpty=false)

Value * getVariableLocationOp(unsigned OpIdx) const

void setExpression(DIExpression *NewExpr)

DILocalVariable * getVariable() const

unsigned getNumVariableLocationOps() const

bool isAddressOfVariable() const

Does this describe the address of a local variable.

DIExpression * getExpression() const

Record of a variable value-assignment, aka a non instruction representation of the dbg....

bool isAddressOfVariable() const

Does this describe the address of a local variable.

DbgVariableRecord * clone() const

void setExpression(DIExpression *NewExpr)

DIExpression * getExpression() const

Value * getVariableLocationOp(unsigned OpIdx) const

DILocalVariable * getVariable() const

unsigned getNumVariableLocationOps() const

void replaceVariableLocationOp(Value *OldValue, Value *NewValue, bool AllowEmpty=false)

iterator_range< location_op_iterator > location_ops() const

Get the locations corresponding to the variable referenced by the debug info intrinsic.

DILocation * get() const

Get the underlying DILocation.

iterator find(const_arg_type_t< KeyT > Val)

size_type count(const_arg_type_t< KeyT > Val) const

Return 1 if the specified key is in the map, 0 otherwise.

std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)

Implements a dense probed hash-table based set.

void deleteBB(BasicBlock *DelBB)

Delete DelBB.

Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.

bool dominates(const BasicBlock *BB, const Use &U) const

Return true if the (end of the) basic block BB dominates the use U.

const BasicBlock & getEntryBlock() const

void applyUpdatesPermissive(ArrayRef< UpdateT > Updates)

Submit updates to all available trees.

void applyUpdates(ArrayRef< UpdateT > Updates)

Submit updates to all available trees.

bool hasDomTree() const

Returns true if it holds a DomTreeT.

bool isBBPendingDeletion(BasicBlockT *DelBB) const

Returns true if DelBB is awaiting deletion.

void recalculate(FuncT &F)

Notify DTU that the entry block was replaced.

an instruction for type-safe pointer arithmetic to access elements of arrays and structs

This instruction compares its operands according to the predicate given to the constructor.

Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")

BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)

Create a conditional 'br Cond, TrueDest, FalseDest' instruction.

BranchInst * CreateBr(BasicBlock *Dest)

Create an unconditional 'br label X' instruction.

This provides a uniform API for creating instructions and inserting them into a basic block: either a...

iterator_range< simple_ilist< DbgRecord >::iterator > getDbgRecordRange() const

Return a range over the DbgRecords attached to this instruction.

void insertBefore(Instruction *InsertPos)

Insert an unlinked instruction into a basic block immediately before the specified instruction.

const DebugLoc & getDebugLoc() const

Return the debug location for this node as a DebugLoc.

bool extractProfTotalWeight(uint64_t &TotalVal) const

Retrieve total raw weight values of a branch.

void moveAfter(Instruction *MovePos)

Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...

bool isEHPad() const

Return true if the instruction is a variety of EH-block.

InstListType::iterator eraseFromParent()

This method unlinks 'this' from the containing basic block and deletes it.

bool isIdenticalToWhenDefined(const Instruction *I, bool IntersectAttrs=false) const LLVM_READONLY

This is like isIdenticalTo, except that it ignores the SubclassOptionalData flags,...

MDNode * getMetadata(unsigned KindID) const

Get the metadata of given kind attached to this Instruction.

const Instruction * getNextNonDebugInstruction(bool SkipPseudoOp=false) const

Return a pointer to the next non-debug instruction in the same basic block as 'this',...

void setMetadata(unsigned KindID, MDNode *Node)

Set the metadata of the specified kind to the specified node.

void dropPoisonGeneratingFlags()

Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs.

void setDebugLoc(DebugLoc Loc)

Set the debug location information for this instruction.

void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())

Copy metadata from SrcInst to this instruction.

void dropDbgRecords()

Erase any DbgRecords attached to this instruction.

const DataLayout & getDataLayout() const

Get the data layout of the module this instruction belongs to.

A wrapper class for inspecting calls to intrinsic functions.

static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)

This is an important class for using LLVM in a threaded context.

An instruction for reading from memory.

Value * getPointerOperand()

MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)

Return metadata containing two branch weights.

MDNode * createRange(const APInt &Lo, const APInt &Hi)

Return metadata describing the range [Lo, Hi).

static MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)

static MDNode * getMergedCallsiteMetadata(MDNode *A, MDNode *B)

static MDNode * getMostGenericTBAA(MDNode *A, MDNode *B)

static MDNode * getMostGenericNoaliasAddrspace(MDNode *A, MDNode *B)

static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)

static MDNode * getMergedProfMetadata(MDNode *A, MDNode *B, const Instruction *AInstr, const Instruction *BInstr)

Merge !prof metadata from two instructions.

static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)

static MDNode * getMostGenericRange(MDNode *A, MDNode *B)

static MDNode * getMergedMemProfMetadata(MDNode *A, MDNode *B)

static MDNode * intersect(MDNode *A, MDNode *B)

LLVMContext & getContext() const

static MDNode * getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B)

This class implements a map that also provides access to all stored values in a deterministic order.

iterator find(const KeyT &Key)

std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)

std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)

void changeToUnreachable(const Instruction *I)

Instruction I will be changed to an unreachable.

void removeBlocks(const SmallSetVector< BasicBlock *, 8 > &DeadBlocks)

Remove all MemoryAcceses in a set of BasicBlocks about to be deleted.

void removeMemoryAccess(MemoryAccess *, bool OptimizePhis=false)

Remove a MemoryAccess from MemorySSA, including updating all definitions and uses.

A Module instance is used to store all the information related to an LLVM module.

const DataLayout & getDataLayout() const

Get the data layout for the module's target platform.

void addIncoming(Value *V, BasicBlock *BB)

Add an incoming value to the end of the PHI list.

const_block_iterator block_begin() const

Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)

Remove an incoming value.

void setIncomingValue(unsigned i, Value *V)

const_block_iterator block_end() const

Value * getIncomingValueForBlock(const BasicBlock *BB) const

BasicBlock * getIncomingBlock(unsigned i) const

Return incoming basic block number i.

Value * getIncomingValue(unsigned i) const

Return incoming value number x.

unsigned getNumIncomingValues() const

Return the number of incoming edges.

static PoisonValue * get(Type *T)

Static factory methods - Return an 'poison' object of the specified type.

size_type size() const

Determine the number of elements in the SetVector.

Vector takeVector()

Clear the SetVector and return the underlying vector.

size_type count(const key_type &key) const

Count the number of elements of a given key in the SetVector.

bool empty() const

Determine if the SetVector is empty or not.

bool insert(const value_type &X)

Insert a new element into the SetVector.

value_type pop_back_val()

A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...

size_type count(ConstPtrType Ptr) const

count - Return 1 if the specified pointer is in the set, 0 otherwise.

std::pair< iterator, bool > insert(PtrType Ptr)

Inserts Ptr if and only if there is no element in the container equal to Ptr.

bool contains(ConstPtrType Ptr) const

SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.

A SetVector that performs no allocations if smaller than a certain size.

SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...

std::pair< const_iterator, bool > insert(const T &V)

insert - Insert an element into the set if it isn't already there.

This class consists of common code factored out of the SmallVector class to reduce code duplication b...

void reserve(size_type N)

void append(ItTy in_start, ItTy in_end)

Add the specified range to the end of the SmallVector.

iterator insert(iterator I, T &&Elt)

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

An instruction for storing to memory.

Provides information about what library functions are available for the current target.

bool hasOptimizedCodeGen(LibFunc F) const

Tests if the function is both available and a candidate for optimized code generation.

bool has(LibFunc F) const

Tests whether a library function is available.

bool getLibFunc(StringRef funcName, LibFunc &F) const

Searches for a particular function name.

TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...

static constexpr TypeSize getFixed(ScalarTy ExactSize)

The instances of the Type class are immutable: once they are created, they are never changed.

bool isVectorTy() const

True if this is an instance of VectorType.

bool isArrayTy() const

True if this is an instance of ArrayType.

bool isIntOrIntVectorTy() const

Return true if this is an integer type or a vector of integer types.

bool isPointerTy() const

True if this is an instance of PointerType.

static IntegerType * getIntNTy(LLVMContext &C, unsigned N)

unsigned getScalarSizeInBits() const LLVM_READONLY

If this is a vector type, return the getPrimitiveSizeInBits value for the element type.

bool isStructTy() const

True if this is an instance of StructType.

bool isFloatingPointTy() const

Return true if this is one of the floating-point types.

bool isIntOrPtrTy() const

Return true if this is an integer type or a pointer type.

static IntegerType * getInt32Ty(LLVMContext &C)

bool isIntegerTy() const

True if this is an instance of IntegerType.

bool isTokenTy() const

Return true if this is 'token'.

TypeSize getPrimitiveSizeInBits() const LLVM_READONLY

Return the basic size of this type if it is a primitive type.

static UndefValue * get(Type *T)

Static factory methods - Return an 'undef' object of the specified type.

This function has undefined behavior.

A Use represents the edge between a Value definition and its users.

value_op_iterator value_op_end()

Value * getOperand(unsigned i) const

value_op_iterator value_op_begin()

iterator find(const KeyT &Val)

std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)

LLVM Value Representation.

Type * getType() const

All values are typed, get the type of this value.

void replaceAllUsesWith(Value *V)

Change all uses of this to point to a new Value.

iterator_range< user_iterator > users()

void replaceUsesWithIf(Value *New, llvm::function_ref< bool(Use &U)> ShouldReplace)

Go through the uses list for this definition and make each use point to "V" if the callback ShouldRep...

LLVMContext & getContext() const

All values hold a context through their type.

static constexpr unsigned MaxAlignmentExponent

The maximum alignment for instructions.

user_iterator_impl< User > user_iterator

StringRef getName() const

Return a constant reference to the value's name.

void takeName(Value *V)

Transfer the name from V to this value.

Represents an op.with.overflow intrinsic.

std::pair< iterator, bool > insert(const ValueT &V)

void reserve(size_t Size)

Grow the DenseSet so that it can contain at least NumEntries items before resizing again.

An efficient, type-erasing, non-owning reference to a callable.

const ParentTy * getParent() const

self_iterator getIterator()

#define llvm_unreachable(msg)

Marks that the current location is not supposed to be reachable.

@ C

The default llvm calling convention, compatible with C.

Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})

Look up the Function declaration of the intrinsic id in the Module M.

BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)

m_Intrinsic_Ty< Opnd0 >::Ty m_BitReverse(const Opnd0 &Op0)

CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)

Matches Trunc.

bool match(Val *V, const Pattern &P)

specificval_ty m_Specific(const Value *V)

Match if we have a specific specified value.

BinOpPred_match< LHS, RHS, is_logical_shift_op > m_LogicalShift(const LHS &L, const RHS &R)

Matches logical shift operations.

bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)

Match a with overflow intrinsic, capturing it if we match.

CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)

Matches ZExt.

m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)

apint_match m_APInt(const APInt *&Res)

Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.

class_match< Value > m_Value()

Match an arbitrary value and ignore it.

m_Intrinsic_Ty< Opnd0, Opnd1, Opnd2 >::Ty m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2)

auto m_Undef()

Match an arbitrary undef constant.

BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)

Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.

BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)

m_Intrinsic_Ty< Opnd0 >::Ty m_BSwap(const Opnd0 &Op0)

is_zero m_Zero()

Match any null constant or a vector with all elements equal to 0.

match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)

Combine two pattern matchers matching L || R.

initializer< Ty > init(const Ty &Val)

@ DW_OP_LLVM_arg

Only used in LLVM metadata.

@ ebStrict

This corresponds to "fpexcept.strict".

This is an optimization pass for GlobalISel generic memory operations.

auto find(R &&Range, const T &Val)

Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.

bool RemoveRedundantDbgInstrs(BasicBlock *BB)

Try to remove redundant dbg.value instructions from given basic block.

UnaryFunction for_each(R &&Range, UnaryFunction F)

Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.

bool all_of(R &&range, UnaryPredicate P)

Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.

bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())

If the specified value is a trivially dead instruction, delete it.

bool succ_empty(const Instruction *I)

BasicBlock * changeToInvokeAndSplitBasicBlock(CallInst *CI, BasicBlock *UnwindEdge, DomTreeUpdater *DTU=nullptr)

Convert the CallInst to InvokeInst with the specified unwind edge basic block.

bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)

If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...

unsigned replaceDominatedUsesWithIf(Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Edge, function_ref< bool(const Use &U, const Value *To)> ShouldReplace)

Replace each use of 'From' with 'To' if that use is dominated by the given edge and the callback Shou...

TinyPtrVector< DbgDeclareInst * > findDbgDeclares(Value *V)

Finds dbg.declare intrinsics declaring local variables as living in the memory that 'V' points to.

std::pair< unsigned, unsigned > removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB)

Remove all instructions from a basic block other than its terminator and any present EH pad instructi...

auto pred_end(const MachineBasicBlock *BB)

unsigned replaceNonLocalUsesWith(Instruction *From, Value *To)

void salvageDebugInfoForDbgValues(Instruction &I, ArrayRef< DbgVariableIntrinsic * > Insns, ArrayRef< DbgVariableRecord * > DPInsns)

Implementation of salvageDebugInfo, applying only to instructions in Insns, rather than all debug use...

void findDbgUsers(SmallVectorImpl< DbgVariableIntrinsic * > &DbgInsts, Value *V, SmallVectorImpl< DbgVariableRecord * > *DbgVariableRecords=nullptr)

Finds the debug info intrinsics describing a value.

void salvageDebugInfo(const MachineRegisterInfo &MRI, MachineInstr &MI)

Assuming the instruction MI is going to be deleted, attempt to salvage debug users of MI by writing t...

auto successors(const MachineBasicBlock *BB)

bool isRemovableAlloc(const CallBase *V, const TargetLibraryInfo *TLI)

Return true if this is a call to an allocation function that does not have side effects that we are r...

CallInst * changeToCall(InvokeInst *II, DomTreeUpdater *DTU=nullptr)

This function converts the specified invoke into a normal call.

bool isMathLibCallNoop(const CallBase *Call, const TargetLibraryInfo *TLI)

Check whether the given call has no side-effects.

void copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source)

Copy the metadata from the source instruction to the destination (the replacement for the source inst...

void InsertDebugValueAtStoreLoc(DbgVariableRecord *DVR, StoreInst *SI, DIBuilder &Builder)

===------------------------------------------------------------------—===// Dbg Intrinsic utilities

bool hasNItemsOrLess(IterTy &&Begin, IterTy &&End, unsigned N, Pred &&ShouldBeCounted=[](const decltype(*std::declval< IterTy >()) &) { return true;})

Returns true if the sequence [Begin, End) has N or less items.

void remapDebugVariable(ValueToValueMapTy &Mapping, Instruction *Inst)

Remap the operands of the debug records attached to Inst, and the operands of Inst itself if it's a d...

iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)

Make a range that does early increment to allow mutation of the underlying range without disrupting i...

auto pred_size(const MachineBasicBlock *BB)

bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)

Scan the specified basic block and try to simplify any instructions in it and recursively delete dead...

bool isAssumeWithEmptyBundle(const AssumeInst &Assume)

Return true iff the operand bundles of the provided llvm.assume doesn't contain any valuable informat...

void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)

Delete the specified block, which must have no predecessors.

bool hasBranchWeightOrigin(const Instruction &I)

Check if Branch Weight Metadata has an "expected" field from an llvm.expect* intrinsic.

void findDbgValues(SmallVectorImpl< DbgValueInst * > &DbgValues, Value *V, SmallVectorImpl< DbgVariableRecord * > *DbgVariableRecords=nullptr)

Finds the llvm.dbg.value intrinsics describing a value.

void insertDebugValuesForPHIs(BasicBlock *BB, SmallVectorImpl< PHINode * > &InsertedPHIs)

Propagate dbg.value intrinsics through the newly inserted PHIs.

ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)

Parse out a conservative ConstantRange from !range metadata.

MDNode * intersectAccessGroups(const Instruction *Inst1, const Instruction *Inst2)

Compute the access-group list of access groups that Inst1 and Inst2 are both in.

bool handleUnreachableTerminator(Instruction *I, SmallVectorImpl< Value * > &PoisonedValues)

If a terminator in an unreachable basic block has an operand of type Instruction, transform it into p...

bool canSimplifyInvokeNoUnwind(const Function *F)

Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)

See if we can compute a simplified version of this instruction.

bool any_of(R &&range, UnaryPredicate P)

Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.

bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)

Return true if the result produced by the instruction is not used, and the instruction will return.

bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)

BB is known to contain an unconditional branch, and contains no instructions other than PHI nodes,...

bool recognizeBSwapOrBitReverseIdiom(Instruction *I, bool MatchBSwaps, bool MatchBitReversals, SmallVectorImpl< Instruction * > &InsertedInsts)

Try to match a bswap or bitreverse idiom.

void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected)

Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...

MDNode * getValidBranchWeightMDNode(const Instruction &I)

Get the valid branch weights metadata node.

Align getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)

Try to ensure that the alignment of V is at least PrefAlign bytes.

bool wouldInstructionBeTriviallyDeadOnUnusedPaths(Instruction *I, const TargetLibraryInfo *TLI=nullptr)

Return true if the result produced by the instruction has no side effects on any paths other than whe...

bool LowerDbgDeclare(Function &F)

Lowers llvm.dbg.declare intrinsics into appropriate set of llvm.dbg.value intrinsics.

bool NullPointerIsDefined(const Function *F, unsigned AS=0)

Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...

DIExpression * getExpressionForConstant(DIBuilder &DIB, const Constant &C, Type &Ty)

Given a constant, create a debug information expression.

CallInst * createCallMatchingInvoke(InvokeInst *II)

Create a call that matches the invoke II in terms of arguments, attributes, debug information,...

raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

Instruction * removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)

Replace 'BB's terminator with one that does not have an unwind successor block.

bool wouldInstructionBeTriviallyDead(const Instruction *I, const TargetLibraryInfo *TLI=nullptr)

Return true if the result produced by the instruction would have no side effects if it was not used.

void patchReplacementInstruction(Instruction *I, Value *Repl)

Patch the replacement so that it is not more restrictive than the value being replaced.

DebugLoc getDebugValueLoc(DbgVariableIntrinsic *DII)

Produce a DebugLoc to use for each dbg.declare that is promoted to a dbg.value.

void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, StoreInst *SI, DIBuilder &Builder)

Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value that has an associated llvm....

unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, const BasicBlockEdge &Edge)

Replace each use of 'From' with 'To' if that use is dominated by the given edge.

unsigned changeToUnreachable(Instruction *I, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)

Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...

bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, DominatorTree &DT)

Point debug users of From to To or salvage them.

Value * salvageDebugInfoImpl(Instruction &I, uint64_t CurrentLocOps, SmallVectorImpl< uint64_t > &Ops, SmallVectorImpl< Value * > &AdditionalValues)

RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)

void combineMetadataForCSE(Instruction *K, const Instruction *J, bool DoesKMove)

Combine the metadata of two instructions so that K can replace J.

void dropDebugUsers(Instruction &I)

Remove the debug intrinsic instructions for the given instruction.

@ First

Helpers to iterate all locations in the MemoryEffectsBase class.

void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)

BB is a block with one predecessor and its predecessor is known to have one successor (BB!...

bool replaceDbgUsesWithUndef(Instruction *I)

Replace all the uses of an SSA value in @llvm.dbg intrinsics with undef.

void hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt, BasicBlock *BB)

Hoist all of the instructions in the IfBlock to the dominant block DomBlock, by moving its instructio...

void copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, MDNode *N, LoadInst &NewLI)

Copy a range metadata node to a new load instruction.

void copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, LoadInst &NewLI)

Copy a nonnull metadata node to a new load instruction.

void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)

Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...

bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx)

Given an instruction, is it legal to set operand OpIdx to a non-constant value?

void replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress, DIBuilder &Builder, int Offset=0)

Replaces multiple llvm.dbg.value instructions when the alloca it describes is replaced with a new val...

Align tryEnforceAlignment(Value *V, Align PrefAlign, const DataLayout &DL)

If the specified pointer points to an object that we control, try to modify the object's alignment to...

Value * getFreedOperand(const CallBase *CB, const TargetLibraryInfo *TLI)

If this if a call to a free function, return the freed operand.

bool RecursivelyDeleteTriviallyDeadInstructionsPermissive(SmallVectorImpl< WeakTrackingVH > &DeadInsts, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())

Same functionality as RecursivelyDeleteTriviallyDeadInstructions, but allow instructions that are not...

constexpr unsigned BitWidth

bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)

Extract branch weights from MD_prof metadata.

auto count_if(R &&Range, UnaryPredicate P)

Wrapper function around std::count_if to count the number of times an element satisfying a given pred...

auto pred_begin(const MachineBasicBlock *BB)

BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)

Split the specified block at the specified instruction.

gep_type_iterator gep_type_begin(const User *GEP)

TinyPtrVector< DbgVariableRecord * > findDVRDeclares(Value *V)

As above, for DVRDeclares.

auto predecessors(const MachineBasicBlock *BB)

bool is_contained(R &&Range, const E &Element)

Returns true if Element is found in Range.

void combineAAMetadata(Instruction *K, const Instruction *J)

Combine metadata of two instructions, where instruction J is a memory access that has been merged int...

bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)

If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...

bool inferAttributesFromOthers(Function &F)

If we can infer one attribute from another on the declaration of a function, explicitly materialize t...

Value * invertCondition(Value *Condition)

Invert the given true/false value, possibly reusing an existing copy.

hash_code hash_combine(const Ts &...args)

Combine values into a single hash_code.

void DeleteDeadBlocks(ArrayRef< BasicBlock * > BBs, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)

Delete the specified blocks from BB.

void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI, const TargetLibraryInfo *TLI)

Given a CallInst, check if it calls a string function known to CodeGen, and mark it with NoBuiltin if...

static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)

Filter the DbgRecord range to DbgVariableRecord types only and downcast.

bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)

Remove all blocks that can not be reached from the function's entry.

bool EliminateDuplicatePHINodes(BasicBlock *BB)

Check for and eliminate duplicate PHI nodes in this block.

bool callsGCLeafFunction(const CallBase *Call, const TargetLibraryInfo &TLI)

Return true if this call calls a gc leaf function.

hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)

Compute a hash_code for a sequence of values.

bool replaceDbgDeclare(Value *Address, Value *NewAddress, DIBuilder &Builder, uint8_t DIExprFlags, int Offset)

Replaces llvm.dbg.declare instruction when the address it describes is replaced with a new value.

void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)

Implement std::swap in terms of BitVector swap.

This struct is a compact representation of a valid (non-zero power of two) alignment.

An information struct used to provide DenseMap with the various necessary components for a given valu...

unsigned countMinTrailingZeros() const

Returns the minimum number of trailing zero bits.

unsigned getBitWidth() const

Get the bit width of this value.

This struct is a compact representation of a valid (power of two) or undefined (0) alignment.

A MapVector that performs no allocations if smaller than a certain size.