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