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/InstrSelection.h"
13 #include "llvm/CodeGen/InstrScheduling.h"
14 #include "llvm/CodeGen/RegisterAllocation.h"
15 #include "llvm/CodeGen/PeepholeOpts.h"
16 #include "llvm/CodeGen/MachineCodeForMethod.h"
17 #include "llvm/CodeGen/MachineCodeForInstruction.h"
18 #include "llvm/Reoptimizer/Mapping/MappingInfo.h"
19 #include "llvm/Reoptimizer/Mapping/FInfo.h"
20 #include "llvm/Transforms/Scalar.h"
21 #include "Support/CommandLine.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/Function.h"
24 #include "llvm/DerivedTypes.h"
26 //---------------------------------------------------------------------------
27 // Command line options to control choice of code generation passes.
28 //---------------------------------------------------------------------------
30 static cl::opt<bool> DisablePreSelect("nopreselect",
31 cl::desc("Disable preselection pass"));
33 static cl::opt<bool> DisableSched("nosched",
34 cl::desc("Disable local scheduling pass"));
36 static cl::opt<bool> DisablePeephole("nopeephole",
37 cl::desc("Disable peephole optimization pass"));
39 //---------------------------------------------------------------------------
40 // class TargetMachine
43 // Machine description.
45 //---------------------------------------------------------------------------
48 // function TargetMachine::findOptimalStorageSize
51 // This default implementation assumes that all sub-word data items use
52 // space equal to optSizeForSubWordData, and all other primitive data
53 // items use space according to the type.
56 TargetMachine::findOptimalStorageSize(const Type* ty) const
58 switch(ty->getPrimitiveID())
63 case Type::UShortTyID:
65 return optSizeForSubWordData;
68 return DataLayout.getTypeSize(ty);
73 //===---------------------------------------------------------------------===//
74 // Default code generation passes.
76 // Native code generation for a specified target.
77 //===---------------------------------------------------------------------===//
79 class ConstructMachineCodeForFunction : public FunctionPass {
80 TargetMachine &Target;
82 inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
84 const char *getPassName() const {
85 return "ConstructMachineCodeForFunction";
88 bool runOnFunction(Function &F) {
89 MachineCodeForMethod::construct(&F, Target);
94 struct FreeMachineCodeForFunction : public FunctionPass {
95 const char *getPassName() const { return "FreeMachineCodeForFunction"; }
97 static void freeMachineCode(Instruction &I) {
98 MachineCodeForInstruction::destroy(&I);
101 bool runOnFunction(Function &F) {
102 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
103 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
104 MachineCodeForInstruction::get(I).dropAllReferences();
106 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
107 for_each(FI->begin(), FI->end(), freeMachineCode);
113 // addPassesToEmitAssembly - This method controls the entire code generation
114 // process for the ultra sparc.
117 TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
119 // Construct and initialize the MachineCodeForMethod object for this fn.
120 PM.add(new ConstructMachineCodeForFunction(*this));
122 // Specialize LLVM code for this target machine and then
123 // run basic dataflow optimizations on LLVM code.
124 if (!DisablePreSelect)
126 PM.add(createPreSelectionPass(*this));
127 PM.add(createReassociatePass());
128 PM.add(createGCSEPass());
129 PM.add(createLICMPass());
132 PM.add(createInstructionSelectionPass(*this));
135 PM.add(createInstructionSchedulingWithSSAPass(*this));
137 PM.add(getRegisterAllocator(*this));
139 PM.add(getPrologEpilogInsertionPass());
141 if (!DisablePeephole)
142 PM.add(createPeepholeOptsPass(*this));
144 PM.add(MappingInfoForFunction(Out));
146 // Output assembly language to the .s file. Assembly emission is split into
147 // two parts: Function output and Global value output. This is because
148 // function output is pipelined with all of the rest of code generation stuff,
149 // allowing machine code representations for functions to be free'd after the
150 // function has been emitted.
152 PM.add(getFunctionAsmPrinterPass(Out));
153 PM.add(new FreeMachineCodeForFunction()); // Free stuff no longer needed
155 // Emit Module level assembly after all of the functions have been processed.
156 PM.add(getModuleAsmPrinterPass(Out));
158 // Emit bytecode to the assembly file into its special section next
159 PM.add(getEmitBytecodeToAsmPass(Out));
160 PM.add(getFunctionInfo(Out));
164 //---------------------------------------------------------------------------
165 // class MachineInstructionInfo
166 // Interface to description of machine instructions
167 //---------------------------------------------------------------------------
171 MachineInstrInfo::MachineInstrInfo(const TargetMachine& tgt,
172 const MachineInstrDescriptor* _desc,
173 unsigned int _descSize,
174 unsigned int _numRealOpCodes)
176 desc(_desc), descSize(_descSize), numRealOpCodes(_numRealOpCodes)
178 // FIXME: TargetInstrDescriptors should not be global
179 assert(TargetInstrDescriptors == NULL && desc != NULL);
180 TargetInstrDescriptors = desc; // initialize global variable
184 MachineInstrInfo::~MachineInstrInfo()
186 TargetInstrDescriptors = NULL; // reset global variable
191 MachineInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
192 int64_t intValue) const
194 // First, check if opCode has an immed field.
196 uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended);
197 if (maxImmedValue != 0)
199 // NEED TO HANDLE UNSIGNED VALUES SINCE THEY MAY BECOME MUCH
200 // SMALLER AFTER CASTING TO SIGN-EXTENDED int, short, or char.
201 // See CreateUIntSetInstruction in SparcInstrInfo.cpp.
203 // Now check if the constant fits
204 if (intValue <= (int64_t) maxImmedValue &&
205 intValue >= -((int64_t) maxImmedValue+1))
213 //---------------------------------------------------------------------------
214 // class MachineCacheInfo
217 // Describes properties of the target cache architecture.
218 //---------------------------------------------------------------------------
221 MachineCacheInfo::MachineCacheInfo(const TargetMachine& tgt)
228 MachineCacheInfo::Initialize()
231 cacheLineSizes.push_back(16); cacheLineSizes.push_back(32);
232 cacheSizes.push_back(1 << 15); cacheSizes.push_back(1 << 20);
233 cacheAssoc.push_back(1); cacheAssoc.push_back(4);