Add new optional getPassName() virtual function that a Pass can override
[oota-llvm.git] / lib / CodeGen / MachineInstr.cpp
index 8cbf55e840f3e9fc1814c9e589664ef784916c39..b113c602cf2c44926518dca2c219203eeb5c3950 100644 (file)
 //     7/2/01   -  Vikram Adve  -  Created
 //**************************************************************************/
 
-
 #include "llvm/CodeGen/MachineInstr.h"
-#include "llvm/Target/MachineRegInfo.h"
-#include "llvm/Method.h"
-#include "llvm/Instruction.h"
+#include "llvm/Value.h"
+#include <iostream>
+using std::cerr;
 
 
 //************************ Class Implementations **************************/
@@ -42,7 +41,7 @@ MachineInstr::MachineInstr(MachineOpCode _opCode,
 }
 
 void
-MachineInstr::SetMachineOperand(unsigned int i,
+MachineInstr::SetMachineOperandVal(unsigned int i,
                                MachineOperand::MachineOperandType operandType,
                                Value* _val, bool isdef=false)
 {
@@ -53,22 +52,25 @@ MachineInstr::SetMachineOperand(unsigned int i,
 }
 
 void
-MachineInstr::SetMachineOperand(unsigned int i,
+MachineInstr::SetMachineOperandConst(unsigned int i,
                                MachineOperand::MachineOperandType operandType,
-                               int64_t intValue, bool isdef=false)
+                                     int64_t intValue)
 {
   assert(i < operands.size());
+  assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
+         "immed. constant cannot be defined");
   operands[i].InitializeConst(operandType, intValue);
-  operands[i].isDef = isdef ||
-    TargetInstrDescriptors[opCode].resultPos == (int) i;
+  operands[i].isDef = false;
 }
 
 void
-MachineInstr::SetMachineOperand(unsigned int i,
-                               unsigned int regNum, bool isdef=false)
+MachineInstr::SetMachineOperandReg(unsigned int i,
+                                   int regNum,
+                                   bool isdef=false,
+                                   bool isCCReg=false)
 {
   assert(i < operands.size());
-  operands[i].InitializeReg(regNum);
+  operands[i].InitializeReg(regNum, isCCReg);
   operands[i].isDef = isdef ||
     TargetInstrDescriptors[opCode].resultPos == (int) i;
 }
@@ -77,43 +79,57 @@ void
 MachineInstr::dump(unsigned int indent) const 
 {
   for (unsigned i=0; i < indent; i++)
-    cout << "    ";
+    cerr << "    ";
   
-  cout << *this;
+  cerr << *this;
+}
+
+static inline std::ostream &OutputValue(std::ostream &os,
+                                        const Value* val)
+{
+  os << "(val ";
+  if (val && val->hasName())
+    return os << val->getName();
+  else
+    return os << (void*) val;              // print address only
+  os << ")";
 }
 
-ostream&
-operator<< (ostream& os, const MachineInstr& minstr)
+std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
 {
   os << TargetInstrDescriptors[minstr.opCode].opCodeString;
   
-  for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
+  for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
     os << "\t" << minstr.getOperand(i);
+    if( minstr.getOperand(i).opIsDef() ) 
+      os << "*";
+  }
   
-#undef DEBUG_VAL_OP_ITERATOR
-#ifdef DEBUG_VAL_OP_ITERATOR
-  os << endl << "\tValue operands are: ";
-  for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
-    {
-      const Value* val = *vo;
-      os << val << (vo.isDef()? "(def), " : ", ");
+  // code for printing implict references
+  unsigned NumOfImpRefs =  minstr.getNumImplicitRefs();
+  if(  NumOfImpRefs > 0 ) {
+    os << "\tImplicit: ";
+    for(unsigned z=0; z < NumOfImpRefs; z++) {
+      OutputValue(os, minstr.getImplicitRef(z)); 
+      if( minstr.implicitRefIsDefined(z)) os << "*";
+      os << "\t";
     }
-  os << endl;
-#endif
+  }
   
-  return os;
+  return os << "\n";
 }
 
-static inline ostream&
-OutputOperand(ostream &os, const MachineOperand &mop)
+static inline std::ostream &OutputOperand(std::ostream &os,
+                                          const MachineOperand &mop)
 {
+  Value* val;
   switch (mop.getOperandType())
     {
     case MachineOperand::MO_CCRegister:
     case MachineOperand::MO_VirtualRegister:
-      return os << "(val " << mop.getVRegValue() << ")";
+      return OutputValue(os, mop.getVRegValue());
     case MachineOperand::MO_MachineRegister:
-      return os << "("     << mop.getMachineRegNum() << ")";
+      return os << "(" << mop.getMachineRegNum() << ")";
     default:
       assert(0 && "Unknown operand type");
       return os;
@@ -121,8 +137,7 @@ OutputOperand(ostream &os, const MachineOperand &mop)
 }
 
 
-ostream&
-operator<<(ostream &os, const MachineOperand &mop)
+std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
 {
   switch(mop.opType)
     {
@@ -134,16 +149,19 @@ operator<<(ostream &os, const MachineOperand &mop)
       os << "%ccreg";
       return OutputOperand(os, mop);
     case MachineOperand::MO_SignExtendedImmed:
-      return os << mop.immedVal;
+      return os << (long)mop.immedVal;
     case MachineOperand::MO_UnextendedImmed:
-      return os << mop.immedVal;
+      return os << (long)mop.immedVal;
     case MachineOperand::MO_PCRelativeDisp:
       {
         const Value* opVal = mop.getVRegValue();
-        bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
-        return os << "%disp("
-                  << (isLabel? "label " : "addr-of-val ")
-                  << opVal << ")";
+        bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
+        os << "%disp(" << (isLabel? "label " : "addr-of-val ");
+        if (opVal->hasName())
+          os << opVal->getName();
+        else
+          os << opVal;
+        return os << ")";
       }
     default:
       assert(0 && "Unrecognized operand type");
@@ -152,26 +170,3 @@ operator<<(ostream &os, const MachineOperand &mop)
   
   return os;
 }
-
-
-void
-PrintMachineInstructions(const Method *const method)
-{
-  cout << "\n" << method->getReturnType()
-       << " \"" << method->getName() << "\"" << endl;
-  
-  for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
-    {
-      BasicBlock* bb = *BI;
-      cout << "\n"
-          << (bb->hasName()? bb->getName() : "Label")
-          << " (" << bb << ")" << ":"
-          << endl;
-      
-      MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
-      for (unsigned i=0; i < mvec.size(); i++)
-       cout << "\t" << *mvec[i] << endl;
-    } 
-  cout << endl << "End method \"" << method->getName() << "\""
-       << endl << endl;
-}