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
35 template<typename T> class SmallVectorImpl;
37 //===----------------------------------------------------------------------===//
38 // Local constant propagation.
41 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
42 /// constant value, convert it into an unconditional branch to the constant
43 /// destination. This is a nontrivial operation because the successors of this
44 /// basic block must have their PHI nodes updated.
46 bool ConstantFoldTerminator(BasicBlock *BB);
48 //===----------------------------------------------------------------------===//
49 // Local dead code elimination.
52 /// isInstructionTriviallyDead - Return true if the result produced by the
53 /// instruction is not used, and the instruction has no side effects.
55 bool isInstructionTriviallyDead(Instruction *I);
57 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
58 /// trivially dead instruction, delete it. If that makes any of its operands
59 /// trivially dead, delete them too, recursively. Return true if any
60 /// instructions were deleted.
61 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V);
63 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
64 /// dead PHI node, due to being a def-use chain of single-use nodes that
65 /// either forms a cycle or is terminated by a trivially dead instruction,
66 /// delete it. If that makes any of its operands trivially dead, delete them
67 /// too, recursively. Return true if a change was made.
68 bool RecursivelyDeleteDeadPHINode(PHINode *PN);
71 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
72 /// simplify any instructions in it and recursively delete dead instructions.
74 /// This returns true if it changed the code, note that it can delete
75 /// instructions in other blocks as well in this block.
77 /// WARNING: Do not use this function on unreachable blocks, as recursive
78 /// simplification is not able to handle corner-case scenarios that can
80 bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD = 0);
82 //===----------------------------------------------------------------------===//
83 // Control Flow Graph Restructuring.
86 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
87 /// method is called when we're about to delete Pred as a predecessor of BB. If
88 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
90 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
91 /// nodes that collapse into identity values. For example, if we have:
92 /// x = phi(1, 0, 0, 0)
95 /// .. and delete the predecessor corresponding to the '1', this will attempt to
96 /// recursively fold the 'and' to 0.
97 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
101 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
102 /// predecessor is known to have one successor (BB!). Eliminate the edge
103 /// between them, moving the instructions in the predecessor into BB. This
104 /// deletes the predecessor block.
106 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0);
109 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
110 /// unconditional branch, and contains no instructions other than PHI nodes,
111 /// potential debug intrinsics and the branch. If possible, eliminate BB by
112 /// rewriting all the predecessors to branch to the successor block and return
113 /// true. If we can't transform, return false.
114 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
116 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
117 /// nodes in this block. This doesn't try to be clever about PHI nodes
118 /// which differ only in the order of the incoming values, but instcombine
119 /// orders them so it usually won't matter.
121 bool EliminateDuplicatePHINodes(BasicBlock *BB);
123 /// SimplifyCFG - This function is used to do simplification of a CFG. For
124 /// example, it adjusts branches to branches to eliminate the extra hop, it
125 /// eliminates unreachable basic blocks, and does other "peephole" optimization
126 /// of the CFG. It returns true if a modification was made, possibly deleting
127 /// the basic block that was pointed to.
129 bool SimplifyCFG(BasicBlock *BB, const TargetData *TD = 0);
131 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
132 /// and if a predecessor branches to us and one of our successors, fold the
133 /// setcc into the predecessor and use logical operations to pick the right
135 bool FoldBranchToCommonDest(BranchInst *BI);
137 /// DemoteRegToStack - This function takes a virtual register computed by an
138 /// Instruction and replaces it with a slot in the stack frame, allocated via
139 /// alloca. This allows the CFG to be changed around without fear of
140 /// invalidating the SSA information for the value. It returns the pointer to
141 /// the alloca inserted to create a stack slot for X.
143 AllocaInst *DemoteRegToStack(Instruction &X,
144 bool VolatileLoads = false,
145 Instruction *AllocaPoint = 0);
147 /// DemotePHIToStack - This function takes a virtual register computed by a phi
148 /// node and replaces it with a slot in the stack frame, allocated via alloca.
149 /// The phi node is deleted and it returns the pointer to the alloca inserted.
150 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
152 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that
153 /// we can determine, return it, otherwise return 0. If PrefAlign is specified,
154 /// and it is more than the alignment of the ultimate object, see if we can
155 /// increase the alignment of the ultimate object, making this check succeed.
156 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
157 const TargetData *TD = 0);
159 /// getKnownAlignment - Try to infer an alignment for the specified pointer.
160 static inline unsigned getKnownAlignment(Value *V, const TargetData *TD = 0) {
161 return getOrEnforceKnownAlignment(V, 0, TD);
164 ///===---------------------------------------------------------------------===//
165 /// Dbg Intrinsic utilities
168 /// Inserts a llvm.dbg.value instrinsic before the stores to an alloca'd value
169 /// that has an associated llvm.dbg.decl intrinsic.
170 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
171 StoreInst *SI, DIBuilder &Builder);
173 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
174 /// of llvm.dbg.value intrinsics.
175 bool LowerDbgDeclare(Function &F);
177 } // End llvm namespace