Skip to content

Commit 6fcb039

Browse files
siddheshserge-sans-paille
authored andcommitted
Fold comparison of __builtin_object_size expression with -1 for non-const size
When __builtin_dynamic_object_size returns a non-constant expression, it cannot be -1 since that is an invalid return value for object size. However since passes running after the substitution don't know this, they are unable to optimize away the comparison and hence the comparison and branch stays in there. This change generates an appropriate call to llvm.assume to help the optimizer folding the test. glibc is considering adopting __builtin_dynamic_object_size for additional protection[1] and this change will help reduce branching overhead in fortified implementations of all of the functions that don't have the __builtin___*_chk type builtins, e.g. __ppoll_chk. Also remove the test limit-max-iterations.ll because it was deemed unnecessary during review. [1] https://sourceware.org/pipermail/libc-alpha/2020-November/120191.html Differential Revision: https://reviews.llvm.org/D93015
1 parent c0c0ae1 commit 6fcb039

File tree

3 files changed

+66
-42
lines changed

3 files changed

+66
-42
lines changed

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,16 @@ Value *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
566566
Value *UseZero =
567567
Builder.CreateICmpULT(SizeOffsetPair.first, SizeOffsetPair.second);
568568
ResultSize = Builder.CreateZExtOrTrunc(ResultSize, ResultType);
569-
return Builder.CreateSelect(UseZero, ConstantInt::get(ResultType, 0),
570-
ResultSize);
569+
Value *Ret = Builder.CreateSelect(
570+
UseZero, ConstantInt::get(ResultType, 0), ResultSize);
571+
572+
// The non-constant size expression cannot evaluate to -1.
573+
if (!isa<Constant>(SizeOffsetPair.first) ||
574+
!isa<Constant>(SizeOffsetPair.second))
575+
Builder.CreateAssumption(
576+
Builder.CreateICmpNE(Ret, ConstantInt::get(ResultType, -1)));
577+
578+
return Ret;
571579
}
572580
}
573581

llvm/test/Transforms/InstCombine/builtin-dynamic-object-size.ll

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ entry:
1414

1515
; CHECK: define i64 @weird_identity_but_ok(i64 %sz)
1616
; CHECK-NEXT: entry:
17-
; CHECK-NEXT: ret i64 %sz
17+
; CHECK: ret i64 %sz
1818
; CHECK-NEXT: }
1919

2020
define i64 @phis_are_neat(i1 %which) {
@@ -101,6 +101,57 @@ for.end: ; preds = %for.body, %entry
101101
; CHECK: define void @f()
102102
; CHECK: call i64 @llvm.objectsize.i64.p0i8(
103103

104+
define void @bdos_cmpm1(i64 %alloc) {
105+
entry:
106+
%obj = call i8* @malloc(i64 %alloc)
107+
%objsize = call i64 @llvm.objectsize.i64.p0i8(i8* %obj, i1 0, i1 0, i1 1)
108+
%cmp.not = icmp eq i64 %objsize, -1
109+
br i1 %cmp.not, label %if.else, label %if.then
110+
111+
if.then:
112+
call void @fortified_chk(i8* %obj, i64 %objsize)
113+
br label %if.end
114+
115+
if.else:
116+
call void @unfortified(i8* %obj, i64 %objsize)
117+
br label %if.end
118+
119+
if.end: ; preds = %if.else, %if.then
120+
ret void
121+
}
122+
123+
; CHECK: define void @bdos_cmpm1(
124+
; CHECK: [[TMP:%.*]] = icmp ne i64 %alloc, -1
125+
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP]])
126+
; CHECK-NEXT: br i1 false, label %if.else, label %if.then
127+
; CHECK: call void @fortified_chk(i8* %obj, i64 %alloc)
128+
129+
define void @bdos_cmpm1_expr(i64 %alloc, i64 %part) {
130+
entry:
131+
%sz = udiv i64 %alloc, %part
132+
%obj = call i8* @malloc(i64 %sz)
133+
%objsize = call i64 @llvm.objectsize.i64.p0i8(i8* %obj, i1 0, i1 0, i1 1)
134+
%cmp.not = icmp eq i64 %objsize, -1
135+
br i1 %cmp.not, label %if.else, label %if.then
136+
137+
if.then:
138+
call void @fortified_chk(i8* %obj, i64 %objsize)
139+
br label %if.end
140+
141+
if.else:
142+
call void @unfortified(i8* %obj, i64 %objsize)
143+
br label %if.end
144+
145+
if.end: ; preds = %if.else, %if.then
146+
ret void
147+
}
148+
149+
; CHECK: define void @bdos_cmpm1_expr(
150+
; CHECK: [[TMP:%.*]] = icmp ne i64 [[SZ:%.*]], -1
151+
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP]])
152+
; CHECK-NEXT: br i1 false, label %if.else, label %if.then
153+
; CHECK: call void @fortified_chk(i8* %obj, i64 [[SZ]])
154+
104155
declare void @bury(i32) local_unnamed_addr #2
105156

106157
; Function Attrs: nounwind allocsize(0)
@@ -113,3 +164,7 @@ declare void @free(i8* nocapture)
113164

114165
; Function Attrs: nounwind readnone speculatable
115166
declare i64 @llvm.objectsize.i64.p0i8(i8*, i1, i1, i1)
167+
168+
declare void @fortified_chk(i8*, i64)
169+
170+
declare void @unfortified(i8*, i64)

llvm/test/Transforms/InstCombine/limit-max-iterations.ll

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)