OLD | NEW |
1 //===- FlattenGlobals.cpp - Flatten global variable initializers-----------===// | 1 //===- FlattenGlobals.cpp - Flatten global variable initializers-----------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This pass converts initializers for global variables into a | 10 // This pass converts initializers for global variables into a |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 | 248 |
249 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); | 249 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
250 I != E; ) { | 250 I != E; ) { |
251 GlobalVariable *Global = I++; | 251 GlobalVariable *Global = I++; |
252 // Variables with "appending" linkage must always be arrays and so | 252 // Variables with "appending" linkage must always be arrays and so |
253 // cannot be normalized, so leave them alone. | 253 // cannot be normalized, so leave them alone. |
254 if (Global->hasAppendingLinkage()) | 254 if (Global->hasAppendingLinkage()) |
255 continue; | 255 continue; |
256 Modified = true; | 256 Modified = true; |
257 | 257 |
258 uint64_t Size = DL.getTypeAllocSize( | 258 Type *GlobalType = Global->getType()->getPointerElementType(); |
259 Global->getType()->getPointerElementType()); | 259 uint64_t Size = DL.getTypeAllocSize(GlobalType); |
260 Constant *NewInit; | 260 Constant *NewInit; |
261 Type *NewType; | 261 Type *NewType; |
262 if (Global->hasInitializer()) { | 262 if (Global->hasInitializer()) { |
263 if (Global->getInitializer()->isNullValue()) { | 263 if (Global->getInitializer()->isNullValue()) { |
264 // As optimization, for large BSS variables, avoid allocating | 264 // As optimization, for large BSS variables, avoid allocating |
265 // a buffer that would only be filled with zeroes. | 265 // a buffer that would only be filled with zeroes. |
266 NewType = ArrayType::get(I8, Size); | 266 NewType = ArrayType::get(I8, Size); |
267 NewInit = ConstantAggregateZero::get(NewType); | 267 NewInit = ConstantAggregateZero::get(NewType); |
268 } else { | 268 } else { |
269 FlattenedConstant Buffer(&DL, Global->getInitializer()); | 269 FlattenedConstant Buffer(&DL, Global->getInitializer()); |
270 NewInit = Buffer.getAsNormalFormConstant(); | 270 NewInit = Buffer.getAsNormalFormConstant(); |
271 NewType = NewInit->getType(); | 271 NewType = NewInit->getType(); |
272 } | 272 } |
273 } else { | 273 } else { |
274 NewInit = NULL; | 274 NewInit = NULL; |
275 NewType = ArrayType::get(I8, Size); | 275 NewType = ArrayType::get(I8, Size); |
276 } | 276 } |
277 GlobalVariable *NewGlobal = new GlobalVariable( | 277 GlobalVariable *NewGlobal = new GlobalVariable( |
278 M, NewType, | 278 M, NewType, |
279 Global->isConstant(), | 279 Global->isConstant(), |
280 Global->getLinkage(), | 280 Global->getLinkage(), |
281 NewInit, "", Global, | 281 NewInit, "", Global, |
282 Global->getThreadLocalMode()); | 282 Global->getThreadLocalMode()); |
283 NewGlobal->copyAttributesFrom(Global); | 283 NewGlobal->copyAttributesFrom(Global); |
| 284 if (NewGlobal->getAlignment() == 0) |
| 285 NewGlobal->setAlignment(DL.getPrefTypeAlignment(GlobalType)); |
284 NewGlobal->setExternallyInitialized(Global->isExternallyInitialized()); | 286 NewGlobal->setExternallyInitialized(Global->isExternallyInitialized()); |
285 NewGlobal->takeName(Global); | 287 NewGlobal->takeName(Global); |
286 Global->replaceAllUsesWith( | 288 Global->replaceAllUsesWith( |
287 ConstantExpr::getBitCast(NewGlobal, Global->getType())); | 289 ConstantExpr::getBitCast(NewGlobal, Global->getType())); |
288 Global->eraseFromParent(); | 290 Global->eraseFromParent(); |
289 } | 291 } |
290 return Modified; | 292 return Modified; |
291 | 293 |
292 } | 294 } |
293 | 295 |
294 ModulePass *llvm::createFlattenGlobalsPass() { | 296 ModulePass *llvm::createFlattenGlobalsPass() { |
295 return new FlattenGlobals(); | 297 return new FlattenGlobals(); |
296 } | 298 } |
OLD | NEW |