blob: ddfda7e27793947dfa1df82d1565de16c58f2ea8 [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"
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +000026#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include "llvm/Transforms/Scalar.h"
Tony Linthicum1213a7a2011-12-12 21:14:40 +000028
Tony Linthicum1213a7a2011-12-12 21:14:40 +000029using namespace llvm;
30
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +000031static cl::opt<bool> EnableCExtOpt("hexagon-cext", cl::Hidden, cl::ZeroOrMore,
32 cl::init(true), cl::desc("Enable Hexagon constant-extender optimization"));
33
Krzysztof Parzyszek12798812016-01-12 19:09:01 +000034static cl::opt<bool> EnableRDFOpt("rdf-opt", cl::Hidden, cl::ZeroOrMore,
35 cl::init(true), cl::desc("Enable RDF-based optimizations"));
36
37static cl::opt<bool> DisableHardwareLoops("disable-hexagon-hwloops",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000038 cl::Hidden, cl::desc("Disable Hardware Loops for Hexagon target"));
Tony Linthicum1213a7a2011-12-12 21:14:40 +000039
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000040static cl::opt<bool> DisableAModeOpt("disable-hexagon-amodeopt",
41 cl::Hidden, cl::ZeroOrMore, cl::init(false),
42 cl::desc("Disable Hexagon Addressing Mode Optimization"));
43
Jyotsna Verma653d8832013-03-27 11:14:24 +000044static cl::opt<bool> DisableHexagonCFGOpt("disable-hexagon-cfgopt",
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000045 cl::Hidden, cl::ZeroOrMore, cl::init(false),
46 cl::desc("Disable Hexagon CFG Optimization"));
47
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +000048static cl::opt<bool> DisableHCP("disable-hcp", cl::init(false), cl::Hidden,
49 cl::ZeroOrMore, cl::desc("Disable Hexagon constant propagation"));
50
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +000051static cl::opt<bool> DisableStoreWidening("disable-store-widen",
52 cl::Hidden, cl::init(false), cl::desc("Disable store widening"));
53
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +000054static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
55 cl::init(true), cl::Hidden, cl::ZeroOrMore,
56 cl::desc("Early expansion of MUX"));
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +000057
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +000058static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(true), cl::Hidden,
59 cl::ZeroOrMore, cl::desc("Enable early if-conversion"));
60
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +000061static cl::opt<bool> EnableGenInsert("hexagon-insert", cl::init(true),
62 cl::Hidden, cl::desc("Generate \"insert\" instructions"));
Jyotsna Verma653d8832013-03-27 11:14:24 +000063
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000064static cl::opt<bool> EnableCommGEP("hexagon-commgep", cl::init(true),
65 cl::Hidden, cl::ZeroOrMore, cl::desc("Enable commoning of GEP instructions"));
66
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +000067static cl::opt<bool> EnableGenExtract("hexagon-extract", cl::init(true),
68 cl::Hidden, cl::desc("Generate \"extract\" instructions"));
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +000069
Krzysztof Parzyszek92172202015-07-20 21:23:25 +000070static cl::opt<bool> EnableGenMux("hexagon-mux", cl::init(true), cl::Hidden,
71 cl::desc("Enable converting conditional transfers into MUX instructions"));
72
Krzysztof Parzyszek75874472015-07-14 19:30:21 +000073static cl::opt<bool> EnableGenPred("hexagon-gen-pred", cl::init(true),
74 cl::Hidden, cl::desc("Enable conversion of arithmetic operations to "
75 "predicate instructions"));
76
Krzysztof Parzyszekd3d0a4b2016-07-22 14:22:43 +000077static cl::opt<bool> EnableLoopPrefetch("hexagon-loop-prefetch",
78 cl::init(false), cl::Hidden, cl::ZeroOrMore,
79 cl::desc("Enable loop data prefetch on Hexagon"));
80
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +000081static cl::opt<bool> DisableHSDR("disable-hsdr", cl::init(false), cl::Hidden,
82 cl::desc("Disable splitting double registers"));
83
Krzysztof Parzyszekced99412015-10-20 22:57:13 +000084static cl::opt<bool> EnableBitSimplify("hexagon-bit", cl::init(true),
85 cl::Hidden, cl::desc("Bit simplification"));
86
87static cl::opt<bool> EnableLoopResched("hexagon-loop-resched", cl::init(true),
88 cl::Hidden, cl::desc("Loop rescheduling"));
89
Krzysztof Parzyszekd5590052016-05-11 15:01:30 +000090static cl::opt<bool> HexagonNoOpt("hexagon-noopt", cl::init(false),
91 cl::Hidden, cl::desc("Disable backend optimizations"));
92
Ron Lieberman8123b962016-08-01 19:36:39 +000093static cl::opt<bool> EnableVectorPrint("enable-hexagon-vector-print",
94 cl::Hidden, cl::ZeroOrMore, cl::init(false),
95 cl::desc("Enable Hexagon Vector print instr pass"));
96
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +000097static cl::opt<bool> EnableVExtractOpt("hexagon-opt-vextract", cl::Hidden,
98 cl::ZeroOrMore, cl::init(true), cl::desc("Enable vextract optimization"));
99
Krzysztof Parzyszekd91a9e22018-08-02 22:17:53 +0000100static cl::opt<bool> EnableInitialCFGCleanup("hexagon-initial-cfg-cleanup",
101 cl::Hidden, cl::ZeroOrMore, cl::init(true),
102 cl::desc("Simplify the CFG after atomic expansion pass"));
103
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000104/// HexagonTargetMachineModule - Note that this is used on hosts that
105/// cannot link in a library unless there are references into the
106/// library. In particular, it seems that it is not possible to get
107/// things to work on Win32 without this. Though it is unused, do not
108/// remove it.
109extern "C" int HexagonTargetMachineModule;
110int HexagonTargetMachineModule = 0;
111
Sergei Larin4d8986a2012-09-04 14:49:56 +0000112static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
Krzysztof Parzyszek697297a2017-08-28 15:52:54 +0000113 ScheduleDAGMILive *DAG =
114 new VLIWMachineScheduler(C, make_unique<ConvergingVLIWScheduler>());
Krzysztof Parzyszek95da97e2017-08-28 16:24:22 +0000115 DAG->addMutation(make_unique<HexagonSubtarget::UsrOverflowMutation>());
116 DAG->addMutation(make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
117 DAG->addMutation(make_unique<HexagonSubtarget::CallMutation>());
Krzysztof Parzyszek697297a2017-08-28 15:52:54 +0000118 DAG->addMutation(createCopyConstrainDAGMutation(DAG->TII, DAG->TRI));
119 return DAG;
Sergei Larin4d8986a2012-09-04 14:49:56 +0000120}
121
122static MachineSchedRegistry
123SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
124 createVLIWMachineSched);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000125
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000126namespace llvm {
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000127 extern char &HexagonExpandCondsetsID;
Krzysztof Parzyszekb404fae2018-02-20 14:29:43 +0000128 void initializeHexagonBitSimplifyPass(PassRegistry&);
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +0000129 void initializeHexagonConstExtendersPass(PassRegistry&);
Krzysztof Parzyszek96690ce2018-02-23 20:33:26 +0000130 void initializeHexagonConstPropagationPass(PassRegistry&);
Krzysztof Parzyszek1966fd72017-08-09 21:22:05 +0000131 void initializeHexagonEarlyIfConversionPass(PassRegistry&);
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000132 void initializeHexagonExpandCondsetsPass(PassRegistry&);
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000133 void initializeHexagonGenMuxPass(PassRegistry&);
Krzysztof Parzyszek3818aea2017-10-20 16:56:33 +0000134 void initializeHexagonHardwareLoopsPass(PassRegistry&);
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000135 void initializeHexagonLoopIdiomRecognizePass(PassRegistry&);
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000136 void initializeHexagonVectorLoopCarriedReusePass(PassRegistry&);
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000137 void initializeHexagonNewValueJumpPass(PassRegistry&);
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000138 void initializeHexagonOptAddrModePass(PassRegistry&);
139 void initializeHexagonPacketizerPass(PassRegistry&);
Krzysztof Parzyszekbef1c562017-10-30 14:11:52 +0000140 void initializeHexagonRDFOptPass(PassRegistry&);
Krzysztof Parzyszekeffcc2f2018-05-04 15:04:48 +0000141 void initializeHexagonSplitDoubleRegsPass(PassRegistry&);
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +0000142 void initializeHexagonVExtractPass(PassRegistry&);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000143 Pass *createHexagonLoopIdiomPass();
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000144 Pass *createHexagonVectorLoopCarriedReusePass();
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000145
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000146 FunctionPass *createHexagonBitSimplify();
Krzysztof Parzyszek7b59ae22016-04-19 18:30:18 +0000147 FunctionPass *createHexagonBranchRelaxation();
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000148 FunctionPass *createHexagonCallFrameInformation();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000149 FunctionPass *createHexagonCFGOptimizer();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000150 FunctionPass *createHexagonCommonGEP();
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +0000151 FunctionPass *createHexagonConstExtenders();
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +0000152 FunctionPass *createHexagonConstPropagationPass();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000153 FunctionPass *createHexagonCopyToCombine();
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000154 FunctionPass *createHexagonEarlyIfConversion();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000155 FunctionPass *createHexagonFixupHwLoops();
156 FunctionPass *createHexagonGenExtract();
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000157 FunctionPass *createHexagonGenInsert();
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000158 FunctionPass *createHexagonGenMux();
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000159 FunctionPass *createHexagonGenPredicate();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000160 FunctionPass *createHexagonHardwareLoops();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000161 FunctionPass *createHexagonISelDag(HexagonTargetMachine &TM,
162 CodeGenOpt::Level OptLevel);
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000163 FunctionPass *createHexagonLoopRescheduling();
Colin LeMahieu56efafc2015-06-15 19:05:35 +0000164 FunctionPass *createHexagonNewValueJump();
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +0000165 FunctionPass *createHexagonOptimizeSZextends();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000166 FunctionPass *createHexagonOptAddrMode();
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +0000167 FunctionPass *createHexagonPacketizer(bool Minimal);
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000168 FunctionPass *createHexagonPeephole();
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000169 FunctionPass *createHexagonRDFOpt();
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000170 FunctionPass *createHexagonSplitConst32AndConst64();
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000171 FunctionPass *createHexagonSplitDoubleRegs();
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000172 FunctionPass *createHexagonStoreWidening();
Ron Lieberman8123b962016-08-01 19:36:39 +0000173 FunctionPass *createHexagonVectorPrint();
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +0000174 FunctionPass *createHexagonVExtract();
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000175} // end namespace llvm;
Krzysztof Parzyszekc05dff12015-03-31 13:35:12 +0000176
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000177static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
178 if (!RM.hasValue())
179 return Reloc::Static;
180 return *RM;
181}
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000182
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000183extern "C" void LLVMInitializeHexagonTarget() {
184 // Register the target.
185 RegisterTargetMachine<HexagonTargetMachine> X(getTheHexagonTarget());
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000186
187 PassRegistry &PR = *PassRegistry::getPassRegistry();
Krzysztof Parzyszekb404fae2018-02-20 14:29:43 +0000188 initializeHexagonBitSimplifyPass(PR);
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +0000189 initializeHexagonConstExtendersPass(PR);
Krzysztof Parzyszek96690ce2018-02-23 20:33:26 +0000190 initializeHexagonConstPropagationPass(PR);
Krzysztof Parzyszek1966fd72017-08-09 21:22:05 +0000191 initializeHexagonEarlyIfConversionPass(PR);
Krzysztof Parzyszekde2ac172017-06-13 16:07:36 +0000192 initializeHexagonGenMuxPass(PR);
Krzysztof Parzyszek3818aea2017-10-20 16:56:33 +0000193 initializeHexagonHardwareLoopsPass(PR);
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000194 initializeHexagonLoopIdiomRecognizePass(PR);
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000195 initializeHexagonVectorLoopCarriedReusePass(PR);
Krzysztof Parzyszek5ddd2e52017-06-27 18:37:16 +0000196 initializeHexagonNewValueJumpPass(PR);
Krzysztof Parzyszekdf4a05d2017-07-10 18:38:52 +0000197 initializeHexagonOptAddrModePass(PR);
198 initializeHexagonPacketizerPass(PR);
Krzysztof Parzyszekbef1c562017-10-30 14:11:52 +0000199 initializeHexagonRDFOptPass(PR);
Krzysztof Parzyszekeffcc2f2018-05-04 15:04:48 +0000200 initializeHexagonSplitDoubleRegsPass(PR);
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +0000201 initializeHexagonVExtractPass(PR);
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000202}
203
Daniel Sanders3e5de882015-06-11 19:41:26 +0000204HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000205 StringRef CPU, StringRef FS,
Craig Topperb5454082012-03-17 09:24:09 +0000206 const TargetOptions &Options,
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000207 Optional<Reloc::Model> RM,
Rafael Espindola79e238a2017-08-03 02:16:21 +0000208 Optional<CodeModel::Model> CM,
209 CodeGenOpt::Level OL, bool JIT)
Krzysztof Parzyszeke5996432016-02-12 14:47:38 +0000210 // Specify the vector alignment explicitly. For v512x1, the calculated
211 // alignment would be 512*alignment(i1), which is 512 bytes, instead of
212 // the required minimum of 64 bytes.
Matthias Braunbb8507e2017-10-12 22:57:28 +0000213 : LLVMTargetMachine(
Rafael Espindola79e238a2017-08-03 02:16:21 +0000214 T,
215 "e-m:e-p:32:32:32-a:0-n16:32-"
216 "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
217 "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048",
218 TT, CPU, FS, Options, getEffectiveRelocModel(RM),
David Greenca29c272018-12-07 12:10:23 +0000219 getEffectiveCodeModel(CM, CodeModel::Small),
220 (HexagonNoOpt ? CodeGenOpt::None : OL)),
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000221 TLOF(make_unique<HexagonTargetObjectFile>()) {
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000222 initializeHexagonExpandCondsetsPass(*PassRegistry::getPassRegistry());
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000223 initAsmInfo();
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000224}
225
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000226const HexagonSubtarget *
227HexagonTargetMachine::getSubtargetImpl(const Function &F) const {
Reid Klecknerb5180542017-03-21 16:57:19 +0000228 AttributeList FnAttrs = F.getAttributes();
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000229 Attribute CPUAttr =
Reid Klecknerb5180542017-03-21 16:57:19 +0000230 FnAttrs.getAttribute(AttributeList::FunctionIndex, "target-cpu");
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000231 Attribute FSAttr =
Reid Klecknerb5180542017-03-21 16:57:19 +0000232 FnAttrs.getAttribute(AttributeList::FunctionIndex, "target-features");
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000233
234 std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
235 ? CPUAttr.getValueAsString().str()
236 : TargetCPU;
237 std::string FS = !FSAttr.hasAttribute(Attribute::None)
238 ? FSAttr.getValueAsString().str()
239 : TargetFS;
240
241 auto &I = SubtargetMap[CPU + FS];
242 if (!I) {
243 // This needs to be done before we create a new subtarget since any
244 // creation will depend on the TM and the code generation flags on the
245 // function that reside in TargetOptions.
246 resetTargetOptions(F);
247 I = llvm::make_unique<HexagonSubtarget>(TargetTriple, CPU, FS, *this);
248 }
249 return I.get();
250}
251
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000252void HexagonTargetMachine::adjustPassManager(PassManagerBuilder &PMB) {
253 PMB.addExtension(
254 PassManagerBuilder::EP_LateLoopOptimizations,
255 [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
256 PM.add(createHexagonLoopIdiomPass());
257 });
Pranav Bhandarkar931d0b72017-09-21 21:48:23 +0000258 PMB.addExtension(
259 PassManagerBuilder::EP_LoopOptimizerEnd,
260 [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
261 PM.add(createHexagonVectorLoopCarriedReusePass());
262 });
Krzysztof Parzyszekc8b94382017-01-26 21:41:10 +0000263}
264
Sanjoy Das26d11ca2017-12-22 18:21:59 +0000265TargetTransformInfo
266HexagonTargetMachine::getTargetTransformInfo(const Function &F) {
267 return TargetTransformInfo(HexagonTTIImpl(this, F));
Krzysztof Parzyszek73e66f32015-08-05 18:35:37 +0000268}
269
270
Reid Kleckner357600e2014-11-20 23:37:18 +0000271HexagonTargetMachine::~HexagonTargetMachine() {}
272
Andrew Trickccb67362012-02-03 05:12:41 +0000273namespace {
274/// Hexagon Code Generator Pass Configuration Options.
275class HexagonPassConfig : public TargetPassConfig {
276public:
Matthias Braun5e394c32017-05-30 21:36:41 +0000277 HexagonPassConfig(HexagonTargetMachine &TM, PassManagerBase &PM)
Krzysztof Parzyszekd0f8e1c2016-05-27 20:48:39 +0000278 : TargetPassConfig(TM, PM) {}
Andrew Trickccb67362012-02-03 05:12:41 +0000279
280 HexagonTargetMachine &getHexagonTargetMachine() const {
281 return getTM<HexagonTargetMachine>();
282 }
283
Craig Topper906c2cd2014-04-29 07:58:16 +0000284 ScheduleDAGInstrs *
285 createMachineScheduler(MachineSchedContext *C) const override {
Andrew Trick978674b2013-09-20 05:14:41 +0000286 return createVLIWMachineSched(C);
287 }
288
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000289 void addIRPasses() override;
Craig Topper906c2cd2014-04-29 07:58:16 +0000290 bool addInstSelector() override;
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000291 void addPreRegAlloc() override;
292 void addPostRegAlloc() override;
293 void addPreSched2() override;
294 void addPreEmitPass() override;
Andrew Trickccb67362012-02-03 05:12:41 +0000295};
296} // namespace
297
Andrew Trickf8ea1082012-02-04 02:56:59 +0000298TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
Matthias Braun5e394c32017-05-30 21:36:41 +0000299 return new HexagonPassConfig(*this, PM);
Andrew Trickccb67362012-02-03 05:12:41 +0000300}
301
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000302void HexagonPassConfig::addIRPasses() {
303 TargetPassConfig::addIRPasses();
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000304 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Krzysztof Parzyszekfeaf7b82015-07-09 14:51:21 +0000305
Krzysztof Parzyszekcf3ad582018-01-24 17:48:11 +0000306 if (!NoOpt) {
307 addPass(createConstantPropagationPass());
308 addPass(createDeadCodeEliminationPass());
309 }
310
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000311 addPass(createAtomicExpandPass());
Krzysztof Parzyszekd91a9e22018-08-02 22:17:53 +0000312
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000313 if (!NoOpt) {
Krzysztof Parzyszekd91a9e22018-08-02 22:17:53 +0000314 if (EnableInitialCFGCleanup)
315 addPass(createCFGSimplificationPass(1, true, true, false, true));
Krzysztof Parzyszekd3d0a4b2016-07-22 14:22:43 +0000316 if (EnableLoopPrefetch)
317 addPass(createLoopDataPrefetchPass());
Krzysztof Parzyszeka0ecf072015-07-14 17:07:24 +0000318 if (EnableCommGEP)
319 addPass(createHexagonCommonGEP());
320 // Replace certain combinations of shifts and ands with extracts.
321 if (EnableGenExtract)
322 addPass(createHexagonGenExtract());
323 }
Krzysztof Parzyszek79b24332015-07-08 19:22:28 +0000324}
325
Andrew Trickccb67362012-02-03 05:12:41 +0000326bool HexagonPassConfig::addInstSelector() {
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000327 HexagonTargetMachine &TM = getHexagonTargetMachine();
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000328 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Jyotsna Verma653d8832013-03-27 11:14:24 +0000329
Krzysztof Parzyszek055c5fd2015-10-19 19:10:48 +0000330 if (!NoOpt)
331 addPass(createHexagonOptimizeSZextends());
332
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000333 addPass(createHexagonISelDag(TM, getOptLevel()));
Jyotsna Verma653d8832013-03-27 11:14:24 +0000334
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000335 if (!NoOpt) {
Krzysztof Parzyszek95614ac2018-01-26 21:17:14 +0000336 if (EnableVExtractOpt)
337 addPass(createHexagonVExtract());
Krzysztof Parzyszek75874472015-07-14 19:30:21 +0000338 // Create logical operations on predicate registers.
339 if (EnableGenPred)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000340 addPass(createHexagonGenPredicate());
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000341 // Rotate loops to expose bit-simplification opportunities.
342 if (EnableLoopResched)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000343 addPass(createHexagonLoopRescheduling());
Krzysztof Parzyszeka7c5f042015-10-16 20:38:54 +0000344 // Split double registers.
345 if (!DisableHSDR)
346 addPass(createHexagonSplitDoubleRegs());
Krzysztof Parzyszekced99412015-10-20 22:57:13 +0000347 // Bit simplification.
348 if (EnableBitSimplify)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000349 addPass(createHexagonBitSimplify());
Jyotsna Verma653d8832013-03-27 11:14:24 +0000350 addPass(createHexagonPeephole());
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +0000351 // Constant propagation.
352 if (!DisableHCP) {
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000353 addPass(createHexagonConstPropagationPass());
354 addPass(&UnreachableMachineBlockElimID);
Krzysztof Parzyszek167d9182016-07-28 20:01:59 +0000355 }
Krzysztof Parzyszek21b53a52015-07-08 14:47:34 +0000356 if (EnableGenInsert)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000357 addPass(createHexagonGenInsert());
Krzysztof Parzyszekfb338242015-10-06 15:49:14 +0000358 if (EnableEarlyIf)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000359 addPass(createHexagonEarlyIfConversion());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000360 }
Jyotsna Verma653d8832013-03-27 11:14:24 +0000361
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000362 return false;
363}
364
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000365void HexagonPassConfig::addPreRegAlloc() {
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000366 if (getOptLevel() != CodeGenOpt::None) {
Krzysztof Parzyszek7c9c0582017-10-13 19:02:59 +0000367 if (EnableCExtOpt)
368 addPass(createHexagonConstExtenders());
Krzysztof Parzyszek951fb362016-08-24 22:27:36 +0000369 if (EnableExpandCondsets)
370 insertPass(&RegisterCoalescerID, &HexagonExpandCondsetsID);
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000371 if (!DisableStoreWidening)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000372 addPass(createHexagonStoreWidening());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000373 if (!DisableHardwareLoops)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000374 addPass(createHexagonHardwareLoops());
Krzysztof Parzyszek5b7dd0c2015-10-16 19:43:56 +0000375 }
Brendon Cahoon254f8892016-07-29 16:44:44 +0000376 if (TM->getOptLevel() >= CodeGenOpt::Default)
377 addPass(&MachinePipelinerID);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000378}
379
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000380void HexagonPassConfig::addPostRegAlloc() {
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000381 if (getOptLevel() != CodeGenOpt::None) {
382 if (EnableRDFOpt)
383 addPass(createHexagonRDFOpt());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000384 if (!DisableHexagonCFGOpt)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000385 addPass(createHexagonCFGOptimizer());
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000386 if (!DisableAModeOpt)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000387 addPass(createHexagonOptAddrMode());
Krzysztof Parzyszek12798812016-01-12 19:09:01 +0000388 }
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000389}
390
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000391void HexagonPassConfig::addPreSched2() {
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000392 addPass(createHexagonCopyToCombine());
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000393 if (getOptLevel() != CodeGenOpt::None)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000394 addPass(&IfConverterID);
Eric Christopher01f875e2015-02-02 22:11:43 +0000395 addPass(createHexagonSplitConst32AndConst64());
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000396}
397
Matthias Braun7e37a5f2014-12-11 21:26:47 +0000398void HexagonPassConfig::addPreEmitPass() {
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000399 bool NoOpt = (getOptLevel() == CodeGenOpt::None);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000400
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000401 if (!NoOpt)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000402 addPass(createHexagonNewValueJump());
Sirish Pande4bd20c52012-05-12 05:10:30 +0000403
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000404 addPass(createHexagonBranchRelaxation());
Krzysztof Parzyszek7b59ae22016-04-19 18:30:18 +0000405
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000406 if (!NoOpt) {
407 if (!DisableHardwareLoops)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000408 addPass(createHexagonFixupHwLoops());
Krzysztof Parzyszek92172202015-07-20 21:23:25 +0000409 // Generate MUX from pairs of conditional transfers.
410 if (EnableGenMux)
Krzysztof Parzyszekb1ada4e2017-06-08 21:25:36 +0000411 addPass(createHexagonGenMux());
Krzysztof Parzyszek59df52c2013-05-06 21:25:45 +0000412 }
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000413
Krzysztof Parzyszek39a979c2018-08-17 14:24:24 +0000414 // Packetization is mandatory: it handles gather/scatter at all opt levels.
415 addPass(createHexagonPacketizer(NoOpt), false);
Krzysztof Parzyszeka8ab1b72017-12-11 18:57:54 +0000416
Ron Lieberman8123b962016-08-01 19:36:39 +0000417 if (EnableVectorPrint)
418 addPass(createHexagonVectorPrint(), false);
Krzysztof Parzyszekdb867702015-10-19 17:46:01 +0000419
420 // Add CFI instructions if necessary.
421 addPass(createHexagonCallFrameInformation(), false);
Tony Linthicum1213a7a2011-12-12 21:14:40 +0000422}