MachineFunction.cpp revision 7cf12c7efd37dc12c3ed536a3f4c373dddac2b85
13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===-- MachineFunction.cpp -----------------------------------------------===//
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                     The LLVM Compiler Infrastructure
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// This file is distributed under the University of Illinois Open Source
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// License. See LICENSE.TXT for details.
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Collect native machine code information for a function.  This allows
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// target-specific information about the generated code to be stored with each
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// function.
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/DerivedTypes.h"
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Function.h"
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Instructions.h"
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/ADT/STLExtras.h"
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Config/config.h"
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/CodeGen/MachineConstantPool.h"
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/CodeGen/MachineFunctionPass.h"
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/CodeGen/MachineFrameInfo.h"
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/CodeGen/MachineInstr.h"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/CodeGen/MachineJumpTableInfo.h"
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/CodeGen/MachineRegisterInfo.h"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/CodeGen/Passes.h"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Target/TargetData.h"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Target/TargetLowering.h"
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Target/TargetMachine.h"
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Target/TargetFrameInfo.h"
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Support/Compiler.h"
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Support/GraphWriter.h"
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "llvm/Support/raw_ostream.h"
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <fstream>
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <sstream>
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace llvm;
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyrybool MachineFunctionPass::runOnFunction(Function &F) {
408cc4650120c587d5c00ad08cd1e0d81e804d7578Pyry Haulos  // Do not codegen any 'available_externally' functions at all, they have
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // definitions outside the translation unit.
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (F.hasAvailableExternallyLinkage())
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return false;
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return runOnMachineFunction(MachineFunction::get(&F));
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace {
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    static char ID;
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    std::ostream *OS;
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const std::string Banner;
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Printer (std::ostream *os, const std::string &banner)
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const char *getPassName() const { return "MachineFunction Printer"; }
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      AU.setPreservesAll();
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    bool runOnMachineFunction(MachineFunction &MF) {
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      (*OS) << Banner;
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      MF.print (*OS);
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return false;
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  };
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  char Printer::ID = 0;
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// Returns a newly-created MachineFunction Printer pass. The default output
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// stream is std::cerr; the default banner is empty.
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
763c827367444ee418f129b2c238299f49d3264554Jarkko PoyryFunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                                     const std::string &Banner){
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return new Printer(OS, Banner);
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace {
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    static char ID;
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Deleter() : MachineFunctionPass(&ID) {}
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const char *getPassName() const { return "Machine Code Deleter"; }
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    bool runOnMachineFunction(MachineFunction &MF) {
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Delete the annotation from the function now.
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      MachineFunction::destruct(MF.getFunction());
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return true;
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  };
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  char Deleter::ID = 0;
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// the current function, which should happen after the function has been
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// emitted to a .s file or to memory.
1003c827367444ee418f129b2c238299f49d3264554Jarkko PoyryFunctionPass *llvm::createMachineCodeDeleter() {
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return new Deleter();
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===---------------------------------------------------------------------===//
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// MachineFunction implementation
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===---------------------------------------------------------------------===//
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MBB->getParent()->DeleteMachineBasicBlock(MBB);
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1143c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction::MachineFunction(const Function *F,
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                 const TargetMachine &TM)
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  : Annotation(AnnotationManager::getID("CodeGen::MachineCodeForFunction")),
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Fn(F), Target(TM) {
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (TM.getRegisterInfo())
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                  MachineRegisterInfo(*TM.getRegisterInfo());
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  else
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    RegInfo = 0;
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MFInfo = 0;
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                  MachineFrameInfo(*TM.getFrameInfo());
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                     MachineConstantPool(TM.getTargetData());
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Set up jump table.
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const TargetData &TD = *TM.getTargetData();
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned TyAlignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                               : TD.getPointerABIAlignment();
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                      MachineJumpTableInfo(EntrySize, TyAlignment);
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1403c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction::~MachineFunction() {
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  BasicBlocks.clear();
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  InstructionRecycler.clear(Allocator);
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  BasicBlockRecycler.clear(Allocator);
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (RegInfo) {
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    RegInfo->~MachineRegisterInfo();
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Allocator.Deallocate(RegInfo);
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (MFInfo) {
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    MFInfo->~MachineFunctionInfo();
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    Allocator.Deallocate(MFInfo);
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  FrameInfo->~MachineFrameInfo();         Allocator.Deallocate(FrameInfo);
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ConstantPool->~MachineConstantPool();   Allocator.Deallocate(ConstantPool);
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// recomputes them.  This guarantees that the MBB numbers are sequential,
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// dense, and match the ordering of the blocks within the function.  If a
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// specific MachineBasicBlock is specified, only that block and those after
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// it are renumbered.
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (empty()) { MBBNumbering.clear(); return; }
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MachineFunction::iterator MBBI, E = end();
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (MBB == 0)
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    MBBI = begin();
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  else
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    MBBI = MBB;
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Figure out the block number this should have.
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned BlockNo = 0;
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (MBBI != begin())
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    BlockNo = prior(MBBI)->getNumber()+1;
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (; MBBI != E; ++MBBI, ++BlockNo) {
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (MBBI->getNumber() != (int)BlockNo) {
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Remove use of the old number.
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (MBBI->getNumber() != -1) {
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry               "MBB number mismatch!");
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        MBBNumbering[MBBI->getNumber()] = 0;
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      }
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // If BlockNo is already taken, set that block's number to -1.
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (MBBNumbering[BlockNo])
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        MBBNumbering[BlockNo]->setNumber(-1);
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      MBBNumbering[BlockNo] = MBBI;
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      MBBI->setNumber(BlockNo);
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Okay, all the blocks are renumbered.  If we have compactified the block
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // numbering, shrink MBBNumbering now.
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MBBNumbering.resize(BlockNo);
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// of `new MachineInstr'.
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
2033c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineInstr *
2043c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                    DebugLoc DL, bool NoImp) {
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    MachineInstr(TID, DL, NoImp);
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// 'Orig' instruction, identical in all ways except the the instruction
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// has no parent, prev, or next.
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
2143c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineInstr *
2153c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry             MachineInstr(*this, *Orig);
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// DeleteMachineInstr - Delete the given MachineInstr.
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid
2233c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction::DeleteMachineInstr(MachineInstr *MI) {
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Clear the instructions memoperands. This must be done manually because
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // the instruction's parent pointer is now null, so it can't properly
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // deallocate them on its own.
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MI->clearMemOperands(*this);
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MI->~MachineInstr();
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  InstructionRecycler.Deallocate(Allocator, MI);
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// instead of `new MachineBasicBlock'.
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
2363c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineBasicBlock *
2373c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry             MachineBasicBlock(*this, bb);
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid
2453c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(MBB->getParent() == this && "MBB parent mismatch!");
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MBB->~MachineBasicBlock();
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  BasicBlockRecycler.Deallocate(Allocator, MBB);
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineFunction::dump() const {
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  print(*cerr.stream());
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineFunction::print(std::ostream &OS) const {
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  OS << "# Machine code for " << Fn->getName () << "():\n";
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Print Frame Information
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  FrameInfo->print(*this, OS);
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Print JumpTable Information
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  JumpTableInfo->print(OS);
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Print Constant Pool
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  {
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    raw_os_ostream OSS(OS);
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    ConstantPool->print(OSS);
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (RegInfo && !RegInfo->livein_empty()) {
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "Live Ins:";
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (MachineRegisterInfo::livein_iterator
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry         I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (TRI)
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        OS << " " << TRI->getName(I->first);
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      else
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        OS << " Reg #" << I->first;
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (I->second)
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        OS << " in VR#" << I->second << " ";
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "\n";
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (RegInfo && !RegInfo->liveout_empty()) {
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "Live Outs:";
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (MachineRegisterInfo::liveout_iterator
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry         I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (TRI)
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        OS << " " << TRI->getName(*I);
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      else
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        OS << " Reg #" << *I;
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "\n";
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (const_iterator BB = begin(); BB != end(); ++BB)
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    BB->print(OS);
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace llvm {
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  template<>
3053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    static std::string getGraphName(const MachineFunction *F) {
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return "CFG for '" + F->getFunction()->getName() + "' function";
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
3093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    static std::string getNodeLabel(const MachineBasicBlock *Node,
3113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                    const MachineFunction *Graph,
3123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                    bool ShortNames) {
3133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (ShortNames && Node->getBasicBlock() &&
3143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          !Node->getBasicBlock()->getName().empty())
3153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        return Node->getBasicBlock()->getName() + ":";
3163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      std::ostringstream Out;
3183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (ShortNames) {
3193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        Out << Node->getNumber() << ':';
3203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        return Out.str();
3213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      }
3223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Node->print(Out);
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      std::string OutStr = Out.str();
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      // Process string output to make it nicer...
3293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      for (unsigned i = 0; i != OutStr.length(); ++i)
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        if (OutStr[i] == '\n') {                            // Left justify
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          OutStr[i] = '\\';
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry          OutStr.insert(OutStr.begin()+i+1, 'l');
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        }
3343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return OutStr;
3353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
3363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  };
3373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3393c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineFunction::viewCFG() const
3403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef NDEBUG
3423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ViewGraph(this, "mf" + getFunction()->getName());
3433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else
3443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  cerr << "SelectionDAG::viewGraph is only available in debug builds on "
3453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       << "systems with Graphviz or gv!\n";
3463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // NDEBUG
3473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineFunction::viewCFGOnly() const
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef NDEBUG
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ViewGraph(this, "mf" + getFunction()->getName(), true);
3533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  cerr << "SelectionDAG::viewGraph is only available in debug builds on "
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       << "systems with Graphviz or gv!\n";
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif // NDEBUG
3573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// The next two methods are used to construct and to retrieve
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// the MachineCodeForFunction object for the given function.
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// construct() -- Allocates and initializes for a given function and target
3623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// get()       -- Returns a handle to the object.
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                This should not be called before "construct()"
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                for a given Function.
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
3663c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction&
3673c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
3683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  AnnotationID MF_AID =
3703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    AnnotationManager::getID("CodeGen::MachineCodeForFunction");
3713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(Fn->getAnnotation(MF_AID) == 0 &&
3723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry         "Object already exists for this function!");
3733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
3743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Fn->addAnnotation(mcInfo);
3753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return *mcInfo;
3763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3783c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineFunction::destruct(const Function *Fn) {
3793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  AnnotationID MF_AID =
3803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    AnnotationManager::getID("CodeGen::MachineCodeForFunction");
3813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  bool Deleted = Fn->deleteAnnotation(MF_AID);
3823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(Deleted && "Machine code did not exist for function!");
3833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Deleted = Deleted; // silence warning when no assertions.
3843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3863c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineFunction& MachineFunction::get(const Function *F)
3873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  AnnotationID MF_AID =
3893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                    AnnotationManager::getID("CodeGen::MachineCodeForFunction");
3903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
3913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(mc && "Call construct() method first to allocate the object");
3923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return *mc;
3933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// addLiveIn - Add the specified physical register as a live-in value and
3963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// create a corresponding virtual register for it.
3973c827367444ee418f129b2c238299f49d3264554Jarkko Poyryunsigned MachineFunction::addLiveIn(unsigned PReg,
3983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                    const TargetRegisterClass *RC) {
3993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(RC->contains(PReg) && "Not the correct regclass!");
4003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned VReg = getRegInfo().createVirtualRegister(RC);
4013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  getRegInfo().addLiveIn(PReg, VReg);
4023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return VReg;
4033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
4063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// source file, line, and column. If none currently exists, create a new
4073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// DebugLocTuple, and insert it into the DebugIdMap.
4083c827367444ee418f129b2c238299f49d3264554Jarkko Poyryunsigned MachineFunction::getOrCreateDebugLocID(GlobalVariable *CompileUnit,
4093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                                unsigned Line, unsigned Col) {
4103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  DebugLocTuple Tuple(CompileUnit, Line, Col);
4113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  DenseMap<DebugLocTuple, unsigned>::iterator II
4123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    = DebugLocInfo.DebugIdMap.find(Tuple);
4133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (II != DebugLocInfo.DebugIdMap.end())
4143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return II->second;
4153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Add a new tuple.
4163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned Id = DebugLocInfo.DebugLocations.size();
4173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  DebugLocInfo.DebugLocations.push_back(Tuple);
4183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  DebugLocInfo.DebugIdMap[Tuple] = Id;
4193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Id;
4203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
4233c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
4243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  unsigned Idx = DL.getIndex();
4253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(Idx < DebugLocInfo.DebugLocations.size() &&
4263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry         "Invalid index into debug locations!");
4273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return DebugLocInfo.DebugLocations[Idx];
4283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
4313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//  MachineFrameInfo implementation
4323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
4333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// CreateFixedObject - Create a new object at a fixed location on the stack.
4353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// All fixed objects should be created before other objects are created for
4363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// efficiency. By default, fixed objects are immutable. This returns an
4373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// index with a negative value.
4383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
4393c827367444ee418f129b2c238299f49d3264554Jarkko Poyryint MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
4403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                        bool Immutable) {
4413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
4423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
4433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return -++NumFixedObjects;
4443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4473c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
4483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
4493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
4503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
4523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const StackObject &SO = Objects[i];
4533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "  <fi#" << (int)(i-NumFixedObjects) << ">: ";
4543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (SO.Size == ~0ULL) {
4553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OS << "dead\n";
4563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      continue;
4573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
4583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (SO.Size == 0)
4593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OS << "variable sized";
4603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    else
4613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
4623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << " alignment is " << SO.Alignment << " byte"
4633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       << (SO.Alignment != 1 ? "s," : ",");
4643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (i < NumFixedObjects)
4663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OS << " fixed";
4673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (i < NumFixedObjects || SO.SPOffset != -1) {
4683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      int64_t Off = SO.SPOffset - ValOffset;
4693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OS << " at location [SP";
4703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (Off > 0)
4713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        OS << "+" << Off;
4723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      else if (Off < 0)
4733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        OS << Off;
4743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OS << "]";
4753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
4763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "\n";
4773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
4783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (HasVarSizedObjects)
4803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "  Stack frame contains variable sized objects\n";
4813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4833c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineFrameInfo::dump(const MachineFunction &MF) const {
4843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  print(MF, *cerr.stream());
4853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
4893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//  MachineJumpTableInfo implementation
4903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
4913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// getJumpTableIndex - Create a new jump table entry in the jump table info
4933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// or return an existing one.
4943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
4953c827367444ee418f129b2c238299f49d3264554Jarkko Poyryunsigned MachineJumpTableInfo::getJumpTableIndex(
4963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                               const std::vector<MachineBasicBlock*> &DestBBs) {
4973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(!DestBBs.empty() && "Cannot create an empty jump table!");
4983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
4993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (JumpTables[i].MBBs == DestBBs)
5003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return i;
5013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  JumpTables.push_back(MachineJumpTableEntry(DestBBs));
5033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return JumpTables.size()-1;
5043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
5073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// the jump tables to branch to New instead.
5083c827367444ee418f129b2c238299f49d3264554Jarkko Poyrybool
5093c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
5103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                             MachineBasicBlock *New) {
5113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(Old != New && "Not making a change?");
5123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  bool MadeChange = false;
5133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (size_t i = 0, e = JumpTables.size(); i != e; ++i) {
5143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    MachineJumpTableEntry &JTE = JumpTables[i];
5153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
5163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      if (JTE.MBBs[j] == Old) {
5173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        JTE.MBBs[j] = New;
5183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        MadeChange = true;
5193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      }
5203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
5213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return MadeChange;
5223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5243c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineJumpTableInfo::print(std::ostream &OS) const {
5253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME: this is lame, maybe we could print out the MBB numbers or something
5263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // like {1, 2, 4, 5, 3, 0}
5273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
5283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "  <jt#" << i << "> has " << JumpTables[i].MBBs.size()
5293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry       << " entries\n";
5303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
5313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5333c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
5343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
5373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//  MachineConstantPool implementation
5383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
5393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5403c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst Type *MachineConstantPoolEntry::getType() const {
5413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (isMachineConstantPoolEntry())
5423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return Val.MachineCPVal->getType();
5433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Val.ConstVal->getType();
5443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5473c827367444ee418f129b2c238299f49d3264554Jarkko Poyryunsigned MachineConstantPoolEntry::getRelocationInfo() const {
5483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (isMachineConstantPoolEntry())
5493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return Val.MachineCPVal->getRelocationInfo();
5503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Val.ConstVal->getRelocationInfo();
5513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5533c827367444ee418f129b2c238299f49d3264554Jarkko PoyryMachineConstantPool::~MachineConstantPool() {
5543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (unsigned i = 0, e = Constants.size(); i != e; ++i)
5553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Constants[i].isMachineConstantPoolEntry())
5563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      delete Constants[i].Val.MachineCPVal;
5573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// getConstantPoolIndex - Create a new entry in the constant pool or return
5603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// an existing one.  User must specify the log2 of the minimum required
5613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/// alignment for the object.
5623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry///
5633c827367444ee418f129b2c238299f49d3264554Jarkko Poyryunsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
5643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                                   unsigned Alignment) {
5653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(Alignment && "Alignment must be specified!");
5663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Alignment > PoolAlignment) PoolAlignment = Alignment;
5673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Check to see if we already have this constant.
5693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  //
5703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME, this could be made much more efficient for large constant pools.
5713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (unsigned i = 0, e = Constants.size(); i != e; ++i)
5723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Constants[i].Val.ConstVal == C &&
5733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        (Constants[i].getAlignment() & (Alignment - 1)) == 0)
5743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return i;
5753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Constants.push_back(MachineConstantPoolEntry(C, Alignment));
5773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Constants.size()-1;
5783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5803c827367444ee418f129b2c238299f49d3264554Jarkko Poyryunsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
5813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                                                   unsigned Alignment) {
5823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  assert(Alignment && "Alignment must be specified!");
5833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Alignment > PoolAlignment) PoolAlignment = Alignment;
5843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Check to see if we already have this constant.
5863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  //
5873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // FIXME, this could be made much more efficient for large constant pools.
5883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  int Idx = V->getExistingMachineCPValue(this, Alignment);
5893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  if (Idx != -1)
5903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return (unsigned)Idx;
5913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  Constants.push_back(MachineConstantPoolEntry(V, Alignment));
5933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return Constants.size()-1;
5943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5963c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineConstantPool::print(raw_ostream &OS) const {
5973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
5983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "  <cp#" << i << "> is";
5993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (Constants[i].isMachineConstantPoolEntry())
6003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      Constants[i].Val.MachineCPVal->print(OS);
6013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    else
6023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      OS << *(Value*)Constants[i].Val.ConstVal;
6033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << " , alignment=" << Constants[i].getAlignment();
6043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    OS << "\n";
6053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
6063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
6073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6083c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid MachineConstantPool::dump() const { print(errs()); }
6093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry