| Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 1 | //===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===// | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 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. | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 7 | // | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
 | 9 | // | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 10 | // This file implements the Link Time Optimization library. This library is | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 11 | // intended to be used by linker to optimize code at link time. | 
 | 12 | // | 
 | 13 | //===----------------------------------------------------------------------===// | 
 | 14 |  | 
| Peter Collingbourne | cc48854 | 2013-09-24 23:52:22 +0000 | [diff] [blame] | 15 | #include "llvm/LTO/LTOModule.h" | 
| Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" | 
 | 17 | #include "llvm/Bitcode/ReaderWriter.h" | 
| Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" | 
 | 19 | #include "llvm/IR/LLVMContext.h" | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 20 | #include "llvm/IR/Metadata.h" | 
| Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCExpr.h" | 
 | 23 | #include "llvm/MC/MCInst.h" | 
| Joey Gouly | 9de16c1 | 2013-09-12 12:55:29 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInstrInfo.h" | 
| Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCParser/MCAsmParser.h" | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 26 | #include "llvm/MC/MCSection.h" | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCStreamer.h" | 
| Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSubtargetInfo.h" | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSymbol.h" | 
| Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCTargetAsmParser.h" | 
| Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 31 | #include "llvm/MC/SubtargetFeature.h" | 
| Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" | 
| Rafael Espindola | 6c21feb | 2013-06-11 18:05:26 +0000 | [diff] [blame] | 33 | #include "llvm/Support/FileSystem.h" | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 34 | #include "llvm/Support/Host.h" | 
| Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 35 | #include "llvm/Support/MemoryBuffer.h" | 
 | 36 | #include "llvm/Support/Path.h" | 
| Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/SourceMgr.h" | 
| Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 38 | #include "llvm/Support/TargetRegistry.h" | 
 | 39 | #include "llvm/Support/TargetSelect.h" | 
 | 40 | #include "llvm/Support/system_error.h" | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 41 | #include "llvm/Target/TargetLowering.h" | 
 | 42 | #include "llvm/Target/TargetLoweringObjectFile.h" | 
| Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetRegisterInfo.h" | 
| Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/GlobalStatus.h" | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 45 | using namespace llvm; | 
 | 46 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 47 | LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t) | 
 | 48 |   : _module(m), _target(t), | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 49 |     _context(_target->getMCAsmInfo(), _target->getRegisterInfo(), &ObjFileInfo), | 
 | 50 |     _mangler(t->getDataLayout()) { | 
 | 51 |   ObjFileInfo.InitMCObjectFileInfo(t->getTargetTriple(), | 
 | 52 |                                    t->getRelocationModel(), t->getCodeModel(), | 
 | 53 |                                    _context); | 
 | 54 | } | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 55 |  | 
 | 56 | /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM | 
 | 57 | /// bitcode. | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 58 | bool LTOModule::isBitcodeFile(const void *mem, size_t length) { | 
| Rafael Espindola | 6c21feb | 2013-06-11 18:05:26 +0000 | [diff] [blame] | 59 |   return sys::fs::identify_magic(StringRef((const char *)mem, length)) == | 
 | 60 |          sys::fs::file_magic::bitcode; | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 61 | } | 
 | 62 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 63 | bool LTOModule::isBitcodeFile(const char *path) { | 
| Rafael Espindola | c3907f3 | 2013-06-12 15:13:57 +0000 | [diff] [blame] | 64 |   sys::fs::file_magic type; | 
 | 65 |   if (sys::fs::identify_magic(path, type)) | 
 | 66 |     return false; | 
 | 67 |   return type == sys::fs::file_magic::bitcode; | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 68 | } | 
 | 69 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 70 | /// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is | 
 | 71 | /// LLVM bitcode for the specified triple. | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 72 | bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, | 
 | 73 |                                        const char *triplePrefix) { | 
 | 74 |   MemoryBuffer *buffer = makeBuffer(mem, length); | 
 | 75 |   if (!buffer) | 
| Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 76 |     return false; | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 77 |   return isTargetMatch(buffer, triplePrefix); | 
| Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 78 | } | 
 | 79 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 80 | bool LTOModule::isBitcodeFileForTarget(const char *path, | 
 | 81 |                                        const char *triplePrefix) { | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 82 |   std::unique_ptr<MemoryBuffer> buffer; | 
| Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 83 |   if (MemoryBuffer::getFile(path, buffer)) | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 84 |     return false; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 85 |   return isTargetMatch(buffer.release(), triplePrefix); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 86 | } | 
 | 87 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 88 | /// isTargetMatch - Returns 'true' if the memory buffer is for the specified | 
 | 89 | /// target triple. | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 90 | bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) { | 
| Bill Wendling | 3471174 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 91 |   std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); | 
 | 92 |   delete buffer; | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 93 |   return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0; | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 94 | } | 
 | 95 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 96 | /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of | 
 | 97 | /// the buffer. | 
| Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 98 | LTOModule *LTOModule::makeLTOModule(const char *path, TargetOptions options, | 
 | 99 |                                     std::string &errMsg) { | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 100 |   std::unique_ptr<MemoryBuffer> buffer; | 
| Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 101 |   if (error_code ec = MemoryBuffer::getFile(path, buffer)) { | 
| Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 102 |     errMsg = ec.message(); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 103 |     return NULL; | 
| Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 104 |   } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 105 |   return makeLTOModule(buffer.release(), options, errMsg); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 106 | } | 
 | 107 |  | 
| Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 108 | LTOModule *LTOModule::makeLTOModule(int fd, const char *path, | 
| Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 109 |                                     size_t size, TargetOptions options, | 
 | 110 |                                     std::string &errMsg) { | 
 | 111 |   return makeLTOModule(fd, path, size, 0, options, errMsg); | 
| Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 112 | } | 
 | 113 |  | 
 | 114 | LTOModule *LTOModule::makeLTOModule(int fd, const char *path, | 
| Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 115 |                                     size_t map_size, | 
 | 116 |                                     off_t offset, | 
| Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 117 |                                     TargetOptions options, | 
| Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 118 |                                     std::string &errMsg) { | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 119 |   std::unique_ptr<MemoryBuffer> buffer; | 
| Rafael Espindola | 70c7e48 | 2013-07-23 20:25:01 +0000 | [diff] [blame] | 120 |   if (error_code ec = | 
 | 121 |           MemoryBuffer::getOpenFileSlice(fd, path, buffer, map_size, offset)) { | 
| Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 122 |     errMsg = ec.message(); | 
 | 123 |     return NULL; | 
 | 124 |   } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 125 |   return makeLTOModule(buffer.release(), options, errMsg); | 
| Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 126 | } | 
 | 127 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 128 | LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length, | 
| Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 129 |                                     TargetOptions options, | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 130 |                                     std::string &errMsg, StringRef path) { | 
 | 131 |   std::unique_ptr<MemoryBuffer> buffer(makeBuffer(mem, length, path)); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 132 |   if (!buffer) | 
 | 133 |     return NULL; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 134 |   return makeLTOModule(buffer.release(), options, errMsg); | 
| Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame] | 135 | } | 
 | 136 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 137 | LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer, | 
| Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 138 |                                     TargetOptions options, | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 139 |                                     std::string &errMsg) { | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 140 |   // parse bitcode buffer | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 141 |   ErrorOr<Module *> ModuleOrErr = | 
 | 142 |       getLazyBitcodeModule(buffer, getGlobalContext()); | 
 | 143 |   if (error_code EC = ModuleOrErr.getError()) { | 
 | 144 |     errMsg = EC.message(); | 
| Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 145 |     delete buffer; | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 146 |     return NULL; | 
| Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 147 |   } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 148 |   std::unique_ptr<Module> m(ModuleOrErr.get()); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 149 |  | 
| Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 150 |   std::string TripleStr = m->getTargetTriple(); | 
 | 151 |   if (TripleStr.empty()) | 
 | 152 |     TripleStr = sys::getDefaultTargetTriple(); | 
 | 153 |   llvm::Triple Triple(TripleStr); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 154 |  | 
 | 155 |   // find machine architecture for this module | 
| Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 156 |   const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 157 |   if (!march) | 
 | 158 |     return NULL; | 
 | 159 |  | 
| Nick Lewycky | 333ed45 | 2011-04-21 01:54:08 +0000 | [diff] [blame] | 160 |   // construct LTOModule, hand over ownership of module and target | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 161 |   SubtargetFeatures Features; | 
| Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 162 |   Features.getDefaultSubtargetFeatures(Triple); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 163 |   std::string FeatureStr = Features.getString(); | 
| Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 164 |   // Set a default CPU for Darwin triples. | 
| Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 165 |   std::string CPU; | 
| Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 166 |   if (Triple.isOSDarwin()) { | 
 | 167 |     if (Triple.getArch() == llvm::Triple::x86_64) | 
 | 168 |       CPU = "core2"; | 
 | 169 |     else if (Triple.getArch() == llvm::Triple::x86) | 
 | 170 |       CPU = "yonah"; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 171 |     else if (Triple.getArch() == llvm::Triple::arm64) | 
 | 172 |       CPU = "cyclone"; | 
| Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 173 |   } | 
| Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 174 |  | 
| Bob Wilson | 47ed8a1 | 2012-10-12 17:39:25 +0000 | [diff] [blame] | 175 |   TargetMachine *target = march->createTargetMachine(TripleStr, CPU, FeatureStr, | 
| Rafael Espindola | c13c9e5 | 2013-09-30 16:39:19 +0000 | [diff] [blame] | 176 |                                                      options); | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 177 |   m->materializeAllPermanently(); | 
| Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 178 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 179 |   LTOModule *Ret = new LTOModule(m.release(), target); | 
 | 180 |  | 
 | 181 |   // We need a MCContext set up in order to get mangled names of private | 
 | 182 |   // symbols. It is a bit odd that we need to report uses and definitions | 
 | 183 |   // of private symbols, but it does look like ld64 expects to be informed | 
 | 184 |   // of at least the ones with an 'l' prefix. | 
 | 185 |   MCContext &Context = Ret->_context; | 
 | 186 |   const TargetLoweringObjectFile &TLOF = | 
 | 187 |       target->getTargetLowering()->getObjFileLowering(); | 
 | 188 |   const_cast<TargetLoweringObjectFile &>(TLOF).Initialize(Context, *target); | 
 | 189 |  | 
| Bill Wendling | 3bb1738 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 190 |   if (Ret->parseSymbols(errMsg)) { | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 191 |     delete Ret; | 
 | 192 |     return NULL; | 
 | 193 |   } | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 194 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 195 |   Ret->parseMetadata(); | 
 | 196 |  | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 197 |   return Ret; | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 198 | } | 
 | 199 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 200 | /// Create a MemoryBuffer from a memory range with an optional name. | 
 | 201 | MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length, | 
 | 202 |                                     StringRef name) { | 
| Roman Divacky | 5932429 | 2012-09-05 22:26:57 +0000 | [diff] [blame] | 203 |   const char *startPtr = (const char*)mem; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 204 |   return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), name, false); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 205 | } | 
 | 206 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 207 | /// objcClassNameFromExpression - Get string that the data pointer points to. | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 208 | bool | 
 | 209 | LTOModule::objcClassNameFromExpression(const Constant *c, std::string &name) { | 
 | 210 |   if (const ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) { | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 211 |     Constant *op = ce->getOperand(0); | 
 | 212 |     if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) { | 
 | 213 |       Constant *cn = gvn->getInitializer(); | 
| Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 214 |       if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) { | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 215 |         if (ca->isCString()) { | 
| Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 216 |           name = ".objc_class_name_" + ca->getAsCString().str(); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 217 |           return true; | 
| Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 218 |         } | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 219 |       } | 
| Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 220 |     } | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 221 |   } | 
 | 222 |   return false; | 
 | 223 | } | 
 | 224 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 225 | /// addObjCClass - Parse i386/ppc ObjC class data structure. | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 226 | void LTOModule::addObjCClass(const GlobalVariable *clgv) { | 
 | 227 |   const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 228 |   if (!c) return; | 
| Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 229 |  | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 230 |   // second slot in __OBJC,__class is pointer to superclass name | 
 | 231 |   std::string superclassName; | 
 | 232 |   if (objcClassNameFromExpression(c->getOperand(1), superclassName)) { | 
 | 233 |     NameAndAttributes info; | 
 | 234 |     StringMap<NameAndAttributes>::value_type &entry = | 
 | 235 |       _undefines.GetOrCreateValue(superclassName); | 
 | 236 |     if (!entry.getValue().name) { | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 237 |       const char *symbolName = entry.getKey().data(); | 
 | 238 |       info.name = symbolName; | 
 | 239 |       info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 240 |       info.isFunction = false; | 
 | 241 |       info.symbol = clgv; | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 242 |       entry.setValue(info); | 
| Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 243 |     } | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 244 |   } | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 245 |  | 
 | 246 |   // third slot in __OBJC,__class is pointer to class name | 
 | 247 |   std::string className; | 
 | 248 |   if (objcClassNameFromExpression(c->getOperand(2), className)) { | 
 | 249 |     StringSet::value_type &entry = _defines.GetOrCreateValue(className); | 
 | 250 |     entry.setValue(1); | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 251 |  | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 252 |     NameAndAttributes info; | 
 | 253 |     info.name = entry.getKey().data(); | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 254 |     info.attributes = LTO_SYMBOL_PERMISSIONS_DATA | | 
 | 255 |       LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT; | 
 | 256 |     info.isFunction = false; | 
 | 257 |     info.symbol = clgv; | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 258 |     _symbols.push_back(info); | 
 | 259 |   } | 
 | 260 | } | 
 | 261 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 262 | /// addObjCCategory - Parse i386/ppc ObjC category data structure. | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 263 | void LTOModule::addObjCCategory(const GlobalVariable *clgv) { | 
 | 264 |   const ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 265 |   if (!c) return; | 
 | 266 |  | 
 | 267 |   // second slot in __OBJC,__category is pointer to target class name | 
 | 268 |   std::string targetclassName; | 
 | 269 |   if (!objcClassNameFromExpression(c->getOperand(1), targetclassName)) | 
 | 270 |     return; | 
 | 271 |  | 
 | 272 |   NameAndAttributes info; | 
 | 273 |   StringMap<NameAndAttributes>::value_type &entry = | 
 | 274 |     _undefines.GetOrCreateValue(targetclassName); | 
 | 275 |  | 
 | 276 |   if (entry.getValue().name) | 
 | 277 |     return; | 
 | 278 |  | 
 | 279 |   const char *symbolName = entry.getKey().data(); | 
 | 280 |   info.name = symbolName; | 
 | 281 |   info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 282 |   info.isFunction = false; | 
 | 283 |   info.symbol = clgv; | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 284 |   entry.setValue(info); | 
| Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 285 | } | 
 | 286 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 287 | /// addObjCClassRef - Parse i386/ppc ObjC class list data structure. | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 288 | void LTOModule::addObjCClassRef(const GlobalVariable *clgv) { | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 289 |   std::string targetclassName; | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 290 |   if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) | 
 | 291 |     return; | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 292 |  | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 293 |   NameAndAttributes info; | 
 | 294 |   StringMap<NameAndAttributes>::value_type &entry = | 
 | 295 |     _undefines.GetOrCreateValue(targetclassName); | 
 | 296 |   if (entry.getValue().name) | 
 | 297 |     return; | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 298 |  | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 299 |   const char *symbolName = entry.getKey().data(); | 
 | 300 |   info.name = symbolName; | 
 | 301 |   info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 302 |   info.isFunction = false; | 
 | 303 |   info.symbol = clgv; | 
| Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 304 |   entry.setValue(info); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 305 | } | 
 | 306 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 307 | /// addDefinedDataSymbol - Add a data symbol as defined to the list. | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 308 | void LTOModule::addDefinedDataSymbol(const GlobalValue *v) { | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 309 |   // Add to list of defined symbols. | 
| Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 310 |   addDefinedSymbol(v, false); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 311 |  | 
| Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 312 |   if (!v->hasSection() /* || !isTargetDarwin */) | 
 | 313 |     return; | 
 | 314 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 315 |   // Special case i386/ppc ObjC data structures in magic sections: | 
 | 316 |   // The issue is that the old ObjC object format did some strange | 
 | 317 |   // contortions to avoid real linker symbols.  For instance, the | 
 | 318 |   // ObjC class data structure is allocated statically in the executable | 
 | 319 |   // that defines that class.  That data structures contains a pointer to | 
 | 320 |   // its superclass.  But instead of just initializing that part of the | 
 | 321 |   // struct to the address of its superclass, and letting the static and | 
 | 322 |   // dynamic linkers do the rest, the runtime works by having that field | 
 | 323 |   // instead point to a C-string that is the name of the superclass. | 
 | 324 |   // At runtime the objc initialization updates that pointer and sets | 
 | 325 |   // it to point to the actual super class.  As far as the linker | 
 | 326 |   // knows it is just a pointer to a string.  But then someone wanted the | 
 | 327 |   // linker to issue errors at build time if the superclass was not found. | 
 | 328 |   // So they figured out a way in mach-o object format to use an absolute | 
 | 329 |   // symbols (.objc_class_name_Foo = 0) and a floating reference | 
 | 330 |   // (.reference .objc_class_name_Bar) to cause the linker into erroring when | 
 | 331 |   // a class was missing. | 
 | 332 |   // The following synthesizes the implicit .objc_* symbols for the linker | 
 | 333 |   // from the ObjC data structures generated by the front end. | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 334 |  | 
| Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 335 |   // special case if this data blob is an ObjC class definition | 
 | 336 |   if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) { | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 337 |     if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { | 
| Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 338 |       addObjCClass(gv); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 339 |     } | 
| Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 340 |   } | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 341 |  | 
| Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 342 |   // special case if this data blob is an ObjC category definition | 
 | 343 |   else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) { | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 344 |     if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { | 
| Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 345 |       addObjCCategory(gv); | 
 | 346 |     } | 
 | 347 |   } | 
 | 348 |  | 
 | 349 |   // special case if this data blob is the list of referenced classes | 
 | 350 |   else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) { | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 351 |     if (const GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { | 
| Bill Wendling | eda3fc6 | 2012-08-06 22:52:45 +0000 | [diff] [blame] | 352 |       addObjCClassRef(gv); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 353 |     } | 
 | 354 |   } | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 355 | } | 
 | 356 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 357 | /// addDefinedFunctionSymbol - Add a function symbol as defined to the list. | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 358 | void LTOModule::addDefinedFunctionSymbol(const Function *f) { | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 359 |   // add to list of defined symbols | 
 | 360 |   addDefinedSymbol(f, true); | 
 | 361 | } | 
 | 362 |  | 
| Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 363 | static bool canBeHidden(const GlobalValue *GV) { | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 364 |   // FIXME: this is duplicated with another static function in AsmPrinter.cpp | 
| Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 365 |   GlobalValue::LinkageTypes L = GV->getLinkage(); | 
 | 366 |  | 
| Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 367 |   if (L != GlobalValue::LinkOnceODRLinkage) | 
 | 368 |     return false; | 
 | 369 |  | 
 | 370 |   if (GV->hasUnnamedAddr()) | 
 | 371 |     return true; | 
 | 372 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 373 |   // If it is a non constant variable, it needs to be uniqued across shared | 
 | 374 |   // objects. | 
 | 375 |   if (const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV)) { | 
 | 376 |     if (!Var->isConstant()) | 
 | 377 |       return false; | 
 | 378 |   } | 
 | 379 |  | 
| Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 380 |   GlobalStatus GS; | 
 | 381 |   if (GlobalStatus::analyzeGlobal(GV, GS)) | 
 | 382 |     return false; | 
 | 383 |  | 
 | 384 |   return !GS.IsCompared; | 
 | 385 | } | 
 | 386 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 387 | /// addDefinedSymbol - Add a defined symbol to the list. | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 388 | void LTOModule::addDefinedSymbol(const GlobalValue *def, bool isFunction) { | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 389 |   // ignore all llvm.* symbols | 
 | 390 |   if (def->getName().startswith("llvm.")) | 
 | 391 |     return; | 
 | 392 |  | 
 | 393 |   // string is owned by _defines | 
| Rafael Espindola | ef1860a | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 394 |   SmallString<64> Buffer; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 395 |   _target->getNameWithPrefix(Buffer, def, _mangler); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 396 |  | 
 | 397 |   // set alignment part log2() can have rounding errors | 
 | 398 |   uint32_t align = def->getAlignment(); | 
| Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 399 |   uint32_t attr = align ? countTrailingZeros(def->getAlignment()) : 0; | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 400 |  | 
 | 401 |   // set permissions part | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 402 |   if (isFunction) { | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 403 |     attr |= LTO_SYMBOL_PERMISSIONS_CODE; | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 404 |   } else { | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 405 |     const GlobalVariable *gv = dyn_cast<GlobalVariable>(def); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 406 |     if (gv && gv->isConstant()) | 
 | 407 |       attr |= LTO_SYMBOL_PERMISSIONS_RODATA; | 
 | 408 |     else | 
 | 409 |       attr |= LTO_SYMBOL_PERMISSIONS_DATA; | 
 | 410 |   } | 
 | 411 |  | 
 | 412 |   // set definition part | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 413 |   if (def->hasWeakLinkage() || def->hasLinkOnceLinkage()) | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 414 |     attr |= LTO_SYMBOL_DEFINITION_WEAK; | 
| Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 415 |   else if (def->hasCommonLinkage()) | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 416 |     attr |= LTO_SYMBOL_DEFINITION_TENTATIVE; | 
| Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 417 |   else | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 418 |     attr |= LTO_SYMBOL_DEFINITION_REGULAR; | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 419 |  | 
 | 420 |   // set scope part | 
 | 421 |   if (def->hasHiddenVisibility()) | 
 | 422 |     attr |= LTO_SYMBOL_SCOPE_HIDDEN; | 
 | 423 |   else if (def->hasProtectedVisibility()) | 
 | 424 |     attr |= LTO_SYMBOL_SCOPE_PROTECTED; | 
| Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 425 |   else if (canBeHidden(def)) | 
 | 426 |     attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN; | 
| Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 427 |   else if (def->hasExternalLinkage() || def->hasWeakLinkage() || | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 428 |            def->hasLinkOnceLinkage() || def->hasCommonLinkage()) | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 429 |     attr |= LTO_SYMBOL_SCOPE_DEFAULT; | 
 | 430 |   else | 
 | 431 |     attr |= LTO_SYMBOL_SCOPE_INTERNAL; | 
 | 432 |  | 
| Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 433 |   StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer); | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 434 |   entry.setValue(1); | 
 | 435 |  | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 436 |   // fill information structure | 
 | 437 |   NameAndAttributes info; | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 438 |   StringRef Name = entry.getKey(); | 
 | 439 |   info.name = Name.data(); | 
 | 440 |   assert(info.name[Name.size()] == '\0'); | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 441 |   info.attributes = attr; | 
 | 442 |   info.isFunction = isFunction; | 
 | 443 |   info.symbol = def; | 
 | 444 |  | 
 | 445 |   // add to table of symbols | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 446 |   _symbols.push_back(info); | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 447 | } | 
 | 448 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 449 | /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the | 
 | 450 | /// defined list. | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 451 | void LTOModule::addAsmGlobalSymbol(const char *name, | 
 | 452 |                                    lto_symbol_attributes scope) { | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 453 |   StringSet::value_type &entry = _defines.GetOrCreateValue(name); | 
 | 454 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 455 |   // only add new define if not already defined | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 456 |   if (entry.getValue()) | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 457 |     return; | 
 | 458 |  | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 459 |   entry.setValue(1); | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 460 |  | 
 | 461 |   NameAndAttributes &info = _undefines[entry.getKey().data()]; | 
 | 462 |  | 
| Bill Wendling | 1fcbca0 | 2012-04-02 03:33:31 +0000 | [diff] [blame] | 463 |   if (info.symbol == 0) { | 
| Bill Wendling | 8ba9405 | 2012-04-02 10:01:21 +0000 | [diff] [blame] | 464 |     // FIXME: This is trying to take care of module ASM like this: | 
 | 465 |     // | 
 | 466 |     //   module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0" | 
 | 467 |     // | 
 | 468 |     // but is gross and its mother dresses it funny. Have the ASM parser give us | 
 | 469 |     // more details for this type of situation so that we're not guessing so | 
 | 470 |     // much. | 
 | 471 |  | 
 | 472 |     // fill information structure | 
| Rafael Espindola | 383fd7a | 2012-05-11 03:42:13 +0000 | [diff] [blame] | 473 |     info.name = entry.getKey().data(); | 
| Bill Wendling | 8ba9405 | 2012-04-02 10:01:21 +0000 | [diff] [blame] | 474 |     info.attributes = | 
 | 475 |       LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope; | 
 | 476 |     info.isFunction = false; | 
 | 477 |     info.symbol = 0; | 
 | 478 |  | 
 | 479 |     // add to table of symbols | 
 | 480 |     _symbols.push_back(info); | 
| Bill Wendling | 1fcbca0 | 2012-04-02 03:33:31 +0000 | [diff] [blame] | 481 |     return; | 
 | 482 |   } | 
 | 483 |  | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 484 |   if (info.isFunction) | 
 | 485 |     addDefinedFunctionSymbol(cast<Function>(info.symbol)); | 
 | 486 |   else | 
 | 487 |     addDefinedDataSymbol(info.symbol); | 
| Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 488 |  | 
 | 489 |   _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK; | 
 | 490 |   _symbols.back().attributes |= scope; | 
| Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 491 | } | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 492 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 493 | /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the | 
 | 494 | /// undefined list. | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 495 | void LTOModule::addAsmGlobalSymbolUndef(const char *name) { | 
 | 496 |   StringMap<NameAndAttributes>::value_type &entry = | 
 | 497 |     _undefines.GetOrCreateValue(name); | 
 | 498 |  | 
 | 499 |   _asm_undefines.push_back(entry.getKey().data()); | 
 | 500 |  | 
 | 501 |   // we already have the symbol | 
 | 502 |   if (entry.getValue().name) | 
 | 503 |     return; | 
 | 504 |  | 
 | 505 |   uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;; | 
 | 506 |   attr |= LTO_SYMBOL_SCOPE_DEFAULT; | 
 | 507 |   NameAndAttributes info; | 
 | 508 |   info.name = entry.getKey().data(); | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 509 |   info.attributes = attr; | 
 | 510 |   info.isFunction = false; | 
 | 511 |   info.symbol = 0; | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 512 |  | 
 | 513 |   entry.setValue(info); | 
 | 514 | } | 
 | 515 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 516 | /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a | 
 | 517 | /// list to be resolved later. | 
