Remove using declarations
[oota-llvm.git] / lib / CodeGen / InstrSelection / InstrForest.cpp
index 660f3112d72a68f396c4151065d1a3cc15b338e0..c33faec9a04a6774f09bb2977f14aff52748a544 100644 (file)
@@ -1,12 +1,5 @@
-// $Id$
-//---------------------------------------------------------------------------
-// File:
-//     InstrForest.cpp
-// 
-// Purpose:
-//     Convert SSA graph to instruction trees for instruction selection.
-// 
-// Strategy:
+//===-- InstrForest.cpp - Build instruction forest for inst selection -----===//
+//
 //  The key goal is to group instructions into a single
 //  tree if one or more of them might be potentially combined into a single
 //  complex instruction in the target machine.
 //  and (2) O and I are part of the same basic block,
 //  and (3) O has only a single use, viz., I.
 // 
-// History:
-//     6/28/01  -  Vikram Adve  -  Created
-// 
-//---------------------------------------------------------------------------
+//===----------------------------------------------------------------------===//
 
 #include "llvm/CodeGen/InstrForest.h"
 #include "llvm/CodeGen/MachineCodeForInstruction.h"
-#include "llvm/Method.h"
+#include "llvm/Function.h"
 #include "llvm/iTerminators.h"
 #include "llvm/iMemory.h"
-#include "llvm/iPHINode.h"
-#include "llvm/ConstantVals.h"
-#include "llvm/BasicBlock.h"
+#include "llvm/Constant.h"
+#include "llvm/Type.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "Support/STLExtras.h"
-#include <iostream>
+#include <alloca.h>
 using std::cerr;
 using std::vector;
 
@@ -79,16 +68,20 @@ InstructionNode::InstructionNode(Instruction* I)
     {
       opLabel = AllocaN;                // Alloca(ptr, N) operation
     }
