Updates for LLVM merge to r171906 on 20130108.

Change-Id: I096cb90103b19e3110ea562d60e5eb8ad48d9b67
diff --git a/bcinfo/BitReader_2_7/BitReader_2_7.h b/bcinfo/BitReader_2_7/BitReader_2_7.h
index e76e66d..48a0bd3 100644
--- a/bcinfo/BitReader_2_7/BitReader_2_7.h
+++ b/bcinfo/BitReader_2_7/BitReader_2_7.h
@@ -16,9 +16,9 @@
 #define BITREADER_2_7_H
 
 #include "llvm/GVMaterializer.h"
-#include "llvm/Attributes.h"
-#include "llvm/Type.h"
-#include "llvm/OperandTraits.h"
+#include "llvm/IR/Attributes.h"
+#include "llvm/IR/Type.h"
+#include "llvm/IR/OperandTraits.h"
 #include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Bitcode/LLVMBitCodes.h"
 #include "llvm/Support/ValueHandle.h"
diff --git a/bcinfo/BitReader_2_7/BitcodeReader.cpp b/bcinfo/BitReader_2_7/BitcodeReader.cpp
index 299e679..24c7525 100644
--- a/bcinfo/BitReader_2_7/BitcodeReader.cpp
+++ b/bcinfo/BitReader_2_7/BitcodeReader.cpp
@@ -11,22 +11,22 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "BitcodeReader.h"
 #include "BitReader_2_7.h"
 
 #include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/Constants.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/InlineAsm.h"
-#include "llvm/IntrinsicInst.h"
-#include "llvm/Module.h"
-#include "llvm/Operator.h"
-#include "llvm/AutoUpgrade.h"
+#include "BitcodeReader.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/AutoUpgrade.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/InlineAsm.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/OperandTraits.h"
+#include "llvm/IR/Operator.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MemoryBuffer.h"
-#include "llvm/OperandTraits.h"
 
 using namespace llvm;
 using namespace llvm_2_7;
@@ -57,25 +57,25 @@
         cast<CallInst>(FuncStart->use_back())->eraseFromParent();
       FuncStart->eraseFromParent();
     }
-    
+
     if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
       while (!StopPoint->use_empty())
         cast<CallInst>(StopPoint->use_back())->eraseFromParent();
       StopPoint->eraseFromParent();
     }
-  
+
     if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
       while (!RegionStart->use_empty())
         cast<CallInst>(RegionStart->use_back())->eraseFromParent();
       RegionStart->eraseFromParent();
     }
-  
+
     if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
       while (!RegionEnd->use_empty())
         cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
       RegionEnd->eraseFromParent();
     }
-    
+
     if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
       if (!Declare->use_empty()) {
         DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
@@ -100,7 +100,7 @@
   ValueList.clear();
   MDValueList.clear();
 
-  std::vector<AttrListPtr>().swap(MAttributes);
+  std::vector<AttributeSet>().swap(MAttributes);
   std::vector<BasicBlock*>().swap(FunctionBBs);
   std::vector<Function*>().swap(FunctionsWithBodies);
   DeferredFunctionInfo.clear();
@@ -432,20 +432,20 @@
   // The type table size is always specified correctly.
   if (ID >= TypeList.size())
     return 0;
-  
+
   if (Type *Ty = TypeList[ID])
     return Ty;
 
   // If we have a forward reference, the only possible case is when it is to a
   // named struct.  Just create a placeholder for now.
-  return TypeList[ID] = StructType::create(Context, "");
+  return TypeList[ID] = StructType::create(Context);
 }
 
 /// FIXME: Remove in LLVM 3.1, only used by ParseOldTypeTable.
 Type *BitcodeReader::getTypeByIDOrNull(unsigned ID) {
   if (ID >= TypeList.size())
     TypeList.resize(ID+1);
-  
+
   return TypeList[ID];
 }
 
@@ -496,6 +496,11 @@
       if (Record.size() & 1)
         return Error("Invalid ENTRY record");
 
+      for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
+        Attribute ReconstitutedAttr =
+          Attribute::decodeLLVMAttributesForBitcode(Context, Record[i+1]);
+        Record[i+1] = ReconstitutedAttr.getBitMask();
+#if 0
       // FIXME : Remove this autoupgrade code in LLVM 3.0.
       // If Function attributes are using index 0 then transfer them
       // to index ~0. Index 0 is used for return value attributes but used to be
@@ -536,21 +541,17 @@
 
         FnAttribute  |= RetAttribute & OldRetAttrs;
         RetAttribute &= ~OldRetAttrs;
+#endif
       }
 
       for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
-        if (Record[i] == 0) {
-          if (RetAttribute != Attribute::None)
-            Attrs.push_back(AttributeWithIndex::get(0, RetAttribute));
-        } else if (Record[i] == ~0U) {
-          if (FnAttribute != Attribute::None)
-            Attrs.push_back(AttributeWithIndex::get(~0U, FnAttribute));
-        } else if (Attributes(Record[i+1]) != Attribute::None)
+        AttrBuilder B(Record[i+1]);
+        if (B.hasAttributes())
           Attrs.push_back(AttributeWithIndex::get(Record[i],
-                                                  Attributes(Record[i+1])));
+                                                  Attribute::get(Context, B)));
       }
 
-      MAttributes.push_back(AttrListPtr::get(Attrs));
+      MAttributes.push_back(AttributeSet::get(Context, Attrs));
       Attrs.clear();
       break;
     }
@@ -561,7 +562,7 @@
 bool BitcodeReader::ParseTypeTable() {
   if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_NEW))
     return Error("Malformed block record");
-  
+
   return ParseTypeTableBody();
 }
 
@@ -573,7 +574,7 @@
   unsigned NumRecords = 0;
 
   SmallString<64> TypeName;
