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"
24 #include "llvm/ADT/SmallPtrSet.h"
41 class AssumptionCache;
44 class TargetLibraryInfo;
45 class TargetTransformInfo;
50 template<typename T> class SmallVectorImpl;
52 //===----------------------------------------------------------------------===//
53 // Local constant propagation.
56 /// If a terminator instruction is predicated on a constant value, convert it
57 /// into an unconditional branch to the constant destination.
58 /// This is a nontrivial operation because the successors of this basic block
59 /// must have their PHI nodes updated.
60 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
61 /// conditions and indirectbr addresses this might make dead if
62 /// DeleteDeadConditions is true.
63 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false,
64 const TargetLibraryInfo *TLI = nullptr);
66 //===----------------------------------------------------------------------===//
67 // Local dead code elimination.
70 /// Return true if the result produced by the instruction is not used, and the
71 /// instruction has no side effects.
72 bool isInstructionTriviallyDead(Instruction *I,
73 const TargetLibraryInfo *TLI = nullptr);
75 /// Return true if the result produced by the instruction would have no side
76 /// effects if it was not used. This is equivalent to checking whether
77 /// isInstructionTriviallyDead would be true if the use count was 0.
78 bool wouldInstructionBeTriviallyDead(Instruction *I,
79 const TargetLibraryInfo *TLI = nullptr);
81 /// If the specified value is a trivially dead instruction, delete it.
82 /// If that makes any of its operands trivially dead, delete them too,
83 /// recursively. Return true if any instructions were deleted.
84 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V,
85 const TargetLibraryInfo *TLI = nullptr);
87 /// If the specified value is an effectively dead PHI node, due to being a
88 /// def-use chain of single-use nodes that either forms a cycle or is terminated
89 /// by a trivially dead instruction, delete it. If that makes any of its
90 /// operands trivially dead, delete them too, recursively. Return true if a
92 bool RecursivelyDeleteDeadPHINode(PHINode *PN,
93 const TargetLibraryInfo *TLI = nullptr);
95 /// Scan the specified basic block and try to simplify any instructions in it
96 /// and recursively delete dead instructions.
98 /// This returns true if it changed the code, note that it can delete
99 /// instructions in other blocks as well in this block.
100 bool SimplifyInstructionsInBlock(BasicBlock *BB,
101 const TargetLibraryInfo *TLI = nullptr);
103 //===----------------------------------------------------------------------===//
104 // Control Flow Graph Restructuring.
107 /// Like BasicBlock::removePredecessor, this method is called when we're about
108 /// to delete Pred as a predecessor of BB. If BB contains any PHI nodes, this
109 /// drops the entries in the PHI nodes for Pred.
111 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
112 /// nodes that collapse into identity values. For example, if we have:
113 /// x = phi(1, 0, 0, 0)
116 /// .. and delete the predecessor corresponding to the '1', this will attempt to
117 /// recursively fold the 'and' to 0.
118 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred);
120 /// BB is a block with one predecessor and its predecessor is known to have one
121 /// successor (BB!). Eliminate the edge between them, moving the instructions in
122 /// the predecessor into BB. This deletes the predecessor block.
123 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr);
125 /// BB is known to contain an unconditional branch, and contains no instructions
126 /// other than PHI nodes, potential debug intrinsics and the branch. If
127 /// possible, eliminate BB by rewriting all the predecessors to branch to the
128 /// successor block and return true. If we can't transform, return false.
129 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
131 /// Check for and eliminate duplicate PHI nodes in this block. This doesn't try
132 /// to be clever about PHI nodes which differ only in the order of the incoming
133 /// values, but instcombine orders them so it usually won't matter.
134 bool EliminateDuplicatePHINodes(BasicBlock *BB);
136 /// This function is used to do simplification of a CFG. For
137 /// example, it adjusts branches to branches to eliminate the extra hop, it
138 /// eliminates unreachable basic blocks, and does other "peephole" optimization
139 /// of the CFG. It returns true if a modification was made, possibly deleting
140 /// the basic block that was pointed to. LoopHeaders is an optional input
141 /// parameter, providing the set of loop header that SimplifyCFG should not
143 bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
144 unsigned BonusInstThreshold, AssumptionCache *AC = nullptr,
145 SmallPtrSetImpl<BasicBlock *> *LoopHeaders = nullptr,
146 bool LateSimplifyCFG = false);
148 /// This function is used to flatten a CFG. For example, it uses parallel-and
149 /// and parallel-or mode to collapse if-conditions and merge if-regions with
150 /// identical statements.
151 bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA = nullptr);
153 /// If this basic block is ONLY a setcc and a branch, and if a predecessor
154 /// branches to us and one of our successors, fold the setcc into the
155 /// predecessor and use logical operations to pick the right destination.
156 bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold = 1);
158 /// This function takes a virtual register computed by an Instruction and
159 /// replaces it with a slot in the stack frame, allocated via alloca.
160 /// This allows the CFG to be changed around without fear of invalidating the
161 /// SSA information for the value. It returns the pointer to the alloca inserted
162 /// to create a stack slot for X.
163 AllocaInst *DemoteRegToStack(Instruction &X,
164 bool VolatileLoads = false,
165 Instruction *AllocaPoint = nullptr);
167 /// This function takes a virtual register computed by a phi node and replaces
168 /// it with a slot in the stack frame, allocated via alloca. The phi node is
169 /// deleted and it returns the pointer to the alloca inserted.
170 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = nullptr);
172 /// Try to ensure that the alignment of \p V is at least \p PrefAlign bytes. If
173 /// the owning object can be modified and has an alignment less than \p
174 /// PrefAlign, it will be increased and \p PrefAlign returned. If the alignment
175 /// cannot be increased, the known alignment of the value is returned.
177 /// It is not always possible to modify the alignment of the underlying object,
178 /// so if alignment is important, a more reliable approach is to simply align
179 /// all global variables and allocation instructions to their preferred
180 /// alignment from the beginning.
181 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
182 const DataLayout &DL,
183 const Instruction *CxtI = nullptr,
184 AssumptionCache *AC = nullptr,
185 const DominatorTree *DT = nullptr);
187 /// Try to infer an alignment for the specified pointer.
188 static inline unsigned getKnownAlignment(Value *V, const DataLayout &DL,
189 const Instruction *CxtI = nullptr,
190 AssumptionCache *AC = nullptr,
191 const DominatorTree *DT = nullptr) {
192 return getOrEnforceKnownAlignment(V, 0, DL, CxtI, AC, DT);
195 /// Given a getelementptr instruction/constantexpr, emit the code necessary to
196 /// compute the offset from the base pointer (without adding in the base
197 /// pointer). Return the result as a signed integer of intptr size.
198 /// When NoAssumptions is true, no assumptions about index computation not
199 /// overflowing is made.
200 template <typename IRBuilderTy>
201 Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP,
202 bool NoAssumptions = false) {
203 GEPOperator *GEPOp = cast<GEPOperator>(GEP);
204 Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
205 Value *Result = Constant::getNullValue(IntPtrTy);
207 // If the GEP is inbounds, we know that none of the addressing operations will
208 // overflow in an unsigned sense.
209 bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
211 // Build a mask for high order bits.
212 unsigned IntPtrWidth = IntPtrTy->getScalarType()->getIntegerBitWidth();
213 uint64_t PtrSizeMask = ~0ULL >> (64 - IntPtrWidth);
215 gep_type_iterator GTI = gep_type_begin(GEP);
216 for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
219 uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
220 if (Constant *OpC = dyn_cast<Constant>(Op)) {
221 if (OpC->isZeroValue())
224 // Handle a struct index, which adds its field offset to the pointer.
225 if (StructType *STy = GTI.getStructTypeOrNull()) {
226 if (OpC->getType()->isVectorTy())
227 OpC = OpC->getSplatValue();
229 uint64_t OpValue = cast<ConstantInt>(OpC)->getZExtValue();
230 Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
233 Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
234 GEP->getName()+".offs");
238 Constant *Scale = ConstantInt::get(IntPtrTy, Size);
239 Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
240 Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/);
241 // Emit an add instruction.
242 Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
245 // Convert to correct type.
246 if (Op->getType() != IntPtrTy)
247 Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
249 // We'll let instcombine(mul) convert this to a shl if possible.
250 Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size),
251 GEP->getName()+".idx", isInBounds /*NUW*/);
254 // Emit an add instruction.
255 Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
260 ///===---------------------------------------------------------------------===//
261 /// Dbg Intrinsic utilities
264 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
265 /// that has an associated llvm.dbg.decl intrinsic.
266 void ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
267 StoreInst *SI, DIBuilder &Builder);
269 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
270 /// that has an associated llvm.dbg.decl intrinsic.
271 void ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
272 LoadInst *LI, DIBuilder &Builder);
274 /// Inserts a llvm.dbg.value intrinsic after a phi of an alloca'd value
275 /// that has an associated llvm.dbg.decl intrinsic.
276 void ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
277 PHINode *LI, DIBuilder &Builder);
279 /// Lowers llvm.dbg.declare intrinsics into appropriate set of
280 /// llvm.dbg.value intrinsics.
281 bool LowerDbgDeclare(Function &F);
283 /// Finds the llvm.dbg.declare intrinsic corresponding to an alloca, if any.
284 DbgDeclareInst *FindAllocaDbgDeclare(Value *V);
286 /// Finds the llvm.dbg.value intrinsics describing a value.
287 void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V);
289 /// Constants for \p replaceDbgDeclare and friends.
290 enum { NoDeref = false, WithDeref = true };
292 /// Replaces llvm.dbg.declare instruction when the address it describes
293 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
294 /// prepended to the expression. If Offset is non-zero, a constant displacement
295 /// is added to the expression (after the optional Deref). Offset can be
297 bool replaceDbgDeclare(Value *Address, Value *NewAddress,
298 Instruction *InsertBefore, DIBuilder &Builder,
299 bool Deref, int Offset);
301 /// Replaces llvm.dbg.declare instruction when the alloca it describes
302 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
303 /// prepended to the expression. If Offset is non-zero, a constant displacement
304 /// is added to the expression (after the optional Deref). Offset can be
305 /// negative. New llvm.dbg.declare is inserted immediately before AI.
306 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
307 DIBuilder &Builder, bool Deref, int Offset = 0);
309 /// Replaces multiple llvm.dbg.value instructions when the alloca it describes
310 /// is replaced with a new value. If Offset is non-zero, a constant displacement
311 /// is added to the expression (after the mandatory Deref). Offset can be
312 /// negative. New llvm.dbg.value instructions are inserted at the locations of
313 /// the instructions they replace.
314 void replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
315 DIBuilder &Builder, int Offset = 0);
317 /// Assuming the instruction \p I is going to be deleted, attempt to salvage any
318 /// dbg.value intrinsics referring to \p I by rewriting its effect into a
320 void salvageDebugInfo(Instruction &I);
322 /// Remove all instructions from a basic block other than it's terminator
323 /// and any present EH pad instructions.
324 unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB);
326 /// Insert an unreachable instruction before the specified
327 /// instruction, making it and the rest of the code in the block dead.
328 unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap,
329 bool PreserveLCSSA = false);
331 /// Convert the CallInst to InvokeInst with the specified unwind edge basic
332 /// block. This also splits the basic block where CI is located, because
333 /// InvokeInst is a terminator instruction. Returns the newly split basic
335 BasicBlock *changeToInvokeAndSplitBasicBlock(CallInst *CI,
336 BasicBlock *UnwindEdge);
338 /// Replace 'BB's terminator with one that does not have an unwind successor
339 /// block. Rewrites `invoke` to `call`, etc. Updates any PHIs in unwind
342 /// \param BB Block whose terminator will be replaced. Its terminator must
343 /// have an unwind successor.
344 void removeUnwindEdge(BasicBlock *BB);
346 /// Remove all blocks that can not be reached from the function's entry.
348 /// Returns true if any basic block was removed.
349 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr);
351 /// Combine the metadata of two instructions so that K can replace J
353 /// Metadata not listed as known via KnownIDs is removed
354 void combineMetadata(Instruction *K, const Instruction *J, ArrayRef<unsigned> KnownIDs);
356 /// Combine the metadata of two instructions so that K can replace J. This
357 /// specifically handles the case of CSE-like transformations.
359 /// Unknown metadata is removed.
360 void combineMetadataForCSE(Instruction *K, const Instruction *J);
362 /// Replace each use of 'From' with 'To' if that use is dominated by
363 /// the given edge. Returns the number of replacements made.
364 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
365 const BasicBlockEdge &Edge);
366 /// Replace each use of 'From' with 'To' if that use is dominated by
367 /// the end of the given BasicBlock. Returns the number of replacements made.
368 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
369 const BasicBlock *BB);
372 /// Return true if the CallSite CS calls a gc leaf function.
374 /// A leaf function is a function that does not safepoint the thread during its
375 /// execution. During a call or invoke to such a function, the callers stack
376 /// does not have to be made parseable.
378 /// Most passes can and should ignore this information, and it is only used
379 /// during lowering by the GC infrastructure.
380 bool callsGCLeafFunction(ImmutableCallSite CS);
382 //===----------------------------------------------------------------------===//
383 // Intrinsic pattern matching
386 /// Try and match a bswap or bitreverse idiom.
388 /// If an idiom is matched, an intrinsic call is inserted before \c I. Any added
389 /// instructions are returned in \c InsertedInsts. They will all have been added
390 /// to a basic block.
392 /// A bitreverse idiom normally requires around 2*BW nodes to be searched (where
393 /// BW is the bitwidth of the integer type). A bswap idiom requires anywhere up
394 /// to BW / 4 nodes to be searched, so is significantly faster.
396 /// This function returns true on a successful match or false otherwise.
397 bool recognizeBSwapOrBitReverseIdiom(
398 Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
399 SmallVectorImpl<Instruction *> &InsertedInsts);
401 //===----------------------------------------------------------------------===//
402 // Sanitizer utilities
405 /// Given a CallInst, check if it calls a string function known to CodeGen,
406 /// and mark it with NoBuiltin if so. To be used by sanitizers that intend
407 /// to intercept string functions and want to avoid converting them to target
408 /// specific instructions.
409 void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI,
410 const TargetLibraryInfo *TLI);
412 } // End llvm namespace