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