OLD | NEW |
(Empty) | |
| 1 /* Copyright 2014 The Native Client Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can |
| 3 * be found in the LICENSE file. |
| 4 */ |
| 5 |
| 6 #include "IceCfg.h" |
| 7 #include "IceCfgNode.h" |
| 8 #include "IceInst.h" |
| 9 #include "IceOperand.h" |
| 10 |
| 11 IceCfgNode::IceCfgNode(IceCfg *Cfg, uint32_t LabelNumber, IceString Name) |
| 12 : Cfg(Cfg), Number(LabelNumber), Name(Name) {} |
| 13 |
| 14 // Returns the name the node was created with. If no name was given, |
| 15 // it synthesizes a (hopefully) unique name. |
| 16 IceString IceCfgNode::getName(void) const { |
| 17 if (Name != "") |
| 18 return Name; |
| 19 char buf[30]; |
| 20 sprintf(buf, "__%u", getIndex()); |
| 21 return buf; |
| 22 } |
| 23 |
| 24 // Adds an instruction to either the Phi list or the regular |
| 25 // instruction list. Validates that all Phis are added before all |
| 26 // regular instructions. |
| 27 void IceCfgNode::appendInst(IceInst *Inst) { |
| 28 if (IceInstPhi *Phi = llvm::dyn_cast<IceInstPhi>(Inst)) { |
| 29 if (!Insts.empty()) { |
| 30 Cfg->setError("Phi instruction added to the middle of a block"); |
| 31 return; |
| 32 } |
| 33 Phis.push_back(Phi); |
| 34 } else { |
| 35 Insts.push_back(Inst); |
| 36 } |
| 37 Inst->updateVars(this); |
| 38 } |
| 39 |
| 40 // When a node is created, the OutEdges are immediately knows, but the |
| 41 // InEdges have to be built up incrementally. After the CFG has been |
| 42 // constructed, the registerEdges() pass finalizes it by creating the |
| 43 // InEdges list. |
| 44 void IceCfgNode::registerEdges(void) { |
| 45 OutEdges = (*Insts.rbegin())->getTerminatorEdges(); |
| 46 for (IceNodeList::const_iterator I = OutEdges.begin(), E = OutEdges.end(); |
| 47 I != E; ++I) { |
| 48 IceCfgNode *Node = *I; |
| 49 Node->InEdges.push_back(this); |
| 50 } |
| 51 } |
| 52 |
| 53 // ======================== Dump routines ======================== // |
| 54 |
| 55 void IceCfgNode::dump(IceOstream &Str) const { |
| 56 Str.setCurrentNode(this); |
| 57 if (Str.isVerbose(IceV_Instructions)) { |
| 58 Str << getName() << ":\n"; |
| 59 } |
| 60 // Dump list of predecessor nodes. |
| 61 if (Str.isVerbose(IceV_Preds) && !InEdges.empty()) { |
| 62 Str << " // preds = "; |
| 63 for (IceNodeList::const_iterator I = InEdges.begin(), E = InEdges.end(); |
| 64 I != E; ++I) { |
| 65 if (I != InEdges.begin()) |
| 66 Str << ", "; |
| 67 Str << "%" << (*I)->getName(); |
| 68 } |
| 69 Str << "\n"; |
| 70 } |
| 71 // Dump each instruction. |
| 72 if (Str.isVerbose(IceV_Instructions)) { |
| 73 for (IcePhiList::const_iterator I = Phis.begin(), E = Phis.end(); I != E; |
| 74 ++I) { |
| 75 Str << (*I); |
| 76 } |
| 77 IceInstList::const_iterator I = Insts.begin(), E = Insts.end(); |
| 78 while (I != E) { |
| 79 IceInst *Inst = *I++; |
| 80 Str << Inst; |
| 81 } |
| 82 } |
| 83 // Dump list of successor nodes. |
| 84 if (Str.isVerbose(IceV_Succs)) { |
| 85 Str << " // succs = "; |
| 86 for (IceNodeList::const_iterator I = OutEdges.begin(), E = OutEdges.end(); |
| 87 I != E; ++I) { |
| 88 if (I != OutEdges.begin()) |
| 89 Str << ", "; |
| 90 Str << "%" << (*I)->getName(); |
| 91 } |
| 92 Str << "\n"; |
| 93 } |
| 94 } |
OLD | NEW |