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