| Rafael Espindola | 613abf3 | 2012-12-11 03:10:43 +0000 | [diff] [blame] | 518 | void | 
 | 519 | LTOModule::addPotentialUndefinedSymbol(const GlobalValue *decl, bool isFunc) { | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 520 |   // ignore all llvm.* symbols | 
 | 521 |   if (decl->getName().startswith("llvm.")) | 
 | 522 |     return; | 
| Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 523 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 524 |   // ignore all aliases | 
 | 525 |   if (isa<GlobalAlias>(decl)) | 
 | 526 |     return; | 
| Nick Lewycky | 485ded0 | 2009-07-09 06:03:04 +0000 | [diff] [blame] | 527 |  | 
| Rafael Espindola | ef1860a | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 528 |   SmallString<64> name; | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 529 |   _target->getNameWithPrefix(name, decl, _mangler); | 
| Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 530 |  | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 531 |   StringMap<NameAndAttributes>::value_type &entry = | 
| Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 532 |     _undefines.GetOrCreateValue(name); | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 533 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 534 |   // we already have the symbol | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 535 |   if (entry.getValue().name) | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 536 |     return; | 
| Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 537 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 538 |   NameAndAttributes info; | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 539 |  | 
 | 540 |   info.name = entry.getKey().data(); | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 541 |  | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 542 |   if (decl->hasExternalWeakLinkage()) | 
 | 543 |     info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF; | 
 | 544 |   else | 
 | 545 |     info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 546 |  | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 547 |   info.isFunction = isFunc; | 
 | 548 |   info.symbol = decl; | 
 | 549 |  | 
| Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 550 |   entry.setValue(info); | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 551 | } | 
 | 552 |  | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 553 | namespace { | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 554 |  | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 555 |   class RecordStreamer : public MCStreamer { | 
 | 556 |   public: | 
| Bill Wendling | 90e7d4f | 2012-04-03 03:56:52 +0000 | [diff] [blame] | 557 |     enum State { NeverSeen, Global, Defined, DefinedGlobal, Used }; | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 558 |  | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 559 |   private: | 
 | 560 |     StringMap<State> Symbols; | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 561 |  | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 562 |     void markDefined(const MCSymbol &Symbol) { | 
 | 563 |       State &S = Symbols[Symbol.getName()]; | 
 | 564 |       switch (S) { | 
 | 565 |       case DefinedGlobal: | 
 | 566 |       case Global: | 
 | 567 |         S = DefinedGlobal; | 
 | 568 |         break; | 
 | 569 |       case NeverSeen: | 
 | 570 |       case Defined: | 
 | 571 |       case Used: | 
 | 572 |         S = Defined; | 
 | 573 |         break; | 
 | 574 |       } | 
 | 575 |     } | 
 | 576 |     void markGlobal(const MCSymbol &Symbol) { | 
 | 577 |       State &S = Symbols[Symbol.getName()]; | 
 | 578 |       switch (S) { | 
 | 579 |       case DefinedGlobal: | 
 | 580 |       case Defined: | 
 | 581 |         S = DefinedGlobal; | 
 | 582 |         break; | 
 | 583 |  | 
 | 584 |       case NeverSeen: | 
 | 585 |       case Global: | 
 | 586 |       case Used: | 
 | 587 |         S = Global; | 
 | 588 |         break; | 
 | 589 |       } | 
 | 590 |     } | 
 | 591 |     void markUsed(const MCSymbol &Symbol) { | 
 | 592 |       State &S = Symbols[Symbol.getName()]; | 
 | 593 |       switch (S) { | 
 | 594 |       case DefinedGlobal: | 
 | 595 |       case Defined: | 
 | 596 |       case Global: | 
 | 597 |         break; | 
 | 598 |  | 
 | 599 |       case NeverSeen: | 
 | 600 |       case Used: | 
 | 601 |         S = Used; | 
 | 602 |         break; | 
 | 603 |       } | 
 | 604 |     } | 
 | 605 |  | 
 | 606 |     // FIXME: mostly copied for the obj streamer. | 
 | 607 |     void AddValueSymbols(const MCExpr *Value) { | 
 | 608 |       switch (Value->getKind()) { | 
 | 609 |       case MCExpr::Target: | 
 | 610 |         // FIXME: What should we do in here? | 
 | 611 |         break; | 
 | 612 |  | 
 | 613 |       case MCExpr::Constant: | 
 | 614 |         break; | 
 | 615 |  | 
 | 616 |       case MCExpr::Binary: { | 
 | 617 |         const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); | 
 | 618 |         AddValueSymbols(BE->getLHS()); | 
 | 619 |         AddValueSymbols(BE->getRHS()); | 
 | 620 |         break; | 
 | 621 |       } | 
 | 622 |  | 
 | 623 |       case MCExpr::SymbolRef: | 
 | 624 |         markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol()); | 
 | 625 |         break; | 
 | 626 |  | 
 | 627 |       case MCExpr::Unary: | 
 | 628 |         AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); | 
 | 629 |         break; | 
 | 630 |       } | 
 | 631 |     } | 
 | 632 |  | 
 | 633 |   public: | 
 | 634 |     typedef StringMap<State>::const_iterator const_iterator; | 
 | 635 |  | 
 | 636 |     const_iterator begin() { | 
 | 637 |       return Symbols.begin(); | 
 | 638 |     } | 
 | 639 |  | 
 | 640 |     const_iterator end() { | 
 | 641 |       return Symbols.end(); | 
 | 642 |     } | 
 | 643 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 644 |     RecordStreamer(MCContext &Context) : MCStreamer(Context) {} | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 645 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 646 |     void EmitInstruction(const MCInst &Inst, | 
 | 647 |                          const MCSubtargetInfo &STI) override { | 
| Bill Wendling | 90e7d4f | 2012-04-03 03:56:52 +0000 | [diff] [blame] | 648 |       // Scan for values. | 
 | 649 |       for (unsigned i = Inst.getNumOperands(); i--; ) | 
 | 650 |         if (Inst.getOperand(i).isExpr()) | 
 | 651 |           AddValueSymbols(Inst.getOperand(i).getExpr()); | 
 | 652 |     } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 653 |     void EmitLabel(MCSymbol *Symbol) override { | 
| Peter Collingbourne | df39be6 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 654 |       Symbol->setSection(*getCurrentSection().first); | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 655 |       markDefined(*Symbol); | 
 | 656 |     } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 657 |     void EmitDebugLabel(MCSymbol *Symbol) override { | 
| Reed Kotler | 2c3a464 | 2012-12-16 04:00:45 +0000 | [diff] [blame] | 658 |       EmitLabel(Symbol); | 
 | 659 |     } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 660 |     void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override { | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 661 |       // FIXME: should we handle aliases? | 
 | 662 |       markDefined(*Symbol); | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 663 |       AddValueSymbols(Value); | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 664 |     } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 665 |     bool EmitSymbolAttribute(MCSymbol *Symbol, | 
 | 666 |                              MCSymbolAttr Attribute) override { | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 667 |       if (Attribute == MCSA_Global) | 
 | 668 |         markGlobal(*Symbol); | 
| Saleem Abdulrasool | 1c9cd02 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 669 |       return true; | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 670 |     } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 671 |     void EmitZerofill(const MCSection *Section, MCSymbol *Symbol, | 
 | 672 |                       uint64_t Size , unsigned ByteAlignment) override { | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 673 |       markDefined(*Symbol); | 
 | 674 |     } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 675 |     void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, | 
 | 676 |                           unsigned ByteAlignment) override { | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 677 |       markDefined(*Symbol); | 
 | 678 |     } | 
