Rearrange emit vs emitIAS. Wait till function is done before dumping text.

Eventually, I wanted to have a flag "UseELFWriter" like:
https://codereview.chromium.org/678533005/diff/120001/src/IceCfg.cpp

Where the emit OStream would not have text, and only have
binary. This refactor hopefully means fewer places to
check for a flag to disable the text version of IAS,
and be able to write binary. Otherwise, there are some
text labels for branches that are still being dumped out.

BUG=none
R=stichnot@chromium.org

Review URL: https://codereview.chromium.org/700263003
diff --git a/src/IceCfgNode.cpp b/src/IceCfgNode.cpp
index 52789b9..543f4e9 100644
--- a/src/IceCfgNode.cpp
+++ b/src/IceCfgNode.cpp
@@ -864,6 +864,24 @@
   }
 }
 
+void updateStats(Cfg *Func, const Inst *I) {
+  // Update emitted instruction count, plus fill/spill count for
+  // Variable operands without a physical register.
+  if (uint32_t Count = I->getEmitInstCount()) {
+    Func->getContext()->statsUpdateEmitted(Count);
+    if (Variable *Dest = I->getDest()) {
+      if (!Dest->hasReg())
+        Func->getContext()->statsUpdateFills();
+    }
+    for (SizeT S = 0; S < I->getSrcSize(); ++S) {
+      if (Variable *Src = llvm::dyn_cast<Variable>(I->getSrc(S))) {
+        if (!Src->hasReg())
+          Func->getContext()->statsUpdateSpills();
+      }
+    }
+  }
+}
+
 } // end of anonymous namespace
 
 void CfgNode::emit(Cfg *Func) const {
@@ -871,14 +889,7 @@
   Ostream &Str = Func->getContext()->getStrEmit();
   Liveness *Liveness = Func->getLiveness();
   bool DecorateAsm = Liveness && Func->getContext()->getFlags().DecorateAsm;
-  if (Func->getEntryNode() == this) {
-    Str << Func->getContext()->mangleName(Func->getFunctionName()) << ":\n";
-  }
   Str << getAsmName() << ":\n";
-  if (Func->useIntegratedAssembler()) {
-    Assembler *Asm = Func->getAssembler<Assembler>();
-    Asm->BindCfgNodeLabel(getIndex());
-  }
   std::vector<SizeT> LiveRegCount(Func->getTarget()->getNumRegisters());
   if (DecorateAsm)
     emitRegisterUsage(Str, Func, this, true, LiveRegCount);
@@ -899,34 +910,37 @@
         ++LiveRegCount[Dest->getRegNum()];
       continue;
     }
-    if (Func->useIntegratedAssembler()) {
-      I->emitIAS(Func);
-    } else {
-      I->emit(Func);
-      if (DecorateAsm)
-        emitLiveRangesEnded(Str, Func, I, LiveRegCount);
-      Str << "\n";
-    }
-    // Update emitted instruction count, plus fill/spill count for
-    // Variable operands without a physical register.
-    if (uint32_t Count = I->getEmitInstCount()) {
-      Func->getContext()->statsUpdateEmitted(Count);
-      if (Variable *Dest = I->getDest()) {
-        if (!Dest->hasReg())
-          Func->getContext()->statsUpdateFills();
-      }
-      for (SizeT S = 0; S < I->getSrcSize(); ++S) {
-        if (Variable *Src = llvm::dyn_cast<Variable>(I->getSrc(S))) {
-          if (!Src->hasReg())
-            Func->getContext()->statsUpdateSpills();
-        }
-      }
-    }
+    I->emit(Func);
+    if (DecorateAsm)
+      emitLiveRangesEnded(Str, Func, I, LiveRegCount);
+    Str << "\n";
+    updateStats(Func, I);
   }
   if (DecorateAsm)
     emitRegisterUsage(Str, Func, this, false, LiveRegCount);
 }
 
+void CfgNode::emitIAS(Cfg *Func) const {
+  Func->setCurrentNode(this);
+  Assembler *Asm = Func->getAssembler<Assembler>();
+  Asm->BindCfgNodeLabel(getIndex());
+  for (InstPhi *Phi : Phis) {
+    if (Phi->isDeleted())
+      continue;
+    // Emitting a Phi instruction should cause an error.
+    Inst *Instr = Phi;
+    Instr->emitIAS(Func);
+  }
+  for (Inst *I : Insts) {
+    if (I->isDeleted())
+      continue;
+    if (I->isRedundantAssign())
+      continue;
+    I->emitIAS(Func);
+    updateStats(Func, I);
+  }
+}
+
 void CfgNode::dump(Cfg *Func) const {
   Func->setCurrentNode(this);
   Ostream &Str = Func->getContext()->getStrDump();