Subzero: Refactor Operand::dump().

A "standalone" version of dump() is provided, taking just an Ostream
argument and not requiring a Cfg or GlobalContext argument.

BUG= none
R=kschimpf@google.com

Review URL: https://codereview.chromium.org/570713006
diff --git a/src/IceInstX8632.cpp b/src/IceInstX8632.cpp
index 373ab59..06804f3 100644
--- a/src/IceInstX8632.cpp
+++ b/src/IceInstX8632.cpp
@@ -1356,11 +1356,6 @@
   dumpSources(Func);
 }
 
-void OperandX8632::dump(const Cfg *Func) const {
-  Ostream &Str = Func->getContext()->getStrDump();
-  Str << "<OperandX8632>";
-}
-
 void OperandX8632Mem::emit(const Cfg *Func) const {
   Ostream &Str = Func->getContext()->getStrEmit();
   Str << TypeX8632Attributes[getType()].WidthString << " ";
@@ -1405,8 +1400,7 @@
   Str << "]";
 }
 
-void OperandX8632Mem::dump(const Cfg *Func) const {
-  Ostream &Str = Func->getContext()->getStrDump();
+void OperandX8632Mem::dump(const Cfg *Func, Ostream &Str) const {
   if (SegmentReg != DefaultSegment) {
     assert(SegmentReg >= 0 && SegmentReg < SegReg_NUM);
     Str << InstX8632SegmentRegNames[SegmentReg] << ":";
@@ -1414,7 +1408,10 @@
   bool Dumped = false;
   Str << "[";
   if (Base) {
-    Base->dump(Func);
+    if (Func)
+      Base->dump(Func);
+    else
+      Base->dump(Str);
     Dumped = true;
   }
   if (Index) {
@@ -1422,7 +1419,10 @@
     Str << "+";
     if (Shift > 0)
       Str << (1u << Shift) << "*";
-    Index->dump(Func);
+    if (Func)
+      Index->dump(Func);
+    else
+      Index->dump(Str);
     Dumped = true;
   }
   // Pretty-print the Offset.
@@ -1438,11 +1438,11 @@
     if (!OffsetIsZero) {     // Suppress if Offset is known to be 0
       if (!OffsetIsNegative) // Suppress if Offset is known to be negative
         Str << "+";
-      Offset->dump(Func);
+      Offset->dump(Func, Str);
     }
   } else {
     // There is only the offset.
-    Offset->dump(Func);
+    Offset->dump(Func, Str);
   }
   Str << "]";
 }
@@ -1468,8 +1468,7 @@
   Str << "]";
 }
 
-void VariableSplit::dump(const Cfg *Func) const {
-  Ostream &Str = Func->getContext()->getStrDump();
+void VariableSplit::dump(const Cfg *Func, Ostream &Str) const {
   switch (Part) {
   case Low:
     Str << "low";
@@ -1482,7 +1481,10 @@
     break;
   }
   Str << "(";
-  Var->dump(Func);
+  if (Func)
+    Var->dump(Func);
+  else
+    Var->dump(Str);
   Str << ")";
 }
 
diff --git a/src/IceInstX8632.h b/src/IceInstX8632.h
index 918b774..ed538f4 100644
--- a/src/IceInstX8632.h
+++ b/src/IceInstX8632.h
@@ -35,7 +35,10 @@
     kSplit
   };
   virtual void emit(const Cfg *Func) const = 0;
-  void dump(const Cfg *Func) const;
+  using Operand::dump;
+  virtual void dump(const Cfg *, Ostream &Str) const {
+    Str << "<OperandX8632>";
+  }
 
 protected:
   OperandX8632(OperandKindX8632 Kind, Type Ty)
@@ -72,7 +75,8 @@
   uint16_t getShift() const { return Shift; }
   SegmentRegisters getSegmentRegister() const { return SegmentReg; }
   virtual void emit(const Cfg *Func) const;
-  virtual void dump(const Cfg *Func) const;
+  using OperandX8632::dump;
+  virtual void dump(const Cfg *Func, Ostream &Str) const;
 
   static bool classof(const Operand *Operand) {
     return Operand->getKind() == static_cast<OperandKind>(kMem);
@@ -107,7 +111,8 @@
     return new (Func->allocate<VariableSplit>()) VariableSplit(Func, Var, Part);
   }
   virtual void emit(const Cfg *Func) const;
-  virtual void dump(const Cfg *Func) const;
+  using OperandX8632::dump;
+  virtual void dump(const Cfg *Func, Ostream &Str) const;
 
   static bool classof(const Operand *Operand) {
     return Operand->getKind() == static_cast<OperandKind>(kSplit);
diff --git a/src/IceOperand.cpp b/src/IceOperand.cpp
index b718ba5..1f232dd 100644
--- a/src/IceOperand.cpp
+++ b/src/IceOperand.cpp
@@ -201,8 +201,11 @@
   Func->getTarget()->emitVariable(this, Func);
 }
 
