[cfe-commits] r53885 - in /cfe/trunk: include/clang/AST/Builtins.def include/clang/Basic/DiagnosticKinds.def lib/CodeGen/CGBuiltin.cpp lib/Sema/Sema.h lib/Sema/SemaChecking.cpp test/Sema/builtin-prefetch.c
Daniel Dunbar
daniel at zuster.org
Mon Jul 21 15:59:14 PDT 2008
Author: ddunbar
Date: Mon Jul 21 17:59:13 2008
New Revision: 53885
URL: http://llvm.org/viewvc/llvm-project?rev=53885&view=rev
Log:
Implement nans, prefetch, and trap builtins.
This closes <rdar://problem/6080720>, support for __builtin_constant_p
has been filed separately.
Added:
cfe/trunk/test/Sema/builtin-prefetch.c
Modified:
cfe/trunk/include/clang/AST/Builtins.def
cfe/trunk/include/clang/Basic/DiagnosticKinds.def
cfe/trunk/lib/CodeGen/CGBuiltin.cpp
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaChecking.cpp
Modified: cfe/trunk/include/clang/AST/Builtins.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Builtins.def?rev=53885&r1=53884&r2=53885&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Builtins.def (original)
+++ cfe/trunk/include/clang/AST/Builtins.def Mon Jul 21 17:59:13 2008
@@ -63,6 +63,9 @@
BUILTIN(__builtin_nan, "dcC*" , "ncF")
BUILTIN(__builtin_nanf, "fcC*" , "ncF")
BUILTIN(__builtin_nanl, "LdcC*", "ncF")
+BUILTIN(__builtin_nans, "dcC*" , "ncF")
+BUILTIN(__builtin_nansf, "fcC*" , "ncF")
+BUILTIN(__builtin_nansl, "LdcC*", "ncF")
BUILTIN(__builtin_abs , "ii" , "ncF")
BUILTIN(__builtin_fabs , "dd" , "ncF")
BUILTIN(__builtin_fabsf, "ff" , "ncF")
@@ -126,6 +129,8 @@
BUILTIN(__builtin___strcpy_chk, "c*cC*cC*z", "n")
BUILTIN(__builtin___strcat_chk, "c*c*cC*z", "n")
BUILTIN(__builtin_expect, "iii" , "nc")
+BUILTIN(__builtin_prefetch, "vCv*.", "nc")
+BUILTIN(__builtin_trap, "v", "n")
BUILTIN(__builtin_shufflevector, "v." , "nc")
Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=53885&r1=53884&r2=53885&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Mon Jul 21 17:59:13 2008
@@ -1132,4 +1132,9 @@
DIAG(err_stack_const_level, ERROR,
"the level argument for a stack address builtin must be constant")
+DIAG(err_prefetch_invalid_argument, ERROR,
+ "argument to __builtin_prefetch must be a constant integer")
+DIAG(err_prefetch_invalid_range, ERROR,
+ "argument should be a value from %0 to %1")
+
#undef DIAG
Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=53885&r1=53884&r2=53885&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Mon Jul 21 17:59:13 2008
@@ -190,7 +190,6 @@
Result = Builder.CreateIntCast(Result, ResultType, "cast");
return RValue::get(Result);
}
-
case Builtin::BI__builtin_expect:
// FIXME: pass expect through to LLVM
return RValue::get(EmitScalarExpr(E->getArg(0)));
@@ -200,7 +199,22 @@
const llvm::Type *ArgType = ArgValue->getType();
Value *F = CGM.getIntrinsic(Intrinsic::bswap, &ArgType, 1);
return RValue::get(Builder.CreateCall(F, ArgValue, "tmp"));
+ }
+ case Builtin::BI__builtin_prefetch: {
+ Value *Locality, *RW, *Address = EmitScalarExpr(E->getArg(0));
+ // FIXME: Technically these constants should of type 'int', yes?
+ RW = (E->getNumArgs() > 1) ? EmitScalarExpr(E->getArg(1)) :
+ ConstantInt::get(llvm::Type::Int32Ty, 0);
+ Locality = (E->getNumArgs() > 2) ? EmitScalarExpr(E->getArg(2)) :
+ ConstantInt::get(llvm::Type::Int32Ty, 3);
+ Value *F = CGM.getIntrinsic(Intrinsic::prefetch, 0, 0);
+ return RValue::get(Builder.CreateCall3(F, Address, RW, Locality));
+ }
+ case Builtin::BI__builtin_trap: {
+ Value *F = CGM.getIntrinsic(Intrinsic::trap, 0, 0);
+ return RValue::get(Builder.CreateCall(F));
}
+
case Builtin::BI__builtin_huge_val:
case Builtin::BI__builtin_huge_valf:
case Builtin::BI__builtin_huge_vall:
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=53885&r1=53884&r2=53885&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Mon Jul 21 17:59:13 2008
@@ -909,6 +909,7 @@
bool SemaBuiltinUnorderedCompare(CallExpr *TheCall);
bool SemaBuiltinStackAddress(CallExpr *TheCall);
Action::ExprResult SemaBuiltinShuffleVector(CallExpr *TheCall);
+ bool SemaBuiltinPrefetch(CallExpr *TheCall);
void CheckPrintfArguments(CallExpr *TheCall,
bool HasVAListArg, unsigned format_idx);
void CheckReturnStackAddr(Expr *RetValExp, QualType lhsType,
Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=53885&r1=53884&r2=53885&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Jul 21 17:59:13 2008
@@ -66,6 +66,10 @@
return TheCall.take();
case Builtin::BI__builtin_shufflevector:
return SemaBuiltinShuffleVector(TheCall.get());
+ case Builtin::BI__builtin_prefetch:
+ if (SemaBuiltinPrefetch(TheCall.get()))
+ return true;
+ return TheCall.take();
}
// Search the KnownFunctionIDs for the identifier.
@@ -293,6 +297,55 @@
return E;
}
+/// SemaBuiltinPrefetch - Handle __builtin_prefetch.
+// This is declared to take (const void*, ...) and can take two
+// optional constant int args.
+bool Sema::SemaBuiltinPrefetch(CallExpr *TheCall) {
+ unsigned numArgs = TheCall->getNumArgs();
+ bool res = false;
+
+ if (numArgs > 3) {
+ res |= Diag(TheCall->getLocEnd(), diag::err_typecheck_call_too_many_args,
+ TheCall->getSourceRange());
+ }
+
+ // Argument 0 is checked for us and the remaining arguments must be
+ // constant integers.
+ for (unsigned i=1; i<numArgs; ++i) {
+ Expr *Arg = TheCall->getArg(i);
+ QualType RWType = Arg->getType();
+
+ const BuiltinType *BT = RWType->getAsBuiltinType();
+ // FIXME: 32 is wrong, needs to be proper width of Int
+ llvm::APSInt Result(32);
+ if (!BT || BT->getKind() != BuiltinType::Int ||
+ !Arg->isIntegerConstantExpr(Result, Context)) {
+ if (Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_argument,
+ SourceRange(Arg->getLocStart(), Arg->getLocEnd()))) {
+ res = true;
+ continue;
+ }
+ }
+
+ // FIXME: gcc issues a warning and rewrites these to 0. These
+ // seems especially odd for the third argument since the default
+ // is 3.
+ if (i==1) {
+ if (Result.getSExtValue() < 0 || Result.getSExtValue() > 1)
+ res |= Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_range,
+ "0", "1",
+ SourceRange(Arg->getLocStart(), Arg->getLocEnd()));
+ } else {
+ if (Result.getSExtValue() < 0 || Result.getSExtValue() > 3)
+ res |= Diag(TheCall->getLocStart(), diag::err_prefetch_invalid_range,
+ "0", "3",
+ SourceRange(Arg->getLocStart(), Arg->getLocEnd()));
+ }
+ }
+
+ return res;
+}
+
/// CheckPrintfArguments - Check calls to printf (and similar functions) for
/// correct use of format strings.
///
Added: cfe/trunk/test/Sema/builtin-prefetch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/builtin-prefetch.c?rev=53885&view=auto
==============================================================================
--- cfe/trunk/test/Sema/builtin-prefetch.c (added)
+++ cfe/trunk/test/Sema/builtin-prefetch.c Mon Jul 21 17:59:13 2008
@@ -0,0 +1,13 @@
+// RUN: clang -fsyntax-only -verify %s
+
+int foo() {
+ int a;
+ __builtin_prefetch(&a);
+ __builtin_prefetch(&a, 1);
+ __builtin_prefetch(&a, 1, 2);
+ __builtin_prefetch(&a, 1, 9, 3); // expected-error{{too many arguments to function}}, expected-error{{argument should be a value from 0 to 3}}
+ __builtin_prefetch(&a, "hello", 2); // expected-error{{argument to __builtin_prefetch must be a constant integer}}
+ __builtin_prefetch(&a, 2); // expected-error{{argument should be a value from 0 to 1}}
+ __builtin_prefetch(&a, 0, 4); // expected-error{{argument should be a value from 0 to 3}}
+ __builtin_prefetch(&a, -1, 4); // expected-error{{argument should be a value from 0 to 1}}, expected-error{{argument should be a value from 0 to 3}}
+}
More information about the cfe-commits
mailing list