blob: 84149f8934b131a9b2061b9e0057d776cde7b1e3 [file] [log] [blame]
Chris Lattnerb26bcc52001-09-14 05:34:53 +00001//===-- TargetMachine.cpp - General Target Information ---------------------==//
2//
3// This file describes the general parts of a Target machine.
Vikram S. Adve4a48c332001-11-09 02:20:18 +00004// This file also implements MachineInstrInfo and MachineCacheInfo.
Chris Lattnerb26bcc52001-09-14 05:34:53 +00005//
6//===----------------------------------------------------------------------===//
Vikram S. Advedaae6992001-07-21 12:42:08 +00007
Vikram S. Adve4a48c332001-11-09 02:20:18 +00008#include "llvm/Target/TargetMachine.h"
Vikram S. Adve0799fc42001-09-18 12:58:33 +00009#include "llvm/Target/MachineInstrInfo.h"
Vikram S. Adve4a48c332001-11-09 02:20:18 +000010#include "llvm/Target/MachineCacheInfo.h"
Vikram S. Advee1f72802002-09-16 15:39:26 +000011#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"
Chris Lattner68498ce2001-07-21 23:24:48 +000023#include "llvm/DerivedTypes.h"
Vikram S. Advedaae6992001-07-21 12:42:08 +000024
Vikram S. Advedaae6992001-07-21 12:42:08 +000025//---------------------------------------------------------------------------
Vikram S. Advee1f72802002-09-16 15:39:26 +000026// Command line options to control choice of code generation passes.
27//---------------------------------------------------------------------------
28
29static cl::opt<bool> DisablePreSelect("nopreselect",
30 cl::desc("Disable preselection pass"));
31
32static cl::opt<bool> DisableSched("nosched",
33 cl::desc("Disable local scheduling pass"));
34
35//---------------------------------------------------------------------------
Vikram S. Adve44a853c2001-07-28 04:09:37 +000036// class TargetMachine
Vikram S. Advedaae6992001-07-21 12:42:08 +000037//
38// Purpose:
Vikram S. Adve44a853c2001-07-28 04:09:37 +000039// Machine description.
40//
Vikram S. Advedaae6992001-07-21 12:42:08 +000041//---------------------------------------------------------------------------
42
Vikram S. Adve0799fc42001-09-18 12:58:33 +000043
Vikram S. Adve44a853c2001-07-28 04:09:37 +000044// function TargetMachine::findOptimalStorageSize
45//
46// Purpose:
Vikram S. Adve44a853c2001-07-28 04:09:37 +000047// 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//
Vikram S. Adve0799fc42001-09-18 12:58:33 +000051unsigned int
52TargetMachine::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;
Vikram S. Advedaae6992001-07-21 12:42:08 +000062
Vikram S. Adve0799fc42001-09-18 12:58:33 +000063 default:
64 return DataLayout.getTypeSize(ty);
65 }
Vikram S. Advedaae6992001-07-21 12:42:08 +000066}
67
Vikram S. Adve44a853c2001-07-28 04:09:37 +000068
Vikram S. Advee1f72802002-09-16 15:39:26 +000069//===---------------------------------------------------------------------===//
70// Default code generation passes.
71//
72// Native code generation for a specified target.
73//===---------------------------------------------------------------------===//
74
75class ConstructMachineCodeForFunction : public FunctionPass {
76 TargetMachine &Target;
77public:
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
90struct 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//
112void
113TargetMachine::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
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000162//---------------------------------------------------------------------------
163// class MachineInstructionInfo
164// Interface to description of machine instructions
165//---------------------------------------------------------------------------
166
167
168/*ctor*/
Vikram S. Adve7a2f1e72001-11-08 05:15:08 +0000169MachineInstrInfo::MachineInstrInfo(const TargetMachine& tgt,
170 const MachineInstrDescriptor* _desc,
Vikram S. Advebf242332001-08-28 23:09:36 +0000171 unsigned int _descSize,
172 unsigned int _numRealOpCodes)
Vikram S. Adve7a2f1e72001-11-08 05:15:08 +0000173 : target(tgt),
174 desc(_desc), descSize(_descSize), numRealOpCodes(_numRealOpCodes)
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000175{
Chris Lattner78a81a22001-09-14 16:08:12 +0000176 // FIXME: TargetInstrDescriptors should not be global
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000177 assert(TargetInstrDescriptors == NULL && desc != NULL);
178 TargetInstrDescriptors = desc; // initialize global variable
179}
180
181
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000182MachineInstrInfo::~MachineInstrInfo()
183{
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000184 TargetInstrDescriptors = NULL; // reset global variable
185}
186
187
188bool
189MachineInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
190 int64_t intValue) const
191{
192 // First, check if opCode has an immed field.
193 bool isSignExtended;
Chris Lattnerc7634612001-09-14 15:43:58 +0000194 uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended);
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000195 if (maxImmedValue != 0)
196 {
Vikram S. Advee1f72802002-09-16 15:39:26 +0000197 // 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
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000201 // Now check if the constant fits
202 if (intValue <= (int64_t) maxImmedValue &&
203 intValue >= -((int64_t) maxImmedValue+1))
204 return true;
205 }
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000206
207 return false;
208}
Vikram S. Adve4a48c332001-11-09 02:20:18 +0000209
210
211//---------------------------------------------------------------------------
212// class MachineCacheInfo
213//
214// Purpose:
215// Describes properties of the target cache architecture.
216//---------------------------------------------------------------------------
217
218/*ctor*/
219MachineCacheInfo::MachineCacheInfo(const TargetMachine& tgt)
220 : target(tgt)
221{
222 Initialize();
223}
224
225void
226MachineCacheInfo::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}