-void Variable::dump(const Cfg *Func) const {
-  Ostream &Str = Func->getContext()->getStrDump();
+void Variable::dump(const Cfg *Func, Ostream &Str) const {
+  if (Func == NULL) {
+    Str << "%" << getName();
+    return;
+  }
   const CfgNode *CurrentNode = Func->getCurrentNode();
   (void)CurrentNode; // used only in assert()
   assert(CurrentNode == NULL || DefNode == NULL || DefNode == CurrentNode);
@@ -241,8 +244,7 @@
   }
 }
 
-void ConstantRelocatable::dump(GlobalContext *Ctx) const {
-  Ostream &Str = Ctx->getStrDump();
+void ConstantRelocatable::dump(const Cfg *, Ostream &Str) const {
   Str << "@" << Name;
   if (Offset)
     Str << "+" << Offset;
diff --git a/src/IceOperand.h b/src/IceOperand.h
index 7e7c4f0..c46330d 100644
--- a/src/IceOperand.h
+++ b/src/IceOperand.h
@@ -51,7 +51,14 @@
     return Vars[I];
   }
   virtual void emit(const Cfg *Func) const = 0;
-  virtual void dump(const Cfg *Func) const = 0;
+  // The dump(Func,Str) implementation must be sure to handle the
+  // situation where Func==NULL.
+  virtual void dump(const Cfg *Func, Ostream &Str) const = 0;
+  void dump(const Cfg *Func) const {
+    assert(Func);
+    dump(Func, Func->getContext()->getStrDump());
+  }
+  void dump(Ostream &Str) const { dump(NULL, Str); }
 
   // Query whether this object was allocated in isolation, or added to
   // some higher-level pool.  This determines whether a containing
@@ -82,10 +89,10 @@
 class Constant : public Operand {
 public:
   uint32_t getPoolEntryID() const { return PoolEntryID; }
+  using Operand::dump;
   virtual void emit(const Cfg *Func) const { emit(Func->getContext()); }
-  virtual void dump(const Cfg *Func) const { dump(Func->getContext()); }
   virtual void emit(GlobalContext *Ctx) const = 0;
-  virtual void dump(GlobalContext *Ctx) const = 0;
+  virtual void dump(const Cfg *Func, Ostream &Str) const = 0;
 
   static bool classof(const Operand *Operand) {
     OperandKind Kind = Operand->getKind();
@@ -124,10 +131,7 @@
   // specialization.
   virtual void emit(GlobalContext *Ctx) const;
   using Constant::dump;
-  virtual void dump(GlobalContext *Ctx) const {
-    Ostream &Str = Ctx->getStrDump();
-    Str << getValue();
-  }
+  virtual void dump(const Cfg *, Ostream &Str) const { Str << getValue(); }
 
   static bool classof(const Operand *Operand) {
     return Operand->getKind() == K;
@@ -146,8 +150,7 @@
 typedef ConstantPrimitive<float, Operand::kConstFloat> ConstantFloat;
 typedef ConstantPrimitive<double, Operand::kConstDouble> ConstantDouble;
 
-template <> inline void ConstantInteger::dump(GlobalContext *Ctx) const {
-  Ostream &Str = Ctx->getStrDump();
+template <> inline void ConstantInteger::dump(const Cfg *, Ostream &Str) const {
   if (getType() == IceType_i1)
     Str << (getValue() ? "true" : "false");
   else
@@ -193,7 +196,7 @@
   using Constant::emit;
   using Constant::dump;
   virtual void emit(GlobalContext *Ctx) const;
-  virtual void dump(GlobalContext *Ctx) const;
+  virtual void dump(const Cfg *Func, Ostream &Str) const;
 
   static bool classof(const Operand *Operand) {
     OperandKind Kind = Operand->getKind();
@@ -225,14 +228,10 @@
   }
 
   using Constant::emit;
+  using Constant::dump;
   // The target needs to implement this.
   virtual void emit(GlobalContext *Ctx) const;
-
-  using Constant::dump;
-  virtual void dump(GlobalContext *Ctx) const {
-    Ostream &Str = Ctx->getStrEmit();
-    Str << "undef";
-  }
+  virtual void dump(const Cfg *, Ostream &Str) const { Str << "undef"; }
 
   static bool classof(const Operand *Operand) {
     return Operand->getKind() == kConstUndef;
@@ -415,7 +414,8 @@
   Variable asType(Type Ty);
 
   virtual void emit(const Cfg *Func) const;
-  virtual void dump(const Cfg *Func) const;
+  using Operand::dump;
+  virtual void dump(const Cfg *Func, Ostream &Str) const;
 
   static bool classof(const Operand *Operand) {
     return Operand->getKind() == kVariable;