Skip to content

Commit 0711638

Browse files
committed
!fixup address latest comments, thanks!
1 parent 596d134 commit 0711638

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ def BuiltinAssumeAligned : Builtin {
841841

842842
def BuiltinAssumeDereferenceable : Builtin {
843843
let Spellings = ["__builtin_assume_dereferenceable"];
844-
let Attributes = [NoThrow, Const, Constexpr];
844+
let Attributes = [NoThrow, Const];
845845
let Prototype = "void(void const*, _Constant size_t)";
846846
}
847847

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3728,12 +3728,14 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
37283728
}
37293729
case Builtin::BI__builtin_assume_dereferenceable: {
37303730
const Expr *Ptr = E->getArg(0);
3731+
const Expr *Size= E->getArg(1);
37313732
Value *PtrValue = EmitScalarExpr(Ptr);
3732-
Value *SizeValue = EmitScalarExpr(E->getArg(1));
3733+
Value *SizeValue = EmitScalarExpr(Size);
37333734
if (SizeValue->getType() != IntPtrTy)
37343735
SizeValue =
37353736
Builder.CreateIntCast(SizeValue, IntPtrTy, false, "casted.size");
37363737
Builder.CreateDereferenceableAssumption(PtrValue, SizeValue);
3738+
Builder.CreateNonNullAssumption(PtrValue);
37373739
return RValue::get(nullptr);
37383740
}
37393741
case Builtin::BI__assume:

clang/test/CodeGen/builtin-assume-dereferenceable.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// CHECK-NEXT: store ptr [[A:%.*]], ptr [[A_ADDR]], align 8
88
// CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8
99
// CHECK-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(ptr [[TMP0]], i64 10) ]
10+
// CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[TMP0]]) ]
1011
// CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr [[A_ADDR]], align 8
1112
// CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[TMP1]], i64 0
1213
// CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
@@ -23,6 +24,7 @@ int test1(int *a) {
2324
// CHECK-NEXT: store ptr [[A:%.*]], ptr [[A_ADDR]], align 8
2425
// CHECK-NEXT: [[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8
2526
// CHECK-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(ptr [[TMP0]], i64 32) ]
27+
// CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(ptr [[TMP0]]) ]
2628
// CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr [[A_ADDR]], align 8
2729
// CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[TMP1]], i64 0
2830
// CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX]], align 4

clang/test/Sema/builtin-assume-dereferenceable.c renamed to clang/test/SemaCXX/builtin-assume-dereferenceable.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %clang_cc1 -DSIZE_T_64 -fsyntax-only -Wno-strict-prototypes -triple x86_64-linux -verify %s
1+
// RUN: %clang_cc1 -DSIZE_T_64 -fsyntax-only -verify -std=c++11 -triple x86_64-linux-gnu %s
2+
// RUN: %clang_cc1 -DSIZE_T_64 -fsyntax-only -verify -std=c++11 -triple x86_64-linux-gnu %s -fexperimental-new-constant-interpreter
3+
24

35
int test1(int *a) {
46
__builtin_assume_dereferenceable(a, 32);
@@ -26,7 +28,7 @@ int test5(int *a, unsigned long long size) {
2628
}
2729

2830
int test6(float a) {
29-
__builtin_assume_dereferenceable(a, 2); // expected-error {{passing 'float' to parameter of incompatible type 'const void *'}}
31+
__builtin_assume_dereferenceable(a, 2); // expected-error {{cannot initialize a parameter of type 'const void *' with an lvalue of type 'float'}}
3032
return 0;;
3133
}
3234

@@ -39,3 +41,15 @@ int test8(int *a) {
3941
__builtin_assume_dereferenceable(a); // expected-error {{too few arguments to function call, expected 2, have 1}}
4042
return a[0];
4143
}
44+
45+
int test9(int *a) {
46+
a[0] = __builtin_assume_dereferenceable(a, 32); // expected-error {{assigning to 'int' from incompatible type 'void'}}
47+
return a[0];
48+
}
49+
50+
constexpr int *p = 0;
51+
constexpr void *l = __builtin_assume_dereferenceable(p, 4); // expected-error {{cannot initialize a variable of type 'void *const' with an rvalue of type 'void'}}
52+
53+
void *foo() {
54+
return l;
55+
}

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2681,8 +2681,11 @@ class IRBuilderBase {
26812681

26822682
/// Create an assume intrinsic call that represents an dereferencable
26832683
/// assumption on the provided pointer.
2684-
///
26852684
CallInst *CreateDereferenceableAssumption(Value *PtrValue, Value *SizeValue);
2685+
2686+
/// Create an assume intrinsic call that represents a nonnull assumption
2687+
/// on the provided pointer.
2688+
CallInst *CreateNonNullAssumption(Value *PtrValue);
26862689
};
26872690

26882691
/// This provides a uniform API for creating instructions and inserting

llvm/lib/IR/IRBuilder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,14 @@ CallInst *IRBuilderBase::CreateDereferenceableAssumption(Value *PtrValue,
12841284
{DereferenceableOpB});
12851285
}
12861286

1287+
CallInst *IRBuilderBase::CreateNonNullAssumption(Value *PtrValue) {
1288+
assert(isa<PointerType>(PtrValue->getType()) &&
1289+
"trying to create an nonnull assumption on a non-pointer?");
1290+
SmallVector<Value *, 4> Vals({PtrValue});
1291+
OperandBundleDefT<Value *> NonNullOpB("nonnull", Vals);
1292+
return CreateAssumption(ConstantInt::getTrue(getContext()), {NonNullOpB});
1293+
}
1294+
12871295
IRBuilderDefaultInserter::~IRBuilderDefaultInserter() = default;
12881296
IRBuilderCallbackInserter::~IRBuilderCallbackInserter() = default;
12891297
IRBuilderFolder::~IRBuilderFolder() = default;

0 commit comments

Comments
 (0)