blob: 499e226a794103e79a994820191a6a4d7be870a9 [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//===----------------------------------------------------------------------===//
Andrew Scull9612d322015-07-06 14:53:25 -07009///
10/// \file
11/// This file declares the Cfg class, which represents the control flow
12/// graph and the overall per-function compilation context.
13///
Jim Stichnothf7c9a142014-04-29 10:52:43 -070014//===----------------------------------------------------------------------===//
15
16#ifndef SUBZERO_SRC_ICECFG_H
17#define SUBZERO_SRC_ICECFG_H
18
John Portoaff4ccf2015-06-10 16:35:06 -070019#include "IceAssembler.h"
Jan Voung8acded02014-09-22 18:02:25 -070020#include "IceClFlags.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070021#include "IceDefs.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070022#include "IceGlobalContext.h"
Jim Stichnotha18cc9c2014-09-30 19:10:22 -070023#include "IceTypes.h"
Jim Stichnothf7c9a142014-04-29 10:52:43 -070024
25namespace Ice {
26
27class Cfg {
Jim Stichnothc6ead202015-02-24 09:30:30 -080028 Cfg() = delete;
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 Stichnothbbca7542015-02-11 16:08:31 -080035 static std::unique_ptr<Cfg> create(GlobalContext *Ctx,
36 uint32_t SequenceNumber) {
37 return std::unique_ptr<Cfg>(new Cfg(Ctx, SequenceNumber));
Jim Stichnoth31c95592014-12-19 12:51:35 -080038 }
Andrew Scull9612d322015-07-06 14:53:25 -070039 /// Gets a pointer to the current thread's Cfg.
Jim Stichnotha5fe17a2015-01-26 11:10:03 -080040 static const Cfg *getCurrentCfg() { return ICE_TLS_GET_FIELD(CurrentCfg); }
Jim Stichnoth8e928382015-02-02 17:03:08 -080041 static void setCurrentCfg(const Cfg *Func) {
42 ICE_TLS_SET_FIELD(CurrentCfg, Func);
43 }
Andrew Scull9612d322015-07-06 14:53:25 -070044 /// Gets a pointer to the current thread's Cfg's allocator.
Jan Voung1d62cf02015-01-09 14:57:32 -080045 static ArenaAllocator<> *getCurrentCfgAllocator() {
Jim Stichnotha5fe17a2015-01-26 11:10:03 -080046 assert(ICE_TLS_GET_FIELD(CurrentCfg));
47 return ICE_TLS_GET_FIELD(CurrentCfg)->Allocator.get();
Jim Stichnoth31c95592014-12-19 12:51:35 -080048 }
49
Jim Stichnothf7c9a142014-04-29 10:52:43 -070050 GlobalContext *getContext() const { return Ctx; }
Jim Stichnothbbca7542015-02-11 16:08:31 -080051 uint32_t getSequenceNumber() const { return SequenceNumber; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070052
Andrew Scull9612d322015-07-06 14:53:25 -070053 /// Returns true if any of the specified options in the verbose mask
54 /// are set. If the argument is omitted, it checks if any verbose
55 /// options at all are set.
Jim Stichnothfa4efea2015-01-27 05:06:03 -080056 bool isVerbose(VerboseMask Mask = IceV_All) const { return VMask & Mask; }
57 void setVerbose(VerboseMask Mask) { VMask = Mask; }
58
Andrew Scull9612d322015-07-06 14:53:25 -070059 /// \name Manage the name and return type of the function being translated.
60 /// @{
Jim Stichnothf7c9a142014-04-29 10:52:43 -070061 void setFunctionName(const IceString &Name) { FunctionName = Name; }
62 IceString getFunctionName() const { return FunctionName; }
63 void setReturnType(Type Ty) { ReturnType = Ty; }
Andrew Scull9612d322015-07-06 14:53:25 -070064 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070065
Andrew Scull9612d322015-07-06 14:53:25 -070066 /// \name Manage the "internal" attribute of the function.
67 /// @{
Jim Stichnothf7c9a142014-04-29 10:52:43 -070068 void setInternal(bool Internal) { IsInternalLinkage = Internal; }
69 bool getInternal() const { return IsInternalLinkage; }
Andrew Scull9612d322015-07-06 14:53:25 -070070 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070071
Andrew Scull9612d322015-07-06 14:53:25 -070072 /// \name Manage errors.
73 /// @{
74
75 /// Translation error flagging. If support for some construct is
76 /// known to be missing, instead of an assertion failure, setError()
77 /// should be called and the error should be propagated back up.
78 /// This way, we can gracefully fail to translate and let a fallback
79 /// translator handle the function.
Jim Stichnothf7c9a142014-04-29 10:52:43 -070080 void setError(const IceString &Message);
81 bool hasError() const { return HasError; }
82 IceString getError() const { return ErrorMessage; }
Andrew Scull9612d322015-07-06 14:53:25 -070083 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070084
Andrew Scull9612d322015-07-06 14:53:25 -070085 /// \name Manage nodes (a.k.a. basic blocks, CfgNodes).
86 /// @{
Jim Stichnothf7c9a142014-04-29 10:52:43 -070087 void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; }
88 CfgNode *getEntryNode() const { return Entry; }
Andrew Scull9612d322015-07-06 14:53:25 -070089 /// Create a node and append it to the end of the linearized list.
Jim Stichnoth668a7a32014-12-10 15:32:25 -080090 CfgNode *makeNode();
Jim Stichnothf7c9a142014-04-29 10:52:43 -070091 SizeT getNumNodes() const { return Nodes.size(); }
92 const NodeList &getNodes() const { return Nodes; }
Andrew Scull9612d322015-07-06 14:53:25 -070093 /// @}
Jim Stichnoth9a04c072014-12-11 15:51:42 -080094
95 typedef int32_t IdentifierIndexType;
Andrew Scull9612d322015-07-06 14:53:25 -070096 /// Adds a name to the list and returns its index, suitable for the
97 /// argument to getIdentifierName(). No checking for duplicates is
98 /// done. This is generally used for node names and variable names
99 /// to avoid embedding a std::string inside an arena-allocated
100 /// object.
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800101 IdentifierIndexType addIdentifierName(const IceString &Name) {
102 IdentifierIndexType Index = IdentifierNames.size();
103 IdentifierNames.push_back(Name);
Jim Stichnoth668a7a32014-12-10 15:32:25 -0800104 return Index;
105 }
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800106 const IceString &getIdentifierName(IdentifierIndexType Index) const {
107 return IdentifierNames[Index];
108 }
Jim Stichnothdd842db2015-01-27 12:53:53 -0800109 enum { IdentifierIndexInvalid = -1 };
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700110
Andrew Scull9612d322015-07-06 14:53:25 -0700111 /// \name Manage instruction numbering.
112 /// @{
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700113 InstNumberT newInstNumber() { return NextInstNumber++; }
Jim Stichnoth47752552014-10-13 17:15:08 -0700114 InstNumberT getNextInstNumber() const { return NextInstNumber; }
Andrew Scull9612d322015-07-06 14:53:25 -0700115 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700116
Andrew Scull9612d322015-07-06 14:53:25 -0700117 /// \name Manage Variables.
118 /// @{
119
120 /// Create a new Variable with a particular type and an optional
121 /// name. The Node argument is the node where the variable is defined.
John Porto36087cd2015-06-24 16:16:13 -0700122 // TODO(jpp): untemplate this with two separate methods: makeVariable and
123 // makeSpillVariable.
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800124 template <typename T = Variable> T *makeVariable(Type Ty) {
Jim Stichnoth800dab22014-09-20 12:25:02 -0700125 SizeT Index = Variables.size();
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800126 T *Var = T::create(this, Ty, Index);
Jim Stichnoth800dab22014-09-20 12:25:02 -0700127 Variables.push_back(Var);
128 return Var;
129 }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700130 SizeT getNumVariables() const { return Variables.size(); }
131 const VarList &getVariables() const { return Variables; }
Andrew Scull9612d322015-07-06 14:53:25 -0700132 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700133
Andrew Scull9612d322015-07-06 14:53:25 -0700134 /// \name Manage arguments to the function.
135 /// @{
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700136 void addArg(Variable *Arg);
137 const VarList &getArgs() const { return Args; }
Matt Wala45a06232014-07-09 16:33:22 -0700138 VarList &getArgs() { return Args; }
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700139 void addImplicitArg(Variable *Arg);
140 const VarList &getImplicitArgs() const { return ImplicitArgs; }
Andrew Scull9612d322015-07-06 14:53:25 -0700141 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700142
Andrew Scull86df4e92015-07-30 13:54:44 -0700143 /// \name Manage the jump tables.
144 /// @{
145 void addJumpTable(InstJumpTable *JumpTable) {
146 JumpTables.emplace_back(JumpTable);
147 }
148 /// @}
149
Andrew Scull9612d322015-07-06 14:53:25 -0700150 /// \name Miscellaneous accessors.
151 /// @{
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700152 TargetLowering *getTarget() const { return Target.get(); }
Jim Stichnoth144cdce2014-09-22 16:02:59 -0700153 VariablesMetadata *getVMetadata() const { return VMetadata.get(); }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700154 Liveness *getLiveness() const { return Live.get(); }
Jim Stichnothbbca7542015-02-11 16:08:31 -0800155 template <typename T = Assembler> T *getAssembler() const {
John Porto2da710c2015-06-29 07:57:02 -0700156 return llvm::dyn_cast<T>(TargetAssembler.get());
Jan Voung8acded02014-09-22 18:02:25 -0700157 }
Jim Stichnothbbca7542015-02-11 16:08:31 -0800158 Assembler *releaseAssembler() { return TargetAssembler.release(); }
John Portof8b4cc82015-06-09 18:06:19 -0700159 std::unique_ptr<VariableDeclarationList> getGlobalInits() {
160 return std::move(GlobalInits);
161 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700162 bool hasComputedFrame() const;
Jim Stichnoth8363a062014-10-07 10:02:38 -0700163 bool getFocusedTiming() const { return FocusedTiming; }
164 void setFocusedTiming() { FocusedTiming = true; }
Andrew Scull9612d322015-07-06 14:53:25 -0700165 /// @}
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700166
Andrew Scull9612d322015-07-06 14:53:25 -0700167 /// Returns true if Var is a global variable that is used by the profiling
168 /// code.
John Portof8b4cc82015-06-09 18:06:19 -0700169 static bool isProfileGlobal(const VariableDeclaration &Var);
170
Andrew Scull9612d322015-07-06 14:53:25 -0700171 /// Passes over the CFG.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700172 void translate();
Andrew Scull9612d322015-07-06 14:53:25 -0700173 /// After the CFG is fully constructed, iterate over the nodes and
174 /// compute the predecessor and successor edges, in the form of
175 /// CfgNode::InEdges[] and CfgNode::OutEdges[].
Jim Stichnoth69d3f9c2015-03-23 10:33:38 -0700176 void computeInOutEdges();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700177 void renumberInstructions();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700178 void placePhiLoads();
179 void placePhiStores();
180 void deletePhis();
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700181 void advancedPhiLowering();
182 void reorderNodes();
Qining Lu969f6a32015-07-31 09:58:34 -0700183 void shuffleNodes();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700184 void doAddressOpt();
Matt Wala45a06232014-07-09 16:33:22 -0700185 void doArgLowering();
Matt Walac3302742014-08-15 16:21:56 -0700186 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700187 void genCode();
188 void genFrame();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700189 void livenessLightweight();
190 void liveness(LivenessMode Mode);
191 bool validateLiveness() const;
Jim Stichnoth336f6c42014-10-30 15:01:31 -0700192 void contractEmptyNodes();
Jim Stichnothff9c7062014-09-18 04:50:49 -0700193 void doBranchOpt();
Andrew Scull86df4e92015-07-30 13:54:44 -0700194 void markNodesForSandboxing();
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700195
Andrew Scull9612d322015-07-06 14:53:25 -0700196 /// \name Manage the CurrentNode field.
197 /// CurrentNode is used for validating the Variable::DefNode field during
198 /// dumping/emitting.
199 /// @{
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700200 void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; }
Jim Stichnothae953202014-12-20 06:17:49 -0800201 void resetCurrentNode() { setCurrentNode(nullptr); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700202 const CfgNode *getCurrentNode() const { return CurrentNode; }
Andrew Scull9612d322015-07-06 14:53:25 -0700203 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700204
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -0700205 void emit();
Jan Voung0faec4c2014-11-05 17:29:56 -0800206 void emitIAS();
Jim Stichnothbbca7542015-02-11 16:08:31 -0800207 static void emitTextHeader(const IceString &MangledName, GlobalContext *Ctx,
208 const Assembler *Asm);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700209 void dump(const IceString &Message = "");
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700210
Andrew Scull9612d322015-07-06 14:53:25 -0700211 /// Allocate data of type T using the per-Cfg allocator.
Jim Stichnoth31c95592014-12-19 12:51:35 -0800212 template <typename T> T *allocate() { return Allocator->Allocate<T>(); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700213
Andrew Scull9612d322015-07-06 14:53:25 -0700214 /// Allocate an array of data of type T using the per-Cfg allocator.
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700215 template <typename T> T *allocateArrayOf(size_t NumElems) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800216 return Allocator->Allocate<T>(NumElems);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700217 }
218
Andrew Scull9612d322015-07-06 14:53:25 -0700219 /// Deallocate data that was allocated via allocate<T>().
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700220 template <typename T> void deallocate(T *Object) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800221 Allocator->Deallocate(Object);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700222 }
223
Andrew Scull9612d322015-07-06 14:53:25 -0700224 /// Deallocate data that was allocated via allocateArrayOf<T>().
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700225 template <typename T> void deallocateArrayOf(T *Array) {
Jim Stichnoth31c95592014-12-19 12:51:35 -0800226 Allocator->Deallocate(Array);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700227 }
228
229private:
Jim Stichnothbbca7542015-02-11 16:08:31 -0800230 Cfg(GlobalContext *Ctx, uint32_t SequenceNumber);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700231
Andrew Scull9612d322015-07-06 14:53:25 -0700232 /// Adds a call to the ProfileSummary runtime function as the first
233 /// instruction in this CFG's entry block.
John Portof8b4cc82015-06-09 18:06:19 -0700234 void addCallToProfileSummary();
235
Andrew Scull9612d322015-07-06 14:53:25 -0700236 /// Iterates over the basic blocks in this CFG, adding profiling code to each
237 /// one of them. It returns a list with all the globals that the profiling
238 /// code needs to be defined.
John Portof8b4cc82015-06-09 18:06:19 -0700239 void profileBlocks();
240
Andrew Scull86df4e92015-07-30 13:54:44 -0700241 /// Delete registered jump table placeholder instructions. This should only be
242 /// called once all repointing has taken place.
243 void deleteJumpTableInsts();
244 /// Iterate through the registered jump tables and emit them.
245 void emitJumpTables();
246
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700247 GlobalContext *Ctx;
Andrew Scull9612d322015-07-06 14:53:25 -0700248 uint32_t SequenceNumber; /// output order for emission
Jim Stichnothfa4efea2015-01-27 05:06:03 -0800249 VerboseMask VMask;
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700250 IceString FunctionName = "";
251 Type ReturnType = IceType_void;
252 bool IsInternalLinkage = false;
253 bool HasError = false;
254 bool FocusedTiming = false;
255 IceString ErrorMessage = "";
Andrew Scull9612d322015-07-06 14:53:25 -0700256 CfgNode *Entry = nullptr; /// entry basic block
257 NodeList Nodes; /// linearized node list; Entry should be first
Jim Stichnoth9a04c072014-12-11 15:51:42 -0800258 std::vector<IceString> IdentifierNames;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700259 InstNumberT NextInstNumber;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700260 VarList Variables;
Andrew Scull9612d322015-07-06 14:53:25 -0700261 VarList Args; /// subset of Variables, in argument order
262 VarList ImplicitArgs; /// subset of Variables
Jan Voung1d62cf02015-01-09 14:57:32 -0800263 std::unique_ptr<ArenaAllocator<>> Allocator;
Jim Stichnotha18cc9c2014-09-30 19:10:22 -0700264 std::unique_ptr<Liveness> Live;
265 std::unique_ptr<TargetLowering> Target;
266 std::unique_ptr<VariablesMetadata> VMetadata;
267 std::unique_ptr<Assembler> TargetAssembler;
Andrew Scull9612d322015-07-06 14:53:25 -0700268 /// Globals required by this CFG. Mostly used for the profiler's globals.
John Portof8b4cc82015-06-09 18:06:19 -0700269 std::unique_ptr<VariableDeclarationList> GlobalInits;
Andrew Scull86df4e92015-07-30 13:54:44 -0700270 std::vector<InstJumpTable *> JumpTables;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700271
Andrew Scull9612d322015-07-06 14:53:25 -0700272 /// CurrentNode is maintained during dumping/emitting just for
273 /// validating Variable::DefNode. Normally, a traversal over
274 /// CfgNodes maintains this, but before global operations like
275 /// register allocation, resetCurrentNode() should be called to avoid
276 /// spurious validation failures.
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700277 const CfgNode *CurrentNode = nullptr;
Jim Stichnoth31c95592014-12-19 12:51:35 -0800278
Andrew Scull9612d322015-07-06 14:53:25 -0700279 /// Maintain a pointer in TLS to the current Cfg being translated.
280 /// This is primarily for accessing its allocator statelessly, but
281 /// other uses are possible.
Jim Stichnotha5fe17a2015-01-26 11:10:03 -0800282 ICE_TLS_DECLARE_FIELD(const Cfg *, CurrentCfg);
283
284public:
285 static void TlsInit() { ICE_TLS_INIT_FIELD(CurrentCfg); }
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700286};
287
288} // end of namespace Ice
289
290#endif // SUBZERO_SRC_ICECFG_H