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