-  else if ((opLabel == Instruction::Load ||
-           opLabel == Instruction::GetElementPtr) &&
-          cast<MemAccessInst>(I)->hasIndices())
+  else if (opLabel == Instruction::GetElementPtr &&
+          cast<GetElementPtrInst>(I)->hasIndices())
     {
-      opLabel = opLabel + 100;          // load/getElem with index vector
+      opLabel = opLabel + 100;          // getElem with index vector
+    }
+  else if (opLabel == Instruction::Xor &&
+           BinaryOperator::isNot(I))
+    {
+      opLabel = (I->getType() == Type::BoolTy)?  NotOp  // boolean Not operator
+                                              : BNotOp; // bitwise Not operator
     }
   else if (opLabel == Instruction::And ||
            opLabel == Instruction::Or ||
-           opLabel == Instruction::Xor ||
-           opLabel == Instruction::Not)
+           opLabel == Instruction::Xor)
     {
       // Distinguish bitwise operators from logical operators!
       if (I->getType() != Type::BoolTy)
@@ -125,21 +118,8 @@ InstructionNode::dumpNode(int indent) const
 {
   for (int i=0; i < indent; i++)
     cerr << "    ";
-  
-  cerr << getInstruction()->getOpcodeName();
-  const MachineCodeForInstruction &mvec =
-    MachineCodeForInstruction::get(getInstruction());
-
-  if (mvec.size() > 0)
-    cerr << "\tMachine Instructions:  ";
-
-  for (unsigned int i=0; i < mvec.size(); ++i) {
-    mvec[i]->dump(0);
-    if (i < mvec.size() - 1)
-      cerr << ";  ";
-  }
-  
-  cerr << "\n";
+  cerr << getInstruction()->getOpcodeName()
+       << " [label " << getOpLabel() << "]" << "\n";
 }
 
 
@@ -188,20 +168,17 @@ LabelNode::dumpNode(int indent) const
 // A forest of instruction trees, usually for a single method.
 //------------------------------------------------------------------------ 
 
-InstrForest::InstrForest(Method *M)
+InstrForest::InstrForest(Function *F)
 {
-  for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
-    BasicBlock *BB = *MI;
-    for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
-      buildTreeForInstruction(*I);
+  for (Function::iterator BB = F->begin(), FE = F->end(); BB != FE; ++BB) {
+    for(BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
+      buildTreeForInstruction(I);
   }
 }
 
 InstrForest::~InstrForest()
 {
-  for (std::hash_map<const Instruction*,InstructionNode*>::iterator I=begin();
-       I != end(); ++I)
-      delete I->second;
+  for_each(treeRoots.begin(), treeRoots.end(), deleter<InstructionNode>);
 }
 
 void
@@ -224,7 +201,6 @@ inline void
 InstrForest::noteTreeNodeForInstr(Instruction *instr,
                                  InstructionNode *treeNode)
 {
-  assert(treeNode->getNodeType() == InstrTreeNode::NTInstructionNode);
   (*this)[instr] = treeNode;
   treeRoots.push_back(treeNode);       // mark node as root of a new tree
 }
@@ -235,8 +211,8 @@ InstrForest::setLeftChild(InstrTreeNode *parent, InstrTreeNode *child)
 {
   parent->LeftChild = child;
   child->Parent = parent;
-  if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
-    eraseRoot((InstructionNode*) child); // no longer a tree root
+  if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
+    eraseRoot(instrNode); // no longer a tree root
 }
 
 inline void
@@ -244,8 +220,8 @@ InstrForest::setRightChild(InstrTreeNode *parent, InstrTreeNode *child)
 {
   parent->RightChild = child;
   child->Parent = parent;
-  if (child->getNodeType() == InstrTreeNode::NTInstructionNode)
-    eraseRoot((InstructionNode*) child); // no longer a tree root
+  if (InstructionNode* instrNode = dyn_cast<InstructionNode>(child))
+    eraseRoot(instrNode); // no longer a tree root
 }
 
 
@@ -282,11 +258,8 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
   // if a fixed array is too small.
   // 
   int numChildren = 0;
-  const unsigned int MAX_CHILD = 8;
-  static InstrTreeNode *fixedChildArray[MAX_CHILD];
   InstrTreeNode **childArray =
-    (instr->getNumOperands() > MAX_CHILD)
-    ? new (InstrTreeNode*)[instr->getNumOperands()] : fixedChildArray;
+    (InstrTreeNode **)alloca(instr->getNumOperands()*sizeof(InstrTreeNode *));
   
   //
   // Walk the operands of the instruction
@@ -302,11 +275,11 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
     
       // Check latter condition here just to simplify the next IF.
       bool includeAddressOperand =
-       (isa<BasicBlock>(operand) || isa<Method>(operand))
+       (isa<BasicBlock>(operand) || isa<Function>(operand))
        && !instr->isTerminator();
     
       if (includeAddressOperand || isa<Instruction>(operand) ||
-         isa<Constant>(operand) || isa<MethodArgument>(operand) ||
+         isa<Constant>(operand) || isa<Argument>(operand) ||
          isa<GlobalVariable>(operand))
        {
          // This operand is a data value
@@ -329,7 +302,7 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
          InstrTreeNode* opTreeNode;
          if (isa<Instruction>(operand) && operand->use_size() == 1 &&
              cast<Instruction>(operand)->getParent() == instr->getParent() &&
-             !isa<PHINode>(instr) &&
+             instr->getOpcode() != Instruction::PHINode &&
              instr->getOpcode() != Instruction::Call)
            {
              // Recursively create a treeNode for it.
@@ -394,9 +367,5 @@ InstrForest::buildTreeForInstruction(Instruction *instr)
       setRightChild(parent, childArray[numChildren - 1]);
     }
   
-  if (childArray != fixedChildArray)
-    delete [] childArray; 
-  
   return treeNode;
 }
-