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