blob: b4c3748455829fb4e44a3325d6dc6f83d0953db2 [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
Jan Voung8acded02014-09-22 18:02:25 -070018#include "assembler.h"
19#include "IceClFlags.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070020#include "IceDefs.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070021#include "IceGlobalContext.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070022#include "IceTypes.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070023
24namespace Ice {
25
26class Cfg {
Jim Stichnothc6ead202015-02-24 09:30:30 -080027 Cfg() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -070028 Cfg(const Cfg &) = delete;
29 Cfg &operator=(const Cfg &) = delete;
30
Jim Stichnothf7c9a142014-04-29 10:52:43 -070031public:
Jim Stichnothf7c9a142014-04-29 10:52:43 -070032 ~Cfg();
33
Jim Stichnothbbca7542015-02-11 16:08:31 -080034 static std::unique_ptr<Cfg> create(GlobalContext *Ctx,
35 uint32_t SequenceNumber) {
36 return std::unique_ptr<Cfg>(new Cfg(Ctx, SequenceNumber));
Jim Stichnoth31c95592014-12-19 12:51:35 -080037 }
38 // Gets a pointer to the current thread's Cfg.
Jim Stichnotha5fe17a2015-01-26 11:10:03 -080039 static const Cfg *getCurrentCfg() { return ICE_TLS_GET_FIELD(CurrentCfg); }
Jim Stichnoth8e928382015-02-02 17:03:08 -080040 static void setCurrentCfg(const Cfg *Func) {
41 ICE_TLS_SET_FIELD(CurrentCfg, Func);
42 }
Jim Stichnoth31c95592014-12-19 12:51:35 -080043 // Gets a pointer to the current thread's Cfg's allocator.
Jan Voung1d62cf02015-01-09 14:57:32 -080044 static ArenaAllocator<> *getCurrentCfgAllocator() {
Jim Stichnotha5fe17a2015-01-26 11:10:03 -080045 assert(ICE_TLS_GET_FIELD(CurrentCfg));
46 return ICE_TLS_GET_FIELD(CurrentCfg)->Allocator.get();
Jim Stichnoth31c95592014-12-19 12:51:35 -080047 }
48
Jim Stichnothf7c9a142014-04-29 10:52:43 -070049 GlobalContext *getContext() const { return Ctx; }
Jim Stichnothbbca7542015-02-11 16:08:31 -080050 uint32_t getSequenceNumber() const { return SequenceNumber; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070051
Jim Stichnothfa4efea2015-01-27 05:06:03 -080052 // Returns true if any of the specified options in the verbose mask
53 // are set. If the argument is omitted, it checks if any verbose
54 // options at all are set.
55 bool isVerbose(VerboseMask Mask = IceV_All) const { return VMask & Mask; }
56 void setVerbose(VerboseMask Mask) { VMask = Mask; }
57
Jim Stichnothf7c9a142014-04-29 10:52:43 -070058 // Manage the name and return type of the function being translated.
59 void setFunctionName(const IceString &Name) { FunctionName = Name; }
60 IceString getFunctionName() const { return FunctionName; }
61 void setReturnType(Type Ty) { ReturnType = Ty; }
62
63 // Manage the "internal" attribute of the function.
64 void setInternal(bool Internal) { IsInternalLinkage = Internal; }
65 bool getInternal() const { return IsInternalLinkage; }
66
67 // Translation error flagging. If support for some construct is
68 // known to be missing, instead of an assertion failure, setError()
69 // should be called and the error should be propagated back up.
70 // This way, we can gracefully fail to translate and let a fallback
71 // translator handle the function.
72 void setError(const IceString &Message);
73 bool hasError() const { return HasError; }
74 IceString getError() const { return ErrorMessage; }
75
76 // Manage nodes (a.k.a. basic blocks, CfgNodes).
77 void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; }
78 CfgNode *getEntryNode() const { return Entry; }
79 // Create a node and append it to the end of the linearized list.
Jim Stichnoth668a7a32014-12-10 15:32:25 -080080 CfgNode *makeNode();
Jim Stichnothf7c9a142014-04-29 10:52:43 -070081 SizeT getNumNodes() const { return Nodes.size(); }
82 const NodeList &getNodes() const { return Nodes; }
Jim Stichnoth9a04c072014-12-11 15:51:42 -080083
84 typedef int32_t IdentifierIndexType;
Jim Stichnoth668a7a32014-12-10 15:32:25 -080085 // Adds a name to the list and returns its index, suitable for the
Jim Stichnoth9a04c072014-12-11 15:51:42 -080086 // argument to getIdentifierName(). No checking for duplicates is
87 // done. This is generally used for node names and variable names
88 // to avoid embedding a std::string inside an arena-allocated
89 // object.
90 IdentifierIndexType addIdentifierName(const IceString &Name) {
91 IdentifierIndexType Index = IdentifierNames.size();
92 IdentifierNames.push_back(Name);
Jim Stichnoth668a7a32014-12-10 15:32:25 -080093 return Index;
94 }
Jim Stichnoth9a04c072014-12-11 15:51:42 -080095 const IceString &getIdentifierName(IdentifierIndexType Index) const {
96 return IdentifierNames[Index];
97 }
Jim Stichnothdd842db2015-01-27 12:53:53 -080098 enum { IdentifierIndexInvalid = -1 };
Jim Stichnothf7c9a142014-04-29 10:52:43 -070099
100 // Manage instruction numbering.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700101 InstNumberT newInstNumber() { return NextInstNumber++; }
Jim Stichnoth47752552014-10-13 17:15:08 -0700102 InstNumberT getNextInstNumber() const { return NextInstNumber; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700103
104 // Manage Variables.
Jim Stichnoth800dab22014-09-20 12:25:02 -0700105 // Create a new Variable with a particular type and an optional
106 // name. The Node argument is the node where the variable is defined.
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800107 template <typename T = Variable> T *makeVariable(Type Ty) {
Jim Stichnoth800dab22014-09-20 12:25:02 -0700108 SizeT Index = Variables.size();
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800109 T *Var = T::create(this, Ty, Index);
Jim Stichnoth800dab22014-09-20 12:25:02 -0700110 Variables.push_back(Var);
111 return Var;
112 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700113 SizeT getNumVariables() const { return Variables.size(); }
114 const VarList &getVariables() const { return Variables; }
115
116 // Manage arguments to the function.
117 void addArg(Variable *Arg);
118 const VarList &getArgs() const { return Args; }
Matt Wala45a06232014-07-09 16:33:22 -0700119 VarList &getArgs() { return Args; }
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700120 void addImplicitArg(Variable *Arg);
121 const VarList &getImplicitArgs() const { return ImplicitArgs; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700122
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700123 // Miscellaneous accessors.
124 TargetLowering *getTarget() const { return Target.get(); }
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700125 VariablesMetadata *getVMetadata() const { return VMetadata.get(); }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700126 Liveness *getLiveness() const { return Live.get(); }
Jim Stichnothbbca7542015-02-11 16:08:31 -0800127 template <typename T = Assembler> T *getAssembler() const {
Jan Voung8acded02014-09-22 18:02:25 -0700128 return static_cast<T *>(TargetAssembler.get());
129 }
Jim Stichnothbbca7542015-02-11 16:08:31 -0800130 Assembler *releaseAssembler() { return TargetAssembler.release(); }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700131 bool hasComputedFrame() const;
Jim Stichnoth8363a062014-10-07 10:02:38 -0700132 bool getFocusedTiming() const { return FocusedTiming; }
133 void setFocusedTiming() { FocusedTiming = true; }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700134
135 // Passes over the CFG.
136 void translate();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700137 // After the CFG is fully constructed, iterate over the nodes and
138 // compute the predecessor edges, in the form of
139 // CfgNode::InEdges[].
140 void computePredecessors();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700141 void renumberInstructions();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700142 void placePhiLoads();
143 void placePhiStores();
144 void deletePhis();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700145 void advancedPhiLowering();
146 void reorderNodes();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700147 void doAddressOpt();
Matt Wala45a06232014-07-09 16:33:22 -0700148 void doArgLowering();
Matt Walac3302742014-08-15 16:21:56 -0700149 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700150 void genCode();
151 void genFrame();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700152 void livenessLightweight();
153 void liveness(LivenessMode Mode);
154 bool validateLiveness() const;
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700155 void contractEmptyNodes();
Jim Stichnothff9c7062014-09-18 04:50:49 -0700156 void doBranchOpt();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700157
158 // Manage the CurrentNode field, which is used for validating the
159 // Variable::DefNode field during dumping/emitting.
160 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; }
Jim Stichnothae953202014-12-20 06:17:49 -0800161 void resetCurrentNode() { setCurrentNode(nullptr); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700162 const CfgNode *getCurrentNode() const { return CurrentNode; }
163
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700164 void emit();
Jan Voung0faec4c2014-11-05 17:29:56 -0800165 void emitIAS();
Jim Stichnothbbca7542015-02-11 16:08:31 -0800166 static void emitTextHeader(const IceString &MangledName, GlobalContext *Ctx,
167 const Assembler *Asm);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700168 void dump(const IceString &Message = "");
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700169
170 // Allocate data of type T using the per-Cfg allocator.
Jim Stichnoth31c95592014-12-19 12:51:35 -0800171 template <typename T> T *allocate() { return Allocator->Allocate<T>(); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700172
173 // Allocate an array of data of type T using the per-Cfg allocator.
174 template <typename T> T *allocateArrayOf(size_t NumElems) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800175 return Allocator->Allocate<T>(NumElems);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700176 }
177
178 // Deallocate data that was allocated via allocate<T>().
179 template <typename T> void deallocate(T *Object) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800180 Allocator->Deallocate(Object);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700181 }
182
183 // Deallocate data that was allocated via allocateArrayOf<T>().
184 template <typename T> void deallocateArrayOf(T *Array) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800185 Allocator->Deallocate(Array);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700186 }
187
188private:
Jim Stichnothbbca7542015-02-11 16:08:31 -0800189 Cfg(GlobalContext *Ctx, uint32_t SequenceNumber);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700190
191 GlobalContext *Ctx;
Jim Stichnothbbca7542015-02-11 16:08:31 -0800192 uint32_t SequenceNumber; // output order for emission
Jim Stichnothfa4efea2015-01-27 05:06:03 -0800193 VerboseMask VMask;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700194 IceString FunctionName;
195 Type ReturnType;
196 bool IsInternalLinkage;
197 bool HasError;
Jim Stichnoth8363a062014-10-07 10:02:38 -0700198 bool FocusedTiming;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700199 IceString ErrorMessage;
200 CfgNode *Entry; // entry basic block
201 NodeList Nodes; // linearized node list; Entry should be first
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800202 std::vector<IceString> IdentifierNames;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700203 InstNumberT NextInstNumber;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700204 VarList Variables;
Jim Stichnothdd842db2015-01-27 12:53:53 -0800205 VarList Args; // subset of Variables, in argument order
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700206 VarList ImplicitArgs; // subset of Variables
Jan Voung1d62cf02015-01-09 14:57:32 -0800207 std::unique_ptr<ArenaAllocator<>> Allocator;
Jim Stichnotha18cc9c2014-09-30 19:10:22 -0700208 std::unique_ptr<Liveness> Live;
209 std::unique_ptr<TargetLowering> Target;
210 std::unique_ptr<VariablesMetadata> VMetadata;
211 std::unique_ptr<Assembler> TargetAssembler;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700212
213 // CurrentNode is maintained during dumping/emitting just for
214 // validating Variable::DefNode. Normally, a traversal over
215 // CfgNodes maintains this, but before global operations like
Jim Stichnoth800dab22014-09-20 12:25:02 -0700216 // register allocation, resetCurrentNode() should be called to avoid
217 // spurious validation failures.
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700218 const CfgNode *CurrentNode;
Jim Stichnoth31c95592014-12-19 12:51:35 -0800219
220 // Maintain a pointer in TLS to the current Cfg being translated.
221 // This is primarily for accessing its allocator statelessly, but
222 // other uses are possible.
Jim Stichnotha5fe17a2015-01-26 11:10:03 -0800223 ICE_TLS_DECLARE_FIELD(const Cfg *, CurrentCfg);
224
225public:
226 static void TlsInit() { ICE_TLS_INIT_FIELD(CurrentCfg); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700227};
228
229} // end of namespace Ice
230
231#endif // SUBZERO_SRC_ICECFG_H