1 //===- ImplicitNullChecks.cpp - Fold null checks into memory accesses -----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This pass turns explicit null checks of the form 10 // 11 // test %r10, %r10 12 // je throw_npe 13 // movl (%r10), %esi 14 // ... 15 // 16 // to 17 // 18 // faulting_load_op("movl (%r10), %esi", throw_npe) 19 // ... 20 // 21 // With the help of a runtime that understands the .fault_maps section, 22 // faulting_load_op branches to throw_npe if executing movl (%r10), %esi incurs 23 // a page fault. 24 // Store and LoadStore are also supported. 25 // 26 //===----------------------------------------------------------------------===// 27 28 #include "llvm/ADT/ArrayRef.h" 29 #include "llvm/ADT/None.h" 30 #include "llvm/ADT/Optional.h" 31 #include "llvm/ADT/STLExtras.h" 32 #include "llvm/ADT/SmallVector.h" 33 #include "llvm/ADT/Statistic.h" 34 #include "llvm/Analysis/AliasAnalysis.h" 35 #include "llvm/Analysis/MemoryLocation.h" 36 #include "llvm/CodeGen/FaultMaps.h" 37 #include "llvm/CodeGen/MachineBasicBlock.h" 38 #include "llvm/CodeGen/MachineFunction.h" 39 #include "llvm/CodeGen/MachineFunctionPass.h" 40 #include "llvm/CodeGen/MachineInstr.h" 41 #include "llvm/CodeGen/MachineInstrBuilder.h" 42 #include "llvm/CodeGen/MachineMemOperand.h" 43 #include "llvm/CodeGen/MachineOperand.h" 44 #include "llvm/CodeGen/MachineRegisterInfo.h" 45 #include "llvm/CodeGen/PseudoSourceValue.h" 46 #include "llvm/CodeGen/TargetInstrInfo.h" 47 #include "llvm/CodeGen/TargetOpcodes.h" 48 #include "llvm/CodeGen/TargetRegisterInfo.h" 49 #include "llvm/CodeGen/TargetSubtargetInfo.h" 50 #include "llvm/IR/BasicBlock.h" 51 #include "llvm/IR/DebugLoc.h" 52 #include "llvm/IR/LLVMContext.h" 53 #include "llvm/InitializePasses.h" 54 #include "llvm/MC/MCInstrDesc.h" 55 #include "llvm/MC/MCRegisterInfo.h" 56 #include "llvm/Pass.h" 57 #include "llvm/Support/CommandLine.h" 58 #include <cassert> 59 #include <cstdint> 60 #include <iterator> 61 62 using namespace llvm; 63 64 static cl::opt<int> PageSize("imp-null-check-page-size", 65 cl::desc("The page size of the target in bytes"), 66 cl::init(4096), cl::Hidden); 67 68 static cl::opt<unsigned> MaxInstsToConsider( 69 "imp-null-max-insts-to-consider", 70 cl::desc("The max number of instructions to consider hoisting loads over " 71 "(the algorithm is quadratic over this number)"), 72 cl::Hidden, cl::init(8)); 73 74 #define DEBUG_TYPE "implicit-null-checks" 75 76 STATISTIC(NumImplicitNullChecks, 77 "Number of explicit null checks made implicit"); 78 79 namespace { 80 81 class ImplicitNullChecks : public MachineFunctionPass { 82 /// Return true if \c computeDependence can process \p MI. 83 static bool canHandle(const MachineInstr *MI); 84 85 /// Helper function for \c computeDependence. Return true if \p A 86 /// and \p B do not have any dependences between them, and can be 87 /// re-ordered without changing program semantics. 88 bool canReorder(const MachineInstr *A, const MachineInstr *B); 89 90 /// A data type for representing the result computed by \c 91 /// computeDependence. States whether it is okay to reorder the 92 /// instruction passed to \c computeDependence with at most one 93 /// dependency. 94 struct DependenceResult { 95 /// Can we actually re-order \p MI with \p Insts (see \c 96 /// computeDependence). 97 bool CanReorder; 98 99 /// If non-None, then an instruction in \p Insts that also must be 100 /// hoisted. 101 Optional<ArrayRef<MachineInstr *>::iterator> PotentialDependence; 102 103 /*implicit*/ DependenceResult( 104 bool CanReorder, 105 Optional<ArrayRef<MachineInstr *>::iterator> PotentialDependence) 106 : CanReorder(CanReorder), PotentialDependence(PotentialDependence) { 107 assert((!PotentialDependence || CanReorder) && 108 "!CanReorder && PotentialDependence.hasValue() not allowed!"); 109 } 110 }; 111 112 /// Compute a result for the following question: can \p MI be 113 /// re-ordered from after \p Insts to before it. 114 /// 115 /// \c canHandle should return true for all instructions in \p 116 /// Insts. 117 DependenceResult computeDependence(const MachineInstr *MI, 118 ArrayRef<MachineInstr *> Block); 119 120 /// Represents one null check that can be made implicit. 121 class NullCheck { 122 // The memory operation the null check can be folded into. 123 MachineInstr *MemOperation; 124 125 // The instruction actually doing the null check (Ptr != 0). 126 MachineInstr *CheckOperation; 127 128 // The block the check resides in. 129 MachineBasicBlock *CheckBlock; 130 131 // The block branched to if the pointer is non-null. 132 MachineBasicBlock *NotNullSucc; 133 134 // The block branched to if the pointer is null. 135 MachineBasicBlock *NullSucc; 136 137 // If this is non-null, then MemOperation has a dependency on this 138 // instruction; and it needs to be hoisted to execute before MemOperation. 139 MachineInstr *OnlyDependency; 140 141 public: 142 explicit NullCheck(MachineInstr *memOperation, MachineInstr *checkOperation, 143 MachineBasicBlock *checkBlock, 144 MachineBasicBlock *notNullSucc, 145 MachineBasicBlock *nullSucc, 146 MachineInstr *onlyDependency) 147 : MemOperation(memOperation), CheckOperation(checkOperation), 148 CheckBlock(checkBlock), NotNullSucc(notNullSucc), NullSucc(nullSucc), 149 OnlyDependency(onlyDependency) {} 150 151 MachineInstr *getMemOperation() const { return MemOperation; } 152 153 MachineInstr *getCheckOperation() const { return CheckOperation; } 154 155 MachineBasicBlock *getCheckBlock() const { return CheckBlock; } 156 157 MachineBasicBlock *getNotNullSucc() const { return NotNullSucc; } 158 159 MachineBasicBlock *getNullSucc() const { return NullSucc; } 160 161 MachineInstr *getOnlyDependency() const { return OnlyDependency; } 162 }; 163 164 const TargetInstrInfo *TII = nullptr; 165 const TargetRegisterInfo *TRI = nullptr; 166 AliasAnalysis *AA = nullptr; 167 MachineFrameInfo *MFI = nullptr; 168 169 bool analyzeBlockForNullChecks(MachineBasicBlock &MBB, 170 SmallVectorImpl<NullCheck> &NullCheckList); 171 MachineInstr *insertFaultingInstr(MachineInstr *MI, MachineBasicBlock *MBB, 172 MachineBasicBlock *HandlerMBB); 173 void rewriteNullChecks(ArrayRef<NullCheck> NullCheckList); 174 175 enum AliasResult { 176 AR_NoAlias, 177 AR_MayAlias, 178 AR_WillAliasEverything 179 }; 180 181 /// Returns AR_NoAlias if \p MI memory operation does not alias with 182 /// \p PrevMI, AR_MayAlias if they may alias and AR_WillAliasEverything if 183 /// they may alias and any further memory operation may alias with \p PrevMI. 184 AliasResult areMemoryOpsAliased(const MachineInstr &MI, 185 const MachineInstr *PrevMI) const; 186 187 enum SuitabilityResult { 188 SR_Suitable, 189 SR_Unsuitable, 190 SR_Impossible 191 }; 192 193 /// Return SR_Suitable if \p MI a memory operation that can be used to 194 /// implicitly null check the value in \p PointerReg, SR_Unsuitable if 195 /// \p MI cannot be used to null check and SR_Impossible if there is 196 /// no sense to continue lookup due to any other instruction will not be able 197 /// to be used. \p PrevInsts is the set of instruction seen since 198 /// the explicit null check on \p PointerReg. 199 SuitabilityResult isSuitableMemoryOp(const MachineInstr &MI, 200 unsigned PointerReg, 201 ArrayRef<MachineInstr *> PrevInsts); 202 203 /// Return true if \p FaultingMI can be hoisted from after the 204 /// instructions in \p InstsSeenSoFar to before them. Set \p Dependence to a 205 /// non-null value if we also need to (and legally can) hoist a depedency. 206 bool canHoistInst(MachineInstr *FaultingMI, unsigned PointerReg, 207 ArrayRef<MachineInstr *> InstsSeenSoFar, 208 MachineBasicBlock *NullSucc, MachineInstr *&Dependence); 209 210 public: 211 static char ID; 212 213 ImplicitNullChecks() : MachineFunctionPass(ID) { 214 initializeImplicitNullChecksPass(*PassRegistry::getPassRegistry()); 215 } 216 217 bool runOnMachineFunction(MachineFunction &MF) override; 218 219 void getAnalysisUsage(AnalysisUsage &AU) const override { 220 AU.addRequired<AAResultsWrapperPass>(); 221 MachineFunctionPass::getAnalysisUsage(AU); 222 } 223 224 MachineFunctionProperties getRequiredProperties() const override { 225 return MachineFunctionProperties().set( 226 MachineFunctionProperties::Property::NoVRegs); 227 } 228 }; 229 230 } // end anonymous namespace 231 232 bool ImplicitNullChecks::canHandle(const MachineInstr *MI) { 233 if (MI->isCall() || MI->mayRaiseFPException() || 234 MI->hasUnmodeledSideEffects()) 235 return false; 236 auto IsRegMask = [](const MachineOperand &MO) { return MO.isRegMask(); }; 237 (void)IsRegMask; 238 239 assert(!llvm::any_of(MI->operands(), IsRegMask) && 240 "Calls were filtered out above!"); 241 242 auto IsUnordered = [](MachineMemOperand *MMO) { return MMO->isUnordered(); }; 243 return llvm::all_of(MI->memoperands(), IsUnordered); 244 } 245 246 ImplicitNullChecks::DependenceResult 247 ImplicitNullChecks::computeDependence(const MachineInstr *MI, 248 ArrayRef<MachineInstr *> Block) { 249 assert(llvm::all_of(Block, canHandle) && "Check this first!"); 250 assert(!is_contained(Block, MI) && "Block must be exclusive of MI!"); 251 252 Optional<ArrayRef<MachineInstr *>::iterator> Dep; 253 254 for (auto I = Block.begin(), E = Block.end(); I != E; ++I) { 255 if (canReorder(*I, MI)) 256 continue; 257 258 if (Dep == None) { 259 // Found one possible dependency, keep track of it. 260 Dep = I; 261 } else { 262 // We found two dependencies, so bail out. 263 return {false, None}; 264 } 265 } 266 267 return {true, Dep}; 268 } 269 270 bool ImplicitNullChecks::canReorder(const MachineInstr *A, 271 const MachineInstr *B) { 272 assert(canHandle(A) && canHandle(B) && "Precondition!"); 273 274 // canHandle makes sure that we _can_ correctly analyze the dependencies 275 // between A and B here -- for instance, we should not be dealing with heap 276 // load-store dependencies here. 277 278 for (auto MOA : A->operands()) { 279 if (!(MOA.isReg() && MOA.getReg())) 280 continue; 281 282 Register RegA = MOA.getReg(); 283 for (auto MOB : B->operands()) { 284 if (!(MOB.isReg() && MOB.getReg())) 285 continue; 286 287 Register RegB = MOB.getReg(); 288 289 if (TRI->regsOverlap(RegA, RegB) && (MOA.isDef() || MOB.isDef())) 290 return false; 291 } 292 } 293 294 return true; 295 } 296 297 bool ImplicitNullChecks::runOnMachineFunction(MachineFunction &MF) { 298 TII = MF.getSubtarget().getInstrInfo(); 299 TRI = MF.getRegInfo().getTargetRegisterInfo(); 300 MFI = &MF.getFrameInfo(); 301 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults(); 302 303 SmallVector<NullCheck, 16> NullCheckList; 304 305 for (auto &MBB : MF) 306 analyzeBlockForNullChecks(MBB, NullCheckList); 307 308 if (!NullCheckList.empty()) 309 rewriteNullChecks(NullCheckList); 310 311 return !NullCheckList.empty(); 312 } 313 314 // Return true if any register aliasing \p Reg is live-in into \p MBB. 315 static bool AnyAliasLiveIn(const TargetRegisterInfo *TRI, 316 MachineBasicBlock *MBB, unsigned Reg) { 317 for (MCRegAliasIterator AR(Reg, TRI, /*IncludeSelf*/ true); AR.isValid(); 318 ++AR) 319 if (MBB->isLiveIn(*AR)) 320 return true; 321 return false; 322 } 323 324 ImplicitNullChecks::AliasResult 325 ImplicitNullChecks::areMemoryOpsAliased(const MachineInstr &MI, 326 const MachineInstr *PrevMI) const { 327 // If it is not memory access, skip the check. 328 if (!(PrevMI->mayStore() || PrevMI->mayLoad())) 329 return AR_NoAlias; 330 // Load-Load may alias 331 if (!(MI.mayStore() || PrevMI->mayStore())) 332 return AR_NoAlias; 333 // We lost info, conservatively alias. If it was store then no sense to 334 // continue because we won't be able to check against it further. 335 if (MI.memoperands_empty()) 336 return MI.mayStore() ? AR_WillAliasEverything : AR_MayAlias; 337 if (PrevMI->memoperands_empty()) 338 return PrevMI->mayStore() ? AR_WillAliasEverything : AR_MayAlias; 339 340 for (MachineMemOperand *MMO1 : MI.memoperands()) { 341 // MMO1 should have a value due it comes from operation we'd like to use 342 // as implicit null check. 343 assert(MMO1->getValue() && "MMO1 should have a Value!"); 344 for (MachineMemOperand *MMO2 : PrevMI->memoperands()) { 345 if (const PseudoSourceValue *PSV = MMO2->getPseudoValue()) { 346 if (PSV->mayAlias(MFI)) 347 return AR_MayAlias; 348 continue; 349 } 350 llvm::AliasResult AAResult = 351 AA->alias(MemoryLocation(MMO1->getValue(), LocationSize::unknown(), 352 MMO1->getAAInfo()), 353 MemoryLocation(MMO2->getValue(), LocationSize::unknown(), 354 MMO2->getAAInfo())); 355 if (AAResult != NoAlias) 356 return AR_MayAlias; 357 } 358 } 359 return AR_NoAlias; 360 } 361 362 ImplicitNullChecks::SuitabilityResult 363 ImplicitNullChecks::isSuitableMemoryOp(const MachineInstr &MI, 364 unsigned PointerReg, 365 ArrayRef<MachineInstr *> PrevInsts) { 366 int64_t Offset; 367 bool OffsetIsScalable; 368 const MachineOperand *BaseOp; 369 370 371 // FIXME: This handles only simple addressing mode. 372 if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI)) 373 return SR_Unsuitable; 374 375 // We need the base of the memory instruction to be same as the register 376 // where the null check is performed (i.e. PointerReg). 377 if (!BaseOp->isReg() || BaseOp->getReg() != PointerReg) 378 return SR_Unsuitable; 379 380 // Scalable offsets are a part of scalable vectors (SVE for AArch64). That 381 // target is in-practice unsupported for ImplicitNullChecks. 382 if (OffsetIsScalable) 383 return SR_Unsuitable; 384 385 if (!MI.mayLoadOrStore() || MI.isPredicable()) 386 return SR_Unsuitable; 387 388 // We want the mem access to be issued at a sane offset from PointerReg, 389 // so that if PointerReg is null then the access reliably page faults. 390 if (!(-PageSize < Offset && Offset < PageSize)) 391 return SR_Unsuitable; 392 393 // Finally, check whether the current memory access aliases with previous one. 394 for (auto *PrevMI : PrevInsts) { 395 AliasResult AR = areMemoryOpsAliased(MI, PrevMI); 396 if (AR == AR_WillAliasEverything) 397 return SR_Impossible; 398 if (AR == AR_MayAlias) 399 return SR_Unsuitable; 400 } 401 return SR_Suitable; 402 } 403 404 bool ImplicitNullChecks::canHoistInst(MachineInstr *FaultingMI, 405 unsigned PointerReg, 406 ArrayRef<MachineInstr *> InstsSeenSoFar, 407 MachineBasicBlock *NullSucc, 408 MachineInstr *&Dependence) { 409 auto DepResult = computeDependence(FaultingMI, InstsSeenSoFar); 410 if (!DepResult.CanReorder) 411 return false; 412 413 if (!DepResult.PotentialDependence) { 414 Dependence = nullptr; 415 return true; 416 } 417 418 auto DependenceItr = *DepResult.PotentialDependence; 419 auto *DependenceMI = *DependenceItr; 420 421 // We don't want to reason about speculating loads. Note -- at this point 422 // we should have already filtered out all of the other non-speculatable 423 // things, like calls and stores. 424 // We also do not want to hoist stores because it might change the memory 425 // while the FaultingMI may result in faulting. 426 assert(canHandle(DependenceMI) && "Should never have reached here!"); 427 if (DependenceMI->mayLoadOrStore()) 428 return false; 429 430 for (auto &DependenceMO : DependenceMI->operands()) { 431 if (!(DependenceMO.isReg() && DependenceMO.getReg())) 432 continue; 433 434 // Make sure that we won't clobber any live ins to the sibling block by 435 // hoisting Dependency. For instance, we can't hoist INST to before the 436 // null check (even if it safe, and does not violate any dependencies in 437 // the non_null_block) if %rdx is live in to _null_block. 438 // 439 // test %rcx, %rcx 440 // je _null_block 441 // _non_null_block: 442 // %rdx = INST 443 // ... 444 // 445 // This restriction does not apply to the faulting load inst because in 446 // case the pointer loaded from is in the null page, the load will not 447 // semantically execute, and affect machine state. That is, if the load 448 // was loading into %rax and it faults, the value of %rax should stay the 449 // same as it would have been had the load not have executed and we'd have 450 // branched to NullSucc directly. 451 if (AnyAliasLiveIn(TRI, NullSucc, DependenceMO.getReg())) 452 return false; 453 454 // The Dependency can't be re-defining the base register -- then we won't 455 // get the memory operation on the address we want. This is already 456 // checked in \c IsSuitableMemoryOp. 457 assert(!(DependenceMO.isDef() && 458 TRI->regsOverlap(DependenceMO.getReg(), PointerReg)) && 459 "Should have been checked before!"); 460 } 461 462 auto DepDepResult = 463 computeDependence(DependenceMI, {InstsSeenSoFar.begin(), DependenceItr}); 464 465 if (!DepDepResult.CanReorder || DepDepResult.PotentialDependence) 466 return false; 467 468 Dependence = DependenceMI; 469 return true; 470 } 471 472 /// Analyze MBB to check if its terminating branch can be turned into an 473 /// implicit null check. If yes, append a description of the said null check to 474 /// NullCheckList and return true, else return false. 475 bool ImplicitNullChecks::analyzeBlockForNullChecks( 476 MachineBasicBlock &MBB, SmallVectorImpl<NullCheck> &NullCheckList) { 477 using MachineBranchPredicate = TargetInstrInfo::MachineBranchPredicate; 478 479 MDNode *BranchMD = nullptr; 480 if (auto *BB = MBB.getBasicBlock()) 481 BranchMD = BB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit); 482 483 if (!BranchMD) 484 return false; 485 486 MachineBranchPredicate MBP; 487 488 if (TII->analyzeBranchPredicate(MBB, MBP, true)) 489 return false; 490 491 // Is the predicate comparing an integer to zero? 492 if (!(MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 && 493 (MBP.Predicate == MachineBranchPredicate::PRED_NE || 494 MBP.Predicate == MachineBranchPredicate::PRED_EQ))) 495 return false; 496 497 // If we cannot erase the test instruction itself, then making the null check 498 // implicit does not buy us much. 499 if (!MBP.SingleUseCondition) 500 return false; 501 502 MachineBasicBlock *NotNullSucc, *NullSucc; 503 504 if (MBP.Predicate == MachineBranchPredicate::PRED_NE) { 505 NotNullSucc = MBP.TrueDest; 506 NullSucc = MBP.FalseDest; 507 } else { 508 NotNullSucc = MBP.FalseDest; 509 NullSucc = MBP.TrueDest; 510 } 511 512 // We handle the simplest case for now. We can potentially do better by using 513 // the machine dominator tree. 514 if (NotNullSucc->pred_size() != 1) 515 return false; 516 517 // To prevent the invalid transformation of the following code: 518 // 519 // mov %rax, %rcx 520 // test %rax, %rax 521 // %rax = ... 522 // je throw_npe 523 // mov(%rcx), %r9 524 // mov(%rax), %r10 525 // 526 // into: 527 // 528 // mov %rax, %rcx 529 // %rax = .... 530 // faulting_load_op("movl (%rax), %r10", throw_npe) 531 // mov(%rcx), %r9 532 // 533 // we must ensure that there are no instructions between the 'test' and 534 // conditional jump that modify %rax. 535 const Register PointerReg = MBP.LHS.getReg(); 536 537 assert(MBP.ConditionDef->getParent() == &MBB && "Should be in basic block"); 538 539 for (auto I = MBB.rbegin(); MBP.ConditionDef != &*I; ++I) 540 if (I->modifiesRegister(PointerReg, TRI)) 541 return false; 542 543 // Starting with a code fragment like: 544 // 545 // test %rax, %rax 546 // jne LblNotNull 547 // 548 // LblNull: 549 // callq throw_NullPointerException 550 // 551 // LblNotNull: 552 // Inst0 553 // Inst1 554 // ... 555 // Def = Load (%rax + <offset>) 556 // ... 557 // 558 // 559 // we want to end up with 560 // 561 // Def = FaultingLoad (%rax + <offset>), LblNull 562 // jmp LblNotNull ;; explicit or fallthrough 563 // 564 // LblNotNull: 565 // Inst0 566 // Inst1 567 // ... 568 // 569 // LblNull: 570 // callq throw_NullPointerException 571 // 572 // 573 // To see why this is legal, consider the two possibilities: 574 // 575 // 1. %rax is null: since we constrain <offset> to be less than PageSize, the 576 // load instruction dereferences the null page, causing a segmentation 577 // fault. 578 // 579 // 2. %rax is not null: in this case we know that the load cannot fault, as 580 // otherwise the load would've faulted in the original program too and the 581 // original program would've been undefined. 582 // 583 // This reasoning cannot be extended to justify hoisting through arbitrary 584 // control flow. For instance, in the example below (in pseudo-C) 585 // 586 // if (ptr == null) { throw_npe(); unreachable; } 587 // if (some_cond) { return 42; } 588 // v = ptr->field; // LD 589 // ... 590 // 591 // we cannot (without code duplication) use the load marked "LD" to null check 592 // ptr -- clause (2) above does not apply in this case. In the above program 593 // the safety of ptr->field can be dependent on some_cond; and, for instance, 594 // ptr could be some non-null invalid reference that never gets loaded from 595 // because some_cond is always true. 596 597 SmallVector<MachineInstr *, 8> InstsSeenSoFar; 598 599 for (auto &MI : *NotNullSucc) { 600 if (!canHandle(&MI) || InstsSeenSoFar.size() >= MaxInstsToConsider) 601 return false; 602 603 MachineInstr *Dependence; 604 SuitabilityResult SR = isSuitableMemoryOp(MI, PointerReg, InstsSeenSoFar); 605 if (SR == SR_Impossible) 606 return false; 607 if (SR == SR_Suitable && 608 canHoistInst(&MI, PointerReg, InstsSeenSoFar, NullSucc, Dependence)) { 609 NullCheckList.emplace_back(&MI, MBP.ConditionDef, &MBB, NotNullSucc, 610 NullSucc, Dependence); 611 return true; 612 } 613 614 // If MI re-defines the PointerReg then we cannot move further. 615 if (llvm::any_of(MI.operands(), [&](MachineOperand &MO) { 616 return MO.isReg() && MO.getReg() && MO.isDef() && 617 TRI->regsOverlap(MO.getReg(), PointerReg); 618 })) 619 return false; 620 InstsSeenSoFar.push_back(&MI); 621 } 622 623 return false; 624 } 625 626 /// Wrap a machine instruction, MI, into a FAULTING machine instruction. 627 /// The FAULTING instruction does the same load/store as MI 628 /// (defining the same register), and branches to HandlerMBB if the mem access 629 /// faults. The FAULTING instruction is inserted at the end of MBB. 630 MachineInstr *ImplicitNullChecks::insertFaultingInstr( 631 MachineInstr *MI, MachineBasicBlock *MBB, MachineBasicBlock *HandlerMBB) { 632 const unsigned NoRegister = 0; // Guaranteed to be the NoRegister value for 633 // all targets. 634 635 DebugLoc DL; 636 unsigned NumDefs = MI->getDesc().getNumDefs(); 637 assert(NumDefs <= 1 && "other cases unhandled!"); 638 639 unsigned DefReg = NoRegister; 640 if (NumDefs != 0) { 641 DefReg = MI->getOperand(0).getReg(); 642 assert(NumDefs == 1 && "expected exactly one def!"); 643 } 644 645 FaultMaps::FaultKind FK; 646 if (MI->mayLoad()) 647 FK = 648 MI->mayStore() ? FaultMaps::FaultingLoadStore : FaultMaps::FaultingLoad; 649 else 650 FK = FaultMaps::FaultingStore; 651 652 auto MIB = BuildMI(MBB, DL, TII->get(TargetOpcode::FAULTING_OP), DefReg) 653 .addImm(FK) 654 .addMBB(HandlerMBB) 655 .addImm(MI->getOpcode()); 656 657 for (auto &MO : MI->uses()) { 658 if (MO.isReg()) { 659 MachineOperand NewMO = MO; 660 if (MO.isUse()) { 661 NewMO.setIsKill(false); 662 } else { 663 assert(MO.isDef() && "Expected def or use"); 664 NewMO.setIsDead(false); 665 } 666 MIB.add(NewMO); 667 } else { 668 MIB.add(MO); 669 } 670 } 671 672 MIB.setMemRefs(MI->memoperands()); 673 674 return MIB; 675 } 676 677 /// Rewrite the null checks in NullCheckList into implicit null checks. 678 void ImplicitNullChecks::rewriteNullChecks( 679 ArrayRef<ImplicitNullChecks::NullCheck> NullCheckList) { 680 DebugLoc DL; 681 682 for (auto &NC : NullCheckList) { 683 // Remove the conditional branch dependent on the null check. 684 unsigned BranchesRemoved = TII->removeBranch(*NC.getCheckBlock()); 685 (void)BranchesRemoved; 686 assert(BranchesRemoved > 0 && "expected at least one branch!"); 687 688 if (auto *DepMI = NC.getOnlyDependency()) { 689 DepMI->removeFromParent(); 690 NC.getCheckBlock()->insert(NC.getCheckBlock()->end(), DepMI); 691 } 692 693 // Insert a faulting instruction where the conditional branch was 694 // originally. We check earlier ensures that this bit of code motion 695 // is legal. We do not touch the successors list for any basic block 696 // since we haven't changed control flow, we've just made it implicit. 697 MachineInstr *FaultingInstr = insertFaultingInstr( 698 NC.getMemOperation(), NC.getCheckBlock(), NC.getNullSucc()); 699 // Now the values defined by MemOperation, if any, are live-in of 700 // the block of MemOperation. 701 // The original operation may define implicit-defs alongside 702 // the value. 703 MachineBasicBlock *MBB = NC.getMemOperation()->getParent(); 704 for (const MachineOperand &MO : FaultingInstr->operands()) { 705 if (!MO.isReg() || !MO.isDef()) 706 continue; 707 Register Reg = MO.getReg(); 708 if (!Reg || MBB->isLiveIn(Reg)) 709 continue; 710 MBB->addLiveIn(Reg); 711 } 712 713 if (auto *DepMI = NC.getOnlyDependency()) { 714 for (auto &MO : DepMI->operands()) { 715 if (!MO.isReg() || !MO.getReg() || !MO.isDef() || MO.isDead()) 716 continue; 717 if (!NC.getNotNullSucc()->isLiveIn(MO.getReg())) 718 NC.getNotNullSucc()->addLiveIn(MO.getReg()); 719 } 720 } 721 722 NC.getMemOperation()->eraseFromParent(); 723 NC.getCheckOperation()->eraseFromParent(); 724 725 // Insert an *unconditional* branch to not-null successor. 726 TII->insertBranch(*NC.getCheckBlock(), NC.getNotNullSucc(), nullptr, 727 /*Cond=*/None, DL); 728 729 NumImplicitNullChecks++; 730 } 731 } 732 733 char ImplicitNullChecks::ID = 0; 734 735 char &llvm::ImplicitNullChecksID = ImplicitNullChecks::ID; 736 737 INITIALIZE_PASS_BEGIN(ImplicitNullChecks, DEBUG_TYPE, 738 "Implicit null checks", false, false) 739 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass) 740 INITIALIZE_PASS_END(ImplicitNullChecks, DEBUG_TYPE, 741 "Implicit null checks", false, false) 742