Merge "Add fract(float) functions to the stubs white list."
diff --git a/bcinfo/MetadataExtractor.cpp b/bcinfo/MetadataExtractor.cpp
index add1ab1..96f3692 100644
--- a/bcinfo/MetadataExtractor.cpp
+++ b/bcinfo/MetadataExtractor.cpp
@@ -64,6 +64,13 @@
   return c;
 }
 
+const char *createStringFromOptionalValue(llvm::MDNode *n, unsigned opndNum) {
+  llvm::Metadata *opnd;
+  if (opndNum >= n->getNumOperands() || !(opnd = n->getOperand(opndNum)))
+    return nullptr;
+  return createStringFromValue(opnd);
+}
+
 // Collect metadata from NamedMDNodes that contain a list of names
 // (strings).
 //
@@ -146,10 +153,14 @@
 // (should be synced with slang_rs_metadata.h)
 static const llvm::StringRef ExportForEachMetadataName = "#rs_export_foreach";
 
-// Name of metadata node where exported reduce name information resides
+// Name of metadata node where exported simple reduce name information resides
 // (should be synced with slang_rs_metadata.h)
 static const llvm::StringRef ExportReduceMetadataName = "#rs_export_reduce";
 
+// Name of metadata node where exported general reduce information resides
+// (should be synced with slang_rs_metadata.h)
+static const llvm::StringRef ExportReduceNewMetadataName = "#rs_export_reduce_new";
+
 // Name of metadata node where RS object slot info resides (should be
 // synced with slang_rs_metadata.h)
 static const llvm::StringRef ObjectSlotMetadataName = "#rs_object_slots";
@@ -160,17 +171,23 @@
 // be synced with libbcc/lib/Core/Source.cpp)
 static const llvm::StringRef ChecksumMetadataName = "#rs_build_checksum";
 
+// Name of metadata node which contains a list of compile units that have debug
+// metadata. If this is null then there is no debug metadata in the compile
+// unit.
+static const llvm::StringRef DebugInfoMetadataName = "llvm.dbg.cu";
+
 MetadataExtractor::MetadataExtractor(const char *bitcode, size_t bitcodeSize)
     : mModule(nullptr), mBitcode(bitcode), mBitcodeSize(bitcodeSize),
       mExportVarCount(0), mExportFuncCount(0), mExportForEachSignatureCount(0),
-      mExportReduceCount(0), mExportVarNameList(nullptr),
+      mExportReduceCount(0), mExportReduceNewCount(0), mExportVarNameList(nullptr),
       mExportFuncNameList(nullptr), mExportForEachNameList(nullptr),
       mExportForEachSignatureList(nullptr),
       mExportForEachInputCountList(nullptr), mExportReduceNameList(nullptr),
+      mExportReduceNewList(nullptr),
       mPragmaCount(0), mPragmaKeyList(nullptr), mPragmaValueList(nullptr),
       mObjectSlotCount(0), mObjectSlotList(nullptr),
       mRSFloatPrecision(RS_FP_Full), mIsThreadable(true),
