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 | #include "llvm/Transforms/Scalar.h" |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallPtrSet.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 57 | #include "llvm/IR/Attributes.h" |
| 58 | #include "llvm/IR/Constants.h" |
| 59 | #include "llvm/IR/DataLayout.h" |
| 60 | #include "llvm/IR/DerivedTypes.h" |
| 61 | #include "llvm/IR/Function.h" |
| 62 | #include "llvm/IR/GlobalVariable.h" |
| 63 | #include "llvm/IR/Instructions.h" |
| 64 | #include "llvm/IR/Intrinsics.h" |
| 65 | #include "llvm/IR/Module.h" |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 66 | #include "llvm/Pass.h" |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 67 | #include "llvm/CodeGen/Passes.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 | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 73 | #define DEBUG_TYPE "global-merge" |
| 74 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 75 | cl::opt<bool> |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 76 | EnableGlobalMerge("enable-global-merge", cl::Hidden, |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 77 | cl::desc("Enable global merge pass"), |
| 78 | cl::init(true)); |
| 79 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 80 | static cl::opt<bool> |
| 81 | EnableGlobalMergeOnConst("global-merge-on-const", cl::Hidden, |
Jakub Staszak | dca13e0 | 2013-07-22 21:11:30 +0000 | [diff] [blame] | 82 | cl::desc("Enable global merge pass on constants"), |
| 83 | cl::init(false)); |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 84 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 85 | // FIXME: this could be a transitional option, and we probably need to remove |
| 86 | // it if only we are sure this optimization could always benefit all targets. |
| 87 | static cl::opt<bool> |
| 88 | EnableGlobalMergeOnExternal("global-merge-on-external", cl::Hidden, |
| 89 | cl::desc("Enable global merge pass on external linkage"), |
| 90 | cl::init(false)); |
| 91 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 92 | STATISTIC(NumMerged , "Number of globals merged"); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 93 | namespace { |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 94 | class GlobalMerge : public FunctionPass { |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 95 | const TargetMachine *TM; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 96 | |
| 97 | bool doMerge(SmallVectorImpl<GlobalVariable*> &Globals, |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 98 | Module &M, bool isConst, unsigned AddrSpace) const; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 99 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 100 | /// \brief Check if the given variable has been identified as must keep |
| 101 | /// \pre setMustKeepGlobalVariables must have been called on the Module that |
| 102 | /// contains GV |
| 103 | bool isMustKeepGlobalVariable(const GlobalVariable *GV) const { |
| 104 | return MustKeepGlobalVariables.count(GV); |
| 105 | } |
| 106 | |
| 107 | /// Collect every variables marked as "used" or used in a landing pad |
| 108 | /// instruction for this Module. |
| 109 | void setMustKeepGlobalVariables(Module &M); |
| 110 | |
| 111 | /// Collect every variables marked as "used" |
| 112 | void collectUsedGlobalVariables(Module &M); |
| 113 | |
Quentin Colombet | 9deb917 | 2013-03-19 21:46:49 +0000 | [diff] [blame] | 114 | /// Keep track of the GlobalVariable that must not be merged away |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 115 | SmallPtrSet<const GlobalVariable *, 16> MustKeepGlobalVariables; |
| 116 | |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 117 | public: |
| 118 | static char ID; // Pass identification, replacement for typeid. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 119 | explicit GlobalMerge(const TargetMachine *TM = nullptr) |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 120 | : FunctionPass(ID), TM(TM) { |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 121 | initializeGlobalMergePass(*PassRegistry::getPassRegistry()); |
| 122 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 123 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 124 | bool doInitialization(Module &M) override; |
| 125 | bool runOnFunction(Function &F) override; |
| 126 | bool doFinalization(Module &M) override; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 127 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 128 | const char *getPassName() const override { |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 129 | return "Merge internal globals"; |
| 130 | } |
| 131 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 132 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 133 | AU.setPreservesCFG(); |
| 134 | FunctionPass::getAnalysisUsage(AU); |
| 135 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 136 | }; |
| 137 | } // end anonymous namespace |
| 138 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 139 | char GlobalMerge::ID = 0; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 140 | INITIALIZE_TM_PASS(GlobalMerge, "global-merge", "Merge global variables", |
| 141 | false, false) |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 142 | |
| 143 | bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable*> &Globals, |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 144 | Module &M, bool isConst, unsigned AddrSpace) const { |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 145 | const TargetLowering *TLI = TM->getTargetLowering(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 146 | const DataLayout *DL = TLI->getDataLayout(); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 147 | |
| 148 | // FIXME: Infer the maximum possible offset depending on the actual users |
| 149 | // (these max offsets are different for the users inside Thumb or ARM |
| 150 | // functions) |
| 151 | unsigned MaxOffset = TLI->getMaximalGlobalOffset(); |
| 152 | |
| 153 | // FIXME: Find better heuristics |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 154 | std::stable_sort(Globals.begin(), Globals.end(), |
| 155 | [DL](const GlobalVariable *GV1, const GlobalVariable *GV2) { |
| 156 | Type *Ty1 = cast<PointerType>(GV1->getType())->getElementType(); |
| 157 | Type *Ty2 = cast<PointerType>(GV2->getType())->getElementType(); |
| 158 | |
| 159 | return (DL->getTypeAllocSize(Ty1) < DL->getTypeAllocSize(Ty2)); |
| 160 | }); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 161 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 162 | Type *Int32Ty = Type::getInt32Ty(M.getContext()); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 163 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 164 | assert(Globals.size() > 1); |
| 165 | |
| 166 | // FIXME: This simple solution merges globals all together as maximum as |
| 167 | // possible. However, with this solution it would be hard to remove dead |
| 168 | // global symbols at link-time. An alternative solution could be checking |
| 169 | // global symbols references function by function, and make the symbols |
| 170 | // being referred in the same function merged and we would probably need |
| 171 | // to introduce heuristic algorithm to solve the merge conflict from |
| 172 | // different functions. |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 173 | for (size_t i = 0, e = Globals.size(); i != e; ) { |
| 174 | size_t j = 0; |
| 175 | uint64_t MergedSize = 0; |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 176 | std::vector<Type*> Tys; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 177 | std::vector<Constant*> Inits; |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 178 | |
| 179 | bool HasExternal = false; |
| 180 | GlobalVariable *TheFirstExternal = 0; |
Bob Wilson | 619a372 | 2010-11-17 21:25:36 +0000 | [diff] [blame] | 181 | for (j = i; j != e; ++j) { |
Jay Foad | 5fdd6c8 | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 182 | Type *Ty = Globals[j]->getType()->getElementType(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 183 | MergedSize += DL->getTypeAllocSize(Ty); |
Bob Wilson | 619a372 | 2010-11-17 21:25:36 +0000 | [diff] [blame] | 184 | if (MergedSize > MaxOffset) { |
| 185 | break; |
| 186 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 187 | Tys.push_back(Ty); |
| 188 | Inits.push_back(Globals[j]->getInitializer()); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 189 | |
| 190 | if (Globals[j]->hasExternalLinkage() && !HasExternal) { |
| 191 | HasExternal = true; |
| 192 | TheFirstExternal = Globals[j]; |
| 193 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 196 | // If merged variables doesn't have external linkage, we needn't to expose |
| 197 | // the symbol after merging. |
| 198 | GlobalValue::LinkageTypes Linkage = HasExternal |
| 199 | ? GlobalValue::ExternalLinkage |
| 200 | : GlobalValue::InternalLinkage; |
| 201 | |
Chris Lattner | 252b491 | 2010-09-05 21:18:45 +0000 | [diff] [blame] | 202 | StructType *MergedTy = StructType::get(M.getContext(), Tys); |
| 203 | Constant *MergedInit = ConstantStruct::get(MergedTy, Inits); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 204 | |
| 205 | // If merged variables have external linkage, we use symbol name of the |
| 206 | // first variable merged as the suffix of global symbol name. This would |
| 207 | // be able to avoid the link-time naming conflict for globalm symbols. |
| 208 | GlobalVariable *MergedGV = new GlobalVariable( |
| 209 | M, MergedTy, isConst, Linkage, MergedInit, |
| 210 | HasExternal ? "_MergedGlobals_" + TheFirstExternal->getName() |
| 211 | : "_MergedGlobals", |
| 212 | nullptr, GlobalVariable::NotThreadLocal, AddrSpace); |
| 213 | |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 214 | for (size_t k = i; k < j; ++k) { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 215 | GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage(); |
| 216 | std::string Name = Globals[k]->getName(); |
| 217 | |
Chris Lattner | 252b491 | 2010-09-05 21:18:45 +0000 | [diff] [blame] | 218 | Constant *Idx[2] = { |
| 219 | ConstantInt::get(Int32Ty, 0), |
| 220 | ConstantInt::get(Int32Ty, k-i) |
| 221 | }; |
Jay Foad | dab3d29 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 222 | Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedGV, Idx); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 223 | Globals[k]->replaceAllUsesWith(GEP); |
| 224 | Globals[k]->eraseFromParent(); |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 225 | |
| 226 | if (Linkage != GlobalValue::InternalLinkage) { |
| 227 | // Generate a new alias... |
| 228 | auto *PTy = cast<PointerType>(GEP->getType()); |
| 229 | GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(), |
| 230 | Linkage, Name, GEP, &M); |
| 231 | } |
| 232 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 233 | NumMerged++; |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 234 | } |
| 235 | i = j; |
| 236 | } |
| 237 | |
| 238 | return true; |
| 239 | } |
| 240 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 241 | void GlobalMerge::collectUsedGlobalVariables(Module &M) { |
| 242 | // Extract global variables from llvm.used array |
| 243 | const GlobalVariable *GV = M.getGlobalVariable("llvm.used"); |
| 244 | if (!GV || !GV->hasInitializer()) return; |
| 245 | |
| 246 | // Should be an array of 'i8*'. |
Rafael Espindola | cde25b4 | 2013-04-22 14:58:02 +0000 | [diff] [blame] | 247 | const ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer()); |
| 248 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 249 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 250 | if (const GlobalVariable *G = |
| 251 | dyn_cast<GlobalVariable>(InitList->getOperand(i)->stripPointerCasts())) |
| 252 | MustKeepGlobalVariables.insert(G); |
| 253 | } |
| 254 | |
| 255 | void GlobalMerge::setMustKeepGlobalVariables(Module &M) { |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 256 | collectUsedGlobalVariables(M); |
| 257 | |
| 258 | for (Module::iterator IFn = M.begin(), IEndFn = M.end(); IFn != IEndFn; |
| 259 | ++IFn) { |
| 260 | for (Function::iterator IBB = IFn->begin(), IEndBB = IFn->end(); |
| 261 | IBB != IEndBB; ++IBB) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 262 | // Follow the invoke link to find the landing pad instruction |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 263 | const InvokeInst *II = dyn_cast<InvokeInst>(IBB->getTerminator()); |
| 264 | if (!II) continue; |
| 265 | |
| 266 | const LandingPadInst *LPInst = II->getUnwindDest()->getLandingPadInst(); |
| 267 | // Look for globals in the clauses of the landing pad instruction |
| 268 | for (unsigned Idx = 0, NumClauses = LPInst->getNumClauses(); |
| 269 | Idx != NumClauses; ++Idx) |
| 270 | if (const GlobalVariable *GV = |
| 271 | dyn_cast<GlobalVariable>(LPInst->getClause(Idx) |
| 272 | ->stripPointerCasts())) |
| 273 | MustKeepGlobalVariables.insert(GV); |
| 274 | } |
| 275 | } |
| 276 | } |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 277 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 278 | bool GlobalMerge::doInitialization(Module &M) { |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 279 | if (!EnableGlobalMerge) |
| 280 | return false; |
| 281 | |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 282 | DenseMap<unsigned, SmallVector<GlobalVariable*, 16> > Globals, ConstGlobals, |
| 283 | BSSGlobals; |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 284 | const TargetLowering *TLI = TM->getTargetLowering(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 285 | const DataLayout *DL = TLI->getDataLayout(); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 286 | unsigned MaxOffset = TLI->getMaximalGlobalOffset(); |
| 287 | bool Changed = false; |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 288 | setMustKeepGlobalVariables(M); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 289 | |
| 290 | // Grab all non-const globals. |
| 291 | for (Module::global_iterator I = M.global_begin(), |
| 292 | E = M.global_end(); I != E; ++I) { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 293 | // Merge is safe for "normal" internal or external globals only |
| 294 | if (I->isDeclaration() || I->isThreadLocal() || I->hasSection()) |
| 295 | continue; |
| 296 | |
| 297 | if (!(EnableGlobalMergeOnExternal && I->hasExternalLinkage()) && |
| 298 | !I->hasInternalLinkage()) |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 299 | continue; |
| 300 | |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 301 | PointerType *PT = dyn_cast<PointerType>(I->getType()); |
| 302 | assert(PT && "Global variable is not a pointer!"); |
| 303 | |
| 304 | unsigned AddressSpace = PT->getAddressSpace(); |
| 305 | |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 306 | // Ignore fancy-aligned globals for now. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 307 | unsigned Alignment = DL->getPreferredAlignment(I); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 308 | Type *Ty = I->getType()->getElementType(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 309 | if (Alignment > DL->getABITypeAlignment(Ty)) |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 310 | continue; |
| 311 | |
Anton Korobeynikov | b5a0ef9 | 2010-07-26 18:45:39 +0000 | [diff] [blame] | 312 | // Ignore all 'special' globals. |
| 313 | if (I->getName().startswith("llvm.") || |
| 314 | I->getName().startswith(".llvm.")) |
| 315 | continue; |
| 316 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 317 | // Ignore all "required" globals: |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 318 | if (isMustKeepGlobalVariable(I)) |
| 319 | continue; |
| 320 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 321 | if (DL->getTypeAllocSize(Ty) < MaxOffset) { |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 322 | if (TargetLoweringObjectFile::getKindForGlobal(I, *TM).isBSSLocal()) |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 323 | BSSGlobals[AddressSpace].push_back(I); |
Bob Wilson | 0564609 | 2010-11-17 21:25:39 +0000 | [diff] [blame] | 324 | else if (I->isConstant()) |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 325 | ConstGlobals[AddressSpace].push_back(I); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 326 | else |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 327 | Globals[AddressSpace].push_back(I); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
Silviu Baranga | e971659 | 2013-01-07 12:31:25 +0000 | [diff] [blame] | 331 | for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator |
| 332 | I = Globals.begin(), E = Globals.end(); I != E; ++I) |
| 333 | if (I->second.size() > 1) |
| 334 | Changed |= doMerge(I->second, M, false, I->first); |
| 335 | |
| 336 | for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator |
| 337 | I = BSSGlobals.begin(), E = BSSGlobals.end(); I != E; ++I) |
| 338 | if (I->second.size() > 1) |
| 339 | Changed |= doMerge(I->second, M, false, I->first); |
Bob Wilson | 0564609 | 2010-11-17 21:25:39 +0000 | [diff] [blame] | 340 | |
Quentin Colombet | e572809 | 2013-03-18 22:30:07 +0000 | [diff] [blame] | 341 | if (EnableGlobalMergeOnConst) |
| 342 | for (DenseMap<unsigned, SmallVector<GlobalVariable*, 16> >::iterator |
| 343 | I = ConstGlobals.begin(), E = ConstGlobals.end(); I != E; ++I) |
| 344 | if (I->second.size() > 1) |
| 345 | Changed |= doMerge(I->second, M, true, I->first); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 346 | |
| 347 | return Changed; |
| 348 | } |
| 349 | |
Devang Patel | 827454e | 2011-10-17 17:17:43 +0000 | [diff] [blame] | 350 | bool GlobalMerge::runOnFunction(Function &F) { |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
Quentin Colombet | 9deb917 | 2013-03-19 21:46:49 +0000 | [diff] [blame] | 354 | bool GlobalMerge::doFinalization(Module &M) { |
| 355 | MustKeepGlobalVariables.clear(); |
| 356 | return false; |
| 357 | } |
| 358 | |
Bill Wendling | f9fd58a | 2013-06-19 21:07:11 +0000 | [diff] [blame] | 359 | Pass *llvm::createGlobalMergePass(const TargetMachine *TM) { |
| 360 | return new GlobalMerge(TM); |
Anton Korobeynikov | cec36f4 | 2010-07-24 21:52:08 +0000 | [diff] [blame] | 361 | } |