OpenASIP  2.0
MachineInstrDDG.hh
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2012 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file MachineInstrDDG.hh
26  *
27  * Declaration of MachineInstrDDG class.
28  *
29  * @author Pekka Jääskeläinen 2010-2012
30  * @note rating: red
31  */
32 
33 #ifndef TCE_MACHINE_INSTR_DDG_HH
34 #define TCE_MACHINE_INSTR_DDG_HH
35 
36 #include "CompilerWarnings.hh"
37 
38 IGNORE_COMPILER_WARNING("-Wunused-parameter")
39 #ifdef __clang__
40 IGNORE_COMPILER_WARNING("-Wunused-private-field")
41 #endif
42 
43 #include "BoostGraph.hh"
44 #include "GraphNode.hh"
45 #include "GraphEdge.hh"
46 #include <set>
47 #include <map>
48 #include <sstream>
49 #include "llvm/CodeGen/TargetRegisterInfo.h"
50 #include "llvm/CodeGen/TargetInstrInfo.h"
51 #include "llvm/Target/TargetMachine.h"
52 
53 namespace llvm {
54  class MachineFunction;
55  class MachineInstr;
56 }
57 
58 struct MIDDGNode : public GraphNode {
59  MIDDGNode() : GraphNode(), mi_(NULL), address_(-1), optimalCycle_(-1) {}
60  MIDDGNode(const llvm::MachineInstr& mi, int sequentialAddress) :
62 
63  virtual ~MIDDGNode() {}
64 
65  bool operator<(const MIDDGNode& other) const {
66  return other.sequentialAddress() < this->sequentialAddress();
67  }
68  bool operator==(const MIDDGNode& other) const {
69  return other.mi_ == this->mi_;
70  }
71 
72  const llvm::MachineInstr* machineInstr() const { return mi_; }
73  int sequentialAddress() const { return address_; }
74 
75  std::string dotString() const;
77 
78  void setOptimalCycle(int cycle) { optimalCycle_ = cycle; }
79  int optimalCycle() const { return optimalCycle_; }
80 private:
81  const llvm::MachineInstr* mi_;
82  int address_;
84 };
85 
86 struct MIDDGEdge : public GraphEdge {
89  DEP_RAW = 1,
90  DEP_WAR = 2,
91  DEP_WAW = 3};
92 
93  enum EdgeReason {
96 
97  MIDDGEdge(unsigned reg) :
99 
100  MIDDGEdge(unsigned reg, DependenceType type) :
101  GraphEdge(), reg_(reg), dependenceType_(type) {}
102 
103 
104  virtual ~MIDDGEdge() {}
105 
106  TCEString dotString() const {
107  return (boost::format("label=\"%d %s\"") % reg_ % typeAsString()).
108  str();
109  }
110 
111  TCEString toString() const {
112  return (boost::format("%d %s") % reg_ % typeAsString()).str();
113  }
114 
115 private:
116 
117  std::string typeAsString() const {
118  std::string type = "unknown";
119  if (dependenceType_ == DEP_RAW)
120  type = "RaW";
121  else if (dependenceType_ == DEP_WAR)
122  type = "WaR";
123  else if (dependenceType_ == DEP_WAW)
124  type = "WaW";
125  return type;
126  }
127 
128  unsigned reg_;
129  unsigned char dependenceType_; // DependenceType
130 };
131 
132 /**
133  * Data Dependence Graph constructed from non-register allocated LLVM
134  * MachineInstructions.
135  *
136  * Only true dependencies supported at the moment. Later we can might add
137  * support for adding the false dependencies introduced by the register
138  * allocator, if needed.
139  */
141  public BoostGraph<MIDDGNode, MIDDGEdge> {
142 public:
143  typedef unsigned Register;
144  typedef std::set<Register> RegisterSet;
145 
146  explicit MachineInstrDDG(const MachineInstrDDG& parent);
147 
149  llvm::MachineFunction& mf,
150  bool onlyTrueDeps=true);
151 
152  virtual ~MachineInstrDDG();
153 
155 
156  MIDDGNode* vregDefiner(Register vreg) const { return definers_[vreg]; };
157  MIDDGNode* lastVregUser(Register vreg) const;
158 
159  int falseDepHeightDelta(Register vreg, Register physReg) const;
160  void assignPhysReg(Register vreg, Register physReg);
161 
163  const MIDDGNode& node,
164  Register physReg) const;
165 
166  void computeOptimalSchedule();
167 
168  TCEString dotString() const;
169 
171 
172 private:
173  typedef std::map<Register, MIDDGNode*> DefinerMap;
174  typedef std::map<Register, NodeSet> UserMap;
175  typedef std::map<Register, Register> RegisterMap;
176 
177  std::pair<MIDDGNode*, MIDDGNode*>
178  createFalseDepEdge(Register vreg, Register physReg) const;
179 
180  // all register indices in the DDG
182  // the MachineInstructions* that define the virtual regs
184  mutable UserMap users_;
185 
187  std::set<MIDDGEdge*> edges_;
188  std::map<Register, MIDDGNode*> lastPhysRegUsers_;
189  std::map<Register, MIDDGNode*> lastPhysRegDefiners_;
191 
192  // do not add any false deps in case this is true
193  const bool onlyTrueDeps_;
194 
195  // in case a schedule has been computed, these contain the limits
198  mutable std::map<int, std::list<MIDDGNode*> > schedule_;
199 
200  llvm::MachineFunction& mf_;
201  const llvm::TargetRegisterInfo* regInfo_;
202 
204 };
205 
207 
208 
209 #endif
210 
MIDDGNode::osalOperationName
TCEString osalOperationName() const
Definition: MachineInstrDDG.cc:566
MIDDGEdge
Definition: MachineInstrDDG.hh:86
llvm
Definition: InlineAsmParser.hh:49
MIDDGEdge::reg_
unsigned reg_
Definition: MachineInstrDDG.hh:128
MachineInstrDDG::largestCycle_
int largestCycle_
Definition: MachineInstrDDG.hh:197
MIDDGNode::mi_
const llvm::MachineInstr * mi_
Definition: MachineInstrDDG.hh:81
MachineInstrDDG::lastPhysRegUsers_
std::map< Register, MIDDGNode * > lastPhysRegUsers_
Definition: MachineInstrDDG.hh:188
MIDDGEdge::DependenceType
DependenceType
Definition: MachineInstrDDG.hh:87
MIDDGEdge::DEP_RAW
@ DEP_RAW
Definition: MachineInstrDDG.hh:89
MIDDGNode::optimalCycle_
int optimalCycle_
Definition: MachineInstrDDG.hh:83
BoostGraph< MIDDGNode, MIDDGEdge >::node
Node & node(const int index) const
MIDDGEdge::MIDDGEdge
MIDDGEdge(unsigned reg, DependenceType type)
Definition: MachineInstrDDG.hh:100
MachineInstrDDG::UserMap
std::map< Register, NodeSet > UserMap
Definition: MachineInstrDDG.hh:174
MachineInstrDDG::allRegisters
RegisterSet allRegisters() const
Definition: MachineInstrDDG.hh:154
GraphEdge.hh
BoostGraph< MIDDGNode, MIDDGEdge >::NodeSet
std::set< MIDDGNode *, typename MIDDGNode ::Comparator > NodeSet
Definition: BoostGraph.hh:86
BoostGraph.hh
MachineInstrDDG::DefinerMap
std::map< Register, MIDDGNode * > DefinerMap
Definition: MachineInstrDDG.hh:173
MIDDGNode::operator<
bool operator<(const MIDDGNode &other) const
Definition: MachineInstrDDG.hh:65
GraphNode.hh
MachineInstrDDG::lastPhysRegDefiners_
std::map< Register, MIDDGNode * > lastPhysRegDefiners_
Definition: MachineInstrDDG.hh:189
MIDDGNode::MIDDGNode
MIDDGNode()
Definition: MachineInstrDDG.hh:59
MIDDGNode::address_
int address_
Definition: MachineInstrDDG.hh:82
MIDDGNode::MIDDGNode
MIDDGNode(const llvm::MachineInstr &mi, int sequentialAddress)
Definition: MachineInstrDDG.hh:60
MIDDGEdge::typeAsString
std::string typeAsString() const
Definition: MachineInstrDDG.hh:117
MIDDGNode::optimalCycle
int optimalCycle() const
Definition: MachineInstrDDG.hh:79
MachineInstrDDG::assignPhysReg
void assignPhysReg(Register vreg, Register physReg)
Definition: MachineInstrDDG.cc:506
MachineInstrDDG::regInfo_
const llvm::TargetRegisterInfo * regInfo_
Definition: MachineInstrDDG.hh:201
MIDDGEdge::dependenceType_
unsigned char dependenceType_
Definition: MachineInstrDDG.hh:129
MIDDGNode::~MIDDGNode
virtual ~MIDDGNode()
Definition: MachineInstrDDG.hh:63
MachineInstrDDG::allRegisters_
RegisterSet allRegisters_
Definition: MachineInstrDDG.hh:181
MIDDGEdge::toString
TCEString toString() const
Definition: MachineInstrDDG.hh:111
MachineInstrDDG::schedule_
std::map< int, std::list< MIDDGNode * > > schedule_
Definition: MachineInstrDDG.hh:198
MIDDGNode::setOptimalCycle
void setOptimalCycle(int cycle)
Definition: MachineInstrDDG.hh:78
MachineInstrDDG::RegisterSet
std::set< Register > RegisterSet
Definition: MachineInstrDDG.hh:144
MachineInstrDDG
Definition: MachineInstrDDG.hh:140
MachineInstrDDG::createFalseDepEdge
std::pair< MIDDGNode *, MIDDGNode * > createFalseDepEdge(Register vreg, Register physReg) const
Definition: MachineInstrDDG.cc:202
MIDDGEdge::DEP_WAR
@ DEP_WAR
Definition: MachineInstrDDG.hh:90
MachineInstrDDG::lastVregUser
MIDDGNode * lastVregUser(Register vreg) const
Definition: MachineInstrDDG.cc:297
MachineInstrDDG::RegisterMap
std::map< Register, Register > RegisterMap
Definition: MachineInstrDDG.hh:175
MIDDGEdge::~MIDDGEdge
virtual ~MIDDGEdge()
Definition: MachineInstrDDG.hh:104
MachineInstrDDG::preceedingNodeUsesOrDefinesReg
bool preceedingNodeUsesOrDefinesReg(const MIDDGNode &node, Register physReg) const
Definition: MachineInstrDDG.cc:322
MIDDGNode
Definition: MachineInstrDDG.hh:58
MachineInstrDDG::definers_
DefinerMap definers_
Definition: MachineInstrDDG.hh:183
MachineInstrDDG::onlyTrueDeps_
const bool onlyTrueDeps_
Definition: MachineInstrDDG.hh:193
MIDDGEdge::DEP_UNKNOWN
@ DEP_UNKNOWN
Definition: MachineInstrDDG.hh:88
MIDDGEdge::dotString
TCEString dotString() const
Definition: MachineInstrDDG.hh:106
MIDDGNode::dotString
std::string dotString() const
Definition: MachineInstrDDG.cc:606
MIDDGEdge::EdgeReason
EdgeReason
Definition: MachineInstrDDG.hh:93
MIDDGEdge::EDGE_MEMORY
@ EDGE_MEMORY
Definition: MachineInstrDDG.hh:95
MIDDGNode::operator==
bool operator==(const MIDDGNode &other) const
Definition: MachineInstrDDG.hh:68
MachineInstrDDG::smallestCycle_
int smallestCycle_
Definition: MachineInstrDDG.hh:196
MachineInstrDDG::~MachineInstrDDG
virtual ~MachineInstrDDG()
Definition: MachineInstrDDG.cc:188
MachineInstrDDG::dotString
TCEString dotString() const
Definition: MachineInstrDDG.cc:370
MIDDGEdge::EDGE_REGISTER
@ EDGE_REGISTER
Definition: MachineInstrDDG.hh:94
IGNORE_COMPILER_WARNING
#define IGNORE_COMPILER_WARNING(X)
Definition: CompilerWarnings.hh:51
MachineInstrDDG::computeOptimalSchedule
void computeOptimalSchedule()
Definition: MachineInstrDDG.cc:356
MachineInstrDDG::users_
UserMap users_
Definition: MachineInstrDDG.hh:184
MachineInstrDDG::nodes_
NodeSet nodes_
Definition: MachineInstrDDG.hh:186
MachineInstrDDG::setPrintOnlyCriticalPath
void setPrintOnlyCriticalPath(bool flag)
Definition: MachineInstrDDG.hh:170
GraphEdge
Definition: GraphEdge.hh:52
MachineInstrDDG::Register
unsigned Register
Definition: MachineInstrDDG.hh:143
TCEString
Definition: TCEString.hh:53
POP_COMPILER_DIAGS
#define POP_COMPILER_DIAGS
Definition: CompilerWarnings.hh:68
MachineInstrDDG::edges_
std::set< MIDDGEdge * > edges_
Definition: MachineInstrDDG.hh:187
MIDDGEdge::MIDDGEdge
MIDDGEdge(unsigned reg)
Definition: MachineInstrDDG.hh:97
BoostGraph
Definition: BoostGraph.hh:83
GraphNode
Definition: GraphNode.hh:42
MIDDGEdge::DEP_WAW
@ DEP_WAW
Definition: MachineInstrDDG.hh:91
MachineInstrDDG::falseDepHeightDelta
int falseDepHeightDelta(Register vreg, Register physReg) const
Definition: MachineInstrDDG.cc:281
MachineInstrDDG::printOnlyCriticalPath_
bool printOnlyCriticalPath_
Definition: MachineInstrDDG.hh:203
MachineInstrDDG::vregDefiner
MIDDGNode * vregDefiner(Register vreg) const
Definition: MachineInstrDDG.hh:156
MIDDGNode::sequentialAddress
int sequentialAddress() const
Definition: MachineInstrDDG.hh:73
MIDDGNode::machineInstr
const llvm::MachineInstr * machineInstr() const
Definition: MachineInstrDDG.hh:72
CompilerWarnings.hh
MachineInstrDDG::regAssignments_
RegisterMap regAssignments_
Definition: MachineInstrDDG.hh:190
MachineInstrDDG::MachineInstrDDG
MachineInstrDDG(const MachineInstrDDG &parent)
Definition: MachineInstrDDG.cc:181
MachineInstrDDG::mf_
llvm::MachineFunction & mf_
Definition: MachineInstrDDG.hh:200