comparison gen/passes/StripMetaData.cpp @ 1287:6c8af78364f5

There's an issue with LLVM metadata support; it triggers an assert when trying to generate asm for code with metadata globals. This new pass is used as a workaround: it strips metadata from the module before it reaches the code generator. Obviously, this is disabled if LLVM doesn't support metadata.
author Frits van Bommel <fvbommel wxs.nl>
date Sat, 02 May 2009 12:19:43 +0200
parents
children c21a6654cce2
comparison
equal deleted inserted replaced
1286:23b23b74e326 1287:6c8af78364f5
1 //===- StripMetaData - Strips D-specific metadata -------------------------===//
2 //
3 // The LLVM D Compiler
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // There's an issue with the new LLVM metadata support; an assertion fires when
11 // trying to generate asm for metadata globals.
12 //
13 // This pass is a workaround; it deletes the metadata LDC generates so the code
14 // generator doesn't see it.
15 // Obviously, this should only run after all passes that make use of that
16 // metadata or they won't work.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #include "gen/metadata.h"
21
22 // This pass isn't needed without metadata, so #ifdef it out entirely if the
23 // LLVM version in use doesn't support it.
24 #ifdef USE_METADATA
25
26
27 #define DEBUG_TYPE "strip-metadata"
28
29 #include "Passes.h"
30
31 #include "llvm/Pass.h"
32 #include "llvm/Module.h"
33 #include "llvm/Constants.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/Debug.h"
37 using namespace llvm;
38
39 STATISTIC(NumDeleted, "Number of metadata globals deleted");
40
41 //===----------------------------------------------------------------------===//
42 // StripMetaData Pass Implementation
43 //===----------------------------------------------------------------------===//
44
45 namespace {
46 /// This pass optimizes library functions from the D runtime as used by LDC.
47 ///
48 class VISIBILITY_HIDDEN StripMetaData : public ModulePass {
49 public:
50 static char ID; // Pass identification
51 StripMetaData() : ModulePass(&ID) {}
52
53 bool runOnModule(Module &M);
54 };
55 char StripMetaData::ID = 0;
56 } // end anonymous namespace.
57
58 static RegisterPass<StripMetaData>
59 X("strip-metadata", "Delete D-specific metadata");
60
61 // Public interface to the pass.
62 ModulePass *createStripMetaData() {
63 return new StripMetaData();
64 }
65
66 /// runOnFunction - Top level algorithm.
67 ///
68 bool StripMetaData::runOnModule(Module &M) {
69 bool Changed = false;
70 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E;) {
71 GlobalVariable* G = I++;
72 if (G->getNameLen() >= 9 && !strncmp(G->getNameStart(), "llvm.ldc.", 9)) {
73 assert(G->hasInitializer() && isa<MDNode>(G->getInitializer())
74 && "Not a metadata global?");
75 Changed = true;
76 NumDeleted++;
77 DEBUG(DOUT << "Deleting " << *G << '\n');
78 G->eraseFromParent();
79 }
80 }
81 return Changed;
82 }
83
84
85 #endif //USE_METADATA