1 //===-- MachineFunction.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Collect native machine code information for a function. This allows
11 // target-specific information about the generated code to be stored with each
14 //===----------------------------------------------------------------------===//
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/SSARegMap.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineJumpTableInfo.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Target/TargetFrameInfo.h"
26 #include "llvm/Function.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/GraphWriter.h"
30 #include "llvm/Support/LeakDetector.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/Config/config.h"
39 static AnnotationID MF_AID(
40 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
42 // Out of line virtual function to home classes.
43 void MachineFunctionPass::virtfn() {}
46 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
48 const std::string Banner;
50 Printer (std::ostream *_OS, const std::string &_Banner) :
51 OS (_OS), Banner (_Banner) { }
53 const char *getPassName() const { return "MachineFunction Printer"; }
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 bool runOnMachineFunction(MachineFunction &MF) {
67 /// Returns a newly-created MachineFunction Printer pass. The default output
68 /// stream is std::cerr; the default banner is empty.
70 FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
71 const std::string &Banner){
72 return new Printer(OS, Banner);
76 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
77 const char *getPassName() const { return "Machine Code Deleter"; }
79 bool runOnMachineFunction(MachineFunction &MF) {
80 // Delete the annotation from the function now.
81 MachineFunction::destruct(MF.getFunction());
87 /// MachineCodeDeletion Pass - This pass deletes all of the machine code for
88 /// the current function, which should happen after the function has been
89 /// emitted to a .s file or to memory.
90 FunctionPass *llvm::createMachineCodeDeleter() {
96 //===---------------------------------------------------------------------===//
97 // MachineFunction implementation
98 //===---------------------------------------------------------------------===//
100 MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
101 MachineBasicBlock* dummy = new MachineBasicBlock();
102 LeakDetector::removeGarbageObject(dummy);
106 void ilist_traits<MachineBasicBlock>::transferNodesFromList(
107 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
108 ilist_iterator<MachineBasicBlock> first,
109 ilist_iterator<MachineBasicBlock> last) {
110 if (Parent != toList.Parent)
111 for (; first != last; ++first)
112 first->Parent = toList.Parent;
115 MachineFunction::MachineFunction(const Function *F,
116 const TargetMachine &TM)
117 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
118 SSARegMapping = new SSARegMap();
120 FrameInfo = new MachineFrameInfo();
121 ConstantPool = new MachineConstantPool(TM.getTargetData());
122 JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData());
123 BasicBlocks.Parent = this;
126 MachineFunction::~MachineFunction() {
128 delete SSARegMapping;
132 delete JumpTableInfo;
133 delete[] UsedPhysRegs;
137 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
138 /// recomputes them. This guarantees that the MBB numbers are sequential,
139 /// dense, and match the ordering of the blocks within the function. If a
140 /// specific MachineBasicBlock is specified, only that block and those after
141 /// it are renumbered.
142 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
143 if (empty()) { MBBNumbering.clear(); return; }
144 MachineFunction::iterator MBBI, E = end();
150 // Figure out the block number this should have.
151 unsigned BlockNo = 0;
153 BlockNo = prior(MBBI)->getNumber()+1;
155 for (; MBBI != E; ++MBBI, ++BlockNo) {
156 if (MBBI->getNumber() != (int)BlockNo) {
157 // Remove use of the old number.
158 if (MBBI->getNumber() != -1) {
159 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
160 "MBB number mismatch!");
161 MBBNumbering[MBBI->getNumber()] = 0;
164 // If BlockNo is already taken, set that block's number to -1.
165 if (MBBNumbering[BlockNo])
166 MBBNumbering[BlockNo]->setNumber(-1);
168 MBBNumbering[BlockNo] = MBBI;
169 MBBI->setNumber(BlockNo);
173 // Okay, all the blocks are renumbered. If we have compactified the block
174 // numbering, shrink MBBNumbering now.
175 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
176 MBBNumbering.resize(BlockNo);
180 void MachineFunction::dump() const { print(std::cerr); }
182 void MachineFunction::print(std::ostream &OS) const {
183 OS << "# Machine code for " << Fn->getName () << "():\n";
185 // Print Frame Information
186 getFrameInfo()->print(*this, OS);
188 // Print JumpTable Information
189 getJumpTableInfo()->print(OS);
191 // Print Constant Pool
192 getConstantPool()->print(OS);
194 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
196 if (livein_begin() != livein_end()) {
198 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
200 OS << " " << MRI->getName(I->first);
202 OS << " Reg #" << I->first;
205 OS << " in VR#" << I->second << " ";
209 if (liveout_begin() != liveout_end()) {
211 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
213 OS << " " << MRI->getName(*I);
215 OS << " Reg #" << *I;
219 for (const_iterator BB = begin(); BB != end(); ++BB)
222 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
225 /// CFGOnly flag - This is used to control whether or not the CFG graph printer
226 /// prints out the contents of basic blocks or not. This is acceptable because
227 /// this code is only really used for debugging purposes.
229 static bool CFGOnly = false;
233 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
234 static std::string getGraphName(const MachineFunction *F) {
235 return "CFG for '" + F->getFunction()->getName() + "' function";
238 static std::string getNodeLabel(const MachineBasicBlock *Node,
239 const MachineFunction *Graph) {
240 if (CFGOnly && Node->getBasicBlock() &&
241 !Node->getBasicBlock()->getName().empty())
242 return Node->getBasicBlock()->getName() + ":";
244 std::ostringstream Out;
246 Out << Node->getNumber() << ':';
252 std::string OutStr = Out.str();
253 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
255 // Process string output to make it nicer...
256 for (unsigned i = 0; i != OutStr.length(); ++i)
257 if (OutStr[i] == '\n') { // Left justify
259 OutStr.insert(OutStr.begin()+i+1, 'l');
266 void MachineFunction::viewCFG() const
269 ViewGraph(this, "mf" + getFunction()->getName());
271 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
272 << "systems with Graphviz or gv!\n";
276 void MachineFunction::viewCFGOnly() const
283 // The next two methods are used to construct and to retrieve
284 // the MachineCodeForFunction object for the given function.
285 // construct() -- Allocates and initializes for a given function and target
286 // get() -- Returns a handle to the object.
287 // This should not be called before "construct()"
288 // for a given Function.
291 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
293 assert(Fn->getAnnotation(MF_AID) == 0 &&
294 "Object already exists for this function!");
295 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
296 Fn->addAnnotation(mcInfo);
300 void MachineFunction::destruct(const Function *Fn) {
301 bool Deleted = Fn->deleteAnnotation(MF_AID);
302 assert(Deleted && "Machine code did not exist for function!");
305 MachineFunction& MachineFunction::get(const Function *F)
307 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
308 assert(mc && "Call construct() method first to allocate the object");
312 void MachineFunction::clearSSARegMap() {
313 delete SSARegMapping;
317 //===----------------------------------------------------------------------===//
318 // MachineFrameInfo implementation
319 //===----------------------------------------------------------------------===//
321 void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
322 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
324 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
325 const StackObject &SO = Objects[i];
326 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
328 OS << "variable sized";
330 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
331 OS << " alignment is " << SO.Alignment << " byte"
332 << (SO.Alignment != 1 ? "s," : ",");
334 if (i < NumFixedObjects)
336 if (i < NumFixedObjects || SO.SPOffset != -1) {
337 int Off = SO.SPOffset - ValOffset;
338 OS << " at location [SP";
348 if (HasVarSizedObjects)
349 OS << " Stack frame contains variable sized objects\n";
352 void MachineFrameInfo::dump(const MachineFunction &MF) const {
353 print(MF, std::cerr);
357 //===----------------------------------------------------------------------===//
358 // MachineJumpTableInfo implementation
359 //===----------------------------------------------------------------------===//
361 /// getJumpTableIndex - Create a new jump table entry in the jump table info
362 /// or return an existing one.
364 unsigned MachineJumpTableInfo::getJumpTableIndex(
365 std::vector<MachineBasicBlock*> &DestBBs) {
366 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
367 if (JumpTables[i].MBBs == DestBBs)
370 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
371 return JumpTables.size()-1;
375 void MachineJumpTableInfo::print(std::ostream &OS) const {
376 // FIXME: this is lame, maybe we could print out the MBB numbers or something
377 // like {1, 2, 4, 5, 3, 0}
378 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
379 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
384 unsigned MachineJumpTableInfo::getEntrySize() const {
385 return TD->getPointerSize();
388 unsigned MachineJumpTableInfo::getAlignment() const {
389 return TD->getPointerAlignment();
392 void MachineJumpTableInfo::dump() const { print(std::cerr); }
395 //===----------------------------------------------------------------------===//
396 // MachineConstantPool implementation
397 //===----------------------------------------------------------------------===//
399 const Type *MachineConstantPoolEntry::getType() const {
400 if (isMachineConstantPoolEntry())
401 return Val.MachineCPVal->getType();
402 return Val.ConstVal->getType();
405 MachineConstantPool::~MachineConstantPool() {
406 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
407 if (Constants[i].isMachineConstantPoolEntry())
408 delete Constants[i].Val.MachineCPVal;
411 /// getConstantPoolIndex - Create a new entry in the constant pool or return
412 /// an existing one. User must specify an alignment in bytes for the object.
414 unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
415 unsigned Alignment) {
416 assert(Alignment && "Alignment must be specified!");
417 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
419 // Check to see if we already have this constant.
421 // FIXME, this could be made much more efficient for large constant pools.
422 unsigned AlignMask = (1 << Alignment)-1;
423 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
424 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
428 if (!Constants.empty()) {
429 Offset = Constants.back().getOffset();
430 Offset += TD->getTypeSize(Constants.back().getType());
431 Offset = (Offset+AlignMask)&~AlignMask;
434 Constants.push_back(MachineConstantPoolEntry(C, Offset));
435 return Constants.size()-1;
438 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
439 unsigned Alignment) {
440 assert(Alignment && "Alignment must be specified!");
441 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
443 // Check to see if we already have this constant.
445 // FIXME, this could be made much more efficient for large constant pools.
446 unsigned AlignMask = (1 << Alignment)-1;
447 int Idx = V->getExistingMachineCPValue(this, Alignment);
449 return (unsigned)Idx;
452 if (!Constants.empty()) {
453 Offset = Constants.back().getOffset();
454 Offset += TD->getTypeSize(Constants.back().getType());
455 Offset = (Offset+AlignMask)&~AlignMask;
458 Constants.push_back(MachineConstantPoolEntry(V, Offset));
459 return Constants.size()-1;
463 void MachineConstantPool::print(std::ostream &OS) const {
464 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
465 OS << " <cp #" << i << "> is";
466 if (Constants[i].isMachineConstantPoolEntry())
467 Constants[i].Val.MachineCPVal->print(OS);
469 OS << *(Value*)Constants[i].Val.ConstVal;
470 OS << " , offset=" << Constants[i].Offset;
475 void MachineConstantPool::dump() const { print(std::cerr); }