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