[llvm-commits] [llvm] r104655 - in /llvm/trunk: include/llvm/CodeGen/MachineFunction.h lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp lib/CodeGen/StackSlotColoring.cpp

Bill Wendling isanbard at gmail.com
Tue May 25 17:32:40 PDT 2010


Author: void
Date: Tue May 25 19:32:40 2010
New Revision: 104655

URL: http://llvm.org/viewvc/llvm-project?rev=104655&view=rev
Log:
Dale and Evan suggested putting the "check for setjmp" much earlier in the
machine code generation. That's a good idea, so I made it so.

Modified:
    llvm/trunk/include/llvm/CodeGen/MachineFunction.h
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
    llvm/trunk/lib/CodeGen/StackSlotColoring.cpp

Modified: llvm/trunk/include/llvm/CodeGen/MachineFunction.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineFunction.h?rev=104655&r1=104654&r2=104655&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineFunction.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineFunction.h Tue May 25 19:32:40 2010
@@ -114,9 +114,13 @@
   ///
   unsigned FunctionNumber;
   
-  /// The alignment of the function.
+  /// Alignment - The alignment of the function.
   unsigned Alignment;
 
+  /// HasReturnsTwiceCall - Returns true if there's a call with a
+  /// "returns_twice" attribute, like setjmp.
+  bool HasReturnsTwiceCall;
+
   MachineFunction(const MachineFunction &); // DO NOT IMPLEMENT
   void operator=(const MachineFunction&);   // DO NOT IMPLEMENT
 public:
@@ -181,6 +185,15 @@
   void EnsureAlignment(unsigned A) {
     if (Alignment < A) Alignment = A;
   }
+
+  /// hasReturnsTwiceCall - Returns true if there's a call with a
+  /// "returns_twice" attribute, like setjmp.
+  bool hasReturnsTwiceCall() const {
+    return HasReturnsTwiceCall;
+  }
+  void setReturnsTwiceCall(bool B) {
+    HasReturnsTwiceCall = B;
+  }
   
   /// getInfo - Keep track of various per-function pieces of information for
   /// backends that would like to do so.

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp?rev=104655&r1=104654&r2=104655&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp Tue May 25 19:32:40 2010
@@ -25,6 +25,7 @@
 #include "llvm/Intrinsics.h"
 #include "llvm/IntrinsicInst.h"
 #include "llvm/LLVMContext.h"
+#include "llvm/Module.h"
 #include "llvm/CodeGen/FastISel.h"
 #include "llvm/CodeGen/GCStrategy.h"
 #include "llvm/CodeGen/GCMetadata.h"
@@ -253,6 +254,39 @@
   done:;
   }
 
+  // Set a flag indicating if the machine function makes a call to setjmp /
+  // sigsetjmp (i.e., a function marked "returns_twice"). We'll use this to
+  // disable certain optimizations which cannot handle such control flows.
+  // 
+  // FIXME: This goes beyond the setjmp/sigsetjmp functions. We should check for
+  // the GCC "returns twice" attribute.
+  const Module *M = Fn.getParent();
+  const Function *SetJmp = M->getFunction("setjmp");
+  const Function *SigSetJmp = M->getFunction("sigsetjmp");
+  bool HasReturnsTwiceCall = false;
+
+  if (SetJmp || SigSetJmp) {
+    if (SetJmp && !SetJmp->use_empty())
+      for (Value::const_use_iterator
+             I = SetJmp->use_begin(), E = SetJmp->use_end(); I != E; ++I)
+        if (const CallInst *CI = dyn_cast<CallInst>(I))
+          if (CI->getParent()->getParent() == &Fn) {
+            HasReturnsTwiceCall = true;
+            break;
+          }
+
+    if (!HasReturnsTwiceCall && SigSetJmp && !SigSetJmp->use_empty())
+      for (Value::const_use_iterator
+             I = SigSetJmp->use_begin(), E = SigSetJmp->use_end(); I != E; ++I)
+        if (const CallInst *CI = dyn_cast<CallInst>(I))
+          if (CI->getParent()->getParent() == &Fn) {
+            HasReturnsTwiceCall = true;
+            break;
+          }
+
+    mf.setReturnsTwiceCall(HasReturnsTwiceCall);
+  }
+
   // Release function-specific state. SDB and CurDAG are already cleared
   // at this point.
   FuncInfo->clear();

Modified: llvm/trunk/lib/CodeGen/StackSlotColoring.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackSlotColoring.cpp?rev=104655&r1=104654&r2=104655&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/StackSlotColoring.cpp (original)
+++ llvm/trunk/lib/CodeGen/StackSlotColoring.cpp Tue May 25 19:32:40 2010
@@ -118,7 +118,6 @@
 
   private:
     void InitializeSlots();
-    bool CheckForSetJmpCall(const MachineFunction &MF) const;
     void ScanForSpillSlotRefs(MachineFunction &MF);
     bool OverlapWithAssignments(LiveInterval *li, int Color) const;
     int ColorSlot(LiveInterval *li);
@@ -162,34 +161,6 @@
   };
 }
 
-/// CheckForSetJmpCall - Return true if there's a call to setjmp/sigsetjmp in
-/// this function.
-bool StackSlotColoring::CheckForSetJmpCall(const MachineFunction &MF) const {
-  const Function *F = MF.getFunction();
-  const Module *M = F->getParent();
-  const Function *SetJmp = M->getFunction("setjmp");
-  const Function *SigSetJmp = M->getFunction("sigsetjmp");
-
-  if (!SetJmp && !SigSetJmp)
-    return false;
-
-  if (SetJmp && !SetJmp->use_empty())
-    for (Value::const_use_iterator
-           I = SetJmp->use_begin(), E = SetJmp->use_end(); I != E; ++I)
-      if (const CallInst *CI = dyn_cast<CallInst>(I))
-        if (CI->getParent()->getParent() == F)
-          return true;
-
-  if (SigSetJmp && !SigSetJmp->use_empty())
-    for (Value::const_use_iterator
-           I = SigSetJmp->use_begin(), E = SigSetJmp->use_end(); I != E; ++I)
-      if (const CallInst *CI = dyn_cast<CallInst>(I))
-        if (CI->getParent()->getParent() == F)
-          return true;
-
-  return false;
-}
-
 /// ScanForSpillSlotRefs - Scan all the machine instructions for spill slot
 /// references and update spill slot weights.
 void StackSlotColoring::ScanForSpillSlotRefs(MachineFunction &MF) {
@@ -753,14 +724,11 @@
       return false;
   }
 
-  // If there are calls to setjmp or sigsetjmp, don't perform stack slot
-  // coloring. The stack could be modified before the longjmp is executed,
-  // resulting in the wrong value being used afterwards. (See
-  // <rdar://problem/8007500>.)
-  //
-  // FIXME: This goes beyond the setjmp/sigsetjmp functions. Ideally, we should
-  // check for the GCC "returns twice" attribute.
-  if (CheckForSetJmpCall(MF))
+  // If there is a call to a function with the attribute "returns_twice" (like
+  // setjmp or sigsetjmp), don't perform stack slot coloring. The stack could be
+  // modified before the the second return, resulting in the wrong value being
+  // used afterwards. (See <rdar://problem/8007500>.)
+  if (MF.hasReturnsTwiceCall())
     return false;
 
   // Gather spill slot references





More information about the llvm-commits mailing list