blob: fee70b00154baf1b77f32c281e0fa5bb4e3cee5a [file] [log] [blame]
Chris Lattner47877052006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner47877052006-09-04 04:16:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetMachine.h"
15#include "llvm/PassManager.h"
16#include "llvm/Pass.h"
Chris Lattner31442f92007-03-31 00:24:43 +000017#include "llvm/Assembly/PrintModulePass.h"
Devang Patel0f54dcb2007-03-06 21:14:09 +000018#include "llvm/Analysis/LoopPass.h"
Chris Lattner47877052006-09-04 04:16:09 +000019#include "llvm/CodeGen/Passes.h"
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.h"
Chris Lattner47877052006-09-04 04:16:09 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesen1532f3d2008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner47877052006-09-04 04:16:09 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattner31442f92007-03-31 00:24:43 +000024#include "llvm/Support/CommandLine.h"
Owen Andersoncb371882008-08-21 00:14:44 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner47877052006-09-04 04:16:09 +000026using namespace llvm;
27
Dan Gohman2c4bf112008-09-25 01:14:49 +000028namespace llvm {
29 bool EnableFastISel;
30}
31
Chris Lattner85ef2542007-06-19 05:47:49 +000032static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
33 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
34static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
35 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000036static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
37 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000038static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39 cl::desc("Dump garbage collector data"));
Chris Lattner85ef2542007-06-19 05:47:49 +000040
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000041// Hidden options to help debugging
42static cl::opt<bool>
43EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
44 cl::desc("Perform sinking on machine code"));
Bill Wendlingcc8f6032008-01-04 08:11:03 +000045static cl::opt<bool>
Evan Cheng3f32d652008-06-04 09:18:41 +000046EnableLICM("machine-licm",
47 cl::init(false), cl::Hidden,
48 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000049
Chris Lattner459525d2008-01-14 19:00:06 +000050// When this works it will be on by default.
51static cl::opt<bool>
52DisablePostRAScheduler("disable-post-RA-scheduler",
53 cl::desc("Disable scheduling after register allocation"),
54 cl::init(true));
55
Dan Gohmandc756852008-10-01 20:39:19 +000056// Enable or disable FastISel. Both options are needed, because
57// FastISel is enabled by default with -fast, and we wish to be
58// able to enable or disable fast-isel independently from -fast.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +000059static cl::opt<cl::boolOrDefault>
Dan Gohmandc756852008-10-01 20:39:19 +000060EnableFastISelOption("fast-isel", cl::Hidden,
61 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman2c4bf112008-09-25 01:14:49 +000062
Bill Wendling2b58ce52008-11-04 02:10:20 +000063// Enable stack protectors.
Bill Wendling80a320d2008-11-04 21:53:09 +000064static cl::opt<SSP::StackProtectorLevel>
65EnableStackProtector("enable-stack-protector",
66 cl::desc("Stack canary protection level: (default: off)"),
67 cl::init(SSP::OFF),
68 cl::values(clEnumValN(SSP::ALL, "all",
69 "All functions get stack protectors."),
70 clEnumValN(SSP::SOME, "some",
71 "Only functions requiring stack protectors get them."),
72 clEnumValN(SSP::OFF, "off",
73 "No functions get stack protectors."),
74 clEnumValEnd));
Bill Wendling2b58ce52008-11-04 02:10:20 +000075
Bill Wendling04523ea2007-02-08 01:36:53 +000076FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000077LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000078 raw_ostream &Out,
Bill Wendling04523ea2007-02-08 01:36:53 +000079 CodeGenFileType FileType,
80 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000081 // Add common CodeGen passes.
82 if (addCommonCodeGenPasses(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000083 return FileModel::Error;
84
Jim Laskey9d4209f2006-11-07 19:33:46 +000085 // Fold redundant debug labels.
86 PM.add(createDebugLabelFoldingPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +000087
88 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000089 PM.add(createMachineFunctionPrinterPass(cerr));
90
Chris Lattner47877052006-09-04 04:16:09 +000091 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000092 PM.add(createMachineFunctionPrinterPass(cerr));
93
Devang Patel4ae641f2008-10-01 23:18:38 +000094 if (!Fast)
Evan Chengd703ed62008-02-28 23:29:57 +000095 PM.add(createLoopAlignerPass());
96
Chris Lattner47877052006-09-04 04:16:09 +000097 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000098 default:
99 break;
100 case TargetMachine::AssemblyFile:
101 if (addAssemblyEmitter(PM, Fast, Out))
102 return FileModel::Error;
103 return FileModel::AsmFile;
104 case TargetMachine::ObjectFile:
105 if (getMachOWriterInfo())
106 return FileModel::MachOFile;
107 else if (getELFWriterInfo())
108 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000109 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000110
111 return FileModel::Error;
112}
Dan Gohman02dae4b2008-09-25 00:37:07 +0000113
Bill Wendling04523ea2007-02-08 01:36:53 +0000114/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
115/// be split up (e.g., to add an object writer pass), this method can be used to
116/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +0000117bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000118 MachineCodeEmitter *MCE,
119 bool Fast) {
120 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000121 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000122
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000123 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000124
Chris Lattner47877052006-09-04 04:16:09 +0000125 // Delete machine code for this function
126 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000127
Chris Lattner47877052006-09-04 04:16:09 +0000128 return false; // success!
129}
130
131/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
132/// get machine code emitted. This uses a MachineCodeEmitter object to handle
133/// actually outputting the machine code and resolving things like the address
134/// of functions. This method should returns true if machine code emission is
135/// not supported.
136///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000137bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000138 MachineCodeEmitter &MCE,
139 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000140 // Add common CodeGen passes.
141 if (addCommonCodeGenPasses(PM, Fast))
142 return true;
143
144 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
145 PM.add(createMachineFunctionPrinterPass(cerr));
146
147 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
148
149 PM.add(createGCInfoDeleter());
150
151 // Delete machine code for this function
152 PM.add(createMachineCodeDeleter());
153
154 return false; // success!
155}
156
157/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
158/// both emitting to assembly files or machine code output.
159///
160bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +0000161 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000162
Chris Lattner47877052006-09-04 04:16:09 +0000163 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000164 if (!Fast) {
165 PM.add(createLoopStrengthReducePass(getTargetLowering()));
166 if (PrintLSR)
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000167 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000168 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000169
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000170 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000171
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000172 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000173 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000174
Chris Lattner47877052006-09-04 04:16:09 +0000175 // Make sure that no unreachable blocks are instruction selected.
176 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000177
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000178 if (!Fast)
179 PM.add(createCodeGenPreparePass(getTargetLowering()));
180
Bill Wendling80a320d2008-11-04 21:53:09 +0000181 if (EnableStackProtector != SSP::OFF)
182 PM.add(createStackProtectorPass(EnableStackProtector, getTargetLowering()));
Bill Wendling2b58ce52008-11-04 02:10:20 +0000183
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000184 if (PrintISelInput)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000185 PM.add(createPrintFunctionPass("\n\n"
186 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000187 &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000188
Dan Gohman02dae4b2008-09-25 00:37:07 +0000189 // Standard Lower-Level Passes.
190
Dan Gohmandc756852008-10-01 20:39:19 +0000191 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000192 if (EnableFastISelOption == cl::BOU_TRUE ||
193 (Fast && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmandc756852008-10-01 20:39:19 +0000194 EnableFastISel = true;
195
Chris Lattner47877052006-09-04 04:16:09 +0000196 // Ask the target for an isel.
197 if (addInstSelector(PM, Fast))
198 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000199
Chris Lattner47877052006-09-04 04:16:09 +0000200 // Print the instruction selected machine code...
201 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000202 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000203
Evan Cheng3f32d652008-06-04 09:18:41 +0000204 if (EnableLICM)
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000205 PM.add(createMachineLICMPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000206
Chris Lattner3c42f122008-01-05 06:14:16 +0000207 if (EnableSinking)
208 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000209
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000210 // Run pre-ra passes.
211 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
212 PM.add(createMachineFunctionPrinterPass(cerr));
213
Evan Cheng3f32d652008-06-04 09:18:41 +0000214 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000215 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000216
217 // Perform stack slot coloring.
Evan Cheng9ef4c532008-06-30 22:33:16 +0000218 if (!Fast)
219 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000220
Dan Gohman02dae4b2008-09-25 00:37:07 +0000221 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000222 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000223
Evan Cheng3f32d652008-06-04 09:18:41 +0000224 // Run post-ra passes.
225 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
226 PM.add(createMachineFunctionPrinterPass(cerr));
227
Dan Gohman02dae4b2008-09-25 00:37:07 +0000228 if (PrintMachineCode)
Evan Cheng3f32d652008-06-04 09:18:41 +0000229 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000230
Christopher Lambada779f2007-07-27 07:36:14 +0000231 PM.add(createLowerSubregsPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000232
Christopher Lambada779f2007-07-27 07:36:14 +0000233 if (PrintMachineCode) // Print the subreg lowered code
234 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000235
Chris Lattner47877052006-09-04 04:16:09 +0000236 // Insert prolog/epilog code. Eliminate abstract frame index references...
237 PM.add(createPrologEpilogCodeInserter());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000238
Evan Cheng3f32d652008-06-04 09:18:41 +0000239 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000240 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000241
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000242 // Second pass scheduler.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000243 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000244 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000245
Chris Lattnere01eaa02006-11-16 01:00:07 +0000246 // Branch folding must be run after regalloc and prolog/epilog insertion.
247 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000248 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000249
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000250 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000251
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000252 if (PrintMachineCode)
253 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000254
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000255 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000256 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000257
Dan Gohman02dae4b2008-09-25 00:37:07 +0000258 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000259}