llc.cpp revision ea8aed7ef38077e17253b15f5e7b52b1b5d1db92
1//===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This is the llc code generator driver. It provides a convenient 11// command-line interface for generating native assembly-language code 12// or C code, given LLVM bytecode. 13// 14//===----------------------------------------------------------------------===// 15 16#include "llvm/Bytecode/Reader.h" 17#include "llvm/Target/SubtargetFeature.h" 18#include "llvm/Target/TargetMachine.h" 19#include "llvm/Target/TargetMachineRegistry.h" 20#include "llvm/Transforms/Scalar.h" 21#include "llvm/Module.h" 22#include "llvm/PassManager.h" 23#include "llvm/Pass.h" 24#include "llvm/Support/CommandLine.h" 25#include "llvm/Support/PluginLoader.h" 26#include "llvm/Support/PassNameParser.h" 27#include "llvm/Support/FileUtilities.h" 28#include "llvm/Analysis/Verifier.h" 29#include "llvm/System/Signals.h" 30#include "llvm/Config/config.h" 31#include <fstream> 32#include <iostream> 33#include <memory> 34#include <cstdio> 35 36using namespace llvm; 37 38// General options for llc. Other pass-specific options are specified 39// within the corresponding llc passes, and target-specific options 40// and back-end code generation options are specified with the target machine. 41// 42static cl::opt<std::string> 43InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-")); 44 45static cl::opt<std::string> 46OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename")); 47 48static cl::opt<bool> Force("f", cl::desc("Overwrite output files")); 49 50static cl::opt<bool> Fast("fast", 51 cl::desc("Generate code quickly, potentially sacrificing code quality")); 52 53static cl::opt<std::string> 54TargetTriple("mtriple", cl::desc("Override target triple for module")); 55 56static cl::opt<const TargetMachineRegistry::Entry*, false, TargetNameParser> 57MArch("march", cl::desc("Architecture to generate code for:")); 58 59static cl::opt<std::string> 60MCPU("mcpu", 61 cl::desc("Target a specific cpu type (-mcpu=help for details)"), 62 cl::value_desc("cpu-name"), 63 cl::init("")); 64 65static cl::list<std::string> 66MAttrs("mattr", 67 cl::CommaSeparated, 68 cl::desc("Target specific attributes (-mattr=help for details)"), 69 cl::value_desc("a1,+a2,-a3,...")); 70 71cl::opt<TargetMachine::CodeGenFileType> 72FileType("filetype", cl::init(TargetMachine::AssemblyFile), 73 cl::desc("Choose a file type (not all types are supported by all targets):"), 74 cl::values( 75 clEnumValN(TargetMachine::AssemblyFile, "asm", 76 " Emit an assembly ('.s') file"), 77 clEnumValN(TargetMachine::ObjectFile, "obj", 78 " Emit a native object ('.o') file [experimental]"), 79 clEnumValN(TargetMachine::DynamicLibrary, "dynlib", 80 " Emit a native dynamic library ('.so') file"), 81 clEnumValEnd)); 82 83// The LLCPassList is populated with passes that were registered using 84// PassInfo::LLC by the FilteredPassNameParser: 85cl::list<const PassInfo*, bool, FilteredPassNameParser<PassInfo::LLC> > 86LLCPassList(cl::desc("Passes Available")); 87 88cl::opt<bool> NoVerify("disable-verify", cl::Hidden, 89 cl::desc("Do not verify input module")); 90 91 92// GetFileNameRoot - Helper function to get the basename of a filename. 93static inline std::string 94GetFileNameRoot(const std::string &InputFilename) { 95 std::string IFN = InputFilename; 96 std::string outputFilename; 97 int Len = IFN.length(); 98 if ((Len > 2) && 99 IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') { 100 outputFilename = std::string(IFN.begin(), IFN.end()-3); // s/.bc/.s/ 101 } else { 102 outputFilename = IFN; 103 } 104 return outputFilename; 105} 106 107 108// main - Entry point for the llc compiler. 109// 110int main(int argc, char **argv) { 111 try { 112 cl::ParseCommandLineOptions(argc, argv, " llvm system compiler\n"); 113 sys::PrintStackTraceOnErrorSignal(); 114 115 // Load the module to be compiled... 116 std::auto_ptr<Module> M(ParseBytecodeFile(InputFilename)); 117 if (M.get() == 0) { 118 std::cerr << argv[0] << ": bytecode didn't read correctly.\n"; 119 return 1; 120 } 121 Module &mod = *M.get(); 122 123 // If we are supposed to override the target triple, do so now. 124 if (!TargetTriple.empty()) 125 mod.setTargetTriple(TargetTriple); 126 127 // Allocate target machine. First, check whether the user has 128 // explicitly specified an architecture to compile for. 129 TargetMachine* (*TargetMachineAllocator)(const Module&, 130 IntrinsicLowering *) = 0; 131 if (MArch == 0) { 132 std::string Err; 133 MArch = TargetMachineRegistry::getClosestStaticTargetForModule(mod, Err); 134 if (MArch == 0) { 135 std::cerr << argv[0] << ": error auto-selecting target for module '" 136 << Err << "'. Please use the -march option to explicitly " 137 << "pick a target.\n"; 138 return 1; 139 } 140 } 141 142 // Package up features to be passed to target/subtarget 143 std::string FeaturesStr; 144 if (MCPU.size() || MAttrs.size()) { 145 SubtargetFeatures Features; 146 Features.setCPU(MCPU); 147 for (unsigned i = 0; i != MAttrs.size(); ++i) 148 Features.AddFeature(MAttrs[i]); 149 FeaturesStr = Features.getString(); 150 } 151 152 std::auto_ptr<TargetMachine> target(MArch->CtorFn(mod, 0, FeaturesStr)); 153 assert(target.get() && "Could not allocate target machine!"); 154 TargetMachine &Target = *target.get(); 155 const TargetData &TD = Target.getTargetData(); 156 157 // Build up all of the passes that we want to do to the module... 158 PassManager Passes; 159 Passes.add(new TargetData(TD)); 160 161 // Create a new pass for each one specified on the command line 162 for (unsigned i = 0; i < LLCPassList.size(); ++i) { 163 const PassInfo *aPass = LLCPassList[i]; 164 165 if (aPass->getNormalCtor()) { 166 Pass *P = aPass->getNormalCtor()(); 167 Passes.add(P); 168 } else { 169 std::cerr << argv[0] << ": cannot create pass: " 170 << aPass->getPassName() << "\n"; 171 } 172 } 173 174#ifndef NDEBUG 175 if(!NoVerify) 176 Passes.add(createVerifierPass()); 177#endif 178 179 // Figure out where we are going to send the output... 180 std::ostream *Out = 0; 181 if (OutputFilename != "") { 182 if (OutputFilename != "-") { 183 // Specified an output filename? 184 if (!Force && std::ifstream(OutputFilename.c_str())) { 185 // If force is not specified, make sure not to overwrite a file! 186 std::cerr << argv[0] << ": error opening '" << OutputFilename 187 << "': file exists!\n" 188 << "Use -f command line argument to force output\n"; 189 return 1; 190 } 191 Out = new std::ofstream(OutputFilename.c_str()); 192 193 // Make sure that the Out file gets unlinked from the disk if we get a 194 // SIGINT 195 sys::RemoveFileOnSignal(sys::Path(OutputFilename)); 196 } else { 197 Out = &std::cout; 198 } 199 } else { 200 if (InputFilename == "-") { 201 OutputFilename = "-"; 202 Out = &std::cout; 203 } else { 204 OutputFilename = GetFileNameRoot(InputFilename); 205 206 switch (FileType) { 207 case TargetMachine::AssemblyFile: 208 if (MArch->Name[0] != 'c' || MArch->Name[1] != 0) // not CBE 209 OutputFilename += ".s"; 210 else 211 OutputFilename += ".cbe.c"; 212 break; 213 case TargetMachine::ObjectFile: 214 OutputFilename += ".o"; 215 break; 216 case TargetMachine::DynamicLibrary: 217 OutputFilename += LTDL_SHLIB_EXT; 218 break; 219 } 220 221 if (!Force && std::ifstream(OutputFilename.c_str())) { 222 // If force is not specified, make sure not to overwrite a file! 223 std::cerr << argv[0] << ": error opening '" << OutputFilename 224 << "': file exists!\n" 225 << "Use -f command line argument to force output\n"; 226 return 1; 227 } 228 229 Out = new std::ofstream(OutputFilename.c_str()); 230 if (!Out->good()) { 231 std::cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; 232 delete Out; 233 return 1; 234 } 235 236 // Make sure that the Out file gets unlinked from the disk if we get a 237 // SIGINT 238 sys::RemoveFileOnSignal(sys::Path(OutputFilename)); 239 } 240 } 241 242 // Ask the target to add backend passes as necessary. 243 if (Target.addPassesToEmitFile(Passes, *Out, FileType, Fast)) { 244 std::cerr << argv[0] << ": target '" << Target.getName() 245 << "' does not support generation of this file type!\n"; 246 if (Out != &std::cout) delete Out; 247 // And the Out file is empty and useless, so remove it now. 248 sys::Path(OutputFilename).eraseFromDisk(); 249 return 1; 250 } else { 251 // Run our queue of passes all at once now, efficiently. 252 Passes.run(*M.get()); 253 } 254 255 // Delete the ostream if it's not a stdout stream 256 if (Out != &std::cout) delete Out; 257 258 return 0; 259 } catch (const std::string& msg) { 260 std::cerr << argv[0] << ": " << msg << "\n"; 261 } catch (...) { 262 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n"; 263 } 264 return 1; 265} 266