1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- MachineFunction.cpp -----------------------------------------------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Collect native machine code information for a function.  This allows
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// target-specific information about the generated code to be stored with each
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// function.
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/DerivedTypes.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Function.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Instructions.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Config/config.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineConstantPool.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunction.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunctionPass.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFrameInfo.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstr.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineJumpTableInfo.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineModuleInfo.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineRegisterInfo.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/Passes.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCAsmInfo.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCContext.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Analysis/DebugInfo.h"
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetData.h"
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetLowering.h"
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetMachine.h"
3619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Target/TargetFrameLowering.h"
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallString.h"
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/STLExtras.h"
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/GraphWriter.h"
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// MachineFunction implementation
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Out of line virtual method.
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunctionInfo::~MachineFunctionInfo() {}
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MBB->getParent()->DeleteMachineBasicBlock(MBB);
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::MachineFunction(const Function *F, const TargetMachine &TM,
5519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 unsigned FunctionNum, MachineModuleInfo &mmi,
5619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 GCModuleInfo* gmi)
5719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  : Fn(F), Target(TM), Ctx(mmi.getContext()), MMI(mmi), GMI(gmi) {
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TM.getRegisterInfo())
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RegInfo = new (Allocator) MachineRegisterInfo(*TM.getRegisterInfo());
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RegInfo = 0;
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MFInfo = 0;
6319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FrameInfo = new (Allocator) MachineFrameInfo(*TM.getFrameLowering());
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Fn->hasFnAttr(Attribute::StackAlignment))
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs(
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Fn->getAttributes().getFnAttributes()));
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantPool = new (Allocator) MachineConstantPool(TM.getTargetData());
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Alignment = TM.getTargetLowering()->getMinFunctionAlignment();
6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn.
7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!Fn->hasFnAttr(Attribute::OptimizeForSize))
7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Alignment = std::max(Alignment,
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         TM.getTargetLowering()->getPrefFunctionAlignment());
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  FunctionNumber = FunctionNum;
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  JumpTableInfo = 0;
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::~MachineFunction() {
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlocks.clear();
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InstructionRecycler.clear(Allocator);
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlockRecycler.clear(Allocator);
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RegInfo) {
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RegInfo->~MachineRegisterInfo();
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Allocator.Deallocate(RegInfo);
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MFInfo) {
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MFInfo->~MachineFunctionInfo();
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Allocator.Deallocate(MFInfo);
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  FrameInfo->~MachineFrameInfo();         Allocator.Deallocate(FrameInfo);
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantPool->~MachineConstantPool();   Allocator.Deallocate(ConstantPool);
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (JumpTableInfo) {
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    JumpTableInfo->~MachineJumpTableInfo();
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Allocator.Deallocate(JumpTableInfo);
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// does already exist, allocate one.
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineJumpTableInfo *MachineFunction::
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumangetOrCreateJumpTableInfo(unsigned EntryKind) {
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (JumpTableInfo) return JumpTableInfo;
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  JumpTableInfo = new (Allocator)
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return JumpTableInfo;
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// recomputes them.  This guarantees that the MBB numbers are sequential,
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// dense, and match the ordering of the blocks within the function.  If a
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// specific MachineBasicBlock is specified, only that block and those after
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// it are renumbered.
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (empty()) { MBBNumbering.clear(); return; }
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator MBBI, E = end();
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MBB == 0)
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MBBI = begin();
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MBBI = MBB;
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Figure out the block number this should have.
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned BlockNo = 0;
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MBBI != begin())
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BlockNo = prior(MBBI)->getNumber()+1;
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (; MBBI != E; ++MBBI, ++BlockNo) {
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MBBI->getNumber() != (int)BlockNo) {
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Remove use of the old number.
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MBBI->getNumber() != -1) {
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman               "MBB number mismatch!");
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MBBNumbering[MBBI->getNumber()] = 0;
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // If BlockNo is already taken, set that block's number to -1.
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MBBNumbering[BlockNo])
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MBBNumbering[BlockNo]->setNumber(-1);
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MBBNumbering[BlockNo] = MBBI;
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MBBI->setNumber(BlockNo);
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Okay, all the blocks are renumbered.  If we have compactified the block
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // numbering, shrink MBBNumbering now.
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MBBNumbering.resize(BlockNo);
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// of `new MachineInstr'.
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineInstr *
15519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    DebugLoc DL, bool NoImp) {
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineInstr(MCID, DL, NoImp);
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// 'Orig' instruction, identical in all ways except the instruction
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// has no parent, prev, or next.
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineInstr *
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             MachineInstr(*this, *Orig);
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// DeleteMachineInstr - Delete the given MachineInstr.
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::DeleteMachineInstr(MachineInstr *MI) {
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->~MachineInstr();
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InstructionRecycler.Deallocate(Allocator, MI);
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// instead of `new MachineBasicBlock'.
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             MachineBasicBlock(*this, bb);
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(MBB->getParent() == this && "MBB parent mismatch!");
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MBB->~MachineBasicBlock();
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BasicBlockRecycler.Deallocate(Allocator, MBB);
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineMemOperand *
19819bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMachineFunction::getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f,
19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                      uint64_t s, unsigned base_alignment,
20019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                      const MDNode *TBAAInfo) {
20119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return new (Allocator) MachineMemOperand(PtrInfo, f, s, base_alignment,
20219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           TBAAInfo);
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineMemOperand *
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      int64_t Offset, uint64_t Size) {
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return new (Allocator)
20919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             MachineMemOperand(MachinePointerInfo(MMO->getValue(),
21019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                  MMO->getOffset()+Offset),
21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               MMO->getFlags(), Size,
21219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               MMO->getBaseAlignment(), 0);
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineInstr::mmo_iterator
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::allocateMemRefsArray(unsigned long Num) {
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Allocator.Allocate<MachineMemOperand *>(Num);
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    MachineInstr::mmo_iterator End) {
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Count the number of load mem refs.
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Num = 0;
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((*I)->isLoad())
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++Num;
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Allocate a new array and populate it with the load information.
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Index = 0;
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((*I)->isLoad()) {
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!(*I)->isStore())
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Reuse the MMO.
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result[Index] = *I;
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else {
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Clone the MMO and unset the store flag.
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MachineMemOperand *JustLoad =
24019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          getMachineMemOperand((*I)->getPointerInfo(),
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               (*I)->getFlags() & ~MachineMemOperand::MOStore,
24219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               (*I)->getSize(), (*I)->getBaseAlignment(),
24319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               (*I)->getTBAAInfo());
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result[Index] = JustLoad;
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++Index;
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return std::make_pair(Result, Result + Num);
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     MachineInstr::mmo_iterator End) {
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Count the number of load mem refs.
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Num = 0;
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((*I)->isStore())
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++Num;
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Allocate a new array and populate it with the store information.
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Index = 0;
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if ((*I)->isStore()) {
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!(*I)->isLoad())
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Reuse the MMO.
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result[Index] = *I;
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else {
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Clone the MMO and unset the load flag.
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MachineMemOperand *JustStore =
27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          getMachineMemOperand((*I)->getPointerInfo(),
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               (*I)->getFlags() & ~MachineMemOperand::MOLoad,
27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               (*I)->getSize(), (*I)->getBaseAlignment(),
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               (*I)->getTBAAInfo());
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Result[Index] = JustStore;
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ++Index;
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return std::make_pair(Result, Result + Num);
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineFunction::dump() const {
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  print(dbgs());
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
28819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid MachineFunction::print(raw_ostream &OS, SlotIndexes *Indexes) const {
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << "# Machine code for function " << Fn->getName() << ":\n";
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print Frame Information
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  FrameInfo->print(*this, OS);
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print JumpTable Information
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (JumpTableInfo)
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    JumpTableInfo->print(OS);
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Print Constant Pool
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantPool->print(OS);
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RegInfo && !RegInfo->livein_empty()) {
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "Function Live Ins: ";
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (MachineRegisterInfo::livein_iterator
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OS << PrintReg(I->first, TRI);
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (I->second)
30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        OS << " in " << PrintReg(I->second, TRI);
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (llvm::next(I) != E)
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        OS << ", ";
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << '\n';
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RegInfo && !RegInfo->liveout_empty()) {
31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << "Function Live Outs:";
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (MachineRegisterInfo::liveout_iterator
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OS << ' ' << PrintReg(*I, TRI);
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << '\n';
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << '\n';
32519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    BB->print(OS, Indexes);
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  template<>
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    static std::string getGraphName(const MachineFunction *F) {
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return "CFG for '" + F->getFunction()->getNameStr() + "' function";
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::string getNodeLabel(const MachineBasicBlock *Node,
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             const MachineFunction *Graph) {
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::string OutStr;
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      {
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        raw_string_ostream OSS(OutStr);
34619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
34719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (isSimple()) {
34819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          OSS << "BB#" << Node->getNumber();
34919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (const BasicBlock *BB = Node->getBasicBlock())
35019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            OSS << ": " << BB->getName();
35119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        } else
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          Node->print(OSS);
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Process string output to make it nicer...
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0; i != OutStr.length(); ++i)
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (OutStr[i] == '\n') {                            // Left justify
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          OutStr[i] = '\\';
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          OutStr.insert(OutStr.begin()+i+1, 'l');
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return OutStr;
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineFunction::viewCFG() const
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ViewGraph(this, "mf" + getFunction()->getNameStr());
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  errs() << "MachineFunction::viewCFG is only available in debug builds on "
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         << "systems with Graphviz or gv!\n";
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // NDEBUG
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineFunction::viewCFGOnly() const
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman{
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#else
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         << "systems with Graphviz or gv!\n";
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif // NDEBUG
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// addLiveIn - Add the specified physical register as a live-in value and
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// create a corresponding virtual register for it.
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MachineFunction::addLiveIn(unsigned PReg,
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    const TargetRegisterClass *RC) {
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineRegisterInfo &MRI = getRegInfo();
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned VReg = MRI.getLiveInVirtReg(PReg);
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VReg) {
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(MRI.getRegClass(VReg) == RC && "Register class mismatch!");
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return VReg;
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  VReg = MRI.createVirtualRegister(RC);
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MRI.addLiveIn(PReg, VReg);
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return VReg;
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// normal 'L' label is returned.
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        bool isLinkerPrivate) const {
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(JumpTableInfo && "No jump tables");
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const MCAsmInfo &MAI = *getTarget().getMCAsmInfo();
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() :
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         MAI.getPrivateGlobalPrefix();
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallString<60> Name;
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  raw_svector_ostream(Name)
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Ctx.GetOrCreateSymbol(Name.str());
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
42119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// getPICBaseSymbol - Return a function-local symbol to represent the PIC
42219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// base.
42319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMCSymbol *MachineFunction::getPICBaseSymbol() const {
42419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const MCAsmInfo &MAI = *Target.getMCAsmInfo();
42519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
42619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               Twine(getFunctionNumber())+"$pb");
42719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
428894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
429894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
430894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  MachineFrameInfo implementation
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CreateFixedObject - Create a new object at a fixed location on the stack.
434894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// All fixed objects should be created before other objects are created for
435894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// efficiency. By default, fixed objects are immutable. This returns an
436894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// index with a negative value.
437894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
438894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanint MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
439894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        bool Immutable) {
440894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
441894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The alignment of the frame index can be determined from its offset from
442894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the incoming frame position.  If the frame object is at offset 32 and
443894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the stack is guaranteed to be 16-byte aligned, then we know that the
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // object is 16-byte aligned.
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned StackAlign = TFI.getStackAlignment();
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Align = MinAlign(SPOffset, StackAlign);
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                              /*isSS*/false, false));
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return -++NumFixedObjects;
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanBitVector
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(MBB && "MBB must be valid");
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const MachineFunction *MF = MBB->getParent();
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(MF && "MBB must be part of a MachineFunction");
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetMachine &TM = MF->getTarget();
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterInfo *TRI = TM.getRegisterInfo();
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BitVector BV(TRI->getNumRegs());
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Before CSI is calculated, no registers are considered pristine. They can be
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // freely used and PEI will make sure they are saved.
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!isCalleeSavedInfoValid())
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return BV;
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BV.set(*CSR);
469894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
470894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The entry MBB always has all CSRs pristine.
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MBB == &MF->front())
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return BV;
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // On other MBBs the saved CSRs are not pristine.
475894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
476894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
477894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = CSI.end(); I != E; ++I)
478894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BV.reset(I->getReg());
479894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
480894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return BV;
481894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Objects.empty()) return;
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const TargetFrameLowering *FI = MF.getTarget().getFrameLowering();
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << "Frame Objects:\n";
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const StackObject &SO = Objects[i];
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "  fi#" << (int)(i-NumFixedObjects) << ": ";
495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SO.Size == ~0ULL) {
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << "dead\n";
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
498894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
499894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SO.Size == 0)
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << "variable sized";
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << "size=" << SO.Size;
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << ", align=" << SO.Alignment;
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (i < NumFixedObjects)
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << ", fixed";
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (i < NumFixedObjects || SO.SPOffset != -1) {
508894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int64_t Off = SO.SPOffset - ValOffset;
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << ", at location [SP";
510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Off > 0)
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        OS << "+" << Off;
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (Off < 0)
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        OS << Off;
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << "]";
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "\n";
517894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineFrameInfo::dump(const MachineFunction &MF) const {
521894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  print(MF, dbgs());
522894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  MachineJumpTableInfo implementation
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getEntrySize - Return the size of each entry in the jump table.
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const {
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The size of a jump table entry is 4 bytes unless the entry is just the
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // address of a block, in which case it is the pointer size.
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (getEntryKind()) {
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_BlockAddress:
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return TD.getPointerSize();
535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_GPRel32BlockAddress:
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_LabelDifference32:
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_Custom32:
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 4;
539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_Inline:
540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(0 && "Unknown jump table encoding!");
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ~0;
544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getEntryAlignment - Return the alignment of each entry in the jump table.
547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const {
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The alignment of a jump table entry is the alignment of int32 unless the
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // entry is just the address of a block, in which case it is the pointer
550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // alignment.
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (getEntryKind()) {
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_BlockAddress:
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return TD.getPointerABIAlignment();
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_GPRel32BlockAddress:
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_LabelDifference32:
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_Custom32:
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return TD.getABIIntegerTypeAlignment(32);
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MachineJumpTableInfo::EK_Inline:
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 1;
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(0 && "Unknown jump table encoding!");
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return ~0;
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// createJumpTableIndex - Create a new jump table entry in the jump table info.
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MachineJumpTableInfo::createJumpTableIndex(
568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               const std::vector<MachineBasicBlock*> &DestBBs) {
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(!DestBBs.empty() && "Cannot create an empty jump table!");
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  JumpTables.push_back(MachineJumpTableEntry(DestBBs));
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return JumpTables.size()-1;
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the jump tables to branch to New instead.
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                  MachineBasicBlock *New) {
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Old != New && "Not making a change?");
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool MadeChange = false;
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ReplaceMBBInJumpTable(i, Old, New);
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MadeChange;
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// the jump table to branch to New instead.
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 MachineBasicBlock *Old,
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 MachineBasicBlock *New) {
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Old != New && "Not making a change?");
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool MadeChange = false;
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineJumpTableEntry &JTE = JumpTables[Idx];
593894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
594894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (JTE.MBBs[j] == Old) {
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      JTE.MBBs[j] = New;
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MadeChange = true;
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return MadeChange;
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineJumpTableInfo::print(raw_ostream &OS) const {
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (JumpTables.empty()) return;
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << "Jump Tables:\n";
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "  jt#" << i << ": ";
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << '\n';
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineJumpTableInfo::dump() const { print(dbgs()); }
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//  MachineConstantPool implementation
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
62219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanType *MachineConstantPoolEntry::getType() const {
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isMachineConstantPoolEntry())
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.MachineCPVal->getType();
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Val.ConstVal->getType();
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
629894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MachineConstantPoolEntry::getRelocationInfo() const {
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isMachineConstantPoolEntry())
631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Val.MachineCPVal->getRelocationInfo();
632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Val.ConstVal->getRelocationInfo();
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineConstantPool::~MachineConstantPool() {
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Constants.size(); i != e; ++i)
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Constants[i].isMachineConstantPoolEntry())
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      delete Constants[i].Val.MachineCPVal;
63919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (DenseSet<MachineConstantPoolValue*>::iterator I =
64019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
64119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       I != E; ++I)
64219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    delete *I;
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CanShareConstantPoolEntry - Test whether the given two constants
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// can be allocated the same constant pool entry.
647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      const TargetData *TD) {
649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Handle the trivial case quickly.
650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (A == B) return true;
651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If they have the same type but weren't the same constant, quickly
653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // reject them.
654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (A->getType() == B->getType()) return false;
655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // For now, only support constants with the same size.
657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return false;
659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If a floating-point value and an integer value have the same encoding,
661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // they can share a constant-pool entry.
662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantFP *AFP = dyn_cast<ConstantFP>(A))
663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const ConstantInt *BI = dyn_cast<ConstantInt>(B))
664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantFP *BFP = dyn_cast<ConstantFP>(B))
666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const ConstantInt *AI = dyn_cast<ConstantInt>(A))
667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Two vectors can share an entry if each pair of corresponding
670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // elements could.
671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantVector *AV = dyn_cast<ConstantVector>(A))
672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (const ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!CanShareConstantPoolEntry(AV->getOperand(i),
677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       BV->getOperand(i), TD))
678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          return false;
679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TODO: Handle other cases.
683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getConstantPoolIndex - Create a new entry in the constant pool or return
688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// an existing one.  User must specify the log2 of the minimum required
689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// alignment for the object.
690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   unsigned Alignment) {
693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Alignment && "Alignment must be specified!");
694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Alignment > PoolAlignment) PoolAlignment = Alignment;
695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check to see if we already have this constant.
697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME, this could be made much more efficient for large constant pools.
699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Constants.size(); i != e; ++i)
700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!Constants[i].isMachineConstantPoolEntry() &&
701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if ((unsigned)Constants[i].getAlignment() < Alignment)
703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Constants[i].Alignment = Alignment;
704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return i;
705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constants.push_back(MachineConstantPoolEntry(C, Alignment));
708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Constants.size()-1;
709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanunsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                   unsigned Alignment) {
713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Alignment && "Alignment must be specified!");
714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Alignment > PoolAlignment) PoolAlignment = Alignment;
715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check to see if we already have this constant.
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME, this could be made much more efficient for large constant pools.
719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  int Idx = V->getExistingMachineCPValue(this, Alignment);
72019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Idx != -1) {
72119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MachineCPVsSharingEntries.insert(V);
722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return (unsigned)Idx;
72319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Constants.push_back(MachineConstantPoolEntry(V, Alignment));
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Constants.size()-1;
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineConstantPool::print(raw_ostream &OS) const {
730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constants.empty()) return;
731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  OS << "Constant Pool:\n";
733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "  cp#" << i << ": ";
735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Constants[i].isMachineConstantPoolEntry())
736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Constants[i].Val.MachineCPVal->print(OS);
737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OS << *(Value*)Constants[i].Val.ConstVal;
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << ", align=" << Constants[i].getAlignment();
740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OS << "\n";
741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MachineConstantPool::dump() const { print(dbgs()); }
745