Merge "[MIPS] Set default MIPS64 ISA revision to R6"
diff --git a/Android.mk b/Android.mk
index c35d499..ecfea90 100644
--- a/Android.mk
+++ b/Android.mk
@@ -14,9 +14,6 @@
 # limitations under the License.
 #
 
-# Don't build for unbundled branches
-ifeq (,$(TARGET_BUILD_APPS))
-
 LOCAL_PATH := $(call my-dir)
 LIBBCC_ROOT_PATH := $(LOCAL_PATH)
 include $(LIBBCC_ROOT_PATH)/libbcc.mk
@@ -37,10 +34,6 @@
 # Device Shared Library libbcc
 #=====================================================================
 ifneq (true,$(DISABLE_LLVM_DEVICE_BUILDS))
-ifeq ($(TARGET_ARCH),arm64)
-$(info TODOArm64: $(LOCAL_PATH)/Android.mk Enable libbcc build)
-endif
-
 ifeq ($(TARGET_ARCH),mips64)
 $(info TODOMips64: $(LOCAL_PATH)/Android.mk Enable libbcc build)
 endif
@@ -77,6 +70,9 @@
 # Host Shared Library libbcc
 #=====================================================================
 
+# Don't build for unbundled branches
+ifeq (,$(TARGET_BUILD_APPS))
+
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := libbcc
diff --git a/bcinfo/Android.mk b/bcinfo/Android.mk
index 6e073c1..cb6af97 100644
--- a/bcinfo/Android.mk
+++ b/bcinfo/Android.mk
@@ -14,9 +14,6 @@
 # limitations under the License.
 #
 
-# Don't build for unbundled branches
-ifeq (,$(TARGET_BUILD_APPS))
-
 local_cflags_for_libbcinfo := -Wall -Wno-unused-parameter -Werror
 ifneq ($(TARGET_BUILD_VARIANT),eng)
 local_cflags_for_libbcinfo += -D__DISABLE_ASSERTS
@@ -72,6 +69,9 @@
 include $(BUILD_SHARED_LIBRARY)
 endif
 
