blob: 15f353cb56226d331eee5fd198d1fef1d601d399 [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- subzero/src/IceCfg.h - Control flow graph ----------------*- C++ -*-===//
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 declares the Cfg class, which represents the control flow
11// graph and the overall per-function compilation context.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef SUBZERO_SRC_ICECFG_H
16#define SUBZERO_SRC_ICECFG_H
17
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070018#include <memory>
19
Jan Voung8acded02014-09-22 18:02:25 -070020#include "assembler.h"
21#include "IceClFlags.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070022#include "IceDefs.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070023#include "IceGlobalContext.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070024#include "IceTypes.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070025
26namespace Ice {
27
28class Cfg {
Jim Stichnoth7b451a92014-10-15 14:39:23 -070029 Cfg(const Cfg &) = delete;
30 Cfg &operator=(const Cfg &) = delete;
31
Jim Stichnothf7c9a142014-04-29 10:52:43 -070032public:
Jim Stichnothf7c9a142014-04-29 10:52:43 -070033 ~Cfg();
34
Jim Stichnoth31c95592014-12-19 12:51:35 -080035 // TODO(stichnot): Change this to return unique_ptr<Cfg>, and plumb
36 // it through the callers, to make ownership and lifetime and
37 // destruction requirements more explicit.
38 static Cfg *create(GlobalContext *Ctx) {
39 Cfg *Func = new Cfg(Ctx);
40 CurrentCfg = Func;
41 return Func;
42 }
43 // Gets a pointer to the current thread's Cfg.
44 static const Cfg *getCurrentCfg() { return CurrentCfg; }
45 // Gets a pointer to the current thread's Cfg's allocator.
Jan Voung1d62cf02015-01-09 14:57:32 -080046 static ArenaAllocator<> *getCurrentCfgAllocator() {
Jim Stichnoth31c95592014-12-19 12:51:35 -080047 assert(CurrentCfg);
48 return CurrentCfg->Allocator.get();
49 }
50
Jim Stichnothf7c9a142014-04-29 10:52:43 -070051 GlobalContext *getContext() const { return Ctx; }
52
53 // Manage the name and return type of the function being translated.
54 void setFunctionName(const IceString &Name) { FunctionName = Name; }
55 IceString getFunctionName() const { return FunctionName; }
56 void setReturnType(Type Ty) { ReturnType = Ty; }
57
58 // Manage the "internal" attribute of the function.
59 void setInternal(bool Internal) { IsInternalLinkage = Internal; }
60 bool getInternal() const { return IsInternalLinkage; }
61
62 // Translation error flagging. If support for some construct is
63 // known to be missing, instead of an assertion failure, setError()
64 // should be called and the error should be propagated back up.
65 // This way, we can gracefully fail to translate and let a fallback
66 // translator handle the function.
67 void setError(const IceString &Message);
68 bool hasError() const { return HasError; }
69 IceString getError() const { return ErrorMessage; }
70
71 // Manage nodes (a.k.a. basic blocks, CfgNodes).
72 void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; }
73 CfgNode *getEntryNode() const { return Entry; }
74 // Create a node and append it to the end of the linearized list.
Jim Stichnoth668a7a32014-12-10 15:32:25 -080075 CfgNode *makeNode();
Jim Stichnothf7c9a142014-04-29 10:52:43 -070076 SizeT getNumNodes() const { return Nodes.size(); }
77 const NodeList &getNodes() const { return Nodes; }
Jim Stichnoth9a04c072014-12-11 15:51:42 -080078
79 typedef int32_t IdentifierIndexType;
Jim Stichnoth668a7a32014-12-10 15:32:25 -080080 // Adds a name to the list and returns its index, suitable for the
Jim Stichnoth9a04c072014-12-11 15:51:42 -080081 // argument to getIdentifierName(). No checking for duplicates is
82 // done. This is generally used for node names and variable names
83 // to avoid embedding a std::string inside an arena-allocated
84 // object.
85 IdentifierIndexType addIdentifierName(const IceString &Name) {
86 IdentifierIndexType Index = IdentifierNames.size();
87 IdentifierNames.push_back(Name);
Jim Stichnoth668a7a32014-12-10 15:32:25 -080088 return Index;
89 }
Jim Stichnoth9a04c072014-12-11 15:51:42 -080090 const IceString &getIdentifierName(IdentifierIndexType Index) const {
91 return IdentifierNames[Index];
92 }
93 enum {
94 IdentifierIndexInvalid = -1
95 };
Jim Stichnothf7c9a142014-04-29 10:52:43 -070096
97 // Manage instruction numbering.
Jim Stichnothd97c7df2014-06-04 11:57:08 -070098 InstNumberT newInstNumber() { return NextInstNumber++; }
Jim Stichnoth47752552014-10-13 17:15:08 -070099 InstNumberT getNextInstNumber() const { return NextInstNumber; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700100
101 // Manage Variables.
Jim Stichnoth800dab22014-09-20 12:25:02 -0700102 // Create a new Variable with a particular type and an optional
103 // name. The Node argument is the node where the variable is defined.
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800104 template <typename T = Variable> T *makeVariable(Type Ty) {
Jim Stichnoth800dab22014-09-20 12:25:02 -0700105 SizeT Index = Variables.size();
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800106 T *Var = T::create(this, Ty, Index);
Jim Stichnoth800dab22014-09-20 12:25:02 -0700107 Variables.push_back(Var);
108 return Var;
109 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700110 SizeT getNumVariables() const { return Variables.size(); }
111 const VarList &getVariables() const { return Variables; }
112
113 // Manage arguments to the function.
114 void addArg(Variable *Arg);
115 const VarList &getArgs() const { return Args; }
Matt Wala45a06232014-07-09 16:33:22 -0700116 VarList &getArgs() { return Args; }
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700117 void addImplicitArg(Variable *Arg);
118 const VarList &getImplicitArgs() const { return ImplicitArgs; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700119
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700120 // Miscellaneous accessors.
121 TargetLowering *getTarget() const { return Target.get(); }
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700122 VariablesMetadata *getVMetadata() const { return VMetadata.get(); }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700123 Liveness *getLiveness() const { return Live.get(); }
Jan Voung8acded02014-09-22 18:02:25 -0700124 template <typename T> T *getAssembler() const {
125 return static_cast<T *>(TargetAssembler.get());
126 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700127 bool hasComputedFrame() const;
Jim Stichnoth8363a062014-10-07 10:02:38 -0700128 bool getFocusedTiming() const { return FocusedTiming; }
129 void setFocusedTiming() { FocusedTiming = true; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700130
131 // Passes over the CFG.
132 void translate();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700133 // After the CFG is fully constructed, iterate over the nodes and
134 // compute the predecessor edges, in the form of
135 // CfgNode::InEdges[].
136 void computePredecessors();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700137 void renumberInstructions();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700138 void placePhiLoads();
139 void placePhiStores();
140 void deletePhis();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700141 void advancedPhiLowering();
142 void reorderNodes();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700143 void doAddressOpt();
Matt Wala45a06232014-07-09 16:33:22 -0700144 void doArgLowering();
Matt Walac3302742014-08-15 16:21:56 -0700145 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700146 void genCode();
147 void genFrame();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700148 void livenessLightweight();
149 void liveness(LivenessMode Mode);
150 bool validateLiveness() const;
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700151 void contractEmptyNodes();
Jim Stichnothff9c7062014-09-18 04:50:49 -0700152 void doBranchOpt();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700153
154 // Manage the CurrentNode field, which is used for validating the
155 // Variable::DefNode field during dumping/emitting.
156 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; }
Jim Stichnothae953202014-12-20 06:17:49 -0800157 void resetCurrentNode() { setCurrentNode(nullptr); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700158 const CfgNode *getCurrentNode() const { return CurrentNode; }
159
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700160 void emit();
Jan Voung0faec4c2014-11-05 17:29:56 -0800161 void emitIAS();
162 void emitTextHeader(const IceString &MangledName);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700163 void dump(const IceString &Message = "");
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700164
165 // Allocate data of type T using the per-Cfg allocator.
Jim Stichnoth31c95592014-12-19 12:51:35 -0800166 template <typename T> T *allocate() { return Allocator->Allocate<T>(); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700167
168 // Allocate an array of data of type T using the per-Cfg allocator.
169 template <typename T> T *allocateArrayOf(size_t NumElems) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800170 return Allocator->Allocate<T>(NumElems);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700171 }
172
173 // Deallocate data that was allocated via allocate<T>().
174 template <typename T> void deallocate(T *Object) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800175 Allocator->Deallocate(Object);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700176 }
177
178 // Deallocate data that was allocated via allocateArrayOf<T>().
179 template <typename T> void deallocateArrayOf(T *Array) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800180 Allocator->Deallocate(Array);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700181 }
182
183private:
Jim Stichnoth31c95592014-12-19 12:51:35 -0800184 Cfg(GlobalContext *Ctx);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700185
186 GlobalContext *Ctx;
187 IceString FunctionName;
188 Type ReturnType;
189 bool IsInternalLinkage;
190 bool HasError;
Jim Stichnoth8363a062014-10-07 10:02:38 -0700191 bool FocusedTiming;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700192 IceString ErrorMessage;
193 CfgNode *Entry; // entry basic block
194 NodeList Nodes; // linearized node list; Entry should be first
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800195 std::vector<IceString> IdentifierNames;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700196 InstNumberT NextInstNumber;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700197 VarList Variables;
198 VarList Args; // subset of Variables, in argument order
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700199 VarList ImplicitArgs; // subset of Variables
Jan Voung1d62cf02015-01-09 14:57:32 -0800200 std::unique_ptr<ArenaAllocator<>> Allocator;
Jim Stichnotha18cc9c2014-09-30 19:10:22 -0700201 std::unique_ptr<Liveness> Live;
202 std::unique_ptr<TargetLowering> Target;
203 std::unique_ptr<VariablesMetadata> VMetadata;
204 std::unique_ptr<Assembler> TargetAssembler;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700205
206 // CurrentNode is maintained during dumping/emitting just for
207 // validating Variable::DefNode. Normally, a traversal over
208 // CfgNodes maintains this, but before global operations like
Jim Stichnoth800dab22014-09-20 12:25:02 -0700209 // register allocation, resetCurrentNode() should be called to avoid
210 // spurious validation failures.
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700211 const CfgNode *CurrentNode;
Jim Stichnoth31c95592014-12-19 12:51:35 -0800212
213 // Maintain a pointer in TLS to the current Cfg being translated.
214 // This is primarily for accessing its allocator statelessly, but
215 // other uses are possible.
216 thread_local static const Cfg *CurrentCfg;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700217};
218
219} // end of namespace Ice
220
221#endif // SUBZERO_SRC_ICECFG_H