[llvm-commits] [llvm] r104640 - /llvm/trunk/lib/CodeGen/StackSlotColoring.cpp

Bill Wendling isanbard at gmail.com
Tue May 25 14:44:27 PDT 2010


Author: void
Date: Tue May 25 16:44:26 2010
New Revision: 104640

URL: http://llvm.org/viewvc/llvm-project?rev=104640&view=rev
Log:
Okay, bear with me here...

If you have a setjmp/longjmp situation, it's possible for stack slot coloring to
reuse a stack slot before it's really dead. For instance, if we have something
like this:

1:  y = g;
    x = sigsetjmp(env, 0);
    switch (x) {
    case 1:
      /* ... */
      goto run;  
    case 0:
  run:
      do_run(); /* marked as "no return" */
      break;
    case 3:
      if (...) {
        /* ... */
        goto run;
      }
      /* ... */
      break;
    }

2:  g = y;

"y" may be put onto the stack, so the expression "g = y" is relying upon the
fact that the stack slot containing "y" isn't modified between (1) and (2). But
it can be, because of the "no return" calls in there. A longjmp might come back
with 3, modify the stack slot, and then go to case 0. And it's perfectly
acceptable to reuse the stack slot there because there's no CFG flow from case 3
to (2).

The fix is to disable certain optimizations in these situations. Ideally, we'd
disable them for all "returns twice" functions. But we don't support that
attribute. Check for "setjmp" and "sigsetjmp" instead.

Modified:
    llvm/trunk/lib/CodeGen/StackSlotColoring.cpp

Modified: llvm/trunk/lib/CodeGen/StackSlotColoring.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackSlotColoring.cpp?rev=104640&r1=104639&r2=104640&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/StackSlotColoring.cpp (original)
+++ llvm/trunk/lib/CodeGen/StackSlotColoring.cpp Tue May 25 16:44:26 2010
@@ -13,6 +13,8 @@
 
 #define DEBUG_TYPE "stackcoloring"
 #include "VirtRegMap.h"
+#include "llvm/Function.h"
+#include "llvm/Module.h"
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
 #include "llvm/CodeGen/LiveStackAnalysis.h"
@@ -116,6 +118,7 @@
 
   private:
     void InitializeSlots();
+    bool CheckForSetJmpCall(const MachineFunction &MF);
     void ScanForSpillSlotRefs(MachineFunction &MF);
     bool OverlapWithAssignments(LiveInterval *li, int Color) const;
     int ColorSlot(LiveInterval *li);
@@ -159,6 +162,34 @@
   };
 }
 
+/// CheckForSetJmpCall - Return true if there's a call to setjmp/sigsetjmp in
+/// this function.
+bool StackSlotColoring::CheckForSetJmpCall(const MachineFunction &MF) {
+  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) {
@@ -722,6 +753,16 @@
       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))
+    return false;
+
   // Gather spill slot references
   ScanForSpillSlotRefs(MF);
   InitializeSlots();





More information about the llvm-commits mailing list