blob: 7f709bd33816fdcaf28ee72807dc8b70d370fcc4 [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 Ueyama6a607b62013-10-18 02:56:31 +000023namespace {
24// Return "reason (leftval, rightval)"
25std::string formatReason(StringRef reason, int leftVal, int rightVal) {
26 Twine msg =
27 Twine(reason) + " (" + Twine(leftVal) + ", " + Twine(rightVal) + ")";
28 return std::move(msg.str());
29}
30}
31
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000032/// The function compares atoms by sorting atoms in the following order
Shankar Easwarand6d1b522013-09-12 15:59:34 +000033/// a) Sorts atoms by Section position preference
34/// b) Sorts atoms by their ordinal overrides
35/// (layout-after/layout-before/ingroup)
36/// c) Sorts atoms by their permissions
37/// d) Sorts atoms by their content
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000038/// e) Sorts atoms on how they appear using File Ordinality
39/// f) Sorts atoms on how they appear within the File
Rui Ueyama6a607b62013-10-18 02:56:31 +000040bool LayoutPass::CompareAtoms::compare(const DefinedAtom *left,
41 const DefinedAtom *right,
42 std::string &reason) const {
43 if (left == right) {
44 reason = "same";
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000045 return false;
Rui Ueyama6a607b62013-10-18 02:56:31 +000046 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000047
Shankar Easwarand8da9892013-05-22 17:41:04 +000048 // Sort by section position preference.
49 DefinedAtom::SectionPosition leftPos = left->sectionPosition();
50 DefinedAtom::SectionPosition rightPos = right->sectionPosition();
51
Shankar Easwarand8da9892013-05-22 17:41:04 +000052 bool leftSpecialPos = (leftPos != DefinedAtom::sectionPositionAny);
53 bool rightSpecialPos = (rightPos != DefinedAtom::sectionPositionAny);
54 if (leftSpecialPos || rightSpecialPos) {
Rui Ueyama6a607b62013-10-18 02:56:31 +000055 if (leftPos != rightPos) {
56 DEBUG(reason = formatReason("sectionPos", (int)leftPos, (int)rightPos));
Shankar Easwarand8da9892013-05-22 17:41:04 +000057 return leftPos < rightPos;
Rui Ueyama6a607b62013-10-18 02:56:31 +000058 }
Shankar Easwarand8da9892013-05-22 17:41:04 +000059 }
60
Shankar Easwaran3c5d2c82013-05-10 16:44:02 +000061 AtomToOrdinalT::const_iterator lPos = _layout._ordinalOverrideMap.find(left);
62 AtomToOrdinalT::const_iterator rPos = _layout._ordinalOverrideMap.find(right);
63 AtomToOrdinalT::const_iterator end = _layout._ordinalOverrideMap.end();
Shankar Easwaranf1b341c2013-09-12 15:43:09 +000064
65 // Sort atoms by their ordinal overrides only if they fall in the same
66 // chain.
Michael J. Spencerc80f88a2013-10-02 23:21:07 +000067 auto leftAtom = _layout._followOnRoots.find(left);
68 auto rightAtom = _layout._followOnRoots.find(right);
Shankar Easwaranf1b341c2013-09-12 15:43:09 +000069
Michael J. Spencerc80f88a2013-10-02 23:21:07 +000070 if (leftAtom != _layout._followOnRoots.end() &&
71 rightAtom != _layout._followOnRoots.end() &&
72 leftAtom->second == rightAtom->second) {
Shankar Easwaranf1b341c2013-09-12 15:43:09 +000073 if ((lPos != end) && (rPos != end)) {
Rui Ueyama6a607b62013-10-18 02:56:31 +000074 DEBUG(reason = formatReason("override", lPos->second, rPos->second));
Shankar Easwaranf1b341c2013-09-12 15:43:09 +000075 return lPos->second < rPos->second;
Shankar Easwaran3c5d2c82013-05-10 16:44:02 +000076 }
77 }
78
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000079 // Sort same permissions together.
80 DefinedAtom::ContentPermissions leftPerms = left->permissions();
81 DefinedAtom::ContentPermissions rightPerms = right->permissions();
Shankar Easwaran8c256852013-03-13 04:05:38 +000082
Rui Ueyama6a607b62013-10-18 02:56:31 +000083 if (leftPerms != rightPerms) {
84 DEBUG(reason =
85 formatReason("contentPerms", (int)leftPerms, (int)rightPerms));
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000086 return leftPerms < rightPerms;
Rui Ueyama6a607b62013-10-18 02:56:31 +000087 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000088
89 // Sort same content types together.
90 DefinedAtom::ContentType leftType = left->contentType();
91 DefinedAtom::ContentType rightType = right->contentType();
Shankar Easwaran8c256852013-03-13 04:05:38 +000092
Rui Ueyama6a607b62013-10-18 02:56:31 +000093 if (leftType != rightType) {
94 DEBUG(reason = formatReason("contentType", (int)leftType, (int)rightType));
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000095 return leftType < rightType;
Rui Ueyama6a607b62013-10-18 02:56:31 +000096 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000097
Shankar Easwaran34ab70f2013-02-07 20:16:12 +000098 // Sort by .o order.
99 const File *leftFile = &left->file();
100 const File *rightFile = &right->file();
Shankar Easwaran8c256852013-03-13 04:05:38 +0000101
Rui Ueyama6a607b62013-10-18 02:56:31 +0000102 if (leftFile != rightFile) {
103 DEBUG(reason = formatReason(".o order", (int)leftFile->ordinal(),
104 (int)rightFile->ordinal()));
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000105 return leftFile->ordinal() < rightFile->ordinal();
Rui Ueyama6a607b62013-10-18 02:56:31 +0000106 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000107
108 // Sort by atom order with .o file.
109 uint64_t leftOrdinal = left->ordinal();
110 uint64_t rightOrdinal = right->ordinal();
Shankar Easwaran8c256852013-03-13 04:05:38 +0000111
Rui Ueyama6a607b62013-10-18 02:56:31 +0000112 if (leftOrdinal != rightOrdinal) {
113 DEBUG(reason = formatReason("ordinal", (int)left->ordinal(),
114 (int)right->ordinal()));
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000115 return leftOrdinal < rightOrdinal;
Rui Ueyama6a607b62013-10-18 02:56:31 +0000116 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000117
Michael J. Spencer7f09a3d2013-02-26 01:35:30 +0000118 DEBUG(llvm::dbgs() << "Unordered\n");
Shankar Easwaranbcf36562013-10-11 01:50:04 +0000119 llvm_unreachable("Atoms with Same Ordinal!");
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000120}
121
Rui Ueyama6a607b62013-10-18 02:56:31 +0000122bool LayoutPass::CompareAtoms::operator()(const DefinedAtom *left,
123 const DefinedAtom *right) const {
124 std::string reason;
125 bool result = compare(left, right, reason);
126 DEBUG({
127 StringRef comp = result ? "<" : ">";
128 llvm::dbgs() << "Layout: '" << left->name() << "' " << comp << " '"
129 << right->name() << "' (" << reason << ")\n";
130 });
131 return result;
132}
133
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000134// Returns the atom immediately followed by the given atom in the followon
135// chain.
136const DefinedAtom *LayoutPass::findAtomFollowedBy(
137 const DefinedAtom *targetAtom) {
138 // Start from the beginning of the chain and follow the chain until
139 // we find the targetChain.
140 const DefinedAtom *atom = _followOnRoots[targetAtom];
141 while (true) {
142 const DefinedAtom *prevAtom = atom;
143 AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom);
144 // The target atom must be in the chain of its root.
145 assert(targetFollowOnAtomsIter != _followOnNexts.end());
146 atom = targetFollowOnAtomsIter->second;
147 if (atom == targetAtom)
148 return prevAtom;
149 }
150}
151
152// Check if all the atoms followed by the given target atom are of size zero.
153// When this method is called, an atom being added is not of size zero and
154// will be added to the head of the followon chain. All the atoms between the
155// atom and the targetAtom (specified by layout-after) need to be of size zero
156// in this case. Otherwise the desired layout is impossible.
157bool LayoutPass::checkAllPrevAtomsZeroSize(const DefinedAtom *targetAtom) {
158 const DefinedAtom *atom = _followOnRoots[targetAtom];
159 while (true) {
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000160 if (atom == targetAtom)
161 return true;
Rui Ueyama0196d1062013-05-14 16:53:59 +0000162 if (atom->size() != 0)
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000163 // TODO: print warning that an impossible layout is being desired by the
164 // user.
165 return false;
Rui Ueyama5ec6d1a2013-05-14 01:51:56 +0000166 AtomToAtomT::iterator targetFollowOnAtomsIter = _followOnNexts.find(atom);
167 // The target atom must be in the chain of its root.
168 assert(targetFollowOnAtomsIter != _followOnNexts.end());
169 atom = targetFollowOnAtomsIter->second;
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000170 }
171}
172
173// Set the root of all atoms in targetAtom's chain to the given root.
174void LayoutPass::setChainRoot(const DefinedAtom *targetAtom,
175 const DefinedAtom *root) {
176 // Walk through the followon chain and override each node's root.
177 while (true) {
178 _followOnRoots[targetAtom] = root;
179 AtomToAtomT::iterator targetFollowOnAtomsIter =
180 _followOnNexts.find(targetAtom);
181 if (targetFollowOnAtomsIter == _followOnNexts.end())
182 return;
183 targetAtom = targetFollowOnAtomsIter->second;
184 }
185}
186
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000187/// This pass builds the followon tables described by two DenseMaps
188/// followOnRoots and followonNexts.
189/// The followOnRoots map contains a mapping of a DefinedAtom to its root
190/// The followOnNexts map contains a mapping of what DefinedAtom follows the
191/// current Atom
192/// The algorithm follows a very simple approach
193/// a) If the atom is first seen, then make that as the root atom
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000194/// b) The targetAtom which this Atom contains, has the root thats set to the
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000195/// root of the current atom
196/// c) If the targetAtom is part of a different tree and the root of the
197/// targetAtom is itself, Chain all the atoms that are contained in the tree
198/// to the current Tree
199/// d) If the targetAtom is part of a different chain and the root of the
200/// targetAtom until the targetAtom has all atoms of size 0, then chain the
201/// targetAtoms and its tree to the current chain
202void LayoutPass::buildFollowOnTable(MutableFile::DefinedAtomRange &range) {
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +0000203 ScopedTask task(getDefaultDomain(), "LayoutPass::buildFollowOnTable");
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000204 // Set the initial size of the followon and the followonNext hash to the
205 // number of atoms that we have.
Shankar Easwaran45a5f932013-04-29 03:27:57 +0000206 _followOnRoots.resize(range.size());
207 _followOnNexts.resize(range.size());
Rui Ueyama0196d1062013-05-14 16:53:59 +0000208 for (const DefinedAtom *ai : range) {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000209 for (const Reference *r : *ai) {
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000210 if (r->kind() != lld::Reference::kindLayoutAfter)
211 continue;
212 const DefinedAtom *targetAtom = llvm::dyn_cast<DefinedAtom>(r->target());
213 _followOnNexts[ai] = targetAtom;
214
215 // If we find a followon for the first time, lets make that atom as the
216 // root atom.
217 if (_followOnRoots.count(ai) == 0)
218 _followOnRoots[ai] = ai;
219
220 auto iter = _followOnRoots.find(targetAtom);
221 if (iter == _followOnRoots.end()) {
222 // If the targetAtom is not a root of any chain, lets make the root of
223 // the targetAtom to the root of the current chain.
224 _followOnRoots[targetAtom] = _followOnRoots[ai];
225 } else if (iter->second == targetAtom) {
226 // If the targetAtom is the root of a chain, the chain becomes part of
227 // the current chain. Rewrite the subchain's root to the current
228 // chain's root.
229 setChainRoot(targetAtom, _followOnRoots[ai]);
230 } else {
231 // The targetAtom is already a part of a chain. If the current atom is
232 // of size zero, we can insert it in the middle of the chain just
233 // before the target atom, while not breaking other atom's followon
234 // relationships. If it's not, we can only insert the current atom at
235 // the beginning of the chain. All the atoms followed by the target
236 // atom must be of size zero in that case to satisfy the followon
237 // relationships.
Rui Ueyama0196d1062013-05-14 16:53:59 +0000238 size_t currentAtomSize = ai->size();
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000239 if (currentAtomSize == 0) {
240 const DefinedAtom *targetPrevAtom = findAtomFollowedBy(targetAtom);
241 _followOnNexts[targetPrevAtom] = ai;
242 _followOnRoots[ai] = _followOnRoots[targetPrevAtom];
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000243 } else {
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000244 if (!checkAllPrevAtomsZeroSize(targetAtom))
245 break;
246 _followOnNexts[ai] = _followOnRoots[targetAtom];
247 setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]);
248 }
249 }
250 }
251 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000252}
253
254/// This pass builds the followon tables using InGroup relationships
255/// The algorithm follows a very simple approach
256/// a) If the rootAtom is not part of any root, create a new root with the
257/// as the head
258/// b) If the current Atom root is not found, then make the current atoms root
259/// point to the rootAtom
260/// c) If the root of the current Atom is itself a root of some other tree
261/// make all the atoms in the chain point to the ingroup reference
262/// d) Check to see if the current atom is part of the chain from the rootAtom
263/// if not add the atom to the chain, so that the current atom is part of the
264/// the chain where the rootAtom is in
265void LayoutPass::buildInGroupTable(MutableFile::DefinedAtomRange &range) {
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +0000266 ScopedTask task(getDefaultDomain(), "LayoutPass::buildInGroupTable");
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000267 // This table would convert precededby references to follow on
268 // references so that we have only one table
Rui Ueyama0196d1062013-05-14 16:53:59 +0000269 for (const DefinedAtom *ai : range) {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000270 for (const Reference *r : *ai) {
271 if (r->kind() == lld::Reference::kindInGroup) {
272 const DefinedAtom *rootAtom = llvm::dyn_cast<DefinedAtom>(r->target());
273 // If the root atom is not part of any root
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000274 // create a new root
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000275 if (_followOnRoots.count(rootAtom) == 0) {
276 _followOnRoots[rootAtom] = rootAtom;
277 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000278 // If the current Atom has not been seen yet and there is no root
279 // that has been set, set the root of the atom to the targetAtom
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000280 // as the targetAtom points to the ingroup root
281 auto iter = _followOnRoots.find(ai);
282 if (iter == _followOnRoots.end()) {
283 _followOnRoots[ai] = rootAtom;
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000284 } else if (iter->second == ai) {
285 if (iter->second != rootAtom)
286 setChainRoot(iter->second, rootAtom);
287 } else {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000288 // TODO : Flag an error that the root of the tree
289 // is different, Here is an example
290 // Say there are atoms
291 // chain 1 : a->b->c
292 // chain 2 : d->e->f
293 // and e,f have their ingroup reference as a
294 // this could happen only if the root of e,f that is d
295 // has root as 'a'
296 continue;
297 }
298
299 // Check if the current atom is part of the chain
300 bool isAtomInChain = false;
301 const DefinedAtom *lastAtom = rootAtom;
302 while (true) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000303 AtomToAtomT::iterator followOnAtomsIter =
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000304 _followOnNexts.find(lastAtom);
305 if (followOnAtomsIter != _followOnNexts.end()) {
306 lastAtom = followOnAtomsIter->second;
307 if (lastAtom == ai) {
308 isAtomInChain = true;
309 break;
310 }
311 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000312 else
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000313 break;
314 } // findAtomInChain
315
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000316 if (!isAtomInChain)
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000317 _followOnNexts[lastAtom] = ai;
318 }
319 }
320 }
321}
322
323/// This pass builds the followon tables using Preceded By relationships
324/// The algorithm follows a very simple approach
325/// a) If the targetAtom is not part of any root and the current atom is not
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000326/// part of any root, create a chain with the current atom as root and
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000327/// the targetAtom as following the current atom
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000328/// b) Chain the targetAtom to the current Atom if the targetAtom is not part
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000329/// of any chain and the currentAtom has no followOn's
330/// c) If the targetAtom is part of a different tree and the root of the
331/// targetAtom is itself, and if the current atom is not part of any root
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000332/// chain all the atoms together
333/// d) If the current atom has no followon and the root of the targetAtom is
334/// not equal to the root of the current atom(the targetAtom is not in the
335/// same chain), chain all the atoms that are lead by the targetAtom into
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000336/// the current chain
337void LayoutPass::buildPrecededByTable(MutableFile::DefinedAtomRange &range) {
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +0000338 ScopedTask task(getDefaultDomain(), "LayoutPass::buildPrecededByTable");
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000339 // This table would convert precededby references to follow on
340 // references so that we have only one table
Rui Ueyama0196d1062013-05-14 16:53:59 +0000341 for (const DefinedAtom *ai : range) {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000342 for (const Reference *r : *ai) {
343 if (r->kind() == lld::Reference::kindLayoutBefore) {
344 const DefinedAtom *targetAtom = llvm::dyn_cast<DefinedAtom>(r->target());
345 // Is the targetAtom not chained
346 if (_followOnRoots.count(targetAtom) == 0) {
347 // Is the current atom not part of any root ?
348 if (_followOnRoots.count(ai) == 0) {
349 _followOnRoots[ai] = ai;
350 _followOnNexts[ai] = targetAtom;
351 _followOnRoots[targetAtom] = _followOnRoots[ai];
352 } else if (_followOnNexts.count(ai) == 0) {
353 // Chain the targetAtom to the current Atom
354 // if the currentAtom has no followon references
355 _followOnNexts[ai] = targetAtom;
356 _followOnRoots[targetAtom] = _followOnRoots[ai];
357 }
358 } else if (_followOnRoots.find(targetAtom)->second == targetAtom) {
359 // Is the targetAtom in chain with the targetAtom as the root ?
360 bool changeRoots = false;
361 if (_followOnRoots.count(ai) == 0) {
362 _followOnRoots[ai] = ai;
363 _followOnNexts[ai] = targetAtom;
364 _followOnRoots[targetAtom] = _followOnRoots[ai];
365 changeRoots = true;
366 } else if (_followOnNexts.count(ai) == 0) {
367 // Chain the targetAtom to the current Atom
368 // if the currentAtom has no followon references
369 if (_followOnRoots[ai] != _followOnRoots[targetAtom]) {
370 _followOnNexts[ai] = targetAtom;
371 _followOnRoots[targetAtom] = _followOnRoots[ai];
372 changeRoots = true;
373 }
374 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000375 // Change the roots of the targetAtom and its chain to
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000376 // the current atoms root
377 if (changeRoots) {
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000378 setChainRoot(_followOnRoots[targetAtom], _followOnRoots[ai]);
379 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000380 } // Is targetAtom root
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000381 } // kindLayoutBefore
Rui Ueyamaca8ca552013-05-14 00:41:52 +0000382 } // Reference
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000383 } // atom iteration
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000384} // end function
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000385
386
387/// Build an ordinal override map by traversing the followon chain, and
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000388/// assigning ordinals to each atom, if the atoms have their ordinals
389/// already assigned skip the atom and move to the next. This is the
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000390/// main map thats used to sort the atoms while comparing two atoms together
391void LayoutPass::buildOrdinalOverrideMap(MutableFile::DefinedAtomRange &range) {
Michael J. Spencerd4eb47c2013-04-06 00:56:40 +0000392 ScopedTask task(getDefaultDomain(), "LayoutPass::buildOrdinalOverrideMap");
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000393 uint64_t index = 0;
Rui Ueyama0196d1062013-05-14 16:53:59 +0000394 for (const DefinedAtom *ai : range) {
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000395 const DefinedAtom *atom = ai;
Michael J. Spencer1ecf8902013-03-12 00:10:00 +0000396 if (_ordinalOverrideMap.find(atom) != _ordinalOverrideMap.end())
397 continue;
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000398 AtomToAtomT::iterator start = _followOnRoots.find(atom);
399 if (start != _followOnRoots.end()) {
400 for (const DefinedAtom *nextAtom = start->second; nextAtom != NULL;
401 nextAtom = _followOnNexts[nextAtom]) {
402 AtomToOrdinalT::iterator pos = _ordinalOverrideMap.find(nextAtom);
403 if (pos == _ordinalOverrideMap.end()) {
404 _ordinalOverrideMap[nextAtom] = index++;
405 }
406 }
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000407 }
408 }
409}
410
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000411// Helper functions to check follow-on graph.
412#ifndef NDEBUG
413namespace {
414typedef llvm::DenseMap<const DefinedAtom *, const DefinedAtom *> AtomToAtomT;
415
416std::string atomToDebugString(const Atom *atom) {
417 const DefinedAtom *definedAtom = llvm::dyn_cast<DefinedAtom>(atom);
418 std::string str;
419 llvm::raw_string_ostream s(str);
420 if (definedAtom->name().empty())
421 s << "<anonymous " << definedAtom << ">";
422 else
423 s << definedAtom->name();
424 s << " in ";
425 if (definedAtom->customSectionName().empty())
426 s << "<anonymous>";
427 else
428 s << definedAtom->customSectionName();
429 s.flush();
430 return str;
431}
432
433void showCycleDetectedError(AtomToAtomT &followOnNexts,
434 const DefinedAtom *atom) {
435 const DefinedAtom *start = atom;
436 llvm::dbgs() << "There's a cycle in a follow-on chain!\n";
437 do {
438 llvm::dbgs() << " " << atomToDebugString(atom) << "\n";
439 for (const Reference *ref : *atom) {
440 llvm::dbgs() << " " << ref->kindToString()
441 << ": " << atomToDebugString(ref->target()) << "\n";
442 }
443 atom = followOnNexts[atom];
444 } while (atom != start);
Rui Ueyama5b274f32013-07-29 21:50:33 +0000445 llvm::report_fatal_error("Cycle detected");
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000446}
447
448/// Exit if there's a cycle in a followon chain reachable from the
449/// given root atom. Uses the tortoise and hare algorithm to detect a
450/// cycle.
451void checkNoCycleInFollowonChain(AtomToAtomT &followOnNexts,
452 const DefinedAtom *root) {
453 const DefinedAtom *tortoise = root;
454 const DefinedAtom *hare = followOnNexts[root];
455 while (true) {
456 if (!tortoise || !hare)
457 return;
458 if (tortoise == hare)
459 showCycleDetectedError(followOnNexts, tortoise);
460 tortoise = followOnNexts[tortoise];
461 hare = followOnNexts[followOnNexts[hare]];
462 }
463}
464
465void checkReachabilityFromRoot(AtomToAtomT &followOnRoots,
466 const DefinedAtom *atom) {
467 if (!atom) return;
468 auto i = followOnRoots.find(atom);
469 if (i == followOnRoots.end()) {
470 Twine msg(Twine("Atom <") + atomToDebugString(atom)
471 + "> has no follow-on root!");
472 llvm_unreachable(msg.str().c_str());
473 }
474 const DefinedAtom *ap = i->second;
475 while (true) {
476 const DefinedAtom *next = followOnRoots[ap];
477 if (!next) {
478 Twine msg(Twine("Atom <" + atomToDebugString(atom)
479 + "> is not reachable from its root!"));
480 llvm_unreachable(msg.str().c_str());
481 }
482 if (next == ap)
483 return;
484 ap = next;
485 }
486}
487
488void printDefinedAtoms(const MutableFile::DefinedAtomRange &atomRange) {
489 for (const DefinedAtom *atom : atomRange) {
490 llvm::dbgs() << " file=" << atom->file().path()
491 << ", name=" << atom->name()
492 << ", size=" << atom->size()
493 << ", type=" << atom->contentType()
494 << ", ordinal=" << atom->ordinal()
495 << "\n";
496 }
497}
498} // end anonymous namespace
499
500/// Verify that the followon chain is sane. Should not be called in
501/// release binary.
502void LayoutPass::checkFollowonChain(MutableFile::DefinedAtomRange &range) {
503 ScopedTask task(getDefaultDomain(), "LayoutPass::checkFollowonChain");
504
505 // Verify that there's no cycle in follow-on chain.
506 std::set<const DefinedAtom *> roots;
507 for (const auto &ai : _followOnRoots)
508 roots.insert(ai.second);
509 for (const DefinedAtom *root : roots)
510 checkNoCycleInFollowonChain(_followOnNexts, root);
511
512 // Verify that all the atoms in followOnNexts have references to
513 // their roots.
514 for (const auto &ai : _followOnNexts) {
515 checkReachabilityFromRoot(_followOnRoots, ai.first);
516 checkReachabilityFromRoot(_followOnRoots, ai.second);
517 }
518}
519#endif // #ifndef NDEBUG
520
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000521/// Perform the actual pass
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000522void LayoutPass::perform(MutableFile &mergedFile) {
Michael J. Spencerbd66d042013-05-28 18:55:39 +0000523 ScopedTask task(getDefaultDomain(), "LayoutPass");
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000524 MutableFile::DefinedAtomRange atomRange = mergedFile.definedAtoms();
525
526 // Build follow on tables
527 buildFollowOnTable(atomRange);
528
529 // Build Ingroup reference table
530 buildInGroupTable(atomRange);
531
532 // Build preceded by tables
533 buildPrecededByTable(atomRange);
534
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000535 // Check the structure of followon graph if running in debug mode.
536 DEBUG(checkFollowonChain(atomRange));
537
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000538 // Build override maps
539 buildOrdinalOverrideMap(atomRange);
540
Rui Ueyama9c4f89a2013-05-23 01:31:25 +0000541 DEBUG({
Nick Kledzikf4fa8c02013-04-04 20:32:18 +0000542 llvm::dbgs() << "unsorted atoms:\n";
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000543 printDefinedAtoms(atomRange);
Nick Kledzikf4fa8c02013-04-04 20:32:18 +0000544 });
Shankar Easwaran45a5f932013-04-29 03:27:57 +0000545
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000546 // sort the atoms
Shankar Easwaranbcf36562013-10-11 01:50:04 +0000547 std::sort(atomRange.begin(), atomRange.end(), _compareAtoms);
Shankar Easwaran45a5f932013-04-29 03:27:57 +0000548
Rui Ueyama9c4f89a2013-05-23 01:31:25 +0000549 DEBUG({
Nick Kledzikf4fa8c02013-04-04 20:32:18 +0000550 llvm::dbgs() << "sorted atoms:\n";
Rui Ueyamaa6b71ca2013-06-07 20:18:39 +0000551 printDefinedAtoms(atomRange);
Nick Kledzikf4fa8c02013-04-04 20:32:18 +0000552 });
Shankar Easwaran34ab70f2013-02-07 20:16:12 +0000553}