OpenASIP  2.0
PRegionMarkerAnalyzer.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012-2020 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 PRegionMarkerAnalyzer.cc
26  *
27  * @author Pekka Jääskeläinen 2012,2020
28  */
29 
30 #include <climits>
31 
32 #include "tce_config.h"
33 #include "TCEString.hh"
34 #include "Conversion.hh"
35 #include "PRegionMarkerAnalyzer.hh"
36 
37 #include "CompilerWarnings.hh"
38 IGNORE_COMPILER_WARNING("-Wunused-parameter")
39 IGNORE_COMPILER_WARNING("-Wcomment")
40 
41 #include <llvm/CodeGen/MachineFunction.h>
42 #include <llvm/IR/Function.h>
43 
44 //#define DEBUG_PREGIONS
45 
46 using namespace llvm;
47 
49  const llvm::MachineFunction& MF_) : MF(MF_), markersFound_(false) {
50 
51  for (MachineFunction::const_iterator i = MF.begin();
52  i != MF.end() && !markersFound_; i++) {
53  for (MachineBasicBlock::const_iterator j = i->begin();
54  j != i->end(); j++) {
55  const llvm::MachineInstr& mi = *j;
57  markersFound_ = true;
58  break;
59  }
60  }
61  }
62 
63  if (!markersFound_) return;
64  findPregions();
65 }
66 
67 void
69  const llvm::MachineInstr& start, unsigned id) {
70  const llvm::MachineBasicBlock* mbb = start.getParent();
71 
72  for (MachineBasicBlock::const_iterator i = &start;
73  i != mbb->end();
74  ++i) {
75  const llvm::MachineInstr* mi = &*i;
76  if (mi != &start &&
78  return;
79 
80  if (mi->isCall() && !mi->isInlineAsm()) return;
81 
82 #ifdef DEBUG_PREGIONS
83  std::cerr << "### PRA: propagating region id " << id << " to ";
84  mi->dump();
85 #endif
86  pregionIDs_[mi] = id;
87  }
88  for (MachineBasicBlock::const_succ_iterator i = mbb->succ_begin();
89  i != mbb->succ_end(); ++i) {
90  const llvm::MachineBasicBlock* child = *i;
91  const llvm::MachineInstr* first = &child->front();
92  // avoid looping forever by checking whether the info
93  // has been propagated already to the recusrively
94  // traversed basic blocks
95  if (pregionIDs_.find(first) == pregionIDs_.end() &&
96  !isPregionStartMarker(*first) && !isPregionEndMarker(*first))
97  propagatePregionID(child->front(), id);
98  }
99 }
100 
101 /**
102  * Finds the parallel region markers and propagates the
103  * parallel region ids to the instructions dominated by
104  * it.
105  */
106 void
108 
109  for (MachineFunction::const_iterator i = MF.begin();
110  i != MF.end(); i++) {
111  for (MachineBasicBlock::const_iterator j = i->begin();
112  j != i->end(); j++) {
113  const llvm::MachineInstr& mi = *j;
114  if (!isPregionStartMarker(mi))
115  continue;
116  unsigned pregionId = parsePregionID(mi);
117  propagatePregionID(mi, pregionId);
118  }
119  }
120 }
121 
122 bool
124  return markersFound_;
125 }
126 
127 bool
128 PRegionMarkerAnalyzer::isPregionEndMarker(const llvm::MachineInstr &I) const {
129  // Count the number of register definitions.
130  if (!I.isInlineAsm()) return false;
131  unsigned numDefs = 0;
132  while (I.getOperand(numDefs).isReg() &&
133  I.getOperand(numDefs).isDef())
134  ++numDefs;
135  TCEString opName = I.getOperand(numDefs).getSymbolName();
136  return opName.startsWith(".pregion_end");
137 }
138 
139 bool
140 PRegionMarkerAnalyzer::isPregionStartMarker(const llvm::MachineInstr &I) const {
141  if (!I.isInlineAsm()) return false;
142  // Count the number of register definitions.
143  unsigned numDefs = 0;
144  while (I.getOperand(numDefs).isReg() &&
145  I.getOperand(numDefs).isDef())
146  ++numDefs;
147  TCEString opName = I.getOperand(numDefs).getSymbolName();
148  return opName.startsWith(".pregion_start.");
149 }
150 
151 unsigned
152 PRegionMarkerAnalyzer::parsePregionID(const llvm::MachineInstr &I) const {
153  if (!isPregionStartMarker(I)) return UINT_MAX;
154 
155  unsigned numDefs = 0;
156  while (I.getOperand(numDefs).isReg() &&
157  I.getOperand(numDefs).isDef())
158  ++numDefs;
159 
160  TCEString opName = I.getOperand(numDefs).getSymbolName();
161 
162  return Conversion::toUnsignedInt(opName.split(".").at(2));
163 }
164 
165 unsigned
166 PRegionMarkerAnalyzer::pregion(const llvm::MachineInstr &I) const {
167  if (pregionIDs_.find(&I) != pregionIDs_.end())
168  return (*pregionIDs_.find(&I)).second;
169  else
170  return UINT_MAX;
171 }
172 
llvm
Definition: InlineAsmParser.hh:49
TCEString::split
std::vector< TCEString > split(const std::string &delim) const
Definition: TCEString.cc:114
TCEString::startsWith
bool startsWith(const std::string &str) const
PRegionMarkerAnalyzer::parsePregionID
unsigned parsePregionID(const llvm::MachineInstr &I) const
Definition: PRegionMarkerAnalyzer.cc:152
PRegionMarkerAnalyzer::findPregions
void findPregions()
Definition: PRegionMarkerAnalyzer.cc:107
PRegionMarkerAnalyzer::propagatePregionID
void propagatePregionID(const llvm::MachineInstr &start, unsigned id)
Definition: PRegionMarkerAnalyzer.cc:68
PRegionMarkerAnalyzer.hh
TCEString.hh
Conversion.hh
PRegionMarkerAnalyzer::isPregionEndMarker
bool isPregionEndMarker(const llvm::MachineInstr &I) const
Definition: PRegionMarkerAnalyzer.cc:128
PRegionMarkerAnalyzer::MF
const llvm::MachineFunction & MF
Definition: PRegionMarkerAnalyzer.hh:60
PRegionMarkerAnalyzer::isPregionStartMarker
bool isPregionStartMarker(const llvm::MachineInstr &I) const
Definition: PRegionMarkerAnalyzer.cc:140
PRegionMarkerAnalyzer::PRegionMarkerAnalyzer
PRegionMarkerAnalyzer(const llvm::MachineFunction &MF_)
Definition: PRegionMarkerAnalyzer.cc:48
PRegionMarkerAnalyzer::markersFound
bool markersFound() const
Definition: PRegionMarkerAnalyzer.cc:123
PRegionMarkerAnalyzer::pregionIDs_
std::map< const llvm::MachineInstr *, unsigned > pregionIDs_
Definition: PRegionMarkerAnalyzer.hh:62
PRegionMarkerAnalyzer::markersFound_
bool markersFound_
Definition: PRegionMarkerAnalyzer.hh:61
Conversion::toUnsignedInt
static unsigned int toUnsignedInt(const T &source)
IGNORE_COMPILER_WARNING
#define IGNORE_COMPILER_WARNING(X)
Definition: CompilerWarnings.hh:51
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
PRegionMarkerAnalyzer::pregion
unsigned pregion(const llvm::MachineInstr &I) const
Definition: PRegionMarkerAnalyzer.cc:166
TCEString
Definition: TCEString.hh:53
CompilerWarnings.hh