ImplicitNullChecks.cpp 29 KB

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