-      mBuildChecksum(nullptr) {
+      mBuildChecksum(nullptr), mHasDebugInfo(false) {
   BitcodeWrapper wrapper(bitcode, bitcodeSize);
   mTargetAPI = wrapper.getTargetAPI();
   mCompilerVersion = wrapper.getCompilerVersion();
@@ -180,10 +197,11 @@
 MetadataExtractor::MetadataExtractor(const llvm::Module *module)
     : mModule(module), mBitcode(nullptr), mBitcodeSize(0),
       mExportVarCount(0), mExportFuncCount(0), mExportForEachSignatureCount(0),
-      mExportReduceCount(0), mExportVarNameList(nullptr),
+      mExportReduceCount(0), mExportReduceNewCount(0), mExportVarNameList(nullptr),
       mExportFuncNameList(nullptr), mExportForEachNameList(nullptr),
       mExportForEachSignatureList(nullptr),
       mExportForEachInputCountList(nullptr), mExportReduceNameList(nullptr),
+      mExportReduceNewList(nullptr),
       mPragmaCount(0), mPragmaKeyList(nullptr), mPragmaValueList(nullptr),
       mObjectSlotCount(0), mObjectSlotList(nullptr),
       mRSFloatPrecision(RS_FP_Full), mIsThreadable(true),
@@ -224,6 +242,9 @@
   delete [] mExportForEachSignatureList;
   mExportForEachSignatureList = nullptr;
 
+  delete [] mExportForEachInputCountList;
+  mExportForEachInputCountList = nullptr;
+
   if (mExportReduceNameList) {
     for (size_t i = 0; i < mExportReduceCount; i++) {
       delete [] mExportReduceNameList[i];
@@ -233,6 +254,9 @@
   delete [] mExportReduceNameList;
   mExportReduceNameList = nullptr;
 
+  delete [] mExportReduceNewList;
+  mExportReduceNewList = nullptr;
+
   for (size_t i = 0; i < mPragmaCount; i++) {
     if (mPragmaKeyList) {
       delete [] mPragmaKeyList[i];
@@ -468,6 +492,61 @@
 }
 
 
+bool MetadataExtractor::populateReduceNewMetadata(const llvm::NamedMDNode *ReduceNewMetadata) {
+  mExportReduceNewCount = 0;
+  mExportReduceNewList = nullptr;
+
+  if (!ReduceNewMetadata || !(mExportReduceNewCount = ReduceNewMetadata->getNumOperands()))
+    return true;
+
+  ReduceNew *TmpReduceNewList = new ReduceNew[mExportReduceNewCount];
+
+  for (size_t i = 0; i < mExportReduceNewCount; i++) {
+    llvm::MDNode *Node = ReduceNewMetadata->getOperand(i);
+    if (!Node || Node->getNumOperands() < 3) {
+      ALOGE("Missing reduce metadata");
+      return false;
+    }
+
+    TmpReduceNewList[i].mReduceName = createStringFromValue(Node->getOperand(0));
+
+    if (!extractUIntFromMetadataString(&TmpReduceNewList[i].mAccumulatorDataSize,
+                                       Node->getOperand(1))) {
+      ALOGE("Non-integer accumulator data size value in reduce metadata");
+      return false;
+    }
+
+    llvm::MDNode *AccumulatorNode = llvm::dyn_cast<llvm::MDNode>(Node->getOperand(2));
+    if (!AccumulatorNode || AccumulatorNode->getNumOperands() != 2) {
+      ALOGE("Malformed accumulator node in reduce metadata");
+      return false;
+    }
+    TmpReduceNewList[i].mAccumulatorName = createStringFromValue(AccumulatorNode->getOperand(0));
+    if (!extractUIntFromMetadataString(&TmpReduceNewList[i].mSignature,
+                                       AccumulatorNode->getOperand(1))) {
+      ALOGE("Non-integer signature value in reduce metadata");
+      return false;
+    }
+    llvm::Function *Func =
+        mModule->getFunction(llvm::StringRef(TmpReduceNewList[i].mAccumulatorName));
+    if (!Func) {
+      ALOGE("reduce metadata names missing accumulator function");
+      return false;
+    }
+    // Why calculateNumInputs() - 1?  The "-1" is because we don't
+    // want to treat the accumulator argument as an input.
+    TmpReduceNewList[i].mInputCount = calculateNumInputs(Func, TmpReduceNewList[i].mSignature) - 1;
+
+    TmpReduceNewList[i].mInitializerName = createStringFromOptionalValue(Node, 3);
+    TmpReduceNewList[i].mCombinerName = createStringFromOptionalValue(Node, 4);
+    TmpReduceNewList[i].mOutConverterName = createStringFromOptionalValue(Node, 5);
+    TmpReduceNewList[i].mHalterName = createStringFromOptionalValue(Node, 6);
+  }
+
+  mExportReduceNewList = TmpReduceNewList;
+  return true;
+}
+
 void MetadataExtractor::readThreadableFlag(
     const llvm::NamedMDNode *ThreadableMetadata) {
 
@@ -544,6 +623,8 @@
       mModule->getNamedMetadata(ExportForEachMetadataName);
   const llvm::NamedMDNode *ExportReduceMetadata =
       mModule->getNamedMetadata(ExportReduceMetadataName);
+  const llvm::NamedMDNode *ExportReduceNewMetadata =
+      mModule->getNamedMetadata(ExportReduceNewMetadataName);
   const llvm::NamedMDNode *PragmaMetadata =
       mModule->getNamedMetadata(PragmaMetadataName);
   const llvm::NamedMDNode *ObjectSlotMetadata =
@@ -552,6 +633,8 @@
       mModule->getNamedMetadata(ThreadableMetadataName);
   const llvm::NamedMDNode *ChecksumMetadata =
       mModule->getNamedMetadata(ChecksumMetadataName);
+  const llvm::NamedMDNode *DebugInfoMetadata =
+      mModule->getNamedMetadata(DebugInfoMetadataName);
 
   if (!populateNameMetadata(ExportVarMetadata, mExportVarNameList,
                             mExportVarCount)) {
@@ -577,6 +660,11 @@
     return false;
   }
 
+  if (!populateReduceNewMetadata(ExportReduceNewMetadata)) {
+    ALOGE("Could not populate export general reduction metadata");
+    return false;
+  }
+
   populatePragmaMetadata(PragmaMetadata);
 
   if (!populateObjectSlotMetadata(ObjectSlotMetadata)) {
@@ -587,6 +675,8 @@
   readThreadableFlag(ThreadableMetadata);
   readBuildChecksumMetadata(ChecksumMetadata);
 
+  mHasDebugInfo = DebugInfoMetadata != nullptr;
+
   return true;
 }
 
diff --git a/bcinfo/tools/main.cpp b/bcinfo/tools/main.cpp
index c4e40f4..855199d 100644
--- a/bcinfo/tools/main.cpp
+++ b/bcinfo/tools/main.cpp
@@ -110,6 +110,11 @@
 }
 
 
+static void dumpReduceNewInfo(FILE *info, const char *Kind, const char *Name) {
+  if (Name)
+    fprintf(info, "  %s(%s)\n", Kind, Name);
+}
+
 static int dumpInfo(bcinfo::MetadataExtractor *ME) {
   if (!ME) {
     return 1;
@@ -149,6 +154,20 @@
     fprintf(info, "%s\n", reduceNameList[i]);
   }
 
+  fprintf(info, "exportReduceNewCount: %zu\n", ME->getExportReduceNewCount());
+  const bcinfo::MetadataExtractor::ReduceNew *reduceNewList =
+      ME->getExportReduceNewList();
+  for (size_t i = 0; i < ME->getExportReduceNewCount(); i++) {
+    const bcinfo::MetadataExtractor::ReduceNew &reduceNew = reduceNewList[i];
+    fprintf(info, "%u - %s - %u - %u\n", reduceNew.mSignature, reduceNew.mReduceName,
+            reduceNew.mInputCount, reduceNew.mAccumulatorDataSize);
+    dumpReduceNewInfo(info, "initializer",  reduceNew.mInitializerName);
+    dumpReduceNewInfo(info, "accumulator",  reduceNew.mAccumulatorName);
+    dumpReduceNewInfo(info, "combiner",     reduceNew.mCombinerName);
+    dumpReduceNewInfo(info, "outconverter", reduceNew.mOutConverterName);
+    dumpReduceNewInfo(info, "halter",       reduceNew.mHalterName);
+  }
+
   fprintf(info, "objectSlotCount: %zu\n", ME->getObjectSlotCount());
   const uint32_t *slotList = ME->getObjectSlotList();
   for (size_t i = 0; i < ME->getObjectSlotCount(); i++) {
@@ -210,6 +229,20 @@
   }
   printf("\n");
 
+  printf("exportReduceNewCount: %zu\n", ME->getExportReduceNewCount());
+  const bcinfo::MetadataExtractor::ReduceNew *reduceNewList = ME->getExportReduceNewList();
+  for (size_t i = 0; i < ME->getExportReduceNewCount(); i++) {
+    const bcinfo::MetadataExtractor::ReduceNew &reduceNew = reduceNewList[i];
+    printf("exportReduceNewList[%zu]: %s - 0x%08x - %u - %u\n", i, reduceNew.mReduceName,
+           reduceNew.mSignature, reduceNew.mInputCount, reduceNew.mAccumulatorDataSize);
+    dumpReduceNewInfo(stdout, "initializer",  reduceNew.mInitializerName);
+    dumpReduceNewInfo(stdout, "accumulator",  reduceNew.mAccumulatorName);
+    dumpReduceNewInfo(stdout, "combiner",     reduceNew.mCombinerName);
+    dumpReduceNewInfo(stdout, "outconverter", reduceNew.mOutConverterName);
+    dumpReduceNewInfo(stdout, "halter",       reduceNew.mHalterName);
+  }
+  printf("\n");
+
   printf("pragmaCount: %zu\n", ME->getPragmaCount());
   const char **keyList = ME->getPragmaKeyList();
   const char **valueList = ME->getPragmaValueList();
diff --git a/include/bcc/Compiler.h b/include/bcc/Compiler.h
index 8a30c38..a0925b8 100644
--- a/include/bcc/Compiler.h
+++ b/include/bcc/Compiler.h
@@ -82,6 +82,7 @@
 
   bool addInternalizeSymbolsPass(Script &pScript, llvm::legacy::PassManager &pPM);
   void addExpandKernelPass(llvm::legacy::PassManager &pPM);
+  void addDebugInfoPass(Script &pScript, llvm::legacy::PassManager &pPM);
   void addGlobalInfoPass(Script &pScript, llvm::legacy::PassManager &pPM);
   void addInvariantPass(llvm::legacy::PassManager &pPM);
   void addInvokeHelperPass(llvm::legacy::PassManager &pPM);
diff --git a/include/bcc/Renderscript/RSTransforms.h b/include/bcc/Renderscript/RSTransforms.h
index 6dcfedd..3c81860 100644
--- a/include/bcc/Renderscript/RSTransforms.h
+++ b/include/bcc/Renderscript/RSTransforms.h
@@ -43,6 +43,8 @@
 
 llvm::ModulePass * createRSX86_64CallConvPass();
 
+llvm::ModulePass * createRSAddDebugInfoPass();
+
 } // end namespace bcc
 
 #endif // BCC_RS_TRANSFORMS_H
diff --git a/include/bcc/Source.h b/include/bcc/Source.h
index 54263a2..e6bef2e 100644
--- a/include/bcc/Source.h
+++ b/include/bcc/Source.h
@@ -82,6 +82,10 @@
 
   void addBuildChecksumMetadata(const char *) const;
 
+  // Get whether debugging has been enabled for this module by checking
+  // for presence of debug info in the module.
+  bool getDebugInfoEnabled() const;
+
   ~Source();
 };
 
diff --git a/include/bcinfo/MetadataExtractor.h b/include/bcinfo/MetadataExtractor.h
index 742346a..fcaaee8 100644
--- a/include/bcinfo/MetadataExtractor.h
+++ b/include/bcinfo/MetadataExtractor.h
@@ -46,6 +46,40 @@
 };
 
 class MetadataExtractor {
+ public:
+  struct ReduceNew {
+    // These strings are owned by the ReduceNew instance, and deleted upon its destruction.
+    // They are assumed to have been allocated by "new []" and hence are deleted by "delete []".
+    const char *mReduceName;
+    const char *mInitializerName;
+    const char *mAccumulatorName;
+    const char *mCombinerName;
+    const char *mOutConverterName;
+    const char *mHalterName;
+
+    uint32_t mSignature;   // of accumulator function
+    uint32_t mInputCount;  // of accumulator function (and of kernel itself)
+    uint32_t mAccumulatorDataSize;  // in bytes
+
+    ReduceNew() :
+        mReduceName(nullptr),
+        mInitializerName(nullptr), mAccumulatorName(nullptr), mCombinerName(nullptr),
+        mOutConverterName(nullptr), mHalterName(nullptr),
+        mSignature(0), mInputCount(0), mAccumulatorDataSize(0) {
+    }
+    ~ReduceNew() {
+      delete [] mReduceName;
+      delete [] mInitializerName;
+      delete [] mAccumulatorName;
+      delete [] mCombinerName;
+      delete [] mOutConverterName;
+      delete [] mHalterName;
+    }
+
+    ReduceNew(const ReduceNew &) = delete;
+    void operator=(const ReduceNew &) = delete;
+  };
+
  private:
   const llvm::Module *mModule;
   const char *mBitcode;
@@ -55,12 +89,14 @@
   size_t mExportFuncCount;
   size_t mExportForEachSignatureCount;
   size_t mExportReduceCount;
+  size_t mExportReduceNewCount;
   const char **mExportVarNameList;
   const char **mExportFuncNameList;
   const char **mExportForEachNameList;
   const uint32_t *mExportForEachSignatureList;
   const uint32_t *mExportForEachInputCountList;
   const char **mExportReduceNameList;
+  const ReduceNew *mExportReduceNewList;
 
   size_t mPragmaCount;
   const char **mPragmaKeyList;
@@ -80,9 +116,12 @@
 
   const char *mBuildChecksum;
 
+  bool mHasDebugInfo;
+
   // Helper functions for extraction
   bool populateForEachMetadata(const llvm::NamedMDNode *Names,
                                const llvm::NamedMDNode *Signatures);
+  bool populateReduceNewMetadata(const llvm::NamedMDNode *ReduceNewMetadata);
   bool populateObjectSlotMetadata(const llvm::NamedMDNode *ObjectSlotMetadata);
   void populatePragmaMetadata(const llvm::NamedMDNode *PragmaMetadata);
   void readThreadableFlag(const llvm::NamedMDNode *ThreadableMetadata);
@@ -183,20 +222,34 @@
   }
 
   /**
-   * \return number of exported reduce kernels (slots) in this script/module.
+   * \return number of exported simple reduce kernels (slots) in this script/module.
    */
   size_t getExportReduceCount() const {
     return mExportReduceCount;
   }
 
   /**
-   * \return array of exported reduce kernel names.
+   * \return array of exported simple reduce kernel names.
    */
   const char **getExportReduceNameList() const {
     return mExportReduceNameList;
   }
 
   /**
+   * \return number of exported general reduce kernels (slots) in this script/module.
+   */
+  size_t getExportReduceNewCount() const {
+    return mExportReduceNewCount;
+  }
+
+  /**
+   * \return array of exported general reduce kernel descriptions.
+   */
+  const ReduceNew *getExportReduceNewList() const {
+    return mExportReduceNewList;
+  }
+
+  /**
    * \return number of pragmas contained in pragmaKeyList and pragmaValueList.
    */
   size_t getPragmaCount() const {
@@ -348,6 +401,13 @@
   const char *getBuildChecksum() const {
     return mBuildChecksum;
   }
+
+  /**
+   * \return whether the module contains debug metadata
+   */
+  bool hasDebugInfo() const {
+    return mHasDebugInfo;
+  }
 };
 
 }  // namespace bcinfo
diff --git a/lib/Core/Compiler.cpp b/lib/Core/Compiler.cpp
index 5c769b4..35f19d7 100644
--- a/lib/Core/Compiler.cpp
+++ b/lib/Core/Compiler.cpp
@@ -148,69 +148,78 @@
 enum Compiler::ErrorCode Compiler::runPasses(Script &pScript,
                                              llvm::raw_pwrite_stream &pResult) {
   // Pass manager for link-time optimization
-  llvm::legacy::PassManager passes;
+  llvm::legacy::PassManager transformPasses;
 
   // Empty MCContext.
   llvm::MCContext *mc_context = nullptr;
 
-  passes.add(createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis()));
+  transformPasses.add(
+      createTargetTransformInfoWrapperPass(mTarget->getTargetIRAnalysis()));
 
   // Add some initial custom passes.
-  addInvokeHelperPass(passes);
-  addExpandKernelPass(passes);
-  addInvariantPass(passes);
-  if (!addInternalizeSymbolsPass(pScript, passes))
+  addInvokeHelperPass(transformPasses);
+  addExpandKernelPass(transformPasses);
+  addDebugInfoPass(pScript, transformPasses);
+  addInvariantPass(transformPasses);
+  if (!addInternalizeSymbolsPass(pScript, transformPasses))
     return kErrCustomPasses;
-  addGlobalInfoPass(pScript, passes);
+  addGlobalInfoPass(pScript, transformPasses);
 
   if (mTarget->getOptLevel() == llvm::CodeGenOpt::None) {
-    passes.add(llvm::createGlobalOptimizerPass());
-    passes.add(llvm::createConstantMergePass());
+    transformPasses.add(llvm::createGlobalOptimizerPass());
+    transformPasses.add(llvm::createConstantMergePass());
 
   } else {
     // FIXME: Figure out which passes should be executed.
     llvm::PassManagerBuilder Builder;
     Builder.Inliner = llvm::createFunctionInliningPass();
-    Builder.populateLTOPassManager(passes);
+    Builder.populateLTOPassManager(transformPasses);
 
     /* FIXME: Reenable autovectorization after rebase.
        bug 19324423
     // Add vectorization passes after LTO passes are in
     // additional flag: -unroll-runtime
-    passes.add(llvm::createLoopUnrollPass(-1, 16, 0, 1));
+    transformPasses.add(llvm::createLoopUnrollPass(-1, 16, 0, 1));
     // Need to pass appropriate flags here: -scalarize-load-store
-    passes.add(llvm::createScalarizerPass());
-    passes.add(llvm::createCFGSimplificationPass());
-    passes.add(llvm::createScopedNoAliasAAPass());
-    passes.add(llvm::createScalarEvolutionAliasAnalysisPass());
+    transformPasses.add(llvm::createScalarizerPass());
+    transformPasses.add(llvm::createCFGSimplificationPass());
+    transformPasses.add(llvm::createScopedNoAliasAAPass());
+    transformPasses.add(llvm::createScalarEvolutionAliasAnalysisPass());
     // additional flags: -slp-vectorize-hor -slp-vectorize-hor-store (unnecessary?)
-    passes.add(llvm::createSLPVectorizerPass());
-    passes.add(llvm::createDeadCodeEliminationPass());
-    passes.add(llvm::createInstructionCombiningPass());
+    transformPasses.add(llvm::createSLPVectorizerPass());
+    transformPasses.add(llvm::createDeadCodeEliminationPass());
+    transformPasses.add(llvm::createInstructionCombiningPass());
     */
   }
 
   // These passes have to come after LTO, since we don't want to examine
   // functions that are never actually called.
   if (llvm::Triple(getTargetMachine().getTargetTriple()).getArch() == llvm::Triple::x86_64)
-    passes.add(createRSX86_64CallConvPass());  // Add pass to correct calling convention for X86-64.
-  passes.add(createRSIsThreadablePass());      // Add pass to mark script as threadable.
+    transformPasses.add(createRSX86_64CallConvPass());  // Add pass to correct calling convention for X86-64.
+  transformPasses.add(createRSIsThreadablePass());      // Add pass to mark script as threadable.
 
   // RSEmbedInfoPass needs to come after we have scanned for non-threadable
   // functions.
   // Script passed to RSCompiler must be a RSScript.
   RSScript &script = static_cast<RSScript &>(pScript);
   if (script.getEmbedInfo())
-    passes.add(createRSEmbedInfoPass());
+    transformPasses.add(createRSEmbedInfoPass());
+
+  // Execute the passes.
+  transformPasses.run(pScript.getSource().getModule());
+
+  // Run backend separately to avoid interference between debug metadata
+  // generation and backend initialization.
+  llvm::legacy::PassManager codeGenPasses;
 
   // Add passes to the pass manager to emit machine code through MC layer.
-  if (mTarget->addPassesToEmitMC(passes, mc_context, pResult,
+  if (mTarget->addPassesToEmitMC(codeGenPasses, mc_context, pResult,
                                  /* DisableVerify */false)) {
     return kPrepareCodeGenPass;
   }
 
   // Execute the passes.
-  passes.run(pScript.getSource().getModule());
+  codeGenPasses.run(pScript.getSource().getModule());
 
   return kSuccess;
 }
@@ -330,10 +339,12 @@
   size_t exportFuncCount = me.getExportFuncCount();
   size_t exportForEachCount = me.getExportForEachSignatureCount();
   size_t exportReduceCount = me.getExportReduceCount();
+  size_t exportReduceNewCount = me.getExportReduceNewCount();
   const char **exportVarNameList = me.getExportVarNameList();
   const char **exportFuncNameList = me.getExportFuncNameList();
   const char **exportForEachNameList = me.getExportForEachNameList();
   const char **exportReduceNameList = me.getExportReduceNameList();
+  const bcinfo::MetadataExtractor::ReduceNew *exportReduceNewList = me.getExportReduceNewList();
   size_t i;
 
   for (i = 0; i < exportVarCount; ++i) {
@@ -349,7 +360,7 @@
   // functions around until createInternalizePass() is finished making
   // its own copy of the visible symbols.
   std::vector<std::string> expanded_funcs;
-  expanded_funcs.reserve(exportForEachCount + exportReduceCount);
+  expanded_funcs.reserve(exportForEachCount + exportReduceCount + exportReduceNewCount);
 
   for (i = 0; i < exportForEachCount; ++i) {
     expanded_funcs.push_back(std::string(exportForEachNameList[i]) + ".expand");
@@ -357,6 +368,9 @@
   for (i = 0; i < exportReduceCount; ++i) {
     expanded_funcs.push_back(std::string(exportReduceNameList[i]) + ".expand");
   }
+  for (i = 0; i < exportReduceNewCount; ++i) {
+    expanded_funcs.push_back(std::string(exportReduceNewList[i].mAccumulatorName) + ".expand");
+  }
 
   for (auto &symbol_name : expanded_funcs) {
     export_symbols.push_back(symbol_name.c_str());
@@ -374,6 +388,11 @@
   }
 }
 
+void Compiler::addDebugInfoPass(Script &pScript, llvm::legacy::PassManager &pPM) {
+  if (pScript.getSource().getDebugInfoEnabled())
+    pPM.add(createRSAddDebugInfoPass());
+}
+
 void Compiler::addExpandKernelPass(llvm::legacy::PassManager &pPM) {
   // Expand ForEach and reduce on CPU path to reduce launch overhead.
   bool pEnableStepOpt = true;
diff --git a/lib/Core/Source.cpp b/lib/Core/Source.cpp
index 94da98e..25b263f 100644
--- a/lib/Core/Source.cpp
+++ b/lib/Core/Source.cpp
@@ -181,4 +181,8 @@
     node->addOperand(llvm::MDNode::get(context, val));
 }
 
+bool Source::getDebugInfoEnabled() const {
+  return mModule->getNamedMetadata("llvm.dbg.cu") != nullptr;
+}
+
 } // namespace bcc
diff --git a/lib/Renderscript/Android.mk b/lib/Renderscript/Android.mk
index 7744447..32e65b3 100644
--- a/lib/Renderscript/Android.mk
+++ b/lib/Renderscript/Android.mk
@@ -22,6 +22,7 @@
 #=====================================================================
 
 libbcc_renderscript_SRC_FILES := \
+  RSAddDebugInfoPass.cpp \
   RSCompilerDriver.cpp \
   RSEmbedInfo.cpp \
   RSKernelExpand.cpp \
diff --git a/lib/Renderscript/RSAddDebugInfoPass.cpp b/lib/Renderscript/RSAddDebugInfoPass.cpp
new file mode 100644
index 0000000..03ad24a
--- /dev/null
+++ b/lib/Renderscript/RSAddDebugInfoPass.cpp
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2015, 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.
+ */
+
+#include "bcc/Assert.h"
+#include "bcc/Renderscript/RSTransforms.h"
+
+#include <llvm/Pass.h>
+#include <llvm/IR/DataLayout.h>
+#include <llvm/IR/DIBuilder.h>
+#include <llvm/IR/Function.h>
+#include <llvm/IR/Function.h>
+#include <llvm/IR/InstIterator.h>
+#include <llvm/IR/Instructions.h>
+#include <llvm/IR/MDBuilder.h>
+#include <llvm/IR/Module.h>
+#include <llvm/IR/Type.h>
+#include <llvm/Transforms/Utils/BasicBlockUtils.h>
+
+namespace {
+
+const char DEBUG_SOURCE_PATH[] = "/opt/renderscriptdebugger/1";
+const char DEBUG_GENERATED_FILE[] = "generated.rs";
+
+/*
+ * LLVM pass to attach debug information to the bits of code
+ * generated by the compiler.
+ */
+class RSAddDebugInfoPass : public llvm::ModulePass {
+
+public:
+  // Pass ID
+  static char ID;
+
+  RSAddDebugInfoPass() : ModulePass(ID) {
+  }
+
+  virtual bool runOnModule(llvm::Module &Module) {
+    // Set up the debug info builder.
+    llvm::DIBuilder DebugInfo(Module);
+    DebugInfo.createCompileUnit(llvm::dwarf::DW_LANG_C99,
+                                DEBUG_GENERATED_FILE, DEBUG_SOURCE_PATH,
+                                "RS", false, "", 0);
+
+    // Attach DI metadata to each generated function.
+    for (llvm::Function &Func : Module)
+      if (Func.getName().endswith(".expand"))
+        attachDebugInfo(DebugInfo, Func);
+
+    DebugInfo.finalize();
+
+    return true;
+  }
+
+private:
+
+  /// @brief Add debug information to a generated function.
+  ///
+  /// This function is used to attach source file and line information
+  /// to the generated function code.
+  void attachDebugInfo(llvm::DIBuilder &DebugInfo, llvm::Function &Func) {
+    llvm::DIFile *sourceFileName =
+      DebugInfo.createFile(DEBUG_GENERATED_FILE, DEBUG_SOURCE_PATH);
+
+    // Fabricated function type
+    llvm::MDTuple *nullMD = llvm::MDTuple::get(Func.getParent()->getContext(),
+                        { DebugInfo.createUnspecifiedType("void") });
+
+    // Create function-level debug metadata.
+    llvm::DIScope *GeneratedScope = DebugInfo.createFunction(
+        sourceFileName, // scope
+        Func.getName(), Func.getName(),
+        sourceFileName, 1,
+        DebugInfo.createSubroutineType(sourceFileName, nullMD),
+        false, true, 1, 0, false, &Func
+    );
+
+    // Attach location inforamtion to each instruction in the function
+    for (llvm::inst_iterator Inst    = llvm::inst_begin(Func),
+                             InstEnd = llvm::inst_end(Func);
+                             Inst != InstEnd;
+                             ++Inst) {
+      Inst->setDebugLoc(llvm::DebugLoc::get(1, 1, GeneratedScope));
+    }
+  }
+
+}; // end class RSAddDebugInfoPass
+
+char RSAddDebugInfoPass::ID = 0;
+static llvm::RegisterPass<RSAddDebugInfoPass> X("addrsdi", "Add RS DebugInfo Pass");
+
+} // end anonymous namespace
+
+namespace bcc {
+
+llvm::ModulePass * createRSAddDebugInfoPass() {
+  return new RSAddDebugInfoPass();
+}
+
+} // end namespace bcc
diff --git a/lib/Renderscript/RSEmbedInfo.cpp b/lib/Renderscript/RSEmbedInfo.cpp
index b0c2767..54e0acb 100644
--- a/lib/Renderscript/RSEmbedInfo.cpp
+++ b/lib/Renderscript/RSEmbedInfo.cpp
@@ -74,6 +74,7 @@
     size_t exportFuncCount = me.getExportFuncCount();
     size_t exportForEachCount = me.getExportForEachSignatureCount();
     size_t exportReduceCount = me.getExportReduceCount();
+    size_t exportReduceNewCount = me.getExportReduceNewCount();
     size_t objectSlotCount = me.getObjectSlotCount();
     size_t pragmaCount = me.getPragmaCount();
     const char **exportVarNameList = me.getExportVarNameList();
@@ -82,6 +83,8 @@
     const char **exportReduceNameList = me.getExportReduceNameList();
     const uint32_t *exportForEachSignatureList =
         me.getExportForEachSignatureList();
+    const bcinfo::MetadataExtractor::ReduceNew *exportReduceNewList =
+        me.getExportReduceNewList();
     const uint32_t *objectSlotList = me.getObjectSlotList();
     const char **pragmaKeyList = me.getPragmaKeyList();
     const char **pragmaValueList = me.getPragmaValueList();
@@ -90,13 +93,22 @@
 
     size_t i;
 
-    // We use a simple text format here that the compatibility library can
-    // easily parse. Each section starts out with its name followed by a count.
-    // The count denotes the number of lines to parse for that particular
-    // category. Variables and Functions merely put the appropriate identifier
-    // on the line, while ForEach kernels have the encoded int signature,
-    // followed by a hyphen followed by the identifier (function to look up).
-    // Object Slots are just listed as one integer per line.
+    // We use a simple text format here that the compatibility library
+    // can easily parse. Each section starts out with its name
+    // followed by a count.  The count denotes the number of lines to
+    // parse for that particular category. Variables and Functions and
+    // simple reduce kernels merely put the appropriate identifier on
+    // the line. ForEach kernels have the encoded int signature,
+    // followed by a hyphen followed by the identifier (function to
+    // look up). General reduce kernels have the encoded int
+    // signature, followed by a hyphen followed by the accumulator
+    // data size, followed by a hyphen followed by the identifier
+    // (reduction name); and then for each possible constituent
+    // function, a hyphen followed by the identifier (function name)
+    // -- in the case where the function is omitted, "." is used in
+    // place of the identifier.  Object Slots are just listed as one
+    // integer per line.
+
     s << "exportVarCount: " << exportVarCount << "\n";
     for (i = 0; i < exportVarCount; ++i) {
       s << exportVarNameList[i] << "\n";
@@ -118,6 +130,21 @@
       s << exportReduceNameList[i] << "\n";
     }
 
+    s << "exportReduceNewCount: " << exportReduceNewCount << "\n";
+    auto reduceNewFnName = [](const char *Name) { return Name ? Name : "."; };
+    for (i = 0; i < exportReduceNewCount; ++i) {
+      const bcinfo::MetadataExtractor::ReduceNew &reduceNew = exportReduceNewList[i];
+      s << reduceNew.mSignature << " - "
+        << reduceNew.mAccumulatorDataSize << " - "
+        << reduceNew.mReduceName << " - "
+        << reduceNewFnName(reduceNew.mInitializerName) << " - "
+        << reduceNewFnName(reduceNew.mAccumulatorName) << " - "
+        << reduceNewFnName(reduceNew.mCombinerName) << " - "
+        << reduceNewFnName(reduceNew.mOutConverterName) << " - "
+        << reduceNewFnName(reduceNew.mHalterName)
+        << "\n";
+    }
+
     s << "objectSlotCount: " << objectSlotCount << "\n";
     for (i = 0; i < objectSlotCount; ++i) {
       s << objectSlotList[i] << "\n";
diff --git a/lib/Renderscript/RSGlobalInfoPass.cpp b/lib/Renderscript/RSGlobalInfoPass.cpp
index 0600692..68d082a 100644
--- a/lib/Renderscript/RSGlobalInfoPass.cpp
+++ b/lib/Renderscript/RSGlobalInfoPass.cpp
@@ -138,6 +138,11 @@
         continue;
       }
 
+      // Skip intrinsic variables.
+      if (GV.getName().startswith("llvm.")) {
+        continue;
+      }
+
       // In LLVM, an instance of GlobalVariable is actually a Value
       // corresponding to the address of it.
       GVAddresses.push_back(llvm::ConstantExpr::getBitCast(&GV, VoidPtrTy));
diff --git a/lib/Renderscript/RSKernelExpand.cpp b/lib/Renderscript/RSKernelExpand.cpp
index 34611d7..d06cb5b 100644
--- a/lib/Renderscript/RSKernelExpand.cpp
+++ b/lib/Renderscript/RSKernelExpand.cpp
@@ -19,6 +19,7 @@
 
 #include <cstdlib>
 #include <functional>
+#include <unordered_set>
 
 #include <llvm/IR/DerivedTypes.h>
 #include <llvm/IR/Function.h>
@@ -42,6 +43,7 @@
 // Only used in bccAssert()
 const int kNumExpandedForeachParams = 4;
 const int kNumExpandedReduceParams = 3;
+const int kNumExpandedReduceNewAccumulatorParams = 4;
 #endif
 
 const char kRenderScriptTBAARootName[] = "RenderScript Distinct TBAA";
@@ -70,6 +72,8 @@
 private:
   static const size_t RS_KERNEL_INPUT_LIMIT = 8; // see frameworks/base/libs/rs/cpu_ref/rsCpuCoreRuntime.h
 
+  typedef std::unordered_set<llvm::Function *> FunctionSet;
+
   enum RsLaunchDimensionsField {
     RsLaunchDimensionsFieldX,
     RsLaunchDimensionsFieldY,
@@ -105,6 +109,7 @@
    * the pass is run on.
    */
   llvm::FunctionType *ExpandedForEachType, *ExpandedReduceType;
+  llvm::Type *RsExpandKernelDriverInfoPfxTy;
 
   uint32_t mExportForEachCount;
   const char **mExportForEachNameList;
@@ -294,7 +299,7 @@
     RsExpandKernelDriverInfoPfxTypes.push_back(RsLaunchDimensionsTy);     // RsLaunchDimensions current
     RsExpandKernelDriverInfoPfxTypes.push_back(VoidPtrTy);                // const void *usr
     RsExpandKernelDriverInfoPfxTypes.push_back(Int32Ty);                  // uint32_t usrLen
-    llvm::StructType *RsExpandKernelDriverInfoPfxTy =
+    RsExpandKernelDriverInfoPfxTy =
         llvm::StructType::create(RsExpandKernelDriverInfoPfxTypes, "RsExpandKernelDriverInfoPfx");
 
     // Create the function type for expanded kernels.
@@ -369,6 +374,55 @@
     return ExpandedFunction;
   }
 
+  // Create skeleton of a general reduce kernel's expanded accumulator.
+  //
+  // This creates a function with the following signature:
+  //
+  //  void @func.expand(%RsExpandKernelDriverInfoPfx* nocapture %p,
+  //                    i32 %x1, i32 %x2, accumType* nocapture %accum)
+  //
+  llvm::Function *createEmptyExpandedReduceNewAccumulator(llvm::StringRef OldName,
+                                                          llvm::Type *AccumArgTy) {
+    llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*Context);
+    llvm::Type *VoidTy = llvm::Type::getVoidTy(*Context);
+    llvm::FunctionType *ExpandedReduceNewAccumulatorType =
+        llvm::FunctionType::get(VoidTy,
+                                {RsExpandKernelDriverInfoPfxTy->getPointerTo(),
+                                 Int32Ty, Int32Ty, AccumArgTy}, false);
+    llvm::Function *FnExpandedAccumulator =
+      llvm::Function::Create(ExpandedReduceNewAccumulatorType,
+                             llvm::GlobalValue::ExternalLinkage,
+                             OldName + ".expand", Module);
+    bccAssert(FnExpandedAccumulator->arg_size() == kNumExpandedReduceNewAccumulatorParams);
+
+    llvm::Function::arg_iterator AI = FnExpandedAccumulator->arg_begin();
+
+    using llvm::Attribute;
+
+    llvm::Argument *Arg_p = &(*AI++);
+    Arg_p->setName("p");
+    Arg_p->addAttr(llvm::AttributeSet::get(*Context, Arg_p->getArgNo() + 1,
+                                           llvm::makeArrayRef(Attribute::NoCapture)));
+
+    llvm::Argument *Arg_x1 = &(*AI++);
+    Arg_x1->setName("x1");
+
+    llvm::Argument *Arg_x2 = &(*AI++);
+    Arg_x2->setName("x2");
+
+    llvm::Argument *Arg_accum = &(*AI++);
+    Arg_accum->setName("accum");
+    Arg_accum->addAttr(llvm::AttributeSet::get(*Context, Arg_accum->getArgNo() + 1,
+                                               llvm::makeArrayRef(Attribute::NoCapture)));
+
+    llvm::BasicBlock *Begin = llvm::BasicBlock::Create(*Context, "Begin",
+                                                       FnExpandedAccumulator);
+    llvm::IRBuilder<> Builder(Begin);
+    Builder.CreateRetVoid();
+
+    return FnExpandedAccumulator;
+  }
+
   /// @brief Create an empty loop
   ///
   /// Create a loop of the form:
@@ -504,12 +558,12 @@
   }
 
   // Build contribution to outgoing argument list for calling a
-  // ForEach-able function, based on the special parameters of that
-  // function.
+  // ForEach-able function or a general reduction accumulator
+  // function, based on the special parameters of that function.
   //
-  // Signature - metadata bits for the signature of the ForEach-able function
+  // Signature - metadata bits for the signature of the callee
   // X, Arg_p - values derived directly from expanded function,
-  //            suitable for computing arguments for the ForEach-able function
+  //            suitable for computing arguments for the callee
   // CalleeArgs - contribution is accumulated here
   // Bump - invoked once for each contributed outgoing argument
   // LoopHeaderInsertionPoint - an Instruction in the loop header, before which
@@ -571,6 +625,126 @@
     return Return;
   }
 
