1
//===-- Local.h - Functions to perform local transformations ----*- 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
// This family of functions perform various local transformations to the
13
//===----------------------------------------------------------------------===//
15
#ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H
16
#define LLVM_TRANSFORMS_UTILS_LOCAL_H
31
template<typename T> class SmallVectorImpl;
33
//===----------------------------------------------------------------------===//
34
// Local constant propagation.
37
/// ConstantFoldTerminator - If a terminator instruction is predicated on a
38
/// constant value, convert it into an unconditional branch to the constant
39
/// destination. This is a nontrivial operation because the successors of this
40
/// basic block must have their PHI nodes updated.
42
bool ConstantFoldTerminator(BasicBlock *BB);
44
//===----------------------------------------------------------------------===//
45
// Local dead code elimination.
48
/// isInstructionTriviallyDead - Return true if the result produced by the
49
/// instruction is not used, and the instruction has no side effects.
51
bool isInstructionTriviallyDead(Instruction *I);
53
/// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
54
/// trivially dead instruction, delete it. If that makes any of its operands
55
/// trivially dead, delete them too, recursively. Return true if any
56
/// instructions were deleted.
57
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V);
59
/// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
60
/// dead PHI node, due to being a def-use chain of single-use nodes that
61
/// either forms a cycle or is terminated by a trivially dead instruction,
62
/// delete it. If that makes any of its operands trivially dead, delete them
63
/// too, recursively. Return true if the PHI node is actually deleted.
64
bool RecursivelyDeleteDeadPHINode(PHINode *PN);
67
/// SimplifyInstructionsInBlock - Scan the specified basic block and try to
68
/// simplify any instructions in it and recursively delete dead instructions.
70
/// This returns true if it changed the code, note that it can delete
71
/// instructions in other blocks as well in this block.
72
bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD = 0);
74
//===----------------------------------------------------------------------===//
75
// Control Flow Graph Restructuring.
78
/// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
79
/// method is called when we're about to delete Pred as a predecessor of BB. If
80
/// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
82
/// Unlike the removePredecessor method, this attempts to simplify uses of PHI
83
/// nodes that collapse into identity values. For example, if we have:
84
/// x = phi(1, 0, 0, 0)
87
/// .. and delete the predecessor corresponding to the '1', this will attempt to
88
/// recursively fold the 'and' to 0.
89
void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
93
/// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
94
/// predecessor is known to have one successor (BB!). Eliminate the edge
95
/// between them, moving the instructions in the predecessor into BB. This
96
/// deletes the predecessor block.
98
void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0);
101
/// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
102
/// unconditional branch, and contains no instructions other than PHI nodes,
103
/// potential debug intrinsics and the branch. If possible, eliminate BB by
104
/// rewriting all the predecessors to branch to the successor block and return
105
/// true. If we can't transform, return false.
106
bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
108
/// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
109
/// nodes in this block. This doesn't try to be clever about PHI nodes
110
/// which differ only in the order of the incoming values, but instcombine
111
/// orders them so it usually won't matter.
113
bool EliminateDuplicatePHINodes(BasicBlock *BB);
115
/// SimplifyCFG - This function is used to do simplification of a CFG. For
116
/// example, it adjusts branches to branches to eliminate the extra hop, it
117
/// eliminates unreachable basic blocks, and does other "peephole" optimization
118
/// of the CFG. It returns true if a modification was made, possibly deleting
119
/// the basic block that was pointed to.
121
bool SimplifyCFG(BasicBlock *BB, const TargetData *TD = 0);
123
/// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
124
/// and if a predecessor branches to us and one of our successors, fold the
125
/// setcc into the predecessor and use logical operations to pick the right
127
bool FoldBranchToCommonDest(BranchInst *BI);
129
/// DemoteRegToStack - This function takes a virtual register computed by an
130
/// Instruction and replaces it with a slot in the stack frame, allocated via
131
/// alloca. This allows the CFG to be changed around without fear of
132
/// invalidating the SSA information for the value. It returns the pointer to
133
/// the alloca inserted to create a stack slot for X.
135
AllocaInst *DemoteRegToStack(Instruction &X,
136
bool VolatileLoads = false,
137
Instruction *AllocaPoint = 0);
139
/// DemotePHIToStack - This function takes a virtual register computed by a phi
140
/// node and replaces it with a slot in the stack frame, allocated via alloca.
141
/// The phi node is deleted and it returns the pointer to the alloca inserted.
142
AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
144
} // End llvm namespace