1 //===- Local.h - Functions to perform local transformations -----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This family of functions perform various local transformations to the 10 // program. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_TRANSFORMS_UTILS_LOCAL_H 15 #define LLVM_TRANSFORMS_UTILS_LOCAL_H 16 17 #include "llvm/ADT/ArrayRef.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/TinyPtrVector.h" 21 #include "llvm/Analysis/Utils/Local.h" 22 #include "llvm/IR/Constant.h" 23 #include "llvm/IR/Constants.h" 24 #include "llvm/IR/DataLayout.h" 25 #include "llvm/IR/Dominators.h" 26 #include "llvm/IR/Operator.h" 27 #include "llvm/IR/Type.h" 28 #include "llvm/IR/User.h" 29 #include "llvm/IR/Value.h" 30 #include "llvm/IR/ValueHandle.h" 31 #include "llvm/Support/Casting.h" 32 #include "llvm/Support/CommandLine.h" 33 #include "llvm/Transforms/Utils/SimplifyCFGOptions.h" 34 #include <cstdint> 35 #include <limits> 36 37 namespace llvm { 38 39 class AAResults; 40 class AllocaInst; 41 class AssumptionCache; 42 class BasicBlock; 43 class BranchInst; 44 class CallBase; 45 class CallInst; 46 class DbgDeclareInst; 47 class DbgVariableIntrinsic; 48 class DbgValueInst; 49 class DIBuilder; 50 class DomTreeUpdater; 51 class Function; 52 class Instruction; 53 class InvokeInst; 54 class LoadInst; 55 class MDNode; 56 class MemorySSAUpdater; 57 class PHINode; 58 class StoreInst; 59 class TargetLibraryInfo; 60 class TargetTransformInfo; 61 62 //===----------------------------------------------------------------------===// 63 // Local constant propagation. 64 // 65 66 /// If a terminator instruction is predicated on a constant value, convert it 67 /// into an unconditional branch to the constant destination. 68 /// This is a nontrivial operation because the successors of this basic block 69 /// must have their PHI nodes updated. 70 /// Also calls RecursivelyDeleteTriviallyDeadInstructions() on any branch/switch 71 /// conditions and indirectbr addresses this might make dead if 72 /// DeleteDeadConditions is true. 73 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false, 74 const TargetLibraryInfo *TLI = nullptr, 75 DomTreeUpdater *DTU = nullptr); 76 77 //===----------------------------------------------------------------------===// 78 // Local dead code elimination. 79 // 80 81 /// Return true if the result produced by the instruction is not used, and the 82 /// instruction has no side effects. 83 bool isInstructionTriviallyDead(Instruction *I, 84 const TargetLibraryInfo *TLI = nullptr); 85 86 /// Return true if the result produced by the instruction would have no side 87 /// effects if it was not used. This is equivalent to checking whether 88 /// isInstructionTriviallyDead would be true if the use count was 0. 89 bool wouldInstructionBeTriviallyDead(Instruction *I, 90 const TargetLibraryInfo *TLI = nullptr); 91 92 /// If the specified value is a trivially dead instruction, delete it. 93 /// If that makes any of its operands trivially dead, delete them too, 94 /// recursively. Return true if any instructions were deleted. 95 bool RecursivelyDeleteTriviallyDeadInstructions( 96 Value *V, const TargetLibraryInfo *TLI = nullptr, 97 MemorySSAUpdater *MSSAU = nullptr, 98 std::function<void(Value *)> AboutToDeleteCallback = 99 std::function<void(Value *)>()); 100 101 /// Delete all of the instructions in `DeadInsts`, and all other instructions 102 /// that deleting these in turn causes to be trivially dead. 103 /// 104 /// The initial instructions in the provided vector must all have empty use 105 /// lists and satisfy `isInstructionTriviallyDead`. 106 /// 107 /// `DeadInsts` will be used as scratch storage for this routine and will be 108 /// empty afterward. 109 void RecursivelyDeleteTriviallyDeadInstructions( 110 SmallVectorImpl<WeakTrackingVH> &DeadInsts, 111 const TargetLibraryInfo *TLI = nullptr, MemorySSAUpdater *MSSAU = nullptr, 112 std::function<void(Value *)> AboutToDeleteCallback = 113 std::function<void(Value *)>()); 114 115 /// Same functionality as RecursivelyDeleteTriviallyDeadInstructions, but allow 116 /// instructions that are not trivially dead. These will be ignored. 117 /// Returns true if any changes were made, i.e. any instructions trivially dead 118 /// were found and deleted. 119 bool RecursivelyDeleteTriviallyDeadInstructionsPermissive( 120 SmallVectorImpl<WeakTrackingVH> &DeadInsts, 121 const TargetLibraryInfo *TLI = nullptr, MemorySSAUpdater *MSSAU = nullptr, 122 std::function<void(Value *)> AboutToDeleteCallback = 123 std::function<void(Value *)>()); 124 125 /// If the specified value is an effectively dead PHI node, due to being a 126 /// def-use chain of single-use nodes that either forms a cycle or is terminated 127 /// by a trivially dead instruction, delete it. If that makes any of its 128 /// operands trivially dead, delete them too, recursively. Return true if a 129 /// change was made. 130 bool RecursivelyDeleteDeadPHINode(PHINode *PN, 131 const TargetLibraryInfo *TLI = nullptr, 132 MemorySSAUpdater *MSSAU = nullptr); 133 134 /// Scan the specified basic block and try to simplify any instructions in it 135 /// and recursively delete dead instructions. 136 /// 137 /// This returns true if it changed the code, note that it can delete 138 /// instructions in other blocks as well in this block. 139 bool SimplifyInstructionsInBlock(BasicBlock *BB, 140 const TargetLibraryInfo *TLI = nullptr); 141 142 /// Replace all the uses of an SSA value in @llvm.dbg intrinsics with 143 /// undef. This is useful for signaling that a variable, e.g. has been 144 /// found dead and hence it's unavailable at a given program point. 145 /// Returns true if the dbg values have been changed. 146 bool replaceDbgUsesWithUndef(Instruction *I); 147 148 //===----------------------------------------------------------------------===// 149 // Control Flow Graph Restructuring. 150 // 151 152 /// BB is a block with one predecessor and its predecessor is known to have one 153 /// successor (BB!). Eliminate the edge between them, moving the instructions in 154 /// the predecessor into BB. This deletes the predecessor block. 155 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DomTreeUpdater *DTU = nullptr); 156 157 /// BB is known to contain an unconditional branch, and contains no instructions 158 /// other than PHI nodes, potential debug intrinsics and the branch. If 159 /// possible, eliminate BB by rewriting all the predecessors to branch to the 160 /// successor block and return true. If we can't transform, return false. 161 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, 162 DomTreeUpdater *DTU = nullptr); 163 164 /// Check for and eliminate duplicate PHI nodes in this block. This doesn't try 165 /// to be clever about PHI nodes which differ only in the order of the incoming 166 /// values, but instcombine orders them so it usually won't matter. 167 bool EliminateDuplicatePHINodes(BasicBlock *BB); 168 169 /// This function is used to do simplification of a CFG. For example, it 170 /// adjusts branches to branches to eliminate the extra hop, it eliminates 171 /// unreachable basic blocks, and does other peephole optimization of the CFG. 172 /// It returns true if a modification was made, possibly deleting the basic 173 /// block that was pointed to. LoopHeaders is an optional input parameter 174 /// providing the set of loop headers that SimplifyCFG should not eliminate. 175 extern cl::opt<bool> RequireAndPreserveDomTree; 176 bool simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI, 177 DomTreeUpdater *DTU = nullptr, 178 const SimplifyCFGOptions &Options = {}, 179 ArrayRef<WeakVH> LoopHeaders = {}); 180 181 /// This function is used to flatten a CFG. For example, it uses parallel-and 182 /// and parallel-or mode to collapse if-conditions and merge if-regions with 183 /// identical statements. 184 bool FlattenCFG(BasicBlock *BB, AAResults *AA = nullptr); 185 186 /// If this basic block is ONLY a setcc and a branch, and if a predecessor 187 /// branches to us and one of our successors, fold the setcc into the 188 /// predecessor and use logical operations to pick the right destination. 189 bool FoldBranchToCommonDest(BranchInst *BI, llvm::DomTreeUpdater *DTU = nullptr, 190 MemorySSAUpdater *MSSAU = nullptr, 191 const TargetTransformInfo *TTI = nullptr, 192 unsigned BonusInstThreshold = 1); 193 194 /// This function takes a virtual register computed by an Instruction and 195 /// replaces it with a slot in the stack frame, allocated via alloca. 196 /// This allows the CFG to be changed around without fear of invalidating the 197 /// SSA information for the value. It returns the pointer to the alloca inserted 198 /// to create a stack slot for X. 199 AllocaInst *DemoteRegToStack(Instruction &X, 200 bool VolatileLoads = false, 201 Instruction *AllocaPoint = nullptr); 202 203 /// This function takes a virtual register computed by a phi node and replaces 204 /// it with a slot in the stack frame, allocated via alloca. The phi node is 205 /// deleted and it returns the pointer to the alloca inserted. 206 AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = nullptr); 207 208 /// Try to ensure that the alignment of \p V is at least \p PrefAlign bytes. If 209 /// the owning object can be modified and has an alignment less than \p 210 /// PrefAlign, it will be increased and \p PrefAlign returned. If the alignment 211 /// cannot be increased, the known alignment of the value is returned. 212 /// 213 /// It is not always possible to modify the alignment of the underlying object, 214 /// so if alignment is important, a more reliable approach is to simply align 215 /// all global variables and allocation instructions to their preferred 216 /// alignment from the beginning. 217 Align getOrEnforceKnownAlignment(Value *V, MaybeAlign PrefAlign, 218 const DataLayout &DL, 219 const Instruction *CxtI = nullptr, 220 AssumptionCache *AC = nullptr, 221 const DominatorTree *DT = nullptr); 222 223 /// Try to infer an alignment for the specified pointer. 224 inline Align getKnownAlignment(Value *V, const DataLayout &DL, 225 const Instruction *CxtI = nullptr, 226 AssumptionCache *AC = nullptr, 227 const DominatorTree *DT = nullptr) { 228 return getOrEnforceKnownAlignment(V, MaybeAlign(), DL, CxtI, AC, DT); 229 } 230 231 /// Create a call that matches the invoke \p II in terms of arguments, 232 /// attributes, debug information, etc. The call is not placed in a block and it 233 /// will not have a name. The invoke instruction is not removed, nor are the 234 /// uses replaced by the new call. 235 CallInst *createCallMatchingInvoke(InvokeInst *II); 236 237 /// This function converts the specified invoek into a normall call. 238 void changeToCall(InvokeInst *II, DomTreeUpdater *DTU = nullptr); 239 240 ///===---------------------------------------------------------------------===// 241 /// Dbg Intrinsic utilities 242 /// 243 244 /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value 245 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic. 246 void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, 247 StoreInst *SI, DIBuilder &Builder); 248 249 /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value 250 /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic. 251 void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, 252 LoadInst *LI, DIBuilder &Builder); 253 254 /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated 255 /// llvm.dbg.declare or llvm.dbg.addr intrinsic. 256 void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII, 257 PHINode *LI, DIBuilder &Builder); 258 259 /// Lowers llvm.dbg.declare intrinsics into appropriate set of 260 /// llvm.dbg.value intrinsics. 261 bool LowerDbgDeclare(Function &F); 262 263 /// Propagate dbg.value intrinsics through the newly inserted PHIs. 264 void insertDebugValuesForPHIs(BasicBlock *BB, 265 SmallVectorImpl<PHINode *> &InsertedPHIs); 266 267 /// Finds all intrinsics declaring local variables as living in the memory that 268 /// 'V' points to. This may include a mix of dbg.declare and 269 /// dbg.addr intrinsics. 270 TinyPtrVector<DbgVariableIntrinsic *> FindDbgAddrUses(Value *V); 271 272 /// Like \c FindDbgAddrUses, but only returns dbg.declare intrinsics, not 273 /// dbg.addr. 274 TinyPtrVector<DbgDeclareInst *> FindDbgDeclareUses(Value *V); 275 276 /// Finds the llvm.dbg.value intrinsics describing a value. 277 void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V); 278 279 /// Finds the debug info intrinsics describing a value. 280 void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V); 281 282 /// Replaces llvm.dbg.declare instruction when the address it 283 /// describes is replaced with a new value. If Deref is true, an 284 /// additional DW_OP_deref is prepended to the expression. If Offset 285 /// is non-zero, a constant displacement is added to the expression 286 /// (between the optional Deref operations). Offset can be negative. 287 bool replaceDbgDeclare(Value *Address, Value *NewAddress, DIBuilder &Builder, 288 uint8_t DIExprFlags, int Offset); 289 290 /// Replaces multiple llvm.dbg.value instructions when the alloca it describes 291 /// is replaced with a new value. If Offset is non-zero, a constant displacement 292 /// is added to the expression (after the mandatory Deref). Offset can be 293 /// negative. New llvm.dbg.value instructions are inserted at the locations of 294 /// the instructions they replace. 295 void replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress, 296 DIBuilder &Builder, int Offset = 0); 297 298 /// Assuming the instruction \p I is going to be deleted, attempt to salvage 299 /// debug users of \p I by writing the effect of \p I in a DIExpression. If it 300 /// cannot be salvaged changes its debug uses to undef. 301 void salvageDebugInfo(Instruction &I); 302 303 304 /// Implementation of salvageDebugInfo, applying only to instructions in 305 /// \p Insns, rather than all debug users from findDbgUsers( \p I). 306 /// Returns true if any debug users were updated. 307 /// Mark undef if salvaging cannot be completed. 308 void salvageDebugInfoForDbgValues(Instruction &I, 309 ArrayRef<DbgVariableIntrinsic *> Insns); 310 311 /// Given an instruction \p I and DIExpression \p DIExpr operating on it, write 312 /// the effects of \p I into the returned DIExpression, or return nullptr if 313 /// it cannot be salvaged. \p StackVal: whether DW_OP_stack_value should be 314 /// appended to the expression. 315 DIExpression *salvageDebugInfoImpl(Instruction &I, DIExpression *DIExpr, 316 bool StackVal); 317 318 /// Point debug users of \p From to \p To or salvage them. Use this function 319 /// only when replacing all uses of \p From with \p To, with a guarantee that 320 /// \p From is going to be deleted. 321 /// 322 /// Follow these rules to prevent use-before-def of \p To: 323 /// . If \p To is a linked Instruction, set \p DomPoint to \p To. 324 /// . If \p To is an unlinked Instruction, set \p DomPoint to the Instruction 325 /// \p To will be inserted after. 326 /// . If \p To is not an Instruction (e.g a Constant), the choice of 327 /// \p DomPoint is arbitrary. Pick \p From for simplicity. 328 /// 329 /// If a debug user cannot be preserved without reordering variable updates or 330 /// introducing a use-before-def, it is either salvaged (\ref salvageDebugInfo) 331 /// or deleted. Returns true if any debug users were updated. 332 bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint, 333 DominatorTree &DT); 334 335 /// Remove all instructions from a basic block other than its terminator 336 /// and any present EH pad instructions. Returns a pair where the first element 337 /// is the number of instructions (excluding debug info instrinsics) that have 338 /// been removed, and the second element is the number of debug info intrinsics 339 /// that have been removed. 340 std::pair<unsigned, unsigned> 341 removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB); 342 343 /// Insert an unreachable instruction before the specified 344 /// instruction, making it and the rest of the code in the block dead. 345 unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap, 346 bool PreserveLCSSA = false, 347 DomTreeUpdater *DTU = nullptr, 348 MemorySSAUpdater *MSSAU = nullptr); 349 350 /// Convert the CallInst to InvokeInst with the specified unwind edge basic 351 /// block. This also splits the basic block where CI is located, because 352 /// InvokeInst is a terminator instruction. Returns the newly split basic 353 /// block. 354 BasicBlock *changeToInvokeAndSplitBasicBlock(CallInst *CI, 355 BasicBlock *UnwindEdge); 356 357 /// Replace 'BB's terminator with one that does not have an unwind successor 358 /// block. Rewrites `invoke` to `call`, etc. Updates any PHIs in unwind 359 /// successor. 360 /// 361 /// \param BB Block whose terminator will be replaced. Its terminator must 362 /// have an unwind successor. 363 void removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU = nullptr); 364 365 /// Remove all blocks that can not be reached from the function's entry. 366 /// 367 /// Returns true if any basic block was removed. 368 bool removeUnreachableBlocks(Function &F, DomTreeUpdater *DTU = nullptr, 369 MemorySSAUpdater *MSSAU = nullptr); 370 371 /// Combine the metadata of two instructions so that K can replace J. Some 372 /// metadata kinds can only be kept if K does not move, meaning it dominated 373 /// J in the original IR. 374 /// 375 /// Metadata not listed as known via KnownIDs is removed 376 void combineMetadata(Instruction *K, const Instruction *J, 377 ArrayRef<unsigned> KnownIDs, bool DoesKMove); 378 379 /// Combine the metadata of two instructions so that K can replace J. This 380 /// specifically handles the case of CSE-like transformations. Some 381 /// metadata can only be kept if K dominates J. For this to be correct, 382 /// K cannot be hoisted. 383 /// 384 /// Unknown metadata is removed. 385 void combineMetadataForCSE(Instruction *K, const Instruction *J, 386 bool DoesKMove); 387 388 /// Copy the metadata from the source instruction to the destination (the 389 /// replacement for the source instruction). 390 void copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source); 391 392 /// Patch the replacement so that it is not more restrictive than the value 393 /// being replaced. It assumes that the replacement does not get moved from 394 /// its original position. 395 void patchReplacementInstruction(Instruction *I, Value *Repl); 396 397 // Replace each use of 'From' with 'To', if that use does not belong to basic 398 // block where 'From' is defined. Returns the number of replacements made. 399 unsigned replaceNonLocalUsesWith(Instruction *From, Value *To); 400 401 /// Replace each use of 'From' with 'To' if that use is dominated by 402 /// the given edge. Returns the number of replacements made. 403 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, 404 const BasicBlockEdge &Edge); 405 /// Replace each use of 'From' with 'To' if that use is dominated by 406 /// the end of the given BasicBlock. Returns the number of replacements made. 407 unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT, 408 const BasicBlock *BB); 409 410 /// Return true if this call calls a gc leaf function. 411 /// 412 /// A leaf function is a function that does not safepoint the thread during its 413 /// execution. During a call or invoke to such a function, the callers stack 414 /// does not have to be made parseable. 415 /// 416 /// Most passes can and should ignore this information, and it is only used 417 /// during lowering by the GC infrastructure. 418 bool callsGCLeafFunction(const CallBase *Call, const TargetLibraryInfo &TLI); 419 420 /// Copy a nonnull metadata node to a new load instruction. 421 /// 422 /// This handles mapping it to range metadata if the new load is an integer 423 /// load instead of a pointer load. 424 void copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, LoadInst &NewLI); 425 426 /// Copy a range metadata node to a new load instruction. 427 /// 428 /// This handles mapping it to nonnull metadata if the new load is a pointer 429 /// load instead of an integer load and the range doesn't cover null. 430 void copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, MDNode *N, 431 LoadInst &NewLI); 432 433 /// Remove the debug intrinsic instructions for the given instruction. 434 void dropDebugUsers(Instruction &I); 435 436 /// Hoist all of the instructions in the \p IfBlock to the dominant block 437 /// \p DomBlock, by moving its instructions to the insertion point \p InsertPt. 438 /// 439 /// The moved instructions receive the insertion point debug location values 440 /// (DILocations) and their debug intrinsic instructions are removed. 441 void hoistAllInstructionsInto(BasicBlock *DomBlock, Instruction *InsertPt, 442 BasicBlock *BB); 443 444 //===----------------------------------------------------------------------===// 445 // Intrinsic pattern matching 446 // 447 448 /// Try to match a bswap or bitreverse idiom. 449 /// 450 /// If an idiom is matched, an intrinsic call is inserted before \c I. Any added 451 /// instructions are returned in \c InsertedInsts. They will all have been added 452 /// to a basic block. 453 /// 454 /// A bitreverse idiom normally requires around 2*BW nodes to be searched (where 455 /// BW is the bitwidth of the integer type). A bswap idiom requires anywhere up 456 /// to BW / 4 nodes to be searched, so is significantly faster. 457 /// 458 /// This function returns true on a successful match or false otherwise. 459 bool recognizeBSwapOrBitReverseIdiom( 460 Instruction *I, bool MatchBSwaps, bool MatchBitReversals, 461 SmallVectorImpl<Instruction *> &InsertedInsts); 462 463 //===----------------------------------------------------------------------===// 464 // Sanitizer utilities 465 // 466 467 /// Given a CallInst, check if it calls a string function known to CodeGen, 468 /// and mark it with NoBuiltin if so. To be used by sanitizers that intend 469 /// to intercept string functions and want to avoid converting them to target 470 /// specific instructions. 471 void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI, 472 const TargetLibraryInfo *TLI); 473 474 //===----------------------------------------------------------------------===// 475 // Transform predicates 476 // 477 478 /// Given an instruction, is it legal to set operand OpIdx to a non-constant 479 /// value? 480 bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx); 481 482 //===----------------------------------------------------------------------===// 483 // Value helper functions 484 // 485 486 /// Invert the given true/false value, possibly reusing an existing copy. 487 Value *invertCondition(Value *Condition); 488 489 } // end namespace llvm 490 491 #endif // LLVM_TRANSFORMS_UTILS_LOCAL_H 492