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