Metadata support for general reduction.

Needs corresponding change in frameworks/rs: The driver and bcinfo
need to remain precisely in sync with regard to metadata format in
order for RenderScript to work at all.

Also fixed memory leak in MetadataExtractor.

Bug: 23535724
Change-Id: I6857f605c79139bdce362a7aeef73c91fc502ff6
diff --git a/bcinfo/MetadataExtractor.cpp b/bcinfo/MetadataExtractor.cpp
index add1ab1..f190c48 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";
@@ -163,10 +174,11 @@
 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),
@@ -180,10 +192,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 +237,9 @@
   delete [] mExportForEachSignatureList;
   mExportForEachSignatureList = nullptr;
 
+  delete [] mExportForEachInputCountList;
+  mExportForEachInputCountList = nullptr;
+
   if (mExportReduceNameList) {
     for (size_t i = 0; i < mExportReduceCount; i++) {
       delete [] mExportReduceNameList[i];
@@ -233,6 +249,9 @@
   delete [] mExportReduceNameList;
   mExportReduceNameList = nullptr;
 
+  delete [] mExportReduceNewList;
+  mExportReduceNewList = nullptr;
+
   for (size_t i = 0; i < mPragmaCount; i++) {
     if (mPragmaKeyList) {
       delete [] mPragmaKeyList[i];
@@ -468,6 +487,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 +618,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 =
@@ -577,6 +653,11 @@
     return false;
   }
 
+  if (!populateReduceNewMetadata(ExportReduceNewMetadata)) {
+    ALOGE("Could not populate export general reduction metadata");
+    return false;
+  }
+
   populatePragmaMetadata(PragmaMetadata);
 
   if (!populateObjectSlotMetadata(ObjectSlotMetadata)) {
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/bcinfo/MetadataExtractor.h b/include/bcinfo/MetadataExtractor.h
index 742346a..738c1c2 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;
@@ -83,6 +119,7 @@
   // 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 +220,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 {
diff --git a/lib/Core/Compiler.cpp b/lib/Core/Compiler.cpp
index 5c769b4..71c0ed2 100644
--- a/lib/Core/Compiler.cpp
+++ b/lib/Core/Compiler.cpp
@@ -330,10 +330,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 +351,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 +359,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());
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/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}