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