1 //===- InstCombineInternal.h - InstCombine pass internals -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \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 
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/InstructionSimplify.h"
20 #include "llvm/Analysis/TargetFolder.h"
21 #include "llvm/Analysis/ValueTracking.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/InstVisitor.h"
24 #include "llvm/IR/PatternMatch.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/KnownBits.h"
27 #include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
28 #include "llvm/Transforms/InstCombine/InstCombiner.h"
29 #include "llvm/Transforms/Utils/Local.h"
30 #include <cassert>
31 
32 #define DEBUG_TYPE "instcombine"
33 
34 using namespace llvm::PatternMatch;
35 
36 // As a default, let's assume that we want to be aggressive,
37 // and attempt to traverse with no limits in attempt to sink negation.
38 static constexpr unsigned NegatorDefaultMaxDepth = ~0U;
39 
40 // Let's guesstimate that most often we will end up visiting/producing
41 // fairly small number of new instructions.
42 static constexpr unsigned NegatorMaxNodesSSO = 16;
43 
44 namespace llvm {
45 
46 class AAResults;
47 class APInt;
48 class AssumptionCache;
49 class BlockFrequencyInfo;
50 class DataLayout;
51 class DominatorTree;
52 class GEPOperator;
53 class GlobalVariable;
54 class LoopInfo;
55 class OptimizationRemarkEmitter;
56 class ProfileSummaryInfo;
57 class TargetLibraryInfo;
58 class User;
59 
60 class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
61     : public InstCombiner,
62       public InstVisitor<InstCombinerImpl, Instruction *> {
63 public:
InstCombinerImpl(InstCombineWorklist & Worklist,BuilderTy & Builder,bool MinimizeSize,AAResults * AA,AssumptionCache & AC,TargetLibraryInfo & TLI,TargetTransformInfo & TTI,DominatorTree & DT,OptimizationRemarkEmitter & ORE,BlockFrequencyInfo * BFI,ProfileSummaryInfo * PSI,const DataLayout & DL,LoopInfo * LI)64   InstCombinerImpl(InstCombineWorklist &Worklist, BuilderTy &Builder,
65                    bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
66                    TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
67                    DominatorTree &DT, OptimizationRemarkEmitter &ORE,
68                    BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
69                    const DataLayout &DL, LoopInfo *LI)
70       : InstCombiner(Worklist, Builder, MinimizeSize, AA, AC, TLI, TTI, DT, ORE,
71                      BFI, PSI, DL, LI) {}
72 
~InstCombinerImpl()73   virtual ~InstCombinerImpl() {}
74 
75   /// Run the combiner over the entire worklist until it is empty.
76   ///
77   /// \returns true if the IR is changed.
78   bool run();
79 
80   // Visitation implementation - Implement instruction combining for different
81   // instruction types.  The semantics are as follows:
82   // Return Value:
83   //    null        - No change was made
84   //     I          - Change was made, I is still valid, I may be dead though
85   //   otherwise    - Change was made, replace I with returned instruction
86   //
87   Instruction *visitFNeg(UnaryOperator &I);
88   Instruction *visitAdd(BinaryOperator &I);
89   Instruction *visitFAdd(BinaryOperator &I);
90   Value *OptimizePointerDifference(
91       Value *LHS, Value *RHS, Type *Ty, bool isNUW);
92   Instruction *visitSub(BinaryOperator &I);
93   Instruction *visitFSub(BinaryOperator &I);
94   Instruction *visitMul(BinaryOperator &I);
95   Instruction *visitFMul(BinaryOperator &I);
96   Instruction *visitURem(BinaryOperator &I);
97   Instruction *visitSRem(BinaryOperator &I);
98   Instruction *visitFRem(BinaryOperator &I);
99   bool simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I);
100   Instruction *commonIRemTransforms(BinaryOperator &I);
101   Instruction *commonIDivTransforms(BinaryOperator &I);
102   Instruction *visitUDiv(BinaryOperator &I);
103   Instruction *visitSDiv(BinaryOperator &I);
104   Instruction *visitFDiv(BinaryOperator &I);
105   Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
106   Instruction *visitAnd(BinaryOperator &I);
107   Instruction *visitOr(BinaryOperator &I);
108   bool sinkNotIntoOtherHandOfAndOrOr(BinaryOperator &I);
109   Instruction *visitXor(BinaryOperator &I);
110   Instruction *visitShl(BinaryOperator &I);
111   Value *reassociateShiftAmtsOfTwoSameDirectionShifts(
112       BinaryOperator *Sh0, const SimplifyQuery &SQ,
113       bool AnalyzeForSignBitExtraction = false);
114   Instruction *canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(
115       BinaryOperator &I);
116   Instruction *foldVariableSignZeroExtensionOfVariableHighBitExtract(
117       BinaryOperator &OldAShr);
118   Instruction *visitAShr(BinaryOperator &I);
119   Instruction *visitLShr(BinaryOperator &I);
120   Instruction *commonShiftTransforms(BinaryOperator &I);
121   Instruction *visitFCmpInst(FCmpInst &I);
122   CmpInst *canonicalizeICmpPredicate(CmpInst &I);
123   Instruction *visitICmpInst(ICmpInst &I);
124   Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
125                                    BinaryOperator &I);
126   Instruction *commonCastTransforms(CastInst &CI);
127   Instruction *commonPointerCastTransforms(CastInst &CI);
128   Instruction *visitTrunc(TruncInst &CI);
129   Instruction *visitZExt(ZExtInst &CI);
130   Instruction *visitSExt(SExtInst &CI);
131   Instruction *visitFPTrunc(FPTruncInst &CI);
132   Instruction *visitFPExt(CastInst &CI);
133   Instruction *visitFPToUI(FPToUIInst &FI);
134   Instruction *visitFPToSI(FPToSIInst &FI);
135   Instruction *visitUIToFP(CastInst &CI);
136   Instruction *visitSIToFP(CastInst &CI);
137   Instruction *visitPtrToInt(PtrToIntInst &CI);
138   Instruction *visitIntToPtr(IntToPtrInst &CI);
139   Instruction *visitBitCast(BitCastInst &CI);
140   Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
141   Instruction *foldItoFPtoI(CastInst &FI);
142   Instruction *visitSelectInst(SelectInst &SI);
143   Instruction *visitCallInst(CallInst &CI);
144   Instruction *visitInvokeInst(InvokeInst &II);
145   Instruction *visitCallBrInst(CallBrInst &CBI);
146 
147   Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
148   Instruction *visitPHINode(PHINode &PN);
149   Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
150   Instruction *visitAllocaInst(AllocaInst &AI);
151   Instruction *visitAllocSite(Instruction &FI);
152   Instruction *visitFree(CallInst &FI);
153   Instruction *visitLoadInst(LoadInst &LI);
154   Instruction *visitStoreInst(StoreInst &SI);
155   Instruction *visitAtomicRMWInst(AtomicRMWInst &SI);
156   Instruction *visitUnconditionalBranchInst(BranchInst &BI);
157   Instruction *visitBranchInst(BranchInst &BI);
158   Instruction *visitFenceInst(FenceInst &FI);
159   Instruction *visitSwitchInst(SwitchInst &SI);
160   Instruction *visitReturnInst(ReturnInst &RI);
161   Instruction *visitUnreachableInst(UnreachableInst &I);
162   Instruction *
163   foldAggregateConstructionIntoAggregateReuse(InsertValueInst &OrigIVI);
164   Instruction *visitInsertValueInst(InsertValueInst &IV);
165   Instruction *visitInsertElementInst(InsertElementInst &IE);
166   Instruction *visitExtractElementInst(ExtractElementInst &EI);
167   Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
168   Instruction *visitExtractValueInst(ExtractValueInst &EV);
169   Instruction *visitLandingPadInst(LandingPadInst &LI);
170   Instruction *visitVAEndInst(VAEndInst &I);
171   Value *pushFreezeToPreventPoisonFromPropagating(FreezeInst &FI);
172   bool freezeDominatedUses(FreezeInst &FI);
173   Instruction *visitFreeze(FreezeInst &I);
174 
175   /// Specify what to return for unhandled instructions.
visitInstruction(Instruction & I)176   Instruction *visitInstruction(Instruction &I) { return nullptr; }
177 
178   /// True when DB dominates all uses of DI except UI.
179   /// UI must be in the same block as DI.
180   /// The routine checks that the DI parent and DB are different.
181   bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
182                         const BasicBlock *DB) const;
183 
184   /// Try to replace select with select operand SIOpd in SI-ICmp sequence.
185   bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
186                                  const unsigned SIOpd);
187 
188   LoadInst *combineLoadToNewType(LoadInst &LI, Type *NewTy,
189                                  const Twine &Suffix = "");
190 
191 private:
192   void annotateAnyAllocSite(CallBase &Call, const TargetLibraryInfo *TLI);
193   bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
194   bool shouldChangeType(Type *From, Type *To) const;
195   Value *dyn_castNegVal(Value *V) const;
196   Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
197                             SmallVectorImpl<Value *> &NewIndices);
198 
199   /// Classify whether a cast is worth optimizing.
200   ///
201   /// This is a helper to decide whether the simplification of
202   /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
203   ///
204   /// \param CI The cast we are interested in.
205   ///
206   /// \return true if this cast actually results in any code being generated and
207   /// if it cannot already be eliminated by some other transformation.
208   bool shouldOptimizeCast(CastInst *CI);
209 
210   /// Try to optimize a sequence of instructions checking if an operation
211   /// on LHS and RHS overflows.
212   ///
213   /// If this overflow check is done via one of the overflow check intrinsics,
214   /// then CtxI has to be the call instruction calling that intrinsic.  If this
215   /// overflow check is done by arithmetic followed by a compare, then CtxI has
216   /// to be the arithmetic instruction.
217   ///
218   /// If a simplification is possible, stores the simplified result of the
219   /// operation in OperationResult and result of the overflow check in
220   /// OverflowResult, and return true.  If no simplification is possible,
221   /// returns false.
222   bool OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, bool IsSigned,
223                              Value *LHS, Value *RHS,
224                              Instruction &CtxI, Value *&OperationResult,
225                              Constant *&OverflowResult);
226 
227   Instruction *visitCallBase(CallBase &Call);
228   Instruction *tryOptimizeCall(CallInst *CI);
229   bool transformConstExprCastCall(CallBase &Call);
230   Instruction *transformCallThroughTrampoline(CallBase &Call,
231                                               IntrinsicInst &Tramp);
232 
233   Value *simplifyMaskedLoad(IntrinsicInst &II);
234   Instruction *simplifyMaskedStore(IntrinsicInst &II);
235   Instruction *simplifyMaskedGather(IntrinsicInst &II);
236   Instruction *simplifyMaskedScatter(IntrinsicInst &II);
237 
238   /// Transform (zext icmp) to bitwise / integer operations in order to
239   /// eliminate it.
240   ///
241   /// \param ICI The icmp of the (zext icmp) pair we are interested in.
242   /// \parem CI The zext of the (zext icmp) pair we are interested in.
243   /// \param DoTransform Pass false to just test whether the given (zext icmp)
244   /// would be transformed. Pass true to actually perform the transformation.
245   ///
246   /// \return null if the transformation cannot be performed. If the
247   /// transformation can be performed the new instruction that replaces the
248   /// (zext icmp) pair will be returned (if \p DoTransform is false the
249   /// unmodified \p ICI will be returned in this case).
250   Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI,
251                                  bool DoTransform = true);
252 
253   Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
254 
willNotOverflowSignedAdd(const Value * LHS,const Value * RHS,const Instruction & CxtI)255   bool willNotOverflowSignedAdd(const Value *LHS, const Value *RHS,
256                                 const Instruction &CxtI) const {
257     return computeOverflowForSignedAdd(LHS, RHS, &CxtI) ==
258            OverflowResult::NeverOverflows;
259   }
260 
willNotOverflowUnsignedAdd(const Value * LHS,const Value * RHS,const Instruction & CxtI)261   bool willNotOverflowUnsignedAdd(const Value *LHS, const Value *RHS,
262                                   const Instruction &CxtI) const {
263     return computeOverflowForUnsignedAdd(LHS, RHS, &CxtI) ==
264            OverflowResult::NeverOverflows;
265   }
266 
willNotOverflowAdd(const Value * LHS,const Value * RHS,const Instruction & CxtI,bool IsSigned)267   bool willNotOverflowAdd(const Value *LHS, const Value *RHS,
268                           const Instruction &CxtI, bool IsSigned) const {
269     return IsSigned ? willNotOverflowSignedAdd(LHS, RHS, CxtI)
270                     : willNotOverflowUnsignedAdd(LHS, RHS, CxtI);
271   }
272 
willNotOverflowSignedSub(const Value * LHS,const Value * RHS,const Instruction & CxtI)273   bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS,
274                                 const Instruction &CxtI) const {
275     return computeOverflowForSignedSub(LHS, RHS, &CxtI) ==
276            OverflowResult::NeverOverflows;
277   }
278 
willNotOverflowUnsignedSub(const Value * LHS,const Value * RHS,const Instruction & CxtI)279   bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS,
280                                   const Instruction &CxtI) const {
281     return computeOverflowForUnsignedSub(LHS, RHS, &CxtI) ==
282            OverflowResult::NeverOverflows;
283   }
284 
willNotOverflowSub(const Value * LHS,const Value * RHS,const Instruction & CxtI,bool IsSigned)285   bool willNotOverflowSub(const Value *LHS, const Value *RHS,
286                           const Instruction &CxtI, bool IsSigned) const {
287     return IsSigned ? willNotOverflowSignedSub(LHS, RHS, CxtI)
288                     : willNotOverflowUnsignedSub(LHS, RHS, CxtI);
289   }
290 
willNotOverflowSignedMul(const Value * LHS,const Value * RHS,const Instruction & CxtI)291   bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS,
292                                 const Instruction &CxtI) const {
293     return computeOverflowForSignedMul(LHS, RHS, &CxtI) ==
294            OverflowResult::NeverOverflows;
295   }
296 
willNotOverflowUnsignedMul(const Value * LHS,const Value * RHS,const Instruction & CxtI)297   bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
298                                   const Instruction &CxtI) const {
299     return computeOverflowForUnsignedMul(LHS, RHS, &CxtI) ==
300            OverflowResult::NeverOverflows;
301   }
302 
willNotOverflowMul(const Value * LHS,const Value * RHS,const Instruction & CxtI,bool IsSigned)303   bool willNotOverflowMul(const Value *LHS, const Value *RHS,
304                           const Instruction &CxtI, bool IsSigned) const {
305     return IsSigned ? willNotOverflowSignedMul(LHS, RHS, CxtI)
306                     : willNotOverflowUnsignedMul(LHS, RHS, CxtI);
307   }
308 
willNotOverflow(BinaryOperator::BinaryOps Opcode,const Value * LHS,const Value * RHS,const Instruction & CxtI,bool IsSigned)309   bool willNotOverflow(BinaryOperator::BinaryOps Opcode, const Value *LHS,
310                        const Value *RHS, const Instruction &CxtI,
311                        bool IsSigned) const {
312     switch (Opcode) {
313     case Instruction::Add: return willNotOverflowAdd(LHS, RHS, CxtI, IsSigned);
314     case Instruction::Sub: return willNotOverflowSub(LHS, RHS, CxtI, IsSigned);
315     case Instruction::Mul: return willNotOverflowMul(LHS, RHS, CxtI, IsSigned);
316     default: llvm_unreachable("Unexpected opcode for overflow query");
317     }
318   }
319 
320   Value *EmitGEPOffset(User *GEP);
321   Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
322   Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
323   Instruction *narrowBinOp(TruncInst &Trunc);
324   Instruction *narrowMaskedBinOp(BinaryOperator &And);
325   Instruction *narrowMathIfNoOverflow(BinaryOperator &I);
326   Instruction *narrowFunnelShift(TruncInst &Trunc);
327   Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
328   Instruction *matchSAddSubSat(SelectInst &MinMax1);
329 
330   void freelyInvertAllUsersOf(Value *V);
331 
332   /// Determine if a pair of casts can be replaced by a single cast.
333   ///
334   /// \param CI1 The first of a pair of casts.
335   /// \param CI2 The second of a pair of casts.
336   ///
337   /// \return 0 if the cast pair cannot be eliminated, otherwise returns an
338   /// Instruction::CastOps value for a cast that can replace the pair, casting
339   /// CI1->getSrcTy() to CI2->getDstTy().
340   ///
341   /// \see CastInst::isEliminableCastPair
342   Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
343                                             const CastInst *CI2);
344   Value *simplifyIntToPtrRoundTripCast(Value *Val);
345 
346   Value *foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &And);
347   Value *foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &Or);
348   Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &Xor);
349 
350   Value *foldEqOfParts(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd);
351 
352   /// Optimize (fcmp)&(fcmp) or (fcmp)|(fcmp).
353   /// NOTE: Unlike most of instcombine, this returns a Value which should
354   /// already be inserted into the function.
355   Value *foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd);
356 
357   Value *foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS,
358                                        Instruction *CxtI, bool IsAnd,
359                                        bool IsLogical = false);
360   Value *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D);
361   Value *getSelectCondition(Value *A, Value *B);
362 
363   Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
364   Instruction *foldFPSignBitOps(BinaryOperator &I);
365 
366   // Optimize one of these forms:
367   //   and i1 Op, SI / select i1 Op, i1 SI, i1 false (if IsAnd = true)
368   //   or i1 Op, SI  / select i1 Op, i1 true, i1 SI  (if IsAnd = false)
369   // into simplier select instruction using isImpliedCondition.
370   Instruction *foldAndOrOfSelectUsingImpliedCond(Value *Op, SelectInst &SI,
371                                                  bool IsAnd);
372 
373 public:
374   /// Inserts an instruction \p New before instruction \p Old
375   ///
376   /// Also adds the new instruction to the worklist and returns \p New so that
377   /// it is suitable for use as the return from the visitation patterns.
InsertNewInstBefore(Instruction * New,Instruction & Old)378   Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
379     assert(New && !New->getParent() &&
380            "New instruction already inserted into a basic block!");
381     BasicBlock *BB = Old.getParent();
382     BB->getInstList().insert(Old.getIterator(), New); // Insert inst
383     Worklist.add(New);
384     return New;
385   }
386 
387   /// Same as InsertNewInstBefore, but also sets the debug loc.
InsertNewInstWith(Instruction * New,Instruction & Old)388   Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
389     New->setDebugLoc(Old.getDebugLoc());
390     return InsertNewInstBefore(New, Old);
391   }
392 
393   /// A combiner-aware RAUW-like routine.
394   ///
395   /// This method is to be used when an instruction is found to be dead,
396   /// replaceable with another preexisting expression. Here we add all uses of
397   /// I to the worklist, replace all uses of I with the new value, then return
398   /// I, so that the inst combiner will know that I was modified.
replaceInstUsesWith(Instruction & I,Value * V)399   Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
400     // If there are no uses to replace, then we return nullptr to indicate that
401     // no changes were made to the program.
402     if (I.use_empty()) return nullptr;
403 
404     Worklist.pushUsersToWorkList(I); // Add all modified instrs to worklist.
405 
406     // If we are replacing the instruction with itself, this must be in a
407     // segment of unreachable code, so just clobber the instruction.
408     if (&I == V)
409       V = UndefValue::get(I.getType());
410 
411     LLVM_DEBUG(dbgs() << "IC: Replacing " << I << "\n"
412                       << "    with " << *V << '\n');
413 
414     I.replaceAllUsesWith(V);
415     MadeIRChange = true;
416     return &I;
417   }
418 
419   /// Replace operand of instruction and add old operand to the worklist.
replaceOperand(Instruction & I,unsigned OpNum,Value * V)420   Instruction *replaceOperand(Instruction &I, unsigned OpNum, Value *V) {
421     Worklist.addValue(I.getOperand(OpNum));
422     I.setOperand(OpNum, V);
423     return &I;
424   }
425 
426   /// Replace use and add the previously used value to the worklist.
replaceUse(Use & U,Value * NewValue)427   void replaceUse(Use &U, Value *NewValue) {
428     Worklist.addValue(U);
429     U = NewValue;
430   }
431 
432   /// Create and insert the idiom we use to indicate a block is unreachable
433   /// without having to rewrite the CFG from within InstCombine.
CreateNonTerminatorUnreachable(Instruction * InsertAt)434   void CreateNonTerminatorUnreachable(Instruction *InsertAt) {
435     auto &Ctx = InsertAt->getContext();
436     new StoreInst(ConstantInt::getTrue(Ctx),
437                   UndefValue::get(Type::getInt1PtrTy(Ctx)),
438                   InsertAt);
439   }
440 
441 
442   /// 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.
eraseInstFromFunction(Instruction & I)447   Instruction *eraseInstFromFunction(Instruction &I) override {
448     LLVM_DEBUG(dbgs() << "IC: ERASE " << I << '\n');
449     assert(I.use_empty() && "Cannot erase instruction that is used!");
450     salvageDebugInfo(I);
451 
452     // Make sure that we reprocess all operands now that we reduced their
453     // use counts.
454     for (Use &Operand : I.operands())
455       if (auto *Inst = dyn_cast<Instruction>(Operand))
456         Worklist.add(Inst);
457 
458     Worklist.remove(&I);
459     I.eraseFromParent();
460     MadeIRChange = true;
461     return nullptr; // Don't do anything with FI
462   }
463 
computeKnownBits(const Value * V,KnownBits & Known,unsigned Depth,const Instruction * CxtI)464   void computeKnownBits(const Value *V, KnownBits &Known,
465                         unsigned Depth, const Instruction *CxtI) const {
466     llvm::computeKnownBits(V, Known, DL, Depth, &AC, CxtI, &DT);
467   }
468 
computeKnownBits(const Value * V,unsigned Depth,const Instruction * CxtI)469   KnownBits computeKnownBits(const Value *V, unsigned Depth,
470                              const Instruction *CxtI) const {
471     return llvm::computeKnownBits(V, DL, Depth, &AC, CxtI, &DT);
472   }
473 
474   bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
475                               unsigned Depth = 0,
476                               const Instruction *CxtI = nullptr) {
477     return llvm::isKnownToBeAPowerOfTwo(V, DL, OrZero, Depth, &AC, CxtI, &DT);
478   }
479 
480   bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0,
481                          const Instruction *CxtI = nullptr) const {
482     return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT);
483   }
484 
485   unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0,
486                               const Instruction *CxtI = nullptr) const {
487     return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
488   }
489 
computeOverflowForUnsignedMul(const Value * LHS,const Value * RHS,const Instruction * CxtI)490   OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
491                                                const Value *RHS,
492                                                const Instruction *CxtI) const {
493     return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
494   }
495 
computeOverflowForSignedMul(const Value * LHS,const Value * RHS,const Instruction * CxtI)496   OverflowResult computeOverflowForSignedMul(const Value *LHS,
497                                              const Value *RHS,
498                                              const Instruction *CxtI) const {
499     return llvm::computeOverflowForSignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
500   }
501 
computeOverflowForUnsignedAdd(const Value * LHS,const Value * RHS,const Instruction * CxtI)502   OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
503                                                const Value *RHS,
504                                                const Instruction *CxtI) const {
505     return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
506   }
507 
computeOverflowForSignedAdd(const Value * LHS,const Value * RHS,const Instruction * CxtI)508   OverflowResult computeOverflowForSignedAdd(const Value *LHS,
509                                              const Value *RHS,
510                                              const Instruction *CxtI) const {
511     return llvm::computeOverflowForSignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
512   }
513 
computeOverflowForUnsignedSub(const Value * LHS,const Value * RHS,const Instruction * CxtI)514   OverflowResult computeOverflowForUnsignedSub(const Value *LHS,
515                                                const Value *RHS,
516                                                const Instruction *CxtI) const {
517     return llvm::computeOverflowForUnsignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
518   }
519 
computeOverflowForSignedSub(const Value * LHS,const Value * RHS,const Instruction * CxtI)520   OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
521                                              const Instruction *CxtI) const {
522     return llvm::computeOverflowForSignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
523   }
524 
525   OverflowResult computeOverflow(
526       Instruction::BinaryOps BinaryOp, bool IsSigned,
527       Value *LHS, Value *RHS, Instruction *CxtI) const;
528 
529   /// Performs a few simplifications for operators which are associative
530   /// or commutative.
531   bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
532 
533   /// Tries to simplify binary operations which some other binary
534   /// operation distributes over.
535   ///
536   /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
537   /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
538   /// & (B | C) -> (A&B) | (A&C)" if this is a win).  Returns the simplified
539   /// value, or null if it didn't simplify.
540   Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
541 
542   /// Tries to simplify add operations using the definition of remainder.
543   ///
544   /// The definition of remainder is X % C = X - (X / C ) * C. The add
545   /// expression X % C0 + (( X / C0 ) % C1) * C0 can be simplified to
546   /// X % (C0 * C1)
547   Value *SimplifyAddWithRemainder(BinaryOperator &I);
548 
549   // Binary Op helper for select operations where the expression can be
550   // efficiently reorganized.
551   Value *SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS,
552                                         Value *RHS);
553 
554   /// This tries to simplify binary operations by factorizing out common terms
555   /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
556   Value *tryFactorization(BinaryOperator &, Instruction::BinaryOps, Value *,
557                           Value *, Value *, Value *);
558 
559   /// Match a select chain which produces one of three values based on whether
560   /// the LHS is less than, equal to, or greater than RHS respectively.
561   /// Return true if we matched a three way compare idiom. The LHS, RHS, Less,
562   /// Equal and Greater values are saved in the matching process and returned to
563   /// the caller.
564   bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS,
565                                ConstantInt *&Less, ConstantInt *&Equal,
566                                ConstantInt *&Greater);
567 
568   /// Attempts to replace V with a simpler value based on the demanded
569   /// bits.
570   Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, KnownBits &Known,
571                                  unsigned Depth, Instruction *CxtI);
572   bool SimplifyDemandedBits(Instruction *I, unsigned Op,
573                             const APInt &DemandedMask, KnownBits &Known,
574                             unsigned Depth = 0) override;
575 
576   /// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
577   /// bits. It also tries to handle simplifications that can be done based on
578   /// DemandedMask, but without modifying the Instruction.
579   Value *SimplifyMultipleUseDemandedBits(Instruction *I,
580                                          const APInt &DemandedMask,
581                                          KnownBits &Known,
582                                          unsigned Depth, Instruction *CxtI);
583 
584   /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
585   /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
586   Value *simplifyShrShlDemandedBits(
587       Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
588       const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known);
589 
590   /// Tries to simplify operands to an integer instruction based on its
591   /// demanded bits.
592   bool SimplifyDemandedInstructionBits(Instruction &Inst);
593 
594   virtual Value *
595   SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts,
596                              unsigned Depth = 0,
597                              bool AllowMultipleUsers = false) override;
598 
599   /// Canonicalize the position of binops relative to shufflevector.
600   Instruction *foldVectorBinop(BinaryOperator &Inst);
601   Instruction *foldVectorSelect(SelectInst &Sel);
602 
603   /// Given a binary operator, cast instruction, or select which has a PHI node
604   /// as operand #0, see if we can fold the instruction into the PHI (which is
605   /// only possible if all operands to the PHI are constants).
606   Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN);
607 
608   /// Given an instruction with a select as one operand and a constant as the
609   /// other operand, try to fold the binary operator into the select arguments.
610   /// This also works for Cast instructions, which obviously do not have a
611   /// second operand.
612   Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
613 
614   /// This is a convenience wrapper function for the above two functions.
615   Instruction *foldBinOpIntoSelectOrPhi(BinaryOperator &I);
616 
617   Instruction *foldAddWithConstant(BinaryOperator &Add);
618 
619   /// Try to rotate an operation below a PHI node, using PHI nodes for
620   /// its operands.
621   Instruction *foldPHIArgOpIntoPHI(PHINode &PN);
622   Instruction *foldPHIArgBinOpIntoPHI(PHINode &PN);
623   Instruction *foldPHIArgInsertValueInstructionIntoPHI(PHINode &PN);
624   Instruction *foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN);
625   Instruction *foldPHIArgGEPIntoPHI(PHINode &PN);
626   Instruction *foldPHIArgLoadIntoPHI(PHINode &PN);
627   Instruction *foldPHIArgZextsIntoPHI(PHINode &PN);
628 
629   /// If an integer typed PHI has only one use which is an IntToPtr operation,
630   /// replace the PHI with an existing pointer typed PHI if it exists. Otherwise
631   /// insert a new pointer typed PHI and replace the original one.
632   Instruction *foldIntegerTypedPHI(PHINode &PN);
633 
634   /// Helper function for FoldPHIArgXIntoPHI() to set debug location for the
635   /// folded operation.
636   void PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN);
637 
638   Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
639                            ICmpInst::Predicate Cond, Instruction &I);
640   Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca,
641                              const Value *Other);
642   Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
643                                             GlobalVariable *GV, CmpInst &ICI,
644                                             ConstantInt *AndCst = nullptr);
645   Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
646                                     Constant *RHSC);
647   Instruction *foldICmpAddOpConst(Value *X, const APInt &C,
648                                   ICmpInst::Predicate Pred);
649   Instruction *foldICmpWithCastOp(ICmpInst &ICI);
650 
651   Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
652   Instruction *foldICmpWithDominatingICmp(ICmpInst &Cmp);
653   Instruction *foldICmpWithConstant(ICmpInst &Cmp);
654   Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
655   Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
656   Instruction *foldICmpBinOp(ICmpInst &Cmp, const SimplifyQuery &SQ);
657   Instruction *foldICmpEquality(ICmpInst &Cmp);
658   Instruction *foldIRemByPowerOfTwoToBitTest(ICmpInst &I);
659   Instruction *foldSignBitTest(ICmpInst &I);
660   Instruction *foldICmpWithZero(ICmpInst &Cmp);
661 
662   Value *foldUnsignedMultiplicationOverflowCheck(ICmpInst &Cmp);
663 
664   Instruction *foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select,
665                                       ConstantInt *C);
666   Instruction *foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc,
667                                      const APInt &C);
668   Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
669                                    const APInt &C);
670   Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
671                                    const APInt &C);
672   Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
673                                   const APInt &C);
674   Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
675                                    const APInt &C);
676   Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
677                                    const APInt &C);
678   Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
679                                    const APInt &C);
680   Instruction *foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
681                                     const APInt &C);
682   Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
683                                     const APInt &C);
684   Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
685                                    const APInt &C);
686   Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
687                                    const APInt &C);
688   Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
689                                    const APInt &C);
690   Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
691                                      const APInt &C1);
692   Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
693                                 const APInt &C1, const APInt &C2);
694   Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
695                                      const APInt &C2);
696   Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
697                                      const APInt &C2);
698 
699   Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
700                                                  BinaryOperator *BO,
701                                                  const APInt &C);
702   Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
703                                              const APInt &C);
704   Instruction *foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
705                                                const APInt &C);
706 
707   // Helpers of visitSelectInst().
708   Instruction *foldSelectExtConst(SelectInst &Sel);
709   Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
710   Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
711   Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
712                             Value *A, Value *B, Instruction &Outer,
713                             SelectPatternFlavor SPF2, Value *C);
714   Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
715   Instruction *foldSelectValueEquivalence(SelectInst &SI, ICmpInst &ICI);
716 
717   Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
718                          bool isSigned, bool Inside);
719   Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
720   bool mergeStoreIntoSuccessor(StoreInst &SI);
721 
722   /// Given an initial instruction, check to see if it is the root of a
723   /// bswap/bitreverse idiom. If so, return the equivalent bswap/bitreverse
724   /// intrinsic.
725   Instruction *matchBSwapOrBitReverse(Instruction &I, bool MatchBSwaps,
726                                       bool MatchBitReversals);
727 
728   Instruction *SimplifyAnyMemTransfer(AnyMemTransferInst *MI);
729   Instruction *SimplifyAnyMemSet(AnyMemSetInst *MI);
730 
731   Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
732 
733   /// Returns a value X such that Val = X * Scale, or null if none.
734   ///
735   /// If the multiplication is known not to overflow then NoSignedWrap is set.
736   Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
737 };
738 
739 class Negator final {
740   /// Top-to-bottom, def-to-use negated instruction tree we produced.
741   SmallVector<Instruction *, NegatorMaxNodesSSO> NewInstructions;
742 
743   using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
744   BuilderTy Builder;
745 
746   const DataLayout &DL;
747   AssumptionCache &AC;
748   const DominatorTree &DT;
749 
750   const bool IsTrulyNegation;
751 
752   SmallDenseMap<Value *, Value *> NegationsCache;
753 
754   Negator(LLVMContext &C, const DataLayout &DL, AssumptionCache &AC,
755           const DominatorTree &DT, bool IsTrulyNegation);
756 
757 #if LLVM_ENABLE_STATS
758   unsigned NumValuesVisitedInThisNegator = 0;
759   ~Negator();
760 #endif
761 
762   using Result = std::pair<ArrayRef<Instruction *> /*NewInstructions*/,
763                            Value * /*NegatedRoot*/>;
764 
765   std::array<Value *, 2> getSortedOperandsOfBinOp(Instruction *I);
766 
767   LLVM_NODISCARD Value *visitImpl(Value *V, unsigned Depth);
768 
769   LLVM_NODISCARD Value *negate(Value *V, unsigned Depth);
770 
771   /// Recurse depth-first and attempt to sink the negation.
772   /// FIXME: use worklist?
773   LLVM_NODISCARD Optional<Result> run(Value *Root);
774 
775   Negator(const Negator &) = delete;
776   Negator(Negator &&) = delete;
777   Negator &operator=(const Negator &) = delete;
778   Negator &operator=(Negator &&) = delete;
779 
780 public:
781   /// Attempt to negate \p Root. Retuns nullptr if negation can't be performed,
782   /// otherwise returns negated value.
783   LLVM_NODISCARD static Value *Negate(bool LHSIsZero, Value *Root,
784                                       InstCombinerImpl &IC);
785 };
786 
787 } // end namespace llvm
788 
789 #undef DEBUG_TYPE
790 
791 #endif // LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H
792