blob: fc9aa843e7889413d2e0c22f0a6b366ae64dac7b [file] [log] [blame]
Chris Lattner6b944532002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00009//
Chris Lattner6b944532002-10-28 01:16:38 +000010// 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 Lattnerf2868ce2002-02-03 07:54:50 +000015
Chris Lattnerd2b7cec2007-02-14 05:52:17 +000016#include "llvm/DerivedTypes.h"
Chris Lattner4d149cd2003-01-13 00:23:03 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineInstr.h"
Nate Begeman37efe672006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000023#include "llvm/CodeGen/Passes.h"
Owen Anderson07000c62006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000026#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000028#include "llvm/Instructions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000030#include "llvm/Support/GraphWriter.h"
Chris Lattner944fac72008-08-23 22:23:09 +000031#include "llvm/Support/raw_ostream.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000032#include "llvm/ADT/STLExtras.h"
Jim Laskey851a22d2005-10-12 12:09:05 +000033#include "llvm/Config/config.h"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000034#include <fstream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000035#include <sstream>
Chris Lattner07f32d42003-12-20 09:17:07 +000036using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000037
Chris Lattnere316efc2002-10-29 23:18:43 +000038static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000039 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000040
Chris Lattnerefb9b812006-07-14 23:08:47 +000041// Out of line virtual function to home classes.
42void MachineFunctionPass::virtfn() {}
Chris Lattner227c3d32002-10-28 01:12:41 +000043
Chris Lattner227c3d32002-10-28 01:12:41 +000044namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000045 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000046 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000047
Brian Gaeke09caa372004-01-30 21:53:46 +000048 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000049 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000050
Dan Gohman80f3d462008-07-21 19:48:15 +000051 Printer (std::ostream *os, const std::string &banner)
Dan Gohmanae73dc12008-09-04 17:05:41 +000052 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
Brian Gaeke09caa372004-01-30 21:53:46 +000053
Chris Lattner10491642002-10-30 00:48:05 +000054 const char *getPassName() const { return "MachineFunction Printer"; }
55
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesAll();
58 }
59
Chris Lattner16c45e92003-12-20 10:20:58 +000060 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000061 (*OS) << Banner;
62 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000063 return false;
64 }
65 };
Devang Patel19974732007-05-03 01:11:54 +000066 char Printer::ID = 0;
Chris Lattner227c3d32002-10-28 01:12:41 +000067}
68
Brian Gaeke09caa372004-01-30 21:53:46 +000069/// Returns a newly-created MachineFunction Printer pass. The default output
70/// stream is std::cerr; the default banner is empty.
71///
72FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000073 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000074 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000075}
76
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000077namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000078 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000079 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000080 Deleter() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000081
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000082 const char *getPassName() const { return "Machine Code Deleter"; }
83
84 bool runOnMachineFunction(MachineFunction &MF) {
85 // Delete the annotation from the function now.
86 MachineFunction::destruct(MF.getFunction());
87 return true;
88 }
89 };
Devang Patel19974732007-05-03 01:11:54 +000090 char Deleter::ID = 0;
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000091}
92
93/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
94/// the current function, which should happen after the function has been
95/// emitted to a .s file or to memory.
96FunctionPass *llvm::createMachineCodeDeleter() {
97 return new Deleter();
98}
99
100
101
Chris Lattner227c3d32002-10-28 01:12:41 +0000102//===---------------------------------------------------------------------===//
103// MachineFunction implementation
104//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +0000105
Dan Gohmanfed90b62008-07-28 21:51:04 +0000106void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000107 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattner792699c2004-05-24 06:11:51 +0000108}
Chris Lattner227c3d32002-10-28 01:12:41 +0000109
Chris Lattner10491642002-10-30 00:48:05 +0000110MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000111 const TargetMachine &TM)
Evan Cheng505e5512007-04-25 22:10:09 +0000112 : Annotation(MF_AID), Fn(F), Target(TM) {
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000113 if (TM.getRegisterInfo())
114 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
115 MachineRegisterInfo(*TM.getRegisterInfo());
116 else
117 RegInfo = 0;
Chris Lattnerad828162004-08-16 22:36:34 +0000118 MFInfo = 0;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000119 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
120 MachineFrameInfo(*TM.getFrameInfo());
121 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
122 MachineConstantPool(TM.getTargetData());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000123
124 // Set up jump table.
125 const TargetData &TD = *TM.getTargetData();
126 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
127 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000128 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
Chris Lattner58092e32007-01-20 22:35:55 +0000129 : TD.getPointerABIAlignment();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000130 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
131 MachineJumpTableInfo(EntrySize, Alignment);
Chris Lattner831fdcf2002-12-25 05:03:22 +0000132}
133
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000134MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000135 BasicBlocks.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000136 InstructionRecycler.clear(Allocator);
137 BasicBlockRecycler.clear(Allocator);
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000138 if (RegInfo)
139 RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000140 if (MFInfo) {
141 MFInfo->~MachineFunctionInfo(); Allocator.Deallocate(MFInfo);
142 }
143 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
144 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
145 JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
Chris Lattner10491642002-10-30 00:48:05 +0000146}
147
Chris Lattnere70cab02006-10-03 19:18:57 +0000148
149/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
150/// recomputes them. This guarantees that the MBB numbers are sequential,
151/// dense, and match the ordering of the blocks within the function. If a
152/// specific MachineBasicBlock is specified, only that block and those after
153/// it are renumbered.
154void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
155 if (empty()) { MBBNumbering.clear(); return; }
156 MachineFunction::iterator MBBI, E = end();
157 if (MBB == 0)
158 MBBI = begin();
159 else
160 MBBI = MBB;
161
162 // Figure out the block number this should have.
163 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000164 if (MBBI != begin())
165 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000166
167 for (; MBBI != E; ++MBBI, ++BlockNo) {
168 if (MBBI->getNumber() != (int)BlockNo) {
169 // Remove use of the old number.
170 if (MBBI->getNumber() != -1) {
171 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
172 "MBB number mismatch!");
173 MBBNumbering[MBBI->getNumber()] = 0;
174 }
175
176 // If BlockNo is already taken, set that block's number to -1.
177 if (MBBNumbering[BlockNo])
178 MBBNumbering[BlockNo]->setNumber(-1);
179
180 MBBNumbering[BlockNo] = MBBI;
181 MBBI->setNumber(BlockNo);
182 }
183 }
184
185 // Okay, all the blocks are renumbered. If we have compactified the block
186 // numbering, shrink MBBNumbering now.
187 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
188 MBBNumbering.resize(BlockNo);
189}
190
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000191/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
192/// of `new MachineInstr'.
193///
194MachineInstr *
Bill Wendling9bc96a52009-02-03 00:55:04 +0000195MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
196 DebugLoc DL, bool NoImp) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000197 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling9bc96a52009-02-03 00:55:04 +0000198 MachineInstr(TID, DL, NoImp);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000199}
Chris Lattnere70cab02006-10-03 19:18:57 +0000200
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000201/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
202/// 'Orig' instruction, identical in all ways except the the instruction
203/// has no parent, prev, or next.
204///
205MachineInstr *
206MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
207 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
208 MachineInstr(*this, *Orig);
209}
210
211/// DeleteMachineInstr - Delete the given MachineInstr.
212///
213void
214MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
215 // Clear the instructions memoperands. This must be done manually because
216 // the instruction's parent pointer is now null, so it can't properly
217 // deallocate them on its own.
218 MI->clearMemOperands(*this);
219
220 MI->~MachineInstr();
221 InstructionRecycler.Deallocate(Allocator, MI);
222}
223
224/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
225/// instead of `new MachineBasicBlock'.
226///
227MachineBasicBlock *
228MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
229 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
230 MachineBasicBlock(*this, bb);
231}
232
233/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
234///
235void
236MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
237 assert(MBB->getParent() == this && "MBB parent mismatch!");
238 MBB->~MachineBasicBlock();
239 BasicBlockRecycler.Deallocate(Allocator, MBB);
240}
241
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000242void MachineFunction::dump() const {
243 print(*cerr.stream());
244}
Chris Lattner10491642002-10-30 00:48:05 +0000245
246void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000247 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000248
249 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000250 FrameInfo->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000251
252 // Print JumpTable Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000253 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000254
255 // Print Constant Pool
Chris Lattner62ca3252008-08-23 22:53:13 +0000256 {
257 raw_os_ostream OSS(OS);
258 ConstantPool->print(OSS);
259 }
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000260
Dan Gohman6f0d0242008-02-10 18:45:23 +0000261 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000262
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000263 if (RegInfo && !RegInfo->livein_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000264 OS << "Live Ins:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000265 for (MachineRegisterInfo::livein_iterator
266 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000267 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000268 OS << " " << TRI->getName(I->first);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000269 else
270 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000271
272 if (I->second)
273 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000274 }
275 OS << "\n";
276 }
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000277 if (RegInfo && !RegInfo->liveout_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000278 OS << "Live Outs:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000279 for (MachineRegisterInfo::liveout_iterator
280 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Dan Gohman6f0d0242008-02-10 18:45:23 +0000281 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000282 OS << " " << TRI->getName(*I);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000283 else
284 OS << " Reg #" << *I;
285 OS << "\n";
286 }
287
Brian Gaeke90421cd2004-02-13 04:39:55 +0000288 for (const_iterator BB = begin(); BB != end(); ++BB)
289 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000290
291 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000292}
293
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000294/// CFGOnly flag - This is used to control whether or not the CFG graph printer
295/// prints out the contents of basic blocks or not. This is acceptable because
296/// this code is only really used for debugging purposes.
297///
298static bool CFGOnly = false;
299
300namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000301 template<>
302 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
303 static std::string getGraphName(const MachineFunction *F) {
304 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000305 }
306
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000307 static std::string getNodeLabel(const MachineBasicBlock *Node,
308 const MachineFunction *Graph) {
309 if (CFGOnly && Node->getBasicBlock() &&
310 !Node->getBasicBlock()->getName().empty())
311 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000312
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000313 std::ostringstream Out;
314 if (CFGOnly) {
315 Out << Node->getNumber() << ':';
316 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000317 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000318
319 Node->print(Out);
320
321 std::string OutStr = Out.str();
322 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
323
324 // Process string output to make it nicer...
325 for (unsigned i = 0; i != OutStr.length(); ++i)
326 if (OutStr[i] == '\n') { // Left justify
327 OutStr[i] = '\\';
328 OutStr.insert(OutStr.begin()+i+1, 'l');
329 }
330 return OutStr;
331 }
332 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000333}
334
335void MachineFunction::viewCFG() const
336{
Jim Laskey851a22d2005-10-12 12:09:05 +0000337#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000338 ViewGraph(this, "mf" + getFunction()->getName());
339#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000340 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
341 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000342#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000343}
344
345void MachineFunction::viewCFGOnly() const
346{
347 CFGOnly = true;
348 viewCFG();
349 CFGOnly = false;
350}
351
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000352// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000353// the MachineCodeForFunction object for the given function.
354// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000355// get() -- Returns a handle to the object.
356// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000357// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000358//
Misha Brukmanfce11432002-10-28 00:28:31 +0000359MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000360MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000361{
Chris Lattnere316efc2002-10-29 23:18:43 +0000362 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000363 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000364 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
365 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000366 return *mcInfo;
367}
368
Chris Lattner16c45e92003-12-20 10:20:58 +0000369void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000370 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner409a3d02008-04-06 21:46:45 +0000371 assert(Deleted && "Machine code did not exist for function!");
372 Deleted = Deleted; // silence warning when no assertions.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000373}
374
Chris Lattner335d5c32002-10-28 05:58:46 +0000375MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000376{
Chris Lattnere316efc2002-10-29 23:18:43 +0000377 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000378 assert(mc && "Call construct() method first to allocate the object");
379 return *mc;
380}
381
Evan Chengaaeea9e2009-01-27 21:15:07 +0000382/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
Bill Wendling85e3af92009-02-03 22:49:58 +0000383/// source file, line, and column. If none currently exists, create a new
384/// DebugLocTuple, and insert it into the DebugIdMap.
Evan Chengaaeea9e2009-01-27 21:15:07 +0000385unsigned MachineFunction::getOrCreateDebugLocID(unsigned Src, unsigned Line,
386 unsigned Col) {
Bill Wendling85e3af92009-02-03 22:49:58 +0000387 DebugLocTuple Tuple(Src, Line, Col);
Evan Chengaaeea9e2009-01-27 21:15:07 +0000388 DenseMap<DebugLocTuple, unsigned>::iterator II
389 = DebugLocInfo.DebugIdMap.find(Tuple);
Evan Chengd0adbb52009-01-26 07:41:49 +0000390 if (II != DebugLocInfo.DebugIdMap.end())
391 return II->second;
392 // Add a new tuple.
Evan Chengb9f66cf2009-01-26 23:47:30 +0000393 unsigned Id = DebugLocInfo.DebugLocations.size();
Evan Chengd0adbb52009-01-26 07:41:49 +0000394 DebugLocInfo.DebugLocations.push_back(Tuple);
Evan Chengb9f66cf2009-01-26 23:47:30 +0000395 DebugLocInfo.DebugIdMap[Tuple] = Id;
396 return Id;
Evan Chengd0adbb52009-01-26 07:41:49 +0000397}
398
Bill Wendling85e3af92009-02-03 22:49:58 +0000399/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
400const DebugLocTuple &MachineFunction::getDebugLocTuple(DebugLoc DL) {
401 unsigned Idx;
402 assert(Idx < DebugLocInfo.DebugLocations.size() &&
403 "Invalid index into debug locations!");
404 return DebugLocInfo.DebugLocations[Idx];
405}
406
Chris Lattner955fad12002-12-28 20:37:16 +0000407//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000408// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000409//===----------------------------------------------------------------------===//
410
Chris Lattner1612faa2008-01-25 07:19:06 +0000411/// CreateFixedObject - Create a new object at a fixed location on the stack.
412/// All fixed objects should be created before other objects are created for
413/// efficiency. By default, fixed objects are immutable. This returns an
414/// index with a negative value.
415///
416int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
417 bool Immutable) {
418 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
419 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
420 return -++NumFixedObjects;
421}
422
423
Chris Lattner9085d8a2003-01-16 18:35:57 +0000424void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Matthijs Kooijman06140882008-11-03 11:16:43 +0000425 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
426 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Chris Lattner9085d8a2003-01-16 18:35:57 +0000427
Chris Lattner955fad12002-12-28 20:37:16 +0000428 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
429 const StackObject &SO = Objects[i];
Dan Gohman26367472008-10-15 02:57:38 +0000430 OS << " <fi#" << (int)(i-NumFixedObjects) << ">: ";
Evan Chengd3653122008-02-27 03:04:06 +0000431 if (SO.Size == ~0ULL) {
432 OS << "dead\n";
433 continue;
434 }
Chris Lattner955fad12002-12-28 20:37:16 +0000435 if (SO.Size == 0)
436 OS << "variable sized";
437 else
Chris Lattner0db07092005-05-13 22:54:44 +0000438 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
439 OS << " alignment is " << SO.Alignment << " byte"
440 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000441
Chris Lattner955fad12002-12-28 20:37:16 +0000442 if (i < NumFixedObjects)
443 OS << " fixed";
444 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000445 int64_t Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000446 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000447 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000448 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000449 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000450 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000451 OS << "]";
452 }
453 OS << "\n";
454 }
455
456 if (HasVarSizedObjects)
457 OS << " Stack frame contains variable sized objects\n";
458}
459
Chris Lattner9085d8a2003-01-16 18:35:57 +0000460void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000461 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000462}
Chris Lattner955fad12002-12-28 20:37:16 +0000463
464
465//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000466// MachineJumpTableInfo implementation
467//===----------------------------------------------------------------------===//
468
469/// getJumpTableIndex - Create a new jump table entry in the jump table info
470/// or return an existing one.
471///
472unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000473 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000474 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000475 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
476 if (JumpTables[i].MBBs == DestBBs)
477 return i;
478
479 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
480 return JumpTables.size()-1;
481}
482
483
484void MachineJumpTableInfo::print(std::ostream &OS) const {
485 // FIXME: this is lame, maybe we could print out the MBB numbers or something
486 // like {1, 2, 4, 5, 3, 0}
487 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman26367472008-10-15 02:57:38 +0000488 OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
Nate Begeman37efe672006-04-22 18:53:45 +0000489 << " entries\n";
490 }
491}
492
Bill Wendlingbcd24982006-12-07 20:28:15 +0000493void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000494
495
496//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000497// MachineConstantPool implementation
498//===----------------------------------------------------------------------===//
499
Evan Cheng9abd7c32006-09-14 05:50:57 +0000500const Type *MachineConstantPoolEntry::getType() const {
501 if (isMachineConstantPoolEntry())
502 return Val.MachineCPVal->getType();
503 return Val.ConstVal->getType();
504}
505
Evan Chengd6594ae2006-09-12 21:00:35 +0000506MachineConstantPool::~MachineConstantPool() {
507 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
508 if (Constants[i].isMachineConstantPoolEntry())
509 delete Constants[i].Val.MachineCPVal;
510}
511
Chris Lattner3029f922006-02-09 04:46:04 +0000512/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman05ae9832008-09-16 20:45:53 +0000513/// an existing one. User must specify the log2 of the minimum required
514/// alignment for the object.
Chris Lattner3029f922006-02-09 04:46:04 +0000515///
516unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
517 unsigned Alignment) {
518 assert(Alignment && "Alignment must be specified!");
519 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
520
521 // Check to see if we already have this constant.
522 //
523 // FIXME, this could be made much more efficient for large constant pools.
524 unsigned AlignMask = (1 << Alignment)-1;
525 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Chengd6594ae2006-09-12 21:00:35 +0000526 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000527 return i;
528
529 unsigned Offset = 0;
530 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000531 Offset = Constants.back().getOffset();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000532 Offset += TD->getTypePaddedSize(Constants.back().getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000533 Offset = (Offset+AlignMask)&~AlignMask;
534 }
535
536 Constants.push_back(MachineConstantPoolEntry(C, Offset));
537 return Constants.size()-1;
538}
539
Evan Chengd6594ae2006-09-12 21:00:35 +0000540unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
541 unsigned Alignment) {
542 assert(Alignment && "Alignment must be specified!");
543 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
544
545 // Check to see if we already have this constant.
546 //
547 // FIXME, this could be made much more efficient for large constant pools.
548 unsigned AlignMask = (1 << Alignment)-1;
549 int Idx = V->getExistingMachineCPValue(this, Alignment);
550 if (Idx != -1)
551 return (unsigned)Idx;
552
553 unsigned Offset = 0;
554 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000555 Offset = Constants.back().getOffset();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000556 Offset += TD->getTypePaddedSize(Constants.back().getType());
Evan Chengd6594ae2006-09-12 21:00:35 +0000557 Offset = (Offset+AlignMask)&~AlignMask;
558 }
559
560 Constants.push_back(MachineConstantPoolEntry(V, Offset));
561 return Constants.size()-1;
562}
563
Chris Lattner62ca3252008-08-23 22:53:13 +0000564void MachineConstantPool::print(raw_ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000565 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman26367472008-10-15 02:57:38 +0000566 OS << " <cp#" << i << "> is";
Evan Chengd6594ae2006-09-12 21:00:35 +0000567 if (Constants[i].isMachineConstantPoolEntry())
568 Constants[i].Val.MachineCPVal->print(OS);
569 else
570 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Chengd472ad72006-12-22 02:04:05 +0000571 OS << " , offset=" << Constants[i].getOffset();
Evan Chengb8973bd2006-01-31 22:23:14 +0000572 OS << "\n";
573 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000574}
575
Chris Lattner62ca3252008-08-23 22:53:13 +0000576void MachineConstantPool::dump() const { print(errs()); errs().flush(); }