OLD | NEW |
(Empty) | |
| 1 //===- subzero/src/IceCfg.cpp - Control flow graph implementation ---------===// |
| 2 // |
| 3 // The Subzero Code Generator |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This file implements the IceCfg class, including constant pool |
| 11 // management. |
| 12 // |
| 13 //===----------------------------------------------------------------------===// |
| 14 |
| 15 #include "IceCfg.h" |
| 16 #include "IceCfgNode.h" |
| 17 #include "IceDefs.h" |
| 18 #include "IceInst.h" |
| 19 #include "IceOperand.h" |
| 20 |
| 21 class IceConstantPool { |
| 22 public: |
| 23 IceConstantPool(IceCfg *Cfg) : Cfg(Cfg) {} |
| 24 IceConstantRelocatable *getOrAddRelocatable(IceType Type, const void *Handle, |
| 25 int64_t Offset, |
| 26 const IceString &Name) { |
| 27 uint32_t Index = NameToIndex.translate(KeyType(Name, Type)); |
| 28 if (Index >= RelocatablePool.size()) { |
| 29 RelocatablePool.resize(Index + 1); |
| 30 void *Handle = NULL; |
| 31 RelocatablePool[Index] = IceConstantRelocatable::create( |
| 32 Cfg, Index, Type, Handle, Offset, Name); |
| 33 } |
| 34 IceConstantRelocatable *Constant = RelocatablePool[Index]; |
| 35 assert(Constant); |
| 36 return Constant; |
| 37 } |
| 38 uint32_t getSize() const { return RelocatablePool.size(); } |
| 39 IceConstantRelocatable *getEntry(uint32_t Index) const { |
| 40 assert(Index < RelocatablePool.size()); |
| 41 return RelocatablePool[Index]; |
| 42 } |
| 43 |
| 44 private: |
| 45 typedef std::pair<IceString, IceType> KeyType; |
| 46 // TODO: Cfg is being captured primarily for arena allocation for |
| 47 // new IceConstants. If IceConstants live beyond a function/Cfg, |
| 48 // they need to be allocated from a global arena and there needs to |
| 49 // be appropriate locking. |
| 50 IceCfg *Cfg; |
| 51 // Use IceValueTranslation<> to map (Name,Type) pairs to an index. |
| 52 IceValueTranslation<KeyType> NameToIndex; |
| 53 std::vector<IceConstantRelocatable *> RelocatablePool; |
| 54 }; |
| 55 |
| 56 IceCfg::IceCfg() |
| 57 : Str(this), Name(""), TestPrefix(""), Type(IceType_void), |
| 58 IsInternal(false), HasError(false), ErrorMessage(""), Entry(NULL), |
| 59 NextInstNumber(1) { |
| 60 ConstantPool = new IceConstantPool(this); |
| 61 } |
| 62 |
| 63 IceCfg::~IceCfg() { delete ConstantPool; } |
| 64 |
| 65 void IceCfg::setError(const IceString &Message) { |
| 66 HasError = true; |
| 67 ErrorMessage = Message; |
| 68 Str << "ICE translation error: " << ErrorMessage << "\n"; |
| 69 } |
| 70 |
| 71 IceCfgNode *IceCfg::makeNode(const IceString &Name) { |
| 72 uint32_t LabelIndex = Nodes.size(); |
| 73 IceCfgNode *Node = IceCfgNode::create(this, LabelIndex, Name); |
| 74 Nodes.push_back(Node); |
| 75 return Node; |
| 76 } |
| 77 |
| 78 // Create a new IceVariable with a particular type and an optional |
| 79 // name. The Node argument is the node where the variable is defined. |
| 80 IceVariable *IceCfg::makeVariable(IceType Type, const IceCfgNode *Node, |
| 81 const IceString &Name) { |
| 82 uint32_t Index = Variables.size(); |
| 83 Variables.push_back(IceVariable::create(this, Type, Node, Index, Name)); |
| 84 return Variables[Index]; |
| 85 } |
| 86 |
| 87 void IceCfg::addArg(IceVariable *Arg) { |
| 88 Arg->setIsArg(this); |
| 89 Args.push_back(Arg); |
| 90 } |
| 91 |
| 92 IceConstant *IceCfg::getConstantInt(IceType Type, uint64_t ConstantInt64) { |
| 93 return IceConstantInteger::create(this, Type, ConstantInt64); |
| 94 } |
| 95 |
| 96 // TODO: Add float and double constants to the global constant pool, |
| 97 // instead of creating a new instance each time. |
| 98 IceConstant *IceCfg::getConstantFloat(float ConstantFloat) { |
| 99 return IceConstantFloat::create(this, IceType_f32, ConstantFloat); |
| 100 } |
| 101 |
| 102 IceConstant *IceCfg::getConstantDouble(double ConstantDouble) { |
| 103 return IceConstantDouble::create(this, IceType_f64, ConstantDouble); |
| 104 } |
| 105 |
| 106 IceConstant *IceCfg::getConstantSym(IceType Type, const void *Handle, |
| 107 int64_t Offset, const IceString &Name, |
| 108 bool SuppressMangling) { |
| 109 IceConstantRelocatable *Const = |
| 110 ConstantPool->getOrAddRelocatable(Type, Handle, Offset, Name); |
| 111 Const->setSuppressMangling(SuppressMangling); |
| 112 return Const; |
| 113 } |
| 114 |
| 115 void IceCfg::registerEdges() { |
| 116 for (IceNodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { |
| 117 (*I)->registerEdges(); |
| 118 } |
| 119 } |
| 120 |
| 121 // ======================== Dump routines ======================== // |
| 122 |
| 123 void IceCfg::dump() const { |
| 124 Str.setCurrentNode(getEntryNode()); |
| 125 // Print function name+args |
| 126 if (Str.isVerbose(IceV_Instructions)) { |
| 127 Str << "define "; |
| 128 if (getInternal()) |
| 129 Str << "internal "; |
| 130 Str << Type << " @" << Name << "("; |
| 131 for (uint32_t i = 0; i < Args.size(); ++i) { |
| 132 if (i > 0) |
| 133 Str << ", "; |
| 134 Str << Args[i]->getType() << " " << Args[i]; |
| 135 } |
| 136 Str << ") {\n"; |
| 137 } |
| 138 Str.setCurrentNode(NULL); |
| 139 // Print each basic block |
| 140 for (IceNodeList::const_iterator I = Nodes.begin(), E = Nodes.end(); I != E; |
| 141 ++I) { |
| 142 (*I)->dump(Str); |
| 143 } |
| 144 if (Str.isVerbose(IceV_Instructions)) { |
| 145 Str << "}\n"; |
| 146 } |
| 147 } |
OLD | NEW |