Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 1 | //===- ThinLTOBitcodeWriter.cpp - Bitcode writing pass for ThinLTO --------===// |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 9 | |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h" |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 12 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
Teresa Johnson | 94624ac | 2017-05-10 18:52:16 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/TypeMetadataUtils.h" |
| 15 | #include "llvm/Bitcode/BitcodeWriter.h" |
| 16 | #include "llvm/IR/Constants.h" |
Peter Collingbourne | 28ffd32 | 2017-02-08 20:44:00 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DebugInfo.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Intrinsics.h" |
| 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/IR/PassManager.h" |
| 21 | #include "llvm/Pass.h" |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ScopedPrinter.h" |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/Transforms/IPO.h" |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/IPO/FunctionAttrs.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/Cloning.h" |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/ModuleUtils.h" |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 33 | // Promote each local-linkage entity defined by ExportM and used by ImportM by |
| 34 | // changing visibility and appending the given ModuleId. |
Evgeniy Stepanov | 4d4ee93 | 2017-06-16 00:18:29 +0000 | [diff] [blame] | 35 | void promoteInternals(Module &ExportM, Module &ImportM, StringRef ModuleId, |
| 36 | SetVector<GlobalValue *> &PromoteExtra) { |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 37 | DenseMap<const Comdat *, Comdat *> RenamedComdats; |
Peter Collingbourne | 6b19396 | 2017-03-30 23:43:08 +0000 | [diff] [blame] | 38 | for (auto &ExportGV : ExportM.global_values()) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 39 | if (!ExportGV.hasLocalLinkage()) |
Peter Collingbourne | 6b19396 | 2017-03-30 23:43:08 +0000 | [diff] [blame] | 40 | continue; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 41 | |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 42 | auto Name = ExportGV.getName(); |
| 43 | GlobalValue *ImportGV = ImportM.getNamedValue(Name); |
Evgeniy Stepanov | 4d4ee93 | 2017-06-16 00:18:29 +0000 | [diff] [blame] | 44 | if ((!ImportGV || ImportGV->use_empty()) && !PromoteExtra.count(&ExportGV)) |
Peter Collingbourne | 6b19396 | 2017-03-30 23:43:08 +0000 | [diff] [blame] | 45 | continue; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 46 | |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 47 | std::string NewName = (Name + ModuleId).str(); |
| 48 | |
| 49 | if (const auto *C = ExportGV.getComdat()) |
| 50 | if (C->getName() == Name) |
| 51 | RenamedComdats.try_emplace(C, ExportM.getOrInsertComdat(NewName)); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 52 | |
| 53 | ExportGV.setName(NewName); |
| 54 | ExportGV.setLinkage(GlobalValue::ExternalLinkage); |
| 55 | ExportGV.setVisibility(GlobalValue::HiddenVisibility); |
| 56 | |
Evgeniy Stepanov | 4d4ee93 | 2017-06-16 00:18:29 +0000 | [diff] [blame] | 57 | if (ImportGV) { |
| 58 | ImportGV->setName(NewName); |
| 59 | ImportGV->setVisibility(GlobalValue::HiddenVisibility); |
| 60 | } |
Peter Collingbourne | 6b19396 | 2017-03-30 23:43:08 +0000 | [diff] [blame] | 61 | } |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 62 | |
| 63 | if (!RenamedComdats.empty()) |
| 64 | for (auto &GO : ExportM.global_objects()) |
| 65 | if (auto *C = GO.getComdat()) { |
| 66 | auto Replacement = RenamedComdats.find(C); |
| 67 | if (Replacement != RenamedComdats.end()) |
| 68 | GO.setComdat(Replacement->second); |
| 69 | } |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Promote all internal (i.e. distinct) type ids used by the module by replacing |
| 73 | // them with external type ids formed using the module id. |
| 74 | // |
| 75 | // Note that this needs to be done before we clone the module because each clone |
| 76 | // will receive its own set of distinct metadata nodes. |
| 77 | void promoteTypeIds(Module &M, StringRef ModuleId) { |
| 78 | DenseMap<Metadata *, Metadata *> LocalToGlobal; |
| 79 | auto ExternalizeTypeId = [&](CallInst *CI, unsigned ArgNo) { |
| 80 | Metadata *MD = |
| 81 | cast<MetadataAsValue>(CI->getArgOperand(ArgNo))->getMetadata(); |
| 82 | |
| 83 | if (isa<MDNode>(MD) && cast<MDNode>(MD)->isDistinct()) { |
| 84 | Metadata *&GlobalMD = LocalToGlobal[MD]; |
| 85 | if (!GlobalMD) { |
| 86 | std::string NewName = |
| 87 | (to_string(LocalToGlobal.size()) + ModuleId).str(); |
| 88 | GlobalMD = MDString::get(M.getContext(), NewName); |
| 89 | } |
| 90 | |
| 91 | CI->setArgOperand(ArgNo, |
| 92 | MetadataAsValue::get(M.getContext(), GlobalMD)); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | if (Function *TypeTestFunc = |
| 97 | M.getFunction(Intrinsic::getName(Intrinsic::type_test))) { |
| 98 | for (const Use &U : TypeTestFunc->uses()) { |
| 99 | auto CI = cast<CallInst>(U.getUser()); |
| 100 | ExternalizeTypeId(CI, 1); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (Function *TypeCheckedLoadFunc = |
| 105 | M.getFunction(Intrinsic::getName(Intrinsic::type_checked_load))) { |
| 106 | for (const Use &U : TypeCheckedLoadFunc->uses()) { |
| 107 | auto CI = cast<CallInst>(U.getUser()); |
| 108 | ExternalizeTypeId(CI, 2); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | for (GlobalObject &GO : M.global_objects()) { |
| 113 | SmallVector<MDNode *, 1> MDs; |
| 114 | GO.getMetadata(LLVMContext::MD_type, MDs); |
| 115 | |
| 116 | GO.eraseMetadata(LLVMContext::MD_type); |
| 117 | for (auto MD : MDs) { |
| 118 | auto I = LocalToGlobal.find(MD->getOperand(1)); |
| 119 | if (I == LocalToGlobal.end()) { |
| 120 | GO.addMetadata(LLVMContext::MD_type, *MD); |
| 121 | continue; |
| 122 | } |
| 123 | GO.addMetadata( |
| 124 | LLVMContext::MD_type, |
| 125 | *MDNode::get(M.getContext(), |
| 126 | ArrayRef<Metadata *>{MD->getOperand(0), I->second})); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Drop unused globals, and drop type information from function declarations. |
| 132 | // FIXME: If we made functions typeless then there would be no need to do this. |
| 133 | void simplifyExternals(Module &M) { |
| 134 | FunctionType *EmptyFT = |
| 135 | FunctionType::get(Type::getVoidTy(M.getContext()), false); |
| 136 | |
| 137 | for (auto I = M.begin(), E = M.end(); I != E;) { |
| 138 | Function &F = *I++; |
| 139 | if (F.isDeclaration() && F.use_empty()) { |
| 140 | F.eraseFromParent(); |
| 141 | continue; |
| 142 | } |
| 143 | |
Peter Collingbourne | 93fdaca | 2017-07-19 17:54:29 +0000 | [diff] [blame] | 144 | if (!F.isDeclaration() || F.getFunctionType() == EmptyFT || |
| 145 | // Changing the type of an intrinsic may invalidate the IR. |
| 146 | F.getName().startswith("llvm.")) |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 147 | continue; |
| 148 | |
| 149 | Function *NewF = |
| 150 | Function::Create(EmptyFT, GlobalValue::ExternalLinkage, "", &M); |
| 151 | NewF->setVisibility(F.getVisibility()); |
| 152 | NewF->takeName(&F); |
| 153 | F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType())); |
| 154 | F.eraseFromParent(); |
| 155 | } |
| 156 | |
| 157 | for (auto I = M.global_begin(), E = M.global_end(); I != E;) { |
| 158 | GlobalVariable &GV = *I++; |
| 159 | if (GV.isDeclaration() && GV.use_empty()) { |
| 160 | GV.eraseFromParent(); |
| 161 | continue; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void filterModule( |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 167 | Module *M, function_ref<bool(const GlobalValue *)> ShouldKeepDefinition) { |
Bob Haarman | 6de8134 | 2017-04-05 00:42:07 +0000 | [diff] [blame] | 168 | for (Module::alias_iterator I = M->alias_begin(), E = M->alias_end(); |
| 169 | I != E;) { |
| 170 | GlobalAlias *GA = &*I++; |
| 171 | if (ShouldKeepDefinition(GA)) |
| 172 | continue; |
| 173 | |
| 174 | GlobalObject *GO; |
| 175 | if (GA->getValueType()->isFunctionTy()) |
| 176 | GO = Function::Create(cast<FunctionType>(GA->getValueType()), |
| 177 | GlobalValue::ExternalLinkage, "", M); |
| 178 | else |
| 179 | GO = new GlobalVariable( |
| 180 | *M, GA->getValueType(), false, GlobalValue::ExternalLinkage, |
Serge Guelton | f4dc59b | 2017-05-11 08:53:00 +0000 | [diff] [blame] | 181 | nullptr, "", nullptr, |
Bob Haarman | 6de8134 | 2017-04-05 00:42:07 +0000 | [diff] [blame] | 182 | GA->getThreadLocalMode(), GA->getType()->getAddressSpace()); |
| 183 | GO->takeName(GA); |
| 184 | GA->replaceAllUsesWith(GO); |
| 185 | GA->eraseFromParent(); |
| 186 | } |
| 187 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 188 | for (Function &F : *M) { |
| 189 | if (ShouldKeepDefinition(&F)) |
| 190 | continue; |
| 191 | |
| 192 | F.deleteBody(); |
Peter Collingbourne | 20a0093 | 2017-01-18 20:03:02 +0000 | [diff] [blame] | 193 | F.setComdat(nullptr); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 194 | F.clearMetadata(); |
| 195 | } |
| 196 | |
| 197 | for (GlobalVariable &GV : M->globals()) { |
| 198 | if (ShouldKeepDefinition(&GV)) |
| 199 | continue; |
| 200 | |
| 201 | GV.setInitializer(nullptr); |
| 202 | GV.setLinkage(GlobalValue::ExternalLinkage); |
Peter Collingbourne | 20a0093 | 2017-01-18 20:03:02 +0000 | [diff] [blame] | 203 | GV.setComdat(nullptr); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 204 | GV.clearMetadata(); |
| 205 | } |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 208 | void forEachVirtualFunction(Constant *C, function_ref<void(Function *)> Fn) { |
| 209 | if (auto *F = dyn_cast<Function>(C)) |
| 210 | return Fn(F); |
Peter Collingbourne | 3baa72a | 2017-03-02 23:10:17 +0000 | [diff] [blame] | 211 | if (isa<GlobalValue>(C)) |
| 212 | return; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 213 | for (Value *Op : C->operands()) |
| 214 | forEachVirtualFunction(cast<Constant>(Op), Fn); |
| 215 | } |
| 216 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 217 | // If it's possible to split M into regular and thin LTO parts, do so and write |
| 218 | // a multi-module bitcode file with the two parts to OS. Otherwise, write only a |
| 219 | // regular LTO bitcode file to OS. |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 220 | void splitAndWriteThinLTOBitcode( |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 221 | raw_ostream &OS, raw_ostream *ThinLinkOS, |
| 222 | function_ref<AAResults &(Function &)> AARGetter, Module &M) { |
Evgeniy Stepanov | 964f466 | 2017-04-27 20:27:27 +0000 | [diff] [blame] | 223 | std::string ModuleId = getUniqueModuleId(&M); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 224 | if (ModuleId.empty()) { |
| 225 | // We couldn't generate a module ID for this module, just write it out as a |
| 226 | // regular LTO module. |
| 227 | WriteBitcodeToFile(&M, OS); |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 228 | if (ThinLinkOS) |
| 229 | // We don't have a ThinLTO part, but still write the module to the |
| 230 | // ThinLinkOS if requested so that the expected output file is produced. |
| 231 | WriteBitcodeToFile(&M, *ThinLinkOS); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | promoteTypeIds(M, ModuleId); |
| 236 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 237 | // Returns whether a global has attached type metadata. Such globals may |
| 238 | // participate in CFI or whole-program devirtualization, so they need to |
| 239 | // appear in the merged module instead of the thin LTO module. |
| 240 | auto HasTypeMetadata = [&](const GlobalObject *GO) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 241 | SmallVector<MDNode *, 1> MDs; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 242 | GO->getMetadata(LLVMContext::MD_type, MDs); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 243 | return !MDs.empty(); |
| 244 | }; |
| 245 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 246 | // Collect the set of virtual functions that are eligible for virtual constant |
| 247 | // propagation. Each eligible function must not access memory, must return |
| 248 | // an integer of width <=64 bits, must take at least one argument, must not |
| 249 | // use its first argument (assumed to be "this") and all arguments other than |
| 250 | // the first one must be of <=64 bit integer type. |
| 251 | // |
| 252 | // Note that we test whether this copy of the function is readnone, rather |
| 253 | // than testing function attributes, which must hold for any copy of the |
| 254 | // function, even a less optimized version substituted at link time. This is |
| 255 | // sound because the virtual constant propagation optimizations effectively |
| 256 | // inline all implementations of the virtual function into each call site, |
| 257 | // rather than using function attributes to perform local optimization. |
| 258 | std::set<const Function *> EligibleVirtualFns; |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 259 | // If any member of a comdat lives in MergedM, put all members of that |
| 260 | // comdat in MergedM to keep the comdat together. |
| 261 | DenseSet<const Comdat *> MergedMComdats; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 262 | for (GlobalVariable &GV : M.globals()) |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 263 | if (HasTypeMetadata(&GV)) { |
| 264 | if (const auto *C = GV.getComdat()) |
| 265 | MergedMComdats.insert(C); |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 266 | forEachVirtualFunction(GV.getInitializer(), [&](Function *F) { |
| 267 | auto *RT = dyn_cast<IntegerType>(F->getReturnType()); |
| 268 | if (!RT || RT->getBitWidth() > 64 || F->arg_empty() || |
| 269 | !F->arg_begin()->use_empty()) |
| 270 | return; |
| 271 | for (auto &Arg : make_range(std::next(F->arg_begin()), F->arg_end())) { |
| 272 | auto *ArgT = dyn_cast<IntegerType>(Arg.getType()); |
| 273 | if (!ArgT || ArgT->getBitWidth() > 64) |
| 274 | return; |
| 275 | } |
Chandler Carruth | 01f0c8a | 2017-07-11 05:39:20 +0000 | [diff] [blame] | 276 | if (!F->isDeclaration() && |
| 277 | computeFunctionBodyMemoryAccess(*F, AARGetter(*F)) == MAK_ReadNone) |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 278 | EligibleVirtualFns.insert(F); |
| 279 | }); |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 280 | } |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 281 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 282 | ValueToValueMapTy VMap; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 283 | std::unique_ptr<Module> MergedM( |
| 284 | CloneModule(&M, VMap, [&](const GlobalValue *GV) -> bool { |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 285 | if (const auto *C = GV->getComdat()) |
| 286 | if (MergedMComdats.count(C)) |
| 287 | return true; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 288 | if (auto *F = dyn_cast<Function>(GV)) |
| 289 | return EligibleVirtualFns.count(F); |
| 290 | if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject())) |
| 291 | return HasTypeMetadata(GVar); |
| 292 | return false; |
| 293 | })); |
Peter Collingbourne | 28ffd32 | 2017-02-08 20:44:00 +0000 | [diff] [blame] | 294 | StripDebugInfo(*MergedM); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 295 | |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 296 | for (Function &F : *MergedM) |
| 297 | if (!F.isDeclaration()) { |
| 298 | // Reset the linkage of all functions eligible for virtual constant |
| 299 | // propagation. The canonical definitions live in the thin LTO module so |
| 300 | // that they can be imported. |
| 301 | F.setLinkage(GlobalValue::AvailableExternallyLinkage); |
| 302 | F.setComdat(nullptr); |
| 303 | } |
| 304 | |
Evgeniy Stepanov | 4d4ee93 | 2017-06-16 00:18:29 +0000 | [diff] [blame] | 305 | SetVector<GlobalValue *> CfiFunctions; |
| 306 | for (auto &F : M) |
| 307 | if ((!F.hasLocalLinkage() || F.hasAddressTaken()) && HasTypeMetadata(&F)) |
| 308 | CfiFunctions.insert(&F); |
| 309 | |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 310 | // Remove all globals with type metadata, globals with comdats that live in |
| 311 | // MergedM, and aliases pointing to such globals from the thin LTO module. |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 312 | filterModule(&M, [&](const GlobalValue *GV) { |
| 313 | if (auto *GVar = dyn_cast_or_null<GlobalVariable>(GV->getBaseObject())) |
Bob Haarman | 4075ccc | 2017-04-12 01:43:07 +0000 | [diff] [blame] | 314 | if (HasTypeMetadata(GVar)) |
| 315 | return false; |
| 316 | if (const auto *C = GV->getComdat()) |
| 317 | if (MergedMComdats.count(C)) |
| 318 | return false; |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 319 | return true; |
| 320 | }); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 321 | |
Evgeniy Stepanov | 4d4ee93 | 2017-06-16 00:18:29 +0000 | [diff] [blame] | 322 | promoteInternals(*MergedM, M, ModuleId, CfiFunctions); |
| 323 | promoteInternals(M, *MergedM, ModuleId, CfiFunctions); |
| 324 | |
| 325 | SmallVector<MDNode *, 8> CfiFunctionMDs; |
| 326 | for (auto V : CfiFunctions) { |
| 327 | Function &F = *cast<Function>(V); |
| 328 | SmallVector<MDNode *, 2> Types; |
| 329 | F.getMetadata(LLVMContext::MD_type, Types); |
| 330 | |
| 331 | auto &Ctx = MergedM->getContext(); |
| 332 | SmallVector<Metadata *, 4> Elts; |
| 333 | Elts.push_back(MDString::get(Ctx, F.getName())); |
| 334 | CfiFunctionLinkage Linkage; |
| 335 | if (!F.isDeclarationForLinker()) |
| 336 | Linkage = CFL_Definition; |
| 337 | else if (F.isWeakForLinker()) |
| 338 | Linkage = CFL_WeakDeclaration; |
| 339 | else |
| 340 | Linkage = CFL_Declaration; |
| 341 | Elts.push_back(ConstantAsMetadata::get( |
| 342 | llvm::ConstantInt::get(Type::getInt8Ty(Ctx), Linkage))); |
| 343 | for (auto Type : Types) |
| 344 | Elts.push_back(Type); |
| 345 | CfiFunctionMDs.push_back(MDTuple::get(Ctx, Elts)); |
| 346 | } |
| 347 | |
| 348 | if(!CfiFunctionMDs.empty()) { |
| 349 | NamedMDNode *NMD = MergedM->getOrInsertNamedMetadata("cfi.functions"); |
| 350 | for (auto MD : CfiFunctionMDs) |
| 351 | NMD->addOperand(MD); |
| 352 | } |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 353 | |
| 354 | simplifyExternals(*MergedM); |
| 355 | |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 356 | // FIXME: Try to re-use BSI and PFI from the original module here. |
Teresa Johnson | 94624ac | 2017-05-10 18:52:16 +0000 | [diff] [blame] | 357 | ProfileSummaryInfo PSI(M); |
| 358 | ModuleSummaryIndex Index = buildModuleSummaryIndex(M, nullptr, &PSI); |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 359 | |
Peter Collingbourne | e357fbd | 2017-06-08 23:01:49 +0000 | [diff] [blame] | 360 | // Mark the merged module as requiring full LTO. We still want an index for |
| 361 | // it though, so that it can participate in summary-based dead stripping. |
| 362 | MergedM->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0)); |
| 363 | ModuleSummaryIndex MergedMIndex = |
| 364 | buildModuleSummaryIndex(*MergedM, nullptr, &PSI); |
| 365 | |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 366 | SmallVector<char, 0> Buffer; |
| 367 | |
| 368 | BitcodeWriter W(Buffer); |
| 369 | // Save the module hash produced for the full bitcode, which will |
| 370 | // be used in the backends, and use that in the minimized bitcode |
| 371 | // produced for the full link. |
| 372 | ModuleHash ModHash = {{0}}; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 373 | W.writeModule(&M, /*ShouldPreserveUseListOrder=*/false, &Index, |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 374 | /*GenerateHash=*/true, &ModHash); |
Peter Collingbourne | e357fbd | 2017-06-08 23:01:49 +0000 | [diff] [blame] | 375 | W.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false, |
| 376 | &MergedMIndex); |
Peter Collingbourne | 92648c2 | 2017-06-27 23:50:11 +0000 | [diff] [blame] | 377 | W.writeSymtab(); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 378 | W.writeStrtab(); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 379 | OS << Buffer; |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 380 | |
Haojie Wang | 1dec57d | 2017-07-21 17:25:20 +0000 | [diff] [blame] | 381 | // If a minimized bitcode module was requested for the thin link, only |
| 382 | // the information that is needed by thin link will be written in the |
| 383 | // given OS (the merged module will be written as usual). |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 384 | if (ThinLinkOS) { |
| 385 | Buffer.clear(); |
| 386 | BitcodeWriter W2(Buffer); |
| 387 | StripDebugInfo(M); |
Haojie Wang | 1dec57d | 2017-07-21 17:25:20 +0000 | [diff] [blame] | 388 | W2.writeThinLinkBitcode(&M, Index, ModHash); |
Peter Collingbourne | e357fbd | 2017-06-08 23:01:49 +0000 | [diff] [blame] | 389 | W2.writeModule(MergedM.get(), /*ShouldPreserveUseListOrder=*/false, |
| 390 | &MergedMIndex); |
Peter Collingbourne | 92648c2 | 2017-06-27 23:50:11 +0000 | [diff] [blame] | 391 | W2.writeSymtab(); |
Peter Collingbourne | a0f371a | 2017-04-17 17:51:36 +0000 | [diff] [blame] | 392 | W2.writeStrtab(); |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 393 | *ThinLinkOS << Buffer; |
| 394 | } |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | // Returns whether this module needs to be split because it uses type metadata. |
| 398 | bool requiresSplit(Module &M) { |
| 399 | SmallVector<MDNode *, 1> MDs; |
| 400 | for (auto &GO : M.global_objects()) { |
| 401 | GO.getMetadata(LLVMContext::MD_type, MDs); |
| 402 | if (!MDs.empty()) |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | return false; |
| 407 | } |
| 408 | |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 409 | void writeThinLTOBitcode(raw_ostream &OS, raw_ostream *ThinLinkOS, |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 410 | function_ref<AAResults &(Function &)> AARGetter, |
| 411 | Module &M, const ModuleSummaryIndex *Index) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 412 | // See if this module has any type metadata. If so, we need to split it. |
| 413 | if (requiresSplit(M)) |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 414 | return splitAndWriteThinLTOBitcode(OS, ThinLinkOS, AARGetter, M); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 415 | |
| 416 | // Otherwise we can just write it out as a regular module. |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 417 | |
| 418 | // Save the module hash produced for the full bitcode, which will |
| 419 | // be used in the backends, and use that in the minimized bitcode |
| 420 | // produced for the full link. |
| 421 | ModuleHash ModHash = {{0}}; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 422 | WriteBitcodeToFile(&M, OS, /*ShouldPreserveUseListOrder=*/false, Index, |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 423 | /*GenerateHash=*/true, &ModHash); |
Haojie Wang | 1dec57d | 2017-07-21 17:25:20 +0000 | [diff] [blame] | 424 | // If a minimized bitcode module was requested for the thin link, only |
| 425 | // the information that is needed by thin link will be written in the |
| 426 | // given OS. |
| 427 | if (ThinLinkOS && Index) |
| 428 | WriteThinLinkBitcodeToFile(&M, *ThinLinkOS, *Index, ModHash); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | class WriteThinLTOBitcode : public ModulePass { |
| 432 | raw_ostream &OS; // raw_ostream to print on |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 433 | // The output stream on which to emit a minimized module for use |
| 434 | // just in the thin link, if requested. |
| 435 | raw_ostream *ThinLinkOS; |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 436 | |
| 437 | public: |
| 438 | static char ID; // Pass identification, replacement for typeid |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 439 | WriteThinLTOBitcode() : ModulePass(ID), OS(dbgs()), ThinLinkOS(nullptr) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 440 | initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry()); |
| 441 | } |
| 442 | |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 443 | explicit WriteThinLTOBitcode(raw_ostream &o, raw_ostream *ThinLinkOS) |
| 444 | : ModulePass(ID), OS(o), ThinLinkOS(ThinLinkOS) { |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 445 | initializeWriteThinLTOBitcodePass(*PassRegistry::getPassRegistry()); |
| 446 | } |
| 447 | |
| 448 | StringRef getPassName() const override { return "ThinLTO Bitcode Writer"; } |
| 449 | |
| 450 | bool runOnModule(Module &M) override { |
| 451 | const ModuleSummaryIndex *Index = |
| 452 | &(getAnalysis<ModuleSummaryIndexWrapperPass>().getIndex()); |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 453 | writeThinLTOBitcode(OS, ThinLinkOS, LegacyAARGetter(*this), M, Index); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 454 | return true; |
| 455 | } |
| 456 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 457 | AU.setPreservesAll(); |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 458 | AU.addRequired<AssumptionCacheTracker>(); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 459 | AU.addRequired<ModuleSummaryIndexWrapperPass>(); |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 460 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 461 | } |
| 462 | }; |
| 463 | } // anonymous namespace |
| 464 | |
| 465 | char WriteThinLTOBitcode::ID = 0; |
| 466 | INITIALIZE_PASS_BEGIN(WriteThinLTOBitcode, "write-thinlto-bitcode", |
| 467 | "Write ThinLTO Bitcode", false, true) |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 468 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 469 | INITIALIZE_PASS_DEPENDENCY(ModuleSummaryIndexWrapperPass) |
Peter Collingbourne | 002c2d5 | 2017-02-14 03:42:38 +0000 | [diff] [blame] | 470 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 471 | INITIALIZE_PASS_END(WriteThinLTOBitcode, "write-thinlto-bitcode", |
| 472 | "Write ThinLTO Bitcode", false, true) |
| 473 | |
Teresa Johnson | 0c6a4ff | 2017-03-23 19:47:39 +0000 | [diff] [blame] | 474 | ModulePass *llvm::createWriteThinLTOBitcodePass(raw_ostream &Str, |
| 475 | raw_ostream *ThinLinkOS) { |
| 476 | return new WriteThinLTOBitcode(Str, ThinLinkOS); |
Peter Collingbourne | 1398a32 | 2016-12-16 00:26:30 +0000 | [diff] [blame] | 477 | } |
Tim Shen | 6b411418 | 2017-06-01 01:02:12 +0000 | [diff] [blame] | 478 | |
| 479 | PreservedAnalyses |
| 480 | llvm::ThinLTOBitcodeWriterPass::run(Module &M, ModuleAnalysisManager &AM) { |
| 481 | FunctionAnalysisManager &FAM = |
| 482 | AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 483 | writeThinLTOBitcode(OS, ThinLinkOS, |
| 484 | [&FAM](Function &F) -> AAResults & { |
| 485 | return FAM.getResult<AAManager>(F); |
| 486 | }, |
| 487 | M, &AM.getResult<ModuleSummaryIndexAnalysis>(M)); |
| 488 | return PreservedAnalyses::all(); |
| 489 | } |