Debug Info: drop debug info via upgrading path if version number does not match.
[oota-llvm.git] / lib / Bitcode / Reader / BitcodeReader.cpp
index f2039d4ed66de705fb056a74d1601ddffdf96e85..ce3b7d163e51ad668baa3e3b61ed241c372e7dc4 100644 (file)
@@ -128,6 +128,7 @@ static int GetDecodedCastOpcode(unsigned Val) {
   case bitc::CAST_PTRTOINT: return Instruction::PtrToInt;
   case bitc::CAST_INTTOPTR: return Instruction::IntToPtr;
   case bitc::CAST_BITCAST : return Instruction::BitCast;
+  case bitc::CAST_ADDRSPACECAST: return Instruction::AddrSpaceCast;
   }
 }
 static int GetDecodedBinaryOpcode(unsigned Val, Type *Ty) {
@@ -508,125 +509,96 @@ error_code BitcodeReader::ParseAttributeBlock() {
   }
 }
 
-error_code BitcodeReader::ParseAttrKind(uint64_t Code,
-                                        Attribute::AttrKind *Kind) {
+// Returns Attribute::None on unrecognized codes.
+static Attribute::AttrKind GetAttrFromCode(uint64_t Code) {
   switch (Code) {
+  default:
+    return Attribute::None;
   case bitc::ATTR_KIND_ALIGNMENT:
-    *Kind = Attribute::Alignment;
-    return error_code::success();
+    return Attribute::Alignment;
   case bitc::ATTR_KIND_ALWAYS_INLINE:
-    *Kind = Attribute::AlwaysInline;
-    return error_code::success();
+    return Attribute::AlwaysInline;
   case bitc::ATTR_KIND_BUILTIN:
-    *Kind = Attribute::Builtin;
-    return error_code::success();
+    return Attribute::Builtin;
   case bitc::ATTR_KIND_BY_VAL:
-    *Kind = Attribute::ByVal;
-    return error_code::success();
+    return Attribute::ByVal;
   case bitc::ATTR_KIND_COLD:
-    *Kind = Attribute::Cold;
-    return error_code::success();
+    return Attribute::Cold;
   case bitc::ATTR_KIND_INLINE_HINT:
-    *Kind = Attribute::InlineHint;
-    return error_code::success();
+    return Attribute::InlineHint;
   case bitc::ATTR_KIND_IN_REG:
-    *Kind = Attribute::InReg;
-    return error_code::success();
+    return Attribute::InReg;
   case bitc::ATTR_KIND_MIN_SIZE:
-    *Kind = Attribute::MinSize;
-    return error_code::success();
+    return Attribute::MinSize;
   case bitc::ATTR_KIND_NAKED:
-    *Kind = Attribute::Naked;
-    return error_code::success();
+    return Attribute::Naked;
   case bitc::ATTR_KIND_NEST:
-    *Kind = Attribute::Nest;
-    return error_code::success();
+    return Attribute::Nest;
   case bitc::ATTR_KIND_NO_ALIAS:
-    *Kind = Attribute::NoAlias;
-    return error_code::success();
+    return Attribute::NoAlias;
   case bitc::ATTR_KIND_NO_BUILTIN:
-    *Kind = Attribute::NoBuiltin;
-    return error_code::success();
+    return Attribute::NoBuiltin;
   case bitc::ATTR_KIND_NO_CAPTURE:
-    *Kind = Attribute::NoCapture;
-    return error_code::success();
+    return Attribute::NoCapture;
   case bitc::ATTR_KIND_NO_DUPLICATE:
-    *Kind = Attribute::NoDuplicate;
-    return error_code::success();
+    return Attribute::NoDuplicate;
   case bitc::ATTR_KIND_NO_IMPLICIT_FLOAT:
-    *Kind = Attribute::NoImplicitFloat;
-    return error_code::success();
+    return Attribute::NoImplicitFloat;
   case bitc::ATTR_KIND_NO_INLINE:
-    *Kind = Attribute::NoInline;
-    return error_code::success();
+    return Attribute::NoInline;
   case bitc::ATTR_KIND_NON_LAZY_BIND:
-    *Kind = Attribute::NonLazyBind;
-    return error_code::success();
+    return Attribute::NonLazyBind;
   case bitc::ATTR_KIND_NO_RED_ZONE:
-    *Kind = Attribute::NoRedZone;
-    return error_code::success();
+    return Attribute::NoRedZone;
   case bitc::ATTR_KIND_NO_RETURN:
-    *Kind = Attribute::NoReturn;
-    return error_code::success();
+    return Attribute::NoReturn;
   case bitc::ATTR_KIND_NO_UNWIND:
-    *Kind = Attribute::NoUnwind;
-    return error_code::success();
+    return Attribute::NoUnwind;
   case bitc::ATTR_KIND_OPTIMIZE_FOR_SIZE:
-    *Kind = Attribute::OptimizeForSize;
-    return error_code::success();
+    return Attribute::OptimizeForSize;
   case bitc::ATTR_KIND_OPTIMIZE_NONE:
-    *Kind = Attribute::OptimizeNone;
-    return error_code::success();
+    return Attribute::OptimizeNone;
   case bitc::ATTR_KIND_READ_NONE:
-    *Kind = Attribute::ReadNone;
-    return error_code::success();
+    return Attribute::ReadNone;
   case bitc::ATTR_KIND_READ_ONLY:
-    *Kind = Attribute::ReadOnly;
-    return error_code::success();
+    return Attribute::ReadOnly;
   case bitc::ATTR_KIND_RETURNED:
-    *Kind = Attribute::Returned;
-    return error_code::success();
+    return Attribute::Returned;
   case bitc::ATTR_KIND_RETURNS_TWICE:
-    *Kind = Attribute::ReturnsTwice;
-    return error_code::success();
+    return Attribute::ReturnsTwice;
   case bitc::ATTR_KIND_S_EXT:
-    *Kind = Attribute::SExt;
-    return error_code::success();
+    return Attribute::SExt;
   case bitc::ATTR_KIND_STACK_ALIGNMENT:
-    *Kind = Attribute::StackAlignment;
-    return error_code::success();
+    return Attribute::StackAlignment;
   case bitc::ATTR_KIND_STACK_PROTECT:
-    *Kind = Attribute::StackProtect;
-    return error_code::success();
+    return Attribute::StackProtect;
   case bitc::ATTR_KIND_STACK_PROTECT_REQ:
-    *Kind = Attribute::StackProtectReq;
-    return error_code::success();
+    return Attribute::StackProtectReq;
   case bitc::ATTR_KIND_STACK_PROTECT_STRONG:
-    *Kind = Attribute::StackProtectStrong;
-    return error_code::success();
+    return Attribute::StackProtectStrong;
   case bitc::ATTR_KIND_STRUCT_RET:
-    *Kind = Attribute::StructRet;
-    return error_code::success();
+    return Attribute::StructRet;
   case bitc::ATTR_KIND_SANITIZE_ADDRESS:
-    *Kind = Attribute::SanitizeAddress;
-    return error_code::success();
+    return Attribute::SanitizeAddress;
   case bitc::ATTR_KIND_SANITIZE_THREAD:
-    *Kind = Attribute::SanitizeThread;
-    return error_code::success();
+    return Attribute::SanitizeThread;
   case bitc::ATTR_KIND_SANITIZE_MEMORY:
-    *Kind = Attribute::SanitizeMemory;
-    return error_code::success();
+    return Attribute::SanitizeMemory;
   case bitc::ATTR_KIND_UW_TABLE:
-    *Kind = Attribute::UWTable;
-    return error_code::success();
+    return Attribute::UWTable;
   case bitc::ATTR_KIND_Z_EXT:
-    *Kind = Attribute::ZExt;
-    return error_code::success();
-  default:
-    return Error(InvalidValue);
+    return Attribute::ZExt;
   }
 }
 
+error_code BitcodeReader::ParseAttrKind(uint64_t Code,
+                                        Attribute::AttrKind *Kind) {
+  *Kind = GetAttrFromCode(Code);
+  if (*Kind == Attribute::None)
+    return Error(InvalidValue);
+  return error_code::success();
+}
+
 error_code BitcodeReader::ParseAttributeGroupBlock() {
   if (Stream.EnterSubBlock(bitc::PARAMATTR_GROUP_BLOCK_ID))
     return Error(InvalidRecord);
@@ -1385,7 +1357,8 @@ error_code BitcodeReader::ParseConstants() {
         if (!OpTy)
           return Error(InvalidRecord);
         Constant *Op = ValueList.getConstantFwdRef(Record[2], OpTy);
-        V = ConstantExpr::getCast(Opc, Op, CurTy);
+        V = UpgradeBitCastExpr(Opc, Op, CurTy);
+        if (!V) V = ConstantExpr::getCast(Opc, Op, CurTy);
       }
       break;
     }
@@ -2325,7 +2298,15 @@ error_code BitcodeReader::ParseFunctionBody(Function *F) {
       int Opc = GetDecodedCastOpcode(Record[OpNum+1]);
       if (Opc == -1 || ResTy == 0)
         return Error(InvalidRecord);
-      I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
+      Instruction *Temp = 0;
+      if ((I = UpgradeBitCastInst(Opc, Op, ResTy, Temp))) {
+        if (Temp) {
+          InstructionList.push_back(Temp);
+          CurBB->getInstList().push_back(Temp);
+        }
+      } else {
+        I = CastInst::Create((Instruction::CastOps)Opc, Op, ResTy);
+      }
       InstructionList.push_back(I);
       break;
     }
@@ -3049,17 +3030,18 @@ OutOfRecordLoop:
   return error_code::success();
 }
 
