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