+  // Generate loop-invariant input processing setup code for an expanded
+  // ForEach-able function or an expanded general reduction accumulator
+  // function.
+  //
+  // LoopHeader - block at the end of which the setup code will be inserted
+  // Arg_p - RSKernelDriverInfo pointer passed to the expanded function
+  // TBAAPointer - metadata for marking loads of pointer values out of RSKernelDriverInfo
+  // ArgIter - iterator pointing to first input of the UNexpanded function
+  // NumInputs - number of inputs (NOT number of ARGUMENTS)
+  //
+  // InBufPtrs[] - this function sets each array element to point to the first
+  //               cell of the corresponding input allocation
+  // InStructTempSlots[] - this function sets each array element either to nullptr
+  //                       or to the result of an alloca (for the case where the
+  //                       calling convention dictates that a value must be passed
+  //                       by reference, and so we need a stacked temporary to hold
+  //                       a copy of that value)
+  void ExpandInputsLoopInvariant(llvm::IRBuilder<> &Builder, llvm::BasicBlock *LoopHeader,
+                                 llvm::Value *Arg_p,
+                                 llvm::MDNode *TBAAPointer,
+                                 llvm::Function::arg_iterator ArgIter,
+                                 const size_t NumInputs,
+                                 llvm::SmallVectorImpl<llvm::Value *> &InBufPtrs,
+                                 llvm::SmallVectorImpl<llvm::Value *> &InStructTempSlots) {
+    bccAssert(NumInputs <= RS_KERNEL_INPUT_LIMIT);
+
+    // Extract information about input slots. The work done
+    // here is loop-invariant, so we can hoist the operations out of the loop.
+    auto OldInsertionPoint = Builder.saveIP();
+    Builder.SetInsertPoint(LoopHeader->getTerminator());
+
+    for (size_t InputIndex = 0; InputIndex < NumInputs; ++InputIndex, ArgIter++) {
+      llvm::Type *InType = ArgIter->getType();
+
+      /*
+       * AArch64 calling conventions dictate that structs of sufficient size
+       * get passed by pointer instead of passed by value.  This, combined
+       * with the fact that we don't allow kernels to operate on pointer
+       * data means that if we see a kernel with a pointer parameter we know
+       * that it is a struct input that has been promoted.  As such we don't
+       * need to convert its type to a pointer.  Later we will need to know
+       * to create a temporary copy on the stack, so we save this information
+       * in InStructTempSlots.
+       */
+      if (auto PtrType = llvm::dyn_cast<llvm::PointerType>(InType)) {
+        llvm::Type *ElementType = PtrType->getElementType();
+        InStructTempSlots.push_back(Builder.CreateAlloca(ElementType, nullptr,
+                                                         "input_struct_slot"));
+      } else {
+        InType = InType->getPointerTo();
+        InStructTempSlots.push_back(nullptr);
+      }
+
+      SmallGEPIndices InBufPtrGEP(GEPHelper({0, RsExpandKernelDriverInfoPfxFieldInPtr,
+                                             static_cast<int32_t>(InputIndex)}));
+      llvm::Value    *InBufPtrAddr = Builder.CreateInBoundsGEP(Arg_p, InBufPtrGEP, "input_buf.gep");
+      llvm::LoadInst *InBufPtr = Builder.CreateLoad(InBufPtrAddr, "input_buf");
+      llvm::Value    *CastInBufPtr = Builder.CreatePointerCast(InBufPtr, InType, "casted_in");
+
+      if (gEnableRsTbaa) {
+        InBufPtr->setMetadata("tbaa", TBAAPointer);
+      }
+
+      InBufPtrs.push_back(CastInBufPtr);
+    }
+
+    Builder.restoreIP(OldInsertionPoint);
+  }
+
+  // Generate loop-varying input processing code for an expanded ForEach-able function
+  // or an expanded general reduction accumulator function.  Also, for the call to the
+  // UNexpanded function, collect the portion of the argument list corresponding to the
+  // inputs.
+  //
+  // Arg_x1 - first X coordinate to be processed by the expanded function
+  // TBAAAllocation - metadata for marking loads of input values out of allocations
+  // NumInputs -- number of inputs (NOT number of ARGUMENTS)
+  // InBufPtrs[] - this function consumes the information produced by ExpandInputsLoopInvariant()
+  // InStructTempSlots[] - this function consumes the information produced by ExpandInputsLoopInvariant()
+  // IndVar - value of loop induction variable (X coordinate) for a given loop iteration
+  //
+  // RootArgs - this function sets this to the list of outgoing argument values corresponding
+  //            to the inputs
+  void ExpandInputsBody(llvm::IRBuilder<> &Builder,
+                        llvm::Value *Arg_x1,
+                        llvm::MDNode *TBAAAllocation,
+                        const size_t NumInputs,
+                        const llvm::SmallVectorImpl<llvm::Value *> &InBufPtrs,
+                        const llvm::SmallVectorImpl<llvm::Value *> &InStructTempSlots,
+                        llvm::Value *IndVar,
+                        llvm::SmallVectorImpl<llvm::Value *> &RootArgs) {
+    llvm::Value *Offset = Builder.CreateSub(IndVar, Arg_x1);
+
+    for (size_t Index = 0; Index < NumInputs; ++Index) {
+      llvm::Value *InPtr = Builder.CreateInBoundsGEP(InBufPtrs[Index], Offset);
+      llvm::Value *Input;
+
+      llvm::LoadInst *InputLoad = Builder.CreateLoad(InPtr, "input");
+
+      if (gEnableRsTbaa) {
+        InputLoad->setMetadata("tbaa", TBAAAllocation);
+      }
+
+      if (llvm::Value *TemporarySlot = InStructTempSlots[Index]) {
+        // Pass a pointer to a temporary on the stack, rather than
+        // passing a pointer to the original value. We do not want
+        // the kernel to potentially modify the input data.
+
+        // Note: don't annotate with TBAA, since the kernel might
+        // have its own TBAA annotations for the pointer argument.
+        Builder.CreateStore(InputLoad, TemporarySlot);
+        Input = TemporarySlot;
+      } else {
+        Input = InputLoad;
+      }
+
+      RootArgs.push_back(Input);
+    }
+  }
+
   /* Performs the actual optimization on a selected function. On success, the
    * Module will contain a new function of the name "<NAME>.expand" that
    * invokes <NAME>() in a loop with the appropriate parameters.
@@ -595,7 +769,7 @@
 
     /*
      * Extract the expanded function's parameters.  It is guaranteed by
-     * createEmptyExpandedFunction that there will be four parameters.
+     * createEmptyExpandedForEachKernel that there will be four parameters.
      */
 
     bccAssert(ExpandedFunction->arg_size() == kNumExpandedForeachParams);
