InstCombineInternal.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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/ArrayRef.h"
  17. #include "llvm/Analysis/AliasAnalysis.h"
  18. #include "llvm/Analysis/InstructionSimplify.h"
  19. #include "llvm/Analysis/TargetFolder.h"
  20. #include "llvm/Analysis/ValueTracking.h"
  21. #include "llvm/IR/Argument.h"
  22. #include "llvm/IR/BasicBlock.h"
  23. #include "llvm/IR/Constant.h"
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/DerivedTypes.h"
  26. #include "llvm/IR/IRBuilder.h"
  27. #include "llvm/IR/InstVisitor.h"
  28. #include "llvm/IR/InstrTypes.h"
  29. #include "llvm/IR/Instruction.h"
  30. #include "llvm/IR/IntrinsicInst.h"
  31. #include "llvm/IR/Intrinsics.h"
  32. #include "llvm/IR/PatternMatch.h"
  33. #include "llvm/IR/Use.h"
  34. #include "llvm/IR/Value.h"
  35. #include "llvm/Support/Casting.h"
  36. #include "llvm/Support/Compiler.h"
  37. #include "llvm/Support/Debug.h"
  38. #include "llvm/Support/KnownBits.h"
  39. #include "llvm/Support/raw_ostream.h"
  40. #include "llvm/Transforms/InstCombine/InstCombineWorklist.h"
  41. #include "llvm/Transforms/Utils/Local.h"
  42. #include <cassert>
  43. #include <cstdint>
  44. #define DEBUG_TYPE "instcombine"
  45. using namespace llvm::PatternMatch;
  46. namespace llvm {
  47. class APInt;
  48. class AssumptionCache;
  49. class BlockFrequencyInfo;
  50. class DataLayout;
  51. class DominatorTree;
  52. class GEPOperator;
  53. class GlobalVariable;
  54. class LoopInfo;
  55. class OptimizationRemarkEmitter;
  56. class ProfileSummaryInfo;
  57. class TargetLibraryInfo;
  58. class User;
  59. /// Assign a complexity or rank value to LLVM Values. This is used to reduce
  60. /// the amount of pattern matching needed for compares and commutative
  61. /// instructions. For example, if we have:
  62. /// icmp ugt X, Constant
  63. /// or
  64. /// xor (add X, Constant), cast Z
  65. ///
  66. /// We do not have to consider the commuted variants of these patterns because
  67. /// canonicalization based on complexity guarantees the above ordering.
  68. ///
  69. /// This routine maps IR values to various complexity ranks:
  70. /// 0 -> undef
  71. /// 1 -> Constants
  72. /// 2 -> Other non-instructions
  73. /// 3 -> Arguments
  74. /// 4 -> Cast and (f)neg/not instructions
  75. /// 5 -> Other instructions
  76. static inline unsigned getComplexity(Value *V) {
  77. if (isa<Instruction>(V)) {
  78. if (isa<CastInst>(V) || match(V, m_Neg(m_Value())) ||
  79. match(V, m_Not(m_Value())) || match(V, m_FNeg(m_Value())))
  80. return 4;
  81. return 5;
  82. }
  83. if (isa<Argument>(V))
  84. return 3;
  85. return isa<Constant>(V) ? (isa<UndefValue>(V) ? 0 : 1) : 2;
  86. }
  87. /// Predicate canonicalization reduces the number of patterns that need to be
  88. /// matched by other transforms. For example, we may swap the operands of a
  89. /// conditional branch or select to create a compare with a canonical (inverted)
  90. /// predicate which is then more likely to be matched with other values.
  91. static inline bool isCanonicalPredicate(CmpInst::Predicate Pred) {
  92. switch (Pred) {
  93. case CmpInst::ICMP_NE:
  94. case CmpInst::ICMP_ULE:
  95. case CmpInst::ICMP_SLE:
  96. case CmpInst::ICMP_UGE:
  97. case CmpInst::ICMP_SGE:
  98. // TODO: There are 16 FCMP predicates. Should others be (not) canonical?
  99. case CmpInst::FCMP_ONE:
  100. case CmpInst::FCMP_OLE:
  101. case CmpInst::FCMP_OGE:
  102. return false;
  103. default:
  104. return true;
  105. }
  106. }
  107. /// Given an exploded icmp instruction, return true if the comparison only
  108. /// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if the
  109. /// result of the comparison is true when the input value is signed.
  110. inline bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS,
  111. bool &TrueIfSigned) {
  112. switch (Pred) {
  113. case ICmpInst::ICMP_SLT: // True if LHS s< 0
  114. TrueIfSigned = true;
  115. return RHS.isNullValue();
  116. case ICmpInst::ICMP_SLE: // True if LHS s<= -1
  117. TrueIfSigned = true;
  118. return RHS.isAllOnesValue();
  119. case ICmpInst::ICMP_SGT: // True if LHS s> -1
  120. TrueIfSigned = false;
  121. return RHS.isAllOnesValue();
  122. case ICmpInst::ICMP_SGE: // True if LHS s>= 0
  123. TrueIfSigned = false;
  124. return RHS.isNullValue();
  125. case ICmpInst::ICMP_UGT:
  126. // True if LHS u> RHS and RHS == sign-bit-mask - 1
  127. TrueIfSigned = true;
  128. return RHS.isMaxSignedValue();
  129. case ICmpInst::ICMP_UGE:
  130. // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
  131. TrueIfSigned = true;
  132. return RHS.isMinSignedValue();
  133. case ICmpInst::ICMP_ULT:
  134. // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
  135. TrueIfSigned = false;
  136. return RHS.isMinSignedValue();
  137. case ICmpInst::ICMP_ULE:
  138. // True if LHS u<= RHS and RHS == sign-bit-mask - 1
  139. TrueIfSigned = false;
  140. return RHS.isMaxSignedValue();
  141. default:
  142. return false;
  143. }
  144. }
  145. llvm::Optional<std::pair<CmpInst::Predicate, Constant *>>
  146. getFlippedStrictnessPredicateAndConstant(CmpInst::Predicate Pred, Constant *C);
  147. /// Return the source operand of a potentially bitcasted value while optionally
  148. /// checking if it has one use. If there is no bitcast or the one use check is
  149. /// not met, return the input value itself.
  150. static inline Value *peekThroughBitcast(Value *V, bool OneUseOnly = false) {
  151. if (auto *BitCast = dyn_cast<BitCastInst>(V))
  152. if (!OneUseOnly || BitCast->hasOneUse())
  153. return BitCast->getOperand(0);
  154. // V is not a bitcast or V has more than one use and OneUseOnly is true.
  155. return V;
  156. }
  157. /// Add one to a Constant
  158. static inline Constant *AddOne(Constant *C) {
  159. return ConstantExpr::getAdd(C, ConstantInt::get(C->getType(), 1));
  160. }
  161. /// Subtract one from a Constant
  162. static inline Constant *SubOne(Constant *C) {
  163. return ConstantExpr::getSub(C, ConstantInt::get(C->getType(), 1));
  164. }
  165. /// Return true if the specified value is free to invert (apply ~ to).
  166. /// This happens in cases where the ~ can be eliminated. If WillInvertAllUses
  167. /// is true, work under the assumption that the caller intends to remove all
  168. /// uses of V and only keep uses of ~V.
  169. ///
  170. /// See also: canFreelyInvertAllUsersOf()
  171. static inline bool isFreeToInvert(Value *V, bool WillInvertAllUses) {
  172. // ~(~(X)) -> X.
  173. if (match(V, m_Not(m_Value())))
  174. return true;
  175. // Constants can be considered to be not'ed values.
  176. if (match(V, m_AnyIntegralConstant()))
  177. return true;
  178. // Compares can be inverted if all of their uses are being modified to use the
  179. // ~V.
  180. if (isa<CmpInst>(V))
  181. return WillInvertAllUses;
  182. // If `V` is of the form `A + Constant` then `-1 - V` can be folded into `(-1
  183. // - Constant) - A` if we are willing to invert all of the uses.
  184. if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V))
  185. if (BO->getOpcode() == Instruction::Add ||
  186. BO->getOpcode() == Instruction::Sub)
  187. if (isa<Constant>(BO->getOperand(0)) || isa<Constant>(BO->getOperand(1)))
  188. return WillInvertAllUses;
  189. // Selects with invertible operands are freely invertible
  190. if (match(V, m_Select(m_Value(), m_Not(m_Value()), m_Not(m_Value()))))
  191. return WillInvertAllUses;
  192. return false;
  193. }
  194. /// Given i1 V, can every user of V be freely adapted if V is changed to !V ?
  195. ///
  196. /// See also: isFreeToInvert()
  197. static inline bool canFreelyInvertAllUsersOf(Value *V, Value *IgnoredUser) {
  198. // Look at every user of V.
  199. for (User *U : V->users()) {
  200. if (U == IgnoredUser)
  201. continue; // Don't consider this user.
  202. auto *I = cast<Instruction>(U);
  203. switch (I->getOpcode()) {
  204. case Instruction::Select:
  205. case Instruction::Br:
  206. break; // Free to invert by swapping true/false values/destinations.
  207. case Instruction::Xor: // Can invert 'xor' if it's a 'not', by ignoring it.
  208. if (!match(I, m_Not(m_Value())))
  209. return false; // Not a 'not'.
  210. break;
  211. default:
  212. return false; // Don't know, likely not freely invertible.
  213. }
  214. // So far all users were free to invert...
  215. }
  216. return true; // Can freely invert all users!
  217. }
  218. /// Some binary operators require special handling to avoid poison and undefined
  219. /// behavior. If a constant vector has undef elements, replace those undefs with
  220. /// identity constants if possible because those are always safe to execute.
  221. /// If no identity constant exists, replace undef with some other safe constant.
  222. static inline Constant *getSafeVectorConstantForBinop(
  223. BinaryOperator::BinaryOps Opcode, Constant *In, bool IsRHSConstant) {
  224. assert(In->getType()->isVectorTy() && "Not expecting scalars here");
  225. Type *EltTy = In->getType()->getVectorElementType();
  226. auto *SafeC = ConstantExpr::getBinOpIdentity(Opcode, EltTy, IsRHSConstant);
  227. if (!SafeC) {
  228. // TODO: Should this be available as a constant utility function? It is
  229. // similar to getBinOpAbsorber().
  230. if (IsRHSConstant) {
  231. switch (Opcode) {
  232. case Instruction::SRem: // X % 1 = 0
  233. case Instruction::URem: // X %u 1 = 0
  234. SafeC = ConstantInt::get(EltTy, 1);
  235. break;
  236. case Instruction::FRem: // X % 1.0 (doesn't simplify, but it is safe)
  237. SafeC = ConstantFP::get(EltTy, 1.0);
  238. break;
  239. default:
  240. llvm_unreachable("Only rem opcodes have no identity constant for RHS");
  241. }
  242. } else {
  243. switch (Opcode) {
  244. case Instruction::Shl: // 0 << X = 0
  245. case Instruction::LShr: // 0 >>u X = 0
  246. case Instruction::AShr: // 0 >> X = 0
  247. case Instruction::SDiv: // 0 / X = 0
  248. case Instruction::UDiv: // 0 /u X = 0
  249. case Instruction::SRem: // 0 % X = 0
  250. case Instruction::URem: // 0 %u X = 0
  251. case Instruction::Sub: // 0 - X (doesn't simplify, but it is safe)
  252. case Instruction::FSub: // 0.0 - X (doesn't simplify, but it is safe)
  253. case Instruction::FDiv: // 0.0 / X (doesn't simplify, but it is safe)
  254. case Instruction::FRem: // 0.0 % X = 0
  255. SafeC = Constant::getNullValue(EltTy);
  256. break;
  257. default:
  258. llvm_unreachable("Expected to find identity constant for opcode");
  259. }
  260. }
  261. }
  262. assert(SafeC && "Must have safe constant for binop");
  263. unsigned NumElts = In->getType()->getVectorNumElements();
  264. SmallVector<Constant *, 16> Out(NumElts);
  265. for (unsigned i = 0; i != NumElts; ++i) {
  266. Constant *C = In->getAggregateElement(i);
  267. Out[i] = isa<UndefValue>(C) ? SafeC : C;
  268. }
  269. return ConstantVector::get(Out);
  270. }
  271. /// The core instruction combiner logic.
  272. ///
  273. /// This class provides both the logic to recursively visit instructions and
  274. /// combine them.
  275. class LLVM_LIBRARY_VISIBILITY InstCombiner
  276. : public InstVisitor<InstCombiner, Instruction *> {
  277. // FIXME: These members shouldn't be public.
  278. public:
  279. /// A worklist of the instructions that need to be simplified.
  280. InstCombineWorklist &Worklist;
  281. /// An IRBuilder that automatically inserts new instructions into the
  282. /// worklist.
  283. using BuilderTy = IRBuilder<TargetFolder, IRBuilderCallbackInserter>;
  284. BuilderTy &Builder;
  285. private:
  286. // Mode in which we are running the combiner.
  287. const bool MinimizeSize;
  288. /// Enable combines that trigger rarely but are costly in compiletime.
  289. const bool ExpensiveCombines;
  290. AliasAnalysis *AA;
  291. // Required analyses.
  292. AssumptionCache &AC;
  293. TargetLibraryInfo &TLI;
  294. DominatorTree &DT;
  295. const DataLayout &DL;
  296. const SimplifyQuery SQ;
  297. OptimizationRemarkEmitter &ORE;
  298. BlockFrequencyInfo *BFI;
  299. ProfileSummaryInfo *PSI;
  300. // Optional analyses. When non-null, these can both be used to do better
  301. // combining and will be updated to reflect any changes.
  302. LoopInfo *LI;
  303. bool MadeIRChange = false;
  304. public:
  305. InstCombiner(InstCombineWorklist &Worklist, BuilderTy &Builder,
  306. bool MinimizeSize, bool ExpensiveCombines, AliasAnalysis *AA,
  307. AssumptionCache &AC, TargetLibraryInfo &TLI, DominatorTree &DT,
  308. OptimizationRemarkEmitter &ORE, BlockFrequencyInfo *BFI,
  309. ProfileSummaryInfo *PSI, const DataLayout &DL, LoopInfo *LI)
  310. : Worklist(Worklist), Builder(Builder), MinimizeSize(MinimizeSize),
  311. ExpensiveCombines(ExpensiveCombines), AA(AA), AC(AC), TLI(TLI), DT(DT),
  312. DL(DL), SQ(DL, &TLI, &DT, &AC), ORE(ORE), BFI(BFI), PSI(PSI), LI(LI) {}
  313. /// Run the combiner over the entire worklist until it is empty.
  314. ///
  315. /// \returns true if the IR is changed.
  316. bool run();
  317. AssumptionCache &getAssumptionCache() const { return AC; }
  318. const DataLayout &getDataLayout() const { return DL; }
  319. DominatorTree &getDominatorTree() const { return DT; }
  320. LoopInfo *getLoopInfo() const { return LI; }
  321. TargetLibraryInfo &getTargetLibraryInfo() const { return TLI; }
  322. // Visitation implementation - Implement instruction combining for different
  323. // instruction types. The semantics are as follows:
  324. // Return Value:
  325. // null - No change was made
  326. // I - Change was made, I is still valid, I may be dead though
  327. // otherwise - Change was made, replace I with returned instruction
  328. //
  329. Instruction *visitFNeg(UnaryOperator &I);
  330. Instruction *visitAdd(BinaryOperator &I);
  331. Instruction *visitFAdd(BinaryOperator &I);
  332. Value *OptimizePointerDifference(Value *LHS, Value *RHS, Type *Ty);
  333. Instruction *visitSub(BinaryOperator &I);
  334. Instruction *visitFSub(BinaryOperator &I);
  335. Instruction *visitMul(BinaryOperator &I);
  336. Instruction *visitFMul(BinaryOperator &I);
  337. Instruction *visitURem(BinaryOperator &I);
  338. Instruction *visitSRem(BinaryOperator &I);
  339. Instruction *visitFRem(BinaryOperator &I);
  340. bool simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I);
  341. Instruction *commonRemTransforms(BinaryOperator &I);
  342. Instruction *commonIRemTransforms(BinaryOperator &I);
  343. Instruction *commonDivTransforms(BinaryOperator &I);
  344. Instruction *commonIDivTransforms(BinaryOperator &I);
  345. Instruction *visitUDiv(BinaryOperator &I);
  346. Instruction *visitSDiv(BinaryOperator &I);
  347. Instruction *visitFDiv(BinaryOperator &I);
  348. Value *simplifyRangeCheck(ICmpInst *Cmp0, ICmpInst *Cmp1, bool Inverted);
  349. Instruction *visitAnd(BinaryOperator &I);
  350. Instruction *visitOr(BinaryOperator &I);
  351. Instruction *visitXor(BinaryOperator &I);
  352. Instruction *visitShl(BinaryOperator &I);
  353. Value *reassociateShiftAmtsOfTwoSameDirectionShifts(
  354. BinaryOperator *Sh0, const SimplifyQuery &SQ,
  355. bool AnalyzeForSignBitExtraction = false);
  356. Instruction *canonicalizeCondSignextOfHighBitExtractToSignextHighBitExtract(
  357. BinaryOperator &I);
  358. Instruction *foldVariableSignZeroExtensionOfVariableHighBitExtract(
  359. BinaryOperator &OldAShr);
  360. Instruction *visitAShr(BinaryOperator &I);
  361. Instruction *visitLShr(BinaryOperator &I);
  362. Instruction *commonShiftTransforms(BinaryOperator &I);
  363. Instruction *visitFCmpInst(FCmpInst &I);
  364. Instruction *visitICmpInst(ICmpInst &I);
  365. Instruction *FoldShiftByConstant(Value *Op0, Constant *Op1,
  366. BinaryOperator &I);
  367. Instruction *commonCastTransforms(CastInst &CI);
  368. Instruction *commonPointerCastTransforms(CastInst &CI);
  369. Instruction *visitTrunc(TruncInst &CI);
  370. Instruction *visitZExt(ZExtInst &CI);
  371. Instruction *visitSExt(SExtInst &CI);
  372. Instruction *visitFPTrunc(FPTruncInst &CI);
  373. Instruction *visitFPExt(CastInst &CI);
  374. Instruction *visitFPToUI(FPToUIInst &FI);
  375. Instruction *visitFPToSI(FPToSIInst &FI);
  376. Instruction *visitUIToFP(CastInst &CI);
  377. Instruction *visitSIToFP(CastInst &CI);
  378. Instruction *visitPtrToInt(PtrToIntInst &CI);
  379. Instruction *visitIntToPtr(IntToPtrInst &CI);
  380. Instruction *visitBitCast(BitCastInst &CI);
  381. Instruction *visitAddrSpaceCast(AddrSpaceCastInst &CI);
  382. Instruction *FoldItoFPtoI(Instruction &FI);
  383. Instruction *visitSelectInst(SelectInst &SI);
  384. Instruction *visitCallInst(CallInst &CI);
  385. Instruction *visitInvokeInst(InvokeInst &II);
  386. Instruction *visitCallBrInst(CallBrInst &CBI);
  387. Instruction *SliceUpIllegalIntegerPHI(PHINode &PN);
  388. Instruction *visitPHINode(PHINode &PN);
  389. Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
  390. Instruction *visitAllocaInst(AllocaInst &AI);
  391. Instruction *visitAllocSite(Instruction &FI);
  392. Instruction *visitFree(CallInst &FI);
  393. Instruction *visitLoadInst(LoadInst &LI);
  394. Instruction *visitStoreInst(StoreInst &SI);
  395. Instruction *visitAtomicRMWInst(AtomicRMWInst &SI);
  396. Instruction *visitBranchInst(BranchInst &BI);
  397. Instruction *visitFenceInst(FenceInst &FI);
  398. Instruction *visitSwitchInst(SwitchInst &SI);
  399. Instruction *visitReturnInst(ReturnInst &RI);
  400. Instruction *visitInsertValueInst(InsertValueInst &IV);
  401. Instruction *visitInsertElementInst(InsertElementInst &IE);
  402. Instruction *visitExtractElementInst(ExtractElementInst &EI);
  403. Instruction *visitShuffleVectorInst(ShuffleVectorInst &SVI);
  404. Instruction *visitExtractValueInst(ExtractValueInst &EV);
  405. Instruction *visitLandingPadInst(LandingPadInst &LI);
  406. Instruction *visitVAStartInst(VAStartInst &I);
  407. Instruction *visitVACopyInst(VACopyInst &I);
  408. /// Specify what to return for unhandled instructions.
  409. Instruction *visitInstruction(Instruction &I) { return nullptr; }
  410. /// True when DB dominates all uses of DI except UI.
  411. /// UI must be in the same block as DI.
  412. /// The routine checks that the DI parent and DB are different.
  413. bool dominatesAllUses(const Instruction *DI, const Instruction *UI,
  414. const BasicBlock *DB) const;
  415. /// Try to replace select with select operand SIOpd in SI-ICmp sequence.
  416. bool replacedSelectWithOperand(SelectInst *SI, const ICmpInst *Icmp,
  417. const unsigned SIOpd);
  418. /// Try to replace instruction \p I with value \p V which are pointers
  419. /// in different address space.
  420. /// \return true if successful.
  421. bool replacePointer(Instruction &I, Value *V);
  422. private:
  423. bool shouldChangeType(unsigned FromBitWidth, unsigned ToBitWidth) const;
  424. bool shouldChangeType(Type *From, Type *To) const;
  425. Value *dyn_castNegVal(Value *V) const;
  426. Type *FindElementAtOffset(PointerType *PtrTy, int64_t Offset,
  427. SmallVectorImpl<Value *> &NewIndices);
  428. /// Classify whether a cast is worth optimizing.
  429. ///
  430. /// This is a helper to decide whether the simplification of
  431. /// logic(cast(A), cast(B)) to cast(logic(A, B)) should be performed.
  432. ///
  433. /// \param CI The cast we are interested in.
  434. ///
  435. /// \return true if this cast actually results in any code being generated and
  436. /// if it cannot already be eliminated by some other transformation.
  437. bool shouldOptimizeCast(CastInst *CI);
  438. /// Try to optimize a sequence of instructions checking if an operation
  439. /// on LHS and RHS overflows.
  440. ///
  441. /// If this overflow check is done via one of the overflow check intrinsics,
  442. /// then CtxI has to be the call instruction calling that intrinsic. If this
  443. /// overflow check is done by arithmetic followed by a compare, then CtxI has
  444. /// to be the arithmetic instruction.
  445. ///
  446. /// If a simplification is possible, stores the simplified result of the
  447. /// operation in OperationResult and result of the overflow check in
  448. /// OverflowResult, and return true. If no simplification is possible,
  449. /// returns false.
  450. bool OptimizeOverflowCheck(Instruction::BinaryOps BinaryOp, bool IsSigned,
  451. Value *LHS, Value *RHS,
  452. Instruction &CtxI, Value *&OperationResult,
  453. Constant *&OverflowResult);
  454. Instruction *visitCallBase(CallBase &Call);
  455. Instruction *tryOptimizeCall(CallInst *CI);
  456. bool transformConstExprCastCall(CallBase &Call);
  457. Instruction *transformCallThroughTrampoline(CallBase &Call,
  458. IntrinsicInst &Tramp);
  459. Value *simplifyMaskedLoad(IntrinsicInst &II);
  460. Instruction *simplifyMaskedStore(IntrinsicInst &II);
  461. Instruction *simplifyMaskedGather(IntrinsicInst &II);
  462. Instruction *simplifyMaskedScatter(IntrinsicInst &II);
  463. /// Transform (zext icmp) to bitwise / integer operations in order to
  464. /// eliminate it.
  465. ///
  466. /// \param ICI The icmp of the (zext icmp) pair we are interested in.
  467. /// \parem CI The zext of the (zext icmp) pair we are interested in.
  468. /// \param DoTransform Pass false to just test whether the given (zext icmp)
  469. /// would be transformed. Pass true to actually perform the transformation.
  470. ///
  471. /// \return null if the transformation cannot be performed. If the
  472. /// transformation can be performed the new instruction that replaces the
  473. /// (zext icmp) pair will be returned (if \p DoTransform is false the
  474. /// unmodified \p ICI will be returned in this case).
  475. Instruction *transformZExtICmp(ICmpInst *ICI, ZExtInst &CI,
  476. bool DoTransform = true);
  477. Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
  478. bool willNotOverflowSignedAdd(const Value *LHS, const Value *RHS,
  479. const Instruction &CxtI) const {
  480. return computeOverflowForSignedAdd(LHS, RHS, &CxtI) ==
  481. OverflowResult::NeverOverflows;
  482. }
  483. bool willNotOverflowUnsignedAdd(const Value *LHS, const Value *RHS,
  484. const Instruction &CxtI) const {
  485. return computeOverflowForUnsignedAdd(LHS, RHS, &CxtI) ==
  486. OverflowResult::NeverOverflows;
  487. }
  488. bool willNotOverflowAdd(const Value *LHS, const Value *RHS,
  489. const Instruction &CxtI, bool IsSigned) const {
  490. return IsSigned ? willNotOverflowSignedAdd(LHS, RHS, CxtI)
  491. : willNotOverflowUnsignedAdd(LHS, RHS, CxtI);
  492. }
  493. bool willNotOverflowSignedSub(const Value *LHS, const Value *RHS,
  494. const Instruction &CxtI) const {
  495. return computeOverflowForSignedSub(LHS, RHS, &CxtI) ==
  496. OverflowResult::NeverOverflows;
  497. }
  498. bool willNotOverflowUnsignedSub(const Value *LHS, const Value *RHS,
  499. const Instruction &CxtI) const {
  500. return computeOverflowForUnsignedSub(LHS, RHS, &CxtI) ==
  501. OverflowResult::NeverOverflows;
  502. }
  503. bool willNotOverflowSub(const Value *LHS, const Value *RHS,
  504. const Instruction &CxtI, bool IsSigned) const {
  505. return IsSigned ? willNotOverflowSignedSub(LHS, RHS, CxtI)
  506. : willNotOverflowUnsignedSub(LHS, RHS, CxtI);
  507. }
  508. bool willNotOverflowSignedMul(const Value *LHS, const Value *RHS,
  509. const Instruction &CxtI) const {
  510. return computeOverflowForSignedMul(LHS, RHS, &CxtI) ==
  511. OverflowResult::NeverOverflows;
  512. }
  513. bool willNotOverflowUnsignedMul(const Value *LHS, const Value *RHS,
  514. const Instruction &CxtI) const {
  515. return computeOverflowForUnsignedMul(LHS, RHS, &CxtI) ==
  516. OverflowResult::NeverOverflows;
  517. }
  518. bool willNotOverflowMul(const Value *LHS, const Value *RHS,
  519. const Instruction &CxtI, bool IsSigned) const {
  520. return IsSigned ? willNotOverflowSignedMul(LHS, RHS, CxtI)
  521. : willNotOverflowUnsignedMul(LHS, RHS, CxtI);
  522. }
  523. bool willNotOverflow(BinaryOperator::BinaryOps Opcode, const Value *LHS,
  524. const Value *RHS, const Instruction &CxtI,
  525. bool IsSigned) const {
  526. switch (Opcode) {
  527. case Instruction::Add: return willNotOverflowAdd(LHS, RHS, CxtI, IsSigned);
  528. case Instruction::Sub: return willNotOverflowSub(LHS, RHS, CxtI, IsSigned);
  529. case Instruction::Mul: return willNotOverflowMul(LHS, RHS, CxtI, IsSigned);
  530. default: llvm_unreachable("Unexpected opcode for overflow query");
  531. }
  532. }
  533. Value *EmitGEPOffset(User *GEP);
  534. Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
  535. Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
  536. Instruction *narrowBinOp(TruncInst &Trunc);
  537. Instruction *narrowMaskedBinOp(BinaryOperator &And);
  538. Instruction *narrowMathIfNoOverflow(BinaryOperator &I);
  539. Instruction *narrowRotate(TruncInst &Trunc);
  540. Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
  541. Instruction *matchSAddSubSat(SelectInst &MinMax1);
  542. /// Determine if a pair of casts can be replaced by a single cast.
  543. ///
  544. /// \param CI1 The first of a pair of casts.
  545. /// \param CI2 The second of a pair of casts.
  546. ///
  547. /// \return 0 if the cast pair cannot be eliminated, otherwise returns an
  548. /// Instruction::CastOps value for a cast that can replace the pair, casting
  549. /// CI1->getSrcTy() to CI2->getDstTy().
  550. ///
  551. /// \see CastInst::isEliminableCastPair
  552. Instruction::CastOps isEliminableCastPair(const CastInst *CI1,
  553. const CastInst *CI2);
  554. Value *foldAndOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI);
  555. Value *foldOrOfICmps(ICmpInst *LHS, ICmpInst *RHS, Instruction &CxtI);
  556. Value *foldXorOfICmps(ICmpInst *LHS, ICmpInst *RHS, BinaryOperator &I);
  557. /// Optimize (fcmp)&(fcmp) or (fcmp)|(fcmp).
  558. /// NOTE: Unlike most of instcombine, this returns a Value which should
  559. /// already be inserted into the function.
  560. Value *foldLogicOfFCmps(FCmpInst *LHS, FCmpInst *RHS, bool IsAnd);
  561. Value *foldAndOrOfICmpsOfAndWithPow2(ICmpInst *LHS, ICmpInst *RHS,
  562. bool JoinedByAnd, Instruction &CxtI);
  563. Value *matchSelectFromAndOr(Value *A, Value *B, Value *C, Value *D);
  564. Value *getSelectCondition(Value *A, Value *B);
  565. Instruction *foldIntrinsicWithOverflowCommon(IntrinsicInst *II);
  566. public:
  567. /// Inserts an instruction \p New before instruction \p Old
  568. ///
  569. /// Also adds the new instruction to the worklist and returns \p New so that
  570. /// it is suitable for use as the return from the visitation patterns.
  571. Instruction *InsertNewInstBefore(Instruction *New, Instruction &Old) {
  572. assert(New && !New->getParent() &&
  573. "New instruction already inserted into a basic block!");
  574. BasicBlock *BB = Old.getParent();
  575. BB->getInstList().insert(Old.getIterator(), New); // Insert inst
  576. Worklist.Add(New);
  577. return New;
  578. }
  579. /// Same as InsertNewInstBefore, but also sets the debug loc.
  580. Instruction *InsertNewInstWith(Instruction *New, Instruction &Old) {
  581. New->setDebugLoc(Old.getDebugLoc());
  582. return InsertNewInstBefore(New, Old);
  583. }
  584. /// A combiner-aware RAUW-like routine.
  585. ///
  586. /// This method is to be used when an instruction is found to be dead,
  587. /// replaceable with another preexisting expression. Here we add all uses of
  588. /// I to the worklist, replace all uses of I with the new value, then return
  589. /// I, so that the inst combiner will know that I was modified.
  590. Instruction *replaceInstUsesWith(Instruction &I, Value *V) {
  591. // If there are no uses to replace, then we return nullptr to indicate that
  592. // no changes were made to the program.
  593. if (I.use_empty()) return nullptr;
  594. Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist.
  595. // If we are replacing the instruction with itself, this must be in a
  596. // segment of unreachable code, so just clobber the instruction.
  597. if (&I == V)
  598. V = UndefValue::get(I.getType());
  599. LLVM_DEBUG(dbgs() << "IC: Replacing " << I << "\n"
  600. << " with " << *V << '\n');
  601. I.replaceAllUsesWith(V);
  602. return &I;
  603. }
  604. /// Creates a result tuple for an overflow intrinsic \p II with a given
  605. /// \p Result and a constant \p Overflow value.
  606. Instruction *CreateOverflowTuple(IntrinsicInst *II, Value *Result,
  607. Constant *Overflow) {
  608. Constant *V[] = {UndefValue::get(Result->getType()), Overflow};
  609. StructType *ST = cast<StructType>(II->getType());
  610. Constant *Struct = ConstantStruct::get(ST, V);
  611. return InsertValueInst::Create(Struct, Result, 0);
  612. }
  613. /// Create and insert the idiom we use to indicate a block is unreachable
  614. /// without having to rewrite the CFG from within InstCombine.
  615. void CreateNonTerminatorUnreachable(Instruction *InsertAt) {
  616. auto &Ctx = InsertAt->getContext();
  617. new StoreInst(ConstantInt::getTrue(Ctx),
  618. UndefValue::get(Type::getInt1PtrTy(Ctx)),
  619. InsertAt);
  620. }
  621. /// Combiner aware instruction erasure.
  622. ///
  623. /// When dealing with an instruction that has side effects or produces a void
  624. /// value, we can't rely on DCE to delete the instruction. Instead, visit
  625. /// methods should return the value returned by this function.
  626. Instruction *eraseInstFromFunction(Instruction &I) {
  627. LLVM_DEBUG(dbgs() << "IC: ERASE " << I << '\n');
  628. assert(I.use_empty() && "Cannot erase instruction that is used!");
  629. salvageDebugInfo(I);
  630. // Make sure that we reprocess all operands now that we reduced their
  631. // use counts.
  632. if (I.getNumOperands() < 8) {
  633. for (Use &Operand : I.operands())
  634. if (auto *Inst = dyn_cast<Instruction>(Operand))
  635. Worklist.Add(Inst);
  636. }
  637. Worklist.Remove(&I);
  638. I.eraseFromParent();
  639. MadeIRChange = true;
  640. return nullptr; // Don't do anything with FI
  641. }
  642. void computeKnownBits(const Value *V, KnownBits &Known,
  643. unsigned Depth, const Instruction *CxtI) const {
  644. llvm::computeKnownBits(V, Known, DL, Depth, &AC, CxtI, &DT);
  645. }
  646. KnownBits computeKnownBits(const Value *V, unsigned Depth,
  647. const Instruction *CxtI) const {
  648. return llvm::computeKnownBits(V, DL, Depth, &AC, CxtI, &DT);
  649. }
  650. bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
  651. unsigned Depth = 0,
  652. const Instruction *CxtI = nullptr) {
  653. return llvm::isKnownToBeAPowerOfTwo(V, DL, OrZero, Depth, &AC, CxtI, &DT);
  654. }
  655. bool MaskedValueIsZero(const Value *V, const APInt &Mask, unsigned Depth = 0,
  656. const Instruction *CxtI = nullptr) const {
  657. return llvm::MaskedValueIsZero(V, Mask, DL, Depth, &AC, CxtI, &DT);
  658. }
  659. unsigned ComputeNumSignBits(const Value *Op, unsigned Depth = 0,
  660. const Instruction *CxtI = nullptr) const {
  661. return llvm::ComputeNumSignBits(Op, DL, Depth, &AC, CxtI, &DT);
  662. }
  663. OverflowResult computeOverflowForUnsignedMul(const Value *LHS,
  664. const Value *RHS,
  665. const Instruction *CxtI) const {
  666. return llvm::computeOverflowForUnsignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
  667. }
  668. OverflowResult computeOverflowForSignedMul(const Value *LHS,
  669. const Value *RHS,
  670. const Instruction *CxtI) const {
  671. return llvm::computeOverflowForSignedMul(LHS, RHS, DL, &AC, CxtI, &DT);
  672. }
  673. OverflowResult computeOverflowForUnsignedAdd(const Value *LHS,
  674. const Value *RHS,
  675. const Instruction *CxtI) const {
  676. return llvm::computeOverflowForUnsignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
  677. }
  678. OverflowResult computeOverflowForSignedAdd(const Value *LHS,
  679. const Value *RHS,
  680. const Instruction *CxtI) const {
  681. return llvm::computeOverflowForSignedAdd(LHS, RHS, DL, &AC, CxtI, &DT);
  682. }
  683. OverflowResult computeOverflowForUnsignedSub(const Value *LHS,
  684. const Value *RHS,
  685. const Instruction *CxtI) const {
  686. return llvm::computeOverflowForUnsignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
  687. }
  688. OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS,
  689. const Instruction *CxtI) const {
  690. return llvm::computeOverflowForSignedSub(LHS, RHS, DL, &AC, CxtI, &DT);
  691. }
  692. OverflowResult computeOverflow(
  693. Instruction::BinaryOps BinaryOp, bool IsSigned,
  694. Value *LHS, Value *RHS, Instruction *CxtI) const;
  695. /// Maximum size of array considered when transforming.
  696. uint64_t MaxArraySizeForCombine = 0;
  697. private:
  698. /// Performs a few simplifications for operators which are associative
  699. /// or commutative.
  700. bool SimplifyAssociativeOrCommutative(BinaryOperator &I);
  701. /// Tries to simplify binary operations which some other binary
  702. /// operation distributes over.
  703. ///
  704. /// It does this by either by factorizing out common terms (eg "(A*B)+(A*C)"
  705. /// -> "A*(B+C)") or expanding out if this results in simplifications (eg: "A
  706. /// & (B | C) -> (A&B) | (A&C)" if this is a win). Returns the simplified
  707. /// value, or null if it didn't simplify.
  708. Value *SimplifyUsingDistributiveLaws(BinaryOperator &I);
  709. /// Tries to simplify add operations using the definition of remainder.
  710. ///
  711. /// The definition of remainder is X % C = X - (X / C ) * C. The add
  712. /// expression X % C0 + (( X / C0 ) % C1) * C0 can be simplified to
  713. /// X % (C0 * C1)
  714. Value *SimplifyAddWithRemainder(BinaryOperator &I);
  715. // Binary Op helper for select operations where the expression can be
  716. // efficiently reorganized.
  717. Value *SimplifySelectsFeedingBinaryOp(BinaryOperator &I, Value *LHS,
  718. Value *RHS);
  719. /// This tries to simplify binary operations by factorizing out common terms
  720. /// (e. g. "(A*B)+(A*C)" -> "A*(B+C)").
  721. Value *tryFactorization(BinaryOperator &, Instruction::BinaryOps, Value *,
  722. Value *, Value *, Value *);
  723. /// Match a select chain which produces one of three values based on whether
  724. /// the LHS is less than, equal to, or greater than RHS respectively.
  725. /// Return true if we matched a three way compare idiom. The LHS, RHS, Less,
  726. /// Equal and Greater values are saved in the matching process and returned to
  727. /// the caller.
  728. bool matchThreeWayIntCompare(SelectInst *SI, Value *&LHS, Value *&RHS,
  729. ConstantInt *&Less, ConstantInt *&Equal,
  730. ConstantInt *&Greater);
  731. /// Attempts to replace V with a simpler value based on the demanded
  732. /// bits.
  733. Value *SimplifyDemandedUseBits(Value *V, APInt DemandedMask, KnownBits &Known,
  734. unsigned Depth, Instruction *CxtI);
  735. bool SimplifyDemandedBits(Instruction *I, unsigned Op,
  736. const APInt &DemandedMask, KnownBits &Known,
  737. unsigned Depth = 0);
  738. /// Helper routine of SimplifyDemandedUseBits. It computes KnownZero/KnownOne
  739. /// bits. It also tries to handle simplifications that can be done based on
  740. /// DemandedMask, but without modifying the Instruction.
  741. Value *SimplifyMultipleUseDemandedBits(Instruction *I,
  742. const APInt &DemandedMask,
  743. KnownBits &Known,
  744. unsigned Depth, Instruction *CxtI);
  745. /// Helper routine of SimplifyDemandedUseBits. It tries to simplify demanded
  746. /// bit for "r1 = shr x, c1; r2 = shl r1, c2" instruction sequence.
  747. Value *simplifyShrShlDemandedBits(
  748. Instruction *Shr, const APInt &ShrOp1, Instruction *Shl,
  749. const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known);
  750. /// Tries to simplify operands to an integer instruction based on its
  751. /// demanded bits.
  752. bool SimplifyDemandedInstructionBits(Instruction &Inst);
  753. Value *simplifyAMDGCNMemoryIntrinsicDemanded(IntrinsicInst *II,
  754. APInt DemandedElts,
  755. int DmaskIdx = -1);
  756. Value *SimplifyDemandedVectorElts(Value *V, APInt DemandedElts,
  757. APInt &UndefElts, unsigned Depth = 0,
  758. bool AllowMultipleUsers = false);
  759. /// Canonicalize the position of binops relative to shufflevector.
  760. Instruction *foldVectorBinop(BinaryOperator &Inst);
  761. /// Given a binary operator, cast instruction, or select which has a PHI node
  762. /// as operand #0, see if we can fold the instruction into the PHI (which is
  763. /// only possible if all operands to the PHI are constants).
  764. Instruction *foldOpIntoPhi(Instruction &I, PHINode *PN);
  765. /// Given an instruction with a select as one operand and a constant as the
  766. /// other operand, try to fold the binary operator into the select arguments.
  767. /// This also works for Cast instructions, which obviously do not have a
  768. /// second operand.
  769. Instruction *FoldOpIntoSelect(Instruction &Op, SelectInst *SI);
  770. /// This is a convenience wrapper function for the above two functions.
  771. Instruction *foldBinOpIntoSelectOrPhi(BinaryOperator &I);
  772. Instruction *foldAddWithConstant(BinaryOperator &Add);
  773. /// Try to rotate an operation below a PHI node, using PHI nodes for
  774. /// its operands.
  775. Instruction *FoldPHIArgOpIntoPHI(PHINode &PN);
  776. Instruction *FoldPHIArgBinOpIntoPHI(PHINode &PN);
  777. Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);
  778. Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);
  779. Instruction *FoldPHIArgZextsIntoPHI(PHINode &PN);
  780. /// If an integer typed PHI has only one use which is an IntToPtr operation,
  781. /// replace the PHI with an existing pointer typed PHI if it exists. Otherwise
  782. /// insert a new pointer typed PHI and replace the original one.
  783. Instruction *FoldIntegerTypedPHI(PHINode &PN);
  784. /// Helper function for FoldPHIArgXIntoPHI() to set debug location for the
  785. /// folded operation.
  786. void PHIArgMergedDebugLoc(Instruction *Inst, PHINode &PN);
  787. Instruction *foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
  788. ICmpInst::Predicate Cond, Instruction &I);
  789. Instruction *foldAllocaCmp(ICmpInst &ICI, const AllocaInst *Alloca,
  790. const Value *Other);
  791. Instruction *foldCmpLoadFromIndexedGlobal(GetElementPtrInst *GEP,
  792. GlobalVariable *GV, CmpInst &ICI,
  793. ConstantInt *AndCst = nullptr);
  794. Instruction *foldFCmpIntToFPConst(FCmpInst &I, Instruction *LHSI,
  795. Constant *RHSC);
  796. Instruction *foldICmpAddOpConst(Value *X, const APInt &C,
  797. ICmpInst::Predicate Pred);
  798. Instruction *foldICmpWithCastOp(ICmpInst &ICI);
  799. Instruction *foldICmpUsingKnownBits(ICmpInst &Cmp);
  800. Instruction *foldICmpWithDominatingICmp(ICmpInst &Cmp);
  801. Instruction *foldICmpWithConstant(ICmpInst &Cmp);
  802. Instruction *foldICmpInstWithConstant(ICmpInst &Cmp);
  803. Instruction *foldICmpInstWithConstantNotInt(ICmpInst &Cmp);
  804. Instruction *foldICmpBinOp(ICmpInst &Cmp, const SimplifyQuery &SQ);
  805. Instruction *foldICmpEquality(ICmpInst &Cmp);
  806. Instruction *foldIRemByPowerOfTwoToBitTest(ICmpInst &I);
  807. Instruction *foldSignBitTest(ICmpInst &I);
  808. Instruction *foldICmpWithZero(ICmpInst &Cmp);
  809. Value *foldUnsignedMultiplicationOverflowCheck(ICmpInst &Cmp);
  810. Instruction *foldICmpSelectConstant(ICmpInst &Cmp, SelectInst *Select,
  811. ConstantInt *C);
  812. Instruction *foldICmpTruncConstant(ICmpInst &Cmp, TruncInst *Trunc,
  813. const APInt &C);
  814. Instruction *foldICmpAndConstant(ICmpInst &Cmp, BinaryOperator *And,
  815. const APInt &C);
  816. Instruction *foldICmpXorConstant(ICmpInst &Cmp, BinaryOperator *Xor,
  817. const APInt &C);
  818. Instruction *foldICmpOrConstant(ICmpInst &Cmp, BinaryOperator *Or,
  819. const APInt &C);
  820. Instruction *foldICmpMulConstant(ICmpInst &Cmp, BinaryOperator *Mul,
  821. const APInt &C);
  822. Instruction *foldICmpShlConstant(ICmpInst &Cmp, BinaryOperator *Shl,
  823. const APInt &C);
  824. Instruction *foldICmpShrConstant(ICmpInst &Cmp, BinaryOperator *Shr,
  825. const APInt &C);
  826. Instruction *foldICmpSRemConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
  827. const APInt &C);
  828. Instruction *foldICmpUDivConstant(ICmpInst &Cmp, BinaryOperator *UDiv,
  829. const APInt &C);
  830. Instruction *foldICmpDivConstant(ICmpInst &Cmp, BinaryOperator *Div,
  831. const APInt &C);
  832. Instruction *foldICmpSubConstant(ICmpInst &Cmp, BinaryOperator *Sub,
  833. const APInt &C);
  834. Instruction *foldICmpAddConstant(ICmpInst &Cmp, BinaryOperator *Add,
  835. const APInt &C);
  836. Instruction *foldICmpAndConstConst(ICmpInst &Cmp, BinaryOperator *And,
  837. const APInt &C1);
  838. Instruction *foldICmpAndShift(ICmpInst &Cmp, BinaryOperator *And,
  839. const APInt &C1, const APInt &C2);
  840. Instruction *foldICmpShrConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
  841. const APInt &C2);
  842. Instruction *foldICmpShlConstConst(ICmpInst &I, Value *ShAmt, const APInt &C1,
  843. const APInt &C2);
  844. Instruction *foldICmpBinOpEqualityWithConstant(ICmpInst &Cmp,
  845. BinaryOperator *BO,
  846. const APInt &C);
  847. Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
  848. const APInt &C);
  849. Instruction *foldICmpEqIntrinsicWithConstant(ICmpInst &ICI, IntrinsicInst *II,
  850. const APInt &C);
  851. // Helpers of visitSelectInst().
  852. Instruction *foldSelectExtConst(SelectInst &Sel);
  853. Instruction *foldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI);
  854. Instruction *foldSelectIntoOp(SelectInst &SI, Value *, Value *);
  855. Instruction *foldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1,
  856. Value *A, Value *B, Instruction &Outer,
  857. SelectPatternFlavor SPF2, Value *C);
  858. Instruction *foldSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI);
  859. Instruction *OptAndOp(BinaryOperator *Op, ConstantInt *OpRHS,
  860. ConstantInt *AndRHS, BinaryOperator &TheAnd);
  861. Value *insertRangeTest(Value *V, const APInt &Lo, const APInt &Hi,
  862. bool isSigned, bool Inside);
  863. Instruction *PromoteCastOfAllocation(BitCastInst &CI, AllocaInst &AI);
  864. bool mergeStoreIntoSuccessor(StoreInst &SI);
  865. /// Given an 'or' instruction, check to see if it is part of a bswap idiom.
  866. /// If so, return the equivalent bswap intrinsic.
  867. Instruction *matchBSwap(BinaryOperator &Or);
  868. Instruction *SimplifyAnyMemTransfer(AnyMemTransferInst *MI);
  869. Instruction *SimplifyAnyMemSet(AnyMemSetInst *MI);
  870. Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
  871. /// Returns a value X such that Val = X * Scale, or null if none.
  872. ///
  873. /// If the multiplication is known not to overflow then NoSignedWrap is set.
  874. Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap);
  875. };
  876. } // end namespace llvm
  877. #undef DEBUG_TYPE
  878. #endif // LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H