+
+ freeSymName(Sym);
+ }
+
+ ValueToValueMapTy VM;
+ LocalValueMaterializer Materializer(Drop);
+ for (GlobalAlias *GA : KeptAliases) {
+ // Gold told us to keep GA. It is possible that a GV usied in the aliasee
+ // expression is being dropped. If that is the case, that GV must be copied.
+ Constant *Aliasee = GA->getAliasee();
+ Constant *Replacement = mapConstantToLocalCopy(Aliasee, VM, &Materializer);
+ GA->setAliasee(Replacement);
+ }
+
+ for (auto *GV : Drop)
+ drop(*GV);
+
+ return Obj.takeModule();
+}
+
+static void runLTOPasses(Module &M, TargetMachine &TM) {
+ if (const DataLayout *DL = TM.getDataLayout())
+ M.setDataLayout(DL);
+
+ legacy::PassManager passes;
+ passes.add(new DataLayoutPass());
+ passes.add(createTargetTransformInfoWrapperPass(TM.getTargetIRAnalysis()));
+
+ PassManagerBuilder PMB;
+ PMB.LibraryInfo = new TargetLibraryInfoImpl(Triple(TM.getTargetTriple()));
+ PMB.Inliner = createFunctionInliningPass();
+ PMB.VerifyInput = true;
+ PMB.VerifyOutput = true;
+ PMB.LoopVectorize = true;
+ PMB.SLPVectorize = true;
+ PMB.populateLTOPassManager(passes);
+ passes.run(M);
+}
+
+static void saveBCFile(StringRef Path, Module &M) {
+ std::error_code EC;
+ raw_fd_ostream OS(Path, EC, sys::fs::OpenFlags::F_None);
+ if (EC)
+ message(LDPL_FATAL, "Failed to write the output file.");
+ WriteBitcodeToFile(&M, OS);
+}
+
+static void codegen(Module &M) {
+ const std::string &TripleStr = M.getTargetTriple();
+ Triple TheTriple(TripleStr);
+
+ std::string ErrMsg;
+ const Target *TheTarget = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
+ if (!TheTarget)
+ message(LDPL_FATAL, "Target not found: %s", ErrMsg.c_str());
+
+ if (unsigned NumOpts = options::extra.size())
+ cl::ParseCommandLineOptions(NumOpts, &options::extra[0]);
+
+ SubtargetFeatures Features;
+ Features.getDefaultSubtargetFeatures(TheTriple);
+ for (const std::string &A : MAttrs)
+ Features.AddFeature(A);
+
+ TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+ std::unique_ptr<TargetMachine> TM(TheTarget->createTargetMachine(
+ TripleStr, options::mcpu, Features.getString(), Options, RelocationModel,
+ CodeModel::Default, CodeGenOpt::Aggressive));
+
+ runLTOPasses(M, *TM);
+
+ if (options::TheOutputType == options::OT_SAVE_TEMPS)
+ saveBCFile(output_name + ".opt.bc", M);
+
+ legacy::PassManager CodeGenPasses;
+ CodeGenPasses.add(new DataLayoutPass());
+
+ SmallString<128> Filename;
+ int FD;
+ if (options::obj_path.empty()) {
+ std::error_code EC =
+ sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
+ if (EC)
+ message(LDPL_FATAL, "Could not create temporary file: %s",
+ EC.message().c_str());
+ } else {
+ Filename = options::obj_path;
+ std::error_code EC =
+ sys::fs::openFileForWrite(Filename.c_str(), FD, sys::fs::F_None);
+ if (EC)
+ message(LDPL_FATAL, "Could not open file: %s", EC.message().c_str());