1//===-- HexagonTargetMachine.cpp - Define TargetMachine for Hexagon -------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// Implements the info about Hexagon target spec.
10//
11//===----------------------------------------------------------------------===//
12
13#include "HexagonTargetMachine.h"
14#include "Hexagon.h"
15#include "HexagonISelLowering.h"
16#include "HexagonLoopIdiomRecognition.h"
17#include "HexagonMachineFunctionInfo.h"
18#include "HexagonMachineScheduler.h"
19#include "HexagonTargetObjectFile.h"
20#include "HexagonTargetTransformInfo.h"
21#include "HexagonVectorLoopCarriedReuse.h"
22#include "TargetInfo/HexagonTargetInfo.h"
23#include "llvm/CodeGen/MIRParser/MIParser.h"
24#include "llvm/CodeGen/Passes.h"
25#include "llvm/CodeGen/TargetPassConfig.h"
26#include "llvm/CodeGen/VLIWMachineScheduler.h"
27#include "llvm/InitializePasses.h"
28#include "llvm/MC/TargetRegistry.h"
29#include "llvm/Passes/PassBuilder.h"
30#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Compiler.h"
32#include "llvm/Transforms/Scalar.h"
33#include <optional>
34
35using namespace llvm;
36
37static cl::opt<bool>
38 EnableCExtOpt("hexagon-cext", cl::Hidden, cl::init(Val: true),
39 cl::desc("Enable Hexagon constant-extender optimization"));
40
41static cl::opt<bool> EnableRDFOpt("rdf-opt", cl::Hidden, cl::init(Val: true),
42 cl::desc("Enable RDF-based optimizations"));
43
44static cl::opt<bool> EnablePostRAHandleQFP(
45 "hexagon-handle-qfloat", cl::init(Val: true), cl::Hidden,
46 cl::desc("Fix up QFloat spills and reloads after register allocation"));
47
48cl::opt<unsigned> RDFFuncBlockLimit(
49 "rdf-bb-limit", cl::Hidden, cl::init(Val: 1000),
50 cl::desc("Basic block limit for a function for RDF optimizations"));
51
52static cl::opt<bool>
53 DisableHardwareLoops("disable-hexagon-hwloops", cl::Hidden,
54 cl::desc("Disable Hardware Loops for Hexagon target"));
55
56static cl::opt<bool> EnableMCR("hexagon-mcr", cl::Hidden, cl::init(Val: true),
57 cl::desc("Enable the machine combiner pass"));
58
59static cl::opt<bool>
60 DisableAModeOpt("disable-hexagon-amodeopt", cl::Hidden,
61 cl::desc("Disable Hexagon Addressing Mode Optimization"));
62
63static cl::opt<bool>
64 DisableHexagonCFGOpt("disable-hexagon-cfgopt", cl::Hidden,
65 cl::desc("Disable Hexagon CFG Optimization"));
66
67static cl::opt<bool>
68 DisableHCP("disable-hcp", cl::Hidden,
69 cl::desc("Disable Hexagon constant propagation"));
70
71static cl::opt<bool> DisableHexagonMask(
72 "disable-mask", cl::Hidden,
73 cl::desc("Disable Hexagon specific Mask generation pass"));
74
75static cl::opt<bool> DisableHexagonLiveVars(
76 "disable-hlv", cl::Hidden,
77 cl::desc("Disable Hexagon specific post-RA live-variable analysis"));
78static cl::opt<bool> DisableStoreWidening("disable-store-widen", cl::Hidden,
79 cl::init(Val: false),
80 cl::desc("Disable store widening"));
81
82static cl::opt<bool> DisableLoadWidening("disable-load-widen", cl::Hidden,
83 cl::desc("Disable load widening"));
84
85static cl::opt<bool> EnableExpandCondsets("hexagon-expand-condsets",
86 cl::init(Val: true), cl::Hidden,
87 cl::desc("Early expansion of MUX"));
88
89static cl::opt<bool> EnableTfrCleanup("hexagon-tfr-cleanup", cl::init(Val: true),
90 cl::Hidden,
91 cl::desc("Cleanup of TFRs/COPYs"));
92
93static cl::opt<bool> EnableEarlyIf("hexagon-eif", cl::init(Val: true), cl::Hidden,
94 cl::desc("Enable early if-conversion"));
95
96static cl::opt<bool> EnableCopyHoist("hexagon-copy-hoist", cl::init(Val: true),
97 cl::Hidden,
98 cl::desc("Enable Hexagon copy hoisting"));
99
100static cl::opt<bool>
101 EnableGenInsert("hexagon-insert", cl::init(Val: true), cl::Hidden,
102 cl::desc("Generate \"insert\" instructions"));
103
104static cl::opt<bool>
105 EnableCommGEP("hexagon-commgep", cl::init(Val: true), cl::Hidden,
106 cl::desc("Enable commoning of GEP instructions"));
107
108static cl::opt<bool>
109 EnableGenExtract("hexagon-extract", cl::init(Val: true), cl::Hidden,
110 cl::desc("Generate \"extract\" instructions"));
111
112static cl::opt<bool> EnableGenMux(
113 "hexagon-mux", cl::init(Val: true), cl::Hidden,
114 cl::desc("Enable converting conditional transfers into MUX instructions"));
115
116static cl::opt<bool>
117 EnableGenPred("hexagon-gen-pred", cl::init(Val: true), cl::Hidden,
118 cl::desc("Enable conversion of arithmetic operations to "
119 "predicate instructions"));
120
121static cl::opt<bool>
122 EnableLoopPrefetch("hexagon-loop-prefetch", cl::Hidden,
123 cl::desc("Enable loop data prefetch on Hexagon"));
124
125static cl::opt<bool>
126 DisableHSDR("disable-hsdr", cl::init(Val: false), cl::Hidden,
127 cl::desc("Disable splitting double registers"));
128
129static cl::opt<bool>
130 EnableGenMemAbs("hexagon-mem-abs", cl::init(Val: true), cl::Hidden,
131 cl::desc("Generate absolute set instructions"));
132
133static cl::opt<bool> EnableBitSimplify("hexagon-bit", cl::init(Val: true),
134 cl::Hidden,
135 cl::desc("Bit simplification"));
136
137static cl::opt<bool> EnableLoopResched("hexagon-loop-resched", cl::init(Val: true),
138 cl::Hidden,
139 cl::desc("Loop rescheduling"));
140
141static cl::opt<bool> HexagonNoOpt("hexagon-noopt", cl::init(Val: false), cl::Hidden,
142 cl::desc("Disable backend optimizations"));
143
144static cl::opt<bool>
145 EnableVectorPrint("enable-hexagon-vector-print", cl::Hidden,
146 cl::desc("Enable Hexagon Vector print instr pass"));
147
148static cl::opt<bool>
149 EnableVExtractOpt("hexagon-opt-vextract", cl::Hidden, cl::init(Val: true),
150 cl::desc("Enable vextract optimization"));
151
152static cl::opt<bool>
153 EnableVectorCombine("hexagon-vector-combine", cl::Hidden, cl::init(Val: true),
154 cl::desc("Enable HVX vector combining"));
155
156static cl::opt<bool> EnableInitialCFGCleanup(
157 "hexagon-initial-cfg-cleanup", cl::Hidden, cl::init(Val: true),
158 cl::desc("Simplify the CFG after atomic expansion pass"));
159
160static cl::opt<bool> EnableInstSimplify("hexagon-instsimplify", cl::Hidden,
161 cl::init(Val: true),
162 cl::desc("Enable instsimplify"));
163
164cl::opt<QFloatMode> QFloatModeValue(
165 "hexagon-qfloat-mode", cl::desc("Specify the qfloat mode to operate on."),
166 cl::Hidden, cl::init(Val: QFloatMode::Legacy),
167 cl::values(
168 clEnumValN(QFloatMode::StrictIEEE, "strict-ieee",
169 "Enable code generation for qfloat strict IEEE-754 mode"),
170 clEnumValN(QFloatMode::IEEE, "ieee",
171 "Enable code generation for qfloat IEEE-754 mode"),
172 clEnumValN(QFloatMode::Lossy, "lossy",
173 "Enable code generation for qfloat lossy-subnormals mode"),
174 clEnumValN(QFloatMode::Legacy, "legacy",
175 "Enable code generation for qfloat legacy mode")));
176
177/// HexagonTargetMachineModule - Note that this is used on hosts that
178/// cannot link in a library unless there are references into the
179/// library. In particular, it seems that it is not possible to get
180/// things to work on Win32 without this. Though it is unused, do not
181/// remove it.
182extern "C" int HexagonTargetMachineModule;
183int HexagonTargetMachineModule = 0;
184
185static ScheduleDAGInstrs *createVLIWMachineSched(MachineSchedContext *C) {
186 ScheduleDAGMILive *DAG = new VLIWMachineScheduler(
187 C, std::make_unique<HexagonConvergingVLIWScheduler>());
188 DAG->addMutation(Mutation: std::make_unique<HexagonSubtarget::UsrOverflowMutation>());
189 DAG->addMutation(Mutation: std::make_unique<HexagonSubtarget::HVXMemLatencyMutation>());
190 DAG->addMutation(
191 Mutation: std::make_unique<HexagonSubtarget::HMXActivateWeightMutation>());
192 DAG->addMutation(Mutation: std::make_unique<HexagonSubtarget::CallMutation>());
193 DAG->addMutation(Mutation: createCopyConstrainDAGMutation(TII: DAG->TII));
194 DAG->addMutation(Mutation: std::make_unique<HexagonSubtarget::HLXPseudoMutation>());
195 return DAG;
196}
197
198static MachineSchedRegistry
199 SchedCustomRegistry("hexagon", "Run Hexagon's custom scheduler",
200 createVLIWMachineSched);
201
202static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
203 return RM.value_or(u: Reloc::Static);
204}
205
206extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void
207LLVMInitializeHexagonTarget() {
208 // Register the target.
209 RegisterTargetMachine<HexagonTargetMachine> X(getTheHexagonTarget());
210
211 PassRegistry &PR = *PassRegistry::getPassRegistry();
212 initializeHexagonAlignGlobalArraysPass(PR);
213 initializeHexagonAsmPrinterPass(PR);
214 initializeHexagonBitSimplifyPass(PR);
215 initializeHexagonConstExtendersPass(PR);
216 initializeHexagonConstPropagationPass(PR);
217 initializeHexagonCopyToCombinePass(PR);
218 initializeHexagonEarlyIfConversionPass(PR);
219 initializeHexagonGenMemAbsolutePass(PR);
220 initializeHexagonGenMuxPass(PR);
221 initializeHexagonGlobalSchedulerPass(PR);
222 initializeHLXExpandPseudosPass(PR);
223 initializeHexagonLiveVariablesPass(PR);
224 initializeHexagonHardwareLoopsPass(PR);
225 initializeHexagonHVXSaveRemarkPass(PR);
226 initializeHexagonLoopIdiomRecognizeLegacyPassPass(PR);
227 initializeHexagonLowerHLXPairAllocasPass(PR);
228 initializeHexagonMergeActivateWeightPass(PR);
229 initializeHexagonMergeActivateWeight2Pass(PR);
230 initializeHexagonNewValueJumpPass(PR);
231 initializeHexagonOptAddrModePass(PR);
232 initializeHexagonPacketizerPass(PR);
233 initializeHexagonRDFOptPass(PR);
234 initializeHexagonSplitDoubleRegsPass(PR);
235 initializeHexagonVectorCombineLegacyPass(PR);
236 initializeHexagonVectorLoopCarriedReuseLegacyPassPass(PR);
237 initializeHexagonVExtractPass(PR);
238 initializeHexagonDAGToDAGISelLegacyPass(PR);
239 initializeHexagonLoopReschedulingPass(PR);
240 initializeHexagonBranchRelaxationPass(PR);
241 initializeHexagonCFGOptimizerPass(PR);
242 initializeHexagonCommonGEPPass(PR);
243 initializeHexagonCopyHoistingPass(PR);
244 initializeHexagonExpandCondsetsPass(PR);
245 initializeHexagonLoopAlignPass(PR);
246 initializeHexagonTfrCleanupPass(PR);
247 initializeHexagonFixupHwLoopsPass(PR);
248 initializeHexagonCallFrameInformationPass(PR);
249 initializeHexagonGenExtractPass(PR);
250 initializeHexagonGenInsertPass(PR);
251 initializeHexagonGenPredicatePass(PR);
252 initializeHexagonLoadWideningPass(PR);
253 initializeHexagonStoreWideningPass(PR);
254 initializeHexagonMaskPass(PR);
255 initializeHexagonOptimizeSZextendsPass(PR);
256 initializeHexagonPeepholePass(PR);
257 initializeHexagonSplitConst32AndConst64Pass(PR);
258 initializeHexagonVectorPrintPass(PR);
259 initializeHexagonQFPOptimizerPass(PR);
260 initializeHexagonXQFloatGeneratorPass(PR);
261 initializeHexagonPostRAHandleQFPPass(PR);
262 initializeMachineKCFILegacyPass(PR);
263}
264
265HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
266 StringRef CPU, StringRef FS,
267 const TargetOptions &Options,
268 std::optional<Reloc::Model> RM,
269 std::optional<CodeModel::Model> CM,
270 CodeGenOptLevel OL, bool JIT)
271 : CodeGenTargetMachineImpl(T, TT, CPU, FS, Options,
272 getEffectiveRelocModel(RM),
273 getEffectiveCodeModel(CM, Default: CodeModel::Small),
274 (HexagonNoOpt ? CodeGenOptLevel::None : OL)),
275 TLOF(std::make_unique<HexagonTargetObjectFile>()),
276 Subtarget(Triple(TT), CPU, FS, *this) {
277 initAsmInfo();
278}
279
280const HexagonSubtarget *
281HexagonTargetMachine::getSubtargetImpl(const Function &F) const {
282 AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
283 const Module *M = F.getParent();
284 if (const HexagonSubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M))
285 return ST;
286
287 SubtargetAttrs Attrs;
288 Attrs.CPU = FnAttrs.getAttribute(Kind: "target-cpu");
289 Attrs.FS = FnAttrs.getAttribute(Kind: "target-features");
290
291 if (const HexagonSubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs))
292 return ST;
293
294 std::string CPU =
295 Attrs.CPU.isValid() ? Attrs.CPU.getValueAsString().str() : TargetCPU;
296 std::string FS =
297 Attrs.FS.isValid() ? Attrs.FS.getValueAsString().str() : TargetFS;
298
299 auto &I = SubtargetMap[CPU + FS];
300 if (!I)
301 I = std::make_unique<HexagonSubtarget>(args: TargetTriple, args&: CPU, args&: FS, args: *this);
302 LastSubtarget.set(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs, NewSubtarget: I.get());
303 return I.get();
304}
305
306void HexagonTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
307#define GET_PASS_REGISTRY "HexagonPassRegistry.def"
308#include "llvm/Passes/TargetPassRegistry.inc"
309
310 PB.registerLateLoopOptimizationsEPCallback(
311 C: [=](LoopPassManager &LPM, OptimizationLevel Level) {
312 if (Level != OptimizationLevel::O0)
313 LPM.addPass(Pass: HexagonLoopIdiomRecognitionPass());
314 });
315 PB.registerLoopOptimizerEndEPCallback(
316 C: [=](LoopPassManager &LPM, OptimizationLevel Level) {
317 if (Level != OptimizationLevel::O0)
318 LPM.addPass(Pass: HexagonVectorLoopCarriedReusePass());
319 });
320}
321
322TargetTransformInfo
323HexagonTargetMachine::getTargetTransformInfo(const Function &F) const {
324 return TargetTransformInfo(std::make_unique<HexagonTTIImpl>(args: this, args: F));
325}
326
327MachineFunctionInfo *HexagonTargetMachine::createMachineFunctionInfo(
328 BumpPtrAllocator &Allocator, const Function &F,
329 const TargetSubtargetInfo *STI) const {
330 return HexagonMachineFunctionInfo::create<HexagonMachineFunctionInfo>(
331 Allocator, F, STI);
332}
333
334yaml::MachineFunctionInfo *
335HexagonTargetMachine::createDefaultFuncInfoYAML() const {
336 return new yaml::HexagonFunctionInfo();
337}
338
339yaml::MachineFunctionInfo *
340HexagonTargetMachine::convertFuncInfoToYAML(const MachineFunction &MF) const {
341 const auto *MFI = MF.getInfo<HexagonMachineFunctionInfo>();
342 const auto &TRI = *MF.getSubtarget().getRegisterInfo();
343 return new yaml::HexagonFunctionInfo(*MFI, TRI);
344}
345
346bool HexagonTargetMachine::parseMachineFunctionInfo(
347 const yaml::MachineFunctionInfo &MFI_, PerFunctionMIParsingState &PFS,
348 SMDiagnostic &Error, SMRange &SourceRange) const {
349 const auto &YamlMFI = static_cast<const yaml::HexagonFunctionInfo &>(MFI_);
350 MachineFunction &MF = PFS.MF;
351 HexagonMachineFunctionInfo *MFI = MF.getInfo<HexagonMachineFunctionInfo>();
352
353 MFI->initializeBaseYamlFields(YamlMFI);
354
355 // Parse StackAlignBaseReg register name
356 if (!YamlMFI.StackAlignBaseReg.Value.empty()) {
357 Register Reg;
358 if (parseNamedRegisterReference(PFS, Reg, Src: YamlMFI.StackAlignBaseReg.Value,
359 Error)) {
360 SourceRange = YamlMFI.StackAlignBaseReg.SourceRange;
361 return true;
362 }
363 MFI->setStackAlignBaseReg(Reg);
364 }
365
366 return false;
367}
368
369HexagonTargetMachine::~HexagonTargetMachine() = default;
370
371ScheduleDAGInstrs *
372HexagonTargetMachine::createMachineScheduler(MachineSchedContext *C) const {
373 return createVLIWMachineSched(C);
374}
375
376namespace {
377/// Hexagon Code Generator Pass Configuration Options.
378class HexagonPassConfig : public TargetPassConfig {
379public:
380 HexagonPassConfig(HexagonTargetMachine &TM, PassManagerBase &PM)
381 : TargetPassConfig(TM, PM) {}
382
383 HexagonTargetMachine &getHexagonTargetMachine() const {
384 return getTM<HexagonTargetMachine>();
385 }
386
387 void addIRPasses() override;
388 bool addPreISel() override;
389 bool addInstSelector() override;
390 bool addILPOpts() override;
391 void addMachineSSAOptimization() override;
392 void addPreRegAlloc() override;
393 bool isMachinePipelinerEnabled() const override {
394 return getOptLevel() >= CodeGenOptLevel::Default;
395 }
396 void addPostRegAlloc() override;
397 void addPreSched2() override;
398 void addPreEmitPass() override;
399};
400} // namespace
401
402TargetPassConfig *HexagonTargetMachine::createPassConfig(PassManagerBase &PM) {
403 return new HexagonPassConfig(*this, PM);
404}
405
406void HexagonPassConfig::addIRPasses() {
407 TargetPassConfig::addIRPasses();
408 bool NoOpt = (getOptLevel() == CodeGenOptLevel::None);
409
410 if (!NoOpt) {
411 // Raise the alignment of global integer arrays to 8 bytes. At -O1/-O2,
412 // reduce .rodata size by keeping byte/half-word arrays at their natural
413 // alignment; apply full 8-byte alignment at -O3.
414 addPass(P: createHexagonAlignGlobalArrays(ReduceRodataSize: getOptLevel() !=
415 CodeGenOptLevel::Aggressive));
416 if (EnableInstSimplify)
417 addPass(P: createInstSimplifyLegacyPass());
418 addPass(P: createDeadCodeEliminationPass());
419 }
420
421 addPass(P: createAtomicExpandLegacyPass());
422
423 if (!NoOpt) {
424 if (EnableInitialCFGCleanup)
425 addPass(P: createCFGSimplificationPass(Options: SimplifyCFGOptions()
426 .forwardSwitchCondToPhi(B: true)
427 .convertSwitchRangeToICmp(B: true)
428 .convertSwitchToLookupTable(B: true)
429 .needCanonicalLoops(B: false)
430 .hoistCommonInsts(B: true)
431 .sinkCommonInsts(B: true)));
432 if (EnableLoopPrefetch)
433 addPass(P: createLoopDataPrefetchPass());
434 if (EnableVectorCombine)
435 addPass(P: createHexagonVectorCombineLegacyPass());
436 if (EnableCommGEP)
437 addPass(P: createHexagonCommonGEP());
438 // Replace certain combinations of shifts and ands with extracts.
439 if (EnableGenExtract)
440 addPass(P: createHexagonGenExtract());
441 }
442}
443
444bool HexagonPassConfig::addPreISel() {
445 // Functions can enable HLX through target-features, so the pass filters
446 // individual functions instead of being gated by the default subtarget.
447 addPass(P: createHexagonLowerHLXPairAllocas());
448 return false;
449}
450
451bool HexagonPassConfig::addInstSelector() {
452 HexagonTargetMachine &TM = getHexagonTargetMachine();
453 const HexagonSubtarget *HST = TM.getHexagonSubtarget();
454 bool NoOpt = (getOptLevel() == CodeGenOptLevel::None);
455
456 if (!NoOpt)
457 addPass(P: createHexagonOptimizeSZextends());
458
459 addPass(P: createHexagonISelDag(TM, OptLevel: getOptLevel()));
460 // Run the QFloat mode code generation pass only if v79 or greater.
461 // Do not run this pass, if legacy mode is passed on command line.
462 if (HST->useHVXV79Ops() && (QFloatModeValue != QFloatMode::Legacy))
463 addPass(P: createHexagonXQFloatGenerator());
464
465 if (!NoOpt) {
466 if (EnableVExtractOpt)
467 addPass(P: createHexagonVExtract());
468 // Create logical operations on predicate registers.
469 if (EnableGenPred)
470 addPass(P: createHexagonGenPredicate());
471 // Rotate loops to expose bit-simplification opportunities.
472 if (EnableLoopResched)
473 addPass(P: createHexagonLoopRescheduling());
474 // Split double registers.
475 if (!DisableHSDR)
476 addPass(P: createHexagonSplitDoubleRegs());
477 // Bit simplification.
478 if (EnableBitSimplify)
479 addPass(P: createHexagonBitSimplify());
480 addPass(P: createHexagonPeephole());
481 // Constant propagation.
482 if (!DisableHCP) {
483 addPass(P: createHexagonConstPropagationPass());
484 addPass(PassID: &UnreachableMachineBlockElimID);
485 }
486 if (EnableGenInsert)
487 addPass(P: createHexagonGenInsert());
488 if (EnableEarlyIf)
489 addPass(P: createHexagonEarlyIfConversion());
490 // For v75 or below, or if legacy mode is requested, run QFPOptizer pass
491 // to preserve backward compatibility.
492 if (!HST->useHVXV79Ops() || (QFloatModeValue == QFloatMode::Legacy))
493 addPass(P: createHexagonQFPOptimizer());
494 }
495
496 return false;
497}
498
499bool HexagonPassConfig::addILPOpts() {
500 if (EnableMCR)
501 addPass(PassID: &MachineCombinerID);
502
503 return true;
504}
505
506void HexagonPassConfig::addMachineSSAOptimization() {
507 TargetPassConfig::addMachineSSAOptimization();
508
509 if (EnableCExtOpt)
510 addPass(P: createHexagonConstExtenders());
511 if (!DisableStoreWidening)
512 addPass(P: createHexagonStoreWidening());
513 if (!DisableLoadWidening)
514 addPass(P: createHexagonLoadWidening());
515 if (EnableGenMemAbs)
516 addPass(P: createHexagonGenMemAbsolute());
517 if (!DisableHardwareLoops)
518 addPass(P: createHexagonHardwareLoops());
519}
520
521void HexagonPassConfig::addPreRegAlloc() {
522 // HMX activation/weight pairs must be adjacent before register allocation.
523 // With optimized register allocation, run after the machine scheduler,
524 // which may separate the pairs.
525 if (getOptimizeRegAlloc())
526 insertPass(TargetPassID: &MachineSchedulerID, InsertedPassID: &HexagonMergeActivateWeightID);
527 else
528 insertPass(TargetPassID: &PHIEliminationID, InsertedPassID: &HexagonMergeActivateWeightID);
529
530 if (getOptLevel() != CodeGenOptLevel::None) {
531 if (EnableExpandCondsets)
532 insertPass(TargetPassID: &RegisterCoalescerID, InsertedPassID: &HexagonExpandCondsetsID);
533 if (EnableCopyHoist)
534 insertPass(TargetPassID: &RegisterCoalescerID, InsertedPassID: &HexagonCopyHoistingID);
535 if (EnableTfrCleanup)
536 insertPass(TargetPassID: &VirtRegRewriterID, InsertedPassID: &HexagonTfrCleanupID);
537 }
538 addPass(P: createHexagonHVXSaveRemark());
539}
540
541void HexagonPassConfig::addPostRegAlloc() {
542 HexagonTargetMachine &HTM = getHexagonTargetMachine();
543 const HexagonSubtarget *HST = HTM.getHexagonSubtarget();
544 // Run PostRAQFP on v79 and above.
545 if (EnablePostRAHandleQFP && HST->useHVXV79Ops())
546 addPass(P: createHexagonPostRAHandleQFP());
547
548 if (getOptLevel() != CodeGenOptLevel::None) {
549 if (EnableRDFOpt)
550 addPass(P: createHexagonRDFOpt());
551 if (!DisableHexagonCFGOpt)
552 addPass(P: createHexagonCFGOptimizer());
553 if (!DisableAModeOpt)
554 addPass(P: createHexagonOptAddrMode());
555 }
556}
557
558void HexagonPassConfig::addPreSched2() {
559 bool NoOpt = (getOptLevel() == CodeGenOptLevel::None);
560 addPass(P: createHexagonCopyToCombine());
561 if (getOptLevel() != CodeGenOptLevel::None)
562 addPass(PassID: &IfConverterID);
563 addPass(P: createHexagonSplitConst32AndConst64());
564 if (!NoOpt && !DisableHexagonMask)
565 addPass(P: createHexagonMask());
566
567 if (!NoOpt && !DisableHexagonLiveVars) {
568 addPass(PassID: &HexagonLiveVariablesID);
569 }
570}
571
572void HexagonPassConfig::addPreEmitPass() {
573 bool NoOpt = (getOptLevel() == CodeGenOptLevel::None);
574
575 if (!NoOpt)
576 addPass(P: createHexagonNewValueJump());
577
578 addPass(P: createHexagonBranchRelaxation());
579
580 if (!NoOpt) {
581 if (!DisableHardwareLoops)
582 addPass(P: createHexagonFixupHwLoops());
583 // Generate MUX from pairs of conditional transfers.
584 if (EnableGenMux)
585 addPass(P: createHexagonGenMux());
586 if (!DisableHexagonLiveVars)
587 addPass(PassID: &HexagonLiveVariablesID);
588 }
589
590 addPass(P: createHexagonMergeActivateWeight2());
591
592 // Emit KCFI checks for indirect calls. Must run before packetization so
593 // the check and call can be bundled together into a VLIW packet.
594 addPass(P: createKCFIPass());
595
596 // Packetization is mandatory: it handles gather/scatter at all opt levels.
597 addPass(P: createHexagonPacketizer(Minimal: NoOpt));
598
599 // Expand HLX load/store pseudos into their final bundled form. This must run
600 // after packetization so the resulting bundles are not seen by the scheduler.
601 addPass(P: createHLXExpandPseudos());
602
603 if (!NoOpt) {
604 // Global pull-up scheduler
605 addPass(P: createHexagonGlobalScheduler());
606
607 addPass(P: createHexagonLoopAlign());
608 }
609
610 if (EnableVectorPrint)
611 addPass(P: createHexagonVectorPrint());
612
613 // Add CFI instructions if necessary.
614 addPass(P: createHexagonCallFrameInformation());
615}
616