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