blob: 22dd1bb73fb1526b65996d14e05fbb7d915ba7c5 [file] [log] [blame]
Shankar Easwaran34ab70f2013-02-07 20:16:12 +00001//===- 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. Spencer7f09a3d2013-02-26 01:35:30 +000011#define DEBUG_TYPE "LayoutPass"
12
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +000013#include <set>
14
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000015#include "lld/Passes/LayoutPass.h"
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +000016#include "lld/Core/Instrumentation.h"
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +000017
18#include "llvm/ADT/Twine.h"
Michael J. Spencer7f09a3d2013-02-26 01:35:30 +000019#include "llvm/Support/Debug.h"
20
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000021using namespace lld;
22
Rui Ueyama52062222013-10-18 03:18:54 +000023#ifndef NDEBUG
Rui Ueyama6a607b62013-10-18 02:56:31 +000024namespace {
25// Return "reason (leftval, rightval)"
26std::string formatReason(StringRef reason, int leftVal, int rightVal) {
27 Twine msg =
28 Twine(reason) + " (" + Twine(leftVal) + ", " + Twine(rightVal) + ")";
29 return std::move(msg.str());
30}
31}
Rui Ueyama52062222013-10-18 03:18:54 +000032#endif // NDEBUG
Rui Ueyama6a607b62013-10-18 02:56:31 +000033
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000034/// The function compares atoms by sorting atoms in the following order
Shankar Easwarand6d1b522013-09-12 15:59:34 +000035/// a) Sorts atoms by Section position preference
36/// b) Sorts atoms by their ordinal overrides
37/// (layout-after/layout-before/ingroup)
38/// c) Sorts atoms by their permissions
39/// d) Sorts atoms by their content
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000040/// e) Sorts atoms on how they appear using File Ordinality
41/// f) Sorts atoms on how they appear within the File
Rui Ueyama6a607b62013-10-18 02:56:31 +000042bool LayoutPass::CompareAtoms::compare(const DefinedAtom *left,
43 const DefinedAtom *right,
44 std::string &reason) const {
45 if (left == right) {
46 reason = "same";
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000047 return false;
Rui Ueyama6a607b62013-10-18 02:56:31 +000048 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000049
Shankar Easwarand8da9892013-05-22 17:41:04 +000050 // Sort by section position preference.
51 DefinedAtom::SectionPosition leftPos = left->sectionPosition();
52 DefinedAtom::SectionPosition rightPos = right->sectionPosition();
53
Shankar Easwarand8da9892013-05-22 17:41:04 +000054 bool leftSpecialPos = (leftPos != DefinedAtom::sectionPositionAny);
55 bool rightSpecialPos = (rightPos != DefinedAtom::sectionPositionAny);
56 if (leftSpecialPos || rightSpecialPos) {
Rui Ueyama6a607b62013-10-18 02:56:31 +000057 if (leftPos != rightPos) {
58 DEBUG(reason = formatReason("sectionPos", (int)leftPos, (int)rightPos));
Shankar Easwarand8da9892013-05-22 17:41:04 +000059 return leftPos < rightPos;
Rui Ueyama6a607b62013-10-18 02:56:31 +000060 }
Shankar Easwarand8da9892013-05-22 17:41:04 +000061 }
62
Shankar Easwaran3c5d2c82013-05-10 16:44:02 +000063 AtomToOrdinalT::const_iterator lPos = _layout._ordinalOverrideMap.find(left);
64 AtomToOrdinalT::const_iterator rPos = _layout._ordinalOverrideMap.find(right);
65 AtomToOrdinalT::const_iterator end = _layout._ordinalOverrideMap.end();
Shankar Easwaranf1b341c2013-09-12 15:43:09 +000066
67 // Sort atoms by their ordinal overrides only if they fall in the same
68 // chain.
Michael J. Spencerc80f88a2013-10-02 23:21:07 +000069 auto leftAtom = _layout._followOnRoots.find(left);
70 auto rightAtom = _layout._followOnRoots.find(right);
Shankar Easwaranf1b341c2013-09-12 15:43:09 +000071
Michael J. Spencerc80f88a2013-10-02 23:21:07 +000072 if (leftAtom != _layout._followOnRoots.end() &&
73 rightAtom != _layout._followOnRoots.end() &&
74 leftAtom->second == rightAtom->second) {
Shankar Easwaranf1b341c2013-09-12 15:43:09 +000075 if ((lPos != end) && (rPos != end)) {
Rui Ueyama6a607b62013-10-18 02:56:31 +000076 DEBUG(reason = formatReason("override", lPos->second, rPos->second));
Shankar Easwaranf1b341c2013-09-12 15:43:09 +000077 return lPos->second < rPos->second;
Shankar Easwaran3c5d2c82013-05-10 16:44:02 +000078 }
79 }
80
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000081 // Sort same permissions together.
82 DefinedAtom::ContentPermissions leftPerms = left->permissions();
83 DefinedAtom::ContentPermissions rightPerms = right->permissions();
Shankar Easwaran8c256852013-03-13 04:05:38 +000084
Rui Ueyama6a607b62013-10-18 02:56:31 +000085 if (leftPerms != rightPerms) {
86 DEBUG(reason =
87 formatReason("contentPerms", (int)leftPerms, (int)rightPerms));
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000088 return leftPerms < rightPerms;
Rui Ueyama6a607b62013-10-18 02:56:31 +000089 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000090
91 // Sort same content types together.
92 DefinedAtom::ContentType leftType = left->contentType();
93 DefinedAtom::ContentType rightType = right->contentType();
Shankar Easwaran8c256852013-03-13 04:05:38 +000094
Rui Ueyama6a607b62013-10-18 02:56:31 +000095 if (leftType != rightType) {
96 DEBUG(reason = formatReason("contentType", (int)leftType, (int)rightType));
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000097 return leftType < rightType;
Rui Ueyama6a607b62013-10-18 02:56:31 +000098 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000099
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000100 // Sort by .o order.
101 const File *leftFile = &left->file();
102 const File *rightFile = &right->file();
Shankar Easwaran8c256852013-03-13 04:05:38 +0000103
Rui Ueyama6a607b62013-10-18 02:56:31 +0000104 if (leftFile != rightFile) {
105 DEBUG(reason = formatReason(".o order", (int)leftFile->ordinal(),
106 (int)rightFile->ordinal()));
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000107 return leftFile->ordinal() < rightFile->ordinal();
Rui Ueyama6a607b62013-10-18 02:56:31 +0000108 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000109
110 // Sort by atom order with .o file.
111 uint64_t leftOrdinal = left->ordinal();
112 uint64_t rightOrdinal = right->ordinal();
Shankar Easwaran8c256852013-03-13 04:05:38 +0000113
Rui Ueyama6a607b62013-10-18 02:56:31 +0000114 if (leftOrdinal != rightOrdinal) {
115 DEBUG(reason = formatReason("ordinal", (int)left->ordinal(),
116 (int)right->ordinal()));
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000117 return leftOrdinal < rightOrdinal;
Rui Ueyama6a607b62013-10-18 02:56:31 +0000118 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000119
Michael J. Spencer7f09a3d2013-02-26 01:35:30 +0000120 DEBUG(llvm::dbgs() << "Unordered\n");
Shankar Easwaranbcf36562013-10-11 01:50:04 +0000121 llvm_unreachable("Atoms with Same Ordinal!");
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000122}
123
Rui Ueyama6a607b62013-10-18 02:56:31 +0000124bool LayoutPass::CompareAtoms::operator()(const DefinedAtom *left,
125 const DefinedAtom *right) const {
126 std::string reason;
127 bool result = compare(left, right, reason);
128 DEBUG({
Rui Ueyama4050b202013-10-18 03:18:52 +0000129 StringRef comp = result ? "<" : ">=";
Rui Ueyama6a607b62013-10-18 02:56:31 +0000130 llvm::dbgs() << "Layout: '" << left->name() << "' " << comp << " '"
131 << right->name() << "' (" << reason << ")\n";
132 });
133 return result;
134}
135
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000136// Returns the atom immediately followed by the given atom in the followon
137// chain.
138const DefinedAtom *LayoutPass::findAtomFollowedBy(
139 const DefinedAtom *targetAtom) {
140 // Start from the beginning of the chain and follow the chain until
141 // we find the targetChain.
142 const DefinedAtom *atom = _followOnRoots[targetAtom];
143 while (true) {
144 const DefinedAtom *prevAtom = atom;
145 AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom);
146 // The target atom must be in the chain of its root.
147 assert(targetFollowOnAtomsIter != _followOnNexts.end());
148 atom = targetFollowOnAtomsIter->second;
149 if (atom == targetAtom)
150 return prevAtom;
151 }
152}
153
154// Check if all the atoms followed by the given target atom are of size zero.
155// When this method is called, an atom being added is not of size zero and
156// will be added to the head of the followon chain. All the atoms between the
157// atom and the targetAtom (specified by layout-after) need to be of size zero
158// in this case. Otherwise the desired layout is impossible.
159bool LayoutPass::checkAllPrevAtomsZeroSize(const DefinedAtom *targetAtom) {
160 const DefinedAtom *atom = _followOnRoots[targetAtom];
161 while (true) {
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000162 if (atom == targetAtom)
163 return true;
Rui Ueyama0196d1062013-05-14 16:53:59 +0000164 if (atom->size() != 0)
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000165 // TODO: print warning that an impossible layout is being desired by the
166 // user.
167 return false;
Rui Ueyama5ec6d1a2013-05-14 01:51:56 +0000168 AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom);
169 // The target atom must be in the chain of its root.
170 assert(targetFollowOnAtomsIter != _followOnNexts.end());
171 atom = targetFollowOnAtomsIter->second;
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000172 }
173}
174
175// Set the root of all atoms in targetAtom's chain to the given root.
176void LayoutPass::setChainRoot(const DefinedAtom *targetAtom,
177 const DefinedAtom *root) {
178 // Walk through the followon chain and override each node's root.
179 while (true) {
180 _followOnRoots[targetAtom] = root;
181 AtomToAtomT::iterator targetFollowOnAtomsIter =
182 _followOnNexts.find(targetAtom);
183 if (targetFollowOnAtomsIter == _followOnNexts.end())
184 return;
185 targetAtom = targetFollowOnAtomsIter->second;
186 }
187}
188
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000189/// This pass builds the followon tables described by two DenseMaps
190/// followOnRoots and followonNexts.
191/// The followOnRoots map contains a mapping of a DefinedAtom to its root
192/// The followOnNexts map contains a mapping of what DefinedAtom follows the
193/// current Atom
194/// The algorithm follows a very simple approach
195/// a) If the atom is first seen, then make that as the root atom
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000196/// b) The targetAtom which this Atom contains, has the root thats set to the
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000197/// root of the current atom
198/// c) If the targetAtom is part of a different tree and the root of the
199/// targetAtom is itself, Chain all the atoms that are contained in the tree
200/// to the current Tree
201/// d) If the targetAtom is part of a different chain and the root of the
202/// targetAtom until the targetAtom has all atoms of size 0, then chain the
203/// targetAtoms and its tree to the current chain
204void LayoutPass::buildFollowOnTable(MutableFile::DefinedAtomRange &range) {
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +0000205 ScopedTask task(getDefaultDomain(), "LayoutPass::buildFollowOnTable");
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000206 // Set the initial size of the followon and the followonNext hash to the
207 // number of atoms that we have.
Shankar Easwaran45a5f932013-04-29 03:27:57 +0000208 _followOnRoots.resize(range.size());
209 _followOnNexts.resize(range.size());
Rui Ueyama0196d1062013-05-14 16:53:59 +0000210 for (const DefinedAtom *ai : range) {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000211 for (const Reference *r : *ai) {
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000212 if (r->kind() != lld::Reference::kindLayoutAfter)
213 continue;
214 const DefinedAtom *targetAtom = llvm::dyn_cast<DefinedAtom>(r->target());
215 _followOnNexts[ai] = targetAtom;
216
217 // If we find a followon for the first time, lets make that atom as the
218 // root atom.
219 if (_followOnRoots.count(ai) == 0)
220 _followOnRoots[ai] = ai;
221
222 auto iter = _followOnRoots.find(targetAtom);
223 if (iter == _followOnRoots.end()) {
224 // If the targetAtom is not a root of any chain, lets make the root of
225 // the targetAtom to the root of the current chain.
226 _followOnRoots[targetAtom] = _followOnRoots[ai];
227 } else if (iter->second == targetAtom) {
228 // If the targetAtom is the root of a chain, the chain becomes part of
229 // the current chain. Rewrite the subchain's root to the current
230 // chain's root.
231 setChainRoot(targetAtom, _followOnRoots[ai]);
232 } else {
233 // The targetAtom is already a part of a chain. If the current atom is
234 // of size zero, we can insert it in the middle of the chain just
235 // before the target atom, while not breaking other atom's followon
236 // relationships. If it's not, we can only insert the current atom at
237 // the beginning of the chain. All the atoms followed by the target
238 // atom must be of size zero in that case to satisfy the followon
239 // relationships.
Rui Ueyama0196d1062013-05-14 16:53:59 +0000240 size_t currentAtomSize = ai->size();
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000241 if (currentAtomSize == 0) {
242 const DefinedAtom *targetPrevAtom = findAtomFollowedBy(targetAtom);
243 _followOnNexts[targetPrevAtom] = ai;
244 _followOnRoots[ai] = _followOnRoots[targetPrevAtom];
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000245 } else {
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000246 if (!checkAllPrevAtomsZeroSize(targetAtom))
247 break;
248 _followOnNexts[ai] = _followOnRoots[targetAtom];
249 setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]);
250 }
251 }
252 }
253 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000254}
255
256/// This pass builds the followon tables using InGroup relationships
257/// The algorithm follows a very simple approach
258/// a) If the rootAtom is not part of any root, create a new root with the
259/// as the head
260/// b) If the current Atom root is not found, then make the current atoms root
261/// point to the rootAtom
262/// c) If the root of the current Atom is itself a root of some other tree
263/// make all the atoms in the chain point to the ingroup reference
264/// d) Check to see if the current atom is part of the chain from the rootAtom
265/// if not add the atom to the chain, so that the current atom is part of the
266/// the chain where the rootAtom is in
267void LayoutPass::buildInGroupTable(MutableFile::DefinedAtomRange &range) {
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +0000268 ScopedTask task(getDefaultDomain(), "LayoutPass::buildInGroupTable");
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000269 // This table would convert precededby references to follow on
270 // references so that we have only one table
Rui Ueyama0196d1062013-05-14 16:53:59 +0000271 for (const DefinedAtom *ai : range) {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000272 for (const Reference *r : *ai) {
273 if (r->kind() == lld::Reference::kindInGroup) {
274 const DefinedAtom *rootAtom = llvm::dyn_cast<DefinedAtom>(r->target());
275 // If the root atom is not part of any root
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000276 // create a new root
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000277 if (_followOnRoots.count(rootAtom) == 0) {
278 _followOnRoots[rootAtom] = rootAtom;
279 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000280 // If the current Atom has not been seen yet and there is no root
281 // that has been set, set the root of the atom to the targetAtom
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000282 // as the targetAtom points to the ingroup root
283 auto iter = _followOnRoots.find(ai);
284 if (iter == _followOnRoots.end()) {
285 _followOnRoots[ai] = rootAtom;
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000286 } else if (iter->second == ai) {
287 if (iter->second != rootAtom)
288 setChainRoot(iter->second, rootAtom);
289 } else {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000290 // TODO : Flag an error that the root of the tree
291 // is different, Here is an example
292 // Say there are atoms
293 // chain 1 : a->b->c
294 // chain 2 : d->e->f
295 // and e,f have their ingroup reference as a
296 // this could happen only if the root of e,f that is d
297 // has root as 'a'
298 continue;
299 }
300
301 // Check if the current atom is part of the chain
302 bool isAtomInChain = false;
303 const DefinedAtom *lastAtom = rootAtom;
304 while (true) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000305 AtomToAtomT::iterator followOnAtomsIter =
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000306 _followOnNexts.find(lastAtom);
307 if (followOnAtomsIter != _followOnNexts.end()) {
308 lastAtom = followOnAtomsIter->second;
309 if (lastAtom == ai) {
310 isAtomInChain = true;
311 break;
312 }
313 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000314 else
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000315 break;
316 } // findAtomInChain
317
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000318 if (!isAtomInChain)
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000319 _followOnNexts[lastAtom] = ai;
320 }
321 }
322 }
323}
324
325/// This pass builds the followon tables using Preceded By relationships
326/// The algorithm follows a very simple approach
327/// a) If the targetAtom is not part of any root and the current atom is not
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000328/// part of any root, create a chain with the current atom as root and
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000329/// the targetAtom as following the current atom
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000330/// b) Chain the targetAtom to the current Atom if the targetAtom is not part
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000331/// of any chain and the currentAtom has no followOn's
332/// c) If the targetAtom is part of a different tree and the root of the
333/// targetAtom is itself, and if the current atom is not part of any root
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000334/// chain all the atoms together
335/// d) If the current atom has no followon and the root of the targetAtom is
336/// not equal to the root of the current atom(the targetAtom is not in the
337/// same chain), chain all the atoms that are lead by the targetAtom into
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000338/// the current chain
339void LayoutPass::buildPrecededByTable(MutableFile::DefinedAtomRange &range) {
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +0000340 ScopedTask task(getDefaultDomain(), "LayoutPass::buildPrecededByTable");
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000341 // This table would convert precededby references to follow on
342 // references so that we have only one table
Rui Ueyama0196d1062013-05-14 16:53:59 +0000343 for (const DefinedAtom *ai : range) {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000344 for (const Reference *r : *ai) {
345 if (r->kind() == lld::Reference::kindLayoutBefore) {
346 const DefinedAtom *targetAtom = llvm::dyn_cast<DefinedAtom>(r->target());
347 // Is the targetAtom not chained
348 if (_followOnRoots.count(targetAtom) == 0) {
349 // Is the current atom not part of any root ?
350 if (_followOnRoots.count(ai) == 0) {
351 _followOnRoots[ai] = ai;
352 _followOnNexts[ai] = targetAtom;
353 _followOnRoots[targetAtom] = _followOnRoots[ai];
354 } else if (_followOnNexts.count(ai) == 0) {
355 // Chain the targetAtom to the current Atom
356 // if the currentAtom has no followon references
357 _followOnNexts[ai] = targetAtom;
358 _followOnRoots[targetAtom] = _followOnRoots[ai];
359 }
360 } else if (_followOnRoots.find(targetAtom)->second == targetAtom) {
361 // Is the targetAtom in chain with the targetAtom as the root ?
362 bool changeRoots = false;
363 if (_followOnRoots.count(ai) == 0) {
364 _followOnRoots[ai] = ai;
365 _followOnNexts[ai] = targetAtom;
366 _followOnRoots[targetAtom] = _followOnRoots[ai];
367 changeRoots = true;
368 } else if (_followOnNexts.count(ai) == 0) {
369 // Chain the targetAtom to the current Atom
370 // if the currentAtom has no followon references
371 if (_followOnRoots[ai] != _followOnRoots[targetAtom]) {
372 _followOnNexts[ai] = targetAtom;
373 _followOnRoots[targetAtom] = _followOnRoots[ai];
374 changeRoots = true;
375 }
376 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000377 // Change the roots of the targetAtom and its chain to
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000378 // the current atoms root
379 if (changeRoots) {
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000380 setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]);
381 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000382 } // Is targetAtom root
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000383 } // kindLayoutBefore
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000384 } // Reference
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000385 } // atom iteration
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000386} // end function
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000387
388
389/// Build an ordinal override map by traversing the followon chain, and
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000390/// assigning ordinals to each atom, if the atoms have their ordinals
391/// already assigned skip the atom and move to the next. This is the
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000392/// main map thats used to sort the atoms while comparing two atoms together
393void LayoutPass::buildOrdinalOverrideMap(MutableFile::DefinedAtomRange &range) {
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +0000394 ScopedTask task(getDefaultDomain(), "LayoutPass::buildOrdinalOverrideMap");
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000395 uint64_t index = 0;
Rui Ueyama0196d1062013-05-14 16:53:59 +0000396 for (const DefinedAtom *ai : range) {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000397 const DefinedAtom *atom = ai;
Michael J. Spencer1ecf8902013-03-12 00:10:00 +0000398 if (_ordinalOverrideMap.find(atom) != _ordinalOverrideMap.end())
399 continue;
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000400 AtomToAtomT::iterator start = _followOnRoots.find(atom);
401 if (start != _followOnRoots.end()) {
402 for (const DefinedAtom *nextAtom = start->second; nextAtom != NULL;
403 nextAtom = _followOnNexts[nextAtom]) {
404 AtomToOrdinalT::iterator pos = _ordinalOverrideMap.find(nextAtom);
405 if (pos == _ordinalOverrideMap.end()) {
406 _ordinalOverrideMap[nextAtom] = index++;
407 }
408 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000409 }
410 }
411}
412
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000413// Helper functions to check follow-on graph.
414#ifndef NDEBUG
415namespace {
416typedef llvm::DenseMap<const DefinedAtom *, const DefinedAtom *> AtomToAtomT;
417
418std::string atomToDebugString(const Atom *atom) {
419 const DefinedAtom *definedAtom = llvm::dyn_cast<DefinedAtom>(atom);
420 std::string str;
421 llvm::raw_string_ostream s(str);
422 if (definedAtom->name().empty())
423 s << "<anonymous " << definedAtom << ">";
424 else
425 s << definedAtom->name();
426 s << " in ";
427 if (definedAtom->customSectionName().empty())
428 s << "<anonymous>";
429 else
430 s << definedAtom->customSectionName();
431 s.flush();
432 return str;
433}
434
435void showCycleDetectedError(AtomToAtomT &followOnNexts,
436 const DefinedAtom *atom) {
437 const DefinedAtom *start = atom;
438 llvm::dbgs() << "There's a cycle in a follow-on chain!\n";
439 do {
440 llvm::dbgs() << " " << atomToDebugString(atom) << "\n";
441 for (const Reference *ref : *atom) {
442 llvm::dbgs() << " " << ref->kindToString()
443 << ": " << atomToDebugString(ref->target()) << "\n";
444 }
445 atom = followOnNexts[atom];
446 } while (atom != start);
Rui Ueyama5b274f32013-07-29 21:50:33 +0000447 llvm::report_fatal_error("Cycle detected");
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000448}
449
450/// Exit if there's a cycle in a followon chain reachable from the
451/// given root atom. Uses the tortoise and hare algorithm to detect a
452/// cycle.
453void checkNoCycleInFollowonChain(AtomToAtomT &followOnNexts,
454 const DefinedAtom *root) {
455 const DefinedAtom *tortoise = root;
456 const DefinedAtom *hare = followOnNexts[root];
457 while (true) {
458 if (!tortoise || !hare)
459 return;
460 if (tortoise == hare)
461 showCycleDetectedError(followOnNexts, tortoise);
462 tortoise = followOnNexts[tortoise];
463 hare = followOnNexts[followOnNexts[hare]];
464 }
465}
466
467void checkReachabilityFromRoot(AtomToAtomT &followOnRoots,
468 const DefinedAtom *atom) {
469 if (!atom) return;
470 auto i = followOnRoots.find(atom);
471 if (i == followOnRoots.end()) {
472 Twine msg(Twine("Atom <") + atomToDebugString(atom)
473 + "> has no follow-on root!");
474 llvm_unreachable(msg.str().c_str());
475 }
476 const DefinedAtom *ap = i->second;
477 while (true) {
478 const DefinedAtom *next = followOnRoots[ap];
479 if (!next) {
480 Twine msg(Twine("Atom <" + atomToDebugString(atom)
481 + "> is not reachable from its root!"));
482 llvm_unreachable(msg.str().c_str());
483 }
484 if (next == ap)
485 return;
486 ap = next;
487 }
488}
489
490void printDefinedAtoms(const MutableFile::DefinedAtomRange &atomRange) {
491 for (const DefinedAtom *atom : atomRange) {
492 llvm::dbgs() << " file=" << atom->file().path()
493 << ", name=" << atom->name()
494 << ", size=" << atom->size()
495 << ", type=" << atom->contentType()
496 << ", ordinal=" << atom->ordinal()
497 << "\n";
498 }
499}
500} // end anonymous namespace
501
502/// Verify that the followon chain is sane. Should not be called in
503/// release binary.
504void LayoutPass::checkFollowonChain(MutableFile::DefinedAtomRange &range) {
505 ScopedTask task(getDefaultDomain(), "LayoutPass::checkFollowonChain");
506
507 // Verify that there's no cycle in follow-on chain.
508 std::set<const DefinedAtom *> roots;
509 for (const auto &ai : _followOnRoots)
510 roots.insert(ai.second);
511 for (const DefinedAtom *root : roots)
512 checkNoCycleInFollowonChain(_followOnNexts, root);
513
514 // Verify that all the atoms in followOnNexts have references to
515 // their roots.
516 for (const auto &ai : _followOnNexts) {
517 checkReachabilityFromRoot(_followOnRoots, ai.first);
518 checkReachabilityFromRoot(_followOnRoots, ai.second);
519 }
520}
521#endif // #ifndef NDEBUG
522
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000523/// Perform the actual pass
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000524void LayoutPass::perform(MutableFile &mergedFile) {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000525 ScopedTask task(getDefaultDomain(), "LayoutPass");
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000526 MutableFile::DefinedAtomRange atomRange = mergedFile.definedAtoms();
527
528 // Build follow on tables
529 buildFollowOnTable(atomRange);
530
531 // Build Ingroup reference table
532 buildInGroupTable(atomRange);
533
534 // Build preceded by tables
535 buildPrecededByTable(atomRange);
536
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000537 // Check the structure of followon graph if running in debug mode.
538 DEBUG(checkFollowonChain(atomRange));
539
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000540 // Build override maps
541 buildOrdinalOverrideMap(atomRange);
542
Rui Ueyama9c4f89a2013-05-23 01:31:25 +0000543 DEBUG({
Nick Kledzikf4fa8c02013-04-04 20:32:18 +0000544 llvm::dbgs() << "unsorted atoms:\n";
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000545 printDefinedAtoms(atomRange);
Nick Kledzikf4fa8c02013-04-04 20:32:18 +0000546 });
Shankar Easwaran45a5f932013-04-29 03:27:57 +0000547
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000548 // sort the atoms
Shankar Easwaranbcf36562013-10-11 01:50:04 +0000549 std::sort(atomRange.begin(), atomRange.end(), _compareAtoms);
Shankar Easwaran45a5f932013-04-29 03:27:57 +0000550
Rui Ueyama9c4f89a2013-05-23 01:31:25 +0000551 DEBUG({
Nick Kledzikf4fa8c02013-04-04 20:32:18 +0000552 llvm::dbgs() << "sorted atoms:\n";
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000553 printDefinedAtoms(atomRange);
Nick Kledzikf4fa8c02013-04-04 20:32:18 +0000554 });
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000555}