am 4cd60938: am e35a905e: Fix mad performance.

* commit '4cd60938784ac6bad16a7855f81897280456b27a':
  Fix mad performance.
diff --git a/bcinfo/MetadataExtractor.cpp b/bcinfo/MetadataExtractor.cpp
index 2537fdd..98aad18 100644
--- a/bcinfo/MetadataExtractor.cpp
+++ b/bcinfo/MetadataExtractor.cpp
@@ -20,6 +20,7 @@
 
 #define LOG_TAG "bcinfo"
 #include <cutils/log.h>
+#include <cutils/properties.h>
 
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/Bitcode/ReaderWriter.h"
@@ -221,6 +222,7 @@
   // Check to see if we have any FP precision-related pragmas.
   std::string Relaxed("rs_fp_relaxed");
   std::string Imprecise("rs_fp_imprecise");
+  std::string Full("rs_fp_full");
   bool RelaxedPragmaSeen = false;
   bool ImprecisePragmaSeen = false;
 
@@ -246,6 +248,26 @@
     mRSFloatPrecision = RS_FP_Relaxed;
   }
 
+  // Provide an override for precsion via adb shell setprop
+  // adb shell setprop debug.rs.precision rs_fp_full
+  // adb shell setprop debug.rs.precision rs_fp_relaxed
+  // adb shell setprop debug.rs.precision rs_fp_imprecise
+  char PrecisionPropBuf[PROPERTY_VALUE_MAX];
+  const std::string PrecisionPropName("debug.rs.precision");
+  property_get("debug.rs.precision", PrecisionPropBuf, "");
+  if (PrecisionPropBuf[0]) {
+    if (!Relaxed.compare(PrecisionPropBuf)) {
+      ALOGE("Switching to RS FP relaxed mode via setprop");
+      mRSFloatPrecision = RS_FP_Relaxed;
+    } else if (!Imprecise.compare(PrecisionPropBuf)) {
+      ALOGE("Switching to RS FP imprecise mode via setprop");
+      mRSFloatPrecision = RS_FP_Imprecise;
+    } else if (!Full.compare(PrecisionPropBuf)) {
+      ALOGE("Switching to RS FP full mode via setprop");
+      mRSFloatPrecision = RS_FP_Full;
+    }
+  }
+
   return;
 }
 
diff --git a/bcinfo/tools/Android.mk b/bcinfo/tools/Android.mk
index 0d99f71..f7cf34b 100644
--- a/bcinfo/tools/Android.mk
+++ b/bcinfo/tools/Android.mk
@@ -16,6 +16,8 @@
 
 LOCAL_PATH := $(call my-dir)
 
+LLVM_ROOT_PATH := external/llvm
+
 # Executable for host
 # ========================================================
 include $(CLEAR_VARS)
@@ -28,6 +30,12 @@
 LOCAL_SHARED_LIBRARIES := \
   libbcinfo
 
+LOCAL_STATIC_LIBRARIES := \
+  libLLVMBitReader \
+  libLLVMBitWriter \
+  libLLVMCore \
+  libLLVMSupport
+
 LOCAL_CFLAGS += -D__HOST__
 
 LOCAL_C_INCLUDES := \
@@ -37,5 +45,6 @@
 
 LOCAL_LDLIBS = -ldl
 
+include $(LLVM_ROOT_PATH)/llvm-host-build.mk
 include $(BUILD_HOST_EXECUTABLE)
 
diff --git a/bcinfo/tools/main.cpp b/bcinfo/tools/main.cpp
index 1360c68..2d1e449 100644
--- a/bcinfo/tools/main.cpp
+++ b/bcinfo/tools/main.cpp
@@ -18,6 +18,16 @@
 #include <bcinfo/BitcodeWrapper.h>
 #include <bcinfo/MetadataExtractor.h>
 
+#include <llvm/ADT/OwningPtr.h>
+#include <llvm/ADT/StringRef.h>
+#include <llvm/Assembly/AssemblyAnnotationWriter.h>
+#include <llvm/Bitcode/ReaderWriter.h>
+#include <llvm/LLVMContext.h>
+#include <llvm/Module.h>
+#include <llvm/Support/ManagedStatic.h>
+#include <llvm/Support/MemoryBuffer.h>
+#include <llvm/Support/ToolOutputFile.h>
+
 #include <ctype.h>
 #include <dlfcn.h>
 #include <stdarg.h>
@@ -33,12 +43,14 @@
 
 #include <unistd.h>
 
