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!

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155764 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/Builtins.def b/include/clang/Basic/Builtins.def
index f0b22a8..3bebfa3 100644
--- a/include/clang/Basic/Builtins.def
+++ b/include/clang/Basic/Builtins.def
@@ -882,7 +882,7 @@
 // FIXME: Also declare NSConcreteGlobalBlock and NSConcreteStackBlock.
 
 // Annotation function
-BUILTIN(__builtin_annotation, "UiUicC*", "nc")
+BUILTIN(__builtin_annotation, "v.", "tn")
 
 #undef BUILTIN
 #undef LIBBUILTIN
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index cf7de93..887f908 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5178,9 +5178,11 @@
 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<
diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index 0d15ce2..d12f707 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -66,16 +66,31 @@
     << 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 @@
     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;
   }
diff --git a/test/CodeGen/annotations-builtin.c b/test/CodeGen/annotations-builtin.c
index 42421a0..7938e49 100644
--- a/test/CodeGen/annotations-builtin.c
+++ b/test/CodeGen/annotations-builtin.c
@@ -25,9 +25,7 @@
 // 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 @@
 // 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
diff --git a/test/Sema/annotate.c b/test/Sema/annotate.c
index 5b27277..ef878d4 100644
--- a/test/Sema/annotate.c
+++ b/test/Sema/annotate.c
@@ -4,7 +4,8 @@
   __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}}
 }