Merge "Preserve generalized reduction combiner function for use by driver." into nyc-dev
diff --git a/bcinfo/Android.mk b/bcinfo/Android.mk
index 89dd817..46b61a7 100644
--- a/bcinfo/Android.mk
+++ b/bcinfo/Android.mk
@@ -59,6 +59,7 @@
 LOCAL_SHARED_LIBRARIES := libLLVM libcutils liblog
 
 include $(LLVM_ROOT_PATH)/llvm-device-build.mk
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(BUILD_SHARED_LIBRARY)
 endif
 
@@ -93,6 +94,7 @@
 LOCAL_SHARED_LIBRARIES_windows += libLLVM
 
 include $(LLVM_ROOT_PATH)/llvm-host-build.mk
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(BUILD_HOST_SHARED_LIBRARY)
 
 endif # don't build for unbundled branches
diff --git a/bcinfo/BitReader_2_7/Android.mk b/bcinfo/BitReader_2_7/Android.mk
index f966459..773c3c9 100644
--- a/bcinfo/BitReader_2_7/Android.mk
+++ b/bcinfo/BitReader_2_7/Android.mk
@@ -19,6 +19,7 @@
 LOCAL_MODULE_HOST_OS := darwin linux windows
 
 include $(LLVM_HOST_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(LLVM_GEN_INTRINSICS_MK)
 include $(BUILD_HOST_STATIC_LIBRARY)
 
@@ -32,6 +33,7 @@
 LOCAL_MODULE:= libLLVMBitReader_2_7
 
 include $(LLVM_DEVICE_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(LLVM_GEN_INTRINSICS_MK)
 include $(BUILD_STATIC_LIBRARY)
 endif
diff --git a/bcinfo/BitReader_2_7/BitcodeReader.cpp b/bcinfo/BitReader_2_7/BitcodeReader.cpp
index 894b801..fdb7b08 100644
--- a/bcinfo/BitReader_2_7/BitcodeReader.cpp
+++ b/bcinfo/BitReader_2_7/BitcodeReader.cpp
@@ -189,7 +189,7 @@
   std::unique_ptr<MemoryBuffer> Buffer;
   std::unique_ptr<BitstreamReader> StreamFile;
   BitstreamCursor Stream;
-  DataStreamer *LazyStreamer;
+  std::unique_ptr<DataStreamer> LazyStreamer;
   uint64_t NextUnreadBit;
   bool SeenValueSymbolTable;
 
@@ -260,11 +260,11 @@
 
   void releaseBuffer();
 
-  bool isDematerializable(const GlobalValue *GV) const override;
+  bool isDematerializable(const GlobalValue *GV) const;
   std::error_code materialize(GlobalValue *GV) override;
-  std::error_code materializeModule(Module *M) override;
+  std::error_code materializeModule() override;
   std::vector<StructType *> getIdentifiedStructTypes() const override;
-  void dematerialize(GlobalValue *GV) override;
+  void dematerialize(GlobalValue *GV);
 
   /// @brief Main interface to parsing a bitcode buffer.
   /// @returns true if an error occurred.
@@ -1992,16 +1992,16 @@
   for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
        FI != FE; ++FI) {
     Function *NewFn;
-    if (UpgradeIntrinsicFunction(FI, NewFn))
-      UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
+    if (UpgradeIntrinsicFunction(&*FI, NewFn))
+      UpgradedIntrinsics.push_back(std::make_pair(&*FI, NewFn));
   }
 
   // Look for global variables which need to be renamed.
   for (Module::global_iterator
          GI = TheModule->global_begin(), GE = TheModule->global_end();
-       GI != GE;) {
-    GlobalVariable *GV = GI++;
-    UpgradeGlobalVariable(GV);
+       GI != GE; GI++) {
+    GlobalVariable *GV = &*GI;
+    UpgradeGlobalVariable(&*GV);
   }
 
   // Force deallocation of memory for these vectors to favor the client that
@@ -2039,15 +2039,15 @@
       for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
            FI != FE; ++FI) {
         Function* NewFn;
-        if (UpgradeIntrinsicFunction(FI, NewFn))
-          UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
+        if (UpgradeIntrinsicFunction(&*FI, NewFn))
+          UpgradedIntrinsics.push_back(std::make_pair(&*FI, NewFn));
       }
 
       // Look for global variables which need to be renamed.
       for (Module::global_iterator
              GI = TheModule->global_begin(), GE = TheModule->global_end();
            GI != GE; ++GI)
-        UpgradeGlobalVariable(GI);
+        UpgradeGlobalVariable(&*GI);
 
       // Force deallocation of memory for these vectors to favor the client that
       // want lazy deserialization.
@@ -2291,7 +2291,7 @@
     }
     // ALIAS: [alias type, aliasee val#, linkage]
     // ALIAS: [alias type, aliasee val#, linkage, visibility]
-    case bitc::MODULE_CODE_ALIAS: {
+    case bitc::MODULE_CODE_ALIAS_OLD: {
       if (Record.size() < 3)
         return Error("Invalid record");
       Type *Ty = getTypeByID(Record[0]);
@@ -2302,7 +2302,8 @@
         return Error("Invalid type for value");
 
       auto *NewGA =
-          GlobalAlias::create(PTy, getDecodedLinkage(Record[2]), "", TheModule);
+          GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
+                              getDecodedLinkage(Record[2]), "", TheModule);
       // Old bitcode files didn't have visibility field.
       if (Record.size() > 3)
         NewGA->setVisibility(GetDecodedVisibility(Record[3]));
@@ -2536,7 +2537,7 @@
 
   // Add all the function arguments to the value table.
   for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
-    ValueList.push_back(I);
+    ValueList.push_back(&*I);
 
   unsigned NextValueNo = ValueList.size();
   BasicBlock *CurBB = nullptr;
@@ -3037,12 +3038,8 @@
       // Replace 'unwind' with 'landingpad' and 'resume'.
       Type *ExnTy = StructType::get(Type::getInt8PtrTy(Context),
                                     Type::getInt32Ty(Context), nullptr);
-      Constant *PersFn =
-        F->getParent()->
-        getOrInsertFunction("__gcc_personality_v0",
-                          FunctionType::get(Type::getInt32Ty(Context), true));
 
-      LandingPadInst *LP = LandingPadInst::Create(ExnTy, PersFn, 1);
+      LandingPadInst *LP = LandingPadInst::Create(ExnTy, 1);
       LP->setCleanup(true);
 
       CurBB->getInstList().push_back(LP);
@@ -3361,14 +3358,12 @@
   F->setIsMaterializable(true);
 }
 
