Add a new option to indicate we want the code generator to emit code quickly,
[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/IntrinsicLowering.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Target/TargetMachineRegistry.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Support/CommandLine.h"
28 #include <iostream>
29 using namespace llvm;
30
31 namespace {
32   static cl::opt<bool> DisablePPCDAGDAG("disable-ppc-dag-isel", cl::Hidden,
33                              cl::desc("Disable DAG-to-DAG isel for PPC"));
34   
35   // Register the targets
36   RegisterTarget<PPCTargetMachine>
37   X("ppc32", "  PowerPC");
38 }
39
40 unsigned PPCTargetMachine::getJITMatchQuality() {
41 #if defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
42   return 10;
43 #else
44   return 0;
45 #endif
46 }
47
48 unsigned PPCTargetMachine::getModuleMatchQuality(const Module &M) {
49   // We strongly match "powerpc-*".
50   std::string TT = M.getTargetTriple();
51   if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "powerpc-")
52     return 20;
53   
54   if (M.getEndianness()  == Module::BigEndian &&
55       M.getPointerSize() == Module::Pointer32)
56     return 10;                                   // Weak match
57   else if (M.getEndianness() != Module::AnyEndianness ||
58            M.getPointerSize() != Module::AnyPointerSize)
59     return 0;                                    // Match for some other target
60   
61   return getJITMatchQuality()/2;
62 }
63
64 PPCTargetMachine::PPCTargetMachine(const Module &M, IntrinsicLowering *IL,
65                                    const std::string &FS)
66 : TargetMachine("PowerPC", IL, false, 4, 4, 4, 4, 4, 4, 2, 1, 1),
67   Subtarget(M, FS), FrameInfo(*this, false), JITInfo(*this),
68   InstrItins(Subtarget.getInstrItineraryData()) {
69   if (TargetDefault == PPCTarget) {
70     if (Subtarget.isAIX()) PPCTarget = TargetAIX;
71     if (Subtarget.isDarwin()) PPCTarget = TargetDarwin;
72   }
73 }
74
75 /// addPassesToEmitFile - Add passes to the specified pass manager to implement
76 /// a static compiler for this target.
77 ///
78 bool PPCTargetMachine::addPassesToEmitFile(PassManager &PM,
79                                            std::ostream &Out,
80                                            CodeGenFileType FileType,
81                                            bool Fast) {
82   if (FileType != TargetMachine::AssemblyFile) return true;
83
84   // Run loop strength reduction before anything else.
85   if (!Fast) PM.add(createLoopStrengthReducePass());
86
87   // FIXME: Implement efficient support for garbage collection intrinsics.
88   PM.add(createLowerGCPass());
89
90   // FIXME: Implement the invoke/unwind instructions!
91   PM.add(createLowerInvokePass());
92   
93   // Clean up after other passes, e.g. merging critical edges.
94   if (!Fast) PM.add(createCFGSimplificationPass());
95
96   // FIXME: Implement the switch instruction in the instruction selector!
97   PM.add(createLowerSwitchPass());
98
99   // Make sure that no unreachable blocks are instruction selected.
100   PM.add(createUnreachableBlockEliminationPass());
101
102   // Install an instruction selector.
103   if (!DisablePPCDAGDAG)
104     PM.add(createPPCISelDag(*this));
105   else
106     PM.add(createPPCISelPattern(*this));
107
108   if (PrintMachineCode)
109     PM.add(createMachineFunctionPrinterPass(&std::cerr));
110
111   PM.add(createRegisterAllocator());
112
113   if (PrintMachineCode)
114     PM.add(createMachineFunctionPrinterPass(&std::cerr));
115
116   PM.add(createPrologEpilogCodeInserter());
117
118   // Must run branch selection immediately preceding the asm printer
119   PM.add(createPPCBranchSelectionPass());
120
121   // Decide which asm printer to use.  If the user has not specified one on
122   // the command line, choose whichever one matches the default (current host).
123   switch (PPCTarget) {
124   case TargetAIX:
125     PM.add(createAIXAsmPrinter(Out, *this));
126     break;
127   case TargetDefault:
128   case TargetDarwin:
129     PM.add(createDarwinAsmPrinter(Out, *this));
130     break;
131   }
132
133   PM.add(createMachineCodeDeleter());
134   return false;
135 }
136
137 void PPCJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
138   // The JIT does not support or need PIC.
139   PICEnabled = false;
140
141   // Run loop strength reduction before anything else.
142   PM.add(createLoopStrengthReducePass());
143
144   // FIXME: Implement efficient support for garbage collection intrinsics.
145   PM.add(createLowerGCPass());
146
147   // FIXME: Implement the invoke/unwind instructions!
148   PM.add(createLowerInvokePass());
149
150   // Clean up after other passes, e.g. merging critical edges.
151   PM.add(createCFGSimplificationPass());
152
153   // FIXME: Implement the switch instruction in the instruction selector!
154   PM.add(createLowerSwitchPass());
155
156   // Make sure that no unreachable blocks are instruction selected.
157   PM.add(createUnreachableBlockEliminationPass());
158
159   // Install an instruction selector.
160   if (!DisablePPCDAGDAG)
161     PM.add(createPPCISelDag(TM));
162   else
163     PM.add(createPPCISelPattern(TM));
164
165   PM.add(createRegisterAllocator());
166   PM.add(createPrologEpilogCodeInserter());
167
168   // Must run branch selection immediately preceding the asm printer
169   PM.add(createPPCBranchSelectionPass());
170
171   if (PrintMachineCode)
172     PM.add(createMachineFunctionPrinterPass(&std::cerr));
173 }
174