blob: 16718d7ce647b9f88707371e4c27b869e459faec [file] [log] [blame]
Nick Kledzik07b4a622008-02-26 20:26:43 +00001//===-LTOCodeGenerator.cpp - LLVM Link Time Optimizer ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling39d942b2012-03-31 10:50:14 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Bill Wendling39d942b2012-03-31 10:50:14 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000015#include "llvm/LTO/LTOCodeGenerator.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000016#include "llvm/ADT/StringExtras.h"
Nick Lewycky510dae32009-06-17 06:52:10 +000017#include "llvm/Analysis/Passes.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000018#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000019#include "llvm/Analysis/TargetTransformInfo.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000020#include "llvm/Bitcode/ReaderWriter.h"
Justin Bognerb10a5202013-11-12 21:44:01 +000021#include "llvm/CodeGen/RuntimeLibcalls.h"
Bill Wendling0e1824c2012-03-31 11:15:43 +000022#include "llvm/Config/config.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Constants.h"
24#include "llvm/IR/DataLayout.h"
25#include "llvm/IR/DerivedTypes.h"
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +000026#include "llvm/IR/DiagnosticInfo.h"
27#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/LLVMContext.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000029#include "llvm/IR/LegacyPassManager.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000030#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Module.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000032#include "llvm/IR/Verifier.h"
Shuxin Yang1e6d80e2013-07-22 18:40:34 +000033#include "llvm/InitializePasses.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000034#include "llvm/LTO/LTOModule.h"
Chandler Carruth6cc07df2014-03-06 03:42:23 +000035#include "llvm/Linker/Linker.h"
Chris Lattner2eff5052010-03-12 18:44:54 +000036#include "llvm/MC/MCAsmInfo.h"
37#include "llvm/MC/MCContext.h"
Evan Cheng8264e272011-06-29 01:14:12 +000038#include "llvm/MC/SubtargetFeature.h"
Nick Kledzikc2323472008-07-08 21:14:10 +000039#include "llvm/Support/CommandLine.h"
Rafael Espindola40c908b2013-06-17 18:05:35 +000040#include "llvm/Support/FileSystem.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000041#include "llvm/Support/Host.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000042#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencerab425d82010-11-29 18:47:54 +000043#include "llvm/Support/Signals.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000044#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/TargetSelect.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000046#include "llvm/Support/ToolOutputFile.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000047#include "llvm/Support/raw_ostream.h"
Justin Bognerb10a5202013-11-12 21:44:01 +000048#include "llvm/Target/TargetLowering.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000049#include "llvm/Target/TargetOptions.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000050#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000051#include "llvm/Target/TargetSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000052#include "llvm/Transforms/IPO.h"
53#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Bob Wilsonf36f15f2013-03-29 23:28:55 +000054#include "llvm/Transforms/ObjCARC.h"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000055#include <system_error>
Shuxin Yang76d082b2013-08-12 18:29:43 +000056using namespace llvm;
Shuxin Yang76d082b2013-08-12 18:29:43 +000057
Bill Wendling534a6582012-03-31 10:49:43 +000058const char* LTOCodeGenerator::getVersionString() {
Nick Kledzik07b4a622008-02-26 20:26:43 +000059#ifdef LLVM_VERSION_INFO
Bill Wendling534a6582012-03-31 10:49:43 +000060 return PACKAGE_NAME " version " PACKAGE_VERSION ", " LLVM_VERSION_INFO;
Nick Kledzik07b4a622008-02-26 20:26:43 +000061#else
Bill Wendling534a6582012-03-31 10:49:43 +000062 return PACKAGE_NAME " version " PACKAGE_VERSION;
Nick Kledzik07b4a622008-02-26 20:26:43 +000063#endif
64}
65
Bill Wendling39d942b2012-03-31 10:50:14 +000066LTOCodeGenerator::LTOCodeGenerator()
Duncan P. N. Exon Smithde5e32b2014-11-11 23:03:29 +000067 : Context(getGlobalContext()), IRLinker(new Module("ld-temp.o", Context)) {
68 initialize();
69}
70
71LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
72 : OwnedContext(std::move(Context)), Context(*OwnedContext),
Peter Collingbourne070843d2015-03-19 22:01:00 +000073 IRLinker(new Module("ld-temp.o", *OwnedContext)), OptLevel(2) {
Duncan P. N. Exon Smithde5e32b2014-11-11 23:03:29 +000074 initialize();
75}
76
77void LTOCodeGenerator::initialize() {
78 TargetMach = nullptr;
79 EmitDwarfDebugInfo = false;
80 ScopeRestrictionsDone = false;
81 CodeModel = LTO_CODEGEN_PIC_MODEL_DEFAULT;
82 DiagHandler = nullptr;
83 DiagContext = nullptr;
Manman Ren082a3362015-02-25 21:20:53 +000084 OwnedModule = nullptr;
Manman Rence0a0662015-04-17 17:10:09 +000085 ShouldInternalize = true;
Duncan P. N. Exon Smithde5e32b2014-11-11 23:03:29 +000086
Shuxin Yang1e6d80e2013-07-22 18:40:34 +000087 initializeLTOPasses();
Nick Kledzik07b4a622008-02-26 20:26:43 +000088}
89
Manman Ren082a3362015-02-25 21:20:53 +000090void LTOCodeGenerator::destroyMergedModule() {
91 if (OwnedModule) {
92 assert(IRLinker.getModule() == &OwnedModule->getModule() &&
93 "The linker's module should be the same as the owned module");
94 delete OwnedModule;
95 OwnedModule = nullptr;
96 } else if (IRLinker.getModule())
97 IRLinker.deleteModule();
98}
99
Bill Wendling534a6582012-03-31 10:49:43 +0000100LTOCodeGenerator::~LTOCodeGenerator() {
Manman Ren082a3362015-02-25 21:20:53 +0000101 destroyMergedModule();
102
Rafael Espindolac80c9692013-09-04 17:44:24 +0000103 delete TargetMach;
Craig Topper2617dcc2014-04-15 06:32:26 +0000104 TargetMach = nullptr;
Bill Wendling91e6f6e2013-10-16 08:59:57 +0000105
Rafael Espindolac80c9692013-09-04 17:44:24 +0000106 for (std::vector<char *>::iterator I = CodegenOptions.begin(),
107 E = CodegenOptions.end();
108 I != E; ++I)
Bill Wendling534a6582012-03-31 10:49:43 +0000109 free(*I);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000110}
111
Shuxin Yang1e6d80e2013-07-22 18:40:34 +0000112// Initialize LTO passes. Please keep this funciton in sync with
Shuxin Yangca760852013-07-23 06:44:34 +0000113// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
Duncan P. N. Exon Smithbccb4fd2014-01-10 20:24:35 +0000114// passes are initialized.
Shuxin Yang1e6d80e2013-07-22 18:40:34 +0000115void LTOCodeGenerator::initializeLTOPasses() {
116 PassRegistry &R = *PassRegistry::getPassRegistry();
117
118 initializeInternalizePassPass(R);
119 initializeIPSCCPPass(R);
120 initializeGlobalOptPass(R);
121 initializeConstantMergePass(R);
122 initializeDAHPass(R);
Chandler Carruth1edb9d62015-01-20 22:44:35 +0000123 initializeInstructionCombiningPassPass(R);
Shuxin Yang1e6d80e2013-07-22 18:40:34 +0000124 initializeSimpleInlinerPass(R);
125 initializePruneEHPass(R);
126 initializeGlobalDCEPass(R);
127 initializeArgPromotionPass(R);
128 initializeJumpThreadingPass(R);
129 initializeSROAPass(R);
130 initializeSROA_DTPass(R);
131 initializeSROA_SSAUpPass(R);
132 initializeFunctionAttrsPass(R);
133 initializeGlobalsModRefPass(R);
134 initializeLICMPass(R);
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000135 initializeMergedLoadStoreMotionPass(R);
Shuxin Yang1e6d80e2013-07-22 18:40:34 +0000136 initializeGVNPass(R);
137 initializeMemCpyOptPass(R);
138 initializeDCEPass(R);
Tom Stellardaa664d92013-08-06 02:43:45 +0000139 initializeCFGSimplifyPassPass(R);
Shuxin Yang1e6d80e2013-07-22 18:40:34 +0000140}
141
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000142bool LTOCodeGenerator::addModule(LTOModule *mod) {
Duncan P. N. Exon Smith94198632014-11-11 23:13:10 +0000143 assert(&mod->getModule().getContext() == &Context &&
144 "Expected module in same context");
145
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000146 bool ret = IRLinker.linkInModule(&mod->getModule());
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000147
148 const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
149 for (int i = 0, e = undefs.size(); i != e; ++i)
Rafael Espindolac80c9692013-09-04 17:44:24 +0000150 AsmUndefinedRefs[undefs[i]] = 1;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000151
Shuxin Yangb6696a92013-08-07 05:19:23 +0000152 return !ret;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000153}
Bill Wendling39d942b2012-03-31 10:50:14 +0000154
Manman Ren6487ce92015-02-24 00:45:56 +0000155void LTOCodeGenerator::setModule(LTOModule *Mod) {
156 assert(&Mod->getModule().getContext() == &Context &&
157 "Expected module in same context");
158
159 // Delete the old merged module.
Manman Ren082a3362015-02-25 21:20:53 +0000160 destroyMergedModule();
Manman Ren6487ce92015-02-24 00:45:56 +0000161 AsmUndefinedRefs.clear();
162
Manman Ren082a3362015-02-25 21:20:53 +0000163 OwnedModule = Mod;
Manman Ren6487ce92015-02-24 00:45:56 +0000164 IRLinker.setModule(&Mod->getModule());
165
166 const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
167 for (int I = 0, E = Undefs.size(); I != E; ++I)
168 AsmUndefinedRefs[Undefs[I]] = 1;
169}
170
Rafael Espindola0b385c72013-09-30 16:39:19 +0000171void LTOCodeGenerator::setTargetOptions(TargetOptions options) {
Rafael Espindola70d3c202014-06-19 22:27:46 +0000172 Options = options;
Rafael Espindola0b385c72013-09-30 16:39:19 +0000173}
174
Shuxin Yangb6696a92013-08-07 05:19:23 +0000175void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
Bill Wendling0e1824c2012-03-31 11:15:43 +0000176 switch (debug) {
177 case LTO_DEBUG_MODEL_NONE:
Rafael Espindolac80c9692013-09-04 17:44:24 +0000178 EmitDwarfDebugInfo = false;
Shuxin Yangb6696a92013-08-07 05:19:23 +0000179 return;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000180
Bill Wendling0e1824c2012-03-31 11:15:43 +0000181 case LTO_DEBUG_MODEL_DWARF:
Rafael Espindolac80c9692013-09-04 17:44:24 +0000182 EmitDwarfDebugInfo = true;
Shuxin Yangb6696a92013-08-07 05:19:23 +0000183 return;
Bill Wendling0e1824c2012-03-31 11:15:43 +0000184 }
185 llvm_unreachable("Unknown debug format!");
Nick Kledzik07b4a622008-02-26 20:26:43 +0000186}
187
Shuxin Yangb6696a92013-08-07 05:19:23 +0000188void LTOCodeGenerator::setCodePICModel(lto_codegen_model model) {
Bill Wendling0e1824c2012-03-31 11:15:43 +0000189 switch (model) {
190 case LTO_CODEGEN_PIC_MODEL_STATIC:
191 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
192 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
James Molloy951e5292014-04-14 13:54:16 +0000193 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
Rafael Espindolac80c9692013-09-04 17:44:24 +0000194 CodeModel = model;
Shuxin Yangb6696a92013-08-07 05:19:23 +0000195 return;
Bill Wendling0e1824c2012-03-31 11:15:43 +0000196 }
197 llvm_unreachable("Unknown PIC model!");
Nick Kledzik07b4a622008-02-26 20:26:43 +0000198}
199
Chris Lattner69733952009-08-23 07:49:08 +0000200bool LTOCodeGenerator::writeMergedModules(const char *path,
201 std::string &errMsg) {
Shuxin Yang95866fa2013-08-06 21:51:21 +0000202 if (!determineTarget(errMsg))
Shuxin Yangb6696a92013-08-07 05:19:23 +0000203 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000204
Bill Wendling2bbbfef2013-08-08 23:51:04 +0000205 // mark which symbols can not be internalized
206 applyScopeRestrictions();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000207
Chris Lattner69733952009-08-23 07:49:08 +0000208 // create output file
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000209 std::error_code EC;
210 tool_output_file Out(path, EC, sys::fs::F_None);
211 if (EC) {
Chris Lattner69733952009-08-23 07:49:08 +0000212 errMsg = "could not open bitcode file for writing: ";
213 errMsg += path;
Shuxin Yangb6696a92013-08-07 05:19:23 +0000214 return false;
Chris Lattner69733952009-08-23 07:49:08 +0000215 }
Bill Wendling39d942b2012-03-31 10:50:14 +0000216
Chris Lattner69733952009-08-23 07:49:08 +0000217 // write bitcode to it
Duncan P. N. Exon Smitha052ed62015-04-15 00:10:50 +0000218 WriteBitcodeToFile(IRLinker.getModule(), Out.os(),
Duncan P. N. Exon Smith8a7b84b2015-04-15 03:14:06 +0000219 /* ShouldPreserveUseListOrder */ true);
Dan Gohmana2233f22010-09-01 14:20:41 +0000220 Out.os().close();
Dan Gohmanab366f02010-05-27 20:19:47 +0000221
Dan Gohmana2233f22010-09-01 14:20:41 +0000222 if (Out.os().has_error()) {
Chris Lattner69733952009-08-23 07:49:08 +0000223 errMsg = "could not write bitcode file: ";
224 errMsg += path;
Dan Gohmana2233f22010-09-01 14:20:41 +0000225 Out.os().clear_error();
Shuxin Yangb6696a92013-08-07 05:19:23 +0000226 return false;
Chris Lattner69733952009-08-23 07:49:08 +0000227 }
Bill Wendling39d942b2012-03-31 10:50:14 +0000228
Dan Gohman8525fe72010-08-20 16:59:15 +0000229 Out.keep();
Shuxin Yangb6696a92013-08-07 05:19:23 +0000230 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000231}
232
Manman Ren8121e1d2015-02-03 18:39:15 +0000233bool LTOCodeGenerator::compileOptimizedToFile(const char **name,
234 std::string &errMsg) {
Shuxin Yang1826ae22013-08-12 21:07:31 +0000235 // make unique temp .o file to put generated object file
236 SmallString<128> Filename;
237 int FD;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +0000238 std::error_code EC =
239 sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
Shuxin Yang1826ae22013-08-12 21:07:31 +0000240 if (EC) {
241 errMsg = EC.message();
Shuxin Yangb6696a92013-08-07 05:19:23 +0000242 return false;
Shuxin Yang1826ae22013-08-12 21:07:31 +0000243 }
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000244
Shuxin Yang1826ae22013-08-12 21:07:31 +0000245 // generate object file
246 tool_output_file objFile(Filename.c_str(), FD);
Bill Wendling0e1824c2012-03-31 11:15:43 +0000247
Manman Ren8121e1d2015-02-03 18:39:15 +0000248 bool genResult = compileOptimized(objFile.os(), errMsg);
Shuxin Yang1826ae22013-08-12 21:07:31 +0000249 objFile.os().close();
250 if (objFile.os().has_error()) {
251 objFile.os().clear_error();
252 sys::fs::remove(Twine(Filename));
Shuxin Yangb6696a92013-08-07 05:19:23 +0000253 return false;
Shuxin Yang1826ae22013-08-12 21:07:31 +0000254 }
Bill Wendling0e1824c2012-03-31 11:15:43 +0000255
Shuxin Yang1826ae22013-08-12 21:07:31 +0000256 objFile.keep();
257 if (!genResult) {
258 sys::fs::remove(Twine(Filename));
Shuxin Yangb6696a92013-08-07 05:19:23 +0000259 return false;
Shuxin Yang1826ae22013-08-12 21:07:31 +0000260 }
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000261
Rafael Espindolac80c9692013-09-04 17:44:24 +0000262 NativeObjectPath = Filename.c_str();
263 *name = NativeObjectPath.c_str();
Shuxin Yangb6696a92013-08-07 05:19:23 +0000264 return true;
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000265}
266
Manman Ren8121e1d2015-02-03 18:39:15 +0000267const void *LTOCodeGenerator::compileOptimized(size_t *length,
268 std::string &errMsg) {
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000269 const char *name;
Manman Ren8121e1d2015-02-03 18:39:15 +0000270 if (!compileOptimizedToFile(&name, errMsg))
Craig Topper2617dcc2014-04-15 06:32:26 +0000271 return nullptr;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000272
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000273 // read .o file into memory buffer
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000274 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
275 MemoryBuffer::getFile(name, -1, false);
276 if (std::error_code EC = BufferOrErr.getError()) {
277 errMsg = EC.message();
Rafael Espindolac80c9692013-09-04 17:44:24 +0000278 sys::fs::remove(NativeObjectPath);
Craig Topper2617dcc2014-04-15 06:32:26 +0000279 return nullptr;
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000280 }
David Blaikie78fdec52014-09-02 18:21:06 +0000281 NativeObjectFile = std::move(*BufferOrErr);
Rafael Espindolafac373c2011-02-24 21:04:06 +0000282
Shuxin Yang1826ae22013-08-12 21:07:31 +0000283 // remove temp files
Rafael Espindolac80c9692013-09-04 17:44:24 +0000284 sys::fs::remove(NativeObjectPath);
Rafael Espindolafac373c2011-02-24 21:04:06 +0000285
Shuxin Yang1826ae22013-08-12 21:07:31 +0000286 // return buffer, unless error
Craig Topper2617dcc2014-04-15 06:32:26 +0000287 if (!NativeObjectFile)
288 return nullptr;
Rafael Espindolac80c9692013-09-04 17:44:24 +0000289 *length = NativeObjectFile->getBufferSize();
290 return NativeObjectFile->getBufferStart();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000291}
292
Manman Ren8121e1d2015-02-03 18:39:15 +0000293
294bool LTOCodeGenerator::compile_to_file(const char **name,
Manman Ren8121e1d2015-02-03 18:39:15 +0000295 bool disableInline,
296 bool disableGVNLoadPRE,
297 bool disableVectorization,
298 std::string &errMsg) {
Peter Collingbourne070843d2015-03-19 22:01:00 +0000299 if (!optimize(disableInline, disableGVNLoadPRE,
Manman Ren8121e1d2015-02-03 18:39:15 +0000300 disableVectorization, errMsg))
301 return false;
302
303 return compileOptimizedToFile(name, errMsg);
304}
305
306const void* LTOCodeGenerator::compile(size_t *length,
Manman Ren8121e1d2015-02-03 18:39:15 +0000307 bool disableInline,
308 bool disableGVNLoadPRE,
309 bool disableVectorization,
310 std::string &errMsg) {
Peter Collingbourne070843d2015-03-19 22:01:00 +0000311 if (!optimize(disableInline, disableGVNLoadPRE,
Manman Ren8121e1d2015-02-03 18:39:15 +0000312 disableVectorization, errMsg))
313 return nullptr;
314
315 return compileOptimized(length, errMsg);
316}
317
Bill Wendlingf44b2a22013-05-23 21:21:50 +0000318bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000319 if (TargetMach)
Shuxin Yang95866fa2013-08-06 21:51:21 +0000320 return true;
Daniel Dunbar0f16ea52009-08-03 04:03:51 +0000321
Rafael Espindola80df4bb2014-05-03 15:28:13 +0000322 std::string TripleStr = IRLinker.getModule()->getTargetTriple();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000323 if (TripleStr.empty())
324 TripleStr = sys::getDefaultTargetTriple();
325 llvm::Triple Triple(TripleStr);
Bill Wendling9b2c5732008-06-18 06:35:30 +0000326
Bill Wendling45f74e32012-08-06 22:52:45 +0000327 // create target machine from info for merged modules
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000328 const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
Craig Topper2617dcc2014-04-15 06:32:26 +0000329 if (!march)
Shuxin Yang95866fa2013-08-06 21:51:21 +0000330 return false;
Bill Wendling0e1824c2012-03-31 11:15:43 +0000331
Bill Wendling45f74e32012-08-06 22:52:45 +0000332 // The relocation model is actually a static member of TargetMachine and
333 // needs to be set before the TargetMachine is instantiated.
334 Reloc::Model RelocModel = Reloc::Default;
Rafael Espindolac80c9692013-09-04 17:44:24 +0000335 switch (CodeModel) {
Bill Wendling45f74e32012-08-06 22:52:45 +0000336 case LTO_CODEGEN_PIC_MODEL_STATIC:
337 RelocModel = Reloc::Static;
338 break;
339 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
340 RelocModel = Reloc::PIC_;
341 break;
342 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
343 RelocModel = Reloc::DynamicNoPIC;
344 break;
James Molloy951e5292014-04-14 13:54:16 +0000345 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
346 // RelocModel is already the default, so leave it that way.
347 break;
Bill Wendling0e1824c2012-03-31 11:15:43 +0000348 }
Bill Wendling45f74e32012-08-06 22:52:45 +0000349
Tom Roederfd1bc602014-04-25 21:46:51 +0000350 // Construct LTOModule, hand over ownership of module and target. Use MAttr as
351 // the default set of features.
352 SubtargetFeatures Features(MAttr);
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000353 Features.getDefaultSubtargetFeatures(Triple);
Bill Wendling45f74e32012-08-06 22:52:45 +0000354 std::string FeatureStr = Features.getString();
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000355 // Set a default CPU for Darwin triples.
Rafael Espindolac80c9692013-09-04 17:44:24 +0000356 if (MCpu.empty() && Triple.isOSDarwin()) {
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000357 if (Triple.getArch() == llvm::Triple::x86_64)
Rafael Espindolac80c9692013-09-04 17:44:24 +0000358 MCpu = "core2";
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000359 else if (Triple.getArch() == llvm::Triple::x86)
Rafael Espindolac80c9692013-09-04 17:44:24 +0000360 MCpu = "yonah";
Tim Northovere19bed72014-07-23 12:32:47 +0000361 else if (Triple.getArch() == llvm::Triple::aarch64)
Tim Northover00ed9962014-03-29 10:18:08 +0000362 MCpu = "cyclone";
Bob Wilson3f7e7c02012-10-12 17:39:25 +0000363 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000364
Peter Collingbourne070843d2015-03-19 22:01:00 +0000365 CodeGenOpt::Level CGOptLevel;
366 switch (OptLevel) {
367 case 0:
368 CGOptLevel = CodeGenOpt::None;
369 break;
370 case 1:
371 CGOptLevel = CodeGenOpt::Less;
372 break;
373 case 2:
374 CGOptLevel = CodeGenOpt::Default;
375 break;
376 case 3:
377 CGOptLevel = CodeGenOpt::Aggressive;
378 break;
379 }
380
Rafael Espindolac80c9692013-09-04 17:44:24 +0000381 TargetMach = march->createTargetMachine(TripleStr, MCpu, FeatureStr, Options,
382 RelocModel, CodeModel::Default,
Peter Collingbourne070843d2015-03-19 22:01:00 +0000383 CGOptLevel);
Shuxin Yang95866fa2013-08-06 21:51:21 +0000384 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000385}
386
Bill Wendling0e1824c2012-03-31 11:15:43 +0000387void LTOCodeGenerator::
388applyRestriction(GlobalValue &GV,
Craig Topper3af97222014-08-27 05:25:00 +0000389 ArrayRef<StringRef> Libcalls,
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000390 std::vector<const char*> &MustPreserveList,
Craig Topper71b7b682014-08-21 05:55:13 +0000391 SmallPtrSetImpl<GlobalValue*> &AsmUsed,
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000392 Mangler &Mangler) {
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000393 // There are no restrictions to apply to declarations.
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000394 if (GV.isDeclaration())
395 return;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000396
397 // There is nothing more restrictive than private linkage.
398 if (GV.hasPrivateLinkage())
399 return;
400
401 SmallString<64> Buffer;
Rafael Espindolaa3ad4e62014-02-19 20:30:41 +0000402 TargetMach->getNameWithPrefix(Buffer, &GV, Mangler);
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000403
Rafael Espindolac80c9692013-09-04 17:44:24 +0000404 if (MustPreserveSymbols.count(Buffer))
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000405 MustPreserveList.push_back(GV.getName().data());
Rafael Espindolac80c9692013-09-04 17:44:24 +0000406 if (AsmUndefinedRefs.count(Buffer))
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000407 AsmUsed.insert(&GV);
Justin Bognerb10a5202013-11-12 21:44:01 +0000408
409 // Conservatively append user-supplied runtime library functions to
410 // llvm.compiler.used. These could be internalized and deleted by
411 // optimizations like -globalopt, causing problems when later optimizations
412 // add new library calls (e.g., llvm.memset => memset and printf => puts).
413 // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
414 if (isa<Function>(GV) &&
415 std::binary_search(Libcalls.begin(), Libcalls.end(), GV.getName()))
416 AsmUsed.insert(&GV);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000417}
418
419static void findUsedValues(GlobalVariable *LLVMUsed,
Craig Topper71b7b682014-08-21 05:55:13 +0000420 SmallPtrSetImpl<GlobalValue*> &UsedValues) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000421 if (!LLVMUsed) return;
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000422
Rafael Espindolacc111b22013-04-24 17:54:35 +0000423 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000424 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Bill Wendling39d942b2012-03-31 10:50:14 +0000425 if (GlobalValue *GV =
Bill Wendling0e1824c2012-03-31 11:15:43 +0000426 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000427 UsedValues.insert(GV);
428}
429
Akira Hatanaka8fba18e2015-01-30 01:16:24 +0000430// Collect names of runtime library functions. User-defined functions with the
431// same names are added to llvm.compiler.used to prevent them from being
432// deleted by optimizations.
Justin Bognerb10a5202013-11-12 21:44:01 +0000433static void accumulateAndSortLibcalls(std::vector<StringRef> &Libcalls,
434 const TargetLibraryInfo& TLI,
Akira Hatanaka8fba18e2015-01-30 01:16:24 +0000435 const Module &Mod,
436 const TargetMachine &TM) {
Justin Bognerb10a5202013-11-12 21:44:01 +0000437 // TargetLibraryInfo has info on C runtime library calls on the current
438 // target.
439 for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
440 I != E; ++I) {
441 LibFunc::Func F = static_cast<LibFunc::Func>(I);
442 if (TLI.has(F))
443 Libcalls.push_back(TLI.getName(F));
444 }
445
Akira Hatanaka8fba18e2015-01-30 01:16:24 +0000446 SmallPtrSet<const TargetLowering *, 1> TLSet;
447
448 for (const Function &F : Mod) {
449 const TargetLowering *Lowering =
450 TM.getSubtargetImpl(F)->getTargetLowering();
451
452 if (Lowering && TLSet.insert(Lowering).second)
453 // TargetLowering has info on library calls that CodeGen expects to be
454 // available, both from the C runtime and compiler-rt.
455 for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
456 I != E; ++I)
457 if (const char *Name =
458 Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
459 Libcalls.push_back(Name);
460 }
Justin Bognerb10a5202013-11-12 21:44:01 +0000461
Duncan P. N. Exon Smith0c8d6042013-11-16 16:15:56 +0000462 array_pod_sort(Libcalls.begin(), Libcalls.end());
Justin Bognerb10a5202013-11-12 21:44:01 +0000463 Libcalls.erase(std::unique(Libcalls.begin(), Libcalls.end()),
464 Libcalls.end());
465}
466
Chris Lattner2eff5052010-03-12 18:44:54 +0000467void LTOCodeGenerator::applyScopeRestrictions() {
Manman Rence0a0662015-04-17 17:10:09 +0000468 if (ScopeRestrictionsDone || !ShouldInternalize)
Rafael Espindolac80c9692013-09-04 17:44:24 +0000469 return;
Rafael Espindola80df4bb2014-05-03 15:28:13 +0000470 Module *mergedModule = IRLinker.getModule();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000471
Chris Lattner2eff5052010-03-12 18:44:54 +0000472 // Start off with a verification pass.
Chandler Carruth30d69c22015-02-13 10:01:29 +0000473 legacy::PassManager passes;
Chris Lattner2eff5052010-03-12 18:44:54 +0000474 passes.add(createVerifierPass());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000475
Bill Wendling39d942b2012-03-31 10:50:14 +0000476 // mark which symbols can not be internalized
Eric Christopher8b770652015-01-26 19:03:15 +0000477 Mangler Mangler(TargetMach->getDataLayout());
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000478 std::vector<const char*> MustPreserveList;
479 SmallPtrSet<GlobalValue*, 8> AsmUsed;
Justin Bognerb10a5202013-11-12 21:44:01 +0000480 std::vector<StringRef> Libcalls;
Chandler Carruthc0291862015-01-24 02:06:09 +0000481 TargetLibraryInfoImpl TLII(Triple(TargetMach->getTargetTriple()));
482 TargetLibraryInfo TLI(TLII);
Akira Hatanaka8fba18e2015-01-30 01:16:24 +0000483
484 accumulateAndSortLibcalls(Libcalls, TLI, *mergedModule, *TargetMach);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000485
486 for (Module::iterator f = mergedModule->begin(),
487 e = mergedModule->end(); f != e; ++f)
Justin Bognerb10a5202013-11-12 21:44:01 +0000488 applyRestriction(*f, Libcalls, MustPreserveList, AsmUsed, Mangler);
Bill Wendling39d942b2012-03-31 10:50:14 +0000489 for (Module::global_iterator v = mergedModule->global_begin(),
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000490 e = mergedModule->global_end(); v != e; ++v)
Justin Bognerb10a5202013-11-12 21:44:01 +0000491 applyRestriction(*v, Libcalls, MustPreserveList, AsmUsed, Mangler);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000492 for (Module::alias_iterator a = mergedModule->alias_begin(),
493 e = mergedModule->alias_end(); a != e; ++a)
Justin Bognerb10a5202013-11-12 21:44:01 +0000494 applyRestriction(*a, Libcalls, MustPreserveList, AsmUsed, Mangler);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000495
496 GlobalVariable *LLVMCompilerUsed =
497 mergedModule->getGlobalVariable("llvm.compiler.used");
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000498 findUsedValues(LLVMCompilerUsed, AsmUsed);
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000499 if (LLVMCompilerUsed)
500 LLVMCompilerUsed->eraseFromParent();
501
Rafael Espindolab7c0b4a2013-09-04 20:08:46 +0000502 if (!AsmUsed.empty()) {
Rafael Espindolac80c9692013-09-04 17:44:24 +0000503 llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(Context);
Rafael Espindolacc111b22013-04-24 17:54:35 +0000504 std::vector<Constant*> asmUsed2;
Rafael Espindola9c8c96f2014-05-05 20:06:41 +0000505 for (auto *GV : AsmUsed) {
Rafael Espindolacc111b22013-04-24 17:54:35 +0000506 Constant *c = ConstantExpr::getBitCast(GV, i8PTy);
507 asmUsed2.push_back(c);
508 }
509
510 llvm::ArrayType *ATy = llvm::ArrayType::get(i8PTy, asmUsed2.size());
511 LLVMCompilerUsed =
512 new llvm::GlobalVariable(*mergedModule, ATy, false,
513 llvm::GlobalValue::AppendingLinkage,
514 llvm::ConstantArray::get(ATy, asmUsed2),
515 "llvm.compiler.used");
516
517 LLVMCompilerUsed->setSection("llvm.metadata");
Chris Lattner2eff5052010-03-12 18:44:54 +0000518 }
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000519
Duncan P. N. Exon Smith4680f402014-04-02 22:05:57 +0000520 passes.add(createInternalizePass(MustPreserveList));
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000521
Chris Lattner2eff5052010-03-12 18:44:54 +0000522 // apply scope restrictions
523 passes.run(*mergedModule);
Bill Wendling39d942b2012-03-31 10:50:14 +0000524
Rafael Espindolac80c9692013-09-04 17:44:24 +0000525 ScopeRestrictionsDone = true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000526}
527
Nick Kledzik07b4a622008-02-26 20:26:43 +0000528/// Optimize merged modules using various IPO passes
Peter Collingbourne070843d2015-03-19 22:01:00 +0000529bool LTOCodeGenerator::optimize(bool DisableInline,
Manman Ren8121e1d2015-02-03 18:39:15 +0000530 bool DisableGVNLoadPRE,
531 bool DisableVectorization,
532 std::string &errMsg) {
Shuxin Yang95866fa2013-08-06 21:51:21 +0000533 if (!this->determineTarget(errMsg))
534 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000535
Rafael Espindola80df4bb2014-05-03 15:28:13 +0000536 Module *mergedModule = IRLinker.getModule();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000537
Bill Wendlingf44b2a22013-05-23 21:21:50 +0000538 // Mark which symbols can not be internalized
Bill Wendling383fda22012-04-09 22:18:01 +0000539 this->applyScopeRestrictions();
540
Bill Wendling0e1824c2012-03-31 11:15:43 +0000541 // Instantiate the pass manager to organize the passes.
Chandler Carruth30d69c22015-02-13 10:01:29 +0000542 legacy::PassManager passes;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000543
Micah Villmow9cfc13d2012-10-08 16:39:34 +0000544 // Add an appropriate DataLayout instance for this module...
Mehdi Amini46a43552015-03-04 18:43:29 +0000545 mergedModule->setDataLayout(*TargetMach->getDataLayout());
Bill Wendling39d942b2012-03-31 10:50:14 +0000546
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +0000547 passes.add(
548 createTargetTransformInfoWrapperPass(TargetMach->getTargetIRAnalysis()));
Chandler Carruth1efa12d2015-01-30 13:33:42 +0000549
Rafael Espindola216e0c02014-08-21 18:49:52 +0000550 Triple TargetTriple(TargetMach->getTargetTriple());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000551 PassManagerBuilder PMB;
552 PMB.DisableGVNLoadPRE = DisableGVNLoadPRE;
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000553 PMB.LoopVectorize = !DisableVectorization;
554 PMB.SLPVectorize = !DisableVectorization;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000555 if (!DisableInline)
556 PMB.Inliner = createFunctionInliningPass();
Chandler Carruthc0291862015-01-24 02:06:09 +0000557 PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
Peter Collingbourne070843d2015-03-19 22:01:00 +0000558 PMB.OptLevel = OptLevel;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000559 PMB.VerifyInput = true;
560 PMB.VerifyOutput = true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000561
Chandler Carruth1efa12d2015-01-30 13:33:42 +0000562 PMB.populateLTOPassManager(passes);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000563
Manman Ren8121e1d2015-02-03 18:39:15 +0000564 // Run our queue of passes all at once now, efficiently.
565 passes.run(*mergedModule);
566
567 return true;
568}
569
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000570bool LTOCodeGenerator::compileOptimized(raw_pwrite_stream &out,
571 std::string &errMsg) {
Manman Ren8121e1d2015-02-03 18:39:15 +0000572 if (!this->determineTarget(errMsg))
573 return false;
574
575 Module *mergedModule = IRLinker.getModule();
576
Chandler Carruth30d69c22015-02-13 10:01:29 +0000577 legacy::PassManager codeGenPasses;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000578
Bob Wilsonf36f15f2013-03-29 23:28:55 +0000579 // If the bitcode files contain ARC code and were compiled with optimization,
580 // the ObjCARCContractPass must be run, so do it unconditionally here.
581 codeGenPasses.add(createObjCARCContractPass());
582
Rafael Espindola5682ce22015-04-09 21:06:08 +0000583 if (TargetMach->addPassesToEmitFile(codeGenPasses, out,
Rafael Espindolac80c9692013-09-04 17:44:24 +0000584 TargetMachine::CGFT_ObjectFile)) {
Bill Wendling0e1824c2012-03-31 11:15:43 +0000585 errMsg = "target file type not supported";
Shuxin Yang95866fa2013-08-06 21:51:21 +0000586 return false;
Bill Wendling0e1824c2012-03-31 11:15:43 +0000587 }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000588
Bill Wendling0e1824c2012-03-31 11:15:43 +0000589 // Run the code generator, and write assembly file
Lang Hamesdfa3f8f2013-03-13 21:18:46 +0000590 codeGenPasses.run(*mergedModule);
Nick Lewyckyfd6a2492009-07-26 22:16:39 +0000591
Shuxin Yang95866fa2013-08-06 21:51:21 +0000592 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000593}
594
Bill Wendling534a6582012-03-31 10:49:43 +0000595/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
596/// LTO problems.
597void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
598 for (std::pair<StringRef, StringRef> o = getToken(options);
599 !o.first.empty(); o = getToken(o.second)) {
600 // ParseCommandLineOptions() expects argv[0] to be program name. Lazily add
601 // that.
Rafael Espindolac80c9692013-09-04 17:44:24 +0000602 if (CodegenOptions.empty())
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000603 CodegenOptions.push_back(strdup("libLLVMLTO"));
Rafael Espindolac80c9692013-09-04 17:44:24 +0000604 CodegenOptions.push_back(strdup(o.first.str().c_str()));
Bill Wendling534a6582012-03-31 10:49:43 +0000605 }
Nick Kledzikc2323472008-07-08 21:14:10 +0000606}
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000607
608void LTOCodeGenerator::parseCodeGenDebugOptions() {
609 // if options were requested, set them
610 if (!CodegenOptions.empty())
611 cl::ParseCommandLineOptions(CodegenOptions.size(),
612 const_cast<char **>(&CodegenOptions[0]));
613}
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000614
615void LTOCodeGenerator::DiagnosticHandler(const DiagnosticInfo &DI,
616 void *Context) {
617 ((LTOCodeGenerator *)Context)->DiagnosticHandler2(DI);
618}
619
620void LTOCodeGenerator::DiagnosticHandler2(const DiagnosticInfo &DI) {
621 // Map the LLVM internal diagnostic severity to the LTO diagnostic severity.
622 lto_codegen_diagnostic_severity_t Severity;
623 switch (DI.getSeverity()) {
624 case DS_Error:
625 Severity = LTO_DS_ERROR;
626 break;
627 case DS_Warning:
628 Severity = LTO_DS_WARNING;
629 break;
Tobias Grossere8d4c9a2014-02-28 09:08:45 +0000630 case DS_Remark:
631 Severity = LTO_DS_REMARK;
632 break;
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000633 case DS_Note:
634 Severity = LTO_DS_NOTE;
635 break;
636 }
637 // Create the string that will be reported to the external diagnostic handler.
Alp Tokere69170a2014-06-26 22:52:05 +0000638 std::string MsgStorage;
639 raw_string_ostream Stream(MsgStorage);
640 DiagnosticPrinterRawOStream DP(Stream);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000641 DI.print(DP);
Alp Tokere69170a2014-06-26 22:52:05 +0000642 Stream.flush();
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000643
644 // If this method has been called it means someone has set up an external
645 // diagnostic handler. Assert on that.
646 assert(DiagHandler && "Invalid diagnostic handler");
Alp Tokere69170a2014-06-26 22:52:05 +0000647 (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000648}
649
650void
651LTOCodeGenerator::setDiagnosticHandler(lto_diagnostic_handler_t DiagHandler,
652 void *Ctxt) {
653 this->DiagHandler = DiagHandler;
654 this->DiagContext = Ctxt;
655 if (!DiagHandler)
Craig Topper2617dcc2014-04-15 06:32:26 +0000656 return Context.setDiagnosticHandler(nullptr, nullptr);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000657 // Register the LTOCodeGenerator stub in the LLVMContext to forward the
658 // diagnostic to the external DiagHandler.
Duncan P. N. Exon Smithf02fe702014-10-02 21:11:04 +0000659 Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
660 /* RespectFilters */ true);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000661}