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
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"
38 class AssumptionCache;
41 class TargetLibraryInfo;
42 class TargetTransformInfo;
47 template<typename T> class SmallVectorImpl;
49 //===----------------------------------------------------------------------===//
50 // Local constant propagation.
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);
63 //===----------------------------------------------------------------------===//
64 // Local dead code elimination.
67 /// isInstructionTriviallyDead - Return true if the result produced by the
68 /// instruction is not used, and the instruction has no side effects.
70 bool isInstructionTriviallyDead(Instruction *I,
71 const TargetLibraryInfo *TLI = nullptr);
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);
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);
88 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
89 /// simplify any instructions in it and recursively delete dead instructions.
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);
96 //===----------------------------------------------------------------------===//
97 // Control Flow Graph Restructuring.
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.
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)
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);
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.
118 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr);
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);
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.
132 bool EliminateDuplicatePHINodes(BasicBlock *BB);
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.
140 bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
141 unsigned BonusInstThreshold, AssumptionCache *AC = nullptr);
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.
147 bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA = nullptr);
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
153 bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold = 1);
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.
161 AllocaInst *DemoteRegToStack(Instruction &X,
162 bool VolatileLoads = false,
163 Instruction *AllocaPoint = nullptr);
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);
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);
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);
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);
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;
204 // Build a mask for high order bits.
205 unsigned IntPtrWidth = IntPtrTy->getScalarType()->getIntegerBitWidth();
206 uint64_t PtrSizeMask = ~0ULL >> (64 - IntPtrWidth);
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;
212 uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
213 if (Constant *OpC = dyn_cast<Constant>(Op)) {
214 if (OpC->isZeroValue())
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();
222 uint64_t OpValue = cast<ConstantInt>(OpC)->getZExtValue();
223 Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
226 Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
227 GEP->getName()+".offs");
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");
238 // Convert to correct type.
239 if (Op->getType() != IntPtrTy)
240 Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
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*/);
247 // Emit an add instruction.
248 Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
253 ///===---------------------------------------------------------------------===//
254 /// Dbg Intrinsic utilities
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);
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);
267 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
268 /// of llvm.dbg.value intrinsics.
269 bool LowerDbgDeclare(Function &F);
271 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic corresponding to
272 /// an alloca, if any.
273 DbgDeclareInst *FindAllocaDbgDeclare(Value *V);
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
280 bool replaceDbgDeclare(Value *Address, Value *NewAddress,
281 Instruction *InsertBefore, DIBuilder &Builder,
282 bool Deref, int Offset);
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);
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);
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
300 /// \param BB Block whose terminator will be replaced. Its terminator must
301 /// have an unwind successor.
302 void removeUnwindEdge(BasicBlock *BB);
304 /// \brief Remove all blocks that can not be reached from the function's entry.
306 /// Returns true if any basic block was removed.
307 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr);
309 /// \brief Combine the metadata of two instructions so that K can replace J
311 /// Metadata not listed as known via KnownIDs is removed
312 void combineMetadata(Instruction *K, const Instruction *J, ArrayRef<unsigned> KnownIDs);
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);
324 /// \brief Return true if the CallSite CS calls a gc leaf function.
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.
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);
334 //===----------------------------------------------------------------------===//
335 // Intrinsic pattern matching
338 /// Try and match a bitreverse or bswap idiom.
340 /// If an idiom is matched, an intrinsic call is inserted before \c I. Any added
341 /// instructions are returned in \c InsertedInsts. They will all have been added
342 /// to a basic block.
344 /// A bitreverse idiom normally requires around 2*BW nodes to be searched (where
345 /// BW is the bitwidth of the integer type). A bswap idiom requires anywhere up
346 /// to BW / 4 nodes to be searched, so is significantly faster.
348 /// This function returns true on a successful match or false otherwise.
349 bool recognizeBitReverseOrBSwapIdiom(
350 Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
351 SmallVectorImpl<Instruction *> &InsertedInsts);
353 } // End llvm namespace