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