• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- 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 /// \file
11 /// \brief Declares the Cfg class, which represents the control flow graph and
12 /// the overall per-function compilation context.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef SUBZERO_SRC_ICECFG_H
17 #define SUBZERO_SRC_ICECFG_H
18 
19 #include "IceAssembler.h"
20 #include "IceClFlags.h"
21 #include "IceDefs.h"
22 #include "IceGlobalContext.h"
23 #include "IceLoopAnalyzer.h"
24 #include "IceStringPool.h"
25 #include "IceTypes.h"
26 
27 namespace Ice {
28 
29 class Cfg {
30   Cfg() = delete;
31   Cfg(const Cfg &) = delete;
32   Cfg &operator=(const Cfg &) = delete;
33 
34   std::unique_ptr<ArenaAllocator> Allocator;
35 
36 public:
37   ~Cfg();
38 
create(GlobalContext * Ctx,uint32_t SequenceNumber)39   static std::unique_ptr<Cfg> create(GlobalContext *Ctx,
40                                      uint32_t SequenceNumber) {
41     return std::unique_ptr<Cfg>(new Cfg(Ctx, SequenceNumber));
42   }
43 
getContext()44   GlobalContext *getContext() const { return Ctx; }
getSequenceNumber()45   uint32_t getSequenceNumber() const { return SequenceNumber; }
getOptLevel()46   OptLevel getOptLevel() const { return OptimizationLevel; }
47 
defaultVerboseMask()48   static constexpr VerboseMask defaultVerboseMask() {
49     return (IceV_NO_PER_PASS_DUMP_BEYOND << 1) - 1;
50   }
51   /// Returns true if any of the specified options in the verbose mask are set.
52   /// If the argument is omitted, it checks if any verbose options at all are
53   /// set.
54   bool isVerbose(VerboseMask Mask = defaultVerboseMask()) const {
55     return VMask & Mask;
56   }
setVerbose(VerboseMask Mask)57   void setVerbose(VerboseMask Mask) { VMask = Mask; }
58 
59   /// \name Manage the name and return type of the function being translated.
60   /// @{
setFunctionName(GlobalString Name)61   void setFunctionName(GlobalString Name) { FunctionName = Name; }
getFunctionName()62   GlobalString getFunctionName() const { return FunctionName; }
63   std::string getFunctionNameAndSize() const;
setReturnType(Type Ty)64   void setReturnType(Type Ty) { ReturnType = Ty; }
getReturnType()65   Type getReturnType() const { return ReturnType; }
66   /// @}
67 
68   /// \name Manage the "internal" attribute of the function.
69   /// @{
setInternal(bool Internal)70   void setInternal(bool Internal) { IsInternalLinkage = Internal; }
getInternal()71   bool getInternal() const { return IsInternalLinkage; }
72   /// @}
73 
74   /// \name Manage errors.
75   /// @{
76 
77   /// Translation error flagging. If support for some construct is known to be
78   /// missing, instead of an assertion failure, setError() should be called and
79   /// the error should be propagated back up. This way, we can gracefully fail
80   /// to translate and let a fallback translator handle the function.
81   void setError(const std::string &Message);
hasError()82   bool hasError() const { return HasError; }
getError()83   std::string getError() const { return ErrorMessage; }
84   /// @}
85 
86   /// \name Manage nodes (a.k.a. basic blocks, CfgNodes).
87   /// @{
setEntryNode(CfgNode * EntryNode)88   void setEntryNode(CfgNode *EntryNode) { Entry = EntryNode; }
getEntryNode()89   CfgNode *getEntryNode() const { return Entry; }
90   /// Create a node and append it to the end of the linearized list. The loop
91   /// nest depth of the new node may not be valid if it is created after
92   /// computeLoopNestDepth.
93   CfgNode *makeNode();
getNumNodes()94   SizeT getNumNodes() const { return Nodes.size(); }
getNodes()95   const NodeList &getNodes() const { return Nodes; }
96   /// Swap nodes of Cfg with given list of nodes.  The number of nodes must
97   /// remain unchanged.
98   void swapNodes(NodeList &NewNodes);
99   /// @}
100 
101   /// String pool for CfgNode::Name values.
getNodeStrings()102   StringPool *getNodeStrings() const { return NodeStrings.get(); }
103   /// String pool for Variable::Name values.
getVarStrings()104   StringPool *getVarStrings() const { return VarStrings.get(); }
105 
106   /// \name Manage instruction numbering.
107   /// @{
newInstNumber()108   InstNumberT newInstNumber() { return NextInstNumber++; }
getNextInstNumber()109   InstNumberT getNextInstNumber() const { return NextInstNumber; }
110   /// @}
111 
112   /// \name Manage Variables.
113   /// @{
114 
115   /// Create a new Variable with a particular type and an optional name. The
116   /// Node argument is the node where the variable is defined.
117   // TODO(jpp): untemplate this with separate methods: makeVariable and
118   // makeStackVariable.
makeVariable(Type Ty)119   template <typename T = Variable> T *makeVariable(Type Ty) {
120     SizeT Index = Variables.size();
121     auto *Var = T::create(this, Ty, Index);
122     Variables.push_back(Var);
123     return Var;
124   }
getNumVariables()125   SizeT getNumVariables() const { return Variables.size(); }
getVariables()126   const VarList &getVariables() const { return Variables; }
127   /// @}
128 
129   /// \name Manage arguments to the function.
130   /// @{
131   void addArg(Variable *Arg);
getArgs()132   const VarList &getArgs() const { return Args; }
getArgs()133   VarList &getArgs() { return Args; }
134   void addImplicitArg(Variable *Arg);
getImplicitArgs()135   const VarList &getImplicitArgs() const { return ImplicitArgs; }
136   /// @}
137 
138   /// \name Manage the jump tables.
139   /// @{
addJumpTable(InstJumpTable * JumpTable)140   void addJumpTable(InstJumpTable *JumpTable) {
141     JumpTables.emplace_back(JumpTable);
142   }
143   /// @}
144 
145   /// \name Manage the Globals used by this function.
146   /// @{
getGlobalInits()147   std::unique_ptr<VariableDeclarationList> getGlobalInits() {
148     return std::move(GlobalInits);
149   }
addGlobal(VariableDeclaration * Global)150   void addGlobal(VariableDeclaration *Global) {
151     if (GlobalInits == nullptr) {
152       GlobalInits.reset(new VariableDeclarationList);
153     }
154     GlobalInits->push_back(Global);
155   }
getGlobalPool()156   VariableDeclarationList *getGlobalPool() {
157     if (GlobalInits == nullptr) {
158       GlobalInits.reset(new VariableDeclarationList);
159     }
160     return GlobalInits.get();
161   }
162   /// @}
163 
164   /// \name Miscellaneous accessors.
165   /// @{
getTarget()166   TargetLowering *getTarget() const { return Target.get(); }
getVMetadata()167   VariablesMetadata *getVMetadata() const { return VMetadata.get(); }
getLiveness()168   Liveness *getLiveness() const { return Live.get(); }
getAssembler()169   template <typename T = Assembler> T *getAssembler() const {
170     return llvm::dyn_cast<T>(TargetAssembler.get());
171   }
releaseAssembler()172   std::unique_ptr<Assembler> releaseAssembler() {
173     return std::move(TargetAssembler);
174   }
175   bool hasComputedFrame() const;
getFocusedTiming()176   bool getFocusedTiming() const { return FocusedTiming; }
setFocusedTiming()177   void setFocusedTiming() { FocusedTiming = true; }
178   /// @}
179 
180   /// Passes over the CFG.
181   void translate();
182   /// After the CFG is fully constructed, iterate over the nodes and compute the
183   /// predecessor and successor edges, in the form of CfgNode::InEdges[] and
184   /// CfgNode::OutEdges[].
185   void computeInOutEdges();
186   /// Renumber the non-deleted instructions in the Cfg.  This needs to be done
187   /// in preparation for live range analysis.  The instruction numbers in a
188   /// block must be monotonically increasing.  The range of instruction numbers
189   /// in a block, from lowest to highest, must not overlap with the range of any
190   /// other block.
191   ///
192   /// Also, if the configuration specifies to do so, remove/unlink all deleted
193   /// instructions from the Cfg, to speed up later passes over the instructions.
194   void renumberInstructions();
195   void placePhiLoads();
196   void placePhiStores();
197   void deletePhis();
198   void advancedPhiLowering();
199   void reorderNodes();
200   void localCSE(bool AssumeSSA);
201   void floatConstantCSE();
202   void shortCircuitJumps();
203   void loopInvariantCodeMotion();
204 
205   /// Scan allocas to determine whether we need to use a frame pointer.
206   /// If SortAndCombine == true, merge all the fixed-size allocas in the
207   /// entry block and emit stack or frame pointer-relative addressing.
208   void processAllocas(bool SortAndCombine);
209   void doAddressOpt();
210   /// Find clusters of insertelement/extractelement instructions that can be
211   /// replaced by a shufflevector instruction.
212   void materializeVectorShuffles();
213   void doArgLowering();
214   void genCode();
215   void genFrame();
216   void generateLoopInfo();
217   void livenessLightweight();
218   void liveness(LivenessMode Mode);
219   bool validateLiveness() const;
220   void contractEmptyNodes();
221   void doBranchOpt();
222   void markNodesForSandboxing();
223 
224   /// \name  Manage the CurrentNode field.
225   /// CurrentNode is used for validating the Variable::DefNode field during
226   /// dumping/emitting.
227   /// @{
setCurrentNode(const CfgNode * Node)228   void setCurrentNode(const CfgNode *Node) { CurrentNode = Node; }
resetCurrentNode()229   void resetCurrentNode() { setCurrentNode(nullptr); }
getCurrentNode()230   const CfgNode *getCurrentNode() const { return CurrentNode; }
231   /// @}
232 
233   /// Get the total amount of memory held by the per-Cfg allocator.
234   size_t getTotalMemoryMB() const;
235 
236   /// Get the current memory usage due to liveness data structures.
237   size_t getLivenessMemoryMB() const;
238 
239   void emit();
240   void emitIAS();
241   static void emitTextHeader(GlobalString Name, GlobalContext *Ctx,
242                              const Assembler *Asm);
243   void dump(const char *Message = "");
244 
245   /// Allocate data of type T using the per-Cfg allocator.
allocate()246   template <typename T> T *allocate() { return Allocator->Allocate<T>(); }
247 
248   /// Allocate an array of data of type T using the per-Cfg allocator.
allocateArrayOf(size_t NumElems)249   template <typename T> T *allocateArrayOf(size_t NumElems) {
250     return Allocator->Allocate<T>(NumElems);
251   }
252 
253   /// Deallocate data that was allocated via allocate<T>().
deallocate(T * Object)254   template <typename T> void deallocate(T *Object) {
255     Allocator->Deallocate(Object);
256   }
257 
258   /// Deallocate data that was allocated via allocateArrayOf<T>().
deallocateArrayOf(T * Array)259   template <typename T> void deallocateArrayOf(T *Array) {
260     Allocator->Deallocate(Array);
261   }
262 
263   /// Update Phi labels with InEdges.
264   ///
265   /// The WASM translator cannot always determine the right incoming edge for a
266   /// value due to the CFG being built incrementally. The fixPhiNodes pass fills
267   /// in the correct information once everything is known.
268   void fixPhiNodes();
269 
setStackSizeLimit(uint32_t Limit)270   void setStackSizeLimit(uint32_t Limit) { StackSizeLimit = Limit; }
getStackSizeLimit()271   uint32_t getStackSizeLimit() const { return StackSizeLimit; }
272 
273 private:
274   friend class CfgAllocatorTraits; // for Allocator access.
275 
276   Cfg(GlobalContext *Ctx, uint32_t SequenceNumber);
277 
278   void createNodeNameDeclaration(const std::string &NodeAsmName);
279   void
280   createBlockProfilingInfoDeclaration(const std::string &NodeAsmName,
281                                       VariableDeclaration *NodeNameDeclaration);
282 
283   /// Iterate through the registered jump tables and emit them.
284   void emitJumpTables();
285 
286   enum AllocaBaseVariableType {
287     BVT_StackPointer,
288     BVT_FramePointer,
289     BVT_UserPointer
290   };
291   void sortAndCombineAllocas(CfgVector<InstAlloca *> &Allocas,
292                              uint32_t CombinedAlignment, InstList &Insts,
293                              AllocaBaseVariableType BaseVariableType);
294   void findRematerializable();
295   CfgVector<Inst *>
296   findLoopInvariantInstructions(const CfgUnorderedSet<SizeT> &Body);
297 
298   static ArenaAllocator *createAllocator();
299 
300   GlobalContext *Ctx;
301   uint32_t SequenceNumber; /// output order for emission
302   OptLevel OptimizationLevel = Opt_m1;
303   VerboseMask VMask;
304   GlobalString FunctionName;
305   Type ReturnType = IceType_void;
306   bool IsInternalLinkage = false;
307   bool HasError = false;
308   bool FocusedTiming = false;
309   std::string ErrorMessage = "";
310   CfgNode *Entry = nullptr; /// entry basic block
311   NodeList Nodes;           /// linearized node list; Entry should be first
312   InstNumberT NextInstNumber;
313   VarList Variables;
314   VarList Args;         /// subset of Variables, in argument order
315   VarList ImplicitArgs; /// subset of Variables
316   // Separate string pools for CfgNode and Variable names, due to a combination
317   // of the uniqueness requirement, and assumptions in lit tests.
318   std::unique_ptr<StringPool> NodeStrings;
319   std::unique_ptr<StringPool> VarStrings;
320   std::unique_ptr<Liveness> Live;
321   std::unique_ptr<TargetLowering> Target;
322   std::unique_ptr<VariablesMetadata> VMetadata;
323   std::unique_ptr<Assembler> TargetAssembler;
324   /// Globals required by this CFG.
325   std::unique_ptr<VariableDeclarationList> GlobalInits;
326   CfgVector<InstJumpTable *> JumpTables;
327   /// CurrentNode is maintained during dumping/emitting just for validating
328   /// Variable::DefNode. Normally, a traversal over CfgNodes maintains this, but
329   /// before global operations like register allocation, resetCurrentNode()
330   /// should be called to avoid spurious validation failures.
331   const CfgNode *CurrentNode = nullptr;
332   CfgVector<Loop> LoopInfo;
333   uint32_t StackSizeLimit = 1 * 1024 * 1024; // 1 MiB
334 
335 public:
TlsInit()336   static void TlsInit() { CfgAllocatorTraits::init(); }
337 };
338 
339 template <> Variable *Cfg::makeVariable<Variable>(Type Ty);
340 
341 struct NodeStringPoolTraits {
342   using OwnerType = Cfg;
getStringsNodeStringPoolTraits343   static StringPool *getStrings(const OwnerType *PoolOwner) {
344     return PoolOwner->getNodeStrings();
345   }
346 };
347 using NodeString = StringID<NodeStringPoolTraits>;
348 
349 struct VariableStringPoolTraits {
350   using OwnerType = Cfg;
getStringsVariableStringPoolTraits351   static StringPool *getStrings(const OwnerType *PoolOwner) {
352     return PoolOwner->getVarStrings();
353   }
354 };
355 using VariableString = StringID<VariableStringPoolTraits>;
356 
357 } // end of namespace Ice
358 
359 #endif // SUBZERO_SRC_ICECFG_H
360