PPCTargetMachine.cpp revision 3d6721a4a1cf9e9a27d05b268f86b7a9ce663d6f
1//===-- PPCTargetMachine.cpp - Define TargetMachine for PowerPC -----------===//
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// Top-level implementation for the PowerPC target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPC.h"
15#include "PPCFrameInfo.h"
16#include "PPCTargetMachine.h"
17#include "PPCJITInfo.h"
18#include "llvm/Module.h"
19#include "llvm/PassManager.h"
20#include "llvm/Analysis/Verifier.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/Passes.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/Target/TargetMachineRegistry.h"
25#include "llvm/Transforms/Scalar.h"
26#include "llvm/Support/CommandLine.h"
27#include <iostream>
28using namespace llvm;
29
30namespace {
31  // Register the targets
32  RegisterTarget<PPC32TargetMachine>
33  X("ppc32", "  PowerPC 32");
34  RegisterTarget<PPC64TargetMachine>
35  Y("ppc64", "  PowerPC 64");
36}
37
38unsigned PPC32TargetMachine::getJITMatchQuality() {
39#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
40  if (sizeof(void*) == 4)
41    return 10;
42#endif
43  return 0;
44}
45unsigned PPC64TargetMachine::getJITMatchQuality() {
46#if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
47  if (sizeof(void*) == 8)
48    return 10;
49#endif
50  return 0;
51}
52
53unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
54  // We strongly match "powerpc-*".
55  std::string TT = M.getTargetTriple();
56  if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
57    return 20;
58
59  if (M.getEndianness()  == Module::BigEndian &&
60      M.getPointerSize() == Module::Pointer32)
61    return 10;                                   // Weak match
62  else if (M.getEndianness() != Module::AnyEndianness ||
63           M.getPointerSize() != Module::AnyPointerSize)
64    return 0;                                    // Match for some other target
65
66  return getJITMatchQuality()/2;
67}
68
69unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
70  // We strongly match "powerpc64-*".
71  std::string TT = M.getTargetTriple();
72  if (TT.size() >= 10 && std::string(TT.begin(), TT.begin()+10) == "powerpc64-")
73    return 20;
74
75  if (M.getEndianness()  == Module::BigEndian &&
76      M.getPointerSize() == Module::Pointer64)
77    return 10;                                   // Weak match
78  else if (M.getEndianness() != Module::AnyEndianness ||
79           M.getPointerSize() != Module::AnyPointerSize)
80    return 0;                                    // Match for some other target
81
82  return getJITMatchQuality()/2;
83}
84
85
86PPCTargetMachine::PPCTargetMachine(const Module &M, const std::string &FS,
87                                   bool is64Bit)
88  : TargetMachine("PowerPC"), Subtarget(M, FS, is64Bit),
89    DataLayout(Subtarget.getTargetDataString()), InstrInfo(*this),
90    FrameInfo(*this, false), JITInfo(*this), TLInfo(*this),
91    InstrItins(Subtarget.getInstrItineraryData()) {
92
93  if (getRelocationModel() == Reloc::Default)
94    if (Subtarget.isDarwin())
95      setRelocationModel(Reloc::DynamicNoPIC);
96    else
97      setRelocationModel(Reloc::PIC);
98}
99
100PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS)
101  : PPCTargetMachine(M, FS, false) {
102}
103
104
105PPC64TargetMachine::PPC64TargetMachine(const Module &M, const std::string &FS)
106  : PPCTargetMachine(M, FS, true) {
107}
108
109/// addPassesToEmitFile - Add passes to the specified pass manager to implement
110/// a static compiler for this target.
111///
112bool PPCTargetMachine::addPassesToEmitFile(PassManager &PM,
113                                           std::ostream &Out,
114                                           CodeGenFileType FileType,
115                                           bool Fast) {
116  if (FileType != TargetMachine::AssemblyFile) return true;
117
118  // Run loop strength reduction before anything else.
119  if (!Fast) PM.add(createLoopStrengthReducePass(&TLInfo));
120
121  // FIXME: Implement efficient support for garbage collection intrinsics.
122  PM.add(createLowerGCPass());
123
124  // FIXME: Implement the invoke/unwind instructions!
125  PM.add(createLowerInvokePass());
126
127  // Clean up after other passes, e.g. merging critical edges.
128  if (!Fast) PM.add(createCFGSimplificationPass());
129
130  // Make sure that no unreachable blocks are instruction selected.
131  PM.add(createUnreachableBlockEliminationPass());
132
133  // Install an instruction selector.
134  PM.add(createPPCISelDag(*this));
135
136  if (PrintMachineCode)
137    PM.add(createMachineFunctionPrinterPass(&std::cerr));
138
139  PM.add(createRegisterAllocator());
140
141  if (PrintMachineCode)
142    PM.add(createMachineFunctionPrinterPass(&std::cerr));
143
144  PM.add(createPrologEpilogCodeInserter());
145
146  // Must run branch selection immediately preceding the asm printer
147  PM.add(createPPCBranchSelectionPass());
148
149  // Decide which asm printer to use.  If the user has not specified one on
150  // the command line, choose whichever one matches the default (current host).
151  if (Subtarget.isAIX())
152    PM.add(createAIXAsmPrinter(Out, *this));
153  else
154    PM.add(createDarwinAsmPrinter(Out, *this));
155
156  PM.add(createMachineCodeDeleter());
157  return false;
158}
159
160void PPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
161  // The JIT should use the static relocation model.
162  TM.setRelocationModel(Reloc::Static);
163
164  // Run loop strength reduction before anything else.
165  PM.add(createLoopStrengthReducePass(TM.getTargetLowering()));
166
167  // FIXME: Implement efficient support for garbage collection intrinsics.
168  PM.add(createLowerGCPass());
169
170  // FIXME: Implement the invoke/unwind instructions!
171  PM.add(createLowerInvokePass());
172
173  // Clean up after other passes, e.g. merging critical edges.
174  PM.add(createCFGSimplificationPass());
175
176  // Make sure that no unreachable blocks are instruction selected.
177  PM.add(createUnreachableBlockEliminationPass());
178
179  // Install an instruction selector.
180  PM.add(createPPCISelDag(TM));
181
182  PM.add(createRegisterAllocator());
183  PM.add(createPrologEpilogCodeInserter());
184
185  // Must run branch selection immediately preceding the asm printer
186  PM.add(createPPCBranchSelectionPass());
187
188  if (PrintMachineCode)
189    PM.add(createMachineFunctionPrinterPass(&std::cerr));
190}
191
192