[InstCombine] Signed saturation patterns
[llvm-complete.git] / lib / Target / BPF / BPFTargetMachine.cpp
blob0c4f2c74e7a4a08c8f3a06c0a29d3beb7e0bc53b
1 //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
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 BPF target spec.
11 //===----------------------------------------------------------------------===//
13 #include "BPFTargetMachine.h"
14 #include "BPF.h"
15 #include "MCTargetDesc/BPFMCAsmInfo.h"
16 #include "TargetInfo/BPFTargetInfo.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
19 #include "llvm/CodeGen/TargetPassConfig.h"
20 #include "llvm/IR/LegacyPassManager.h"
21 #include "llvm/Support/FormattedStream.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/Target/TargetOptions.h"
24 using namespace llvm;
26 static cl::
27 opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
28 cl::desc("Disable machine peepholes for BPF"));
30 extern "C" void LLVMInitializeBPFTarget() {
31 // Register the target.
32 RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget());
33 RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget());
34 RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
36 PassRegistry &PR = *PassRegistry::getPassRegistry();
37 initializeBPFAbstractMemberAccessPass(PR);
38 initializeBPFMIPeepholePass(PR);
39 initializeBPFMIPeepholeTruncElimPass(PR);
42 // DataLayout: little or big endian
43 static std::string computeDataLayout(const Triple &TT) {
44 if (TT.getArch() == Triple::bpfeb)
45 return "E-m:e-p:64:64-i64:64-n32:64-S128";
46 else
47 return "e-m:e-p:64:64-i64:64-n32:64-S128";
50 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
51 if (!RM.hasValue())
52 return Reloc::PIC_;
53 return *RM;
56 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
57 StringRef CPU, StringRef FS,
58 const TargetOptions &Options,
59 Optional<Reloc::Model> RM,
60 Optional<CodeModel::Model> CM,
61 CodeGenOpt::Level OL, bool JIT)
62 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
63 getEffectiveRelocModel(RM),
64 getEffectiveCodeModel(CM, CodeModel::Small), OL),
65 TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
66 Subtarget(TT, CPU, FS, *this) {
67 initAsmInfo();
69 BPFMCAsmInfo *MAI =
70 static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
71 MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
74 namespace {
75 // BPF Code Generator Pass Configuration Options.
76 class BPFPassConfig : public TargetPassConfig {
77 public:
78 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
79 : TargetPassConfig(TM, PM) {}
81 BPFTargetMachine &getBPFTargetMachine() const {
82 return getTM<BPFTargetMachine>();
85 void addIRPasses() override;
86 bool addInstSelector() override;
87 void addMachineSSAOptimization() override;
88 void addPreEmitPass() override;
92 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
93 return new BPFPassConfig(*this, PM);
96 void BPFPassConfig::addIRPasses() {
98 addPass(createBPFAbstractMemberAccess(&getBPFTargetMachine()));
100 TargetPassConfig::addIRPasses();
103 // Install an instruction selector pass using
104 // the ISelDag to gen BPF code.
105 bool BPFPassConfig::addInstSelector() {
106 addPass(createBPFISelDag(getBPFTargetMachine()));
108 return false;
111 void BPFPassConfig::addMachineSSAOptimization() {
112 addPass(createBPFMISimplifyPatchablePass());
114 // The default implementation must be called first as we want eBPF
115 // Peephole ran at last.
116 TargetPassConfig::addMachineSSAOptimization();
118 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
119 if (!DisableMIPeephole) {
120 if (Subtarget->getHasAlu32())
121 addPass(createBPFMIPeepholePass());
122 addPass(createBPFMIPeepholeTruncElimPass());
126 void BPFPassConfig::addPreEmitPass() {
127 addPass(createBPFMIPreEmitCheckingPass());
128 if (getOptLevel() != CodeGenOpt::None)
129 if (!DisableMIPeephole)
130 addPass(createBPFMIPreEmitPeepholePass());