-  
+
   // Read all the records for this type table.
   while (1) {
     unsigned Code = Stream.ReadCode();
@@ -656,18 +657,18 @@
       break;
     }
     case bitc::TYPE_CODE_FUNCTION_OLD: {
-      // FIXME: attrid is dead, remove it in LLVM 3.0
+      // FIXME: attrid is dead, remove it in LLVM 4.0
       // FUNCTION: [vararg, attrid, retty, paramty x N]
       if (Record.size() < 3)
         return Error("Invalid FUNCTION type record");
-      std::vector<Type*> ArgTys;
+      SmallVector<Type*, 8> ArgTys;
       for (unsigned i = 3, e = Record.size(); i != e; ++i) {
         if (Type *T = getTypeByID(Record[i]))
           ArgTys.push_back(T);
         else
           break;
       }
-      
+
       ResultTy = getTypeByID(Record[2]);
       if (ResultTy == 0 || ArgTys.size() < Record.size()-3)
         return Error("invalid type in function type");
@@ -678,7 +679,7 @@
     case bitc::TYPE_CODE_STRUCT_ANON: {  // STRUCT: [ispacked, eltty x N]
       if (Record.size() < 1)
         return Error("Invalid STRUCT type record");
-      std::vector<Type*> EltTys;
+      SmallVector<Type*, 8> EltTys;
       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
         if (Type *T = getTypeByID(Record[i]))
           EltTys.push_back(T);
@@ -698,10 +699,10 @@
     case bitc::TYPE_CODE_STRUCT_NAMED: { // STRUCT: [ispacked, eltty x N]
       if (Record.size() < 1)
         return Error("Invalid STRUCT type record");
-      
+
       if (NumRecords >= TypeList.size())
         return Error("invalid TYPE table");
-      
+
       // Check to see if this was forward referenced, if so fill in the temp.
       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
       if (Res) {
@@ -710,7 +711,7 @@
       } else  // Otherwise, create a new struct.
         Res = StructType::create(Context, TypeName);
       TypeName.clear();
-      
+
       SmallVector<Type*, 8> EltTys;
       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
         if (Type *T = getTypeByID(Record[i]))
@@ -730,7 +731,7 @@
 
       if (NumRecords >= TypeList.size())
         return Error("invalid TYPE table");
-      
+
       // Check to see if this was forward referenced, if so fill in the temp.
       StructType *Res = cast_or_null<StructType>(TypeList[NumRecords]);
       if (Res) {
@@ -741,7 +742,7 @@
       TypeName.clear();
       ResultTy = Res;
       break;
-    }        
+    }
     case bitc::TYPE_CODE_ARRAY:     // ARRAY: [numelts, eltty]
       if (Record.size() < 2)
         return Error("Invalid ARRAY type record");
@@ -775,43 +776,43 @@
 
   if (!TypeList.empty())
     return Error("Multiple TYPE_BLOCKs found!");
-  
-  
+
+
   // While horrible, we have no good ordering of types in the bc file.  Just
   // iteratively parse types out of the bc file in multiple passes until we get
   // them all.  Do this by saving a cursor for the start of the type block.
   BitstreamCursor StartOfTypeBlockCursor(Stream);
-  
+
   unsigned NumTypesRead = 0;
-  
+
   SmallVector<uint64_t, 64> Record;
 RestartScan:
   unsigned NextTypeID = 0;
   bool ReadAnyTypes = false;
-  
+
   // Read all the records for this type table.
   while (1) {
     unsigned Code = Stream.ReadCode();
     if (Code == bitc::END_BLOCK) {
       if (NextTypeID != TypeList.size())
         return Error("Invalid type forward reference in TYPE_BLOCK_ID_OLD");
-      
+
       // If we haven't read all of the types yet, iterate again.
       if (NumTypesRead != TypeList.size()) {
         // If we didn't successfully read any types in this pass, then we must
         // have an unhandled forward reference.
         if (!ReadAnyTypes)
           return Error("Obsolete bitcode contains unhandled recursive type");
-        
+
         Stream = StartOfTypeBlockCursor;
         goto RestartScan;
       }
-      
+
       if (Stream.ReadBlockEnd())
         return Error("Error at end of type table block");
       return false;
     }
-    
+
     if (Code == bitc::ENTER_SUBBLOCK) {
       // No known subblocks, always skip them.
       Stream.ReadSubBlockID();
@@ -819,12 +820,12 @@
         return Error("Malformed block record");
       continue;
     }
-    
+
     if (Code == bitc::DEFINE_ABBREV) {
       Stream.ReadAbbrevRecord();
       continue;
     }
-    
+
     // Read a record.
     Record.clear();
     Type *ResultTy = 0;
@@ -894,7 +895,7 @@
 
       if (EltTys.size() != Record.size()-1)
         break;      // Not all elements are ready.
-      
+
       cast<StructType>(TypeList[NextTypeID])->setBody(EltTys, Record[0]);
       ResultTy = TypeList[NextTypeID];
       TypeList[NextTypeID] = 0;
@@ -942,17 +943,17 @@
         ResultTy = VectorType::get(ResultTy, Record[0]);
       break;
     }
-    
+
     if (NextTypeID >= TypeList.size())
       return Error("invalid TYPE table");
-    
+
     if (ResultTy && TypeList[NextTypeID] == 0) {
       ++NumTypesRead;
       ReadAnyTypes = true;
-      
+
       TypeList[NextTypeID] = ResultTy;
     }
-    
+
     ++NextTypeID;
   }
 }
@@ -1190,7 +1191,7 @@
       unsigned Kind = Record[0];
       for (unsigned i = 1; i != RecordLength; ++i)
         Name[i-1] = Record[i];
-      
+
       unsigned NewKind = TheModule->getMDKindID(Name.str());
       if (!MDKindMap.insert(std::make_pair(Kind, NewKind)).second)
         return Error("Conflicting METADATA_KIND records");
@@ -1341,7 +1342,7 @@
         return Error("Invalid CST_AGGREGATE record");
 
       unsigned Size = Record.size();
-      std::vector<Constant*> Elts;
+      SmallVector<Constant*, 16> Elts;
 
       if (StructType *STy = dyn_cast<StructType>(CurTy)) {
         for (unsigned i = 0; i != Size; ++i)
@@ -1547,7 +1548,7 @@
       Function *Fn =
         dyn_cast_or_null<Function>(ValueList.getConstantFwdRef(Record[1],FnTy));
       if (Fn == 0) return Error("Invalid CE_BLOCKADDRESS record");
-      
+
       GlobalVariable *FwdRef = new GlobalVariable(*Fn->getParent(),
                                                   Type::getInt8Ty(Context),
                                             false, GlobalValue::InternalLinkage,
@@ -1555,7 +1556,7 @@
       BlockAddrFwdRefs[Fn].push_back(std::make_pair(Record[2], FwdRef));
       V = FwdRef;
       break;
-    }  
+    }
     }
 
     ValueList.AssignValue(V, NextCstNo);
@@ -1700,13 +1701,14 @@
     // Read a record.
     switch (Stream.ReadRecord(Code, Record)) {
     default: break;  // Default behavior, ignore unknown content.
-    case bitc::MODULE_CODE_VERSION:  // VERSION: [version#]
+    case bitc::MODULE_CODE_VERSION: {  // VERSION: [version#]
       if (Record.size() < 1)
         return Error("Malformed MODULE_CODE_VERSION");
       // Only version #0 is supported so far.
       if (Record[0] != 0)
         return Error("Unknown bitstream version!");
       break;
+    }
     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
       std::string S;
       if (ConvertToString(Record, 0, S))
@@ -1732,7 +1734,8 @@
       std::string S;
       if (ConvertToString(Record, 0, S))
         return Error("Invalid MODULE_CODE_DEPLIB record");
-      TheModule->addLibrary(S);
+      // ANDROID: Ignore value, since we never used it anyways.
+      // TheModule->addLibrary(S);
       break;
     }
     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
@@ -1921,7 +1924,7 @@
       // ignore these final 4 bytes :-(
       if (Stream.GetAbbrevIDWidth() == 2 && Code == 2 &&
           Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
-	  Stream.AtEndOfStream())
+          Stream.AtEndOfStream())
         return false;
 
       return Error("Invalid record at top-level");
@@ -2122,7 +2125,7 @@
   unsigned CurBBNo = 0;
 
   DebugLoc LastLoc;
-  
+
   // Read all the records.
   SmallVector<uint64_t, 64> Record;
   while (1) {
@@ -2177,24 +2180,24 @@
         FunctionBBs[i] = BasicBlock::Create(Context, "", F);
       CurBB = FunctionBBs[0];
       continue;
-        
+
     case bitc::FUNC_CODE_DEBUG_LOC_AGAIN:  // DEBUG_LOC_AGAIN
       // This record indicates that the last instruction is at the same
       // location as the previous instruction with a location.
       I = 0;
-        
+
       // Get the last instruction emitted.
       if (CurBB && !CurBB->empty())
         I = &CurBB->back();
       else if (CurBBNo && FunctionBBs[CurBBNo-1] &&
                !FunctionBBs[CurBBNo-1]->empty())
         I = &FunctionBBs[CurBBNo-1]->back();
-        
+
       if (I == 0) return Error("Invalid DEBUG_LOC_AGAIN record");
       I->setDebugLoc(LastLoc);
       I = 0;
       continue;
-        
+
     case FUNC_CODE_DEBUG_LOC_2_7:
       LLVM2_7MetadataDetected = true;
     case bitc::FUNC_CODE_DEBUG_LOC: {      // DEBUG_LOC: [line, col, scope, ia]
@@ -2206,10 +2209,10 @@
         I = &FunctionBBs[CurBBNo-1]->back();
       if (I == 0 || Record.size() < 4)
         return Error("Invalid FUNC_CODE_DEBUG_LOC record");
-      
+
       unsigned Line = Record[0], Col = Record[1];
       unsigned ScopeID = Record[2], IAID = Record[3];
-      
+
       MDNode *Scope = 0, *IA = 0;
       if (ScopeID) Scope = cast<MDNode>(MDValueList.getValueFwdRef(ScopeID-1));
       if (IAID)    IA = cast<MDNode>(MDValueList.getValueFwdRef(IAID-1));
@@ -2533,11 +2536,11 @@
       I = IBI;
       break;
     }
-        
+
     case bitc::FUNC_CODE_INST_INVOKE: {
       // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
       if (Record.size() < 4) return Error("Invalid INVOKE record");
-      AttrListPtr PAL = getAttributes(Record[0]);
+      AttributeSet PAL = getAttributes(Record[0]);
       unsigned CCInfo = Record[1];
       BasicBlock *NormalBB = getBasicBlock(Record[2]);
       BasicBlock *UnwindBB = getBasicBlock(Record[3]);
@@ -2717,7 +2720,7 @@
       if (Record.size() < 3)
         return Error("Invalid CALL record");
 
-      AttrListPtr PAL = getAttributes(Record[0]);
+      AttributeSet PAL = getAttributes(Record[0]);
       unsigned CCInfo = Record[1];
 
       unsigned OpNum = 2;
@@ -2822,12 +2825,12 @@
       unsigned BlockIdx = RefList[i].first;
       if (BlockIdx >= FunctionBBs.size())
         return Error("Invalid blockaddress block #");
-    
+
       GlobalVariable *FwdRef = RefList[i].second;
       FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
       FwdRef->eraseFromParent();
     }
-    
+
     BlockAddrFwdRefs.erase(BAFRI);
   }
 
