Jim Grosbach | 946227d | 2011-11-15 16:46:22 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCModule.cpp - MCModule implementation ----------------------===// |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 2 | // |
| 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 Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 10 | #include "llvm/MC/MCAnalysis/MCModule.h" |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 11 | #include "llvm/ADT/STLExtras.h" |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 12 | #include "llvm/MC/MCAnalysis/MCAtom.h" |
| 13 | #include "llvm/MC/MCAnalysis/MCFunction.h" |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 14 | #include <algorithm> |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 18 | static bool AtomComp(const MCAtom *L, uint64_t Addr) { |
| 19 | return L->getEndAddr() < Addr; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Ahmed Bougacha | 46d353f | 2013-08-21 07:28:17 +0000 | [diff] [blame] | 22 | static bool AtomCompInv(uint64_t Addr, const MCAtom *R) { |
| 23 | return Addr < R->getEndAddr(); |
| 24 | } |
| 25 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 26 | void MCModule::map(MCAtom *NewAtom) { |
Daniel Jasper | 35b2a7a | 2013-05-24 06:26:18 +0000 | [diff] [blame] | 27 | uint64_t Begin = NewAtom->Begin; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 28 | |
Ahmed Bougacha | 7ab184a | 2013-06-19 20:18:59 +0000 | [diff] [blame] | 29 | assert(Begin <= NewAtom->End && "Creating MCAtom with endpoints reversed?"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 30 | |
| 31 | // Check for atoms already covering this range. |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 32 | AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(), |
| 33 | Begin, AtomComp); |
Daniel Jasper | 35b2a7a | 2013-05-24 06:26:18 +0000 | [diff] [blame] | 34 | assert((I == atom_end() || (*I)->getBeginAddr() > NewAtom->End) |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 35 | && "Offset range already occupied!"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 36 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 37 | // Insert the new atom to the list. |
| 38 | Atoms.insert(I, NewAtom); |
| 39 | } |
| 40 | |
| 41 | MCTextAtom *MCModule::createTextAtom(uint64_t Begin, uint64_t End) { |
| 42 | MCTextAtom *NewAtom = new MCTextAtom(this, Begin, End); |
| 43 | map(NewAtom); |
| 44 | return NewAtom; |
| 45 | } |
| 46 | |
| 47 | MCDataAtom *MCModule::createDataAtom(uint64_t Begin, uint64_t End) { |
| 48 | MCDataAtom *NewAtom = new MCDataAtom(this, Begin, End); |
| 49 | map(NewAtom); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 50 | return NewAtom; |
| 51 | } |
| 52 | |
| 53 | // remap - Update the interval mapping for an atom. |
| 54 | void MCModule::remap(MCAtom *Atom, uint64_t NewBegin, uint64_t NewEnd) { |
| 55 | // Find and erase the old mapping. |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 56 | AtomListTy::iterator I = std::lower_bound(atom_begin(), atom_end(), |
| 57 | Atom->Begin, AtomComp); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 58 | assert(I != atom_end() && "Atom offset not found in module!"); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 59 | assert(*I == Atom && "Previous atom mapping was invalid!"); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 60 | Atoms.erase(I); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 61 | |
Ahmed Bougacha | 7dac32d | 2013-08-21 07:27:55 +0000 | [diff] [blame] | 62 | // FIXME: special case NewBegin == Atom->Begin |
| 63 | |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 64 | // Insert the new mapping. |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 65 | AtomListTy::iterator NewI = std::lower_bound(atom_begin(), atom_end(), |
| 66 | NewBegin, AtomComp); |
Ahmed Bougacha | 7dac32d | 2013-08-21 07:27:55 +0000 | [diff] [blame] | 67 | assert((NewI == atom_end() || (*NewI)->getBeginAddr() > Atom->End) |
| 68 | && "Offset range already occupied!"); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 69 | Atoms.insert(NewI, Atom); |
Owen Anderson | 124e182 | 2011-09-22 22:32:22 +0000 | [diff] [blame] | 70 | |
| 71 | // Update the atom internal bounds. |
| 72 | Atom->Begin = NewBegin; |
| 73 | Atom->End = NewEnd; |
| 74 | } |
| 75 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 76 | const MCAtom *MCModule::findAtomContaining(uint64_t Addr) const { |
Ahmed Bougacha | 71dc2e6 | 2013-07-23 17:44:11 +0000 | [diff] [blame] | 77 | AtomListTy::const_iterator I = std::lower_bound(atom_begin(), atom_end(), |
| 78 | Addr, AtomComp); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 79 | if (I != atom_end() && (*I)->getBeginAddr() <= Addr) |
| 80 | return *I; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 81 | return nullptr; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | MCAtom *MCModule::findAtomContaining(uint64_t Addr) { |
Ahmed Bougacha | 46d353f | 2013-08-21 07:28:17 +0000 | [diff] [blame] | 85 | return const_cast<MCAtom*>( |
| 86 | const_cast<const MCModule *>(this)->findAtomContaining(Addr)); |
| 87 | } |
| 88 | |
| 89 | const 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 Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 93 | return *I; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 94 | return nullptr; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Ahmed Bougacha | 46d353f | 2013-08-21 07:28:17 +0000 | [diff] [blame] | 97 | MCAtom *MCModule::findFirstAtomAfter(uint64_t Addr) { |
| 98 | return const_cast<MCAtom*>( |
| 99 | const_cast<const MCModule *>(this)->findFirstAtomAfter(Addr)); |
| 100 | } |
| 101 | |
Ahmed Bougacha | 7dac32d | 2013-08-21 07:27:55 +0000 | [diff] [blame] | 102 | MCFunction *MCModule::createFunction(StringRef Name) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 103 | std::unique_ptr<MCFunction> MCF(new MCFunction(Name, this)); |
| 104 | Functions.push_back(std::move(MCF)); |
| 105 | return Functions.back().get(); |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Ahmed Bougacha | aeb2bbc | 2013-08-21 07:28:24 +0000 | [diff] [blame] | 108 | static bool CompBBToAtom(MCBasicBlock *BB, const MCTextAtom *Atom) { |
| 109 | return BB->getInsts() < Atom; |
| 110 | } |
| 111 | |
| 112 | void 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 | |
| 124 | void 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 Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 135 | MCModule::MCModule() : Entrypoint(0) { } |
| 136 | |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 137 | MCModule::~MCModule() { |
| 138 | for (AtomListTy::iterator AI = atom_begin(), |
| 139 | AE = atom_end(); |
| 140 | AI != AE; ++AI) |
| 141 | delete *AI; |
Ahmed Bougacha | ef99356 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 142 | } |