blob: cacfd4d8cba1d3e08ec394ed8f1c65982361c9a0 [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 Parzyszek12798812016-01-12 19:09:01 +000029
30static cl::opt<bool> EnableRDFOpt("rdf-opt", cl::Hidden, cl::ZeroOrMore,
31 cl::init(true), cl::desc("Enable RDF-based optimizations"));
32
33static cl::opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000034 cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000035
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000036static cl::opt<bool> DisableAModeOpt("disable-hexagon-amodeopt",
37 cl::Hidden, cl::ZeroOrMore, cl::init(false),
38 cl::desc("Disable Hexagon Addressing Mode Optimization"));
39
Jyotsna Verma653d8832013-03-27 11:14:24 +000040static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000041 cl::Hidden, cl::ZeroOrMore, cl::init(false),
42 cl::desc("Disable Hexagon CFG Optimization"));
43
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +000044static cl::opt<bool> DisableStoreWidening("disable-store-widen",
45 cl::Hidden, cl::init(false), cl::desc("Disable store widening"));
46
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000047static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
48 cl::init(true), cl::Hidden, cl::ZeroOrMore,
49 cl::desc("Early expansion of MUX"));
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000050
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +000051static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
52 cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
53
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000054static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
55 cl::Hidden, cl::desc("Generate \"insert\" instructions"));
Jyotsna Verma653d8832013-03-27 11:14:24 +000056
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000057static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
58 cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
59
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000060static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
61 cl::Hidden, cl::desc("Generate \"extract\" instructions"));
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000062
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000063static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
64 cl::desc("Enable converting conditional transfers into MUX instructions"));
65
Krzysztof Parzyszek75874472015-07-14 19:30:21 +000066static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
67 cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
68 "predicate instructions"));
69
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +000070static cl::opt<bool> DisableHSDR("disable-hsdr", cl::init(false), cl::Hidden,
71 cl::desc("Disable splitting double registers"));
72
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000073static cl::opt<bool> EnableBitSimplify("hexagon-bit", cl::init(true),
74 cl::Hidden, cl::desc("Bit simplification"));
75
76static cl::opt<bool> EnableLoopResched("hexagon-loop-resched", cl::init(true),
77 cl::Hidden, cl::desc("Loop rescheduling"));
78
Tony Linthicum1213a7a2011-12-12 21:14:40 +000079/// HexagonTargetMachineModule - Note that this is used on hosts that
80/// cannot link in a library unless there are references into the
81/// library. In particular, it seems that it is not possible to get
82/// things to work on Win32 without this. Though it is unused, do not
83/// remove it.
84extern "C" int HexagonTargetMachineModule;
85int HexagonTargetMachineModule = 0;
86
87extern "C" void LLVMInitializeHexagonTarget() {
88 // Register the target.
89 RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000090}
91
Sergei Larin4d8986a2012-09-04 14:49:56 +000092static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
David Blaikie422b93d2014-04-21 20:32:32 +000093 return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
Sergei Larin4d8986a2012-09-04 14:49:56 +000094}
95
96static MachineSchedRegistry
97SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
98 createVLIWMachineSched);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000099
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000100namespace llvm {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000101 FunctionPass *createHexagonBitSimplify();
Krzysztof Parzyszek7b59ae22016-04-19 18:30:18 +0000102 FunctionPass *createHexagonBranchRelaxation();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000103 FunctionPass *createHexagonCallFrameInformation();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000104 FunctionPass *createHexagonCFGOptimizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000105 FunctionPass *createHexagonCommonGEP();
106 FunctionPass *createHexagonCopyToCombine();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000107 FunctionPass *createHexagonEarlyIfConversion();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000108 FunctionPass *createHexagonExpandCondsets();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000109 FunctionPass *createHexagonFixupHwLoops();
110 FunctionPass *createHexagonGenExtract();
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000111 FunctionPass *createHexagonGenInsert();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000112 FunctionPass *createHexagonGenMux();
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000113 FunctionPass *createHexagonGenPredicate();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000114 FunctionPass *createHexagonHardwareLoops();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000115 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
116 CodeGenOpt::Level OptLevel);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000117 FunctionPass *createHexagonLoopRescheduling();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000118 FunctionPass *createHexagonNewValueJump();
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +0000119 FunctionPass *createHexagonOptimizeSZextends();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000120 FunctionPass *createHexagonOptAddrMode();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000121 FunctionPass *createHexagonPacketizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000122 FunctionPass *createHexagonPeephole();
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000123 FunctionPass *createHexagonRDFOpt();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000124 FunctionPass *createHexagonSplitConst32AndConst64();
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000125 FunctionPass *createHexagonSplitDoubleRegs();
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000126 FunctionPass *createHexagonStoreWidening();
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000127} // end namespace llvm;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000128
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000129
Daniel Sanders3e5de882015-06-11 19:41:26 +0000130HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000131 StringRef CPU, StringRef FS,
Craig Topperb5454082012-03-17 09:24:09 +0000132 const TargetOptions &Options,
Eric Christopher0d0b3602014-06-27 00:13:43 +0000133 Reloc::Model RM, CodeModel::Model CM,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000134 CodeGenOpt::Level OL)
Krzysztof Parzyszeke5996432016-02-12 14:47:38 +0000135 // Specify the vector alignment explicitly. For v512x1, the calculated
136 // alignment would be 512*alignment(i1), which is 512 bytes, instead of
137 // the required minimum of 64 bytes.
138 : LLVMTargetMachine(T, "e-m:e-p:32:32:32-a:0-n16:32-"
139 "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
140 "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048",
141 TT, CPU, FS, Options, RM, CM, OL),
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000142 TLOF(make_unique<HexagonTargetObjectFile>()) {
143 initAsmInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000144}
145
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000146const HexagonSubtarget *
147HexagonTargetMachine::getSubtargetImpl(const Function &F) const {
148 AttributeSet FnAttrs = F.getAttributes();
149 Attribute CPUAttr =
150 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
151 Attribute FSAttr =
152 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
153
154 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
155 ? CPUAttr.getValueAsString().str()
156 : TargetCPU;
157 std::string FS = !FSAttr.hasAttribute(Attribute::None)
158 ? FSAttr.getValueAsString().str()
159 : TargetFS;
160
161 auto &I = SubtargetMap[CPU + FS];
162 if (!I) {
163 // This needs to be done before we create a new subtarget since any
164 // creation will depend on the TM and the code generation flags on the
165 // function that reside in TargetOptions.
166 resetTargetOptions(F);
167 I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
168 }
169 return I.get();
170}
171
172TargetIRAnalysis HexagonTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000173 return TargetIRAnalysis([this](const Function &F) {
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000174 return TargetTransformInfo(HexagonTTIImpl(this, F));
175 });
176}
177
178
Reid Kleckner357600e2014-11-20 23:37:18 +0000179HexagonTargetMachine::~HexagonTargetMachine() {}
180
Andrew Trickccb67362012-02-03 05:12:41 +0000181namespace {
182/// Hexagon Code Generator Pass Configuration Options.
183class HexagonPassConfig : public TargetPassConfig {
184public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000185 HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000186 : TargetPassConfig(TM, PM) {
187 bool NoOpt = (TM->getOptLevel() == CodeGenOpt::None);
188 if (!NoOpt) {
189 if (EnableExpandCondsets) {
190 Pass *Exp = createHexagonExpandCondsets();
191 insertPass(&RegisterCoalescerID, IdentifyingPassPtr(Exp));
192 }
193 }
194 }
Andrew Trickccb67362012-02-03 05:12:41 +0000195
196 HexagonTargetMachine &getHexagonTargetMachine() const {
197 return getTM<HexagonTargetMachine>();
198 }
199
Craig Topper906c2cd2014-04-29 07:58:16 +0000200 ScheduleDAGInstrs *
201 createMachineScheduler(MachineSchedContext *C) const override {
Andrew Trick978674b2013-09-20 05:14:41 +0000202 return createVLIWMachineSched(C);
203 }
204
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000205 void addIRPasses() override;
Craig Topper906c2cd2014-04-29 07:58:16 +0000206 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000207 void addPreRegAlloc() override;
208 void addPostRegAlloc() override;
209 void addPreSched2() override;
210 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000211};
212} // namespace
213
Andrew Trickf8ea1082012-02-04 02:56:59 +0000214TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
215 return new HexagonPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000216}
217
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000218void HexagonPassConfig::addIRPasses() {
219 TargetPassConfig::addIRPasses();
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000220 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +0000221
222 addPass(createAtomicExpandPass(TM));
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000223 if (!NoOpt) {
224 if (EnableCommGEP)
225 addPass(createHexagonCommonGEP());
226 // Replace certain combinations of shifts and ands with extracts.
227 if (EnableGenExtract)
228 addPass(createHexagonGenExtract());
229 }
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000230}
231
Andrew Trickccb67362012-02-03 05:12:41 +0000232bool HexagonPassConfig::addInstSelector() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000233 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000234 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000235
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +0000236 if (!NoOpt)
237 addPass(createHexagonOptimizeSZextends());
238
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000239 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000240
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000241 if (!NoOpt) {
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000242 // Create logical operations on predicate registers.
243 if (EnableGenPred)
244 addPass(createHexagonGenPredicate(), false);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000245 // Rotate loops to expose bit-simplification opportunities.
246 if (EnableLoopResched)
247 addPass(createHexagonLoopRescheduling(), false);
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000248 // Split double registers.
249 if (!DisableHSDR)
250 addPass(createHexagonSplitDoubleRegs());
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000251 // Bit simplification.
252 if (EnableBitSimplify)
253 addPass(createHexagonBitSimplify(), false);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000254 addPass(createHexagonPeephole());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000255 printAndVerify("After hexagon peephole pass");
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000256 if (EnableGenInsert)
257 addPass(createHexagonGenInsert(), false);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000258 if (EnableEarlyIf)
259 addPass(createHexagonEarlyIfConversion(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000260 }
Jyotsna Verma653d8832013-03-27 11:14:24 +0000261
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000262 return false;
263}
264
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000265void HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000266 if (getOptLevel() != CodeGenOpt::None) {
267 if (!DisableStoreWidening)
268 addPass(createHexagonStoreWidening(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000269 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000270 addPass(createHexagonHardwareLoops(), false);
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000271 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000272}
273
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000274void HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000275 if (getOptLevel() != CodeGenOpt::None) {
276 if (EnableRDFOpt)
277 addPass(createHexagonRDFOpt());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000278 if (!DisableHexagonCFGOpt)
Eric Christopher5c3376a2015-02-02 18:46:27 +0000279 addPass(createHexagonCFGOptimizer(), false);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000280 if (!DisableAModeOpt)
281 addPass(createHexagonOptAddrMode(), false);
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000282 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000283}
284
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000285void HexagonPassConfig::addPreSched2() {
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000286 addPass(createHexagonCopyToCombine(), false);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000287 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000288 addPass(&IfConverterID, false);
Eric Christopher01f875e2015-02-02 22:11:43 +0000289 addPass(createHexagonSplitConst32AndConst64());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000290}
291
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000292void HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000293 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000294
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000295 if (!NoOpt)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000296 addPass(createHexagonNewValueJump(), false);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000297
Krzysztof Parzyszek7b59ae22016-04-19 18:30:18 +0000298 addPass(createHexagonBranchRelaxation(), false);
299
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000300 // Create Packets.
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000301 if (!NoOpt) {
302 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000303 addPass(createHexagonFixupHwLoops(), false);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000304 // Generate MUX from pairs of conditional transfers.
305 if (EnableGenMux)
306 addPass(createHexagonGenMux(), false);
307
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000308 addPass(createHexagonPacketizer(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000309 }
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000310
311 // Add CFI instructions if necessary.
312 addPass(createHexagonCallFrameInformation(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000313}