diff --git a/bcinfo/BitReader_2_7/BitcodeReader.h b/bcinfo/BitReader_2_7/BitcodeReader.h
index 033aad1..f29d485 100644
--- a/bcinfo/BitReader_2_7/BitcodeReader.h
+++ b/bcinfo/BitReader_2_7/BitcodeReader.h
@@ -14,14 +14,14 @@
 #ifndef BITCODE_READER_H
 #define BITCODE_READER_H
 
-#include "llvm/GVMaterializer.h"
-#include "llvm/Attributes.h"
-#include "llvm/Type.h"
-#include "llvm/OperandTraits.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Bitcode/LLVMBitCodes.h"
+#include "llvm/GVMaterializer.h"
+#include "llvm/IR/Attributes.h"
+#include "llvm/IR/OperandTraits.h"
+#include "llvm/IR/Type.h"
 #include "llvm/Support/ValueHandle.h"
-#include "llvm/ADT/DenseMap.h"
 #include <vector>
 
 namespace llvm {
@@ -39,7 +39,7 @@
 
 class BitcodeReaderValueList {
   std::vector<WeakVH> ValuePtrs;
-  
+
   /// ResolveConstants - As we resolve forward-referenced constants, we add
   /// information about them to this vector.  This allows us to resolve them in
   /// bulk instead of resolving each reference at a time.  See the code in
@@ -62,17 +62,17 @@
   void push_back(Value *V) {
     ValuePtrs.push_back(V);
   }
-  
+
   void clear() {
     assert(ResolveConstants.empty() && "Constants not resolved?");
     ValuePtrs.clear();
   }
-  
+
   Value *operator[](unsigned i) const {
     assert(i < ValuePtrs.size());
     return ValuePtrs[i];
   }
-  
+
   Value *back() const { return ValuePtrs.back(); }
     void pop_back() { ValuePtrs.pop_back(); }
   bool empty() const { return ValuePtrs.empty(); }
@@ -80,12 +80,12 @@
     assert(N <= size() && "Invalid shrinkTo request!");
     ValuePtrs.resize(N);
   }
-  
+
   Constant *getConstantFwdRef(unsigned Idx, Type *Ty);
   Value *getValueFwdRef(unsigned Idx, Type *Ty);
-  
+
   void AssignValue(Value *V, unsigned Idx);
-  
+
   /// ResolveConstantForwardRefs - Once all constants are read, this method bulk
   /// resolves any forward references.
   void ResolveConstantForwardRefs();
@@ -98,7 +98,7 @@
 
 class BitcodeReaderMDValueList {
   std::vector<WeakVH> MDValuePtrs;
-  
+
   LLVMContext &Context;
 public:
   BitcodeReaderMDValueList(LLVMContext& C) : Context(C) {}
@@ -111,12 +111,12 @@
   Value *back() const         { return MDValuePtrs.back(); }
   void pop_back()             { MDValuePtrs.pop_back(); }
   bool empty() const          { return MDValuePtrs.empty(); }
-  
+
   Value *operator[](unsigned i) const {
     assert(i < MDValuePtrs.size());
     return MDValuePtrs[i];
   }
-  
+
   void shrinkTo(unsigned N) {
     assert(N <= size() && "Invalid shrinkTo request!");
     MDValuePtrs.resize(N);
@@ -135,7 +135,7 @@
   BitstreamCursor Stream;
   
   const char *ErrorString;
-  
+
   std::vector<Type*> TypeList;
   BitcodeReaderValueList ValueList;
   BitcodeReaderMDValueList MDValueList;
@@ -143,16 +143,16 @@
 
   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
-  
+
   /// MAttributes - The set of attributes by index.  Index zero in the
   /// file is for null, and is thus not represented here.  As such all indices
   /// are off by one.
-  std::vector<AttrListPtr> MAttributes;
-  
+  std::vector<AttributeSet> MAttributes;
+
   /// FunctionBBs - While parsing a function body, this is a list of the basic
   /// blocks for the function.
   std::vector<BasicBlock*> FunctionBBs;
-  
+
   // When reading the module header, this list is populated with functions that
   // have bodies later in the file.
   std::vector<Function*> FunctionsWithBodies;
@@ -164,7 +164,7 @@
 
   // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
   DenseMap<unsigned, unsigned> MDKindMap;
-  
+
   // After the module header has been read, the FunctionsWithBodies list is 
   // reversed.  This keeps track of whether we've done this yet.
   bool HasReversedFunctionsWithBodies;
@@ -173,7 +173,7 @@
   /// map contains info about where to find deferred function body in the
   /// stream.
   DenseMap<Function*, uint64_t> DeferredFunctionInfo;
-  
+
   /// BlockAddrFwdRefs - These are blockaddr references to basic blocks.  These
   /// are resolved lazily when functions are loaded.
   typedef std::pair<unsigned, GlobalVariable*> BlockAddrRefTy;
@@ -195,13 +195,13 @@
   ~BitcodeReader() {
     FreeState();
   }
-  
+
   void FreeState();
-  
+
   /// setBufferOwned - If this is true, the reader will destroy the MemoryBuffer
   /// when the reader is destroyed.
   void setBufferOwned(bool Owned) { BufferOwned = Owned; }
-  
+
   virtual bool isMaterializable(const GlobalValue *GV) const;
   virtual bool isDematerializable(const GlobalValue *GV) const;
   virtual bool Materialize(GlobalValue *GV, std::string *ErrInfo = 0);
@@ -213,7 +213,7 @@
     return true;
   }
   const char *getErrorString() const { return ErrorString; }
-  
+
   /// @brief Main interface to parsing a bitcode buffer.
   /// @returns true if an error occurred.
   bool ParseBitcodeInto(Module *M);
@@ -233,12 +233,12 @@
     if (ID >= FunctionBBs.size()) return 0; // Invalid ID
     return FunctionBBs[ID];
   }
-  AttrListPtr getAttributes(unsigned i) const {
+  AttributeSet getAttributes(unsigned i) const {
     if (i-1 < MAttributes.size())
       return MAttributes[i-1];
-    return AttrListPtr();
+    return AttributeSet();
   }
-  
+
   /// getValueTypePair - Read a value/type pair out of the specified record from
   /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
   /// Return true on failure.
@@ -254,7 +254,7 @@
     } else if (Slot == Record.size()) {
       return true;
     }
-    
+
     unsigned TypeNo = (unsigned)Record[Slot++];
     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
     return ResVal == 0;
@@ -284,7 +284,7 @@
   bool ParseMetadataAttachment();
   bool ParseModuleTriple(std::string &Triple);
 };
