Add an unwind_to field to basic blocks, making them Users instead of Values.
This is the first checkin for PR1269, the new EH infrastructure.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47802 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AsmParser/LLLexer.cpp b/lib/AsmParser/LLLexer.cpp
index f78079b..18b774b 100644
--- a/lib/AsmParser/LLLexer.cpp
+++ b/lib/AsmParser/LLLexer.cpp
@@ -474,6 +474,7 @@
   KEYWORD("asm", ASM_TOK);
   KEYWORD("sideeffect", SIDEEFFECT);
   KEYWORD("gc", GC);
+  KEYWORD("unwind_to", UNWIND_TO);
 
   KEYWORD("cc", CC_TOK);
   KEYWORD("ccc", CCC_TOK);
diff --git a/lib/AsmParser/llvmAsmParser.y b/lib/AsmParser/llvmAsmParser.y
index 5a824ae..c564069 100644
--- a/lib/AsmParser/llvmAsmParser.y
+++ b/lib/AsmParser/llvmAsmParser.y
@@ -518,7 +518,7 @@
 
 /// defineBBVal - This is a definition of a new basic block with the specified
 /// identifier which must be the same as CurFun.NextValNum, if its numeric.
-static BasicBlock *defineBBVal(const ValID &ID) {
+static BasicBlock *defineBBVal(const ValID &ID, BasicBlock *unwindDest) {
   assert(inFunctionScope() && "Can't get basic block at global scope!");
 
   BasicBlock *BB = 0;
@@ -548,21 +548,19 @@
       assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
       InsertValue(BB);
     }
-
-    ID.destroy();
-    return BB;
-  } 
-  
-  // We haven't seen this BB before and its first mention is a definition. 
-  // Just create it and return it.
-  std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
-  BB = new BasicBlock(Name, CurFun.CurrentFunction);
-  if (ID.Type == ValID::LocalID) {
-    assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
-    InsertValue(BB);
+  } else { 
+    // We haven't seen this BB before and its first mention is a definition. 
+    // Just create it and return it.
+    std::string Name (ID.Type == ValID::LocalName ? ID.getName() : "");
+    BB = new BasicBlock(Name, CurFun.CurrentFunction);
+    if (ID.Type == ValID::LocalID) {
+      assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
+      InsertValue(BB);
+    }
   }
 
-  ID.destroy(); // Free strdup'd memory
+  ID.destroy();
+  BB->setUnwindDest(unwindDest);
   return BB;
 }
 
@@ -1066,7 +1064,7 @@
 %token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
 %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
 %token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
-%token DATALAYOUT
+%token DATALAYOUT UNWIND_TO
 %type <UIntVal> OptCallingConv
 %type <ParamAttrs> OptParamAttrs ParamAttr 
 %type <ParamAttrs> OptFuncAttrs  FuncAttr
@@ -2568,14 +2566,22 @@
     CHECK_FOR_ERROR
   }
   | /* empty */ {          // Empty space between instruction lists
-    $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
+    $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), 0);
+    CHECK_FOR_ERROR
+  }
+  | UNWIND_TO ValueRef {   // Only the unwind to block
+    $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), getBBVal($2));
     CHECK_FOR_ERROR
   }
   | LABELSTR {             // Labelled (named) basic block
-    $$ = defineBBVal(ValID::createLocalName(*$1));
+    $$ = defineBBVal(ValID::createLocalName(*$1), 0);
     delete $1;
     CHECK_FOR_ERROR
-
+  }
+  | LABELSTR UNWIND_TO ValueRef {
+    $$ = defineBBVal(ValID::createLocalName(*$1), getBBVal($3));
+    delete $1;
+    CHECK_FOR_ERROR
   };
 
 BBTerminatorInst : 
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp
index 61e0ab9..c1cfa97 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1216,6 +1216,15 @@
       CurBB = FunctionBBs[0];
       continue;
       
+    case bitc::FUNC_CODE_INST_BB_UNWINDDEST:   // BB_UNWINDDEST: [bb#]
+      if (CurBB->getUnwindDest())
+        return Error("Only permit one BB_UNWINDDEST per BB");
+      if (Record.size() != 1)
+        return Error("Invalid BB_UNWINDDEST record");
+
+      CurBB->setUnwindDest(getBasicBlock(Record[0]));
+      continue;
+      
     case bitc::FUNC_CODE_INST_BINOP: {    // BINOP: [opval, ty, opval, opcode]
       unsigned OpNum = 0;
       Value *LHS, *RHS;
diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp
index 5de38b8..67b13e3 100644
--- a/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -969,13 +969,20 @@
   unsigned InstID = CstEnd;
   
   // Finally, emit all the instructions, in order.
-  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
+  for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
+    if (const BasicBlock *unwindDest = BB->getUnwindDest()) {
+      Vals.push_back(VE.getValueID(unwindDest));
+      Stream.EmitRecord(bitc::FUNC_CODE_INST_BB_UNWINDDEST, Vals);
+      Vals.clear();
+    }   
+
     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
          I != E; ++I) {
       WriteInstruction(*I, InstID, VE, Stream, Vals);
       if (I->getType() != Type::VoidTy)
         ++InstID;
     }
