blob: 18ea25f3c3604085622c5f292560d5557a9cadfb [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 Lattner16c45e92003-12-20 10:20:58 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000018#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner4d149cd2003-01-13 00:23:03 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000022#include "llvm/CodeGen/Passes.h"
Owen Anderson07000c62006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000025#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000026#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000027#include "llvm/Instructions.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/LeakDetector.h"
29#include "llvm/Support/GraphWriter.h"
Reid Spencer3e0c1542006-06-05 15:44:46 +000030#include "llvm/System/Path.h"
31#include "llvm/System/Program.h"
Jim Laskey851a22d2005-10-12 12:09:05 +000032#include "llvm/Config/config.h"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000033#include <fstream>
Reid Spencer954da372004-07-04 12:19:56 +000034#include <iostream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000035#include <sstream>
Tanya Lattner792699c2004-05-24 06:11:51 +000036
Chris Lattner07f32d42003-12-20 09:17:07 +000037using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000038
Chris Lattnere316efc2002-10-29 23:18:43 +000039static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000040 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000041
Chris Lattner227c3d32002-10-28 01:12:41 +000042
Chris Lattner227c3d32002-10-28 01:12:41 +000043namespace {
Chris Lattner16c45e92003-12-20 10:20:58 +000044 struct Printer : public MachineFunctionPass {
Brian Gaeke09caa372004-01-30 21:53:46 +000045 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000046 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000047
48 Printer (std::ostream *_OS, const std::string &_Banner) :
49 OS (_OS), Banner (_Banner) { }
50
Chris Lattner10491642002-10-30 00:48:05 +000051 const char *getPassName() const { return "MachineFunction Printer"; }
52
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.setPreservesAll();
55 }
56
Chris Lattner16c45e92003-12-20 10:20:58 +000057 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000058 (*OS) << Banner;
59 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000060 return false;
61 }
62 };
Chris Lattner227c3d32002-10-28 01:12:41 +000063}
64
Brian Gaeke09caa372004-01-30 21:53:46 +000065/// Returns a newly-created MachineFunction Printer pass. The default output
66/// stream is std::cerr; the default banner is empty.
67///
68FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000069 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000070 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000071}
72
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000073namespace {
74 struct Deleter : public MachineFunctionPass {
75 const char *getPassName() const { return "Machine Code Deleter"; }
76
77 bool runOnMachineFunction(MachineFunction &MF) {
78 // Delete the annotation from the function now.
79 MachineFunction::destruct(MF.getFunction());
80 return true;
81 }
82 };
83}
84
85/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
86/// the current function, which should happen after the function has been
87/// emitted to a .s file or to memory.
88FunctionPass *llvm::createMachineCodeDeleter() {
89 return new Deleter();
90}
91
92
93
Chris Lattner227c3d32002-10-28 01:12:41 +000094//===---------------------------------------------------------------------===//
95// MachineFunction implementation
96//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000097
Chris Lattnerbca81442005-01-30 00:09:23 +000098MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000099 MachineBasicBlock* dummy = new MachineBasicBlock();
100 LeakDetector::removeGarbageObject(dummy);
101 return dummy;
Tanya Lattner792699c2004-05-24 06:11:51 +0000102}
103
104void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000105 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
106 ilist_iterator<MachineBasicBlock> first,
Chris Lattner9d5d7592005-01-29 18:41:25 +0000107 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000108 if (Parent != toList.Parent)
109 for (; first != last; ++first)
110 first->Parent = toList.Parent;
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)
Jim Laskeyf99c2322006-01-04 13:43:56 +0000115 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000116 SSARegMapping = new SSARegMap();
Chris Lattnerad828162004-08-16 22:36:34 +0000117 MFInfo = 0;
Chris Lattnereb24db92002-12-28 21:08:26 +0000118 FrameInfo = new MachineFrameInfo();
Chris Lattner3029f922006-02-09 04:46:04 +0000119 ConstantPool = new MachineConstantPool(TM.getTargetData());
Nate Begeman37efe672006-04-22 18:53:45 +0000120 JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData());
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000121 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000122}
123
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000124MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000125 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000126 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000127 delete MFInfo;
128 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000129 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000130 delete JumpTableInfo;
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000131 delete[] UsedPhysRegs;
Chris Lattner10491642002-10-30 00:48:05 +0000132}
133
134void MachineFunction::dump() const { print(std::cerr); }
135
136void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000137 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000138
139 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000140 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000141
142 // Print JumpTable Information
143 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000144
145 // Print Constant Pool
146 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000147
148 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
149
150 if (livein_begin() != livein_end()) {
151 OS << "Live Ins:";
152 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
153 if (MRI)
154 OS << " " << MRI->getName(I->first);
155 else
156 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000157
158 if (I->second)
159 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000160 }
161 OS << "\n";
162 }
163 if (liveout_begin() != liveout_end()) {
164 OS << "Live Outs:";
165 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
166 if (MRI)
167 OS << " " << MRI->getName(*I);
168 else
169 OS << " Reg #" << *I;
170 OS << "\n";
171 }
172
Brian Gaeke90421cd2004-02-13 04:39:55 +0000173 for (const_iterator BB = begin(); BB != end(); ++BB)
174 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000175
176 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000177}
178
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000179/// CFGOnly flag - This is used to control whether or not the CFG graph printer
180/// prints out the contents of basic blocks or not. This is acceptable because
181/// this code is only really used for debugging purposes.
182///
183static bool CFGOnly = false;
184
185namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000186 template<>
187 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
188 static std::string getGraphName(const MachineFunction *F) {
189 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000190 }
191
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000192 static std::string getNodeLabel(const MachineBasicBlock *Node,
193 const MachineFunction *Graph) {
194 if (CFGOnly && Node->getBasicBlock() &&
195 !Node->getBasicBlock()->getName().empty())
196 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000197
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000198 std::ostringstream Out;
199 if (CFGOnly) {
200 Out << Node->getNumber() << ':';
201 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000202 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000203
204 Node->print(Out);
205
206 std::string OutStr = Out.str();
207 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
208
209 // Process string output to make it nicer...
210 for (unsigned i = 0; i != OutStr.length(); ++i)
211 if (OutStr[i] == '\n') { // Left justify
212 OutStr[i] = '\\';
213 OutStr.insert(OutStr.begin()+i+1, 'l');
214 }
215 return OutStr;
216 }
217 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000218}
219
220void MachineFunction::viewCFG() const
221{
Jim Laskey851a22d2005-10-12 12:09:05 +0000222#ifndef NDEBUG
Reid Spencer3e0c1542006-06-05 15:44:46 +0000223 char pathsuff[9];
224
225 sprintf(pathsuff, "%06u", unsigned(rand()));
226
227 sys::Path TempDir = sys::Path::GetTemporaryDirectory();
228 sys::Path Filename = TempDir;
229 Filename.appendComponent("mf" + getFunction()->getName() + "." + pathsuff + ".dot");
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000230 std::cerr << "Writing '" << Filename << "'... ";
231 std::ofstream F(Filename.c_str());
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000232
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000233 if (!F) {
234 std::cerr << " error opening file for writing!\n";
235 return;
236 }
237
238 WriteGraph(F, this);
239 F.close();
240 std::cerr << "\n";
241
Reid Spencer3e0c1542006-06-05 15:44:46 +0000242#if HAVE_GRAPHVIZ
243 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
244 std::vector<const char*> args;
245 args.push_back(Graphviz.c_str());
246 args.push_back(Filename.c_str());
247 args.push_back(0);
248
Jim Laskey851a22d2005-10-12 12:09:05 +0000249 std::cerr << "Running 'Graphviz' program... " << std::flush;
Reid Spencer3e0c1542006-06-05 15:44:46 +0000250 if (sys::Program::ExecuteAndWait(Graphviz, &args[0])) {
Jim Laskey851a22d2005-10-12 12:09:05 +0000251 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
252 } else {
Reid Spencer3e0c1542006-06-05 15:44:46 +0000253 Filename.eraseFromDisk();
Jim Laskey851a22d2005-10-12 12:09:05 +0000254 return;
255 }
Reid Spencer3e0c1542006-06-05 15:44:46 +0000256#elif (HAVE_GV && HAVE_DOT)
257 sys::Path PSFilename = TempDir;
258 PSFilename.appendComponent(std::string("mf.tempgraph") + "." + pathsuff + ".ps");
Jim Laskey851a22d2005-10-12 12:09:05 +0000259
Reid Spencer3e0c1542006-06-05 15:44:46 +0000260 sys::Path dot(LLVM_PATH_DOT);
261 std::vector<const char*> args;
262 args.push_back(dot.c_str());
263 args.push_back("-Tps");
264 args.push_back("-Nfontname=Courier");
265 args.push_back("-Gsize=7.5,10");
266 args.push_back(Filename.c_str());
267 args.push_back("-o");
268 args.push_back(PSFilename.c_str());
269 args.push_back(0);
270
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000271 std::cerr << "Running 'dot' program... " << std::flush;
Reid Spencer3e0c1542006-06-05 15:44:46 +0000272 if (sys::Program::ExecuteAndWait(dot, &args[0])) {
273 std::cerr << "Error viewing graph: 'dot' not in path?\n";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000274 } else {
275 std::cerr << "\n";
Reid Spencer3e0c1542006-06-05 15:44:46 +0000276
277 sys::Path gv(LLVM_PATH_GV);
278 args.clear();
279 args.push_back(gv.c_str());
280 args.push_back(PSFilename.c_str());
281 args.push_back(0);
282
283 sys::Program::ExecuteAndWait(gv, &args[0]);
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000284 }
Reid Spencer3e0c1542006-06-05 15:44:46 +0000285 Filename.eraseFromDisk();
286 PSFilename.eraseFromDisk();
Jim Laskey851a22d2005-10-12 12:09:05 +0000287 return;
Reid Spencer3e0c1542006-06-05 15:44:46 +0000288#elif HAVE_DOTTY
289 sys::Path dotty(LLVM_PATH_DOTTY);
290 std::vector<const char*> args;
291 args.push_back(dotty.c_str());
292 args.push_back(Filename.c_str());
293 args.push_back(0);
294
295 std::cerr << "Running 'dotty' program... " << std::flush;
296 if (sys::Program::ExecuteAndWait(dotty, &args[0])) {
297 std::cerr << "Error viewing graph: 'dotty' not in path?\n";
298 } else {
299#ifndef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
300 Filename.eraseFromDisk();
301#endif
302 return;
303 }
304#endif
305
Jim Laskey851a22d2005-10-12 12:09:05 +0000306#endif // NDEBUG
307 std::cerr << "MachineFunction::viewCFG is only available in debug builds on "
308 << "systems with Graphviz or gv!\n";
309
310#ifndef NDEBUG
Reid Spencer3e0c1542006-06-05 15:44:46 +0000311 Filename.eraseFromDisk();
312 TempDir.eraseFromDisk(true);
Jim Laskey851a22d2005-10-12 12:09:05 +0000313#endif
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000314}
315
316void MachineFunction::viewCFGOnly() const
317{
318 CFGOnly = true;
319 viewCFG();
320 CFGOnly = false;
321}
322
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000323// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000324// the MachineCodeForFunction object for the given function.
325// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000326// get() -- Returns a handle to the object.
327// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000328// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000329//
Misha Brukmanfce11432002-10-28 00:28:31 +0000330MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000331MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000332{
Chris Lattnere316efc2002-10-29 23:18:43 +0000333 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000334 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000335 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
336 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000337 return *mcInfo;
338}
339
Chris Lattner16c45e92003-12-20 10:20:58 +0000340void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000341 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000342 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000343}
344
Chris Lattner335d5c32002-10-28 05:58:46 +0000345MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000346{
Chris Lattnere316efc2002-10-29 23:18:43 +0000347 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000348 assert(mc && "Call construct() method first to allocate the object");
349 return *mc;
350}
351
Chris Lattner831fdcf2002-12-25 05:03:22 +0000352void MachineFunction::clearSSARegMap() {
353 delete SSARegMapping;
354 SSARegMapping = 0;
355}
356
Chris Lattner955fad12002-12-28 20:37:16 +0000357//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000358// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000359//===----------------------------------------------------------------------===//
360
Chris Lattner9085d8a2003-01-16 18:35:57 +0000361void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000362 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000363
Chris Lattner955fad12002-12-28 20:37:16 +0000364 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
365 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000366 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000367 if (SO.Size == 0)
368 OS << "variable sized";
369 else
Chris Lattner0db07092005-05-13 22:54:44 +0000370 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
371 OS << " alignment is " << SO.Alignment << " byte"
372 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000373
Chris Lattner955fad12002-12-28 20:37:16 +0000374 if (i < NumFixedObjects)
375 OS << " fixed";
376 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000377 int Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000378 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000379 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000380 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000381 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000382 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000383 OS << "]";
384 }
385 OS << "\n";
386 }
387
388 if (HasVarSizedObjects)
389 OS << " Stack frame contains variable sized objects\n";
390}
391
Chris Lattner9085d8a2003-01-16 18:35:57 +0000392void MachineFrameInfo::dump(const MachineFunction &MF) const {
393 print(MF, std::cerr);
394}
Chris Lattner955fad12002-12-28 20:37:16 +0000395
396
397//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000398// MachineJumpTableInfo implementation
399//===----------------------------------------------------------------------===//
400
401/// getJumpTableIndex - Create a new jump table entry in the jump table info
402/// or return an existing one.
403///
404unsigned MachineJumpTableInfo::getJumpTableIndex(
405 std::vector<MachineBasicBlock*> &DestBBs) {
406 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
407 if (JumpTables[i].MBBs == DestBBs)
408 return i;
409
410 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
411 return JumpTables.size()-1;
412}
413
414
415void MachineJumpTableInfo::print(std::ostream &OS) const {
416 // FIXME: this is lame, maybe we could print out the MBB numbers or something
417 // like {1, 2, 4, 5, 3, 0}
418 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
419 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
420 << " entries\n";
421 }
422}
423
Nate Begeman3700a4d2006-04-22 23:52:35 +0000424unsigned MachineJumpTableInfo::getEntrySize() const {
Owen Andersona69571c2006-05-03 01:29:57 +0000425 return TD->getPointerSize();
Nate Begeman3700a4d2006-04-22 23:52:35 +0000426}
427
428unsigned MachineJumpTableInfo::getAlignment() const {
Owen Andersona69571c2006-05-03 01:29:57 +0000429 return TD->getPointerAlignment();
Nate Begeman3700a4d2006-04-22 23:52:35 +0000430}
431
Nate Begeman37efe672006-04-22 18:53:45 +0000432void MachineJumpTableInfo::dump() const { print(std::cerr); }
433
434
435//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000436// MachineConstantPool implementation
437//===----------------------------------------------------------------------===//
438
Chris Lattner3029f922006-02-09 04:46:04 +0000439/// getConstantPoolIndex - Create a new entry in the constant pool or return
440/// an existing one. User must specify an alignment in bytes for the object.
441///
442unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
443 unsigned Alignment) {
444 assert(Alignment && "Alignment must be specified!");
445 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
446
447 // Check to see if we already have this constant.
448 //
449 // FIXME, this could be made much more efficient for large constant pools.
450 unsigned AlignMask = (1 << Alignment)-1;
451 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
452 if (Constants[i].Val == C && (Constants[i].Offset & AlignMask) == 0)
453 return i;
454
455 unsigned Offset = 0;
456 if (!Constants.empty()) {
457 Offset = Constants.back().Offset;
Owen Andersona69571c2006-05-03 01:29:57 +0000458 Offset += TD->getTypeSize(Constants.back().Val->getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000459 Offset = (Offset+AlignMask)&~AlignMask;
460 }
461
462 Constants.push_back(MachineConstantPoolEntry(C, Offset));
463 return Constants.size()-1;
464}
465
466
Chris Lattner4d149cd2003-01-13 00:23:03 +0000467void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000468 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000469 OS << " <cp #" << i << "> is" << *(Value*)Constants[i].Val;
Chris Lattner3029f922006-02-09 04:46:04 +0000470 OS << " , offset=" << Constants[i].Offset;
Evan Chengb8973bd2006-01-31 22:23:14 +0000471 OS << "\n";
472 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000473}
474
475void MachineConstantPool::dump() const { print(std::cerr); }