-  
+
 } // End llvm namespace
 
 #endif
diff --git a/bcinfo/BitReader_3_0/BitReader_3_0.h b/bcinfo/BitReader_3_0/BitReader_3_0.h
index 2eb4d72..ae26374 100644
--- a/bcinfo/BitReader_3_0/BitReader_3_0.h
+++ b/bcinfo/BitReader_3_0/BitReader_3_0.h
@@ -15,9 +15,9 @@
 #define BITREADER_3_0_H
 
 #include "llvm/GVMaterializer.h"
-#include "llvm/Attributes.h"
-#include "llvm/Type.h"
-#include "llvm/OperandTraits.h"
+#include "llvm/IR/Attributes.h"
+#include "llvm/IR/Type.h"
+#include "llvm/IR/OperandTraits.h"
 #include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Bitcode/LLVMBitCodes.h"
 #include "llvm/Support/ValueHandle.h"
diff --git a/bcinfo/BitReader_3_0/BitcodeReader.cpp b/bcinfo/BitReader_3_0/BitcodeReader.cpp
index de6bf50..c06971f 100644
--- a/bcinfo/BitReader_3_0/BitcodeReader.cpp
+++ b/bcinfo/BitReader_3_0/BitcodeReader.cpp
@@ -14,21 +14,21 @@
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "BitcodeReader.h"
 #include "BitReader_3_0.h"
-#include "llvm/Constants.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/InlineAsm.h"
-#include "llvm/IntrinsicInst.h"
-#include "llvm/IRBuilder.h"
-#include "llvm/Module.h"
-#include "llvm/Operator.h"
-#include "llvm/AutoUpgrade.h"
-#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/AutoUpgrade.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/InlineAsm.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/OperandTraits.h"
+#include "llvm/IR/Operator.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/MemoryBuffer.h"
-#include "llvm/OperandTraits.h"
 using namespace llvm;
 using namespace llvm_3_0;
 
@@ -341,7 +341,7 @@
   ValueList.clear();
   MDValueList.clear();
 
-  std::vector<AttrListPtr>().swap(MAttributes);
+  std::vector<AttributeSet>().swap(MAttributes);
   std::vector<BasicBlock*>().swap(FunctionBBs);
   std::vector<Function*>().swap(FunctionsWithBodies);
   DeferredFunctionInfo.clear();
@@ -775,6 +775,11 @@
       if (Record.size() & 1)
         return Error("Invalid ENTRY record");
 
+      for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
+        Attribute ReconstitutedAttr =
+          Attribute::decodeLLVMAttributesForBitcode(Context, Record[i+1]);
+        Record[i+1] = ReconstitutedAttr.getBitMask();
+#if 0
       // FIXME : Remove this autoupgrade code in LLVM 3.0.
       // If Function attributes are using index 0 then transfer them
       // to index ~0. Index 0 is used for return value attributes but used to be
@@ -815,21 +820,17 @@
 
         FnAttribute  |= RetAttribute & OldRetAttrs;
         RetAttribute &= ~OldRetAttrs;
+#endif
       }
 
       for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
-        if (Record[i] == 0) {
-          if (RetAttribute != Attribute::None)
-            Attrs.push_back(AttributeWithIndex::get(0, RetAttribute));
-        } else if (Record[i] == ~0U) {
-          if (FnAttribute != Attribute::None)
-            Attrs.push_back(AttributeWithIndex::get(~0U, FnAttribute));
-        } else if (Attributes(Record[i+1]) != Attribute::None)
+        AttrBuilder B(Record[i+1]);
+        if (B.hasAttributes())
           Attrs.push_back(AttributeWithIndex::get(Record[i],
-                                                  Attributes(Record[i+1])));
+                                                  Attribute::get(Context, B)));
       }
 
-      MAttributes.push_back(AttrListPtr::get(Attrs));
+      MAttributes.push_back(AttributeSet::get(Context, Attrs));
       Attrs.clear();
       break;
     }
