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 | |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 15 | #include "LTOModule.h" |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 17 | #include "llvm/LLVMContext.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 19 | #include "llvm/Bitcode/ReaderWriter.h" |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCExpr.h" |
| 21 | #include "llvm/MC/MCInst.h" |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCStreamer.h" |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSubtargetInfo.h" |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSymbol.h" |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCTargetAsmParser.h" |
Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 26 | #include "llvm/MC/SubtargetFeature.h" |
| 27 | #include "llvm/MC/MCParser/MCAsmParser.h" |
Evan Cheng | 0e6a052 | 2011-07-18 20:57:22 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetRegisterInfo.h" |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame^] | 29 | #include "llvm/Support/CommandLine.h" |
Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Host.h" |
Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
| 32 | #include "llvm/Support/Path.h" |
Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/SourceMgr.h" |
Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/TargetRegistry.h" |
| 35 | #include "llvm/Support/TargetSelect.h" |
| 36 | #include "llvm/Support/system_error.h" |
| 37 | #include "llvm/ADT/OwningPtr.h" |
| 38 | #include "llvm/ADT/Triple.h" |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame^] | 41 | static cl::opt<bool> |
| 42 | EnableFPMAD("enable-fp-mad", |
| 43 | cl::desc("Enable less precise MAD instructions to be generated"), |
| 44 | cl::init(false)); |
| 45 | |
| 46 | static cl::opt<bool> |
| 47 | DisableFPElim("disable-fp-elim", |
| 48 | cl::desc("Disable frame pointer elimination optimization"), |
| 49 | cl::init(false)); |
| 50 | |
| 51 | static cl::opt<bool> |
| 52 | DisableFPElimNonLeaf("disable-non-leaf-fp-elim", |
| 53 | cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"), |
| 54 | cl::init(false)); |
| 55 | |
| 56 | static cl::opt<bool> |
| 57 | EnableUnsafeFPMath("enable-unsafe-fp-math", |
| 58 | cl::desc("Enable optimizations that may decrease FP precision"), |
| 59 | cl::init(false)); |
| 60 | |
| 61 | static cl::opt<bool> |
| 62 | EnableNoInfsFPMath("enable-no-infs-fp-math", |
| 63 | cl::desc("Enable FP math optimizations that assume no +-Infs"), |
| 64 | cl::init(false)); |
| 65 | |
| 66 | static cl::opt<bool> |
| 67 | EnableNoNaNsFPMath("enable-no-nans-fp-math", |
| 68 | cl::desc("Enable FP math optimizations that assume no NaNs"), |
| 69 | cl::init(false)); |
| 70 | |
| 71 | static cl::opt<bool> |
| 72 | EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math", |
| 73 | cl::Hidden, |
| 74 | cl::desc("Force codegen to assume rounding mode can change dynamically"), |
| 75 | cl::init(false)); |
| 76 | |
| 77 | static cl::opt<bool> |
| 78 | GenerateSoftFloatCalls("soft-float", |
| 79 | cl::desc("Generate software floating point library calls"), |
| 80 | cl::init(false)); |
| 81 | |
| 82 | static cl::opt<llvm::FloatABI::ABIType> |
| 83 | FloatABIForCalls("float-abi", |
| 84 | cl::desc("Choose float ABI type"), |
| 85 | cl::init(FloatABI::Default), |
| 86 | cl::values( |
| 87 | clEnumValN(FloatABI::Default, "default", |
| 88 | "Target default float ABI type"), |
| 89 | clEnumValN(FloatABI::Soft, "soft", |
| 90 | "Soft float ABI (implied by -soft-float)"), |
| 91 | clEnumValN(FloatABI::Hard, "hard", |
| 92 | "Hard float ABI (uses FP registers)"), |
| 93 | clEnumValEnd)); |
| 94 | |
| 95 | static cl::opt<llvm::FPOpFusion::FPOpFusionMode> |
| 96 | FuseFPOps("fp-contract", |
| 97 | cl::desc("Enable aggresive formation of fused FP ops"), |
| 98 | cl::init(FPOpFusion::Standard), |
| 99 | cl::values( |
| 100 | clEnumValN(FPOpFusion::Fast, "fast", |
| 101 | "Fuse FP ops whenever profitable"), |
| 102 | clEnumValN(FPOpFusion::Standard, "on", |
| 103 | "Only fuse 'blessed' FP ops."), |
| 104 | clEnumValN(FPOpFusion::Strict, "off", |
| 105 | "Only fuse FP ops when the result won't be effected."), |
| 106 | clEnumValEnd)); |
| 107 | |
| 108 | static cl::opt<bool> |
| 109 | DontPlaceZerosInBSS("nozero-initialized-in-bss", |
| 110 | cl::desc("Don't place zero-initialized symbols into bss section"), |
| 111 | cl::init(false)); |
| 112 | |
| 113 | static cl::opt<bool> |
| 114 | EnableGuaranteedTailCallOpt("tailcallopt", |
| 115 | cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."), |
| 116 | cl::init(false)); |
| 117 | |
| 118 | static cl::opt<bool> |
| 119 | DisableTailCalls("disable-tail-calls", |
| 120 | cl::desc("Never emit tail calls"), |
| 121 | cl::init(false)); |
| 122 | |
| 123 | static cl::opt<unsigned> |
| 124 | OverrideStackAlignment("stack-alignment", |
| 125 | cl::desc("Override default stack alignment"), |
| 126 | cl::init(0)); |
| 127 | |
| 128 | static cl::opt<bool> |
| 129 | EnableRealignStack("realign-stack", |
| 130 | cl::desc("Realign stack if needed"), |
| 131 | cl::init(true)); |
| 132 | |
| 133 | static cl::opt<std::string> |
| 134 | TrapFuncName("trap-func", cl::Hidden, |
| 135 | cl::desc("Emit a call to trap function rather than a trap instruction"), |
| 136 | cl::init("")); |
| 137 | |
| 138 | static cl::opt<bool> |
| 139 | EnablePIE("enable-pie", |
| 140 | cl::desc("Assume the creation of a position independent executable."), |
| 141 | cl::init(false)); |
| 142 | |
| 143 | static cl::opt<bool> |
| 144 | SegmentedStacks("segmented-stacks", |
| 145 | cl::desc("Use segmented stacks if possible."), |
| 146 | cl::init(false)); |
| 147 | |
| 148 | static cl::opt<bool> |
| 149 | UseInitArray("use-init-array", |
| 150 | cl::desc("Use .init_array instead of .ctors."), |
| 151 | cl::init(false)); |
| 152 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 153 | LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t) |
| 154 | : _module(m), _target(t), |
| 155 | _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL), |
| 156 | _mangler(_context, *_target->getTargetData()) {} |
| 157 | |
| 158 | /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM |
| 159 | /// bitcode. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 160 | bool LTOModule::isBitcodeFile(const void *mem, size_t length) { |
| 161 | return llvm::sys::IdentifyFileType((char*)mem, length) |
| 162 | == llvm::sys::Bitcode_FileType; |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 165 | bool LTOModule::isBitcodeFile(const char *path) { |
| 166 | return llvm::sys::Path(path).isBitcodeFile(); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 169 | /// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is |
| 170 | /// LLVM bitcode for the specified triple. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 171 | bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, |
| 172 | const char *triplePrefix) { |
| 173 | MemoryBuffer *buffer = makeBuffer(mem, length); |
| 174 | if (!buffer) |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 175 | return false; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 176 | return isTargetMatch(buffer, triplePrefix); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 179 | bool LTOModule::isBitcodeFileForTarget(const char *path, |
| 180 | const char *triplePrefix) { |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 181 | OwningPtr<MemoryBuffer> buffer; |
| 182 | if (MemoryBuffer::getFile(path, buffer)) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 183 | return false; |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 184 | return isTargetMatch(buffer.take(), triplePrefix); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 187 | /// isTargetMatch - Returns 'true' if the memory buffer is for the specified |
| 188 | /// target triple. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 189 | bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) { |
Bill Wendling | 3471174 | 2010-10-06 01:22:42 +0000 | [diff] [blame] | 190 | std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); |
| 191 | delete buffer; |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 192 | return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 195 | /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of |
| 196 | /// the buffer. |
| 197 | LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) { |
Michael J. Spencer | 3ff9563 | 2010-12-16 03:29:14 +0000 | [diff] [blame] | 198 | OwningPtr<MemoryBuffer> buffer; |
| 199 | if (error_code ec = MemoryBuffer::getFile(path, buffer)) { |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 200 | errMsg = ec.message(); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 201 | return NULL; |
Michael J. Spencer | f2f516f | 2010-12-09 18:06:07 +0000 | [diff] [blame] | 202 | } |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 203 | return makeLTOModule(buffer.take(), errMsg); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 206 | LTOModule *LTOModule::makeLTOModule(int fd, const char *path, |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 207 | size_t size, std::string &errMsg) { |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 208 | return makeLTOModule(fd, path, size, size, 0, errMsg); |
| 209 | } |
| 210 | |
| 211 | LTOModule *LTOModule::makeLTOModule(int fd, const char *path, |
| 212 | size_t file_size, |
| 213 | size_t map_size, |
| 214 | off_t offset, |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 215 | std::string &errMsg) { |
| 216 | OwningPtr<MemoryBuffer> buffer; |
Rafael Espindola | f21b105 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 217 | if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size, |
| 218 | map_size, offset, false)) { |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 219 | errMsg = ec.message(); |
| 220 | return NULL; |
| 221 | } |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 222 | return makeLTOModule(buffer.take(), errMsg); |
Rafael Espindola | b4cc031 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 225 | LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length, |
| 226 | std::string &errMsg) { |
| 227 | OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length)); |
| 228 | if (!buffer) |
| 229 | return NULL; |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 230 | return makeLTOModule(buffer.take(), errMsg); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame^] | 233 | void LTOModule::getTargetOptions(TargetOptions &Options) { |
| 234 | Options.LessPreciseFPMADOption = EnableFPMAD; |
| 235 | Options.NoFramePointerElim = DisableFPElim; |
| 236 | Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf; |
| 237 | Options.AllowFPOpFusion = FuseFPOps; |
| 238 | Options.UnsafeFPMath = EnableUnsafeFPMath; |
| 239 | Options.NoInfsFPMath = EnableNoInfsFPMath; |
| 240 | Options.NoNaNsFPMath = EnableNoNaNsFPMath; |
| 241 | Options.HonorSignDependentRoundingFPMathOption = |
| 242 | EnableHonorSignDependentRoundingFPMath; |
| 243 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 244 | if (FloatABIForCalls != FloatABI::Default) |
| 245 | Options.FloatABIType = FloatABIForCalls; |
| 246 | Options.NoZerosInBSS = DontPlaceZerosInBSS; |
| 247 | Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; |
| 248 | Options.DisableTailCalls = DisableTailCalls; |
| 249 | Options.StackAlignmentOverride = OverrideStackAlignment; |
| 250 | Options.RealignStack = EnableRealignStack; |
| 251 | Options.TrapFuncName = TrapFuncName; |
| 252 | Options.PositionIndependentExecutable = EnablePIE; |
| 253 | Options.EnableSegmentedStacks = SegmentedStacks; |
| 254 | Options.UseInitArray = UseInitArray; |
| 255 | } |
| 256 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 257 | LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer, |
| 258 | std::string &errMsg) { |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 259 | static bool Initialized = false; |
| 260 | if (!Initialized) { |
| 261 | InitializeAllTargets(); |
Evan Cheng | e78085a | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 262 | InitializeAllTargetMCs(); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 263 | InitializeAllAsmParsers(); |
| 264 | Initialized = true; |
| 265 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 266 | |
| 267 | // parse bitcode buffer |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 268 | OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(), |
| 269 | &errMsg)); |
| 270 | if (!m) { |
| 271 | delete buffer; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 272 | return NULL; |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 273 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 274 | |
| 275 | std::string Triple = m->getTargetTriple(); |
| 276 | if (Triple.empty()) |
Sebastian Pop | 0173864 | 2011-11-01 21:32:20 +0000 | [diff] [blame] | 277 | Triple = sys::getDefaultTargetTriple(); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 278 | |
| 279 | // find machine architecture for this module |
| 280 | const Target *march = TargetRegistry::lookupTarget(Triple, errMsg); |
| 281 | if (!march) |
| 282 | return NULL; |
| 283 | |
Nick Lewycky | 333ed45 | 2011-04-21 01:54:08 +0000 | [diff] [blame] | 284 | // construct LTOModule, hand over ownership of module and target |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 285 | SubtargetFeatures Features; |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 286 | Features.getDefaultSubtargetFeatures(llvm::Triple(Triple)); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 287 | std::string FeatureStr = Features.getString(); |
Evan Cheng | 276365d | 2011-06-30 01:53:36 +0000 | [diff] [blame] | 288 | std::string CPU; |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 289 | TargetOptions Options; |
Bill Wendling | 9ac0aaa | 2012-08-06 21:34:54 +0000 | [diff] [blame^] | 290 | getTargetOptions(Options); |
Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 291 | TargetMachine *target = march->createTargetMachine(Triple, CPU, FeatureStr, |
| 292 | Options); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 293 | LTOModule *Ret = new LTOModule(m.take(), target); |
Bill Wendling | 3bb1738 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 294 | if (Ret->parseSymbols(errMsg)) { |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 295 | delete Ret; |
| 296 | return NULL; |
| 297 | } |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 298 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 299 | return Ret; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 302 | /// makeBuffer - Create a MemoryBuffer from a memory range. |
| 303 | MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) { |
| 304 | const char *startPtr = (char*)mem; |
| 305 | return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 308 | /// objcClassNameFromExpression - Get string that the data pointer points to. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 309 | bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) { |
| 310 | if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) { |
| 311 | Constant *op = ce->getOperand(0); |
| 312 | if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) { |
| 313 | Constant *cn = gvn->getInitializer(); |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 314 | if (ConstantDataArray *ca = dyn_cast<ConstantDataArray>(cn)) { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 315 | if (ca->isCString()) { |
Chris Lattner | 18c7f80 | 2012-02-05 02:29:43 +0000 | [diff] [blame] | 316 | name = ".objc_class_name_" + ca->getAsCString().str(); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 317 | return true; |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 318 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 319 | } |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 320 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 321 | } |
| 322 | return false; |
| 323 | } |
| 324 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 325 | /// addObjCClass - Parse i386/ppc ObjC class data structure. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 326 | void LTOModule::addObjCClass(GlobalVariable *clgv) { |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 327 | ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); |
| 328 | if (!c) return; |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 329 | |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 330 | // second slot in __OBJC,__class is pointer to superclass name |
| 331 | std::string superclassName; |
| 332 | if (objcClassNameFromExpression(c->getOperand(1), superclassName)) { |
| 333 | NameAndAttributes info; |
| 334 | StringMap<NameAndAttributes>::value_type &entry = |
| 335 | _undefines.GetOrCreateValue(superclassName); |
| 336 | if (!entry.getValue().name) { |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 337 | const char *symbolName = entry.getKey().data(); |
| 338 | info.name = symbolName; |
| 339 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 340 | info.isFunction = false; |
| 341 | info.symbol = clgv; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 342 | entry.setValue(info); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 343 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 344 | } |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 345 | |
| 346 | // third slot in __OBJC,__class is pointer to class name |
| 347 | std::string className; |
| 348 | if (objcClassNameFromExpression(c->getOperand(2), className)) { |
| 349 | StringSet::value_type &entry = _defines.GetOrCreateValue(className); |
| 350 | entry.setValue(1); |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 351 | |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 352 | NameAndAttributes info; |
| 353 | info.name = entry.getKey().data(); |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 354 | info.attributes = LTO_SYMBOL_PERMISSIONS_DATA | |
| 355 | LTO_SYMBOL_DEFINITION_REGULAR | LTO_SYMBOL_SCOPE_DEFAULT; |
| 356 | info.isFunction = false; |
| 357 | info.symbol = clgv; |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 358 | _symbols.push_back(info); |
| 359 | } |
| 360 | } |
| 361 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 362 | /// addObjCCategory - Parse i386/ppc ObjC category data structure. |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 363 | void LTOModule::addObjCCategory(GlobalVariable *clgv) { |
| 364 | ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); |
| 365 | if (!c) return; |
| 366 | |
| 367 | // second slot in __OBJC,__category is pointer to target class name |
| 368 | std::string targetclassName; |
| 369 | if (!objcClassNameFromExpression(c->getOperand(1), targetclassName)) |
| 370 | return; |
| 371 | |
| 372 | NameAndAttributes info; |
| 373 | StringMap<NameAndAttributes>::value_type &entry = |
| 374 | _undefines.GetOrCreateValue(targetclassName); |
| 375 | |
| 376 | if (entry.getValue().name) |
| 377 | return; |
| 378 | |
| 379 | const char *symbolName = entry.getKey().data(); |
| 380 | info.name = symbolName; |
| 381 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 382 | info.isFunction = false; |
| 383 | info.symbol = clgv; |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 384 | entry.setValue(info); |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 387 | /// addObjCClassRef - Parse i386/ppc ObjC class list data structure. |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 388 | void LTOModule::addObjCClassRef(GlobalVariable *clgv) { |
| 389 | std::string targetclassName; |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 390 | if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) |
| 391 | return; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 392 | |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 393 | NameAndAttributes info; |
| 394 | StringMap<NameAndAttributes>::value_type &entry = |
| 395 | _undefines.GetOrCreateValue(targetclassName); |
| 396 | if (entry.getValue().name) |
| 397 | return; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 398 | |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 399 | const char *symbolName = entry.getKey().data(); |
| 400 | info.name = symbolName; |
| 401 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 402 | info.isFunction = false; |
| 403 | info.symbol = clgv; |
Bill Wendling | 931d4c2 | 2011-11-04 18:48:00 +0000 | [diff] [blame] | 404 | entry.setValue(info); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 407 | /// addDefinedDataSymbol - Add a data symbol as defined to the list. |
Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 408 | void LTOModule::addDefinedDataSymbol(GlobalValue *v) { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 409 | // Add to list of defined symbols. |
Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 410 | addDefinedSymbol(v, false); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 411 | |
| 412 | // Special case i386/ppc ObjC data structures in magic sections: |
| 413 | // The issue is that the old ObjC object format did some strange |
| 414 | // contortions to avoid real linker symbols. For instance, the |
| 415 | // ObjC class data structure is allocated statically in the executable |
| 416 | // that defines that class. That data structures contains a pointer to |
| 417 | // its superclass. But instead of just initializing that part of the |
| 418 | // struct to the address of its superclass, and letting the static and |
| 419 | // dynamic linkers do the rest, the runtime works by having that field |
| 420 | // instead point to a C-string that is the name of the superclass. |
| 421 | // At runtime the objc initialization updates that pointer and sets |
| 422 | // it to point to the actual super class. As far as the linker |
| 423 | // knows it is just a pointer to a string. But then someone wanted the |
| 424 | // linker to issue errors at build time if the superclass was not found. |
| 425 | // So they figured out a way in mach-o object format to use an absolute |
| 426 | // symbols (.objc_class_name_Foo = 0) and a floating reference |
| 427 | // (.reference .objc_class_name_Bar) to cause the linker into erroring when |
| 428 | // a class was missing. |
| 429 | // The following synthesizes the implicit .objc_* symbols for the linker |
| 430 | // from the ObjC data structures generated by the front end. |
| 431 | if (v->hasSection() /* && isTargetDarwin */) { |
| 432 | // special case if this data blob is an ObjC class definition |
| 433 | if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) { |
| 434 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 435 | addObjCClass(gv); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // special case if this data blob is an ObjC category definition |
| 440 | else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) { |
| 441 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 442 | addObjCCategory(gv); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // special case if this data blob is the list of referenced classes |
| 447 | else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) { |
| 448 | if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) { |
| 449 | addObjCClassRef(gv); |
| 450 | } |
| 451 | } |
| 452 | } |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 455 | /// addDefinedFunctionSymbol - Add a function symbol as defined to the list. |
| 456 | void LTOModule::addDefinedFunctionSymbol(Function *f) { |
| 457 | // add to list of defined symbols |
| 458 | addDefinedSymbol(f, true); |
| 459 | } |
| 460 | |
| 461 | /// addDefinedSymbol - Add a defined symbol to the list. |
Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 462 | void LTOModule::addDefinedSymbol(GlobalValue *def, bool isFunction) { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 463 | // ignore all llvm.* symbols |
| 464 | if (def->getName().startswith("llvm.")) |
| 465 | return; |
| 466 | |
| 467 | // string is owned by _defines |
Rafael Espindola | ef1860a | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 468 | SmallString<64> Buffer; |
Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 469 | _mangler.getNameWithPrefix(Buffer, def, false); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 470 | |
| 471 | // set alignment part log2() can have rounding errors |
| 472 | uint32_t align = def->getAlignment(); |
| 473 | uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0; |
| 474 | |
| 475 | // set permissions part |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 476 | if (isFunction) { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 477 | attr |= LTO_SYMBOL_PERMISSIONS_CODE; |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 478 | } else { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 479 | GlobalVariable *gv = dyn_cast<GlobalVariable>(def); |
| 480 | if (gv && gv->isConstant()) |
| 481 | attr |= LTO_SYMBOL_PERMISSIONS_RODATA; |
| 482 | else |
| 483 | attr |= LTO_SYMBOL_PERMISSIONS_DATA; |
| 484 | } |
| 485 | |
| 486 | // set definition part |
Bill Wendling | 563ef5e | 2010-09-27 18:05:19 +0000 | [diff] [blame] | 487 | if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() || |
| 488 | def->hasLinkerPrivateWeakLinkage() || |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 489 | def->hasLinkerPrivateWeakDefAutoLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 490 | attr |= LTO_SYMBOL_DEFINITION_WEAK; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 491 | else if (def->hasCommonLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 492 | attr |= LTO_SYMBOL_DEFINITION_TENTATIVE; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 493 | else |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 494 | attr |= LTO_SYMBOL_DEFINITION_REGULAR; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 495 | |
| 496 | // set scope part |
| 497 | if (def->hasHiddenVisibility()) |
| 498 | attr |= LTO_SYMBOL_SCOPE_HIDDEN; |
| 499 | else if (def->hasProtectedVisibility()) |
| 500 | attr |= LTO_SYMBOL_SCOPE_PROTECTED; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 501 | else if (def->hasExternalLinkage() || def->hasWeakLinkage() || |
| 502 | def->hasLinkOnceLinkage() || def->hasCommonLinkage() || |
| 503 | def->hasLinkerPrivateWeakLinkage()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 504 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
Bill Wendling | 7afea0c | 2010-09-27 20:17:45 +0000 | [diff] [blame] | 505 | else if (def->hasLinkerPrivateWeakDefAutoLinkage()) |
| 506 | attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 507 | else |
| 508 | attr |= LTO_SYMBOL_SCOPE_INTERNAL; |
| 509 | |
Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 510 | StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 511 | entry.setValue(1); |
| 512 | |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 513 | // fill information structure |
| 514 | NameAndAttributes info; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 515 | StringRef Name = entry.getKey(); |
| 516 | info.name = Name.data(); |
| 517 | assert(info.name[Name.size()] == '\0'); |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 518 | info.attributes = attr; |
| 519 | info.isFunction = isFunction; |
| 520 | info.symbol = def; |
| 521 | |
| 522 | // add to table of symbols |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 523 | _symbols.push_back(info); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 526 | /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the |
| 527 | /// defined list. |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 528 | void LTOModule::addAsmGlobalSymbol(const char *name, |
| 529 | lto_symbol_attributes scope) { |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 530 | StringSet::value_type &entry = _defines.GetOrCreateValue(name); |
| 531 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 532 | // only add new define if not already defined |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 533 | if (entry.getValue()) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 534 | return; |
| 535 | |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 536 | entry.setValue(1); |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 537 | |
| 538 | NameAndAttributes &info = _undefines[entry.getKey().data()]; |
| 539 | |
Bill Wendling | 1fcbca0 | 2012-04-02 03:33:31 +0000 | [diff] [blame] | 540 | if (info.symbol == 0) { |
Bill Wendling | 8ba9405 | 2012-04-02 10:01:21 +0000 | [diff] [blame] | 541 | // FIXME: This is trying to take care of module ASM like this: |
| 542 | // |
| 543 | // module asm ".zerofill __FOO, __foo, _bar_baz_qux, 0" |
| 544 | // |
| 545 | // but is gross and its mother dresses it funny. Have the ASM parser give us |
| 546 | // more details for this type of situation so that we're not guessing so |
| 547 | // much. |
| 548 | |
| 549 | // fill information structure |
Rafael Espindola | 383fd7a | 2012-05-11 03:42:13 +0000 | [diff] [blame] | 550 | info.name = entry.getKey().data(); |
Bill Wendling | 8ba9405 | 2012-04-02 10:01:21 +0000 | [diff] [blame] | 551 | info.attributes = |
| 552 | LTO_SYMBOL_PERMISSIONS_DATA | LTO_SYMBOL_DEFINITION_REGULAR | scope; |
| 553 | info.isFunction = false; |
| 554 | info.symbol = 0; |
| 555 | |
| 556 | // add to table of symbols |
| 557 | _symbols.push_back(info); |
Bill Wendling | 1fcbca0 | 2012-04-02 03:33:31 +0000 | [diff] [blame] | 558 | return; |
| 559 | } |
| 560 | |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 561 | if (info.isFunction) |
| 562 | addDefinedFunctionSymbol(cast<Function>(info.symbol)); |
| 563 | else |
| 564 | addDefinedDataSymbol(info.symbol); |
Bill Wendling | 5ff4bc2 | 2012-03-30 23:26:06 +0000 | [diff] [blame] | 565 | |
| 566 | _symbols.back().attributes &= ~LTO_SYMBOL_SCOPE_MASK; |
| 567 | _symbols.back().attributes |= scope; |
Devang Patel | c2aec57 | 2008-07-16 18:06:52 +0000 | [diff] [blame] | 568 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 569 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 570 | /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the |
| 571 | /// undefined list. |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 572 | void LTOModule::addAsmGlobalSymbolUndef(const char *name) { |
| 573 | StringMap<NameAndAttributes>::value_type &entry = |
| 574 | _undefines.GetOrCreateValue(name); |
| 575 | |
| 576 | _asm_undefines.push_back(entry.getKey().data()); |
| 577 | |
| 578 | // we already have the symbol |
| 579 | if (entry.getValue().name) |
| 580 | return; |
| 581 | |
| 582 | uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;; |
| 583 | attr |= LTO_SYMBOL_SCOPE_DEFAULT; |
| 584 | NameAndAttributes info; |
| 585 | info.name = entry.getKey().data(); |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 586 | info.attributes = attr; |
| 587 | info.isFunction = false; |
| 588 | info.symbol = 0; |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 589 | |
| 590 | entry.setValue(info); |
| 591 | } |
| 592 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 593 | /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a |
| 594 | /// list to be resolved later. |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 595 | void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl, bool isFunc) { |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 596 | // ignore all llvm.* symbols |
| 597 | if (decl->getName().startswith("llvm.")) |
| 598 | return; |
Nick Kledzik | 3eb445f | 2009-06-01 20:33:09 +0000 | [diff] [blame] | 599 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 600 | // ignore all aliases |
| 601 | if (isa<GlobalAlias>(decl)) |
| 602 | return; |
Nick Lewycky | 485ded0 | 2009-07-09 06:03:04 +0000 | [diff] [blame] | 603 | |
Rafael Espindola | ef1860a | 2011-02-11 05:23:09 +0000 | [diff] [blame] | 604 | SmallString<64> name; |
Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 605 | _mangler.getNameWithPrefix(name, decl, false); |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 606 | |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 607 | StringMap<NameAndAttributes>::value_type &entry = |
Chad Rosier | bd35f27 | 2011-06-28 18:26:12 +0000 | [diff] [blame] | 608 | _undefines.GetOrCreateValue(name); |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 609 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 610 | // we already have the symbol |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 611 | if (entry.getValue().name) |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 612 | return; |
Rafael Espindola | 7431af0 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 613 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 614 | NameAndAttributes info; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 615 | |
| 616 | info.name = entry.getKey().data(); |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 617 | |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 618 | if (decl->hasExternalWeakLinkage()) |
| 619 | info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF; |
| 620 | else |
| 621 | info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED; |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 622 | |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 623 | info.isFunction = isFunc; |
| 624 | info.symbol = decl; |
| 625 | |
Rafael Espindola | cd6c93e | 2011-02-20 16:27:25 +0000 | [diff] [blame] | 626 | entry.setValue(info); |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 629 | namespace { |
| 630 | class RecordStreamer : public MCStreamer { |
| 631 | public: |
Bill Wendling | 90e7d4f | 2012-04-03 03:56:52 +0000 | [diff] [blame] | 632 | enum State { NeverSeen, Global, Defined, DefinedGlobal, Used }; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 633 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 634 | private: |
| 635 | StringMap<State> Symbols; |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 636 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 637 | void markDefined(const MCSymbol &Symbol) { |
| 638 | State &S = Symbols[Symbol.getName()]; |
| 639 | switch (S) { |
| 640 | case DefinedGlobal: |
| 641 | case Global: |
| 642 | S = DefinedGlobal; |
| 643 | break; |
| 644 | case NeverSeen: |
| 645 | case Defined: |
| 646 | case Used: |
| 647 | S = Defined; |
| 648 | break; |
| 649 | } |
| 650 | } |
| 651 | void markGlobal(const MCSymbol &Symbol) { |
| 652 | State &S = Symbols[Symbol.getName()]; |
| 653 | switch (S) { |
| 654 | case DefinedGlobal: |
| 655 | case Defined: |
| 656 | S = DefinedGlobal; |
| 657 | break; |
| 658 | |
| 659 | case NeverSeen: |
| 660 | case Global: |
| 661 | case Used: |
| 662 | S = Global; |
| 663 | break; |
| 664 | } |
| 665 | } |
| 666 | void markUsed(const MCSymbol &Symbol) { |
| 667 | State &S = Symbols[Symbol.getName()]; |
| 668 | switch (S) { |
| 669 | case DefinedGlobal: |
| 670 | case Defined: |
| 671 | case Global: |
| 672 | break; |
| 673 | |
| 674 | case NeverSeen: |
| 675 | case Used: |
| 676 | S = Used; |
| 677 | break; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // FIXME: mostly copied for the obj streamer. |
| 682 | void AddValueSymbols(const MCExpr *Value) { |
| 683 | switch (Value->getKind()) { |
| 684 | case MCExpr::Target: |
| 685 | // FIXME: What should we do in here? |
| 686 | break; |
| 687 | |
| 688 | case MCExpr::Constant: |
| 689 | break; |
| 690 | |
| 691 | case MCExpr::Binary: { |
| 692 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value); |
| 693 | AddValueSymbols(BE->getLHS()); |
| 694 | AddValueSymbols(BE->getRHS()); |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | case MCExpr::SymbolRef: |
| 699 | markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol()); |
| 700 | break; |
| 701 | |
| 702 | case MCExpr::Unary: |
| 703 | AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr()); |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | public: |
| 709 | typedef StringMap<State>::const_iterator const_iterator; |
| 710 | |
| 711 | const_iterator begin() { |
| 712 | return Symbols.begin(); |
| 713 | } |
| 714 | |
| 715 | const_iterator end() { |
| 716 | return Symbols.end(); |
| 717 | } |
| 718 | |
| 719 | RecordStreamer(MCContext &Context) : MCStreamer(Context) {} |
| 720 | |
Bill Wendling | 90e7d4f | 2012-04-03 03:56:52 +0000 | [diff] [blame] | 721 | virtual void EmitInstruction(const MCInst &Inst) { |
| 722 | // Scan for values. |
| 723 | for (unsigned i = Inst.getNumOperands(); i--; ) |
| 724 | if (Inst.getOperand(i).isExpr()) |
| 725 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
| 726 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 727 | virtual void EmitLabel(MCSymbol *Symbol) { |
| 728 | Symbol->setSection(*getCurrentSection()); |
| 729 | markDefined(*Symbol); |
| 730 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 731 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 732 | // FIXME: should we handle aliases? |
| 733 | markDefined(*Symbol); |
| 734 | } |
| 735 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) { |
| 736 | if (Attribute == MCSA_Global) |
| 737 | markGlobal(*Symbol); |
| 738 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 739 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
Evan Cheng | df42d41 | 2012-06-22 20:30:39 +0000 | [diff] [blame] | 740 | uint64_t Size , unsigned ByteAlignment) { |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 741 | markDefined(*Symbol); |
| 742 | } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 743 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 744 | unsigned ByteAlignment) { |
| 745 | markDefined(*Symbol); |
| 746 | } |
Bill Wendling | 90e7d4f | 2012-04-03 03:56:52 +0000 | [diff] [blame] | 747 | |
| 748 | // Noop calls. |
| 749 | virtual void ChangeSection(const MCSection *Section) {} |
| 750 | virtual void InitSections() {} |
| 751 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {} |
| 752 | virtual void EmitThumbFunc(MCSymbol *Func) {} |
| 753 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {} |
| 754 | virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {} |
| 755 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {} |
| 756 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) {} |
| 757 | virtual void EmitCOFFSymbolType(int Type) {} |
| 758 | virtual void EndCOFFSymbolDef() {} |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 759 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {} |
Benjamin Kramer | 36a1601 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 760 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 761 | unsigned ByteAlignment) {} |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 762 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 763 | uint64_t Size, unsigned ByteAlignment) {} |
| 764 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {} |
| 765 | virtual void EmitValueImpl(const MCExpr *Value, unsigned Size, |
Rafael Espindola | debd7e4 | 2011-05-01 03:50:49 +0000 | [diff] [blame] | 766 | unsigned AddrSpace) {} |
Rafael Espindola | e8cfbd8 | 2011-04-21 23:39:26 +0000 | [diff] [blame] | 767 | virtual void EmitULEB128Value(const MCExpr *Value) {} |
| 768 | virtual void EmitSLEB128Value(const MCExpr *Value) {} |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 769 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value, |
| 770 | unsigned ValueSize, |
| 771 | unsigned MaxBytesToEmit) {} |
| 772 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 773 | unsigned MaxBytesToEmit) {} |
Jim Grosbach | ebd4c05 | 2012-01-27 00:37:08 +0000 | [diff] [blame] | 774 | virtual bool EmitValueToOffset(const MCExpr *Offset, |
| 775 | unsigned char Value ) { return false; } |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 776 | virtual void EmitFileDirective(StringRef Filename) {} |
| 777 | virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta, |
| 778 | const MCSymbol *LastLabel, |
Evan Cheng | 672b93a | 2011-07-14 05:43:07 +0000 | [diff] [blame] | 779 | const MCSymbol *Label, |
| 780 | unsigned PointerSize) {} |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 781 | virtual void FinishImpl() {} |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 782 | }; |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 783 | } // end anonymous namespace |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 784 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 785 | /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the |
| 786 | /// defined or undefined lists. |
Bill Wendling | b9bff96 | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 787 | bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) { |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 788 | const std::string &inlineAsm = _module->getModuleInlineAsm(); |
Ivan Krasin | 6d483c2 | 2011-09-08 07:38:25 +0000 | [diff] [blame] | 789 | if (inlineAsm.empty()) |
| 790 | return false; |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 791 | |
Bill Wendling | b9bff96 | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 792 | OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context)); |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 793 | MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm); |
| 794 | SourceMgr SrcMgr; |
| 795 | SrcMgr.AddNewSourceBuffer(Buffer, SMLoc()); |
Jim Grosbach | 1b84cce | 2011-08-16 18:33:49 +0000 | [diff] [blame] | 796 | OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr, |
Bill Wendling | b9bff96 | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 797 | _context, *Streamer, |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 798 | *_target->getMCAsmInfo())); |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 799 | OwningPtr<MCSubtargetInfo> STI(_target->getTarget(). |
| 800 | createMCSubtargetInfo(_target->getTargetTriple(), |
| 801 | _target->getTargetCPU(), |
| 802 | _target->getTargetFeatureString())); |
Evan Cheng | 94b9550 | 2011-07-26 00:24:13 +0000 | [diff] [blame] | 803 | OwningPtr<MCTargetAsmParser> |
| 804 | TAP(_target->getTarget().createMCAsmParser(*STI, *Parser.get())); |
Ivan Krasin | 603e103 | 2011-09-08 07:36:39 +0000 | [diff] [blame] | 805 | if (!TAP) { |
| 806 | errMsg = "target " + std::string(_target->getTarget().getName()) + |
| 807 | " does not define AsmParser."; |
| 808 | return true; |
| 809 | } |
| 810 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 811 | Parser->setTargetParser(*TAP); |
| 812 | int Res = Parser->Run(false); |
| 813 | if (Res) |
| 814 | return true; |
| 815 | |
| 816 | for (RecordStreamer::const_iterator i = Streamer->begin(), |
| 817 | e = Streamer->end(); i != e; ++i) { |
| 818 | StringRef Key = i->first(); |
| 819 | RecordStreamer::State Value = i->second; |
| 820 | if (Value == RecordStreamer::DefinedGlobal) |
| 821 | addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT); |
| 822 | else if (Value == RecordStreamer::Defined) |
| 823 | addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL); |
| 824 | else if (Value == RecordStreamer::Global || |
| 825 | Value == RecordStreamer::Used) |
| 826 | addAsmGlobalSymbolUndef(Key.data()); |
| 827 | } |
| 828 | return false; |
| 829 | } |
| 830 | |
Bill Wendling | 62cf01e | 2012-03-28 20:46:54 +0000 | [diff] [blame] | 831 | /// isDeclaration - Return 'true' if the global value is a declaration. |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 832 | static bool isDeclaration(const GlobalValue &V) { |
| 833 | if (V.hasAvailableExternallyLinkage()) |
| 834 | return true; |
| 835 | if (V.isMaterializable()) |
| 836 | return false; |
| 837 | return V.isDeclaration(); |
| 838 | } |
| 839 | |
Bill Wendling | 3bb1738 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 840 | /// 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] | 841 | /// them to either the defined or undefined lists. |
Bill Wendling | 3bb1738 | 2012-03-28 23:12:18 +0000 | [diff] [blame] | 842 | bool LTOModule::parseSymbols(std::string &errMsg) { |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 843 | // add functions |
Bill Wendling | 9f3b483 | 2012-03-29 03:34:57 +0000 | [diff] [blame] | 844 | for (Module::iterator f = _module->begin(), e = _module->end(); f != e; ++f) { |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 845 | if (isDeclaration(*f)) |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 846 | addPotentialUndefinedSymbol(f, true); |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 847 | else |
Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 848 | addDefinedFunctionSymbol(f); |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 849 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 850 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 851 | // add data |
| 852 | for (Module::global_iterator v = _module->global_begin(), |
| 853 | e = _module->global_end(); v != e; ++v) { |
Rafael Espindola | f19d7a7 | 2011-03-18 19:51:00 +0000 | [diff] [blame] | 854 | if (isDeclaration(*v)) |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 855 | addPotentialUndefinedSymbol(v, false); |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 856 | else |
Bill Wendling | a7280fd | 2011-11-04 09:30:19 +0000 | [diff] [blame] | 857 | addDefinedDataSymbol(v); |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 858 | } |
Nick Kledzik | 77595fc | 2008-02-26 20:26:43 +0000 | [diff] [blame] | 859 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 860 | // add asm globals |
Bill Wendling | b9bff96 | 2011-11-04 09:24:40 +0000 | [diff] [blame] | 861 | if (addAsmGlobalSymbols(errMsg)) |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 862 | return true; |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 863 | |
Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 864 | // add aliases |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 865 | for (Module::alias_iterator a = _module->alias_begin(), |
| 866 | e = _module->alias_end(); a != e; ++a) { |
| 867 | if (isDeclaration(*a->getAliasedGlobal())) |
Bill Wendling | 61476d6 | 2012-03-28 20:48:49 +0000 | [diff] [blame] | 868 | // Is an alias to a declaration. |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 869 | addPotentialUndefinedSymbol(a, false); |
Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 870 | else |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 871 | addDefinedDataSymbol(a); |
Rafael Espindola | 02003ca | 2010-10-20 04:57:22 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Daniel Dunbar | e41d900 | 2010-08-10 23:46:46 +0000 | [diff] [blame] | 874 | // make symbols for all undefines |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 875 | for (StringMap<NameAndAttributes>::iterator u =_undefines.begin(), |
| 876 | e = _undefines.end(); u != e; ++u) { |
| 877 | // If this symbol also has a definition, then don't make an undefine because |
| 878 | // it is a tentative definition. |
| 879 | if (_defines.count(u->getKey())) continue; |
| 880 | NameAndAttributes info = u->getValue(); |
| 881 | _symbols.push_back(info); |
Daniel Dunbar | b06913d | 2010-08-10 23:46:39 +0000 | [diff] [blame] | 882 | } |
Bill Wendling | 24b8780 | 2012-03-29 08:27:32 +0000 | [diff] [blame] | 883 | |
Rafael Espindola | 38c4e53 | 2011-03-02 04:14:42 +0000 | [diff] [blame] | 884 | return false; |
Nick Kledzik | ef194ed | 2008-02-27 22:25:36 +0000 | [diff] [blame] | 885 | } |