-/// FindFunctionInStream - Find the function body in the bitcode stream
-bool BitcodeReader::FindFunctionInStream(Function *F,
+/// Find the function body in the bitcode stream
+error_code BitcodeReader::FindFunctionInStream(Function *F,
        DenseMap<Function*, uint64_t>::iterator DeferredFunctionInfoIterator) {
   while (DeferredFunctionInfoIterator->second == 0) {
     if (Stream.AtEndOfStream())
       return Error(CouldNotFindFunctionInStream);
     // ParseModule will parse the next body in the stream and set its
     // position in the DeferredFunctionInfo map.
-    if (ParseModule(true)) return true;
+    if (error_code EC = ParseModule(true))
+      return EC;
   }
-  return false;
+  return error_code::success();
 }
 
 //===----------------------------------------------------------------------===//
@@ -3075,26 +3057,25 @@ bool BitcodeReader::isMaterializable(const GlobalValue *GV) const {
   return false;
 }
 
-bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
+error_code BitcodeReader::Materialize(GlobalValue *GV) {
   Function *F = dyn_cast<Function>(GV);
   // If it's not a function or is already material, ignore the request.
-  if (!F || !F->isMaterializable()) return false;
+  if (!F || !F->isMaterializable())
+    return error_code::success();
 
   DenseMap<Function*, uint64_t>::iterator DFII = DeferredFunctionInfo.find(F);
   assert(DFII != DeferredFunctionInfo.end() && "Deferred function not found!");
   // If its position is recorded as 0, its body is somewhere in the stream
   // but we haven't seen it yet.
-  if (DFII->second == 0)
-    if (LazyStreamer && FindFunctionInStream(F, DFII)) return true;
+  if (DFII->second == 0 && LazyStreamer)
+    if (error_code EC = FindFunctionInStream(F, DFII))
+      return EC;
 
   // Move the bit stream to the saved position of the deferred function body.
   Stream.JumpToBit(DFII->second);
 
-  if (error_code EC = ParseFunctionBody(F)) {
-    if (ErrInfo)
-      *ErrInfo = EC.message();
-    return true;
-  }
+  if (error_code EC = ParseFunctionBody(F))
+    return EC;
 
   // Upgrade any old intrinsic calls in the function.
   for (UpgradedIntrinsicMap::iterator I = UpgradedIntrinsics.begin(),
@@ -3108,7 +3089,7 @@ bool BitcodeReader::Materialize(GlobalValue *GV, std::string *ErrInfo) {
     }
   }
 
-  return false;
+  return error_code::success();
 }
 
 bool BitcodeReader::isDematerializable(const GlobalValue *GV) const {
@@ -3131,17 +3112,18 @@ void BitcodeReader::Dematerialize(GlobalValue *GV) {
 }
 
 
-bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
+error_code BitcodeReader::MaterializeModule(Module *M) {
   assert(M == TheModule &&
          "Can only Materialize the Module this BitcodeReader is attached to.");
   // 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 (F->isMaterializable() &&
-        Materialize(F, ErrInfo))
-      return true;
-
+       F != E; ++F) {
+    if (F->isMaterializable()) {
+      if (error_code EC = Materialize(F))
+        return EC;
+    }
+  }
   // At this point, if there are any function bodies, the current bit is
   // pointing to the END_BLOCK record after them. Now make sure the rest
   // of the bits in the module have been read.
@@ -3170,7 +3152,8 @@ bool BitcodeReader::MaterializeModule(Module *M, std::string *ErrInfo) {
   for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
     UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
 
-  return false;
+  UpgradeDebugInfo(*M);
+  return error_code::success();
 }
 
 error_code BitcodeReader::InitStream() {