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