| Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 1 | //===--Passes/LayoutPass.cpp - Layout atoms -------------------------------===// |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 9 | |
| Michael J. Spencer | 7f09a3d | 2013-02-26 01:35:30 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "LayoutPass" |
| 11 | |
| Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 12 | #include <set> |
| 13 | |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 14 | #include "lld/Passes/LayoutPass.h" |
| Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 15 | #include "lld/Core/Instrumentation.h" |
| Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 16 | |
| 17 | #include "llvm/ADT/Twine.h" |
| Michael J. Spencer | 7f09a3d | 2013-02-26 01:35:30 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 20 | using namespace lld; |
| 21 | |
| Rui Ueyama | 5206222 | 2013-10-18 03:18:54 +0000 | [diff] [blame] | 22 | #ifndef NDEBUG |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | // Return "reason (leftval, rightval)" |
| 25 | std::string formatReason(StringRef reason, int leftVal, int rightVal) { |
| 26 | Twine msg = |
| 27 | Twine(reason) + " (" + Twine(leftVal) + ", " + Twine(rightVal) + ")"; |
| Rui Ueyama | c74157c | 2013-11-01 20:40:33 +0000 | [diff] [blame] | 28 | return msg.str(); |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 29 | } |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 30 | } // end anonymous namespace |
| 31 | |
| 32 | // Less-than relationship of two atoms must be transitive, which is, if a < b |
| 33 | // and b < c, a < c must be true. This function checks the transitivity by |
| 34 | // checking the sort results. |
| 35 | void LayoutPass::checkTransitivity(DefinedAtomIter begin, |
| 36 | DefinedAtomIter end) const { |
| 37 | for (DefinedAtomIter i = begin; (i + 1) != end; ++i) { |
| 38 | for (DefinedAtomIter j = i + 1; j != end; ++j) { |
| 39 | assert(_compareAtoms(*i, *j)); |
| 40 | assert(!_compareAtoms(*j, *i)); |
| 41 | } |
| 42 | } |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 43 | } |
| Rui Ueyama | 5206222 | 2013-10-18 03:18:54 +0000 | [diff] [blame] | 44 | #endif // NDEBUG |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 45 | |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 46 | /// The function compares atoms by sorting atoms in the following order |
| Shankar Easwaran | d6d1b52 | 2013-09-12 15:59:34 +0000 | [diff] [blame] | 47 | /// a) Sorts atoms by Section position preference |
| 48 | /// b) Sorts atoms by their ordinal overrides |
| 49 | /// (layout-after/layout-before/ingroup) |
| 50 | /// c) Sorts atoms by their permissions |
| 51 | /// d) Sorts atoms by their content |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 52 | /// e) Sorts atoms on how they appear using File Ordinality |
| 53 | /// f) Sorts atoms on how they appear within the File |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 54 | bool LayoutPass::CompareAtoms::compare(const DefinedAtom *left, |
| 55 | const DefinedAtom *right, |
| 56 | std::string &reason) const { |
| 57 | if (left == right) { |
| 58 | reason = "same"; |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 59 | return false; |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 60 | } |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 61 | |
| Shankar Easwaran | d8da989 | 2013-05-22 17:41:04 +0000 | [diff] [blame] | 62 | // Sort by section position preference. |
| 63 | DefinedAtom::SectionPosition leftPos = left->sectionPosition(); |
| 64 | DefinedAtom::SectionPosition rightPos = right->sectionPosition(); |
| 65 | |
| Shankar Easwaran | d8da989 | 2013-05-22 17:41:04 +0000 | [diff] [blame] | 66 | bool leftSpecialPos = (leftPos != DefinedAtom::sectionPositionAny); |
| 67 | bool rightSpecialPos = (rightPos != DefinedAtom::sectionPositionAny); |
| 68 | if (leftSpecialPos || rightSpecialPos) { |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 69 | if (leftPos != rightPos) { |
| 70 | DEBUG(reason = formatReason("sectionPos", (int)leftPos, (int)rightPos)); |
| Shankar Easwaran | d8da989 | 2013-05-22 17:41:04 +0000 | [diff] [blame] | 71 | return leftPos < rightPos; |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 72 | } |
| Shankar Easwaran | d8da989 | 2013-05-22 17:41:04 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 75 | // Find the root of the chain if it is a part of a follow-on chain. |
| 76 | auto leftFind = _layout._followOnRoots.find(left); |
| 77 | auto rightFind = _layout._followOnRoots.find(right); |
| 78 | const DefinedAtom *leftRoot = |
| 79 | (leftFind == _layout._followOnRoots.end()) ? left : leftFind->second; |
| 80 | const DefinedAtom *rightRoot = |
| 81 | (rightFind == _layout._followOnRoots.end()) ? right : rightFind->second; |
| Shankar Easwaran | f1b341c | 2013-09-12 15:43:09 +0000 | [diff] [blame] | 82 | |
| 83 | // Sort atoms by their ordinal overrides only if they fall in the same |
| 84 | // chain. |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 85 | AtomToOrdinalT::const_iterator lPos = _layout._ordinalOverrideMap.find(left); |
| 86 | AtomToOrdinalT::const_iterator rPos = _layout._ordinalOverrideMap.find(right); |
| 87 | AtomToOrdinalT::const_iterator end = _layout._ordinalOverrideMap.end(); |
| 88 | if (leftRoot == rightRoot && lPos != end && rPos != end) { |
| 89 | DEBUG(reason = formatReason("override", lPos->second, rPos->second)); |
| 90 | return lPos->second < rPos->second; |
| Shankar Easwaran | 3c5d2c8 | 2013-05-10 16:44:02 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 93 | // Sort same permissions together. |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 94 | DefinedAtom::ContentPermissions leftPerms = leftRoot->permissions(); |
| 95 | DefinedAtom::ContentPermissions rightPerms = rightRoot->permissions(); |
| Shankar Easwaran | 8c25685 | 2013-03-13 04:05:38 +0000 | [diff] [blame] | 96 | |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 97 | if (leftPerms != rightPerms) { |
| 98 | DEBUG(reason = |
| 99 | formatReason("contentPerms", (int)leftPerms, (int)rightPerms)); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 100 | return leftPerms < rightPerms; |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 101 | } |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 102 | |
| 103 | // Sort same content types together. |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 104 | DefinedAtom::ContentType leftType = leftRoot->contentType(); |
| 105 | DefinedAtom::ContentType rightType = rightRoot->contentType(); |
| Shankar Easwaran | 8c25685 | 2013-03-13 04:05:38 +0000 | [diff] [blame] | 106 | |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 107 | if (leftType != rightType) { |
| 108 | DEBUG(reason = formatReason("contentType", (int)leftType, (int)rightType)); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 109 | return leftType < rightType; |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 110 | } |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 111 | |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 112 | // Sort by .o order. |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 113 | const File *leftFile = &leftRoot->file(); |
| 114 | const File *rightFile = &rightRoot->file(); |
| Shankar Easwaran | 8c25685 | 2013-03-13 04:05:38 +0000 | [diff] [blame] | 115 | |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 116 | if (leftFile != rightFile) { |
| 117 | DEBUG(reason = formatReason(".o order", (int)leftFile->ordinal(), |
| 118 | (int)rightFile->ordinal())); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 119 | return leftFile->ordinal() < rightFile->ordinal(); |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 120 | } |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 121 | |
| 122 | // Sort by atom order with .o file. |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 123 | uint64_t leftOrdinal = leftRoot->ordinal(); |
| 124 | uint64_t rightOrdinal = rightRoot->ordinal(); |
| Shankar Easwaran | 8c25685 | 2013-03-13 04:05:38 +0000 | [diff] [blame] | 125 | |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 126 | if (leftOrdinal != rightOrdinal) { |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 127 | DEBUG(reason = formatReason("ordinal", (int)leftRoot->ordinal(), |
| 128 | (int)rightRoot->ordinal())); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 129 | return leftOrdinal < rightOrdinal; |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 130 | } |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 131 | |
| Rui Ueyama | cd48075 | 2013-11-27 01:33:42 +0000 | [diff] [blame] | 132 | llvm::errs() << "Unordered: <" << left->name() << "> <" |
| 133 | << right->name() << ">\n"; |
| Shankar Easwaran | bcf3656 | 2013-10-11 01:50:04 +0000 | [diff] [blame] | 134 | llvm_unreachable("Atoms with Same Ordinal!"); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 137 | bool LayoutPass::CompareAtoms::operator()(const DefinedAtom *left, |
| 138 | const DefinedAtom *right) const { |
| 139 | std::string reason; |
| 140 | bool result = compare(left, right, reason); |
| 141 | DEBUG({ |
| Rui Ueyama | 4050b20 | 2013-10-18 03:18:52 +0000 | [diff] [blame] | 142 | StringRef comp = result ? "<" : ">="; |
| Rui Ueyama | 6a607b6 | 2013-10-18 02:56:31 +0000 | [diff] [blame] | 143 | llvm::dbgs() << "Layout: '" << left->name() << "' " << comp << " '" |
| 144 | << right->name() << "' (" << reason << ")\n"; |
| 145 | }); |
| 146 | return result; |
| 147 | } |
| 148 | |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 149 | // Returns the atom immediately followed by the given atom in the followon |
| 150 | // chain. |
| 151 | const DefinedAtom *LayoutPass::findAtomFollowedBy( |
| 152 | const DefinedAtom *targetAtom) { |
| 153 | // Start from the beginning of the chain and follow the chain until |
| 154 | // we find the targetChain. |
| 155 | const DefinedAtom *atom = _followOnRoots[targetAtom]; |
| 156 | while (true) { |
| 157 | const DefinedAtom *prevAtom = atom; |
| 158 | AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom); |
| 159 | // The target atom must be in the chain of its root. |
| 160 | assert(targetFollowOnAtomsIter != _followOnNexts.end()); |
| 161 | atom = targetFollowOnAtomsIter->second; |
| 162 | if (atom == targetAtom) |
| 163 | return prevAtom; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // Check if all the atoms followed by the given target atom are of size zero. |
| 168 | // When this method is called, an atom being added is not of size zero and |
| 169 | // will be added to the head of the followon chain. All the atoms between the |
| 170 | // atom and the targetAtom (specified by layout-after) need to be of size zero |
| 171 | // in this case. Otherwise the desired layout is impossible. |
| 172 | bool LayoutPass::checkAllPrevAtomsZeroSize(const DefinedAtom *targetAtom) { |
| 173 | const DefinedAtom *atom = _followOnRoots[targetAtom]; |
| 174 | while (true) { |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 175 | if (atom == targetAtom) |
| 176 | return true; |
| Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 177 | if (atom->size() != 0) |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 178 | // TODO: print warning that an impossible layout is being desired by the |
| 179 | // user. |
| 180 | return false; |
| Rui Ueyama | 5ec6d1a | 2013-05-14 01:51:56 +0000 | [diff] [blame] | 181 | AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom); |
| 182 | // The target atom must be in the chain of its root. |
| 183 | assert(targetFollowOnAtomsIter != _followOnNexts.end()); |
| 184 | atom = targetFollowOnAtomsIter->second; |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | // Set the root of all atoms in targetAtom's chain to the given root. |
| 189 | void LayoutPass::setChainRoot(const DefinedAtom *targetAtom, |
| 190 | const DefinedAtom *root) { |
| 191 | // Walk through the followon chain and override each node's root. |
| 192 | while (true) { |
| 193 | _followOnRoots[targetAtom] = root; |
| 194 | AtomToAtomT::iterator targetFollowOnAtomsIter = |
| 195 | _followOnNexts.find(targetAtom); |
| 196 | if (targetFollowOnAtomsIter == _followOnNexts.end()) |
| 197 | return; |
| 198 | targetAtom = targetFollowOnAtomsIter->second; |
| 199 | } |
| 200 | } |
| 201 | |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 202 | /// This pass builds the followon tables described by two DenseMaps |
| 203 | /// followOnRoots and followonNexts. |
| 204 | /// The followOnRoots map contains a mapping of a DefinedAtom to its root |
| 205 | /// The followOnNexts map contains a mapping of what DefinedAtom follows the |
| 206 | /// current Atom |
| 207 | /// The algorithm follows a very simple approach |
| 208 | /// a) If the atom is first seen, then make that as the root atom |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 209 | /// b) The targetAtom which this Atom contains, has the root thats set to the |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 210 | /// root of the current atom |
| 211 | /// c) If the targetAtom is part of a different tree and the root of the |
| 212 | /// targetAtom is itself, Chain all the atoms that are contained in the tree |
| 213 | /// to the current Tree |
| 214 | /// d) If the targetAtom is part of a different chain and the root of the |
| 215 | /// targetAtom until the targetAtom has all atoms of size 0, then chain the |
| 216 | /// targetAtoms and its tree to the current chain |
| 217 | void LayoutPass::buildFollowOnTable(MutableFile::DefinedAtomRange &range) { |
| Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 218 | ScopedTask task(getDefaultDomain(), "LayoutPass::buildFollowOnTable"); |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 219 | // Set the initial size of the followon and the followonNext hash to the |
| 220 | // number of atoms that we have. |
| Shankar Easwaran | 45a5f93 | 2013-04-29 03:27:57 +0000 | [diff] [blame] | 221 | _followOnRoots.resize(range.size()); |
| 222 | _followOnNexts.resize(range.size()); |
| Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 223 | for (const DefinedAtom *ai : range) { |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 224 | for (const Reference *r : *ai) { |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 225 | if (r->kind() != lld::Reference::kindLayoutAfter) |
| 226 | continue; |
| Rui Ueyama | c1800be | 2013-11-05 01:37:40 +0000 | [diff] [blame] | 227 | const DefinedAtom *targetAtom = dyn_cast<DefinedAtom>(r->target()); |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 228 | _followOnNexts[ai] = targetAtom; |
| 229 | |
| Alp Toker | 2259376 | 2013-12-02 01:28:14 +0000 | [diff] [blame^] | 230 | // If we find a followon for the first time, let's make that atom as the |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 231 | // root atom. |
| 232 | if (_followOnRoots.count(ai) == 0) |
| 233 | _followOnRoots[ai] = ai; |
| 234 | |
| 235 | auto iter = _followOnRoots.find(targetAtom); |
| 236 | if (iter == _followOnRoots.end()) { |
| Alp Toker | 2259376 | 2013-12-02 01:28:14 +0000 | [diff] [blame^] | 237 | // If the targetAtom is not a root of any chain, let's make the root of |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 238 | // the targetAtom to the root of the current chain. |
| 239 | _followOnRoots[targetAtom] = _followOnRoots[ai]; |
| 240 | } else if (iter->second == targetAtom) { |
| 241 | // If the targetAtom is the root of a chain, the chain becomes part of |
| 242 | // the current chain. Rewrite the subchain's root to the current |
| 243 | // chain's root. |
| 244 | setChainRoot(targetAtom, _followOnRoots[ai]); |
| 245 | } else { |
| 246 | // The targetAtom is already a part of a chain. If the current atom is |
| 247 | // of size zero, we can insert it in the middle of the chain just |
| 248 | // before the target atom, while not breaking other atom's followon |
| 249 | // relationships. If it's not, we can only insert the current atom at |
| 250 | // the beginning of the chain. All the atoms followed by the target |
| 251 | // atom must be of size zero in that case to satisfy the followon |
| 252 | // relationships. |
| Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 253 | size_t currentAtomSize = ai->size(); |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 254 | if (currentAtomSize == 0) { |
| 255 | const DefinedAtom *targetPrevAtom = findAtomFollowedBy(targetAtom); |
| 256 | _followOnNexts[targetPrevAtom] = ai; |
| 257 | _followOnRoots[ai] = _followOnRoots[targetPrevAtom]; |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 258 | } else { |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 259 | if (!checkAllPrevAtomsZeroSize(targetAtom)) |
| 260 | break; |
| 261 | _followOnNexts[ai] = _followOnRoots[targetAtom]; |
| 262 | setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /// This pass builds the followon tables using InGroup relationships |
| 270 | /// The algorithm follows a very simple approach |
| 271 | /// a) If the rootAtom is not part of any root, create a new root with the |
| 272 | /// as the head |
| 273 | /// b) If the current Atom root is not found, then make the current atoms root |
| 274 | /// point to the rootAtom |
| 275 | /// c) If the root of the current Atom is itself a root of some other tree |
| 276 | /// make all the atoms in the chain point to the ingroup reference |
| 277 | /// d) Check to see if the current atom is part of the chain from the rootAtom |
| 278 | /// if not add the atom to the chain, so that the current atom is part of the |
| 279 | /// the chain where the rootAtom is in |
| 280 | void LayoutPass::buildInGroupTable(MutableFile::DefinedAtomRange &range) { |
| Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 281 | ScopedTask task(getDefaultDomain(), "LayoutPass::buildInGroupTable"); |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 282 | // This table would convert precededby references to follow on |
| 283 | // references so that we have only one table |
| Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 284 | for (const DefinedAtom *ai : range) { |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 285 | for (const Reference *r : *ai) { |
| 286 | if (r->kind() == lld::Reference::kindInGroup) { |
| Rui Ueyama | c1800be | 2013-11-05 01:37:40 +0000 | [diff] [blame] | 287 | const DefinedAtom *rootAtom = dyn_cast<DefinedAtom>(r->target()); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 288 | // If the root atom is not part of any root |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 289 | // create a new root |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 290 | if (_followOnRoots.count(rootAtom) == 0) { |
| 291 | _followOnRoots[rootAtom] = rootAtom; |
| 292 | } |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 293 | // If the current Atom has not been seen yet and there is no root |
| 294 | // that has been set, set the root of the atom to the targetAtom |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 295 | // as the targetAtom points to the ingroup root |
| 296 | auto iter = _followOnRoots.find(ai); |
| 297 | if (iter == _followOnRoots.end()) { |
| 298 | _followOnRoots[ai] = rootAtom; |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 299 | } else if (iter->second == ai) { |
| 300 | if (iter->second != rootAtom) |
| 301 | setChainRoot(iter->second, rootAtom); |
| 302 | } else { |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 303 | // TODO : Flag an error that the root of the tree |
| 304 | // is different, Here is an example |
| 305 | // Say there are atoms |
| 306 | // chain 1 : a->b->c |
| 307 | // chain 2 : d->e->f |
| 308 | // and e,f have their ingroup reference as a |
| 309 | // this could happen only if the root of e,f that is d |
| 310 | // has root as 'a' |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | // Check if the current atom is part of the chain |
| 315 | bool isAtomInChain = false; |
| 316 | const DefinedAtom *lastAtom = rootAtom; |
| 317 | while (true) { |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 318 | AtomToAtomT::iterator followOnAtomsIter = |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 319 | _followOnNexts.find(lastAtom); |
| 320 | if (followOnAtomsIter != _followOnNexts.end()) { |
| 321 | lastAtom = followOnAtomsIter->second; |
| 322 | if (lastAtom == ai) { |
| 323 | isAtomInChain = true; |
| 324 | break; |
| 325 | } |
| 326 | } |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 327 | else |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 328 | break; |
| 329 | } // findAtomInChain |
| 330 | |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 331 | if (!isAtomInChain) |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 332 | _followOnNexts[lastAtom] = ai; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /// This pass builds the followon tables using Preceded By relationships |
| 339 | /// The algorithm follows a very simple approach |
| 340 | /// a) If the targetAtom is not part of any root and the current atom is not |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 341 | /// part of any root, create a chain with the current atom as root and |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 342 | /// the targetAtom as following the current atom |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 343 | /// b) Chain the targetAtom to the current Atom if the targetAtom is not part |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 344 | /// of any chain and the currentAtom has no followOn's |
| 345 | /// c) If the targetAtom is part of a different tree and the root of the |
| 346 | /// targetAtom is itself, and if the current atom is not part of any root |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 347 | /// chain all the atoms together |
| 348 | /// d) If the current atom has no followon and the root of the targetAtom is |
| 349 | /// not equal to the root of the current atom(the targetAtom is not in the |
| 350 | /// same chain), chain all the atoms that are lead by the targetAtom into |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 351 | /// the current chain |
| 352 | void LayoutPass::buildPrecededByTable(MutableFile::DefinedAtomRange &range) { |
| Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 353 | ScopedTask task(getDefaultDomain(), "LayoutPass::buildPrecededByTable"); |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 354 | // This table would convert precededby references to follow on |
| 355 | // references so that we have only one table |
| Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 356 | for (const DefinedAtom *ai : range) { |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 357 | for (const Reference *r : *ai) { |
| 358 | if (r->kind() == lld::Reference::kindLayoutBefore) { |
| Rui Ueyama | c1800be | 2013-11-05 01:37:40 +0000 | [diff] [blame] | 359 | const DefinedAtom *targetAtom = dyn_cast<DefinedAtom>(r->target()); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 360 | // Is the targetAtom not chained |
| 361 | if (_followOnRoots.count(targetAtom) == 0) { |
| 362 | // Is the current atom not part of any root ? |
| 363 | if (_followOnRoots.count(ai) == 0) { |
| 364 | _followOnRoots[ai] = ai; |
| 365 | _followOnNexts[ai] = targetAtom; |
| 366 | _followOnRoots[targetAtom] = _followOnRoots[ai]; |
| 367 | } else if (_followOnNexts.count(ai) == 0) { |
| 368 | // Chain the targetAtom to the current Atom |
| 369 | // if the currentAtom has no followon references |
| 370 | _followOnNexts[ai] = targetAtom; |
| 371 | _followOnRoots[targetAtom] = _followOnRoots[ai]; |
| 372 | } |
| 373 | } else if (_followOnRoots.find(targetAtom)->second == targetAtom) { |
| 374 | // Is the targetAtom in chain with the targetAtom as the root ? |
| 375 | bool changeRoots = false; |
| 376 | if (_followOnRoots.count(ai) == 0) { |
| 377 | _followOnRoots[ai] = ai; |
| 378 | _followOnNexts[ai] = targetAtom; |
| 379 | _followOnRoots[targetAtom] = _followOnRoots[ai]; |
| 380 | changeRoots = true; |
| 381 | } else if (_followOnNexts.count(ai) == 0) { |
| 382 | // Chain the targetAtom to the current Atom |
| 383 | // if the currentAtom has no followon references |
| 384 | if (_followOnRoots[ai] != _followOnRoots[targetAtom]) { |
| 385 | _followOnNexts[ai] = targetAtom; |
| 386 | _followOnRoots[targetAtom] = _followOnRoots[ai]; |
| 387 | changeRoots = true; |
| 388 | } |
| 389 | } |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 390 | // Change the roots of the targetAtom and its chain to |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 391 | // the current atoms root |
| 392 | if (changeRoots) { |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 393 | setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]); |
| 394 | } |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 395 | } // Is targetAtom root |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 396 | } // kindLayoutBefore |
| Rui Ueyama | ca8ca55 | 2013-05-14 00:41:52 +0000 | [diff] [blame] | 397 | } // Reference |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 398 | } // atom iteration |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 399 | } // end function |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 400 | |
| 401 | |
| 402 | /// Build an ordinal override map by traversing the followon chain, and |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 403 | /// assigning ordinals to each atom, if the atoms have their ordinals |
| 404 | /// already assigned skip the atom and move to the next. This is the |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 405 | /// main map thats used to sort the atoms while comparing two atoms together |
| 406 | void LayoutPass::buildOrdinalOverrideMap(MutableFile::DefinedAtomRange &range) { |
| Michael J. Spencer | d4eb47c | 2013-04-06 00:56:40 +0000 | [diff] [blame] | 407 | ScopedTask task(getDefaultDomain(), "LayoutPass::buildOrdinalOverrideMap"); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 408 | uint64_t index = 0; |
| Rui Ueyama | 0196d106 | 2013-05-14 16:53:59 +0000 | [diff] [blame] | 409 | for (const DefinedAtom *ai : range) { |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 410 | const DefinedAtom *atom = ai; |
| Michael J. Spencer | 1ecf890 | 2013-03-12 00:10:00 +0000 | [diff] [blame] | 411 | if (_ordinalOverrideMap.find(atom) != _ordinalOverrideMap.end()) |
| 412 | continue; |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 413 | AtomToAtomT::iterator start = _followOnRoots.find(atom); |
| 414 | if (start != _followOnRoots.end()) { |
| 415 | for (const DefinedAtom *nextAtom = start->second; nextAtom != NULL; |
| 416 | nextAtom = _followOnNexts[nextAtom]) { |
| 417 | AtomToOrdinalT::iterator pos = _ordinalOverrideMap.find(nextAtom); |
| 418 | if (pos == _ordinalOverrideMap.end()) { |
| 419 | _ordinalOverrideMap[nextAtom] = index++; |
| 420 | } |
| 421 | } |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 426 | // Helper functions to check follow-on graph. |
| 427 | #ifndef NDEBUG |
| 428 | namespace { |
| 429 | typedef llvm::DenseMap<const DefinedAtom *, const DefinedAtom *> AtomToAtomT; |
| 430 | |
| 431 | std::string atomToDebugString(const Atom *atom) { |
| Rui Ueyama | c1800be | 2013-11-05 01:37:40 +0000 | [diff] [blame] | 432 | const DefinedAtom *definedAtom = dyn_cast<DefinedAtom>(atom); |
| Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 433 | std::string str; |
| 434 | llvm::raw_string_ostream s(str); |
| 435 | if (definedAtom->name().empty()) |
| 436 | s << "<anonymous " << definedAtom << ">"; |
| 437 | else |
| 438 | s << definedAtom->name(); |
| 439 | s << " in "; |
| 440 | if (definedAtom->customSectionName().empty()) |
| 441 | s << "<anonymous>"; |
| 442 | else |
| 443 | s << definedAtom->customSectionName(); |
| 444 | s.flush(); |
| 445 | return str; |
| 446 | } |
| 447 | |
| 448 | void showCycleDetectedError(AtomToAtomT &followOnNexts, |
| 449 | const DefinedAtom *atom) { |
| 450 | const DefinedAtom *start = atom; |
| 451 | llvm::dbgs() << "There's a cycle in a follow-on chain!\n"; |
| 452 | do { |
| 453 | llvm::dbgs() << " " << atomToDebugString(atom) << "\n"; |
| 454 | for (const Reference *ref : *atom) { |
| 455 | llvm::dbgs() << " " << ref->kindToString() |
| 456 | << ": " << atomToDebugString(ref->target()) << "\n"; |
| 457 | } |
| 458 | atom = followOnNexts[atom]; |
| 459 | } while (atom != start); |
| Rui Ueyama | 5b274f3 | 2013-07-29 21:50:33 +0000 | [diff] [blame] | 460 | llvm::report_fatal_error("Cycle detected"); |
| Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /// Exit if there's a cycle in a followon chain reachable from the |
| 464 | /// given root atom. Uses the tortoise and hare algorithm to detect a |
| 465 | /// cycle. |
| 466 | void checkNoCycleInFollowonChain(AtomToAtomT &followOnNexts, |
| 467 | const DefinedAtom *root) { |
| 468 | const DefinedAtom *tortoise = root; |
| 469 | const DefinedAtom *hare = followOnNexts[root]; |
| 470 | while (true) { |
| 471 | if (!tortoise || !hare) |
| 472 | return; |
| 473 | if (tortoise == hare) |
| 474 | showCycleDetectedError(followOnNexts, tortoise); |
| 475 | tortoise = followOnNexts[tortoise]; |
| 476 | hare = followOnNexts[followOnNexts[hare]]; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | void checkReachabilityFromRoot(AtomToAtomT &followOnRoots, |
| 481 | const DefinedAtom *atom) { |
| 482 | if (!atom) return; |
| 483 | auto i = followOnRoots.find(atom); |
| 484 | if (i == followOnRoots.end()) { |
| 485 | Twine msg(Twine("Atom <") + atomToDebugString(atom) |
| 486 | + "> has no follow-on root!"); |
| 487 | llvm_unreachable(msg.str().c_str()); |
| 488 | } |
| 489 | const DefinedAtom *ap = i->second; |
| 490 | while (true) { |
| 491 | const DefinedAtom *next = followOnRoots[ap]; |
| 492 | if (!next) { |
| 493 | Twine msg(Twine("Atom <" + atomToDebugString(atom) |
| 494 | + "> is not reachable from its root!")); |
| 495 | llvm_unreachable(msg.str().c_str()); |
| 496 | } |
| 497 | if (next == ap) |
| 498 | return; |
| 499 | ap = next; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void printDefinedAtoms(const MutableFile::DefinedAtomRange &atomRange) { |
| 504 | for (const DefinedAtom *atom : atomRange) { |
| 505 | llvm::dbgs() << " file=" << atom->file().path() |
| 506 | << ", name=" << atom->name() |
| 507 | << ", size=" << atom->size() |
| 508 | << ", type=" << atom->contentType() |
| 509 | << ", ordinal=" << atom->ordinal() |
| 510 | << "\n"; |
| 511 | } |
| 512 | } |
| 513 | } // end anonymous namespace |
| 514 | |
| 515 | /// Verify that the followon chain is sane. Should not be called in |
| 516 | /// release binary. |
| 517 | void LayoutPass::checkFollowonChain(MutableFile::DefinedAtomRange &range) { |
| 518 | ScopedTask task(getDefaultDomain(), "LayoutPass::checkFollowonChain"); |
| 519 | |
| 520 | // Verify that there's no cycle in follow-on chain. |
| 521 | std::set<const DefinedAtom *> roots; |
| 522 | for (const auto &ai : _followOnRoots) |
| 523 | roots.insert(ai.second); |
| 524 | for (const DefinedAtom *root : roots) |
| 525 | checkNoCycleInFollowonChain(_followOnNexts, root); |
| 526 | |
| 527 | // Verify that all the atoms in followOnNexts have references to |
| 528 | // their roots. |
| 529 | for (const auto &ai : _followOnNexts) { |
| 530 | checkReachabilityFromRoot(_followOnRoots, ai.first); |
| 531 | checkReachabilityFromRoot(_followOnRoots, ai.second); |
| 532 | } |
| 533 | } |
| 534 | #endif // #ifndef NDEBUG |
| 535 | |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 536 | /// Perform the actual pass |
| Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 537 | void LayoutPass::perform(std::unique_ptr<MutableFile> &mergedFile) { |
| Michael J. Spencer | bd66d04 | 2013-05-28 18:55:39 +0000 | [diff] [blame] | 538 | ScopedTask task(getDefaultDomain(), "LayoutPass"); |
| Shankar Easwaran | 2bc2492 | 2013-10-29 05:12:14 +0000 | [diff] [blame] | 539 | MutableFile::DefinedAtomRange atomRange = mergedFile->definedAtoms(); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 540 | |
| 541 | // Build follow on tables |
| 542 | buildFollowOnTable(atomRange); |
| 543 | |
| 544 | // Build Ingroup reference table |
| 545 | buildInGroupTable(atomRange); |
| 546 | |
| 547 | // Build preceded by tables |
| 548 | buildPrecededByTable(atomRange); |
| 549 | |
| Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 550 | // Check the structure of followon graph if running in debug mode. |
| 551 | DEBUG(checkFollowonChain(atomRange)); |
| 552 | |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 553 | // Build override maps |
| 554 | buildOrdinalOverrideMap(atomRange); |
| 555 | |
| Rui Ueyama | 9c4f89a | 2013-05-23 01:31:25 +0000 | [diff] [blame] | 556 | DEBUG({ |
| Nick Kledzik | f4fa8c0 | 2013-04-04 20:32:18 +0000 | [diff] [blame] | 557 | llvm::dbgs() << "unsorted atoms:\n"; |
| Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 558 | printDefinedAtoms(atomRange); |
| Nick Kledzik | f4fa8c0 | 2013-04-04 20:32:18 +0000 | [diff] [blame] | 559 | }); |
| Shankar Easwaran | 45a5f93 | 2013-04-29 03:27:57 +0000 | [diff] [blame] | 560 | |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 561 | // sort the atoms |
| Shankar Easwaran | bcf3656 | 2013-10-11 01:50:04 +0000 | [diff] [blame] | 562 | std::sort(atomRange.begin(), atomRange.end(), _compareAtoms); |
| Shankar Easwaran | 45a5f93 | 2013-04-29 03:27:57 +0000 | [diff] [blame] | 563 | |
| Rui Ueyama | 46bf828 | 2013-10-19 03:18:18 +0000 | [diff] [blame] | 564 | DEBUG(checkTransitivity(atomRange.begin(), atomRange.end())); |
| 565 | |
| Rui Ueyama | 9c4f89a | 2013-05-23 01:31:25 +0000 | [diff] [blame] | 566 | DEBUG({ |
| Nick Kledzik | f4fa8c0 | 2013-04-04 20:32:18 +0000 | [diff] [blame] | 567 | llvm::dbgs() << "sorted atoms:\n"; |
| Rui Ueyama | a6b71ca | 2013-06-07 20:18:39 +0000 | [diff] [blame] | 568 | printDefinedAtoms(atomRange); |
| Nick Kledzik | f4fa8c0 | 2013-04-04 20:32:18 +0000 | [diff] [blame] | 569 | }); |
| Shankar Easwaran | 34ab70f | 2013-02-07 20:16:12 +0000 | [diff] [blame] | 570 | } |