If we are compiling with -fno-builtin then don't do constant folding of
authorDavid Chisnall <csdavec@swan.ac.uk>
Fri, 6 Jan 2012 12:20:19 +0000 (12:20 +0000)
committerDavid Chisnall <csdavec@swan.ac.uk>
Fri, 6 Jan 2012 12:20:19 +0000 (12:20 +0000)
builtins.

This fixes PR11711.

llvm-svn: 147655

clang/lib/CodeGen/CGBuiltin.cpp
clang/test/CodeGen/no-builtin.c [new file with mode: 0644]

index 3e5a677..f462ee6 100644 (file)
@@ -175,7 +175,8 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
                                         unsigned BuiltinID, const CallExpr *E) {
   // See if we can constant fold this builtin.  If so, don't emit it at all.
   Expr::EvalResult Result;
-  if (E->EvaluateAsRValue(Result, CGM.getContext()) &&
+  if (!getContext().getLangOptions().NoBuiltin &&
+      E->EvaluateAsRValue(Result, CGM.getContext()) &&
       !Result.hasSideEffects()) {
     if (Result.Val.isInt())
       return RValue::get(llvm::ConstantInt::get(getLLVMContext(),
diff --git a/clang/test/CodeGen/no-builtin.c b/clang/test/CodeGen/no-builtin.c
new file mode 100644 (file)
index 0000000..ff1c192
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -fno-builtin -emit-llvm %s -o - | FileCheck %s
+//
+// Check that -fno-builtin prevents us from constant-folding through builtins
+// (PR11711)
+
+double
+cos(double x)
+{
+  printf("ok\n");
+  exit(0);
+}
+
+int
+main(int argc, char *argv[])
+{
+  cos(1); // CHECK: cos
+  printf("not ok\n");
+  abort();
+}
+