Currently __builtin_annotation() only annotates an i32.

i32 __builtin_annotation(i32, string);

Applying it to i64 (e.g., long long) generates the following IR.

        trunc i64 {{.*}} to i32
        call i32 @llvm.annotation.i32
        zext i32 {{.*}} to i64

The redundant truncation and extension make the result difficult to use.

This patch makes __builtin_annotation() generic.

        type __builtin_annotation(type, string);

For the i64 example, it simplifies the generated IR to:

        call i64 @llvm.annotation.i64

Patch by Xi Wang!

llvm-svn: 155764
This commit is contained in:
Julien Lerouge 2012-04-28 17:39:16 +00:00
parent 9de0b35648
commit 4a5b444371
5 changed files with 37 additions and 25 deletions

View File

@ -882,7 +882,7 @@ LIBBUILTIN(_Block_object_dispose, "vvC*iC", "f", "Blocks.h", ALL_LANGUAGES)
// FIXME: Also declare NSConcreteGlobalBlock and NSConcreteStackBlock.
// Annotation function
BUILTIN(__builtin_annotation, "UiUicC*", "nc")
BUILTIN(__builtin_annotation, "v.", "tn")
#undef BUILTIN
#undef LIBBUILTIN

View File

@ -5178,9 +5178,11 @@ def err_blocks_disable : Error<"blocks support disabled - compile with -fblocks"
def err_block_returning_array_function : Error<
"block cannot return %select{array|function}0 type %1">;
// Builtin annotation string.
def err_builtin_annotation_not_string_constant : Error<
"__builtin_annotation requires a non wide string constant">;
// Builtin annotation
def err_builtin_annotation_first_arg : Error<
"first argument to __builtin_annotation must be an integer">;
def err_builtin_annotation_second_arg : Error<
"second argument to __builtin_annotation must be a non-wide string constant">;
// CFString checking
def err_cfstring_literal_not_string_constant : Error<

View File

@ -66,16 +66,31 @@ static bool checkArgCount(Sema &S, CallExpr *call, unsigned desiredArgCount) {
<< call->getArg(1)->getSourceRange();
}
/// CheckBuiltinAnnotationString - Checks that string argument to the builtin
/// annotation is a non wide string literal.
static bool CheckBuiltinAnnotationString(Sema &S, Expr *Arg) {
Arg = Arg->IgnoreParenCasts();
StringLiteral *Literal = dyn_cast<StringLiteral>(Arg);
if (!Literal || !Literal->isAscii()) {
S.Diag(Arg->getLocStart(), diag::err_builtin_annotation_not_string_constant)
<< Arg->getSourceRange();
/// Check that the first argument to __builtin_annotation is an integer
/// and the second argument is a non-wide string literal.
static bool SemaBuiltinAnnotation(Sema &S, CallExpr *TheCall) {
if (checkArgCount(S, TheCall, 2))
return true;
// First argument should be an integer.
Expr *ValArg = TheCall->getArg(0);
QualType Ty = ValArg->getType();
if (!Ty->isIntegerType()) {
S.Diag(ValArg->getLocStart(), diag::err_builtin_annotation_first_arg)
<< ValArg->getSourceRange();
return true;
}
// Second argument should be a constant string.
Expr *StrArg = TheCall->getArg(1)->IgnoreParenCasts();
StringLiteral *Literal = dyn_cast<StringLiteral>(StrArg);
if (!Literal || !Literal->isAscii()) {
S.Diag(StrArg->getLocStart(), diag::err_builtin_annotation_second_arg)
<< StrArg->getSourceRange();
return true;
}
TheCall->setType(Ty);
return false;
}
@ -256,7 +271,7 @@ Sema::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) {
return SemaAtomicOpsOverloaded(move(TheCallResult), AtomicExpr::AO##ID);
#include "clang/Basic/Builtins.def"
case Builtin::BI__builtin_annotation:
if (CheckBuiltinAnnotationString(*this, TheCall->getArg(1)))
if (SemaBuiltinAnnotation(*this, TheCall))
return ExprError();
break;
}

View File

@ -25,9 +25,7 @@ int main(int argc, char **argv) {
// CHECK: call i32 @llvm.annotation.i32
long long lla = __builtin_annotation(llfoo, "annotation_a");
// CHECK: trunc i64 {{.*}} to i32
// CHECK-NEXT: call i32 @llvm.annotation.i32
// CHECK-NEXT: zext i32 {{.*}} to i64
// CHECK: call i64 @llvm.annotation.i64
int inta = __builtin_annotation(intfoo, "annotation_a");
// CHECK: load i32* @intfoo
@ -35,15 +33,11 @@ int main(int argc, char **argv) {
// CHECK-NEXT: store
short shorta = __builtin_annotation(shortfoo, "annotation_a");
// CHECK: sext i16 {{.*}} to i32
// CHECK-NEXT: call i32 @llvm.annotation.i32
// CHECK-NEXT: trunc i32 {{.*}} to i16
// CHECK: call i16 @llvm.annotation.i16
char chara = __builtin_annotation(charfoo, "annotation_a");
// CHECK: sext i8 {{.*}} to i32
// CHECK-NEXT: call i32 @llvm.annotation.i32
// CHECK-NEXT: trunc i32 {{.*}} to i8
//
// CHECK: call i8 @llvm.annotation.i8
char **arg = (char**) __builtin_annotation((int) argv, "annotation_a");
// CHECK: ptrtoint i8** {{.*}} to
// CHECK: call i32 @llvm.annotation.i32

View File

@ -4,7 +4,8 @@ void __attribute__((annotate("foo"))) foo(float *a) {
__attribute__((annotate("bar"))) int x;
__attribute__((annotate(1))) int y; // expected-error {{argument to annotate attribute was not a string literal}}
__attribute__((annotate("bar", 1))) int z; // expected-error {{attribute takes one argument}}
int u = __builtin_annotation(z, (char*) 0); // expected-error {{__builtin_annotation requires a non wide string constant}}
int v = __builtin_annotation(z, (char*) L"bar"); // expected-error {{__builtin_annotation requires a non wide string constant}}
int u = __builtin_annotation(z, (char*) 0); // expected-error {{second argument to __builtin_annotation must be a non-wide string constant}}
int v = __builtin_annotation(z, (char*) L"bar"); // expected-error {{second argument to __builtin_annotation must be a non-wide string constant}}
int w = __builtin_annotation(z, "foo");
float b = __builtin_annotation(*a, "foo"); // expected-error {{first argument to __builtin_annotation must be an integer}}
}