blob: dbf08c66ba3e5841223a47482c653a84914f871e [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
18#include "IceDefs.h"
19#include "IceTypes.h"
20#include "IceGlobalContext.h"
21
22#include "llvm/ADT/OwningPtr.h"
23#include "llvm/Support/Allocator.h"
24
25namespace Ice {
26
27class Cfg {
28public:
29 Cfg(GlobalContext *Ctx);
30 ~Cfg();
31
32 GlobalContext *getContext() const { return Ctx; }
33
34 // Manage the name and return type of the function being translated.
35 void setFunctionName(const IceString &Name) { FunctionName = Name; }
36 IceString getFunctionName() const { return FunctionName; }
37 void setReturnType(Type Ty) { ReturnType = Ty; }
38
39 // Manage the "internal" attribute of the function.
40 void setInternal(bool Internal) { IsInternalLinkage = Internal; }
41 bool getInternal() const { return IsInternalLinkage; }
42
43 // Translation error flagging. If support for some construct is
44 // known to be missing, instead of an assertion failure, setError()
45 // should be called and the error should be propagated back up.
46 // This way, we can gracefully fail to translate and let a fallback
47 // translator handle the function.
48 void setError(const IceString &Message);
49 bool hasError() const { return HasError; }
50 IceString getError() const { return ErrorMessage; }
51
52 // Manage nodes (a.k.a. basic blocks, CfgNodes).
53 void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; }
54 CfgNode *getEntryNode() const { return Entry; }
55 // Create a node and append it to the end of the linearized list.
56 CfgNode *makeNode(const IceString &Name = "");
57 SizeT getNumNodes() const { return Nodes.size(); }
58 const NodeList &getNodes() const { return Nodes; }
59
60 // Manage instruction numbering.
Jim Stichnothd97c7df2014-06-04 11:57:08 -070061 InstNumberT newInstNumber() { return NextInstNumber++; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070062
63 // Manage Variables.
64 Variable *makeVariable(Type Ty, const CfgNode *Node,
65 const IceString &Name = "");
66 SizeT getNumVariables() const { return Variables.size(); }
67 const VarList &getVariables() const { return Variables; }
68
69 // Manage arguments to the function.
70 void addArg(Variable *Arg);
71 const VarList &getArgs() const { return Args; }
72
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070073 // Miscellaneous accessors.
74 TargetLowering *getTarget() const { return Target.get(); }
Jim Stichnothd97c7df2014-06-04 11:57:08 -070075 Liveness *getLiveness() const { return Live.get(); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070076 bool hasComputedFrame() const;
77
78 // Passes over the CFG.
79 void translate();
Jim Stichnothf7c9a142014-04-29 10:52:43 -070080 // After the CFG is fully constructed, iterate over the nodes and
81 // compute the predecessor edges, in the form of
82 // CfgNode::InEdges[].
83 void computePredecessors();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070084 void renumberInstructions();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070085 void placePhiLoads();
86 void placePhiStores();
87 void deletePhis();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070088 void doAddressOpt();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070089 void genCode();
90 void genFrame();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070091 void livenessLightweight();
92 void liveness(LivenessMode Mode);
93 bool validateLiveness() const;
Jim Stichnothf7c9a142014-04-29 10:52:43 -070094
95 // Manage the CurrentNode field, which is used for validating the
96 // Variable::DefNode field during dumping/emitting.
97 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; }
98 const CfgNode *getCurrentNode() const { return CurrentNode; }
99
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700100 void emit();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700101 void dump(const IceString &Message = "");
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700102
103 // Allocate data of type T using the per-Cfg allocator.
104 template <typename T> T *allocate() { return Allocator.Allocate<T>(); }
105
106 // Allocate an instruction of type T using the per-Cfg instruction allocator.
107 template <typename T> T *allocateInst() { return Allocator.Allocate<T>(); }
108
109 // Allocate an array of data of type T using the per-Cfg allocator.
110 template <typename T> T *allocateArrayOf(size_t NumElems) {
111 return Allocator.Allocate<T>(NumElems);
112 }
113
114 // Deallocate data that was allocated via allocate<T>().
115 template <typename T> void deallocate(T *Object) {
116 Allocator.Deallocate(Object);
117 }
118
119 // Deallocate data that was allocated via allocateInst<T>().
120 template <typename T> void deallocateInst(T *Instr) {
121 Allocator.Deallocate(Instr);
122 }
123
124 // Deallocate data that was allocated via allocateArrayOf<T>().
125 template <typename T> void deallocateArrayOf(T *Array) {
126 Allocator.Deallocate(Array);
127 }
128
129private:
130 // TODO: for now, everything is allocated from the same allocator. In the
131 // future we may want to split this to several allocators, for example in
132 // order to use a "Recycler" to preserve memory. If we keep all allocation
133 // requests from the Cfg exposed via methods, we can always switch the
134 // implementation over at a later point.
135 llvm::BumpPtrAllocator Allocator;
136
137 GlobalContext *Ctx;
138 IceString FunctionName;
139 Type ReturnType;
140 bool IsInternalLinkage;
141 bool HasError;
142 IceString ErrorMessage;
143 CfgNode *Entry; // entry basic block
144 NodeList Nodes; // linearized node list; Entry should be first
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700145 InstNumberT NextInstNumber;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700146 VarList Variables;
147 VarList Args; // subset of Variables, in argument order
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700148 llvm::OwningPtr<Liveness> Live;
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700149 llvm::OwningPtr<TargetLowering> Target;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700150
151 // CurrentNode is maintained during dumping/emitting just for
152 // validating Variable::DefNode. Normally, a traversal over
153 // CfgNodes maintains this, but before global operations like
154 // register allocation, setCurrentNode(NULL) should be called to
155 // avoid spurious validation failures.
156 const CfgNode *CurrentNode;
157
158 Cfg(const Cfg &) LLVM_DELETED_FUNCTION;
159 Cfg &operator=(const Cfg &) LLVM_DELETED_FUNCTION;
160};
161
162} // end of namespace Ice
163
164#endif // SUBZERO_SRC_ICECFG_H