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