+#include <string>
 #include <vector>
 
 // This file corresponds to the standalone bcinfo tool. It prints a variety of
 // information about a supplied bitcode input file.
 
-const char* inFile = NULL;
+std::string inFile;
+std::string outFile;
 
 extern int opterr;
 extern int optind;
@@ -72,6 +84,13 @@
   }
 
   inFile = argv[optind];
+
+  int l = inFile.length();
+  if (l > 3 && inFile[l-3] == '.' && inFile[l-2] == 'b' && inFile[l-1] == 'c') {
+    outFile = std::string(inFile.begin(), inFile.end() - 3) + ".ll";
+  } else {
+    outFile = inFile + ".ll";
+  }
   return 1;
 }
 
@@ -141,26 +160,26 @@
 
 
 static size_t readBitcode(const char **bitcode) {
-  if (!inFile) {
+  if (!inFile.length()) {
     fprintf(stderr, "input file required\n");
-    return NULL;
+    return 0;
   }
 
   struct stat statInFile;
-  if (stat(inFile, &statInFile) < 0) {
+  if (stat(inFile.c_str(), &statInFile) < 0) {
     fprintf(stderr, "Unable to stat input file: %s\n", strerror(errno));
-    return NULL;
+    return 0;
   }
 
   if (!S_ISREG(statInFile.st_mode)) {
     fprintf(stderr, "Input file should be a regular file.\n");
-    return NULL;
+    return 0;
   }
 
-  FILE *in = fopen(inFile, "r");
+  FILE *in = fopen(inFile.c_str(), "r");
   if (!in) {
-    fprintf(stderr, "Could not open input file %s\n", inFile);
-    return NULL;
+    fprintf(stderr, "Could not open input file %s\n", inFile.c_str());
+    return 0;
   }
 
   size_t bitcodeSize = statInFile.st_size;
@@ -169,7 +188,7 @@
   size_t nread = fread((void*) *bitcode, 1, bitcodeSize, in);
 
   if (nread != bitcodeSize)
-      fprintf(stderr, "Could not read all of file %s\n", inFile);
+      fprintf(stderr, "Could not read all of file %s\n", inFile.c_str());
 
   fclose(in);
   return nread;
@@ -192,7 +211,6 @@
   }
 
   const char *bitcode = NULL;
-  const char *translatedBitcode = NULL;
   size_t bitcodeSize = readBitcode(&bitcode);
 
   unsigned int version = 0;
@@ -209,25 +227,58 @@
   printf("compilerVersion: %u\n", bcWrapper.getCompilerVersion());
   printf("optimizationLevel: %u\n\n", bcWrapper.getOptimizationLevel());
 
-  bcinfo::BitcodeTranslator *BT =
-      new bcinfo::BitcodeTranslator(bitcode, bitcodeSize, version);
+  llvm::OwningPtr<bcinfo::BitcodeTranslator> BT;
+  BT.reset(new bcinfo::BitcodeTranslator(bitcode, bitcodeSize, version));
   if (!BT->translate()) {
     fprintf(stderr, "failed to translate bitcode\n");
     return 3;
   }
 
-  bcinfo::MetadataExtractor *ME =
-      new bcinfo::MetadataExtractor(BT->getTranslatedBitcode(),
-                                    BT->getTranslatedBitcodeSize());
+  llvm::OwningPtr<bcinfo::MetadataExtractor> ME;
+  ME.reset(new bcinfo::MetadataExtractor(BT->getTranslatedBitcode(),
+                                         BT->getTranslatedBitcodeSize()));
   if (!ME->extract()) {
     fprintf(stderr, "failed to get metadata\n");
     return 4;
   }
 
-  dumpMetadata(ME);
+  dumpMetadata(ME.get());
 
-  delete ME;
-  delete BT;
+  const char *translatedBitcode = BT->getTranslatedBitcode();
+  size_t translatedBitcodeSize = BT->getTranslatedBitcodeSize();
+
+  llvm::LLVMContext &ctx = llvm::getGlobalContext();
+  llvm::llvm_shutdown_obj called_on_exit;
+
+  llvm::OwningPtr<llvm::MemoryBuffer> mem;
+
+  mem.reset(llvm::MemoryBuffer::getMemBuffer(
+      llvm::StringRef(translatedBitcode, translatedBitcodeSize),
+      inFile.c_str(), false));
+
+  llvm::OwningPtr<llvm::Module> module;
+  std::string errmsg;
+  module.reset(llvm::ParseBitcodeFile(mem.get(), ctx, &errmsg));
+  if (module.get() != 0 && module->MaterializeAllPermanently(&errmsg)) {
+    module.reset();
+  }
+
+  if (module.get() == 0) {
+    if (errmsg.size()) {
+      fprintf(stderr, "error: %s\n", errmsg.c_str());
+    } else {
+      fprintf(stderr, "error: failed to parse bitcode file\n");
+    }
+    return 5;
+  }
+
+  llvm::OwningPtr<llvm::tool_output_file> tof(
+      new llvm::tool_output_file(outFile.c_str(), errmsg,
+                                 llvm::raw_fd_ostream::F_Binary));
+  llvm::OwningPtr<llvm::AssemblyAnnotationWriter> ann;
+  module->print(tof->os(), ann.get());
+
+  tof->keep();
 
   releaseBitcode(&bitcode);
 
diff --git a/lib/ExecutionEngine/Script.cpp b/lib/ExecutionEngine/Script.cpp
index 11ef0e1..0c3c7a5 100644
--- a/lib/ExecutionEngine/Script.cpp
+++ b/lib/ExecutionEngine/Script.cpp
@@ -48,6 +48,12 @@
   return strcmp(buf, "0") != 0;
 }
 
+bool isSetProp(const char *str) {
+  char buf[PROPERTY_VALUE_MAX];
+  property_get(str, buf, "");
+  return buf[0] != '\0';
+}
+
 } // namespace anonymous
 
 namespace bcc {
@@ -762,6 +768,11 @@
     return false;
   }
 
