InstCombineInternal.h 35 KB

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