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 //===----------------------------------------------------------------------===//
37 /// isSafeToLoadUnconditionally - Return true if we know that executing a load
38 /// from this value cannot trap. If it is not obviously safe to load from the
39 /// specified pointer, we do a quick local scan of the basic block containing
40 /// ScanFrom, to determine if the address is already accessed.
41 bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom);
43 //===----------------------------------------------------------------------===//
44 // Local constant propagation.
47 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
48 /// constant value, convert it into an unconditional branch to the constant
49 /// destination. This is a nontrivial operation because the successors of this
50 /// basic block must have their PHI nodes updated.
52 bool ConstantFoldTerminator(BasicBlock *BB);
54 //===----------------------------------------------------------------------===//
55 // Local dead code elimination.
58 /// isInstructionTriviallyDead - Return true if the result produced by the
59 /// instruction is not used, and the instruction has no side effects.
61 bool isInstructionTriviallyDead(Instruction *I);
63 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
64 /// trivially dead instruction, delete it. If that makes any of its operands
65 /// trivially dead, delete them too, recursively. Return true if any
66 /// instructions were deleted.
67 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V);
69 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
70 /// dead PHI node, due to being a def-use chain of single-use nodes that
71 /// either forms a cycle or is terminated by a trivially dead instruction,
72 /// delete it. If that makes any of its operands trivially dead, delete them
73 /// too, recursively. Return true if the PHI node is actually deleted.
74 bool RecursivelyDeleteDeadPHINode(PHINode *PN);
77 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
78 /// simplify any instructions in it and recursively delete dead instructions.
80 /// This returns true if it changed the code, note that it can delete
81 /// instructions in other blocks as well in this block.
82 bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD = 0);
84 //===----------------------------------------------------------------------===//
85 // Control Flow Graph Restructuring.
88 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
89 /// method is called when we're about to delete Pred as a predecessor of BB. If
90 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
92 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
93 /// nodes that collapse into identity values. For example, if we have:
94 /// x = phi(1, 0, 0, 0)
97 /// .. and delete the predecessor corresponding to the '1', this will attempt to
98 /// recursively fold the 'and' to 0.
99 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
103 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
104 /// predecessor is known to have one successor (BB!). Eliminate the edge
105 /// between them, moving the instructions in the predecessor into BB. This
106 /// deletes the predecessor block.
108 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0);
111 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
112 /// unconditional branch, and contains no instructions other than PHI nodes,
113 /// potential debug intrinsics and the branch. If possible, eliminate BB by
114 /// rewriting all the predecessors to branch to the successor block and return
115 /// true. If we can't transform, return false.
116 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
118 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
119 /// nodes in this block. This doesn't try to be clever about PHI nodes
120 /// which differ only in the order of the incoming values, but instcombine
121 /// orders them so it usually won't matter.
123 bool EliminateDuplicatePHINodes(BasicBlock *BB);
125 /// SimplifyCFG - This function is used to do simplification of a CFG. For
126 /// example, it adjusts branches to branches to eliminate the extra hop, it
127 /// eliminates unreachable basic blocks, and does other "peephole" optimization
128 /// of the CFG. It returns true if a modification was made, possibly deleting
129 /// the basic block that was pointed to.
131 /// WARNING: The entry node of a method may not be simplified.
133 bool SimplifyCFG(BasicBlock *BB);
135 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
136 /// and if a predecessor branches to us and one of our successors, fold the
137 /// setcc into the predecessor and use logical operations to pick the right
139 bool FoldBranchToCommonDest(BranchInst *BI);
141 /// DemoteRegToStack - This function takes a virtual register computed by an
142 /// Instruction and replaces it with a slot in the stack frame, allocated via
143 /// alloca. This allows the CFG to be changed around without fear of
144 /// invalidating the SSA information for the value. It returns the pointer to
145 /// the alloca inserted to create a stack slot for X.
147 AllocaInst *DemoteRegToStack(Instruction &X,
148 bool VolatileLoads = false,
149 Instruction *AllocaPoint = 0);
151 /// DemotePHIToStack - This function takes a virtual register computed by a phi
152 /// node and replaces it with a slot in the stack frame, allocated via alloca.
153 /// The phi node is deleted and it returns the pointer to the alloca inserted.
154 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
156 } // End llvm namespace