Merge "Moving libRS"
diff --git a/bcinfo/BitcodeWrapper.cpp b/bcinfo/BitcodeWrapper.cpp
index 67f95b8..e04db88 100644
--- a/bcinfo/BitcodeWrapper.cpp
+++ b/bcinfo/BitcodeWrapper.cpp
@@ -31,7 +31,8 @@
 BitcodeWrapper::BitcodeWrapper(const char *bitcode, size_t bitcodeSize)
     : mFileType(BC_NOT_BC), mBitcode(bitcode),
       mBitcodeEnd(bitcode + bitcodeSize - 1), mBitcodeSize(bitcodeSize),
-      mHeaderVersion(0), mTargetAPI(0) {
+      mHeaderVersion(0), mTargetAPI(0), mCompilerVersion(0),
+      mOptimizationLevel(3) {
   InMemoryWrapperInput inMem(mBitcode, mBitcodeSize);
   BitcodeWrapperer wrapperer(&inMem, NULL);
   if (wrapperer.IsInputBitcodeWrapper()) {
diff --git a/bcinfo/MetadataExtractor.cpp b/bcinfo/MetadataExtractor.cpp
index 4c5a8bf..02875fb 100644
--- a/bcinfo/MetadataExtractor.cpp
+++ b/bcinfo/MetadataExtractor.cpp
@@ -16,6 +16,8 @@
 
 #include "bcinfo/MetadataExtractor.h"
 
+#include "bcinfo/BitcodeWrapper.h"
+
 #define LOG_TAG "bcinfo"
 #include <cutils/log.h>
 
@@ -55,10 +57,6 @@
 // synced with slang_rs_metadata.h)
 static const llvm::StringRef ObjectSlotMetadataName = "#rs_object_slots";
 
-// Name of metadata node where RS optimization level resides (should be
-// synced with slang_rs_metadata.h)
-static const llvm::StringRef OptimizationLevelMetadataName = "#optimization_level";
-
 
 MetadataExtractor::MetadataExtractor(const char *bitcode, size_t bitcodeSize)
     : mModule(NULL), mBitcode(bitcode), mBitcodeSize(bitcodeSize),
@@ -66,8 +64,11 @@
       mExportVarNameList(NULL), mExportFuncNameList(NULL),
       mExportForEachNameList(NULL), mExportForEachSignatureList(NULL),
       mPragmaCount(0), mPragmaKeyList(NULL), mPragmaValueList(NULL),
-      mObjectSlotCount(0), mObjectSlotList(NULL), mOptimizationLevel(3),
+      mObjectSlotCount(0), mObjectSlotList(NULL),
       mRSFloatPrecision(RS_FP_Full) {
+  BitcodeWrapper wrapper(bitcode, bitcodeSize);
+  mCompilerVersion = wrapper.getCompilerVersion();
+  mOptimizationLevel = wrapper.getOptimizationLevel();
 }
 
 
@@ -77,7 +78,9 @@
       mExportVarNameList(NULL), mExportFuncNameList(NULL),
       mExportForEachNameList(NULL), mExportForEachSignatureList(NULL),
       mPragmaCount(0), mPragmaKeyList(NULL), mPragmaValueList(NULL),
-      mObjectSlotCount(0), mObjectSlotList(NULL), mOptimizationLevel(3) {
+      mObjectSlotCount(0), mObjectSlotList(NULL) {
+  mCompilerVersion = 0;
+  mOptimizationLevel = 3;
 }
 
 
@@ -393,8 +396,6 @@
       mModule->getNamedMetadata(PragmaMetadataName);
   const llvm::NamedMDNode *ObjectSlotMetadata =
       mModule->getNamedMetadata(ObjectSlotMetadataName);
-  const llvm::NamedMDNode *OptimizationLevelMetadata =
-      mModule->getNamedMetadata(OptimizationLevelMetadataName);
 
 
   if (!populateVarNameMetadata(ExportVarMetadata)) {
@@ -420,13 +421,6 @@
     return false;
   }
 
-  if (OptimizationLevelMetadata) {
-    llvm::ConstantInt* OL = llvm::dyn_cast<llvm::ConstantInt>(
-      OptimizationLevelMetadata->getOperand(0)->getOperand(0));
-    mOptimizationLevel = OL->getZExtValue();
-  }
-
-
   return true;
 }
 
diff --git a/bcinfo/Wrap/bitcode_wrapperer.cpp b/bcinfo/Wrap/bitcode_wrapperer.cpp
index c8b7d26..6638536 100644
--- a/bcinfo/Wrap/bitcode_wrapperer.cpp
+++ b/bcinfo/Wrap/bitcode_wrapperer.cpp
@@ -95,7 +95,7 @@
 }
 
 bool BitcodeWrapperer::Seek(uint32_t pos) {
-  if (infile_->Seek(pos)) {
+  if (infile_ != NULL && infile_->Seek(pos)) {
     ClearBuffer();
     return true;
   }
@@ -131,6 +131,11 @@
     buffer_size_ = 0;
   }
 
+  // If we don't have an input, we can't refill the buffer at all.
+  if (infile_ == NULL) {
+    return;
+  }
+
   // Now fill in remaining space.
   size_t needed = buffer_.size() - buffer_size_;
 
diff --git a/include/bcinfo/MetadataExtractor.h b/include/bcinfo/MetadataExtractor.h
index 4a3e6bb..d1a88d9 100644
--- a/include/bcinfo/MetadataExtractor.h
+++ b/include/bcinfo/MetadataExtractor.h
@@ -54,6 +54,7 @@
   size_t mObjectSlotCount;
   const uint32_t *mObjectSlotList;
 
+  uint32_t mCompilerVersion;
   uint32_t mOptimizationLevel;
 
   enum RSFloatPrecision mRSFloatPrecision;
@@ -176,6 +177,16 @@
     return mObjectSlotList;
   }
 
+  /**
+   * \return compiler version that generated this bitcode.
+   */
+  uint32_t getCompilerVersion() const {
+    return mCompilerVersion;
+  }
+
+  /**
+   * \return compiler optimization level for this bitcode.
+   */
   uint32_t getOptimizationLevel() const {
     return mOptimizationLevel;
   }
diff --git a/include/bcinfo/Wrap/bitcode_wrapperer.h b/include/bcinfo/Wrap/bitcode_wrapperer.h
index 2fbce21..97f6294 100644
--- a/include/bcinfo/Wrap/bitcode_wrapperer.h
+++ b/include/bcinfo/Wrap/bitcode_wrapperer.h
@@ -101,7 +101,13 @@
   void FillBuffer();
 
   // Returns the number of bytes in infile.
-  off_t GetInFileSize() { return infile_->Size(); }
+  off_t GetInFileSize() {
+    if (infile_ != NULL) {
+      return infile_->Size();
+    } else {
+      return 0;
+    }
+  }
 
   // Returns the offset of bitcode (i.e. the size of the wrapper header)
   // if the output file were to be written now.
diff --git a/lib/ExecutionEngine/Compiler.cpp b/lib/ExecutionEngine/Compiler.cpp
index 5895057..58a49c5 100644
--- a/lib/ExecutionEngine/Compiler.cpp
+++ b/lib/ExecutionEngine/Compiler.cpp
@@ -274,7 +274,7 @@
   std::vector<const char*> ExportSymbols;
 
   // Defaults to maximum optimization level from MetadataExtractor.
-  int OptimizationLevel = ME.getOptimizationLevel();
+  uint32_t OptimizationLevel = ME.getOptimizationLevel();
 
   if (OptimizationLevel == 0) {
     CodeGenOptLevel = llvm::CodeGenOpt::None;
diff --git a/lib/ExecutionEngine/Script.cpp b/lib/ExecutionEngine/Script.cpp
index 445d075..87d6e1d 100644
--- a/lib/ExecutionEngine/Script.cpp
+++ b/lib/ExecutionEngine/Script.cpp
@@ -17,6 +17,7 @@
 #include "Script.h"
 
 #include "Config.h"
+#include "bcinfo/BitcodeWrapper.h"
 
 #include "MCCacheReader.h"
 #include "MCCacheWriter.h"
@@ -95,6 +96,10 @@
     return 1;
   }
 
+  bcinfo::BitcodeWrapper wrapper(bitcode, bitcodeSize);
+  mCompilerVersion = wrapper.getCompilerVersion();
+  mOptimizationLevel = wrapper.getOptimizationLevel();
+
   mSourceList[idx] = SourceInfo::createFromBuffer(resName,
                                                   bitcode, bitcodeSize,
                                                   flags);
diff --git a/lib/ExecutionEngine/Script.h b/lib/ExecutionEngine/Script.h
index 16a453c..0560f55 100644
--- a/lib/ExecutionEngine/Script.h
+++ b/lib/ExecutionEngine/Script.h
@@ -98,10 +98,15 @@
     BCCSymbolLookupFn mpExtSymbolLookupFn;
     void *mpExtSymbolLookupFnContext;
 
+    uint32_t mCompilerVersion;
+    uint32_t mOptimizationLevel;
+
   public:
     Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
-               mObjectType(ScriptObject::Unknown), mIsContextSlotNotAvail(false),
-               mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
+               mObjectType(ScriptObject::Unknown),
+               mIsContextSlotNotAvail(false),
+               mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL),
+               mCompilerVersion(0), mOptimizationLevel(3) {
       Compiler::GlobalInitialization();
 
       mSourceList[0] = NULL;
@@ -164,6 +169,13 @@
 
     void *lookup(const char *name);
 
+    uint32_t getCompilerVersion() const {
+      return mCompilerVersion;
+    }
+
+    uint32_t getOptimizationLevel() const {
+      return mOptimizationLevel;
+    }
 
     size_t getExportVarCount() const;
 
diff --git a/lib/ExecutionEngine/ScriptCompiled.h b/lib/ExecutionEngine/ScriptCompiled.h
index d945024..5c442c7 100644
--- a/lib/ExecutionEngine/ScriptCompiled.h
+++ b/lib/ExecutionEngine/ScriptCompiled.h
@@ -91,6 +91,13 @@
 
     void *lookup(const char *name);
 
+    uint32_t getCompilerVersion() const {
+      return mpOwner->getCompilerVersion();
+    }
+
+    uint32_t getOptimizationLevel() const {
+      return mpOwner->getOptimizationLevel();
+    }
 
     size_t getExportVarCount() const {
       return mExportVars.size();