+  if (isSetProp("debug.rs.precision")) {
+    // If we have a floating point precision override, don't use the cache.
+    return false;
+  }
+
   if (mCacheDir.empty() || mCacheName.empty()) {
     // The application developer has not specified the cachePath, so
     // we don't know where to open the cache file.
diff --git a/lib/ScriptCRT/rs_core.c b/lib/ScriptCRT/rs_core.c
index f655c05..aaf1336 100644
--- a/lib/ScriptCRT/rs_core.c
+++ b/lib/ScriptCRT/rs_core.c
@@ -2,29 +2,11 @@
 #include "rs_graphics.rsh"
 #include "rs_structs.h"
 
-/* Declaration of 4 basic functions in libRS */
-extern void __attribute__((overloadable))
-    rsDebug(const char *, float, float);
-extern void __attribute__((overloadable))
-    rsDebug(const char *, float, float, float);
-extern void __attribute__((overloadable))
-    rsDebug(const char *, float, float, float, float);
+/* Function declarations from libRS */
 extern float4 __attribute__((overloadable)) convert_float4(uchar4 c);
 
 /* Implementation of Core Runtime */
 
-extern void __attribute__((overloadable)) rsDebug(const char *s, float2 v) {
-    rsDebug(s, v.x, v.y);
-}
-
-extern void __attribute__((overloadable)) rsDebug(const char *s, float3 v) {
-    rsDebug(s, v.x, v.y, v.z);
-}
-
-extern void __attribute__((overloadable)) rsDebug(const char *s, float4 v) {
-    rsDebug(s, v.x, v.y, v.z, v.w);
-}
-
 /*
 extern uchar4 __attribute__((overloadable)) rsPackColorTo8888(float r, float g, float b)
 {
diff --git a/tools/bcc/Android.mk b/tools/bcc/Android.mk
index 414b4d2..3231228 100644
--- a/tools/bcc/Android.mk
+++ b/tools/bcc/Android.mk
@@ -14,9 +14,6 @@
 # limitations under the License.
 #
 
-ifeq (darwin,$(BUILD_OS))
-else
-
 LOCAL_PATH := $(call my-dir)
 
 # Executable for host
@@ -36,6 +33,10 @@
 
 LOCAL_MODULE_TAGS := tests eng
 
+# The definition of those functions in libLLVMSupport may elude libbcc due to linker.
+# Should include libLLVMSupport since bcc references some functions within it.
+LOCAL_STATIC_LIBRARIES := libLLVMSupport
+LOCAL_SHARED_LIBRARIES := libbcc
 LOCAL_LDLIBS = -ldl
 
 LOCAL_CFLAGS += -D__HOST__ -Wall -Werror
@@ -60,5 +61,3 @@
 
 include external/stlport/libstlport.mk
 include $(BUILD_EXECUTABLE)
-
-endif