Move all the code that creates code generation passes from Sparc.cpp to
[oota-llvm.git] / lib / Target / TargetMachine.cpp
1 //===-- TargetMachine.cpp - General Target Information ---------------------==//
2 //
3 // This file describes the general parts of a Target machine.
4 // This file also implements MachineInstrInfo and MachineCacheInfo.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Target/TargetMachine.h"
9 #include "llvm/Target/MachineInstrInfo.h"
10 #include "llvm/Target/MachineCacheInfo.h"
11 #include "llvm/CodeGen/PreSelection.h"
12 #include "llvm/CodeGen/InstrSelection.h"
13 #include "llvm/CodeGen/InstrScheduling.h"
14 #include "llvm/CodeGen/RegisterAllocation.h"
15 #include "llvm/CodeGen/MachineCodeForMethod.h"
16 #include "llvm/CodeGen/MachineCodeForInstruction.h"
17 #include "llvm/Reoptimizer/Mapping/MappingInfo.h" 
18 #include "llvm/Reoptimizer/Mapping/FInfo.h" 
19 #include "llvm/Transforms/Scalar.h"
20 #include "Support/CommandLine.h"
21 #include "llvm/PassManager.h"
22 #include "llvm/Function.h"
23 #include "llvm/DerivedTypes.h"
24
25 //---------------------------------------------------------------------------
26 // Command line options to control choice of code generation passes.
27 //---------------------------------------------------------------------------
28
29 static cl::opt<bool> DisablePreSelect("nopreselect",
30                                       cl::desc("Disable preselection pass"));
31
32 static cl::opt<bool> DisableSched("nosched",
33                                   cl::desc("Disable local scheduling pass"));
34
35 //---------------------------------------------------------------------------
36 // class TargetMachine
37 // 
38 // Purpose:
39 //   Machine description.
40 // 
41 //---------------------------------------------------------------------------
42
43
44 // function TargetMachine::findOptimalStorageSize 
45 // 
46 // Purpose:
47 //   This default implementation assumes that all sub-word data items use
48 //   space equal to optSizeForSubWordData, and all other primitive data
49 //   items use space according to the type.
50 //   
51 unsigned int
52 TargetMachine::findOptimalStorageSize(const Type* ty) const
53 {
54   switch(ty->getPrimitiveID())
55     {
56     case Type::BoolTyID:
57     case Type::UByteTyID:
58     case Type::SByteTyID:     
59     case Type::UShortTyID:
60     case Type::ShortTyID:     
61       return optSizeForSubWordData;
62     
63     default:
64       return DataLayout.getTypeSize(ty);
65     }
66 }
67
68
69 //===---------------------------------------------------------------------===//
70 // Default code generation passes.
71 // 
72 // Native code generation for a specified target.
73 //===---------------------------------------------------------------------===//
74
75 class ConstructMachineCodeForFunction : public FunctionPass {
76   TargetMachine &Target;
77 public:
78   inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
79
80   const char *getPassName() const {
81     return "ConstructMachineCodeForFunction";
82   }
83
84   bool runOnFunction(Function &F) {
85     MachineCodeForMethod::construct(&F, Target);
86     return false;
87   }
88 };
89
90 struct FreeMachineCodeForFunction : public FunctionPass {
91   const char *getPassName() const { return "FreeMachineCodeForFunction"; }
92
93   static void freeMachineCode(Instruction &I) {
94     MachineCodeForInstruction::destroy(&I);
95   }
96   
97   bool runOnFunction(Function &F) {
98     for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
99       for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
100         MachineCodeForInstruction::get(I).dropAllReferences();
101     
102     for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
103       for_each(FI->begin(), FI->end(), freeMachineCode);
104     
105     return false;
106   }
107 };
108
109 // addPassesToEmitAssembly - This method controls the entire code generation
110 // process for the ultra sparc.
111 //
112 void
113 TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
114 {
115   // Construct and initialize the MachineCodeForMethod object for this fn.
116   PM.add(new ConstructMachineCodeForFunction(*this));
117
118   // Specialize LLVM code for this target machine and then
119   // run basic dataflow optimizations on LLVM code.
120   if (!DisablePreSelect)
121     {
122       PM.add(createPreSelectionPass(*this));
123       PM.add(createReassociatePass());
124       PM.add(createGCSEPass());
125       PM.add(createLICMPass());
126     }
127
128   PM.add(createInstructionSelectionPass(*this));
129
130   if (!DisableSched)
131     PM.add(createInstructionSchedulingWithSSAPass(*this));
132
133   PM.add(getRegisterAllocator(*this));
134
135   //PM.add(new OptimizeLeafProcedures());
136   //PM.add(new DeleteFallThroughBranches());
137   //PM.add(new RemoveChainedBranches());    // should be folded with previous
138   //PM.add(new RemoveRedundantOps());       // operations with %g0, NOP, etc.
139
140   PM.add(getPrologEpilogInsertionPass());
141
142   PM.add(MappingInfoForFunction(Out));  
143
144   // Output assembly language to the .s file.  Assembly emission is split into
145   // two parts: Function output and Global value output.  This is because
146   // function output is pipelined with all of the rest of code generation stuff,
147   // allowing machine code representations for functions to be free'd after the
148   // function has been emitted.
149   //
150   PM.add(getFunctionAsmPrinterPass(Out));
151   PM.add(new FreeMachineCodeForFunction());  // Free stuff no longer needed
152
153   // Emit Module level assembly after all of the functions have been processed.
154   PM.add(getModuleAsmPrinterPass(Out));
155
156   // Emit bytecode to the assembly file into its special section next
157   PM.add(getEmitBytecodeToAsmPass(Out));
158   PM.add(getFunctionInfo(Out)); 
159 }
160
161
162 //---------------------------------------------------------------------------
163 // class MachineInstructionInfo
164 //      Interface to description of machine instructions
165 //---------------------------------------------------------------------------
166
167
168 /*ctor*/
169 MachineInstrInfo::MachineInstrInfo(const TargetMachine& tgt,
170                                    const MachineInstrDescriptor* _desc,
171                                    unsigned int _descSize,
172                                    unsigned int _numRealOpCodes)
173   : target(tgt),
174     desc(_desc), descSize(_descSize), numRealOpCodes(_numRealOpCodes)
175 {
176   // FIXME: TargetInstrDescriptors should not be global
177   assert(TargetInstrDescriptors == NULL && desc != NULL);
178   TargetInstrDescriptors = desc;        // initialize global variable
179 }  
180
181
182 MachineInstrInfo::~MachineInstrInfo()
183 {
184   TargetInstrDescriptors = NULL;        // reset global variable
185 }
186
187
188 bool
189 MachineInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
190                                            int64_t intValue) const
191 {
192   // First, check if opCode has an immed field.
193   bool isSignExtended;
194   uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended);
195   if (maxImmedValue != 0)
196     {
197       // NEED TO HANDLE UNSIGNED VALUES SINCE THEY MAY BECOME MUCH
198       // SMALLER AFTER CASTING TO SIGN-EXTENDED int, short, or char.
199       // See CreateUIntSetInstruction in SparcInstrInfo.cpp.
200       
201       // Now check if the constant fits
202       if (intValue <= (int64_t) maxImmedValue &&
203           intValue >= -((int64_t) maxImmedValue+1))
204         return true;
205     }
206   
207   return false;
208 }
209
210
211 //---------------------------------------------------------------------------
212 // class MachineCacheInfo 
213 // 
214 // Purpose:
215 //   Describes properties of the target cache architecture.
216 //---------------------------------------------------------------------------
217
218 /*ctor*/
219 MachineCacheInfo::MachineCacheInfo(const TargetMachine& tgt)
220   : target(tgt)
221 {
222   Initialize();
223 }
224
225 void
226 MachineCacheInfo::Initialize()
227 {
228   numLevels = 2;
229   cacheLineSizes.push_back(16);  cacheLineSizes.push_back(32); 
230   cacheSizes.push_back(1 << 15); cacheSizes.push_back(1 << 20);
231   cacheAssoc.push_back(1);       cacheAssoc.push_back(4);
232 }