| Bill Wendling | 90e7d4f | 2012-04-03 03:56:52 +0000 | [diff] [blame] | 679 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 680 |     void EmitBundleAlignMode(unsigned AlignPow2) override {} | 
 | 681 |     void EmitBundleLock(bool AlignToEnd) override {} | 
 | 682 |     void EmitBundleUnlock() override {} | 
| Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 683 |  | 
| Bill Wendling | 90e7d4f | 2012-04-03 03:56:52 +0000 | [diff] [blame] | 684 |     // Noop calls. | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 685 |     void ChangeSection(const MCSection *Section, | 
 | 686 |                        const MCExpr *Subsection) override {} | 
 | 687 |     void EmitAssemblerFlag(MCAssemblerFlag Flag) override {} | 
 | 688 |     void EmitThumbFunc(MCSymbol *Func) override {} | 
 | 689 |     void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override {} | 
 | 690 |     void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) override {} | 
 | 691 |     void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {} | 
 | 692 |     void EmitCOFFSymbolStorageClass(int StorageClass) override {} | 
 | 693 |     void EmitCOFFSymbolType(int Type) override {} | 
 | 694 |     void EndCOFFSymbolDef() override {} | 
 | 695 |     void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {} | 
 | 696 |     void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, | 
 | 697 |                                unsigned ByteAlignment) override {} | 
 | 698 |     void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, | 
 | 699 |                         uint64_t Size, unsigned ByteAlignment) override {} | 
 | 700 |     void EmitBytes(StringRef Data) override {} | 
 | 701 |     void EmitValueImpl(const MCExpr *Value, unsigned Size) override {} | 
 | 702 |     void EmitULEB128Value(const MCExpr *Value) override {} | 
 | 703 |     void EmitSLEB128Value(const MCExpr *Value) override {} | 
 | 704 |     void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, | 
 | 705 |                               unsigned ValueSize, | 
 | 706 |                               unsigned MaxBytesToEmit) override {} | 
 | 707 |     void EmitCodeAlignment(unsigned ByteAlignment, | 
 | 708 |                            unsigned MaxBytesToEmit) override {} | 
 | 709 |     bool EmitValueToOffset(const MCExpr *Offset, | 
 | 710 |                            unsigned char Value) override { return false; } | 
 | 711 |     void EmitFileDirective(StringRef Filename) override {} | 
 | 712 |     void EmitDwarfAdvanceLineAddr(int64_t LineDelta, const MCSymbol *LastLabel, | 
 | 713 |                                   const MCSymbol *Label, | 
 | 714 |                                   unsigned PointerSize) override {} | 
 | 715 |     void FinishImpl() override {} | 
 | 716 |     void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame) override { | 
| Peter Collingbourne | 88fae0e | 2013-09-19 22:15:52 +0000 | [diff] [blame] | 717 |       RecordProcEnd(Frame); | 
 | 718 |     } | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 719 |   }; | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 720 | } // end anonymous namespace | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 721 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 722 | /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the | 
 | 723 | /// defined or undefined lists. | 
| Bill Wendling | b9bff96 | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 724 | bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) { | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 725 |   const std::string &inlineAsm = _module->getModuleInlineAsm(); | 
| Ivan Krasin | 6d483c2 | 2011-09-08 07:38:25 +0000 | [diff] [blame] | 726 |   if (inlineAsm.empty()) | 
 | 727 |     return false; | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 728 |  | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 729 |   std::unique_ptr<RecordStreamer> Streamer(new RecordStreamer(_context)); | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 730 |   MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm); | 
 | 731 |   SourceMgr SrcMgr; | 
 | 732 |   SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 733 |   std::unique_ptr<MCAsmParser> Parser( | 
 | 734 |       createMCAsmParser(SrcMgr, _context, *Streamer, *_target->getMCAsmInfo())); | 
| Bill Wendling | 5682527 | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 735 |   const Target &T = _target->getTarget(); | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 736 |   std::unique_ptr<MCInstrInfo> MCII(T.createMCInstrInfo()); | 
 | 737 |   std::unique_ptr<MCSubtargetInfo> STI(T.createMCSubtargetInfo( | 
 | 738 |       _target->getTargetTriple(), _target->getTargetCPU(), | 
 | 739 |       _target->getTargetFeatureString())); | 
 | 740 |   std::unique_ptr<MCTargetAsmParser> TAP( | 
 | 741 |       T.createMCAsmParser(*STI, *Parser.get(), *MCII)); | 
| Ivan Krasin | 603e103 | 2011-09-08 07:36:39 +0000 | [diff] [blame] | 742 |   if (!TAP) { | 
| Bill Wendling | 5682527 | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 743 |     errMsg = "target " + std::string(T.getName()) + | 
 | 744 |       " does not define AsmParser."; | 
| Ivan Krasin | 603e103 | 2011-09-08 07:36:39 +0000 | [diff] [blame] | 745 |     return true; | 
 | 746 |   } | 
 | 747 |  | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 748 |   Parser->setTargetParser(*TAP); | 
