Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- MachineFunction.cpp -----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Collect native machine code information for a function. This allows |
| 11 | // target-specific information about the generated code to be stored with each |
| 12 | // function. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "llvm/DerivedTypes.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineInstr.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Passes.h" |
| 24 | #include "llvm/Target/TargetData.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
| 26 | #include "llvm/Target/TargetFrameInfo.h" |
| 27 | #include "llvm/Function.h" |
| 28 | #include "llvm/Instructions.h" |
| 29 | #include "llvm/Support/Compiler.h" |
| 30 | #include "llvm/Support/GraphWriter.h" |
Chris Lattner | 1fefaac | 2008-08-23 22:23:09 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
| 33 | #include "llvm/Config/config.h" |
| 34 | #include <fstream> |
| 35 | #include <sstream> |
| 36 | using namespace llvm; |
| 37 | |
| 38 | static AnnotationID MF_AID( |
| 39 | AnnotationManager::getID("CodeGen::MachineCodeForFunction")); |
| 40 | |
Chris Lattner | 6843344 | 2009-04-13 05:44:34 +0000 | [diff] [blame] | 41 | bool MachineFunctionPass::runOnFunction(Function &F) { |
| 42 | // Do not codegen any 'available_externally' functions at all, they have |
| 43 | // definitions outside the translation unit. |
| 44 | if (F.hasAvailableExternallyLinkage()) |
| 45 | return false; |
| 46 | |
| 47 | return runOnMachineFunction(MachineFunction::get(&F)); |
| 48 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | |
| 50 | namespace { |
| 51 | struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass { |
| 52 | static char ID; |
| 53 | |
| 54 | std::ostream *OS; |
| 55 | const std::string Banner; |
| 56 | |
Dan Gohman | 4ae0828 | 2008-07-21 19:48:15 +0000 | [diff] [blame] | 57 | Printer (std::ostream *os, const std::string &banner) |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 58 | : MachineFunctionPass(&ID), OS(os), Banner(banner) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | |
| 60 | const char *getPassName() const { return "MachineFunction Printer"; } |
| 61 | |
| 62 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 63 | AU.setPreservesAll(); |
| 64 | } |
| 65 | |
| 66 | bool runOnMachineFunction(MachineFunction &MF) { |
| 67 | (*OS) << Banner; |
| 68 | MF.print (*OS); |
| 69 | return false; |
| 70 | } |
| 71 | }; |
| 72 | char Printer::ID = 0; |
| 73 | } |
| 74 | |
| 75 | /// Returns a newly-created MachineFunction Printer pass. The default output |
| 76 | /// stream is std::cerr; the default banner is empty. |
| 77 | /// |
| 78 | FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS, |
| 79 | const std::string &Banner){ |
| 80 | return new Printer(OS, Banner); |
| 81 | } |
| 82 | |
| 83 | namespace { |
| 84 | struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass { |
| 85 | static char ID; |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 86 | Deleter() : MachineFunctionPass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | |
| 88 | const char *getPassName() const { return "Machine Code Deleter"; } |
| 89 | |
| 90 | bool runOnMachineFunction(MachineFunction &MF) { |
| 91 | // Delete the annotation from the function now. |
| 92 | MachineFunction::destruct(MF.getFunction()); |
| 93 | return true; |
| 94 | } |
| 95 | }; |
| 96 | char Deleter::ID = 0; |
| 97 | } |
| 98 | |
| 99 | /// MachineCodeDeletion Pass - This pass deletes all of the machine code for |
| 100 | /// the current function, which should happen after the function has been |
| 101 | /// emitted to a .s file or to memory. |
| 102 | FunctionPass *llvm::createMachineCodeDeleter() { |
| 103 | return new Deleter(); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | //===---------------------------------------------------------------------===// |
| 109 | // MachineFunction implementation |
| 110 | //===---------------------------------------------------------------------===// |
| 111 | |
Dan Gohman | 2fcbc7e | 2008-07-28 21:51:04 +0000 | [diff] [blame] | 112 | void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 113 | MBB->getParent()->DeleteMachineBasicBlock(MBB); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | MachineFunction::MachineFunction(const Function *F, |
| 117 | const TargetMachine &TM) |
| 118 | : Annotation(MF_AID), Fn(F), Target(TM) { |
Matthijs Kooijman | 1cb065e | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 119 | if (TM.getRegisterInfo()) |
| 120 | RegInfo = new (Allocator.Allocate<MachineRegisterInfo>()) |
| 121 | MachineRegisterInfo(*TM.getRegisterInfo()); |
| 122 | else |
| 123 | RegInfo = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | MFInfo = 0; |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 125 | FrameInfo = new (Allocator.Allocate<MachineFrameInfo>()) |
| 126 | MachineFrameInfo(*TM.getFrameInfo()); |
| 127 | ConstantPool = new (Allocator.Allocate<MachineConstantPool>()) |
| 128 | MachineConstantPool(TM.getTargetData()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 129 | |
| 130 | // Set up jump table. |
| 131 | const TargetData &TD = *TM.getTargetData(); |
| 132 | bool IsPic = TM.getRelocationModel() == Reloc::PIC_; |
| 133 | unsigned EntrySize = IsPic ? 4 : TD.getPointerSize(); |
| 134 | unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty) |
| 135 | : TD.getPointerABIAlignment(); |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 136 | JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>()) |
| 137 | MachineJumpTableInfo(EntrySize, Alignment); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | MachineFunction::~MachineFunction() { |
| 141 | BasicBlocks.clear(); |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 142 | InstructionRecycler.clear(Allocator); |
| 143 | BasicBlockRecycler.clear(Allocator); |
Matthijs Kooijman | 1cb065e | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 144 | if (RegInfo) |
| 145 | RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo); |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 146 | if (MFInfo) { |
| 147 | MFInfo->~MachineFunctionInfo(); Allocator.Deallocate(MFInfo); |
| 148 | } |
| 149 | FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo); |
| 150 | ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool); |
| 151 | JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
| 155 | /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and |
| 156 | /// recomputes them. This guarantees that the MBB numbers are sequential, |
| 157 | /// dense, and match the ordering of the blocks within the function. If a |
| 158 | /// specific MachineBasicBlock is specified, only that block and those after |
| 159 | /// it are renumbered. |
| 160 | void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { |
| 161 | if (empty()) { MBBNumbering.clear(); return; } |
| 162 | MachineFunction::iterator MBBI, E = end(); |
| 163 | if (MBB == 0) |
| 164 | MBBI = begin(); |
| 165 | else |
| 166 | MBBI = MBB; |
| 167 | |
| 168 | // Figure out the block number this should have. |
| 169 | unsigned BlockNo = 0; |
| 170 | if (MBBI != begin()) |
| 171 | BlockNo = prior(MBBI)->getNumber()+1; |
| 172 | |
| 173 | for (; MBBI != E; ++MBBI, ++BlockNo) { |
| 174 | if (MBBI->getNumber() != (int)BlockNo) { |
| 175 | // Remove use of the old number. |
| 176 | if (MBBI->getNumber() != -1) { |
| 177 | assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && |
| 178 | "MBB number mismatch!"); |
| 179 | MBBNumbering[MBBI->getNumber()] = 0; |
| 180 | } |
| 181 | |
| 182 | // If BlockNo is already taken, set that block's number to -1. |
| 183 | if (MBBNumbering[BlockNo]) |
| 184 | MBBNumbering[BlockNo]->setNumber(-1); |
| 185 | |
| 186 | MBBNumbering[BlockNo] = MBBI; |
| 187 | MBBI->setNumber(BlockNo); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Okay, all the blocks are renumbered. If we have compactified the block |
| 192 | // numbering, shrink MBBNumbering now. |
| 193 | assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); |
| 194 | MBBNumbering.resize(BlockNo); |
| 195 | } |
| 196 | |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 197 | /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead |
| 198 | /// of `new MachineInstr'. |
| 199 | /// |
| 200 | MachineInstr * |
Bill Wendling | 5aa0ddb | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 201 | MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID, |
| 202 | DebugLoc DL, bool NoImp) { |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 203 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
Bill Wendling | 5aa0ddb | 2009-02-03 00:55:04 +0000 | [diff] [blame] | 204 | MachineInstr(TID, DL, NoImp); |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 205 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 206 | |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 207 | /// CloneMachineInstr - Create a new MachineInstr which is a copy of the |
| 208 | /// 'Orig' instruction, identical in all ways except the the instruction |
| 209 | /// has no parent, prev, or next. |
| 210 | /// |
| 211 | MachineInstr * |
| 212 | MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { |
| 213 | return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) |
| 214 | MachineInstr(*this, *Orig); |
| 215 | } |
| 216 | |
| 217 | /// DeleteMachineInstr - Delete the given MachineInstr. |
| 218 | /// |
| 219 | void |
| 220 | MachineFunction::DeleteMachineInstr(MachineInstr *MI) { |
| 221 | // Clear the instructions memoperands. This must be done manually because |
| 222 | // the instruction's parent pointer is now null, so it can't properly |
| 223 | // deallocate them on its own. |
| 224 | MI->clearMemOperands(*this); |
| 225 | |
| 226 | MI->~MachineInstr(); |
| 227 | InstructionRecycler.Deallocate(Allocator, MI); |
| 228 | } |
| 229 | |
| 230 | /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this |
| 231 | /// instead of `new MachineBasicBlock'. |
| 232 | /// |
| 233 | MachineBasicBlock * |
| 234 | MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { |
| 235 | return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) |
| 236 | MachineBasicBlock(*this, bb); |
| 237 | } |
| 238 | |
| 239 | /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. |
| 240 | /// |
| 241 | void |
| 242 | MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { |
| 243 | assert(MBB->getParent() == this && "MBB parent mismatch!"); |
| 244 | MBB->~MachineBasicBlock(); |
| 245 | BasicBlockRecycler.Deallocate(Allocator, MBB); |
| 246 | } |
| 247 | |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 248 | void MachineFunction::dump() const { |
| 249 | print(*cerr.stream()); |
| 250 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 251 | |
| 252 | void MachineFunction::print(std::ostream &OS) const { |
| 253 | OS << "# Machine code for " << Fn->getName () << "():\n"; |
| 254 | |
| 255 | // Print Frame Information |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 256 | FrameInfo->print(*this, OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 257 | |
| 258 | // Print JumpTable Information |
Dan Gohman | 221a437 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 259 | JumpTableInfo->print(OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | |
| 261 | // Print Constant Pool |
Chris Lattner | 491f783 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 262 | { |
| 263 | raw_os_ostream OSS(OS); |
| 264 | ConstantPool->print(OSS); |
| 265 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 266 | |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 267 | const TargetRegisterInfo *TRI = getTarget().getRegisterInfo(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 268 | |
Matthijs Kooijman | 1cb065e | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 269 | if (RegInfo && !RegInfo->livein_empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 270 | OS << "Live Ins:"; |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 271 | for (MachineRegisterInfo::livein_iterator |
| 272 | I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 273 | if (TRI) |
Bill Wendling | 9b0baeb | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 274 | OS << " " << TRI->getName(I->first); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 275 | else |
| 276 | OS << " Reg #" << I->first; |
| 277 | |
| 278 | if (I->second) |
| 279 | OS << " in VR#" << I->second << " "; |
| 280 | } |
| 281 | OS << "\n"; |
| 282 | } |
Matthijs Kooijman | 1cb065e | 2008-10-13 12:37:16 +0000 | [diff] [blame] | 283 | if (RegInfo && !RegInfo->liveout_empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 284 | OS << "Live Outs:"; |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 285 | for (MachineRegisterInfo::liveout_iterator |
| 286 | I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I) |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 287 | if (TRI) |
Bill Wendling | 9b0baeb | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 288 | OS << " " << TRI->getName(*I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 289 | else |
| 290 | OS << " Reg #" << *I; |
| 291 | OS << "\n"; |
| 292 | } |
| 293 | |
| 294 | for (const_iterator BB = begin(); BB != end(); ++BB) |
| 295 | BB->print(OS); |
| 296 | |
| 297 | OS << "\n# End machine code for " << Fn->getName () << "().\n\n"; |
| 298 | } |
| 299 | |
| 300 | /// CFGOnly flag - This is used to control whether or not the CFG graph printer |
| 301 | /// prints out the contents of basic blocks or not. This is acceptable because |
| 302 | /// this code is only really used for debugging purposes. |
| 303 | /// |
| 304 | static bool CFGOnly = false; |
| 305 | |
| 306 | namespace llvm { |
| 307 | template<> |
| 308 | struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { |
| 309 | static std::string getGraphName(const MachineFunction *F) { |
| 310 | return "CFG for '" + F->getFunction()->getName() + "' function"; |
| 311 | } |
| 312 | |
| 313 | static std::string getNodeLabel(const MachineBasicBlock *Node, |
| 314 | const MachineFunction *Graph) { |
| 315 | if (CFGOnly && Node->getBasicBlock() && |
| 316 | !Node->getBasicBlock()->getName().empty()) |
| 317 | return Node->getBasicBlock()->getName() + ":"; |
| 318 | |
| 319 | std::ostringstream Out; |
| 320 | if (CFGOnly) { |
| 321 | Out << Node->getNumber() << ':'; |
| 322 | return Out.str(); |
| 323 | } |
| 324 | |
| 325 | Node->print(Out); |
| 326 | |
| 327 | std::string OutStr = Out.str(); |
| 328 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 329 | |
| 330 | // Process string output to make it nicer... |
| 331 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 332 | if (OutStr[i] == '\n') { // Left justify |
| 333 | OutStr[i] = '\\'; |
| 334 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 335 | } |
| 336 | return OutStr; |
| 337 | } |
| 338 | }; |
| 339 | } |
| 340 | |
| 341 | void MachineFunction::viewCFG() const |
| 342 | { |
| 343 | #ifndef NDEBUG |
| 344 | ViewGraph(this, "mf" + getFunction()->getName()); |
| 345 | #else |
| 346 | cerr << "SelectionDAG::viewGraph is only available in debug builds on " |
| 347 | << "systems with Graphviz or gv!\n"; |
| 348 | #endif // NDEBUG |
| 349 | } |
| 350 | |
| 351 | void MachineFunction::viewCFGOnly() const |
| 352 | { |
| 353 | CFGOnly = true; |
| 354 | viewCFG(); |
| 355 | CFGOnly = false; |
| 356 | } |
| 357 | |
| 358 | // The next two methods are used to construct and to retrieve |
| 359 | // the MachineCodeForFunction object for the given function. |
| 360 | // construct() -- Allocates and initializes for a given function and target |
| 361 | // get() -- Returns a handle to the object. |
| 362 | // This should not be called before "construct()" |
| 363 | // for a given Function. |
| 364 | // |
| 365 | MachineFunction& |
| 366 | MachineFunction::construct(const Function *Fn, const TargetMachine &Tar) |
| 367 | { |
| 368 | assert(Fn->getAnnotation(MF_AID) == 0 && |
| 369 | "Object already exists for this function!"); |
| 370 | MachineFunction* mcInfo = new MachineFunction(Fn, Tar); |
| 371 | Fn->addAnnotation(mcInfo); |
| 372 | return *mcInfo; |
| 373 | } |
| 374 | |
| 375 | void MachineFunction::destruct(const Function *Fn) { |
| 376 | bool Deleted = Fn->deleteAnnotation(MF_AID); |
Chris Lattner | 845e05c | 2008-04-06 21:46:45 +0000 | [diff] [blame] | 377 | assert(Deleted && "Machine code did not exist for function!"); |
| 378 | Deleted = Deleted; // silence warning when no assertions. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | MachineFunction& MachineFunction::get(const Function *F) |
| 382 | { |
| 383 | MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID); |
| 384 | assert(mc && "Call construct() method first to allocate the object"); |
| 385 | return *mc; |
| 386 | } |
| 387 | |
evancheng | c611f1d | 2009-01-27 21:15:07 +0000 | [diff] [blame] | 388 | /// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given |
Bill Wendling | 4756f3b | 2009-02-03 22:49:58 +0000 | [diff] [blame] | 389 | /// source file, line, and column. If none currently exists, create a new |
| 390 | /// DebugLocTuple, and insert it into the DebugIdMap. |
evancheng | c611f1d | 2009-01-27 21:15:07 +0000 | [diff] [blame] | 391 | unsigned MachineFunction::getOrCreateDebugLocID(unsigned Src, unsigned Line, |
| 392 | unsigned Col) { |
Bill Wendling | 4756f3b | 2009-02-03 22:49:58 +0000 | [diff] [blame] | 393 | DebugLocTuple Tuple(Src, Line, Col); |
evancheng | c611f1d | 2009-01-27 21:15:07 +0000 | [diff] [blame] | 394 | DenseMap<DebugLocTuple, unsigned>::iterator II |
| 395 | = DebugLocInfo.DebugIdMap.find(Tuple); |
evancheng | 2dbde50 | 2009-01-26 07:41:49 +0000 | [diff] [blame] | 396 | if (II != DebugLocInfo.DebugIdMap.end()) |
| 397 | return II->second; |
| 398 | // Add a new tuple. |
Evan Cheng | a40c3dd | 2009-01-26 23:47:30 +0000 | [diff] [blame] | 399 | unsigned Id = DebugLocInfo.DebugLocations.size(); |
evancheng | 2dbde50 | 2009-01-26 07:41:49 +0000 | [diff] [blame] | 400 | DebugLocInfo.DebugLocations.push_back(Tuple); |
Evan Cheng | a40c3dd | 2009-01-26 23:47:30 +0000 | [diff] [blame] | 401 | DebugLocInfo.DebugIdMap[Tuple] = Id; |
| 402 | return Id; |
evancheng | 2dbde50 | 2009-01-26 07:41:49 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Bill Wendling | 4756f3b | 2009-02-03 22:49:58 +0000 | [diff] [blame] | 405 | /// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object. |
Bill Wendling | acf9574 | 2009-02-04 00:05:34 +0000 | [diff] [blame] | 406 | DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const { |
Bill Wendling | c607509 | 2009-02-03 22:55:54 +0000 | [diff] [blame] | 407 | unsigned Idx = DL.getIndex(); |
Bill Wendling | 4756f3b | 2009-02-03 22:49:58 +0000 | [diff] [blame] | 408 | assert(Idx < DebugLocInfo.DebugLocations.size() && |
| 409 | "Invalid index into debug locations!"); |
| 410 | return DebugLocInfo.DebugLocations[Idx]; |
| 411 | } |
| 412 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 413 | //===----------------------------------------------------------------------===// |
| 414 | // MachineFrameInfo implementation |
| 415 | //===----------------------------------------------------------------------===// |
| 416 | |
Chris Lattner | 610b9eb | 2008-01-25 07:19:06 +0000 | [diff] [blame] | 417 | /// CreateFixedObject - Create a new object at a fixed location on the stack. |
| 418 | /// All fixed objects should be created before other objects are created for |
| 419 | /// efficiency. By default, fixed objects are immutable. This returns an |
| 420 | /// index with a negative value. |
| 421 | /// |
| 422 | int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset, |
| 423 | bool Immutable) { |
| 424 | assert(Size != 0 && "Cannot allocate zero size fixed stack objects!"); |
| 425 | Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable)); |
| 426 | return -++NumFixedObjects; |
| 427 | } |
| 428 | |
| 429 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 430 | void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{ |
Matthijs Kooijman | 0e9ee53 | 2008-11-03 11:16:43 +0000 | [diff] [blame] | 431 | const TargetFrameInfo *FI = MF.getTarget().getFrameInfo(); |
| 432 | int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 433 | |
| 434 | for (unsigned i = 0, e = Objects.size(); i != e; ++i) { |
| 435 | const StackObject &SO = Objects[i]; |
Dan Gohman | d743014 | 2008-10-15 02:57:38 +0000 | [diff] [blame] | 436 | OS << " <fi#" << (int)(i-NumFixedObjects) << ">: "; |
Evan Cheng | da87253 | 2008-02-27 03:04:06 +0000 | [diff] [blame] | 437 | if (SO.Size == ~0ULL) { |
| 438 | OS << "dead\n"; |
| 439 | continue; |
| 440 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 441 | if (SO.Size == 0) |
| 442 | OS << "variable sized"; |
| 443 | else |
| 444 | OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ","); |
| 445 | OS << " alignment is " << SO.Alignment << " byte" |
| 446 | << (SO.Alignment != 1 ? "s," : ","); |
| 447 | |
| 448 | if (i < NumFixedObjects) |
| 449 | OS << " fixed"; |
| 450 | if (i < NumFixedObjects || SO.SPOffset != -1) { |
| 451 | int64_t Off = SO.SPOffset - ValOffset; |
| 452 | OS << " at location [SP"; |
| 453 | if (Off > 0) |
| 454 | OS << "+" << Off; |
| 455 | else if (Off < 0) |
| 456 | OS << Off; |
| 457 | OS << "]"; |
| 458 | } |
| 459 | OS << "\n"; |
| 460 | } |
| 461 | |
| 462 | if (HasVarSizedObjects) |
| 463 | OS << " Stack frame contains variable sized objects\n"; |
| 464 | } |
| 465 | |
| 466 | void MachineFrameInfo::dump(const MachineFunction &MF) const { |
| 467 | print(MF, *cerr.stream()); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | //===----------------------------------------------------------------------===// |
| 472 | // MachineJumpTableInfo implementation |
| 473 | //===----------------------------------------------------------------------===// |
| 474 | |
| 475 | /// getJumpTableIndex - Create a new jump table entry in the jump table info |
| 476 | /// or return an existing one. |
| 477 | /// |
| 478 | unsigned MachineJumpTableInfo::getJumpTableIndex( |
| 479 | const std::vector<MachineBasicBlock*> &DestBBs) { |
| 480 | assert(!DestBBs.empty() && "Cannot create an empty jump table!"); |
| 481 | for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) |
| 482 | if (JumpTables[i].MBBs == DestBBs) |
| 483 | return i; |
| 484 | |
| 485 | JumpTables.push_back(MachineJumpTableEntry(DestBBs)); |
| 486 | return JumpTables.size()-1; |
| 487 | } |
| 488 | |
Dan Gohman | 352406d | 2009-04-15 01:18:49 +0000 | [diff] [blame^] | 489 | /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update |
| 490 | /// the jump tables to branch to New instead. |
| 491 | bool |
| 492 | MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, |
| 493 | MachineBasicBlock *New) { |
| 494 | assert(Old != New && "Not making a change?"); |
| 495 | bool MadeChange = false; |
| 496 | for (size_t i = 0, e = JumpTables.size(); i != e; ++i) { |
| 497 | MachineJumpTableEntry &JTE = JumpTables[i]; |
| 498 | for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) |
| 499 | if (JTE.MBBs[j] == Old) { |
| 500 | JTE.MBBs[j] = New; |
| 501 | MadeChange = true; |
| 502 | } |
| 503 | } |
| 504 | return MadeChange; |
| 505 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 506 | |
| 507 | void MachineJumpTableInfo::print(std::ostream &OS) const { |
| 508 | // FIXME: this is lame, maybe we could print out the MBB numbers or something |
| 509 | // like {1, 2, 4, 5, 3, 0} |
| 510 | for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { |
Dan Gohman | d743014 | 2008-10-15 02:57:38 +0000 | [diff] [blame] | 511 | OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size() |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 512 | << " entries\n"; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | void MachineJumpTableInfo::dump() const { print(*cerr.stream()); } |
| 517 | |
| 518 | |
| 519 | //===----------------------------------------------------------------------===// |
| 520 | // MachineConstantPool implementation |
| 521 | //===----------------------------------------------------------------------===// |
| 522 | |
| 523 | const Type *MachineConstantPoolEntry::getType() const { |
| 524 | if (isMachineConstantPoolEntry()) |
| 525 | return Val.MachineCPVal->getType(); |
| 526 | return Val.ConstVal->getType(); |
| 527 | } |
| 528 | |
| 529 | MachineConstantPool::~MachineConstantPool() { |
| 530 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
| 531 | if (Constants[i].isMachineConstantPoolEntry()) |
| 532 | delete Constants[i].Val.MachineCPVal; |
| 533 | } |
| 534 | |
| 535 | /// getConstantPoolIndex - Create a new entry in the constant pool or return |
Dan Gohman | 62e7d9f | 2008-09-16 20:45:53 +0000 | [diff] [blame] | 536 | /// an existing one. User must specify the log2 of the minimum required |
| 537 | /// alignment for the object. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 538 | /// |
| 539 | unsigned MachineConstantPool::getConstantPoolIndex(Constant *C, |
| 540 | unsigned Alignment) { |
| 541 | assert(Alignment && "Alignment must be specified!"); |
| 542 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
| 543 | |
| 544 | // Check to see if we already have this constant. |
| 545 | // |
| 546 | // FIXME, this could be made much more efficient for large constant pools. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 547 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) |
Evan Cheng | 68c1868 | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 548 | if (Constants[i].Val.ConstVal == C && |
| 549 | (Constants[i].getAlignment() & (Alignment - 1)) == 0) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 550 | return i; |
| 551 | |
Evan Cheng | 68c1868 | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 552 | Constants.push_back(MachineConstantPoolEntry(C, Alignment)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 553 | return Constants.size()-1; |
| 554 | } |
| 555 | |
| 556 | unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, |
| 557 | unsigned Alignment) { |
| 558 | assert(Alignment && "Alignment must be specified!"); |
| 559 | if (Alignment > PoolAlignment) PoolAlignment = Alignment; |
| 560 | |
| 561 | // Check to see if we already have this constant. |
| 562 | // |
| 563 | // FIXME, this could be made much more efficient for large constant pools. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 564 | int Idx = V->getExistingMachineCPValue(this, Alignment); |
| 565 | if (Idx != -1) |
| 566 | return (unsigned)Idx; |
Evan Cheng | 68c1868 | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 567 | |
| 568 | Constants.push_back(MachineConstantPoolEntry(V, Alignment)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 569 | return Constants.size()-1; |
| 570 | } |
| 571 | |
Chris Lattner | 491f783 | 2008-08-23 22:53:13 +0000 | [diff] [blame] | 572 | void MachineConstantPool::print(raw_ostream &OS) const { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 573 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Dan Gohman | d743014 | 2008-10-15 02:57:38 +0000 | [diff] [blame] | 574 | OS << " <cp#" << i << "> is"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 575 | if (Constants[i].isMachineConstantPoolEntry()) |
| 576 | Constants[i].Val.MachineCPVal->print(OS); |
| 577 | else |
| 578 | OS << *(Value*)Constants[i].Val.ConstVal; |
Evan Cheng | 68c1868 | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 579 | OS << " , alignment=" << Constants[i].getAlignment(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 580 | OS << "\n"; |
| 581 | } |
| 582 | } |
| 583 | |
Dan Gohman | e7d7b0f | 2009-03-23 15:57:19 +0000 | [diff] [blame] | 584 | void MachineConstantPool::dump() const { print(errs()); } |