1
//===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// Collect native machine code for a function. This class contains a list of
11
// MachineBasicBlock instances that make up the current compiled function.
13
// This class also contains pointers to various classes which hold
14
// target-specific information about the generated code.
16
//===----------------------------------------------------------------------===//
18
#ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
19
#define LLVM_CODEGEN_MACHINEFUNCTION_H
21
#include "llvm/CodeGen/MachineBasicBlock.h"
22
#include "llvm/ADT/ilist.h"
23
#include "llvm/Support/DebugLoc.h"
24
#include "llvm/Support/Allocator.h"
25
#include "llvm/Support/Recycler.h"
32
class MachineRegisterInfo;
33
class MachineFrameInfo;
34
class MachineConstantPool;
35
class MachineJumpTableInfo;
38
class TargetRegisterClass;
41
struct ilist_traits<MachineBasicBlock>
42
: public ilist_default_traits<MachineBasicBlock> {
43
mutable ilist_half_node<MachineBasicBlock> Sentinel;
45
MachineBasicBlock *createSentinel() const {
46
return static_cast<MachineBasicBlock*>(&Sentinel);
48
void destroySentinel(MachineBasicBlock *) const {}
50
MachineBasicBlock *provideInitialHead() const { return createSentinel(); }
51
MachineBasicBlock *ensureHead(MachineBasicBlock*) const {
52
return createSentinel();
54
static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {}
56
void addNodeToList(MachineBasicBlock* MBB);
57
void removeNodeFromList(MachineBasicBlock* MBB);
58
void deleteNode(MachineBasicBlock *MBB);
60
void createNode(const MachineBasicBlock &);
63
/// MachineFunctionInfo - This class can be derived from and used by targets to
64
/// hold private target-specific information for each MachineFunction. Objects
65
/// of type are accessed/created with MF::getInfo and destroyed when the
66
/// MachineFunction is destroyed.
67
struct MachineFunctionInfo {
68
virtual ~MachineFunctionInfo();
71
class MachineFunction {
73
const TargetMachine &Target;
75
// RegInfo - Information about each register in use in the function.
76
MachineRegisterInfo *RegInfo;
78
// Used to keep track of target-specific per-machine function information for
79
// the target implementation.
80
MachineFunctionInfo *MFInfo;
82
// Keep track of objects allocated on the stack.
83
MachineFrameInfo *FrameInfo;
85
// Keep track of constants which are spilled to memory
86
MachineConstantPool *ConstantPool;
88
// Keep track of jump tables for switch instructions
89
MachineJumpTableInfo *JumpTableInfo;
91
// Function-level unique numbering for MachineBasicBlocks. When a
92
// MachineBasicBlock is inserted into a MachineFunction is it automatically
93
// numbered and this vector keeps track of the mapping from ID's to MBB's.
94
std::vector<MachineBasicBlock*> MBBNumbering;
96
// Pool-allocate MachineFunction-lifetime and IR objects.
97
BumpPtrAllocator Allocator;
99
// Allocation management for instructions in function.
100
Recycler<MachineInstr> InstructionRecycler;
102
// Allocation management for basic blocks in function.
103
Recycler<MachineBasicBlock> BasicBlockRecycler;
105
// List of machine basic blocks in function
106
typedef ilist<MachineBasicBlock> BasicBlockListType;
107
BasicBlockListType BasicBlocks;
109
// Default debug location. Used to print out the debug label at the beginning
111
DebugLoc DefaultDebugLoc;
113
// Tracks debug locations.
114
DebugLocTracker DebugLocInfo;
116
/// FunctionNumber - This provides a unique ID for each function emitted in
117
/// this translation unit.
119
unsigned FunctionNumber;
121
// The alignment of the function.
124
MachineFunction(const MachineFunction &); // intentionally unimplemented
125
void operator=(const MachineFunction&); // intentionally unimplemented
128
MachineFunction(Function *Fn, const TargetMachine &TM, unsigned FunctionNum);
131
/// getFunction - Return the LLVM function that this machine code represents
133
Function *getFunction() const { return Fn; }
135
/// getFunctionNumber - Return a unique ID for the current function.
137
unsigned getFunctionNumber() const { return FunctionNumber; }
139
/// getTarget - Return the target machine this machine code is compiled with
141
const TargetMachine &getTarget() const { return Target; }
143
/// getRegInfo - Return information about the registers currently in use.
145
MachineRegisterInfo &getRegInfo() { return *RegInfo; }
146
const MachineRegisterInfo &getRegInfo() const { return *RegInfo; }
148
/// getFrameInfo - Return the frame info object for the current function.
149
/// This object contains information about objects allocated on the stack
150
/// frame of the current function in an abstract way.
152
MachineFrameInfo *getFrameInfo() { return FrameInfo; }
153
const MachineFrameInfo *getFrameInfo() const { return FrameInfo; }
155
/// getJumpTableInfo - Return the jump table info object for the current
156
/// function. This object contains information about jump tables in the
157
/// current function. If the current function has no jump tables, this will
159
const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; }
160
MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; }
162
/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
163
/// does already exist, allocate one.
164
MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind);
167
/// getConstantPool - Return the constant pool object for the current
170
MachineConstantPool *getConstantPool() { return ConstantPool; }
171
const MachineConstantPool *getConstantPool() const { return ConstantPool; }
173
/// getAlignment - Return the alignment (log2, not bytes) of the function.
175
unsigned getAlignment() const { return Alignment; }
177
/// setAlignment - Set the alignment (log2, not bytes) of the function.
179
void setAlignment(unsigned A) { Alignment = A; }
181
/// EnsureAlignment - Make sure the function is at least 'A' bits aligned.
182
void EnsureAlignment(unsigned A) {
183
if (Alignment < A) Alignment = A;
186
/// getInfo - Keep track of various per-function pieces of information for
187
/// backends that would like to do so.
189
template<typename Ty>
192
// This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but
193
// that apparently breaks GCC 3.3.
194
Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty),
195
AlignOf<Ty>::Alignment));
196
MFInfo = new (Loc) Ty(*this);
198
return static_cast<Ty*>(MFInfo);
201
template<typename Ty>
202
const Ty *getInfo() const {
203
return const_cast<MachineFunction*>(this)->getInfo<Ty>();
206
/// getBlockNumbered - MachineBasicBlocks are automatically numbered when they
207
/// are inserted into the machine function. The block number for a machine
208
/// basic block can be found by using the MBB::getBlockNumber method, this
209
/// method provides the inverse mapping.
211
MachineBasicBlock *getBlockNumbered(unsigned N) const {
212
assert(N < MBBNumbering.size() && "Illegal block number");
213
assert(MBBNumbering[N] && "Block was removed from the machine function!");
214
return MBBNumbering[N];
217
/// getNumBlockIDs - Return the number of MBB ID's allocated.
219
unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); }
221
/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
222
/// recomputes them. This guarantees that the MBB numbers are sequential,
223
/// dense, and match the ordering of the blocks within the function. If a
224
/// specific MachineBasicBlock is specified, only that block and those after
225
/// it are renumbered.
226
void RenumberBlocks(MachineBasicBlock *MBBFrom = 0);
228
/// print - Print out the MachineFunction in a format suitable for debugging
229
/// to the specified stream.
231
void print(raw_ostream &OS) const;
233
/// viewCFG - This function is meant for use from the debugger. You can just
234
/// say 'call F->viewCFG()' and a ghostview window should pop up from the
235
/// program, displaying the CFG of the current function with the code for each
236
/// basic block inside. This depends on there being a 'dot' and 'gv' program
239
void viewCFG() const;
241
/// viewCFGOnly - This function is meant for use from the debugger. It works
242
/// just like viewCFG, but it does not include the contents of basic blocks
243
/// into the nodes, just the label. If you are only interested in the CFG
244
/// this can make the graph smaller.
246
void viewCFGOnly() const;
248
/// dump - Print the current MachineFunction to cerr, useful for debugger use.
252
/// verify - Run the current MachineFunction through the machine code
253
/// verifier, useful for debugger use.
254
void verify(Pass *p=NULL, bool allowDoubleDefs=false) const;
256
// Provide accessors for the MachineBasicBlock list...
257
typedef BasicBlockListType::iterator iterator;
258
typedef BasicBlockListType::const_iterator const_iterator;
259
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
260
typedef std::reverse_iterator<iterator> reverse_iterator;
262
/// addLiveIn - Add the specified physical register as a live-in value and
263
/// create a corresponding virtual register for it.
264
unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC);
266
//===--------------------------------------------------------------------===//
267
// BasicBlock accessor functions.
269
iterator begin() { return BasicBlocks.begin(); }
270
const_iterator begin() const { return BasicBlocks.begin(); }
271
iterator end () { return BasicBlocks.end(); }
272
const_iterator end () const { return BasicBlocks.end(); }
274
reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
275
const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
276
reverse_iterator rend () { return BasicBlocks.rend(); }
277
const_reverse_iterator rend () const { return BasicBlocks.rend(); }
279
unsigned size() const { return (unsigned)BasicBlocks.size();}
280
bool empty() const { return BasicBlocks.empty(); }
281
const MachineBasicBlock &front() const { return BasicBlocks.front(); }
282
MachineBasicBlock &front() { return BasicBlocks.front(); }
283
const MachineBasicBlock & back() const { return BasicBlocks.back(); }
284
MachineBasicBlock & back() { return BasicBlocks.back(); }
286
void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); }
287
void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); }
288
void insert(iterator MBBI, MachineBasicBlock *MBB) {
289
BasicBlocks.insert(MBBI, MBB);
291
void splice(iterator InsertPt, iterator MBBI) {
292
BasicBlocks.splice(InsertPt, BasicBlocks, MBBI);
294
void splice(iterator InsertPt, iterator MBBI, iterator MBBE) {
295
BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE);
298
void remove(iterator MBBI) {
299
BasicBlocks.remove(MBBI);
301
void erase(iterator MBBI) {
302
BasicBlocks.erase(MBBI);
305
//===--------------------------------------------------------------------===//
306
// Internal functions used to automatically number MachineBasicBlocks
309
/// getNextMBBNumber - Returns the next unique number to be assigned
310
/// to a MachineBasicBlock in this MachineFunction.
312
unsigned addToMBBNumbering(MachineBasicBlock *MBB) {
313
MBBNumbering.push_back(MBB);
314
return (unsigned)MBBNumbering.size()-1;
317
/// removeFromMBBNumbering - Remove the specific machine basic block from our
318
/// tracker, this is only really to be used by the MachineBasicBlock
320
void removeFromMBBNumbering(unsigned N) {
321
assert(N < MBBNumbering.size() && "Illegal basic block #");
325
/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
326
/// of `new MachineInstr'.
328
MachineInstr *CreateMachineInstr(const TargetInstrDesc &TID,
332
/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
333
/// 'Orig' instruction, identical in all ways except the instruction
334
/// has no parent, prev, or next.
336
/// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned
338
MachineInstr *CloneMachineInstr(const MachineInstr *Orig);
340
/// DeleteMachineInstr - Delete the given MachineInstr.
342
void DeleteMachineInstr(MachineInstr *MI);
344
/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
345
/// instead of `new MachineBasicBlock'.
347
MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0);
349
/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
351
void DeleteMachineBasicBlock(MachineBasicBlock *MBB);
353
/// getMachineMemOperand - Allocate a new MachineMemOperand.
354
/// MachineMemOperands are owned by the MachineFunction and need not be
355
/// explicitly deallocated.
356
MachineMemOperand *getMachineMemOperand(const Value *v, unsigned f,
357
int64_t o, uint64_t s,
358
unsigned base_alignment);
360
/// getMachineMemOperand - Allocate a new MachineMemOperand by copying
361
/// an existing one, adjusting by an offset and using the given size.
362
/// MachineMemOperands are owned by the MachineFunction and need not be
363
/// explicitly deallocated.
364
MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO,
365
int64_t Offset, uint64_t Size);
367
/// allocateMemRefsArray - Allocate an array to hold MachineMemOperand
368
/// pointers. This array is owned by the MachineFunction.
369
MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num);
371
/// extractLoadMemRefs - Allocate an array and populate it with just the
372
/// load information from the given MachineMemOperand sequence.
373
std::pair<MachineInstr::mmo_iterator,
374
MachineInstr::mmo_iterator>
375
extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
376
MachineInstr::mmo_iterator End);
378
/// extractStoreMemRefs - Allocate an array and populate it with just the
379
/// store information from the given MachineMemOperand sequence.
380
std::pair<MachineInstr::mmo_iterator,
381
MachineInstr::mmo_iterator>
382
extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
383
MachineInstr::mmo_iterator End);
385
//===--------------------------------------------------------------------===//
386
// Label Manipulation.
389
/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
390
/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
391
/// normal 'L' label is returned.
392
MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx,
393
bool isLinkerPrivate = false) const;
396
//===--------------------------------------------------------------------===//
400
/// getDILocation - Get the DILocation for a given DebugLoc object.
401
DILocation getDILocation(DebugLoc DL) const;
403
/// getDefaultDebugLoc - Get the default debug location for the machine
405
DebugLoc getDefaultDebugLoc() const { return DefaultDebugLoc; }
407
/// setDefaultDebugLoc - Get the default debug location for the machine
409
void setDefaultDebugLoc(DebugLoc DL) { DefaultDebugLoc = DL; }
411
/// getDebugLocInfo - Get the debug info location tracker.
412
DebugLocTracker &getDebugLocInfo() { return DebugLocInfo; }
415
//===--------------------------------------------------------------------===//
416
// GraphTraits specializations for function basic block graphs (CFGs)
417
//===--------------------------------------------------------------------===//
419
// Provide specializations of GraphTraits to be able to treat a
420
// machine function as a graph of machine basic blocks... these are
421
// the same as the machine basic block iterators, except that the root
422
// node is implicitly the first node of the function.
424
template <> struct GraphTraits<MachineFunction*> :
425
public GraphTraits<MachineBasicBlock*> {
426
static NodeType *getEntryNode(MachineFunction *F) {
430
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
431
typedef MachineFunction::iterator nodes_iterator;
432
static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); }
433
static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); }
435
template <> struct GraphTraits<const MachineFunction*> :
436
public GraphTraits<const MachineBasicBlock*> {
437
static NodeType *getEntryNode(const MachineFunction *F) {
441
// nodes_iterator/begin/end - Allow iteration over all nodes in the graph
442
typedef MachineFunction::const_iterator nodes_iterator;
443
static nodes_iterator nodes_begin(const MachineFunction *F) {
446
static nodes_iterator nodes_end (const MachineFunction *F) {
452
// Provide specializations of GraphTraits to be able to treat a function as a
453
// graph of basic blocks... and to walk it in inverse order. Inverse order for
454
// a function is considered to be when traversing the predecessor edges of a BB
455
// instead of the successor edges.
457
template <> struct GraphTraits<Inverse<MachineFunction*> > :
458
public GraphTraits<Inverse<MachineBasicBlock*> > {
459
static NodeType *getEntryNode(Inverse<MachineFunction*> G) {
460
return &G.Graph->front();
463
template <> struct GraphTraits<Inverse<const MachineFunction*> > :
464
public GraphTraits<Inverse<const MachineBasicBlock*> > {
465
static NodeType *getEntryNode(Inverse<const MachineFunction *> G) {
466
return &G.Graph->front();
470
} // End llvm namespace