911c6f14da0b98425d2d9972a3dd4bed831e1df2
[oota-llvm.git] / include / llvm / Transforms / Utils / Local.h
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
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/GetElementPtrTypeIterator.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Operator.h"
24
25 namespace llvm {
26
27 class User;
28 class BasicBlock;
29 class Function;
30 class BranchInst;
31 class Instruction;
32 class DbgDeclareInst;
33 class StoreInst;
34 class LoadInst;
35 class Value;
36 class PHINode;
37 class AllocaInst;
38 class AssumptionCache;
39 class ConstantExpr;
40 class DataLayout;
41 class TargetLibraryInfo;
42 class TargetTransformInfo;
43 class DIBuilder;
44 class DominatorTree;
45 class LazyValueInfo;
46
47 template<typename T> class SmallVectorImpl;
48
49 //===----------------------------------------------------------------------===//
50 //  Local constant propagation.
51 //
52
53 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
54 /// constant value, convert it into an unconditional branch to the constant
55 /// destination.  This is a nontrivial operation because the successors of this
56 /// basic block must have their PHI nodes updated.
57 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
58 /// conditions and indirectbr addresses this might make dead if
59 /// DeleteDeadConditions is true.
60 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false,
61                             const TargetLibraryInfo *TLI = nullptr);
62
63 //===----------------------------------------------------------------------===//
64 //  Local dead code elimination.
65 //
66
67 /// isInstructionTriviallyDead - Return true if the result produced by the
68 /// instruction is not used, and the instruction has no side effects.
69 ///
70 bool isInstructionTriviallyDead(Instruction *I,
71                                 const TargetLibraryInfo *TLI = nullptr);
72
73 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
74 /// trivially dead instruction, delete it.  If that makes any of its operands
75 /// trivially dead, delete them too, recursively.  Return true if any
76 /// instructions were deleted.
77 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V,
78                                         const TargetLibraryInfo *TLI = nullptr);
79
80 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
81 /// dead PHI node, due to being a def-use chain of single-use nodes that
82 /// either forms a cycle or is terminated by a trivially dead instruction,
83 /// delete it.  If that makes any of its operands trivially dead, delete them
84 /// too, recursively.  Return true if a change was made.
85 bool RecursivelyDeleteDeadPHINode(PHINode *PN,
86                                   const TargetLibraryInfo *TLI = nullptr);
87
88 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
89 /// simplify any instructions in it and recursively delete dead instructions.
90 ///
91 /// This returns true if it changed the code, note that it can delete
92 /// instructions in other blocks as well in this block.
93 bool SimplifyInstructionsInBlock(BasicBlock *BB,
94                                  const TargetLibraryInfo *TLI = nullptr);
95
96 //===----------------------------------------------------------------------===//
97 //  Control Flow Graph Restructuring.
98 //
99
100 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
101 /// method is called when we're about to delete Pred as a predecessor of BB.  If
102 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
103 ///
104 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
105 /// nodes that collapse into identity values.  For example, if we have:
106 ///   x = phi(1, 0, 0, 0)
107 ///   y = and x, z
108 ///
109 /// .. and delete the predecessor corresponding to the '1', this will attempt to
110 /// recursively fold the 'and' to 0.
111 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred);
112
113 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
114 /// predecessor is known to have one successor (BB!).  Eliminate the edge
115 /// between them, moving the instructions in the predecessor into BB.  This
116 /// deletes the predecessor block.
117 ///
118 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr);
119
120 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
121 /// unconditional branch, and contains no instructions other than PHI nodes,
122 /// potential debug intrinsics and the branch.  If possible, eliminate BB by
123 /// rewriting all the predecessors to branch to the successor block and return
124 /// true.  If we can't transform, return false.
125 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
126
127 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
128 /// nodes in this block. This doesn't try to be clever about PHI nodes
129 /// which differ only in the order of the incoming values, but instcombine
130 /// orders them so it usually won't matter.
131 ///
132 bool EliminateDuplicatePHINodes(BasicBlock *BB);
133
134 /// SimplifyCFG - This function is used to do simplification of a CFG.  For
135 /// example, it adjusts branches to branches to eliminate the extra hop, it
136 /// eliminates unreachable basic blocks, and does other "peephole" optimization
137 /// of the CFG.  It returns true if a modification was made, possibly deleting
138 /// the basic block that was pointed to.
139 ///
140 bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
141                  unsigned BonusInstThreshold, AssumptionCache *AC = nullptr);
142
143 /// FlatternCFG - This function is used to flatten a CFG.  For
144 /// example, it uses parallel-and and parallel-or mode to collapse
145 //  if-conditions and merge if-regions with identical statements.
146 ///
147 bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA = nullptr);
148
149 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
150 /// and if a predecessor branches to us and one of our successors, fold the
151 /// setcc into the predecessor and use logical operations to pick the right
152 /// destination.
153 bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold = 1);
154
155 /// DemoteRegToStack - This function takes a virtual register computed by an
156 /// Instruction and replaces it with a slot in the stack frame, allocated via
157 /// alloca.  This allows the CFG to be changed around without fear of
158 /// invalidating the SSA information for the value.  It returns the pointer to
159 /// the alloca inserted to create a stack slot for X.
160 ///
161 AllocaInst *DemoteRegToStack(Instruction &X,
162                              bool VolatileLoads = false,
163                              Instruction *AllocaPoint = nullptr);
164
165 /// DemotePHIToStack - This function takes a virtual register computed by a phi
166 /// node and replaces it with a slot in the stack frame, allocated via alloca.
167 /// The phi node is deleted and it returns the pointer to the alloca inserted.
168 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = nullptr);
169
170 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that
171 /// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
172 /// and it is more than the alignment of the ultimate object, see if we can
173 /// increase the alignment of the ultimate object, making this check succeed.
174 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
175                                     const DataLayout &DL,
176                                     const Instruction *CxtI = nullptr,
177                                     AssumptionCache *AC = nullptr,
178                                     const DominatorTree *DT = nullptr);
179
180 /// getKnownAlignment - Try to infer an alignment for the specified pointer.
181 static inline unsigned getKnownAlignment(Value *V, const DataLayout &DL,
182                                          const Instruction *CxtI = nullptr,
183                                          AssumptionCache *AC = nullptr,
184                                          const DominatorTree *DT = nullptr) {
185   return getOrEnforceKnownAlignment(V, 0, DL, CxtI, AC, DT);
186 }
187
188 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
189 /// code necessary to compute the offset from the base pointer (without adding
190 /// in the base pointer).  Return the result as a signed integer of intptr size.
191 /// When NoAssumptions is true, no assumptions about index computation not
192 /// overflowing is made.
193 template <typename IRBuilderTy>
194 Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP,
195                      bool NoAssumptions = false) {
196   GEPOperator *GEPOp = cast<GEPOperator>(GEP);
197   Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
198   Value *Result = Constant::getNullValue(IntPtrTy);
199
200   // If the GEP is inbounds, we know that none of the addressing operations will
201   // overflow in an unsigned sense.
202   bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
203
204   // Build a mask for high order bits.
205   unsigned IntPtrWidth = IntPtrTy->getScalarType()->getIntegerBitWidth();
206   uint64_t PtrSizeMask = ~0ULL >> (64 - IntPtrWidth);
207
208   gep_type_iterator GTI = gep_type_begin(GEP);
209   for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
210        ++i, ++GTI) {
211     Value *Op = *i;
212     uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
213     if (Constant *OpC = dyn_cast<Constant>(Op)) {
214       if (OpC->isZeroValue())
215         continue;
216
217       // Handle a struct index, which adds its field offset to the pointer.
218       if (StructType *STy = dyn_cast<StructType>(*GTI)) {
219         if (OpC->getType()->isVectorTy())
220           OpC = OpC->getSplatValue();
221
222         uint64_t OpValue = cast<ConstantInt>(OpC)->getZExtValue();
223         Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
224
225         if (Size)
226           Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
227                                       GEP->getName()+".offs");
228         continue;
229       }
230
231       Constant *Scale = ConstantInt::get(IntPtrTy, Size);
232       Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
233       Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/);
234       // Emit an add instruction.
235       Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
236       continue;
237     }
238     // Convert to correct type.
239     if (Op->getType() != IntPtrTy)
240       Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
241     if (Size != 1) {
242       // We'll let instcombine(mul) convert this to a shl if possible.
243       Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size),
244                               GEP->getName()+".idx", isInBounds /*NUW*/);
245     }
246
247     // Emit an add instruction.
248     Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
249   }
250   return Result;
251 }
252
253 ///===---------------------------------------------------------------------===//
254 ///  Dbg Intrinsic utilities
255 ///
256
257 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
258 /// that has an associated llvm.dbg.decl intrinsic.
259 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
260                                      StoreInst *SI, DIBuilder &Builder);
261
262 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
263 /// that has an associated llvm.dbg.decl intrinsic.
264 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
265                                      LoadInst *LI, DIBuilder &Builder);
266
267 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
268 /// of llvm.dbg.value intrinsics.
269 bool LowerDbgDeclare(Function &F);
270
271 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic corresponding to
272 /// an alloca, if any.
273 DbgDeclareInst *FindAllocaDbgDeclare(Value *V);
274
275 /// \brief Replaces llvm.dbg.declare instruction when the address it describes
276 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
277 /// prepended to the expression. If Offset is non-zero, a constant displacement
278 /// is added to the expression (after the optional Deref). Offset can be
279 /// negative.
280 bool replaceDbgDeclare(Value *Address, Value *NewAddress,
281                        Instruction *InsertBefore, DIBuilder &Builder,
282                        bool Deref, int Offset);
283
284 /// \brief Replaces llvm.dbg.declare instruction when the alloca it describes
285 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
286 /// prepended to the expression. If Offset is non-zero, a constant displacement
287 /// is added to the expression (after the optional Deref). Offset can be
288 /// negative. New llvm.dbg.declare is inserted immediately before AI.
289 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
290                                 DIBuilder &Builder, bool Deref, int Offset = 0);
291
292 /// \brief Insert an unreachable instruction before the specified
293 /// instruction, making it and the rest of the code in the block dead.
294 void changeToUnreachable(Instruction *I, bool UseLLVMTrap);
295
296 /// Replace 'BB's terminator with one that does not have an unwind successor
297 /// block.  Rewrites `invoke` to `call`, etc.  Updates any PHIs in unwind
298 /// successor.
299 ///
300 /// \param BB  Block whose terminator will be replaced.  Its terminator must
301 ///            have an unwind successor.
302 void removeUnwindEdge(BasicBlock *BB);
303
304 /// \brief Remove all blocks that can not be reached from the function's entry.
305 ///
306 /// Returns true if any basic block was removed.
307 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr);
308
309 /// \brief Combine the metadata of two instructions so that K can replace J
310 ///
311 /// Metadata not listed as known via KnownIDs is removed
312 void combineMetadata(Instruction *K, const Instruction *J, ArrayRef<unsigned> KnownIDs);
313
314 /// \brief Replace each use of 'From' with 'To' if that use is dominated by 
315 /// the given edge.  Returns the number of replacements made.
316 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
317                                   const BasicBlockEdge &Edge);
318 /// \brief Replace each use of 'From' with 'To' if that use is dominated by
319 /// the given BasicBlock. Returns the number of replacements made.
320 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
321                                   const BasicBlock *BB);
322
323
324 /// \brief Return true if the CallSite CS calls a gc leaf function.
325 ///
326 /// A leaf function is a function that does not safepoint the thread during its
327 /// execution.  During a call or invoke to such a function, the callers stack
328 /// does not have to be made parseable.
329 ///
330 /// Most passes can and should ignore this information, and it is only used
331 /// during lowering by the GC infrastructure.
332 bool callsGCLeafFunction(ImmutableCallSite CS);
333
334 } // End llvm namespace
335
336 #endif