Apply changes to migrate to upstream (Dec 16th 2011)

- Following constants has been removed from llvm::bitc by the
  upstream, so we have to define our version.

  - TYPE_BLOCK_ID_OLD 10
  - TYPE_SYMTAB_BLOCK_ID_OLD 13
  - TYPE_CODE_STRUCT_OLD 10

- CheckDebugInfoIntrinsics has been removed by the upstream,
  so we have to copy it from lib/VMCore/AutoUpgrade.cpp

- llvm::TargetMachine::createTargetMachine now takes a new
  parameter for options, such as float ABI, soft float, and
  no frame elimination, and etc.

Change-Id: Ied522748bf92956d23f41a6195916eda1c19c589
diff --git a/bcinfo/BitReader_2_7/BitcodeReader.cpp b/bcinfo/BitReader_2_7/BitcodeReader.cpp
index 2edb153..c6d821f 100644
--- a/bcinfo/BitReader_2_7/BitcodeReader.cpp
+++ b/bcinfo/BitReader_2_7/BitcodeReader.cpp
@@ -40,6 +40,55 @@
 #define FUNC_CODE_INST_GETRESULT_2_7  25
 #define FUNC_CODE_DEBUG_LOC_2_7       32
 
+#define TYPE_BLOCK_ID_OLD_3_0         10
+#define TYPE_SYMTAB_BLOCK_ID_OLD_3_0  13
+#define TYPE_CODE_STRUCT_OLD_3_0      10
+
+namespace {
+  /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
+  /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
+  /// strips that use.
+  void CheckDebugInfoIntrinsics(Module *M) {
+    if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
+      while (!FuncStart->use_empty())
+        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());
+        if (!isa<MDNode>(DDI->getArgOperand(0)) ||
+            !isa<MDNode>(DDI->getArgOperand(1))) {
+          while (!Declare->use_empty()) {
+            CallInst *CI = cast<CallInst>(Declare->use_back());
+            CI->eraseFromParent();
+          }
+          Declare->eraseFromParent();
+        }
+      }
+    }
+  }
+} // end anonymous namespace
+
 void BitcodeReader::FreeState() {
   if (BufferOwned)
     delete Buffer;
@@ -705,7 +754,7 @@
 
 // FIXME: Remove in LLVM 3.1
 bool BitcodeReader::ParseOldTypeTable() {
-  if (Stream.EnterSubBlock(bitc::TYPE_BLOCK_ID_OLD))
+  if (Stream.EnterSubBlock(TYPE_BLOCK_ID_OLD_3_0))
     return Error("Malformed block record");
 
   if (!TypeList.empty())
@@ -808,7 +857,7 @@
       if (NextTypeID < TypeList.size() && TypeList[NextTypeID] == 0)
         ResultTy = StructType::create(Context, "");
       break;
-    case bitc::TYPE_CODE_STRUCT_OLD: {// STRUCT_OLD
+    case TYPE_CODE_STRUCT_OLD_3_0: {// STRUCT_OLD
       if (NextTypeID >= TypeList.size()) break;
       // If we already read it, don't reprocess.
       if (TypeList[NextTypeID] &&
@@ -894,7 +943,7 @@
 
 
 bool BitcodeReader::ParseOldTypeSymbolTable() {
-  if (Stream.EnterSubBlock(bitc::TYPE_SYMTAB_BLOCK_ID_OLD))
+  if (Stream.EnterSubBlock(TYPE_SYMTAB_BLOCK_ID_OLD_3_0))
     return Error("Malformed block record");
 
   SmallVector<uint64_t, 64> Record;
@@ -1592,11 +1641,11 @@
         if (ParseTypeTable())
           return true;
         break;
-      case bitc::TYPE_BLOCK_ID_OLD:
+      case TYPE_BLOCK_ID_OLD_3_0:
         if (ParseOldTypeTable())
           return true;
         break;
-      case bitc::TYPE_SYMTAB_BLOCK_ID_OLD:
+      case TYPE_SYMTAB_BLOCK_ID_OLD_3_0:
         if (ParseOldTypeSymbolTable())
           return true;
         break;
diff --git a/lib/ExecutionEngine/Compiler.cpp b/lib/ExecutionEngine/Compiler.cpp
index f456066..1e9bdb8 100644
--- a/lib/ExecutionEngine/Compiler.cpp
+++ b/lib/ExecutionEngine/Compiler.cpp
@@ -200,22 +200,6 @@
   // -O3: llvm::CodeGenOpt::Aggressive
   CodeGenOptLevel = llvm::CodeGenOpt::Aggressive;
 
-  // Below are the global settings to LLVM
-
-  // Disable frame pointer elimination optimization
-  llvm::NoFramePointerElim = false;
-
-  // Use hardfloat ABI
-  //
-  // TODO(all): Need to detect the CPU capability and decide whether to use
-  // softfp. To use softfp, change following 2 lines to
-  //
-  // llvm::FloatABIType = llvm::FloatABI::Soft;
-  // llvm::UseSoftFloat = true;
-  //
-  llvm::FloatABIType = llvm::FloatABI::Soft;
-  llvm::UseSoftFloat = false;
-
   // Register the scheduler
   llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
 
@@ -316,10 +300,15 @@
   llvm::NamedMDNode const *ExportFuncMetadata;
   llvm::NamedMDNode const *ObjectSlotMetadata;
 
+  llvm::TargetOptions Options;
+
+  llvm::CodeModel::Model CM;
+  llvm::Reloc::Model RM;
+
   if (mModule == NULL)  // No module was loaded
     return 0;
 
-  // Create TargetMachine
+  // Find LLVM Target
   Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
   if (hasError())
     goto on_bcc_compile_error;
@@ -335,18 +324,34 @@
     FeaturesStr = F.getString();
   }
 
+  // Setup LLVM Target Machine Options
+  // Disable frame pointer elimination optimization
+  Options.NoFramePointerElim = false;
+
+  // Use hardfloat ABI
+  //
+  // TODO(all): Need to detect the CPU capability and decide whether to use
+  // softfp. To use softfp, change following 2 lines to
+  //
+  // options.FloatABIType = llvm::FloatABI::Soft;
+  // options.UseSoftFloat = true;
+  Options.FloatABIType = llvm::FloatABI::Soft;
+  Options.UseSoftFloat = false;
+
 #if defined(DEFAULT_X86_64_CODEGEN)
   // Data address in X86_64 architecture may reside in a far-away place
-  TM = Target->createTargetMachine(Triple, CPU, FeaturesStr,
-                                   llvm::Reloc::Static,
-                                   llvm::CodeModel::Medium);
+  CM = llvm::CodeModel::Medium;
 #else
   // This is set for the linker (specify how large of the virtual addresses
   // we can access for all unknown symbols.)
-  TM = Target->createTargetMachine(Triple, CPU, FeaturesStr,
-                                   llvm::Reloc::Static,
-                                   llvm::CodeModel::Small);
+  CM = llvm::CodeModel::Small;
 #endif
+
+  RM = llvm::Reloc::Static;
+
+  // Create LLVM Target Machine
+  TM = Target->createTargetMachine(Triple, CPU, FeaturesStr, Options, RM, CM);
+
   if (TM == NULL) {
     setError("Failed to create target machine implementation for the"
              " specified triple '" + Triple + "'");