]> granicus.if.org Git - llvm/commitdiff
[Nios2] Subtarget, basic infrastructure for frame, instructions and registers
authorNikolai Bozhenov <nikolai.bozhenov@intel.com>
Tue, 19 Sep 2017 11:54:29 +0000 (11:54 +0000)
committerNikolai Bozhenov <nikolai.bozhenov@intel.com>
Tue, 19 Sep 2017 11:54:29 +0000 (11:54 +0000)
This is the second minimal patch keeping Nios2 target buildable.
I'm adding subtarget here and other stuff for frame lowering, instruction,
register information methods. I do not add any test cases, as still there
are missing parts like DAG selector and assembly printing. I plan to include
them into the next patch.

Patch by Andrei Grischenko <andrei.l.grischenko@intel.com>

Differential Revision: https://reviews.llvm.org/D37256

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313626 91177308-0d34-0410-b5e6-96231b3b80d8

15 files changed:
lib/Target/Nios2/CMakeLists.txt
lib/Target/Nios2/MCTargetDesc/Nios2MCTargetDesc.cpp
lib/Target/Nios2/MCTargetDesc/Nios2MCTargetDesc.h
lib/Target/Nios2/Nios2.td
lib/Target/Nios2/Nios2FrameLowering.cpp [new file with mode: 0644]
lib/Target/Nios2/Nios2FrameLowering.h [new file with mode: 0644]
lib/Target/Nios2/Nios2InstrInfo.cpp [new file with mode: 0644]
lib/Target/Nios2/Nios2InstrInfo.h [new file with mode: 0644]
lib/Target/Nios2/Nios2RegisterInfo.cpp [new file with mode: 0644]
lib/Target/Nios2/Nios2RegisterInfo.h [new file with mode: 0644]
lib/Target/Nios2/Nios2Schedule.td [new file with mode: 0644]
lib/Target/Nios2/Nios2Subtarget.cpp [new file with mode: 0644]
lib/Target/Nios2/Nios2Subtarget.h [new file with mode: 0644]
lib/Target/Nios2/Nios2TargetMachine.cpp
lib/Target/Nios2/Nios2TargetMachine.h

index 78db452094bd06e02ec082929c8cd07d2c103b07..eaf7a0b6f4c4edae0c38bb7ffa5437aad2d369dd 100644 (file)
@@ -6,12 +6,19 @@ set(LLVM_TARGET_DEFINITIONS Nios2.td)
 #came from Nios2InstrInfo.td.
 tablegen(LLVM Nios2GenRegisterInfo.inc -gen-register-info)
 tablegen(LLVM Nios2GenInstrInfo.inc -gen-instr-info)
+tablegen(LLVM Nios2GenSubtargetInfo.inc -gen-subtarget)
 
 #Nios2CommonTableGen must be defined
 add_public_tablegen_target(Nios2CommonTableGen)
 
 #Nios2CodeGen should match with LLVMBuild.txt Nios2CodeGen
-add_llvm_target(Nios2CodeGen Nios2TargetMachine.cpp)
+add_llvm_target(Nios2CodeGen
+ Nios2InstrInfo.cpp
+ Nios2FrameLowering.cpp
+ Nios2RegisterInfo.cpp
+ Nios2Subtarget.cpp
+ Nios2TargetMachine.cpp
+ )
 
 #Should match with "subdirectories =  MCTargetDesc TargetInfo" in LLVMBuild.txt
 add_subdirectory(TargetInfo)
index d913166399c677f149947489f78c8d3f752868de..0c70dc0bedc9a0ecbdc17318d804e7e58c1b2084 100644 (file)
 
 #include "Nios2MCTargetDesc.h"
 #include "llvm/MC/MCInstrInfo.h"
+#include "llvm/MC/MCSubtargetInfo.h"
 
 using namespace llvm;
 
 #define GET_INSTRINFO_MC_DESC
 #include "Nios2GenInstrInfo.inc"
 
