//===-- W65816TargetMachine.h - Define TargetMachine for W65816 -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file declares the W65816 specific subclass of TargetMachine.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_TARGET_W65816_W65816TARGETMACHINE_H
#define LLVM_LIB_TARGET_W65816_W65816TARGETMACHINE_H

#include "W65816Subtarget.h"
#include "llvm/CodeGen/CodeGenTargetMachineImpl.h"
#include <optional>

namespace llvm {

class PassBuilder;
class StringRef;

class W65816TargetMachine : public CodeGenTargetMachineImpl {
  std::unique_ptr<TargetLoweringObjectFile> TLOF;
  W65816Subtarget Subtarget;

public:
  W65816TargetMachine(const Target &T, const Triple &TT, StringRef CPU,
                      StringRef FS, const TargetOptions &Options,
                      std::optional<Reloc::Model> RM,
                      std::optional<CodeModel::Model> CM, CodeGenOptLevel OL,
                      bool JIT);
  ~W65816TargetMachine() override;

  const W65816Subtarget *getSubtargetImpl(const Function &F) const override {
    return &Subtarget;
  }

  TargetPassConfig *createPassConfig(PassManagerBase &PM) override;

  TargetLoweringObjectFile *getObjFileLowering() const override {
    return TLOF.get();
  }

  MachineFunctionInfo *
  createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
                            const TargetSubtargetInfo *STI) const override;

  // Wire W65816-specific new-PM passes into the optimization pipeline.
  // Currently registers W65816Layer2StampPass at PipelineStart (so the
  // "w65816-layer2" function attribute lands in IR before bitcode is
  // written for LTO) and exposes both the stamp and gate passes for
  // explicit invocation via `opt -passes=...`.  Phase 1.12 of
  // GAP_CLOSURE_PLAN.md.
  void registerPassBuilderCallbacks(PassBuilder &PB) override;

  // Provide the minimal W65816 TTI (Phase 5.2 of GAP_CLOSURE_PLAN.md)
  // so the LTO inliner sees i32 / float as more expensive than the
  // LLVM stock cost model (which assumes a 32-bit, FPU-equipped host).
  // See W65816TargetTransformInfo.h for cost multipliers + the bsearch
  // regression history that led to the conservative numbers.
  TargetTransformInfo
  getTargetTransformInfo(const Function &F) const override;
};

// Implemented in W65816Layer2Gate.cpp.  Registers W65816Layer2StampPass
// + W65816Layer2GatePass with the new pass manager.
void registerW65816Layer2GatePasses(PassBuilder &PB);

} // namespace llvm

#endif
