comparison lib/CodeGen/LLVMTargetMachine.cpp @ 77:54457678186b LLVM3.6

LLVM 3.6
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Mon, 08 Sep 2014 22:06:00 +0900
parents 95c75e76d11b
children 60c9769439b8
comparison
equal deleted inserted replaced
34:e874dbf0ad9d 77:54457678186b
10 // This file implements the LLVMTargetMachine class. 10 // This file implements the LLVMTargetMachine class.
11 // 11 //
12 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
13 13
14 #include "llvm/Target/TargetMachine.h" 14 #include "llvm/Target/TargetMachine.h"
15 #include "llvm/ADT/OwningPtr.h" 15
16 #include "llvm/Assembly/PrintModulePass.h" 16 #include "llvm/Analysis/Passes.h"
17 #include "llvm/CodeGen/AsmPrinter.h" 17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/JumpInstrTables.h"
18 #include "llvm/CodeGen/MachineFunctionAnalysis.h" 19 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h" 20 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/CodeGen/Passes.h" 21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/IR/IRPrintingPasses.h"
23 #include "llvm/IR/Verifier.h"
21 #include "llvm/MC/MCAsmInfo.h" 24 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h" 25 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCInstrInfo.h" 26 #include "llvm/MC/MCInstrInfo.h"
24 #include "llvm/MC/MCStreamer.h" 27 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSubtargetInfo.h" 28 #include "llvm/MC/MCSubtargetInfo.h"
42 // able to enable or disable fast-isel independently from -O0. 45 // able to enable or disable fast-isel independently from -O0.
43 static cl::opt<cl::boolOrDefault> 46 static cl::opt<cl::boolOrDefault>
44 EnableFastISelOption("fast-isel", cl::Hidden, 47 EnableFastISelOption("fast-isel", cl::Hidden,
45 cl::desc("Enable the \"fast\" instruction selector")); 48 cl::desc("Enable the \"fast\" instruction selector"));
46 49
47 static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
48 cl::desc("Show encoding in .s output"));
49 static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
50 cl::desc("Show instruction structure in .s output"));
51
52 static cl::opt<cl::boolOrDefault>
53 AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
54 cl::init(cl::BOU_UNSET));
55
56 static bool getVerboseAsm() {
57 switch (AsmVerbose) {
58 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
59 case cl::BOU_TRUE: return true;
60 case cl::BOU_FALSE: return false;
61 }
62 llvm_unreachable("Invalid verbose asm state");
63 }
64
65 void LLVMTargetMachine::initAsmInfo() { 50 void LLVMTargetMachine::initAsmInfo() {
66 AsmInfo = TheTarget.createMCAsmInfo(*getRegisterInfo(), TargetTriple); 51 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
52 *getSubtargetImpl()->getRegisterInfo(), getTargetTriple());
67 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, 53 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
68 // and if the old one gets included then MCAsmInfo will be NULL and 54 // and if the old one gets included then MCAsmInfo will be NULL and
69 // we'll crash later. 55 // we'll crash later.
70 // Provide the user with a useful error message about what's wrong. 56 // Provide the user with a useful error message about what's wrong.
71 assert(AsmInfo && "MCAsmInfo not initialized. " 57 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
72 "Make sure you include the correct TargetSelect.h" 58 "Make sure you include the correct TargetSelect.h"
73 "and that InitializeAllTargetMCs() is being invoked!"); 59 "and that InitializeAllTargetMCs() is being invoked!");
60
61 if (Options.DisableIntegratedAS)
62 TmpAsmInfo->setUseIntegratedAssembler(false);
63
64 if (Options.CompressDebugSections)
65 TmpAsmInfo->setCompressDebugSections(true);
66
67 AsmInfo = TmpAsmInfo;
74 } 68 }
75 69
76 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple, 70 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
77 StringRef CPU, StringRef FS, 71 StringRef CPU, StringRef FS,
78 TargetOptions Options, 72 TargetOptions Options,
90 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM, 84 static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
91 PassManagerBase &PM, 85 PassManagerBase &PM,
92 bool DisableVerify, 86 bool DisableVerify,
93 AnalysisID StartAfter, 87 AnalysisID StartAfter,
94 AnalysisID StopAfter) { 88 AnalysisID StopAfter) {
95 // Targets may override createPassConfig to provide a target-specific sublass. 89
90 // Add internal analysis passes from the target machine.
91 TM->addAnalysisPasses(PM);
92
93 // Targets may override createPassConfig to provide a target-specific
94 // subclass.
96 TargetPassConfig *PassConfig = TM->createPassConfig(PM); 95 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
97 PassConfig->setStartStopPasses(StartAfter, StopAfter); 96 PassConfig->setStartStopPasses(StartAfter, StopAfter);
98 97
99 // Set PassConfig options provided by TargetMachine. 98 // Set PassConfig options provided by TargetMachine.
100 PassConfig->setDisableVerify(DisableVerify); 99 PassConfig->setDisableVerify(DisableVerify);
109 108
110 PassConfig->addISelPrepare(); 109 PassConfig->addISelPrepare();
111 110
112 // Install a MachineModuleInfo class, which is an immutable pass that holds 111 // Install a MachineModuleInfo class, which is an immutable pass that holds
113 // all the per-module stuff we're generating, including MCContext. 112 // all the per-module stuff we're generating, including MCContext.
114 MachineModuleInfo *MMI = 113 MachineModuleInfo *MMI = new MachineModuleInfo(
115 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(), 114 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
116 &TM->getTargetLowering()->getObjFileLowering()); 115 &TM->getSubtargetImpl()->getTargetLowering()->getObjFileLowering());
117 PM.add(MMI); 116 PM.add(MMI);
118 117
119 // Set up a MachineFunction for the rest of CodeGen to work on. 118 // Set up a MachineFunction for the rest of CodeGen to work on.
120 PM.add(new MachineFunctionAnalysis(*TM)); 119 PM.add(new MachineFunctionAnalysis(*TM));
121 120
125 EnableFastISelOption != cl::BOU_FALSE)) 124 EnableFastISelOption != cl::BOU_FALSE))
126 TM->setFastISel(true); 125 TM->setFastISel(true);
127 126
128 // Ask the target for an isel. 127 // Ask the target for an isel.
129 if (PassConfig->addInstSelector()) 128 if (PassConfig->addInstSelector())
130 return NULL; 129 return nullptr;
131 130
132 PassConfig->addMachinePasses(); 131 PassConfig->addMachinePasses();
133 132
134 PassConfig->setInitialized(); 133 PassConfig->setInitialized();
135 134
140 formatted_raw_ostream &Out, 139 formatted_raw_ostream &Out,
141 CodeGenFileType FileType, 140 CodeGenFileType FileType,
142 bool DisableVerify, 141 bool DisableVerify,
143 AnalysisID StartAfter, 142 AnalysisID StartAfter,
144 AnalysisID StopAfter) { 143 AnalysisID StopAfter) {
144 // Passes to handle jumptable function annotations. These can't be handled at
145 // JIT time, so we don't add them directly to addPassesToGenerateCode.
146 PM.add(createJumpInstrTableInfoPass());
147 PM.add(createJumpInstrTablesPass(Options.JTType));
148
145 // Add common CodeGen passes. 149 // Add common CodeGen passes.
146 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 150 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
147 StartAfter, StopAfter); 151 StartAfter, StopAfter);
148 if (!Context) 152 if (!Context)
149 return true; 153 return true;
152 // FIXME: The intent is that this should eventually write out a YAML file, 156 // FIXME: The intent is that this should eventually write out a YAML file,
153 // containing the LLVM IR, the machine-level IR (when stopping after a 157 // containing the LLVM IR, the machine-level IR (when stopping after a
154 // machine-level pass), and whatever other information is needed to 158 // machine-level pass), and whatever other information is needed to
155 // deserialize the code and resume compilation. For now, just write the 159 // deserialize the code and resume compilation. For now, just write the
156 // LLVM IR. 160 // LLVM IR.
157 PM.add(createPrintModulePass(&Out)); 161 PM.add(createPrintModulePass(Out));
158 return false; 162 return false;
159 } 163 }
160 164
161 if (hasMCSaveTempLabels()) 165 if (Options.MCOptions.MCSaveTempLabels)
162 Context->setAllowTemporaryLabels(false); 166 Context->setAllowTemporaryLabels(false);
163 167
168 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
164 const MCAsmInfo &MAI = *getMCAsmInfo(); 169 const MCAsmInfo &MAI = *getMCAsmInfo();
165 const MCRegisterInfo &MRI = *getRegisterInfo(); 170 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
166 const MCInstrInfo &MII = *getInstrInfo(); 171 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
167 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>(); 172 std::unique_ptr<MCStreamer> AsmStreamer;
168 OwningPtr<MCStreamer> AsmStreamer;
169 173
170 switch (FileType) { 174 switch (FileType) {
171 case CGFT_AssemblyFile: { 175 case CGFT_AssemblyFile: {
172 MCInstPrinter *InstPrinter = 176 MCInstPrinter *InstPrinter =
173 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, 177 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
174 MII, MRI, STI); 178 MII, MRI, STI);
175 179
176 // Create a code emitter if asked to show the encoding. 180 // Create a code emitter if asked to show the encoding.
177 MCCodeEmitter *MCE = 0; 181 MCCodeEmitter *MCE = nullptr;
178 if (ShowMCEncoding) 182 if (Options.MCOptions.ShowMCEncoding)
179 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context); 183 MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, *Context);
180 184
181 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 185 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
182 TargetCPU); 186 TargetCPU);
183 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out, 187 MCStreamer *S = getTarget().createAsmStreamer(
184 getVerboseAsm(), 188 *Context, Out, Options.MCOptions.AsmVerbose,
185 hasMCUseLoc(), 189 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
186 hasMCUseCFI(), 190 Options.MCOptions.ShowMCInst);
187 hasMCUseDwarfDirectory(),
188 InstPrinter,
189 MCE, MAB,
190 ShowMCInst);
191 AsmStreamer.reset(S); 191 AsmStreamer.reset(S);
192 break; 192 break;
193 } 193 }
194 case CGFT_ObjectFile: { 194 case CGFT_ObjectFile: {
195 // Create the code emitter for the target if it exists. If not, .o file 195 // Create the code emitter for the target if it exists. If not, .o file
196 // emission fails. 196 // emission fails.
197 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI, 197 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, STI,
198 *Context); 198 *Context);
199 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 199 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
200 TargetCPU); 200 TargetCPU);
201 if (MCE == 0 || MAB == 0) 201 if (!MCE || !MAB)
202 return true; 202 return true;
203 203
204 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), 204 AsmStreamer.reset(getTarget().createMCObjectStreamer(
205 *Context, *MAB, Out, 205 getTargetTriple(), *Context, *MAB, Out, MCE, STI,
206 MCE, hasMCRelaxAll(), 206 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
207 hasMCNoExecStack()));
208 AsmStreamer.get()->setAutoInitSections(true);
209 break; 207 break;
210 } 208 }
211 case CGFT_Null: 209 case CGFT_Null:
212 // The Null output is intended for use for performance analysis and testing, 210 // The Null output is intended for use for performance analysis and testing,
213 // not real users. 211 // not real users.
214 AsmStreamer.reset(createNullStreamer(*Context)); 212 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
215 break; 213 break;
216 } 214 }
217 215
218 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 216 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
219 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer); 217 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
220 if (Printer == 0) 218 if (!Printer)
221 return true; 219 return true;
222 220
223 // If successful, createAsmPrinter took ownership of AsmStreamer. 221 // If successful, createAsmPrinter took ownership of AsmStreamer.
224 AsmStreamer.take(); 222 AsmStreamer.release();
225 223
226 PM.add(Printer); 224 PM.add(Printer);
227 225
228 return false; 226 return false;
229 }
230
231 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
232 /// get machine code emitted. This uses a JITCodeEmitter object to handle
233 /// actually outputting the machine code and resolving things like the address
234 /// of functions. This method should return true if machine code emission is
235 /// not supported.
236 ///
237 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
238 JITCodeEmitter &JCE,
239 bool DisableVerify) {
240 // Add common CodeGen passes.
241 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
242 if (!Context)
243 return true;
244
245 addCodeEmitter(PM, JCE);
246
247 return false; // success!
248 } 227 }
249 228
250 /// addPassesToEmitMC - Add passes to the specified pass manager to get 229 /// addPassesToEmitMC - Add passes to the specified pass manager to get
251 /// machine code emitted with the MCJIT. This method returns true if machine 230 /// machine code emitted with the MCJIT. This method returns true if machine
252 /// code is not supported. It fills the MCContext Ctx pointer which can be 231 /// code is not supported. It fills the MCContext Ctx pointer which can be
255 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, 234 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
256 MCContext *&Ctx, 235 MCContext *&Ctx,
257 raw_ostream &Out, 236 raw_ostream &Out,
258 bool DisableVerify) { 237 bool DisableVerify) {
259 // Add common CodeGen passes. 238 // Add common CodeGen passes.
260 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0); 239 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
261 if (!Ctx) 240 if (!Ctx)
262 return true; 241 return true;
263 242
264 if (hasMCSaveTempLabels()) 243 if (Options.MCOptions.MCSaveTempLabels)
265 Ctx->setAllowTemporaryLabels(false); 244 Ctx->setAllowTemporaryLabels(false);
266 245
267 // Create the code emitter for the target if it exists. If not, .o file 246 // Create the code emitter for the target if it exists. If not, .o file
268 // emission fails. 247 // emission fails.
269 const MCRegisterInfo &MRI = *getRegisterInfo(); 248 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
270 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>(); 249 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
271 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI, 250 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
272 STI, *Ctx); 251 *getSubtargetImpl()->getInstrInfo(), MRI, STI, *Ctx);
273 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(), 252 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
274 TargetCPU); 253 TargetCPU);
275 if (MCE == 0 || MAB == 0) 254 if (!MCE || !MAB)
276 return true; 255 return true;
277 256
278 OwningPtr<MCStreamer> AsmStreamer; 257 std::unique_ptr<MCStreamer> AsmStreamer;
279 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx, 258 AsmStreamer.reset(getTarget().createMCObjectStreamer(
280 *MAB, Out, MCE, 259 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
281 hasMCRelaxAll(), 260 Options.MCOptions.MCRelaxAll, Options.MCOptions.MCNoExecStack));
282 hasMCNoExecStack()));
283 AsmStreamer.get()->InitSections();
284 261
285 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. 262 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
286 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer); 263 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
287 if (Printer == 0) 264 if (!Printer)
288 return true; 265 return true;
289 266
290 // If successful, createAsmPrinter took ownership of AsmStreamer. 267 // If successful, createAsmPrinter took ownership of AsmStreamer.
291 AsmStreamer.take(); 268 AsmStreamer.release();
292 269
293 PM.add(Printer); 270 PM.add(Printer);
294 271
295 return false; // success! 272 return false; // success!
296 } 273 }