blob: 8851a0819e92b77db29f20552d957bb0430e76ec [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tony Linthicum1213a7a2011-12-12 21:14:40 +00006//
7//===----------------------------------------------------------------------===//
8//
Jia Liub22310f2012-02-18 12:03:15 +00009// Implements the info about Hexagon target spec.
Tony Linthicum1213a7a2011-12-12 21:14:40 +000010//
11//===----------------------------------------------------------------------===//
12
Tony Linthicum1213a7a2011-12-12 21:14:40 +000013#include "HexagonTargetMachine.h"
14#include "Hexagon.h"
15#include "HexagonISelLowering.h"
Sergei Larin4d8986a2012-09-04 14:49:56 +000016#include "HexagonMachineScheduler.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000017#include "HexagonTargetObjectFile.h"
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +000018#include "HexagonTargetTransformInfo.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000019#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000020#include "llvm/CodeGen/TargetPassConfig.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"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000025#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +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 Parzyszek7c9c0582017-10-13 19:02:59 +000030static cl::opt<bool> EnableCExtOpt("hexagon-cext", cl::Hidden, cl::ZeroOrMore,
31 cl::init(true), cl::desc("Enable Hexagon constant-extender optimization"));
32
Krzysztof Parzyszek12798812016-01-12 19:09:01 +000033static cl::opt<bool> EnableRDFOpt("rdf-opt", cl::Hidden, cl::ZeroOrMore,
34 cl::init(true), cl::desc("Enable RDF-based optimizations"));
35
36static cl::opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000037 cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000038
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000039static cl::opt<bool> DisableAModeOpt("disable-hexagon-amodeopt",
40 cl::Hidden, cl::ZeroOrMore, cl::init(false),
41 cl::desc("Disable Hexagon Addressing Mode Optimization"));
42
Jyotsna Verma653d8832013-03-27 11:14:24 +000043static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000044 cl::Hidden, cl::ZeroOrMore, cl::init(false),
45 cl::desc("Disable Hexagon CFG Optimization"));
46
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +000047static cl::opt<bool> DisableHCP("disable-hcp", cl::init(false), cl::Hidden,
48 cl::ZeroOrMore, cl::desc("Disable Hexagon constant propagation"));
49
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +000050static cl::opt<bool> DisableStoreWidening("disable-store-widen",
51 cl::Hidden, cl::init(false), cl::desc("Disable store widening"));
52
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000053static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
54 cl::init(true), cl::Hidden, cl::ZeroOrMore,
55 cl::desc("Early expansion of MUX"));
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000056
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +000057static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
58 cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
59
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000060static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
61 cl::Hidden, cl::desc("Generate \"insert\" instructions"));
Jyotsna Verma653d8832013-03-27 11:14:24 +000062
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000063static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
64 cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
65
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000066static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
67 cl::Hidden, cl::desc("Generate \"extract\" instructions"));
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000068
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000069static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
70 cl::desc("Enable converting conditional transfers into MUX instructions"));
71
Krzysztof Parzyszek75874472015-07-14 19:30:21 +000072static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
73 cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
74 "predicate instructions"));
75
Krzysztof Parzyszekd3d0a4b2016-07-22 14:22:43 +000076static cl::opt<bool> EnableLoopPrefetch("hexagon-loop-prefetch",
77 cl::init(false), cl::Hidden, cl::ZeroOrMore,
78 cl::desc("Enable loop data prefetch on Hexagon"));
79
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +000080static cl::opt<bool> DisableHSDR("disable-hsdr", cl::init(false), cl::Hidden,
81 cl::desc("Disable splitting double registers"));
82
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000083static cl::opt<bool> EnableBitSimplify("hexagon-bit", cl::init(true),
84 cl::Hidden, cl::desc("Bit simplification"));
85
86static cl::opt<bool> EnableLoopResched("hexagon-loop-resched", cl::init(true),
87 cl::Hidden, cl::desc("Loop rescheduling"));
88
Krzysztof Parzyszekd5590052016-05-11 15:01:30 +000089static cl::opt<bool> HexagonNoOpt("hexagon-noopt", cl::init(false),
90 cl::Hidden, cl::desc("Disable backend optimizations"));
91
Ron Lieberman8123b962016-08-01 19:36:39 +000092static cl::opt<bool> EnableVectorPrint("enable-hexagon-vector-print",
93 cl::Hidden, cl::ZeroOrMore, cl::init(false),
94 cl::desc("Enable Hexagon Vector print instr pass"));
95
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +000096static cl::opt<bool> EnableVExtractOpt("hexagon-opt-vextract", cl::Hidden,
97 cl::ZeroOrMore, cl::init(true), cl::desc("Enable vextract optimization"));
98
Krzysztof Parzyszekd91a9e22018-08-02 22:17:53 +000099static cl::opt<bool> EnableInitialCFGCleanup("hexagon-initial-cfg-cleanup",
100 cl::Hidden, cl::ZeroOrMore, cl::init(true),
101 cl::desc("Simplify the CFG after atomic expansion pass"));
102
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000103/// HexagonTargetMachineModule - Note that this is used on hosts that
104/// cannot link in a library unless there are references into the
105/// library. In particular, it seems that it is not possible to get
106/// things to work on Win32 without this. Though it is unused, do not
107/// remove it.
108extern "C" int HexagonTargetMachineModule;
109int HexagonTargetMachineModule = 0;
110
Sergei Larin4d8986a2012-09-04 14:49:56 +0000111static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
Krzysztof Parzyszek697297a2017-08-28 15:52:54 +0000112 ScheduleDAGMILive *DAG =
113 new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000114 DAG->addMutation(make_unique<HexagonSubtarget::UsrOverflowMutation>());
115 DAG->addMutation(make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
116 DAG->addMutation(make_unique<HexagonSubtarget::CallMutation>());
Krzysztof Parzyszek697297a2017-08-28 15:52:54 +0000117 DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI));
118 return DAG;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000119}
120
121static MachineSchedRegistry
122SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
123 createVLIWMachineSched);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000124
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000125namespace llvm {
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000126 extern char &HexagonExpandCondsetsID;
Krzysztof Parzyszekb404fae2018-02-20 14:29:43 +0000127 void initializeHexagonBitSimplifyPass(PassRegistry&);
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +0000128 void initializeHexagonConstExtendersPass(PassRegistry&);
Krzysztof Parzyszek96690ce2018-02-23 20:33:26 +0000129 void initializeHexagonConstPropagationPass(PassRegistry&);
Krzysztof Parzyszek1966fd72017-08-09 21:22:05 +0000130 void initializeHexagonEarlyIfConversionPass(PassRegistry&);
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000131 void initializeHexagonExpandCondsetsPass(PassRegistry&);
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000132 void initializeHexagonGenMuxPass(PassRegistry&);
Krzysztof Parzyszek3818aea2017-10-20 16:56:33 +0000133 void initializeHexagonHardwareLoopsPass(PassRegistry&);
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000134 void initializeHexagonLoopIdiomRecognizePass(PassRegistry&);
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000135 void initializeHexagonVectorLoopCarriedReusePass(PassRegistry&);
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000136 void initializeHexagonNewValueJumpPass(PassRegistry&);
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000137 void initializeHexagonOptAddrModePass(PassRegistry&);
138 void initializeHexagonPacketizerPass(PassRegistry&);
Krzysztof Parzyszekbef1c562017-10-30 14:11:52 +0000139 void initializeHexagonRDFOptPass(PassRegistry&);
Krzysztof Parzyszekeffcc2f2018-05-04 15:04:48 +0000140 void initializeHexagonSplitDoubleRegsPass(PassRegistry&);
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +0000141 void initializeHexagonVExtractPass(PassRegistry&);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000142 Pass *createHexagonLoopIdiomPass();
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000143 Pass *createHexagonVectorLoopCarriedReusePass();
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000144
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000145 FunctionPass *createHexagonBitSimplify();
Krzysztof Parzyszek7b59ae22016-04-19 18:30:18 +0000146 FunctionPass *createHexagonBranchRelaxation();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000147 FunctionPass *createHexagonCallFrameInformation();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000148 FunctionPass *createHexagonCFGOptimizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000149 FunctionPass *createHexagonCommonGEP();
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +0000150 FunctionPass *createHexagonConstExtenders();
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +0000151 FunctionPass *createHexagonConstPropagationPass();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000152 FunctionPass *createHexagonCopyToCombine();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000153 FunctionPass *createHexagonEarlyIfConversion();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000154 FunctionPass *createHexagonFixupHwLoops();
155 FunctionPass *createHexagonGenExtract();
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000156 FunctionPass *createHexagonGenInsert();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000157 FunctionPass *createHexagonGenMux();
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000158 FunctionPass *createHexagonGenPredicate();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000159 FunctionPass *createHexagonHardwareLoops();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000160 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
161 CodeGenOpt::Level OptLevel);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000162 FunctionPass *createHexagonLoopRescheduling();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000163 FunctionPass *createHexagonNewValueJump();
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +0000164 FunctionPass *createHexagonOptimizeSZextends();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000165 FunctionPass *createHexagonOptAddrMode();
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +0000166 FunctionPass *createHexagonPacketizer(bool Minimal);
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000167 FunctionPass *createHexagonPeephole();
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000168 FunctionPass *createHexagonRDFOpt();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000169 FunctionPass *createHexagonSplitConst32AndConst64();
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000170 FunctionPass *createHexagonSplitDoubleRegs();
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000171 FunctionPass *createHexagonStoreWidening();
Ron Lieberman8123b962016-08-01 19:36:39 +0000172 FunctionPass *createHexagonVectorPrint();
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +0000173 FunctionPass *createHexagonVExtract();
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000174} // end namespace llvm;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000175
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000176static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
177 if (!RM.hasValue())
178 return Reloc::Static;
179 return *RM;
180}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000181
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000182extern "C" void LLVMInitializeHexagonTarget() {
183 // Register the target.
184 RegisterTargetMachine<HexagonTargetMachine> X(getTheHexagonTarget());
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000185
186 PassRegistry &PR = *PassRegistry::getPassRegistry();
Krzysztof Parzyszekb404fae2018-02-20 14:29:43 +0000187 initializeHexagonBitSimplifyPass(PR);
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +0000188 initializeHexagonConstExtendersPass(PR);
Krzysztof Parzyszek96690ce2018-02-23 20:33:26 +0000189 initializeHexagonConstPropagationPass(PR);
Krzysztof Parzyszek1966fd72017-08-09 21:22:05 +0000190 initializeHexagonEarlyIfConversionPass(PR);
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000191 initializeHexagonGenMuxPass(PR);
Krzysztof Parzyszek3818aea2017-10-20 16:56:33 +0000192 initializeHexagonHardwareLoopsPass(PR);
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000193 initializeHexagonLoopIdiomRecognizePass(PR);
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000194 initializeHexagonVectorLoopCarriedReusePass(PR);
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000195 initializeHexagonNewValueJumpPass(PR);
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000196 initializeHexagonOptAddrModePass(PR);
197 initializeHexagonPacketizerPass(PR);
Krzysztof Parzyszekbef1c562017-10-30 14:11:52 +0000198 initializeHexagonRDFOptPass(PR);
Krzysztof Parzyszekeffcc2f2018-05-04 15:04:48 +0000199 initializeHexagonSplitDoubleRegsPass(PR);
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +0000200 initializeHexagonVExtractPass(PR);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000201}
202
Daniel Sanders3e5de882015-06-11 19:41:26 +0000203HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000204 StringRef CPU, StringRef FS,
Craig Topperb5454082012-03-17 09:24:09 +0000205 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000206 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000207 Optional<CodeModel::Model> CM,
208 CodeGenOpt::Level OL, bool JIT)
Krzysztof Parzyszeke5996432016-02-12 14:47:38 +0000209 // Specify the vector alignment explicitly. For v512x1, the calculated
210 // alignment would be 512*alignment(i1), which is 512 bytes, instead of
211 // the required minimum of 64 bytes.
Matthias Braunbb8507e2017-10-12 22:57:28 +0000212 : LLVMTargetMachine(
Rafael Espindola79e238a2017-08-03 02:16:21 +0000213 T,
214 "e-m:e-p:32:32:32-a:0-n16:32-"
215 "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
216 "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048",
217 TT, CPU, FS, Options, getEffectiveRelocModel(RM),
David Greenca29c272018-12-07 12:10:23 +0000218 getEffectiveCodeModel(CM, CodeModel::Small),
219 (HexagonNoOpt ? CodeGenOpt::None : OL)),
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000220 TLOF(make_unique<HexagonTargetObjectFile>()) {
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000221 initializeHexagonExpandCondsetsPass(*PassRegistry::getPassRegistry());
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000222 initAsmInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000223}
224
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000225const HexagonSubtarget *
226HexagonTargetMachine::getSubtargetImpl(const Function &F) const {
Reid Klecknerb5180542017-03-21 16:57:19 +0000227 AttributeList FnAttrs = F.getAttributes();
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000228 Attribute CPUAttr =
Reid Klecknerb5180542017-03-21 16:57:19 +0000229 FnAttrs.getAttribute(AttributeList::FunctionIndex, "target-cpu");
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000230 Attribute FSAttr =
Reid Klecknerb5180542017-03-21 16:57:19 +0000231 FnAttrs.getAttribute(AttributeList::FunctionIndex, "target-features");
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000232
233 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
234 ? CPUAttr.getValueAsString().str()
235 : TargetCPU;
236 std::string FS = !FSAttr.hasAttribute(Attribute::None)
237 ? FSAttr.getValueAsString().str()
238 : TargetFS;
239
240 auto &I = SubtargetMap[CPU + FS];
241 if (!I) {
242 // This needs to be done before we create a new subtarget since any
243 // creation will depend on the TM and the code generation flags on the
244 // function that reside in TargetOptions.
245 resetTargetOptions(F);
246 I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
247 }
248 return I.get();
249}
250
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000251void HexagonTargetMachine::adjustPassManager(PassManagerBuilder &PMB) {
252 PMB.addExtension(
253 PassManagerBuilder::EP_LateLoopOptimizations,
254 [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
255 PM.add(createHexagonLoopIdiomPass());
256 });
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000257 PMB.addExtension(
258 PassManagerBuilder::EP_LoopOptimizerEnd,
259 [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
260 PM.add(createHexagonVectorLoopCarriedReusePass());
261 });
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000262}
263
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000264TargetTransformInfo
265HexagonTargetMachine::getTargetTransformInfo(const Function &F) {
266 return TargetTransformInfo(HexagonTTIImpl(this, F));
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000267}
268
269
Reid Kleckner357600e2014-11-20 23:37:18 +0000270HexagonTargetMachine::~HexagonTargetMachine() {}
271
Andrew Trickccb67362012-02-03 05:12:41 +0000272namespace {
273/// Hexagon Code Generator Pass Configuration Options.
274class HexagonPassConfig : public TargetPassConfig {
275public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000276 HexagonPassConfig(HexagonTargetMachine &TM, PassManagerBase &PM)
Krzysztof Parzyszekd0f8e1c2016-05-27 20:48:39 +0000277 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000278
279 HexagonTargetMachine &getHexagonTargetMachine() const {
280 return getTM<HexagonTargetMachine>();
281 }
282
Craig Topper906c2cd2014-04-29 07:58:16 +0000283 ScheduleDAGInstrs *
284 createMachineScheduler(MachineSchedContext *C) const override {
Andrew Trick978674b2013-09-20 05:14:41 +0000285 return createVLIWMachineSched(C);
286 }
287
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000288 void addIRPasses() override;
Craig Topper906c2cd2014-04-29 07:58:16 +0000289 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000290 void addPreRegAlloc() override;
291 void addPostRegAlloc() override;
292 void addPreSched2() override;
293 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000294};
295} // namespace
296
Andrew Trickf8ea1082012-02-04 02:56:59 +0000297TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000298 return new HexagonPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000299}
300
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000301void HexagonPassConfig::addIRPasses() {
302 TargetPassConfig::addIRPasses();
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000303 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +0000304
Krzysztof Parzyszekcf3ad582018-01-24 17:48:11 +0000305 if (!NoOpt) {
306 addPass(createConstantPropagationPass());
307 addPass(createDeadCodeEliminationPass());
308 }
309
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000310 addPass(createAtomicExpandPass());
Krzysztof Parzyszekd91a9e22018-08-02 22:17:53 +0000311
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000312 if (!NoOpt) {
Krzysztof Parzyszekd91a9e22018-08-02 22:17:53 +0000313 if (EnableInitialCFGCleanup)
314 addPass(createCFGSimplificationPass(1, true, true, false, true));
Krzysztof Parzyszekd3d0a4b2016-07-22 14:22:43 +0000315 if (EnableLoopPrefetch)
316 addPass(createLoopDataPrefetchPass());
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000317 if (EnableCommGEP)
318 addPass(createHexagonCommonGEP());
319 // Replace certain combinations of shifts and ands with extracts.
320 if (EnableGenExtract)
321 addPass(createHexagonGenExtract());
322 }
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000323}
324
Andrew Trickccb67362012-02-03 05:12:41 +0000325bool HexagonPassConfig::addInstSelector() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000326 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000327 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000328
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +0000329 if (!NoOpt)
330 addPass(createHexagonOptimizeSZextends());
331
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000332 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000333
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000334 if (!NoOpt) {
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +0000335 if (EnableVExtractOpt)
336 addPass(createHexagonVExtract());
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000337 // Create logical operations on predicate registers.
338 if (EnableGenPred)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000339 addPass(createHexagonGenPredicate());
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000340 // Rotate loops to expose bit-simplification opportunities.
341 if (EnableLoopResched)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000342 addPass(createHexagonLoopRescheduling());
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000343 // Split double registers.
344 if (!DisableHSDR)
345 addPass(createHexagonSplitDoubleRegs());
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000346 // Bit simplification.
347 if (EnableBitSimplify)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000348 addPass(createHexagonBitSimplify());
Jyotsna Verma653d8832013-03-27 11:14:24 +0000349 addPass(createHexagonPeephole());
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +0000350 // Constant propagation.
351 if (!DisableHCP) {
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000352 addPass(createHexagonConstPropagationPass());
353 addPass(&UnreachableMachineBlockElimID);
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +0000354 }
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000355 if (EnableGenInsert)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000356 addPass(createHexagonGenInsert());
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000357 if (EnableEarlyIf)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000358 addPass(createHexagonEarlyIfConversion());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000359 }
Jyotsna Verma653d8832013-03-27 11:14:24 +0000360
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000361 return false;
362}
363
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000364void HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000365 if (getOptLevel() != CodeGenOpt::None) {
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +0000366 if (EnableCExtOpt)
367 addPass(createHexagonConstExtenders());
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000368 if (EnableExpandCondsets)
369 insertPass(&RegisterCoalescerID, &HexagonExpandCondsetsID);
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000370 if (!DisableStoreWidening)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000371 addPass(createHexagonStoreWidening());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000372 if (!DisableHardwareLoops)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000373 addPass(createHexagonHardwareLoops());
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000374 }
Brendon Cahoon254f8892016-07-29 16:44:44 +0000375 if (TM->getOptLevel() >= CodeGenOpt::Default)
376 addPass(&MachinePipelinerID);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000377}
378
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000379void HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000380 if (getOptLevel() != CodeGenOpt::None) {
381 if (EnableRDFOpt)
382 addPass(createHexagonRDFOpt());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000383 if (!DisableHexagonCFGOpt)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000384 addPass(createHexagonCFGOptimizer());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000385 if (!DisableAModeOpt)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000386 addPass(createHexagonOptAddrMode());
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000387 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000388}
389
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000390void HexagonPassConfig::addPreSched2() {
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000391 addPass(createHexagonCopyToCombine());
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000392 if (getOptLevel() != CodeGenOpt::None)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000393 addPass(&IfConverterID);
Eric Christopher01f875e2015-02-02 22:11:43 +0000394 addPass(createHexagonSplitConst32AndConst64());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000395}
396
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000397void HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000398 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000399
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000400 if (!NoOpt)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000401 addPass(createHexagonNewValueJump());
Sirish Pande4bd20c52012-05-12 05:10:30 +0000402
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000403 addPass(createHexagonBranchRelaxation());
Krzysztof Parzyszek7b59ae22016-04-19 18:30:18 +0000404
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000405 if (!NoOpt) {
406 if (!DisableHardwareLoops)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000407 addPass(createHexagonFixupHwLoops());
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000408 // Generate MUX from pairs of conditional transfers.
409 if (EnableGenMux)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000410 addPass(createHexagonGenMux());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000411 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000412
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +0000413 // Packetization is mandatory: it handles gather/scatter at all opt levels.
414 addPass(createHexagonPacketizer(NoOpt), false);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000415
Ron Lieberman8123b962016-08-01 19:36:39 +0000416 if (EnableVectorPrint)
417 addPass(createHexagonVectorPrint(), false);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000418
419 // Add CFI instructions if necessary.
420 addPass(createHexagonCallFrameInformation(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000421}