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