ImplicitNullChecks.cpp 29 KB

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