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