Generate mfocrf when targeting g5. Generate fsqrt/fsqrts when targetin g5.
[oota-llvm.git] / lib / Target / PowerPC / PPCTargetMachine.cpp
1 //===-- PowerPCTargetMachine.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 //
11 //===----------------------------------------------------------------------===//
12
13 #include "PowerPC.h"
14 #include "PowerPCTargetMachine.h"
15 #include "PowerPCFrameInfo.h"
16 #include "PPC32TargetMachine.h"
17 #include "PPC64TargetMachine.h"
18 #include "PPC32JITInfo.h"
19 #include "PPC64JITInfo.h"
20 #include "llvm/Module.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/CodeGen/IntrinsicLowering.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/Target/TargetMachineRegistry.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "llvm/Support/CommandLine.h"
29 #include <iostream>
30 using namespace llvm;
31
32 namespace llvm {
33   cl::opt<bool> AIX("aix",
34                     cl::desc("Generate AIX/xcoff instead of Darwin/MachO"),
35                     cl::Hidden);
36   cl::opt<bool> EnablePPCLSR("enable-lsr-for-ppc",
37                              cl::desc("Enable LSR for PPC (beta)"),
38                              cl::Hidden);
39 }
40
41 namespace {
42   const std::string PPC32ID = "PowerPC/32bit";
43   const std::string PPC64ID = "PowerPC/64bit";
44
45   // Register the targets
46   RegisterTarget<PPC32TargetMachine>
47   X("ppc32", "  PowerPC 32-bit");
48
49 #if 0
50   RegisterTarget<PPC64TargetMachine>
51   Y("ppc64", "  PowerPC 64-bit (unimplemented)");
52 #endif
53 }
54
55 PowerPCTargetMachine::PowerPCTargetMachine(const std::string &name,
56                                            IntrinsicLowering *IL,
57                                            const TargetData &TD,
58                                            const PowerPCFrameInfo &TFI)
59   : TargetMachine(name, IL, TD), FrameInfo(TFI)
60 {}
61
62 unsigned PPC32TargetMachine::getJITMatchQuality() {
63 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
64   return 10;
65 #else
66   return 0;
67 #endif
68 }
69
70 /// addPassesToEmitFile - Add passes to the specified pass manager to implement
71 /// a static compiler for this target.
72 ///
73 bool PowerPCTargetMachine::addPassesToEmitFile(PassManager &PM,
74                                                std::ostream &Out,
75                                                 CodeGenFileType FileType) {
76   if (FileType != TargetMachine::AssemblyFile) return true;
77
78   bool LP64 = (0 != dynamic_cast<PPC64TargetMachine *>(this));
79
80   if (EnablePPCLSR) {
81     PM.add(createLoopStrengthReducePass());
82     PM.add(createCFGSimplificationPass());
83   }
84
85   // FIXME: Implement efficient support for garbage collection intrinsics.
86   PM.add(createLowerGCPass());
87
88   // FIXME: Implement the invoke/unwind instructions!
89   PM.add(createLowerInvokePass());
90
91   // FIXME: Implement the switch instruction in the instruction selector!
92   PM.add(createLowerSwitchPass());
93
94   PM.add(createLowerConstantExpressionsPass());
95
96   // Make sure that no unreachable blocks are instruction selected.
97   PM.add(createUnreachableBlockEliminationPass());
98
99   // Default to pattern ISel
100   if (LP64)
101     PM.add(createPPC64ISelPattern(*this));
102   else if (PatternISelTriState == 0)
103     PM.add(createPPC32ISelSimple(*this));
104   else
105     PM.add(createPPC32ISelPattern(*this));
106
107   if (PrintMachineCode)
108     PM.add(createMachineFunctionPrinterPass(&std::cerr));
109
110   PM.add(createRegisterAllocator());
111
112   if (PrintMachineCode)
113     PM.add(createMachineFunctionPrinterPass(&std::cerr));
114
115   PM.add(createPrologEpilogCodeInserter());
116
117   // Must run branch selection immediately preceding the asm printer
118   PM.add(createPPCBranchSelectionPass());
119
120   if (AIX)
121     PM.add(createAIXAsmPrinter(Out, *this));
122   else
123     PM.add(createDarwinAsmPrinter(Out, *this));
124
125   PM.add(createMachineCodeDeleter());
126   return false;
127 }
128
129 void PowerPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
130   bool LP64 = (0 != dynamic_cast<PPC64TargetMachine *>(&TM));
131
132   if (EnablePPCLSR) {
133     PM.add(createLoopStrengthReducePass());
134     PM.add(createCFGSimplificationPass());
135   }
136
137   // FIXME: Implement efficient support for garbage collection intrinsics.
138   PM.add(createLowerGCPass());
139
140   // FIXME: Implement the invoke/unwind instructions!
141   PM.add(createLowerInvokePass());
142
143   // FIXME: Implement the switch instruction in the instruction selector!
144   PM.add(createLowerSwitchPass());
145
146   PM.add(createLowerConstantExpressionsPass());
147
148   // Make sure that no unreachable blocks are instruction selected.
149   PM.add(createUnreachableBlockEliminationPass());
150
151   // Default to pattern ISel
152   if (LP64)
153     PM.add(createPPC64ISelPattern(TM));
154   else if (PatternISelTriState == 0)
155     PM.add(createPPC32ISelSimple(TM));
156   else
157     PM.add(createPPC32ISelPattern(TM));
158
159   PM.add(createRegisterAllocator());
160   PM.add(createPrologEpilogCodeInserter());
161
162   // Must run branch selection immediately preceding the asm printer
163   PM.add(createPPCBranchSelectionPass());
164
165   if (PrintMachineCode)
166     PM.add(createMachineFunctionPrinterPass(&std::cerr));
167 }
168
169 /// PowerPCTargetMachine ctor - Create an ILP32 architecture model
170 ///
171 PPC32TargetMachine::PPC32TargetMachine(const Module &M, IntrinsicLowering *IL)
172   : PowerPCTargetMachine(PPC32ID, IL,
173                          TargetData(PPC32ID,false,4,4,8,4,4,4,2,1,1),
174                          PowerPCFrameInfo(*this, false)), JITInfo(*this) {}
175
176 /// PPC64TargetMachine ctor - Create a LP64 architecture model
177 ///
178 PPC64TargetMachine::PPC64TargetMachine(const Module &M, IntrinsicLowering *IL)
179   : PowerPCTargetMachine(PPC64ID, IL,
180                          TargetData(PPC64ID,false,8,4,8,4,4,4,2,1,1),
181                          PowerPCFrameInfo(*this, true)) {}
182
183 unsigned PPC32TargetMachine::getModuleMatchQuality(const Module &M) {
184   // We strongly match "powerpc-*".
185   std::string TT = M.getTargetTriple();
186   if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
187     return 20;
188
189   if (M.getEndianness()  == Module::BigEndian &&
190       M.getPointerSize() == Module::Pointer32)
191     return 10;                                   // Weak match
192   else if (M.getEndianness() != Module::AnyEndianness ||
193            M.getPointerSize() != Module::AnyPointerSize)
194     return 0;                                    // Match for some other target
195
196   return getJITMatchQuality()/2;
197 }
198
199 unsigned PPC64TargetMachine::getModuleMatchQuality(const Module &M) {
200   if (M.getEndianness()  == Module::BigEndian &&
201       M.getPointerSize() == Module::Pointer64)
202     return 10;                                   // Direct match
203   else if (M.getEndianness() != Module::AnyEndianness ||
204            M.getPointerSize() != Module::AnyPointerSize)
205     return 0;                                    // Match for some other target
206
207   return getJITMatchQuality()/2;
208 }