+#define GET_SUBTARGETINFO_MC_DESC
+#include "Nios2GenSubtargetInfo.inc"
+
 #define GET_REGINFO_MC_DESC
 #include "Nios2GenRegisterInfo.inc"
 
index d426062db16837116060fd741ab073b06ff90d57..e8fe865fadb2d2d9cdffb9d16e5950953af59706 100644 (file)
@@ -31,4 +31,7 @@ Target &getTheNios2Target();
 #define GET_INSTRINFO_ENUM
 #include "Nios2GenInstrInfo.inc"
 
+#define GET_SUBTARGETINFO_ENUM
+#include "Nios2GenSubtargetInfo.inc"
+
 #endif
index e8abba8633708cd34a67aa110f72d7202526d080..c2b54caf38cae7b2648b889b123597398dc8990e 100644 (file)
@@ -8,22 +8,32 @@
 //===----------------------------------------------------------------------===//
 
 //===----------------------------------------------------------------------===//
-// Target-independent interfaces
+// Calling Conv, Instruction Descriptions
 //===----------------------------------------------------------------------===//
 
 include "llvm/Target/Target.td"
+include "Nios2RegisterInfo.td"
+include "Nios2InstrInfo.td"
+include "Nios2Schedule.td"
+
+def Nios2InstrInfo : InstrInfo;
+
+def Nios2 : Target { let InstructionSet = Nios2InstrInfo; }
 
 //===----------------------------------------------------------------------===//
-// Target-dependent interfaces
+// Nios2 Subtarget features
 //===----------------------------------------------------------------------===//
+def FeatureNios2r1     : SubtargetFeature<"nios2r1", "Nios2ArchVersion", 
+                                "Nios2r1", "Nios2 R1 ISA Support">;
+def FeatureNios2r2     : SubtargetFeature<"nios2r2", "Nios2ArchVersion",                      
+                               "Nios2r2", "Nios2 R2 ISA Support">;
 
 //===----------------------------------------------------------------------===//
-// Calling Conv, Instruction Descriptions
+// Nios2 processors supported.
 //===----------------------------------------------------------------------===//
 
-include "Nios2RegisterInfo.td"
-include "Nios2InstrInfo.td"
-
-def Nios2InstrInfo : InstrInfo;
+class Proc<string Name, list<SubtargetFeature> Features>
+ : Processor<Name, Nios2GenericItineraries, Features>;
 
