OLD | NEW |
(Empty) | |
| 1 //===- StripMetadata.cpp - Strip non-stable non-debug metadata ------===// |
| 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 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // The StripMetadata transformation strips instruction attachment |
| 11 // metadata, such as !tbaa and !prof metadata. |
| 12 // TODO: Strip NamedMetadata too. |
| 13 // |
| 14 // It does not strip debug metadata. Debug metadata is used by debug |
| 15 // intrinsic functions and calls to those intrinsic functions. Use the |
| 16 // -strip-debug or -strip pass to strip that instead. |
| 17 // |
| 18 // The goal of this pass is to reduce bitcode ABI surface area. |
| 19 // We don't know yet which kind of metadata is considered stable. |
| 20 //===----------------------------------------------------------------------===// |
| 21 |
| 22 #include "llvm/IR/Instructions.h" |
| 23 #include "llvm/IR/Module.h" |
| 24 #include "llvm/Pass.h" |
| 25 #include "llvm/Transforms/NaCl.h" |
| 26 |
| 27 using namespace llvm; |
| 28 |
| 29 namespace { |
| 30 class StripMetadata : public ModulePass { |
| 31 public: |
| 32 static char ID; |
| 33 StripMetadata() : ModulePass(ID), ShouldStripModuleFlags(false) { |
| 34 initializeStripMetadataPass(*PassRegistry::getPassRegistry()); |
| 35 } |
| 36 |
| 37 virtual bool runOnModule(Module &M); |
| 38 |
| 39 virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 40 AU.setPreservesCFG(); |
| 41 } |
| 42 |
| 43 protected: |
| 44 bool ShouldStripModuleFlags; |
| 45 }; |
| 46 |
| 47 class StripModuleFlags : public StripMetadata { |
| 48 public: |
| 49 static char ID; |
| 50 StripModuleFlags() : StripMetadata() { |
| 51 initializeStripModuleFlagsPass(*PassRegistry::getPassRegistry()); |
| 52 ShouldStripModuleFlags = true; |
| 53 } |
| 54 }; |
| 55 } |
| 56 |
| 57 char StripMetadata::ID = 0; |
| 58 INITIALIZE_PASS(StripMetadata, "strip-metadata", |
| 59 "Strip all non-stable non-debug metadata from a module.", |
| 60 false, false) |
| 61 |
| 62 char StripModuleFlags::ID = 0; |
| 63 INITIALIZE_PASS(StripModuleFlags, "strip-module-flags", |
| 64 "Strip all non-stable non-debug metadata from a module, " |
| 65 "including the llvm.module.flags metadata.", |
| 66 false, false) |
| 67 |
| 68 ModulePass *llvm::createStripMetadataPass() { |
| 69 return new StripMetadata(); |
| 70 } |
| 71 |
| 72 ModulePass *llvm::createStripModuleFlagsPass() { |
| 73 return new StripModuleFlags(); |
| 74 } |
| 75 |
| 76 static bool IsWhitelistedMetadata(const NamedMDNode *node, |
| 77 bool StripModuleFlags) { |
| 78 // Leave debug metadata to the -strip-debug pass. |
| 79 return (node->getName().startswith("llvm.dbg.") || |
| 80 // "Debug Info Version" is in llvm.module.flags. |
| 81 (!StripModuleFlags && node->getName().equals("llvm.module.flags"))); |
| 82 } |
| 83 |
| 84 static bool DoStripMetadata(Module &M, bool StripModuleFlags) { |
| 85 bool Changed = false; |
| 86 |
| 87 if (!StripModuleFlags) |
| 88 for (Function &F : M) |
| 89 for (BasicBlock &B : F) |
| 90 for (Instruction &I : B) { |
| 91 SmallVector<std::pair<unsigned, MDNode *>, 8> InstMeta; |
| 92 // Let the debug metadata be stripped by the -strip-debug pass. |
| 93 I.getAllMetadataOtherThanDebugLoc(InstMeta); |
| 94 for (size_t i = 0; i < InstMeta.size(); ++i) { |
| 95 I.setMetadata(InstMeta[i].first, NULL); |
| 96 Changed = true; |
| 97 } |
| 98 } |
| 99 |
| 100 // Strip unsupported named metadata. |
| 101 SmallVector<NamedMDNode*, 8> ToErase; |
| 102 for (Module::NamedMDListType::iterator I = M.named_metadata_begin(), |
| 103 E = M.named_metadata_end(); I != E; ++I) { |
| 104 if (!IsWhitelistedMetadata(I, StripModuleFlags)) |
| 105 ToErase.push_back(I); |
| 106 } |
| 107 for (size_t i = 0; i < ToErase.size(); ++i) |
| 108 M.eraseNamedMetadata(ToErase[i]); |
| 109 |
| 110 return Changed; |
| 111 } |
| 112 |
| 113 bool StripMetadata::runOnModule(Module &M) { |
| 114 return DoStripMetadata(M, ShouldStripModuleFlags); |
| 115 } |
OLD | NEW |