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