blob: 8bcf0124a8fac6208ae7d09de2eab7103a5fa29b [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 Lattner266c7bb2009-04-13 05:44:34 +000038bool 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 Lattner227c3d32002-10-28 01:12:41 +000046
Chris Lattner227c3d32002-10-28 01:12:41 +000047namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000048 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000049 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000050
Brian Gaeke09caa372004-01-30 21:53:46 +000051 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000052 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000053
Dan Gohman80f3d462008-07-21 19:48:15 +000054 Printer (std::ostream *os, const std::string &banner)
Dan Gohmanae73dc12008-09-04 17:05:41 +000055 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
Brian Gaeke09caa372004-01-30 21:53:46 +000056
Chris Lattner10491642002-10-30 00:48:05 +000057 const char *getPassName() const { return "MachineFunction Printer"; }
58
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 }
62
Chris Lattner16c45e92003-12-20 10:20:58 +000063 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000064 (*OS) << Banner;
65 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000066 return false;
67 }
68 };
Devang Patel19974732007-05-03 01:11:54 +000069 char Printer::ID = 0;
Chris Lattner227c3d32002-10-28 01:12:41 +000070}
71
Brian Gaeke09caa372004-01-30 21:53:46 +000072/// Returns a newly-created MachineFunction Printer pass. The default output
73/// stream is std::cerr; the default banner is empty.
74///
75FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000076 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000077 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000078}
79
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000080namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000081 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000082 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +000083 Deleter() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000084
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000085 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 Patel19974732007-05-03 01:11:54 +000093 char Deleter::ID = 0;
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000094}
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.
99FunctionPass *llvm::createMachineCodeDeleter() {
100 return new Deleter();
101}
102
103
104
Chris Lattner227c3d32002-10-28 01:12:41 +0000105//===---------------------------------------------------------------------===//
106// MachineFunction implementation
107//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +0000108
Dan Gohmanfed90b62008-07-28 21:51:04 +0000109void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000110 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattner792699c2004-05-24 06:11:51 +0000111}
Chris Lattner227c3d32002-10-28 01:12:41 +0000112
Chris Lattner10491642002-10-30 00:48:05 +0000113MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000114 const TargetMachine &TM)
Owen Anderson2b2c0d712009-05-14 19:17:24 +0000115 : Annotation(AnnotationManager::getID("CodeGen::MachineCodeForFunction")),
116 Fn(F), Target(TM) {
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000117 if (TM.getRegisterInfo())
118 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
119 MachineRegisterInfo(*TM.getRegisterInfo());
120 else
121 RegInfo = 0;
Chris Lattnerad828162004-08-16 22:36:34 +0000122 MFInfo = 0;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000123 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
124 MachineFrameInfo(*TM.getFrameInfo());
125 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
126 MachineConstantPool(TM.getTargetData());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000127
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 Lattnerd2b7cec2007-02-14 05:52:17 +0000132 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
Chris Lattner58092e32007-01-20 22:35:55 +0000133 : TD.getPointerABIAlignment();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000134 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
135 MachineJumpTableInfo(EntrySize, Alignment);
Chris Lattner831fdcf2002-12-25 05:03:22 +0000136}
137
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000138MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000139 BasicBlocks.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000140 InstructionRecycler.clear(Allocator);
141 BasicBlockRecycler.clear(Allocator);
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000142 if (RegInfo)
143 RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000144 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 Lattner10491642002-10-30 00:48:05 +0000150}
151
Chris Lattnere70cab02006-10-03 19:18:57 +0000152
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.
158void 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 Lattnerf28bbda2006-10-03 20:19:23 +0000168 if (MBBI != begin())
169 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000170
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 Gohman8e5f2c62008-07-07 23:14:23 +0000195/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
196/// of `new MachineInstr'.
197///
198MachineInstr *
Bill Wendling9bc96a52009-02-03 00:55:04 +0000199MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
200 DebugLoc DL, bool NoImp) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000201 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling9bc96a52009-02-03 00:55:04 +0000202 MachineInstr(TID, DL, NoImp);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000203}
Chris Lattnere70cab02006-10-03 19:18:57 +0000204
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000205/// 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///
209MachineInstr *
210MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
211 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
212 MachineInstr(*this, *Orig);
213}
214
215/// DeleteMachineInstr - Delete the given MachineInstr.
216///
217void
218MachineFunction::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///
231MachineBasicBlock *
232MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
233 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
234 MachineBasicBlock(*this, bb);
235}
236
237/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
238///
239void
240MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
241 assert(MBB->getParent() == this && "MBB parent mismatch!");
242 MBB->~MachineBasicBlock();
243 BasicBlockRecycler.Deallocate(Allocator, MBB);
244}
245
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000246void MachineFunction::dump() const {
247 print(*cerr.stream());
248}
Chris Lattner10491642002-10-30 00:48:05 +0000249
250void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000251 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000252
253 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000254 FrameInfo->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000255
256 // Print JumpTable Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000257 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000258
259 // Print Constant Pool
Chris Lattner62ca3252008-08-23 22:53:13 +0000260 {
261 raw_os_ostream OSS(OS);
262 ConstantPool->print(OSS);
263 }
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000264
Dan Gohman6f0d0242008-02-10 18:45:23 +0000265 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000266
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000267 if (RegInfo && !RegInfo->livein_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000268 OS << "Live Ins:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000269 for (MachineRegisterInfo::livein_iterator
270 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000271 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000272 OS << " " << TRI->getName(I->first);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000273 else
274 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000275
276 if (I->second)
277 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000278 }
279 OS << "\n";
280 }
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000281 if (RegInfo && !RegInfo->liveout_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000282 OS << "Live Outs:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000283 for (MachineRegisterInfo::liveout_iterator
284 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Dan Gohman6f0d0242008-02-10 18:45:23 +0000285 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000286 OS << " " << TRI->getName(*I);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000287 else
288 OS << " Reg #" << *I;
289 OS << "\n";
290 }
291
Brian Gaeke90421cd2004-02-13 04:39:55 +0000292 for (const_iterator BB = begin(); BB != end(); ++BB)
293 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000294
295 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000296}
297
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000298namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000299 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 Evlogimenos71bf4042004-07-08 00:47:58 +0000303 }
304
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000305 static std::string getNodeLabel(const MachineBasicBlock *Node,
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000306 const MachineFunction *Graph,
307 bool ShortNames) {
308 if (ShortNames && Node->getBasicBlock() &&
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000309 !Node->getBasicBlock()->getName().empty())
310 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000311
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000312 std::ostringstream Out;
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000313 if (ShortNames) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000314 Out << Node->getNumber() << ':';
315 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000316 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000317
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 Evlogimenos71bf4042004-07-08 00:47:58 +0000332}
333
334void MachineFunction::viewCFG() const
335{
Jim Laskey851a22d2005-10-12 12:09:05 +0000336#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000337 ViewGraph(this, "mf" + getFunction()->getName());
338#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000339 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
340 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000341#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000342}
343
344void MachineFunction::viewCFGOnly() const
345{
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000346#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 Evlogimenos71bf4042004-07-08 00:47:58 +0000352}
353
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000354// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000355// the MachineCodeForFunction object for the given function.
356// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000357// get() -- Returns a handle to the object.
358// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000359// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000360//
Misha Brukmanfce11432002-10-28 00:28:31 +0000361MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000362MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000363{
Owen Anderson2b2c0d712009-05-14 19:17:24 +0000364 AnnotationID MF_AID =
365 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
Chris Lattnere316efc2002-10-29 23:18:43 +0000366 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000367 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000368 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
369 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000370 return *mcInfo;
371}
372
Chris Lattner16c45e92003-12-20 10:20:58 +0000373void MachineFunction::destruct(const Function *Fn) {
Owen Anderson2b2c0d712009-05-14 19:17:24 +0000374 AnnotationID MF_AID =
375 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
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{
Owen Anderson2b2c0d712009-05-14 19:17:24 +0000383 AnnotationID MF_AID =
384 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
Chris Lattnere316efc2002-10-29 23:18:43 +0000385 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000386 assert(mc && "Call construct() method first to allocate the object");
387 return *mc;
388}
389
Bob Wilson998e1252009-04-20 18:36:57 +0000390/// addLiveIn - Add the specified physical register as a live-in value and
391/// create a corresponding virtual register for it.
392unsigned 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 Chengaaeea9e2009-01-27 21:15:07 +0000400/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
Bill Wendling85e3af92009-02-03 22:49:58 +0000401/// source file, line, and column. If none currently exists, create a new
402/// DebugLocTuple, and insert it into the DebugIdMap.
Argyrios Kyrtzidisa26eae62009-04-30 23:22:31 +0000403unsigned MachineFunction::getOrCreateDebugLocID(GlobalVariable *CompileUnit,
404 unsigned Line, unsigned Col) {
Bill Wendlingdf7d5d32009-05-21 00:04:55 +0000405 DebugLocTuple Tuple(CompileUnit, Line, Col);
Evan Chengaaeea9e2009-01-27 21:15:07 +0000406 DenseMap<DebugLocTuple, unsigned>::iterator II
407 = DebugLocInfo.DebugIdMap.find(Tuple);
Evan Chengd0adbb52009-01-26 07:41:49 +0000408 if (II != DebugLocInfo.DebugIdMap.end())
409 return II->second;
410 // Add a new tuple.
Evan Chengb9f66cf2009-01-26 23:47:30 +0000411 unsigned Id = DebugLocInfo.DebugLocations.size();
Evan Chengd0adbb52009-01-26 07:41:49 +0000412 DebugLocInfo.DebugLocations.push_back(Tuple);
Evan Chengb9f66cf2009-01-26 23:47:30 +0000413 DebugLocInfo.DebugIdMap[Tuple] = Id;
414 return Id;
Evan Chengd0adbb52009-01-26 07:41:49 +0000415}
416
Bill Wendling85e3af92009-02-03 22:49:58 +0000417/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
Bill Wendlinga929c682009-02-04 00:05:34 +0000418DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
Bill Wendling44f6ac62009-02-03 22:55:54 +0000419 unsigned Idx = DL.getIndex();
Bill Wendling85e3af92009-02-03 22:49:58 +0000420 assert(Idx < DebugLocInfo.DebugLocations.size() &&
421 "Invalid index into debug locations!");
422 return DebugLocInfo.DebugLocations[Idx];
423}
424
Chris Lattner955fad12002-12-28 20:37:16 +0000425//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000426// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000427//===----------------------------------------------------------------------===//
428
Chris Lattner1612faa2008-01-25 07:19:06 +0000429/// 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///
434int 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 Lattner9085d8a2003-01-16 18:35:57 +0000442void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Matthijs Kooijman06140882008-11-03 11:16:43 +0000443 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
444 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Chris Lattner9085d8a2003-01-16 18:35:57 +0000445
Chris Lattner955fad12002-12-28 20:37:16 +0000446 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
447 const StackObject &SO = Objects[i];
Dan Gohman26367472008-10-15 02:57:38 +0000448 OS << " <fi#" << (int)(i-NumFixedObjects) << ">: ";
Evan Chengd3653122008-02-27 03:04:06 +0000449 if (SO.Size == ~0ULL) {
450 OS << "dead\n";
451 continue;
452 }
Chris Lattner955fad12002-12-28 20:37:16 +0000453 if (SO.Size == 0)
454 OS << "variable sized";
455 else
Chris Lattner0db07092005-05-13 22:54:44 +0000456 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
457 OS << " alignment is " << SO.Alignment << " byte"
458 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000459
Chris Lattner955fad12002-12-28 20:37:16 +0000460 if (i < NumFixedObjects)
461 OS << " fixed";
462 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000463 int64_t Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000464 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000465 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000466 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000467 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000468 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000469 OS << "]";
470 }
471 OS << "\n";
472 }
473
474 if (HasVarSizedObjects)
475 OS << " Stack frame contains variable sized objects\n";
476}
477
Chris Lattner9085d8a2003-01-16 18:35:57 +0000478void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000479 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000480}
Chris Lattner955fad12002-12-28 20:37:16 +0000481
482
483//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000484// MachineJumpTableInfo implementation
485//===----------------------------------------------------------------------===//
486
487/// getJumpTableIndex - Create a new jump table entry in the jump table info
488/// or return an existing one.
489///
490unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000491 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000492 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000493 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 Gohman593ea052009-04-15 01:18:49 +0000501/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
502/// the jump tables to branch to New instead.
503bool
504MachineJumpTableInfo::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 Begeman37efe672006-04-22 18:53:45 +0000518
519void 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 Gohman26367472008-10-15 02:57:38 +0000523 OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
Nate Begeman37efe672006-04-22 18:53:45 +0000524 << " entries\n";
525 }
526}
527
Bill Wendlingbcd24982006-12-07 20:28:15 +0000528void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000529
530
531//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000532// MachineConstantPool implementation
533//===----------------------------------------------------------------------===//
534
Evan Cheng9abd7c32006-09-14 05:50:57 +0000535const Type *MachineConstantPoolEntry::getType() const {
536 if (isMachineConstantPoolEntry())
537 return Val.MachineCPVal->getType();
538 return Val.ConstVal->getType();
539}
540
Evan Chengd6594ae2006-09-12 21:00:35 +0000541MachineConstantPool::~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 Lattner3029f922006-02-09 04:46:04 +0000547/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman05ae9832008-09-16 20:45:53 +0000548/// an existing one. User must specify the log2 of the minimum required
549/// alignment for the object.
Chris Lattner3029f922006-02-09 04:46:04 +0000550///
551unsigned 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 Lattner3029f922006-02-09 04:46:04 +0000559 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Cheng1606e8e2009-03-13 07:51:59 +0000560 if (Constants[i].Val.ConstVal == C &&
561 (Constants[i].getAlignment() & (Alignment - 1)) == 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000562 return i;
563
Evan Cheng1606e8e2009-03-13 07:51:59 +0000564 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Chris Lattner3029f922006-02-09 04:46:04 +0000565 return Constants.size()-1;
566}
567
Evan Chengd6594ae2006-09-12 21:00:35 +0000568unsigned 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 Chengd6594ae2006-09-12 21:00:35 +0000576 int Idx = V->getExistingMachineCPValue(this, Alignment);
577 if (Idx != -1)
578 return (unsigned)Idx;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000579
580 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Evan Chengd6594ae2006-09-12 21:00:35 +0000581 return Constants.size()-1;
582}
583
Chris Lattner62ca3252008-08-23 22:53:13 +0000584void MachineConstantPool::print(raw_ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000585 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman26367472008-10-15 02:57:38 +0000586 OS << " <cp#" << i << "> is";
Evan Chengd6594ae2006-09-12 21:00:35 +0000587 if (Constants[i].isMachineConstantPoolEntry())
588 Constants[i].Val.MachineCPVal->print(OS);
589 else
590 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000591 OS << " , alignment=" << Constants[i].getAlignment();
Evan Chengb8973bd2006-01-31 22:23:14 +0000592 OS << "\n";
593 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000594}
595
Dan Gohmanf871ccb2009-03-23 15:57:19 +0000596void MachineConstantPool::dump() const { print(errs()); }