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