| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 75 |
| 76 // Define map from a relocation, appearing in the flattened global variable | 76 // Define map from a relocation, appearing in the flattened global variable |
| 77 // initializers, to it's corresponding use handle. | 77 // initializers, to it's corresponding use handle. |
| 78 typedef DenseMap<Constant*, RelocUserType*> RelocMapType; | 78 typedef DenseMap<Constant*, RelocUserType*> RelocMapType; |
| 79 | 79 |
| 80 // Define the list to hold the list of global variables being flattened. | 80 // Define the list to hold the list of global variables being flattened. |
| 81 struct FlattenedGlobal; | 81 struct FlattenedGlobal; |
| 82 typedef std::vector<FlattenedGlobal*> FlattenedGlobalsVectorType; | 82 typedef std::vector<FlattenedGlobal*> FlattenedGlobalsVectorType; |
| 83 | 83 |
| 84 // Returns the corresponding relocation, for the given user handle. | 84 // Returns the corresponding relocation, for the given user handle. |
| 85 static Constant *getRelocUse(RelocUserType *RelocUser) { | 85 Constant *getRelocUseConstant(RelocUserType *RelocUser) { |
| 86 return cast<Constant>(RelocUser->getReturnValue()); | 86 return cast<Constant>(RelocUser->getReturnValue()); |
| 87 } | 87 } |
| 88 | 88 |
| 89 // The state associated with flattening globals of a module. | 89 // The state associated with flattening globals of a module. |
| 90 struct FlattenGlobalsState { | 90 struct FlattenGlobalsState { |
| 91 /// The module being flattened. | 91 /// The module being flattened. |
| 92 Module &M; | 92 Module &M; |
| 93 /// The data layout to be used. | 93 /// The data layout to be used. |
| 94 DataLayout DL; | 94 DataLayout DL; |
| 95 /// The relocations (within the original global variable initializers) | 95 /// The relocations (within the original global variable initializers) |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 uint8_t *BufEnd; | 168 uint8_t *BufEnd; |
| 169 | 169 |
| 170 // 2) an array of relocations. | 170 // 2) an array of relocations. |
| 171 class Reloc { | 171 class Reloc { |
| 172 private: | 172 private: |
| 173 unsigned RelOffset; // Offset at which the relocation is to be applied. | 173 unsigned RelOffset; // Offset at which the relocation is to be applied. |
| 174 RelocUserType *RelocUser; | 174 RelocUserType *RelocUser; |
| 175 public: | 175 public: |
| 176 | 176 |
| 177 unsigned getRelOffset() const { return RelOffset; } | 177 unsigned getRelOffset() const { return RelOffset; } |
| 178 Constant *getRelocUse() const { return ::getRelocUse(RelocUser); } | 178 Constant *getRelocUse() const { return ::getRelocUseConstant(RelocUser); } |
| 179 Reloc(FlattenGlobalsState &State, unsigned RelOffset, Constant *NewVal) | 179 Reloc(FlattenGlobalsState &State, unsigned RelOffset, Constant *NewVal) |
| 180 : RelOffset(RelOffset), | 180 : RelOffset(RelOffset), RelocUser(State.getRelocUserHandle(NewVal)) {} |
| 181 RelocUser(State.getRelocUserHandle(NewVal)) {} | |
| 182 | 181 |
| 183 explicit Reloc(const Reloc &R) | 182 explicit Reloc(const Reloc &R) |
| 184 : RelOffset(R.RelOffset), RelocUser(R.RelocUser) {} | 183 : RelOffset(R.RelOffset), RelocUser(R.RelocUser) {} |
| 185 | 184 |
| 186 void operator=(const Reloc &R) { | 185 void operator=(const Reloc &R) { |
| 187 RelOffset = R.RelOffset; | 186 RelOffset = R.RelOffset; |
| 188 RelocUser = R.RelocUser; | 187 RelocUser = R.RelocUser; |
| 189 } | 188 } |
| 190 }; | 189 }; |
| 191 typedef SmallVector<Reloc, 10> RelocArray; | 190 typedef SmallVector<Reloc, 10> RelocArray; |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 void FlattenGlobalsState::removeDeadInitializerConstants() { | 504 void FlattenGlobalsState::removeDeadInitializerConstants() { |
| 506 // Detach original initializers. | 505 // Detach original initializers. |
| 507 for (FlattenedGlobalsVectorType::iterator | 506 for (FlattenedGlobalsVectorType::iterator |
| 508 I = FlattenedGlobalsVector.begin(), E = FlattenedGlobalsVector.end(); | 507 I = FlattenedGlobalsVector.begin(), E = FlattenedGlobalsVector.end(); |
| 509 I != E; ++I) { | 508 I != E; ++I) { |
| 510 (*I)->removeOriginalInitializer(); | 509 (*I)->removeOriginalInitializer(); |
| 511 } | 510 } |
| 512 // Do cleanup of old initializers. | 511 // Do cleanup of old initializers. |
| 513 for (RelocMapType::iterator I = RelocMap.begin(), E = RelocMap.end(); | 512 for (RelocMapType::iterator I = RelocMap.begin(), E = RelocMap.end(); |
| 514 I != E; ++I) { | 513 I != E; ++I) { |
| 515 getRelocUse(I->second)->removeDeadConstantUsers(); | 514 getRelocUseConstant(I->second)->removeDeadConstantUsers(); |
| 516 } | 515 } |
| 517 | 516 |
| 518 } | 517 } |
| 519 | 518 |
| 520 void FlattenGlobalsState::replaceGlobalsWithFlattenedGlobals() { | 519 void FlattenGlobalsState::replaceGlobalsWithFlattenedGlobals() { |
| 521 for (FlattenedGlobalsVectorType::iterator | 520 for (FlattenedGlobalsVectorType::iterator |
| 522 I = FlattenedGlobalsVector.begin(), E = FlattenedGlobalsVector.end(); | 521 I = FlattenedGlobalsVector.begin(), E = FlattenedGlobalsVector.end(); |
| 523 I != E; ++I) { | 522 I != E; ++I) { |
| 524 (*I)->replaceGlobalWithFlattenedGlobal(); | 523 (*I)->replaceGlobalWithFlattenedGlobal(); |
| 525 } | 524 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 538 State.flattenGlobalsWithInitializers(); | 537 State.flattenGlobalsWithInitializers(); |
| 539 State.removeDeadInitializerConstants(); | 538 State.removeDeadInitializerConstants(); |
| 540 State.replaceGlobalsWithFlattenedGlobals(); | 539 State.replaceGlobalsWithFlattenedGlobals(); |
| 541 State.installFlattenedGlobalInitializers(); | 540 State.installFlattenedGlobalInitializers(); |
| 542 return State.Modified; | 541 return State.Modified; |
| 543 } | 542 } |
| 544 | 543 |
| 545 ModulePass *llvm::createFlattenGlobalsPass() { | 544 ModulePass *llvm::createFlattenGlobalsPass() { |
| 546 return new FlattenGlobals(); | 545 return new FlattenGlobals(); |
| 547 } | 546 } |
| OLD | NEW |