Home | History | Annotate | Line # | Download | only in InstCombine
      1 //===- InstCombiner.h - InstCombine implementation --------------*- 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 /// \file
      9 ///
     10 /// This file provides the interface for the instcombine pass implementation.
     11 /// The interface is used for generic transformations in this folder and
     12 /// target specific combinations in the targets.
     13 /// The visitor implementation is in \c InstCombinerImpl in
     14 /// \c InstCombineInternal.h.
     15 ///
     16 //===----------------------------------------------------------------------===//
     17 
     18 #ifndef LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINER_H
     19 #define LLVM_TRANSFORMS_INSTCOMBINE_INSTCOMBINER_H
     20 
     21 #include "llvm/Analysis/InstructionSimplify.h"
     22 #include "llvm/Analysis/TargetFolder.h"
     23 #include "llvm/Analysis/ValueTracking.h"
     24 #include "llvm/IR/IRBuilder.h"
     25 #include "llvm/IR/PatternMatch.h"
     26 #include "llvm/Support/Debug.h"
     27 #include "llvm/Support/KnownBits.h"
     28 #include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
     29 #include <cassert>
     30 
     31 #define DEBUG_TYPE "instcombine"
     32 
     33 namespace llvm {
     34 
     35 class AAResults;
     36 class AssumptionCache;
     37 class ProfileSummaryInfo;
     38 class TargetLibraryInfo;
     39 class TargetTransformInfo;
     40 
     41 /// The core instruction combiner logic.
     42 ///
     43 /// This class provides both the logic to recursively visit instructions and
     44 /// combine them.
     45 class LLVM_LIBRARY_VISIBILITY InstCombiner {
     46   /// Only used to call target specific inst combining.
     47   TargetTransformInfo &TTI;
     48 
     49 public:
     50   /// Maximum size of array considered when transforming.
     51   uint64_t MaxArraySizeForCombine = 0;
     52 
     53   /// An IRBuilder that automatically inserts new instructions into the
     54   /// worklist.
     55   using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
     56   BuilderTy &Builder;
     57 
     58 protected:
     59   /// A worklist of the instructions that need to be simplified.
     60   InstCombineWorklist &Worklist;
     61 
     62   // Mode in which we are running the combiner.
     63   const bool MinimizeSize;
     64 
     65   AAResults *AA;
     66 
     67   // Required analyses.
     68   AssumptionCache &AC;
     69   TargetLibraryInfo &TLI;
     70   DominatorTree &DT;
     71   const DataLayout &DL;
     72   const SimplifyQuery SQ;
     73   OptimizationRemarkEmitter &ORE;
     74   BlockFrequencyInfo *BFI;
     75   ProfileSummaryInfo *PSI;
     76 
     77   // Optional analyses. When non-null, these can both be used to do better
     78   // combining and will be updated to reflect any changes.
     79   LoopInfo *LI;
     80 
     81   bool MadeIRChange = false;
     82 
     83 public:
     84   InstCombiner(InstCombineWorklist &Worklist, BuilderTy &Builder,
     85                bool MinimizeSize, AAResults *AA, AssumptionCache &AC,
     86                TargetLibraryInfo &TLI, TargetTransformInfo &TTI,
     87                DominatorTree &DT, OptimizationRemarkEmitter &ORE,
     88                BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
     89                const DataLayout &DL, LoopInfo *LI)
     90       : TTI(TTI), Builder(Builder), Worklist(Worklist),
     91         MinimizeSize(MinimizeSize), AA(AA), AC(AC), TLI(TLI), DT(DT), DL(DL),
     92         SQ(DL, &TLI, &DT, &AC), ORE(ORE), BFI(BFI), PSI(PSI), LI(LI) {}
     93 
     94   virtual ~InstCombiner() {}
     95 
     96   /// Return the source operand of a potentially bitcasted value while
     97   /// optionally checking if it has one use. If there is no bitcast or the one
     98   /// use check is not met, return the input value itself.
     99   static Value *peekThroughBitcast(Value *V, bool OneUseOnly = false) {
    100     if (auto *BitCast = dyn_cast<BitCastInst>(V))
    101       if (!OneUseOnly || BitCast->hasOneUse())
    102         return BitCast->getOperand(0);
    103 
    104     // V is not a bitcast or V has more than one use and OneUseOnly is true.
    105     return V;
    106   }
    107 
    108   /// Assign a complexity or rank value to LLVM Values. This is used to reduce
    109   /// the amount of pattern matching needed for compares and commutative
    110   /// instructions. For example, if we have:
    111   ///   icmp ugt X, Constant
    112   /// or
    113   ///   xor (add X, Constant), cast Z
    114   ///
    115   /// We do not have to consider the commuted variants of these patterns because
    116   /// canonicalization based on complexity guarantees the above ordering.
    117   ///
    118   /// This routine maps IR values to various complexity ranks:
    119   ///   0 -> undef
    120   ///   1 -> Constants
    121   ///   2 -> Other non-instructions
    122   ///   3 -> Arguments
    123   ///   4 -> Cast and (f)neg/not instructions
    124   ///   5 -> Other instructions
    125   static unsigned getComplexity(Value *V) {
    126     if (isa<Instruction>(V)) {
    127       if (isa<CastInst>(V) || match(V, m_Neg(PatternMatch::m_Value())) ||
    128           match(V, m_Not(PatternMatch::m_Value())) ||
    129           match(V, m_FNeg(PatternMatch::m_Value())))
    130         return 4;
    131       return 5;
    132     }
    133     if (isa<Argument>(V))
    134       return 3;
    135     return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
    136   }
    137 
    138   /// Predicate canonicalization reduces the number of patterns that need to be
    139   /// matched by other transforms. For example, we may swap the operands of a
    140   /// conditional branch or select to create a compare with a canonical
    141   /// (inverted) predicate which is then more likely to be matched with other
    142   /// values.
    143   static bool isCanonicalPredicate(CmpInst::Predicate Pred) {
    144     switch (Pred) {
    145     case CmpInst::ICMP_NE:
    146     case CmpInst::ICMP_ULE:
    147     case CmpInst::ICMP_SLE:
    148     case CmpInst::ICMP_UGE:
    149     case CmpInst::ICMP_SGE:
    150     // TODO: There are 16 FCMP predicates. Should others be (not) canonical?
    151     case CmpInst::FCMP_ONE:
    152     case CmpInst::FCMP_OLE:
    153     case CmpInst::FCMP_OGE:
    154       return false;
    155     default:
    156       return true;
    157     }
    158   }
    159 
    160   /// Given an exploded icmp instruction, return true if the comparison only
    161   /// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
    162   /// the result of the comparison is true when the input value is signed.
    163   static bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
    164                              bool &TrueIfSigned) {
    165     switch (Pred) {
    166     case ICmpInst::ICMP_SLT: // True if LHS s< 0
    167       TrueIfSigned = true;
    168       return RHS.isNullValue();
    169     case ICmpInst::ICMP_SLE: // True if LHS s<= -1
    170       TrueIfSigned = true;
    171       return RHS.isAllOnesValue();
    172     case ICmpInst::ICMP_SGT: // True if LHS s> -1
    173       TrueIfSigned = false;
    174       return RHS.isAllOnesValue();
    175     case ICmpInst::ICMP_SGE: // True if LHS s>= 0
    176       TrueIfSigned = false;
    177       return RHS.isNullValue();
    178     case ICmpInst::ICMP_UGT:
    179       // True if LHS u> RHS and RHS == sign-bit-mask - 1
    180       TrueIfSigned = true;
    181       return RHS.isMaxSignedValue();
    182     case ICmpInst::ICMP_UGE:
    183       // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
    184       TrueIfSigned = true;
    185       return RHS.isMinSignedValue();
    186     case ICmpInst::ICMP_ULT:
    187       // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
    188       TrueIfSigned = false;
    189       return RHS.isMinSignedValue();
    190     case ICmpInst::ICMP_ULE:
    191       // True if LHS u<= RHS and RHS == sign-bit-mask - 1
    192       TrueIfSigned = false;
    193       return RHS.isMaxSignedValue();
    194     default:
    195       return false;
    196     }
    197   }
    198 
    199   /// Add one to a Constant
    200   static Constant *AddOne(Constant *C) {
    201     return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
    202   }
    203 
    204   /// Subtract one from a Constant
    205   static Constant *SubOne(Constant *C) {
    206     return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
    207   }
    208 
    209   llvm::Optional<std::pair<
    210       CmpInst::Predicate,
    211       Constant *>> static getFlippedStrictnessPredicateAndConstant(CmpInst::
    212                                                                        Predicate
    213                                                                            Pred,
    214                                                                    Constant *C);
    215 
    216   static bool shouldAvoidAbsorbingNotIntoSelect(const SelectInst &SI) {
    217     // a ? b : false and a ? true : b are the canonical form of logical and/or.
    218     // This includes !a ? b : false and !a ? true : b. Absorbing the not into
    219     // the select by swapping operands would break recognition of this pattern
    220     // in other analyses, so don't do that.
    221     return match(&SI, PatternMatch::m_LogicalAnd(PatternMatch::m_Value(),
    222                                                  PatternMatch::m_Value())) ||
    223            match(&SI, PatternMatch::m_LogicalOr(PatternMatch::m_Value(),
    224                                                 PatternMatch::m_Value()));
    225   }
    226 
    227   /// Return true if the specified value is free to invert (apply ~ to).
    228   /// This happens in cases where the ~ can be eliminated.  If WillInvertAllUses
    229   /// is true, work under the assumption that the caller intends to remove all
    230   /// uses of V and only keep uses of ~V.
    231   ///
    232   /// See also: canFreelyInvertAllUsersOf()
    233   static bool isFreeToInvert(Value *V, bool WillInvertAllUses) {
    234     // ~(~(X)) -> X.
    235     if (match(V, m_Not(PatternMatch::m_Value())))
    236       return true;
    237 
    238     // Constants can be considered to be not'ed values.
    239     if (match(V, PatternMatch::m_AnyIntegralConstant()))
    240       return true;
    241 
    242     // Compares can be inverted if all of their uses are being modified to use
    243     // the ~V.
    244     if (isa<CmpInst>(V))
    245       return WillInvertAllUses;
    246 
    247     // If `V` is of the form `A + Constant` then `-1 - V` can be folded into
    248     // `(-1 - Constant) - A` if we are willing to invert all of the uses.
    249     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
    250       if (BO->getOpcode() == Instruction::Add ||
    251           BO->getOpcode() == Instruction::Sub)
    252         if (match(BO, PatternMatch::m_c_BinOp(PatternMatch::m_Value(),
    253                                               PatternMatch::m_ImmConstant())))
    254           return WillInvertAllUses;
    255 
    256     // Selects with invertible operands are freely invertible
    257     if (match(V,
    258               m_Select(PatternMatch::m_Value(), m_Not(PatternMatch::m_Value()),
    259                        m_Not(PatternMatch::m_Value()))))
    260       return WillInvertAllUses;
    261 
    262     return false;
    263   }
    264 
    265   /// Given i1 V, can every user of V be freely adapted if V is changed to !V ?
    266   /// InstCombine's freelyInvertAllUsersOf() must be kept in sync with this fn.
    267   ///
    268   /// See also: isFreeToInvert()
    269   static bool canFreelyInvertAllUsersOf(Value *V, Value *IgnoredUser) {
    270     // Look at every user of V.
    271     for (Use &U : V->uses()) {
    272       if (U.getUser() == IgnoredUser)
    273         continue; // Don't consider this user.
    274 
    275       auto *I = cast<Instruction>(U.getUser());
    276       switch (I->getOpcode()) {
    277       case Instruction::Select:
    278         if (U.getOperandNo() != 0) // Only if the value is used as select cond.
    279           return false;
    280         if (shouldAvoidAbsorbingNotIntoSelect(*cast<SelectInst>(I)))
    281           return false;
    282         break;
    283       case Instruction::Br:
    284         assert(U.getOperandNo() == 0 && "Must be branching on that value.");
    285         break; // Free to invert by swapping true/false values/destinations.
    286       case Instruction::Xor: // Can invert 'xor' if it's a 'not', by ignoring
    287                              // it.
    288         if (!match(I, m_Not(PatternMatch::m_Value())))
    289           return false; // Not a 'not'.
    290         break;
    291       default:
    292         return false; // Don't know, likely not freely invertible.
    293       }
    294       // So far all users were free to invert...
    295     }
    296     return true; // Can freely invert all users!
    297   }
    298 
    299   /// Some binary operators require special handling to avoid poison and
    300   /// undefined behavior. If a constant vector has undef elements, replace those
    301   /// undefs with identity constants if possible because those are always safe
    302   /// to execute. If no identity constant exists, replace undef with some other
    303   /// safe constant.
    304   static Constant *
    305   getSafeVectorConstantForBinop(BinaryOperator::BinaryOps Opcode, Constant *In,
    306                                 bool IsRHSConstant) {
    307     auto *InVTy = cast<FixedVectorType>(In->getType());
    308 
    309     Type *EltTy = InVTy->getElementType();
    310     auto *SafeC = ConstantExpr::getBinOpIdentity(Opcode, EltTy, IsRHSConstant);
    311     if (!SafeC) {
    312       // TODO: Should this be available as a constant utility function? It is
    313       // similar to getBinOpAbsorber().
    314       if (IsRHSConstant) {
    315         switch (Opcode) {
    316         case Instruction::SRem: // X % 1 = 0
    317         case Instruction::URem: // X %u 1 = 0
    318           SafeC = ConstantInt::get(EltTy, 1);
    319           break;
    320         case Instruction::FRem: // X % 1.0 (doesn't simplify, but it is safe)
    321           SafeC = ConstantFP::get(EltTy, 1.0);
    322           break;
    323         default:
    324           llvm_unreachable(
    325               "Only rem opcodes have no identity constant for RHS");
    326         }
    327       } else {
    328         switch (Opcode) {
    329         case Instruction::Shl:  // 0 << X = 0
    330         case Instruction::LShr: // 0 >>u X = 0
    331         case Instruction::AShr: // 0 >> X = 0
    332         case Instruction::SDiv: // 0 / X = 0
    333         case Instruction::UDiv: // 0 /u X = 0
    334         case Instruction::SRem: // 0 % X = 0
    335         case Instruction::URem: // 0 %u X = 0
    336         case Instruction::Sub:  // 0 - X (doesn't simplify, but it is safe)
    337         case Instruction::FSub: // 0.0 - X (doesn't simplify, but it is safe)
    338         case Instruction::FDiv: // 0.0 / X (doesn't simplify, but it is safe)
    339         case Instruction::FRem: // 0.0 % X = 0
    340           SafeC = Constant::getNullValue(EltTy);
    341           break;
    342         default:
    343           llvm_unreachable("Expected to find identity constant for opcode");
    344         }
    345       }
    346     }
    347     assert(SafeC && "Must have safe constant for binop");
    348     unsigned NumElts = InVTy->getNumElements();
    349     SmallVector<Constant *, 16> Out(NumElts);
    350     for (unsigned i = 0; i != NumElts; ++i) {
    351       Constant *C = In->getAggregateElement(i);
    352       Out[i] = isa<UndefValue>(C) ? SafeC : C;
    353     }
    354     return ConstantVector::get(Out);
    355   }
    356 
    357   /// Create and insert the idiom we use to indicate a block is unreachable
    358   /// without having to rewrite the CFG from within InstCombine.
    359   static void CreateNonTerminatorUnreachable(Instruction *InsertAt) {
    360     auto &Ctx = InsertAt->getContext();
    361     new StoreInst(ConstantInt::getTrue(Ctx),
    362                   UndefValue::get(Type::getInt1PtrTy(Ctx)), InsertAt);
    363   }
    364 
    365   void addToWorklist(Instruction *I) { Worklist.push(I); }
    366 
    367   AssumptionCache &getAssumptionCache() const { return AC; }
    368   TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; }
    369   DominatorTree &getDominatorTree() const { return DT; }
    370   const DataLayout &getDataLayout() const { return DL; }
    371   const SimplifyQuery &getSimplifyQuery() const { return SQ; }
    372   OptimizationRemarkEmitter &getOptimizationRemarkEmitter() const {
    373     return ORE;
    374   }
    375   BlockFrequencyInfo *getBlockFrequencyInfo() const { return BFI; }
    376   ProfileSummaryInfo *getProfileSummaryInfo() const { return PSI; }
    377   LoopInfo *getLoopInfo() const { return LI; }
    378 
    379   // Call target specific combiners
    380   Optional<Instruction *> targetInstCombineIntrinsic(IntrinsicInst &II);
    381   Optional<Value *>
    382   targetSimplifyDemandedUseBitsIntrinsic(IntrinsicInst &II, APInt DemandedMask,
    383                                          KnownBits &Known,
    384                                          bool &KnownBitsComputed);
    385   Optional<Value *> targetSimplifyDemandedVectorEltsIntrinsic(
    386       IntrinsicInst &II, APInt DemandedElts, APInt &UndefElts,
    387       APInt &UndefElts2, APInt &UndefElts3,
    388       std::function<void(Instruction *, unsigned, APInt, APInt &)>
    389           SimplifyAndSetOp);
    390 
    391   /// Inserts an instruction \p New before instruction \p Old
    392   ///
    393   /// Also adds the new instruction to the worklist and returns \p New so that
    394   /// it is suitable for use as the return from the visitation patterns.
    395   Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
    396     assert(New && !New->getParent() &&
    397            "New instruction already inserted into a basic block!");
    398     BasicBlock *BB = Old.getParent();
    399     BB->getInstList().insert(Old.getIterator(), New); // Insert inst
    400     Worklist.push(New);
    401     return New;
    402   }
    403 
    404   /// Same as InsertNewInstBefore, but also sets the debug loc.
    405   Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
    406     New->setDebugLoc(Old.getDebugLoc());
    407     return InsertNewInstBefore(New, Old);
    408   }
    409 
    410   /// A combiner-aware RAUW-like routine.
    411   ///
    412   /// This method is to be used when an instruction is found to be dead,
    413   /// replaceable with another preexisting expression. Here we add all uses of
    414   /// I to the worklist, replace all uses of I with the new value, then return
    415   /// I, so that the inst combiner will know that I was modified.
    416   Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
    417     // If there are no uses to replace, then we return nullptr to indicate that
    418     // no changes were made to the program.
    419     if (I.use_empty())
    420       return nullptr;
    421 
    422     Worklist.pushUsersToWorkList(I); // Add all modified instrs to worklist.
    423 
    424     // If we are replacing the instruction with itself, this must be in a
    425     // segment of unreachable code, so just clobber the instruction.
    426     if (&I == V)
    427       V = UndefValue::get(I.getType());
    428 
    429     LLVM_DEBUG(dbgs() << "IC: Replacing " << I << "\n"
    430                       << "    with " << *V << '\n');
    431 
    432     I.replaceAllUsesWith(V);
    433     return &I;
    434   }
    435 
    436   /// Replace operand of instruction and add old operand to the worklist.
    437   Instruction *replaceOperand(Instruction &I, unsigned OpNum, Value *V) {
    438     Worklist.addValue(I.getOperand(OpNum));
    439     I.setOperand(OpNum, V);
    440     return &I;
    441   }
    442 
    443   /// Replace use and add the previously used value to the worklist.
    444   void replaceUse(Use &U, Value *NewValue) {
    445     Worklist.addValue(U);
    446     U = NewValue;
    447   }
    448 
    449   /// Combiner aware instruction erasure.
    450   ///
    451   /// When dealing with an instruction that has side effects or produces a void
    452   /// value, we can't rely on DCE to delete the instruction. Instead, visit
    453   /// methods should return the value returned by this function.
    454   virtual Instruction *eraseInstFromFunction(Instruction &I) = 0;
    455 
    456   void computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
    457                         const Instruction *CxtI) const {
    458     llvm::computeKnownBits(V, Known, DL, Depth, &AC, CxtI, &DT);
    459   }
    460 
    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 
    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 
    488   OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS,
    489                                              const Instruction *CxtI) const {
    490     return llvm::computeOverflowForSignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
    491   }
    492 
    493   OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
    494                                                const Value *RHS,
    495                                                const Instruction *CxtI) const {
    496     return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
    497   }
    498 
    499   OverflowResult computeOverflowForSignedAdd(const Value *LHS, const Value *RHS,
    500                                              const Instruction *CxtI) const {
    501     return llvm::computeOverflowForSignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
    502   }
    503 
    504   OverflowResult computeOverflowForUnsignedSub(const Value *LHS,
    505                                                const Value *RHS,
    506                                                const Instruction *CxtI) const {
    507     return llvm::computeOverflowForUnsignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
    508   }
    509 
    510   OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
    511                                              const Instruction *CxtI) const {
    512     return llvm::computeOverflowForSignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
    513   }
    514 
    515   virtual bool SimplifyDemandedBits(Instruction *I, unsigned OpNo,
    516                                     const APInt &DemandedMask, KnownBits &Known,
    517                                     unsigned Depth = 0) = 0;
    518   virtual Value *
    519   SimplifyDemandedVectorElts(Value *V, APInt DemandedElts, APInt &UndefElts,
    520                              unsigned Depth = 0,
    521                              bool AllowMultipleUsers = false) = 0;
    522 };
    523 
    524 } // namespace llvm
    525 
    526 #undef DEBUG_TYPE
    527 
    528 #endif
    529