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 struct DbgInfoIntrinsic;
33 template<typename T> class SmallVectorImpl;
35 //===----------------------------------------------------------------------===//
39 /// isSafeToLoadUnconditionally - Return true if we know that executing a load
40 /// from this value cannot trap. If it is not obviously safe to load from the
41 /// specified pointer, we do a quick local scan of the basic block containing
42 /// ScanFrom, to determine if the address is already accessed.
43 bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom);
45 //===----------------------------------------------------------------------===//
46 // Local constant propagation.
49 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
50 /// constant value, convert it into an unconditional branch to the constant
51 /// destination. This is a nontrivial operation because the successors of this
52 /// basic block must have their PHI nodes updated.
54 bool ConstantFoldTerminator(BasicBlock *BB);
56 //===----------------------------------------------------------------------===//
57 // Local dead code elimination.
60 /// isInstructionTriviallyDead - Return true if the result produced by the
61 /// instruction is not used, and the instruction has no side effects.
63 bool isInstructionTriviallyDead(Instruction *I);
65 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
66 /// trivially dead instruction, delete it. If that makes any of its operands
67 /// trivially dead, delete them too, recursively.
68 void RecursivelyDeleteTriviallyDeadInstructions(Value *V);
70 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
71 /// dead PHI node, due to being a def-use chain of single-use nodes that
72 /// either forms a cycle or is terminated by a trivially dead instruction,
73 /// delete it. If that makes any of its operands trivially dead, delete them
75 void RecursivelyDeleteDeadPHINode(PHINode *PN);
77 //===----------------------------------------------------------------------===//
78 // Control Flow Graph Restructuring.
81 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
82 /// predecessor is known to have one successor (BB!). Eliminate the edge
83 /// between them, moving the instructions in the predecessor into BB. This
84 /// deletes the predecessor block.
86 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0);
89 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
90 /// unconditional branch, and contains no instructions other than PHI nodes,
91 /// potential debug intrinsics and the branch. If possible, eliminate BB by
92 /// rewriting all the predecessors to branch to the successor block and return
93 /// true. If we can't transform, return false.
94 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
96 /// SimplifyCFG - This function is used to do simplification of a CFG. For
97 /// example, it adjusts branches to branches to eliminate the extra hop, it
98 /// eliminates unreachable basic blocks, and does other "peephole" optimization
99 /// of the CFG. It returns true if a modification was made, possibly deleting
100 /// the basic block that was pointed to.
102 /// WARNING: The entry node of a method may not be simplified.
104 bool SimplifyCFG(BasicBlock *BB);
106 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
107 /// and if a predecessor branches to us and one of our successors, fold the
108 /// setcc into the predecessor and use logical operations to pick the right
110 bool FoldBranchToCommonDest(BranchInst *BI);
112 /// DemoteRegToStack - This function takes a virtual register computed by an
113 /// Instruction and replaces it with a slot in the stack frame, allocated via
114 /// alloca. This allows the CFG to be changed around without fear of
115 /// invalidating the SSA information for the value. It returns the pointer to
116 /// the alloca inserted to create a stack slot for X.
118 AllocaInst *DemoteRegToStack(Instruction &X,
119 bool VolatileLoads = false,
120 Instruction *AllocaPoint = 0);
122 /// DemotePHIToStack - This function takes a virtual register computed by a phi
123 /// node and replaces it with a slot in the stack frame, allocated via alloca.
124 /// The phi node is deleted and it returns the pointer to the alloca inserted.
125 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
127 /// OnlyUsedByDbgIntrinsics - Return true if the instruction I is only used
128 /// by DbgIntrinsics. If DbgInUses is specified then the vector is filled
129 /// with DbgInfoIntrinsic that use the instruction I.
130 bool OnlyUsedByDbgInfoIntrinsics(Instruction *I,
131 SmallVectorImpl<DbgInfoIntrinsic *> *DbgInUses = 0);
133 } // End llvm namespace