blob: dd9492eb7fb733680991ea574ef96fe39211787e [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
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +000036static cl::opt<bool> DisableStoreWidening("disable-store-widen",
37 cl::Hidden, cl::init(false), cl::desc("Disable store widening"));
38
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000039static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
40 cl::init(true), cl::Hidden, cl::ZeroOrMore,
41 cl::desc("Early expansion of MUX"));
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000042
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +000043static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
44 cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
45
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000046static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
47 cl::Hidden, cl::desc("Generate \"insert\" instructions"));
Jyotsna Verma653d8832013-03-27 11:14:24 +000048
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000049static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
50 cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
51
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000052static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
53 cl::Hidden, cl::desc("Generate \"extract\" instructions"));
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000054
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000055static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
56 cl::desc("Enable converting conditional transfers into MUX instructions"));
57
Krzysztof Parzyszek75874472015-07-14 19:30:21 +000058static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
59 cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
60 "predicate instructions"));
61
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +000062static cl::opt<bool> DisableHSDR("disable-hsdr", cl::init(false), cl::Hidden,
63 cl::desc("Disable splitting double registers"));
64
Tony Linthicum1213a7a2011-12-12 21:14:40 +000065/// HexagonTargetMachineModule - Note that this is used on hosts that
66/// cannot link in a library unless there are references into the
67/// library. In particular, it seems that it is not possible to get
68/// things to work on Win32 without this. Though it is unused, do not
69/// remove it.
70extern "C" int HexagonTargetMachineModule;
71int HexagonTargetMachineModule = 0;
72
73extern "C" void LLVMInitializeHexagonTarget() {
74 // Register the target.
75 RegisterTargetMachine<HexagonTargetMachine> X(TheHexagonTarget);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000076}
77
Sergei Larin4d8986a2012-09-04 14:49:56 +000078static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
David Blaikie422b93d2014-04-21 20:32:32 +000079 return new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
Sergei Larin4d8986a2012-09-04 14:49:56 +000080}
81
82static MachineSchedRegistry
83SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
84 createVLIWMachineSched);
Tony Linthicum1213a7a2011-12-12 21:14:40 +000085
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000086namespace llvm {
Colin LeMahieu56efafc2015-06-15 19:05:35 +000087 FunctionPass *createHexagonCFGOptimizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000088 FunctionPass *createHexagonCommonGEP();
89 FunctionPass *createHexagonCopyToCombine();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +000090 FunctionPass *createHexagonEarlyIfConversion();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000091 FunctionPass *createHexagonExpandCondsets();
Colin LeMahieu56efafc2015-06-15 19:05:35 +000092 FunctionPass *createHexagonExpandPredSpillCode();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000093 FunctionPass *createHexagonFixupHwLoops();
94 FunctionPass *createHexagonGenExtract();
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000095 FunctionPass *createHexagonGenInsert();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000096 FunctionPass *createHexagonGenMux();
Krzysztof Parzyszek75874472015-07-14 19:30:21 +000097 FunctionPass *createHexagonGenPredicate();
Colin LeMahieu56efafc2015-06-15 19:05:35 +000098 FunctionPass *createHexagonHardwareLoops();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000099 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
100 CodeGenOpt::Level OptLevel);
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000101 FunctionPass *createHexagonNewValueJump();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000102 FunctionPass *createHexagonPacketizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000103 FunctionPass *createHexagonPeephole();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000104 FunctionPass *createHexagonSplitConst32AndConst64();
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000105 FunctionPass *createHexagonSplitDoubleRegs();
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000106 FunctionPass *createHexagonStoreWidening();
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000107} // end namespace llvm;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000108
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000109/// HexagonTargetMachine ctor - Create an ILP32 architecture model.
110///
111
112/// Hexagon_TODO: Do I need an aggregate alignment?
113///
Daniel Sanders3e5de882015-06-11 19:41:26 +0000114HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000115 StringRef CPU, StringRef FS,
Craig Topperb5454082012-03-17 09:24:09 +0000116 const TargetOptions &Options,
Eric Christopher0d0b3602014-06-27 00:13:43 +0000117 Reloc::Model RM, CodeModel::Model CM,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000118 CodeGenOpt::Level OL)
Mehdi Amini93e1ea12015-03-12 00:07:24 +0000119 : LLVMTargetMachine(T, "e-m:e-p:32:32-i1:32-i64:64-a:0-n32", TT, CPU, FS,
120 Options, RM, CM, OL),
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000121 TLOF(make_unique<HexagonTargetObjectFile>()) {
122 initAsmInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000123}
124
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000125const HexagonSubtarget *
126HexagonTargetMachine::getSubtargetImpl(const Function &F) const {
127 AttributeSet FnAttrs = F.getAttributes();
128 Attribute CPUAttr =
129 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-cpu");
130 Attribute FSAttr =
131 FnAttrs.getAttribute(AttributeSet::FunctionIndex, "target-features");
132
133 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
134 ? CPUAttr.getValueAsString().str()
135 : TargetCPU;
136 std::string FS = !FSAttr.hasAttribute(Attribute::None)
137 ? FSAttr.getValueAsString().str()
138 : TargetFS;
139
140 auto &I = SubtargetMap[CPU + FS];
141 if (!I) {
142 // This needs to be done before we create a new subtarget since any
143 // creation will depend on the TM and the code generation flags on the
144 // function that reside in TargetOptions.
145 resetTargetOptions(F);
146 I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
147 }
148 return I.get();
149}
150
151TargetIRAnalysis HexagonTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +0000152 return TargetIRAnalysis([this](const Function &F) {
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000153 return TargetTransformInfo(HexagonTTIImpl(this, F));
154 });
155}
156
157
Reid Kleckner357600e2014-11-20 23:37:18 +0000158HexagonTargetMachine::~HexagonTargetMachine() {}
159
Andrew Trickccb67362012-02-03 05:12:41 +0000160namespace {
161/// Hexagon Code Generator Pass Configuration Options.
162class HexagonPassConfig : public TargetPassConfig {
163public:
Andrew Trickf8ea1082012-02-04 02:56:59 +0000164 HexagonPassConfig(HexagonTargetMachine *TM, PassManagerBase &PM)
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000165 : TargetPassConfig(TM, PM) {
166 bool NoOpt = (TM->getOptLevel() == CodeGenOpt::None);
167 if (!NoOpt) {
168 if (EnableExpandCondsets) {
169 Pass *Exp = createHexagonExpandCondsets();
170 insertPass(&RegisterCoalescerID, IdentifyingPassPtr(Exp));
171 }
172 }
173 }
Andrew Trickccb67362012-02-03 05:12:41 +0000174
175 HexagonTargetMachine &getHexagonTargetMachine() const {
176 return getTM<HexagonTargetMachine>();
177 }
178
Craig Topper906c2cd2014-04-29 07:58:16 +0000179 ScheduleDAGInstrs *
180 createMachineScheduler(MachineSchedContext *C) const override {
Andrew Trick978674b2013-09-20 05:14:41 +0000181 return createVLIWMachineSched(C);
182 }
183
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000184 void addIRPasses() override;
Craig Topper906c2cd2014-04-29 07:58:16 +0000185 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000186 void addPreRegAlloc() override;
187 void addPostRegAlloc() override;
188 void addPreSched2() override;
189 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000190};
191} // namespace
192
Andrew Trickf8ea1082012-02-04 02:56:59 +0000193TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
194 return new HexagonPassConfig(this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000195}
196
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000197void HexagonPassConfig::addIRPasses() {
198 TargetPassConfig::addIRPasses();
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000199 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +0000200
201 addPass(createAtomicExpandPass(TM));
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000202 if (!NoOpt) {
203 if (EnableCommGEP)
204 addPass(createHexagonCommonGEP());
205 // Replace certain combinations of shifts and ands with extracts.
206 if (EnableGenExtract)
207 addPass(createHexagonGenExtract());
208 }
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000209}
210
Andrew Trickccb67362012-02-03 05:12:41 +0000211bool HexagonPassConfig::addInstSelector() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000212 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000213 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000214
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000215 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000216
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000217 if (!NoOpt) {
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000218 // Create logical operations on predicate registers.
219 if (EnableGenPred)
220 addPass(createHexagonGenPredicate(), false);
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000221 // Split double registers.
222 if (!DisableHSDR)
223 addPass(createHexagonSplitDoubleRegs());
Jyotsna Verma653d8832013-03-27 11:14:24 +0000224 addPass(createHexagonPeephole());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000225 printAndVerify("After hexagon peephole pass");
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000226 if (EnableGenInsert)
227 addPass(createHexagonGenInsert(), false);
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000228 if (EnableEarlyIf)
229 addPass(createHexagonEarlyIfConversion(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000230 }
Jyotsna Verma653d8832013-03-27 11:14:24 +0000231
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000232 return false;
233}
234
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000235void HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000236 if (getOptLevel() != CodeGenOpt::None) {
237 if (!DisableStoreWidening)
238 addPass(createHexagonStoreWidening(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000239 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000240 addPass(createHexagonHardwareLoops(), false);
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000241 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000242}
243
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000244void HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000245 if (getOptLevel() != CodeGenOpt::None)
246 if (!DisableHexagonCFGOpt)
Eric Christopher5c3376a2015-02-02 18:46:27 +0000247 addPass(createHexagonCFGOptimizer(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000248}
249
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000250void HexagonPassConfig::addPreSched2() {
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000251 addPass(createHexagonCopyToCombine(), false);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000252 if (getOptLevel() != CodeGenOpt::None)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000253 addPass(&IfConverterID, false);
Eric Christopher01f875e2015-02-02 22:11:43 +0000254 addPass(createHexagonSplitConst32AndConst64());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000255}
256
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000257void HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000258 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000259
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000260 if (!NoOpt)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000261 addPass(createHexagonNewValueJump(), false);
Sirish Pande4bd20c52012-05-12 05:10:30 +0000262
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000263 // Expand Spill code for predicate registers.
Eric Christopher6ff7ed62015-02-02 18:46:31 +0000264 addPass(createHexagonExpandPredSpillCode(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000265
Sirish Pandef8e5e3c2012-05-03 21:52:53 +0000266 // Create Packets.
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000267 if (!NoOpt) {
268 if (!DisableHardwareLoops)
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000269 addPass(createHexagonFixupHwLoops(), false);
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000270 // Generate MUX from pairs of conditional transfers.
271 if (EnableGenMux)
272 addPass(createHexagonGenMux(), false);
273
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000274 addPass(createHexagonPacketizer(), false);
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000275 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000276}