+  }
   
   // Emit names for all the instructions etc.
   WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp
index 55d037d..595f478 100644
--- a/lib/VMCore/AsmWriter.cpp
+++ b/lib/VMCore/AsmWriter.cpp
@@ -1130,7 +1130,7 @@
   if (F->isDeclaration()) {
     Out << "\n";
   } else {
-    Out << " {";
+    Out << " {\n";
 
     // Output all of its basic blocks... for the function
     for (Function::const_iterator I = F->begin(), E = F->end(); I != E; ++I)
@@ -1162,10 +1162,19 @@
 /// printBasicBlock - This member is called for each basic block in a method.
 ///
 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
-  if (BB->hasName()) {              // Print out the label if it exists...
-    Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
-  } else if (!BB->use_empty()) {      // Don't print block # of no uses...
-    Out << "\n; <label>:";
+  if (BB->hasName())              // Print out the label if it exists...
+    Out << getLLVMName(BB->getName(), LabelPrefix) << ':';
+
+  if (const BasicBlock* unwindDest = BB->getUnwindDest()) {
+    if (BB->hasName())
+      Out << ' ';
+
+    Out << "unwind_to";
+    writeOperand(unwindDest, false);
+  }
+
+  if (!BB->hasName() && !BB->use_empty()) { // Don't print block # of no uses...
+    Out << "; <label>:";
     int Slot = Machine.getLocalSlot(BB);
     if (Slot != -1)
       Out << Slot;
@@ -1194,7 +1203,9 @@
     }
   }
 
-  Out << "\n";
+  if (BB->hasName() || !BB->use_empty() || BB->getUnwindDest() ||
+      BB != &BB->getParent()->getEntryBlock())
+    Out << "\n";
 
   if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
 
diff --git a/lib/VMCore/BasicBlock.cpp b/lib/VMCore/BasicBlock.cpp
index 3ab5e96..f9bead7 100644
--- a/lib/VMCore/BasicBlock.cpp
+++ b/lib/VMCore/BasicBlock.cpp
@@ -70,8 +70,8 @@
 
 
 BasicBlock::BasicBlock(const std::string &Name, Function *NewParent,
-                       BasicBlock *InsertBefore)
-  : Value(Type::LabelTy, Value::BasicBlockVal), Parent(0) {
+                       BasicBlock *InsertBefore, BasicBlock *Dest)
+  : User(Type::LabelTy, Value::BasicBlockVal, &unwindDest, 0), Parent(0) {
 
   // Make sure that we get added to a function
   LeakDetector::addGarbageObject(this);
@@ -85,6 +85,8 @@
   }
   
   setName(Name);
+  unwindDest.init(NULL, this);
+  setUnwindDest(Dest);
 }
 
 
@@ -113,6 +115,19 @@
   getParent()->getBasicBlockList().erase(this);
 }
 
+const BasicBlock *BasicBlock::getUnwindDest() const {
+  return cast_or_null<const BasicBlock>(unwindDest.get());
+}
+
+BasicBlock *BasicBlock::getUnwindDest() {
+  return cast_or_null<BasicBlock>(unwindDest.get());
+}
+
+void BasicBlock::setUnwindDest(BasicBlock *dest) {
+  NumOperands = unwindDest ? 1 : 0;
+  unwindDest.set(dest);
+}
+
 /// moveBefore - Unlink this basic block from its current function and
 /// insert it into the function that MovePos lives in, right before MovePos.
 void BasicBlock::moveBefore(BasicBlock *MovePos) {
@@ -151,6 +166,7 @@
 }
 
 void BasicBlock::dropAllReferences() {
+  setUnwindDest(NULL);
   for(iterator I = begin(), E = end(); I != E; ++I)
     I->dropAllReferences();
 }
@@ -177,6 +193,9 @@
           find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) &&
          "removePredecessor: BB is not a predecessor!");
 
+  if (Pred == getUnwindDest())
+    setUnwindDest(NULL);
+
   if (InstList.empty()) return;
   PHINode *APN = dyn_cast<PHINode>(&front());
   if (!APN) return;   // Quick exit.