Path: blob/main/contrib/llvm-project/llvm/lib/Target/AVR/AVRTargetMachine.cpp
35266 views
//===-- AVRTargetMachine.cpp - Define TargetMachine for AVR ---------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines the AVR specific subclass of TargetMachine.9//10//===----------------------------------------------------------------------===//1112#include "AVRTargetMachine.h"1314#include "llvm/CodeGen/Passes.h"15#include "llvm/CodeGen/TargetPassConfig.h"16#include "llvm/IR/Module.h"17#include "llvm/MC/TargetRegistry.h"1819#include "AVR.h"20#include "AVRMachineFunctionInfo.h"21#include "AVRTargetObjectFile.h"22#include "MCTargetDesc/AVRMCTargetDesc.h"23#include "TargetInfo/AVRTargetInfo.h"2425#include <optional>2627namespace llvm {2829static const char *AVRDataLayout =30"e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8";3132/// Processes a CPU name.33static StringRef getCPU(StringRef CPU) {34if (CPU.empty() || CPU == "generic") {35return "avr2";36}3738return CPU;39}4041static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {42return RM.value_or(Reloc::Static);43}4445AVRTargetMachine::AVRTargetMachine(const Target &T, const Triple &TT,46StringRef CPU, StringRef FS,47const TargetOptions &Options,48std::optional<Reloc::Model> RM,49std::optional<CodeModel::Model> CM,50CodeGenOptLevel OL, bool JIT)51: LLVMTargetMachine(T, AVRDataLayout, TT, getCPU(CPU), FS, Options,52getEffectiveRelocModel(RM),53getEffectiveCodeModel(CM, CodeModel::Small), OL),54SubTarget(TT, std::string(getCPU(CPU)), std::string(FS), *this) {55this->TLOF = std::make_unique<AVRTargetObjectFile>();56initAsmInfo();57}5859namespace {60/// AVR Code Generator Pass Configuration Options.61class AVRPassConfig : public TargetPassConfig {62public:63AVRPassConfig(AVRTargetMachine &TM, PassManagerBase &PM)64: TargetPassConfig(TM, PM) {}6566AVRTargetMachine &getAVRTargetMachine() const {67return getTM<AVRTargetMachine>();68}6970void addIRPasses() override;71bool addInstSelector() override;72void addPreSched2() override;73void addPreEmitPass() override;74};75} // namespace7677TargetPassConfig *AVRTargetMachine::createPassConfig(PassManagerBase &PM) {78return new AVRPassConfig(*this, PM);79}8081void AVRPassConfig::addIRPasses() {82// Expand instructions like83// %result = shl i32 %n, %amount84// to a loop so that library calls are avoided.85addPass(createAVRShiftExpandPass());8687TargetPassConfig::addIRPasses();88}8990extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRTarget() {91// Register the target.92RegisterTargetMachine<AVRTargetMachine> X(getTheAVRTarget());9394auto &PR = *PassRegistry::getPassRegistry();95initializeAVRExpandPseudoPass(PR);96initializeAVRShiftExpandPass(PR);97initializeAVRDAGToDAGISelLegacyPass(PR);98}99100const AVRSubtarget *AVRTargetMachine::getSubtargetImpl() const {101return &SubTarget;102}103104const AVRSubtarget *AVRTargetMachine::getSubtargetImpl(const Function &) const {105return &SubTarget;106}107108MachineFunctionInfo *AVRTargetMachine::createMachineFunctionInfo(109BumpPtrAllocator &Allocator, const Function &F,110const TargetSubtargetInfo *STI) const {111return AVRMachineFunctionInfo::create<AVRMachineFunctionInfo>(Allocator, F,112STI);113}114115//===----------------------------------------------------------------------===//116// Pass Pipeline Configuration117//===----------------------------------------------------------------------===//118119bool AVRPassConfig::addInstSelector() {120// Install an instruction selector.121addPass(createAVRISelDag(getAVRTargetMachine(), getOptLevel()));122// Create the frame analyzer pass used by the PEI pass.123addPass(createAVRFrameAnalyzerPass());124125return false;126}127128void AVRPassConfig::addPreSched2() {129addPass(createAVRExpandPseudoPass());130}131132void AVRPassConfig::addPreEmitPass() {133// Must run branch selection immediately preceding the asm printer.134addPass(&BranchRelaxationPassID);135}136137} // end of namespace llvm138139140