Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit fa9f420

Browse files
author
Johannes Doerfert
committed
[CaptureTracking] Allow null to be in either icmp operand
Summary: Before we required the comparison against null to be "canonical", hence null to be operand #1. This patch allows null to be in either operand, similar to the handling of loaded globals that follows. Reviewers: sanjoy, hfinkel, aykevl, sstefan1, uenoku Subscribers: hiraditya, bollu, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D66321 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@369158 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 222b354 commit fa9f420

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/Analysis/CaptureTracking.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,17 @@ void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
331331
AddUses(I);
332332
break;
333333
case Instruction::ICmp: {
334-
if (auto *CPN = dyn_cast<ConstantPointerNull>(I->getOperand(1))) {
334+
unsigned Idx = (I->getOperand(0) == V) ? 0 : 1;
335+
unsigned OtherIdx = 1 - Idx;
336+
if (auto *CPN = dyn_cast<ConstantPointerNull>(I->getOperand(OtherIdx))) {
335337
// Don't count comparisons of a no-alias return value against null as
336338
// captures. This allows us to ignore comparisons of malloc results
337339
// with null, for example.
338340
if (CPN->getType()->getAddressSpace() == 0)
339341
if (isNoAliasCall(V->stripPointerCasts()))
340342
break;
341343
if (!I->getFunction()->nullPointerIsDefined()) {
342-
auto *O = I->getOperand(0)->stripPointerCastsSameRepresentation();
344+
auto *O = I->getOperand(Idx)->stripPointerCastsSameRepresentation();
343345
// An inbounds GEP can either be a valid pointer (pointing into
344346
// or to the end of an allocation), or be null in the default
345347
// address space. So for an inbounds GEPs there is no way to let
@@ -353,15 +355,15 @@ void llvm::PointerMayBeCaptured(const Value *V, CaptureTracker *Tracker,
353355
// cannot lead to pointer escapes, because if it is not null it
354356
// must be a valid (in-bounds) pointer.
355357
bool CanBeNull;
356-
if (O->getPointerDereferenceableBytes(I->getModule()->getDataLayout(), CanBeNull))
358+
if (O->getPointerDereferenceableBytes(I->getModule()->getDataLayout(),
359+
CanBeNull))
357360
break;
358361
}
359362
}
360363
// Comparison against value stored in global variable. Given the pointer
361364
// does not escape, its value cannot be guessed and stored separately in a
362365
// global variable.
363-
unsigned OtherIndex = (I->getOperand(0) == V) ? 1 : 0;
364-
auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIndex));
366+
auto *LI = dyn_cast<LoadInst>(I->getOperand(OtherIdx));
365367
if (LI && isa<GlobalVariable>(LI->getPointerOperand()))
366368
break;
367369
// Otherwise, be conservative. There are crazy ways to capture pointers

test/Transforms/FunctionAttrs/nocapture.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,12 @@ define i1 @captureICmp(i32* %x) {
259259
ret i1 %1
260260
}
261261

262+
; CHECK: define i1 @captureICmpRev(i32* readnone %x)
263+
define i1 @captureICmpRev(i32* %x) {
264+
%1 = icmp eq i32* null, %x
265+
ret i1 %1
266+
}
267+
262268
; CHECK: define i1 @nocaptureInboundsGEPICmp(i32* nocapture readnone %x)
263269
define i1 @nocaptureInboundsGEPICmp(i32* %x) {
264270
%1 = getelementptr inbounds i32, i32* %x, i32 5
@@ -267,6 +273,14 @@ define i1 @nocaptureInboundsGEPICmp(i32* %x) {
267273
ret i1 %3
268274
}
269275

276+
; CHECK: define i1 @nocaptureInboundsGEPICmpRev(i32* nocapture readnone %x)
277+
define i1 @nocaptureInboundsGEPICmpRev(i32* %x) {
278+
%1 = getelementptr inbounds i32, i32* %x, i32 5
279+
%2 = bitcast i32* %1 to i8*
280+
%3 = icmp eq i8* null, %2
281+
ret i1 %3
282+
}
283+
270284
; CHECK: define i1 @nocaptureDereferenceableOrNullICmp(i32* nocapture readnone dereferenceable_or_null(4) %x)
271285
define i1 @nocaptureDereferenceableOrNullICmp(i32* dereferenceable_or_null(4) %x) {
272286
%1 = bitcast i32* %x to i8*

0 commit comments

Comments
 (0)