OpenASIP 2.2
Loading...
Searching...
No Matches
LLVMTCEBuilder.hh
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 LLVMTCEBuilder.hh
26 *
27 * Declaration of LLVMTCEBuilder class.
28 *
29 * @author Pekka Jääskeläinen 2010-2015
30 * @note reting: red
31 */
32
33#ifndef LLVM_TCE_BUILDER_H
34#define LLVM_TCE_BUILDER_H
35
36#include <map>
37#include <set>
38#include <llvm/CodeGen/MachineFunctionPass.h>
39#include <llvm/CodeGen/MachineConstantPool.h>
40#include <llvm/CodeGen/MachineOperand.h>
41#include <llvm/CodeGen/MachineInstr.h>
42#include <llvm/CodeGen/MachineBasicBlock.h>
43
44#include "tce_config.h"
45#include <llvm/IR/Mangler.h>
46#include <llvm/IR/Constant.h>
47#include <llvm/Transforms/IPO.h>
48
49// tce_config.h defines these. this undef to avoid warning.
50// TODO: how to do this in tce_config.h???
51#ifdef LLVM_LIBDIR
52#undef LLVM_LIBDIR
53#endif
54
55#include "Exception.hh"
56#include "BaseType.hh"
57#include "TCEString.hh"
58#include "passes/MachineDCE.hh"
59#include "TCETargetMachine.hh"
60#include "ProgramOperation.hh"
61#include "InlineAsmParser.hh"
62
63namespace TTAProgram {
64 class Program;
65 class Procedure;
66 class CodeSnippet;
67 class BasicBlock;
68 class Terminal;
69 class TerminalRegister;
70 class TerminalInstructionAddress;
71 class InstructionReferenceManager;
72 class TerminalProgramOperation;
73 class Instruction;
74 class DataMemory;
75 class DataDefinition;
76 class Move;
77 class MoveGuard;
78}
79
82class Operation;
84
85namespace TTAMachine {
86 class Machine;
87 class AddressSpace;
88 class Bus;
89 class HWOperation;
90}
91namespace llvm {
92
93 class ConstantInt;
94 class ConstantFP;
95 class ConstantExpr;
96 class ConstantDataSequential;
97 class TCETargetMachine;
98
99 /**
100 * Base class for the various LLVM to TCE builder implementations.
101 */
102 class LLVMTCEBuilder : public MachineFunctionPass {
103
104 public:
105 static char ID; // Pass identification, replacement for typeid
106
107 LLVMTCEBuilder(char& ID);
108
110 const TargetMachine& tm,
112 char& ID,
113 bool functionAtATime=false);
114
115 virtual ~LLVMTCEBuilder();
116
118
120 const llvm::BasicBlock* bb) {
121 return bbIndex_[bb];
122 }
124
125 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.addRequired<AAResultsWrapperPass>();
127 AU.addRequired<MachineDCE>();
128 AU.addPreserved<MachineDCE>();
129 MachineFunctionPass::getAnalysisUsage(AU);
130 }
131
132 virtual bool isTTATarget() const {
133 return (dynamic_cast<const TCETargetMachine*>(
134 &targetMachine()) != NULL);
135 }
136
138
139 void setInitialStackPointerValue(unsigned value);
140
141 protected:
142
143 bool doInitialization(Module &M);
144 bool runOnMachineFunction(MachineFunction &MF);
145 bool doFinalization(Module &M);
146
148 const TTAProgram::Instruction& instr) const;
149
150 virtual bool writeMachineFunction(MachineFunction &MF);
151 void initDataSections();
152
153 std::shared_ptr<TTAProgram::Move> createMove(
154 const MachineOperand& src, const MachineOperand& dst,
155 TTAProgram::MoveGuard* guard);
156
157 const TargetMachine& targetMachine() const { return *tm_; }
158
159 /* Methods used for overriding TTA backend-specific behavior. */
160 // the stack pointer register's llvm reg number
161 virtual unsigned spDRegNum() const = 0;
162 // the return address register's llvm reg number
163 virtual unsigned raPortDRegNum() const = 0;
164 // the ADF register file name of the llvm reg number
165 virtual TCEString registerFileName(unsigned llvmRegNum) const = 0;
166 // the ADF register index of the llvm reg number
167 virtual int registerIndex(unsigned llvmRegNum) const = 0;
168 // OSAL operation name from a LLVM MachineInstr
169 TCEString registerName(unsigned llvmRegNum) const;
170 virtual TCEString operationName(const MachineInstr& mi) const = 0;
172 const MachineOperand&) const {
173 return NULL;
174 }
175
176 /* Helper methods */
177 std::string mbbName(const MachineBasicBlock& mbb);
178
179 const TTAMachine::HWOperation& getHWOperation(std::string opName);
180
182 const std::string& rfName, int index);
183
185 const MachineOperand& mo, int bitLimit = 0);
186
187 std::shared_ptr<TTAProgram::Move> createMove(
190 const TTAMachine::Bus &bus,
191 TTAProgram::MoveGuard *guard = NULL);
192
193 void emitConstantPool(const llvm::MachineConstantPool& cp);
194
196 const MachineOperand& mo);
197
199 const MachineOperand& mo);
200
202 const MachineOperand& mo);
203
205 const TCEString& symbolName);
206
208 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
209
211 const MachineInstr* mi, TTAProgram::CodeSnippet* proc,
212 bool conditional=false, bool trueGuard=true);
213
215 const MachineFunction& mf,
216 const MachineInstr* mi,
219
221
223 TCEString label, ProgramOperationPtr po) {
224 labeledPOs_[label] = po;
225 }
226
227 virtual void emitSPInitialization();
228
230
232 labeledPOs_.clear();
233 symbolicPORefs_.clear();
234 }
235
236 static bool isInlineAsm(const MachineInstr& instr);
237
238 /// Code labels.
239 std::map<std::string, TTAProgram::Instruction*> codeLabels_;
240
241 /// Machine for building the program.
243
244 /// Target machine description.
245 const llvm::TargetMachine* tm_;
246 /// Mangler for mangling label strings.
247 llvm::Mangler* mang_;
248
249 /// Current program being built.
251
252 /// The operations supported by the current target machine.
253 std::set<std::string> opset_;
254
255 // set to true in case the builder is used to schedule one
256 // function at a time (the default processes the whole module)
258
260
262
264
265 MachineFrameInfo* curFrameInfo_;
266
267 /// The compiler options.
269
270 private:
271
272 /// Data definition structure for global values.
273 struct DataDef {
274 std::string name;
275 //llvm::Constant* initializer;
276 unsigned address;
278 unsigned alignment;
279 unsigned size;
281 };
282
283 /// Data definition structure for constant pool values
285 unsigned address = 0;
286 unsigned alignment = 0;
287 unsigned size = 0;
288 const llvm::Constant* value = nullptr;
289
291 unsigned addr,
292 unsigned align,
293 unsigned size,
294 const llvm::Constant* value)
295 : address(addr), alignment(align), size(size), value(value) {}
296 };
297
298 typedef std::map<TTAMachine::AddressSpace*, TTAProgram::DataMemory*>
300
301 void initMembers();
302
303 void emitDataDef(const DataDef& def);
304 void emitDataDef(const ConstantDataDef& def);
305
306 unsigned createDataDefinition(
307 int addressSpaceId, unsigned& addr, const Constant* cv,
308 bool forceInitialize=false, unsigned forceAlignment=0);
309
311 int addressSpaceId, unsigned& addr, const llvm::ConstantInt* ci,
312 bool isPointer = false);
313
315 int addressSpaceId, unsigned& addr, const llvm::ConstantFP* cfp);
316
318 int addressSpaceId, unsigned& addr, const GlobalValue* gv,
319 int offset = 0);
320
322 int addressSpaceId, unsigned& addr, const ConstantExpr* gv,
323 int offset = 0);
324
325 void padToAlignment(
326 int addressSpaceId, unsigned& addr, unsigned align);
327
329 const MachineOperand& base, const MachineOperand& offset);
330
332 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
333
335 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
336
338 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
339
341 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
342
344 const std::string op,
345 const MachineInstr* mi,
347
349 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
350
352 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
353
355 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
356
358 const MachineInstr* mi, TTAProgram::CodeSnippet* proc,
359 bool constructors);
360
362 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
363
365 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
366
368 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
369
371 const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
372
374 TCEString firstOp, const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
375
377 TCEString opName, const MachineInstr* mi, TTAProgram::CodeSnippet* proc);
378
379 void createSPInitLoad(
383
384 bool isInitialized(const Constant* cv);
385
387 const TTAProgram::Terminal* guardReg, bool trueOrFalse);
388
390 const llvm::MachineInstr* mi, TTAProgram::Move& move);
392 const llvm::MachineInstr* mi, TTAProgram::Move& move);
393
394 bool isBaseOffsetMemOperation(const Operation& operation) const;
395
396 // Create MoveNodes before calling DDGBuilder.
397 virtual void createMoveNode(
399 std::shared_ptr<TTAProgram::Move> m,
400 bool /*isDestination*/) {}
401
404 unsigned& dataEnd(TTAMachine::AddressSpace& aSpace);
405
407 unsigned asNum, TTAProgram::Move& move);
408
411
413 const std::vector<TTAProgram::Instruction*>& operandMoves,
414 TTAProgram::Move& move) const;
415
416 std::string getAsmString(const MachineInstr& mi) const;
417
418 /// Target architechture MAU size in bits.
419 static unsigned MAU_BITS;
420
421 /// Target architecture pointer size in maus.
422 static unsigned POINTER_SIZE_32;
423 static unsigned POINTER_SIZE_64;
424
425 llvm::Module* mod_;
426
428
429 /// The default data memory address space (address space 0).
431
432 /// Set to true in case this machine has more than one data
433 /// address spaces.
435
436#if 0
437 /// Data memory initializations.
439#endif
441
442 /// Data definitions.
443 std::vector<DataDef> data_;
444 std::vector<DataDef> udata_;
445 std::vector<ConstantDataDef> cpData_;
446
447 /// Machine basic block -> first instruction in the BB map.
448 std::map<std::string, TTAProgram::Instruction*> mbbs_;
449
450 /// Basic Block -> first instruction in the BB map
451 std::map<const llvm::BasicBlock*, TTAProgram::Instruction*> bbIndex_;
452
453 /// Data labels.
454 std::map<std::string, unsigned> dataLabels_;
455
456 /// Dummy code label references that have to be fixed after
457 /// all instrutions have been built.
458 std::map<TTAProgram::TerminalInstructionAddress*, std::string>
460
461 /// Dummy basic block references that have to be fixed after
462 /// all basic blocks have been built.
464 std::string>
466
467 /// Dummy references to the _end symbol.
468 std::vector<std::shared_ptr<TTAProgram::Move> > endReferences_;
469
470 /// Global constant pool for all constants gathered from machine
471 /// functions. Map key is unique constant and the value is address of
472 /// the constant.
473 std::map<const llvm::Constant*, unsigned> globalCP_;
474 /// Constant pool for the current machine function. Map key is constant
475 /// pool index and the value is address.
476 std::map<unsigned, unsigned> currentFnCP_;
477
478 /// The first position after the last data in the given address space.
479 std::map<TTAMachine::AddressSpace*, unsigned> dataEnds_;
480
481#if 0
482 unsigned end_;
483#endif
484
485 /// set to true in case at least one 'noalias' attribute (from
486 /// the use of 'restricted' pointers) has been found
488 /// set to true in case at least one non-default address space
489 /// memory access has been found in the generated code
491
492 /// List of machine functions collected from runForMachineFunction.
493 std::vector<MachineFunction*> functions_;
494
496
497 std::set<TTAProgram::TerminalProgramOperation*> symbolicPORefs_;
498
499 std::map<TCEString, ProgramOperationPtr > labeledPOs_;
500
501 /// The data layout for the machine.
502 const llvm::DataLayout* dl_;
503 };
504}
505#endif
std::shared_ptr< ProgramOperation > ProgramOperationPtr
Definition MoveNode.hh:53
MachineFrameInfo * curFrameInfo_
static unsigned POINTER_SIZE_64
std::map< TTAMachine::AddressSpace *, unsigned > dataEnds_
The first position after the last data in the given address space.
bool hasAmbiguousASpaceRefs(const TTAProgram::Instruction &instr) const
unsigned createDataDefinition(int addressSpaceId, unsigned &addr, const Constant *cv, bool forceInitialize=false, unsigned forceAlignment=0)
bool isProgramUsingRestrictedPointers() const
std::set< std::string > opset_
The operations supported by the current target machine.
virtual TTAProgram::Terminal * createMBBReference(const MachineOperand &mo)
std::string getAsmString(const MachineInstr &mi) const
void copyFUAnnotations(const std::vector< TTAProgram::Instruction * > &operandMoves, TTAProgram::Move &move) const
TTAProgram::Program * result()
TTAProgram::Instruction * emitSelect(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
std::vector< DataDef > data_
Data definitions.
TTAMachine::Machine * mach_
Machine for building the program.
TTAProgram::Instruction * emitSpecialInlineAsm(const std::string op, const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
static unsigned MAU_BITS
Target architechture MAU size in bits.
TTAProgram::DataMemory & dataMemoryForAddressSpace(TTAMachine::AddressSpace &aSpace)
TTAMachine::AddressSpace & addressSpaceById(unsigned id)
TTAProgram::TerminalRegister * createTerminalRegister(const std::string &rfName, int index)
virtual unsigned raPortDRegNum() const =0
std::map< TCEString, ProgramOperationPtr > labeledPOs_
std::vector< DataDef > udata_
std::set< TTAProgram::TerminalProgramOperation * > symbolicPORefs_
TTAProgram::Instruction * emitComparisonForBranch(TCEString firstOp, const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
void createExprDataDefinition(int addressSpaceId, unsigned &addr, const ConstantExpr *gv, int offset=0)
TTAMachine::AddressSpace * defaultDataAddressSpace_
The default data memory address space (address space 0).
TTAProgram::Instruction * emitLoad(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
unsigned & dataEnd(TTAMachine::AddressSpace &aSpace)
TTAProgram::Instruction * firstInstructionOfBasicBlock(const llvm::BasicBlock *bb)
unsigned addressSpaceId(TTAMachine::AddressSpace &aSpace) const
TTAProgram::Instruction * emitOperationMacro(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
std::shared_ptr< TTAProgram::Move > createMove(const MachineOperand &src, const MachineOperand &dst, TTAProgram::MoveGuard *guard)
TTAProgram::Instruction * handleMemoryCategoryInfo(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
static unsigned POINTER_SIZE_32
Target architecture pointer size in maus.
TTAProgram::Instruction * emitReturnTo(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
const llvm::TargetMachine * tm_
Target machine description.
TTAProgram::Instruction * emitRemaingingBrach(TCEString opName, const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
TTAProgram::Terminal * createTerminal(const MachineOperand &mo, int bitLimit=0)
LLVMTCECmdLineOptions * options_
The compiler options.
void createFPDataDefinition(int addressSpaceId, unsigned &addr, const llvm::ConstantFP *cfp)
void createSPInitLoad(TTAProgram::CodeSnippet &target, TTAProgram::Terminal &src, TTAProgram::Terminal &dst)
TTAProgram::Instruction * emitSetjmp(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
virtual unsigned spDRegNum() const =0
std::string mbbName(const MachineBasicBlock &mbb)
TTAProgram::Instruction * emitGlobalXXtructorCalls(const MachineInstr *mi, TTAProgram::CodeSnippet *proc, bool constructors)
void addCandidateLSUAnnotations(unsigned asNum, TTAProgram::Move &move)
TTAProgram::Terminal * createAddrTerminal(const MachineOperand &base, const MachineOperand &offset)
std::map< const llvm::BasicBlock *, TTAProgram::Instruction * > bbIndex_
Basic Block -> first instruction in the BB map.
static bool isInlineAsm(const MachineInstr &instr)
llvm::Mangler * mang_
Mangler for mangling label strings.
std::map< std::string, unsigned > dataLabels_
Data labels.
bool isBaseOffsetMemOperation(const Operation &operation) const
std::map< const llvm::Constant *, unsigned > globalCP_
Global constant pool for all constants gathered from machine functions. Map key is unique constant an...
TTAProgram::Instruction * emitWriteSP(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
std::vector< std::shared_ptr< TTAProgram::Move > > endReferences_
Dummy references to the _end symbol.
TTAProgram::Instruction * emitLongjmp(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
std::map< std::string, TTAProgram::Instruction * > mbbs_
Machine basic block -> first instruction in the BB map.
void createGlobalValueDataDefinition(int addressSpaceId, unsigned &addr, const GlobalValue *gv, int offset=0)
std::vector< MachineFunction * > functions_
List of machine functions collected from runForMachineFunction.
std::map< TTAProgram::TerminalInstructionAddress *, std::string > codeLabelReferences_
Dummy code label references that have to be fixed after all instrutions have been built.
virtual void emitSPInitialization()
void debugDataToAnnotations(const llvm::MachineInstr *mi, TTAProgram::Move &move)
TTAProgram::Program * prog_
Current program being built.
TCEString registerName(unsigned llvmRegNum) const
void addLabelForProgramOperation(TCEString label, ProgramOperationPtr po)
void emitDataDef(const DataDef &def)
TTAProgram::MoveGuard * createGuard(const TTAProgram::Terminal *guardReg, bool trueOrFalse)
std::vector< ConstantDataDef > cpData_
void emitConstantPool(const llvm::MachineConstantPool &cp)
const TTAMachine::HWOperation & getHWOperation(std::string opName)
std::map< TTAMachine::AddressSpace *, TTAProgram::DataMemory * > DataMemIndex
bool multiDataMemMachine_
Set to true in case this machine has more than one data address spaces.
TTAProgram::Instruction * emitInstruction(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
bool isInitialized(const Constant *cv)
bool noAliasFound_
set to true in case at least one 'noalias' attribute (from the use of 'restricted' pointers) has been...
PRegionMarkerAnalyzer * pregions_
std::map< unsigned, unsigned > currentFnCP_
Constant pool for the current machine function. Map key is constant pool index and the value is addre...
const TargetMachine & targetMachine() const
std::map< TTAProgram::TerminalInstructionAddress *, std::string > mbbReferences_
Dummy basic block references that have to be fixed after all basic blocks have been built.
virtual TTAProgram::Terminal * createFUTerminal(const MachineOperand &) const
virtual TCEString operationName(const MachineInstr &mi) const =0
void setInitialStackPointerValue(unsigned value)
void createIntDataDefinition(int addressSpaceId, unsigned &addr, const llvm::ConstantInt *ci, bool isPointer=false)
void addPointerAnnotations(const llvm::MachineInstr *mi, TTAProgram::Move &move)
std::map< std::string, TTAProgram::Instruction * > codeLabels_
Code labels.
bool doFinalization(Module &M)
const llvm::DataLayout * dl_
The data layout for the machine.
TTAProgram::Instruction * emitInlineAsm(const MachineFunction &mf, const MachineInstr *mi, TTAProgram::BasicBlock *bb, TTAProgram::InstructionReferenceManager &irm)
virtual int registerIndex(unsigned llvmRegNum) const =0
virtual TTAProgram::Terminal * createSymbolReference(const MachineOperand &mo)
bool multiAddrSpacesFound_
set to true in case at least one non-default address space memory access has been found in the genera...
virtual TCEString registerFileName(unsigned llvmRegNum) const =0
TTAProgram::Instruction * emitReadSP(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
virtual TTAProgram::Terminal * createProgramOperationReference(const MachineOperand &mo)
bool runOnMachineFunction(MachineFunction &MF)
TTAProgram::Instruction * emitStore(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
virtual TTAProgram::Instruction * emitMove(const MachineInstr *mi, TTAProgram::CodeSnippet *proc, bool conditional=false, bool trueGuard=true)
virtual bool isTTATarget() const
virtual bool writeMachineFunction(MachineFunction &MF)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
TTAProgram::Instruction * emitReturn(const MachineInstr *mi, TTAProgram::CodeSnippet *proc)
TTAMachine::AddressSpace * instrAddressSpace_
virtual void createMoveNode(ProgramOperationPtr &, std::shared_ptr< TTAProgram::Move > m, bool)
bool doInitialization(Module &M)
void padToAlignment(int addressSpaceId, unsigned &addr, unsigned align)
Data definition structure for constant pool values.
ConstantDataDef(unsigned addr, unsigned align, unsigned size, const llvm::Constant *value)
Data definition structure for global values.