SparcTargetMachine.cpp revision a69571c7991813c93cba64e88eced6899ce93d81
1//===-- SparcTargetMachine.cpp - Define TargetMachine for Sparc -----------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11//===----------------------------------------------------------------------===//
12
13#include "SparcTargetMachine.h"
14#include "Sparc.h"
15#include "llvm/Assembly/PrintModulePass.h"
16#include "llvm/Module.h"
17#include "llvm/PassManager.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/Target/TargetOptions.h"
21#include "llvm/Target/TargetMachineRegistry.h"
22#include "llvm/Transforms/Scalar.h"
23#include <iostream>
24using namespace llvm;
25
26namespace {
27  // Register the target.
28  RegisterTarget<SparcTargetMachine> X("sparc", "  SPARC");
29}
30
31/// SparcTargetMachine ctor - Create an ILP32 architecture model
32///
33SparcTargetMachine::SparcTargetMachine(const Module &M, const std::string &FS)
34  : TargetMachine("Sparc"),
35    DataLayout("Sparc", false, 4, 4),
36    Subtarget(M, FS), InstrInfo(Subtarget),
37    FrameInfo(TargetFrameInfo::StackGrowsDown, 8, 0) {
38}
39
40unsigned SparcTargetMachine::getModuleMatchQuality(const Module &M) {
41  std::string TT = M.getTargetTriple();
42  if (TT.size() >= 6 && std::string(TT.begin(), TT.begin()+6) == "sparc-")
43    return 20;
44
45  if (M.getEndianness()  == Module::BigEndian &&
46      M.getPointerSize() == Module::Pointer32)
47#ifdef __sparc__
48    return 20;   // BE/32 ==> Prefer sparc on sparc
49#else
50    return 5;    // BE/32 ==> Prefer ppc elsewhere
51#endif
52  else if (M.getEndianness() != Module::AnyEndianness ||
53           M.getPointerSize() != Module::AnyPointerSize)
54    return 0;                                    // Match for some other target
55
56  return 0;
57}
58
59/// addPassesToEmitFile - Add passes to the specified pass manager
60/// to implement a static compiler for this target.
61///
62bool SparcTargetMachine::addPassesToEmitFile(PassManager &PM, std::ostream &Out,
63                                             CodeGenFileType FileType,
64                                             bool Fast) {
65  if (FileType != TargetMachine::AssemblyFile) return true;
66
67  // Run loop strength reduction before anything else.
68  if (!Fast) PM.add(createLoopStrengthReducePass());
69
70  // FIXME: Implement efficient support for garbage collection intrinsics.
71  PM.add(createLowerGCPass());
72
73  // FIXME: implement the invoke/unwind instructions!
74  PM.add(createLowerInvokePass());
75
76  // Print LLVM code input to instruction selector:
77  if (PrintMachineCode)
78    PM.add(new PrintFunctionPass());
79
80  // Make sure that no unreachable blocks are instruction selected.
81  PM.add(createUnreachableBlockEliminationPass());
82
83  PM.add(createSparcISelDag(*this));
84
85  // Print machine instructions as they were initially generated.
86  if (PrintMachineCode)
87    PM.add(createMachineFunctionPrinterPass(&std::cerr));
88
89  PM.add(createRegisterAllocator());
90  PM.add(createPrologEpilogCodeInserter());
91
92  // Print machine instructions after register allocation and prolog/epilog
93  // insertion.
94  if (PrintMachineCode)
95    PM.add(createMachineFunctionPrinterPass(&std::cerr));
96
97  PM.add(createSparcFPMoverPass(*this));
98
99  PM.add(createSparcDelaySlotFillerPass(*this));
100
101  // Print machine instructions after filling delay slots.
102  if (PrintMachineCode)
103    PM.add(createMachineFunctionPrinterPass(&std::cerr));
104
105  // Output assembly language.
106  PM.add(createSparcCodePrinterPass(Out, *this));
107
108  // Delete the MachineInstrs we generated, since they're no longer needed.
109  PM.add(createMachineCodeDeleter());
110  return false;
111}
112
113