@@ -725,7 +899,7 @@
 
     /*
      * Extract the expanded function's parameters.  It is guaranteed by
-     * createEmptyExpandedFunction that there will be four parameters.
+     * createEmptyExpandedForEachKernel that there will be four parameters.
      */
 
     bccAssert(ExpandedFunction->arg_size() == kNumExpandedForeachParams);
@@ -802,7 +976,6 @@
       CastedOutBasePtr = Builder.CreatePointerCast(OutBasePtr, OutTy, "casted_out");
     }
 
-    llvm::SmallVector<llvm::Type*,  8> InTypes;
     llvm::SmallVector<llvm::Value*, 8> InBufPtrs;
     llvm::SmallVector<llvm::Value*, 8> InStructTempSlots;
 
@@ -826,47 +999,8 @@
     const size_t NumInPtrArguments = NumRemainingInputs;
 
     if (NumInPtrArguments > 0) {
-      // Extract information about input slots and step sizes. The work done
-      // here is loop-invariant, so we can hoist the operations out of the loop.
-      auto OldInsertionPoint = Builder.saveIP();
-      Builder.SetInsertPoint(LoopHeader->getTerminator());
-
-      for (size_t InputIndex = 0; InputIndex < NumInPtrArguments; ++InputIndex, ArgIter++) {
-        llvm::Type *InType = ArgIter->getType();
-
-        /*
-         * AArch64 calling conventions dictate that structs of sufficient size
-         * get passed by pointer instead of passed by value.  This, combined
-         * with the fact that we don't allow kernels to operate on pointer
-         * data means that if we see a kernel with a pointer parameter we know
-         * that it is a struct input that has been promoted.  As such we don't
-         * need to convert its type to a pointer.  Later we will need to know
-         * to create a temporary copy on the stack, so we save this information
-         * in InStructTempSlots.
-         */
-        if (auto PtrType = llvm::dyn_cast<llvm::PointerType>(InType)) {
-          llvm::Type *ElementType = PtrType->getElementType();
-          InStructTempSlots.push_back(Builder.CreateAlloca(ElementType, nullptr,
-                                                           "input_struct_slot"));
-        } else {
-          InType = InType->getPointerTo();
-          InStructTempSlots.push_back(nullptr);
-        }
-
-        SmallGEPIndices InBufPtrGEP(GEPHelper({0, RsExpandKernelDriverInfoPfxFieldInPtr,
-          static_cast<int32_t>(InputIndex)}));
-        llvm::Value    *InBufPtrAddr = Builder.CreateInBoundsGEP(Arg_p, InBufPtrGEP, "input_buf.gep");
-        llvm::LoadInst *InBufPtr = Builder.CreateLoad(InBufPtrAddr, "input_buf");
-        llvm::Value    *CastInBufPtr = Builder.CreatePointerCast(InBufPtr, InType, "casted_in");
-        if (gEnableRsTbaa) {
-          InBufPtr->setMetadata("tbaa", TBAAPointer);
-        }
-
-        InTypes.push_back(InType);
-        InBufPtrs.push_back(CastInBufPtr);
-      }
-
-      Builder.restoreIP(OldInsertionPoint);
+      ExpandInputsLoopInvariant(Builder, LoopHeader, Arg_p, TBAAPointer, ArgIter, NumInPtrArguments,
+                                InBufPtrs, InStructTempSlots);
     }
 
     // Populate the actual call to kernel().
