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