blob: bf1213fd70917fc0acb843f9e49d89676e12dd25 [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"
Matthias Braun31d19d42016-05-10 03:21:59 +000021#include "llvm/CodeGen/TargetPassConfig.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000022#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Module.h"
Benjamin Kramerae87d7b2012-02-06 10:19:29 +000024#include "llvm/Support/CommandLine.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000025#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Transforms/Scalar.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000027
Tony Linthicum1213a7a2011-12-12 21:14:40 +000028using namespace llvm;
29
Krzysztof Parzyszek12798812016-01-12 19:09:01 +000030
31static cl::opt<bool> EnableRDFOpt("rdf-opt", cl::Hidden, cl::ZeroOrMore,
32 cl::init(true), cl::desc("Enable RDF-based optimizations"));
33
34static cl::opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000035 cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000036
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000037static cl::opt<bool> DisableAModeOpt("disable-hexagon-amodeopt",
38 cl::Hidden, cl::ZeroOrMore, cl::init(false),
39 cl::desc("Disable Hexagon Addressing Mode Optimization"));
40
Jyotsna Verma653d8832013-03-27 11:14:24 +000041static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000042 cl::Hidden, cl::ZeroOrMore, cl::init(false),
43 cl::desc("Disable Hexagon CFG Optimization"));
44
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +000045static cl::opt<bool> DisableStoreWidening("disable-store-widen",
46 cl::Hidden, cl::init(false), cl::desc("Disable store widening"));
47
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000048static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
49 cl::init(true), cl::Hidden, cl::ZeroOrMore,
50 cl::desc("Early expansion of MUX"));
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000051
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +000052static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
53 cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
54
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000055static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
56 cl::Hidden, cl::desc("Generate \"insert\" instructions"));
Jyotsna Verma653d8832013-03-27 11:14:24 +000057
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000058static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
59 cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
60
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000061static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
62 cl::Hidden, cl::desc("Generate \"extract\" instructions"));
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000063
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000064static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
65 cl::desc("Enable converting conditional transfers into MUX instructions"));
66
Krzysztof Parzyszek75874472015-07-14 19:30:21 +000067static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
68 cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
69 "predicate instructions"));
70
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +000071static cl::opt<bool> DisableHSDR("disable-hsdr", cl::init(false), cl::Hidden,
72 cl::desc("Disable splitting double registers"));
73
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000074static cl::opt<bool> EnableBitSimplify("hexagon-bit", cl::init(true),
75 cl::Hidden, cl::desc("Bit simplification"));
76
77static cl::opt<bool> EnableLoopResched("hexagon-loop-resched", cl::init(true),
78 cl::Hidden, cl::desc("Loop rescheduling"));
79
Krzysztof Parzyszekd5590052016-05-11 15:01:30 +000080static cl::opt<bool> HexagonNoOpt("hexagon-noopt", cl::init(false),
81 cl::Hidden, cl::desc("Disable backend optimizations"));
82
Tony Linthicum1213a7a2011-12-12 21:14:40 +000083/// HexagonTargetMachineModule - Note that this is used on hosts that
84/// cannot link in a library unless there are references into the
85/// library. In particular, it seems that it is not possible to get
86/// things to work on Win32 without this. Though it is unused, do not
87/// remove it.
88extern "C" int HexagonTargetMachineModule;
89int HexagonTargetMachineModule = 0;
90
91extern "C" void LLVMInitializeHexagonTarget() {
92 // Register the target.
93 RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000094}
95
Sergei Larin4d8986a2012-09-04 14:49:56 +000096static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
David Blaikie422b93d2014-04-21 20:32:32 +000097 return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
Sergei Larin4d8986a2012-09-04 14:49:56 +000098}
99
100static MachineSchedRegistry
101SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
102 createVLIWMachineSched);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000104namespace llvm {
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000105 FunctionPass *createHexagonBitSimplify();
Krzysztof Parzyszek7b59ae22016-04-19 18:30:18 +0000106 FunctionPass *createHexagonBranchRelaxation();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000107 FunctionPass *createHexagonCallFrameInformation();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000108 FunctionPass *createHexagonCFGOptimizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000109 FunctionPass *createHexagonCommonGEP();
110 FunctionPass *createHexagonCopyToCombine();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000111 FunctionPass *createHexagonEarlyIfConversion();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000112 FunctionPass *createHexagonExpandCondsets();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000113 FunctionPass *createHexagonFixupHwLoops();
114 FunctionPass *createHexagonGenExtract();
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000115 FunctionPass *createHexagonGenInsert();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000116 FunctionPass *createHexagonGenMux();
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000117 FunctionPass *createHexagonGenPredicate();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000118 FunctionPass *createHexagonHardwareLoops();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000119 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
120 CodeGenOpt::Level OptLevel);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000121 FunctionPass *createHexagonLoopRescheduling();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000122 FunctionPass *createHexagonNewValueJump();
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +0000123 FunctionPass *createHexagonOptimizeSZextends();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000124 FunctionPass *createHexagonOptAddrMode();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000125 FunctionPass *createHexagonPacketizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000126 FunctionPass *createHexagonPeephole();
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000127 FunctionPass *createHexagonRDFOpt();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000128 FunctionPass *createHexagonSplitConst32AndConst64();
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000129 FunctionPass *createHexagonSplitDoubleRegs();
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000130 FunctionPass *createHexagonStoreWidening();
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000131} // end namespace llvm;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000132
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000133
Daniel Sanders3e5de882015-06-11 19:41:26 +0000134HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000135 StringRef CPU, StringRef FS,
Craig Topperb5454082012-03-17 09:24:09 +0000136 const TargetOptions &Options,
Eric Christopher0d0b3602014-06-27 00:13:43 +0000137 Reloc::Model RM, CodeModel::Model CM,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000138 CodeGenOpt::Level OL)
Krzysztof Parzyszeke5996432016-02-12 14:47:38 +0000139 // Specify the vector alignment explicitly. For v512x1, the calculated
140 // alignment would be 512*alignment(i1), which is 512 bytes, instead of
141 // the required minimum of 64 bytes.
142 : LLVMTargetMachine(T, "e-m:e-p:32:32:32-a:0-n16:32-"
143 "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
144 "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048",
Krzysztof Parzyszekd5590052016-05-11 15:01:30 +0000145 TT, CPU, FS, Options, RM, CM, (HexagonNoOpt ? CodeGenOpt::None : OL)),
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000146 TLOF(make_unique<HexagonTargetObjectFile>()) {
147 initAsmInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000148}
149
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000150const HexagonSubtarget *
151HexagonTargetMachine::getSubtargetImpl(const Function &F) const {
152 AttributeSet FnAttrs = F.getAttributes();
153 Attribute CPUAttr =
154 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
155 Attribute FSAttr =
156 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
157
158 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
159 ? CPUAttr.getValueAsString().str()
160 : TargetCPU;
161 std::string FS = !FSAttr.hasAttribute(Attribute::None)
162 ? FSAttr.getValueAsString().str()
163 : TargetFS;
164
165 auto &I = SubtargetMap[CPU + FS];
166 if (!I) {
167 // This needs to be done before we create a new subtarget since any
168 // creation will depend on the TM and the code generation flags on the
169 // function that reside in TargetOptions.
170 resetTargetOptions(F);
171 I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
172 }
173 return I.get();
174}
175
176TargetIRAnalysis HexagonTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000177 return TargetIRAnalysis([this](const Function &F) {
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000178 return TargetTransformInfo(HexagonTTIImpl(this, F));
179 });
180}
181
182
Reid Kleckner357600e2014-11-20 23:37:18 +0000183HexagonTargetMachine::~HexagonTargetMachine() {}
184
Andrew Trickccb67362012-02-03 05:12:41 +0000185namespace {
186/// Hexagon Code Generator Pass Configuration Options.
187class HexagonPassConfig : public TargetPassConfig {
188public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000189 HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000190 : TargetPassConfig(TM, PM) {
191 bool NoOpt = (TM->getOptLevel() == CodeGenOpt::None);
192 if (!NoOpt) {
193 if (EnableExpandCondsets) {
194 Pass *Exp = createHexagonExpandCondsets();
195 insertPass(&RegisterCoalescerID, IdentifyingPassPtr(Exp));
196 }
197 }
198 }
Andrew Trickccb67362012-02-03 05:12:41 +0000199
200 HexagonTargetMachine &getHexagonTargetMachine() const {
201 return getTM<HexagonTargetMachine>();
202 }
203
Craig Topper906c2cd2014-04-29 07:58:16 +0000204 ScheduleDAGInstrs *
205 createMachineScheduler(MachineSchedContext *C) const override {
Andrew Trick978674b2013-09-20 05:14:41 +0000206 return createVLIWMachineSched(C);
207 }
208
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000209 void addIRPasses() override;
Craig Topper906c2cd2014-04-29 07:58:16 +0000210 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000211 void addPreRegAlloc() override;
212 void addPostRegAlloc() override;
213 void addPreSched2() override;
214 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000215};
216} // namespace
217
Andrew Trickf8ea1082012-02-04 02:56:59 +0000218TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
219 return new HexagonPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000220}
221
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000222void HexagonPassConfig::addIRPasses() {
223 TargetPassConfig::addIRPasses();
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000224 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +0000225
226 addPass(createAtomicExpandPass(TM));
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000227 if (!NoOpt) {
228 if (EnableCommGEP)
229 addPass(createHexagonCommonGEP());
230 // Replace certain combinations of shifts and ands with extracts.
231 if (EnableGenExtract)
232 addPass(createHexagonGenExtract());
233 }
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000234}
235
Andrew Trickccb67362012-02-03 05:12:41 +0000236bool HexagonPassConfig::addInstSelector() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000237 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000238 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000239
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +0000240 if (!NoOpt)
241 addPass(createHexagonOptimizeSZextends());
242
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000243 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000244
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000245 if (!NoOpt) {
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000246 // Create logical operations on predicate registers.
247 if (EnableGenPred)
248 addPass(createHexagonGenPredicate(), false);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000249 // Rotate loops to expose bit-simplification opportunities.
250 if (EnableLoopResched)
251 addPass(createHexagonLoopRescheduling(), false);
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000252 // Split double registers.
253 if (!DisableHSDR)
254 addPass(createHexagonSplitDoubleRegs());
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000255 // Bit simplification.
256 if (EnableBitSimplify)
257 addPass(createHexagonBitSimplify(), false);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000258 addPass(createHexagonPeephole());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000259 printAndVerify("After hexagon peephole pass");
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000260 if (EnableGenInsert)
261 addPass(createHexagonGenInsert(), false);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000262 if (EnableEarlyIf)
263 addPass(createHexagonEarlyIfConversion(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000264 }
Jyotsna Verma653d8832013-03-27 11:14:24 +0000265
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000266 return false;
267}
268
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000269void HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000270 if (getOptLevel() != CodeGenOpt::None) {
271 if (!DisableStoreWidening)
272 addPass(createHexagonStoreWidening(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000273 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000274 addPass(createHexagonHardwareLoops(), false);
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000275 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000276}
277
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000278void HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000279 if (getOptLevel() != CodeGenOpt::None) {
280 if (EnableRDFOpt)
281 addPass(createHexagonRDFOpt());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000282 if (!DisableHexagonCFGOpt)
Eric Christopher5c3376a2015-02-02 18:46:27 +0000283 addPass(createHexagonCFGOptimizer(), false);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000284 if (!DisableAModeOpt)
285 addPass(createHexagonOptAddrMode(), false);
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000286 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000287}
288
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000289void HexagonPassConfig::addPreSched2() {
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000290 addPass(createHexagonCopyToCombine(), false);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000291 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000292 addPass(&IfConverterID, false);
Eric Christopher01f875e2015-02-02 22:11:43 +0000293 addPass(createHexagonSplitConst32AndConst64());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000294}
295
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000296void HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000297 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000298
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000299 if (!NoOpt)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000300 addPass(createHexagonNewValueJump(), false);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000301
Krzysztof Parzyszek7b59ae22016-04-19 18:30:18 +0000302 addPass(createHexagonBranchRelaxation(), false);
303
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000304 // Create Packets.
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000305 if (!NoOpt) {
306 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000307 addPass(createHexagonFixupHwLoops(), false);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000308 // Generate MUX from pairs of conditional transfers.
309 if (EnableGenMux)
310 addPass(createHexagonGenMux(), false);
311
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000312 addPass(createHexagonPacketizer(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000313 }
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000314
315 // Add CFI instructions if necessary.
316 addPass(createHexagonCallFrameInformation(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000317}