@@ -889,33 +1023,8 @@
     // Inputs
 
     if (NumInPtrArguments > 0) {
-      llvm::Value *Offset = Builder.CreateSub(IV, Arg_x1);
-
-      for (size_t Index = 0; Index < NumInPtrArguments; ++Index) {
-        llvm::Value *InPtr = Builder.CreateInBoundsGEP(InBufPtrs[Index], Offset);
-        llvm::Value *Input;
-
-        llvm::LoadInst *InputLoad = Builder.CreateLoad(InPtr, "input");
-
-        if (gEnableRsTbaa) {
-          InputLoad->setMetadata("tbaa", TBAAAllocation);
-        }
-
-        if (llvm::Value *TemporarySlot = InStructTempSlots[Index]) {
-          // Pass a pointer to a temporary on the stack, rather than
-          // passing a pointer to the original value. We do not want
-          // the kernel to potentially modify the input data.
-
-          // Note: don't annotate with TBAA, since the kernel might
-          // have its own TBAA annotations for the pointer argument.
-          Builder.CreateStore(InputLoad, TemporarySlot);
-          Input = TemporarySlot;
-        } else {
-          Input = InputLoad;
-        }
-
-        RootArgs.push_back(Input);
-      }
+      ExpandInputsBody(Builder, Arg_x1, TBAAAllocation, NumInPtrArguments,
+                       InBufPtrs, InStructTempSlots, IV, RootArgs);
     }
 
     finishArgList(RootArgs, CalleeArgs, CalleeArgsContextIdx, *Function, Builder);