-std::error_code BitcodeReader::materializeModule(Module *M) {
-  assert(M == TheModule &&
-         "Can only Materialize the Module this BitcodeReader is attached to.");
+std::error_code BitcodeReader::materializeModule() {
   // Iterate over the module, deserializing any functions that are still on
   // disk.
   for (Module::iterator F = TheModule->begin(), E = TheModule->end();
        F != E; ++F) {
-    if (std::error_code EC = materialize(F))
+    if (std::error_code EC = materialize(&*F))
       return EC;
   }
   // At this point, if there are any function bodies, the current bit is
@@ -3434,7 +3429,8 @@
 std::error_code BitcodeReader::InitLazyStream() {
   // Check and strip off the bitcode wrapper; BitstreamReader expects never to
   // see it.
-  auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(LazyStreamer);
+  auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(
+      std::move(LazyStreamer));
   StreamingMemoryObject &Bytes = *OwnedBytes;
   StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
   Stream.init(&*StreamFile);
@@ -3528,7 +3524,7 @@
     return ModuleOrErr;
   Module *M = ModuleOrErr.get();
   // Read in the entire module, and destroy the BitcodeReader.
-  if (std::error_code EC = M->materializeAllPermanently()) {
+  if (std::error_code EC = M->materializeAll()) {
     delete M;
     return EC;
   }
diff --git a/bcinfo/BitReader_3_0/Android.mk b/bcinfo/BitReader_3_0/Android.mk
index e0d642f..dd6ce8d 100644
--- a/bcinfo/BitReader_3_0/Android.mk
+++ b/bcinfo/BitReader_3_0/Android.mk
@@ -17,6 +17,7 @@
 LOCAL_CFLAGS += -D__HOST__
 
 include $(LLVM_HOST_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(LLVM_GEN_INTRINSICS_MK)
 include $(BUILD_HOST_STATIC_LIBRARY)
 
@@ -30,6 +31,7 @@
 LOCAL_SRC_FILES := $(bitcode_reader_3_0_SRC_FILES)
 
 include $(LLVM_DEVICE_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(LLVM_GEN_INTRINSICS_MK)
 include $(BUILD_STATIC_LIBRARY)
 endif
diff --git a/bcinfo/BitReader_3_0/BitcodeReader.cpp b/bcinfo/BitReader_3_0/BitcodeReader.cpp
index 0d1262c..2274d28 100644
--- a/bcinfo/BitReader_3_0/BitcodeReader.cpp
+++ b/bcinfo/BitReader_3_0/BitcodeReader.cpp
@@ -224,8 +224,7 @@
       IRBuilder<> Builder(Context);
       Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstInsertionPt());
 
-      Value *PersFn = Sel->getArgOperand(1);
-      LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0);
+      LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, 0);
       Value *LPExn = Builder.CreateExtractValue(LPI, 0);
       Value *LPSel = Builder.CreateExtractValue(LPI, 1);
       Builder.CreateStore(LPExn, ExnSlot);
@@ -250,7 +249,7 @@
       std::pair<Value*,Value*> ExnSelSlots = FnToLPadSlotMap[Parent->getParent()];
 
       IRBuilder<> Builder(Context);
-      Builder.SetInsertPoint(Parent, Exn);
+      Builder.SetInsertPoint(Parent, Exn->getIterator());
       LoadInst *LPExn = Builder.CreateLoad(ExnSelSlots.first, "exn.load");
       LoadInst *LPSel = Builder.CreateLoad(ExnSelSlots.second, "sel.load");
 
@@ -275,7 +274,7 @@
       BasicBlock *BB = Resume->getParent();
 
       IRBuilder<> Builder(Context);
-      Builder.SetInsertPoint(BB, Resume);
+      Builder.SetInsertPoint(BB, Resume->getIterator());
 
       Value *LPadVal =
         Builder.CreateInsertValue(UndefValue::get(LPadSlotTy),
@@ -285,7 +284,7 @@
       Builder.CreateResume(LPadVal);
 
       // Remove all instructions after the 'resume.'
-      BasicBlock::iterator I = Resume;
+      BasicBlock::iterator I = Resume->getIterator();
       while (I != BB->end()) {
         Instruction *Inst = &*I++;
         Inst->eraseFromParent();
@@ -432,7 +431,7 @@
   std::unique_ptr<MemoryBuffer> Buffer;
   std::unique_ptr<BitstreamReader> StreamFile;
   BitstreamCursor Stream;
-  DataStreamer *LazyStreamer;
+  std::unique_ptr<DataStreamer> LazyStreamer;
   uint64_t NextUnreadBit;
   bool SeenValueSymbolTable;
 
@@ -498,11 +497,11 @@
 
   void releaseBuffer();
 
-  bool isDematerializable(const GlobalValue *GV) const override;
+  bool isDematerializable(const GlobalValue *GV) const;
   std::error_code materialize(GlobalValue *GV) override;
-  std::error_code materializeModule(Module *M) override;
+  std::error_code materializeModule() override;
   std::vector<StructType *> getIdentifiedStructTypes() const override;
-  void dematerialize(GlobalValue *GV) override;
+  void dematerialize(GlobalValue *GV);
 
   /// @brief Main interface to parsing a bitcode buffer.
   /// @returns true if an error occurred.
@@ -2286,15 +2285,15 @@
   for (Module::iterator FI = TheModule->begin(), FE = TheModule->end();
        FI != FE; ++FI) {
     Function *NewFn;
-    if (UpgradeIntrinsicFunction(FI, NewFn))
-      UpgradedIntrinsics.push_back(std::make_pair(FI, NewFn));
+    if (UpgradeIntrinsicFunction(&*FI, NewFn))
+      UpgradedIntrinsics.push_back(std::make_pair(&*FI, NewFn));
   }
 
   // Look for global variables which need to be renamed.
   for (Module::global_iterator
          GI = TheModule->global_begin(), GE = TheModule->global_end();
-       GI != GE;) {
-    GlobalVariable *GV = GI++;
+       GI != GE; GI++) {
+    GlobalVariable *GV = &*GI;
     UpgradeGlobalVariable(GV);
   }
 
@@ -2559,7 +2558,7 @@
     }
     // ALIAS: [alias type, aliasee val#, linkage]
     // ALIAS: [alias type, aliasee val#, linkage, visibility]
-    case bitc::MODULE_CODE_ALIAS: {
+    case bitc::MODULE_CODE_ALIAS_OLD: {
       if (Record.size() < 3)
         return Error("Invalid record");
       Type *Ty = getTypeByID(Record[0]);
@@ -2570,7 +2569,8 @@
         return Error("Invalid type for value");
 
       auto *NewGA =
-          GlobalAlias::create(PTy, getDecodedLinkage(Record[2]), "", TheModule);
+          GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
+                              getDecodedLinkage(Record[2]), "", TheModule);
       // Old bitcode files didn't have visibility field.
       if (Record.size() > 3)
         NewGA->setVisibility(GetDecodedVisibility(Record[3]));
@@ -2800,7 +2800,7 @@
 
   // Add all the function arguments to the value table.
   for(Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
-    ValueList.push_back(I);
+    ValueList.push_back(&*I);
 
   unsigned NextValueNo = ValueList.size();
   BasicBlock *CurBB = nullptr;
@@ -3295,12 +3295,8 @@
       // Replace 'unwind' with 'landingpad' and 'resume'.
       Type *ExnTy = StructType::get(Type::getInt8PtrTy(Context),
                                     Type::getInt32Ty(Context), nullptr);
-      Constant *PersFn =
-        F->getParent()->
-        getOrInsertFunction("__gcc_personality_v0",
-                          FunctionType::get(Type::getInt32Ty(Context), true));
 
-      LandingPadInst *LP = LandingPadInst::Create(ExnTy, PersFn, 1);
+      LandingPadInst *LP = LandingPadInst::Create(ExnTy, 1);
       LP->setCleanup(true);
 
       CurBB->getInstList().push_back(LP);
@@ -3333,7 +3329,7 @@
       break;
     }
 
-    case bitc::FUNC_CODE_INST_LANDINGPAD: {
+    case bitc::FUNC_CODE_INST_LANDINGPAD_OLD: {
       // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
       unsigned Idx = 0;
       if (Record.size() < 4)
@@ -3347,7 +3343,7 @@
 
       bool IsCleanup = !!Record[Idx++];
       unsigned NumClauses = Record[Idx++];
-      LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
+      LandingPadInst *LP = LandingPadInst::Create(Ty, NumClauses);
       LP->setCleanup(IsCleanup);
       for (unsigned J = 0; J != NumClauses; ++J) {
         LandingPadInst::ClauseType CT =
@@ -3696,14 +3692,12 @@
   F->setIsMaterializable(true);
 }
 
-std::error_code BitcodeReader::materializeModule(Module *M) {
-  assert(M == TheModule &&
-         "Can only Materialize the Module this BitcodeReader is attached to.");
+std::error_code BitcodeReader::materializeModule() {
   // Iterate over the module, deserializing any functions that are still on
   // disk.
   for (Module::iterator F = TheModule->begin(), E = TheModule->end();
        F != E; ++F) {
-    if (std::error_code EC = materialize(F))
+    if (std::error_code EC = materialize(&*F))
       return EC;
   }
   // At this point, if there are any function bodies, the current bit is
@@ -3732,7 +3726,7 @@
   std::vector<std::pair<Function*, Function*> >().swap(UpgradedIntrinsics);
 
   // Upgrade to new EH scheme. N.B. This will go away in 3.1.
-  UpgradeExceptionHandling(M);
+  UpgradeExceptionHandling(TheModule);
 
   // Check debug info intrinsics.
   CheckDebugInfoIntrinsics(TheModule);
@@ -3772,7 +3766,8 @@
 std::error_code BitcodeReader::InitLazyStream() {
   // Check and strip off the bitcode wrapper; BitstreamReader expects never to
   // see it.
-  auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(LazyStreamer);
+  auto OwnedBytes = llvm::make_unique<StreamingMemoryObject>(
+      std::move(LazyStreamer));
   StreamingMemoryObject &Bytes = *OwnedBytes;
   StreamFile = llvm::make_unique<BitstreamReader>(std::move(OwnedBytes));
   Stream.init(&*StreamFile);
@@ -3866,7 +3861,7 @@
     return ModuleOrErr;
   Module *M = ModuleOrErr.get();
   // Read in the entire module, and destroy the BitcodeReader.
-  if (std::error_code EC = M->materializeAllPermanently()) {
+  if (std::error_code EC = M->materializeAll()) {
     delete M;
     return EC;
   }
diff --git a/bcinfo/MetadataExtractor.cpp b/bcinfo/MetadataExtractor.cpp
index 500dd5a..ba0548f 100644
--- a/bcinfo/MetadataExtractor.cpp
+++ b/bcinfo/MetadataExtractor.cpp
@@ -602,6 +602,7 @@
   }
 
   std::unique_ptr<llvm::LLVMContext> mContext;
+  bool shouldNullModule = false;
 
   if (!mModule) {
     mContext.reset(new llvm::LLVMContext());
@@ -610,15 +611,16 @@
         llvm::StringRef(mBitcode, mBitcodeSize), "", false));
     std::string error;
 
-    // Module ownership is handled by the context, so we don't need to free it.
-    llvm::ErrorOr<llvm::Module* > errval = llvm::parseBitcodeFile(MEM.get()->getMemBufferRef(),
-                                                                  *mContext);
+    llvm::ErrorOr<std::unique_ptr<llvm::Module> > errval =
+        llvm::parseBitcodeFile(MEM.get()->getMemBufferRef(), *mContext);
     if (std::error_code ec = errval.getError()) {
         ALOGE("Could not parse bitcode file");
         ALOGE("%s", ec.message().c_str());
         return false;
     }
-    mModule = errval.get();
+
+    mModule = errval.get().release();
+    shouldNullModule = true;
   }
 
   const llvm::NamedMDNode *ExportVarMetadata =
@@ -645,31 +647,31 @@
   if (!populateNameMetadata(ExportVarMetadata, mExportVarNameList,
                             mExportVarCount)) {
     ALOGE("Could not populate export variable metadata");
-    return false;
+    goto err;
   }
 
   if (!populateNameMetadata(ExportFuncMetadata, mExportFuncNameList,
                             mExportFuncCount)) {
     ALOGE("Could not populate export function metadata");
-    return false;
+    goto err;
   }
 
   if (!populateForEachMetadata(ExportForEachNameMetadata,
                                ExportForEachMetadata)) {
     ALOGE("Could not populate ForEach signature metadata");
-    return false;
+    goto err;
   }
 
   if (!populateReduceNewMetadata(ExportReduceNewMetadata)) {
     ALOGE("Could not populate export general reduction metadata");
-    return false;
+    goto err;
   }
 
   populatePragmaMetadata(PragmaMetadata);
 
   if (!populateObjectSlotMetadata(ObjectSlotMetadata)) {
     ALOGE("Could not populate object slot metadata");
-    return false;
+    goto err;
   }
 
   readThreadableFlag(ThreadableMetadata);
@@ -677,7 +679,16 @@
 
   mHasDebugInfo = DebugInfoMetadata != nullptr;
 
+  if (shouldNullModule) {
+    mModule = nullptr;
+  }
   return true;
+
+err:
+  if (shouldNullModule) {
+    mModule = nullptr;
+  }
+  return false;
 }
 
 }  // namespace bcinfo
diff --git a/bcinfo/tools/Android.mk b/bcinfo/tools/Android.mk
index 5781696..876a53c 100644
--- a/bcinfo/tools/Android.mk
+++ b/bcinfo/tools/Android.mk
@@ -17,12 +17,14 @@
 LOCAL_PATH := $(call my-dir)
 
 LLVM_ROOT_PATH := external/llvm
+include $(LLVM_ROOT_PATH)/llvm.mk
 
 # Executable for host
 # ========================================================
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := bcinfo
+LOCAL_MODULE_CLASS := EXECUTABLES
 
 LOCAL_SRC_FILES := \
   main.cpp
@@ -43,6 +45,7 @@
 
 LOCAL_LDLIBS = -ldl -lpthread
 
-include $(LLVM_ROOT_PATH)/llvm-host-build.mk
+include $(LLVM_HOST_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(BUILD_HOST_EXECUTABLE)
 
diff --git a/bcinfo/tools/main.cpp b/bcinfo/tools/main.cpp
index 855199d..b823bf9 100644
--- a/bcinfo/tools/main.cpp
+++ b/bcinfo/tools/main.cpp
@@ -365,11 +365,12 @@
         inFile.c_str(), false);
 
     std::unique_ptr<llvm::Module> module;
-    llvm::ErrorOr<llvm::Module*> moduleOrError = llvm::parseBitcodeFile(mem.get()->getMemBufferRef(), ctx);
+    llvm::ErrorOr<std::unique_ptr<llvm::Module> > moduleOrError =
+        llvm::parseBitcodeFile(mem.get()->getMemBufferRef(), ctx);
     std::error_code ec = moduleOrError.getError();
     if (!ec) {
-        module.reset(moduleOrError.get());
-        ec = module->materializeAllPermanently();
+        module = std::move(moduleOrError.get());
+        ec = module->materializeAll();
     }
     std::string errmsg;
     if (ec) {
diff --git a/include/bcc/Source.h b/include/bcc/Source.h
index e6bef2e..ad337c9 100644
--- a/include/bcc/Source.h
+++ b/include/bcc/Source.h
@@ -23,6 +23,10 @@
   class Module;
 }
 
+namespace bcinfo {
+  class MetadataExtractor;
+}
+
 namespace bcc {
 
 class BCCContext;
@@ -33,9 +37,15 @@
   BCCContext &mContext;
   llvm::Module *mModule;
 
+  bcinfo::MetadataExtractor *mMetadata;
+
   // If true, destructor won't destroy the mModule.
   bool mNoDelete;
 
+  // Keep track of whether mModule is destroyed (possibly as a consequence of
+  // getting linked with a different llvm::Module).
+  bool mIsModuleDestroyed;
+
 private:
   Source(const char* name, BCCContext &pContext, llvm::Module &pModule,
          bool pNoDelete = false);
@@ -86,6 +96,14 @@
   // for presence of debug info in the module.
   bool getDebugInfoEnabled() const;
 
+  // Extract metadata from mModule using MetadataExtractor.
+  bool extractMetadata();
+  bcinfo::MetadataExtractor* getMetadata() const { return mMetadata; }
+
+  // Mark mModule was destroyed in the process of linking with a different
+  // llvm::Module
+  void markModuleDestroyed() { mIsModuleDestroyed = true; }
+
   ~Source();
 };
 
diff --git a/include/bcinfo/MetadataExtractor.h b/include/bcinfo/MetadataExtractor.h
index fcaaee8..71a6de0 100644
--- a/include/bcinfo/MetadataExtractor.h
+++ b/include/bcinfo/MetadataExtractor.h
@@ -18,6 +18,8 @@
 #define __ANDROID_BCINFO_METADATAEXTRACTOR_H__
 
 #include <cstddef>
+#include <memory>
+
 #include <stdint.h>
 
 namespace llvm {
diff --git a/lib/Core/Android.mk b/lib/Core/Android.mk
index e880b5c..d48fd0e 100644
--- a/lib/Core/Android.mk
+++ b/lib/Core/Android.mk
@@ -41,6 +41,7 @@
 
 include $(LIBBCC_DEVICE_BUILD_MK)
 include $(LLVM_DEVICE_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(LLVM_GEN_INTRINSICS_MK)
 include $(BUILD_STATIC_LIBRARY)
 endif
@@ -59,5 +60,6 @@
 
 include $(LIBBCC_HOST_BUILD_MK)
 include $(LLVM_HOST_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(LLVM_GEN_INTRINSICS_MK)
 include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/lib/Core/Compiler.cpp b/lib/Core/Compiler.cpp
index 1dae382..4062294 100644
--- a/lib/Core/Compiler.cpp
+++ b/lib/Core/Compiler.cpp
@@ -237,8 +237,8 @@
   }
 
   const std::string &triple = module.getTargetTriple();
-  const llvm::DataLayout *dl = getTargetMachine().getDataLayout();
-  unsigned int pointerSize = dl->getPointerSizeInBits();
+  const llvm::DataLayout dl = getTargetMachine().createDataLayout();
+  unsigned int pointerSize = dl.getPointerSizeInBits();
   if (triple == "armv7-none-linux-gnueabi") {
     if (pointerSize != 32) {
       return kErrInvalidSource;
@@ -252,15 +252,15 @@
   }
 
   // Sanitize module's target information.
-  module.setTargetTriple(getTargetMachine().getTargetTriple());
-  module.setDataLayout(*getTargetMachine().getDataLayout());
+  module.setTargetTriple(getTargetMachine().getTargetTriple().str());
+  module.setDataLayout(getTargetMachine().createDataLayout());
 
   // Materialize the bitcode module.
   if (module.getMaterializer() != nullptr) {
     // A module with non-null materializer means that it is a lazy-load module.
-    // Materialize it now via invoking MaterializeAllPermanently(). This
-    // function returns false when the materialization is successful.
-    std::error_code ec = module.materializeAllPermanently();
+    // Materialize it now.  This function returns false when the materialization
+    // is successful.
+    std::error_code ec = module.materializeAll();
     if (ec) {
       ALOGE("Failed to materialize the module `%s'! (%s)",
             module.getModuleIdentifier().c_str(), ec.message().c_str());
diff --git a/lib/Core/Source.cpp b/lib/Core/Source.cpp
index 25b263f..d4c0e74 100644
--- a/lib/Core/Source.cpp
+++ b/lib/Core/Source.cpp
@@ -18,6 +18,7 @@
 
 #include <new>
 
+#include <llvm/ADT/STLExtras.h>
 #include <llvm/Bitcode/ReaderWriter.h>
 #include <llvm/IR/LLVMContext.h>
 #include <llvm/IR/Module.h>
@@ -29,6 +30,8 @@
 #include "bcc/BCCContext.h"
 #include "bcc/Support/Log.h"
 
+#include "bcinfo/MetadataExtractor.h"
+
 #include "BCCContextImpl.h"
 
 namespace {
@@ -37,15 +40,16 @@
 // reduce the startup time. On success, return the LLVM module object created
 // and take the ownership of input memory buffer (i.e., pInput). On error,
 // return nullptr and will NOT take the ownership of pInput.
-static inline llvm::Module *helper_load_bitcode(llvm::LLVMContext &pContext,
+static inline std::unique_ptr<llvm::Module> helper_load_bitcode(llvm::LLVMContext &pContext,
                                                 std::unique_ptr<llvm::MemoryBuffer> &&pInput) {
-  llvm::ErrorOr<llvm::Module *> moduleOrError = llvm::getLazyBitcodeModule(std::move(pInput), pContext);
+  llvm::ErrorOr<std::unique_ptr<llvm::Module> > moduleOrError
+      = llvm::getLazyBitcodeModule(std::move(pInput), pContext);
   if (std::error_code ec = moduleOrError.getError()) {
     ALOGE("Unable to parse the given bitcode file `%s'! (%s)",
           pInput->getBufferIdentifier(), ec.message().c_str());
   }
 
-  return moduleOrError.get();
+  return std::move(moduleOrError.get());
 }
 
 } // end anonymous namespace
@@ -70,8 +74,12 @@
     return nullptr;
   }
 
-  llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
-                                             std::move(input_memory));
+  auto managedModule = helper_load_bitcode(pContext.mImpl->mLLVMContext,
+                                           std::move(input_memory));
+
+  // Release the managed llvm::Module* since this object gets deleted either in
+  // the error check below or in ~Source() (since pNoDelete is false).
+  llvm::Module *module = managedModule.release();
   if (module == nullptr) {
     return nullptr;
   }
@@ -96,8 +104,12 @@
   std::unique_ptr<llvm::MemoryBuffer> input_data = std::move(mb_or_error.get());
 
   std::unique_ptr<llvm::MemoryBuffer> input_memory(input_data.release());
-  llvm::Module *module = helper_load_bitcode(pContext.mImpl->mLLVMContext,
-                                             std::move(input_memory));
+  auto managedModule = helper_load_bitcode(pContext.mImpl->mLLVMContext,
+                                           std::move(input_memory));
+
+  // Release the managed llvm::Module* since this object gets deleted either in
+  // the error check below or in ~Source() (since pNoDelete is false).
+  llvm::Module *module = managedModule.release();
   if (module == nullptr) {
     return nullptr;
   }
@@ -114,6 +126,7 @@
                                  bool pNoDelete) {
   std::string ErrorInfo;
   llvm::raw_string_ostream ErrorStream(ErrorInfo);
+  pModule.materializeAll();
   if (llvm::verifyModule(pModule, &ErrorStream)) {
     ALOGE("Bitcode of RenderScript module does not pass verification: `%s'!",
           ErrorStream.str().c_str());
@@ -130,23 +143,27 @@
 
 Source::Source(const char* name, BCCContext &pContext, llvm::Module &pModule,
                bool pNoDelete)
-    : mName(name), mContext(pContext), mModule(&pModule), mNoDelete(pNoDelete) {
+    : mName(name), mContext(pContext), mModule(&pModule), mMetadata(nullptr),
+      mNoDelete(pNoDelete), mIsModuleDestroyed(false) {
     pContext.addSource(*this);
 }
 
 Source::~Source() {
   mContext.removeSource(*this);
-  if (!mNoDelete)
+  if (!mNoDelete && !mIsModuleDestroyed)
     delete mModule;
+  delete mMetadata;
 }
 
 bool Source::merge(Source &pSource) {
   // TODO(srhines): Add back logging of actual diagnostics from linking.
-  if (llvm::Linker::LinkModules(mModule, &pSource.getModule()) != 0) {
+  if (llvm::Linker::linkModules(*mModule, std::unique_ptr<llvm::Module>(&pSource.getModule())) != 0) {
     ALOGE("Failed to link source `%s' with `%s'!",
           getIdentifier().c_str(), pSource.getIdentifier().c_str());
     return false;
   }
+  // pSource.getModule() is destroyed after linking.
+  pSource.markModuleDestroyed();
 
   return true;
 }
@@ -185,4 +202,9 @@
   return mModule->getNamedMetadata("llvm.dbg.cu") != nullptr;
 }
 
+bool Source::extractMetadata() {
+  mMetadata = new bcinfo::MetadataExtractor(mModule);
+  return mMetadata->extract();
+}
+
 } // namespace bcc
diff --git a/lib/Renderscript/Android.mk b/lib/Renderscript/Android.mk
index 32e65b3..b406be7 100644
--- a/lib/Renderscript/Android.mk
+++ b/lib/Renderscript/Android.mk
@@ -49,6 +49,8 @@
 
 include $(LIBBCC_DEVICE_BUILD_MK)
 include $(LLVM_DEVICE_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
+include $(LLVM_GEN_INTRINSICS_MK)
 include $(BUILD_STATIC_LIBRARY)
 endif
 
@@ -67,4 +69,6 @@
 
 include $(LIBBCC_HOST_BUILD_MK)
 include $(LLVM_HOST_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
+include $(LLVM_GEN_INTRINSICS_MK)
 include $(BUILD_HOST_STATIC_LIBRARY)
diff --git a/lib/Renderscript/RSAddDebugInfoPass.cpp b/lib/Renderscript/RSAddDebugInfoPass.cpp
index 2823fae..27aa5de 100644
--- a/lib/Renderscript/RSAddDebugInfoPass.cpp
+++ b/lib/Renderscript/RSAddDebugInfoPass.cpp
@@ -145,7 +145,7 @@
           llvm::dwarf::DW_ATE_unsigned);
 
     // Capture the expanded kernel type debug info.
-    kernelTypeMD = DebugInfo.createSubroutineType(sourceFileName, kernelPrototypeMD);
+    kernelTypeMD = DebugInfo.createSubroutineType(kernelPrototypeMD);
   }
 
   /// @brief Add debug information to a generated function.
@@ -159,7 +159,7 @@
   void attachDebugInfo(llvm::DIBuilder &DebugInfo, llvm::Function &Func) {
     // Lookup the current thread coordinate variable.
     llvm::AllocaInst* indexVar = nullptr;
-    for (llvm::Instruction &inst : llvm::inst_range(Func)) {
+    for (llvm::Instruction &inst : llvm::instructions(Func)) {
       if (auto *allocaInst = llvm::dyn_cast<llvm::AllocaInst>(&inst)) {
         if (allocaInst->getName() == bcc::BCC_INDEX_VAR_NAME) {
           indexVar = allocaInst;
@@ -173,11 +173,11 @@
         sourceFileName, // scope
         Func.getName(), Func.getName(),
         sourceFileName, 1, kernelTypeMD,
-        false, true, 1, 0, false, &Func
+        false, true, 1, 0, false
     );
 
     // IRBuilder for allocating variables for arguments.
-    llvm::IRBuilder<> ir(Func.getEntryBlock().begin());
+    llvm::IRBuilder<> ir(&*Func.getEntryBlock().begin());
 
     // Walk through the argument list and expanded function prototype
     // debuginfo in lockstep to create debug entries for
@@ -190,11 +190,11 @@
         break;
 
       // Create debuginfo entry for the argument and advance.
-      llvm::DILocalVariable *argVarDI = DebugInfo.createLocalVariable(
-          llvm::dwarf::DW_TAG_arg_variable,
-          ExpandedFunc, arg.getName(), sourceFileName, 1,
+      llvm::DILocalVariable *argVarDI = DebugInfo.createParameterVariable(
+          ExpandedFunc, arg.getName(), argIdx, sourceFileName, 1,
           llvm::cast<llvm::DIType>(argTypes->getOperand(argIdx).get()),
-          true, 0, argIdx);
+          true, 0
+      );
 
       // Annotate the argument variable in the IR.
       llvm::AllocaInst *argVar =
@@ -212,9 +212,10 @@
     // Annotate the index variable with metadata.
     if (indexVar) {
       // Debug information for loop index variable.
-      llvm::DILocalVariable *indexVarDI = DebugInfo.createLocalVariable(
-          llvm::dwarf::DW_TAG_auto_variable, ExpandedFunc,
-          bcc::BCC_INDEX_VAR_NAME, sourceFileName, 1, indexVarType, true);
+      llvm::DILocalVariable *indexVarDI = DebugInfo.createAutoVariable(
+          ExpandedFunc, bcc::BCC_INDEX_VAR_NAME, sourceFileName, 1,
+          indexVarType, true
+      );
 
       // Insert declaration annotation in the instruction stream.
       llvm::Instruction *decl = DebugInfo.insertDeclare(
@@ -224,7 +225,7 @@
     }
 
     // Attach location information to each instruction in the function.
-    for (llvm::Instruction &inst : llvm::inst_range(Func)) {
+    for (llvm::Instruction &inst : llvm::instructions(Func)) {
       inst.setDebugLoc(llvm::DebugLoc::get(1, 1, ExpandedFunc));
     }
   }
diff --git a/lib/Renderscript/RSCompilerDriver.cpp b/lib/Renderscript/RSCompilerDriver.cpp
index fc69d50..d851187 100644
--- a/lib/Renderscript/RSCompilerDriver.cpp
+++ b/lib/Renderscript/RSCompilerDriver.cpp
@@ -16,6 +16,7 @@
 
 #include "bcc/Renderscript/RSCompilerDriver.h"
 
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/IR/AssemblyAnnotationWriter.h"
 #include <llvm/IR/Module.h>
 #include "llvm/Linker/Linker.h"
@@ -299,6 +300,16 @@
     const std::list<std::string>& fused,
     const std::list<std::list<std::pair<int, int>>>& invokes,
     const std::list<std::string>& invokeBatchNames) {
+
+  // Read and store metadata before linking the modules together
+  std::vector<bcinfo::MetadataExtractor*> metadata;
+  for (Source* source : sources) {
+    if (!source->extractMetadata()) {
+      ALOGE("Cannot extract metadata from module");
+      return false;
+    }
+  }
+
   // ---------------------------------------------------------------------------
   // Link all input modules into a single module
   // ---------------------------------------------------------------------------
@@ -306,12 +317,15 @@
   llvm::LLVMContext& context = Context.getLLVMContext();
   llvm::Module module("Merged Script Group", context);
 
-  llvm::Linker linker(&module);
+  llvm::Linker linker(module);
   for (Source* source : sources) {
-    if (linker.linkInModule(&source->getModule())) {
+    std::unique_ptr<llvm::Module> sourceModule(&source->getModule());
+    if (linker.linkInModule(std::move(sourceModule))) {
       ALOGE("Linking for module in source failed.");
       return false;
     }
+    // source->getModule() is destroyed after linking.
+    source->markModuleDestroyed();
   }
 
   // ---------------------------------------------------------------------------
diff --git a/lib/Renderscript/RSKernelExpand.cpp b/lib/Renderscript/RSKernelExpand.cpp
index 3514524..8bf5a75 100644
--- a/lib/Renderscript/RSKernelExpand.cpp
+++ b/lib/Renderscript/RSKernelExpand.cpp
@@ -450,7 +450,7 @@
     llvm::Value *Cond, *IVNext, *IV, *IVVar;
 
     CondBB = Builder.GetInsertBlock();
-    AfterBB = llvm::SplitBlock(CondBB, Builder.GetInsertPoint(), nullptr, nullptr);
+    AfterBB = llvm::SplitBlock(CondBB, &*Builder.GetInsertPoint(), nullptr, nullptr);
     HeaderBB = llvm::BasicBlock::Create(*Context, "Loop", CondBB->getParent());
 
     CondBB->getTerminator()->eraseFromParent();
@@ -796,7 +796,7 @@
     llvm::Value *OutStep = nullptr;
 
     // Construct the actual function body.
-    llvm::IRBuilder<> Builder(ExpandedFunction->getEntryBlock().begin());
+    llvm::IRBuilder<> Builder(&*ExpandedFunction->getEntryBlock().begin());
 
     // Collect and construct the arguments for the kernel().
     // Note that we load any loop-invariant arguments before entering the Loop.
@@ -923,7 +923,7 @@
     // Arg_outstep is not used by expanded new-style forEach kernels.
 
     // Construct the actual function body.
-    llvm::IRBuilder<> Builder(ExpandedFunction->getEntryBlock().begin());
+    llvm::IRBuilder<> Builder(&*ExpandedFunction->getEntryBlock().begin());
 
     // Create TBAA meta-data.
     llvm::MDNode *TBAARenderScriptDistinct, *TBAARenderScript,
@@ -1203,7 +1203,7 @@
     bccAssert(DL.getTypeAllocSize(InTy) == DL.getTypeAllocSize(OutTy));
 
     // Construct the actual function body.
-    llvm::IRBuilder<> Builder(ExpandedFunction->getEntryBlock().begin());
+    llvm::IRBuilder<> Builder(&*ExpandedFunction->getEntryBlock().begin());
 
     // Cast input and output buffers to appropriate types.
     llvm::Value *InBuf = Builder.CreatePointerCast(Arg_inBuf, InPtrTy);
@@ -1399,7 +1399,7 @@
     llvm::Value *Arg_accum = &*(ExpandedAccumulatorArgIter++);
 
     // Construct the actual function body.
-    llvm::IRBuilder<> Builder(FnExpandedAccumulator->getEntryBlock().begin());
+    llvm::IRBuilder<> Builder(&*FnExpandedAccumulator->getEntryBlock().begin());
 
     // Create the loop structure.
     llvm::BasicBlock *LoopHeader = Builder.GetInsertBlock();
diff --git a/lib/Renderscript/RSScriptGroupFusion.cpp b/lib/Renderscript/RSScriptGroupFusion.cpp
index bb18540..20a4b86 100644
--- a/lib/Renderscript/RSScriptGroupFusion.cpp
+++ b/lib/Renderscript/RSScriptGroupFusion.cpp
@@ -38,13 +38,8 @@
 
 const Function* getInvokeFunction(const Source& source, const int slot,
                                   Module* newModule) {
-  Module* module = const_cast<Module*>(&source.getModule());
-  bcinfo::MetadataExtractor metadata(module);
-  if (!metadata.extract()) {
-    ALOGE("Kernel fusion (module %s slot %d): failed to extract metadata",
-          source.getName().c_str(), slot);
-    return nullptr;
-  }
+
+  bcinfo::MetadataExtractor &metadata = *source.getMetadata();
   const char* functionName = metadata.getExportFuncNameList()[slot];
   Function* func = newModule->getFunction(functionName);
   // Materialize the function so that later the caller can inspect its argument
@@ -56,9 +51,8 @@
 const Function*
 getFunction(Module* mergedModule, const Source* source, const int slot,
             uint32_t* signature) {
-  bcinfo::MetadataExtractor metadata(&source->getModule());
-  metadata.extract();
 
+  bcinfo::MetadataExtractor &metadata = *source->getMetadata();
   const char* functionName = metadata.getExportForEachNameList()[slot];
   if (functionName == nullptr || !functionName[0]) {
     ALOGE("Kernel fusion (module %s slot %d): failed to find kernel function",
@@ -103,8 +97,7 @@
   auto slotIter = slots.begin();
   for (const Source* source : sources) {
     const int slot = *slotIter++;
-    bcinfo::MetadataExtractor metadata(&source->getModule());
-    metadata.extract();
+    bcinfo::MetadataExtractor &metadata = *source->getMetadata();
 
     if (metadata.getExportForEachInputCountList()[slot] > 1) {
       ALOGE("Kernel fusion (module %s slot %d): cannot handle multiple inputs",
@@ -208,25 +201,25 @@
 
   llvm::Value* dataElement = nullptr;
   if (bcinfo::MetadataExtractor::hasForEachSignatureIn(fusedFunctionSignature)) {
-    dataElement = argIter++;
+    dataElement = &*(argIter++);
     dataElement->setName("DataIn");
   }
 
   llvm::Value* X = nullptr;
   if (bcinfo::MetadataExtractor::hasForEachSignatureX(fusedFunctionSignature)) {
-    X = argIter++;
+    X = &*(argIter++);
     X->setName("x");
   }
 
   llvm::Value* Y = nullptr;
   if (bcinfo::MetadataExtractor::hasForEachSignatureY(fusedFunctionSignature)) {
-    Y = argIter++;
+    Y = &*(argIter++);
     Y->setName("y");
   }
 
   llvm::Value* Z = nullptr;
   if (bcinfo::MetadataExtractor::hasForEachSignatureZ(fusedFunctionSignature)) {
-    Z = argIter++;
+    Z = &*(argIter++);
     Z->setName("z");
   }
 
@@ -345,7 +338,7 @@
   llvm::IRBuilder<> builder(block);
 
   llvm::Function::arg_iterator argIter = newF->arg_begin();
-  llvm::Value* arg1 = argIter++;
+  llvm::Value* arg1 = &*(argIter++);
   builder.CreateCall((llvm::Value*)F, arg1);
 
   builder.CreateRetVoid();
diff --git a/lib/Renderscript/RSStubsWhiteList.cpp b/lib/Renderscript/RSStubsWhiteList.cpp
index 8099e5e..3bbf58e 100644
--- a/lib/Renderscript/RSStubsWhiteList.cpp
+++ b/lib/Renderscript/RSStubsWhiteList.cpp
@@ -423,6 +423,7 @@
 "_Z13convert_char4Dv4_m",
 "_Z13convert_char4Dv4_s",
 "_Z13convert_char4Dv4_t",
+"_Z13convert_half2Dv2_Dh",
 "_Z13convert_half2Dv2_c",
 "_Z13convert_half2Dv2_d",
 "_Z13convert_half2Dv2_f",
@@ -433,6 +434,7 @@
 "_Z13convert_half2Dv2_m",
 "_Z13convert_half2Dv2_s",
 "_Z13convert_half2Dv2_t",
+"_Z13convert_half3Dv3_Dh",
 "_Z13convert_half3Dv3_c",
 "_Z13convert_half3Dv3_d",
 "_Z13convert_half3Dv3_f",
@@ -443,6 +445,7 @@
 "_Z13convert_half3Dv3_m",
 "_Z13convert_half3Dv3_s",
 "_Z13convert_half3Dv3_t",
+"_Z13convert_half4Dv4_Dh",
 "_Z13convert_half4Dv4_c",
 "_Z13convert_half4Dv4_d",
 "_Z13convert_half4Dv4_f",
@@ -813,8 +816,12 @@
 "_Z15convert_ushort4Dv4_m",
 "_Z15convert_ushort4Dv4_s",
 "_Z15convert_ushort4Dv4_t",
+"_Z15native_distanceDhDh",
+"_Z15native_distanceDv2_DhS_",
 "_Z15native_distanceDv2_fS_",
+"_Z15native_distanceDv3_DhS_",
 "_Z15native_distanceDv3_fS_",
+"_Z15native_distanceDv4_DhS_",
 "_Z15native_distanceDv4_fS_",
 "_Z15native_distanceff",
 "_Z15rsCreateElement12rs_data_type",
diff --git a/lib/Renderscript/RSX86CallConvPass.cpp b/lib/Renderscript/RSX86CallConvPass.cpp
index 19c1d9d..5642d7b 100644
--- a/lib/Renderscript/RSX86CallConvPass.cpp
+++ b/lib/Renderscript/RSX86CallConvPass.cpp
@@ -134,7 +134,7 @@
     for (auto AI=OrigFn->arg_begin(), AE=OrigFn->arg_end(),
               NAI=NewFn->arg_begin();
          AI != AE; ++ AI, ++NAI) {
-      NAI->takeName(AI);
+      NAI->takeName(&*AI);
     }
 
     return NewFn;
diff --git a/lib/Support/CompilerConfig.cpp b/lib/Support/CompilerConfig.cpp
index d2b3740..a58b496 100644
--- a/lib/Support/CompilerConfig.cpp
+++ b/lib/Support/CompilerConfig.cpp
@@ -50,11 +50,6 @@
 CompilerConfig::CompilerConfig(const std::string &pTriple)
   : mTriple(pTriple), mFullPrecision(true), mTarget(nullptr) {
   //===--------------------------------------------------------------------===//
-  // Default setting of register sheduler
-  //===--------------------------------------------------------------------===//
-  llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
-
-  //===--------------------------------------------------------------------===//
   // Default setting of target options
   //===--------------------------------------------------------------------===//
 
@@ -150,6 +145,14 @@
     if (features.count("fp16") && features["fp16"])
       attributes.push_back("+fp16");
 
+#if defined(PROVIDE_ARM64_CODEGEN)
+    // On AArch64, asimd in /proc/cpuinfo signals the presence of hardware
+    // half-precision conversion instructions.  getHostCPUFeatures translates
+    // this to "neon".  If PROVIDE_ARM64_CODEGEN is set, enable "+fp16" for ARM
+    // codegen if "neon" is present in features.
+    if (features.count("neon") && features["neon"])
+      attributes.push_back("+fp16");
+#endif // PROVIDE_ARM64_CODEGEN
 
 #if defined(TARGET_BUILD)
     if (!getProperty("debug.rs.arm-no-tune-for-cpu")) {
diff --git a/lib/Support/Initialization.cpp b/lib/Support/Initialization.cpp
index 16447f6..0149435 100644
--- a/lib/Support/Initialization.cpp
+++ b/lib/Support/Initialization.cpp
@@ -58,7 +58,6 @@
   llvm::initializeVectorization(Registry);
   llvm::initializeIPO(Registry);
   llvm::initializeAnalysis(Registry);
-  llvm::initializeIPA(Registry);
   llvm::initializeTransformUtils(Registry);
   llvm::initializeInstCombine(Registry);
   llvm::initializeInstrumentation(Registry);
diff --git a/tools/bcc/Main.cpp b/tools/bcc/Main.cpp
index 37da1bc..941b49c 100644
--- a/tools/bcc/Main.cpp
+++ b/tools/bcc/Main.cpp
@@ -348,7 +348,7 @@
       return EXIT_FAILURE;
     }
 
-    RSScript *s = new (std::nothrow) RSScript(*source, RSCD.getConfig());
+    std::unique_ptr<RSScript> s(new (std::nothrow) RSScript(*source, RSCD.getConfig()));
     if (s == nullptr) {
       llvm::errs() << "Out of memory when creating script for file `"
                    << OptInputFilenames[0] << "'!\n";
diff --git a/tools/bcc_compat/Main.cpp b/tools/bcc_compat/Main.cpp
index 7766f51..7dfc3de 100644
--- a/tools/bcc_compat/Main.cpp
+++ b/tools/bcc_compat/Main.cpp
@@ -274,8 +274,7 @@
     return EXIT_FAILURE;
   }
 
-  RSScript *s = nullptr;
-  s = PrepareRSScript(context, OptInputFilenames);
+  std::unique_ptr<RSScript> s(PrepareRSScript(context, OptInputFilenames));
   if (!rscd.buildForCompatLib(*s, OutputFilename.c_str(), nullptr, OptRuntimePath.c_str(), false)) {
     fprintf(stderr, "Failed to compile script!");
     return EXIT_FAILURE;
diff --git a/tools/bcc_strip_attr/Android.mk b/tools/bcc_strip_attr/Android.mk
index b83a5e0..1158f4e 100644
--- a/tools/bcc_strip_attr/Android.mk
+++ b/tools/bcc_strip_attr/Android.mk
@@ -35,4 +35,5 @@
 
 include $(LIBBCC_HOST_BUILD_MK)
 include $(LLVM_HOST_BUILD_MK)
+include $(LLVM_GEN_ATTRIBUTES_MK)
 include $(BUILD_HOST_EXECUTABLE)