Merge lib/Transforms into lib/ExecutionEngine.

Rename BCCTransforms to RSTransforms and ForEachExpand.cpp to
RSForEachExpand.cpp. These are RenderScript-specific stuffs.

Type of parameter passed to RSForEachExpand pass was also modified to
use "vector of pairs (function name, foreach signature)."

bcc_assert.h is removed in this commit.

This commit is not expected to change any sematics.

Change-Id: I4fbd89d9c36e5df29d03f8c938c111dd742dc079
diff --git a/Android.mk b/Android.mk
index 2aca0da..db76dec 100644
--- a/Android.mk
+++ b/Android.mk
@@ -28,8 +28,7 @@
 
 libbcc_WHOLE_STATIC_LIBRARIES += \
   libbccExecutionEngine \
-  libbccHelper \
-  libbccTransforms
+  libbccHelper
 
 
 #=====================================================================
diff --git a/include/bcc/bcc_assert.h b/include/bcc/bcc_assert.h
deleted file mode 100644
index 757961e..0000000
--- a/include/bcc/bcc_assert.h
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright 2011, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _FRAMEWORKS_COMPILE_LIBBCC_INCLUDE_BCC_BCC_ASSERT_H_  // NOLINT
-#define _FRAMEWORKS_COMPILE_LIBBCC_INCLUDE_BCC_BCC_ASSERT_H_
-
-#include "DebugHelper.h"
-
-#ifdef __cplusplus
-#include <cstdlib>
-#include <cstdio>
-#else
-#include <stdlib.h>
-#include <stdio.h>
-#endif  // __cplusplus
-
-#ifdef __DISABLE_ASSERTS
-#define bccAssert(v) do {} while (0)
-#else
-#define __ABORT_ON_FAILURES 1
-#define bccAssert(v)                                \
-  do {                                              \
-    if (!(v)) {                                     \
-      ALOGE("bccAssert failed at %s:%d - '%s'\n",   \
-          __FILE__, __LINE__, #v);                  \
-      if (__ABORT_ON_FAILURES) {                    \
-        abort();                                    \
-      }                                             \
-    }                                               \
-  } while (0)
-#endif  // __DISABLE_ASSERTS
-
-
-#endif  // _FRAMEWORKS_COMPILE_LIBBCC_INCLUDE_BCC_BCC_ASSERT_H_  NOLINT
diff --git a/lib/ExecutionEngine/Android.mk b/lib/ExecutionEngine/Android.mk
index d101107..f9877d5 100644
--- a/lib/ExecutionEngine/Android.mk
+++ b/lib/ExecutionEngine/Android.mk
@@ -39,6 +39,7 @@
   ObjectLoader.cpp \
   OutputFile.cpp \
   RSExecutable.cpp \
+  RSForEachExpand.cpp \
   RSInfo.cpp \
   RSInfoExtractor.cpp \
   RSInfoReader.cpp \
diff --git a/lib/ExecutionEngine/Compiler.cpp b/lib/ExecutionEngine/Compiler.cpp
index c4a0870..458d835 100644
--- a/lib/ExecutionEngine/Compiler.cpp
+++ b/lib/ExecutionEngine/Compiler.cpp
@@ -33,7 +33,7 @@
 
 #include "librsloader.h"
 
-#include "Transforms/BCCTransforms.h"
+#include "RSTransforms.h"
 
 #include "llvm/ADT/StringRef.h"
 
@@ -270,8 +270,7 @@
   std::vector<std::string> &VarNameList = mpResult->mExportVarsName;
   std::vector<std::string> &FuncNameList = mpResult->mExportFuncsName;
   std::vector<std::string> &ForEachExpandList = mpResult->mExportForEachName;
-  std::vector<std::string> ForEachNameList;
-  std::vector<uint32_t> ForEachSigList;
+  RSInfo::ExportForeachFuncListTy ForEachFuncList;
   std::vector<const char*> ExportSymbols;
 
   // Defaults to maximum optimization level from MetadataExtractor.
@@ -367,10 +366,9 @@
     const char **ForEachNames = ME.getExportForEachNameList();
     const uint32_t *ForEachSigs = ME.getExportForEachSignatureList();
     for (size_t i = 0; i < ForEachSigCount; i++) {
-      std::string Name(ForEachNames[i]);
-      ForEachNameList.push_back(Name);
-      ForEachExpandList.push_back(Name + ".expand");
-      ForEachSigList.push_back(ForEachSigs[i]);
+      ForEachFuncList.push_back(std::make_pair(ForEachNames[i],
+                                               ForEachSigs[i]));
+      ForEachExpandList.push_back(std::string(ForEachNames[i]) + ".expand");
     }
 
     // Need to wait until ForEachExpandList is fully populated to fill in
@@ -388,7 +386,7 @@
     }
   }
 
