blob: cd171725394a74741e7c2833e8899a8acad74719 [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"
Anand Shuklac0789302002-09-21 05:01:21 +000012#include "llvm/CodeGen/StackSlots.h"
Vikram S. Advee1f72802002-09-16 15:39:26 +000013#include "llvm/CodeGen/InstrSelection.h"
14#include "llvm/CodeGen/InstrScheduling.h"
15#include "llvm/CodeGen/RegisterAllocation.h"
Vikram S. Advee5b25652002-09-20 00:52:43 +000016#include "llvm/CodeGen/PeepholeOpts.h"
Vikram S. Advee1f72802002-09-16 15:39:26 +000017#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"
Chris Lattner68498ce2001-07-21 23:24:48 +000025#include "llvm/DerivedTypes.h"
Vikram S. Advedaae6992001-07-21 12:42:08 +000026
Vikram S. Advedaae6992001-07-21 12:42:08 +000027//---------------------------------------------------------------------------
Vikram S. Advee1f72802002-09-16 15:39:26 +000028// Command line options to control choice of code generation passes.
29//---------------------------------------------------------------------------
30
31static cl::opt<bool> DisablePreSelect("nopreselect",
32 cl::desc("Disable preselection pass"));
33
34static cl::opt<bool> DisableSched("nosched",
35 cl::desc("Disable local scheduling pass"));
36
Vikram S. Advee5b25652002-09-20 00:52:43 +000037static cl::opt<bool> DisablePeephole("nopeephole",
38 cl::desc("Disable peephole optimization pass"));
39
Vikram S. Advee1f72802002-09-16 15:39:26 +000040//---------------------------------------------------------------------------
Vikram S. Adve44a853c2001-07-28 04:09:37 +000041// class TargetMachine
Vikram S. Advedaae6992001-07-21 12:42:08 +000042//
43// Purpose:
Vikram S. Adve44a853c2001-07-28 04:09:37 +000044// Machine description.
45//
Vikram S. Advedaae6992001-07-21 12:42:08 +000046//---------------------------------------------------------------------------
47
Vikram S. Adve0799fc42001-09-18 12:58:33 +000048
Vikram S. Adve44a853c2001-07-28 04:09:37 +000049// function TargetMachine::findOptimalStorageSize
50//
51// Purpose:
Vikram S. Adve44a853c2001-07-28 04:09:37 +000052// 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//
Vikram S. Adve0799fc42001-09-18 12:58:33 +000056unsigned int
57TargetMachine::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;
Vikram S. Advedaae6992001-07-21 12:42:08 +000067
Vikram S. Adve0799fc42001-09-18 12:58:33 +000068 default:
69 return DataLayout.getTypeSize(ty);
70 }
Vikram S. Advedaae6992001-07-21 12:42:08 +000071}
72
Vikram S. Adve44a853c2001-07-28 04:09:37 +000073
Vikram S. Advee1f72802002-09-16 15:39:26 +000074//===---------------------------------------------------------------------===//
75// Default code generation passes.
76//
77// Native code generation for a specified target.
78//===---------------------------------------------------------------------===//
79
80class ConstructMachineCodeForFunction : public FunctionPass {
81 TargetMachine &Target;
82public:
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
95struct 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//
117void
118TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
119{
120 // Construct and initialize the MachineCodeForMethod object for this fn.
121 PM.add(new ConstructMachineCodeForFunction(*this));
122
Anand Shuklac0789302002-09-21 05:01:21 +0000123 //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
Vikram S. Advee1f72802002-09-16 15:39:26 +0000127 // 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));
Vikram S. Advec308aef2002-09-23 12:55:50 +0000132 /* PM.add(createReassociatePass()); */
Vikram S. Advee1f72802002-09-16 15:39:26 +0000133 PM.add(createGCSEPass());
134 PM.add(createLICMPass());
135 }
136
137 PM.add(createInstructionSelectionPass(*this));
138
139 if (!DisableSched)
140 PM.add(createInstructionSchedulingWithSSAPass(*this));
141
142 PM.add(getRegisterAllocator(*this));
143
Vikram S. Advee1f72802002-09-16 15:39:26 +0000144 PM.add(getPrologEpilogInsertionPass());
145
Vikram S. Advee5b25652002-09-20 00:52:43 +0000146 if (!DisablePeephole)
147 PM.add(createPeepholeOptsPass(*this));
148
Vikram S. Advee1f72802002-09-16 15:39:26 +0000149 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
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000169//---------------------------------------------------------------------------
170// class MachineInstructionInfo
171// Interface to description of machine instructions
172//---------------------------------------------------------------------------
173
174
175/*ctor*/
Vikram S. Adve7a2f1e72001-11-08 05:15:08 +0000176MachineInstrInfo::MachineInstrInfo(const TargetMachine& tgt,
177 const MachineInstrDescriptor* _desc,
Vikram S. Advebf242332001-08-28 23:09:36 +0000178 unsigned int _descSize,
179 unsigned int _numRealOpCodes)
Vikram S. Adve7a2f1e72001-11-08 05:15:08 +0000180 : target(tgt),
181 desc(_desc), descSize(_descSize), numRealOpCodes(_numRealOpCodes)
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000182{
Chris Lattner78a81a22001-09-14 16:08:12 +0000183 // FIXME: TargetInstrDescriptors should not be global
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000184 assert(TargetInstrDescriptors == NULL && desc != NULL);
185 TargetInstrDescriptors = desc; // initialize global variable
186}
187
188
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000189MachineInstrInfo::~MachineInstrInfo()
190{
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000191 TargetInstrDescriptors = NULL; // reset global variable
192}
193
194
195bool
196MachineInstrInfo::constantFitsInImmedField(MachineOpCode opCode,
197 int64_t intValue) const
198{
199 // First, check if opCode has an immed field.
200 bool isSignExtended;
Chris Lattnerc7634612001-09-14 15:43:58 +0000201 uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended);
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000202 if (maxImmedValue != 0)
203 {
Vikram S. Advee1f72802002-09-16 15:39:26 +0000204 // 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
Vikram S. Adve0799fc42001-09-18 12:58:33 +0000208 // Now check if the constant fits
209 if (intValue <= (int64_t) maxImmedValue &&
210 intValue >= -((int64_t) maxImmedValue+1))
211 return true;
212 }
Vikram S. Adve44a853c2001-07-28 04:09:37 +0000213
214 return false;
215}
Vikram S. Adve4a48c332001-11-09 02:20:18 +0000216
217
218//---------------------------------------------------------------------------
219// class MachineCacheInfo
220//
221// Purpose:
222// Describes properties of the target cache architecture.
223//---------------------------------------------------------------------------
224
225/*ctor*/
226MachineCacheInfo::MachineCacheInfo(const TargetMachine& tgt)
227 : target(tgt)
228{
229 Initialize();
230}
231
232void
233MachineCacheInfo::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}