Initial checkin of the Mach-O emitter. There's plenty of fixmes, but it
[oota-llvm.git] / lib / Target / PowerPC / PPCTargetMachine.cpp
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>
28 using namespace llvm;
29
30 namespace {
31   // Register the targets
32   RegisterTarget<PPC32TargetMachine>
33   X("ppc32", "  PowerPC 32");
34   RegisterTarget<PPC64TargetMachine>
35   Y("ppc64", "  PowerPC 64");
36 }
37
38 unsigned PPC32TargetMachine::getJITMatchQuality() {
39 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
40   if (sizeof(void*) == 4)
41     return 10;
42 #endif
43   return 0;
44 }
45 unsigned 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
53 unsigned 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
69 unsigned 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
86 PPCTargetMachine::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
100 PPC32TargetMachine::PPC32TargetMachine(const Module &M, const std::string &FS) 
101   : PPCTargetMachine(M, FS, false) {
102 }
103
104
105 PPC64TargetMachine::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 ///
112 bool PPCTargetMachine::addPassesToEmitFile(PassManager &PM, std::ostream &Out,
113                                            CodeGenFileType FileType,
114                                            bool Fast) {
115   if (FileType != TargetMachine::AssemblyFile &&
116       FileType != TargetMachine::ObjectFile) 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   if (FileType == TargetMachine::AssemblyFile)
150     PM.add(createDarwinAsmPrinter(Out, *this));
151   else
152     // FIXME: support PPC ELF files at some point
153     addPPCMachOObjectWriterPass(PM, Out, *this);
154
155   PM.add(createMachineCodeDeleter());
156   return false;
157 }
158
159 void PPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
160   // The JIT should use the static relocation model.
161   TM.setRelocationModel(Reloc::Static);
162
163   // Run loop strength reduction before anything else.
164   PM.add(createLoopStrengthReducePass(TM.getTargetLowering()));
165
166   // FIXME: Implement efficient support for garbage collection intrinsics.
167   PM.add(createLowerGCPass());
168
169   // FIXME: Implement the invoke/unwind instructions!
170   PM.add(createLowerInvokePass());
171
172   // Clean up after other passes, e.g. merging critical edges.
173   PM.add(createCFGSimplificationPass());
174
175   // Make sure that no unreachable blocks are instruction selected.
176   PM.add(createUnreachableBlockEliminationPass());
177
178   // Install an instruction selector.
179   PM.add(createPPCISelDag(TM));
180
181   PM.add(createRegisterAllocator());
182   PM.add(createPrologEpilogCodeInserter());
183
184   // Must run branch selection immediately preceding the asm printer
185   PM.add(createPPCBranchSelectionPass());
186
187   if (PrintMachineCode)
188     PM.add(createMachineFunctionPrinterPass(&std::cerr));
189 }
190
191 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
192 /// machine code emitted.  This uses a MachineCodeEmitter object to handle
193 /// actually outputting the machine code and resolving things like the address
194 /// of functions.  This method should returns true if machine code emission is
195 /// not supported.
196 ///
197 bool PPCTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
198                                                   MachineCodeEmitter &MCE) {
199   // Machine code emitter pass for PowerPC
200   PM.add(createPPCCodeEmitterPass(*this, MCE));
201   // Delete machine code for this function after emitting it
202   PM.add(createMachineCodeDeleter());
203   return false;
204 }