-def Nios2 : Target { let InstructionSet = Nios2InstrInfo; }
+def : Proc<"nios2r1", [FeatureNios2r1]>;
+def : Proc<"nios2r2", [FeatureNios2r2]>;
diff --git a/lib/Target/Nios2/Nios2FrameLowering.cpp b/lib/Target/Nios2/Nios2FrameLowering.cpp
new file mode 100644 (file)
index 0000000..f278d80
--- /dev/null
@@ -0,0 +1,31 @@
+//===-- Nios2FrameLowering.cpp - Nios2 Frame Information ------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the Nios2 implementation of TargetFrameLowering class.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Nios2FrameLowering.h"
+
+#include "Nios2Subtarget.h"
+#include "llvm/CodeGen/MachineFunction.h"
+
+using namespace llvm;
+
+bool Nios2FrameLowering::hasFP(const MachineFunction &MF) const { return true; }
+
+void Nios2FrameLowering::emitPrologue(MachineFunction &MF,
+                                      MachineBasicBlock &MBB) const {}
+
+void Nios2FrameLowering::emitEpilogue(MachineFunction &MF,
+                                      MachineBasicBlock &MBB) const {}
+
+const Nios2FrameLowering *Nios2FrameLowering::create(const Nios2Subtarget &ST) {
+  return new Nios2FrameLowering(ST, 4);
+}
diff --git a/lib/Target/Nios2/Nios2FrameLowering.h b/lib/Target/Nios2/Nios2FrameLowering.h
new file mode 100644 (file)
index 0000000..2aaea67
--- /dev/null
@@ -0,0 +1,40 @@
+//===-- Nios2FrameLowering.h - Define frame lowering for Nios2 --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_LIB_TARGET_NIOS2_NIOS2FRAMELOWERING_H
+#define LLVM_LIB_TARGET_NIOS2_NIOS2FRAMELOWERING_H
+
+#include "Nios2.h"
+#include "llvm/Target/TargetFrameLowering.h"
+
+namespace llvm {
+class Nios2Subtarget;
+
+class Nios2FrameLowering : public TargetFrameLowering {
+protected:
+  const Nios2Subtarget &STI;
+
+public:
+  explicit Nios2FrameLowering(const Nios2Subtarget &sti, unsigned Alignment)
+      : TargetFrameLowering(StackGrowsDown, Alignment, 0, Alignment), STI(sti) {
+  }
+
+  static const Nios2FrameLowering *create(const Nios2Subtarget &ST);
+  bool hasFP(const MachineFunction &MF) const override;
+  /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
+  /// the function.
+  void emitPrologue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
+  void emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const override;
+};
+} // namespace llvm
+
+#endif
diff --git a/lib/Target/Nios2/Nios2InstrInfo.cpp b/lib/Target/Nios2/Nios2InstrInfo.cpp
new file mode 100644 (file)
index 0000000..412cfce
--- /dev/null
@@ -0,0 +1,26 @@
+//===-- Nios2InstrInfo.cpp - Nios2 Instruction Information ----------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the Nios2 implementation of the TargetInstrInfo class.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Nios2InstrInfo.h"
+#include "Nios2TargetMachine.h"
+
+using namespace llvm;
+
+#define GET_INSTRINFO_CTOR_DTOR
+#include "Nios2GenInstrInfo.inc"
+
+const Nios2InstrInfo *Nios2InstrInfo::create(Nios2Subtarget &STI) {
+  return new Nios2InstrInfo(STI);
+}
+
+const Nios2RegisterInfo &Nios2InstrInfo::getRegisterInfo() const { return RI; }
diff --git a/lib/Target/Nios2/Nios2InstrInfo.h b/lib/Target/Nios2/Nios2InstrInfo.h
new file mode 100644 (file)
index 0000000..47e5e83
--- /dev/null
@@ -0,0 +1,47 @@
+//===-- Nios2InstrInfo.h - Nios2 Instruction Information --------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the Nios2 implementation of the TargetInstrInfo class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_NIOS2_NIOS2INSTRINFO_H
+#define LLVM_LIB_TARGET_NIOS2_NIOS2INSTRINFO_H
+
+#include "Nios2.h"
+#include "Nios2RegisterInfo.h"
+
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/Target/TargetInstrInfo.h"
+
+#define GET_INSTRINFO_HEADER
+#include "Nios2GenInstrInfo.inc"
+
+namespace llvm {
+
+class Nios2InstrInfo : public Nios2GenInstrInfo {
+protected:
+  const Nios2Subtarget &Subtarget;
+  const Nios2RegisterInfo RI;
+
+public:
+  explicit Nios2InstrInfo(const Nios2Subtarget &STI)
+      : Nios2GenInstrInfo(), Subtarget(STI), RI(STI) {}
+
+  static const Nios2InstrInfo *create(Nios2Subtarget &STI);
+
+  /// getRegisterInfo - TargetInstrInfo is a superset of MRegister info.  As
+  /// such, whenever a client has an instance of instruction info, it should
+  /// always be able to get register info as well (through this method).
+  ///
+  const Nios2RegisterInfo &getRegisterInfo() const;
+};
+} // namespace llvm
+
+#endif
diff --git a/lib/Target/Nios2/Nios2RegisterInfo.cpp b/lib/Target/Nios2/Nios2RegisterInfo.cpp
new file mode 100644 (file)
index 0000000..b938c48
--- /dev/null
@@ -0,0 +1,50 @@
+//===-- Nios2RegisterInfo.cpp - Nios2 Register Information -== ------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the Nios2 implementation of the TargetRegisterInfo class.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "nios2-reg-info"
+
+#include "Nios2RegisterInfo.h"
+
+#include "Nios2.h"
+#include "Nios2Subtarget.h"
+
+#define GET_REGINFO_TARGET_DESC
+#include "Nios2GenRegisterInfo.inc"
+
+using namespace llvm;
+
+Nios2RegisterInfo::Nios2RegisterInfo(const Nios2Subtarget &ST)
+    : Nios2GenRegisterInfo(Nios2::RA), Subtarget(ST) {}
+
+const TargetRegisterClass *Nios2RegisterInfo::intRegClass(unsigned Size) const {
+  return &Nios2::CPURegsRegClass;
+}
+
+const MCPhysReg *
+Nios2RegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
+  return nullptr;
+}
+
+BitVector Nios2RegisterInfo::getReservedRegs(const MachineFunction &MF) const {
+  BitVector Reserved(1);
+
+  return Reserved;
+}
+
+void Nios2RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
+                                            int SPAdj, unsigned FIOperandNum,
+                                            RegScavenger *RS) const {}
+
+unsigned Nios2RegisterInfo::getFrameRegister(const MachineFunction &MF) const {
+  return 0;
+}
diff --git a/lib/Target/Nios2/Nios2RegisterInfo.h b/lib/Target/Nios2/Nios2RegisterInfo.h
new file mode 100644 (file)
index 0000000..c2cdc9a
--- /dev/null
@@ -0,0 +1,52 @@
+//===-- Nios2RegisterInfo.h - Nios2 Register Information Impl ---*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains the Nios2 implementation of the TargetRegisterInfo class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_NIOS2_NIOS2REGISTERINFO_H
+#define LLVM_LIB_TARGET_NIOS2_NIOS2REGISTERINFO_H
+
+#include "Nios2.h"
+#include "llvm/Target/TargetRegisterInfo.h"
+
+#define GET_REGINFO_HEADER
+#include "Nios2GenRegisterInfo.inc"
+
+namespace llvm {
+class Nios2Subtarget;
+class TargetInstrInfo;
+class Type;
+
+class Nios2RegisterInfo : public Nios2GenRegisterInfo {
+protected:
+  const Nios2Subtarget &Subtarget;
+
+public:
+  Nios2RegisterInfo(const Nios2Subtarget &Subtarget);
+
+  const MCPhysReg *getCalleeSavedRegs(const MachineFunction *MF) const override;
+
+  BitVector getReservedRegs(const MachineFunction &MF) const override;
+
+  /// Stack Frame Processing Methods
+  void eliminateFrameIndex(MachineBasicBlock::iterator II, int SPAdj,
+                           unsigned FIOperandNum,
+                           RegScavenger *RS = nullptr) const override;
+
+  /// Debug information queries.
+  unsigned getFrameRegister(const MachineFunction &MF) const override;
+
+  /// Return GPR register class.
+  const TargetRegisterClass *intRegClass(unsigned Size) const;
+};
+
+} // end namespace llvm
+#endif
diff --git a/lib/Target/Nios2/Nios2Schedule.td b/lib/Target/Nios2/Nios2Schedule.td
new file mode 100644 (file)
index 0000000..2d1d9d5
--- /dev/null
@@ -0,0 +1,39 @@
+//===-- Nios2Schedule.td - Nios2 Scheduling Definitions ----*- tablegen -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+//===----------------------------------------------------------------------===//
+// Functional units across Nios2 chips sets. Based on GCC/Nios2 backend files.
+//===----------------------------------------------------------------------===//
+def ALU     : FuncUnit;
+def IMULDIV : FuncUnit;
+
+//===----------------------------------------------------------------------===//
+// Instruction Itinerary classes used for Nios2
+//===----------------------------------------------------------------------===//
+def IIAlu              : InstrItinClass;
+def IILoad             : InstrItinClass;
+def IIStore            : InstrItinClass;
+def IIFlush            : InstrItinClass;
+def IIIdiv             : InstrItinClass;
+def IIBranch           : InstrItinClass;
+
+def IIPseudo           : InstrItinClass;
+
+//===----------------------------------------------------------------------===//
+// Nios2 Generic instruction itineraries.
+//===----------------------------------------------------------------------===//
+//@ http://llvm.org/docs/doxygen/html/structllvm_1_1InstrStage.html
+def Nios2GenericItineraries : ProcessorItineraries<[ALU, IMULDIV], [], [
+  InstrItinData<IIAlu              , [InstrStage<1,  [ALU]>]>,
+  InstrItinData<IILoad             , [InstrStage<3,  [ALU]>]>,
+  InstrItinData<IIStore            , [InstrStage<1,  [ALU]>]>,
+  InstrItinData<IIFlush            , [InstrStage<1,  [ALU]>]>,
+  InstrItinData<IIIdiv             , [InstrStage<38, [IMULDIV]>]>,
+  InstrItinData<IIBranch           , [InstrStage<1,  [ALU]>]>
+]>;
diff --git a/lib/Target/Nios2/Nios2Subtarget.cpp b/lib/Target/Nios2/Nios2Subtarget.cpp
new file mode 100644 (file)
index 0000000..6176d37
--- /dev/null
@@ -0,0 +1,61 @@
+//===-- Nios2Subtarget.cpp - Nios2 Subtarget Information ------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the Nios2 specific subclass of TargetSubtargetInfo.
+//
+//===----------------------------------------------------------------------===//
+
+#include "Nios2Subtarget.h"
+
+#include "Nios2.h"
+#include "Nios2RegisterInfo.h"
+#include "Nios2TargetMachine.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "nios2-subtarget"
+
+#define GET_SUBTARGETINFO_TARGET_DESC
+#define GET_SUBTARGETINFO_CTOR
+#include "Nios2GenSubtargetInfo.inc"
+
+void Nios2Subtarget::anchor() {}
+
+Nios2Subtarget::Nios2Subtarget(const Triple &TT, const std::string &CPU,
+                               const std::string &FS,
+                               const Nios2TargetMachine &_TM)
+    :
+
+      // Nios2GenSubtargetInfo will display features by llc -march=nios2
+      // -mcpu=help
+      Nios2GenSubtargetInfo(TT, CPU, FS), TM(_TM), TargetTriple(TT),
+      InstrInfo(Nios2InstrInfo::create(
+          initializeSubtargetDependencies(CPU, FS, TM))) {}
+
+Nios2Subtarget &
+Nios2Subtarget::initializeSubtargetDependencies(StringRef CPU, StringRef FS,
+                                                const TargetMachine &TM) {
+  if (TargetTriple.getArch() == Triple::nios2) {
+    if (CPU != "nios2r2") {
+      CPU = "nios2r1";
+      Nios2ArchVersion = Nios2r1;
+    } else {
+      Nios2ArchVersion = Nios2r2;
+    }
+  } else {
+    errs() << "!!!Error, TargetTriple.getArch() = " << TargetTriple.getArch()
+           << "CPU = " << CPU << "\n";
+    exit(0);
+  }
+
+  // Parse features string.
+  ParseSubtargetFeatures(CPU, FS);
+
+  return *this;
+}
diff --git a/lib/Target/Nios2/Nios2Subtarget.h b/lib/Target/Nios2/Nios2Subtarget.h
new file mode 100644 (file)
index 0000000..07ff8d9
--- /dev/null
@@ -0,0 +1,88 @@
+//===-- Nios2Subtarget.h - Define Subtarget for the Nios2 -------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file declares the Nios2 specific subclass of TargetSubtargetInfo.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_NIOS2_NIOS2SUBTARGET_H
+#define LLVM_LIB_TARGET_NIOS2_NIOS2SUBTARGET_H
+
+#include "Nios2FrameLowering.h"
+#include "Nios2InstrInfo.h"
+#include "llvm/Target/TargetSubtargetInfo.h"
+
+#define GET_SUBTARGETINFO_HEADER
+#include "Nios2GenSubtargetInfo.inc"
+
+namespace llvm {
+class StringRef;
+
+class Nios2TargetMachine;
+
+class Nios2Subtarget : public Nios2GenSubtargetInfo {
+  virtual void anchor();
+
+public:
+  // Nios2 R2 features
+  // Bit manipulation instructions extension
+  bool HasBMX;
+  // Code Density instructions extension
+  bool HasCDX;
+  // Multi-Processor instructions extension
+  bool HasMPX;
+  // New mandatory instructions
+  bool HasR2Mandatory;
+
+protected:
+  enum Nios2ArchEnum {
+    // Nios2 R1 ISA
+    Nios2r1,
+    // Nios2 R2 ISA
+    Nios2r2
+  };
+
+  // Nios2 architecture version
+  Nios2ArchEnum Nios2ArchVersion;
+
+  const Nios2TargetMachine &TM;
+
+  Triple TargetTriple;
+
+  std::unique_ptr<const Nios2InstrInfo> InstrInfo;
+  std::unique_ptr<const Nios2FrameLowering> FrameLowering;
+
+public:
+  /// This constructor initializes the data members to match that
+  /// of the specified triple.
+  Nios2Subtarget(const Triple &TT, const std::string &CPU,
+                 const std::string &FS, const Nios2TargetMachine &_TM);
+
+  /// ParseSubtargetFeatures - Parses features string setting specified
+  /// subtarget options.  Definition of function is auto generated by tblgen.
+  void ParseSubtargetFeatures(StringRef CPU, StringRef FS);
+
+  bool hasNios2r1() const { return Nios2ArchVersion >= Nios2r1; }
+  bool isNios2r1() const { return Nios2ArchVersion == Nios2r1; }
+  bool hasNios2r2() const { return Nios2ArchVersion >= Nios2r2; }
+  bool isNios2r2() const { return Nios2ArchVersion == Nios2r2; }
+
+  Nios2Subtarget &initializeSubtargetDependencies(StringRef CPU, StringRef FS,
+                                                  const TargetMachine &TM);
+
+  const TargetFrameLowering *getFrameLowering() const override {
+    return FrameLowering.get();
+  }
+  const Nios2RegisterInfo *getRegisterInfo() const override {
+    return &InstrInfo->getRegisterInfo();
+  }
+};
+} // namespace llvm
+
+#endif
index 16d4eabcfaf71d0409675c8d68ca1a4092f87b1e..bea20d8484658451246774a25fc4da81c5690420 100644 (file)
 #include "Nios2TargetMachine.h"
 #include "Nios2.h"
 
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/Support/TargetRegistry.h"
+
 using namespace llvm;
 
 #define DEBUG_TYPE "nios2"
 
 extern "C" void LLVMInitializeNios2Target() {
   // Register the target.
+  RegisterTargetMachine<Nios2TargetMachine> X(getTheNios2Target());
 }
 
