blob: 6ed0d19a1759b3875f3b2d883638fc2fadb21f9a [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- HexagonTargetMachine.cpp - Define TargetMachine for Hexagon -------===//
Tony Linthicum1213a7a2011-12-12 21:14:40 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Jia Liub22310f2012-02-18 12:03:15 +000010// Implements the info about Hexagon target spec.
Tony Linthicum1213a7a2011-12-12 21:14:40 +000011//
12//===----------------------------------------------------------------------===//
13
Tony Linthicum1213a7a2011-12-12 21:14:40 +000014#include "HexagonTargetMachine.h"
15#include "Hexagon.h"
16#include "HexagonISelLowering.h"
Sergei Larin4d8986a2012-09-04 14:49:56 +000017#include "HexagonMachineScheduler.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000018#include "HexagonTargetObjectFile.h"
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +000019#include "HexagonTargetTransformInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000020#include "llvm/CodeGen/Passes.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000021#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Module.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000023#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000024#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Transforms/Scalar.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000026
Tony Linthicum1213a7a2011-12-12 21:14:40 +000027using namespace llvm;
28
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000029static cl:: opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000030 cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000031
Jyotsna Verma653d8832013-03-27 11:14:24 +000032static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000033 cl::Hidden, cl::ZeroOrMore, cl::init(false),
34 cl::desc("Disable Hexagon CFG Optimization"));
35
36static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
37 cl::init(true), cl::Hidden, cl::ZeroOrMore,
38 cl::desc("Early expansion of MUX"));
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000039
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +000040static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
41 cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
42
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000043static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
44 cl::Hidden, cl::desc("Generate \"insert\" instructions"));
Jyotsna Verma653d8832013-03-27 11:14:24 +000045
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000046static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
47 cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
48
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000049static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
50 cl::Hidden, cl::desc("Generate \"extract\" instructions"));
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000051
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000052static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
53 cl::desc("Enable converting conditional transfers into MUX instructions"));
54
Krzysztof Parzyszek75874472015-07-14 19:30:21 +000055static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
56 cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
57 "predicate instructions"));
58
Tony Linthicum1213a7a2011-12-12 21:14:40 +000059/// HexagonTargetMachineModule - Note that this is used on hosts that
60/// cannot link in a library unless there are references into the
61/// library. In particular, it seems that it is not possible to get
62/// things to work on Win32 without this. Though it is unused, do not
63/// remove it.
64extern "C" int HexagonTargetMachineModule;
65int HexagonTargetMachineModule = 0;
66
67extern "C" void LLVMInitializeHexagonTarget() {
68 // Register the target.
69 RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000070}
71
Sergei Larin4d8986a2012-09-04 14:49:56 +000072static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
David Blaikie422b93d2014-04-21 20:32:32 +000073 return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
Sergei Larin4d8986a2012-09-04 14:49:56 +000074}
75
76static MachineSchedRegistry
77SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
78 createVLIWMachineSched);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000079
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000080namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000081 FunctionPass *createHexagonCFGOptimizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000082 FunctionPass *createHexagonCommonGEP();
83 FunctionPass *createHexagonCopyToCombine();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +000084 FunctionPass *createHexagonEarlyIfConversion();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000085 FunctionPass *createHexagonExpandCondsets();
Colin LeMahieu56efafc2015-06-15 19:05:35 +000086 FunctionPass *createHexagonExpandPredSpillCode();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000087 FunctionPass *createHexagonFixupHwLoops();
88 FunctionPass *createHexagonGenExtract();
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000089 FunctionPass *createHexagonGenInsert();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000090 FunctionPass *createHexagonGenMux();
Krzysztof Parzyszek75874472015-07-14 19:30:21 +000091 FunctionPass *createHexagonGenPredicate();
Colin LeMahieu56efafc2015-06-15 19:05:35 +000092 FunctionPass *createHexagonHardwareLoops();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000093 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
94 CodeGenOpt::Level OptLevel);
Colin LeMahieu56efafc2015-06-15 19:05:35 +000095 FunctionPass *createHexagonNewValueJump();
Colin LeMahieu56efafc2015-06-15 19:05:35 +000096 FunctionPass *createHexagonPacketizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000097 FunctionPass *createHexagonPeephole();
98 FunctionPass *createHexagonRemoveExtendArgs(const HexagonTargetMachine &TM);
99 FunctionPass *createHexagonSplitConst32AndConst64();
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000100} // end namespace llvm;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000101
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000102/// HexagonTargetMachine ctor - Create an ILP32 architecture model.
103///
104
105/// Hexagon_TODO: Do I need an aggregate alignment?
106///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000107HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000108 StringRef CPU, StringRef FS,
Craig Topperb5454082012-03-17 09:24:09 +0000109 const TargetOptions &Options,
Eric Christopher0d0b3602014-06-27 00:13:43 +0000110 Reloc::Model RM, CodeModel::Model CM,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000111 CodeGenOpt::Level OL)
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000112 : LLVMTargetMachine(T, "e-m:e-p:32:32-i1:32-i64:64-a:0-n32", TT, CPU, FS,
113 Options, RM, CM, OL),
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000114 TLOF(make_unique<HexagonTargetObjectFile>()) {
115 initAsmInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000116}
117
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000118const HexagonSubtarget *
119HexagonTargetMachine::getSubtargetImpl(const Function &F) const {
120 AttributeSet FnAttrs = F.getAttributes();
121 Attribute CPUAttr =
122 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
123 Attribute FSAttr =
124 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
125
126 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
127 ? CPUAttr.getValueAsString().str()
128 : TargetCPU;
129 std::string FS = !FSAttr.hasAttribute(Attribute::None)
130 ? FSAttr.getValueAsString().str()
131 : TargetFS;
132
133 auto &I = SubtargetMap[CPU + FS];
134 if (!I) {
135 // This needs to be done before we create a new subtarget since any
136 // creation will depend on the TM and the code generation flags on the
137 // function that reside in TargetOptions.
138 resetTargetOptions(F);
139 I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
140 }
141 return I.get();
142}
143
144TargetIRAnalysis HexagonTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000145 return TargetIRAnalysis([this](const Function &F) {
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000146 return TargetTransformInfo(HexagonTTIImpl(this, F));
147 });
148}
149
150
Reid Kleckner357600e2014-11-20 23:37:18 +0000151HexagonTargetMachine::~HexagonTargetMachine() {}
152
Andrew Trickccb67362012-02-03 05:12:41 +0000153namespace {
154/// Hexagon Code Generator Pass Configuration Options.
155class HexagonPassConfig : public TargetPassConfig {
156public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000157 HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000158 : TargetPassConfig(TM, PM) {
159 bool NoOpt = (TM->getOptLevel() == CodeGenOpt::None);
160 if (!NoOpt) {
161 if (EnableExpandCondsets) {
162 Pass *Exp = createHexagonExpandCondsets();
163 insertPass(&RegisterCoalescerID, IdentifyingPassPtr(Exp));
164 }
165 }
166 }
Andrew Trickccb67362012-02-03 05:12:41 +0000167
168 HexagonTargetMachine &getHexagonTargetMachine() const {
169 return getTM<HexagonTargetMachine>();
170 }
171
Craig Topper906c2cd2014-04-29 07:58:16 +0000172 ScheduleDAGInstrs *
173 createMachineScheduler(MachineSchedContext *C) const override {
Andrew Trick978674b2013-09-20 05:14:41 +0000174 return createVLIWMachineSched(C);
175 }
176
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000177 void addIRPasses() override;
Craig Topper906c2cd2014-04-29 07:58:16 +0000178 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000179 void addPreRegAlloc() override;
180 void addPostRegAlloc() override;
181 void addPreSched2() override;
182 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000183};
184} // namespace
185
Andrew Trickf8ea1082012-02-04 02:56:59 +0000186TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
187 return new HexagonPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000188}
189
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000190void HexagonPassConfig::addIRPasses() {
191 TargetPassConfig::addIRPasses();
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000192 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +0000193
194 addPass(createAtomicExpandPass(TM));
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000195 if (!NoOpt) {
196 if (EnableCommGEP)
197 addPass(createHexagonCommonGEP());
198 // Replace certain combinations of shifts and ands with extracts.
199 if (EnableGenExtract)
200 addPass(createHexagonGenExtract());
201 }
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000202}
203
Andrew Trickccb67362012-02-03 05:12:41 +0000204bool HexagonPassConfig::addInstSelector() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000205 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000206 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000207
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000208 if (!NoOpt)
209 addPass(createHexagonRemoveExtendArgs(TM));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000210
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000211 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000212
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000213 if (!NoOpt) {
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000214 // Create logical operations on predicate registers.
215 if (EnableGenPred)
216 addPass(createHexagonGenPredicate(), false);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000217 addPass(createHexagonPeephole());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000218 printAndVerify("After hexagon peephole pass");
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000219 if (EnableGenInsert)
220 addPass(createHexagonGenInsert(), false);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000221 if (EnableEarlyIf)
222 addPass(createHexagonEarlyIfConversion(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000223 }
Jyotsna Verma653d8832013-03-27 11:14:24 +0000224
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000225 return false;
226}
227
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000228void HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000229 if (getOptLevel() != CodeGenOpt::None)
230 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000231 addPass(createHexagonHardwareLoops(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000232}
233
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000234void HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000235 if (getOptLevel() != CodeGenOpt::None)
236 if (!DisableHexagonCFGOpt)
Eric Christopher5c3376a2015-02-02 18:46:27 +0000237 addPass(createHexagonCFGOptimizer(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000238}
239
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000240void HexagonPassConfig::addPreSched2() {
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000241 addPass(createHexagonCopyToCombine(), false);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000242 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000243 addPass(&IfConverterID, false);
Eric Christopher01f875e2015-02-02 22:11:43 +0000244 addPass(createHexagonSplitConst32AndConst64());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000245}
246
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000247void HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000248 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000249
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000250 if (!NoOpt)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000251 addPass(createHexagonNewValueJump(), false);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000252
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000253 // Expand Spill code for predicate registers.
Eric Christopher6ff7ed62015-02-02 18:46:31 +0000254 addPass(createHexagonExpandPredSpillCode(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000256 // Create Packets.
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000257 if (!NoOpt) {
258 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000259 addPass(createHexagonFixupHwLoops(), false);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000260 // Generate MUX from pairs of conditional transfers.
261 if (EnableGenMux)
262 addPass(createHexagonGenMux(), false);
263
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000264 addPass(createHexagonPacketizer(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000265 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000266}