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