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/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"
25 //---------------------------------------------------------------------------
26 // Command line options to control choice of code generation passes.
27 //---------------------------------------------------------------------------
29 static cl::opt<bool> DisablePreSelect("nopreselect",
30 cl::desc("Disable preselection pass"));
32 static cl::opt<bool> DisableSched("nosched",
33 cl::desc("Disable local scheduling pass"));
35 //---------------------------------------------------------------------------
36 // class TargetMachine
39 // Machine description.
41 //---------------------------------------------------------------------------
44 // function TargetMachine::findOptimalStorageSize
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.
52 TargetMachine::findOptimalStorageSize(const Type* ty) const
54 switch(ty->getPrimitiveID())
59 case Type::UShortTyID:
61 return optSizeForSubWordData;
64 return DataLayout.getTypeSize(ty);
69 //===---------------------------------------------------------------------===//
70 // Default code generation passes.
72 // Native code generation for a specified target.
73 //===---------------------------------------------------------------------===//
75 class ConstructMachineCodeForFunction : public FunctionPass {
76 TargetMachine &Target;
78 inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
80 const char *getPassName() const {
81 return "ConstructMachineCodeForFunction";
84 bool runOnFunction(Function &F) {
85 MachineCodeForMethod::construct(&F, Target);
90 struct FreeMachineCodeForFunction : public FunctionPass {
91 const char *getPassName() const { return "FreeMachineCodeForFunction"; }
93 static void freeMachineCode(Instruction &I) {
94 MachineCodeForInstruction::destroy(&I);
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();
102 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
103 for_each(FI->begin(), FI->end(), freeMachineCode);
109 // addPassesToEmitAssembly - This method controls the entire code generation
110 // process for the ultra sparc.
113 TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
115 // Construct and initialize the MachineCodeForMethod object for this fn.
116 PM.add(new ConstructMachineCodeForFunction(*this));
118 // Specialize LLVM code for this target machine and then
119 // run basic dataflow optimizations on LLVM code.
120 if (!DisablePreSelect)
122 PM.add(createPreSelectionPass(*this));
123 PM.add(createReassociatePass());
124 PM.add(createGCSEPass());
125 PM.add(createLICMPass());
128 PM.add(createInstructionSelectionPass(*this));
131 PM.add(createInstructionSchedulingWithSSAPass(*this));
133 PM.add(getRegisterAllocator(*this));
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.
140 PM.add(getPrologEpilogInsertionPass());
142 PM.add(MappingInfoForFunction(Out));
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.
150 PM.add(getFunctionAsmPrinterPass(Out));
151 PM.add(new FreeMachineCodeForFunction()); // Free stuff no longer needed
153 // Emit Module level assembly after all of the functions have been processed.
154 PM.add(getModuleAsmPrinterPass(Out));
156 // Emit bytecode to the assembly file into its special section next
157 PM.add(getEmitBytecodeToAsmPass(Out));
158 PM.add(getFunctionInfo(Out));
162 //---------------------------------------------------------------------------
163 // class MachineInstructionInfo
164 // Interface to description of machine instructions
165 //---------------------------------------------------------------------------
169 MachineInstrInfo::MachineInstrInfo(const TargetMachine& tgt,
170 const MachineInstrDescriptor* _desc,
171 unsigned int _descSize,
172 unsigned int _numRealOpCodes)
174 desc(_desc), descSize(_descSize), numRealOpCodes(_numRealOpCodes)
176 // FIXME: TargetInstrDescriptors should not be global
177 assert(TargetInstrDescriptors == NULL && desc != NULL);
178 TargetInstrDescriptors = desc; // initialize global variable
182 MachineInstrInfo::~MachineInstrInfo()
184 TargetInstrDescriptors = NULL; // reset global variable
189 MachineInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
190 int64_t intValue) const
192 // First, check if opCode has an immed field.
194 uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended);
195 if (maxImmedValue != 0)
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.
201 // Now check if the constant fits
202 if (intValue <= (int64_t) maxImmedValue &&
203 intValue >= -((int64_t) maxImmedValue+1))
211 //---------------------------------------------------------------------------
212 // class MachineCacheInfo
215 // Describes properties of the target cache architecture.
216 //---------------------------------------------------------------------------
219 MachineCacheInfo::MachineCacheInfo(const TargetMachine& tgt)
226 MachineCacheInfo::Initialize()
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);