LLVM  3.7.0
InstCombineInternal.h
Go to the documentation of this file.
1 //===- InstCombineInternal.h - InstCombine pass internals -------*- 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 /// \file
10 ///
11 /// This file provides internal interfaces used to implement the InstCombine.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
16 #define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
17 
20 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/IR/Dominators.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/InstVisitor.h"
26 #include "llvm/IR/IntrinsicInst.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/PatternMatch.h"
29 #include "llvm/Pass.h"
31 
32 #define DEBUG_TYPE "instcombine"
33 
34 namespace llvm {
35 class CallSite;
36 class DataLayout;
37 class DominatorTree;
38 class TargetLibraryInfo;
39 class DbgDeclareInst;
40 class MemIntrinsic;
41 class MemSetInst;
42 
43 /// \brief Assign a complexity or rank value to LLVM Values.
44 ///
45 /// This routine maps IR values to various complexity ranks:
46 /// 0 -> undef
47 /// 1 -> Constants
48 /// 2 -> Other non-instructions
49 /// 3 -> Arguments
50 /// 3 -> Unary operations
51 /// 4 -> Other instructions
52 static inline unsigned getComplexity(Value *V) {
53  if (isa<Instruction>(V)) {
56  return 3;
57  return 4;
58  }
59  if (isa<Argument>(V))
60  return 3;
61  return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
62 }
63 
64 /// \brief Add one to a Constant
65 static inline Constant *AddOne(Constant *C) {
66  return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
67 }
68 /// \brief Subtract one from a Constant
69 static inline Constant *SubOne(Constant *C) {
70  return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
71 }
72 
73 /// \brief Return true if the specified value is free to invert (apply ~ to).
74 /// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
75 /// is true, work under the assumption that the caller intends to remove all
76 /// uses of V and only keep uses of ~V.
77 ///
78 static inline bool IsFreeToInvert(Value *V, bool WillInvertAllUses) {
79  // ~(~(X)) -> X.
80  if (BinaryOperator::isNot(V))
81  return true;
82 
83  // Constants can be considered to be not'ed values.
84  if (isa<ConstantInt>(V))
85  return true;
86 
87  // Compares can be inverted if all of their uses are being modified to use the
88  // ~V.
89  if (isa<CmpInst>(V))
90  return WillInvertAllUses;
91 
92  // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
93  // - Constant) - A` if we are willing to invert all of the uses.
94  if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
95  if (BO->getOpcode() == Instruction::Add ||
96  BO->getOpcode() == Instruction::Sub)
97  if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
98  return WillInvertAllUses;
99 
100  return false;
101 }
102 
103 
104 /// \brief Specific patterns of overflow check idioms that we match.
112 
114 };
115 
116 /// \brief Returns the OverflowCheckFlavor corresponding to a overflow_with_op
117 /// intrinsic.
118 static inline OverflowCheckFlavor
120  switch (ID) {
121  default:
122  return OCF_INVALID;
123  case Intrinsic::uadd_with_overflow:
124  return OCF_UNSIGNED_ADD;
125  case Intrinsic::sadd_with_overflow:
126  return OCF_SIGNED_ADD;
127  case Intrinsic::usub_with_overflow:
128  return OCF_UNSIGNED_SUB;
129  case Intrinsic::ssub_with_overflow:
130  return OCF_SIGNED_SUB;
131  case Intrinsic::umul_with_overflow:
132  return OCF_UNSIGNED_MUL;
133  case Intrinsic::smul_with_overflow:
134  return OCF_SIGNED_MUL;
135  }
136 }
137 
138 /// \brief An IRBuilder inserter that adds new instructions to the instcombine
139 /// worklist.
141  : public IRBuilderDefaultInserter<true> {
142  InstCombineWorklist &Worklist;
143  AssumptionCache *AC;
144 
145 public:
147  : Worklist(WL), AC(AC) {}
148 
150  BasicBlock::iterator InsertPt) const {
151  IRBuilderDefaultInserter<true>::InsertHelper(I, Name, BB, InsertPt);
152  Worklist.Add(I);
153 
154  using namespace llvm::PatternMatch;
155  if (match(I, m_Intrinsic<Intrinsic::assume>()))
156  AC->registerAssumption(cast<CallInst>(I));
157  }
158 };
159 
160 /// \brief The core instruction combiner logic.
161 ///
162 /// This class provides both the logic to recursively visit instructions and
163 /// combine them, as well as the pass infrastructure for running this as part
164 /// of the LLVM pass pipeline.
166  : public InstVisitor<InstCombiner, Instruction *> {
167  // FIXME: These members shouldn't be public.
168 public:
169  /// \brief A worklist of the instructions that need to be simplified.
171 
172  /// \brief An IRBuilder that automatically inserts new instructions into the
173  /// worklist.
176 
177 private:
178  // Mode in which we are running the combiner.
179  const bool MinimizeSize;
180 
181  AliasAnalysis *AA;
182 
183  // Required analyses.
184  // FIXME: These can never be null and should be references.
185  AssumptionCache *AC;
186  TargetLibraryInfo *TLI;
187  DominatorTree *DT;
188  const DataLayout &DL;
189 
190  // Optional analyses. When non-null, these can both be used to do better
191  // combining and will be updated to reflect any changes.
192  LoopInfo *LI;
193 
194  bool MadeIRChange;
195 
196 public:
198  bool MinimizeSize, AliasAnalysis *AA,
200  DominatorTree *DT, const DataLayout &DL, LoopInfo *LI)
201  : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
202  AA(AA), AC(AC), TLI(TLI), DT(DT), DL(DL), LI(LI), MadeIRChange(false) {}
203 
204  /// \brief Run the combiner over the entire worklist until it is empty.
205  ///
206  /// \returns true if the IR is changed.
207  bool run();
208 
209  AssumptionCache *getAssumptionCache() const { return AC; }
210 
211  const DataLayout &getDataLayout() const { return DL; }
212 
213  DominatorTree *getDominatorTree() const { return DT; }
214 
215  LoopInfo *getLoopInfo() const { return LI; }
216 
217  TargetLibraryInfo *getTargetLibraryInfo() const { return TLI; }
218 
219  // Visitation implementation - Implement instruction combining for different
220  // instruction types. The semantics are as follows:
221  // Return Value:
222  // null - No change was made
223  // I - Change was made, I is still valid, I may be dead though
224  // otherwise - Change was made, replace I with returned instruction
225  //
226  Instruction *visitAdd(BinaryOperator &I);
227  Instruction *visitFAdd(BinaryOperator &I);
228  Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
229  Instruction *visitSub(BinaryOperator &I);
230  Instruction *visitFSub(BinaryOperator &I);
231  Instruction *visitMul(BinaryOperator &I);
232  Value *foldFMulConst(Instruction *FMulOrDiv, Constant *C,
233  Instruction *InsertBefore);
234  Instruction *visitFMul(BinaryOperator &I);
235  Instruction *visitURem(BinaryOperator &I);
236  Instruction *visitSRem(BinaryOperator &I);
237  Instruction *visitFRem(BinaryOperator &I);
238  bool SimplifyDivRemOfSelect(BinaryOperator &I);
239  Instruction *commonRemTransforms(BinaryOperator &I);
240  Instruction *commonIRemTransforms(BinaryOperator &I);
241  Instruction *commonDivTransforms(BinaryOperator &I);
242  Instruction *commonIDivTransforms(BinaryOperator &I);
243  Instruction *visitUDiv(BinaryOperator &I);
244  Instruction *visitSDiv(BinaryOperator &I);
245  Instruction *visitFDiv(BinaryOperator &I);
246  Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
247  Value *FoldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS);
248  Value *FoldAndOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
249  Instruction *visitAnd(BinaryOperator &I);
250  Value *FoldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction *CxtI);
251  Value *FoldOrOfFCmps(FCmpInst *LHS, FCmpInst *RHS);
252  Instruction *FoldOrWithConstants(BinaryOperator &I, Value *Op, Value *A,
253  Value *B, Value *C);
254  Instruction *FoldXorWithConstants(BinaryOperator &I, Value *Op, Value *A,
255  Value *B, Value *C);
256  Instruction *visitOr(BinaryOperator &I);
257  Instruction *visitXor(BinaryOperator &I);
258  Instruction *visitShl(BinaryOperator &I);
259  Instruction *visitAShr(BinaryOperator &I);
260  Instruction *visitLShr(BinaryOperator &I);
261  Instruction *commonShiftTransforms(BinaryOperator &I);
262  Instruction *FoldFCmp_IntToFP_Cst(FCmpInst &I, Instruction *LHSI,
263  Constant *RHSC);
264  Instruction *FoldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
265  GlobalVariable *GV, CmpInst &ICI,
266  ConstantInt *AndCst = nullptr);
267  Instruction *visitFCmpInst(FCmpInst &I);
268  Instruction *visitICmpInst(ICmpInst &I);
269  Instruction *visitICmpInstWithCastAndCast(ICmpInst &ICI);
270  Instruction *visitICmpInstWithInstAndIntCst(ICmpInst &ICI, Instruction *LHS,
271  ConstantInt *RHS);
272  Instruction *FoldICmpDivCst(ICmpInst &ICI, BinaryOperator *DivI,
273  ConstantInt *DivRHS);
274  Instruction *FoldICmpShrCst(ICmpInst &ICI, BinaryOperator *DivI,
275  ConstantInt *DivRHS);
276  Instruction *FoldICmpCstShrCst(ICmpInst &I, Value *Op, Value *A,
277  ConstantInt *CI1, ConstantInt *CI2);
278  Instruction *FoldICmpCstShlCst(ICmpInst &I, Value *Op, Value *A,
279  ConstantInt *CI1, ConstantInt *CI2);
280  Instruction *FoldICmpAddOpCst(Instruction &ICI, Value *X, ConstantInt *CI,
281  ICmpInst::Predicate Pred);
282  Instruction *FoldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
284  Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
285  BinaryOperator &I);
286  Instruction *commonCastTransforms(CastInst &CI);
287  Instruction *commonPointerCastTransforms(CastInst &CI);
288  Instruction *visitTrunc(TruncInst &CI);
289  Instruction *visitZExt(ZExtInst &CI);
290  Instruction *visitSExt(SExtInst &CI);
291  Instruction *visitFPTrunc(FPTruncInst &CI);
292  Instruction *visitFPExt(CastInst &CI);
293  Instruction *visitFPToUI(FPToUIInst &FI);
294  Instruction *visitFPToSI(FPToSIInst &FI);
295  Instruction *visitUIToFP(CastInst &CI);
296  Instruction *visitSIToFP(CastInst &CI);
297  Instruction *visitPtrToInt(PtrToIntInst &CI);
298  Instruction *visitIntToPtr(IntToPtrInst &CI);
299  Instruction *visitBitCast(BitCastInst &CI);
300  Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
301  Instruction *FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
302  Instruction *FoldSelectIntoOp(SelectInst &SI, Value *, Value *);
303  Instruction *FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
304  Value *A, Value *B, Instruction &Outer,
305  SelectPatternFlavor SPF2, Value *C);
306  Instruction *FoldItoFPtoI(Instruction &FI);
307  Instruction *visitSelectInst(SelectInst &SI);
308  Instruction *visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
309  Instruction *visitCallInst(CallInst &CI);
310  Instruction *visitInvokeInst(InvokeInst &II);
311 
312  Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
313  Instruction *visitPHINode(PHINode &PN);
314  Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
315  Instruction *visitAllocaInst(AllocaInst &AI);
316  Instruction *visitAllocSite(Instruction &FI);
317  Instruction *visitFree(CallInst &FI);
318  Instruction *visitLoadInst(LoadInst &LI);
319  Instruction *visitStoreInst(StoreInst &SI);
320  Instruction *visitBranchInst(BranchInst &BI);
321  Instruction *visitSwitchInst(SwitchInst &SI);
322  Instruction *visitReturnInst(ReturnInst &RI);
323  Instruction *visitInsertValueInst(InsertValueInst &IV);
324  Instruction *visitInsertElementInst(InsertElementInst &IE);
325  Instruction *visitExtractElementInst(ExtractElementInst &EI);
326  Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
327  Instruction *visitExtractValueInst(ExtractValueInst &EV);
328  Instruction *visitLandingPadInst(LandingPadInst &LI);
329 
330  // visitInstruction - Specify what to return for unhandled instructions...
331  Instruction *visitInstruction(Instruction &I) { return nullptr; }
332 
333  // True when DB dominates all uses of DI execpt UI.
334  // UI must be in the same block as DI.
335  // The routine checks that the DI parent and DB are different.
336  bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
337  const BasicBlock *DB) const;
338 
339  // Replace select with select operand SIOpd in SI-ICmp sequence when possible
340  bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
341  const unsigned SIOpd);
342 
343 private:
344  bool ShouldChangeType(Type *From, Type *To) const;
345  Value *dyn_castNegVal(Value *V) const;
346  Value *dyn_castFNegVal(Value *V, bool NoSignedZero = false) const;
347  Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
348  SmallVectorImpl<Value *> &NewIndices);
349  Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
350 
351  /// \brief Classify whether a cast is worth optimizing.
352  ///
353  /// Returns true if the cast from "V to Ty" actually results in any code
354  /// being generated and is interesting to optimize out. If the cast can be
355  /// eliminated by some other simple transformation, we prefer to do the
356  /// simplification first.
357  bool ShouldOptimizeCast(Instruction::CastOps opcode, const Value *V,
358  Type *Ty);
359 
360  /// \brief Try to optimize a sequence of instructions checking if an operation
361  /// on LHS and RHS overflows.
362  ///
363  /// If a simplification is possible, stores the simplified result of the
364  /// operation in OperationResult and result of the overflow check in
365  /// OverflowResult, and return true. If no simplification is possible,
366  /// returns false.
367  bool OptimizeOverflowCheck(OverflowCheckFlavor OCF, Value *LHS, Value *RHS,
368  Instruction &CtxI, Value *&OperationResult,
370 
371  Instruction *visitCallSite(CallSite CS);
372  Instruction *tryOptimizeCall(CallInst *CI);
373  bool transformConstExprCastCall(CallSite CS);
374  Instruction *transformCallThroughTrampoline(CallSite CS,
375  IntrinsicInst *Tramp);
376  Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
377  bool DoXform = true);
378  Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
379  bool WillNotOverflowSignedAdd(Value *LHS, Value *RHS, Instruction &CxtI);
380  bool WillNotOverflowSignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
381  bool WillNotOverflowUnsignedSub(Value *LHS, Value *RHS, Instruction &CxtI);
382  bool WillNotOverflowSignedMul(Value *LHS, Value *RHS, Instruction &CxtI);
384  Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
385  Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
386 
387 public:
388  /// \brief Inserts an instruction \p New before instruction \p Old
389  ///
390  /// Also adds the new instruction to the worklist and returns \p New so that
391  /// it is suitable for use as the return from the visitation patterns.
393  assert(New && !New->getParent() &&
394  "New instruction already inserted into a basic block!");
395  BasicBlock *BB = Old.getParent();
396  BB->getInstList().insert(&Old, New); // Insert inst
397  Worklist.Add(New);
398  return New;
399  }
400 
401  /// \brief Same as InsertNewInstBefore, but also sets the debug loc.
403  New->setDebugLoc(Old.getDebugLoc());
404  return InsertNewInstBefore(New, Old);
405  }
406 
407  /// \brief A combiner-aware RAUW-like routine.
408  ///
409  /// This method is to be used when an instruction is found to be dead,
410  /// replacable with another preexisting expression. Here we add all uses of
411  /// I to the worklist, replace all uses of I with the new value, then return
412  /// I, so that the inst combiner will know that I was modified.
414  // If there are no uses to replace, then we return nullptr to indicate that
415  // no changes were made to the program.
416  if (I.use_empty()) return nullptr;
417 
418  Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
419 
420  // If we are replacing the instruction with itself, this must be in a
421  // segment of unreachable code, so just clobber the instruction.
422  if (&I == V)
423  V = UndefValue::get(I.getType());
424 
425  DEBUG(dbgs() << "IC: Replacing " << I << "\n"
426  << " with " << *V << '\n');
427 
428  I.replaceAllUsesWith(V);
429  return &I;
430  }
431 
432  /// Creates a result tuple for an overflow intrinsic \p II with a given
433  /// \p Result and a constant \p Overflow value.
435  Constant *Overflow) {
436  Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
437  StructType *ST = cast<StructType>(II->getType());
439  return InsertValueInst::Create(Struct, Result, 0);
440  }
441 
442  /// \brief Combiner aware instruction erasure.
443  ///
444  /// When dealing with an instruction that has side effects or produces a void
445  /// value, we can't rely on DCE to delete the instruction. Instead, visit
446  /// methods should return the value returned by this function.
448  DEBUG(dbgs() << "IC: ERASE " << I << '\n');
449 
450  assert(I.use_empty() && "Cannot erase instruction that is used!");
451  // Make sure that we reprocess all operands now that we reduced their
452  // use counts.
453  if (I.getNumOperands() < 8) {
454  for (User::op_iterator i = I.op_begin(), e = I.op_end(); i != e; ++i)
455  if (Instruction *Op = dyn_cast<Instruction>(*i))
456  Worklist.Add(Op);
457  }
458  Worklist.Remove(&I);
459  I.eraseFromParent();
460  MadeIRChange = true;
461  return nullptr; // Don't do anything with FI
462  }
463 
464  void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
465  unsigned Depth, Instruction *CxtI) const {
466  return llvm::computeKnownBits(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
467  DT);
468  }
469 
470  bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth = 0,
471  Instruction *CxtI = nullptr) const {
472  return llvm::MaskedValueIsZero(V, Mask, DL, Depth, AC, CxtI, DT);
473  }
474  unsigned ComputeNumSignBits(Value *Op, unsigned Depth = 0,
475  Instruction *CxtI = nullptr) const {
476  return llvm::ComputeNumSignBits(Op, DL, Depth, AC, CxtI, DT);
477  }
478  void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne,
479  unsigned Depth = 0, Instruction *CxtI = nullptr) const {
480  return llvm::ComputeSignBit(V, KnownZero, KnownOne, DL, Depth, AC, CxtI,
481  DT);
482  }
484  const Instruction *CxtI) {
485  return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, AC, CxtI, DT);
486  }
488  const Instruction *CxtI) {
489  return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, AC, CxtI, DT);
490  }
491 
492 private:
493  /// \brief Performs a few simplifications for operators which are associative
494  /// or commutative.
495  bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
496 
497  /// \brief Tries to simplify binary operations which some other binary
498  /// operation distributes over.
499  ///
500  /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
501  /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
502  /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
503  /// value, or null if it didn't simplify.
504  Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
505 
506  /// \brief Attempts to replace V with a simpler value based on the demanded
507  /// bits.
508  Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, APInt &KnownZero,
509  APInt &KnownOne, unsigned Depth,
510  Instruction *CxtI);
511  bool SimplifyDemandedBits(Use &U, APInt DemandedMask, APInt &KnownZero,
512  APInt &KnownOne, unsigned Depth = 0);
513  /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
514  /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
515  Value *SimplifyShrShlDemandedBits(Instruction *Lsr, Instruction *Sftl,
516  APInt DemandedMask, APInt &KnownZero,
517  APInt &KnownOne);
518 
519  /// \brief Tries to simplify operands to an integer instruction based on its
520  /// demanded bits.
521  bool SimplifyDemandedInstructionBits(Instruction &Inst);
522 
523  Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
524  APInt &UndefElts, unsigned Depth = 0);
525 
526  Value *SimplifyVectorOp(BinaryOperator &Inst);
527  Value *SimplifyBSwap(BinaryOperator &Inst);
528 
529  // FoldOpIntoPhi - Given a binary operator, cast instruction, or select
530  // which has a PHI node as operand #0, see if we can fold the instruction
531  // into the PHI (which is only possible if all operands to the PHI are
532  // constants).
533  //
534  Instruction *FoldOpIntoPhi(Instruction &I);
535 
536  /// \brief Try to rotate an operation below a PHI node, using PHI nodes for
537  /// its operands.
538  Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
539  Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
540  Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
541  Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
542 
543  Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
544  ConstantInt *AndRHS, BinaryOperator &TheAnd);
545 
546  Value *FoldLogicalPlusAnd(Value *LHS, Value *RHS, ConstantInt *Mask,
547  bool isSub, Instruction &I);
548  Value *InsertRangeTest(Value *V, Constant *Lo, Constant *Hi, bool isSigned,
549  bool Inside);
550  Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
551  Instruction *MatchBSwap(BinaryOperator &I);
552  bool SimplifyStoreAtEndOfBlock(StoreInst &SI);
553  Instruction *SimplifyMemTransfer(MemIntrinsic *MI);
554  Instruction *SimplifyMemSet(MemSetInst *MI);
555 
556  Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
557 
558  /// \brief Returns a value X such that Val = X * Scale, or null if none.
559  ///
560  /// If the multiplication is known not to overflow then NoSignedWrap is set.
561  Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
562 };
563 
564 } // end namespace llvm.
565 
566 #undef DEBUG_TYPE
567 
568 #endif
IRBuilder< true, TargetFolder, InstCombineIRInserter > BuilderTy
An IRBuilder that automatically inserts new instructions into the worklist.
Value * EmitGEPOffset(IRBuilderTy *Builder, const DataLayout &DL, User *GEP, bool NoAssumptions=false)
EmitGEPOffset - Given a getelementptr instruction/constantexpr, emit the code necessary to compute th...
Definition: Local.h:193
ReturnInst - Return a value (possibly void), from a function.
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Same as InsertNewInstBefore, but also sets the debug loc.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:679
static bool IsFreeToInvert(Value *V, bool WillInvertAllUses)
Return true if the specified value is free to invert (apply ~ to).
ExtractValueInst - This instruction extracts a struct member or array element value from an aggregate...
Base class for instruction visitors.
Definition: InstVisitor.h:81
OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS, const Instruction *CxtI)
This class represents zero extension of integer types.
unsigned getNumOperands() const
Definition: User.h:138
InstCombineIRInserter(InstCombineWorklist &WL, AssumptionCache *AC)
CallInst - This class represents a function call, abstracting a target machine's calling convention...
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
const DataLayout & getDataLayout() const
A cache of .assume calls within a function.
ShuffleVectorInst - This instruction constructs a fixed permutation of two input vectors.
MemSetInst - This class wraps the llvm.memset intrinsic.
This class represents a sign extension of integer types.
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
Hexagon Common GEP
static Constant * getSub(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2269
static OverflowCheckFlavor IntrinsicIDToOverflowCheckFlavor(unsigned ID)
Returns the OverflowCheckFlavor corresponding to a overflow_with_op intrinsic.
op_iterator op_begin()
Definition: User.h:183
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, unsigned Depth, Instruction *CxtI) const
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2258
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:41
InstCombiner(InstCombineWorklist &Worklist, BuilderTy *Builder, bool MinimizeSize, AliasAnalysis *AA, AssumptionCache *AC, TargetLibraryInfo *TLI, DominatorTree *DT, const DataLayout &DL, LoopInfo *LI)
This class represents a conversion between pointers from one address space to another.
static unsigned getComplexity(Value *V)
Assign a complexity or rank value to LLVM Values.
SelectInst - This class represents the LLVM 'select' instruction.
OverflowCheckFlavor
Specific patterns of overflow check idioms that we match.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:389
StructType - Class to represent struct types.
Definition: DerivedTypes.h:191
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:517
The core instruction combiner logic.
static Constant * AddOne(Constant *C)
Add one to a Constant.
bool MaskedValueIsZero(Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
MaskedValueIsZero - Return true if 'V & Mask' is known to be zero.
OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS, const Instruction *CxtI)
AssumptionCache * getAssumptionCache() const
#define false
Definition: ConvertUTF.c:65
This class represents a cast from a pointer to an integer.
DominatorTree * getDominatorTree() const
This instruction compares its operands according to the predicate given to the constructor.
This class represents a no-op cast from one type to another.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
Instruction * visitInstruction(Instruction &I)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
This class represents a cast from floating point to signed integer.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
This class represents a truncation of integer types.
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
GetElementPtrInst - an instruction for type-safe pointer arithmetic to access elements of arrays and ...
Definition: Instructions.h:830
OverflowResult computeOverflowForUnsignedAdd(Value *LHS, Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
InsertElementInst - This instruction inserts a single (scalar) element into a VectorType value...
LandingPadInst - The landingpad instruction holds all of the information necessary to generate correc...
void setDebugLoc(DebugLoc Loc)
setDebugLoc - Set the debug location information for this instruction.
Definition: Instruction.h:227
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
BranchInst - Conditional or Unconditional Branch instruction.
This is an important base class in LLVM.
Definition: Constant.h:41
Instruction * ReplaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB, BasicBlock::iterator InsertPt) const
const DebugLoc & getDebugLoc() const
getDebugLoc - Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:230
op_iterator op_end()
Definition: User.h:185
const InstListType & getInstList() const
Return the underlying instruction list container.
Definition: BasicBlock.h:252
OverflowResult computeOverflowForUnsignedMul(Value *LHS, Value *RHS, const DataLayout &DL, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:697
iterator insert(iterator where, NodeTy *New)
Definition: ilist.h:412
Instruction * CreateOverflowTuple(IntrinsicInst *II, Value *Result, Constant *Overflow)
Creates a result tuple for an overflow intrinsic II with a given Result and a constant Overflow value...
#define LLVM_LIBRARY_VISIBILITY
LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked into a shared library...
Definition: Compiler.h:110
static bool isNot(const Value *V)
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1008
void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
ComputeSignBit - Determine whether the sign bit is known to be zero or one.
This class represents a cast from an integer to a pointer.
InstCombineWorklist - This is the worklist management logic for InstCombine.
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
InstCombineWorklist & Worklist
A worklist of the instructions that need to be simplified.
This provides the default implementation of the IRBuilder 'InsertHelper' method that is called whenev...
Definition: IRBuilder.h:41
MemIntrinsic - This is the common base class for memset/memcpy/memmove.
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
LoopInfo * getLoopInfo() const
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
SelectPatternFlavor
Specific patterns of select instructions we can match.
Provides information about what library functions are available for the current target.
unsigned ComputeNumSignBits(Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
ComputeNumSignBits - Return the number of times the sign bit of the register is replicated into the o...
This class represents a cast from floating point to unsigned integer.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
static bool isNeg(const Value *V)
Check if the given Value is a NEG, FNeg, or NOT instruction.
Class for arbitrary precision integers.
Definition: APInt.h:73
TargetLibraryInfo * getTargetLibraryInfo() const
void InsertHelper(Instruction *I, const Twine &Name, BasicBlock *BB, BasicBlock::iterator InsertPt) const
Definition: IRBuilder.h:43
OverflowResult
static bool isFNeg(const Value *V, bool IgnoreZeroSign=false)
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
#define I(x, y, z)
Definition: MD5.cpp:54
ExtractElementInst - This instruction extracts a single (scalar) element from a VectorType value...
Instruction * InsertNewInstBefore(Instruction *New, Instruction &Old)
Inserts an instruction New before instruction Old.
void ComputeSignBit(Value *V, bool &KnownZero, bool &KnownOne, unsigned Depth=0, Instruction *CxtI=nullptr) const
bool MaskedValueIsZero(Value *V, const APInt &Mask, unsigned Depth=0, Instruction *CxtI=nullptr) const
SwitchInst - Multiway switch.
bool use_empty() const
Definition: Value.h:275
This class represents a truncation of floating point types.
unsigned ComputeNumSignBits(Value *Op, unsigned Depth=0, Instruction *CxtI=nullptr) const
LLVM Value Representation.
Definition: Value.h:69
An IRBuilder inserter that adds new instructions to the instcombine worklist.
Instruction * EraseInstFromFunction(Instruction &I)
Combiner aware instruction erasure.
InvokeInst - Invoke instruction.
#define DEBUG(X)
Definition: Debug.h:92
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
const BasicBlock * getParent() const
Definition: Instruction.h:72
static Constant * SubOne(Constant *C)
Subtract one from a Constant.
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:37
AllocaInst - an instruction to allocate memory on the stack.
Definition: Instructions.h:76
InsertValueInst - This instruction inserts a struct field of array element value into an aggregate va...