InstCombineInternal.h 32 KB

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