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

Commit 794c434

Browse files
author
Johannes Doerfert
committed
[Attributor] Handle null differently in capture and alias logic
Summary: `null` in the default address space (=AS 0) cannot be captured nor can it alias anything. We make this clear now as it can be important for callbacks and other cases later on. In addition, this patch improves the debug output for noalias deduction. Reviewers: sstefan1, uenoku Subscribers: hiraditya, bollu, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68624 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@374280 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 8808894 commit 794c434

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lib/Transforms/IPO/Attributor.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,11 @@ struct AANoAliasFloating final : AANoAliasImpl {
19071907
/// See AbstractAttribute::initialize(...).
19081908
void initialize(Attributor &A) override {
19091909
AANoAliasImpl::initialize(A);
1910-
if (isa<AllocaInst>(getAnchorValue()))
1910+
Value &Val = getAssociatedValue();
1911+
if (isa<AllocaInst>(Val))
1912+
indicateOptimisticFixpoint();
1913+
if (isa<ConstantPointerNull>(Val) &&
1914+
Val.getType()->getPointerAddressSpace() == 0)
19111915
indicateOptimisticFixpoint();
19121916
}
19131917

@@ -1971,8 +1975,12 @@ struct AANoAliasCallSiteArgument final : AANoAliasImpl {
19711975
// check only uses possibly executed before this callsite.
19721976

19731977
auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
1974-
if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned())
1978+
if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
1979+
LLVM_DEBUG(
1980+
dbgs() << "[Attributor][AANoAliasCSArg] " << V
1981+
<< " cannot be noalias as it is potentially captured\n");
19751982
return indicatePessimisticFixpoint();
1983+
}
19761984

19771985
// (iii) Check there is no other pointer argument which could alias with the
19781986
// value.
@@ -1986,13 +1994,15 @@ struct AANoAliasCallSiteArgument final : AANoAliasImpl {
19861994

19871995
if (const Function *F = getAnchorScope()) {
19881996
if (AAResults *AAR = A.getInfoCache().getAAResultsForFunction(*F)) {
1997+
bool IsAliasing = AAR->isNoAlias(&getAssociatedValue(), ArgOp);
19891998
LLVM_DEBUG(dbgs()
19901999
<< "[Attributor][NoAliasCSArg] Check alias between "
19912000
"callsite arguments "
19922001
<< AAR->isNoAlias(&getAssociatedValue(), ArgOp) << " "
1993-
<< getAssociatedValue() << " " << *ArgOp << "\n");
2002+
<< getAssociatedValue() << " " << *ArgOp << " => "
2003+
<< (IsAliasing ? "" : "no-") << "alias \n");
19942004

1995-
if (AAR->isNoAlias(&getAssociatedValue(), ArgOp))
2005+
if (IsAliasing)
19962006
continue;
19972007
}
19982008
}
@@ -2881,6 +2891,13 @@ struct AANoCaptureImpl : public AANoCapture {
28812891
void initialize(Attributor &A) override {
28822892
AANoCapture::initialize(A);
28832893

2894+
// You cannot "capture" null in the default address space.
2895+
if (isa<ConstantPointerNull>(getAssociatedValue()) &&
2896+
getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
2897+
indicateOptimisticFixpoint();
2898+
return;
2899+
}
2900+
28842901
const IRPosition &IRP = getIRPosition();
28852902
const Function *F =
28862903
getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();

test/Transforms/FunctionAttrs/callbacks.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ define void @t0_caller(i32* %a) {
2424
; CHECK-NEXT: [[TMP0:%.*]] = bitcast i32* [[B]] to i8*
2525
; CHECK-NEXT: store i32 42, i32* [[B]], align 32
2626
; CHECK-NEXT: store i32* [[B]], i32** [[C]], align 64
27-
; CHECK-NEXT: call void (i32*, i32*, void (i32*, i32*, ...)*, ...) @t0_callback_broker(i32* null, i32* nonnull align 128 dereferenceable(4) [[PTR]], void (i32*, i32*, ...)* nonnull bitcast (void (i32*, i32*, i32*, i64, i32**)* @t0_callback_callee to void (i32*, i32*, ...)*), i32* [[A:%.*]], i64 99, i32** nonnull align 64 dereferenceable(8) [[C]])
27+
; CHECK-NEXT: call void (i32*, i32*, void (i32*, i32*, ...)*, ...) @t0_callback_broker(i32* noalias null, i32* nonnull align 128 dereferenceable(4) [[PTR]], void (i32*, i32*, ...)* nonnull bitcast (void (i32*, i32*, i32*, i64, i32**)* @t0_callback_callee to void (i32*, i32*, ...)*), i32* [[A:%.*]], i64 99, i32** nonnull align 64 dereferenceable(8) [[C]])
2828
; CHECK-NEXT: ret void
2929
;
3030
entry:

test/Transforms/FunctionAttrs/nocapture.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,5 +320,14 @@ define i1 @captureDereferenceableOrNullICmp(i32* dereferenceable_or_null(4) %x)
320320
ret i1 %2
321321
}
322322

323+
declare void @unknown(i8*)
324+
define void @test_callsite() {
325+
entry:
326+
; We know that 'null' in AS 0 does not alias anything and cannot be captured
327+
; CHECK: call void @unknown(i8* noalias nocapture null)
328+
call void @unknown(i8* null)
329+
ret void
330+
}
331+
323332
declare i8* @llvm.launder.invariant.group.p0i8(i8*)
324333
declare i8* @llvm.strip.invariant.group.p0i8(i8*)

0 commit comments

Comments
 (0)