Revert r114097, adding back in the assertion against replacing an Instruction by...
[oota-llvm.git] / include / llvm / Transforms / Utils / Local.h
1 //===-- Local.h - Functions to perform local transformations ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This family of functions perform various local transformations to the
11 // program.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H
16 #define LLVM_TRANSFORMS_UTILS_LOCAL_H
17
18 namespace llvm {
19
20 class User;
21 class BasicBlock;
22 class BranchInst;
23 class Instruction;
24 class Value;
25 class Pass;
26 class PHINode;
27 class AllocaInst;
28 class ConstantExpr;
29 class TargetData;
30
31 template<typename T> class SmallVectorImpl;
32   
33 //===----------------------------------------------------------------------===//
34 //  Local constant propagation.
35 //
36
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.
41 ///
42 bool ConstantFoldTerminator(BasicBlock *BB);
43
44 //===----------------------------------------------------------------------===//
45 //  Local dead code elimination.
46 //
47
48 /// isInstructionTriviallyDead - Return true if the result produced by the
49 /// instruction is not used, and the instruction has no side effects.
50 ///
51 bool isInstructionTriviallyDead(Instruction *I);
52
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);
58
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);
65
66   
67 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
68 /// simplify any instructions in it and recursively delete dead instructions.
69 ///
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 ///
73 /// WARNING: Do not use this function on unreachable blocks, as recursive
74 /// simplification is not able to handle corner-case scenarios that can
75 /// arise in them.
76 bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD = 0);
77     
78 //===----------------------------------------------------------------------===//
79 //  Control Flow Graph Restructuring.
80 //
81
82 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
83 /// method is called when we're about to delete Pred as a predecessor of BB.  If
84 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
85 ///
86 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
87 /// nodes that collapse into identity values.  For example, if we have:
88 ///   x = phi(1, 0, 0, 0)
89 ///   y = and x, z
90 ///
91 /// .. and delete the predecessor corresponding to the '1', this will attempt to
92 /// recursively fold the 'and' to 0.
93 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
94                                   TargetData *TD = 0);
95     
96   
97 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
98 /// predecessor is known to have one successor (BB!).  Eliminate the edge
99 /// between them, moving the instructions in the predecessor into BB.  This
100 /// deletes the predecessor block.
101 ///
102 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0);
103     
104
105 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
106 /// unconditional branch, and contains no instructions other than PHI nodes,
107 /// potential debug intrinsics and the branch.  If possible, eliminate BB by
108 /// rewriting all the predecessors to branch to the successor block and return
109 /// true.  If we can't transform, return false.
110 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
111
112 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
113 /// nodes in this block. This doesn't try to be clever about PHI nodes
114 /// which differ only in the order of the incoming values, but instcombine
115 /// orders them so it usually won't matter.
116 ///
117 bool EliminateDuplicatePHINodes(BasicBlock *BB);
118
119 /// SimplifyCFG - This function is used to do simplification of a CFG.  For
120 /// example, it adjusts branches to branches to eliminate the extra hop, it
121 /// eliminates unreachable basic blocks, and does other "peephole" optimization
122 /// of the CFG.  It returns true if a modification was made, possibly deleting
123 /// the basic block that was pointed to.
124 ///
125 bool SimplifyCFG(BasicBlock *BB, const TargetData *TD = 0);
126
127 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
128 /// and if a predecessor branches to us and one of our successors, fold the
129 /// setcc into the predecessor and use logical operations to pick the right
130 /// destination.
131 bool FoldBranchToCommonDest(BranchInst *BI);
132
133 /// DemoteRegToStack - This function takes a virtual register computed by an
134 /// Instruction and replaces it with a slot in the stack frame, allocated via
135 /// alloca.  This allows the CFG to be changed around without fear of
136 /// invalidating the SSA information for the value.  It returns the pointer to
137 /// the alloca inserted to create a stack slot for X.
138 ///
139 AllocaInst *DemoteRegToStack(Instruction &X,
140                              bool VolatileLoads = false,
141                              Instruction *AllocaPoint = 0);
142
143 /// DemotePHIToStack - This function takes a virtual register computed by a phi
144 /// node and replaces it with a slot in the stack frame, allocated via alloca.
145 /// The phi node is deleted and it returns the pointer to the alloca inserted. 
146 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
147
148 } // End llvm namespace
149
150 #endif