blob: 3caa01e8c6dbfab171ed754421732084a8d67162 [file] [log] [blame]
Sanjoy Das69fad072015-06-15 18:44:27 +00001//===-- 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.
25//
26//===----------------------------------------------------------------------===//
27
Sanjoy Dasb7718452015-07-09 20:13:25 +000028#include "llvm/ADT/DenseSet.h"
Sanjoy Das69fad072015-06-15 18:44:27 +000029#include "llvm/ADT/SmallVector.h"
Sanjoy Das8ee6a302015-07-06 23:32:10 +000030#include "llvm/ADT/Statistic.h"
Sanjoy Dase57bf682016-06-22 22:16:51 +000031#include "llvm/Analysis/AliasAnalysis.h"
Sanjoy Das69fad072015-06-15 18:44:27 +000032#include "llvm/CodeGen/Passes.h"
33#include "llvm/CodeGen/MachineFunction.h"
Sanjoy Dasb7718452015-07-09 20:13:25 +000034#include "llvm/CodeGen/MachineMemOperand.h"
Sanjoy Das69fad072015-06-15 18:44:27 +000035#include "llvm/CodeGen/MachineOperand.h"
36#include "llvm/CodeGen/MachineFunctionPass.h"
37#include "llvm/CodeGen/MachineInstrBuilder.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
39#include "llvm/CodeGen/MachineModuleInfo.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Instruction.h"
Chen Li00038782015-08-04 04:41:34 +000042#include "llvm/IR/LLVMContext.h"
Sanjoy Das69fad072015-06-15 18:44:27 +000043#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/Target/TargetSubtargetInfo.h"
46#include "llvm/Target/TargetInstrInfo.h"
47
48using namespace llvm;
49
Chad Rosierc27a18f2016-03-09 16:00:35 +000050static cl::opt<int> PageSize("imp-null-check-page-size",
51 cl::desc("The page size of the target in bytes"),
52 cl::init(4096));
Sanjoy Das69fad072015-06-15 18:44:27 +000053
Sanjoy Das8ee6a302015-07-06 23:32:10 +000054#define DEBUG_TYPE "implicit-null-checks"
55
56STATISTIC(NumImplicitNullChecks,
57 "Number of explicit null checks made implicit");
58
Sanjoy Das69fad072015-06-15 18:44:27 +000059namespace {
60
61class ImplicitNullChecks : public MachineFunctionPass {
62 /// Represents one null check that can be made implicit.
Sanjoy Dase173b9a2016-06-21 02:10:18 +000063 class NullCheck {
Sanjoy Das69fad072015-06-15 18:44:27 +000064 // The memory operation the null check can be folded into.
65 MachineInstr *MemOperation;
66
67 // The instruction actually doing the null check (Ptr != 0).
68 MachineInstr *CheckOperation;
69
70 // The block the check resides in.
71 MachineBasicBlock *CheckBlock;
72
Eric Christopher572e03a2015-06-19 01:53:21 +000073 // The block branched to if the pointer is non-null.
Sanjoy Das69fad072015-06-15 18:44:27 +000074 MachineBasicBlock *NotNullSucc;
75
Eric Christopher572e03a2015-06-19 01:53:21 +000076 // The block branched to if the pointer is null.
Sanjoy Das69fad072015-06-15 18:44:27 +000077 MachineBasicBlock *NullSucc;
78
Sanjoy Dase57bf682016-06-22 22:16:51 +000079 // If this is non-null, then MemOperation has a dependency on on this
80 // instruction; and it needs to be hoisted to execute before MemOperation.
81 MachineInstr *OnlyDependency;
82
Sanjoy Dase173b9a2016-06-21 02:10:18 +000083 public:
Sanjoy Das69fad072015-06-15 18:44:27 +000084 explicit NullCheck(MachineInstr *memOperation, MachineInstr *checkOperation,
85 MachineBasicBlock *checkBlock,
86 MachineBasicBlock *notNullSucc,
Sanjoy Dase57bf682016-06-22 22:16:51 +000087 MachineBasicBlock *nullSucc,
88 MachineInstr *onlyDependency)
Sanjoy Das69fad072015-06-15 18:44:27 +000089 : MemOperation(memOperation), CheckOperation(checkOperation),
Sanjoy Dase57bf682016-06-22 22:16:51 +000090 CheckBlock(checkBlock), NotNullSucc(notNullSucc), NullSucc(nullSucc),
91 OnlyDependency(onlyDependency) {}
Sanjoy Dase173b9a2016-06-21 02:10:18 +000092
93 MachineInstr *getMemOperation() const { return MemOperation; }
94
95 MachineInstr *getCheckOperation() const { return CheckOperation; }
96
97 MachineBasicBlock *getCheckBlock() const { return CheckBlock; }
98
99 MachineBasicBlock *getNotNullSucc() const { return NotNullSucc; }
100
101 MachineBasicBlock *getNullSucc() const { return NullSucc; }
Sanjoy Dase57bf682016-06-22 22:16:51 +0000102
103 MachineInstr *getOnlyDependency() const { return OnlyDependency; }
Sanjoy Das69fad072015-06-15 18:44:27 +0000104 };
105
106 const TargetInstrInfo *TII = nullptr;
107 const TargetRegisterInfo *TRI = nullptr;
Sanjoy Dase57bf682016-06-22 22:16:51 +0000108 AliasAnalysis *AA = nullptr;
Sanjoy Das69fad072015-06-15 18:44:27 +0000109 MachineModuleInfo *MMI = nullptr;
110
111 bool analyzeBlockForNullChecks(MachineBasicBlock &MBB,
112 SmallVectorImpl<NullCheck> &NullCheckList);
113 MachineInstr *insertFaultingLoad(MachineInstr *LoadMI, MachineBasicBlock *MBB,
Quentin Colombet4e1d3892016-05-02 22:58:54 +0000114 MachineBasicBlock *HandlerMBB);
Sanjoy Das69fad072015-06-15 18:44:27 +0000115 void rewriteNullChecks(ArrayRef<NullCheck> NullCheckList);
116
117public:
118 static char ID;
119
120 ImplicitNullChecks() : MachineFunctionPass(ID) {
121 initializeImplicitNullChecksPass(*PassRegistry::getPassRegistry());
122 }
123
124 bool runOnMachineFunction(MachineFunction &MF) override;
Sanjoy Dase57bf682016-06-22 22:16:51 +0000125 void getAnalysisUsage(AnalysisUsage &AU) const override {
126 AU.addRequired<AAResultsWrapperPass>();
127 MachineFunctionPass::getAnalysisUsage(AU);
128 }
Derek Schuffad154c82016-03-28 17:05:30 +0000129
130 MachineFunctionProperties getRequiredProperties() const override {
131 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000132 MachineFunctionProperties::Property::NoVRegs);
Derek Schuffad154c82016-03-28 17:05:30 +0000133 }
Sanjoy Das69fad072015-06-15 18:44:27 +0000134};
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000135
136/// \brief Detect re-ordering hazards and dependencies.
137///
138/// This class keeps track of defs and uses, and can be queried if a given
139/// machine instruction can be re-ordered from after the machine instructions
140/// seen so far to before them.
141class HazardDetector {
Sanjoy Dase57bf682016-06-22 22:16:51 +0000142 static MachineInstr *getUnknownMI() {
143 return DenseMapInfo<MachineInstr *>::getTombstoneKey();
144 }
145
146 // Maps physical registers to the instruction defining them. If there has
147 // been more than one def of an specific register, that register is mapped to
148 // getUnknownMI().
149 DenseMap<unsigned, MachineInstr *> RegDefs;
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000150 DenseSet<unsigned> RegUses;
151 const TargetRegisterInfo &TRI;
152 bool hasSeenClobber;
Sanjoy Dase57bf682016-06-22 22:16:51 +0000153 AliasAnalysis &AA;
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000154
155public:
Sanjoy Dase57bf682016-06-22 22:16:51 +0000156 explicit HazardDetector(const TargetRegisterInfo &TRI, AliasAnalysis &AA)
157 : TRI(TRI), hasSeenClobber(false), AA(AA) {}
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000158
159 /// \brief Make a note of \p MI for later queries to isSafeToHoist.
160 ///
161 /// May clobber this HazardDetector instance. \see isClobbered.
162 void rememberInstruction(MachineInstr *MI);
163
164 /// \brief Return true if it is safe to hoist \p MI from after all the
Sanjoy Dase57bf682016-06-22 22:16:51 +0000165 /// instructions seen so far (via rememberInstruction) to before it. If \p MI
166 /// has one and only one transitive dependency, set \p Dependency to that
167 /// instruction. If there are more dependencies, return false.
168 bool isSafeToHoist(MachineInstr *MI, MachineInstr *&Dependency);
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000169
170 /// \brief Return true if this instance of HazardDetector has been clobbered
171 /// (i.e. has no more useful information).
172 ///
173 /// A HazardDetecter is clobbered when it sees a construct it cannot
174 /// understand, and it would have to return a conservative answer for all
175 /// future queries. Having a separate clobbered state lets the client code
176 /// bail early, without making queries about all of the future instructions
177 /// (which would have returned the most conservative answer anyway).
178 ///
179 /// Calling rememberInstruction or isSafeToHoist on a clobbered HazardDetector
180 /// is an error.
181 bool isClobbered() { return hasSeenClobber; }
182};
183}
184
185
186void HazardDetector::rememberInstruction(MachineInstr *MI) {
187 assert(!isClobbered() &&
188 "Don't add instructions to a clobbered hazard detector");
189
Sanjoy Dasdf4b1622016-11-16 21:45:22 +0000190 // There may be readonly calls that we can handle in theory, but for
191 // now we don't bother since we don't handle callee clobbered
192 // registers.
193 if (MI->isCall() || MI->mayStore() || MI->hasUnmodeledSideEffects()) {
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000194 hasSeenClobber = true;
195 return;
196 }
197
198 for (auto *MMO : MI->memoperands()) {
199 // Right now we don't want to worry about LLVM's memory model.
200 if (!MMO->isUnordered()) {
201 hasSeenClobber = true;
202 return;
203 }
204 }
205
206 for (auto &MO : MI->operands()) {
207 if (!MO.isReg() || !MO.getReg())
208 continue;
209
Sanjoy Dase57bf682016-06-22 22:16:51 +0000210 if (MO.isDef()) {
211 auto It = RegDefs.find(MO.getReg());
212 if (It == RegDefs.end())
213 RegDefs.insert({MO.getReg(), MI});
214 else {
215 assert(It->second && "Found null MI?");
216 It->second = getUnknownMI();
217 }
218 } else
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000219 RegUses.insert(MO.getReg());
220 }
221}
222
Sanjoy Dase57bf682016-06-22 22:16:51 +0000223bool HazardDetector::isSafeToHoist(MachineInstr *MI,
224 MachineInstr *&Dependency) {
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000225 assert(!isClobbered() && "isSafeToHoist cannot do anything useful!");
Sanjoy Dase57bf682016-06-22 22:16:51 +0000226 Dependency = nullptr;
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000227
228 // Right now we don't want to worry about LLVM's memory model. This can be
229 // made more precise later.
230 for (auto *MMO : MI->memoperands())
231 if (!MMO->isUnordered())
232 return false;
233
234 for (auto &MO : MI->operands()) {
235 if (MO.isReg() && MO.getReg()) {
Sanjoy Dase57bf682016-06-22 22:16:51 +0000236 for (auto &RegDef : RegDefs) {
237 unsigned Reg = RegDef.first;
238 MachineInstr *MI = RegDef.second;
239 if (!TRI.regsOverlap(Reg, MO.getReg()))
240 continue;
241
242 // We found a write-after-write or read-after-write, see if the
243 // instruction causing this dependency can be hoisted too.
244
245 if (MI == getUnknownMI())
246 // We don't have precise dependency information.
247 return false;
248
249 if (Dependency) {
250 if (Dependency == MI)
251 continue;
252 // We already have one dependency, and we can track only one.
253 return false;
254 }
255
256 // Now check if MI is actually a dependency that can be hoisted.
257
258 // We don't want to track transitive dependencies. We already know that
259 // MI is the only instruction that defines Reg, but we need to be sure
260 // that it does not use any registers that have been defined (trivially
261 // checked below by ensuring that there are no register uses), and that
262 // it is the only def for every register it defines (otherwise we could
263 // violate a write after write hazard).
264 auto IsMIOperandSafe = [&](MachineOperand &MO) {
265 if (!MO.isReg() || !MO.getReg())
266 return true;
267 if (MO.isUse())
268 return false;
Sanjoy Das43ccb382016-11-17 07:29:43 +0000269 assert(MO.isDef() &&
270 "Register MachineOperands must either be uses or be defs.");
271 assert(RegDefs.count(MO.getReg()) &&
Sanjoy Dase57bf682016-06-22 22:16:51 +0000272 "All defs must be tracked in RegDefs by now!");
Sanjoy Das4a8fe092016-11-17 07:29:40 +0000273
Sanjoy Das4a8fe092016-11-17 07:29:40 +0000274 for (unsigned Reg : RegUses)
275 if (TRI.regsOverlap(Reg, MO.getReg()))
276 return false; // We found a write-after-read
277
278 for (auto &OtherDef : RegDefs) {
279 unsigned OtherReg = OtherDef.first;
280 MachineInstr *OtherMI = OtherDef.second;
281 if (OtherMI != MI && TRI.regsOverlap(OtherReg, MO.getReg()))
282 return false;
283 }
284
285 return true;
Sanjoy Dase57bf682016-06-22 22:16:51 +0000286 };
287
288 if (!all_of(MI->operands(), IsMIOperandSafe))
289 return false;
290
291 // Now check for speculation safety:
292 bool SawStore = true;
293 if (!MI->isSafeToMove(&AA, SawStore) || MI->mayLoad())
294 return false;
295
296 Dependency = MI;
297 }
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000298
299 if (MO.isDef())
300 for (unsigned Reg : RegUses)
301 if (TRI.regsOverlap(Reg, MO.getReg()))
302 return false; // We found a write-after-read
303 }
304 }
305
306 return true;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000307}
Sanjoy Das69fad072015-06-15 18:44:27 +0000308
309bool ImplicitNullChecks::runOnMachineFunction(MachineFunction &MF) {
310 TII = MF.getSubtarget().getInstrInfo();
311 TRI = MF.getRegInfo().getTargetRegisterInfo();
312 MMI = &MF.getMMI();
Sanjoy Dase57bf682016-06-22 22:16:51 +0000313 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Sanjoy Das69fad072015-06-15 18:44:27 +0000314
315 SmallVector<NullCheck, 16> NullCheckList;
316
317 for (auto &MBB : MF)
318 analyzeBlockForNullChecks(MBB, NullCheckList);
319
320 if (!NullCheckList.empty())
321 rewriteNullChecks(NullCheckList);
322
323 return !NullCheckList.empty();
324}
325
Sanjoy Dase57bf682016-06-22 22:16:51 +0000326// Return true if any register aliasing \p Reg is live-in into \p MBB.
327static bool AnyAliasLiveIn(const TargetRegisterInfo *TRI,
328 MachineBasicBlock *MBB, unsigned Reg) {
329 for (MCRegAliasIterator AR(Reg, TRI, /*IncludeSelf*/ true); AR.isValid();
330 ++AR)
331 if (MBB->isLiveIn(*AR))
332 return true;
333 return false;
334}
335
Sanjoy Das69fad072015-06-15 18:44:27 +0000336/// Analyze MBB to check if its terminating branch can be turned into an
337/// implicit null check. If yes, append a description of the said null check to
338/// NullCheckList and return true, else return false.
339bool ImplicitNullChecks::analyzeBlockForNullChecks(
340 MachineBasicBlock &MBB, SmallVectorImpl<NullCheck> &NullCheckList) {
341 typedef TargetInstrInfo::MachineBranchPredicate MachineBranchPredicate;
342
Sanjoy Dase8b81642015-11-12 20:51:49 +0000343 MDNode *BranchMD = nullptr;
344 if (auto *BB = MBB.getBasicBlock())
345 BranchMD = BB->getTerminator()->getMetadata(LLVMContext::MD_make_implicit);
346
Sanjoy Das9c41a932015-06-30 21:22:32 +0000347 if (!BranchMD)
348 return false;
349
Sanjoy Das69fad072015-06-15 18:44:27 +0000350 MachineBranchPredicate MBP;
351
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000352 if (TII->analyzeBranchPredicate(MBB, MBP, true))
Sanjoy Das69fad072015-06-15 18:44:27 +0000353 return false;
354
355 // Is the predicate comparing an integer to zero?
356 if (!(MBP.LHS.isReg() && MBP.RHS.isImm() && MBP.RHS.getImm() == 0 &&
357 (MBP.Predicate == MachineBranchPredicate::PRED_NE ||
358 MBP.Predicate == MachineBranchPredicate::PRED_EQ)))
359 return false;
360
361 // If we cannot erase the test instruction itself, then making the null check
362 // implicit does not buy us much.
363 if (!MBP.SingleUseCondition)
364 return false;
365
366 MachineBasicBlock *NotNullSucc, *NullSucc;
367
368 if (MBP.Predicate == MachineBranchPredicate::PRED_NE) {
369 NotNullSucc = MBP.TrueDest;
370 NullSucc = MBP.FalseDest;
371 } else {
372 NotNullSucc = MBP.FalseDest;
373 NullSucc = MBP.TrueDest;
374 }
375
376 // We handle the simplest case for now. We can potentially do better by using
377 // the machine dominator tree.
378 if (NotNullSucc->pred_size() != 1)
379 return false;
380
381 // Starting with a code fragment like:
382 //
383 // test %RAX, %RAX
384 // jne LblNotNull
385 //
386 // LblNull:
387 // callq throw_NullPointerException
388 //
389 // LblNotNull:
Sanjoy Dasb7718452015-07-09 20:13:25 +0000390 // Inst0
391 // Inst1
392 // ...
Sanjoy Das69fad072015-06-15 18:44:27 +0000393 // Def = Load (%RAX + <offset>)
394 // ...
395 //
396 //
397 // we want to end up with
398 //
Sanjoy Dasac9c5b12015-11-13 08:14:00 +0000399 // Def = FaultingLoad (%RAX + <offset>), LblNull
Sanjoy Das69fad072015-06-15 18:44:27 +0000400 // jmp LblNotNull ;; explicit or fallthrough
401 //
402 // LblNotNull:
Sanjoy Dasb7718452015-07-09 20:13:25 +0000403 // Inst0
404 // Inst1
Sanjoy Das69fad072015-06-15 18:44:27 +0000405 // ...
406 //
407 // LblNull:
408 // callq throw_NullPointerException
409 //
Sanjoy Dasac9c5b12015-11-13 08:14:00 +0000410 //
411 // To see why this is legal, consider the two possibilities:
412 //
413 // 1. %RAX is null: since we constrain <offset> to be less than PageSize, the
414 // load instruction dereferences the null page, causing a segmentation
415 // fault.
416 //
417 // 2. %RAX is not null: in this case we know that the load cannot fault, as
418 // otherwise the load would've faulted in the original program too and the
419 // original program would've been undefined.
420 //
421 // This reasoning cannot be extended to justify hoisting through arbitrary
422 // control flow. For instance, in the example below (in pseudo-C)
423 //
424 // if (ptr == null) { throw_npe(); unreachable; }
425 // if (some_cond) { return 42; }
426 // v = ptr->field; // LD
427 // ...
428 //
429 // we cannot (without code duplication) use the load marked "LD" to null check
430 // ptr -- clause (2) above does not apply in this case. In the above program
431 // the safety of ptr->field can be dependent on some_cond; and, for instance,
432 // ptr could be some non-null invalid reference that never gets loaded from
433 // because some_cond is always true.
Sanjoy Das69fad072015-06-15 18:44:27 +0000434
435 unsigned PointerReg = MBP.LHS.getReg();
Sanjoy Dasb7718452015-07-09 20:13:25 +0000436
Sanjoy Dase57bf682016-06-22 22:16:51 +0000437 HazardDetector HD(*TRI, *AA);
Sanjoy Dasb7718452015-07-09 20:13:25 +0000438
439 for (auto MII = NotNullSucc->begin(), MIE = NotNullSucc->end(); MII != MIE;
440 ++MII) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000441 MachineInstr &MI = *MII;
Chad Rosierc27a18f2016-03-09 16:00:35 +0000442 unsigned BaseReg;
443 int64_t Offset;
Sanjoy Dase57bf682016-06-22 22:16:51 +0000444 MachineInstr *Dependency = nullptr;
Sanjoy Dasb7718452015-07-09 20:13:25 +0000445 if (TII->getMemOpBaseRegImmOfs(MI, BaseReg, Offset, TRI))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000446 if (MI.mayLoad() && !MI.isPredicable() && BaseReg == PointerReg &&
447 Offset < PageSize && MI.getDesc().getNumDefs() <= 1 &&
448 HD.isSafeToHoist(&MI, Dependency)) {
Sanjoy Dase57bf682016-06-22 22:16:51 +0000449
450 auto DependencyOperandIsOk = [&](MachineOperand &MO) {
451 assert(!(MO.isReg() && MO.isUse()) &&
452 "No transitive dependendencies please!");
453 if (!MO.isReg() || !MO.getReg() || !MO.isDef())
454 return true;
455
456 // Make sure that we won't clobber any live ins to the sibling block
457 // by hoisting Dependency. For instance, we can't hoist INST to
458 // before the null check (even if it safe, and does not violate any
459 // dependencies in the non_null_block) if %rdx is live in to
460 // _null_block.
461 //
462 // test %rcx, %rcx
463 // je _null_block
464 // _non_null_block:
465 // %rdx<def> = INST
466 // ...
467 if (AnyAliasLiveIn(TRI, NullSucc, MO.getReg()))
468 return false;
469
470 // Make sure Dependency isn't re-defining the base register. Then we
471 // won't get the memory operation on the address we want.
472 if (TRI->regsOverlap(MO.getReg(), BaseReg))
473 return false;
474
475 return true;
476 };
477
478 bool DependencyOperandsAreOk =
479 !Dependency ||
480 all_of(Dependency->operands(), DependencyOperandIsOk);
481
482 if (DependencyOperandsAreOk) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000483 NullCheckList.emplace_back(&MI, MBP.ConditionDef, &MBB, NotNullSucc,
Sanjoy Dase57bf682016-06-22 22:16:51 +0000484 NullSucc, Dependency);
485 return true;
486 }
Sanjoy Dasb7718452015-07-09 20:13:25 +0000487 }
488
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000489 HD.rememberInstruction(&MI);
Sanjoy Dasedc394f2015-11-12 20:51:44 +0000490 if (HD.isClobbered())
Sanjoy Dasb7718452015-07-09 20:13:25 +0000491 return false;
Sanjoy Dasb7718452015-07-09 20:13:25 +0000492 }
493
Sanjoy Das69fad072015-06-15 18:44:27 +0000494 return false;
495}
496
497/// Wrap a machine load instruction, LoadMI, into a FAULTING_LOAD_OP machine
498/// instruction. The FAULTING_LOAD_OP instruction does the same load as LoadMI
Quentin Colombet4e1d3892016-05-02 22:58:54 +0000499/// (defining the same register), and branches to HandlerMBB if the load
Sanjoy Das69fad072015-06-15 18:44:27 +0000500/// faults. The FAULTING_LOAD_OP instruction is inserted at the end of MBB.
Quentin Colombet4e1d3892016-05-02 22:58:54 +0000501MachineInstr *
502ImplicitNullChecks::insertFaultingLoad(MachineInstr *LoadMI,
503 MachineBasicBlock *MBB,
504 MachineBasicBlock *HandlerMBB) {
Sanjoy Das93d608c2015-07-20 20:31:39 +0000505 const unsigned NoRegister = 0; // Guaranteed to be the NoRegister value for
506 // all targets.
507
Sanjoy Das69fad072015-06-15 18:44:27 +0000508 DebugLoc DL;
509 unsigned NumDefs = LoadMI->getDesc().getNumDefs();
Sanjoy Das93d608c2015-07-20 20:31:39 +0000510 assert(NumDefs <= 1 && "other cases unhandled!");
Sanjoy Das69fad072015-06-15 18:44:27 +0000511
Sanjoy Das93d608c2015-07-20 20:31:39 +0000512 unsigned DefReg = NoRegister;
513 if (NumDefs != 0) {
514 DefReg = LoadMI->defs().begin()->getReg();
515 assert(std::distance(LoadMI->defs().begin(), LoadMI->defs().end()) == 1 &&
516 "expected exactly one def!");
517 }
Sanjoy Das69fad072015-06-15 18:44:27 +0000518
519 auto MIB = BuildMI(MBB, DL, TII->get(TargetOpcode::FAULTING_LOAD_OP), DefReg)
Quentin Colombet4e1d3892016-05-02 22:58:54 +0000520 .addMBB(HandlerMBB)
Sanjoy Das69fad072015-06-15 18:44:27 +0000521 .addImm(LoadMI->getOpcode());
522
523 for (auto &MO : LoadMI->uses())
524 MIB.addOperand(MO);
525
526 MIB.setMemRefs(LoadMI->memoperands_begin(), LoadMI->memoperands_end());
527
528 return MIB;
529}
530
531/// Rewrite the null checks in NullCheckList into implicit null checks.
532void ImplicitNullChecks::rewriteNullChecks(
533 ArrayRef<ImplicitNullChecks::NullCheck> NullCheckList) {
534 DebugLoc DL;
535
536 for (auto &NC : NullCheckList) {
Sanjoy Das69fad072015-06-15 18:44:27 +0000537 // Remove the conditional branch dependent on the null check.
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000538 unsigned BranchesRemoved = TII->removeBranch(*NC.getCheckBlock());
Sanjoy Das69fad072015-06-15 18:44:27 +0000539 (void)BranchesRemoved;
540 assert(BranchesRemoved > 0 && "expected at least one branch!");
541
Sanjoy Dase57bf682016-06-22 22:16:51 +0000542 if (auto *DepMI = NC.getOnlyDependency()) {
543 DepMI->removeFromParent();
544 NC.getCheckBlock()->insert(NC.getCheckBlock()->end(), DepMI);
545 }
546
Sanjoy Das69fad072015-06-15 18:44:27 +0000547 // Insert a faulting load where the conditional branch was originally. We
548 // check earlier ensures that this bit of code motion is legal. We do not
549 // touch the successors list for any basic block since we haven't changed
550 // control flow, we've just made it implicit.
Sanjoy Dase173b9a2016-06-21 02:10:18 +0000551 MachineInstr *FaultingLoad = insertFaultingLoad(
552 NC.getMemOperation(), NC.getCheckBlock(), NC.getNullSucc());
Quentin Colombet26dab3a2016-05-03 18:09:06 +0000553 // Now the values defined by MemOperation, if any, are live-in of
554 // the block of MemOperation.
555 // The original load operation may define implicit-defs alongside
556 // the loaded value.
Sanjoy Dase173b9a2016-06-21 02:10:18 +0000557 MachineBasicBlock *MBB = NC.getMemOperation()->getParent();
Quentin Colombet26dab3a2016-05-03 18:09:06 +0000558 for (const MachineOperand &MO : FaultingLoad->operands()) {
559 if (!MO.isReg() || !MO.isDef())
560 continue;
561 unsigned Reg = MO.getReg();
562 if (!Reg || MBB->isLiveIn(Reg))
563 continue;
564 MBB->addLiveIn(Reg);
Quentin Colombet12b69912016-04-27 23:26:40 +0000565 }
Sanjoy Dase57bf682016-06-22 22:16:51 +0000566
567 if (auto *DepMI = NC.getOnlyDependency()) {
568 for (auto &MO : DepMI->operands()) {
569 if (!MO.isReg() || !MO.getReg() || !MO.isDef())
570 continue;
571 if (!NC.getNotNullSucc()->isLiveIn(MO.getReg()))
572 NC.getNotNullSucc()->addLiveIn(MO.getReg());
573 }
574 }
575
Sanjoy Dase173b9a2016-06-21 02:10:18 +0000576 NC.getMemOperation()->eraseFromParent();
577 NC.getCheckOperation()->eraseFromParent();
Sanjoy Das69fad072015-06-15 18:44:27 +0000578
579 // Insert an *unconditional* branch to not-null successor.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000580 TII->insertBranch(*NC.getCheckBlock(), NC.getNotNullSucc(), nullptr,
Sanjoy Dase173b9a2016-06-21 02:10:18 +0000581 /*Cond=*/None, DL);
Sanjoy Das69fad072015-06-15 18:44:27 +0000582
Sanjoy Das8ee6a302015-07-06 23:32:10 +0000583 NumImplicitNullChecks++;
Sanjoy Das69fad072015-06-15 18:44:27 +0000584 }
585}
586
587char ImplicitNullChecks::ID = 0;
588char &llvm::ImplicitNullChecksID = ImplicitNullChecks::ID;
589INITIALIZE_PASS_BEGIN(ImplicitNullChecks, "implicit-null-checks",
590 "Implicit null checks", false, false)
Sanjoy Dase57bf682016-06-22 22:16:51 +0000591INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
Sanjoy Das69fad072015-06-15 18:44:27 +0000592INITIALIZE_PASS_END(ImplicitNullChecks, "implicit-null-checks",
593 "Implicit null checks", false, false)