forked from Hoernchen/Epiphany
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EpiphanyTargetMachine.cpp
180 lines (144 loc) · 5.37 KB
/
EpiphanyTargetMachine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//===-- EpiphanyTargetMachine.cpp - Define TargetMachine for Epiphany -------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains the implementation of the EpiphanyTargetMachine
// methods. Principally just setting up the passes needed to generate correct
// code on this architecture.
//
//===----------------------------------------------------------------------===//
#include "EpiphanyTargetMachine.h"
#include "EpiphanyISelDAGToDAG.h"
#include "EpiphanyTargetObjectFile.h"
#include "EpiphanyTargetTransformInfo.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Vectorize.h"
using namespace llvm;
static cl::opt<bool> EnableSROA(
"epiphany-sroa",
cl::desc("Run SROA after promote alloca pass"),
cl::ReallyHidden,
cl::init(true));
static cl::opt<bool> EnableLSOpt(
"epiphany-lsopt",
cl::desc("Run Epiphany Load/Store Optimization Pass"),
cl::ReallyHidden,
cl::init(true));
#define DEBUG_TYPE "epiphany"
extern "C" void LLVMInitializeEpiphanyTarget() {
RegisterTargetMachine<EpiphanyTargetMachine> X(TheEpiphanyTarget);
}
static Reloc::Model getEffectiveRelocModel(CodeModel::Model CM,
Optional<Reloc::Model> RM) {
if (!RM.hasValue() || CM == CodeModel::JITDefault)
return Reloc::Static;
return *RM;
}
static std::string computeDataLayout(const Triple &TT, StringRef CPU,
const TargetOptions &Options) {
std::string Ret = "";
// Always little-endian
Ret += "e";
// Pointers are 32 bit
Ret += "-p:32:32";
// Minimal alignment for E16 is byte
Ret += "-i8:8-i16:16-i32:32-i64:64";
// Vector alignment is better to keep at dword for wide loads/stores
Ret += "-v32:64-v64:64";
// 32 and 64 bit floats should have natural alignment
Ret += "-f32:32-f64:64";
// Native integer is 32 bits
Ret += "-n32";
// Stack is 64 bit aligned (don't want to mess with type-based alignment atm)
Ret += "-S64";
return Ret;
}
EpiphanyTargetMachine::EpiphanyTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
Optional<Reloc::Model> RM,
CodeModel::Model CM,
CodeGenOpt::Level OL)
: LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options),
TT, CPU, FS, Options, getEffectiveRelocModel(CM, RM), CM, OL),
TLOF(make_unique<EpiphanyTargetObjectFile>()),
ABI(EpiphanyABIInfo::computeTargetABI()),
Subtarget(TT, CPU, FS, *this) {
// initAsmInfo will display features by llc -march=cpu0 -mcpu=help on 3.7 but
// not on 3.6
initAsmInfo();
}
EpiphanyTargetMachine::~EpiphanyTargetMachine() {}
namespace {
//@EpiphanyPassConfig
/// Epiphany Code Generator Pass Configuration Options.
class EpiphanyPassConfig : public TargetPassConfig {
public:
EpiphanyPassConfig(EpiphanyTargetMachine *TM, PassManagerBase &PM)
: TargetPassConfig(TM, PM) {}
EpiphanyTargetMachine &getEpiphanyTargetMachine() const {
return getTM<EpiphanyTargetMachine>();
}
bool addILPOpts() override;
bool addInstSelector() override;
void addIRPasses() override;
void addCodeGenPrepare() override;
void addPreRegAlloc() override;
void addPreSched2() override;
void addPreEmitPass() override;
const EpiphanySubtarget &getEpiphanySubtarget() const {
return *getEpiphanyTargetMachine().getSubtargetImpl();
}
};
} // namespace
TargetPassConfig *EpiphanyTargetMachine::createPassConfig(PassManagerBase &PM) {
return new EpiphanyPassConfig(this, PM);
}
void EpiphanyPassConfig::addIRPasses() {
addPass(createAtomicExpandPass(&getEpiphanyTargetMachine()));
if (EnableSROA && (TM->getOptLevel() != CodeGenOpt::None)) {
addPass(createSROAPass());
}
TargetPassConfig::addIRPasses();
}
bool EpiphanyPassConfig::addILPOpts() {
addPass(&EarlyIfConverterID);
//if (EnableMachineCombinerPass)
//addPass(&MachineCombinerID);
return true;
}
bool EpiphanyPassConfig::addInstSelector() {
addPass(new EpiphanyDAGToDAGISel(getEpiphanyTargetMachine(), getOptLevel()));
addPass(createEpiphanyFpuConfigPass());
return false;
}
void EpiphanyPassConfig::addCodeGenPrepare() {
TargetPassConfig::addCodeGenPrepare();
addPass(createLoadStoreVectorizerPass());
}
void EpiphanyPassConfig::addPreRegAlloc() {
addPass(&LiveVariablesID, false);
if (EnableLSOpt && TM->getOptLevel() != CodeGenOpt::None)
addPass(createEpiphanyVregLoadStoreOptimizationPass());
}
void EpiphanyPassConfig::addPreSched2() {
addPass(&IfConverterID, false);
}
void EpiphanyPassConfig::addPreEmitPass() {
if (EnableLSOpt && TM->getOptLevel() != CodeGenOpt::None)
addPass(createEpiphanyLoadStoreOptimizationPass());
}
TargetIRAnalysis EpiphanyTargetMachine::getTargetIRAnalysis() {
return TargetIRAnalysis([this](const Function &F) {
return TargetTransformInfo(EpiphanyTTIImpl(this, F));
});
}