+# Don't build for unbundled branches
+ifeq (,$(TARGET_BUILD_APPS))
+
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := libbcinfo
diff --git a/bcinfo/BitReader_2_7/BitcodeReader.cpp b/bcinfo/BitReader_2_7/BitcodeReader.cpp
index 6b8ee3d..3e100db 100644
--- a/bcinfo/BitReader_2_7/BitcodeReader.cpp
+++ b/bcinfo/BitReader_2_7/BitcodeReader.cpp
@@ -3068,8 +3068,8 @@
 }
 
 namespace {
-class BitcodeErrorCategoryType : public std::__do_message {
-  const char *name() const override {
+class BitcodeErrorCategoryType : public std::error_category {
+  const char *name() const LLVM_NOEXCEPT override {
     return "llvm.bitcode";
   }
   std::string message(int IE) const override {
diff --git a/bcinfo/BitReader_3_0/BitcodeReader.cpp b/bcinfo/BitReader_3_0/BitcodeReader.cpp
index 353949e..c85588a 100644
--- a/bcinfo/BitReader_3_0/BitcodeReader.cpp
+++ b/bcinfo/BitReader_3_0/BitcodeReader.cpp
@@ -3403,8 +3403,8 @@
 }
 
 namespace {
-class BitcodeErrorCategoryType : public std::__do_message {
-  const char *name() const override {
+class BitcodeErrorCategoryType : public std::error_category {
+  const char *name() const LLVM_NOEXCEPT override {
     return "llvm.bitcode";
   }
   std::string message(int IE) const override {
diff --git a/bcinfo/tools/main.cpp b/bcinfo/tools/main.cpp
index 6bcdbdf..64969f3 100644
--- a/bcinfo/tools/main.cpp
+++ b/bcinfo/tools/main.cpp
@@ -121,19 +121,19 @@
     return 2;
   }
 
-  fprintf(info, "exportVarCount: %u\n", ME->getExportVarCount());
+  fprintf(info, "exportVarCount: %zu\n", ME->getExportVarCount());
   const char **varNameList = ME->getExportVarNameList();
   for (size_t i = 0; i < ME->getExportVarCount(); i++) {
     fprintf(info, "%s\n", varNameList[i]);
   }
 
-  fprintf(info, "exportFuncCount: %u\n", ME->getExportFuncCount());
+  fprintf(info, "exportFuncCount: %zu\n", ME->getExportFuncCount());
   const char **funcNameList = ME->getExportFuncNameList();
   for (size_t i = 0; i < ME->getExportFuncCount(); i++) {
     fprintf(info, "%s\n", funcNameList[i]);
   }
 
-  fprintf(info, "exportForEachCount: %u\n",
+  fprintf(info, "exportForEachCount: %zu\n",
           ME->getExportForEachSignatureCount());
   const char **nameList = ME->getExportForEachNameList();
   const uint32_t *sigList = ME->getExportForEachSignatureList();
@@ -143,7 +143,7 @@
             inputCountList[i]);
   }
 
-  fprintf(info, "objectSlotCount: %u\n", ME->getObjectSlotCount());
+  fprintf(info, "objectSlotCount: %zu\n", ME->getObjectSlotCount());
   const uint32_t *slotList = ME->getObjectSlotList();
   for (size_t i = 0; i < ME->getObjectSlotCount(); i++) {
     fprintf(info, "%u\n", slotList[i]);
@@ -172,43 +172,43 @@
     break;
   }
 
-  printf("exportVarCount: %u\n", ME->getExportVarCount());
+  printf("exportVarCount: %zu\n", ME->getExportVarCount());
   const char **varNameList = ME->getExportVarNameList();
   for (size_t i = 0; i < ME->getExportVarCount(); i++) {
-    printf("var[%u]: %s\n", i, varNameList[i]);
+    printf("var[%zu]: %s\n", i, varNameList[i]);
   }
   printf("\n");
 
-  printf("exportFuncCount: %u\n", ME->getExportFuncCount());
+  printf("exportFuncCount: %zu\n", ME->getExportFuncCount());
   const char **funcNameList = ME->getExportFuncNameList();
   for (size_t i = 0; i < ME->getExportFuncCount(); i++) {
-    printf("func[%u]: %s\n", i, funcNameList[i]);
+    printf("func[%zu]: %s\n", i, funcNameList[i]);
   }
   printf("\n");
 
-  printf("exportForEachSignatureCount: %u\n",
+  printf("exportForEachSignatureCount: %zu\n",
          ME->getExportForEachSignatureCount());
   const char **nameList = ME->getExportForEachNameList();
   const uint32_t *sigList = ME->getExportForEachSignatureList();
   const uint32_t *inputCountList = ME->getExportForEachInputCountList();
   for (size_t i = 0; i < ME->getExportForEachSignatureCount(); i++) {
-    printf("exportForEachSignatureList[%u]: %s - %u - %u\n", i, nameList[i],
+    printf("exportForEachSignatureList[%zu]: %s - %u - %u\n", i, nameList[i],
            sigList[i], inputCountList[i]);
   }
   printf("\n");
 
-  printf("pragmaCount: %u\n", ME->getPragmaCount());
+  printf("pragmaCount: %zu\n", ME->getPragmaCount());
   const char **keyList = ME->getPragmaKeyList();
   const char **valueList = ME->getPragmaValueList();
   for (size_t i = 0; i < ME->getPragmaCount(); i++) {
-    printf("pragma[%u]: %s - %s\n", i, keyList[i], valueList[i]);
+    printf("pragma[%zu]: %s - %s\n", i, keyList[i], valueList[i]);
   }
   printf("\n");
 
-  printf("objectSlotCount: %u\n", ME->getObjectSlotCount());
+  printf("objectSlotCount: %zu\n", ME->getObjectSlotCount());
   const uint32_t *slotList = ME->getObjectSlotList();
   for (size_t i = 0; i < ME->getObjectSlotCount(); i++) {
-    printf("objectSlotList[%u]: %u\n", i, slotList[i]);
+    printf("objectSlotList[%zu]: %u\n", i, slotList[i]);
   }
   printf("\n");
 
diff --git a/include/bcc/Renderscript/RSScript.h b/include/bcc/Renderscript/RSScript.h
index 5f9e405..cd4385f 100644
--- a/include/bcc/Renderscript/RSScript.h
+++ b/include/bcc/Renderscript/RSScript.h
@@ -21,10 +21,6 @@
 #include "bcc/Renderscript/RSInfo.h"
 #include "bcc/Support/Sha1Util.h"
 
-namespace llvm {
-  class Module;
-}  // end namespace llvm
-
 namespace bcc {
 
 class RSScript;
diff --git a/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp b/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
index 867b7ab..a013b18 100644
--- a/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
+++ b/lib/ExecutionEngine/ELFObjectLoaderImpl.cpp
@@ -84,7 +84,7 @@
   if (elf_header->e_shoff > pDebugImgSize) {
 #ifdef __LP64__
     ALOGE("Invalid section header table offset found! (e_shoff = %ld)",
-	  elf_header->e_shoff);
+    static_cast<unsigned long>(elf_header->e_shoff));
 #else
     ALOGE("Invalid section header table offset found! (e_shoff = %d)",
           elf_header->e_shoff);
@@ -96,8 +96,8 @@
        sizeof(llvm::ELF::Elf32_Shdr) * elf_header->e_shnum) > pDebugImgSize) {
 #ifdef __LP64__
     ALOGE("Invalid image supplied (debug image doesn't contain all the section"
-	  "header or corrupted image)! (e_shoff = %ld, e_shnum = %d)",
-	  elf_header->e_shoff, elf_header->e_shnum);
+    "header or corrupted image)! (e_shoff = %ld, e_shnum = %d)",
+    static_cast<unsigned long>(elf_header->e_shoff), elf_header->e_shnum);
 #else
     ALOGE("Invalid image supplied (debug image doesn't contain all the section"
           "header or corrupted image)! (e_shoff = %d, e_shnum = %d)",
diff --git a/lib/Renderscript/RSForEachExpand.cpp b/lib/Renderscript/RSForEachExpand.cpp
index ec3d330..a9150a7 100644
--- a/lib/Renderscript/RSForEachExpand.cpp
+++ b/lib/Renderscript/RSForEachExpand.cpp
@@ -120,6 +120,43 @@
     return 0;
   }
 
+  bool isStepOptSupported(llvm::Type *AllocType) {
+
+    llvm::PointerType *PT = llvm::dyn_cast<llvm::PointerType>(AllocType);
+    llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*Context);
+
+    if (mEnableStepOpt) {
+      return false;
+    }
+
+    if (AllocType == VoidPtrTy) {
+      return false;
+    }
+
+    if (!PT) {
+      return false;
+    }
+
+    // remaining conditions are 64-bit only
+    if (VoidPtrTy->getPrimitiveSizeInBits() == 32) {
+      return true;
+    }
+
+    // coerce suggests an upconverted struct type, which we can't support
+    if (AllocType->getStructName().find("coerce") != llvm::StringRef::npos) {
+      return false;
+    }
+
+    // 2xi64 and i128 suggest an upconverted struct type, which are also unsupported
+    llvm::Type *V2xi64Ty = llvm::VectorType::get(llvm::Type::getInt64Ty(*Context), 2);
+    llvm::Type *Int128Ty = llvm::Type::getIntNTy(*Context, 128);
+    if (AllocType == V2xi64Ty || AllocType == Int128Ty) {
+      return false;
+    }
+
+    return true;
+  }
+
   // Get the actual value we should use to step through an allocation.
   //
   // Normally the value we use to step through an allocation is given to us by
@@ -136,8 +173,7 @@
     bccAssert(AllocType);
     bccAssert(OrigStep);
     llvm::PointerType *PT = llvm::dyn_cast<llvm::PointerType>(AllocType);
-    llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*Context);
-    if (mEnableStepOpt && AllocType != VoidPtrTy && PT) {
+    if (isStepOptSupported(AllocType)) {
       llvm::Type *ET = PT->getElementType();
       uint64_t ETSize = DL->getTypeAllocSize(ET);
       llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*Context);
diff --git a/lib/Support/CompilerConfig.cpp b/lib/Support/CompilerConfig.cpp
index 1d9270e..0436a61 100644
--- a/lib/Support/CompilerConfig.cpp
+++ b/lib/Support/CompilerConfig.cpp
@@ -135,7 +135,15 @@
 
 #if defined(TARGET_BUILD)
     if (!getProperty("debug.rs.arm-no-tune-for-cpu")) {
+#ifndef FORCE_CPU_VARIANT_32
       setCPU(llvm::sys::getHostCPUName());
+#else
+#define XSTR(S) #S
+#define STR(S) XSTR(S)
+      setCPU(STR(FORCE_CPU_VARIANT_32));
+#undef STR
+#undef XSTR
+#endif
     }
 #endif  // TARGET_BUILD
 
@@ -147,7 +155,16 @@
   case llvm::Triple::aarch64:
 #if defined(TARGET_BUILD)
     if (!getProperty("debug.rs.arm-no-tune-for-cpu")) {
+#ifndef FORCE_CPU_VARIANT_64
       setCPU(llvm::sys::getHostCPUName());
+#else
+#define XSTR(S) #S
+#define STR(S) XSTR(S)
+      setCPU(STR(FORCE_CPU_VARIANT_64));
+#undef STR
+#undef XSTR
+#endif
+
     }
 #endif  // TARGET_BUILD
     break;
diff --git a/libbcc-device-build.mk b/libbcc-device-build.mk
index 5324e15..0dc21b0 100644
--- a/libbcc-device-build.mk
+++ b/libbcc-device-build.mk
@@ -24,6 +24,15 @@
   $(RS_VERSION_DEFINE) \
   $(LOCAL_CFLAGS)
 
+
+ifneq ($(BOARD_OVERRIDE_RS_CPU_VARIANT_32),)
+LOCAL_CFLAGS += -DFORCE_CPU_VARIANT_32=$(BOARD_OVERRIDE_RS_CPU_VARIANT_32)
+endif
+
+ifneq ($(BOARD_OVERRIDE_RS_CPU_VARIANT_64),)
+LOCAL_CFLAGS += -DFORCE_CPU_VARIANT_64=$(BOARD_OVERRIDE_RS_CPU_VARIANT_64)
+endif
+
 ifeq ($(TARGET_BUILD_VARIANT),eng)
 LOCAL_CFLAGS += -DANDROID_ENGINEERING_BUILD
 else
diff --git a/libbcc-host-build.mk b/libbcc-host-build.mk
index d9b4cdd..5ccd657 100644
--- a/libbcc-host-build.mk
+++ b/libbcc-host-build.mk
@@ -26,6 +26,14 @@
   $(RS_VERSION_DEFINE) \
   $(LOCAL_CFLAGS)
 
+ifneq ($(BOARD_OVERRIDE_RS_CPU_VARIANT_32),)
+LOCAL_CFLAGS += -DFORCE_CPU_VARIANT_32=$(BOARD_OVERRIDE_RS_CPU_VARIANT_32)
+endif
+
+ifneq ($(BOARD_OVERRIDE_RS_CPU_VARIANT_64),)
+LOCAL_CFLAGS += -DFORCE_CPU_VARIANT_64=$(BOARD_OVERRIDE_RS_CPU_VARIANT_64)
+endif
+
 ifeq ($(TARGET_BUILD_VARIANT),eng)
 libbcc_CFLAGS += -DANDROID_ENGINEERING_BUILD
 else
@@ -39,5 +47,3 @@
   $(LOCAL_C_INCLUDES)
 
 LOCAL_IS_HOST_MODULE := true
-
-LOCAL_32_BIT_ONLY := true
diff --git a/libbcc-targets.mk b/libbcc-targets.mk
index 35b1ee7..a95a9aa 100644
--- a/libbcc-targets.mk
+++ b/libbcc-targets.mk
@@ -35,6 +35,12 @@
 LOCAL_CFLAGS_x86 += -DFORCE_X86_CODEGEN
 LOCAL_CFLAGS_x86_64 += -DFORCE_X86_64_CODEGEN
 
+ifeq ($(BUILD_ARM_FOR_X86),true)
+LOCAL_CFLAGS_x86 += -DPROVIDE_ARM_CODEGEN -DFORCE_BUILD_ARM
+LOCAL_CFLAGS_x86_64 += -DPROVIDE_ARM_CODEGEN -DFORCE_BUILD_ARM
+endif
+
+
 ifeq (,$(filter $(TARGET_ARCH),arm64 arm mips mips64 x86 x86_64))
   $(error Unsupported architecture $(TARGET_ARCH))
 endif
diff --git a/tools/bcc_strip_attr/Android.mk b/tools/bcc_strip_attr/Android.mk
index 14ac4a3..a8296bc 100644
--- a/tools/bcc_strip_attr/Android.mk
+++ b/tools/bcc_strip_attr/Android.mk
@@ -16,9 +16,6 @@
 
 LOCAL_PATH := $(call my-dir)
 
-# Don't build for unbundled branches
-ifeq (,$(TARGET_BUILD_APPS))
-
 # Executable for host
 # ========================================================
 include $(CLEAR_VARS)
@@ -40,5 +37,3 @@
 include $(LIBBCC_HOST_BUILD_MK)
 include $(LLVM_HOST_BUILD_MK)
 include $(BUILD_HOST_EXECUTABLE)
-
-endif  # Don't build for unbundled branches