-static std::string computeDataLayout(const Triple &TT, StringRef CPU,
-                                     const TargetOptions &Options) {
+static std::string computeDataLayout() {
   return "e-p:32:32:32-i8:8:32-i16:16:32-n32";
 }
 
-static Reloc::Model getEffectiveRelocModel(CodeModel::Model CM,
-                                           Optional<Reloc::Model> RM) {
-  if (!RM.hasValue() || CM == CodeModel::JITDefault)
+static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
+  if (!RM.hasValue())
     return Reloc::Static;
   return *RM;
 }
@@ -38,9 +40,62 @@ Nios2TargetMachine::Nios2TargetMachine(const Target &T, const Triple &TT,
                                        StringRef CPU, StringRef FS,
                                        const TargetOptions &Options,
                                        Optional<Reloc::Model> RM,
-                                       CodeModel::Model CM,
-                                       CodeGenOpt::Level OL)
-    : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options), TT, CPU, FS,
-                        Options, getEffectiveRelocModel(CM, RM), CM, OL) {}
+                                       Optional<CodeModel::Model> CM,
+                                       CodeGenOpt::Level OL, bool JIT)
+    : LLVMTargetMachine(T, computeDataLayout(), TT, CPU, FS, Options,
+                        getEffectiveRelocModel(RM), *CM, OL),
+      DefaultSubtarget(TT, CPU, FS, *this) {}
 
 Nios2TargetMachine::~Nios2TargetMachine() {}
