1
//===-- llvm/CodeGen/MachineBasicBlock.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 the sequence of machine instructions for a basic block.
12
//===----------------------------------------------------------------------===//
14
#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
15
#define LLVM_CODEGEN_MACHINEBASICBLOCK_H
17
#include "llvm/CodeGen/MachineInstr.h"
18
#include "llvm/ADT/GraphTraits.h"
23
class MachineFunction;
30
struct ilist_traits<MachineInstr> : public ilist_default_traits<MachineInstr> {
32
mutable ilist_half_node<MachineInstr> Sentinel;
34
// this is only set by the MachineBasicBlock owning the LiveList
35
friend class MachineBasicBlock;
36
MachineBasicBlock* Parent;
39
MachineInstr *createSentinel() const {
40
return static_cast<MachineInstr*>(&Sentinel);
42
void destroySentinel(MachineInstr *) const {}
44
MachineInstr *provideInitialHead() const { return createSentinel(); }
45
MachineInstr *ensureHead(MachineInstr*) const { return createSentinel(); }
46
static void noteHead(MachineInstr*, MachineInstr*) {}
48
void addNodeToList(MachineInstr* N);
49
void removeNodeFromList(MachineInstr* N);
50
void transferNodesFromList(ilist_traits &SrcTraits,
51
ilist_iterator<MachineInstr> first,
52
ilist_iterator<MachineInstr> last);
53
void deleteNode(MachineInstr *N);
55
void createNode(const MachineInstr &);
58
class MachineBasicBlock : public ilist_node<MachineBasicBlock> {
59
typedef ilist<MachineInstr> Instructions;
63
MachineFunction *xParent;
65
/// Predecessors/Successors - Keep track of the predecessor / successor
67
std::vector<MachineBasicBlock *> Predecessors;
68
std::vector<MachineBasicBlock *> Successors;
70
/// LiveIns - Keep track of the physical registers that are livein of
72
std::vector<unsigned> LiveIns;
74
/// Alignment - Alignment of the basic block. Zero if the basic block does
75
/// not need to be aligned.
78
/// IsLandingPad - Indicate that this basic block is entered via an
79
/// exception handler.
82
/// AddressTaken - Indicate that this basic block is potentially the
83
/// target of an indirect branch.
86
// Intrusive list support
87
MachineBasicBlock() {}
89
explicit MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb);
93
// MachineBasicBlocks are allocated and owned by MachineFunction.
94
friend class MachineFunction;
97
/// getBasicBlock - Return the LLVM basic block that this instance
98
/// corresponded to originally. Note that this may be NULL if this instance
99
/// does not correspond directly to an LLVM basic block.
101
const BasicBlock *getBasicBlock() const { return BB; }
103
/// getName - Return the name of the corresponding LLVM basic block, or
105
StringRef getName() const;
107
/// hasAddressTaken - Test whether this block is potentially the target
108
/// of an indirect branch.
109
bool hasAddressTaken() const { return AddressTaken; }
111
/// setHasAddressTaken - Set this block to reflect that it potentially
112
/// is the target of an indirect branch.
113
void setHasAddressTaken() { AddressTaken = true; }
115
/// getParent - Return the MachineFunction containing this basic block.
117
const MachineFunction *getParent() const { return xParent; }
118
MachineFunction *getParent() { return xParent; }
120
typedef Instructions::iterator iterator;
121
typedef Instructions::const_iterator const_iterator;
122
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
123
typedef std::reverse_iterator<iterator> reverse_iterator;
125
unsigned size() const { return (unsigned)Insts.size(); }
126
bool empty() const { return Insts.empty(); }
128
MachineInstr& front() { return Insts.front(); }
129
MachineInstr& back() { return Insts.back(); }
130
const MachineInstr& front() const { return Insts.front(); }
131
const MachineInstr& back() const { return Insts.back(); }
133
iterator begin() { return Insts.begin(); }
134
const_iterator begin() const { return Insts.begin(); }
135
iterator end() { return Insts.end(); }
136
const_iterator end() const { return Insts.end(); }
137
reverse_iterator rbegin() { return Insts.rbegin(); }
138
const_reverse_iterator rbegin() const { return Insts.rbegin(); }
139
reverse_iterator rend () { return Insts.rend(); }
140
const_reverse_iterator rend () const { return Insts.rend(); }
142
// Machine-CFG iterators
143
typedef std::vector<MachineBasicBlock *>::iterator pred_iterator;
144
typedef std::vector<MachineBasicBlock *>::const_iterator const_pred_iterator;
145
typedef std::vector<MachineBasicBlock *>::iterator succ_iterator;
146
typedef std::vector<MachineBasicBlock *>::const_iterator const_succ_iterator;
147
typedef std::vector<MachineBasicBlock *>::reverse_iterator
148
pred_reverse_iterator;
149
typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
150
const_pred_reverse_iterator;
151
typedef std::vector<MachineBasicBlock *>::reverse_iterator
152
succ_reverse_iterator;
153
typedef std::vector<MachineBasicBlock *>::const_reverse_iterator
154
const_succ_reverse_iterator;
156
pred_iterator pred_begin() { return Predecessors.begin(); }
157
const_pred_iterator pred_begin() const { return Predecessors.begin(); }
158
pred_iterator pred_end() { return Predecessors.end(); }
159
const_pred_iterator pred_end() const { return Predecessors.end(); }
160
pred_reverse_iterator pred_rbegin()
161
{ return Predecessors.rbegin();}
162
const_pred_reverse_iterator pred_rbegin() const
163
{ return Predecessors.rbegin();}
164
pred_reverse_iterator pred_rend()
165
{ return Predecessors.rend(); }
166
const_pred_reverse_iterator pred_rend() const
167
{ return Predecessors.rend(); }
168
unsigned pred_size() const {
169
return (unsigned)Predecessors.size();
171
bool pred_empty() const { return Predecessors.empty(); }
172
succ_iterator succ_begin() { return Successors.begin(); }
173
const_succ_iterator succ_begin() const { return Successors.begin(); }
174
succ_iterator succ_end() { return Successors.end(); }
175
const_succ_iterator succ_end() const { return Successors.end(); }
176
succ_reverse_iterator succ_rbegin()
177
{ return Successors.rbegin(); }
178
const_succ_reverse_iterator succ_rbegin() const
179
{ return Successors.rbegin(); }
180
succ_reverse_iterator succ_rend()
181
{ return Successors.rend(); }
182
const_succ_reverse_iterator succ_rend() const
183
{ return Successors.rend(); }
184
unsigned succ_size() const {
185
return (unsigned)Successors.size();
187
bool succ_empty() const { return Successors.empty(); }
189
// LiveIn management methods.
191
/// addLiveIn - Add the specified register as a live in. Note that it
192
/// is an error to add the same register to the same set more than once.
193
void addLiveIn(unsigned Reg) { LiveIns.push_back(Reg); }
195
/// removeLiveIn - Remove the specified register from the live in set.
197
void removeLiveIn(unsigned Reg);
199
/// isLiveIn - Return true if the specified register is in the live in set.
201
bool isLiveIn(unsigned Reg) const;
203
// Iteration support for live in sets. These sets are kept in sorted
204
// order by their register number.
205
typedef std::vector<unsigned>::iterator livein_iterator;
206
typedef std::vector<unsigned>::const_iterator const_livein_iterator;
207
livein_iterator livein_begin() { return LiveIns.begin(); }
208
const_livein_iterator livein_begin() const { return LiveIns.begin(); }
209
livein_iterator livein_end() { return LiveIns.end(); }
210
const_livein_iterator livein_end() const { return LiveIns.end(); }
211
bool livein_empty() const { return LiveIns.empty(); }
213
/// getAlignment - Return alignment of the basic block.
215
unsigned getAlignment() const { return Alignment; }
217
/// setAlignment - Set alignment of the basic block.
219
void setAlignment(unsigned Align) { Alignment = Align; }
221
/// isLandingPad - Returns true if the block is a landing pad. That is
222
/// this basic block is entered via an exception handler.
223
bool isLandingPad() const { return IsLandingPad; }
225
/// setIsLandingPad - Indicates the block is a landing pad. That is
226
/// this basic block is entered via an exception handler.
227
void setIsLandingPad() { IsLandingPad = true; }
229
// Code Layout methods.
231
/// moveBefore/moveAfter - move 'this' block before or after the specified
232
/// block. This only moves the block, it does not modify the CFG or adjust
233
/// potential fall-throughs at the end of the block.
234
void moveBefore(MachineBasicBlock *NewAfter);
235
void moveAfter(MachineBasicBlock *NewBefore);
237
/// updateTerminator - Update the terminator instructions in block to account
238
/// for changes to the layout. If the block previously used a fallthrough,
239
/// it may now need a branch, and if it previously used branching it may now
240
/// be able to use a fallthrough.
241
void updateTerminator();
243
// Machine-CFG mutators
245
/// addSuccessor - Add succ as a successor of this MachineBasicBlock.
246
/// The Predecessors list of succ is automatically updated.
248
void addSuccessor(MachineBasicBlock *succ);
250
/// removeSuccessor - Remove successor from the successors list of this
251
/// MachineBasicBlock. The Predecessors list of succ is automatically updated.
253
void removeSuccessor(MachineBasicBlock *succ);
255
/// removeSuccessor - Remove specified successor from the successors list of
256
/// this MachineBasicBlock. The Predecessors list of succ is automatically
257
/// updated. Return the iterator to the element after the one removed.
259
succ_iterator removeSuccessor(succ_iterator I);
261
/// transferSuccessors - Transfers all the successors from MBB to this
262
/// machine basic block (i.e., copies all the successors fromMBB and
263
/// remove all the successors from fromMBB).
264
void transferSuccessors(MachineBasicBlock *fromMBB);
266
/// isSuccessor - Return true if the specified MBB is a successor of this
268
bool isSuccessor(const MachineBasicBlock *MBB) const;
270
/// isLayoutSuccessor - Return true if the specified MBB will be emitted
271
/// immediately after this block, such that if this block exits by
272
/// falling through, control will transfer to the specified MBB. Note
273
/// that MBB need not be a successor at all, for example if this block
274
/// ends with an unconditional branch to some other block.
275
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const;
277
/// canFallThrough - Return true if the block can implicitly transfer
278
/// control to the block after it by falling off the end of it. This should
279
/// return false if it can reach the block after it, but it uses an explicit
280
/// branch to do so (e.g., a table jump). True is a conservative answer.
281
bool canFallThrough();
283
/// getFirstTerminator - returns an iterator to the first terminator
284
/// instruction of this basic block. If a terminator does not exist,
286
iterator getFirstTerminator();
288
void pop_front() { Insts.pop_front(); }
289
void pop_back() { Insts.pop_back(); }
290
void push_back(MachineInstr *MI) { Insts.push_back(MI); }
291
template<typename IT>
292
void insert(iterator I, IT S, IT E) { Insts.insert(I, S, E); }
293
iterator insert(iterator I, MachineInstr *M) { return Insts.insert(I, M); }
295
// erase - Remove the specified element or range from the instruction list.
296
// These functions delete any instructions removed.
298
iterator erase(iterator I) { return Insts.erase(I); }
299
iterator erase(iterator I, iterator E) { return Insts.erase(I, E); }
300
MachineInstr *remove(MachineInstr *I) { return Insts.remove(I); }
301
void clear() { Insts.clear(); }
303
/// splice - Take an instruction from MBB 'Other' at the position From,
304
/// and insert it into this MBB right before 'where'.
305
void splice(iterator where, MachineBasicBlock *Other, iterator From) {
306
Insts.splice(where, Other->Insts, From);
309
/// splice - Take a block of instructions from MBB 'Other' in the range [From,
310
/// To), and insert them into this MBB right before 'where'.
311
void splice(iterator where, MachineBasicBlock *Other, iterator From,
313
Insts.splice(where, Other->Insts, From, To);
316
/// removeFromParent - This method unlinks 'this' from the containing
317
/// function, and returns it, but does not delete it.
318
MachineBasicBlock *removeFromParent();
320
/// eraseFromParent - This method unlinks 'this' from the containing
321
/// function and deletes it.
322
void eraseFromParent();
324
/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to
325
/// 'Old', change the code and CFG so that it branches to 'New' instead.
326
void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New);
328
/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in
329
/// the CFG to be inserted. If we have proven that MBB can only branch to
330
/// DestA and DestB, remove any other MBB successors from the CFG. DestA and
331
/// DestB can be null. Besides DestA and DestB, retain other edges leading
332
/// to LandingPads (currently there can be only one; we don't check or require
333
/// that here). Note it is possible that DestA and/or DestB are LandingPads.
334
bool CorrectExtraCFGEdges(MachineBasicBlock *DestA,
335
MachineBasicBlock *DestB,
338
/// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping
339
/// any DBG_VALUE instructions. Return UnknownLoc if there is none.
340
DebugLoc findDebugLoc(MachineBasicBlock::iterator &MBBI);
342
// Debugging methods.
344
void print(raw_ostream &OS) const;
346
/// getNumber - MachineBasicBlocks are uniquely numbered at the function
347
/// level, unless they're not in a MachineFunction yet, in which case this
350
int getNumber() const { return Number; }
351
void setNumber(int N) { Number = N; }
353
/// getSymbol - Return the MCSymbol for this basic block.
355
MCSymbol *getSymbol(MCContext &Ctx) const;
357
private: // Methods used to maintain doubly linked list of blocks...
358
friend struct ilist_traits<MachineBasicBlock>;
360
// Machine-CFG mutators
362
/// addPredecessor - Remove pred as a predecessor of this MachineBasicBlock.
363
/// Don't do this unless you know what you're doing, because it doesn't
364
/// update pred's successors list. Use pred->addSuccessor instead.
366
void addPredecessor(MachineBasicBlock *pred);
368
/// removePredecessor - Remove pred as a predecessor of this
369
/// MachineBasicBlock. Don't do this unless you know what you're
370
/// doing, because it doesn't update pred's successors list. Use
371
/// pred->removeSuccessor instead.
373
void removePredecessor(MachineBasicBlock *pred);
376
raw_ostream& operator<<(raw_ostream &OS, const MachineBasicBlock &MBB);
378
void WriteAsOperand(raw_ostream &, const MachineBasicBlock*, bool t);
380
//===--------------------------------------------------------------------===//
381
// GraphTraits specializations for machine basic block graphs (machine-CFGs)
382
//===--------------------------------------------------------------------===//
384
// Provide specializations of GraphTraits to be able to treat a
385
// MachineFunction as a graph of MachineBasicBlocks...
388
template <> struct GraphTraits<MachineBasicBlock *> {
389
typedef MachineBasicBlock NodeType;
390
typedef MachineBasicBlock::succ_iterator ChildIteratorType;
392
static NodeType *getEntryNode(MachineBasicBlock *BB) { return BB; }
393
static inline ChildIteratorType child_begin(NodeType *N) {
394
return N->succ_begin();
396
static inline ChildIteratorType child_end(NodeType *N) {
397
return N->succ_end();
401
template <> struct GraphTraits<const MachineBasicBlock *> {
402
typedef const MachineBasicBlock NodeType;
403
typedef MachineBasicBlock::const_succ_iterator ChildIteratorType;
405
static NodeType *getEntryNode(const MachineBasicBlock *BB) { return BB; }
406
static inline ChildIteratorType child_begin(NodeType *N) {
407
return N->succ_begin();
409
static inline ChildIteratorType child_end(NodeType *N) {
410
return N->succ_end();
414
// Provide specializations of GraphTraits to be able to treat a
415
// MachineFunction as a graph of MachineBasicBlocks... and to walk it
416
// in inverse order. Inverse order for a function is considered
417
// to be when traversing the predecessor edges of a MBB
418
// instead of the successor edges.
420
template <> struct GraphTraits<Inverse<MachineBasicBlock*> > {
421
typedef MachineBasicBlock NodeType;
422
typedef MachineBasicBlock::pred_iterator ChildIteratorType;
423
static NodeType *getEntryNode(Inverse<MachineBasicBlock *> G) {
426
static inline ChildIteratorType child_begin(NodeType *N) {
427
return N->pred_begin();
429
static inline ChildIteratorType child_end(NodeType *N) {
430
return N->pred_end();
434
template <> struct GraphTraits<Inverse<const MachineBasicBlock*> > {
435
typedef const MachineBasicBlock NodeType;
436
typedef MachineBasicBlock::const_pred_iterator ChildIteratorType;
437
static NodeType *getEntryNode(Inverse<const MachineBasicBlock*> G) {
440
static inline ChildIteratorType child_begin(NodeType *N) {
441
return N->pred_begin();
443
static inline ChildIteratorType child_end(NodeType *N) {
444
return N->pred_end();
448
} // End llvm namespace