| Bill Wendling | 5682527 | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 749 |   if (Parser->Run(false)) | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 750 |     return true; | 
 | 751 |  | 
 | 752 |   for (RecordStreamer::const_iterator i = Streamer->begin(), | 
 | 753 |          e = Streamer->end(); i != e; ++i) { | 
 | 754 |     StringRef Key = i->first(); | 
 | 755 |     RecordStreamer::State Value = i->second; | 
 | 756 |     if (Value == RecordStreamer::DefinedGlobal) | 
 | 757 |       addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT); | 
 | 758 |     else if (Value == RecordStreamer::Defined) | 
 | 759 |       addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL); | 
 | 760 |     else if (Value == RecordStreamer::Global || | 
 | 761 |              Value == RecordStreamer::Used) | 
 | 762 |       addAsmGlobalSymbolUndef(Key.data()); | 
 | 763 |   } | 
| Bill Wendling | 5682527 | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 764 |  | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 765 |   return false; | 
 | 766 | } | 
 | 767 |  | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 768 | /// isDeclaration - Return 'true' if the global value is a declaration. | 
| Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 769 | static bool isDeclaration(const GlobalValue &V) { | 
 | 770 |   if (V.hasAvailableExternallyLinkage()) | 
 | 771 |     return true; | 
| Bill Wendling | 5682527 | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 772 |  | 
| Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 773 |   if (V.isMaterializable()) | 
 | 774 |     return false; | 
| Bill Wendling | 5682527 | 2012-08-08 22:01:55 +0000 | [diff] [blame] | 775 |  | 
| Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 776 |   return V.isDeclaration(); | 
 | 777 | } | 
 | 778 |  | 
| Bill Wendling | 3bb1738 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 779 | /// parseSymbols - Parse the symbols from the module and model-level ASM and add | 
| Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 780 | /// them to either the defined or undefined lists. | 
| Bill Wendling | 3bb1738 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 781 | bool LTOModule::parseSymbols(std::string &errMsg) { | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 782 |   // add functions | 
| Bill Wendling | 9f3b483 | 2012-03-29 03:34:57 +0000 | [diff] [blame] | 783 |   for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) { | 
| Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 784 |     if (isDeclaration(*f)) | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 785 |       addPotentialUndefinedSymbol(f, true); | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 786 |     else | 
| Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 787 |       addDefinedFunctionSymbol(f); | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 788 |   } | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 789 |  | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 790 |   // add data | 
 | 791 |   for (Module::global_iterator v = _module->global_begin(), | 
 | 792 |          e = _module->global_end(); v !=  e; ++v) { | 
| Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 793 |     if (isDeclaration(*v)) | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 794 |       addPotentialUndefinedSymbol(v, false); | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 795 |     else | 
| Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 796 |       addDefinedDataSymbol(v); | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 797 |   } | 
| Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 798 |  | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 799 |   // add asm globals | 
| Bill Wendling | b9bff96 | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 800 |   if (addAsmGlobalSymbols(errMsg)) | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 801 |     return true; | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 802 |  | 
| Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 803 |   // add aliases | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 804 |   for (Module::alias_iterator a = _module->alias_begin(), | 
 | 805 |          e = _module->alias_end(); a != e; ++a) { | 
 | 806 |     if (isDeclaration(*a->getAliasedGlobal())) | 
| Bill Wendling | 61476d6 | 2012-03-28 20:48:49 +0000 | [diff] [blame] | 807 |       // Is an alias to a declaration. | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 808 |       addPotentialUndefinedSymbol(a, false); | 
| Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 809 |     else | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 810 |       addDefinedDataSymbol(a); | 
| Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 811 |   } | 
 | 812 |  | 
| Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 813 |   // make symbols for all undefines | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 814 |   for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(), | 
 | 815 |          e = _undefines.end(); u != e; ++u) { | 
 | 816 |     // If this symbol also has a definition, then don't make an undefine because | 
 | 817 |     // it is a tentative definition. | 
 | 818 |     if (_defines.count(u->getKey())) continue; | 
 | 819 |     NameAndAttributes info = u->getValue(); | 
 | 820 |     _symbols.push_back(info); | 
| Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 821 |   } | 
| Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 822 |  | 
| Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 823 |   return false; | 
| Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 824 | } | 
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 825 |  | 
 | 826 | /// parseMetadata - Parse metadata from the module | 
 | 827 | void LTOModule::parseMetadata() { | 
 | 828 |   // Linker Options | 
 | 829 |   if (Value *Val = _module->getModuleFlag("Linker Options")) { | 
 | 830 |     MDNode *LinkerOptions = cast<MDNode>(Val); | 
 | 831 |     for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) { | 
 | 832 |       MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i)); | 
 | 833 |       for (unsigned ii = 0, ie = MDOptions->getNumOperands(); ii != ie; ++ii) { | 
 | 834 |         MDString *MDOption = cast<MDString>(MDOptions->getOperand(ii)); | 
 | 835 |         StringRef Op = _linkeropt_strings. | 
 | 836 |             GetOrCreateValue(MDOption->getString()).getKey(); | 
 | 837 |         StringRef DepLibName = _target->getTargetLowering()-> | 
 | 838 |             getObjFileLowering().getDepLibFromLinkerOpt(Op); | 
 | 839 |         if (!DepLibName.empty()) | 
 | 840 |           _deplibs.push_back(DepLibName.data()); | 
 | 841 |         else if (!Op.empty()) | 
 | 842 |           _linkeropts.push_back(Op.data()); | 
 | 843 |       } | 
 | 844 |     } | 
 | 845 |   } | 
 | 846 |  | 
 | 847 |   // Add other interesting metadata here. | 
 | 848 | } |