LLVM  mainline
Local.h
Go to the documentation of this file.
00001 //===-- Local.h - Functions to perform local transformations ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This family of functions perform various local transformations to the
00011 // program.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H
00016 #define LLVM_TRANSFORMS_UTILS_LOCAL_H
00017 
00018 #include "llvm/Analysis/AliasAnalysis.h"
00019 #include "llvm/IR/DataLayout.h"
00020 #include "llvm/IR/Dominators.h"
00021 #include "llvm/IR/GetElementPtrTypeIterator.h"
00022 #include "llvm/IR/IRBuilder.h"
00023 #include "llvm/IR/Operator.h"
00024 
00025 namespace llvm {
00026 
00027 class User;
00028 class BasicBlock;
00029 class Function;
00030 class BranchInst;
00031 class Instruction;
00032 class DbgDeclareInst;
00033 class StoreInst;
00034 class LoadInst;
00035 class Value;
00036 class PHINode;
00037 class AllocaInst;
00038 class AssumptionCache;
00039 class ConstantExpr;
00040 class DataLayout;
00041 class TargetLibraryInfo;
00042 class TargetTransformInfo;
00043 class DIBuilder;
00044 class DominatorTree;
00045 class LazyValueInfo;
00046 
00047 template<typename T> class SmallVectorImpl;
00048 
00049 //===----------------------------------------------------------------------===//
00050 //  Local constant propagation.
00051 //
00052 
00053 /// ConstantFoldTerminator - If a terminator instruction is predicated on a
00054 /// constant value, convert it into an unconditional branch to the constant
00055 /// destination.  This is a nontrivial operation because the successors of this
00056 /// basic block must have their PHI nodes updated.
00057 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
00058 /// conditions and indirectbr addresses this might make dead if
00059 /// DeleteDeadConditions is true.
00060 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false,
00061                             const TargetLibraryInfo *TLI = nullptr);
00062 
00063 //===----------------------------------------------------------------------===//
00064 //  Local dead code elimination.
00065 //
00066 
00067 /// isInstructionTriviallyDead - Return true if the result produced by the
00068 /// instruction is not used, and the instruction has no side effects.
00069 ///
00070 bool isInstructionTriviallyDead(Instruction *I,
00071                                 const TargetLibraryInfo *TLI = nullptr);
00072 
00073 /// RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a
00074 /// trivially dead instruction, delete it.  If that makes any of its operands
00075 /// trivially dead, delete them too, recursively.  Return true if any
00076 /// instructions were deleted.
00077 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V,
00078                                         const TargetLibraryInfo *TLI = nullptr);
00079 
00080 /// RecursivelyDeleteDeadPHINode - If the specified value is an effectively
00081 /// dead PHI node, due to being a def-use chain of single-use nodes that
00082 /// either forms a cycle or is terminated by a trivially dead instruction,
00083 /// delete it.  If that makes any of its operands trivially dead, delete them
00084 /// too, recursively.  Return true if a change was made.
00085 bool RecursivelyDeleteDeadPHINode(PHINode *PN,
00086                                   const TargetLibraryInfo *TLI = nullptr);
00087 
00088 /// SimplifyInstructionsInBlock - Scan the specified basic block and try to
00089 /// simplify any instructions in it and recursively delete dead instructions.
00090 ///
00091 /// This returns true if it changed the code, note that it can delete
00092 /// instructions in other blocks as well in this block.
00093 bool SimplifyInstructionsInBlock(BasicBlock *BB,
00094                                  const TargetLibraryInfo *TLI = nullptr);
00095 
00096 //===----------------------------------------------------------------------===//
00097 //  Control Flow Graph Restructuring.
00098 //
00099 
00100 /// RemovePredecessorAndSimplify - Like BasicBlock::removePredecessor, this
00101 /// method is called when we're about to delete Pred as a predecessor of BB.  If
00102 /// BB contains any PHI nodes, this drops the entries in the PHI nodes for Pred.
00103 ///
00104 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
00105 /// nodes that collapse into identity values.  For example, if we have:
00106 ///   x = phi(1, 0, 0, 0)
00107 ///   y = and x, z
00108 ///
00109 /// .. and delete the predecessor corresponding to the '1', this will attempt to
00110 /// recursively fold the 'and' to 0.
00111 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred);
00112 
00113 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its
00114 /// predecessor is known to have one successor (BB!).  Eliminate the edge
00115 /// between them, moving the instructions in the predecessor into BB.  This
00116 /// deletes the predecessor block.
00117 ///
00118 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr);
00119 
00120 /// TryToSimplifyUncondBranchFromEmptyBlock - BB is known to contain an
00121 /// unconditional branch, and contains no instructions other than PHI nodes,
00122 /// potential debug intrinsics and the branch.  If possible, eliminate BB by
00123 /// rewriting all the predecessors to branch to the successor block and return
00124 /// true.  If we can't transform, return false.
00125 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
00126 
00127 /// EliminateDuplicatePHINodes - Check for and eliminate duplicate PHI
00128 /// nodes in this block. This doesn't try to be clever about PHI nodes
00129 /// which differ only in the order of the incoming values, but instcombine
00130 /// orders them so it usually won't matter.
00131 ///
00132 bool EliminateDuplicatePHINodes(BasicBlock *BB);
00133 
00134 /// SimplifyCFG - This function is used to do simplification of a CFG.  For
00135 /// example, it adjusts branches to branches to eliminate the extra hop, it
00136 /// eliminates unreachable basic blocks, and does other "peephole" optimization
00137 /// of the CFG.  It returns true if a modification was made, possibly deleting
00138 /// the basic block that was pointed to.
00139 ///
00140 bool SimplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
00141                  unsigned BonusInstThreshold, AssumptionCache *AC = nullptr);
00142 
00143 /// FlatternCFG - This function is used to flatten a CFG.  For
00144 /// example, it uses parallel-and and parallel-or mode to collapse
00145 //  if-conditions and merge if-regions with identical statements.
00146 ///
00147 bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA = nullptr);
00148 
00149 /// FoldBranchToCommonDest - If this basic block is ONLY a setcc and a branch,
00150 /// and if a predecessor branches to us and one of our successors, fold the
00151 /// setcc into the predecessor and use logical operations to pick the right
00152 /// destination.
00153 bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold = 1);
00154 
00155 /// DemoteRegToStack - This function takes a virtual register computed by an
00156 /// Instruction and replaces it with a slot in the stack frame, allocated via
00157 /// alloca.  This allows the CFG to be changed around without fear of
00158 /// invalidating the SSA information for the value.  It returns the pointer to
00159 /// the alloca inserted to create a stack slot for X.
00160 ///
00161 AllocaInst *DemoteRegToStack(Instruction &X,
00162                              bool VolatileLoads = false,
00163                              Instruction *AllocaPoint = nullptr);
00164 
00165 /// DemotePHIToStack - This function takes a virtual register computed by a phi
00166 /// node and replaces it with a slot in the stack frame, allocated via alloca.
00167 /// The phi node is deleted and it returns the pointer to the alloca inserted.
00168 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = nullptr);
00169 
00170 /// getOrEnforceKnownAlignment - If the specified pointer has an alignment that
00171 /// we can determine, return it, otherwise return 0.  If PrefAlign is specified,
00172 /// and it is more than the alignment of the ultimate object, see if we can
00173 /// increase the alignment of the ultimate object, making this check succeed.
00174 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
00175                                     const DataLayout &DL,
00176                                     const Instruction *CxtI = nullptr,
00177                                     AssumptionCache *AC = nullptr,
00178                                     const DominatorTree *DT = nullptr);
00179 
00180 /// getKnownAlignment - Try to infer an alignment for the specified pointer.
00181 static inline unsigned getKnownAlignment(Value *V, const DataLayout &DL,
00182                                          const Instruction *CxtI = nullptr,
00183                                          AssumptionCache *AC = nullptr,
00184                                          const DominatorTree *DT = nullptr) {
00185   return getOrEnforceKnownAlignment(V, 0, DL, CxtI, AC, DT);
00186 }
00187 
00188 /// EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the
00189 /// code necessary to compute the offset from the base pointer (without adding
00190 /// in the base pointer).  Return the result as a signed integer of intptr size.
00191 /// When NoAssumptions is true, no assumptions about index computation not
00192 /// overflowing is made.
00193 template <typename IRBuilderTy>
00194 Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP,
00195                      bool NoAssumptions = false) {
00196   GEPOperator *GEPOp = cast<GEPOperator>(GEP);
00197   Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
00198   Value *Result = Constant::getNullValue(IntPtrTy);
00199 
00200   // If the GEP is inbounds, we know that none of the addressing operations will
00201   // overflow in an unsigned sense.
00202   bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
00203 
00204   // Build a mask for high order bits.
00205   unsigned IntPtrWidth = IntPtrTy->getScalarType()->getIntegerBitWidth();
00206   uint64_t PtrSizeMask = ~0ULL >> (64 - IntPtrWidth);
00207 
00208   gep_type_iterator GTI = gep_type_begin(GEP);
00209   for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
00210        ++i, ++GTI) {
00211     Value *Op = *i;
00212     uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
00213     if (Constant *OpC = dyn_cast<Constant>(Op)) {
00214       if (OpC->isZeroValue())
00215         continue;
00216 
00217       // Handle a struct index, which adds its field offset to the pointer.
00218       if (StructType *STy = dyn_cast<StructType>(*GTI)) {
00219         if (OpC->getType()->isVectorTy())
00220           OpC = OpC->getSplatValue();
00221 
00222         uint64_t OpValue = cast<ConstantInt>(OpC)->getZExtValue();
00223         Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
00224 
00225         if (Size)
00226           Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
00227                                       GEP->getName()+".offs");
00228         continue;
00229       }
00230 
00231       Constant *Scale = ConstantInt::get(IntPtrTy, Size);
00232       Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
00233       Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/);
00234       // Emit an add instruction.
00235       Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
00236       continue;
00237     }
00238     // Convert to correct type.
00239     if (Op->getType() != IntPtrTy)
00240       Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
00241     if (Size != 1) {
00242       // We'll let instcombine(mul) convert this to a shl if possible.
00243       Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size),
00244                               GEP->getName()+".idx", isInBounds /*NUW*/);
00245     }
00246 
00247     // Emit an add instruction.
00248     Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
00249   }
00250   return Result;
00251 }
00252 
00253 ///===---------------------------------------------------------------------===//
00254 ///  Dbg Intrinsic utilities
00255 ///
00256 
00257 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
00258 /// that has an associated llvm.dbg.decl intrinsic.
00259 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
00260                                      StoreInst *SI, DIBuilder &Builder);
00261 
00262 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
00263 /// that has an associated llvm.dbg.decl intrinsic.
00264 bool ConvertDebugDeclareToDebugValue(DbgDeclareInst *DDI,
00265                                      LoadInst *LI, DIBuilder &Builder);
00266 
00267 /// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
00268 /// of llvm.dbg.value intrinsics.
00269 bool LowerDbgDeclare(Function &F);
00270 
00271 /// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic corresponding to
00272 /// an alloca, if any.
00273 DbgDeclareInst *FindAllocaDbgDeclare(Value *V);
00274 
00275 /// \brief Replaces llvm.dbg.declare instruction when the address it describes
00276 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
00277 /// prepended to the expression. If Offset is non-zero, a constant displacement
00278 /// is added to the expression (after the optional Deref). Offset can be
00279 /// negative.
00280 bool replaceDbgDeclare(Value *Address, Value *NewAddress,
00281                        Instruction *InsertBefore, DIBuilder &Builder,
00282                        bool Deref, int Offset);
00283 
00284 /// \brief Replaces llvm.dbg.declare instruction when the alloca it describes
00285 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
00286 /// prepended to the expression. If Offset is non-zero, a constant displacement
00287 /// is added to the expression (after the optional Deref). Offset can be
00288 /// negative. New llvm.dbg.declare is inserted immediately before AI.
00289 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
00290                                 DIBuilder &Builder, bool Deref, int Offset = 0);
00291 
00292 /// \brief Remove all instructions from a basic block other than it's terminator
00293 /// and any present EH pad instructions.
00294 unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB);
00295 
00296 /// \brief Insert an unreachable instruction before the specified
00297 /// instruction, making it and the rest of the code in the block dead.
00298 unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap);
00299 
00300 /// Replace 'BB's terminator with one that does not have an unwind successor
00301 /// block.  Rewrites `invoke` to `call`, etc.  Updates any PHIs in unwind
00302 /// successor.
00303 ///
00304 /// \param BB  Block whose terminator will be replaced.  Its terminator must
00305 ///            have an unwind successor.
00306 void removeUnwindEdge(BasicBlock *BB);
00307 
00308 /// \brief Remove all blocks that can not be reached from the function's entry.
00309 ///
00310 /// Returns true if any basic block was removed.
00311 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr);
00312 
00313 /// \brief Combine the metadata of two instructions so that K can replace J
00314 ///
00315 /// Metadata not listed as known via KnownIDs is removed
00316 void combineMetadata(Instruction *K, const Instruction *J, ArrayRef<unsigned> KnownIDs);
00317 
00318 /// \brief Replace each use of 'From' with 'To' if that use is dominated by 
00319 /// the given edge.  Returns the number of replacements made.
00320 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
00321                                   const BasicBlockEdge &Edge);
00322 /// \brief Replace each use of 'From' with 'To' if that use is dominated by
00323 /// the end of the given BasicBlock. Returns the number of replacements made.
00324 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
00325                                   const BasicBlock *BB);
00326 
00327 
00328 /// \brief Return true if the CallSite CS calls a gc leaf function.
00329 ///
00330 /// A leaf function is a function that does not safepoint the thread during its
00331 /// execution.  During a call or invoke to such a function, the callers stack
00332 /// does not have to be made parseable.
00333 ///
00334 /// Most passes can and should ignore this information, and it is only used
00335 /// during lowering by the GC infrastructure.
00336 bool callsGCLeafFunction(ImmutableCallSite CS);
00337 
00338 //===----------------------------------------------------------------------===//
00339 //  Intrinsic pattern matching
00340 //
00341 
00342 /// Try and match a bitreverse or bswap idiom.
00343 ///
00344 /// If an idiom is matched, an intrinsic call is inserted before \c I. Any added
00345 /// instructions are returned in \c InsertedInsts. They will all have been added
00346 /// to a basic block.
00347 ///
00348 /// A bitreverse idiom normally requires around 2*BW nodes to be searched (where
00349 /// BW is the bitwidth of the integer type). A bswap idiom requires anywhere up
00350 /// to BW / 4 nodes to be searched, so is significantly faster.
00351 ///
00352 /// This function returns true on a successful match or false otherwise.
00353 bool recognizeBitReverseOrBSwapIdiom(
00354     Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
00355     SmallVectorImpl<Instruction *> &InsertedInsts);
00356 
00357 } // End llvm namespace
00358 
00359 #endif