@@ -933,7 +1042,7 @@
     return true;
   }
 
-  // Expand a reduce-style kernel function.
+  // Expand a simple reduce-style kernel function.
   //
   // The input is a kernel which represents a binary operation,
   // of the form
@@ -999,7 +1108,7 @@
   bool ExpandReduce(llvm::Function *Function) {
     bccAssert(Function);
 
-    ALOGV("Expanding reduce kernel %s", Function->getName().str().c_str());
+    ALOGV("Expanding simple reduce kernel %s", Function->getName().str().c_str());
 
     llvm::DataLayout DL(Module);
 
@@ -1020,7 +1129,7 @@
       createEmptyExpandedReduceKernel(Function->getName());
 
     // Extract the expanded kernel's parameters.  It is guaranteed by
-    // createEmptyExpandedFunction that there will be 3 parameters.
+    // createEmptyExpandedReduceKernel that there will be 3 parameters.
     auto ExpandedFunctionArgIter = ExpandedFunction->arg_begin();
 
     llvm::Value *Arg_inBuf  = &*(ExpandedFunctionArgIter++);
@@ -1196,6 +1305,118 @@
     return true;
   }
 
+  // Certain categories of functions that make up a general
+  // reduce-style kernel are called directly from the driver with no
+  // expansion needed.  For a function in such a category, we need to
+  // promote linkage from static to external, to ensure that the
+  // function is visible to the driver in the dynamic symbol table.
+  // This promotion is safe because we don't have any kind of cross
+  // translation unit linkage model (except for linking against
+  // RenderScript libraries), so we do not risk name clashes.
+  bool PromoteReduceNewFunction(const char *Name, FunctionSet &PromotedFunctions) {
+    if (!Name)  // a presumably-optional function that is not present
+      return false;
+
+    llvm::Function *Fn = Module->getFunction(Name);
+    bccAssert(Fn != nullptr);
+    if (PromotedFunctions.insert(Fn).second) {
+      bccAssert(Fn->getLinkage() == llvm::GlobalValue::InternalLinkage);
+      Fn->setLinkage(llvm::GlobalValue::ExternalLinkage);
+      return true;
+    }
+
+    return false;
+  }
+
+  // Expand the accumulator function for a general reduce-style kernel.
+  //
+  // The input is a function of the form
+  //
+  //   define void @func(accumType* %accum, foo1 in1[, ... fooN inN] [, special arguments])
+  //
+  // where all arguments except the first are the same as for a foreach kernel.
+  //
+  // The input accumulator function gets expanded into a function of the form
+  //
+  //   define void @func.expand(%RsExpandKernelDriverInfoPfx* %p, i32 %x1, i32 %x2, accumType* %accum)
+  //
+  // which performs a serial accumulaion of elements [x1, x2) into *%accum.
+  //
+  // In pseudocode, @func.expand does:
+  //
+  //   for (i = %x1; i < %x2; ++i) {
+  //     func(%accum,
+  //          *((foo1 *)p->inPtr[0] + i)[, ... *((fooN *)p->inPtr[N-1] + i)
+  //          [, p] [, i] [, p->current.y] [, p->current.z]);
+  //   }
+  //
+  // This is very similar to foreach kernel expansion with no output.
+  bool ExpandReduceNewAccumulator(llvm::Function *FnAccumulator, uint32_t Signature, size_t NumInputs) {
+    ALOGV("Expanding accumulator %s for general reduce kernel",
+          FnAccumulator->getName().str().c_str());
+
+    // Create TBAA meta-data.
+    llvm::MDNode *TBAARenderScriptDistinct, *TBAARenderScript,
+                 *TBAAAllocation, *TBAAPointer;
+    llvm::MDBuilder MDHelper(*Context);
+    TBAARenderScriptDistinct =
+      MDHelper.createTBAARoot(kRenderScriptTBAARootName);
+    TBAARenderScript = MDHelper.createTBAANode(kRenderScriptTBAANodeName,
+        TBAARenderScriptDistinct);
+    TBAAAllocation = MDHelper.createTBAAScalarTypeNode("allocation",
+                                                       TBAARenderScript);
+    TBAAAllocation = MDHelper.createTBAAStructTagNode(TBAAAllocation,
+                                                      TBAAAllocation, 0);
+    TBAAPointer = MDHelper.createTBAAScalarTypeNode("pointer",
+                                                    TBAARenderScript);
+    TBAAPointer = MDHelper.createTBAAStructTagNode(TBAAPointer, TBAAPointer, 0);
+
+    auto AccumulatorArgIter = FnAccumulator->arg_begin();
+
+    // Create empty accumulator function.
+    llvm::Function *FnExpandedAccumulator =
+        createEmptyExpandedReduceNewAccumulator(FnAccumulator->getName(),
+                                                (AccumulatorArgIter++)->getType());
+
+    // Extract the expanded accumulator's parameters.  It is
+    // guaranteed by createEmptyExpandedReduceNewAccumulator that
+    // there will be 4 parameters.
+    bccAssert(FnExpandedAccumulator->arg_size() == kNumExpandedReduceNewAccumulatorParams);
+    auto ExpandedAccumulatorArgIter = FnExpandedAccumulator->arg_begin();
+    llvm::Value *Arg_p     = &*(ExpandedAccumulatorArgIter++);
+    llvm::Value *Arg_x1    = &*(ExpandedAccumulatorArgIter++);
+    llvm::Value *Arg_x2    = &*(ExpandedAccumulatorArgIter++);
+    llvm::Value *Arg_accum = &*(ExpandedAccumulatorArgIter++);
+
+    // Construct the actual function body.
+    llvm::IRBuilder<> Builder(FnExpandedAccumulator->getEntryBlock().begin());
+
+    // Create the loop structure.
+    llvm::BasicBlock *LoopHeader = Builder.GetInsertBlock();
+    llvm::PHINode *IndVar;
+    createLoop(Builder, Arg_x1, Arg_x2, &IndVar);
+
+    llvm::SmallVector<llvm::Value*, 8> CalleeArgs;
+    const int CalleeArgsContextIdx =
+        ExpandSpecialArguments(Signature, IndVar, Arg_p, Builder, CalleeArgs,
+                               [](){}, LoopHeader->getTerminator());
+
+    llvm::SmallVector<llvm::Value*, 8> InBufPtrs;
+    llvm::SmallVector<llvm::Value*, 8> InStructTempSlots;
+    ExpandInputsLoopInvariant(Builder, LoopHeader, Arg_p, TBAAPointer, AccumulatorArgIter, NumInputs,
+                              InBufPtrs, InStructTempSlots);
+
+    // Populate the actual call to the original accumulator.
+    llvm::SmallVector<llvm::Value*, 8> RootArgs;
+    RootArgs.push_back(Arg_accum);
+    ExpandInputsBody(Builder, Arg_x1, TBAAAllocation, NumInputs, InBufPtrs, InStructTempSlots,
+                     IndVar, RootArgs);
+    finishArgList(RootArgs, CalleeArgs, CalleeArgsContextIdx, *FnAccumulator, Builder);
+    Builder.CreateCall(FnAccumulator, RootArgs);
+
+    return true;
+  }
+
   /// @brief Checks if pointers to allocation internals are exposed
   ///
   /// This function verifies if through the parameters passed to the kernel
@@ -1315,7 +1536,7 @@
       }
     }
 
-    // Expand reduce_* style kernels.
+    // Expand simple reduce_* style kernels.
     mExportReduceCount = me.getExportReduceCount();
     mExportReduceNameList = me.getExportReduceNameList();
 
@@ -1326,6 +1547,25 @@
       }
     }
 
+    // Process general reduce_* style functions.
+    const size_t ExportReduceNewCount = me.getExportReduceNewCount();
+    const bcinfo::MetadataExtractor::ReduceNew *ExportReduceNewList = me.getExportReduceNewList();
+    //   Note that functions can be shared between kernels
+    FunctionSet PromotedFunctions, ExpandedAccumulators;
+
+    for (size_t i = 0; i < ExportReduceNewCount; ++i) {
+      Changed |= PromoteReduceNewFunction(ExportReduceNewList[i].mInitializerName, PromotedFunctions);
+      Changed |= PromoteReduceNewFunction(ExportReduceNewList[i].mOutConverterName, PromotedFunctions);
+
+      // Accumulator
+      llvm::Function *accumulator = Module.getFunction(ExportReduceNewList[i].mAccumulatorName);
+      bccAssert(accumulator != nullptr);
+      if (ExpandedAccumulators.insert(accumulator).second)
+        Changed |= ExpandReduceNewAccumulator(accumulator,
+                                              ExportReduceNewList[i].mSignature,
+                                              ExportReduceNewList[i].mInputCount);
+    }
+
     if (gEnableRsTbaa && !allocPointersExposed(Module)) {
       connectRenderScriptTBAAMetadata(Module);
     }
diff --git a/lib/Support/CompilerConfig.cpp b/lib/Support/CompilerConfig.cpp
index 71cd7cc..d2b3740 100644
--- a/lib/Support/CompilerConfig.cpp
+++ b/lib/Support/CompilerConfig.cpp
@@ -110,12 +110,12 @@
   }
 
   // Configure each architecture for any necessary additional flags.
