Implement support for the case when a reference to a addr-of-bb
authorChris Lattner <sabre@nondot.org>
Mon, 15 Mar 2010 20:39:00 +0000 (20:39 +0000)
committerChris Lattner <sabre@nondot.org>
Mon, 15 Mar 2010 20:39:00 +0000 (20:39 +0000)
label is generated, but then the block is deleted.  Since the
value is undefined, we just emit the label right after the entry
label of the function.  It might matter that the label is in the
same section as the function was afterall.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@98579 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/CodeGen/MachineModuleInfo.h
lib/CodeGen/AsmPrinter/AsmPrinter.cpp
lib/CodeGen/MachineModuleInfo.cpp
test/CodeGen/Generic/addr-label.ll [new file with mode: 0644]
test/CodeGen/X86/crash.ll

index accc62eb6c0ff34d06703f3dec9f61acad9d3191..7582017a0f0bee1d4bcb0556a56dcac14588be65 100644 (file)
@@ -214,6 +214,14 @@ public:
   /// block when its address is taken.  This cannot be its normal LBB label
   /// because the block may be accessed outside its containing function.
   MCSymbol *getAddrLabelSymbol(const BasicBlock *BB);
+
+  /// takeDeletedSymbolsForFunction - If the specified function has had any
+  /// references to address-taken blocks generated, but the block got deleted,
+  /// return the symbol now so we can emit it.  This prevents emitting a
+  /// reference to a symbol that has no definition.
+  void takeDeletedSymbolsForFunction(const Function *F, 
+                                     std::vector<MCSymbol*> &Result);
+
   
   //===- EH ---------------------------------------------------------------===//
 
index 63b05365d1cab68ebcdcc5c5ceaf048b369cc966..fe78b23debd1df433fa6db4e26869e6003bd75ac 100644 (file)
@@ -307,6 +307,16 @@ void AsmPrinter::EmitFunctionHeader() {
   // do their wild and crazy things as required.
   EmitFunctionEntryLabel();
   
+  // If the function had address-taken blocks that got deleted, then we have
+  // references to the dangling symbols.  Emit them at the start of the function
+  // so that we don't get references to undefined symbols.
+  std::vector<MCSymbol*> DeadBlockSyms;
+  MMI->takeDeletedSymbolsForFunction(F, DeadBlockSyms);
+  for (unsigned i = 0, e = DeadBlockSyms.size(); i != e; ++i) {
+    OutStreamer.AddComment("Address taken block that was later removed");
+    OutStreamer.EmitLabel(DeadBlockSyms[i]);
+  }
+  
   // Add some workaround for linkonce linkage on Cygwin\MinGW.
   if (MAI->getLinkOnceDirective() != 0 &&
       (F->hasLinkOnceLinkage() || F->hasWeakLinkage()))
index 72a9450dcb405e7d53cf4abfc8001088c93d07dd..b18e34c0d8d56dda4e8a907347c1a3ab7557a31d 100644 (file)
@@ -52,18 +52,35 @@ public:
 class MMIAddrLabelMap {
   MCContext &Context;
   struct AddrLabelSymEntry {
-    MCSymbol *Sym;
-    unsigned Index;
+    MCSymbol *Sym;  // The symbol for the label.
+    Function *Fn;   // The containing function of the BasicBlock.
+    unsigned Index; // The index in BBCallbacks for the BasicBlock.
   };
   
   DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
   
+  /// BBCallbacks - Callbacks for the BasicBlock's that we have entries for.  We
+  /// use this so we get notified if a block is deleted or RAUWd.
   std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
+
+  /// DeletedAddrLabelsNeedingEmission - This is a per-function list of symbols
+  /// whose corresponding BasicBlock got deleted.  These symbols need to be
+  /// emitted at some point in the file, so AsmPrinter emits them after the
+  /// function body.
+  DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >
+    DeletedAddrLabelsNeedingEmission;
 public:
   
   MMIAddrLabelMap(MCContext &context) : Context(context) {}
+  ~MMIAddrLabelMap() {
+    assert(DeletedAddrLabelsNeedingEmission.empty() &&
+           "Some labels for deleted blocks never got emitted");
+  }
   
   MCSymbol *getAddrLabelSymbol(BasicBlock *BB);  
+  void takeDeletedSymbolsForFunction(Function *F, 
+                                     std::vector<MCSymbol*> &Result);
+
   void UpdateForDeletedBlock(BasicBlock *BB);
   void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
 };
@@ -75,16 +92,36 @@ MCSymbol *MMIAddrLabelMap::getAddrLabelSymbol(BasicBlock *BB) {
   AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
   
   // If we already had an entry for this block, just return it.
-  if (Entry.Sym) return Entry.Sym;
+  if (Entry.Sym) {
+    assert(BB->getParent() == Entry.Fn && "Parent changed");
+    return Entry.Sym;
+  }
   
   // Otherwise, this is a new entry, create a new symbol for it and add an
   // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
   BBCallbacks.push_back(BB);
   BBCallbacks.back().setMap(this);
   Entry.Index = BBCallbacks.size()-1;
+  Entry.Fn = BB->getParent();
   return Entry.Sym = Context.CreateTempSymbol();
 }
 
+/// takeDeletedSymbolsForFunction - If we have any deleted symbols for F, return
+/// them.
+void MMIAddrLabelMap::
+takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
+  DenseMap<AssertingVH<Function>, std::vector<MCSymbol*> >::iterator I =
+    DeletedAddrLabelsNeedingEmission.find(F);
+
+  // If there are no entries for the function, just return.
+  if (I == DeletedAddrLabelsNeedingEmission.end()) return;
+  
+  // Otherwise, take the list.
+  std::swap(Result, I->second);
+  DeletedAddrLabelsNeedingEmission.erase(I);
+}
+
+
 void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
   // If the block got deleted, there is no need for the symbol.  If the symbol
   // was already emitted, we can just forget about it, otherwise we need to
