1 //===-- TargetMachine.cpp - General Target Information ---------------------==//
3 // This file describes the general parts of a Target machine.
4 // This file also implements MachineInstrInfo and MachineCacheInfo.
6 //===----------------------------------------------------------------------===//
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"
27 //---------------------------------------------------------------------------
28 // Command line options to control choice of code generation passes.
29 //---------------------------------------------------------------------------
31 static cl::opt<bool> DisablePreSelect("nopreselect",
32 cl::desc("Disable preselection pass"));
34 static cl::opt<bool> DisableSched("nosched",
35 cl::desc("Disable local scheduling pass"));
37 static cl::opt<bool> DisablePeephole("nopeephole",
38 cl::desc("Disable peephole optimization pass"));
40 //---------------------------------------------------------------------------
41 // class TargetMachine
44 // Machine description.
46 //---------------------------------------------------------------------------
49 // function TargetMachine::findOptimalStorageSize
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.
57 TargetMachine::findOptimalStorageSize(const Type* ty) const
59 switch(ty->getPrimitiveID())
64 case Type::UShortTyID:
66 return optSizeForSubWordData;
69 return DataLayout.getTypeSize(ty);
74 //===---------------------------------------------------------------------===//
75 // Default code generation passes.
77 // Native code generation for a specified target.
78 //===---------------------------------------------------------------------===//
80 class ConstructMachineCodeForFunction : public FunctionPass {
81 TargetMachine &Target;
83 inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
85 const char *getPassName() const {
86 return "ConstructMachineCodeForFunction";
89 bool runOnFunction(Function &F) {
90 MachineCodeForMethod::construct(&F, Target);
95 struct FreeMachineCodeForFunction : public FunctionPass {
96 const char *getPassName() const { return "FreeMachineCodeForFunction"; }
98 static void freeMachineCode(Instruction &I) {
99 MachineCodeForInstruction::destroy(&I);
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();
107 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
108 for_each(FI->begin(), FI->end(), freeMachineCode);
114 // addPassesToEmitAssembly - This method controls the entire code generation
115 // process for the ultra sparc.
118 TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
120 // Construct and initialize the MachineCodeForMethod object for this fn.
121 PM.add(new ConstructMachineCodeForFunction(*this));
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));
127 // Specialize LLVM code for this target machine and then
128 // run basic dataflow optimizations on LLVM code.
129 if (!DisablePreSelect)
131 PM.add(createPreSelectionPass(*this));
132 /* PM.add(createReassociatePass()); */
133 PM.add(createLICMPass());
134 PM.add(createGCSEPass());
137 PM.add(createInstructionSelectionPass(*this));
140 PM.add(createInstructionSchedulingWithSSAPass(*this));
142 PM.add(getRegisterAllocator(*this));
144 PM.add(getPrologEpilogInsertionPass());
146 if (!DisablePeephole)
147 PM.add(createPeepholeOptsPass(*this));
149 PM.add(MappingInfoForFunction(Out));
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.
157 PM.add(getFunctionAsmPrinterPass(Out));
158 PM.add(new FreeMachineCodeForFunction()); // Free stuff no longer needed
160 // Emit Module level assembly after all of the functions have been processed.
161 PM.add(getModuleAsmPrinterPass(Out));
163 // Emit bytecode to the assembly file into its special section next
164 PM.add(getEmitBytecodeToAsmPass(Out));
165 PM.add(getFunctionInfo(Out));
169 //---------------------------------------------------------------------------
170 // class MachineInstructionInfo
171 // Interface to description of machine instructions
172 //---------------------------------------------------------------------------
176 MachineInstrInfo::MachineInstrInfo(const TargetMachine& tgt,
177 const MachineInstrDescriptor* _desc,
178 unsigned int _descSize,
179 unsigned int _numRealOpCodes)
181 desc(_desc), descSize(_descSize), numRealOpCodes(_numRealOpCodes)
183 // FIXME: TargetInstrDescriptors should not be global
184 assert(TargetInstrDescriptors == NULL && desc != NULL);
185 TargetInstrDescriptors = desc; // initialize global variable
189 MachineInstrInfo::~MachineInstrInfo()
191 TargetInstrDescriptors = NULL; // reset global variable
196 MachineInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
197 int64_t intValue) const
199 // First, check if opCode has an immed field.
201 uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended);
202 if (maxImmedValue != 0)
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.
208 // Now check if the constant fits
209 if (intValue <= (int64_t) maxImmedValue &&
210 intValue >= -((int64_t) maxImmedValue+1))
218 //---------------------------------------------------------------------------
219 // class MachineCacheInfo
222 // Describes properties of the target cache architecture.
223 //---------------------------------------------------------------------------
226 MachineCacheInfo::MachineCacheInfo(const TargetMachine& tgt)
233 MachineCacheInfo::Initialize()
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);