Line data Source code
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 17471 : : BonusInstThreshold(BonusThreshold),
79 : ForwardSwitchCondToPhi(ForwardSwitchCond),
80 : ConvertSwitchToLookupTable(SwitchToLookup),
81 : NeedCanonicalLoop(CanonicalLoops),
82 : SinkCommonInsts(SinkCommon),
83 17413 : 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 58 : ForwardSwitchCondToPhi = B;
92 : return *this;
93 : }
94 : SimplifyCFGOptions &convertSwitchToLookupTable(bool B) {
95 58 : ConvertSwitchToLookupTable = B;
96 : return *this;
97 : }
98 : SimplifyCFGOptions &needCanonicalLoops(bool B) {
99 58 : NeedCanonicalLoop = B;
100 : return *this;
101 : }
102 : SimplifyCFGOptions &sinkCommonInsts(bool B) {
103 58 : 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 : //===----------------------------------------------------------------------===//
178 : // Control Flow Graph Restructuring.
179 : //
180 :
181 : /// Like BasicBlock::removePredecessor, this method is called when we're about
182 : /// to delete Pred as a predecessor of BB. If BB contains any PHI nodes, this
183 : /// drops the entries in the PHI nodes for Pred.
184 : ///
185 : /// Unlike the removePredecessor method, this attempts to simplify uses of PHI
186 : /// nodes that collapse into identity values. For example, if we have:
187 : /// x = phi(1, 0, 0, 0)
188 : /// y = and x, z
189 : ///
190 : /// .. and delete the predecessor corresponding to the '1', this will attempt to
191 : /// recursively fold the 'and' to 0.
192 : void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
193 : DomTreeUpdater *DTU = nullptr);
194 :
195 : /// BB is a block with one predecessor and its predecessor is known to have one
196 : /// successor (BB!). Eliminate the edge between them, moving the instructions in
197 : /// the predecessor into BB. This deletes the predecessor block.
198 : void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DomTreeUpdater *DTU = nullptr);
199 :
200 : /// BB is known to contain an unconditional branch, and contains no instructions
201 : /// other than PHI nodes, potential debug intrinsics and the branch. If
202 : /// possible, eliminate BB by rewriting all the predecessors to branch to the
203 : /// successor block and return true. If we can't transform, return false.
204 : bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
205 : DomTreeUpdater *DTU = nullptr);
206 :
207 : /// Check for and eliminate duplicate PHI nodes in this block. This doesn't try
208 : /// to be clever about PHI nodes which differ only in the order of the incoming
209 : /// values, but instcombine orders them so it usually won't matter.
210 : bool EliminateDuplicatePHINodes(BasicBlock *BB);
211 :
212 : /// This function is used to do simplification of a CFG. For example, it
213 : /// adjusts branches to branches to eliminate the extra hop, it eliminates
214 : /// unreachable basic blocks, and does other peephole optimization of the CFG.
215 : /// It returns true if a modification was made, possibly deleting the basic
216 : /// block that was pointed to. LoopHeaders is an optional input parameter
217 : /// providing the set of loop headers that SimplifyCFG should not eliminate.
218 : bool simplifyCFG(BasicBlock *BB, const TargetTransformInfo &TTI,
219 : const SimplifyCFGOptions &Options = {},
220 : SmallPtrSetImpl<BasicBlock *> *LoopHeaders = nullptr);
221 :
222 : /// This function is used to flatten a CFG. For example, it uses parallel-and
223 : /// and parallel-or mode to collapse if-conditions and merge if-regions with
224 : /// identical statements.
225 : bool FlattenCFG(BasicBlock *BB, AliasAnalysis *AA = nullptr);
226 :
227 : /// If this basic block is ONLY a setcc and a branch, and if a predecessor
228 : /// branches to us and one of our successors, fold the setcc into the
229 : /// predecessor and use logical operations to pick the right destination.
230 : bool FoldBranchToCommonDest(BranchInst *BI, unsigned BonusInstThreshold = 1);
231 :
232 : /// This function takes a virtual register computed by an Instruction and
233 : /// replaces it with a slot in the stack frame, allocated via alloca.
234 : /// This allows the CFG to be changed around without fear of invalidating the
235 : /// SSA information for the value. It returns the pointer to the alloca inserted
236 : /// to create a stack slot for X.
237 : AllocaInst *DemoteRegToStack(Instruction &X,
238 : bool VolatileLoads = false,
239 : Instruction *AllocaPoint = nullptr);
240 :
241 : /// This function takes a virtual register computed by a phi node and replaces
242 : /// it with a slot in the stack frame, allocated via alloca. The phi node is
243 : /// deleted and it returns the pointer to the alloca inserted.
244 : AllocaInst *DemotePHIToStack(PHINode *P, Instruction *AllocaPoint = nullptr);
245 :
246 : /// Try to ensure that the alignment of \p V is at least \p PrefAlign bytes. If
247 : /// the owning object can be modified and has an alignment less than \p
248 : /// PrefAlign, it will be increased and \p PrefAlign returned. If the alignment
249 : /// cannot be increased, the known alignment of the value is returned.
250 : ///
251 : /// It is not always possible to modify the alignment of the underlying object,
252 : /// so if alignment is important, a more reliable approach is to simply align
253 : /// all global variables and allocation instructions to their preferred
254 : /// alignment from the beginning.
255 : unsigned getOrEnforceKnownAlignment(Value *V, unsigned PrefAlign,
256 : const DataLayout &DL,
257 : const Instruction *CxtI = nullptr,
258 : AssumptionCache *AC = nullptr,
259 : const DominatorTree *DT = nullptr);
260 :
261 : /// Try to infer an alignment for the specified pointer.
262 : inline unsigned getKnownAlignment(Value *V, const DataLayout &DL,
263 : const Instruction *CxtI = nullptr,
264 : AssumptionCache *AC = nullptr,
265 : const DominatorTree *DT = nullptr) {
266 211823 : return getOrEnforceKnownAlignment(V, 0, DL, CxtI, AC, DT);
267 : }
268 :
269 : ///===---------------------------------------------------------------------===//
270 : /// Dbg Intrinsic utilities
271 : ///
272 :
273 : /// Inserts a llvm.dbg.value intrinsic before a store to an alloca'd value
274 : /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
275 : void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
276 : StoreInst *SI, DIBuilder &Builder);
277 :
278 : /// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
279 : /// that has an associated llvm.dbg.declare or llvm.dbg.addr intrinsic.
280 : void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
281 : LoadInst *LI, DIBuilder &Builder);
282 :
283 : /// Inserts a llvm.dbg.value intrinsic after a phi that has an associated
284 : /// llvm.dbg.declare or llvm.dbg.addr intrinsic.
285 : void ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
286 : PHINode *LI, DIBuilder &Builder);
287 :
288 : /// Lowers llvm.dbg.declare intrinsics into appropriate set of
289 : /// llvm.dbg.value intrinsics.
290 : bool LowerDbgDeclare(Function &F);
291 :
292 : /// Propagate dbg.value intrinsics through the newly inserted PHIs.
293 : void insertDebugValuesForPHIs(BasicBlock *BB,
294 : SmallVectorImpl<PHINode *> &InsertedPHIs);
295 :
296 : /// Finds all intrinsics declaring local variables as living in the memory that
297 : /// 'V' points to. This may include a mix of dbg.declare and
298 : /// dbg.addr intrinsics.
299 : TinyPtrVector<DbgVariableIntrinsic *> FindDbgAddrUses(Value *V);
300 :
301 : /// Finds the llvm.dbg.value intrinsics describing a value.
302 : void findDbgValues(SmallVectorImpl<DbgValueInst *> &DbgValues, Value *V);
303 :
304 : /// Finds the debug info intrinsics describing a value.
305 : void findDbgUsers(SmallVectorImpl<DbgVariableIntrinsic *> &DbgInsts, Value *V);
306 :
307 : /// Replaces llvm.dbg.declare instruction when the address it
308 : /// describes is replaced with a new value. If Deref is true, an
309 : /// additional DW_OP_deref is prepended to the expression. If Offset
310 : /// is non-zero, a constant displacement is added to the expression
311 : /// (between the optional Deref operations). Offset can be negative.
312 : bool replaceDbgDeclare(Value *Address, Value *NewAddress,
313 : Instruction *InsertBefore, DIBuilder &Builder,
314 : bool DerefBefore, int Offset, bool DerefAfter);
315 :
316 : /// Replaces llvm.dbg.declare instruction when the alloca it describes
317 : /// is replaced with a new value. If Deref is true, an additional
318 : /// DW_OP_deref is prepended to the expression. If Offset is non-zero,
319 : /// a constant displacement is added to the expression (between the
320 : /// optional Deref operations). Offset can be negative. The new
321 : /// llvm.dbg.declare is inserted immediately after AI.
322 : bool replaceDbgDeclareForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
323 : DIBuilder &Builder, bool DerefBefore,
324 : int Offset, bool DerefAfter);
325 :
326 : /// Replaces multiple llvm.dbg.value instructions when the alloca it describes
327 : /// is replaced with a new value. If Offset is non-zero, a constant displacement
328 : /// is added to the expression (after the mandatory Deref). Offset can be
329 : /// negative. New llvm.dbg.value instructions are inserted at the locations of
330 : /// the instructions they replace.
331 : void replaceDbgValueForAlloca(AllocaInst *AI, Value *NewAllocaAddress,
332 : DIBuilder &Builder, int Offset = 0);
333 :
334 : /// Assuming the instruction \p I is going to be deleted, attempt to salvage
335 : /// debug users of \p I by writing the effect of \p I in a DIExpression.
336 : /// Returns true if any debug users were updated.
337 : bool salvageDebugInfo(Instruction &I);
338 :
339 : /// Point debug users of \p From to \p To or salvage them. Use this function
340 : /// only when replacing all uses of \p From with \p To, with a guarantee that
341 : /// \p From is going to be deleted.
342 : ///
343 : /// Follow these rules to prevent use-before-def of \p To:
344 : /// . If \p To is a linked Instruction, set \p DomPoint to \p To.
345 : /// . If \p To is an unlinked Instruction, set \p DomPoint to the Instruction
346 : /// \p To will be inserted after.
347 : /// . If \p To is not an Instruction (e.g a Constant), the choice of
348 : /// \p DomPoint is arbitrary. Pick \p From for simplicity.
349 : ///
350 : /// If a debug user cannot be preserved without reordering variable updates or
351 : /// introducing a use-before-def, it is either salvaged (\ref salvageDebugInfo)
352 : /// or deleted. Returns true if any debug users were updated.
353 : bool replaceAllDbgUsesWith(Instruction &From, Value &To, Instruction &DomPoint,
354 : DominatorTree &DT);
355 :
356 : /// Remove all instructions from a basic block other than it's terminator
357 : /// and any present EH pad instructions.
358 : unsigned removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB);
359 :
360 : /// Insert an unreachable instruction before the specified
361 : /// instruction, making it and the rest of the code in the block dead.
362 : unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap,
363 : bool PreserveLCSSA = false,
364 : DomTreeUpdater *DTU = nullptr);
365 :
366 : /// Convert the CallInst to InvokeInst with the specified unwind edge basic
367 : /// block. This also splits the basic block where CI is located, because
368 : /// InvokeInst is a terminator instruction. Returns the newly split basic
369 : /// block.
370 : BasicBlock *changeToInvokeAndSplitBasicBlock(CallInst *CI,
371 : BasicBlock *UnwindEdge);
372 :
373 : /// Replace 'BB's terminator with one that does not have an unwind successor
374 : /// block. Rewrites `invoke` to `call`, etc. Updates any PHIs in unwind
375 : /// successor.
376 : ///
377 : /// \param BB Block whose terminator will be replaced. Its terminator must
378 : /// have an unwind successor.
379 : void removeUnwindEdge(BasicBlock *BB, DomTreeUpdater *DTU = nullptr);
380 :
381 : /// Remove all blocks that can not be reached from the function's entry.
382 : ///
383 : /// Returns true if any basic block was removed.
384 : bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr,
385 : DomTreeUpdater *DTU = nullptr,
386 : MemorySSAUpdater *MSSAU = nullptr);
387 :
388 : /// Combine the metadata of two instructions so that K can replace J. Some
389 : /// metadata kinds can only be kept if K does not move, meaning it dominated
390 : /// J in the original IR.
391 : ///
392 : /// Metadata not listed as known via KnownIDs is removed
393 : void combineMetadata(Instruction *K, const Instruction *J,
394 : ArrayRef<unsigned> KnownIDs, bool DoesKMove);
395 :
396 : /// Combine the metadata of two instructions so that K can replace J. This
397 : /// specifically handles the case of CSE-like transformations. Some
398 : /// metadata can only be kept if K dominates J. For this to be correct,
399 : /// K cannot be hoisted.
400 : ///
401 : /// Unknown metadata is removed.
402 : void combineMetadataForCSE(Instruction *K, const Instruction *J,
403 : bool DoesKMove);
404 :
405 : /// Patch the replacement so that it is not more restrictive than the value
406 : /// being replaced. It assumes that the replacement does not get moved from
407 : /// its original position.
408 : void patchReplacementInstruction(Instruction *I, Value *Repl);
409 :
410 : // Replace each use of 'From' with 'To', if that use does not belong to basic
411 : // block where 'From' is defined. Returns the number of replacements made.
412 : unsigned replaceNonLocalUsesWith(Instruction *From, Value *To);
413 :
414 : /// Replace each use of 'From' with 'To' if that use is dominated by
415 : /// the given edge. Returns the number of replacements made.
416 : unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
417 : const BasicBlockEdge &Edge);
418 : /// Replace each use of 'From' with 'To' if that use is dominated by
419 : /// the end of the given BasicBlock. Returns the number of replacements made.
420 : unsigned replaceDominatedUsesWith(Value *From, Value *To, DominatorTree &DT,
421 : const BasicBlock *BB);
422 :
423 : /// Return true if the CallSite CS calls a gc leaf function.
424 : ///
425 : /// A leaf function is a function that does not safepoint the thread during its
426 : /// execution. During a call or invoke to such a function, the callers stack
427 : /// does not have to be made parseable.
428 : ///
429 : /// Most passes can and should ignore this information, and it is only used
430 : /// during lowering by the GC infrastructure.
431 : bool callsGCLeafFunction(ImmutableCallSite CS, const TargetLibraryInfo &TLI);
432 :
433 : /// Copy a nonnull metadata node to a new load instruction.
434 : ///
435 : /// This handles mapping it to range metadata if the new load is an integer
436 : /// load instead of a pointer load.
437 : void copyNonnullMetadata(const LoadInst &OldLI, MDNode *N, LoadInst &NewLI);
438 :
439 : /// Copy a range metadata node to a new load instruction.
440 : ///
441 : /// This handles mapping it to nonnull metadata if the new load is a pointer
442 : /// load instead of an integer load and the range doesn't cover null.
443 : void copyRangeMetadata(const DataLayout &DL, const LoadInst &OldLI, MDNode *N,
444 : LoadInst &NewLI);
445 :
446 : /// Remove the debug intrinsic instructions for the given instruction.
447 : void dropDebugUsers(Instruction &I);
448 :
449 : //===----------------------------------------------------------------------===//
450 : // Intrinsic pattern matching
451 : //
452 :
453 : /// Try to match a bswap or bitreverse idiom.
454 : ///
455 : /// If an idiom is matched, an intrinsic call is inserted before \c I. Any added
456 : /// instructions are returned in \c InsertedInsts. They will all have been added
457 : /// to a basic block.
458 : ///
459 : /// A bitreverse idiom normally requires around 2*BW nodes to be searched (where
460 : /// BW is the bitwidth of the integer type). A bswap idiom requires anywhere up
461 : /// to BW / 4 nodes to be searched, so is significantly faster.
462 : ///
463 : /// This function returns true on a successful match or false otherwise.
464 : bool recognizeBSwapOrBitReverseIdiom(
465 : Instruction *I, bool MatchBSwaps, bool MatchBitReversals,
466 : SmallVectorImpl<Instruction *> &InsertedInsts);
467 :
468 : //===----------------------------------------------------------------------===//
469 : // Sanitizer utilities
470 : //
471 :
472 : /// Given a CallInst, check if it calls a string function known to CodeGen,
473 : /// and mark it with NoBuiltin if so. To be used by sanitizers that intend
474 : /// to intercept string functions and want to avoid converting them to target
475 : /// specific instructions.
476 : void maybeMarkSanitizerLibraryCallNoBuiltin(CallInst *CI,
477 : const TargetLibraryInfo *TLI);
478 :
479 : //===----------------------------------------------------------------------===//
480 : // Transform predicates
481 : //
482 :
483 : /// Given an instruction, is it legal to set operand OpIdx to a non-constant
484 : /// value?
485 : bool canReplaceOperandWithVariable(const Instruction *I, unsigned OpIdx);
486 :
487 : } // end namespace llvm
488 :
489 : #endif // LLVM_TRANSFORMS_UTILS_LOCAL_H
|