aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/NaCl/StripMetadata.cpp
diff options
context:
space:
mode:
authorJan Voung <jvoung@chromium.org>2013-03-28 16:56:41 -0700
committerJan Voung <jvoung@chromium.org>2013-03-28 16:56:41 -0700
commit946417b9fe7da9334c76182f28020ff4f46e11f8 (patch)
tree7fb6f266cec32158396e074aba7195848f652cd2 /lib/Transforms/NaCl/StripMetadata.cpp
parent37a28705d4dfdda8ecbb9aca9bc10a6fd6d80582 (diff)
Add a pass to strip bitcode metadata.
This only works on instruction attachments for now. Since it is a ModulePass we can add something to strip NamedMetadata based on a whitelist, if we want to retain some of that. It does not touch debug metadata, and leaves -strip-debug to handle that. BUG= https://code.google.com/p/nativeclient/issues/detail?id=3348 Review URL: https://codereview.chromium.org/12844027
Diffstat (limited to 'lib/Transforms/NaCl/StripMetadata.cpp')
-rw-r--r--lib/Transforms/NaCl/StripMetadata.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/Transforms/NaCl/StripMetadata.cpp b/lib/Transforms/NaCl/StripMetadata.cpp
new file mode 100644
index 0000000000..a7d2e7eb68
--- /dev/null
+++ b/lib/Transforms/NaCl/StripMetadata.cpp
@@ -0,0 +1,77 @@
+//===- StripMetadata.cpp - Strip non-stable non-debug metadata ------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// The StripMetadata transformation strips instruction attachment
+// metadata, such as !tbaa and !prof metadata.
+// TODO: Strip NamedMetadata too.
+//
+// It does not strip debug metadata. Debug metadata is used by debug
+// intrinsic functions and calls to those intrinsic functions. Use the
+// -strip-debug or -strip pass to strip that instead.
+//
+// The goal of this pass is to reduce bitcode ABI surface area.
+// We don't know yet which kind of metadata is considered stable.
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Pass.h"
+#include "llvm/Transforms/NaCl.h"
+
+using namespace llvm;
+
+namespace {
+ class StripMetadata : public ModulePass {
+ public:
+ static char ID;
+ explicit StripMetadata() : ModulePass(ID) {
+ initializeStripMetadataPass(*PassRegistry::getPassRegistry());
+ }
+
+ virtual bool runOnModule(Module &M);
+
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.setPreservesCFG();
+ }
+ };
+}
+
+char StripMetadata::ID = 0;
+INITIALIZE_PASS(StripMetadata, "strip-metadata",
+ "Strip all non-stable non-debug metadata from a module.",
+ false, false)
+
+ModulePass *llvm::createStripMetadataPass() {
+ return new StripMetadata();
+}
+
+static bool DoStripMetadata(Module &M) {
+ bool Changed = false;
+
+ for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) {
+ for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE; ++FI) {
+ for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
+ ++BI) {
+ SmallVector<std::pair<unsigned, MDNode *>, 8> InstMeta;
+ // Let the debug metadata be stripped by the -strip-debug pass.
+ BI->getAllMetadataOtherThanDebugLoc(InstMeta);
+ for (size_t i = 0; i < InstMeta.size(); ++i) {
+ BI->setMetadata(InstMeta[i].first, NULL);
+ Changed = true;
+ }
+ }
+ }
+ }
+
+ return Changed;
+}
+
+bool StripMetadata::runOnModule(Module &M) {
+ return DoStripMetadata(M);
+}