xref: /freebsd/contrib/llvm-project/llvm/tools/llc/llc.cpp (revision b4e38a41f584ad4391c04b8cfec81f46176b18b0)
1 //===-- llc.cpp - Implement the LLVM Native Code Generator ----------------===//
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 is the llc code generator driver. It provides a convenient
10 // command-line interface for generating native assembly-language code
11 // or C code, given LLVM bitcode.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Analysis/TargetLibraryInfo.h"
18 #include "llvm/CodeGen/CommandFlags.inc"
19 #include "llvm/CodeGen/LinkAllAsmWriterComponents.h"
20 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
21 #include "llvm/CodeGen/MIRParser/MIRParser.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/TargetPassConfig.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/AutoUpgrade.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DiagnosticInfo.h"
29 #include "llvm/IR/DiagnosticPrinter.h"
30 #include "llvm/IR/IRPrintingPasses.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/RemarkStreamer.h"
35 #include "llvm/IR/Verifier.h"
36 #include "llvm/IRReader/IRReader.h"
37 #include "llvm/InitializePasses.h"
38 #include "llvm/MC/SubtargetFeature.h"
39 #include "llvm/Pass.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/FileSystem.h"
43 #include "llvm/Support/FormattedStream.h"
44 #include "llvm/Support/Host.h"
45 #include "llvm/Support/InitLLVM.h"
46 #include "llvm/Support/ManagedStatic.h"
47 #include "llvm/Support/PluginLoader.h"
48 #include "llvm/Support/SourceMgr.h"
49 #include "llvm/Support/TargetRegistry.h"
50 #include "llvm/Support/TargetSelect.h"
51 #include "llvm/Support/ToolOutputFile.h"
52 #include "llvm/Support/WithColor.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include "llvm/Transforms/Utils/Cloning.h"
55 #include <memory>
56 using namespace llvm;
57 
58 // General options for llc.  Other pass-specific options are specified
59 // within the corresponding llc passes, and target-specific options
60 // and back-end code generation options are specified with the target machine.
61 //
62 static cl::opt<std::string>
63 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
64 
65 static cl::opt<std::string>
66 InputLanguage("x", cl::desc("Input language ('ir' or 'mir')"));
67 
68 static cl::opt<std::string>
69 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
70 
71 static cl::opt<std::string>
72     SplitDwarfOutputFile("split-dwarf-output",
73                          cl::desc(".dwo output filename"),
74                          cl::value_desc("filename"));
75 
76 static cl::opt<unsigned>
77 TimeCompilations("time-compilations", cl::Hidden, cl::init(1u),
78                  cl::value_desc("N"),
79                  cl::desc("Repeat compilation N times for timing"));
80 
81 static cl::opt<bool>
82 NoIntegratedAssembler("no-integrated-as", cl::Hidden,
83                       cl::desc("Disable integrated assembler"));
84 
85 static cl::opt<bool>
86     PreserveComments("preserve-as-comments", cl::Hidden,
87                      cl::desc("Preserve Comments in outputted assembly"),
88                      cl::init(true));
89 
90 // Determine optimization level.
91 static cl::opt<char>
92 OptLevel("O",
93          cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
94                   "(default = '-O2')"),
95          cl::Prefix,
96          cl::ZeroOrMore,
97          cl::init(' '));
98 
99 static cl::opt<std::string>
100 TargetTriple("mtriple", cl::desc("Override target triple for module"));
101 
102 static cl::opt<std::string> SplitDwarfFile(
103     "split-dwarf-file",
104     cl::desc(
105         "Specify the name of the .dwo file to encode in the DWARF output"));
106 
107 static cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
108                               cl::desc("Do not verify input module"));
109 
110 static cl::opt<bool> DisableSimplifyLibCalls("disable-simplify-libcalls",
111                                              cl::desc("Disable simplify-libcalls"));
112 
113 static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
114                                     cl::desc("Show encoding in .s output"));
115 
116 static cl::opt<bool> EnableDwarfDirectory(
117     "enable-dwarf-directory", cl::Hidden,
118     cl::desc("Use .file directives with an explicit directory."));
119 
120 static cl::opt<bool> AsmVerbose("asm-verbose",
121                                 cl::desc("Add comments to directives."),
122                                 cl::init(true));
123 
124 static cl::opt<bool>
125     CompileTwice("compile-twice", cl::Hidden,
126                  cl::desc("Run everything twice, re-using the same pass "
127                           "manager and verify the result is the same."),
128                  cl::init(false));
129 
130 static cl::opt<bool> DiscardValueNames(
131     "discard-value-names",
132     cl::desc("Discard names from Value (other than GlobalValue)."),
133     cl::init(false), cl::Hidden);
134 
135 static cl::list<std::string> IncludeDirs("I", cl::desc("include search path"));
136 
137 static cl::opt<bool> RemarksWithHotness(
138     "pass-remarks-with-hotness",
139     cl::desc("With PGO, include profile count in optimization remarks"),
140     cl::Hidden);
141 
142 static cl::opt<unsigned>
143     RemarksHotnessThreshold("pass-remarks-hotness-threshold",
144                             cl::desc("Minimum profile count required for "
145                                      "an optimization remark to be output"),
146                             cl::Hidden);
147 
148 static cl::opt<std::string>
149     RemarksFilename("pass-remarks-output",
150                     cl::desc("Output filename for pass remarks"),
151                     cl::value_desc("filename"));
152 
153 static cl::opt<std::string>
154     RemarksPasses("pass-remarks-filter",
155                   cl::desc("Only record optimization remarks from passes whose "
156                            "names match the given regular expression"),
157                   cl::value_desc("regex"));
158 
159 static cl::opt<std::string> RemarksFormat(
160     "pass-remarks-format",
161     cl::desc("The format used for serializing remarks (default: YAML)"),
162     cl::value_desc("format"), cl::init("yaml"));
163 
164 namespace {
165 static ManagedStatic<std::vector<std::string>> RunPassNames;
166 
167 struct RunPassOption {
168   void operator=(const std::string &Val) const {
169     if (Val.empty())
170       return;
171     SmallVector<StringRef, 8> PassNames;
172     StringRef(Val).split(PassNames, ',', -1, false);
173     for (auto PassName : PassNames)
174       RunPassNames->push_back(PassName);
175   }
176 };
177 }
178 
179 static RunPassOption RunPassOpt;
180 
181 static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass(
182     "run-pass",
183     cl::desc("Run compiler only for specified passes (comma separated list)"),
184     cl::value_desc("pass-name"), cl::ZeroOrMore, cl::location(RunPassOpt));
185 
186 static int compileModule(char **, LLVMContext &);
187 
188 static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
189                                                        Triple::OSType OS,
190                                                        const char *ProgName) {
191   // If we don't yet have an output filename, make one.
192   if (OutputFilename.empty()) {
193     if (InputFilename == "-")
194       OutputFilename = "-";
195     else {
196       // If InputFilename ends in .bc or .ll, remove it.
197       StringRef IFN = InputFilename;
198       if (IFN.endswith(".bc") || IFN.endswith(".ll"))
199         OutputFilename = IFN.drop_back(3);
200       else if (IFN.endswith(".mir"))
201         OutputFilename = IFN.drop_back(4);
202       else
203         OutputFilename = IFN;
204 
205       switch (FileType) {
206       case CGFT_AssemblyFile:
207         if (TargetName[0] == 'c') {
208           if (TargetName[1] == 0)
209             OutputFilename += ".cbe.c";
210           else if (TargetName[1] == 'p' && TargetName[2] == 'p')
211             OutputFilename += ".cpp";
212           else
213             OutputFilename += ".s";
214         } else
215           OutputFilename += ".s";
216         break;
217       case CGFT_ObjectFile:
218         if (OS == Triple::Win32)
219           OutputFilename += ".obj";
220         else
221           OutputFilename += ".o";
222         break;
223       case CGFT_Null:
224         OutputFilename += ".null";
225         break;
226       }
227     }
228   }
229 
230   // Decide if we need "binary" output.
231   bool Binary = false;
232   switch (FileType) {
233   case CGFT_AssemblyFile:
234     break;
235   case CGFT_ObjectFile:
236   case CGFT_Null:
237     Binary = true;
238     break;
239   }
240 
241   // Open the file.
242   std::error_code EC;
243   sys::fs::OpenFlags OpenFlags = sys::fs::OF_None;
244   if (!Binary)
245     OpenFlags |= sys::fs::OF_Text;
246   auto FDOut = std::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags);
247   if (EC) {
248     WithColor::error() << EC.message() << '\n';
249     return nullptr;
250   }
251 
252   return FDOut;
253 }
254 
255 struct LLCDiagnosticHandler : public DiagnosticHandler {
256   bool *HasError;
257   LLCDiagnosticHandler(bool *HasErrorPtr) : HasError(HasErrorPtr) {}
258   bool handleDiagnostics(const DiagnosticInfo &DI) override {
259     if (DI.getSeverity() == DS_Error)
260       *HasError = true;
261 
262     if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI))
263       if (!Remark->isEnabled())
264         return true;
265 
266     DiagnosticPrinterRawOStream DP(errs());
267     errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
268     DI.print(DP);
269     errs() << "\n";
270     return true;
271   }
272 };
273 
274 static void InlineAsmDiagHandler(const SMDiagnostic &SMD, void *Context,
275                                  unsigned LocCookie) {
276   bool *HasError = static_cast<bool *>(Context);
277   if (SMD.getKind() == SourceMgr::DK_Error)
278     *HasError = true;
279 
280   SMD.print(nullptr, errs());
281 
282   // For testing purposes, we print the LocCookie here.
283   if (LocCookie)
284     WithColor::note() << "!srcloc = " << LocCookie << "\n";
285 }
286 
287 // main - Entry point for the llc compiler.
288 //
289 int main(int argc, char **argv) {
290   InitLLVM X(argc, argv);
291 
292   // Enable debug stream buffering.
293   EnableDebugBuffering = true;
294 
295   LLVMContext Context;
296 
297   // Initialize targets first, so that --version shows registered targets.
298   InitializeAllTargets();
299   InitializeAllTargetMCs();
300   InitializeAllAsmPrinters();
301   InitializeAllAsmParsers();
302 
303   // Initialize codegen and IR passes used by llc so that the -print-after,
304   // -print-before, and -stop-after options work.
305   PassRegistry *Registry = PassRegistry::getPassRegistry();
306   initializeCore(*Registry);
307   initializeCodeGen(*Registry);
308   initializeLoopStrengthReducePass(*Registry);
309   initializeLowerIntrinsicsPass(*Registry);
310   initializeEntryExitInstrumenterPass(*Registry);
311   initializePostInlineEntryExitInstrumenterPass(*Registry);
312   initializeUnreachableBlockElimLegacyPassPass(*Registry);
313   initializeConstantHoistingLegacyPassPass(*Registry);
314   initializeScalarOpts(*Registry);
315   initializeVectorization(*Registry);
316   initializeScalarizeMaskedMemIntrinPass(*Registry);
317   initializeExpandReductionsPass(*Registry);
318   initializeHardwareLoopsPass(*Registry);
319 
320   // Initialize debugging passes.
321   initializeScavengerTestPass(*Registry);
322 
323   // Register the target printer for --version.
324   cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
325 
326   cl::ParseCommandLineOptions(argc, argv, "llvm system compiler\n");
327 
328   Context.setDiscardValueNames(DiscardValueNames);
329 
330   // Set a diagnostic handler that doesn't exit on the first error
331   bool HasError = false;
332   Context.setDiagnosticHandler(
333       std::make_unique<LLCDiagnosticHandler>(&HasError));
334   Context.setInlineAsmDiagnosticHandler(InlineAsmDiagHandler, &HasError);
335 
336   Expected<std::unique_ptr<ToolOutputFile>> RemarksFileOrErr =
337       setupOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
338                                RemarksFormat, RemarksWithHotness,
339                                RemarksHotnessThreshold);
340   if (Error E = RemarksFileOrErr.takeError()) {
341     WithColor::error(errs(), argv[0]) << toString(std::move(E)) << '\n';
342     return 1;
343   }
344   std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
345 
346   if (InputLanguage != "" && InputLanguage != "ir" &&
347       InputLanguage != "mir") {
348     WithColor::error(errs(), argv[0])
349         << "input language must be '', 'IR' or 'MIR'\n";
350     return 1;
351   }
352 
353   // Compile the module TimeCompilations times to give better compile time
354   // metrics.
355   for (unsigned I = TimeCompilations; I; --I)
356     if (int RetVal = compileModule(argv, Context))
357       return RetVal;
358 
359   if (RemarksFile)
360     RemarksFile->keep();
361   return 0;
362 }
363 
364 static bool addPass(PassManagerBase &PM, const char *argv0,
365                     StringRef PassName, TargetPassConfig &TPC) {
366   if (PassName == "none")
367     return false;
368 
369   const PassRegistry *PR = PassRegistry::getPassRegistry();
370   const PassInfo *PI = PR->getPassInfo(PassName);
371   if (!PI) {
372     WithColor::error(errs(), argv0)
373         << "run-pass " << PassName << " is not registered.\n";
374     return true;
375   }
376 
377   Pass *P;
378   if (PI->getNormalCtor())
379     P = PI->getNormalCtor()();
380   else {
381     WithColor::error(errs(), argv0)
382         << "cannot create pass: " << PI->getPassName() << "\n";
383     return true;
384   }
385   std::string Banner = std::string("After ") + std::string(P->getPassName());
386   PM.add(P);
387   TPC.printAndVerify(Banner);
388 
389   return false;
390 }
391 
392 static int compileModule(char **argv, LLVMContext &Context) {
393   // Load the module to be compiled...
394   SMDiagnostic Err;
395   std::unique_ptr<Module> M;
396   std::unique_ptr<MIRParser> MIR;
397   Triple TheTriple;
398   std::string CPUStr = getCPUStr(), FeaturesStr = getFeaturesStr();
399 
400   // Set attributes on functions as loaded from MIR from command line arguments.
401   auto setMIRFunctionAttributes = [&CPUStr, &FeaturesStr](Function &F) {
402     setFunctionAttributes(CPUStr, FeaturesStr, F);
403   };
404 
405   bool SkipModule = MCPU == "help" ||
406                     (!MAttrs.empty() && MAttrs.front() == "help");
407 
408   // If user just wants to list available options, skip module loading
409   if (!SkipModule) {
410     if (InputLanguage == "mir" ||
411         (InputLanguage == "" && StringRef(InputFilename).endswith(".mir"))) {
412       MIR = createMIRParserFromFile(InputFilename, Err, Context,
413                                     setMIRFunctionAttributes);
414       if (MIR)
415         M = MIR->parseIRModule();
416     } else
417       M = parseIRFile(InputFilename, Err, Context, false);
418     if (!M) {
419       Err.print(argv[0], WithColor::error(errs(), argv[0]));
420       return 1;
421     }
422 
423     // If we are supposed to override the target triple, do so now.
424     if (!TargetTriple.empty())
425       M->setTargetTriple(Triple::normalize(TargetTriple));
426     TheTriple = Triple(M->getTargetTriple());
427   } else {
428     TheTriple = Triple(Triple::normalize(TargetTriple));
429   }
430 
431   if (TheTriple.getTriple().empty())
432     TheTriple.setTriple(sys::getDefaultTargetTriple());
433 
434   // Get the target specific parser.
435   std::string Error;
436   const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple,
437                                                          Error);
438   if (!TheTarget) {
439     WithColor::error(errs(), argv[0]) << Error;
440     return 1;
441   }
442 
443   CodeGenOpt::Level OLvl = CodeGenOpt::Default;
444   switch (OptLevel) {
445   default:
446     WithColor::error(errs(), argv[0]) << "invalid optimization level.\n";
447     return 1;
448   case ' ': break;
449   case '0': OLvl = CodeGenOpt::None; break;
450   case '1': OLvl = CodeGenOpt::Less; break;
451   case '2': OLvl = CodeGenOpt::Default; break;
452   case '3': OLvl = CodeGenOpt::Aggressive; break;
453   }
454 
455   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
456   Options.DisableIntegratedAS = NoIntegratedAssembler;
457   Options.MCOptions.ShowMCEncoding = ShowMCEncoding;
458   Options.MCOptions.MCUseDwarfDirectory = EnableDwarfDirectory;
459   Options.MCOptions.AsmVerbose = AsmVerbose;
460   Options.MCOptions.PreserveAsmComments = PreserveComments;
461   Options.MCOptions.IASSearchPaths = IncludeDirs;
462   Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
463 
464   std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
465       TheTriple.getTriple(), CPUStr, FeaturesStr, Options, getRelocModel(),
466       getCodeModel(), OLvl));
467 
468   assert(Target && "Could not allocate target machine!");
469 
470   // If we don't have a module then just exit now. We do this down
471   // here since the CPU/Feature help is underneath the target machine
472   // creation.
473   if (SkipModule)
474     return 0;
475 
476   assert(M && "Should have exited if we didn't have a module!");
477   if (FloatABIForCalls != FloatABI::Default)
478     Options.FloatABIType = FloatABIForCalls;
479 
480   // Figure out where we are going to send the output.
481   std::unique_ptr<ToolOutputFile> Out =
482       GetOutputStream(TheTarget->getName(), TheTriple.getOS(), argv[0]);
483   if (!Out) return 1;
484 
485   std::unique_ptr<ToolOutputFile> DwoOut;
486   if (!SplitDwarfOutputFile.empty()) {
487     std::error_code EC;
488     DwoOut = std::make_unique<ToolOutputFile>(SplitDwarfOutputFile, EC,
489                                                sys::fs::OF_None);
490     if (EC) {
491       WithColor::error(errs(), argv[0]) << EC.message() << '\n';
492       return 1;
493     }
494   }
495 
496   // Build up all of the passes that we want to do to the module.
497   legacy::PassManager PM;
498 
499   // Add an appropriate TargetLibraryInfo pass for the module's triple.
500   TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
501 
502   // The -disable-simplify-libcalls flag actually disables all builtin optzns.
503   if (DisableSimplifyLibCalls)
504     TLII.disableAllFunctions();
505   PM.add(new TargetLibraryInfoWrapperPass(TLII));
506 
507   // Add the target data from the target machine, if it exists, or the module.
508   M->setDataLayout(Target->createDataLayout());
509 
510   // This needs to be done after setting datalayout since it calls verifier
511   // to check debug info whereas verifier relies on correct datalayout.
512   UpgradeDebugInfo(*M);
513 
514   // Verify module immediately to catch problems before doInitialization() is
515   // called on any passes.
516   if (!NoVerify && verifyModule(*M, &errs())) {
517     std::string Prefix =
518         (Twine(argv[0]) + Twine(": ") + Twine(InputFilename)).str();
519     WithColor::error(errs(), Prefix) << "input module is broken!\n";
520     return 1;
521   }
522 
523   // Override function attributes based on CPUStr, FeaturesStr, and command line
524   // flags.
525   setFunctionAttributes(CPUStr, FeaturesStr, *M);
526 
527   if (RelaxAll.getNumOccurrences() > 0 &&
528       FileType != CGFT_ObjectFile)
529     WithColor::warning(errs(), argv[0])
530         << ": warning: ignoring -mc-relax-all because filetype != obj";
531 
532   {
533     raw_pwrite_stream *OS = &Out->os();
534 
535     // Manually do the buffering rather than using buffer_ostream,
536     // so we can memcmp the contents in CompileTwice mode
537     SmallVector<char, 0> Buffer;
538     std::unique_ptr<raw_svector_ostream> BOS;
539     if ((FileType != CGFT_AssemblyFile &&
540          !Out->os().supportsSeeking()) ||
541         CompileTwice) {
542       BOS = std::make_unique<raw_svector_ostream>(Buffer);
543       OS = BOS.get();
544     }
545 
546     const char *argv0 = argv[0];
547     LLVMTargetMachine &LLVMTM = static_cast<LLVMTargetMachine &>(*Target);
548     MachineModuleInfoWrapperPass *MMIWP =
549         new MachineModuleInfoWrapperPass(&LLVMTM);
550 
551     // Construct a custom pass pipeline that starts after instruction
552     // selection.
553     if (!RunPassNames->empty()) {
554       if (!MIR) {
555         WithColor::warning(errs(), argv[0])
556             << "run-pass is for .mir file only.\n";
557         return 1;
558       }
559       TargetPassConfig &TPC = *LLVMTM.createPassConfig(PM);
560       if (TPC.hasLimitedCodeGenPipeline()) {
561         WithColor::warning(errs(), argv[0])
562             << "run-pass cannot be used with "
563             << TPC.getLimitedCodeGenPipelineReason(" and ") << ".\n";
564         return 1;
565       }
566 
567       TPC.setDisableVerify(NoVerify);
568       PM.add(&TPC);
569       PM.add(MMIWP);
570       TPC.printAndVerify("");
571       for (const std::string &RunPassName : *RunPassNames) {
572         if (addPass(PM, argv0, RunPassName, TPC))
573           return 1;
574       }
575       TPC.setInitialized();
576       PM.add(createPrintMIRPass(*OS));
577       PM.add(createFreeMachineFunctionPass());
578     } else if (Target->addPassesToEmitFile(PM, *OS,
579                                            DwoOut ? &DwoOut->os() : nullptr,
580                                            FileType, NoVerify, MMIWP)) {
581       WithColor::warning(errs(), argv[0])
582           << "target does not support generation of this"
583           << " file type!\n";
584       return 1;
585     }
586 
587     if (MIR) {
588       assert(MMIWP && "Forgot to create MMIWP?");
589       if (MIR->parseMachineFunctions(*M, MMIWP->getMMI()))
590         return 1;
591     }
592 
593     // Before executing passes, print the final values of the LLVM options.
594     cl::PrintOptionValues();
595 
596     // If requested, run the pass manager over the same module again,
597     // to catch any bugs due to persistent state in the passes. Note that
598     // opt has the same functionality, so it may be worth abstracting this out
599     // in the future.
600     SmallVector<char, 0> CompileTwiceBuffer;
601     if (CompileTwice) {
602       std::unique_ptr<Module> M2(llvm::CloneModule(*M));
603       PM.run(*M2);
604       CompileTwiceBuffer = Buffer;
605       Buffer.clear();
606     }
607 
608     PM.run(*M);
609 
610     auto HasError =
611         ((const LLCDiagnosticHandler *)(Context.getDiagHandlerPtr()))->HasError;
612     if (*HasError)
613       return 1;
614 
615     // Compare the two outputs and make sure they're the same
616     if (CompileTwice) {
617       if (Buffer.size() != CompileTwiceBuffer.size() ||
618           (memcmp(Buffer.data(), CompileTwiceBuffer.data(), Buffer.size()) !=
619            0)) {
620         errs()
621             << "Running the pass manager twice changed the output.\n"
622                "Writing the result of the second run to the specified output\n"
623                "To generate the one-run comparison binary, just run without\n"
624                "the compile-twice option\n";
625         Out->os() << Buffer;
626         Out->keep();
627         return 1;
628       }
629     }
630 
631     if (BOS) {
632       Out->os() << Buffer;
633     }
634   }
635 
636   // Declare success.
637   Out->keep();
638   if (DwoOut)
639     DwoOut->keep();
640 
641   return 0;
642 }
643