+  std::vector<std::string> attributes;
   switch (mArchType) {
 #if defined(PROVIDE_ARM_CODEGEN)
   case llvm::Triple::arm: {
     llvm::StringMap<bool> features;
     llvm::sys::getHostCPUFeatures(features);
-    std::vector<std::string> attributes;
 
 #if defined(__HOST__) || defined(ARCH_ARM_HAVE_VFP)
     attributes.push_back("+vfp3");
@@ -150,7 +150,6 @@
     if (features.count("fp16") && features["fp16"])
       attributes.push_back("+fp16");
 
-    setFeatureString(attributes);
 
 #if defined(TARGET_BUILD)
     if (!getProperty("debug.rs.arm-no-tune-for-cpu")) {
@@ -213,19 +212,41 @@
 #if defined (PROVIDE_X86_CODEGEN)
   case llvm::Triple::x86:
     getTargetOptions().UseInitArray = true;
+#if defined (DEFAULT_X86_CODEGEN) && !defined (DEFAULT_X86_64_CODEGEN)
+    setCPU(llvm::sys::getHostCPUName());
+#else
+    // generic fallback for 32bit x86 targets
+    setCPU("atom");
+#endif // DEFAULT_X86_CODEGEN && !DEFAULT_X86_64_CODEGEN
 
 #ifndef __HOST__
     // If not running on the host, and f16c is available, set it in the feature
     // string
     if (HasF16C())
-      mFeatureString = "+f16c";
+      attributes.push_back("+f16c");
+#if defined(__SSE3__)
+    attributes.push_back("+sse3");
+    attributes.push_back("+ssse3");
+#endif
+#if defined(__SSE4_1__)
+    attributes.push_back("+sse4.1");
+#endif
+#if defined(__SSE4_2__)
+    attributes.push_back("+sse4.2");
+#endif
 #endif // __HOST__
-
     break;
 #endif  // PROVIDE_X86_CODEGEN
 
 #if defined (PROVIDE_X86_CODEGEN)
+// PROVIDE_X86_CODEGEN is defined for both x86 and x86_64
   case llvm::Triple::x86_64:
+#if defined(DEFAULT_X86_64_CODEGEN)
+    setCPU(llvm::sys::getHostCPUName());
+#else
+    // generic fallback for 64bit x86 targets
+    setCPU("core2");
+#endif
     // x86_64 needs small CodeModel if use PIC_ reloc, or else dlopen failed with TEXTREL.
     if (getRelocationModel() == llvm::Reloc::PIC_) {
       setCodeModel(llvm::CodeModel::Small);
@@ -238,7 +259,7 @@
     // If not running on the host, and f16c is available, set it in the feature
     // string
     if (HasF16C())
-      mFeatureString = "+f16c";
+      attributes.push_back("+f16c");
 #endif // __HOST__
 
     break;
@@ -249,6 +270,7 @@
     return false;
   }
 
+  setFeatureString(attributes);
   return true;
 }
 
diff --git a/tests/libbcc/test_reduce_general_metadata.ll b/tests/libbcc/test_reduce_general_metadata.ll
new file mode 100644
index 0000000..d23854a
--- /dev/null
+++ b/tests/libbcc/test_reduce_general_metadata.ll
@@ -0,0 +1,333 @@
+; Check that the #rs_export_reduce_new node is recognized.
+
+; RUN: llvm-rs-as %s -o %t
+; RUN: bcinfo %t | FileCheck %s
+
+; CHECK: exportReduceNewCount: 8
+; CHECK: exportReduceNewList[0]: addint - 0x00000001 - 1 - 4
+; CHECK:   accumulator(aiAccum)
+; CHECK: exportReduceNewList[1]: mpyint - 0x00000001 - 1 - 4
+; CHECK:   initializer(mpyInit)
+; CHECK:   accumulator(mpyAccum)
+; CHECK: exportReduceNewList[2]: dp - 0x00000001 - 2 - 4
+; CHECK:   accumulator(dpAccum)
+; CHECK:   combiner(dpSum)
+; CHECK: exportReduceNewList[3]: findMinAndMax - 0x00000009 - 1 - 16
+; CHECK:   initializer(fMMInit)
+; CHECK:   accumulator(fMMAccumulator)
+; CHECK:   combiner(fMMCombiner)
+; CHECK:   outconverter(fMMOutConverter)
+; CHECK: exportReduceNewList[4]: fz - 0x00000009 - 1 - 4
+; CHECK:   initializer(fzInit)
+; CHECK:   accumulator(fzAccum)
+; CHECK:   combiner(fzCombine)
+; CHECK:   halter(fzFound)
+; CHECK: exportReduceNewList[5]: fz2 - 0x00000019 - 1 - 8
+; CHECK:   initializer(fz2Init)
+; CHECK:   accumulator(fz2Accum)
+; CHECK:   combiner(fz2Combine)
+; CHECK:   halter(fz2Found)
+; CHECK: exportReduceNewList[6]: histogram - 0x00000001 - 1 - 1024
+; CHECK:   accumulator(hsgAccum)
+; CHECK:   combiner(hsgCombine)
+; CHECK: exportReduceNewList[7]: mode - 0x00000001 - 1 - 1024
+; CHECK:   accumulator(hsgAccum)
+; CHECK:   combiner(hsgCombine)
+; CHECK:   outconverter(modeOutConvert)
+
+; ModuleID = 'reduce_general_examples.bc'
+target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
+target triple = "aarch64-none-linux-gnueabi"
+
+%struct.MinAndMax.0 = type { %struct.IndexedVal.1, %struct.IndexedVal.1 }
+%struct.IndexedVal.1 = type { float, i32 }
+
+@fMMInit.r = internal unnamed_addr constant %struct.MinAndMax.0 { %struct.IndexedVal.1 { float 0.000000e+00, i32 -1 }, %struct.IndexedVal.1 { float -0.000000e+00, i32 -1 } }, align 4
+@llvm.used = appending global [20 x i8*] [i8* bitcast (void (<2 x i32>*)* @fz2Init to i8*), i8* bitcast (void ([256 x i32]*, [256 x i32]*)* @hsgCombine to i8*), i8* bitcast (i1 (<2 x i32>*)* @fz2Found to i8*), i8* bitcast (void (i32*, i32)* @mpyAccum to i8*), i8* bitcast (void (%struct.MinAndMax.0*)* @fMMInit to i8*), i8* bitcast (void (float*, float, float)* @dpAccum to i8*), i8* bitcast (void (<2 x i32>*, [256 x i32]*)* @modeOutConvert to i8*), i8* bitcast (void ([256 x i32]*, i8)* @hsgAccum to i8*), i8* bitcast (void (i32*)* @mpyInit to i8*), i8* bitcast (void (%struct.MinAndMax.0*, float, i32)* @fMMAccumulator to i8*), i8* bitcast (void (float*, float*)* @dpSum to i8*), i8* bitcast (void (%struct.MinAndMax.0*, %struct.MinAndMax.0*)* @fMMCombiner to i8*), i8* bitcast (void (i32*, i32*)* @fzCombine to i8*), i8* bitcast (void (i32*, i32)* @aiAccum to i8*), i8* bitcast (void (i32*)* @fzInit to i8*), i8* bitcast (void (i32*, i32, i32)* @fzAccum to i8*), i8* bitcast (i1 (i32*)* @fzFound to i8*), i8* bitcast (void (<2 x i32>*, i32, i32, i32)* @fz2Accum to i8*), i8* bitcast (void (<2 x i32>*, %struct.MinAndMax.0*)* @fMMOutConverter to i8*), i8* bitcast (void (<2 x i32>*, <2 x i32>*)* @fz2Combine to i8*)], section "llvm.metadata"
+
+; Function Attrs: nounwind
+define internal void @aiAccum(i32* nocapture %accum, i32 %val) #0 {
+  %1 = load i32, i32* %accum, align 4, !tbaa !18
+  %2 = add nsw i32 %1, %val
+  store i32 %2, i32* %accum, align 4, !tbaa !18
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @mpyInit(i32* nocapture %accum) #0 {
+  store i32 1, i32* %accum, align 4, !tbaa !18
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @mpyAccum(i32* nocapture %accum, i32 %val) #0 {
+  %1 = load i32, i32* %accum, align 4, !tbaa !18
+  %2 = mul nsw i32 %1, %val
+  store i32 %2, i32* %accum, align 4, !tbaa !18
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @dpAccum(float* nocapture %accum, float %in1, float %in2) #0 {
+  %1 = fmul float %in1, %in2
+  %2 = load float, float* %accum, align 4, !tbaa !22
+  %3 = fadd float %1, %2
+  store float %3, float* %accum, align 4, !tbaa !22
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @dpSum(float* nocapture %accum, float* nocapture %val) #0 {
+  %1 = load float, float* %val, align 4, !tbaa !22
+  %2 = load float, float* %accum, align 4, !tbaa !22
+  %3 = fadd float %1, %2
+  store float %3, float* %accum, align 4, !tbaa !22
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @fMMInit(%struct.MinAndMax.0* nocapture %accum) #0 {
+  %1 = bitcast %struct.MinAndMax.0* %accum to i8*
+  tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %1, i8* bitcast (%struct.MinAndMax.0* @fMMInit.r to i8*), i64 16, i32 4, i1 false), !tbaa.struct !24
+  ret void
+}
+
+; Function Attrs: nounwind
+declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture readonly, i64, i32, i1) #0
+
+; Function Attrs: nounwind
+define internal void @fMMAccumulator(%struct.MinAndMax.0* nocapture %accum, float %in, i32 %x) #0 {
+  %1 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %accum, i64 0, i32 0, i32 0
+  %2 = load float, float* %1, align 4, !tbaa !22
+  %3 = fcmp ogt float %2, %in
+  br i1 %3, label %4, label %6
+
+; <label>:4                                       ; preds = %0
+  store float %in, float* %1, align 4
+  %5 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %accum, i64 0, i32 0, i32 1
+  store i32 %x, i32* %5, align 4
+  br label %6
+
+; <label>:6                                       ; preds = %4, %0
+  %7 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %accum, i64 0, i32 1, i32 0
+  %8 = load float, float* %7, align 4, !tbaa !22
+  %9 = fcmp olt float %8, %in
+  br i1 %9, label %10, label %12
+
+; <label>:10                                      ; preds = %6
+  store float %in, float* %7, align 4
+  %11 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %accum, i64 0, i32 1, i32 1
+  store i32 %x, i32* %11, align 4
+  br label %12
+
+; <label>:12                                      ; preds = %10, %6
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @fMMCombiner(%struct.MinAndMax.0* nocapture %accum, %struct.MinAndMax.0* nocapture %val) #0 {
+  %1 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %val, i64 0, i32 0, i32 0
+  %2 = load float, float* %1, align 4, !tbaa !22
+  %3 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %val, i64 0, i32 0, i32 1
+  %4 = load i32, i32* %3, align 4, !tbaa !18
+  tail call void @fMMAccumulator(%struct.MinAndMax.0* %accum, float %2, i32 %4)
+  %5 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %val, i64 0, i32 1, i32 0
+  %6 = load float, float* %5, align 4, !tbaa !22
+  %7 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %val, i64 0, i32 1, i32 1
+  %8 = load i32, i32* %7, align 4, !tbaa !18
+  tail call void @fMMAccumulator(%struct.MinAndMax.0* %accum, float %6, i32 %8)
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @fMMOutConverter(<2 x i32>* nocapture %result, %struct.MinAndMax.0* nocapture %val) #0 {
+  %1 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %val, i64 0, i32 0, i32 1
+  %2 = load i32, i32* %1, align 4, !tbaa !18
+  %3 = load <2 x i32>, <2 x i32>* %result, align 8
+  %4 = insertelement <2 x i32> %3, i32 %2, i64 0
+  store <2 x i32> %4, <2 x i32>* %result, align 8
+  %5 = getelementptr inbounds %struct.MinAndMax.0, %struct.MinAndMax.0* %val, i64 0, i32 1, i32 1
+  %6 = load i32, i32* %5, align 4, !tbaa !18
+  %7 = insertelement <2 x i32> %4, i32 %6, i64 1
+  store <2 x i32> %7, <2 x i32>* %result, align 8
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @fzInit(i32* nocapture %accumIdx) #0 {
+  store i32 -1, i32* %accumIdx, align 4, !tbaa !18
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @fzAccum(i32* nocapture %accumIdx, i32 %inVal, i32 %x) #0 {
+  %1 = icmp eq i32 %inVal, 0
+  br i1 %1, label %2, label %3
+
+; <label>:2                                       ; preds = %0
+  store i32 %x, i32* %accumIdx, align 4, !tbaa !18
+  br label %3
+
+; <label>:3                                       ; preds = %2, %0
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @fzCombine(i32* nocapture %accumIdx, i32* nocapture %accumIdx2) #0 {
+  %1 = load i32, i32* %accumIdx2, align 4, !tbaa !18
+  %2 = icmp sgt i32 %1, -1
+  br i1 %2, label %3, label %4
+
+; <label>:3                                       ; preds = %0
+  store i32 %1, i32* %accumIdx, align 4, !tbaa !18
+  br label %4
+
+; <label>:4                                       ; preds = %3, %0
+  ret void
+}
+
+; Function Attrs: nounwind readonly
+define internal i1 @fzFound(i32* nocapture %accumIdx) #1 {
+  %1 = load i32, i32* %accumIdx, align 4, !tbaa !18
+  %2 = icmp sgt i32 %1, -1
+  ret i1 %2
+}
+
+; Function Attrs: nounwind
+define internal void @fz2Init(<2 x i32>* nocapture %accum) #0 {
+  store <2 x i32> <i32 -1, i32 -1>, <2 x i32>* %accum, align 8
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @fz2Accum(<2 x i32>* nocapture %accum, i32 %inVal, i32 %x, i32 %y) #0 {
+  %1 = icmp eq i32 %inVal, 0
+  br i1 %1, label %2, label %5
+
+; <label>:2                                       ; preds = %0
+  %3 = insertelement <2 x i32> undef, i32 %x, i64 0
+  %4 = insertelement <2 x i32> %3, i32 %y, i64 1
+  store <2 x i32> %4, <2 x i32>* %accum, align 8
+  br label %5
+
+; <label>:5                                       ; preds = %2, %0
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @fz2Combine(<2 x i32>* nocapture %accum, <2 x i32>* nocapture %accum2) #0 {
+  %1 = load <2 x i32>, <2 x i32>* %accum2, align 8
+  %2 = extractelement <2 x i32> %1, i64 0
+  %3 = icmp sgt i32 %2, -1
+  br i1 %3, label %4, label %5
+
+; <label>:4                                       ; preds = %0
+  store <2 x i32> %1, <2 x i32>* %accum, align 8, !tbaa !25
+  br label %5
+
+; <label>:5                                       ; preds = %4, %0
+  ret void
+}
+
+; Function Attrs: nounwind readonly
+define internal i1 @fz2Found(<2 x i32>* nocapture %accum) #1 {
+  %1 = load <2 x i32>, <2 x i32>* %accum, align 8
+  %2 = extractelement <2 x i32> %1, i64 0
+  %3 = icmp sgt i32 %2, -1
+  ret i1 %3
+}
+
+; Function Attrs: nounwind
+define internal void @hsgAccum([256 x i32]* nocapture %h, i8 %in) #0 {
+  %1 = zext i8 %in to i64
+  %2 = getelementptr inbounds [256 x i32], [256 x i32]* %h, i64 0, i64 %1
+  %3 = load i32, i32* %2, align 4, !tbaa !18
+  %4 = add i32 %3, 1
+  store i32 %4, i32* %2, align 4, !tbaa !18
+  ret void
+}
+
+; Function Attrs: nounwind
+define internal void @hsgCombine([256 x i32]* nocapture %accum, [256 x i32]* nocapture %addend) #0 {
+  br label %2
+
+; <label>:1                                       ; preds = %2
+  ret void
+
+; <label>:2                                       ; preds = %2, %0
+  %indvars.iv = phi i64 [ 0, %0 ], [ %indvars.iv.next, %2 ]
+  %3 = getelementptr inbounds [256 x i32], [256 x i32]* %addend, i64 0, i64 %indvars.iv
+  %4 = load i32, i32* %3, align 4, !tbaa !18
+  %5 = getelementptr inbounds [256 x i32], [256 x i32]* %accum, i64 0, i64 %indvars.iv
+  %6 = load i32, i32* %5, align 4, !tbaa !18
+  %7 = add i32 %6, %4
+  store i32 %7, i32* %5, align 4, !tbaa !18
+  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+  %exitcond = icmp eq i64 %indvars.iv.next, 256
+  br i1 %exitcond, label %1, label %2
+}
+
+; Function Attrs: nounwind
+define internal void @modeOutConvert(<2 x i32>* nocapture %result, [256 x i32]* nocapture %h) #0 {
+  br label %8
+
+; <label>:1                                       ; preds = %8
+  %2 = load <2 x i32>, <2 x i32>* %result, align 8
+  %3 = insertelement <2 x i32> %2, i32 %i.0.mode.0, i64 0
+  store <2 x i32> %3, <2 x i32>* %result, align 8
+  %4 = zext i32 %i.0.mode.0 to i64
+  %5 = getelementptr inbounds [256 x i32], [256 x i32]* %h, i64 0, i64 %4
+  %6 = load i32, i32* %5, align 4, !tbaa !18
+  %7 = insertelement <2 x i32> %3, i32 %6, i64 1
+  store <2 x i32> %7, <2 x i32>* %result, align 8
+  ret void
+
+; <label>:8                                       ; preds = %8, %0
+  %indvars.iv = phi i64 [ 1, %0 ], [ %indvars.iv.next, %8 ]
+  %mode.01 = phi i32 [ 0, %0 ], [ %i.0.mode.0, %8 ]
+  %9 = getelementptr inbounds [256 x i32], [256 x i32]* %h, i64 0, i64 %indvars.iv
+  %10 = load i32, i32* %9, align 4, !tbaa !18
+  %11 = zext i32 %mode.01 to i64
+  %12 = getelementptr inbounds [256 x i32], [256 x i32]* %h, i64 0, i64 %11
+  %13 = load i32, i32* %12, align 4, !tbaa !18
+  %14 = icmp ugt i32 %10, %13
+  %15 = trunc i64 %indvars.iv to i32
+  %i.0.mode.0 = select i1 %14, i32 %15, i32 %mode.01
+  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+  %exitcond = icmp eq i64 %indvars.iv.next, 256
+  br i1 %exitcond, label %1, label %8
+}
+
+attributes #0 = { nounwind }
+attributes #1 = { nounwind readonly }
+
+!llvm.ident = !{!0}
+!\23pragma = !{!1, !2}
+!\23rs_export_reduce_new = !{!3, !5, !7, !9, !11, !13, !15, !17}
+
+!0 = !{!"clang version 3.6 "}
+!1 = !{!"version", !"1"}
+!2 = !{!"java_package_name", !"examples"}
+!3 = !{!"addint", !"4", !4}
+!4 = !{!"aiAccum", !"1"}
+!5 = !{!"mpyint", !"4", !6, !"mpyInit"}
+!6 = !{!"mpyAccum", !"1"}
+!7 = !{!"dp", !"4", !8, null, !"dpSum"}
+!8 = !{!"dpAccum", !"1"}
+!9 = !{!"findMinAndMax", !"16", !10, !"fMMInit", !"fMMCombiner", !"fMMOutConverter"}
+!10 = !{!"fMMAccumulator", !"9"}
+!11 = !{!"fz", !"4", !12, !"fzInit", !"fzCombine", null, !"fzFound"}
+!12 = !{!"fzAccum", !"9"}
+!13 = !{!"fz2", !"8", !14, !"fz2Init", !"fz2Combine", null, !"fz2Found"}
+!14 = !{!"fz2Accum", !"25"}
+!15 = !{!"histogram", !"1024", !16, null, !"hsgCombine"}
+!16 = !{!"hsgAccum", !"1"}
+!17 = !{!"mode", !"1024", !16, null, !"hsgCombine", !"modeOutConvert"}
+!18 = !{!19, !19, i64 0}
+!19 = !{!"int", !20, i64 0}
+!20 = !{!"omnipotent char", !21, i64 0}
+!21 = !{!"Simple C/C++ TBAA"}
+!22 = !{!23, !23, i64 0}
+!23 = !{!"float", !20, i64 0}
+!24 = !{i64 0, i64 4, !22, i64 4, i64 4, !18, i64 8, i64 4, !22, i64 12, i64 4, !18}
+!25 = !{!20, !20, i64 0}
diff --git a/tools/bcc/Main.cpp b/tools/bcc/Main.cpp
index 47dc60f..37da1bc 100644
--- a/tools/bcc/Main.cpp
+++ b/tools/bcc/Main.cpp
@@ -232,30 +232,6 @@
     return false;
   }
 
-  // llvm3.5 has removed the auto-detect feature for x86 subtarget,
-  // so set features explicitly in bcc.
-  if ((config->getTriple().find("i686") != std::string::npos) ||
-    (config->getTriple().find("x86_64") != std::string::npos)) {
-    std::vector<std::string> fv;
-
-#if defined(__SSE3__)
-    fv.push_back("+sse3");
-#endif
-#if defined(__SSSE3__)
-    fv.push_back("+ssse3");
-#endif
-#if defined(__SSE4_1__)
-    fv.push_back("+sse4.1");
-#endif
-#if defined(__SSE4_2__)
-    fv.push_back("+sse4.2");
-#endif
-
-    if (fv.size()) {
-      config->setFeatureString(fv);
-    }
-  }
-
   if (OptPIC) {
     config->setRelocationModel(llvm::Reloc::PIC_);