blob: 7512299c9e0a04221299e3e7c33a1cbf6b855297 [file] [log] [blame]
Jim Grosbach946227d2011-11-15 16:46:22 +00001//===- lib/MC/MCModule.cpp - MCModule implementation ----------------------===//
Owen Anderson124e1822011-09-22 22:32:22 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070010#include "llvm/MC/MCAnalysis/MCModule.h"
Stephen Hinesdce4a402014-05-29 02:49:00 -070011#include "llvm/ADT/STLExtras.h"
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070012#include "llvm/MC/MCAnalysis/MCAtom.h"
13#include "llvm/MC/MCAnalysis/MCFunction.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000014#include <algorithm>
Owen Anderson124e1822011-09-22 22:32:22 +000015
16using namespace llvm;
17
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000018static bool AtomComp(const MCAtom *L, uint64_t Addr) {
19 return L->getEndAddr() < Addr;
Ahmed Bougachaef993562013-05-24 01:07:04 +000020}
21
Ahmed Bougacha46d353f2013-08-21 07:28:17 +000022static bool AtomCompInv(uint64_t Addr, const MCAtom *R) {
23 return Addr < R->getEndAddr();
24}
25
Ahmed Bougachaef993562013-05-24 01:07:04 +000026void MCModule::map(MCAtom *NewAtom) {
Daniel Jasper35b2a7a2013-05-24 06:26:18 +000027 uint64_t Begin = NewAtom->Begin;
Ahmed Bougachaef993562013-05-24 01:07:04 +000028
Ahmed Bougacha7ab184a2013-06-19 20:18:59 +000029 assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?");
Owen Anderson124e1822011-09-22 22:32:22 +000030
31 // Check for atoms already covering this range.
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000032 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
33 Begin, AtomComp);
Daniel Jasper35b2a7a2013-05-24 06:26:18 +000034 assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End)
Ahmed Bougachaef993562013-05-24 01:07:04 +000035 && "Offset range already occupied!");
Owen Anderson124e1822011-09-22 22:32:22 +000036
Ahmed Bougachaef993562013-05-24 01:07:04 +000037 // Insert the new atom to the list.
38 Atoms.insert(I, NewAtom);
39}
40
41MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) {
42 MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End);
43 map(NewAtom);
44 return NewAtom;
45}
46
47MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) {
48 MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End);
49 map(NewAtom);
Owen Anderson124e1822011-09-22 22:32:22 +000050 return NewAtom;
51}
52
53// remap - Update the interval mapping for an atom.
54void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) {
55 // Find and erase the old mapping.
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000056 AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(),
57 Atom->Begin, AtomComp);
Ahmed Bougachaef993562013-05-24 01:07:04 +000058 assert(I != atom_end() && "Atom offset not found in module!");
Owen Anderson124e1822011-09-22 22:32:22 +000059 assert(*I == Atom && "Previous atom mapping was invalid!");
Ahmed Bougachaef993562013-05-24 01:07:04 +000060 Atoms.erase(I);
Owen Anderson124e1822011-09-22 22:32:22 +000061
Ahmed Bougacha7dac32d2013-08-21 07:27:55 +000062 // FIXME: special case NewBegin == Atom->Begin
63
Owen Anderson124e1822011-09-22 22:32:22 +000064 // Insert the new mapping.
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000065 AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(),
66 NewBegin, AtomComp);
Ahmed Bougacha7dac32d2013-08-21 07:27:55 +000067 assert((NewI == atom_end() || (*NewI)->getBeginAddr() > Atom->End)
68 && "Offset range already occupied!");
Ahmed Bougachaef993562013-05-24 01:07:04 +000069 Atoms.insert(NewI, Atom);
Owen Anderson124e1822011-09-22 22:32:22 +000070
71 // Update the atom internal bounds.
72 Atom->Begin = NewBegin;
73 Atom->End = NewEnd;
74}
75
Ahmed Bougachaef993562013-05-24 01:07:04 +000076const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const {
Ahmed Bougacha71dc2e62013-07-23 17:44:11 +000077 AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(),
78 Addr, AtomComp);
Ahmed Bougachaef993562013-05-24 01:07:04 +000079 if (I != atom_end() && (*I)->getBeginAddr() <= Addr)
80 return *I;
Stephen Hinesdce4a402014-05-29 02:49:00 -070081 return nullptr;
Ahmed Bougachaef993562013-05-24 01:07:04 +000082}
83
84MCAtom *MCModule::findAtomContaining(uint64_t Addr) {
Ahmed Bougacha46d353f2013-08-21 07:28:17 +000085 return const_cast<MCAtom*>(
86 const_cast<const MCModule *>(this)->findAtomContaining(Addr));
87}
88
89const MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) const {
90 AtomListTy::const_iterator I = std::upper_bound(atom_begin(), atom_end(),
91 Addr, AtomCompInv);
92 if (I != atom_end())
Ahmed Bougachaef993562013-05-24 01:07:04 +000093 return *I;
Stephen Hinesdce4a402014-05-29 02:49:00 -070094 return nullptr;
Ahmed Bougachaef993562013-05-24 01:07:04 +000095}
96
Ahmed Bougacha46d353f2013-08-21 07:28:17 +000097MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) {
98 return const_cast<MCAtom*>(
99 const_cast<const MCModule *>(this)->findFirstAtomAfter(Addr));
100}
101
Ahmed Bougacha7dac32d2013-08-21 07:27:55 +0000102MCFunction *MCModule::createFunction(StringRef Name) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700103 std::unique_ptr<MCFunction> MCF(new MCFunction(Name, this));
104 Functions.push_back(std::move(MCF));
105 return Functions.back().get();
Ahmed Bougachaef993562013-05-24 01:07:04 +0000106}
107
Ahmed Bougachaaeb2bbc2013-08-21 07:28:24 +0000108static bool CompBBToAtom(MCBasicBlock *BB, const MCTextAtom *Atom) {
109 return BB->getInsts() < Atom;
110}
111
112void MCModule::splitBasicBlocksForAtom(const MCTextAtom *TA,
113 const MCTextAtom *NewTA) {
114 BBsByAtomTy::iterator
115 I = std::lower_bound(BBsByAtom.begin(), BBsByAtom.end(),
116 TA, CompBBToAtom);
117 for (; I != BBsByAtom.end() && (*I)->getInsts() == TA; ++I) {
118 MCBasicBlock *BB = *I;
119 MCBasicBlock *NewBB = &BB->getParent()->createBlock(*NewTA);
120 BB->splitBasicBlock(NewBB);
121 }
122}
123
124void MCModule::trackBBForAtom(const MCTextAtom *Atom, MCBasicBlock *BB) {
125 assert(Atom == BB->getInsts() && "Text atom doesn't back the basic block!");
126 BBsByAtomTy::iterator I = std::lower_bound(BBsByAtom.begin(),
127 BBsByAtom.end(),
128 Atom, CompBBToAtom);
129 for (; I != BBsByAtom.end() && (*I)->getInsts() == Atom; ++I)
130 if (*I == BB)
131 return;
132 BBsByAtom.insert(I, BB);
133}
134
Stephen Hinesdce4a402014-05-29 02:49:00 -0700135MCModule::MCModule() : Entrypoint(0) { }
136
Ahmed Bougachaef993562013-05-24 01:07:04 +0000137MCModule::~MCModule() {
138 for (AtomListTy::iterator AI = atom_begin(),
139 AE = atom_end();
140 AI != AE; ++AI)
141 delete *AI;
Ahmed Bougachaef993562013-05-24 01:07:04 +0000142}