| 1 | //===-- X86CodeGenPassBuilder.cpp ---------------------------------*- C++ -*-=// |
| 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 | /// \file |
| 9 | /// This file contains X86 CodeGen pipeline builder. |
| 10 | /// TODO: Port CodeGen passes to new pass manager. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "X86.h" |
| 14 | #include "X86ISelDAGToDAG.h" |
| 15 | #include "X86TargetMachine.h" |
| 16 | |
| 17 | #include "llvm/CodeGen/AtomicExpand.h" |
| 18 | #include "llvm/CodeGen/EarlyIfConversion.h" |
| 19 | #include "llvm/CodeGen/IndirectBrExpand.h" |
| 20 | #include "llvm/CodeGen/InterleavedAccess.h" |
| 21 | #include "llvm/CodeGen/JMCInstrumenter.h" |
| 22 | #include "llvm/MC/MCStreamer.h" |
| 23 | #include "llvm/Passes/CodeGenPassBuilder.h" |
| 24 | #include "llvm/Passes/PassBuilder.h" |
| 25 | #include "llvm/Support/CodeGen.h" |
| 26 | #include "llvm/Transforms/CFGuard.h" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | extern cl::opt<bool> X86EnableMachineCombinerPass; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | class X86CodeGenPassBuilder |
| 35 | : public CodeGenPassBuilder<X86CodeGenPassBuilder, X86TargetMachine> { |
| 36 | using Base = CodeGenPassBuilder<X86CodeGenPassBuilder, X86TargetMachine>; |
| 37 | |
| 38 | public: |
| 39 | explicit X86CodeGenPassBuilder(X86TargetMachine &TM, |
| 40 | const CGPassBuilderOption &Opts, |
| 41 | PassInstrumentationCallbacks *PIC) |
| 42 | : CodeGenPassBuilder(TM, Opts, PIC) {} |
| 43 | |
| 44 | void addIRPasses(PassManagerWrapper &PMW) const; |
| 45 | void addPreISel(PassManagerWrapper &PMW) const; |
| 46 | Error addInstSelector(PassManagerWrapper &PMW) const; |
| 47 | void addILPOpts(PassManagerWrapper &PMW) const; |
| 48 | void addMachineSSAOptimization(PassManagerWrapper &PMW) const; |
| 49 | void addPreRegAlloc(PassManagerWrapper &PMW) const; |
| 50 | // TODO(boomanaiden154): We need to add addPostFastRegAllocRewrite here once |
| 51 | // it is available to support AMX. |
| 52 | void addPostRegAlloc(PassManagerWrapper &PMW) const; |
| 53 | void addPreSched2(PassManagerWrapper &PMW) const; |
| 54 | void addPreEmitPass(PassManagerWrapper &PMW) const; |
| 55 | void addPreEmitPass2(PassManagerWrapper &PMW) const; |
| 56 | // TODO(boomanaiden154): We need to add addRegAssignAndRewriteOptimized here |
| 57 | // once it is available to support AMX. |
| 58 | void addAsmPrinter(PassManagerWrapper &PMW, CreateMCStreamer) const; |
| 59 | }; |
| 60 | |
| 61 | void X86CodeGenPassBuilder::addIRPasses(PassManagerWrapper &PMW) const { |
| 62 | addFunctionPass(Pass: AtomicExpandPass(TM), PMW); |
| 63 | |
| 64 | // We add both pass anyway and when these two passes run, one will be a |
| 65 | // no-op based on the optimization level/attributes. |
| 66 | addFunctionPass(Pass: X86LowerAMXIntrinsicsPass(&TM), PMW); |
| 67 | addFunctionPass(Pass: X86LowerAMXTypePass(&TM), PMW); |
| 68 | |
| 69 | Base::addIRPasses(PMW); |
| 70 | |
| 71 | if (getOptLevel() != CodeGenOptLevel::None) { |
| 72 | addFunctionPass(Pass: InterleavedAccessPass(TM), PMW); |
| 73 | addFunctionPass(Pass: X86PartialReductionPass(&TM), PMW); |
| 74 | } |
| 75 | |
| 76 | // Add passes that handle indirect branch removal and insertion of a retpoline |
| 77 | // thunk. These will be a no-op unless a function subtarget has the retpoline |
| 78 | // feature enabled. |
| 79 | addFunctionPass(Pass: IndirectBrExpandPass(TM), PMW); |
| 80 | |
| 81 | // Add Control Flow Guard checks. |
| 82 | const Triple &TT = TM.getTargetTriple(); |
| 83 | if (TT.isOSWindows()) |
| 84 | addFunctionPass(Pass: CFGuardPass(TT.isX86_64() ? CFGuardPass::Mechanism::Dispatch |
| 85 | : CFGuardPass::Mechanism::Check), |
| 86 | PMW); |
| 87 | |
| 88 | if (TM.Options.JMCInstrument) { |
| 89 | flushFPMsToMPM(PMW); |
| 90 | addModulePass(Pass: JMCInstrumenterPass(), PMW); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void X86CodeGenPassBuilder::addPreISel(PassManagerWrapper &PMW) const { |
| 95 | // Only add this pass for 32-bit x86 Windows. |
| 96 | const Triple &TT = TM.getTargetTriple(); |
| 97 | if (TT.isOSWindows() && TT.isX86_32()) { |
| 98 | // TODO(boomanaiden154): Add X86WinEHStatePass here once it has been ported. |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | Error X86CodeGenPassBuilder::addInstSelector(PassManagerWrapper &PMW) const { |
| 103 | addMachineFunctionPass(Pass: X86ISelDAGToDAGPass(TM), PMW); |
| 104 | |
| 105 | // For ELF, cleanup any local-dynamic TLS accesses |
| 106 | if (TM.getTargetTriple().isOSBinFormatELF() && |
| 107 | getOptLevel() != CodeGenOptLevel::None) { |
| 108 | // TODO(boomanaiden154): Add CleanupLocalDynamicTLSPass here once it has |
| 109 | // been ported. |
| 110 | } |
| 111 | |
| 112 | // TODO(boomanaiden154): Add X86GlobalPassRegPass here once it has been |
| 113 | // ported. |
| 114 | addMachineFunctionPass(Pass: X86ArgumentStackSlotPass(), PMW); |
| 115 | return Error::success(); |
| 116 | } |
| 117 | |
| 118 | void X86CodeGenPassBuilder::addILPOpts(PassManagerWrapper &PMW) const { |
| 119 | addMachineFunctionPass(Pass: EarlyIfConverterPass(), PMW); |
| 120 | if (X86EnableMachineCombinerPass) { |
| 121 | // TODO(boomanaiden154): Add the MachineCombinerPass here once it has been |
| 122 | // ported to the new pass manager. |
| 123 | } |
| 124 | addMachineFunctionPass(Pass: X86CmovConversionPass(), PMW); |
| 125 | } |
| 126 | |
| 127 | void X86CodeGenPassBuilder::addMachineSSAOptimization( |
| 128 | PassManagerWrapper &PMW) const { |
| 129 | // TODO(boomanaiden154): Add X86DomainReassignmentPass here once it has been |
| 130 | // ported. |
| 131 | Base::addMachineSSAOptimization(PMW); |
| 132 | } |
| 133 | |
| 134 | void X86CodeGenPassBuilder::addPreRegAlloc(PassManagerWrapper &PMW) const { |
| 135 | if (getOptLevel() != CodeGenOptLevel::None) { |
| 136 | addMachineFunctionPass(Pass: LiveRangeShrinkPass(), PMW); |
| 137 | addMachineFunctionPass(Pass: X86FixupSetCCPass(), PMW); |
| 138 | addMachineFunctionPass(Pass: X86CallFrameOptimizationPass(), PMW); |
| 139 | addMachineFunctionPass(Pass: X86AvoidStoreForwardingBlocksPass(), PMW); |
| 140 | } |
| 141 | |
| 142 | addMachineFunctionPass(Pass: X86SuppressAPXForRelocationPass(), PMW); |
| 143 | addMachineFunctionPass(Pass: X86SpeculativeLoadHardeningPass(), PMW); |
| 144 | addMachineFunctionPass(Pass: X86FlagsCopyLoweringPass(), PMW); |
| 145 | addMachineFunctionPass(Pass: X86DynAllocaExpanderPass(), PMW); |
| 146 | |
| 147 | if (getOptLevel() != CodeGenOptLevel::None) |
| 148 | addMachineFunctionPass(Pass: X86PreTileConfigPass(), PMW); |
| 149 | else |
| 150 | addMachineFunctionPass(Pass: X86FastPreTileConfigPass(), PMW); |
| 151 | } |
| 152 | |
| 153 | void X86CodeGenPassBuilder::addPostRegAlloc(PassManagerWrapper &PMW) const { |
| 154 | addMachineFunctionPass(Pass: X86LowerTileCopyPass(), PMW); |
| 155 | addMachineFunctionPass(Pass: X86FPStackifierPass(), PMW); |
| 156 | // When -O0 is enabled, the Load Value Injection Hardening pass will fall back |
| 157 | // to using the Speculative Execution Side Effect Suppression pass for |
| 158 | // mitigation. This is to prevent slow downs due to |
| 159 | // analyses needed by the LVIHardening pass when compiling at -O0. |
| 160 | if (getOptLevel() != CodeGenOptLevel::None) { |
| 161 | // TODO(nigham): Add X86LoadValueInjectionLoadHardeningPass here once |
| 162 | // it has been ported. |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void X86CodeGenPassBuilder::addPreSched2(PassManagerWrapper &PMW) const { |
| 167 | addMachineFunctionPass(Pass: X86ExpandPseudoPass(), PMW); |
| 168 | // TODO(boomanaiden154): Add KCFGPass here once it has been ported. |
| 169 | } |
| 170 | |
| 171 | void X86CodeGenPassBuilder::addPreEmitPass(PassManagerWrapper &PMW) const { |
| 172 | if (getOptLevel() != CodeGenOptLevel::None) { |
| 173 | // TODO(boomanaiden154): Add X86ExecutionDomainFixPass here once it has |
| 174 | // been ported. |
| 175 | addMachineFunctionPass(Pass: BreakFalseDepsPass(), PMW); |
| 176 | } |
| 177 | |
| 178 | // TODO(boomanaiden154): Add X86IndirectBranchTrackingPass here once it has |
| 179 | // been ported. |
| 180 | // TODO(boomanaiden154): Add X86IssueVZeroUpperPass here once it has been |
| 181 | // ported. |
| 182 | |
| 183 | if (getOptLevel() != CodeGenOptLevel::None) { |
| 184 | addMachineFunctionPass(Pass: X86FixupBWInstsPass(), PMW); |
| 185 | // TODO(boomanaiden154): Add X86PadShortFunctionsPass here once it has been |
| 186 | // ported. |
| 187 | addMachineFunctionPass(Pass: X86FixupLEAsPass(), PMW); |
| 188 | addMachineFunctionPass(Pass: X86FixupInstTuningPass(), PMW); |
| 189 | addMachineFunctionPass(Pass: X86FixupVectorConstantsPass(), PMW); |
| 190 | } |
| 191 | addMachineFunctionPass(Pass: X86CompressEVEXPass(), PMW); |
| 192 | // TODO(boomanaiden154): Add InsertX86WaitPass here once it has been ported. |
| 193 | } |
| 194 | |
| 195 | void X86CodeGenPassBuilder::addPreEmitPass2(PassManagerWrapper &PMW) const { |
| 196 | const Triple &TT = TM.getTargetTriple(); |
| 197 | const MCAsmInfo *MAI = TM.getMCAsmInfo(); |
| 198 | |
| 199 | // The X86 Speculative Execution Pass must run after all control |
| 200 | // flow graph modifying passes. As a result it was listed to run right before |
| 201 | // the X86 Retpoline Thunks pass. The reason it must run after control flow |
| 202 | // graph modifications is that the model of LFENCE in LLVM has to be updated |
| 203 | // (FIXME: https://bugs.llvm.org/show_bug.cgi?id=45167). Currently the |
| 204 | // placement of this pass was hand checked to ensure that the subsequent |
| 205 | // passes don't move the code around the LFENCEs in a way that will hurt the |
| 206 | // correctness of this pass. This placement has been shown to work based on |
| 207 | // hand inspection of the codegen output. |
| 208 | addMachineFunctionPass(Pass: X86SpeculativeExecutionSideEffectSuppressionPass(), |
| 209 | PMW); |
| 210 | // TODO(boomanaiden154): Add X86IndirectThunksPass here |
| 211 | // once it has been ported. |
| 212 | addMachineFunctionPass(Pass: X86ReturnThunksPass(), PMW); |
| 213 | |
| 214 | // Insert extra int3 instructions after trailing call instructions to avoid |
| 215 | // issues in the unwinder. |
| 216 | if (TT.isOSWindows() && TT.isX86_64()) |
| 217 | addMachineFunctionPass(Pass: X86AvoidTrailingCallPass(), PMW); |
| 218 | |
| 219 | // Verify basic block incoming and outgoing cfa offset and register values and |
| 220 | // correct CFA calculation rule where needed by inserting appropriate CFI |
| 221 | // instructions. |
| 222 | if (!TT.isOSDarwin() && |
| 223 | (!TT.isOSWindows() || |
| 224 | MAI->getExceptionHandlingType() == ExceptionHandling::DwarfCFI)) { |
| 225 | // TODO(boomanaiden154): Add CFInstrInserterPass here when it has been |
| 226 | // ported. |
| 227 | } |
| 228 | |
| 229 | if (TT.isOSWindows()) { |
| 230 | // Identify valid longjmp targets for Windows Control Flow Guard. |
| 231 | // TODO(boomanaiden154): Add CFGuardLongjmpPass here when it has been |
| 232 | // ported. |
| 233 | // Identify valid eh continuation targets for Windows EHCont Guard. |
| 234 | // TODO(boomanaiden154): Add EHContGuardTargetsPass when it has been |
| 235 | // ported. |
| 236 | } |
| 237 | |
| 238 | addMachineFunctionPass(Pass: X86LoadValueInjectionRetHardeningPass(), PMW); |
| 239 | |
| 240 | // Insert pseudo probe annotation for callsite profiling |
| 241 | // TODO(boomanaiden154): Add PseudoProberInserterPass here once it has been |
| 242 | // ported. |
| 243 | |
| 244 | // KCFI indirect call checks are lowered to a bundle, and on Darwin platforms, |
| 245 | // also CALL_RVMARKER. |
| 246 | // TODO(boomanaiden154): Add UnpackMachineBundlesPass here once it has been |
| 247 | // ported. |
| 248 | |
| 249 | // Analyzes and emits pseudos to support Win x64 Unwind V2. This pass must run |
| 250 | // after all real instructions have been added to the epilog. |
| 251 | if (TT.isOSWindows() && TT.isX86_64()) { |
| 252 | addMachineFunctionPass(Pass: X86WinEHUnwindV2Pass(), PMW); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void X86CodeGenPassBuilder::addAsmPrinter(PassManagerWrapper &PMW, |
| 257 | CreateMCStreamer) const { |
| 258 | // TODO: Add AsmPrinter. |
| 259 | } |
| 260 | |
| 261 | } // namespace |
| 262 | |
| 263 | void X86TargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) { |
| 264 | #define GET_PASS_REGISTRY "X86PassRegistry.def" |
| 265 | #include "llvm/Passes/TargetPassRegistry.inc" |
| 266 | } |
| 267 | |
| 268 | Error X86TargetMachine::buildCodeGenPipeline( |
| 269 | ModulePassManager &MPM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, |
| 270 | CodeGenFileType FileType, const CGPassBuilderOption &Opt, |
| 271 | PassInstrumentationCallbacks *PIC) { |
| 272 | auto CGPB = X86CodeGenPassBuilder(*this, Opt, PIC); |
| 273 | return CGPB.buildPipeline(MPM, Out, DwoOut, FileType); |
| 274 | } |
| 275 | |