Skip to content

Commit 2cafbcb

Browse files
committed
[instcombine] Key deref vs deref_or_null annotation of allocation sites off nonnull attribute
Goal is to remove use of isOpNewLike. I looked at a couple approaches to this, and this turned out to be the cheapest one. Just letting deref_or_null be generated causes a bunch of test diffs, and I couldn't convince myself there wasn't a real regression somewhere. A generic instcombine to convert deref_or_null + nonnull to deref is annoying complicated since you have to mix facts from callsite and declaration while manipulating only existing call site attributes. It just wasn't worth the code complexity. Note that the change in new-delete-itanium.ll is a real regression. If you have a callsite which overrides the builtin status of a nobuiltin declaration, *and* you don't put the apppriate attributes on that callsite, you may lose the deref fact. I decided this didn't matter; if anyone disagrees, you can add this case to the generic non-null inference.
1 parent 6ce732c commit 2cafbcb

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,12 +2571,17 @@ static IntrinsicInst *findInitTrampoline(Value *Callee) {
25712571
}
25722572

25732573
void InstCombinerImpl::annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI) {
2574+
// Note: We only handle cases which can't be driven from generic attributes
2575+
// here. So, for example, nonnull and noalias (which are common properties
2576+
// of some allocation functions) are expected to be handled via annotation
2577+
// of the respective allocator declaration with generic attributes.
25742578

25752579
uint64_t Size;
25762580
ObjectSizeOpts Opts;
25772581
if (getObjectSize(&Call, Size, DL, TLI, Opts) && Size > 0) {
2578-
// TODO: should be annotating these nonnull
2579-
if (isOpNewLikeFn(&Call, TLI))
2582+
// TODO: We really should just emit deref_or_null here and then
2583+
// let the generic inference code combine that with nonnull.
2584+
if (Call.hasRetAttr(Attribute::NonNull))
25802585
Call.addRetAttr(Attribute::getWithDereferenceableBytes(
25812586
Call.getContext(), Size));
25822587
else

llvm/test/Transforms/InstCombine/new-delete-itanium.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ define void @test10() {
201201

202202
define void @test11() {
203203
; CHECK-LABEL: @test11(
204-
; CHECK-NEXT: [[CALL:%.*]] = call dereferenceable(8) i8* @_Znwm(i64 8) #[[ATTR5]]
205-
; CHECK-NEXT: call void @_ZdlPv(i8* nonnull [[CALL]])
204+
; CHECK-NEXT: [[CALL:%.*]] = call dereferenceable_or_null(8) i8* @_Znwm(i64 8) #[[ATTR5]]
205+
; CHECK-NEXT: call void @_ZdlPv(i8* [[CALL]])
206206
; CHECK-NEXT: ret void
207207
;
208208
%call = call i8* @_Znwm(i64 8) builtin

llvm/test/Transforms/InstCombine/objsize-64.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
44

55
declare noalias i8* @malloc(i32) nounwind
6-
declare noalias i8* @_Znwm(i64) ; new(unsigned long)
6+
declare noalias nonnull i8* @_Znwm(i64) ; new(unsigned long)
77
declare i32 @__gxx_personality_v0(...)
88
declare void @__cxa_call_unexpected(i8*)
99
declare i64 @llvm.objectsize.i64(i8*, i1) nounwind readonly
@@ -33,7 +33,7 @@ define i64 @f2(i8** %esc) nounwind uwtable ssp personality i8* bitcast (i32 (...
3333
; CHECK-NEXT: [[TMP0:%.*]] = landingpad { i8*, i32 }
3434
; CHECK-NEXT: filter [0 x i8*] zeroinitializer
3535
; CHECK-NEXT: [[TMP1:%.*]] = extractvalue { i8*, i32 } [[TMP0]], 0
36-
; CHECK-NEXT: tail call void @__cxa_call_unexpected(i8* [[TMP1]]) #3
36+
; CHECK-NEXT: tail call void @__cxa_call_unexpected(i8* [[TMP1]]) #[[ATTR3:[0-9]+]]
3737
; CHECK-NEXT: unreachable
3838
;
3939
entry:

0 commit comments

Comments
 (0)