@@ -935,11 +936,11 @@
       break;
     }
     case bitc::TYPE_CODE_FUNCTION_OLD: {
-      // FIXME: attrid is dead, remove it in LLVM 3.0
+      // FIXME: attrid is dead, remove it in LLVM 4.0
       // FUNCTION: [vararg, attrid, retty, paramty x N]
       if (Record.size() < 3)
         return Error("Invalid FUNCTION type record");
-      std::vector<Type*> ArgTys;
+      SmallVector<Type*, 8> ArgTys;
       for (unsigned i = 3, e = Record.size(); i != e; ++i) {
         if (Type *T = getTypeByID(Record[i]))
           ArgTys.push_back(T);
@@ -976,7 +977,7 @@
     case bitc::TYPE_CODE_STRUCT_ANON: {  // STRUCT: [ispacked, eltty x N]
       if (Record.size() < 1)
         return Error("Invalid STRUCT type record");
-      std::vector<Type*> EltTys;
+      SmallVector<Type*, 8> EltTys;
       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
         if (Type *T = getTypeByID(Record[i]))
           EltTys.push_back(T);
@@ -1169,7 +1170,7 @@
       break;
     case bitc::TYPE_CODE_OPAQUE:    // OPAQUE
       if (NextTypeID < TypeList.size() && TypeList[NextTypeID] == 0)
-        ResultTy = StructType::create(Context);
+        ResultTy = StructType::create(Context, "");
       break;
     case TYPE_CODE_STRUCT_OLD_3_0: {// STRUCT_OLD
       if (NextTypeID >= TypeList.size()) break;
@@ -1180,7 +1181,7 @@
 
       // Set a type.
       if (TypeList[NextTypeID] == 0)
-        TypeList[NextTypeID] = StructType::create(Context);
+        TypeList[NextTypeID] = StructType::create(Context, "");
 
       std::vector<Type*> EltTys;
       for (unsigned i = 1, e = Record.size(); i != e; ++i) {
@@ -1641,7 +1642,7 @@
         return Error("Invalid CST_AGGREGATE record");
 
       unsigned Size = Record.size();
-      std::vector<Constant*> Elts;
+      SmallVector<Constant*, 16> Elts;
 
       if (StructType *STy = dyn_cast<StructType>(CurTy)) {
         for (unsigned i = 0; i != Size; ++i)
@@ -1998,13 +1999,14 @@
     // Read a record.
     switch (Stream.ReadRecord(Code, Record)) {
     default: break;  // Default behavior, ignore unknown content.
-    case bitc::MODULE_CODE_VERSION:  // VERSION: [version#]
+    case bitc::MODULE_CODE_VERSION: {  // VERSION: [version#]
       if (Record.size() < 1)
         return Error("Malformed MODULE_CODE_VERSION");
       // Only version #0 is supported so far.
       if (Record[0] != 0)
         return Error("Unknown bitstream version!");
       break;
+    }
     case bitc::MODULE_CODE_TRIPLE: {  // TRIPLE: [strchr x N]
       std::string S;
       if (ConvertToString(Record, 0, S))
@@ -2030,7 +2032,8 @@
       std::string S;
       if (ConvertToString(Record, 0, S))
         return Error("Invalid MODULE_CODE_DEPLIB record");
-      TheModule->addLibrary(S);
+      // ANDROID: Ignore value, since we never used it anyways.
+      // TheModule->addLibrary(S);
       break;
     }
     case bitc::MODULE_CODE_SECTIONNAME: {  // SECTIONNAME: [strchr x N]
@@ -2219,7 +2222,7 @@
       // have to read and ignore these final 4 bytes :-(
       if (Stream.GetAbbrevIDWidth() == 2 && Code == 2 &&
           Stream.Read(6) == 2 && Stream.Read(24) == 0xa0a0a &&
-	  Stream.AtEndOfStream())
+          Stream.AtEndOfStream())
         return false;
 
       return Error("Invalid record at top-level");
@@ -2818,7 +2821,7 @@
     case bitc::FUNC_CODE_INST_INVOKE: {
       // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
       if (Record.size() < 4) return Error("Invalid INVOKE record");
-      AttrListPtr PAL = getAttributes(Record[0]);
+      AttributeSet PAL = getAttributes(Record[0]);
       unsigned CCInfo = Record[1];
       BasicBlock *NormalBB = getBasicBlock(Record[2]);
       BasicBlock *UnwindBB = getBasicBlock(Record[3]);
@@ -3093,7 +3096,7 @@
       if (Record.size() < 3)
         return Error("Invalid CALL record");
 
-      AttrListPtr PAL = getAttributes(Record[0]);
+      AttributeSet PAL = getAttributes(Record[0]);
       unsigned CCInfo = Record[1];
 
       unsigned OpNum = 2;
diff --git a/bcinfo/BitReader_3_0/BitcodeReader.h b/bcinfo/BitReader_3_0/BitcodeReader.h
index 7dcd331..743f310 100644
--- a/bcinfo/BitReader_3_0/BitcodeReader.h
+++ b/bcinfo/BitReader_3_0/BitcodeReader.h
@@ -14,14 +14,14 @@
 #ifndef BITCODE_READER_H
 #define BITCODE_READER_H
 
-#include "llvm/GVMaterializer.h"
-#include "llvm/Attributes.h"
-#include "llvm/Type.h"
-#include "llvm/OperandTraits.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Bitcode/LLVMBitCodes.h"
+#include "llvm/GVMaterializer.h"
+#include "llvm/IR/Attributes.h"
+#include "llvm/IR/OperandTraits.h"
+#include "llvm/IR/Type.h"
 #include "llvm/Support/ValueHandle.h"
-#include "llvm/ADT/DenseMap.h"
 #include <vector>
 
 namespace llvm {
@@ -147,7 +147,7 @@
   /// MAttributes - The set of attributes by index.  Index zero in the
   /// file is for null, and is thus not represented here.  As such all indices
   /// are off by one.
-  std::vector<AttrListPtr> MAttributes;
+  std::vector<AttributeSet> MAttributes;
 
   /// FunctionBBs - While parsing a function body, this is a list of the basic
   /// blocks for the function.
@@ -226,10 +226,10 @@
     if (ID >= FunctionBBs.size()) return 0; // Invalid ID
     return FunctionBBs[ID];
   }
-  AttrListPtr getAttributes(unsigned i) const {
+  AttributeSet getAttributes(unsigned i) const {
     if (i-1 < MAttributes.size())
       return MAttributes[i-1];
-    return AttrListPtr();
+    return AttributeSet();
   }
 
   /// getValueTypePair - Read a value/type pair out of the specified record from
diff --git a/bcinfo/BitcodeTranslator.cpp b/bcinfo/BitcodeTranslator.cpp
index a2bf86c..b4755c0 100644
--- a/bcinfo/BitcodeTranslator.cpp
+++ b/bcinfo/BitcodeTranslator.cpp
@@ -27,8 +27,8 @@
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/Bitcode/BitstreamWriter.h"
 #include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/LLVMContext.h"
-#include "llvm/Module.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/raw_ostream.h"
 
diff --git a/bcinfo/MetadataExtractor.cpp b/bcinfo/MetadataExtractor.cpp
index e4a2573..c4b2965 100644
--- a/bcinfo/MetadataExtractor.cpp
+++ b/bcinfo/MetadataExtractor.cpp
@@ -24,9 +24,9 @@
 
 #include "llvm/ADT/OwningPtr.h"
 #include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/Constants.h"
-#include "llvm/LLVMContext.h"
-#include "llvm/Module.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
 #include "llvm/Support/MemoryBuffer.h"
 
 #include <cstdlib>
diff --git a/bcinfo/tools/main.cpp b/bcinfo/tools/main.cpp
index 3405f17..28d29aa 100644
--- a/bcinfo/tools/main.cpp
+++ b/bcinfo/tools/main.cpp
@@ -22,8 +22,8 @@
 #include <llvm/ADT/StringRef.h>
 #include <llvm/Assembly/AssemblyAnnotationWriter.h>
 #include <llvm/Bitcode/ReaderWriter.h>
-#include <llvm/LLVMContext.h>
-#include <llvm/Module.h>
+#include <llvm/IR/LLVMContext.h>
+#include <llvm/IR/Module.h>
 #include <llvm/Support/ManagedStatic.h>
 #include <llvm/Support/MemoryBuffer.h>
 #include <llvm/Support/ToolOutputFile.h>
diff --git a/include/bcc/Compiler.h b/include/bcc/Compiler.h
index 1e2676b..265f1e3 100644
--- a/include/bcc/Compiler.h
+++ b/include/bcc/Compiler.h
@@ -21,7 +21,7 @@
 
 class raw_ostream;
 class PassManager;
-class TargetData;
+class DataLayout;
 class TargetMachine;
 
 } // end namespace llvm
@@ -53,7 +53,7 @@
     kErrCreateTargetMachine,
     kErrSwitchTargetMachine,
     kErrNoTargetMachine,
-    kErrTargetDataNoMemory,
+    kErrDataLayoutNoMemory,
     kErrMaterialization,
     kErrInvalidOutputFileState,
     kErrPrepareOutput,
diff --git a/lib/AndroidBitcode/ABCCompiler.cpp b/lib/AndroidBitcode/ABCCompiler.cpp
index 8393d48..2a6a601 100644
--- a/lib/AndroidBitcode/ABCCompiler.cpp
+++ b/lib/AndroidBitcode/ABCCompiler.cpp
@@ -16,9 +16,9 @@
 
 #include "bcc/AndroidBitcode/ABCCompiler.h"
 
-#include <llvm/Module.h>
+#include <llvm/IR/Module.h>
 #include <llvm/PassManager.h>
-#include <llvm/Target/TargetData.h>
+#include <llvm/IR/DataLayout.h>
 #include <llvm/Target/TargetMachine.h>
 
 #include "bcc/AndroidBitcode/ABCCompilerDriver.h"
@@ -34,14 +34,14 @@
   llvm::PassManager pm;
   llvm::Module &module = pScript.getSource().getModule();
   const llvm::TargetMachine &tm = getTargetMachine();
-  llvm::TargetData *target_data =
-    new (std::nothrow) llvm::TargetData(*(tm.getTargetData()));
+  llvm::DataLayout *data_layout =
+    new (std::nothrow) llvm::DataLayout(*(tm.getDataLayout()));
 
-  if (target_data == NULL) {
+  if (data_layout == NULL) {
     return false;
   }
 
-  pm.add(target_data);
+  pm.add(data_layout);
   pm.add(mDriver.createExpandVAArgPass());
   pm.run(module);
 
diff --git a/lib/AndroidBitcode/ABCCompilerDriver.cpp b/lib/AndroidBitcode/ABCCompilerDriver.cpp
index 1e5e906..8139630 100644
--- a/lib/AndroidBitcode/ABCCompilerDriver.cpp
+++ b/lib/AndroidBitcode/ABCCompilerDriver.cpp
@@ -16,7 +16,7 @@
 
 #include "bcc/AndroidBitcode/ABCCompilerDriver.h"
 
-#include <llvm/Module.h>
+#include <llvm/IR/Module.h>
 #include <llvm/Pass.h>
 #include <llvm/Support/MemoryBuffer.h>
 #include <llvm/Support/raw_ostream.h>
@@ -195,14 +195,6 @@
   mLinker.addObject(const_cast<char *>(input_relocatable.data()),
                     input_relocatable.size());
 
-  // Read dependent library list.
-  const Source &source = pScript.getSource();
-  for (llvm::Module::lib_iterator lib_iter = source.getModule().lib_begin(),
-          lib_end = source.getModule().lib_end(); lib_iter != lib_end;
-       ++lib_iter) {
-    mLinker.addNameSpec(*lib_iter);
-  }
-
   // TODO: Refactor libbcc/runtime/ to libcompilerRT.so and use it.
   mLinker.addNameSpec("bcc");
 
diff --git a/lib/AndroidBitcode/ABCExpandVAArgPass.cpp b/lib/AndroidBitcode/ABCExpandVAArgPass.cpp
index 1f0debf..29e3f2c 100644
--- a/lib/AndroidBitcode/ABCExpandVAArgPass.cpp
+++ b/lib/AndroidBitcode/ABCExpandVAArgPass.cpp
@@ -20,7 +20,7 @@
 #include "bcc/AndroidBitcode/ABCExpandVAArgPass.h"
 
 #include <llvm/ADT/STLExtras.h>
-#include <llvm/Instructions.h>
+#include <llvm/IR/Instructions.h>
 #include <llvm/Support/InstIterator.h>
 
 namespace bcc {
diff --git a/lib/AndroidBitcode/ARM/ARMABCExpandVAArg.cpp b/lib/AndroidBitcode/ARM/ARMABCExpandVAArg.cpp
index 7f154af..2f6a2eb 100644
--- a/lib/AndroidBitcode/ARM/ARMABCExpandVAArg.cpp
+++ b/lib/AndroidBitcode/ARM/ARMABCExpandVAArg.cpp
@@ -15,14 +15,14 @@
  */
 
 #include <llvm/ADT/Triple.h>
-#include <llvm/DerivedTypes.h>
-#include <llvm/Function.h>
-#include <llvm/Instructions.h>
-#include <llvm/IRBuilder.h>
-#include <llvm/Module.h>
+#include <llvm/IR/DerivedTypes.h>
+#include <llvm/IR/Function.h>
+#include <llvm/IR/Instructions.h>
+#include <llvm/IR/IRBuilder.h>
+#include <llvm/IR/Module.h>
 #include <llvm/Pass.h>
-#include <llvm/Type.h>
-#include <llvm/Target/TargetData.h>
+#include <llvm/IR/Type.h>
+#include <llvm/IR/DataLayout.h>
 
 #include "bcc/AndroidBitcode/ABCExpandVAArgPass.h"
 
@@ -43,7 +43,7 @@
     llvm::Type *ty = pty->getContainedType(0);
     llvm::Value *va_list_addr = pInst->getOperand(0);
     llvm::IRBuilder<> builder(pInst);
-    const llvm::TargetData *td = getAnalysisIfAvailable<llvm::TargetData>();
+    const llvm::DataLayout *dl = getAnalysisIfAvailable<llvm::DataLayout>();
 
     llvm::Type *bp = llvm::Type::getInt8PtrTy(*mContext);
     llvm::Type *bpp = bp->getPointerTo(0);
@@ -52,7 +52,7 @@
         builder.CreateBitCast(va_list_addr, bpp, "ap");
     llvm::Value *addr = builder.CreateLoad(va_list_addr_bpp, "ap.cur");
     // Handle address alignment for type alignment > 32 bits.
-    uint64_t ty_align = td->getABITypeAlignment(ty);
+    uint64_t ty_align = dl->getABITypeAlignment(ty);
 
     if (ty_align > 4) {
       assert((ty_align & (ty_align - 1)) == 0 &&
@@ -67,7 +67,7 @@
     }
     llvm::Value *addr_typed = builder.CreateBitCast(addr, pty);
 
-    uint64_t offset = llvm::RoundUpToAlignment(td->getTypeSizeInBits(ty)/8, 4);
+    uint64_t offset = llvm::RoundUpToAlignment(dl->getTypeSizeInBits(ty)/8, 4);
     llvm::Value *next_addr = builder.CreateGEP(addr,
       llvm::ConstantInt::get(llvm::Type::getInt32Ty(*mContext), offset),
       "ap.next");
diff --git a/lib/AndroidBitcode/Mips/MipsABCExpandVAArg.cpp b/lib/AndroidBitcode/Mips/MipsABCExpandVAArg.cpp
index 495cef4..d211ccb 100644
--- a/lib/AndroidBitcode/Mips/MipsABCExpandVAArg.cpp
+++ b/lib/AndroidBitcode/Mips/MipsABCExpandVAArg.cpp
@@ -15,14 +15,14 @@
  */
 
 #include <llvm/ADT/Triple.h>
-#include <llvm/DerivedTypes.h>
-#include <llvm/Function.h>
-#include <llvm/Instructions.h>
-#include <llvm/IRBuilder.h>
-#include <llvm/Module.h>
+#include <llvm/IR/DerivedTypes.h>
+#include <llvm/IR/Function.h>
+#include <llvm/IR/Instructions.h>
+#include <llvm/IR/IRBuilder.h>
+#include <llvm/IR/Module.h>
 #include <llvm/Pass.h>
-#include <llvm/Type.h>
-#include <llvm/Target/TargetData.h>
+#include <llvm/IR/Type.h>
+#include <llvm/IR/DataLayout.h>
 
 #include "bcc/AndroidBitcode/ABCExpandVAArgPass.h"
 
@@ -43,14 +43,14 @@
     llvm::Type *ty = pty->getContainedType(0);
     llvm::Value *va_list_addr = pInst->getOperand(0);
     llvm::IRBuilder<> builder(pInst);
-    const llvm::TargetData *td = getAnalysisIfAvailable<llvm::TargetData>();
+    const llvm::DataLayout *dl = getAnalysisIfAvailable<llvm::DataLayout>();
 
     llvm::Type *bp = llvm::Type::getInt8PtrTy(*mContext);
     llvm::Type *bpp = bp->getPointerTo(0);
     llvm::Value *va_list_addr_bpp = builder.CreateBitCast(va_list_addr,
                                                           bpp, "ap");
     llvm::Value *addr = builder.CreateLoad(va_list_addr_bpp, "ap.cur");
-    int64_t type_align = td->getABITypeAlignment(ty);
+    int64_t type_align = dl->getABITypeAlignment(ty);
     llvm::Value *addr_typed;
     llvm::IntegerType *int_ty = llvm::Type::getInt32Ty(*mContext);
 
@@ -69,7 +69,7 @@
     llvm::Value *aligned_addr = builder.CreateBitCast(addr_typed, bp);
     type_align = std::max((unsigned)type_align, (unsigned) 4);
     uint64_t offset =
-      llvm::RoundUpToAlignment(td->getTypeSizeInBits(ty) / 8, type_align);
+      llvm::RoundUpToAlignment(dl->getTypeSizeInBits(ty) / 8, type_align);
     llvm::Value *next_addr =
       builder.CreateGEP(aligned_addr, llvm::ConstantInt::get(int_ty, offset),
                         "ap.next");
diff --git a/lib/AndroidBitcode/X86/X86ABCExpandVAArg.cpp b/lib/AndroidBitcode/X86/X86ABCExpandVAArg.cpp
index 6d9acbc..64fa358 100644
--- a/lib/AndroidBitcode/X86/X86ABCExpandVAArg.cpp
+++ b/lib/AndroidBitcode/X86/X86ABCExpandVAArg.cpp
@@ -15,14 +15,14 @@
  */
 
 #include <llvm/ADT/Triple.h>
-#include <llvm/DerivedTypes.h>
-#include <llvm/Function.h>
-#include <llvm/Instructions.h>
-#include <llvm/IRBuilder.h>
-#include <llvm/Module.h>
+#include <llvm/IR/DerivedTypes.h>
+#include <llvm/IR/Function.h>
+#include <llvm/IR/Instructions.h>
+#include <llvm/IR/IRBuilder.h>
+#include <llvm/IR/Module.h>
 #include <llvm/Pass.h>
-#include <llvm/Type.h>
-#include <llvm/Target/TargetData.h>
+#include <llvm/IR/Type.h>
+#include <llvm/IR/DataLayout.h>
 
 #include "bcc/AndroidBitcode/ABCExpandVAArgPass.h"
 
@@ -43,7 +43,7 @@
     llvm::Type *ty = pty->getContainedType(0);
     llvm::Value *va_list_addr = pInst->getOperand(0);
     llvm::IRBuilder<> builder(pInst);
-    const llvm::TargetData *td = getAnalysisIfAvailable<llvm::TargetData>();
+    const llvm::DataLayout *dl = getAnalysisIfAvailable<llvm::DataLayout>();
 
     llvm::Type *bp = llvm::Type::getInt8PtrTy(*mContext);
     llvm::Type *bpp = bp->getPointerTo(0);
@@ -54,7 +54,7 @@
     llvm::Value *addr_typed = builder.CreateBitCast(addr, pty);
 
     // X86-32 stack type alignment is always 4.
-    uint64_t offset = llvm::RoundUpToAlignment(td->getTypeSizeInBits(ty)/8, 4);
+    uint64_t offset = llvm::RoundUpToAlignment(dl->getTypeSizeInBits(ty)/8, 4);
     llvm::Value *next_addr = builder.CreateGEP(addr,
       llvm::ConstantInt::get(llvm::Type::getInt32Ty(*mContext), offset),
       "ap.next");
diff --git a/lib/Core/BCCContextImpl.h b/lib/Core/BCCContextImpl.h
index a63af49..bcac0c5 100644
--- a/lib/Core/BCCContextImpl.h
+++ b/lib/Core/BCCContextImpl.h
@@ -18,7 +18,7 @@
 #define BCC_CORE_CONTEXT_IMPL_H
 
 #include <llvm/ADT/SmallPtrSet.h>
-#include <llvm/LLVMContext.h>
+#include <llvm/IR/LLVMContext.h>
 
 namespace bcc {
 
diff --git a/lib/Core/Compiler.cpp b/lib/Core/Compiler.cpp
index c229098..458fbc0 100644
--- a/lib/Core/Compiler.cpp
+++ b/lib/Core/Compiler.cpp
@@ -18,11 +18,11 @@
 
 #include <llvm/Analysis/Passes.h>
 #include <llvm/CodeGen/RegAllocRegistry.h>
-#include <llvm/Module.h>
+#include <llvm/IR/Module.h>
 #include <llvm/PassManager.h>
 #include <llvm/Support/TargetRegistry.h>
 #include <llvm/Support/raw_ostream.h>
-#include <llvm/Target/TargetData.h>
+#include <llvm/IR/DataLayout.h>
 #include <llvm/Target/TargetMachine.h>
 #include <llvm/Transforms/IPO.h>
 #include <llvm/Transforms/Scalar.h>
@@ -49,8 +49,8 @@
     /* kErrNoTargetMachine */
     "Failed to compile the script since there's no available TargetMachine."
     " (missing call to Compiler::config()?)",
-    /* kErrTargetDataNoMemory */
-    "Out of memory when create TargetData during compilation.",
+    /* kErrDataLayoutNoMemory */
+    "Out of memory when create DataLayout during compilation.",
     /* kErrMaterialization */
     "Failed to materialize the module.",
     /* kErrInvalidOutputFileState */
@@ -153,19 +153,19 @@
 }
 
 enum Compiler::ErrorCode Compiler::runLTO(Script &pScript) {
-  llvm::TargetData *target_data = NULL;
+  llvm::DataLayout *data_layout = NULL;
 
   // Pass manager for link-time optimization
   llvm::PassManager lto_passes;
 
-  // Prepare TargetData target data from Module
-  target_data = new (std::nothrow) llvm::TargetData(*mTarget->getTargetData());
-  if (target_data == NULL) {
-    return kErrTargetDataNoMemory;
+  // Prepare DataLayout target data from Module
+  data_layout = new (std::nothrow) llvm::DataLayout(*mTarget->getDataLayout());
+  if (data_layout == NULL) {
+    return kErrDataLayoutNoMemory;
   }
 
-  // Add TargetData to the pass manager.
-  lto_passes.add(target_data);
+  // Add DataLayout to the pass manager.
+  lto_passes.add(data_layout);
 
   // Invokde "beforeAddLTOPasses" before adding the first pass.
   if (!beforeAddLTOPasses(pScript, lto_passes)) {
@@ -278,20 +278,20 @@
 
 enum Compiler::ErrorCode Compiler::runCodeGen(Script &pScript,
                                               llvm::raw_ostream &pResult) {
-  llvm::TargetData *target_data;
+  llvm::DataLayout *data_layout;
   llvm::MCContext *mc_context = NULL;
 
   // Create pass manager for MC code generation.
   llvm::PassManager codegen_passes;
 
-  // Prepare TargetData target data from Module
-  target_data = new (std::nothrow) llvm::TargetData(*mTarget->getTargetData());
-  if (target_data == NULL) {
-    return kErrTargetDataNoMemory;
+  // Prepare DataLayout target data from Module
+  data_layout = new (std::nothrow) llvm::DataLayout(*mTarget->getDataLayout());
+  if (data_layout == NULL) {
+    return kErrDataLayoutNoMemory;
   }
 
-  // Add TargetData to the pass manager.
-  codegen_passes.add(target_data);
+  // Add DataLayout to the pass manager.
+  codegen_passes.add(data_layout);
 
   // Invokde "beforeAddCodeGenPasses" before adding the first pass.
   if (!beforeAddCodeGenPasses(pScript, codegen_passes)) {
diff --git a/lib/Core/Source.cpp b/lib/Core/Source.cpp
index 3d712e6..6fa7882 100644
--- a/lib/Core/Source.cpp
+++ b/lib/Core/Source.cpp
@@ -19,9 +19,9 @@
 #include <new>
 
 #include <llvm/Bitcode/ReaderWriter.h>
-#include <llvm/LLVMContext.h>
+#include <llvm/IR/LLVMContext.h>
 #include <llvm/Linker.h>
-#include <llvm/Module.h>
+#include <llvm/IR/Module.h>
 #include <llvm/Support/MemoryBuffer.h>
 #include <llvm/Support/system_error.h>
 
diff --git a/lib/Renderscript/RSCompiler.cpp b/lib/Renderscript/RSCompiler.cpp
index ce43e8b..618c1c3 100644
--- a/lib/Renderscript/RSCompiler.cpp
+++ b/lib/Renderscript/RSCompiler.cpp
@@ -16,7 +16,7 @@
 
 #include "bcc/Renderscript/RSCompiler.h"
 
-#include <llvm/Module.h>
+#include <llvm/IR/Module.h>
 #include <llvm/PassManager.h>
 #include <llvm/Transforms/IPO.h>
 
diff --git a/lib/Renderscript/RSEmbedInfo.cpp b/lib/Renderscript/RSEmbedInfo.cpp
index faa71bf..6f6e9db 100644
--- a/lib/Renderscript/RSEmbedInfo.cpp
+++ b/lib/Renderscript/RSEmbedInfo.cpp
@@ -20,15 +20,14 @@
 #include <cstdlib>
 #include <vector>
 
-#include <llvm/DerivedTypes.h>
-#include <llvm/Function.h>
-#include <llvm/Instructions.h>
-#include <llvm/IRBuilder.h>
-#include <llvm/Module.h>
+#include <llvm/IR/DerivedTypes.h>
+#include <llvm/IR/Function.h>
+#include <llvm/IR/Instructions.h>
+#include <llvm/IR/IRBuilder.h>
+#include <llvm/IR/Module.h>
 #include <llvm/Pass.h>
 #include <llvm/Support/raw_ostream.h>
-#include <llvm/Target/TargetData.h>
-#include <llvm/Type.h>
+#include <llvm/IR/Type.h>
 
 #include "bcc/Config/Config.h"
 #include "bcc/Renderscript/RSInfo.h"
diff --git a/lib/Renderscript/RSForEachExpand.cpp b/lib/Renderscript/RSForEachExpand.cpp
index 443f0af..bf1a199 100644
--- a/lib/Renderscript/RSForEachExpand.cpp
+++ b/lib/Renderscript/RSForEachExpand.cpp
@@ -19,15 +19,15 @@
 
 #include <cstdlib>
 
-#include <llvm/DerivedTypes.h>
-#include <llvm/Function.h>
-#include <llvm/Instructions.h>
-#include <llvm/IRBuilder.h>
-#include <llvm/Module.h>
+#include <llvm/IR/DerivedTypes.h>
+#include <llvm/IR/Function.h>
+#include <llvm/IR/Instructions.h>
+#include <llvm/IR/IRBuilder.h>
+#include <llvm/IR/Module.h>
 #include <llvm/Pass.h>
 #include <llvm/Support/raw_ostream.h>
-#include <llvm/Target/TargetData.h>
-#include <llvm/Type.h>
+#include <llvm/IR/DataLayout.h>
+#include <llvm/IR/Type.h>
 
 #include "bcc/Config/Config.h"
 #include "bcc/Renderscript/RSInfo.h"
@@ -103,19 +103,19 @@
   }
 
   // Get the actual value we should use to step through an allocation.
-  // TD - Target Data size/layout information.
+  // DL - Target Data size/layout information.
   // T - Type of allocation (should be a pointer).
   // OrigStep - Original step increment (root.expand() input from driver).
-  llvm::Value *getStepValue(llvm::TargetData *TD, llvm::Type *T,
+  llvm::Value *getStepValue(llvm::DataLayout *DL, llvm::Type *T,
                             llvm::Value *OrigStep) {
-    bccAssert(TD);
+    bccAssert(DL);
     bccAssert(T);
     bccAssert(OrigStep);
     llvm::PointerType *PT = llvm::dyn_cast<llvm::PointerType>(T);
     llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*C);
     if (mEnableStepOpt && T != VoidPtrTy && PT) {
       llvm::Type *ET = PT->getElementType();
-      uint64_t ETSize = TD->getTypeAllocSize(ET);
+      uint64_t ETSize = DL->getTypeAllocSize(ET);
       llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*C);
       return llvm::ConstantInt::get(Int32Ty, ETSize);
     } else {
@@ -171,7 +171,7 @@
       }
     }
 
-    llvm::TargetData TD(M);
+    llvm::DataLayout DL(M);
 
     llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*C);
     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*C);
@@ -272,7 +272,7 @@
     if (hasIn(Signature)) {
       InTy = Args->getType();
       AIn = Builder.CreateAlloca(InTy, 0, "AIn");
-      InStep = getStepValue(&TD, InTy, Arg_instep);
+      InStep = getStepValue(&DL, InTy, Arg_instep);
       InStep->setName("instep");
       Builder.CreateStore(Builder.CreatePointerCast(Builder.CreateLoad(
           Builder.CreateStructGEP(Arg_p, 0)), InTy), AIn);
@@ -284,7 +284,7 @@
     if (hasOut(Signature)) {
       OutTy = Args->getType();
       AOut = Builder.CreateAlloca(OutTy, 0, "AOut");
-      OutStep = getStepValue(&TD, OutTy, Arg_outstep);
+      OutStep = getStepValue(&DL, OutTy, Arg_outstep);
       OutStep->setName("outstep");
       Builder.CreateStore(Builder.CreatePointerCast(Builder.CreateLoad(
           Builder.CreateStructGEP(Arg_p, 1)), OutTy), AOut);
@@ -391,7 +391,7 @@
     ALOGV("Expanding kernel Function %s", F->getName().str().c_str());
 
     // TODO: Refactor this to share functionality with ExpandFunction.
-    llvm::TargetData TD(M);
+    llvm::DataLayout DL(M);
 
     llvm::Type *VoidPtrTy = llvm::Type::getInt8PtrTy(*C);
     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*C);
@@ -501,7 +501,7 @@
         // We don't increment Args, since we are using the actual return type.
       }
       AOut = Builder.CreateAlloca(OutTy, 0, "AOut");
-      OutStep = getStepValue(&TD, OutTy, Arg_outstep);
+      OutStep = getStepValue(&DL, OutTy, Arg_outstep);
       OutStep->setName("outstep");
       Builder.CreateStore(Builder.CreatePointerCast(Builder.CreateLoad(
           Builder.CreateStructGEP(Arg_p, 1)), OutTy), AOut);
@@ -514,7 +514,7 @@
       InBaseTy = Args->getType();
       InTy =InBaseTy->getPointerTo();
       AIn = Builder.CreateAlloca(InTy, 0, "AIn");
-      InStep = getStepValue(&TD, InTy, Arg_instep);
+      InStep = getStepValue(&DL, InTy, Arg_instep);
       InStep->setName("instep");
       Builder.CreateStore(Builder.CreatePointerCast(Builder.CreateLoad(
           Builder.CreateStructGEP(Arg_p, 0)), InTy), AIn);
diff --git a/lib/Renderscript/RSInfoExtractor.cpp b/lib/Renderscript/RSInfoExtractor.cpp
index c0775b5..09e707b 100644
--- a/lib/Renderscript/RSInfoExtractor.cpp
+++ b/lib/Renderscript/RSInfoExtractor.cpp
@@ -19,9 +19,9 @@
 //===----------------------------------------------------------------------===//
 #include "bcc/Renderscript/RSInfo.h"
 
-#include <llvm/Constants.h>
-#include <llvm/Metadata.h>
-#include <llvm/Module.h>
+#include <llvm/IR/Constants.h>
+#include <llvm/IR/Metadata.h>
+#include <llvm/IR/Module.h>
 
 #include "bcc/Source.h"
 #include "bcc/Support/Log.h"
diff --git a/lib/Renderscript/runtime/build_bc_lib.mk b/lib/Renderscript/runtime/build_bc_lib.mk
index 58f5f6e..d003f5b 100644
--- a/lib/Renderscript/runtime/build_bc_lib.mk
+++ b/lib/Renderscript/runtime/build_bc_lib.mk
@@ -29,7 +29,7 @@
              -O3 \
              -fno-builtin \
              -emit-llvm \
-             -ccc-host-triple armv7-none-linux-gnueabi \
+             -target armv7-none-linux-gnueabi \
              -fsigned-char \
 	     $(bc_translated_clang_cc1_cflags)
 
diff --git a/lib/Renderscript/runtime/build_clcore.sh b/lib/Renderscript/runtime/build_clcore.sh
index 329831f..842245c 100755
--- a/lib/Renderscript/runtime/build_clcore.sh
+++ b/lib/Renderscript/runtime/build_clcore.sh
@@ -9,12 +9,12 @@
 scriptc_path=../../../../base/libs/rs/scriptc
 clang_header_path=../../../../../external/clang/lib/Headers
 
-clang -ccc-host-triple armv7-none-linux-gnueabi -I${scriptc_path} -I${clang_header_path} -c -std=c99 -O3 rs_cl.c -emit-llvm -o rs_cl.bc
+clang -target armv7-none-linux-gnueabi -I${scriptc_path} -I${clang_header_path} -c -std=c99 -O3 rs_cl.c -emit-llvm -o rs_cl.bc
 
 # Generate rs_core.bc
 # ===================
 
-clang -ccc-host-triple armv7-none-linux-gnueabi -I${scriptc_path} -I${clang_header_path} -c -std=c99 -O3 rs_core.c -emit-llvm -o rs_core.bc
+clang -target armv7-none-linux-gnueabi -I${scriptc_path} -I${clang_header_path} -c -std=c99 -O3 rs_core.c -emit-llvm -o rs_core.bc
 
 # Link everything together
 # ========================