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