Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 1 | //===-- GlobalMerge.cpp - Internal globals merging -----------------------===// |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 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 | // This pass merges globals with internal linkage into one. This way all the |
| 10 | // globals which were merged into a biggest one can be addressed using offsets |
| 11 | // from the same base pointer (no need for separate base pointer for each of the |
| 12 | // global). Such a transformation can significantly reduce the register pressure |
| 13 | // when many globals are involved. |
| 14 | // |
Nadav Rotem | a94d6e8 | 2012-07-24 10:51:42 +0000 | [diff] [blame] | 15 | // For example, consider the code which touches several global variables at |
Eric Christopher | a99c3e9 | 2010-09-28 04:18:29 +0000 | [diff] [blame] | 16 | // once: |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 17 | // |
| 18 | // static int foo[N], bar[N], baz[N]; |
| 19 | // |
| 20 | // for (i = 0; i < N; ++i) { |
| 21 | // foo[i] = bar[i] * baz[i]; |
| 22 | // } |
| 23 | // |
| 24 | // On ARM the addresses of 3 arrays should be kept in the registers, thus |
| 25 | // this code has quite large register pressure (loop body): |
| 26 | // |
| 27 | // ldr r1, [r5], #4 |
| 28 | // ldr r2, [r6], #4 |
| 29 | // mul r1, r2, r1 |
| 30 | // str r1, [r0], #4 |
| 31 | // |
| 32 | // Pass converts the code to something like: |
| 33 | // |
| 34 | // static struct { |
| 35 | // int foo[N]; |
| 36 | // int bar[N]; |
| 37 | // int baz[N]; |
| 38 | // } merged; |
| 39 | // |
| 40 | // for (i = 0; i < N; ++i) { |
| 41 | // merged.foo[i] = merged.bar[i] * merged.baz[i]; |
| 42 | // } |
| 43 | // |
| 44 | // and in ARM code this becomes: |
| 45 | // |
| 46 | // ldr r0, [r5, #40] |
| 47 | // ldr r1, [r5, #80] |
| 48 | // mul r0, r1, r0 |
| 49 | // str r0, [r5], #4 |
| 50 | // |
| 51 | // note that we saved 2 registers here almostly "for free". |
Eric Christopher | a99c3e9 | 2010-09-28 04:18:29 +0000 | [diff] [blame] | 52 | // ===---------------------------------------------------------------------===// |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 53 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 54 | #define DEBUG_TYPE "global-merge" |
| 55 | #include "llvm/Transforms/Scalar.h" |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 58 | #include "llvm/IR/Attributes.h" |
| 59 | #include "llvm/IR/Constants.h" |
| 60 | #include "llvm/IR/DataLayout.h" |
| 61 | #include "llvm/IR/DerivedTypes.h" |
| 62 | #include "llvm/IR/Function.h" |
| 63 | #include "llvm/IR/GlobalVariable.h" |
| 64 | #include "llvm/IR/Instructions.h" |
| 65 | #include "llvm/IR/Intrinsics.h" |
| 66 | #include "llvm/IR/Module.h" |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 67 | #include "llvm/Pass.h" |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 68 | #include "llvm/Support/CommandLine.h" |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 69 | #include "llvm/Target/TargetLowering.h" |
Bob Wilson | 0564609 | 2010-11-17 21:25:39 +0000 | [diff] [blame] | 70 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 71 | using namespace llvm; |
| 72 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 73 | cl::opt<bool> |
| 74 | EnableGlobalMerge("global-merge", cl::Hidden, |
| 75 | cl::desc("Enable global merge pass"), |
| 76 | cl::init(true)); |
| 77 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 78 | static cl::opt<bool> |
| 79 | EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden, |
Jakub Staszak | dca13e0 | 2013-07-22 21:11:30 +0000 | [diff] [blame] | 80 | cl::desc("Enable global merge pass on constants"), |
| 81 | cl::init(false)); |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 82 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 83 | STATISTIC(NumMerged , "Number of globals merged"); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 84 | namespace { |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 85 | class GlobalMerge : public FunctionPass { |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 86 | const TargetMachine *TM; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 87 | |
| 88 | bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals, |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 89 | Module &M, bool isConst, unsigned AddrSpace) const; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 90 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 91 | /// \brief Check if the given variable has been identified as must keep |
| 92 | /// \pre setMustKeepGlobalVariables must have been called on the Module that |
| 93 | /// contains GV |
| 94 | bool isMustKeepGlobalVariable(const GlobalVariable *GV) const { |
| 95 | return MustKeepGlobalVariables.count(GV); |
| 96 | } |
| 97 | |
| 98 | /// Collect every variables marked as "used" or used in a landing pad |
| 99 | /// instruction for this Module. |
| 100 | void setMustKeepGlobalVariables(Module &M); |
| 101 | |
| 102 | /// Collect every variables marked as "used" |
| 103 | void collectUsedGlobalVariables(Module &M); |
| 104 | |
Quentin Colombet | 9deb917 | 2013-03-19 21:46:49 +0000 | [diff] [blame] | 105 | /// Keep track of the GlobalVariable that must not be merged away |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 106 | SmallPtrSet<const GlobalVariable *, 16> MustKeepGlobalVariables; |
| 107 | |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 108 | public: |
| 109 | static char ID; // Pass identification, replacement for typeid. |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 110 | explicit GlobalMerge(const TargetMachine *TM = 0) |
| 111 | : FunctionPass(ID), TM(TM) { |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 112 | initializeGlobalMergePass(*PassRegistry::getPassRegistry()); |
| 113 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 114 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 115 | bool doInitialization(Module &M) override; |
| 116 | bool runOnFunction(Function &F) override; |
| 117 | bool doFinalization(Module &M) override; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 118 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 119 | const char *getPassName() const override { |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 120 | return "Merge internal globals"; |
| 121 | } |
| 122 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 123 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 124 | AU.setPreservesCFG(); |
| 125 | FunctionPass::getAnalysisUsage(AU); |
| 126 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 127 | }; |
| 128 | } // end anonymous namespace |
| 129 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 130 | char GlobalMerge::ID = 0; |
| 131 | INITIALIZE_PASS(GlobalMerge, "global-merge", |
| 132 | "Global Merge", false, false) |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 133 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 134 | |
| 135 | bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals, |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 136 | Module &M, bool isConst, unsigned AddrSpace) const { |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 137 | const TargetLowering *TLI = TM->getTargetLowering(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 138 | const DataLayout *DL = TLI->getDataLayout(); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 139 | |
| 140 | // FIXME: Infer the maximum possible offset depending on the actual users |
| 141 | // (these max offsets are different for the users inside Thumb or ARM |
| 142 | // functions) |
| 143 | unsigned MaxOffset = TLI->getMaximalGlobalOffset(); |
| 144 | |
| 145 | // FIXME: Find better heuristics |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 146 | std::stable_sort(Globals.begin(), Globals.end(), |
| 147 | [DL](const GlobalVariable *GV1, const GlobalVariable *GV2) { |
| 148 | Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType(); |
| 149 | Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType(); |
| 150 | |
| 151 | return (DL->getTypeAllocSize(Ty1) < DL->getTypeAllocSize(Ty2)); |
| 152 | }); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 153 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 154 | Type *Int32Ty = Type::getInt32Ty(M.getContext()); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 155 | |
| 156 | for (size_t i = 0, e = Globals.size(); i != e; ) { |
| 157 | size_t j = 0; |
| 158 | uint64_t MergedSize = 0; |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 159 | std::vector<Type*> Tys; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 160 | std::vector<Constant*> Inits; |
Bob Wilson | 619a372 | 2010-11-17 21:25:36 +0000 | [diff] [blame] | 161 | for (j = i; j != e; ++j) { |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 162 | Type *Ty = Globals[j]->getType()->getElementType(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 163 | MergedSize += DL->getTypeAllocSize(Ty); |
Bob Wilson | 619a372 | 2010-11-17 21:25:36 +0000 | [diff] [blame] | 164 | if (MergedSize > MaxOffset) { |
| 165 | break; |
| 166 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 167 | Tys.push_back(Ty); |
| 168 | Inits.push_back(Globals[j]->getInitializer()); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | 252b491 | 2010-09-05 21:18:45 +0000 | [diff] [blame] | 171 | StructType *MergedTy = StructType::get(M.getContext(), Tys); |
| 172 | Constant *MergedInit = ConstantStruct::get(MergedTy, Inits); |
| 173 | GlobalVariable *MergedGV = new GlobalVariable(M, MergedTy, isConst, |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 174 | GlobalValue::InternalLinkage, |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 175 | MergedInit, "_MergedGlobals", |
| 176 | 0, GlobalVariable::NotThreadLocal, |
| 177 | AddrSpace); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 178 | for (size_t k = i; k < j; ++k) { |
Chris Lattner | 252b491 | 2010-09-05 21:18:45 +0000 | [diff] [blame] | 179 | Constant *Idx[2] = { |
| 180 | ConstantInt::get(Int32Ty, 0), |
| 181 | ConstantInt::get(Int32Ty, k-i) |
| 182 | }; |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 183 | Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 184 | Globals[k]->replaceAllUsesWith(GEP); |
| 185 | Globals[k]->eraseFromParent(); |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 186 | NumMerged++; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 187 | } |
| 188 | i = j; |
| 189 | } |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 194 | void GlobalMerge::collectUsedGlobalVariables(Module &M) { |
| 195 | // Extract global variables from llvm.used array |
| 196 | const GlobalVariable *GV = M.getGlobalVariable("llvm.used"); |
| 197 | if (!GV || !GV->hasInitializer()) return; |
| 198 | |
| 199 | // Should be an array of 'i8*'. |
Rafael Espindola | cde25b4 | 2013-04-22 14:58:02 +0000 | [diff] [blame] | 200 | const ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer()); |
| 201 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 202 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 203 | if (const GlobalVariable *G = |
| 204 | dyn_cast<GlobalVariable>(InitList->getOperand(i)->stripPointerCasts())) |
| 205 | MustKeepGlobalVariables.insert(G); |
| 206 | } |
| 207 | |
| 208 | void GlobalMerge::setMustKeepGlobalVariables(Module &M) { |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 209 | collectUsedGlobalVariables(M); |
| 210 | |
| 211 | for (Module::iterator IFn = M.begin(), IEndFn = M.end(); IFn != IEndFn; |
| 212 | ++IFn) { |
| 213 | for (Function::iterator IBB = IFn->begin(), IEndBB = IFn->end(); |
| 214 | IBB != IEndBB; ++IBB) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 215 | // Follow the invoke link to find the landing pad instruction |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 216 | const InvokeInst *II = dyn_cast<InvokeInst>(IBB->getTerminator()); |
| 217 | if (!II) continue; |
| 218 | |
| 219 | const LandingPadInst *LPInst = II->getUnwindDest()->getLandingPadInst(); |
| 220 | // Look for globals in the clauses of the landing pad instruction |
| 221 | for (unsigned Idx = 0, NumClauses = LPInst->getNumClauses(); |
| 222 | Idx != NumClauses; ++Idx) |
| 223 | if (const GlobalVariable *GV = |
| 224 | dyn_cast<GlobalVariable>(LPInst->getClause(Idx) |
| 225 | ->stripPointerCasts())) |
| 226 | MustKeepGlobalVariables.insert(GV); |
| 227 | } |
| 228 | } |
| 229 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 230 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 231 | bool GlobalMerge::doInitialization(Module &M) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 232 | if (!EnableGlobalMerge) |
| 233 | return false; |
| 234 | |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 235 | DenseMap<unsigned, SmallVector<GlobalVariable*, 16> > Globals, ConstGlobals, |
| 236 | BSSGlobals; |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 237 | const TargetLowering *TLI = TM->getTargetLowering(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 238 | const DataLayout *DL = TLI->getDataLayout(); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 239 | unsigned MaxOffset = TLI->getMaximalGlobalOffset(); |
| 240 | bool Changed = false; |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 241 | setMustKeepGlobalVariables(M); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 242 | |
| 243 | // Grab all non-const globals. |
| 244 | for (Module::global_iterator I = M.global_begin(), |
| 245 | E = M.global_end(); I != E; ++I) { |
| 246 | // Merge is safe for "normal" internal globals only |
| 247 | if (!I->hasLocalLinkage() || I->isThreadLocal() || I->hasSection()) |
| 248 | continue; |
| 249 | |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 250 | PointerType *PT = dyn_cast<PointerType>(I->getType()); |
| 251 | assert(PT && "Global variable is not a pointer!"); |
| 252 | |
| 253 | unsigned AddressSpace = PT->getAddressSpace(); |
| 254 | |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 255 | // Ignore fancy-aligned globals for now. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 256 | unsigned Alignment = DL->getPreferredAlignment(I); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 257 | Type *Ty = I->getType()->getElementType(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 258 | if (Alignment > DL->getABITypeAlignment(Ty)) |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 259 | continue; |
| 260 | |
Anton Korobeynikov | b5a0ef9 | 2010-07-26 18:45:39 +0000 | [diff] [blame] | 261 | // Ignore all 'special' globals. |
| 262 | if (I->getName().startswith("llvm.") || |
| 263 | I->getName().startswith(".llvm.")) |
| 264 | continue; |
| 265 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 266 | // Ignore all "required" globals: |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 267 | if (isMustKeepGlobalVariable(I)) |
| 268 | continue; |
| 269 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame^] | 270 | if (DL->getTypeAllocSize(Ty) < MaxOffset) { |
Ahmed Charles | b83a67e | 2012-02-13 06:30:56 +0000 | [diff] [blame] | 271 | if (TargetLoweringObjectFile::getKindForGlobal(I, TLI->getTargetMachine()) |
| 272 | .isBSSLocal()) |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 273 | BSSGlobals[AddressSpace].push_back(I); |
Bob Wilson | 0564609 | 2010-11-17 21:25:39 +0000 | [diff] [blame] | 274 | else if (I->isConstant()) |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 275 | ConstGlobals[AddressSpace].push_back(I); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 276 | else |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 277 | Globals[AddressSpace].push_back(I); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 281 | for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator |
| 282 | I = Globals.begin(), E = Globals.end(); I != E; ++I) |
| 283 | if (I->second.size() > 1) |
| 284 | Changed |= doMerge(I->second, M, false, I->first); |
| 285 | |
| 286 | for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator |
| 287 | I = BSSGlobals.begin(), E = BSSGlobals.end(); I != E; ++I) |
| 288 | if (I->second.size() > 1) |
| 289 | Changed |= doMerge(I->second, M, false, I->first); |
Bob Wilson | 0564609 | 2010-11-17 21:25:39 +0000 | [diff] [blame] | 290 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 291 | if (EnableGlobalMergeOnConst) |
| 292 | for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator |
| 293 | I = ConstGlobals.begin(), E = ConstGlobals.end(); I != E; ++I) |
| 294 | if (I->second.size() > 1) |
| 295 | Changed |= doMerge(I->second, M, true, I->first); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 296 | |
| 297 | return Changed; |
| 298 | } |
| 299 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 300 | bool GlobalMerge::runOnFunction(Function &F) { |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | |
Quentin Colombet | 9deb917 | 2013-03-19 21:46:49 +0000 | [diff] [blame] | 304 | bool GlobalMerge::doFinalization(Module &M) { |
| 305 | MustKeepGlobalVariables.clear(); |
| 306 | return false; |
| 307 | } |
| 308 | |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 309 | Pass *llvm::createGlobalMergePass(const TargetMachine *TM) { |
| 310 | return new GlobalMerge(TM); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 311 | } |