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