diff --git a/lib/Transforms/Scalar/EarlyCSE.cpp b/lib/Transforms/Scalar/EarlyCSE.cpp
index 5241e11..37dc2d3 100644
--- a/lib/Transforms/Scalar/EarlyCSE.cpp
+++ b/lib/Transforms/Scalar/EarlyCSE.cpp
@@ -25,6 +25,7 @@
 #include "llvm/Support/RecyclingAllocator.h"
 #include "llvm/ADT/ScopedHashTable.h"
 #include "llvm/ADT/Statistic.h"
+#include <list>
 using namespace llvm;
 
 STATISTIC(NumSimplify, "Number of instructions simplified or DCE'd");
@@ -259,6 +260,11 @@ public:
   bool runOnFunction(Function &F);
 
 private:
+
+  // nodesToProcess - A stack implemented as a list of DomTreeNode* and
+  // the current generation.
+  typedef std::pair<unsigned, DomTreeNode *> StackNode;
+  std::list<StackNode> nodesToProcess;
   
   bool processNode(DomTreeNode *Node);
   
@@ -447,12 +453,17 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
     }
   }
   
-  unsigned LiveOutGeneration = CurrentGeneration;
+  std::list<StackNode> SubStack;
   for (DomTreeNode::iterator I = Node->begin(), E = Node->end(); I != E; ++I) {
-    Changed |= processNode(*I);
-    // Pop any generation changes off the stack from the recursive walk.
-    CurrentGeneration = LiveOutGeneration;
+    // Add the node and the current generation to the stack.
+    StackNode NodeToProcess(CurrentGeneration, *I);
+    SubStack.push_back(NodeToProcess);
+  }
+
+  if (!SubStack.empty()) {
+    nodesToProcess.splice(nodesToProcess.begin(), SubStack);
   }
+
   return Changed;
 }
 
@@ -471,5 +482,25 @@ bool EarlyCSE::runOnFunction(Function &F) {
   AvailableCalls = &CallTable;
   
   CurrentGeneration = 0;
-  return processNode(DT->getRootNode());
+
+  // Process the root node.
+  bool Changed = processNode(DT->getRootNode());
+
+  // Save the current generation.
+  unsigned LiveOutGeneration = CurrentGeneration;
+
+  // Process the stack.
+  while (!nodesToProcess.empty()) {
+    // Grab the first item off the stack. Set the current generation, remove
+    // the node from the stack, and process it.
+    StackNode NodeToProcess = nodesToProcess.front();
+    CurrentGeneration = NodeToProcess.first;
+    nodesToProcess.pop_front();
+    Changed |= processNode(NodeToProcess.second);
+  }
+
+  // Reset the current generation.
+  CurrentGeneration = LiveOutGeneration;
+
+  return Changed;
 }
