Local.h revision 6c146eefbf75875250af37a0f1ea70fc6b4716ee
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
18namespace llvm {
19
20class User;
21class BasicBlock;
22class BranchInst;
23class Instruction;
24class Value;
25class Pass;
26class PHINode;
27class AllocaInst;
28class ConstantExpr;
29class TargetData;
30
31template<typename T> class SmallVectorImpl;
32
33//===----------------------------------------------------------------------===//
34//  Local analysis.
35//
36
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.
41bool isSafeToLoadUnconditionally(Value *V, Instruction *ScanFrom);
42
43//===----------------------------------------------------------------------===//
44//  Local constant propagation.
45//
46
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.
51///
52bool ConstantFoldTerminator(BasicBlock *BB);
53
54//===----------------------------------------------------------------------===//
55//  Local dead code elimination.
56//
57
58/// isInstructionTriviallyDead - Return true if the result produced by the
59/// instruction is not used, and the instruction has no side effects.
60///
61bool isInstructionTriviallyDead(Instruction *I);
62
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.
67bool RecursivelyDeleteTriviallyDeadInstructions(Value *V);
68
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.
74bool RecursivelyDeleteDeadPHINode(PHINode *PN);
75
76
77/// SimplifyInstructionsInBlock - Scan the specified basic block and try to
78/// simplify any instructions in it and recursively delete dead instructions.
79///
80/// This returns true if it changed the code, note that it can delete
81/// instructions in other blocks as well in this block.
82bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetData *TD = 0);
83
84//===----------------------------------------------------------------------===//
85//  Control Flow Graph Restructuring.
86//
87
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.
91///
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)
95///   y = and x, z
96///
97/// .. and delete the predecessor corresponding to the '1', this will attempt to
98/// recursively fold the 'and' to 0.
99void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
100                                  TargetData *TD = 0);
101
102
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.
107///
108void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, Pass *P = 0);
109
110
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.
116bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
117
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.
122///
123bool EliminateDuplicatePHINodes(BasicBlock *BB);
124
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.
130///
131/// WARNING:  The entry node of a method may not be simplified.
132///
133bool SimplifyCFG(BasicBlock *BB);
134
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
138/// destination.
139bool FoldBranchToCommonDest(BranchInst *BI);
140
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.
146///
147AllocaInst *DemoteRegToStack(Instruction &X,
148                             bool VolatileLoads = false,
149                             Instruction *AllocaPoint = 0);
150
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.
154AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = 0);
155
156} // End llvm namespace
157
158#endif
159