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