1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LLVMTargetMachine class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetMachine.h"
15 #include "llvm/PassManager.h"
16 #include "llvm/Analysis/Passes.h"
17 #include "llvm/Analysis/Verifier.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/CodeGen/GCStrategy.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Target/TargetLowering.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/MC/MCAsmInfo.h"
27 #include "llvm/MC/MCInstrInfo.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSubtargetInfo.h"
30 #include "llvm/Target/TargetData.h"
31 #include "llvm/Target/TargetInstrInfo.h"
32 #include "llvm/Target/TargetLowering.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
35 #include "llvm/Target/TargetSubtargetInfo.h"
36 #include "llvm/Transforms/Scalar.h"
37 #include "llvm/ADT/OwningPtr.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Support/FormattedStream.h"
41 #include "llvm/Support/TargetRegistry.h"
44 static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
45 cl::desc("Disable Post Regalloc"));
46 static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
47 cl::desc("Disable branch folding"));
48 static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
49 cl::desc("Disable tail duplication"));
50 static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
51 cl::desc("Disable pre-register allocation tail duplication"));
52 static cl::opt<bool> EnableBlockPlacement("enable-block-placement",
53 cl::Hidden, cl::desc("Enable probability-driven block placement"));
54 static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
55 cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
56 static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden,
57 cl::desc("Disable code placement"));
58 static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
59 cl::desc("Disable Stack Slot Coloring"));
60 static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
61 cl::desc("Disable Machine Dead Code Elimination"));
62 static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
63 cl::desc("Disable Machine LICM"));
64 static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
65 cl::desc("Disable Machine Common Subexpression Elimination"));
66 static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
68 cl::desc("Disable Machine LICM"));
69 static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
70 cl::desc("Disable Machine Sinking"));
71 static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
72 cl::desc("Disable Loop Strength Reduction Pass"));
73 static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
74 cl::desc("Disable Codegen Prepare"));
75 static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
76 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
77 static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
78 cl::desc("Print LLVM IR input to isel pass"));
79 static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
80 cl::desc("Dump garbage collector data"));
81 static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
82 cl::desc("Show encoding in .s output"));
83 static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
84 cl::desc("Show instruction structure in .s output"));
85 static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
86 cl::desc("Verify generated machine code"),
87 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
89 static cl::opt<cl::boolOrDefault>
90 AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
91 cl::init(cl::BOU_UNSET));
93 static bool getVerboseAsm() {
95 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
96 case cl::BOU_TRUE: return true;
97 case cl::BOU_FALSE: return false;
99 llvm_unreachable("Invalid verbose asm state");
102 // Enable or disable FastISel. Both options are needed, because
103 // FastISel is enabled by default with -fast, and we wish to be
104 // able to enable or disable fast-isel independently from -O0.
105 static cl::opt<cl::boolOrDefault>
106 EnableFastISelOption("fast-isel", cl::Hidden,
107 cl::desc("Enable the \"fast\" instruction selector"));
109 LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
110 StringRef CPU, StringRef FS,
111 TargetOptions Options,
112 Reloc::Model RM, CodeModel::Model CM,
113 CodeGenOpt::Level OL)
114 : TargetMachine(T, Triple, CPU, FS, Options) {
115 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
116 AsmInfo = T.createMCAsmInfo(Triple);
117 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
118 // and if the old one gets included then MCAsmInfo will be NULL and
119 // we'll crash later.
120 // Provide the user with a useful error message about what's wrong.
121 assert(AsmInfo && "MCAsmInfo not initialized."
122 "Make sure you include the correct TargetSelect.h"
123 "and that InitializeAllTargetMCs() is being invoked!");
126 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
127 formatted_raw_ostream &Out,
128 CodeGenFileType FileType,
129 bool DisableVerify) {
130 // Add common CodeGen passes.
131 MCContext *Context = 0;
132 if (addCommonCodeGenPasses(PM, DisableVerify, Context))
134 assert(Context != 0 && "Failed to get MCContext");
136 if (hasMCSaveTempLabels())
137 Context->setAllowTemporaryLabels(false);
139 const MCAsmInfo &MAI = *getMCAsmInfo();
140 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
141 OwningPtr<MCStreamer> AsmStreamer;
144 case CGFT_AssemblyFile: {
145 MCInstPrinter *InstPrinter =
146 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI);
148 // Create a code emitter if asked to show the encoding.
149 MCCodeEmitter *MCE = 0;
150 MCAsmBackend *MAB = 0;
151 if (ShowMCEncoding) {
152 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
153 MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI, *Context);
154 MAB = getTarget().createMCAsmBackend(getTargetTriple());
157 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
161 hasMCUseDwarfDirectory(),
165 AsmStreamer.reset(S);
168 case CGFT_ObjectFile: {
169 // Create the code emitter for the target if it exists. If not, .o file
171 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI,
173 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
174 if (MCE == 0 || MAB == 0)
177 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
179 MCE, hasMCRelaxAll(),
180 hasMCNoExecStack()));
181 AsmStreamer.get()->InitSections();
185 // The Null output is intended for use for performance analysis and testing,
187 AsmStreamer.reset(createNullStreamer(*Context));
191 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
192 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
196 // If successful, createAsmPrinter took ownership of AsmStreamer.
201 PM.add(createGCInfoDeleter());
205 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to
206 /// get machine code emitted. This uses a JITCodeEmitter object to handle
207 /// actually outputting the machine code and resolving things like the address
208 /// of functions. This method should returns true if machine code emission is
211 bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
213 bool DisableVerify) {
214 // Add common CodeGen passes.
216 if (addCommonCodeGenPasses(PM, DisableVerify, Ctx))
219 addCodeEmitter(PM, JCE);
220 PM.add(createGCInfoDeleter());
222 return false; // success!
225 /// addPassesToEmitMC - Add passes to the specified pass manager to get
226 /// machine code emitted with the MCJIT. This method returns true if machine
227 /// code is not supported. It fills the MCContext Ctx pointer which can be
228 /// used to build custom MCStreamer.
230 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
233 bool DisableVerify) {
234 // Add common CodeGen passes.
235 if (addCommonCodeGenPasses(PM, DisableVerify, Ctx))
238 if (hasMCSaveTempLabels())
239 Ctx->setAllowTemporaryLabels(false);
241 // Create the code emitter for the target if it exists. If not, .o file
243 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
244 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(),STI, *Ctx);
245 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
246 if (MCE == 0 || MAB == 0)
249 OwningPtr<MCStreamer> AsmStreamer;
250 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
253 hasMCNoExecStack()));
254 AsmStreamer.get()->InitSections();
256 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
257 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
261 // If successful, createAsmPrinter took ownership of AsmStreamer.
266 return false; // success!
269 void LLVMTargetMachine::printNoVerify(PassManagerBase &PM,
270 const char *Banner) const {
271 if (Options.PrintMachineCode)
272 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
275 void LLVMTargetMachine::printAndVerify(PassManagerBase &PM,
276 const char *Banner) const {
277 if (Options.PrintMachineCode)
278 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
280 if (VerifyMachineCode)
281 PM.add(createMachineVerifierPass(Banner));
284 /// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
285 /// emitting to assembly files or machine code output.
287 bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
289 MCContext *&OutContext) {
290 // Standard LLVM-Level Passes.
292 // Basic AliasAnalysis support.
293 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
294 // BasicAliasAnalysis wins if they disagree. This is intended to help
295 // support "obvious" type-punning idioms.
296 PM.add(createTypeBasedAliasAnalysisPass());
297 PM.add(createBasicAliasAnalysisPass());
299 // Before running any passes, run the verifier to determine if the input
300 // coming from the front-end and/or optimizer is valid.
302 PM.add(createVerifierPass());
304 // Run loop strength reduction before anything else.
305 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
306 PM.add(createLoopStrengthReducePass(getTargetLowering()));
308 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
311 PM.add(createGCLoweringPass());
313 // Make sure that no unreachable blocks are instruction selected.
314 PM.add(createUnreachableBlockEliminationPass());
316 // Turn exception handling constructs into something the code generators can
318 switch (getMCAsmInfo()->getExceptionHandlingType()) {
319 case ExceptionHandling::SjLj:
320 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
321 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
322 // catch info can get misplaced when a selector ends up more than one block
323 // removed from the parent invoke(s). This could happen when a landing
324 // pad is shared by multiple invokes and is also a target of a normal
325 // edge from elsewhere.
326 PM.add(createSjLjEHPass(getTargetLowering()));
328 case ExceptionHandling::DwarfCFI:
329 case ExceptionHandling::ARM:
330 case ExceptionHandling::Win64:
331 PM.add(createDwarfEHPass(this));
333 case ExceptionHandling::None:
334 PM.add(createLowerInvokePass(getTargetLowering()));
336 // The lower invoke pass may create unreachable code. Remove it.
337 PM.add(createUnreachableBlockEliminationPass());
341 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
342 PM.add(createCodeGenPreparePass(getTargetLowering()));
344 PM.add(createStackProtectorPass(getTargetLowering()));
349 PM.add(createPrintFunctionPass("\n\n"
350 "*** Final LLVM Code input to ISel ***\n",
353 // All passes which modify the LLVM IR are now complete; run the verifier
354 // to ensure that the IR is valid.
356 PM.add(createVerifierPass());
358 // Standard Lower-Level Passes.
360 // Install a MachineModuleInfo class, which is an immutable pass that holds
361 // all the per-module stuff we're generating, including MCContext.
362 MachineModuleInfo *MMI =
363 new MachineModuleInfo(*getMCAsmInfo(), *getRegisterInfo(),
364 &getTargetLowering()->getObjFileLowering());
366 OutContext = &MMI->getContext(); // Return the MCContext specifically by-ref.
368 // Set up a MachineFunction for the rest of CodeGen to work on.
369 PM.add(new MachineFunctionAnalysis(*this));
371 // Enable FastISel with -fast, but allow that to be overridden.
372 if (EnableFastISelOption == cl::BOU_TRUE ||
373 (getOptLevel() == CodeGenOpt::None &&
374 EnableFastISelOption != cl::BOU_FALSE))
375 Options.EnableFastISel = true;
377 // Ask the target for an isel.
378 if (addInstSelector(PM))
381 // Print the instruction selected machine code...
382 printAndVerify(PM, "After Instruction Selection");
384 // Expand pseudo-instructions emitted by ISel.
385 PM.add(createExpandISelPseudosPass());
387 // Pre-ra tail duplication.
388 if (getOptLevel() != CodeGenOpt::None && !DisableEarlyTailDup) {
389 PM.add(createTailDuplicatePass(true));
390 printAndVerify(PM, "After Pre-RegAlloc TailDuplicate");
393 // Optimize PHIs before DCE: removing dead PHI cycles may make more
394 // instructions dead.
395 if (getOptLevel() != CodeGenOpt::None)
396 PM.add(createOptimizePHIsPass());
398 // If the target requests it, assign local variables to stack slots relative
399 // to one another and simplify frame index references where possible.
400 PM.add(createLocalStackSlotAllocationPass());
402 if (getOptLevel() != CodeGenOpt::None) {
403 // With optimization, dead code should already be eliminated. However
404 // there is one known exception: lowered code for arguments that are only
405 // used by tail calls, where the tail calls reuse the incoming stack
406 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
407 if (!DisableMachineDCE)
408 PM.add(createDeadMachineInstructionElimPass());
409 printAndVerify(PM, "After codegen DCE pass");
411 if (!DisableMachineLICM)
412 PM.add(createMachineLICMPass());
413 if (!DisableMachineCSE)
414 PM.add(createMachineCSEPass());
415 if (!DisableMachineSink)
416 PM.add(createMachineSinkingPass());
417 printAndVerify(PM, "After Machine LICM, CSE and Sinking passes");
419 PM.add(createPeepholeOptimizerPass());
420 printAndVerify(PM, "After codegen peephole optimization pass");
423 // Run pre-ra passes.
424 if (addPreRegAlloc(PM))
425 printAndVerify(PM, "After PreRegAlloc passes");
427 // Perform register allocation.
428 PM.add(createRegisterAllocator(getOptLevel()));
429 printAndVerify(PM, "After Register Allocation");
431 // Perform stack slot coloring and post-ra machine LICM.
432 if (getOptLevel() != CodeGenOpt::None) {
433 // FIXME: Re-enable coloring with register when it's capable of adding
436 PM.add(createStackSlotColoringPass(false));
438 // Run post-ra machine LICM to hoist reloads / remats.
439 if (!DisablePostRAMachineLICM)
440 PM.add(createMachineLICMPass(false));
442 printAndVerify(PM, "After StackSlotColoring and postra Machine LICM");
445 // Run post-ra passes.
446 if (addPostRegAlloc(PM))
447 printAndVerify(PM, "After PostRegAlloc passes");
449 // Insert prolog/epilog code. Eliminate abstract frame index references...
450 PM.add(createPrologEpilogCodeInserter());
451 printAndVerify(PM, "After PrologEpilogCodeInserter");
453 // Branch folding must be run after regalloc and prolog/epilog insertion.
454 if (getOptLevel() != CodeGenOpt::None && !DisableBranchFold) {
455 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
456 printNoVerify(PM, "After BranchFolding");
460 if (getOptLevel() != CodeGenOpt::None && !DisableTailDuplicate) {
461 PM.add(createTailDuplicatePass(false));
462 printNoVerify(PM, "After TailDuplicate");
466 if (getOptLevel() != CodeGenOpt::None) {
467 PM.add(createMachineCopyPropagationPass());
468 printNoVerify(PM, "After copy propagation pass");
471 // Expand pseudo instructions before second scheduling pass.
472 PM.add(createExpandPostRAPseudosPass());
473 printNoVerify(PM, "After ExpandPostRAPseudos");
475 // Run pre-sched2 passes.
476 if (addPreSched2(PM))
477 printNoVerify(PM, "After PreSched2 passes");
479 // Second pass scheduler.
480 if (getOptLevel() != CodeGenOpt::None && !DisablePostRA) {
481 PM.add(createPostRAScheduler(getOptLevel()));
482 printNoVerify(PM, "After PostRAScheduler");
485 PM.add(createGCMachineCodeAnalysisPass());
488 PM.add(createGCInfoPrinter(dbgs()));
490 if (getOptLevel() != CodeGenOpt::None && !DisableCodePlace) {
491 if (EnableBlockPlacement) {
492 // MachineBlockPlacement is an experimental pass which is disabled by
493 // default currently. Eventually it should subsume CodePlacementOpt, so
494 // when enabled, the other is disabled.
495 PM.add(createMachineBlockPlacementPass());
496 printNoVerify(PM, "After MachineBlockPlacement");
498 PM.add(createCodePlacementOptPass());
499 printNoVerify(PM, "After CodePlacementOpt");
502 // Run a separate pass to collect block placement statistics.
503 if (EnableBlockPlacementStats) {
504 PM.add(createMachineBlockPlacementStatsPass());
505 printNoVerify(PM, "After MachineBlockPlacementStats");
509 if (addPreEmitPass(PM))
510 printNoVerify(PM, "After PreEmit passes");