[InstCombine] Signed saturation patterns
[llvm-complete.git] / tools / llvm-as / llvm-as.cpp
blobc9f50e38fc6182b4d2ddae063bc6bf1efbaebb91
1 //===--- llvm-as.cpp - The low-level LLVM assembler -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This utility may be invoked in the following manner:
10 // llvm-as --help - Output information about command line switches
11 // llvm-as [options] - Read LLVM asm from stdin, write bitcode to stdout
12 // llvm-as [options] x.ll - Read LLVM asm from the x.ll file, write bitcode
13 // to the x.bc file.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/AsmParser/Parser.h"
18 #include "llvm/Bitcode/BitcodeWriter.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/ModuleSummaryIndex.h"
22 #include "llvm/IR/Verifier.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/InitLLVM.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/SystemUtils.h"
29 #include "llvm/Support/ToolOutputFile.h"
30 #include <memory>
31 using namespace llvm;
33 cl::OptionCategory AsCat("llvm-as Options");
35 static cl::opt<std::string> InputFilename(cl::Positional,
36 cl::desc("<input .llvm file>"),
37 cl::init("-"));
39 static cl::opt<std::string> OutputFilename("o",
40 cl::desc("Override output filename"),
41 cl::value_desc("filename"),
42 cl::cat(AsCat));
44 static cl::opt<bool> Force("f", cl::desc("Enable binary output on terminals"),
45 cl::cat(AsCat));
47 static cl::opt<bool> DisableOutput("disable-output", cl::desc("Disable output"),
48 cl::init(false), cl::cat(AsCat));
50 static cl::opt<bool> EmitModuleHash("module-hash", cl::desc("Emit module hash"),
51 cl::init(false), cl::cat(AsCat));
53 static cl::opt<bool> DumpAsm("d", cl::desc("Print assembly as parsed"),
54 cl::Hidden, cl::cat(AsCat));
56 static cl::opt<bool>
57 DisableVerify("disable-verify", cl::Hidden,
58 cl::desc("Do not run verifier on input LLVM (dangerous!)"),
59 cl::cat(AsCat));
61 static cl::opt<bool> PreserveBitcodeUseListOrder(
62 "preserve-bc-uselistorder",
63 cl::desc("Preserve use-list order when writing LLVM bitcode."),
64 cl::init(true), cl::Hidden, cl::cat(AsCat));
66 static cl::opt<std::string> ClDataLayout("data-layout",
67 cl::desc("data layout string to use"),
68 cl::value_desc("layout-string"),
69 cl::init(""), cl::cat(AsCat));
71 static void WriteOutputFile(const Module *M, const ModuleSummaryIndex *Index) {
72 // Infer the output filename if needed.
73 if (OutputFilename.empty()) {
74 if (InputFilename == "-") {
75 OutputFilename = "-";
76 } else {
77 StringRef IFN = InputFilename;
78 OutputFilename = (IFN.endswith(".ll") ? IFN.drop_back(3) : IFN).str();
79 OutputFilename += ".bc";
83 std::error_code EC;
84 std::unique_ptr<ToolOutputFile> Out(
85 new ToolOutputFile(OutputFilename, EC, sys::fs::OF_None));
86 if (EC) {
87 errs() << EC.message() << '\n';
88 exit(1);
91 if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) {
92 const ModuleSummaryIndex *IndexToWrite = nullptr;
93 // Don't attempt to write a summary index unless it contains any entries.
94 // Otherwise we get an empty summary section.
95 if (Index && Index->begin() != Index->end())
96 IndexToWrite = Index;
97 if (!IndexToWrite || (M && (!M->empty() || !M->global_empty())))
98 // If we have a non-empty Module, then we write the Module plus
99 // any non-null Index along with it as a per-module Index.
100 // If both are empty, this will give an empty module block, which is
101 // the expected behavior.
102 WriteBitcodeToFile(*M, Out->os(), PreserveBitcodeUseListOrder,
103 IndexToWrite, EmitModuleHash);
104 else
105 // Otherwise, with an empty Module but non-empty Index, we write a
106 // combined index.
107 WriteIndexToFile(*IndexToWrite, Out->os());
110 // Declare success.
111 Out->keep();
114 int main(int argc, char **argv) {
115 InitLLVM X(argc, argv);
116 LLVMContext Context;
117 cl::HideUnrelatedOptions(AsCat);
118 cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
120 // Parse the file now...
121 SMDiagnostic Err;
122 auto ModuleAndIndex = parseAssemblyFileWithIndex(
123 InputFilename, Err, Context, nullptr, !DisableVerify, ClDataLayout);
124 std::unique_ptr<Module> M = std::move(ModuleAndIndex.Mod);
125 if (!M.get()) {
126 Err.print(argv[0], errs());
127 return 1;
129 std::unique_ptr<ModuleSummaryIndex> Index = std::move(ModuleAndIndex.Index);
131 if (!DisableVerify) {
132 std::string ErrorStr;
133 raw_string_ostream OS(ErrorStr);
134 if (verifyModule(*M.get(), &OS)) {
135 errs() << argv[0]
136 << ": assembly parsed, but does not verify as correct!\n";
137 errs() << OS.str();
138 return 1;
140 // TODO: Implement and call summary index verifier.
143 if (DumpAsm) {
144 errs() << "Here's the assembly:\n" << *M.get();
145 if (Index.get() && Index->begin() != Index->end())
146 Index->print(errs());
149 if (!DisableOutput)
150 WriteOutputFile(M.get(), Index.get());
152 return 0;