blob: 4f3d7d099be60e65eaf4d06309e1d0012a515549 [file] [log] [blame]
Jim Stichnothf7c9a142014-04-29 10:52:43 -07001//===- subzero/src/IceCfgNode.h - Control flow graph node -------*- 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 CfgNode class, which represents a single
12/// basic block as its instruction list, in-edge list, and out-edge
13/// list.
14///
Jim Stichnothf7c9a142014-04-29 10:52:43 -070015//===----------------------------------------------------------------------===//
16
17#ifndef SUBZERO_SRC_ICECFGNODE_H
18#define SUBZERO_SRC_ICECFGNODE_H
19
20#include "IceDefs.h"
Jim Stichnoth607e9f02014-11-06 13:32:05 -080021#include "IceInst.h" // InstList traits
Jim Stichnothf7c9a142014-04-29 10:52:43 -070022
23namespace Ice {
24
25class CfgNode {
Jim Stichnothc6ead202015-02-24 09:30:30 -080026 CfgNode() = delete;
Jim Stichnoth7b451a92014-10-15 14:39:23 -070027 CfgNode(const CfgNode &) = delete;
28 CfgNode &operator=(const CfgNode &) = delete;
29
Jim Stichnothf7c9a142014-04-29 10:52:43 -070030public:
Jim Stichnoth668a7a32014-12-10 15:32:25 -080031 static CfgNode *create(Cfg *Func, SizeT LabelIndex) {
32 return new (Func->allocate<CfgNode>()) CfgNode(Func, LabelIndex);
Jim Stichnothf7c9a142014-04-29 10:52:43 -070033 }
34
Andrew Scull9612d322015-07-06 14:53:25 -070035 /// Access the label number and name for this node.
Jim Stichnothf7c9a142014-04-29 10:52:43 -070036 SizeT getIndex() const { return Number; }
Jim Stichnoth69d3f9c2015-03-23 10:33:38 -070037 void resetIndex(SizeT NewNumber) { Number = NewNumber; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070038 IceString getName() const;
Jim Stichnoth668a7a32014-12-10 15:32:25 -080039 void setName(const IceString &NewName) {
Karl Schimpfc132b762014-09-11 09:43:47 -070040 // Make sure that the name can only be set once.
Jim Stichnoth9a04c072014-12-11 15:51:42 -080041 assert(NameIndex == Cfg::IdentifierIndexInvalid);
Jim Stichnoth668a7a32014-12-10 15:32:25 -080042 if (!NewName.empty())
Jim Stichnoth9a04c072014-12-11 15:51:42 -080043 NameIndex = Func->addIdentifierName(NewName);
Karl Schimpfc132b762014-09-11 09:43:47 -070044 }
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070045 IceString getAsmName() const {
46 return ".L" + Func->getFunctionName() + "$" + getName();
47 }
48
Andrew Scull9612d322015-07-06 14:53:25 -070049 /// The HasReturn flag indicates that this node contains a return
50 /// instruction and therefore needs an epilog.
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070051 void setHasReturn() { HasReturn = true; }
52 bool getHasReturn() const { return HasReturn; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070053
Jim Stichnoth336f6c42014-10-30 15:01:31 -070054 void setNeedsPlacement(bool Value) { NeedsPlacement = Value; }
55 bool needsPlacement() const { return NeedsPlacement; }
56
Andrew Scull9612d322015-07-06 14:53:25 -070057 /// \name Access predecessor and successor edge lists.
58 /// @{
Jim Stichnothf7c9a142014-04-29 10:52:43 -070059 const NodeList &getInEdges() const { return InEdges; }
60 const NodeList &getOutEdges() const { return OutEdges; }
Andrew Scull9612d322015-07-06 14:53:25 -070061 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070062
Andrew Scull9612d322015-07-06 14:53:25 -070063 /// \name Manage the instruction list.
64 /// @{
Jim Stichnothf7c9a142014-04-29 10:52:43 -070065 InstList &getInsts() { return Insts; }
Jim Stichnoth98712a32014-10-24 10:59:02 -070066 PhiList &getPhis() { return Phis; }
Jim Stichnothf7c9a142014-04-29 10:52:43 -070067 void appendInst(Inst *Inst);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070068 void renumberInstructions();
Andrew Scull9612d322015-07-06 14:53:25 -070069 /// Rough and generally conservative estimate of the number of
70 /// instructions in the block. It is updated when an instruction is
71 /// added, but not when deleted. It is recomputed during
72 /// renumberInstructions().
Jim Stichnoth47752552014-10-13 17:15:08 -070073 InstNumberT getInstCountEstimate() const { return InstCountEstimate; }
Andrew Scull9612d322015-07-06 14:53:25 -070074 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070075
Andrew Scull9612d322015-07-06 14:53:25 -070076 /// \name Manage predecessors and successors.
77 /// @{
78
79 /// Add a predecessor edge to the InEdges list for each of this
80 /// node's successors.
Jim Stichnothf7c9a142014-04-29 10:52:43 -070081 void computePredecessors();
Jim Stichnoth69d3f9c2015-03-23 10:33:38 -070082 void computeSuccessors();
Jim Stichnoth336f6c42014-10-30 15:01:31 -070083 CfgNode *splitIncomingEdge(CfgNode *Pred, SizeT InEdgeIndex);
Andrew Scull9612d322015-07-06 14:53:25 -070084 /// @}
Jim Stichnothf7c9a142014-04-29 10:52:43 -070085
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070086 void placePhiLoads();
87 void placePhiStores();
88 void deletePhis();
Jim Stichnoth336f6c42014-10-30 15:01:31 -070089 void advancedPhiLowering();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070090 void doAddressOpt();
Matt Walac3302742014-08-15 16:21:56 -070091 void doNopInsertion();
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070092 void genCode();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070093 void livenessLightweight();
94 bool liveness(Liveness *Liveness);
Jim Stichnothe5b73e62014-12-15 09:58:51 -080095 void livenessAddIntervals(Liveness *Liveness, InstNumberT FirstInstNum,
96 InstNumberT LastInstNum);
Jim Stichnoth336f6c42014-10-30 15:01:31 -070097 void contractIfEmpty();
Jim Stichnothff9c7062014-09-18 04:50:49 -070098 void doBranchOpt(const CfgNode *NextNode);
Jim Stichnoth5bc2b1d2014-05-22 13:38:48 -070099 void emit(Cfg *Func) const;
Jan Voung0faec4c2014-11-05 17:29:56 -0800100 void emitIAS(Cfg *Func) const;
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700101 void dump(Cfg *Func) const;
102
John Portof8b4cc82015-06-09 18:06:19 -0700103 void profileExecutionCount(VariableDeclaration *Var);
104
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700105private:
Jim Stichnoth668a7a32014-12-10 15:32:25 -0800106 CfgNode(Cfg *Func, SizeT LabelIndex);
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700107 Cfg *const Func;
Andrew Scull9612d322015-07-06 14:53:25 -0700108 SizeT Number; /// label index
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700109 Cfg::IdentifierIndexType NameIndex =
Andrew Scull9612d322015-07-06 14:53:25 -0700110 Cfg::IdentifierIndexInvalid; /// index into Cfg::NodeNames table
111 bool HasReturn = false; /// does this block need an epilog?
Jim Stichnotheafb56c2015-06-22 10:35:22 -0700112 bool NeedsPlacement = false;
Andrew Scull9612d322015-07-06 14:53:25 -0700113 InstNumberT InstCountEstimate = 0; /// rough instruction count estimate
114 NodeList InEdges; /// in no particular order
115 NodeList OutEdges; /// in no particular order
116 PhiList Phis; /// unordered set of phi instructions
117 InstList Insts; /// ordered list of non-phi instructions
Jim Stichnothf7c9a142014-04-29 10:52:43 -0700118};
119
120} // end of namespace Ice
121
122#endif // SUBZERO_SRC_ICECFGNODE_H