Home | History | Annotate | Download | only in Utils
      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/ADT/ArrayRef.h"
     19 #include "llvm/ADT/SmallPtrSet.h"
     20 #include "llvm/ADT/SmallVector.h"
     21 #include "llvm/ADT/TinyPtrVector.h"
     22 #include "llvm/Analysis/AliasAnalysis.h"
     23 #include "llvm/IR/CallSite.h"
     24 #include "llvm/IR/Constant.h"
     25 #include "llvm/IR/Constants.h"
     26 #include "llvm/IR/DataLayout.h"
     27 #include "llvm/IR/Dominators.h"
     28 #include "llvm/IR/GetElementPtrTypeIterator.h"
     29 #include "llvm/IR/Operator.h"
     30 #include "llvm/IR/Type.h"
     31 #include "llvm/IR/User.h"
     32 #include "llvm/IR/Value.h"
     33 #include "llvm/Support/Casting.h"
     34 #include <cstdint>
     35 #include <limits>
     36 
     37 namespace llvm {
     38 
     39 class AllocaInst;
     40 class AssumptionCache;
     41 class BasicBlock;
     42 class BranchInst;
     43 class CallInst;
     44 class DbgInfoIntrinsic;
     45 class DbgValueInst;
     46 class DIBuilder;
     47 class Function;
     48 class Instruction;
     49 class LazyValueInfo;
     50 class LoadInst;
     51 class MDNode;
     52 class PHINode;
     53 class StoreInst;
     54 class TargetLibraryInfo;
     55 class TargetTransformInfo;
     56 
     57 /// A set of parameters used to control the transforms in the SimplifyCFG pass.
     58 /// Options may change depending on the position in the optimization pipeline.
     59 /// For example, canonical form that includes switches and branches may later be
     60 /// replaced by lookup tables and selects.
     61 struct SimplifyCFGOptions {
     62   int BonusInstThreshold;
     63   bool ConvertSwitchToLookupTable;
     64   bool NeedCanonicalLoop;
     65   AssumptionCache *AC;
     66 
     67   SimplifyCFGOptions(int BonusThreshold = 1, bool SwitchToLookup = false,
     68                      bool CanonicalLoops = true,
     69                      AssumptionCache *AssumpCache = nullptr)
     70       : BonusInstThreshold(BonusThreshold),
     71         ConvertSwitchToLookupTable(SwitchToLookup),
     72         NeedCanonicalLoop(CanonicalLoops), AC(AssumpCache) {}
     73 };
     74 
     75 //===----------------------------------------------------------------------===//
     76 //  Local constant propagation.
     77 //
     78 
     79 /// If a terminator instruction is predicated on a constant value, convert it
     80 /// into an unconditional branch to the constant destination.
     81 /// This is a nontrivial operation because the successors of this basic block
     82 /// must have their PHI nodes updated.
     83 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch
     84 /// conditions and indirectbr addresses this might make dead if
     85 /// DeleteDeadConditions is true.
     86 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false,
     87                             const TargetLibraryInfo *TLI = nullptr);
     88 
     89 //===----------------------------------------------------------------------===//
     90 //  Local dead code elimination.
     91 //
     92 
     93 /// Return true if the result produced by the instruction is not used, and the
     94 /// instruction has no side effects.
     95 bool isInstructionTriviallyDead(Instruction *I,
     96                                 const TargetLibraryInfo *TLI = nullptr);
     97 
     98 /// Return true if the result produced by the instruction would have no side
     99 /// effects if it was not used. This is equivalent to checking whether
    100 /// isInstructionTriviallyDead would be true if the use count was 0.
    101 bool wouldInstructionBeTriviallyDead(Instruction *I,
    102                                      const TargetLibraryInfo *TLI = nullptr);
    103 
    104 /// If the specified value is a trivially dead instruction, delete it.
    105 /// If that makes any of its operands trivially dead, delete them too,
    106 /// recursively. Return true if any instructions were deleted.
    107 bool RecursivelyDeleteTriviallyDeadInstructions(Value *V,
    108                                         const TargetLibraryInfo *TLI = nullptr);
    109 
    110 /// If the specified value is an effectively dead PHI node, due to being a
    111 /// def-use chain of single-use nodes that either forms a cycle or is terminated
    112 /// by a trivially dead instruction, delete it. If that makes any of its
    113 /// operands trivially dead, delete them too, recursively. Return true if a
    114 /// change was made.
    115 bool RecursivelyDeleteDeadPHINode(PHINode *PN,
    116                                   const TargetLibraryInfo *TLI = nullptr);
    117 
    118 /// Scan the specified basic block and try to simplify any instructions in it
    119 /// and recursively delete dead instructions.
    120 ///
    121 /// This returns true if it changed the code, note that it can delete
    122 /// instructions in other blocks as well in this block.
    123 bool SimplifyInstructionsInBlock(BasicBlock *BB,
    124                                  const TargetLibraryInfo *TLI = nullptr);
    125 
    126 //===----------------------------------------------------------------------===//
    127 //  Control Flow Graph Restructuring.
    128 //
    129 
    130 /// Like BasicBlock::removePredecessor, this method is called when we're about
    131 /// to delete Pred as a predecessor of BB. If BB contains any PHI nodes, this
    132 /// drops the entries in the PHI nodes for Pred.
    133 ///
    134 /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
    135 /// nodes that collapse into identity values.  For example, if we have:
    136 ///   x = phi(1, 0, 0, 0)
    137 ///   y = and x, z
    138 ///
    139 /// .. and delete the predecessor corresponding to the '1', this will attempt to
    140 /// recursively fold the 'and' to 0.
    141 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred);
    142 
    143 /// BB is a block with one predecessor and its predecessor is known to have one
    144 /// successor (BB!). Eliminate the edge between them, moving the instructions in
    145 /// the predecessor into BB. This deletes the predecessor block.
    146 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr);
    147 
    148 /// BB is known to contain an unconditional branch, and contains no instructions
    149 /// other than PHI nodes, potential debug intrinsics and the branch. If
    150 /// possible, eliminate BB by rewriting all the predecessors to branch to the
    151 /// successor block and return true. If we can't transform, return false.
    152 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
    153 
    154 /// Check for and eliminate duplicate PHI nodes in this block. This doesn't try
    155 /// to be clever about PHI nodes which differ only in the order of the incoming
    156 /// values, but instcombine orders them so it usually won't matter.
    157 bool EliminateDuplicatePHINodes(BasicBlock *BB);
    158 
    159 /// This function is used to do simplification of a CFG.  For example, it
    160 /// adjusts branches to branches to eliminate the extra hop, it eliminates
    161 /// unreachable basic blocks, and does other peephole optimization of the CFG.
    162 /// It returns true if a modification was made, possibly deleting the basic
    163 /// block that was pointed to. LoopHeaders is an optional input parameter
    164 /// providing the set of loop headers that SimplifyCFG should not eliminate.
    165 bool simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
    166                  const SimplifyCFGOptions &Options = {},
    167                  SmallPtrSetImpl<BasicBlock *> *LoopHeaders = nullptr);
    168 
    169 /// This function is used to flatten a CFG. For example, it uses parallel-and
    170 /// and parallel-or mode to collapse if-conditions and merge if-regions with
    171 /// identical statements.
    172 bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA = nullptr);
    173 
    174 /// If this basic block is ONLY a setcc and a branch, and if a predecessor
    175 /// branches to us and one of our successors, fold the setcc into the
    176 /// predecessor and use logical operations to pick the right destination.
    177 bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold = 1);
    178 
    179 /// This function takes a virtual register computed by an Instruction and
    180 /// replaces it with a slot in the stack frame, allocated via alloca.
    181 /// This allows the CFG to be changed around without fear of invalidating the
    182 /// SSA information for the value. It returns the pointer to the alloca inserted
    183 /// to create a stack slot for X.
    184 AllocaInst *DemoteRegToStack(Instruction &X,
    185                              bool VolatileLoads = false,
    186                              Instruction *AllocaPoint = nullptr);
    187 
    188 /// This function takes a virtual register computed by a phi node and replaces
    189 /// it with a slot in the stack frame, allocated via alloca. The phi node is
    190 /// deleted and it returns the pointer to the alloca inserted.
    191 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = nullptr);
    192 
    193 /// Try to ensure that the alignment of \p V is at least \p PrefAlign bytes. If
    194 /// the owning object can be modified and has an alignment less than \p
    195 /// PrefAlign, it will be increased and \p PrefAlign returned. If the alignment
    196 /// cannot be increased, the known alignment of the value is returned.
    197 ///
    198 /// It is not always possible to modify the alignment of the underlying object,
    199 /// so if alignment is important, a more reliable approach is to simply align
    200 /// all global variables and allocation instructions to their preferred
    201 /// alignment from the beginning.
    202 unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
    203                                     const DataLayout &DL,
    204                                     const Instruction *CxtI = nullptr,
    205                                     AssumptionCache *AC = nullptr,
    206                                     const DominatorTree *DT = nullptr);
    207 
    208 /// Try to infer an alignment for the specified pointer.
    209 static inline unsigned getKnownAlignment(Value *V, const DataLayout &DL,
    210                                          const Instruction *CxtI = nullptr,
    211                                          AssumptionCache *AC = nullptr,
    212                                          const DominatorTree *DT = nullptr) {
    213   return getOrEnforceKnownAlignment(V, 0, DL, CxtI, AC, DT);
    214 }
    215 
    216 /// Given a getelementptr instruction/constantexpr, emit the code necessary to
    217 /// compute the offset from the base pointer (without adding in the base
    218 /// pointer). Return the result as a signed integer of intptr size.
    219 /// When NoAssumptions is true, no assumptions about index computation not
    220 /// overflowing is made.
    221 template <typename IRBuilderTy>
    222 Value *EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP,
    223                      bool NoAssumptions = false) {
    224   GEPOperator *GEPOp = cast<GEPOperator>(GEP);
    225   Type *IntPtrTy = DL.getIntPtrType(GEP->getType());
    226   Value *Result = Constant::getNullValue(IntPtrTy);
    227 
    228   // If the GEP is inbounds, we know that none of the addressing operations will
    229   // overflow in an unsigned sense.
    230   bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
    231 
    232   // Build a mask for high order bits.
    233   unsigned IntPtrWidth = IntPtrTy->getScalarType()->getIntegerBitWidth();
    234   uint64_t PtrSizeMask =
    235       std::numeric_limits<uint64_t>::max() >> (64 - IntPtrWidth);
    236 
    237   gep_type_iterator GTI = gep_type_begin(GEP);
    238   for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end(); i != e;
    239        ++i, ++GTI) {
    240     Value *Op = *i;
    241     uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
    242     if (Constant *OpC = dyn_cast<Constant>(Op)) {
    243       if (OpC->isZeroValue())
    244         continue;
    245 
    246       // Handle a struct index, which adds its field offset to the pointer.
    247       if (StructType *STy = GTI.getStructTypeOrNull()) {
    248         if (OpC->getType()->isVectorTy())
    249           OpC = OpC->getSplatValue();
    250 
    251         uint64_t OpValue = cast<ConstantInt>(OpC)->getZExtValue();
    252         Size = DL.getStructLayout(STy)->getElementOffset(OpValue);
    253 
    254         if (Size)
    255           Result = Builder->CreateAdd(Result, ConstantInt::get(IntPtrTy, Size),
    256                                       GEP->getName()+".offs");
    257         continue;
    258       }
    259 
    260       Constant *Scale = ConstantInt::get(IntPtrTy, Size);
    261       Constant *OC = ConstantExpr::getIntegerCast(OpC, IntPtrTy, true /*SExt*/);
    262       Scale = ConstantExpr::getMul(OC, Scale, isInBounds/*NUW*/);
    263       // Emit an add instruction.
    264       Result = Builder->CreateAdd(Result, Scale, GEP->getName()+".offs");
    265       continue;
    266     }
    267     // Convert to correct type.
    268     if (Op->getType() != IntPtrTy)
    269       Op = Builder->CreateIntCast(Op, IntPtrTy, true, Op->getName()+".c");
    270     if (Size != 1) {
    271       // We'll let instcombine(mul) convert this to a shl if possible.
    272       Op = Builder->CreateMul(Op, ConstantInt::get(IntPtrTy, Size),
    273                               GEP->getName()+".idx", isInBounds /*NUW*/);
    274     }
    275 
    276     // Emit an add instruction.
    277     Result = Builder->CreateAdd(Op, Result, GEP->getName()+".offs");
    278   }
    279   return Result;
    280 }
    281 
    282 ///===---------------------------------------------------------------------===//
    283 ///  Dbg Intrinsic utilities
    284 ///
    285 
    286 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
    287 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
    288 void ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
    289                                      StoreInst *SI, DIBuilder &Builder);
    290 
    291 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
    292 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
    293 void ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
    294                                      LoadInst *LI, DIBuilder &Builder);
    295 
    296 /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
    297 /// llvm.dbg.declare or llvm.dbg.addr intrinsic.
    298 void ConvertDebugDeclareToDebugValue(DbgInfoIntrinsic *DII,
    299                                      PHINode *LI, DIBuilder &Builder);
    300 
    301 /// Lowers llvm.dbg.declare intrinsics into appropriate set of
    302 /// llvm.dbg.value intrinsics.
    303 bool LowerDbgDeclare(Function &F);
    304 
    305 /// Finds all intrinsics declaring local variables as living in the memory that
    306 /// 'V' points to. This may include a mix of dbg.declare and
    307 /// dbg.addr intrinsics.
    308 TinyPtrVector<DbgInfoIntrinsic *> FindDbgAddrUses(Value *V);
    309 
    310 /// Finds the llvm.dbg.value intrinsics describing a value.
    311 void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V);
    312 
    313 /// Replaces llvm.dbg.declare instruction when the address it describes
    314 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
    315 /// prepended to the expression. If Offset is non-zero, a constant displacement
    316 /// is added to the expression (after the optional Deref). Offset can be
    317 /// negative.
    318 bool replaceDbgDeclare(Value *Address, Value *NewAddress,
    319                        Instruction *InsertBefore, DIBuilder &Builder,
    320                        bool Deref, int Offset);
    321 
    322 /// Replaces llvm.dbg.declare instruction when the alloca it describes
    323 /// is replaced with a new value. If Deref is true, an additional DW_OP_deref is
    324 /// prepended to the expression. If Offset is non-zero, a constant displacement
    325 /// is added to the expression (after the optional Deref). Offset can be
    326 /// negative. New llvm.dbg.declare is inserted immediately before AI.
    327 bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
    328                                 DIBuilder &Builder, bool Deref, int Offset = 0);
    329 
    330 /// Replaces multiple llvm.dbg.value instructions when the alloca it describes
    331 /// is replaced with a new value. If Offset is non-zero, a constant displacement
    332 /// is added to the expression (after the mandatory Deref). Offset can be
    333 /// negative. New llvm.dbg.value instructions are inserted at the locations of
    334 /// the instructions they replace.
    335 void replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
    336                               DIBuilder &Builder, int Offset = 0);
    337 
    338 /// Assuming the instruction \p I is going to be deleted, attempt to salvage any
    339 /// dbg.value intrinsics referring to \p I by rewriting its effect into a
    340 /// DIExpression.
    341 void salvageDebugInfo(Instruction &I);
    342 
    343 /// Remove all instructions from a basic block other than it's terminator
    344 /// and any present EH pad instructions.
    345 unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB);
    346 
    347 /// Insert an unreachable instruction before the specified
    348 /// instruction, making it and the rest of the code in the block dead.
    349 unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap,
    350                              bool PreserveLCSSA = false);
    351 
    352 /// Convert the CallInst to InvokeInst with the specified unwind edge basic
    353 /// block.  This also splits the basic block where CI is located, because
    354 /// InvokeInst is a terminator instruction.  Returns the newly split basic
    355 /// block.
    356 BasicBlock *changeToInvokeAndSplitBasicBlock(CallInst *CI,
    357                                              BasicBlock *UnwindEdge);
    358 
    359 /// Replace 'BB's terminator with one that does not have an unwind successor
    360 /// block. Rewrites `invoke` to `call`, etc. Updates any PHIs in unwind
    361 /// successor.
    362 ///
    363 /// \param BB  Block whose terminator will be replaced.  Its terminator must
    364 ///            have an unwind successor.
    365 void removeUnwindEdge(BasicBlock *BB);
    366 
    367 /// Remove all blocks that can not be reached from the function's entry.
    368 ///
    369 /// Returns true if any basic block was removed.
    370 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr);
    371 
    372 /// Combine the metadata of two instructions so that K can replace J
    373 ///
    374 /// Metadata not listed as known via KnownIDs is removed
    375 void combineMetadata(Instruction *K, const Instruction *J, ArrayRef<unsigned> KnownIDs);
    376 
    377 /// Combine the metadata of two instructions so that K can replace J. This
    378 /// specifically handles the case of CSE-like transformations.
    379 ///
    380 /// Unknown metadata is removed.
    381 void combineMetadataForCSE(Instruction *K, const Instruction *J);
    382 
    383 // Replace each use of 'From' with 'To', if that use does not belong to basic
    384 // block where 'From' is defined. Returns the number of replacements made.
    385 unsigned replaceNonLocalUsesWith(Instruction *From, Value *To);
    386 
    387 /// Replace each use of 'From' with 'To' if that use is dominated by
    388 /// the given edge.  Returns the number of replacements made.
    389 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
    390                                   const BasicBlockEdge &Edge);
    391 /// Replace each use of 'From' with 'To' if that use is dominated by
    392 /// the end of the given BasicBlock. Returns the number of replacements made.
    393 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
    394                                   const BasicBlock *BB);
    395 
    396 /// Return true if the CallSite CS calls a gc leaf function.
    397 ///
    398 /// A leaf function is a function that does not safepoint the thread during its
    399 /// execution.  During a call or invoke to such a function, the callers stack
    400 /// does not have to be made parseable.
    401 ///
    402 /// Most passes can and should ignore this information, and it is only used
    403 /// during lowering by the GC infrastructure.
    404 bool callsGCLeafFunction(ImmutableCallSite CS, const TargetLibraryInfo &TLI);
    405 
    406 /// Copy a nonnull metadata node to a new load instruction.
    407 ///
    408 /// This handles mapping it to range metadata if the new load is an integer
    409 /// load instead of a pointer load.
    410 void copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, LoadInst &NewLI);
    411 
    412 /// Copy a range metadata node to a new load instruction.
    413 ///
    414 /// This handles mapping it to nonnull metadata if the new load is a pointer
    415 /// load instead of an integer load and the range doesn't cover null.
    416 void copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, MDNode *N,
    417                        LoadInst &NewLI);
    418 
    419 //===----------------------------------------------------------------------===//
    420 //  Intrinsic pattern matching
    421 //
    422 
    423 /// Try and match a bswap or bitreverse idiom.
    424 ///
    425 /// If an idiom is matched, an intrinsic call is inserted before \c I. Any added
    426 /// instructions are returned in \c InsertedInsts. They will all have been added
    427 /// to a basic block.
    428 ///
    429 /// A bitreverse idiom normally requires around 2*BW nodes to be searched (where
    430 /// BW is the bitwidth of the integer type). A bswap idiom requires anywhere up
    431 /// to BW / 4 nodes to be searched, so is significantly faster.
    432 ///
    433 /// This function returns true on a successful match or false otherwise.
    434 bool recognizeBSwapOrBitReverseIdiom(
    435     Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
    436     SmallVectorImpl<Instruction *> &InsertedInsts);
    437 
    438 //===----------------------------------------------------------------------===//
    439 //  Sanitizer utilities
    440 //
    441 
    442 /// Given a CallInst, check if it calls a string function known to CodeGen,
    443 /// and mark it with NoBuiltin if so.  To be used by sanitizers that intend
    444 /// to intercept string functions and want to avoid converting them to target
    445 /// specific instructions.
    446 void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI,
    447                                             const TargetLibraryInfo *TLI);
    448 
    449 //===----------------------------------------------------------------------===//
    450 //  Transform predicates
    451 //
    452 
    453 /// Given an instruction, is it legal to set operand OpIdx to a non-constant
    454 /// value?
    455 bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx);
    456 
    457 } // end namespace llvm
    458 
    459 #endif // LLVM_TRANSFORMS_UTILS_LOCAL_H
    460