-  runInternalPasses(ForEachNameList, ForEachSigList);
+  runInternalPasses(ForEachFuncList);
 
   // Perform link-time optimization if we have multiple modules
   if (option.RunLTO) {
@@ -516,12 +514,11 @@
   return 0;
 }
 
-int Compiler::runInternalPasses(std::vector<std::string>& Names,
-                                std::vector<uint32_t>& Signatures) {
+int Compiler::runInternalPasses(RSInfo::ExportForeachFuncListTy pForEachFuncs) {
   llvm::PassManager BCCPasses;
 
   // Expand ForEach on CPU path to reduce launch overhead.
-  BCCPasses.add(createForEachExpandPass(Names, Signatures));
+  BCCPasses.add(createRSForEachExpandPass(pForEachFuncs));
 
   BCCPasses.run(*mModule);
 
diff --git a/lib/ExecutionEngine/Compiler.h b/lib/ExecutionEngine/Compiler.h
index b5872d6..f0e75d1 100644
--- a/lib/ExecutionEngine/Compiler.h
+++ b/lib/ExecutionEngine/Compiler.h
@@ -36,6 +36,8 @@
 #include <vector>
 #include <utility>
 
+#include "RSInfo.h"
+
 
 namespace llvm {
   class Module;
@@ -139,8 +141,7 @@
 
     int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
 
-    int runInternalPasses(std::vector<std::string>& Names,
-                          std::vector<uint32_t>& Signatures);
+    int runInternalPasses(RSInfo::ExportForeachFuncListTy pForEachFuncs);
 
     int runLTO(llvm::TargetData *TD,
                std::vector<const char*>& ExportSymbols,
diff --git a/lib/Transforms/ForEachExpand.cpp b/lib/ExecutionEngine/RSForEachExpand.cpp
similarity index 82%
rename from lib/Transforms/ForEachExpand.cpp
rename to lib/ExecutionEngine/RSForEachExpand.cpp
index 53d6bdc..b30dd23 100644
--- a/lib/Transforms/ForEachExpand.cpp
+++ b/lib/ExecutionEngine/RSForEachExpand.cpp
@@ -14,10 +14,15 @@
  * limitations under the License.
  */
 
+//#define RS_FOREACH_EXPAND_PASS_NDEBUG 0
+#include "RSTransforms.h"
+
+#include <cstdlib>
+
 #include "Config.h"
-#include "bcc/bcc_assert.h"
 
 #include "DebugHelper.h"
+#include "RSInfo.h"
 
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
@@ -27,24 +32,26 @@
 #include "llvm/Type.h"
 #include "llvm/Support/IRBuilder.h"
 
+using namespace bcc;
+
 namespace {
-  /* ForEachExpandPass - This pass operates on functions that are able to be
-   * called via rsForEach() or "foreach_<NAME>". We create an inner loop for
-   * the ForEach-able function to be invoked over the appropriate data cells
-   * of the input/output allocations (adjusting other relevant parameters as
-   * we go). We support doing this for any ForEach-able compute kernels.
-   * The new function name is the original function name followed by
-   * ".expand". Note that we still generate code for the original function.
-   */
-  class ForEachExpandPass : public llvm::ModulePass {
-  private:
+
+/* RSForEachExpandPass - This pass operates on functions that are able to be
+ * called via rsForEach() or "foreach_<NAME>". We create an inner loop for the
+ * ForEach-able function to be invoked over the appropriate data cells of the
+ * input/output allocations (adjusting other relevant parameters as we go). We
+ * support doing this for any ForEach-able compute kernels. The new function
+ * name is the original function name followed by ".expand". Note that we
+ * still generate code for the original function.
+ */
+class RSForEachExpandPass : public llvm::ModulePass {
+private:
   static char ID;
 
   llvm::Module *M;
   llvm::LLVMContext *C;
 
-  std::vector<std::string>& mNames;
-  std::vector<uint32_t>& mSignatures;
+  const RSInfo::ExportForeachFuncListTy &mFuncs;
 
   uint32_t getRootSignature(llvm::Function *F) {
     const llvm::NamedMDNode *ExportForEachMetadata =
@@ -65,7 +72,13 @@
       return (1 << RootArgTys.size()) - 1;
     }
 
-    bccAssert(ExportForEachMetadata->getNumOperands() > 0);
+#if !RS_FOREACH_EXPAND_PASS_NDEBUG
+    if (ExportForEachMetadata->getNumOperands() <= 0) {
+      ALOGE("Assert failed at %s:%d: Invalid #rs_export_foreach metadata in "
+            " '%s'!", __FILE__, __LINE__, M->getModuleIdentifier().c_str());
+      ::abort();
+    }
+#endif
 
     // We only handle the case for legacy root() functions here, so this is
     // hard-coded to look at only the first such function.
@@ -107,11 +120,9 @@
     return Signature & 16;
   }
 
-  public:
-  ForEachExpandPass(std::vector<std::string>& Names,
-                    std::vector<uint32_t>& Signatures)
-      : ModulePass(ID), M(NULL), C(NULL), mNames(Names),
-        mSignatures(Signatures) {
+public:
+  RSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs)
+      : ModulePass(ID), M(NULL), C(NULL), mFuncs(pForeachFuncs) {
   }
 
   /* Performs the actual optimization on a selected function. On success, the
@@ -260,7 +271,13 @@
       Args++;
     }
 
-    bccAssert(Args == F->arg_end());
+#if !RS_FOREACH_EXPAND_PASS_NDEBUG
+    if (Args != F->arg_end()) {
+      ALOGE("Assert failed at %s:%d: Invalid signature to the foreach function "
+            "'%s'!", __FILE__, __LINE__, F->getName().str().c_str());
+      ::abort();
+    }
+#endif
 
     llvm::BasicBlock *Loop = llvm::BasicBlock::Create(*C, "Loop", ExpandedFunc);
     llvm::BasicBlock *Exit = llvm::BasicBlock::Create(*C, "Exit", ExpandedFunc);
@@ -339,11 +356,14 @@
     this->M = &M;
     C = &M.getContext();
 
-    bccAssert(mNames.size() == mSignatures.size());
-    for (int i = 0, e = mNames.size(); i != e; i++) {
-      llvm::Function *kernel = M.getFunction(mNames[i]);
+    for (RSInfo::ExportForeachFuncListTy::const_iterator
+             func_iter = mFuncs.begin(), func_end = mFuncs.end();
+         func_iter != func_end; func_iter++) {
+      const char *name = func_iter->first;
+      uint32_t signature = func_iter->second;
+      llvm::Function *kernel = M.getFunction(name);
       if (kernel && kernel->getReturnType()->isVoidTy()) {
-        Changed |= ExpandFunction(kernel, mSignatures[i]);
+        Changed |= ExpandFunction(kernel, signature);
       }
     }
 
@@ -354,16 +374,17 @@
     return "ForEach-able Function Expansion";
   }
 
-  };
-}  // end anonymous namespace
+}; // end RSForEachExpandPass
 
-char ForEachExpandPass::ID = 0;
+} // end anonymous namespace
+
+char RSForEachExpandPass::ID = 0;
 
 namespace bcc {
 
-  llvm::ModulePass *createForEachExpandPass(std::vector<std::string>& Names,
-                                            std::vector<uint32_t>& Signatures) {
-    return new ForEachExpandPass(Names, Signatures);
-  }
+llvm::ModulePass *
+createRSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs){
+  return new RSForEachExpandPass(pForeachFuncs);
+}
 
-}  // namespace bcc
+} // end namespace bcc
diff --git a/lib/ExecutionEngine/RSScript.h b/lib/ExecutionEngine/RSScript.h
index 50a3eb3..4fc1f90 100644
--- a/lib/ExecutionEngine/RSScript.h
+++ b/lib/ExecutionEngine/RSScript.h
@@ -24,12 +24,12 @@
 #include <stddef.h>
 
 #include <llvm/ADT/SmallVector.h>
+#include <llvm/Support/CodeGen.h>
 
 #include <bcc/bcc.h>
 #include <bcc/bcc_mccache.h>
 #include "bcc_internal.h"
 
-#include "Compiler.h"
 #include "Script.h"
 
 namespace llvm {
diff --git a/lib/Transforms/BCCTransforms.h b/lib/ExecutionEngine/RSTransforms.h
similarity index 66%
rename from lib/Transforms/BCCTransforms.h
rename to lib/ExecutionEngine/RSTransforms.h
index 1ac8174..ec5f690 100644
--- a/lib/Transforms/BCCTransforms.h
+++ b/lib/ExecutionEngine/RSTransforms.h
@@ -14,11 +14,20 @@
  * limitations under the License.
  */
 
-#include "llvm/Pass.h"
+#ifndef BCC_EXECUTION_ENGINE_RS_TRANSFORMS_H
+#define BCC_EXECUTION_ENGINE_RS_TRANSFORMS_H
+
+#include "RSInfo.h"
+
+namespace llvm {
+  class ModulePass;
+}
 
 namespace bcc {
 
-llvm::ModulePass *createForEachExpandPass(std::vector<std::string>& Names,
-                                          std::vector<uint32_t>& Signatures);
+llvm::ModulePass *
+createRSForEachExpandPass(const RSInfo::ExportForeachFuncListTy &pForeachFuncs);
 
-}  // namespace bcc
+} // end namespace bcc
+
+#endif // BCC_EXECUTION_ENGINE_RS_TRANSFORMS_H
diff --git a/lib/Transforms/Android.mk b/lib/Transforms/Android.mk
deleted file mode 100644
index c30dd29..0000000
--- a/lib/Transforms/Android.mk
+++ /dev/null
@@ -1,70 +0,0 @@
-#
-# Copyright (C) 2012 The Android Open Source Project
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#      http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-#
-
-LOCAL_PATH := $(call my-dir)
-include $(LOCAL_PATH)/../../libbcc-config.mk
-
-#=====================================================================
-# Common: libbccTransforms
-#=====================================================================
-
-libbcc_transforms_SRC_FILES := \
-  ForEachExpand.cpp
-
-#=====================================================================
-# Device Static Library: libbccTransforms
-#=====================================================================
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libbccTransforms
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -DTARGET_BUILD
-
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-LOCAL_SRC_FILES := $(libbcc_transforms_SRC_FILES)
-
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-device-build.mk
-include $(BUILD_STATIC_LIBRARY)
-
-
-#=====================================================================
-# Host Static Library: libbccTransforms
-#=====================================================================
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := libbccTransforms
-LOCAL_MODULE_TAGS := optional
-LOCAL_MODULE_CLASS := STATIC_LIBRARIES
-LOCAL_IS_HOST_MODULE := true
-
-LOCAL_CFLAGS += $(libbcc_CFLAGS)
-LOCAL_CFLAGS += -D__HOST__
-LOCAL_C_INCLUDES := $(libbcc_C_INCLUDES)
-
-LOCAL_SRC_FILES := $(libbcc_transforms_SRC_FILES)
-
-include $(LIBBCC_ROOT_PATH)/libbcc-gen-config-from-mk.mk
-include $(LIBBCC_ROOT_PATH)/libbcc-build-rules.mk
-include $(LLVM_ROOT_PATH)/llvm-host-build.mk
-include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/libbcc-config.mk b/libbcc-config.mk
index 90e6fab..ac5372b 100644
--- a/libbcc-config.mk
+++ b/libbcc-config.mk
@@ -51,9 +51,7 @@
 #=====================================================================
 
 libbcc_CFLAGS := -Wall -Wno-unused-parameter -Werror
-ifneq ($(TARGET_BUILD_VARIANT),eng)
-libbcc_CFLAGS += -D__DISABLE_ASSERTS
-else
+ifeq ($(TARGET_BUILD_VARIANT),eng)
 libbcc_CFLAGS += -DANDROID_ENGINEERING_BUILD
 endif