Emit exception handling info for functions which are
not marked nounwind, or for all functions when -enable-eh
is set, provided the target supports Dwarf EH.

llvm-gcc generates nounwind in the right places; other FEs
will need to do so also.  Given such a FE, -enable-eh should
no longer be needed.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49006 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index 80b5c04..ae2b418 100644
--- a/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -169,6 +169,7 @@
     Function &Fn;
     MachineFunction &MF;
     MachineRegisterInfo &RegInfo;
+    bool needsExceptionHandling;
 
     FunctionLoweringInfo(TargetLowering &TLI, Function &Fn,MachineFunction &MF);
 
@@ -304,6 +305,10 @@
         BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i);
     }
   }
+
+  // Figure out whether we need to generate EH info.  Currently we do this for
+  // all functions not marked no-unwind, or if requested via -enable-eh.
+  needsExceptionHandling = ExceptionHandling || !Fn.doesNotThrow();
 }
 
 /// CreateRegForValue - Allocate the appropriate number of virtual registers of
@@ -2827,7 +2832,7 @@
   }
     
   case Intrinsic::eh_exception: {
-    if (ExceptionHandling) {
+    if (FuncInfo.needsExceptionHandling) {
       if (!CurMBB->isLandingPad()) {
         // FIXME: Mark exception register as live in.  Hack for PR1508.
         unsigned Reg = TLI.getExceptionAddressRegister();
@@ -2852,7 +2857,7 @@
     MVT::ValueType VT = (Intrinsic == Intrinsic::eh_selector_i32 ?
                          MVT::i32 : MVT::i64);
     
-    if (ExceptionHandling && MMI) {
+    if (FuncInfo.needsExceptionHandling && MMI) {
       if (CurMBB->isLandingPad())
         addCatchInfo(I, MMI, CurMBB);
       else {
@@ -2902,7 +2907,7 @@
   case Intrinsic::eh_return: {
     MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
 
-    if (MMI && ExceptionHandling) {
+    if (MMI && FuncInfo.needsExceptionHandling) {
       MMI->setCallsEHReturn(true);
       DAG.setRoot(DAG.getNode(ISD::EH_RETURN,
                               MVT::Other,
@@ -2925,7 +2930,7 @@
    }
 
    case Intrinsic::eh_dwarf_cfa: {
-     if (ExceptionHandling) {
+     if (FuncInfo.needsExceptionHandling) {
        MVT::ValueType VT = getValue(I.getOperand(1)).getValueType();
        SDOperand CfaArg;
        if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(TLI.getPointerTy()))
@@ -3176,7 +3181,7 @@
     Args.push_back(Entry);
   }
 
-  if (LandingPad && ExceptionHandling && MMI) {
+  if (LandingPad && FuncInfo.needsExceptionHandling && MMI) {
     // Insert a label before the invoke call to mark the try range.  This can be
     // used to detect deletion of the invoke via the MachineModuleInfo.
     BeginLabel = MMI->NextLabelID();
@@ -3195,7 +3200,7 @@
     setValue(CS.getInstruction(), Result.first);
   DAG.setRoot(Result.second);
 
-  if (LandingPad && ExceptionHandling && MMI) {
+  if (LandingPad && FuncInfo.needsExceptionHandling && MMI) {
     // Insert a label at the end of the invoke call to mark the try range.  This
     // can be used to detect deletion of the invoke via the MachineModuleInfo.
     EndLabel = MMI->NextLabelID();
@@ -4614,7 +4619,7 @@
 
   FunctionLoweringInfo FuncInfo(TLI, Fn, MF);
 
-  if (ExceptionHandling)
+  if (FuncInfo.needsExceptionHandling)
     for (Function::iterator I = Fn.begin(), E = Fn.end(); I != E; ++I)
       if (InvokeInst *Invoke = dyn_cast<InvokeInst>(I->getTerminator()))
         // Mark landing pad.
@@ -4757,7 +4762,7 @@
 
   MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
 
-  if (ExceptionHandling && MMI && BB->isLandingPad()) {
+  if (FuncInfo.needsExceptionHandling && MMI && BB->isLandingPad()) {
     // Add a label to mark the beginning of the landing pad.  Deletion of the
     // landing pad can thus be detected via the MachineModuleInfo.
     unsigned LabelID = MMI->addLandingPad(BB);