1//===-- ARMTargetMachine.cpp - Define TargetMachine for ARM ---------------===//
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//
10//===----------------------------------------------------------------------===//
11
12#include "ARMTargetMachine.h"
13#include "ARM.h"
14#include "ARMLatencyMutations.h"
15#include "ARMMachineFunctionInfo.h"
16#include "ARMMacroFusion.h"
17#include "ARMSubtarget.h"
18#include "ARMTargetObjectFile.h"
19#include "ARMTargetTransformInfo.h"
20#include "MCTargetDesc/ARMMCTargetDesc.h"
21#include "TargetInfo/ARMTargetInfo.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Analysis/TargetTransformInfo.h"
24#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
25#include "llvm/CodeGen/GlobalISel/CallLowering.h"
26#include "llvm/CodeGen/GlobalISel/IRTranslator.h"
27#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
28#include "llvm/CodeGen/GlobalISel/Legalizer.h"
29#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
30#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
31#include "llvm/CodeGen/MIRParser/MIParser.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineScheduler.h"
34#include "llvm/CodeGen/Passes.h"
35#include "llvm/CodeGen/TargetPassConfig.h"
36#include "llvm/IR/Attributes.h"
37#include "llvm/IR/CallingConv.h"
38#include "llvm/IR/DataLayout.h"
39#include "llvm/IR/DiagnosticInfo.h"
40#include "llvm/IR/Function.h"
41#include "llvm/IR/InstIterator.h"
42#include "llvm/IR/InstrTypes.h"
43#include "llvm/IR/Module.h"
44#include "llvm/MC/TargetRegistry.h"
45#include "llvm/Pass.h"
46#include "llvm/Passes/PassBuilder.h"
47#include "llvm/Support/CodeGen.h"
48#include "llvm/Support/CommandLine.h"
49#include "llvm/Support/Compiler.h"
50#include "llvm/Support/ErrorHandling.h"
51#include "llvm/Target/TargetLoweringObjectFile.h"
52#include "llvm/Target/TargetOptions.h"
53#include "llvm/TargetParser/ARMTargetParser.h"
54#include "llvm/TargetParser/Triple.h"
55#include "llvm/Transforms/CFGuard.h"
56#include "llvm/Transforms/IPO.h"
57#include "llvm/Transforms/Scalar.h"
58#include <cassert>
59#include <memory>
60#include <optional>
61#include <string>
62
63using namespace llvm;
64
65static cl::opt<bool>
66DisableA15SDOptimization("disable-a15-sd-optimization", cl::Hidden,
67 cl::desc("Inhibit optimization of S->D register accesses on A15"),
68 cl::init(Val: false));
69
70static cl::opt<bool>
71EnableAtomicTidy("arm-atomic-cfg-tidy", cl::Hidden,
72 cl::desc("Run SimplifyCFG after expanding atomic operations"
73 " to make use of cmpxchg flow-based information"),
74 cl::init(Val: true));
75
76static cl::opt<bool>
77EnableARMLoadStoreOpt("arm-load-store-opt", cl::Hidden,
78 cl::desc("Enable ARM load/store optimization pass"),
79 cl::init(Val: true));
80
81// FIXME: Unify control over GlobalMerge.
82static cl::opt<cl::boolOrDefault>
83EnableGlobalMerge("arm-global-merge", cl::Hidden,
84 cl::desc("Enable the global merge pass"));
85
86extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeARMTarget() {
87 // Register the target.
88 RegisterTargetMachine<ARMLETargetMachine> X(getTheARMLETarget());
89 RegisterTargetMachine<ARMLETargetMachine> A(getTheThumbLETarget());
90 RegisterTargetMachine<ARMBETargetMachine> Y(getTheARMBETarget());
91 RegisterTargetMachine<ARMBETargetMachine> B(getTheThumbBETarget());
92
93 PassRegistry &Registry = *PassRegistry::getPassRegistry();
94 initializeGlobalISel(Registry);
95 initializeARMAsmPrinterPass(Registry);
96 initializeARMLoadStoreOptLegacyPass(Registry);
97 initializeARMPreAllocLoadStoreOptLegacyPass(Registry);
98 initializeARMParallelDSPPass(Registry);
99 initializeARMBranchTargetsPass(Registry);
100 initializeARMConstantIslandsPass(Registry);
101 initializeARMExpandPseudoPass(Registry);
102 initializeThumb2SizeReducePass(Registry);
103 initializeMVEVPTBlockPass(Registry);
104 initializeThumb2ITBlockPass(Registry);
105 initializeMVETPAndVPTOptimisationsPass(Registry);
106 initializeMVETailPredicationPass(Registry);
107 initializeARMLowOverheadLoopsPass(Registry);
108 initializeARMBlockPlacementPass(Registry);
109 initializeMVEGatherScatterLoweringPass(Registry);
110 initializeARMSLSHardeningPass(Registry);
111 initializeMVELaneInterleavingPass(Registry);
112 initializeARMFixCortexA57AES1742098Pass(Registry);
113 initializeARMDAGToDAGISelLegacyPass(Registry);
114 initializeMachineKCFILegacyPass(Registry);
115}
116
117static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
118 if (TT.isOSBinFormatMachO())
119 return std::make_unique<TargetLoweringObjectFileMachO>();
120 if (TT.isOSWindows())
121 return std::make_unique<TargetLoweringObjectFileCOFF>();
122 return std::make_unique<ARMElfTargetObjectFile>();
123}
124
125static Reloc::Model getEffectiveRelocModel(const Triple &TT,
126 std::optional<Reloc::Model> RM) {
127 if (!RM)
128 // Default relocation model on Darwin is PIC.
129 return TT.isOSBinFormatMachO() ? Reloc::PIC_ : Reloc::Static;
130
131 if (*RM == Reloc::ROPI || *RM == Reloc::RWPI || *RM == Reloc::ROPI_RWPI)
132 assert(TT.isOSBinFormatELF() &&
133 "ROPI/RWPI currently only supported for ELF");
134
135 // DynamicNoPIC is only used on darwin.
136 if (*RM == Reloc::DynamicNoPIC && !TT.isOSDarwin())
137 return Reloc::Static;
138
139 return *RM;
140}
141
142/// Create an ARM architecture model.
143///
144ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
145 StringRef CPU, StringRef FS,
146 const TargetOptions &Options,
147 std::optional<Reloc::Model> RM,
148 std::optional<CodeModel::Model> CM,
149 CodeGenOptLevel OL)
150 : CodeGenTargetMachineImpl(T, TT, CPU, FS, Options,
151 getEffectiveRelocModel(TT, RM),
152 getEffectiveCodeModel(CM, Default: CodeModel::Small), OL),
153 TLOF(createTLOF(TT: getTargetTriple())), isLittle(TT.isLittleEndian()) {
154
155 if (TT.isOSBinFormatMachO()) {
156 this->Options.TrapUnreachable = true;
157 this->Options.NoTrapAfterNoreturn = true;
158 }
159
160 // ARM supports the debug entry values.
161 setSupportsDebugEntryValues(true);
162
163 initAsmInfo();
164
165 LLT::setUseExtended(true);
166
167 // ARM supports the MachineOutliner.
168 setMachineOutliner(true);
169 setSupportsDefaultOutlining(true);
170}
171
172ARMBaseTargetMachine::~ARMBaseTargetMachine() = default;
173
174MachineFunctionInfo *ARMBaseTargetMachine::createMachineFunctionInfo(
175 BumpPtrAllocator &Allocator, const Function &F,
176 const TargetSubtargetInfo *STI) const {
177 const auto *ARMSTI = static_cast<const ARMSubtarget *>(STI);
178 if (!ARMSTI->hasFPRegs() || ARMSTI->isThumb1Only() ||
179 ARMSTI->useSoftFloat()) {
180 const StringRef FPRegsUnavailableMsg =
181 ", but floating-point registers are unavailable";
182 const ARMTargetLowering *TLI = ARMSTI->getTargetLowering();
183
184 if (TLI->getEffectiveCallingConv(CC: F.getCallingConv(), isVarArg: F.isVarArg()) ==
185 CallingConv::ARM_AAPCS_VFP) {
186 F.getContext().diagnose(DI: DiagnosticInfoUnsupported(
187 F, Twine("calling convention is hard-float") + FPRegsUnavailableMsg,
188 DiagnosticLocation(F.getSubprogram())));
189 } else {
190 for (const Instruction &I : instructions(F)) {
191 const auto *CB = dyn_cast<CallBase>(Val: &I);
192 if (!CB || CB->isInlineAsm() ||
193 (CB->getCalledFunction() && CB->getCalledFunction()->isIntrinsic()))
194 continue;
195 if (TLI->getEffectiveCallingConv(CC: CB->getCallingConv(),
196 isVarArg: CB->getFunctionType()->isVarArg()) ==
197 CallingConv::ARM_AAPCS_VFP) {
198 const Function *Callee = CB->getCalledFunction();
199 F.getContext().diagnose(DI: DiagnosticInfoUnsupported(
200 F,
201 (Callee ? Twine("'") + F.getName() + "' calls '" +
202 Callee->getName() + "', which"
203 : Twine("'") + F.getName() +
204 "' makes an indirect call that") +
205 " expects a hard-float calling convention" +
206 FPRegsUnavailableMsg,
207 CB->getDebugLoc()));
208 }
209 }
210 }
211 }
212 return ARMFunctionInfo::create<ARMFunctionInfo>(Allocator, F, STI: ARMSTI);
213}
214
215FloatABI::ABIType ARMBaseTargetMachine::getFloatABI(const Module &M) const {
216 // An explicit "float-abi" module flag always wins, even for AAPCS16.
217 if (auto *Val = dyn_cast_or_null<MDString>(Val: M.getModuleFlag(Key: "float-abi")))
218 return *FloatABI::parseABIType(S: Val->getString());
219 return M.getTargetTriple().getDefaultFloatABI(ABIName: getTargetABIName(M));
220}
221
222ARM::ARMABI ARMBaseTargetMachine::getEffectiveABI(const Module &M) const {
223 // Consistency of "target-abi" and -target-abi is validated elsewhere.
224 if (const auto *MD = cast_or_null<MDString>(Val: M.getModuleFlag(Key: "target-abi")))
225 return ARM::computeTargetABI(TT: TargetTriple, ABIName: MD->getString());
226 return ARM::computeTargetABI(TT: TargetTriple, ABIName: Options.MCOptions.getABIName());
227}
228
229const ARMSubtarget *
230ARMBaseTargetMachine::getSubtargetImpl(const Function &F) const {
231 AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
232 const Module *M = F.getParent();
233 if (const ARMSubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M))
234 return ST;
235
236 SubtargetAttrs Attrs;
237 Attrs.CPU = FnAttrs.getAttribute(Kind: "target-cpu");
238 Attrs.FS = FnAttrs.getAttribute(Kind: "target-features");
239 Attrs.SoftFloat = FnAttrs.getAttribute(Kind: "use-soft-float");
240 Attrs.MinSize = FnAttrs.getAttribute(Kind: Attribute::MinSize);
241 Attrs.DenormalFPEnv = FnAttrs.getAttribute(Kind: Attribute::DenormalFPEnv);
242 Attrs.FloatABI = getFloatABI(M: *M);
243 Attrs.ABI = getEffectiveABI(M: *M);
244
245 if (const ARMSubtarget *ST = LastSubtarget.lookup(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs))
246 return ST;
247
248 std::string CPU =
249 Attrs.CPU.isValid() ? Attrs.CPU.getValueAsString().str() : TargetCPU;
250 std::string FS =
251 Attrs.FS.isValid() ? Attrs.FS.getValueAsString().str() : TargetFS;
252
253 // FIXME: This is related to the code below to reset the target options,
254 // we need to know whether or not the soft float flag is set on the
255 // function before we can generate a subtarget. We also need to use
256 // it as a key for the subtarget since that can be the only difference
257 // between two functions.
258 bool SoftFloat = Attrs.SoftFloat.getValueAsBool();
259 // If the soft float attribute is set on the function turn on the soft float
260 // subtarget feature.
261 if (SoftFloat)
262 FS += FS.empty() ? "+soft-float" : ",+soft-float";
263
264 // Use the optminsize to identify the subtarget, but don't use it in the
265 // feature string.
266 std::string Key = CPU + FS;
267 if (Attrs.MinSize.isValid())
268 Key += "+minsize";
269
270 DenormalMode DM = F.getDenormalFPEnv().DefaultMode;
271 if (DM != DenormalMode::getIEEE())
272 Key += "denormal-fp-math=" + DM.str();
273
274 // It is legal to have FloatABI::Hard for targets with SIMD registers
275 // but no floating-point hardware (mve+nofp).
276 Key +=
277 Attrs.FloatABI == FloatABI::Hard ? "+hard-float-abi" : "+soft-float-abi";
278
279 Key += "+abi=" + std::to_string(val: (int)Attrs.ABI);
280
281 auto &I = SubtargetMap[Key];
282 if (!I) {
283 I = std::make_unique<ARMSubtarget>(args: TargetTriple, args&: CPU, args&: FS, args: *this, args: isLittle,
284 args&: Attrs.FloatABI, args&: Attrs.ABI,
285 args: Attrs.MinSize.isValid(), args&: DM);
286
287 if (!I->isThumb() && !I->hasARMOps())
288 F.getContext().emitError(ErrorStr: "Function '" + F.getName() + "' uses ARM "
289 "instructions, but the target does not support ARM mode execution.");
290 }
291
292 LastSubtarget.set(NewFnAttrs: FnAttrs, NewM: M, NewAttrs: Attrs, NewSubtarget: I.get());
293 return I.get();
294}
295
296TargetTransformInfo
297ARMBaseTargetMachine::getTargetTransformInfo(const Function &F) const {
298 return TargetTransformInfo(std::make_unique<ARMTTIImpl>(args: this, args: F));
299}
300
301ScheduleDAGInstrs *
302ARMBaseTargetMachine::createMachineScheduler(MachineSchedContext *C) const {
303 ScheduleDAGMILive *DAG = createSchedLive(C);
304 // add DAG Mutations here.
305 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
306 if (ST.hasFusion())
307 DAG->addMutation(Mutation: createARMMacroFusionDAGMutation());
308 return DAG;
309}
310
311ScheduleDAGInstrs *
312ARMBaseTargetMachine::createPostMachineScheduler(MachineSchedContext *C) const {
313 ScheduleDAGMI *DAG = createSchedPostRA(C);
314 // add DAG Mutations here.
315 const ARMSubtarget &ST = C->MF->getSubtarget<ARMSubtarget>();
316 if (ST.hasFusion())
317 DAG->addMutation(Mutation: createARMMacroFusionDAGMutation());
318 if (auto Mutation = createARMLatencyMutations(ST, AA: C->AA))
319 DAG->addMutation(Mutation: std::move(Mutation));
320 return DAG;
321}
322
323ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
324 StringRef CPU, StringRef FS,
325 const TargetOptions &Options,
326 std::optional<Reloc::Model> RM,
327 std::optional<CodeModel::Model> CM,
328 CodeGenOptLevel OL, bool JIT)
329 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {}
330
331ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
332 StringRef CPU, StringRef FS,
333 const TargetOptions &Options,
334 std::optional<Reloc::Model> RM,
335 std::optional<CodeModel::Model> CM,
336 CodeGenOptLevel OL, bool JIT)
337 : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {}
338
339namespace {
340
341/// ARM Code Generator Pass Configuration Options.
342class ARMPassConfig : public TargetPassConfig {
343public:
344 ARMPassConfig(ARMBaseTargetMachine &TM, PassManagerBase &PM)
345 : TargetPassConfig(TM, PM) {}
346
347 ARMBaseTargetMachine &getARMTargetMachine() const {
348 return getTM<ARMBaseTargetMachine>();
349 }
350
351 void addIRPasses() override;
352 void addCodeGenPrepare() override;
353 bool addPreISel() override;
354 bool addInstSelector() override;
355 bool addIRTranslator() override;
356 bool addLegalizeMachineIR() override;
357 bool addRegBankSelect() override;
358 bool addGlobalInstructionSelect() override;
359 void addPreRegAlloc() override;
360 bool isMachinePipelinerEnabled() const override {
361 return getOptLevel() == CodeGenOptLevel::Aggressive;
362 }
363 void addPreSched2() override;
364 void addPreEmitPass() override;
365 void addPreEmitPass2() override;
366
367 std::unique_ptr<CSEConfigBase> getCSEConfig() const override;
368};
369
370} // end anonymous namespace
371
372void ARMBaseTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
373#define GET_PASS_REGISTRY "ARMPassRegistry.def"
374#include "llvm/Passes/TargetPassRegistry.inc"
375}
376
377TargetPassConfig *ARMBaseTargetMachine::createPassConfig(PassManagerBase &PM) {
378 return new ARMPassConfig(*this, PM);
379}
380
381std::unique_ptr<CSEConfigBase> ARMPassConfig::getCSEConfig() const {
382 return getStandardCSEConfigForOpt(Level: TM->getOptLevel());
383}
384
385void ARMPassConfig::addIRPasses() {
386 addPass(P: createAtomicExpandLegacyPass());
387
388 // Cmpxchg instructions are often used with a subsequent comparison to
389 // determine whether it succeeded. We can exploit existing control-flow in
390 // ldrex/strex loops to simplify this, but it needs tidying up.
391 if (TM->getOptLevel() != CodeGenOptLevel::None && EnableAtomicTidy)
392 addPass(P: createCFGSimplificationPass(
393 Options: SimplifyCFGOptions().hoistCommonInsts(B: true).sinkCommonInsts(B: true),
394 Ftor: [this](const Function &F) {
395 const auto &ST = this->TM->getSubtarget<ARMSubtarget>(F);
396 return ST.hasAnyDataBarrier() && !ST.isThumb1Only();
397 }));
398
399 addPass(P: createMVEGatherScatterLoweringPass());
400 addPass(P: createMVELaneInterleavingPass());
401
402 TargetPassConfig::addIRPasses();
403
404 // Run the parallel DSP pass.
405 if (getOptLevel() == CodeGenOptLevel::Aggressive)
406 addPass(P: createARMParallelDSPPass());
407
408 // Match complex arithmetic patterns
409 if (TM->getOptLevel() >= CodeGenOptLevel::Default)
410 addPass(P: createComplexDeinterleavingPass(TM));
411
412 // Match interleaved memory accesses to ldN/stN intrinsics.
413 if (TM->getOptLevel() != CodeGenOptLevel::None)
414 addPass(P: createInterleavedAccessPass());
415
416 // Add Control Flow Guard checks.
417 if (TM->getTargetTriple().isOSWindows())
418 addPass(P: createCFGuardPass());
419
420 if (TM->Options.JMCInstrument)
421 addPass(P: createJMCInstrumenterPass());
422}
423
424void ARMPassConfig::addCodeGenPrepare() {
425 if (getOptLevel() != CodeGenOptLevel::None)
426 addPass(P: createTypePromotionLegacyPass());
427 TargetPassConfig::addCodeGenPrepare();
428}
429
430bool ARMPassConfig::addPreISel() {
431 if ((TM->getOptLevel() != CodeGenOptLevel::None &&
432 EnableGlobalMerge == cl::boolOrDefault::BOU_UNSET) ||
433 EnableGlobalMerge == cl::boolOrDefault::BOU_TRUE) {
434 // FIXME: This is using the thumb1 only constant value for
435 // maximal global offset for merging globals. We may want
436 // to look into using the old value for non-thumb1 code of
437 // 4095 based on the TargetMachine, but this starts to become
438 // tricky when doing code gen per function.
439 bool OnlyOptimizeForSize =
440 (TM->getOptLevel() < CodeGenOptLevel::Aggressive) &&
441 (EnableGlobalMerge == cl::boolOrDefault::BOU_UNSET);
442 // Merging of extern globals is enabled by default on non-Mach-O as we
443 // expect it to be generally either beneficial or harmless. On Mach-O it
444 // is disabled as we emit the .subsections_via_symbols directive which
445 // means that merging extern globals is not safe.
446 bool MergeExternalByDefault = !TM->getTargetTriple().isOSBinFormatMachO();
447 addPass(P: createGlobalMergePass(TM, MaximalOffset: 127, OnlyOptimizeForSize,
448 MergeExternalByDefault));
449 }
450
451 if (TM->getOptLevel() != CodeGenOptLevel::None) {
452 addPass(P: createHardwareLoopsLegacyPass());
453 addPass(P: createMVETailPredicationPass());
454 // FIXME: IR passes can delete address-taken basic blocks, deleting
455 // corresponding blockaddresses. ARMConstantPoolConstant holds references to
456 // address-taken basic blocks which can be invalidated if the function
457 // containing the blockaddress has already been codegen'd and the basic
458 // block is removed. Work around this by forcing all IR passes to run before
459 // any ISel takes place. We should have a more principled way of handling
460 // this. See D99707 for more details.
461 addPass(P: createBarrierNoopPass());
462 }
463
464 return false;
465}
466
467bool ARMPassConfig::addInstSelector() {
468 addPass(P: createARMISelDag(TM&: getARMTargetMachine(), OptLevel: getOptLevel()));
469 return false;
470}
471
472bool ARMPassConfig::addIRTranslator() {
473 addPass(P: new IRTranslatorLegacy(getOptLevel()));
474 return false;
475}
476
477bool ARMPassConfig::addLegalizeMachineIR() {
478 addPass(P: new LegalizerLegacy());
479 return false;
480}
481
482bool ARMPassConfig::addRegBankSelect() {
483 addPass(P: new RegBankSelectLegacy());
484 return false;
485}
486
487bool ARMPassConfig::addGlobalInstructionSelect() {
488 addPass(P: new InstructionSelectLegacy(getOptLevel()));
489 return false;
490}
491
492void ARMPassConfig::addPreRegAlloc() {
493 if (getOptLevel() != CodeGenOptLevel::None) {
494 addPass(P: createMVETPAndVPTOptimisationsPass());
495
496 addPass(P: createMLxExpansionPass());
497
498 if (EnableARMLoadStoreOpt)
499 addPass(P: createARMLoadStoreOptLegacyPass(/* pre-register alloc */ PreAlloc: true));
500
501 if (!DisableA15SDOptimization)
502 addPass(P: createA15SDOptimizerPass());
503 }
504}
505
506void ARMPassConfig::addPreSched2() {
507 if (getOptLevel() != CodeGenOptLevel::None) {
508 if (EnableARMLoadStoreOpt)
509 addPass(P: createARMLoadStoreOptLegacyPass());
510
511 addPass(P: createExecutionDomainFixLegacyPass());
512 addPass(P: createBreakFalseDepsLegacyPass());
513 }
514
515 // Expand some pseudo instructions into multiple instructions to allow
516 // proper scheduling.
517 addPass(P: createARMExpandPseudoPass());
518
519 // Emit KCFI checks for indirect calls.
520 addPass(P: createKCFIPass());
521
522 if (getOptLevel() != CodeGenOptLevel::None) {
523 // When optimising for size, always run the Thumb2SizeReduction pass before
524 // IfConversion. Otherwise, check whether IT blocks are restricted
525 // (e.g. in v8, IfConversion depends on Thumb instruction widths)
526 addPass(P: createThumb2SizeReductionPass(Ftor: [this](const Function &F) {
527 return this->TM->getSubtarget<ARMSubtarget>(F).hasMinSize() ||
528 this->TM->getSubtarget<ARMSubtarget>(F).restrictIT();
529 }));
530
531 addPass(P: createIfConverterLegacyPass(Ftor: [](const MachineFunction &MF) {
532 return !MF.getSubtarget<ARMSubtarget>().isThumb1Only();
533 }));
534 }
535 addPass(P: createThumb2ITBlockPass());
536
537 // Add both scheduling passes to give the subtarget an opportunity to pick
538 // between them.
539 if (getOptLevel() != CodeGenOptLevel::None) {
540 addPass(PassID: &PostMachineSchedulerID);
541 addPass(PassID: &PostRASchedulerID);
542 }
543
544 addPass(P: createMVEVPTBlockPass());
545 addPass(P: createARMIndirectThunks());
546 addPass(P: createARMSLSHardeningPass());
547}
548
549void ARMPassConfig::addPreEmitPass() {
550 addPass(P: createThumb2SizeReductionPass());
551
552 // Unpack bundles for:
553 // - Thumb2: Constant island pass requires unbundled instructions
554 // - KCFI: KCFI_CHECK pseudo instructions need to be unbundled for AsmPrinter
555 addPass(P: createUnpackMachineBundlesLegacy(Ftor: [](const MachineFunction &MF) {
556 return MF.getSubtarget<ARMSubtarget>().isThumb2() ||
557 MF.getFunction().getParent()->getModuleFlag(Key: "kcfi");
558 }));
559
560 // Don't optimize barriers or block placement at -O0.
561 if (getOptLevel() != CodeGenOptLevel::None) {
562 addPass(P: createARMBlockPlacementPass());
563 addPass(P: createARMOptimizeBarriersPass());
564 }
565}
566
567void ARMPassConfig::addPreEmitPass2() {
568
569 // Inserts fixup instructions before unsafe AES operations. Instructions may
570 // be inserted at the start of blocks and at within blocks so this pass has to
571 // come before those below.
572 addPass(P: createARMFixCortexA57AES1742098Pass());
573 // Inserts BTIs at the start of functions and indirectly-called basic blocks,
574 // so passes cannot add to the start of basic blocks once this has run.
575 addPass(P: createARMBranchTargetsPass());
576 // Inserts Constant Islands. Block sizes cannot be increased after this point,
577 // as this may push the branch ranges and load offsets of accessing constant
578 // pools out of range..
579 addPass(P: createARMConstantIslandPass());
580 // Finalises Low-Overhead Loops. This replaces pseudo instructions with real
581 // instructions, but the pseudos all have conservative sizes so that block
582 // sizes will only be decreased by this pass.
583 addPass(P: createARMLowOverheadLoopsPass());
584
585 if (TM->getTargetTriple().isOSWindows()) {
586 // Identify valid longjmp targets for Windows Control Flow Guard.
587 addPass(P: createCFGuardLongjmpLegacyPass());
588 // Identify valid eh continuation targets for Windows EHCont Guard.
589 addPass(P: createEHContGuardTargetsLegacy());
590 }
591}
592
593yaml::MachineFunctionInfo *
594ARMBaseTargetMachine::createDefaultFuncInfoYAML() const {
595 return new yaml::ARMFunctionInfo();
596}
597
598yaml::MachineFunctionInfo *
599ARMBaseTargetMachine::convertFuncInfoToYAML(const MachineFunction &MF) const {
600 const auto *MFI = MF.getInfo<ARMFunctionInfo>();
601 return new yaml::ARMFunctionInfo(*MFI);
602}
603
604bool ARMBaseTargetMachine::parseMachineFunctionInfo(
605 const yaml::MachineFunctionInfo &MFI, PerFunctionMIParsingState &PFS,
606 SMDiagnostic &Error, SMRange &SourceRange) const {
607 const auto &YamlMFI = static_cast<const yaml::ARMFunctionInfo &>(MFI);
608 MachineFunction &MF = PFS.MF;
609 MF.getInfo<ARMFunctionInfo>()->initializeBaseYamlFields(YamlMFI);
610 return false;
611}
612
613void ARMBaseTargetMachine::reset() {
614 SubtargetMap.clear();
615 LastSubtarget.clear();
616}
617