xref: /aosp_15_r20/external/llvm/lib/CodeGen/ImplicitNullChecks.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- ImplicitNullChecks.cpp - Fold null checks into memory accesses ----===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass turns explicit null checks of the form
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //   test %r10, %r10
13*9880d681SAndroid Build Coastguard Worker //   je throw_npe
14*9880d681SAndroid Build Coastguard Worker //   movl (%r10), %esi
15*9880d681SAndroid Build Coastguard Worker //   ...
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker // to
18*9880d681SAndroid Build Coastguard Worker //
19*9880d681SAndroid Build Coastguard Worker //   faulting_load_op("movl (%r10), %esi", throw_npe)
20*9880d681SAndroid Build Coastguard Worker //   ...
21*9880d681SAndroid Build Coastguard Worker //
22*9880d681SAndroid Build Coastguard Worker // With the help of a runtime that understands the .fault_maps section,
23*9880d681SAndroid Build Coastguard Worker // faulting_load_op branches to throw_npe if executing movl (%r10), %esi incurs
24*9880d681SAndroid Build Coastguard Worker // a page fault.
25*9880d681SAndroid Build Coastguard Worker //
26*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
27*9880d681SAndroid Build Coastguard Worker 
28*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DenseSet.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineMemOperand.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineOperand.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineModuleInfo.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/BasicBlock.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instruction.h"
42*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
43*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
44*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
45*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
46*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker using namespace llvm;
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker static cl::opt<int> PageSize("imp-null-check-page-size",
51*9880d681SAndroid Build Coastguard Worker                              cl::desc("The page size of the target in bytes"),
52*9880d681SAndroid Build Coastguard Worker                              cl::init(4096));
53*9880d681SAndroid Build Coastguard Worker 
54*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "implicit-null-checks"
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker STATISTIC(NumImplicitNullChecks,
57*9880d681SAndroid Build Coastguard Worker           "Number of explicit null checks made implicit");
58*9880d681SAndroid Build Coastguard Worker 
59*9880d681SAndroid Build Coastguard Worker namespace {
60*9880d681SAndroid Build Coastguard Worker 
61*9880d681SAndroid Build Coastguard Worker class ImplicitNullChecks : public MachineFunctionPass {
62*9880d681SAndroid Build Coastguard Worker   /// Represents one null check that can be made implicit.
63*9880d681SAndroid Build Coastguard Worker   class NullCheck {
64*9880d681SAndroid Build Coastguard Worker     // The memory operation the null check can be folded into.
65*9880d681SAndroid Build Coastguard Worker     MachineInstr *MemOperation;
66*9880d681SAndroid Build Coastguard Worker 
67*9880d681SAndroid Build Coastguard Worker     // The instruction actually doing the null check (Ptr != 0).
68*9880d681SAndroid Build Coastguard Worker     MachineInstr *CheckOperation;
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker     // The block the check resides in.
71*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *CheckBlock;
72*9880d681SAndroid Build Coastguard Worker 
73*9880d681SAndroid Build Coastguard Worker     // The block branched to if the pointer is non-null.
74*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *NotNullSucc;
75*9880d681SAndroid Build Coastguard Worker 
76*9880d681SAndroid Build Coastguard Worker     // The block branched to if the pointer is null.
77*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *NullSucc;
78*9880d681SAndroid Build Coastguard Worker 
79*9880d681SAndroid Build Coastguard Worker     // If this is non-null, then MemOperation has a dependency on on this
80*9880d681SAndroid Build Coastguard Worker     // instruction; and it needs to be hoisted to execute before MemOperation.
81*9880d681SAndroid Build Coastguard Worker     MachineInstr *OnlyDependency;
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker   public:
NullCheck(MachineInstr * memOperation,MachineInstr * checkOperation,MachineBasicBlock * checkBlock,MachineBasicBlock * notNullSucc,MachineBasicBlock * nullSucc,MachineInstr * onlyDependency)84*9880d681SAndroid Build Coastguard Worker     explicit NullCheck(MachineInstr *memOperation, MachineInstr *checkOperation,
85*9880d681SAndroid Build Coastguard Worker                        MachineBasicBlock *checkBlock,
86*9880d681SAndroid Build Coastguard Worker                        MachineBasicBlock *notNullSucc,
87*9880d681SAndroid Build Coastguard Worker                        MachineBasicBlock *nullSucc,
88*9880d681SAndroid Build Coastguard Worker                        MachineInstr *onlyDependency)
89*9880d681SAndroid Build Coastguard Worker         : MemOperation(memOperation), CheckOperation(checkOperation),
90*9880d681SAndroid Build Coastguard Worker           CheckBlock(checkBlock), NotNullSucc(notNullSucc), NullSucc(nullSucc),
91*9880d681SAndroid Build Coastguard Worker           OnlyDependency(onlyDependency) {}
92*9880d681SAndroid Build Coastguard Worker 
getMemOperation() const93*9880d681SAndroid Build Coastguard Worker     MachineInstr *getMemOperation() const { return MemOperation; }
94*9880d681SAndroid Build Coastguard Worker 
getCheckOperation() const95*9880d681SAndroid Build Coastguard Worker     MachineInstr *getCheckOperation() const { return CheckOperation; }
96*9880d681SAndroid Build Coastguard Worker 
getCheckBlock() const97*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *getCheckBlock() const { return CheckBlock; }
98*9880d681SAndroid Build Coastguard Worker 
getNotNullSucc() const99*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *getNotNullSucc() const { return NotNullSucc; }
100*9880d681SAndroid Build Coastguard Worker 
getNullSucc() const101*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *getNullSucc() const { return NullSucc; }
102*9880d681SAndroid Build Coastguard Worker 
getOnlyDependency() const103*9880d681SAndroid Build Coastguard Worker     MachineInstr *getOnlyDependency() const { return OnlyDependency; }
104*9880d681SAndroid Build Coastguard Worker   };
105*9880d681SAndroid Build Coastguard Worker 
106*9880d681SAndroid Build Coastguard Worker   const TargetInstrInfo *TII = nullptr;
107*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI = nullptr;
108*9880d681SAndroid Build Coastguard Worker   AliasAnalysis *AA = nullptr;
109*9880d681SAndroid Build Coastguard Worker   MachineModuleInfo *MMI = nullptr;
110*9880d681SAndroid Build Coastguard Worker 
111*9880d681SAndroid Build Coastguard Worker   bool analyzeBlockForNullChecks(MachineBasicBlock &MBB,
112*9880d681SAndroid Build Coastguard Worker                                  SmallVectorImpl<NullCheck> &NullCheckList);
113*9880d681SAndroid Build Coastguard Worker   MachineInstr *insertFaultingLoad(MachineInstr *LoadMI, MachineBasicBlock *MBB,
114*9880d681SAndroid Build Coastguard Worker                                    MachineBasicBlock *HandlerMBB);
115*9880d681SAndroid Build Coastguard Worker   void rewriteNullChecks(ArrayRef<NullCheck> NullCheckList);
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker public:
118*9880d681SAndroid Build Coastguard Worker   static char ID;
119*9880d681SAndroid Build Coastguard Worker 
ImplicitNullChecks()120*9880d681SAndroid Build Coastguard Worker   ImplicitNullChecks() : MachineFunctionPass(ID) {
121*9880d681SAndroid Build Coastguard Worker     initializeImplicitNullChecksPass(*PassRegistry::getPassRegistry());
122*9880d681SAndroid Build Coastguard Worker   }
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &MF) override;
getAnalysisUsage(AnalysisUsage & AU) const125*9880d681SAndroid Build Coastguard Worker   void getAnalysisUsage(AnalysisUsage &AU) const override {
126*9880d681SAndroid Build Coastguard Worker     AU.addRequired<AAResultsWrapperPass>();
127*9880d681SAndroid Build Coastguard Worker     MachineFunctionPass::getAnalysisUsage(AU);
128*9880d681SAndroid Build Coastguard Worker   }
129*9880d681SAndroid Build Coastguard Worker 
getRequiredProperties() const130*9880d681SAndroid Build Coastguard Worker   MachineFunctionProperties getRequiredProperties() const override {
131*9880d681SAndroid Build Coastguard Worker     return MachineFunctionProperties().set(
132*9880d681SAndroid Build Coastguard Worker         MachineFunctionProperties::Property::AllVRegsAllocated);
133*9880d681SAndroid Build Coastguard Worker   }
134*9880d681SAndroid Build Coastguard Worker };
135*9880d681SAndroid Build Coastguard Worker 
136*9880d681SAndroid Build Coastguard Worker /// \brief Detect re-ordering hazards and dependencies.
137*9880d681SAndroid Build Coastguard Worker ///
138*9880d681SAndroid Build Coastguard Worker /// This class keeps track of defs and uses, and can be queried if a given
139*9880d681SAndroid Build Coastguard Worker /// machine instruction can be re-ordered from after the machine instructions
140*9880d681SAndroid Build Coastguard Worker /// seen so far to before them.
141*9880d681SAndroid Build Coastguard Worker class HazardDetector {
getUnknownMI()142*9880d681SAndroid Build Coastguard Worker   static MachineInstr *getUnknownMI() {
143*9880d681SAndroid Build Coastguard Worker     return DenseMapInfo<MachineInstr *>::getTombstoneKey();
144*9880d681SAndroid Build Coastguard Worker   }
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker   // Maps physical registers to the instruction defining them.  If there has
147*9880d681SAndroid Build Coastguard Worker   // been more than one def of an specific register, that register is mapped to
148*9880d681SAndroid Build Coastguard Worker   // getUnknownMI().
149*9880d681SAndroid Build Coastguard Worker   DenseMap<unsigned, MachineInstr *> RegDefs;
150*9880d681SAndroid Build Coastguard Worker   DenseSet<unsigned> RegUses;
151*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo &TRI;
152*9880d681SAndroid Build Coastguard Worker   bool hasSeenClobber;
153*9880d681SAndroid Build Coastguard Worker   AliasAnalysis &AA;
154*9880d681SAndroid Build Coastguard Worker 
155*9880d681SAndroid Build Coastguard Worker public:
HazardDetector(const TargetRegisterInfo & TRI,AliasAnalysis & AA)156*9880d681SAndroid Build Coastguard Worker   explicit HazardDetector(const TargetRegisterInfo &TRI, AliasAnalysis &AA)
157*9880d681SAndroid Build Coastguard Worker       : TRI(TRI), hasSeenClobber(false), AA(AA) {}
158*9880d681SAndroid Build Coastguard Worker 
159*9880d681SAndroid Build Coastguard Worker   /// \brief Make a note of \p MI for later queries to isSafeToHoist.
160*9880d681SAndroid Build Coastguard Worker   ///
161*9880d681SAndroid Build Coastguard Worker   /// May clobber this HazardDetector instance.  \see isClobbered.
162*9880d681SAndroid Build Coastguard Worker   void rememberInstruction(MachineInstr *MI);
163*9880d681SAndroid Build Coastguard Worker 
164*9880d681SAndroid Build Coastguard Worker   /// \brief Return true if it is safe to hoist \p MI from after all the
165*9880d681SAndroid Build Coastguard Worker   /// instructions seen so far (via rememberInstruction) to before it.  If \p MI
166*9880d681SAndroid Build Coastguard Worker   /// has one and only one transitive dependency, set \p Dependency to that
167*9880d681SAndroid Build Coastguard Worker   /// instruction.  If there are more dependencies, return false.
168*9880d681SAndroid Build Coastguard Worker   bool isSafeToHoist(MachineInstr *MI, MachineInstr *&Dependency);
169*9880d681SAndroid Build Coastguard Worker 
170*9880d681SAndroid Build Coastguard Worker   /// \brief Return true if this instance of HazardDetector has been clobbered
171*9880d681SAndroid Build Coastguard Worker   /// (i.e. has no more useful information).
172*9880d681SAndroid Build Coastguard Worker   ///
173*9880d681SAndroid Build Coastguard Worker   /// A HazardDetecter is clobbered when it sees a construct it cannot
174*9880d681SAndroid Build Coastguard Worker   /// understand, and it would have to return a conservative answer for all
175*9880d681SAndroid Build Coastguard Worker   /// future queries.  Having a separate clobbered state lets the client code
176*9880d681SAndroid Build Coastguard Worker   /// bail early, without making queries about all of the future instructions
177*9880d681SAndroid Build Coastguard Worker   /// (which would have returned the most conservative answer anyway).
178*9880d681SAndroid Build Coastguard Worker   ///
179*9880d681SAndroid Build Coastguard Worker   /// Calling rememberInstruction or isSafeToHoist on a clobbered HazardDetector
180*9880d681SAndroid Build Coastguard Worker   /// is an error.
isClobbered()181*9880d681SAndroid Build Coastguard Worker   bool isClobbered() { return hasSeenClobber; }
182*9880d681SAndroid Build Coastguard Worker };
183*9880d681SAndroid Build Coastguard Worker }
184*9880d681SAndroid Build Coastguard Worker 
185*9880d681SAndroid Build Coastguard Worker 
rememberInstruction(MachineInstr * MI)186*9880d681SAndroid Build Coastguard Worker void HazardDetector::rememberInstruction(MachineInstr *MI) {
187*9880d681SAndroid Build Coastguard Worker   assert(!isClobbered() &&
188*9880d681SAndroid Build Coastguard Worker          "Don't add instructions to a clobbered hazard detector");
189*9880d681SAndroid Build Coastguard Worker 
190*9880d681SAndroid Build Coastguard Worker   if (MI->mayStore() || MI->hasUnmodeledSideEffects()) {
191*9880d681SAndroid Build Coastguard Worker     hasSeenClobber = true;
192*9880d681SAndroid Build Coastguard Worker     return;
193*9880d681SAndroid Build Coastguard Worker   }
194*9880d681SAndroid Build Coastguard Worker 
195*9880d681SAndroid Build Coastguard Worker   for (auto *MMO : MI->memoperands()) {
196*9880d681SAndroid Build Coastguard Worker     // Right now we don't want to worry about LLVM's memory model.
197*9880d681SAndroid Build Coastguard Worker     if (!MMO->isUnordered()) {
198*9880d681SAndroid Build Coastguard Worker       hasSeenClobber = true;
199*9880d681SAndroid Build Coastguard Worker       return;
200*9880d681SAndroid Build Coastguard Worker     }
201*9880d681SAndroid Build Coastguard Worker   }
202*9880d681SAndroid Build Coastguard Worker 
203*9880d681SAndroid Build Coastguard Worker   for (auto &MO : MI->operands()) {
204*9880d681SAndroid Build Coastguard Worker     if (!MO.isReg() || !MO.getReg())
205*9880d681SAndroid Build Coastguard Worker       continue;
206*9880d681SAndroid Build Coastguard Worker 
207*9880d681SAndroid Build Coastguard Worker     if (MO.isDef()) {
208*9880d681SAndroid Build Coastguard Worker       auto It = RegDefs.find(MO.getReg());
209*9880d681SAndroid Build Coastguard Worker       if (It == RegDefs.end())
210*9880d681SAndroid Build Coastguard Worker         RegDefs.insert({MO.getReg(), MI});
211*9880d681SAndroid Build Coastguard Worker       else {
212*9880d681SAndroid Build Coastguard Worker         assert(It->second && "Found null MI?");
213*9880d681SAndroid Build Coastguard Worker         It->second = getUnknownMI();
214*9880d681SAndroid Build Coastguard Worker       }
215*9880d681SAndroid Build Coastguard Worker     } else
216*9880d681SAndroid Build Coastguard Worker       RegUses.insert(MO.getReg());
217*9880d681SAndroid Build Coastguard Worker   }
218*9880d681SAndroid Build Coastguard Worker }
219*9880d681SAndroid Build Coastguard Worker 
isSafeToHoist(MachineInstr * MI,MachineInstr * & Dependency)220*9880d681SAndroid Build Coastguard Worker bool HazardDetector::isSafeToHoist(MachineInstr *MI,
221*9880d681SAndroid Build Coastguard Worker                                    MachineInstr *&Dependency) {
222*9880d681SAndroid Build Coastguard Worker   assert(!isClobbered() && "isSafeToHoist cannot do anything useful!");
223*9880d681SAndroid Build Coastguard Worker   Dependency = nullptr;
224*9880d681SAndroid Build Coastguard Worker 
225*9880d681SAndroid Build Coastguard Worker   // Right now we don't want to worry about LLVM's memory model.  This can be
226*9880d681SAndroid Build Coastguard Worker   // made more precise later.
227*9880d681SAndroid Build Coastguard Worker   for (auto *MMO : MI->memoperands())
228*9880d681SAndroid Build Coastguard Worker     if (!MMO->isUnordered())
229*9880d681SAndroid Build Coastguard Worker       return false;
230*9880d681SAndroid Build Coastguard Worker 
231*9880d681SAndroid Build Coastguard Worker   for (auto &MO : MI->operands()) {
232*9880d681SAndroid Build Coastguard Worker     if (MO.isReg() && MO.getReg()) {
233*9880d681SAndroid Build Coastguard Worker       for (auto &RegDef : RegDefs) {
234*9880d681SAndroid Build Coastguard Worker         unsigned Reg = RegDef.first;
235*9880d681SAndroid Build Coastguard Worker         MachineInstr *MI = RegDef.second;
236*9880d681SAndroid Build Coastguard Worker         if (!TRI.regsOverlap(Reg, MO.getReg()))
237*9880d681SAndroid Build Coastguard Worker           continue;
238*9880d681SAndroid Build Coastguard Worker 
239*9880d681SAndroid Build Coastguard Worker         // We found a write-after-write or read-after-write, see if the
240*9880d681SAndroid Build Coastguard Worker         // instruction causing this dependency can be hoisted too.
241*9880d681SAndroid Build Coastguard Worker 
242*9880d681SAndroid Build Coastguard Worker         if (MI == getUnknownMI())
243*9880d681SAndroid Build Coastguard Worker           // We don't have precise dependency information.
244*9880d681SAndroid Build Coastguard Worker           return false;
245*9880d681SAndroid Build Coastguard Worker 
246*9880d681SAndroid Build Coastguard Worker         if (Dependency) {
247*9880d681SAndroid Build Coastguard Worker           if (Dependency == MI)
248*9880d681SAndroid Build Coastguard Worker             continue;
249*9880d681SAndroid Build Coastguard Worker           // We already have one dependency, and we can track only one.
250*9880d681SAndroid Build Coastguard Worker           return false;
251*9880d681SAndroid Build Coastguard Worker         }
252*9880d681SAndroid Build Coastguard Worker 
253*9880d681SAndroid Build Coastguard Worker         // Now check if MI is actually a dependency that can be hoisted.
254*9880d681SAndroid Build Coastguard Worker 
255*9880d681SAndroid Build Coastguard Worker         // We don't want to track transitive dependencies.  We already know that
256*9880d681SAndroid Build Coastguard Worker         // MI is the only instruction that defines Reg, but we need to be sure
257*9880d681SAndroid Build Coastguard Worker         // that it does not use any registers that have been defined (trivially
258*9880d681SAndroid Build Coastguard Worker         // checked below by ensuring that there are no register uses), and that
259*9880d681SAndroid Build Coastguard Worker         // it is the only def for every register it defines (otherwise we could
260*9880d681SAndroid Build Coastguard Worker         // violate a write after write hazard).
261*9880d681SAndroid Build Coastguard Worker         auto IsMIOperandSafe = [&](MachineOperand &MO) {
262*9880d681SAndroid Build Coastguard Worker           if (!MO.isReg() || !MO.getReg())
263*9880d681SAndroid Build Coastguard Worker             return true;
264*9880d681SAndroid Build Coastguard Worker           if (MO.isUse())
265*9880d681SAndroid Build Coastguard Worker             return false;
266*9880d681SAndroid Build Coastguard Worker           assert((!MO.isDef() || RegDefs.count(MO.getReg())) &&
267*9880d681SAndroid Build Coastguard Worker                  "All defs must be tracked in RegDefs by now!");
268*9880d681SAndroid Build Coastguard Worker           return !MO.isDef() || RegDefs.find(MO.getReg())->second == MI;
269*9880d681SAndroid Build Coastguard Worker         };
270*9880d681SAndroid Build Coastguard Worker 
271*9880d681SAndroid Build Coastguard Worker         if (!all_of(MI->operands(), IsMIOperandSafe))
272*9880d681SAndroid Build Coastguard Worker           return false;
273*9880d681SAndroid Build Coastguard Worker 
274*9880d681SAndroid Build Coastguard Worker         // Now check for speculation safety:
275*9880d681SAndroid Build Coastguard Worker         bool SawStore = true;
276*9880d681SAndroid Build Coastguard Worker         if (!MI->isSafeToMove(&AA, SawStore) || MI->mayLoad())
277*9880d681SAndroid Build Coastguard Worker           return false;
278*9880d681SAndroid Build Coastguard Worker 
279*9880d681SAndroid Build Coastguard Worker         Dependency = MI;
280*9880d681SAndroid Build Coastguard Worker       }
281*9880d681SAndroid Build Coastguard Worker 
282*9880d681SAndroid Build Coastguard Worker       if (MO.isDef())
283*9880d681SAndroid Build Coastguard Worker         for (unsigned Reg : RegUses)
284*9880d681SAndroid Build Coastguard Worker           if (TRI.regsOverlap(Reg, MO.getReg()))
285*9880d681SAndroid Build Coastguard Worker             return false;  // We found a write-after-read
286*9880d681SAndroid Build Coastguard Worker     }
287*9880d681SAndroid Build Coastguard Worker   }
288*9880d681SAndroid Build Coastguard Worker 
289*9880d681SAndroid Build Coastguard Worker   return true;
290*9880d681SAndroid Build Coastguard Worker }
291*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & MF)292*9880d681SAndroid Build Coastguard Worker bool ImplicitNullChecks::runOnMachineFunction(MachineFunction &MF) {
293*9880d681SAndroid Build Coastguard Worker   TII = MF.getSubtarget().getInstrInfo();
294*9880d681SAndroid Build Coastguard Worker   TRI = MF.getRegInfo().getTargetRegisterInfo();
295*9880d681SAndroid Build Coastguard Worker   MMI = &MF.getMMI();
296*9880d681SAndroid Build Coastguard Worker   AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
297*9880d681SAndroid Build Coastguard Worker 
298*9880d681SAndroid Build Coastguard Worker   SmallVector<NullCheck, 16> NullCheckList;
299*9880d681SAndroid Build Coastguard Worker 
300*9880d681SAndroid Build Coastguard Worker   for (auto &MBB : MF)
301*9880d681SAndroid Build Coastguard Worker     analyzeBlockForNullChecks(MBB, NullCheckList);
302*9880d681SAndroid Build Coastguard Worker 
303*9880d681SAndroid Build Coastguard Worker   if (!NullCheckList.empty())
304*9880d681SAndroid Build Coastguard Worker     rewriteNullChecks(NullCheckList);
305*9880d681SAndroid Build Coastguard Worker 
306*9880d681SAndroid Build Coastguard Worker   return !NullCheckList.empty();
307*9880d681SAndroid Build Coastguard Worker }
308*9880d681SAndroid Build Coastguard Worker 
309*9880d681SAndroid Build Coastguard Worker // Return true if any register aliasing \p Reg is live-in into \p MBB.
AnyAliasLiveIn(const TargetRegisterInfo * TRI,MachineBasicBlock * MBB,unsigned Reg)310*9880d681SAndroid Build Coastguard Worker static bool AnyAliasLiveIn(const TargetRegisterInfo *TRI,
311*9880d681SAndroid Build Coastguard Worker                            MachineBasicBlock *MBB, unsigned Reg) {
312*9880d681SAndroid Build Coastguard Worker   for (MCRegAliasIterator AR(Reg, TRI, /*IncludeSelf*/ true); AR.isValid();
313*9880d681SAndroid Build Coastguard Worker        ++AR)
314*9880d681SAndroid Build Coastguard Worker     if (MBB->isLiveIn(*AR))
315*9880d681SAndroid Build Coastguard Worker       return true;
316*9880d681SAndroid Build Coastguard Worker   return false;
317*9880d681SAndroid Build Coastguard Worker }
318*9880d681SAndroid Build Coastguard Worker 
319*9880d681SAndroid Build Coastguard Worker /// Analyze MBB to check if its terminating branch can be turned into an
320*9880d681SAndroid Build Coastguard Worker /// implicit null check.  If yes, append a description of the said null check to
321*9880d681SAndroid Build Coastguard Worker /// NullCheckList and return true, else return false.
analyzeBlockForNullChecks(MachineBasicBlock & MBB,SmallVectorImpl<NullCheck> & NullCheckList)322*9880d681SAndroid Build Coastguard Worker bool ImplicitNullChecks::analyzeBlockForNullChecks(
323*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock &MBB, SmallVectorImpl<NullCheck> &NullCheckList) {
324*9880d681SAndroid Build Coastguard Worker   typedef TargetInstrInfo::MachineBranchPredicate MachineBranchPredicate;
325*9880d681SAndroid Build Coastguard Worker 
326*9880d681SAndroid Build Coastguard Worker   MDNode *BranchMD = nullptr;
327*9880d681SAndroid Build Coastguard Worker   if (auto *BB = MBB.getBasicBlock())
328*9880d681SAndroid Build Coastguard Worker     BranchMD = BB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit);
329*9880d681SAndroid Build Coastguard Worker 
330*9880d681SAndroid Build Coastguard Worker   if (!BranchMD)
331*9880d681SAndroid Build Coastguard Worker     return false;
332*9880d681SAndroid Build Coastguard Worker 
333*9880d681SAndroid Build Coastguard Worker   MachineBranchPredicate MBP;
334*9880d681SAndroid Build Coastguard Worker 
335*9880d681SAndroid Build Coastguard Worker   if (TII->analyzeBranchPredicate(MBB, MBP, true))
336*9880d681SAndroid Build Coastguard Worker     return false;
337*9880d681SAndroid Build Coastguard Worker 
338*9880d681SAndroid Build Coastguard Worker   // Is the predicate comparing an integer to zero?
339*9880d681SAndroid Build Coastguard Worker   if (!(MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
340*9880d681SAndroid Build Coastguard Worker         (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
341*9880d681SAndroid Build Coastguard Worker          MBP.Predicate == MachineBranchPredicate::PRED_EQ)))
342*9880d681SAndroid Build Coastguard Worker     return false;
343*9880d681SAndroid Build Coastguard Worker 
344*9880d681SAndroid Build Coastguard Worker   // If we cannot erase the test instruction itself, then making the null check
345*9880d681SAndroid Build Coastguard Worker   // implicit does not buy us much.
346*9880d681SAndroid Build Coastguard Worker   if (!MBP.SingleUseCondition)
347*9880d681SAndroid Build Coastguard Worker     return false;
348*9880d681SAndroid Build Coastguard Worker 
349*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *NotNullSucc, *NullSucc;
350*9880d681SAndroid Build Coastguard Worker 
351*9880d681SAndroid Build Coastguard Worker   if (MBP.Predicate == MachineBranchPredicate::PRED_NE) {
352*9880d681SAndroid Build Coastguard Worker     NotNullSucc = MBP.TrueDest;
353*9880d681SAndroid Build Coastguard Worker     NullSucc = MBP.FalseDest;
354*9880d681SAndroid Build Coastguard Worker   } else {
355*9880d681SAndroid Build Coastguard Worker     NotNullSucc = MBP.FalseDest;
356*9880d681SAndroid Build Coastguard Worker     NullSucc = MBP.TrueDest;
357*9880d681SAndroid Build Coastguard Worker   }
358*9880d681SAndroid Build Coastguard Worker 
359*9880d681SAndroid Build Coastguard Worker   // We handle the simplest case for now.  We can potentially do better by using
360*9880d681SAndroid Build Coastguard Worker   // the machine dominator tree.
361*9880d681SAndroid Build Coastguard Worker   if (NotNullSucc->pred_size() != 1)
362*9880d681SAndroid Build Coastguard Worker     return false;
363*9880d681SAndroid Build Coastguard Worker 
364*9880d681SAndroid Build Coastguard Worker   // Starting with a code fragment like:
365*9880d681SAndroid Build Coastguard Worker   //
366*9880d681SAndroid Build Coastguard Worker   //   test %RAX, %RAX
367*9880d681SAndroid Build Coastguard Worker   //   jne LblNotNull
368*9880d681SAndroid Build Coastguard Worker   //
369*9880d681SAndroid Build Coastguard Worker   //  LblNull:
370*9880d681SAndroid Build Coastguard Worker   //   callq throw_NullPointerException
371*9880d681SAndroid Build Coastguard Worker   //
372*9880d681SAndroid Build Coastguard Worker   //  LblNotNull:
373*9880d681SAndroid Build Coastguard Worker   //   Inst0
374*9880d681SAndroid Build Coastguard Worker   //   Inst1
375*9880d681SAndroid Build Coastguard Worker   //   ...
376*9880d681SAndroid Build Coastguard Worker   //   Def = Load (%RAX + <offset>)
377*9880d681SAndroid Build Coastguard Worker   //   ...
378*9880d681SAndroid Build Coastguard Worker   //
379*9880d681SAndroid Build Coastguard Worker   //
380*9880d681SAndroid Build Coastguard Worker   // we want to end up with
381*9880d681SAndroid Build Coastguard Worker   //
382*9880d681SAndroid Build Coastguard Worker   //   Def = FaultingLoad (%RAX + <offset>), LblNull
383*9880d681SAndroid Build Coastguard Worker   //   jmp LblNotNull ;; explicit or fallthrough
384*9880d681SAndroid Build Coastguard Worker   //
385*9880d681SAndroid Build Coastguard Worker   //  LblNotNull:
386*9880d681SAndroid Build Coastguard Worker   //   Inst0
387*9880d681SAndroid Build Coastguard Worker   //   Inst1
388*9880d681SAndroid Build Coastguard Worker   //   ...
389*9880d681SAndroid Build Coastguard Worker   //
390*9880d681SAndroid Build Coastguard Worker   //  LblNull:
391*9880d681SAndroid Build Coastguard Worker   //   callq throw_NullPointerException
392*9880d681SAndroid Build Coastguard Worker   //
393*9880d681SAndroid Build Coastguard Worker   //
394*9880d681SAndroid Build Coastguard Worker   // To see why this is legal, consider the two possibilities:
395*9880d681SAndroid Build Coastguard Worker   //
396*9880d681SAndroid Build Coastguard Worker   //  1. %RAX is null: since we constrain <offset> to be less than PageSize, the
397*9880d681SAndroid Build Coastguard Worker   //     load instruction dereferences the null page, causing a segmentation
398*9880d681SAndroid Build Coastguard Worker   //     fault.
399*9880d681SAndroid Build Coastguard Worker   //
400*9880d681SAndroid Build Coastguard Worker   //  2. %RAX is not null: in this case we know that the load cannot fault, as
401*9880d681SAndroid Build Coastguard Worker   //     otherwise the load would've faulted in the original program too and the
402*9880d681SAndroid Build Coastguard Worker   //     original program would've been undefined.
403*9880d681SAndroid Build Coastguard Worker   //
404*9880d681SAndroid Build Coastguard Worker   // This reasoning cannot be extended to justify hoisting through arbitrary
405*9880d681SAndroid Build Coastguard Worker   // control flow.  For instance, in the example below (in pseudo-C)
406*9880d681SAndroid Build Coastguard Worker   //
407*9880d681SAndroid Build Coastguard Worker   //    if (ptr == null) { throw_npe(); unreachable; }
408*9880d681SAndroid Build Coastguard Worker   //    if (some_cond) { return 42; }
409*9880d681SAndroid Build Coastguard Worker   //    v = ptr->field;  // LD
410*9880d681SAndroid Build Coastguard Worker   //    ...
411*9880d681SAndroid Build Coastguard Worker   //
412*9880d681SAndroid Build Coastguard Worker   // we cannot (without code duplication) use the load marked "LD" to null check
413*9880d681SAndroid Build Coastguard Worker   // ptr -- clause (2) above does not apply in this case.  In the above program
414*9880d681SAndroid Build Coastguard Worker   // the safety of ptr->field can be dependent on some_cond; and, for instance,
415*9880d681SAndroid Build Coastguard Worker   // ptr could be some non-null invalid reference that never gets loaded from
416*9880d681SAndroid Build Coastguard Worker   // because some_cond is always true.
417*9880d681SAndroid Build Coastguard Worker 
418*9880d681SAndroid Build Coastguard Worker   unsigned PointerReg = MBP.LHS.getReg();
419*9880d681SAndroid Build Coastguard Worker 
420*9880d681SAndroid Build Coastguard Worker   HazardDetector HD(*TRI, *AA);
421*9880d681SAndroid Build Coastguard Worker 
422*9880d681SAndroid Build Coastguard Worker   for (auto MII = NotNullSucc->begin(), MIE = NotNullSucc->end(); MII != MIE;
423*9880d681SAndroid Build Coastguard Worker        ++MII) {
424*9880d681SAndroid Build Coastguard Worker     MachineInstr &MI = *MII;
425*9880d681SAndroid Build Coastguard Worker     unsigned BaseReg;
426*9880d681SAndroid Build Coastguard Worker     int64_t Offset;
427*9880d681SAndroid Build Coastguard Worker     MachineInstr *Dependency = nullptr;
428*9880d681SAndroid Build Coastguard Worker     if (TII->getMemOpBaseRegImmOfs(MI, BaseReg, Offset, TRI))
429*9880d681SAndroid Build Coastguard Worker       if (MI.mayLoad() && !MI.isPredicable() && BaseReg == PointerReg &&
430*9880d681SAndroid Build Coastguard Worker           Offset < PageSize && MI.getDesc().getNumDefs() <= 1 &&
431*9880d681SAndroid Build Coastguard Worker           HD.isSafeToHoist(&MI, Dependency)) {
432*9880d681SAndroid Build Coastguard Worker 
433*9880d681SAndroid Build Coastguard Worker         auto DependencyOperandIsOk = [&](MachineOperand &MO) {
434*9880d681SAndroid Build Coastguard Worker           assert(!(MO.isReg() && MO.isUse()) &&
435*9880d681SAndroid Build Coastguard Worker                  "No transitive dependendencies please!");
436*9880d681SAndroid Build Coastguard Worker           if (!MO.isReg() || !MO.getReg() || !MO.isDef())
437*9880d681SAndroid Build Coastguard Worker             return true;
438*9880d681SAndroid Build Coastguard Worker 
439*9880d681SAndroid Build Coastguard Worker           // Make sure that we won't clobber any live ins to the sibling block
440*9880d681SAndroid Build Coastguard Worker           // by hoisting Dependency.  For instance, we can't hoist INST to
441*9880d681SAndroid Build Coastguard Worker           // before the null check (even if it safe, and does not violate any
442*9880d681SAndroid Build Coastguard Worker           // dependencies in the non_null_block) if %rdx is live in to
443*9880d681SAndroid Build Coastguard Worker           // _null_block.
444*9880d681SAndroid Build Coastguard Worker           //
445*9880d681SAndroid Build Coastguard Worker           //    test %rcx, %rcx
446*9880d681SAndroid Build Coastguard Worker           //    je _null_block
447*9880d681SAndroid Build Coastguard Worker           //  _non_null_block:
448*9880d681SAndroid Build Coastguard Worker           //    %rdx<def> = INST
449*9880d681SAndroid Build Coastguard Worker           //    ...
450*9880d681SAndroid Build Coastguard Worker           if (AnyAliasLiveIn(TRI, NullSucc, MO.getReg()))
451*9880d681SAndroid Build Coastguard Worker             return false;
452*9880d681SAndroid Build Coastguard Worker 
453*9880d681SAndroid Build Coastguard Worker           // Make sure Dependency isn't re-defining the base register.  Then we
454*9880d681SAndroid Build Coastguard Worker           // won't get the memory operation on the address we want.
455*9880d681SAndroid Build Coastguard Worker           if (TRI->regsOverlap(MO.getReg(), BaseReg))
456*9880d681SAndroid Build Coastguard Worker             return false;
457*9880d681SAndroid Build Coastguard Worker 
458*9880d681SAndroid Build Coastguard Worker           return true;
459*9880d681SAndroid Build Coastguard Worker         };
460*9880d681SAndroid Build Coastguard Worker 
461*9880d681SAndroid Build Coastguard Worker         bool DependencyOperandsAreOk =
462*9880d681SAndroid Build Coastguard Worker             !Dependency ||
463*9880d681SAndroid Build Coastguard Worker             all_of(Dependency->operands(), DependencyOperandIsOk);
464*9880d681SAndroid Build Coastguard Worker 
465*9880d681SAndroid Build Coastguard Worker         if (DependencyOperandsAreOk) {
466*9880d681SAndroid Build Coastguard Worker           NullCheckList.emplace_back(&MI, MBP.ConditionDef, &MBB, NotNullSucc,
467*9880d681SAndroid Build Coastguard Worker                                      NullSucc, Dependency);
468*9880d681SAndroid Build Coastguard Worker           return true;
469*9880d681SAndroid Build Coastguard Worker         }
470*9880d681SAndroid Build Coastguard Worker       }
471*9880d681SAndroid Build Coastguard Worker 
472*9880d681SAndroid Build Coastguard Worker     HD.rememberInstruction(&MI);
473*9880d681SAndroid Build Coastguard Worker     if (HD.isClobbered())
474*9880d681SAndroid Build Coastguard Worker       return false;
475*9880d681SAndroid Build Coastguard Worker   }
476*9880d681SAndroid Build Coastguard Worker 
477*9880d681SAndroid Build Coastguard Worker   return false;
478*9880d681SAndroid Build Coastguard Worker }
479*9880d681SAndroid Build Coastguard Worker 
480*9880d681SAndroid Build Coastguard Worker /// Wrap a machine load instruction, LoadMI, into a FAULTING_LOAD_OP machine
481*9880d681SAndroid Build Coastguard Worker /// instruction.  The FAULTING_LOAD_OP instruction does the same load as LoadMI
482*9880d681SAndroid Build Coastguard Worker /// (defining the same register), and branches to HandlerMBB if the load
483*9880d681SAndroid Build Coastguard Worker /// faults.  The FAULTING_LOAD_OP instruction is inserted at the end of MBB.
484*9880d681SAndroid Build Coastguard Worker MachineInstr *
insertFaultingLoad(MachineInstr * LoadMI,MachineBasicBlock * MBB,MachineBasicBlock * HandlerMBB)485*9880d681SAndroid Build Coastguard Worker ImplicitNullChecks::insertFaultingLoad(MachineInstr *LoadMI,
486*9880d681SAndroid Build Coastguard Worker                                        MachineBasicBlock *MBB,
487*9880d681SAndroid Build Coastguard Worker                                        MachineBasicBlock *HandlerMBB) {
488*9880d681SAndroid Build Coastguard Worker   const unsigned NoRegister = 0; // Guaranteed to be the NoRegister value for
489*9880d681SAndroid Build Coastguard Worker                                  // all targets.
490*9880d681SAndroid Build Coastguard Worker 
491*9880d681SAndroid Build Coastguard Worker   DebugLoc DL;
492*9880d681SAndroid Build Coastguard Worker   unsigned NumDefs = LoadMI->getDesc().getNumDefs();
493*9880d681SAndroid Build Coastguard Worker   assert(NumDefs <= 1 && "other cases unhandled!");
494*9880d681SAndroid Build Coastguard Worker 
495*9880d681SAndroid Build Coastguard Worker   unsigned DefReg = NoRegister;
496*9880d681SAndroid Build Coastguard Worker   if (NumDefs != 0) {
497*9880d681SAndroid Build Coastguard Worker     DefReg = LoadMI->defs().begin()->getReg();
498*9880d681SAndroid Build Coastguard Worker     assert(std::distance(LoadMI->defs().begin(), LoadMI->defs().end()) == 1 &&
499*9880d681SAndroid Build Coastguard Worker            "expected exactly one def!");
500*9880d681SAndroid Build Coastguard Worker   }
501*9880d681SAndroid Build Coastguard Worker 
502*9880d681SAndroid Build Coastguard Worker   auto MIB = BuildMI(MBB, DL, TII->get(TargetOpcode::FAULTING_LOAD_OP), DefReg)
503*9880d681SAndroid Build Coastguard Worker                  .addMBB(HandlerMBB)
504*9880d681SAndroid Build Coastguard Worker                  .addImm(LoadMI->getOpcode());
505*9880d681SAndroid Build Coastguard Worker 
506*9880d681SAndroid Build Coastguard Worker   for (auto &MO : LoadMI->uses())
507*9880d681SAndroid Build Coastguard Worker     MIB.addOperand(MO);
508*9880d681SAndroid Build Coastguard Worker 
509*9880d681SAndroid Build Coastguard Worker   MIB.setMemRefs(LoadMI->memoperands_begin(), LoadMI->memoperands_end());
510*9880d681SAndroid Build Coastguard Worker 
511*9880d681SAndroid Build Coastguard Worker   return MIB;
512*9880d681SAndroid Build Coastguard Worker }
513*9880d681SAndroid Build Coastguard Worker 
514*9880d681SAndroid Build Coastguard Worker /// Rewrite the null checks in NullCheckList into implicit null checks.
rewriteNullChecks(ArrayRef<ImplicitNullChecks::NullCheck> NullCheckList)515*9880d681SAndroid Build Coastguard Worker void ImplicitNullChecks::rewriteNullChecks(
516*9880d681SAndroid Build Coastguard Worker     ArrayRef<ImplicitNullChecks::NullCheck> NullCheckList) {
517*9880d681SAndroid Build Coastguard Worker   DebugLoc DL;
518*9880d681SAndroid Build Coastguard Worker 
519*9880d681SAndroid Build Coastguard Worker   for (auto &NC : NullCheckList) {
520*9880d681SAndroid Build Coastguard Worker     // Remove the conditional branch dependent on the null check.
521*9880d681SAndroid Build Coastguard Worker     unsigned BranchesRemoved = TII->RemoveBranch(*NC.getCheckBlock());
522*9880d681SAndroid Build Coastguard Worker     (void)BranchesRemoved;
523*9880d681SAndroid Build Coastguard Worker     assert(BranchesRemoved > 0 && "expected at least one branch!");
524*9880d681SAndroid Build Coastguard Worker 
525*9880d681SAndroid Build Coastguard Worker     if (auto *DepMI = NC.getOnlyDependency()) {
526*9880d681SAndroid Build Coastguard Worker       DepMI->removeFromParent();
527*9880d681SAndroid Build Coastguard Worker       NC.getCheckBlock()->insert(NC.getCheckBlock()->end(), DepMI);
528*9880d681SAndroid Build Coastguard Worker     }
529*9880d681SAndroid Build Coastguard Worker 
530*9880d681SAndroid Build Coastguard Worker     // Insert a faulting load where the conditional branch was originally.  We
531*9880d681SAndroid Build Coastguard Worker     // check earlier ensures that this bit of code motion is legal.  We do not
532*9880d681SAndroid Build Coastguard Worker     // touch the successors list for any basic block since we haven't changed
533*9880d681SAndroid Build Coastguard Worker     // control flow, we've just made it implicit.
534*9880d681SAndroid Build Coastguard Worker     MachineInstr *FaultingLoad = insertFaultingLoad(
535*9880d681SAndroid Build Coastguard Worker         NC.getMemOperation(), NC.getCheckBlock(), NC.getNullSucc());
536*9880d681SAndroid Build Coastguard Worker     // Now the values defined by MemOperation, if any, are live-in of
537*9880d681SAndroid Build Coastguard Worker     // the block of MemOperation.
538*9880d681SAndroid Build Coastguard Worker     // The original load operation may define implicit-defs alongside
539*9880d681SAndroid Build Coastguard Worker     // the loaded value.
540*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *MBB = NC.getMemOperation()->getParent();
541*9880d681SAndroid Build Coastguard Worker     for (const MachineOperand &MO : FaultingLoad->operands()) {
542*9880d681SAndroid Build Coastguard Worker       if (!MO.isReg() || !MO.isDef())
543*9880d681SAndroid Build Coastguard Worker         continue;
544*9880d681SAndroid Build Coastguard Worker       unsigned Reg = MO.getReg();
545*9880d681SAndroid Build Coastguard Worker       if (!Reg || MBB->isLiveIn(Reg))
546*9880d681SAndroid Build Coastguard Worker         continue;
547*9880d681SAndroid Build Coastguard Worker       MBB->addLiveIn(Reg);
548*9880d681SAndroid Build Coastguard Worker     }
549*9880d681SAndroid Build Coastguard Worker 
550*9880d681SAndroid Build Coastguard Worker     if (auto *DepMI = NC.getOnlyDependency()) {
551*9880d681SAndroid Build Coastguard Worker       for (auto &MO : DepMI->operands()) {
552*9880d681SAndroid Build Coastguard Worker         if (!MO.isReg() || !MO.getReg() || !MO.isDef())
553*9880d681SAndroid Build Coastguard Worker           continue;
554*9880d681SAndroid Build Coastguard Worker         if (!NC.getNotNullSucc()->isLiveIn(MO.getReg()))
555*9880d681SAndroid Build Coastguard Worker           NC.getNotNullSucc()->addLiveIn(MO.getReg());
556*9880d681SAndroid Build Coastguard Worker       }
557*9880d681SAndroid Build Coastguard Worker     }
558*9880d681SAndroid Build Coastguard Worker 
559*9880d681SAndroid Build Coastguard Worker     NC.getMemOperation()->eraseFromParent();
560*9880d681SAndroid Build Coastguard Worker     NC.getCheckOperation()->eraseFromParent();
561*9880d681SAndroid Build Coastguard Worker 
562*9880d681SAndroid Build Coastguard Worker     // Insert an *unconditional* branch to not-null successor.
563*9880d681SAndroid Build Coastguard Worker     TII->InsertBranch(*NC.getCheckBlock(), NC.getNotNullSucc(), nullptr,
564*9880d681SAndroid Build Coastguard Worker                       /*Cond=*/None, DL);
565*9880d681SAndroid Build Coastguard Worker 
566*9880d681SAndroid Build Coastguard Worker     NumImplicitNullChecks++;
567*9880d681SAndroid Build Coastguard Worker   }
568*9880d681SAndroid Build Coastguard Worker }
569*9880d681SAndroid Build Coastguard Worker 
570*9880d681SAndroid Build Coastguard Worker char ImplicitNullChecks::ID = 0;
571*9880d681SAndroid Build Coastguard Worker char &llvm::ImplicitNullChecksID = ImplicitNullChecks::ID;
572*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(ImplicitNullChecks, "implicit-null-checks",
573*9880d681SAndroid Build Coastguard Worker                       "Implicit null checks", false, false)
574*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
575*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(ImplicitNullChecks, "implicit-null-checks",
576*9880d681SAndroid Build Coastguard Worker                     "Implicit null checks", false, false)
577