1//===----------------------------------------------------------------------===//
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#include "SystemZ.h"
10#include "SystemZAsmPrinter.h"
11#include "SystemZTargetMachine.h"
12#include "llvm/CodeGen/AtomicExpand.h"
13#include "llvm/CodeGen/EarlyIfConversion.h"
14#include "llvm/CodeGen/IfConversion.h"
15#include "llvm/CodeGen/MachineCombiner.h"
16#include "llvm/CodeGen/MachineScheduler.h"
17#include "llvm/IR/PassInstrumentation.h"
18#include "llvm/Passes/CodeGenPassBuilder.h"
19#include "llvm/Passes/PassBuilder.h"
20#include "llvm/Support/CodeGen.h"
21#include "llvm/Target/CGPassBuilderOption.h"
22#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
23
24using namespace llvm;
25
26extern cl::opt<bool> EnableMachineCombinerPass;
27
28namespace {
29
30class SystemZCodeGenPassBuilder : public CodeGenPassBuilder {
31 using Base = CodeGenPassBuilder;
32
33 SystemZTargetMachine &getTM() const {
34 return static_cast<SystemZTargetMachine &>(TM);
35 }
36
37public:
38 explicit SystemZCodeGenPassBuilder(SystemZTargetMachine &TM,
39 const CGPassBuilderOption &Opts,
40 PassInstrumentationCallbacks *PIC)
41 : CodeGenPassBuilder(TM, Opts, PIC) {}
42
43 void addIRPasses(PassManagerWrapper &PMW) override;
44 Error addInstSelector(PassManagerWrapper &PMW) override;
45 void addILPOpts(PassManagerWrapper &PMW) override;
46 void addPreRegAlloc(PassManagerWrapper &PMW) override;
47 void addPostRewrite(PassManagerWrapper &PMW) override;
48 void addPostRegAlloc(PassManagerWrapper &PMW) override;
49 void addPreSched2(PassManagerWrapper &PMW) override;
50 void addPreEmitPass(PassManagerWrapper &PMW) override;
51 void addAsmPrinterBegin(PassManagerWrapper &PMW) override;
52 void addAsmPrinter(PassManagerWrapper &PMW) override;
53 void addAsmPrinterEnd(PassManagerWrapper &PMW) override;
54};
55
56void SystemZCodeGenPassBuilder::addIRPasses(PassManagerWrapper &PMW) {
57 if (getOptLevel() != CodeGenOptLevel::None) {
58 flushFPMsToMPM(PMW);
59 addModulePass(Pass: SystemZAlignGlobalsPass(), PMW);
60 addFunctionPass(Pass: SystemZTDCPass(getTM()), PMW);
61 addFunctionPass(Pass: LoopDataPrefetchPass(), PMW);
62 }
63
64 addFunctionPass(Pass: AtomicExpandPass(getTM()), PMW);
65
66 Base::addIRPasses(PMW);
67}
68
69Error SystemZCodeGenPassBuilder::addInstSelector(PassManagerWrapper &PMW) {
70 addMachineFunctionPass(Pass: SystemZISelDAGToDAGPass(getTM(), getOptLevel()), PMW);
71
72 if (getOptLevel() != CodeGenOptLevel::None)
73 addMachineFunctionPass(Pass: SystemZLDCleanupPass(), PMW);
74
75 return Error::success();
76}
77
78void SystemZCodeGenPassBuilder::addILPOpts(PassManagerWrapper &PMW) {
79 addMachineFunctionPass(Pass: EarlyIfConverterPass(), PMW);
80
81 if (EnableMachineCombinerPass)
82 addMachineFunctionPass(Pass: MachineCombinerPass(), PMW);
83}
84
85void SystemZCodeGenPassBuilder::addPreRegAlloc(PassManagerWrapper &PMW) {
86 addMachineFunctionPass(Pass: SystemZCopyPhysRegsPass(), PMW);
87}
88
89void SystemZCodeGenPassBuilder::addPostRewrite(PassManagerWrapper &PMW) {
90 addMachineFunctionPass(Pass: SystemZPostRewritePass(), PMW);
91}
92
93void SystemZCodeGenPassBuilder::addPostRegAlloc(PassManagerWrapper &PMW) {
94 // PostRewrite needs to be run at -O0 also (in which case addPostRewrite()
95 // is not called).
96 if (getOptLevel() == CodeGenOptLevel::None)
97 addMachineFunctionPass(Pass: SystemZPostRewritePass(), PMW);
98}
99
100void SystemZCodeGenPassBuilder::addPreSched2(PassManagerWrapper &PMW) {
101 if (getOptLevel() != CodeGenOptLevel::None)
102 addMachineFunctionPass(Pass: IfConverterPass(), PMW);
103}
104
105void SystemZCodeGenPassBuilder::addPreEmitPass(PassManagerWrapper &PMW) {
106 // Do instruction shortening before compare elimination because some
107 // vector instructions will be shortened into opcodes that compare
108 // elimination recognizes.
109 if (getOptLevel() != CodeGenOptLevel::None)
110 addMachineFunctionPass(Pass: SystemZShortenInstPass(), PMW);
111
112 // We eliminate comparisons here rather than earlier because some
113 // transformations can change the set of available CC values and we
114 // generally want those transformations to have priority. This is
115 // especially true in the commonest case where the result of the comparison
116 // is used by a single in-range branch instruction, since we will then
117 // be able to fuse the compare and the branch instead.
118 //
119 // For example, two-address NILF can sometimes be converted into
120 // three-address RISBLG. NILF produces a CC value that indicates whether
121 // the low word is zero, but RISBLG does not modify CC at all. On the
122 // other hand, 64-bit ANDs like NILL can sometimes be converted to RISBG.
123 // The CC value produced by NILL isn't useful for our purposes, but the
124 // value produced by RISBG can be used for any comparison with zero
125 // (not just equality). So there are some transformations that lose
126 // CC values (while still being worthwhile) and others that happen to make
127 // the CC result more useful than it was originally.
128 //
129 // Another reason is that we only want to use BRANCH ON COUNT in cases
130 // where we know that the count register is not going to be spilled.
131 //
132 // Doing it so late makes it more likely that a register will be reused
133 // between the comparison and the branch, but it isn't clear whether
134 // preventing that would be a win or not.
135 if (getOptLevel() != CodeGenOptLevel::None)
136 addMachineFunctionPass(Pass: SystemZElimComparePass(), PMW);
137 addMachineFunctionPass(Pass: SystemZLongBranchPass(), PMW);
138
139 // Do final scheduling after all other optimizations, to get an
140 // optimal input for the decoder (branch relaxation must happen
141 // after block placement).
142 if (getOptLevel() != CodeGenOptLevel::None)
143 addMachineFunctionPass(Pass: PostMachineSchedulerPass(&getTM()), PMW);
144}
145
146void SystemZCodeGenPassBuilder::addAsmPrinterBegin(PassManagerWrapper &PMW) {
147 addModulePass(Pass: SystemZAsmPrinterBeginPass(), PMW, /*Force=*/true);
148}
149
150void SystemZCodeGenPassBuilder::addAsmPrinter(PassManagerWrapper &PMW) {
151 addMachineFunctionPass(Pass: SystemZAsmPrinterPass(), PMW);
152}
153
154void SystemZCodeGenPassBuilder::addAsmPrinterEnd(PassManagerWrapper &PMW) {
155 addModulePass(Pass: SystemZAsmPrinterEndPass(), PMW, /*Force=*/true);
156}
157
158} // namespace
159
160void SystemZTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB){
161#define GET_PASS_REGISTRY "SystemZPassRegistry.def"
162#include "llvm/Passes/TargetPassRegistry.inc"
163}
164
165Error SystemZTargetMachine::buildCodeGenPipeline(
166 ModulePassManager &MPM, ModuleAnalysisManager &MAM, raw_pwrite_stream &Out,
167 raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
168 const CGPassBuilderOption &Opt, MCContext &Ctx,
169 PassInstrumentationCallbacks *PIC) {
170 auto CGPB = SystemZCodeGenPassBuilder(*this, Opt, PIC);
171 return CGPB.buildPipeline(MPM, MAM, Out, DwoOut, FileType, Ctx);
172}
173