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