Author: johannes
Date: Wed Jan 16 13:59:28 2008
New Revision: 46088

URL: http://llvm.org/viewvc/llvm-project?rev=46088&view=rev
Log:
Do not mark EH tables no-dead-strip unless the
associated function is so marked.


Modified:
    llvm/trunk/include/llvm/CodeGen/MachineModuleInfo.h
    llvm/trunk/lib/CodeGen/DwarfWriter.cpp
    llvm/trunk/lib/CodeGen/MachineModuleInfo.cpp

Modified: llvm/trunk/include/llvm/CodeGen/MachineModuleInfo.h
URL: 
http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineModuleInfo.h?rev=46088&r1=46087&r2=46088&view=diff

==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineModuleInfo.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineModuleInfo.h Wed Jan 16 13:59:28 2008
@@ -35,6 +35,7 @@
 #include "llvm/Support/DataTypes.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/UniqueVector.h"
+#include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/GlobalValue.h"
 #include "llvm/Pass.h"
 
@@ -1037,6 +1038,10 @@
   // common EH frames.
   std::vector<Function *> Personalities;
 
+  // UsedFunctions - the functions in the llvm.used list in a more easily
+  // searchable format.
+  SmallPtrSet<const Function *, 32> UsedFunctions;
+
   bool CallsEHReturn;
   bool CallsUnwindInit;
 public:
@@ -1235,6 +1240,11 @@
     return Personalities;
   }
 
+  // UsedFunctions - Return set of the functions in the llvm.used list.
+  const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
+    return UsedFunctions;
+  }
+
   /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
   ///
   void addCatchTypeInfo(MachineBasicBlock *LandingPad,

Modified: llvm/trunk/lib/CodeGen/DwarfWriter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/DwarfWriter.cpp?rev=46088&r1=46087&r2=46088&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/DwarfWriter.cpp (original)
+++ llvm/trunk/lib/CodeGen/DwarfWriter.cpp Wed Jan 16 13:59:28 2008
@@ -2764,15 +2764,14 @@
     bool hasCalls;
     bool hasLandingPads;
     std::vector<MachineMove> Moves;
-    Function::LinkageTypes linkage;
+    const Function * function;
 
     FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
                         bool hC, bool hL,
                         const std::vector<MachineMove> &M,
-                        Function::LinkageTypes l):
+                        const Function *f):
       FnName(FN), Number(Num), PersonalityIndex(P),
-      hasCalls(hC), hasLandingPads(hL), Moves(M),
-      linkage(l) { }
+      hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
   };
 
   std::vector<FunctionEHFrameInfo> EHFrames;
@@ -2869,19 +2868,21 @@
   /// EmitEHFrame - Emit function exception frame information.
   ///
   void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
+    Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
+
     Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
 
     // Externally visible entry into the functions eh frame info.
     // If the corresponding function is static, this should not be
     // externally visible.
-    if (EHFrameInfo.linkage != Function::InternalLinkage) {
+    if (linkage != Function::InternalLinkage) {
       if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
         O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
     }
 
     // If corresponding function is weak definition, this should be too.
-    if ((EHFrameInfo.linkage == Function::WeakLinkage || 
-         EHFrameInfo.linkage == Function::LinkOnceLinkage) &&
+    if ((linkage == Function::WeakLinkage || 
+         linkage == Function::LinkOnceLinkage) &&
         TAI->getWeakDefDirective())
       O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
 
@@ -2889,12 +2890,17 @@
     // omit the EH Frame, but some environments do not handle weak absolute
     // symbols.
     if (!EHFrameInfo.hasCalls &&
-        ((EHFrameInfo.linkage != Function::WeakLinkage && 
-          EHFrameInfo.linkage != Function::LinkOnceLinkage) ||
+        ((linkage != Function::WeakLinkage && 
+          linkage != Function::LinkOnceLinkage) ||
          !TAI->getWeakDefDirective() ||
          TAI->getSupportsWeakOmittedEHFrame()))
     { 
       O << EHFrameInfo.FnName << " = 0\n";
+      // This name has no connection to the function, so it might get 
+      // dead-stripped when the function is not, erroneously.  Prohibit 
+      // dead-stripping unconditionally.
+      if (const char *UsedDirective = TAI->getUsedDirective())
+        O << UsedDirective << EHFrameInfo.FnName << "\n\n";
     } else {
       O << EHFrameInfo.FnName << ":\n";
 
@@ -2941,10 +2947,16 @@
       
       Asm->EmitAlignment(2);
       EmitLabel("eh_frame_end", EHFrameInfo.Number);
-    }
     
-    if (const char *UsedDirective = TAI->getUsedDirective())
-      O << UsedDirective << EHFrameInfo.FnName << "\n\n";
+      // If the function is marked used, this table should be also.  We cannot 
+      // make the mark unconditional in this case, since retaining the table
+      // also retains the function in this case, and there is code around 
+      // that depends on unused functions (calling undefined externals) being
+      // dead-stripped to link correctly.  Yes, there really is.
+      if (MMI->getUsedFunctions().count(EHFrameInfo.function))
+        if (const char *UsedDirective = TAI->getUsedDirective())
+          O << UsedDirective << EHFrameInfo.FnName << "\n\n";
+    }
   }
 
   /// EmitExceptionTable - Emit landing pads and actions.
@@ -3414,7 +3426,7 @@
                                     MF->getFrameInfo()->hasCalls(),
                                     !MMI->getLandingPads().empty(),
                                     MMI->getFrameMoves(),
-                                    MF->getFunction()->getLinkage()));
+                                    MF->getFunction()));
   }
 };
 

Modified: llvm/trunk/lib/CodeGen/MachineModuleInfo.cpp
URL: 
http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineModuleInfo.cpp?rev=46088&r1=46087&r2=46088&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/MachineModuleInfo.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachineModuleInfo.cpp Wed Jan 16 13:59:28 2008
@@ -1552,6 +1552,21 @@
 ///
 void MachineModuleInfo::AnalyzeModule(Module &M) {
   SetupCompileUnits(M);
+
+  // Insert functions in the llvm.used array into UsedFunctions.
+  GlobalVariable *GV = M.getGlobalVariable("llvm.used");
+  if (!GV || !GV->hasInitializer()) return;
+
+  // Should be an array of 'i8*'.
+  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
+  if (InitList == 0) return;
+
+  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
+    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
+      if (CE->getOpcode() == Instruction::BitCast)
+        if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
+          UsedFunctions.insert(F);
+  }
 }
 
 /// needsFrameInfo - Returns true if we need to gather callee-saved register


_______________________________________________
llvm-commits mailing list
llvm-commits@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

Reply via email to