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( | |
28 KeyType(Type, std::pair<IceString, int64_t>(Name, Offset))); | |
29 if (Index >= RelocatablePool.size()) { | |
30 RelocatablePool.resize(Index + 1); | |
31 void *Handle = NULL; | |
32 RelocatablePool[Index] = IceConstantRelocatable::create( | |
33 Cfg, Index, Type, Handle, Offset, Name); | |
34 } | |
35 IceConstantRelocatable *Constant = RelocatablePool[Index]; | |
36 assert(Constant); | |
37 return Constant; | |
38 } | |
39 uint32_t getSize() const { return RelocatablePool.size(); } | |
40 IceConstantRelocatable *getEntry(uint32_t Index) const { | |
41 assert(Index < RelocatablePool.size()); | |
42 return RelocatablePool[Index]; | |
43 } | |
44 | |
45 private: | |
46 // KeyType is a triple of {Type, Name, Offset}. | |
47 typedef std::pair<IceType, std::pair<IceString, int64_t> > KeyType; | |
48 // TODO: Cfg is being captured primarily for arena allocation for | |
49 // new IceConstants. If IceConstants live beyond a function/Cfg, | |
50 // they need to be allocated from a global arena and there needs to | |
51 // be appropriate locking. | |
52 IceCfg *Cfg; | |
53 // Use IceValueTranslation<> to map (Name,Type) pairs to an index. | |
54 IceValueTranslation<KeyType> NameToIndex; | |
55 std::vector<IceConstantRelocatable *> RelocatablePool; | |
56 }; | |
57 | |
58 IceCfg::IceCfg() | |
59 : Str(this), Name(""), TestPrefix(""), Type(IceType_void), | |
60 IsInternal(false), HasError(false), ErrorMessage(""), Entry(NULL), | |
61 NextInstNumber(1) { | |
JF
2014/04/04 04:05:26
Missing Allocator, Nodes, Variables, Args.
Can Co
Jim Stichnoth
2014/04/06 02:16:09
ConstantPool added.
It seems that Allocator can't
| |
62 ConstantPool = new IceConstantPool(this); | |
63 } | |
64 | |
65 IceCfg::~IceCfg() { delete ConstantPool; } | |
66 | |
67 void IceCfg::setError(const IceString &Message) { | |
68 HasError = true; | |
69 ErrorMessage = Message; | |
70 Str << "ICE translation error: " << ErrorMessage << "\n"; | |
71 } | |
72 | |
73 IceCfgNode *IceCfg::makeNode(const IceString &Name) { | |
74 uint32_t LabelIndex = Nodes.size(); | |
75 IceCfgNode *Node = IceCfgNode::create(this, LabelIndex, Name); | |
76 Nodes.push_back(Node); | |
77 return Node; | |
78 } | |
79 | |
80 // Create a new IceVariable with a particular type and an optional | |
81 // name. The Node argument is the node where the variable is defined. | |
82 IceVariable *IceCfg::makeVariable(IceType Type, const IceCfgNode *Node, | |
83 const IceString &Name) { | |
84 uint32_t Index = Variables.size(); | |
85 Variables.push_back(IceVariable::create(this, Type, Node, Index, Name)); | |
86 return Variables[Index]; | |
87 } | |
88 | |
89 void IceCfg::addArg(IceVariable *Arg) { | |
90 Arg->setIsArg(this); | |
91 Args.push_back(Arg); | |
92 } | |
93 | |
94 IceConstant *IceCfg::getConstantInt(IceType Type, uint64_t ConstantInt64) { | |
95 return IceConstantInteger::create(this, Type, ConstantInt64); | |
96 } | |
97 | |
98 // TODO: Add float and double constants to the global constant pool, | |
99 // instead of creating a new instance each time. | |
100 IceConstant *IceCfg::getConstantFloat(float ConstantFloat) { | |
101 return IceConstantFloat::create(this, IceType_f32, ConstantFloat); | |
102 } | |
103 | |
104 IceConstant *IceCfg::getConstantDouble(double ConstantDouble) { | |
105 return IceConstantDouble::create(this, IceType_f64, ConstantDouble); | |
106 } | |
107 | |
108 IceConstant *IceCfg::getConstantSym(IceType Type, const void *Handle, | |
109 int64_t Offset, const IceString &Name, | |
110 bool SuppressMangling) { | |
111 IceConstantRelocatable *Const = | |
112 ConstantPool->getOrAddRelocatable(Type, Handle, Offset, Name); | |
113 Const->setSuppressMangling(SuppressMangling); | |
114 return Const; | |
115 } | |
116 | |
117 void IceCfg::registerEdges() { | |
118 for (IceNodeList::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I) { | |
119 (*I)->registerEdges(); | |
120 } | |
121 } | |
122 | |
123 // ======================== Dump routines ======================== // | |
124 | |
125 void IceCfg::dump() const { | |
126 Str.setCurrentNode(getEntryNode()); | |
127 // Print function name+args | |
128 if (Str.isVerbose(IceV_Instructions)) { | |
129 Str << "define "; | |
130 if (getInternal()) | |
131 Str << "internal "; | |
132 Str << Type << " @" << Name << "("; | |
133 for (uint32_t i = 0; i < Args.size(); ++i) { | |
134 if (i > 0) | |
135 Str << ", "; | |
136 Str << Args[i]->getType() << " " << Args[i]; | |
137 } | |
138 Str << ") {\n"; | |
139 } | |
140 Str.setCurrentNode(NULL); | |
141 // Print each basic block | |
142 for (IceNodeList::const_iterator I = Nodes.begin(), E = Nodes.end(); I != E; | |
143 ++I) { | |
144 (*I)->dump(Str); | |
145 } | |
146 if (Str.isVerbose(IceV_Instructions)) { | |
147 Str << "}\n"; | |
148 } | |
149 } | |
OLD | NEW |