Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1 | //===- BranchIslandFactory.cpp --------------------------------------------===// |
| 2 | // |
| 3 | // The MCLinker Project |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | #include <mcld/LD/BranchIslandFactory.h> |
| 10 | #include <mcld/Fragment/Fragment.h> |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 11 | #include <mcld/LD/LDSection.h> |
| 12 | #include <mcld/LD/SectionData.h> |
| 13 | #include <mcld/Module.h> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 14 | |
| 15 | using namespace mcld; |
| 16 | |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | // BranchIslandFactory |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | /// ctor |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 22 | /// @param pMaxFwdBranchRange - the max forward branch range of the target |
| 23 | /// @param pMaxBwdBranchRange - the max backward branch range of the target |
| 24 | /// @param pMaxIslandSize - the predefined value for the max size of a island |
| 25 | BranchIslandFactory::BranchIslandFactory(int64_t pMaxFwdBranchRange, |
| 26 | int64_t pMaxBwdBranchRange, |
| 27 | size_t pMaxIslandSize) |
| 28 | : GCFactory<BranchIsland, 0>(1u), // magic number |
| 29 | m_MaxFwdBranchRange(pMaxFwdBranchRange - pMaxIslandSize), |
| 30 | m_MaxBwdBranchRange(pMaxBwdBranchRange + pMaxIslandSize), |
| 31 | m_MaxIslandSize(pMaxIslandSize) |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 32 | { |
| 33 | } |
| 34 | |
| 35 | BranchIslandFactory::~BranchIslandFactory() |
| 36 | { |
| 37 | } |
| 38 | |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 39 | /// group - group fragments and create islands when needed |
| 40 | /// @param pSectionData - the SectionData holds fragments need to be grouped |
| 41 | void BranchIslandFactory::group(Module& pModule) |
| 42 | { |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 43 | /* FIXME: Currently only support relaxing .text section! */ |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 44 | LDSection* text = pModule.getSection(".text"); |
| 45 | if (text != NULL && text->hasSectionData()) { |
| 46 | SectionData& sd = *text->getSectionData(); |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 47 | uint64_t group_end = m_MaxFwdBranchRange; |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 48 | for (SectionData::iterator it = sd.begin(), ie = sd.end(); it != ie; ++it) { |
| 49 | if ((*it).getOffset() + (*it).size() > group_end) { |
| 50 | Fragment* frag = (*it).getPrevNode(); |
| 51 | while (frag != NULL && frag->getKind() == Fragment::Alignment) { |
| 52 | frag = frag->getPrevNode(); |
| 53 | } |
| 54 | if (frag != NULL) { |
| 55 | produce(*frag); |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 56 | group_end = (*it).getOffset() + m_MaxFwdBranchRange; |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 60 | if (getIslands(sd.back()).first == NULL) |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 61 | produce(sd.back()); |
| 62 | } |
| 63 | } |
| 64 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 65 | /// produce - produce a island for the given fragment |
| 66 | /// @param pFragment - the fragment needs a branch island |
| 67 | BranchIsland* BranchIslandFactory::produce(Fragment& pFragment) |
| 68 | { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 69 | BranchIsland *island = allocate(); |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 70 | new (island) BranchIsland(pFragment, // entry fragment to the island |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 71 | m_MaxIslandSize, // the max size of the island |
Stephen Hines | f33f6de | 2014-02-14 18:00:16 -0800 | [diff] [blame] | 72 | size() - 1u); // index in the island factory |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 73 | return island; |
| 74 | } |
| 75 | |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 76 | /// getIsland - find fwd and bwd islands for the fragment |
| 77 | /// @param pFragment - the fragment needs a branch island |
| 78 | std::pair<BranchIsland*, BranchIsland*> |
| 79 | BranchIslandFactory::getIslands(const Fragment& pFragment) |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 80 | { |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 81 | BranchIsland* fwd = NULL; |
| 82 | BranchIsland* bwd = NULL; |
| 83 | for (iterator it = begin(), ie = end(), prev = ie; it != ie; |
| 84 | prev = it, ++it) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 85 | if ((pFragment.getOffset() < (*it).offset()) && |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 86 | ((pFragment.getOffset() + m_MaxFwdBranchRange) >= (*it).offset())) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 87 | |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 88 | fwd = &*it; |
| 89 | |
| 90 | if (prev != ie) { |
| 91 | int64_t bwd_off = (int64_t)pFragment.getOffset() + m_MaxBwdBranchRange; |
| 92 | if ((pFragment.getOffset() > (*prev).offset()) && |
Stephen Hines | 48b4262 | 2014-07-15 18:36:27 -0700 | [diff] [blame] | 93 | (bwd_off <= (int64_t) (*prev).offset())) { |
Stephen Hines | a790f0a | 2014-07-15 18:33:32 -0700 | [diff] [blame] | 94 | bwd = &*prev; |
| 95 | } |
| 96 | } |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | return std::make_pair(fwd, bwd); |
| 101 | } |