OpenASIP  2.0
OperationPoolPimpl.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2015 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 OperationPoolPimpl.cc
26  *
27  * Definition of OperationPoolPimpl (private implementation) class.
28  *
29  * @author Viljami Korhonen 2008 (viljami.korhonen-no.spam-tut.fi)
30  * @author Pekka Jääskeläinen 2011,2015
31  * @note rating: red
32  */
33 
34 #include <string>
35 #include <vector>
36 
37 #include "OperationPool.hh"
38 #include "OperationModule.hh"
39 #include "Operation.hh"
40 #include "Operand.hh"
42 #include "OperationDAGBehavior.hh"
44 #include "Environment.hh"
45 #include "FileSystem.hh"
46 #include "Application.hh"
47 #include "AssocTools.hh"
48 #include "SequenceTools.hh"
49 #include "StringTools.hh"
50 #include "Application.hh"
51 #include "OperationIndex.hh"
52 #include "OperationPoolPimpl.hh"
53 #include "OperationSerializer.hh"
54 #include "TCEString.hh"
55 #include "ObjectState.hh"
56 
57 // disable warnings from LLVm headers
58 #pragma GCC diagnostic ignored "-Wunused-parameter"
59 
60 #include <llvm/MC/MCInstrDesc.h>
61 #include <llvm/MC/MCInstrInfo.h>
62 
63 #pragma GCC diagnostic warning "-Wunused-parameter"
64 
65 using std::vector;
66 using std::string;
67 
70 const llvm::MCInstrInfo* OperationPoolPimpl::llvmTargetInstrInfo_(NULL);
71 
72 /**
73  * The constructor
74  */
76  // if this is a first created instance of OperationPool,
77  // initialize the OperationIndex instance with the search paths
78  if (index_ == NULL) {
79  index_ = new OperationIndex();
80  vector<string> paths = Environment::osalPaths();
81  for (unsigned int i = 0; i < paths.size(); i++) {
82  index_->addPath(paths[i]);
83  }
84  }
85 }
86 
87 /**
88  * The destructor
89  */
91 }
92 
93 /**
94  * Cleans up the static Operation cache.
95  *
96  * Deletes also the Operation instances, so be sure you are not using
97  * them after calling this!
98  */
99 void
102  delete index_;
103  index_ = NULL;
104 }
105 
106 /**
107  * Looks up an operation identified by its name and returns a reference to it.
108  *
109  * The first found operation is returned. If operation is not found, a null
110  * operation is returned.
111  *
112  * @param name The name of the operation.
113  * @return The wanted operation.
114  */
115 Operation&
116 OperationPoolPimpl::operation(const char* name) {
117 
118  OperationTable::iterator it =
120  if (it != operationCache_.end()) {
121  return *((*it).second);
122  }
123 
124  // If llvmTargetInstrInfo_ is set, the scheduler is called
125  // directly from LLVM code gen. Use the TargetInstrDesc as
126  // the source for operation info instead.
127  if (llvmTargetInstrInfo_ != NULL) {
128  for (unsigned opc = 0; opc < llvmTargetInstrInfo_->getNumOpcodes();
129  ++opc) {
130  const llvm::MCInstrDesc& tid = llvmTargetInstrInfo_->get(opc);
131  TCEString operName =
132  TCEString(llvmTargetInstrInfo_->getName(opc).str()).lower();
133  if (operName == TCEString(name).lower()) {
134  Operation* llvmOperation = loadFromLLVM(tid);
135  operationCache_[operName] = llvmOperation;
136  return *llvmOperation;
137  }
138  }
140  TCEString("Did not find info for LLVM operation ") + name);
141  }
142 
143  OperationModule& module = index_->moduleOf(name);
144  if (&module == &NullOperationModule::instance()) {
145  return NullOperation::instance();
146  }
147 
148  Operation* effective = index_->effectiveOperation(name);
149  if (effective != NULL) {
150  operationCache_[StringTools::stringToLower(name)] = effective;
151  return *effective;
152  } else {
153  return NullOperation::instance();
154  }
155 }
156 
157 /**
158  * Loads an OSAL Operation from LLVM TargetInstrDesc.
159  *
160  * Used for avoiding the need for .xml OSAL databases when TCE scheduler
161  * is called directly for non-TTA LLVM targets.
162  */
163 Operation*
165 const llvm::MCInstrDesc& tid
166 ) {
167  TCEString opName = llvmTargetInstrInfo_->getName(tid.getOpcode()).str();
169 
170  unsigned outputs = tid.getNumDefs();
171  unsigned inputs = tid.getNumOperands() - outputs;
172 
173  // at least a minimal implicit trigger input
174  // RET of SPU does not include the implicit link register
175  // operand even though it will be generated in the final
176  // assembly to just an absolute jump to it
177  if (inputs == 0)
178  inputs = 1;
179 
180  for (unsigned opr = 0; opr < outputs; ++opr) {
181  Operand* operand =
182  new Operand(false, inputs + opr + 1, Operand::UINT_WORD);
183  op->addOutput(operand);
184  }
185 
186  for (unsigned opr = 0; opr < inputs; ++opr) {
187  Operand* operand = new Operand(true, opr + 1, Operand::UINT_WORD);
188  op->addInput(operand);
189  }
190  if (tid.isCall()) {
191  op->setCall(true);
192  op->setControlFlowOperation(true);
193  }
194  if (tid.isBranch() || tid.isReturn()) {
195  op->setBranch(true);
196  op->setControlFlowOperation(true);
197  }
198  if (tid.mayLoad()) {
199  op->setReadsMemory(true);
200  }
201  if (tid.mayStore()) {
202  op->setWritesMemory(true);
203  }
204  return op;
205 }
206 
207 /**
208  * Returns the operation index of operation pool.
209  *
210  * @return The operation index.
211  */
214  assert(index_ != NULL);
215  return *index_;
216 }
217 
218 bool
220  if (op.affectsCount() > 0 || op.affectedByCount() > 0)
221  return true;
222  for (const auto& entry : operationCache_) {
223  const Operation& other = *entry.second;
224  if (other.dependsOn(op))
225  return true;
226  }
227  return false;
228 }
Operand
Definition: Operand.hh:52
Operation::affectedByCount
virtual int affectedByCount() const
Definition: Operation.cc:416
OperationBehaviorProxy.hh
OperationPoolPimpl::index
OperationIndex & index()
Definition: OperationPoolPimpl.cc:213
OperationIndex::addPath
void addPath(const std::string &path)
Definition: OperationIndex.cc:82
FileSystem.hh
Operation::setReadsMemory
virtual void setReadsMemory(bool setting)
Definition: Operation.cc:631
TCEString::lower
TCEString lower() const
Definition: TCEString.cc:78
OperationPoolPimpl::OperationTable
std::map< std::string, Operation * > OperationTable
Container for operations indexed by their names.
Definition: OperationPoolPimpl.hh:71
OperationDAGBehavior.hh
OperationPoolPimpl::cleanupCache
static void cleanupCache()
Definition: OperationPoolPimpl.cc:100
Operation::addOutput
virtual void addOutput(Operand *operand)
Definition: Operation.cc:513
SequenceTools.hh
Operand::UINT_WORD
@ UINT_WORD
Definition: Operand.hh:60
Operation::dependsOn
virtual bool dependsOn(const Operation &op) const
Definition: Operation.cc:458
OperationBehaviorLoader.hh
NullOperation::instance
static NullOperation & instance()
TCEString.hh
StringTools.hh
assert
#define assert(condition)
Definition: Application.hh:86
OperationPoolPimpl::sharesState
bool sharesState(const Operation &op)
Definition: OperationPoolPimpl.cc:219
Operation::setCall
virtual void setCall(bool setting)
Definition: Operation.cc:359
abortWithError
#define abortWithError(message)
Definition: Application.hh:72
OperationPoolPimpl::operationCache_
static OperationTable operationCache_
Contains all operations that have been already requested by the client.
Definition: OperationPoolPimpl.hh:87
Operation::setControlFlowOperation
virtual void setControlFlowOperation(bool setting)
Definition: Operation.cc:335
OperationPoolPimpl::OperationPoolPimpl
OperationPoolPimpl()
Definition: OperationPoolPimpl.cc:75
OperationIndex.hh
Application.hh
OperationPoolPimpl::index_
static OperationIndex * index_
Indexed table used to find out which operation module contains the given operation.
Definition: OperationPoolPimpl.hh:82
ObjectState.hh
OperationSerializer.hh
Operation.hh
Environment.hh
Environment::osalPaths
static std::vector< std::string > osalPaths()
Definition: Environment.cc:519
Operation
Definition: Operation.hh:59
Operand.hh
OperationPoolPimpl::llvmTargetInstrInfo_
static const llvm::MCInstrInfo * llvmTargetInstrInfo_
If this is set, OSAL data is loaded from the TargetInstrInfo instead of .opp XML files....
Definition: OperationPoolPimpl.hh:93
NullOperationBehavior::instance
static NullOperationBehavior & instance()
Definition: OperationBehavior.hh:95
OperationPoolPimpl::loadFromLLVM
Operation * loadFromLLVM(const llvm::MCInstrDesc &tid)
Definition: OperationPoolPimpl.cc:164
Operation::addInput
virtual void addInput(Operand *operand)
Definition: Operation.cc:508
OperationPoolPimpl::operation
Operation & operation(const char *name)
Definition: OperationPoolPimpl.cc:116
OperationModule
Definition: OperationModule.hh:46
OperationIndex::effectiveOperation
Operation * effectiveOperation(const TCEString &name)
Definition: OperationIndex.cc:454
OperationPoolPimpl.hh
AssocTools.hh
TCEString
Definition: TCEString.hh:53
OperationIndex
Definition: OperationIndex.hh:58
Operation::setWritesMemory
virtual void setWritesMemory(bool setting)
Definition: Operation.cc:639
NullOperationModule::instance
static NullOperationModule & instance()
OperationPool.hh
OperationPoolPimpl::~OperationPoolPimpl
~OperationPoolPimpl()
Definition: OperationPoolPimpl.cc:90
AssocTools::deleteAllValues
static void deleteAllValues(ContainerType &aMap)
Operation::affectsCount
virtual int affectsCount() const
Definition: Operation.cc:402
OperationIndex::moduleOf
OperationModule & moduleOf(const std::string &name)
Definition: OperationIndex.cc:315
Operation::setBranch
virtual void setBranch(bool setting)
Definition: Operation.cc:368
StringTools::stringToLower
static std::string stringToLower(const std::string &source)
Definition: StringTools.cc:160
OperationModule.hh