@@ -98,9 +135,13 @@ void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
     return;
   
   // If the block is not yet defined, we need to emit it at the end of the
-  // function.
-  assert(0 && "Case not handled yet!");
-  abort();
+  // function.  Add the symbol to the DeletedAddrLabelsNeedingEmission list for
+  // the containing Function.  Since the block is being deleted, its parent may
+  // already be removed, we have to get the function from 'Entry'.
+  assert((BB->getParent() == 0 || BB->getParent() == Entry.Fn) &&
+         "Block/parent mismatch");
+  
+  DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Entry.Sym);
 }
 
 void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
@@ -219,6 +260,18 @@ MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) {
   return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB));
 }
 
+/// takeDeletedSymbolsForFunction - If the specified function has had any
+/// references to address-taken blocks generated, but the block got deleted,
+/// return the symbol now so we can emit it.  This prevents emitting a
+/// reference to a symbol that has no definition.
+void MachineModuleInfo::
+takeDeletedSymbolsForFunction(const Function *F,
+                              std::vector<MCSymbol*> &Result) {
+  // If no blocks have had their addresses taken, we're done.
+  if (AddrLabelSymbols == 0) return;
+  return AddrLabelSymbols->
+     takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
+}
 
 //===- EH -----------------------------------------------------------------===//
 
diff --git a/test/CodeGen/Generic/addr-label.ll b/test/CodeGen/Generic/addr-label.ll
new file mode 100644 (file)
index 0000000..49f3cbf
--- /dev/null
@@ -0,0 +1,39 @@
+; RUN: llc %s -o -
+
+;; Reference to a label that gets deleted.
+define i8* @test1() nounwind {
+entry:
+       ret i8* blockaddress(@test1b, %test_label)
+}
+
+define i32 @test1b() nounwind {
+entry:
+       ret i32 -1
+test_label:
+       br label %ret
+ret:
+       ret i32 -1
+}
+
+
+;; Issues with referring to a label that gets RAUW'd later.
+define i32 @test2a() nounwind {
+entry:
+        %target = bitcast i8* blockaddress(@test2b, %test_label) to i8*
+
+        call i32 @test2b(i8* %target)
+
+        ret i32 0
+}
+
+define i32 @test2b(i8* %target) nounwind {
+entry:
+        indirectbr i8* %target, [label %test_label]
+
+test_label:
+; assume some code here...
+        br label %ret
+
+ret:
+        ret i32 -1
+}
index 8ce9e6c248970066db69f609f2a899fb321b6e84..4b7c85099389b79835e86ad0617834a624793612 100644 (file)
@@ -92,26 +92,3 @@ foo:
 }
 
 
-;; Issues with referring to a label that gets RAUW'd later.
-define i32 @test6a() nounwind {
-entry:
-       %target = bitcast i8* blockaddress(@test6b, %test_label) to i8*
-
-       call i32 @test6b(i8* %target)
-
-       ret i32 0
-}
-
-define i32 @test6b(i8* %target) nounwind {
-entry:
-       indirectbr i8* %target, [label %test_label]
-
-test_label:
-; assume some code here...
-       br label %ret
-
-ret:
-       ret i32 -1
-}
-
-