blob: fc0e99fe4bbb84af48dcb5637153ff3ece585b60 [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 Lattner266c7bb2009-04-13 05:44:34 +000041bool 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}
Chris Lattner227c3d32002-10-28 01:12:41 +000049
Chris Lattner227c3d32002-10-28 01:12:41 +000050namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000051 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000052 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000053
Brian Gaeke09caa372004-01-30 21:53:46 +000054 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000055 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000056
Dan Gohman80f3d462008-07-21 19:48:15 +000057 Printer (std::ostream *os, const std::string &banner)
Dan Gohmanae73dc12008-09-04 17:05:41 +000058 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
Brian Gaeke09caa372004-01-30 21:53:46 +000059
Chris Lattner10491642002-10-30 00:48:05 +000060 const char *getPassName() const { return "MachineFunction Printer"; }
61
62 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
63 AU.setPreservesAll();
64 }
65
Chris Lattner16c45e92003-12-20 10:20:58 +000066 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000067 (*OS) << Banner;
68 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000069 return false;
70 }
71 };
Devang Patel19974732007-05-03 01:11:54 +000072 char Printer::ID = 0;
Chris Lattner227c3d32002-10-28 01:12:41 +000073}
74
Brian Gaeke09caa372004-01-30 21:53:46 +000075/// Returns a newly-created MachineFunction Printer pass. The default output
76/// stream is std::cerr; the default banner is empty.
77///
78FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000079 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000080 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000081}
82
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000083namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000084 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000085 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000086 Deleter() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000087
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000088 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 };
Devang Patel19974732007-05-03 01:11:54 +000096 char Deleter::ID = 0;
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000097}
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.
102FunctionPass *llvm::createMachineCodeDeleter() {
103 return new Deleter();
104}
105
106
107
Chris Lattner227c3d32002-10-28 01:12:41 +0000108//===---------------------------------------------------------------------===//
109// MachineFunction implementation
110//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +0000111
Dan Gohmanfed90b62008-07-28 21:51:04 +0000112void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000113 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattner792699c2004-05-24 06:11:51 +0000114}
Chris Lattner227c3d32002-10-28 01:12:41 +0000115
Chris Lattner10491642002-10-30 00:48:05 +0000116MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000117 const TargetMachine &TM)
Evan Cheng505e5512007-04-25 22:10:09 +0000118 : Annotation(MF_AID), Fn(F), Target(TM) {
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000119 if (TM.getRegisterInfo())
120 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
121 MachineRegisterInfo(*TM.getRegisterInfo());
122 else
123 RegInfo = 0;
Chris Lattnerad828162004-08-16 22:36:34 +0000124 MFInfo = 0;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000125 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
126 MachineFrameInfo(*TM.getFrameInfo());
127 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
128 MachineConstantPool(TM.getTargetData());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000129
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();
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000134 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
Chris Lattner58092e32007-01-20 22:35:55 +0000135 : TD.getPointerABIAlignment();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000136 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
137 MachineJumpTableInfo(EntrySize, Alignment);
Chris Lattner831fdcf2002-12-25 05:03:22 +0000138}
139
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000140MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000141 BasicBlocks.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000142 InstructionRecycler.clear(Allocator);
143 BasicBlockRecycler.clear(Allocator);
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000144 if (RegInfo)
145 RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000146 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);
Chris Lattner10491642002-10-30 00:48:05 +0000152}
153
Chris Lattnere70cab02006-10-03 19:18:57 +0000154
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.
160void 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;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000170 if (MBBI != begin())
171 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000172
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 Gohman8e5f2c62008-07-07 23:14:23 +0000197/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
198/// of `new MachineInstr'.
199///
200MachineInstr *
Bill Wendling9bc96a52009-02-03 00:55:04 +0000201MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
202 DebugLoc DL, bool NoImp) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000203 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling9bc96a52009-02-03 00:55:04 +0000204 MachineInstr(TID, DL, NoImp);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000205}
Chris Lattnere70cab02006-10-03 19:18:57 +0000206
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000207/// 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///
211MachineInstr *
212MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
213 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
214 MachineInstr(*this, *Orig);
215}
216
217/// DeleteMachineInstr - Delete the given MachineInstr.
218///
219void
220MachineFunction::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///
233MachineBasicBlock *
234MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
235 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
236 MachineBasicBlock(*this, bb);
237}
238
239/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
240///
241void
242MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
243 assert(MBB->getParent() == this && "MBB parent mismatch!");
244 MBB->~MachineBasicBlock();
245 BasicBlockRecycler.Deallocate(Allocator, MBB);
246}
247
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000248void MachineFunction::dump() const {
249 print(*cerr.stream());
250}
Chris Lattner10491642002-10-30 00:48:05 +0000251
252void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000253 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000254
255 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000256 FrameInfo->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000257
258 // Print JumpTable Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000259 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000260
261 // Print Constant Pool
Chris Lattner62ca3252008-08-23 22:53:13 +0000262 {
263 raw_os_ostream OSS(OS);
264 ConstantPool->print(OSS);
265 }
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000266
Dan Gohman6f0d0242008-02-10 18:45:23 +0000267 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000268
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000269 if (RegInfo && !RegInfo->livein_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000270 OS << "Live Ins:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000271 for (MachineRegisterInfo::livein_iterator
272 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000273 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000274 OS << " " << TRI->getName(I->first);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000275 else
276 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000277
278 if (I->second)
279 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000280 }
281 OS << "\n";
282 }
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000283 if (RegInfo && !RegInfo->liveout_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000284 OS << "Live Outs:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000285 for (MachineRegisterInfo::liveout_iterator
286 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Dan Gohman6f0d0242008-02-10 18:45:23 +0000287 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000288 OS << " " << TRI->getName(*I);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000289 else
290 OS << " Reg #" << *I;
291 OS << "\n";
292 }
293
Brian Gaeke90421cd2004-02-13 04:39:55 +0000294 for (const_iterator BB = begin(); BB != end(); ++BB)
295 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000296
297 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000298}
299
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000300/// 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///
304static bool CFGOnly = false;
305
306namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000307 template<>
308 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
309 static std::string getGraphName(const MachineFunction *F) {
310 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000311 }
312
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000313 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() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000318
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000319 std::ostringstream Out;
320 if (CFGOnly) {
321 Out << Node->getNumber() << ':';
322 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000323 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000324
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 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000339}
340
341void MachineFunction::viewCFG() const
342{
Jim Laskey851a22d2005-10-12 12:09:05 +0000343#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000344 ViewGraph(this, "mf" + getFunction()->getName());
345#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000346 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
347 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000348#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000349}
350
351void MachineFunction::viewCFGOnly() const
352{
353 CFGOnly = true;
354 viewCFG();
355 CFGOnly = false;
356}
357
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000358// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000359// the MachineCodeForFunction object for the given function.
360// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000361// get() -- Returns a handle to the object.
362// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000363// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000364//
Misha Brukmanfce11432002-10-28 00:28:31 +0000365MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000366MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000367{
Chris Lattnere316efc2002-10-29 23:18:43 +0000368 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000369 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000370 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
371 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000372 return *mcInfo;
373}
374
Chris Lattner16c45e92003-12-20 10:20:58 +0000375void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000376 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner409a3d02008-04-06 21:46:45 +0000377 assert(Deleted && "Machine code did not exist for function!");
378 Deleted = Deleted; // silence warning when no assertions.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000379}
380
Chris Lattner335d5c32002-10-28 05:58:46 +0000381MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000382{
Chris Lattnere316efc2002-10-29 23:18:43 +0000383 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000384 assert(mc && "Call construct() method first to allocate the object");
385 return *mc;
386}
387
Evan Chengaaeea9e2009-01-27 21:15:07 +0000388/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
Bill Wendling85e3af92009-02-03 22:49:58 +0000389/// source file, line, and column. If none currently exists, create a new
390/// DebugLocTuple, and insert it into the DebugIdMap.
Evan Chengaaeea9e2009-01-27 21:15:07 +0000391unsigned MachineFunction::getOrCreateDebugLocID(unsigned Src, unsigned Line,
392 unsigned Col) {
Bill Wendling85e3af92009-02-03 22:49:58 +0000393 DebugLocTuple Tuple(Src, Line, Col);
Evan Chengaaeea9e2009-01-27 21:15:07 +0000394 DenseMap<DebugLocTuple, unsigned>::iterator II
395 = DebugLocInfo.DebugIdMap.find(Tuple);
Evan Chengd0adbb52009-01-26 07:41:49 +0000396 if (II != DebugLocInfo.DebugIdMap.end())
397 return II->second;
398 // Add a new tuple.
Evan Chengb9f66cf2009-01-26 23:47:30 +0000399 unsigned Id = DebugLocInfo.DebugLocations.size();
Evan Chengd0adbb52009-01-26 07:41:49 +0000400 DebugLocInfo.DebugLocations.push_back(Tuple);
Evan Chengb9f66cf2009-01-26 23:47:30 +0000401 DebugLocInfo.DebugIdMap[Tuple] = Id;
402 return Id;
Evan Chengd0adbb52009-01-26 07:41:49 +0000403}
404
Bill Wendling85e3af92009-02-03 22:49:58 +0000405/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
Bill Wendlinga929c682009-02-04 00:05:34 +0000406DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
Bill Wendling44f6ac62009-02-03 22:55:54 +0000407 unsigned Idx = DL.getIndex();
Bill Wendling85e3af92009-02-03 22:49:58 +0000408 assert(Idx < DebugLocInfo.DebugLocations.size() &&
409 "Invalid index into debug locations!");
410 return DebugLocInfo.DebugLocations[Idx];
411}
412
Chris Lattner955fad12002-12-28 20:37:16 +0000413//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000414// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000415//===----------------------------------------------------------------------===//
416
Chris Lattner1612faa2008-01-25 07:19:06 +0000417/// 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///
422int 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
Chris Lattner9085d8a2003-01-16 18:35:57 +0000430void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Matthijs Kooijman06140882008-11-03 11:16:43 +0000431 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
432 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Chris Lattner9085d8a2003-01-16 18:35:57 +0000433
Chris Lattner955fad12002-12-28 20:37:16 +0000434 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
435 const StackObject &SO = Objects[i];
Dan Gohman26367472008-10-15 02:57:38 +0000436 OS << " <fi#" << (int)(i-NumFixedObjects) << ">: ";
Evan Chengd3653122008-02-27 03:04:06 +0000437 if (SO.Size == ~0ULL) {
438 OS << "dead\n";
439 continue;
440 }
Chris Lattner955fad12002-12-28 20:37:16 +0000441 if (SO.Size == 0)
442 OS << "variable sized";
443 else
Chris Lattner0db07092005-05-13 22:54:44 +0000444 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
445 OS << " alignment is " << SO.Alignment << " byte"
446 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000447
Chris Lattner955fad12002-12-28 20:37:16 +0000448 if (i < NumFixedObjects)
449 OS << " fixed";
450 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000451 int64_t Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000452 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000453 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000454 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000455 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000456 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000457 OS << "]";
458 }
459 OS << "\n";
460 }
461
462 if (HasVarSizedObjects)
463 OS << " Stack frame contains variable sized objects\n";
464}
465
Chris Lattner9085d8a2003-01-16 18:35:57 +0000466void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000467 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000468}
Chris Lattner955fad12002-12-28 20:37:16 +0000469
470
471//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000472// MachineJumpTableInfo implementation
473//===----------------------------------------------------------------------===//
474
475/// getJumpTableIndex - Create a new jump table entry in the jump table info
476/// or return an existing one.
477///
478unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000479 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000480 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000481 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 Gohman593ea052009-04-15 01:18:49 +0000489/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
490/// the jump tables to branch to New instead.
491bool
492MachineJumpTableInfo::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}
Nate Begeman37efe672006-04-22 18:53:45 +0000506
507void 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 Gohman26367472008-10-15 02:57:38 +0000511 OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
Nate Begeman37efe672006-04-22 18:53:45 +0000512 << " entries\n";
513 }
514}
515
Bill Wendlingbcd24982006-12-07 20:28:15 +0000516void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000517
518
519//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000520// MachineConstantPool implementation
521//===----------------------------------------------------------------------===//
522
Evan Cheng9abd7c32006-09-14 05:50:57 +0000523const Type *MachineConstantPoolEntry::getType() const {
524 if (isMachineConstantPoolEntry())
525 return Val.MachineCPVal->getType();
526 return Val.ConstVal->getType();
527}
528
Evan Chengd6594ae2006-09-12 21:00:35 +0000529MachineConstantPool::~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
Chris Lattner3029f922006-02-09 04:46:04 +0000535/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman05ae9832008-09-16 20:45:53 +0000536/// an existing one. User must specify the log2 of the minimum required
537/// alignment for the object.
Chris Lattner3029f922006-02-09 04:46:04 +0000538///
539unsigned 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.
Chris Lattner3029f922006-02-09 04:46:04 +0000547 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Cheng1606e8e2009-03-13 07:51:59 +0000548 if (Constants[i].Val.ConstVal == C &&
549 (Constants[i].getAlignment() & (Alignment - 1)) == 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000550 return i;
551
Evan Cheng1606e8e2009-03-13 07:51:59 +0000552 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Chris Lattner3029f922006-02-09 04:46:04 +0000553 return Constants.size()-1;
554}
555
Evan Chengd6594ae2006-09-12 21:00:35 +0000556unsigned 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.
Evan Chengd6594ae2006-09-12 21:00:35 +0000564 int Idx = V->getExistingMachineCPValue(this, Alignment);
565 if (Idx != -1)
566 return (unsigned)Idx;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000567
568 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Evan Chengd6594ae2006-09-12 21:00:35 +0000569 return Constants.size()-1;
570}
571
Chris Lattner62ca3252008-08-23 22:53:13 +0000572void MachineConstantPool::print(raw_ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000573 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman26367472008-10-15 02:57:38 +0000574 OS << " <cp#" << i << "> is";
Evan Chengd6594ae2006-09-12 21:00:35 +0000575 if (Constants[i].isMachineConstantPoolEntry())
576 Constants[i].Val.MachineCPVal->print(OS);
577 else
578 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000579 OS << " , alignment=" << Constants[i].getAlignment();
Evan Chengb8973bd2006-01-31 22:23:14 +0000580 OS << "\n";
581 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000582}
583
Dan Gohmanf871ccb2009-03-23 15:57:19 +0000584void MachineConstantPool::dump() const { print(errs()); }