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