+
+const Nios2Subtarget *
+Nios2TargetMachine::getSubtargetImpl(const Function &F) const {
+  Attribute CPUAttr = F.getFnAttribute("target-cpu");
+  Attribute FSAttr = F.getFnAttribute("target-features");
+
+  std::string CPU = !CPUAttr.hasAttribute(Attribute::None)
+                        ? CPUAttr.getValueAsString().str()
+                        : TargetCPU;
+  std::string FS = !FSAttr.hasAttribute(Attribute::None)
+                       ? FSAttr.getValueAsString().str()
+                       : TargetFS;
+
+  auto &I = SubtargetMap[CPU + FS];
+  if (!I) {
+    // This needs to be done before we create a new subtarget since any
+    // creation will depend on the TM and the code generation flags on the
+    // function that reside in TargetOptions.
+    resetTargetOptions(F);
+    I = llvm::make_unique<Nios2Subtarget>(TargetTriple, CPU, FS, *this);
+  }
+  return I.get();
+}
+
+namespace {
+/// Nios2 Code Generator Pass Configuration Options.
+class Nios2PassConfig : public TargetPassConfig {
+public:
+  Nios2PassConfig(Nios2TargetMachine &TM, PassManagerBase *PM)
+      : TargetPassConfig(TM, *PM) {}
+
+  Nios2TargetMachine &getNios2TargetMachine() const {
+    return getTM<Nios2TargetMachine>();
+  }
+
+  const Nios2Subtarget &getNios2Subtarget() const {
+    return *getNios2TargetMachine().getSubtargetImpl();
+  }
+  void addCodeGenPrepare() override;
+  void addIRPasses() override;
+};
+} // namespace
+
+TargetPassConfig *Nios2TargetMachine::createPassConfig(PassManagerBase &PM) {
+  return new Nios2PassConfig(*this, &PM);
+}
+
+void Nios2PassConfig::addCodeGenPrepare() {
+  TargetPassConfig::addCodeGenPrepare();
+}
+
+void Nios2PassConfig::addIRPasses() { TargetPassConfig::addIRPasses(); }
index 7f145c82f32cea1faeb942f3adedfe05a6bd6e57..2cf8a10c57e6b0a511768898ba5034bd6e1da43c 100644 (file)
 #ifndef LLVM_LIB_TARGET_NIOS2_NIOS2TARGETMACHINE_H
 #define LLVM_LIB_TARGET_NIOS2_NIOS2TARGETMACHINE_H
 
+#include "Nios2Subtarget.h"
 #include "llvm/Target/TargetMachine.h"
 
 namespace llvm {
 class Nios2TargetMachine : public LLVMTargetMachine {
+  Nios2Subtarget DefaultSubtarget;
+
+  mutable StringMap<std::unique_ptr<Nios2Subtarget>> SubtargetMap;
+
 public:
   Nios2TargetMachine(const Target &T, const Triple &TT, StringRef CPU,
                      StringRef FS, const TargetOptions &Options,
-                     Optional<Reloc::Model> RM, CodeModel::Model CM,
-                     CodeGenOpt::Level OL);
+                     Optional<Reloc::Model> RM, Optional<CodeModel::Model> CM,
+                     CodeGenOpt::Level OL, bool JIT);
   ~Nios2TargetMachine() override;
+
+  const Nios2Subtarget *getSubtargetImpl() const { return &DefaultSubtarget; }
+
+  const Nios2Subtarget *getSubtargetImpl(const Function &F) const override;
+
+  // Pass Pipeline Configuration
+  TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
 };
 } // namespace llvm