InstCombineMulDivRem.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  1. //===- InstCombineMulDivRem.cpp -------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv,
  11. // srem, urem, frem.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "InstCombineInternal.h"
  15. #include "llvm/Analysis/InstructionSimplify.h"
  16. #include "llvm/IR/IntrinsicInst.h"
  17. #include "llvm/IR/PatternMatch.h"
  18. using namespace llvm;
  19. using namespace PatternMatch;
  20. #define DEBUG_TYPE "instcombine"
  21. /// simplifyValueKnownNonZero - The specific integer value is used in a context
  22. /// where it is known to be non-zero. If this allows us to simplify the
  23. /// computation, do so and return the new operand, otherwise return null.
  24. static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC,
  25. Instruction &CxtI) {
  26. // If V has multiple uses, then we would have to do more analysis to determine
  27. // if this is safe. For example, the use could be in dynamically unreached
  28. // code.
  29. if (!V->hasOneUse()) return nullptr;
  30. bool MadeChange = false;
  31. // ((1 << A) >>u B) --> (1 << (A-B))
  32. // Because V cannot be zero, we know that B is less than A.
  33. Value *A = nullptr, *B = nullptr, *One = nullptr;
  34. if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) &&
  35. match(One, m_One())) {
  36. A = IC.Builder->CreateSub(A, B);
  37. return IC.Builder->CreateShl(One, A);
  38. }
  39. // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
  40. // inexact. Similarly for <<.
  41. if (BinaryOperator *I = dyn_cast<BinaryOperator>(V))
  42. if (I->isLogicalShift() &&
  43. isKnownToBeAPowerOfTwo(I->getOperand(0), IC.getDataLayout(), false, 0,
  44. IC.getAssumptionCache(), &CxtI,
  45. IC.getDominatorTree())) {
  46. // We know that this is an exact/nuw shift and that the input is a
  47. // non-zero context as well.
  48. if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) {
  49. I->setOperand(0, V2);
  50. MadeChange = true;
  51. }
  52. if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
  53. I->setIsExact();
  54. MadeChange = true;
  55. }
  56. if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
  57. I->setHasNoUnsignedWrap();
  58. MadeChange = true;
  59. }
  60. }
  61. // TODO: Lots more we could do here:
  62. // If V is a phi node, we can call this on each of its operands.
  63. // "select cond, X, 0" can simplify to "X".
  64. return MadeChange ? V : nullptr;
  65. }
  66. /// MultiplyOverflows - True if the multiply can not be expressed in an int
  67. /// this size.
  68. static bool MultiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product,
  69. bool IsSigned) {
  70. bool Overflow;
  71. if (IsSigned)
  72. Product = C1.smul_ov(C2, Overflow);
  73. else
  74. Product = C1.umul_ov(C2, Overflow);
  75. return Overflow;
  76. }
  77. /// \brief True if C2 is a multiple of C1. Quotient contains C2/C1.
  78. static bool IsMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
  79. bool IsSigned) {
  80. assert(C1.getBitWidth() == C2.getBitWidth() &&
  81. "Inconsistent width of constants!");
  82. APInt Remainder(C1.getBitWidth(), /*Val=*/0ULL, IsSigned);
  83. if (IsSigned)
  84. APInt::sdivrem(C1, C2, Quotient, Remainder);
  85. else
  86. APInt::udivrem(C1, C2, Quotient, Remainder);
  87. return Remainder.isMinValue();
  88. }
  89. /// \brief A helper routine of InstCombiner::visitMul().
  90. ///
  91. /// If C is a vector of known powers of 2, then this function returns
  92. /// a new vector obtained from C replacing each element with its logBase2.
  93. /// Return a null pointer otherwise.
  94. static Constant *getLogBase2Vector(ConstantDataVector *CV) {
  95. const APInt *IVal;
  96. SmallVector<Constant *, 4> Elts;
  97. for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) {
  98. Constant *Elt = CV->getElementAsConstant(I);
  99. if (!match(Elt, m_APInt(IVal)) || !IVal->isPowerOf2())
  100. return nullptr;
  101. Elts.push_back(ConstantInt::get(Elt->getType(), IVal->logBase2()));
  102. }
  103. return ConstantVector::get(Elts);
  104. }
  105. /// \brief Return true if we can prove that:
  106. /// (mul LHS, RHS) === (mul nsw LHS, RHS)
  107. bool InstCombiner::WillNotOverflowSignedMul(Value *LHS, Value *RHS,
  108. Instruction &CxtI) {
  109. // Multiplying n * m significant bits yields a result of n + m significant
  110. // bits. If the total number of significant bits does not exceed the
  111. // result bit width (minus 1), there is no overflow.
  112. // This means if we have enough leading sign bits in the operands
  113. // we can guarantee that the result does not overflow.
  114. // Ref: "Hacker's Delight" by Henry Warren
  115. unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
  116. // Note that underestimating the number of sign bits gives a more
  117. // conservative answer.
  118. unsigned SignBits =
  119. ComputeNumSignBits(LHS, 0, &CxtI) + ComputeNumSignBits(RHS, 0, &CxtI);
  120. // First handle the easy case: if we have enough sign bits there's
  121. // definitely no overflow.
  122. if (SignBits > BitWidth + 1)
  123. return true;
  124. // There are two ambiguous cases where there can be no overflow:
  125. // SignBits == BitWidth + 1 and
  126. // SignBits == BitWidth
  127. // The second case is difficult to check, therefore we only handle the
  128. // first case.
  129. if (SignBits == BitWidth + 1) {
  130. // It overflows only when both arguments are negative and the true
  131. // product is exactly the minimum negative number.
  132. // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
  133. // For simplicity we just check if at least one side is not negative.
  134. bool LHSNonNegative, LHSNegative;
  135. bool RHSNonNegative, RHSNegative;
  136. ComputeSignBit(LHS, LHSNonNegative, LHSNegative, /*Depth=*/0, &CxtI);
  137. ComputeSignBit(RHS, RHSNonNegative, RHSNegative, /*Depth=*/0, &CxtI);
  138. if (LHSNonNegative || RHSNonNegative)
  139. return true;
  140. }
  141. return false;
  142. }
  143. Instruction *InstCombiner::visitMul(BinaryOperator &I) {
  144. bool Changed = SimplifyAssociativeOrCommutative(I);
  145. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  146. if (Value *V = SimplifyVectorOp(I))
  147. return ReplaceInstUsesWith(I, V);
  148. if (Value *V = SimplifyMulInst(Op0, Op1, DL, TLI, DT, AC))
  149. return ReplaceInstUsesWith(I, V);
  150. if (Value *V = SimplifyUsingDistributiveLaws(I))
  151. return ReplaceInstUsesWith(I, V);
  152. // X * -1 == 0 - X
  153. if (match(Op1, m_AllOnes())) {
  154. BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName());
  155. if (I.hasNoSignedWrap())
  156. BO->setHasNoSignedWrap();
  157. return BO;
  158. }
  159. // Also allow combining multiply instructions on vectors.
  160. {
  161. Value *NewOp;
  162. Constant *C1, *C2;
  163. const APInt *IVal;
  164. if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
  165. m_Constant(C1))) &&
  166. match(C1, m_APInt(IVal))) {
  167. // ((X << C2)*C1) == (X * (C1 << C2))
  168. Constant *Shl = ConstantExpr::getShl(C1, C2);
  169. BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0));
  170. BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl);
  171. if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap())
  172. BO->setHasNoUnsignedWrap();
  173. if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() &&
  174. Shl->isNotMinSignedValue())
  175. BO->setHasNoSignedWrap();
  176. return BO;
  177. }
  178. if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
  179. Constant *NewCst = nullptr;
  180. if (match(C1, m_APInt(IVal)) && IVal->isPowerOf2())
  181. // Replace X*(2^C) with X << C, where C is either a scalar or a splat.
  182. NewCst = ConstantInt::get(NewOp->getType(), IVal->logBase2());
  183. else if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(C1))
  184. // Replace X*(2^C) with X << C, where C is a vector of known
  185. // constant powers of 2.
  186. NewCst = getLogBase2Vector(CV);
  187. if (NewCst) {
  188. unsigned Width = NewCst->getType()->getPrimitiveSizeInBits();
  189. BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
  190. if (I.hasNoUnsignedWrap())
  191. Shl->setHasNoUnsignedWrap();
  192. if (I.hasNoSignedWrap()) {
  193. uint64_t V;
  194. if (match(NewCst, m_ConstantInt(V)) && V != Width - 1)
  195. Shl->setHasNoSignedWrap();
  196. }
  197. return Shl;
  198. }
  199. }
  200. }
  201. if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
  202. // (Y - X) * (-(2**n)) -> (X - Y) * (2**n), for positive nonzero n
  203. // (Y + const) * (-(2**n)) -> (-constY) * (2**n), for positive nonzero n
  204. // The "* (2**n)" thus becomes a potential shifting opportunity.
  205. {
  206. const APInt & Val = CI->getValue();
  207. const APInt &PosVal = Val.abs();
  208. if (Val.isNegative() && PosVal.isPowerOf2()) {
  209. Value *X = nullptr, *Y = nullptr;
  210. if (Op0->hasOneUse()) {
  211. ConstantInt *C1;
  212. Value *Sub = nullptr;
  213. if (match(Op0, m_Sub(m_Value(Y), m_Value(X))))
  214. Sub = Builder->CreateSub(X, Y, "suba");
  215. else if (match(Op0, m_Add(m_Value(Y), m_ConstantInt(C1))))
  216. Sub = Builder->CreateSub(Builder->CreateNeg(C1), Y, "subc");
  217. if (Sub)
  218. return
  219. BinaryOperator::CreateMul(Sub,
  220. ConstantInt::get(Y->getType(), PosVal));
  221. }
  222. }
  223. }
  224. }
  225. // Simplify mul instructions with a constant RHS.
  226. if (isa<Constant>(Op1)) {
  227. // Try to fold constant mul into select arguments.
  228. if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
  229. if (Instruction *R = FoldOpIntoSelect(I, SI))
  230. return R;
  231. if (isa<PHINode>(Op0))
  232. if (Instruction *NV = FoldOpIntoPhi(I))
  233. return NV;
  234. // Canonicalize (X+C1)*CI -> X*CI+C1*CI.
  235. {
  236. Value *X;
  237. Constant *C1;
  238. if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
  239. Value *Mul = Builder->CreateMul(C1, Op1);
  240. // Only go forward with the transform if C1*CI simplifies to a tidier
  241. // constant.
  242. if (!match(Mul, m_Mul(m_Value(), m_Value())))
  243. return BinaryOperator::CreateAdd(Builder->CreateMul(X, Op1), Mul);
  244. }
  245. }
  246. }
  247. if (Value *Op0v = dyn_castNegVal(Op0)) { // -X * -Y = X*Y
  248. if (Value *Op1v = dyn_castNegVal(Op1)) {
  249. BinaryOperator *BO = BinaryOperator::CreateMul(Op0v, Op1v);
  250. if (I.hasNoSignedWrap() &&
  251. match(Op0, m_NSWSub(m_Value(), m_Value())) &&
  252. match(Op1, m_NSWSub(m_Value(), m_Value())))
  253. BO->setHasNoSignedWrap();
  254. return BO;
  255. }
  256. }
  257. // (X / Y) * Y = X - (X % Y)
  258. // (X / Y) * -Y = (X % Y) - X
  259. {
  260. Value *Op1C = Op1;
  261. BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0);
  262. if (!BO ||
  263. (BO->getOpcode() != Instruction::UDiv &&
  264. BO->getOpcode() != Instruction::SDiv)) {
  265. Op1C = Op0;
  266. BO = dyn_cast<BinaryOperator>(Op1);
  267. }
  268. Value *Neg = dyn_castNegVal(Op1C);
  269. if (BO && BO->hasOneUse() &&
  270. (BO->getOperand(1) == Op1C || BO->getOperand(1) == Neg) &&
  271. (BO->getOpcode() == Instruction::UDiv ||
  272. BO->getOpcode() == Instruction::SDiv)) {
  273. Value *Op0BO = BO->getOperand(0), *Op1BO = BO->getOperand(1);
  274. // If the division is exact, X % Y is zero, so we end up with X or -X.
  275. if (PossiblyExactOperator *SDiv = dyn_cast<PossiblyExactOperator>(BO))
  276. if (SDiv->isExact()) {
  277. if (Op1BO == Op1C)
  278. return ReplaceInstUsesWith(I, Op0BO);
  279. return BinaryOperator::CreateNeg(Op0BO);
  280. }
  281. Value *Rem;
  282. if (BO->getOpcode() == Instruction::UDiv)
  283. Rem = Builder->CreateURem(Op0BO, Op1BO);
  284. else
  285. Rem = Builder->CreateSRem(Op0BO, Op1BO);
  286. Rem->takeName(BO);
  287. if (Op1BO == Op1C)
  288. return BinaryOperator::CreateSub(Op0BO, Rem);
  289. return BinaryOperator::CreateSub(Rem, Op0BO);
  290. }
  291. }
  292. /// i1 mul -> i1 and.
  293. if (I.getType()->getScalarType()->isIntegerTy(1))
  294. return BinaryOperator::CreateAnd(Op0, Op1);
  295. // X*(1 << Y) --> X << Y
  296. // (1 << Y)*X --> X << Y
  297. {
  298. Value *Y;
  299. BinaryOperator *BO = nullptr;
  300. bool ShlNSW = false;
  301. if (match(Op0, m_Shl(m_One(), m_Value(Y)))) {
  302. BO = BinaryOperator::CreateShl(Op1, Y);
  303. ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
  304. } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) {
  305. BO = BinaryOperator::CreateShl(Op0, Y);
  306. ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
  307. }
  308. if (BO) {
  309. if (I.hasNoUnsignedWrap())
  310. BO->setHasNoUnsignedWrap();
  311. if (I.hasNoSignedWrap() && ShlNSW)
  312. BO->setHasNoSignedWrap();
  313. return BO;
  314. }
  315. }
  316. // If one of the operands of the multiply is a cast from a boolean value, then
  317. // we know the bool is either zero or one, so this is a 'masking' multiply.
  318. // X * Y (where Y is 0 or 1) -> X & (0-Y)
  319. if (!I.getType()->isVectorTy()) {
  320. // -2 is "-1 << 1" so it is all bits set except the low one.
  321. APInt Negative2(I.getType()->getPrimitiveSizeInBits(), (uint64_t)-2, true);
  322. Value *BoolCast = nullptr, *OtherOp = nullptr;
  323. if (MaskedValueIsZero(Op0, Negative2, 0, &I))
  324. BoolCast = Op0, OtherOp = Op1;
  325. else if (MaskedValueIsZero(Op1, Negative2, 0, &I))
  326. BoolCast = Op1, OtherOp = Op0;
  327. if (BoolCast) {
  328. Value *V = Builder->CreateSub(Constant::getNullValue(I.getType()),
  329. BoolCast);
  330. return BinaryOperator::CreateAnd(V, OtherOp);
  331. }
  332. }
  333. if (!I.hasNoSignedWrap() && WillNotOverflowSignedMul(Op0, Op1, I)) {
  334. Changed = true;
  335. I.setHasNoSignedWrap(true);
  336. }
  337. if (!I.hasNoUnsignedWrap() &&
  338. computeOverflowForUnsignedMul(Op0, Op1, &I) ==
  339. OverflowResult::NeverOverflows) {
  340. Changed = true;
  341. I.setHasNoUnsignedWrap(true);
  342. }
  343. return Changed ? &I : nullptr;
  344. }
  345. /// Detect pattern log2(Y * 0.5) with corresponding fast math flags.
  346. static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) {
  347. if (!Op->hasOneUse())
  348. return;
  349. IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op);
  350. if (!II)
  351. return;
  352. if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra())
  353. return;
  354. Log2 = II;
  355. Value *OpLog2Of = II->getArgOperand(0);
  356. if (!OpLog2Of->hasOneUse())
  357. return;
  358. Instruction *I = dyn_cast<Instruction>(OpLog2Of);
  359. if (!I)
  360. return;
  361. if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra())
  362. return;
  363. if (match(I->getOperand(0), m_SpecificFP(0.5)))
  364. Y = I->getOperand(1);
  365. else if (match(I->getOperand(1), m_SpecificFP(0.5)))
  366. Y = I->getOperand(0);
  367. }
  368. static bool isFiniteNonZeroFp(Constant *C) {
  369. if (C->getType()->isVectorTy()) {
  370. for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E;
  371. ++I) {
  372. ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getAggregateElement(I));
  373. if (!CFP || !CFP->getValueAPF().isFiniteNonZero())
  374. return false;
  375. }
  376. return true;
  377. }
  378. return isa<ConstantFP>(C) &&
  379. cast<ConstantFP>(C)->getValueAPF().isFiniteNonZero();
  380. }
  381. static bool isNormalFp(Constant *C) {
  382. if (C->getType()->isVectorTy()) {
  383. for (unsigned I = 0, E = C->getType()->getVectorNumElements(); I != E;
  384. ++I) {
  385. ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(C->getAggregateElement(I));
  386. if (!CFP || !CFP->getValueAPF().isNormal())
  387. return false;
  388. }
  389. return true;
  390. }
  391. return isa<ConstantFP>(C) && cast<ConstantFP>(C)->getValueAPF().isNormal();
  392. }
  393. /// Helper function of InstCombiner::visitFMul(BinaryOperator(). It returns
  394. /// true iff the given value is FMul or FDiv with one and only one operand
  395. /// being a normal constant (i.e. not Zero/NaN/Infinity).
  396. static bool isFMulOrFDivWithConstant(Value *V) {
  397. Instruction *I = dyn_cast<Instruction>(V);
  398. if (!I || (I->getOpcode() != Instruction::FMul &&
  399. I->getOpcode() != Instruction::FDiv))
  400. return false;
  401. Constant *C0 = dyn_cast<Constant>(I->getOperand(0));
  402. Constant *C1 = dyn_cast<Constant>(I->getOperand(1));
  403. if (C0 && C1)
  404. return false;
  405. return (C0 && isFiniteNonZeroFp(C0)) || (C1 && isFiniteNonZeroFp(C1));
  406. }
  407. /// foldFMulConst() is a helper routine of InstCombiner::visitFMul().
  408. /// The input \p FMulOrDiv is a FMul/FDiv with one and only one operand
  409. /// being a constant (i.e. isFMulOrFDivWithConstant(FMulOrDiv) == true).
  410. /// This function is to simplify "FMulOrDiv * C" and returns the
  411. /// resulting expression. Note that this function could return NULL in
  412. /// case the constants cannot be folded into a normal floating-point.
  413. ///
  414. Value *InstCombiner::foldFMulConst(Instruction *FMulOrDiv, Constant *C,
  415. Instruction *InsertBefore) {
  416. assert(isFMulOrFDivWithConstant(FMulOrDiv) && "V is invalid");
  417. Value *Opnd0 = FMulOrDiv->getOperand(0);
  418. Value *Opnd1 = FMulOrDiv->getOperand(1);
  419. Constant *C0 = dyn_cast<Constant>(Opnd0);
  420. Constant *C1 = dyn_cast<Constant>(Opnd1);
  421. BinaryOperator *R = nullptr;
  422. // (X * C0) * C => X * (C0*C)
  423. if (FMulOrDiv->getOpcode() == Instruction::FMul) {
  424. Constant *F = ConstantExpr::getFMul(C1 ? C1 : C0, C);
  425. if (isNormalFp(F))
  426. R = BinaryOperator::CreateFMul(C1 ? Opnd0 : Opnd1, F);
  427. } else {
  428. if (C0) {
  429. // (C0 / X) * C => (C0 * C) / X
  430. if (FMulOrDiv->hasOneUse()) {
  431. // It would otherwise introduce another div.
  432. Constant *F = ConstantExpr::getFMul(C0, C);
  433. if (isNormalFp(F))
  434. R = BinaryOperator::CreateFDiv(F, Opnd1);
  435. }
  436. } else {
  437. // (X / C1) * C => X * (C/C1) if C/C1 is not a denormal
  438. Constant *F = ConstantExpr::getFDiv(C, C1);
  439. if (isNormalFp(F)) {
  440. R = BinaryOperator::CreateFMul(Opnd0, F);
  441. } else {
  442. // (X / C1) * C => X / (C1/C)
  443. Constant *F = ConstantExpr::getFDiv(C1, C);
  444. if (isNormalFp(F))
  445. R = BinaryOperator::CreateFDiv(Opnd0, F);
  446. }
  447. }
  448. }
  449. if (R) {
  450. R->setHasUnsafeAlgebra(true);
  451. InsertNewInstWith(R, *InsertBefore);
  452. }
  453. return R;
  454. }
  455. Instruction *InstCombiner::visitFMul(BinaryOperator &I) {
  456. bool Changed = SimplifyAssociativeOrCommutative(I);
  457. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  458. if (Value *V = SimplifyVectorOp(I))
  459. return ReplaceInstUsesWith(I, V);
  460. if (isa<Constant>(Op0))
  461. std::swap(Op0, Op1);
  462. if (Value *V =
  463. SimplifyFMulInst(Op0, Op1, I.getFastMathFlags(), DL, TLI, DT, AC))
  464. return ReplaceInstUsesWith(I, V);
  465. bool AllowReassociate = I.hasUnsafeAlgebra();
  466. // Simplify mul instructions with a constant RHS.
  467. if (isa<Constant>(Op1)) {
  468. // Try to fold constant mul into select arguments.
  469. if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
  470. if (Instruction *R = FoldOpIntoSelect(I, SI))
  471. return R;
  472. if (isa<PHINode>(Op0))
  473. if (Instruction *NV = FoldOpIntoPhi(I))
  474. return NV;
  475. // (fmul X, -1.0) --> (fsub -0.0, X)
  476. if (match(Op1, m_SpecificFP(-1.0))) {
  477. Constant *NegZero = ConstantFP::getNegativeZero(Op1->getType());
  478. Instruction *RI = BinaryOperator::CreateFSub(NegZero, Op0);
  479. RI->copyFastMathFlags(&I);
  480. return RI;
  481. }
  482. Constant *C = cast<Constant>(Op1);
  483. if (AllowReassociate && isFiniteNonZeroFp(C)) {
  484. // Let MDC denote an expression in one of these forms:
  485. // X * C, C/X, X/C, where C is a constant.
  486. //
  487. // Try to simplify "MDC * Constant"
  488. if (isFMulOrFDivWithConstant(Op0))
  489. if (Value *V = foldFMulConst(cast<Instruction>(Op0), C, &I))
  490. return ReplaceInstUsesWith(I, V);
  491. // (MDC +/- C1) * C => (MDC * C) +/- (C1 * C)
  492. Instruction *FAddSub = dyn_cast<Instruction>(Op0);
  493. if (FAddSub &&
  494. (FAddSub->getOpcode() == Instruction::FAdd ||
  495. FAddSub->getOpcode() == Instruction::FSub)) {
  496. Value *Opnd0 = FAddSub->getOperand(0);
  497. Value *Opnd1 = FAddSub->getOperand(1);
  498. Constant *C0 = dyn_cast<Constant>(Opnd0);
  499. Constant *C1 = dyn_cast<Constant>(Opnd1);
  500. bool Swap = false;
  501. if (C0) {
  502. std::swap(C0, C1);
  503. std::swap(Opnd0, Opnd1);
  504. Swap = true;
  505. }
  506. if (C1 && isFiniteNonZeroFp(C1) && isFMulOrFDivWithConstant(Opnd0)) {
  507. Value *M1 = ConstantExpr::getFMul(C1, C);
  508. Value *M0 = isNormalFp(cast<Constant>(M1)) ?
  509. foldFMulConst(cast<Instruction>(Opnd0), C, &I) :
  510. nullptr;
  511. if (M0 && M1) {
  512. if (Swap && FAddSub->getOpcode() == Instruction::FSub)
  513. std::swap(M0, M1);
  514. Instruction *RI = (FAddSub->getOpcode() == Instruction::FAdd)
  515. ? BinaryOperator::CreateFAdd(M0, M1)
  516. : BinaryOperator::CreateFSub(M0, M1);
  517. RI->copyFastMathFlags(&I);
  518. return RI;
  519. }
  520. }
  521. }
  522. }
  523. }
  524. // sqrt(X) * sqrt(X) -> X
  525. if (AllowReassociate && (Op0 == Op1))
  526. if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op0))
  527. if (II->getIntrinsicID() == Intrinsic::sqrt)
  528. return ReplaceInstUsesWith(I, II->getOperand(0));
  529. // Under unsafe algebra do:
  530. // X * log2(0.5*Y) = X*log2(Y) - X
  531. if (AllowReassociate) {
  532. Value *OpX = nullptr;
  533. Value *OpY = nullptr;
  534. IntrinsicInst *Log2;
  535. detectLog2OfHalf(Op0, OpY, Log2);
  536. if (OpY) {
  537. OpX = Op1;
  538. } else {
  539. detectLog2OfHalf(Op1, OpY, Log2);
  540. if (OpY) {
  541. OpX = Op0;
  542. }
  543. }
  544. // if pattern detected emit alternate sequence
  545. if (OpX && OpY) {
  546. BuilderTy::FastMathFlagGuard Guard(*Builder);
  547. Builder->SetFastMathFlags(Log2->getFastMathFlags());
  548. Log2->setArgOperand(0, OpY);
  549. Value *FMulVal = Builder->CreateFMul(OpX, Log2);
  550. Value *FSub = Builder->CreateFSub(FMulVal, OpX);
  551. FSub->takeName(&I);
  552. return ReplaceInstUsesWith(I, FSub);
  553. }
  554. }
  555. // Handle symmetric situation in a 2-iteration loop
  556. Value *Opnd0 = Op0;
  557. Value *Opnd1 = Op1;
  558. for (int i = 0; i < 2; i++) {
  559. bool IgnoreZeroSign = I.hasNoSignedZeros();
  560. if (BinaryOperator::isFNeg(Opnd0, IgnoreZeroSign)) {
  561. BuilderTy::FastMathFlagGuard Guard(*Builder);
  562. Builder->SetFastMathFlags(I.getFastMathFlags());
  563. Value *N0 = dyn_castFNegVal(Opnd0, IgnoreZeroSign);
  564. Value *N1 = dyn_castFNegVal(Opnd1, IgnoreZeroSign);
  565. // -X * -Y => X*Y
  566. if (N1) {
  567. Value *FMul = Builder->CreateFMul(N0, N1);
  568. FMul->takeName(&I);
  569. return ReplaceInstUsesWith(I, FMul);
  570. }
  571. if (Opnd0->hasOneUse()) {
  572. // -X * Y => -(X*Y) (Promote negation as high as possible)
  573. Value *T = Builder->CreateFMul(N0, Opnd1);
  574. Value *Neg = Builder->CreateFNeg(T);
  575. Neg->takeName(&I);
  576. return ReplaceInstUsesWith(I, Neg);
  577. }
  578. }
  579. // (X*Y) * X => (X*X) * Y where Y != X
  580. // The purpose is two-fold:
  581. // 1) to form a power expression (of X).
  582. // 2) potentially shorten the critical path: After transformation, the
  583. // latency of the instruction Y is amortized by the expression of X*X,
  584. // and therefore Y is in a "less critical" position compared to what it
  585. // was before the transformation.
  586. //
  587. if (AllowReassociate) {
  588. Value *Opnd0_0, *Opnd0_1;
  589. if (Opnd0->hasOneUse() &&
  590. match(Opnd0, m_FMul(m_Value(Opnd0_0), m_Value(Opnd0_1)))) {
  591. Value *Y = nullptr;
  592. if (Opnd0_0 == Opnd1 && Opnd0_1 != Opnd1)
  593. Y = Opnd0_1;
  594. else if (Opnd0_1 == Opnd1 && Opnd0_0 != Opnd1)
  595. Y = Opnd0_0;
  596. if (Y) {
  597. BuilderTy::FastMathFlagGuard Guard(*Builder);
  598. Builder->SetFastMathFlags(I.getFastMathFlags());
  599. Value *T = Builder->CreateFMul(Opnd1, Opnd1);
  600. Value *R = Builder->CreateFMul(T, Y);
  601. R->takeName(&I);
  602. return ReplaceInstUsesWith(I, R);
  603. }
  604. }
  605. }
  606. if (!isa<Constant>(Op1))
  607. std::swap(Opnd0, Opnd1);
  608. else
  609. break;
  610. }
  611. return Changed ? &I : nullptr;
  612. }
  613. /// SimplifyDivRemOfSelect - Try to fold a divide or remainder of a select
  614. /// instruction.
  615. bool InstCombiner::SimplifyDivRemOfSelect(BinaryOperator &I) {
  616. SelectInst *SI = cast<SelectInst>(I.getOperand(1));
  617. // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
  618. int NonNullOperand = -1;
  619. if (Constant *ST = dyn_cast<Constant>(SI->getOperand(1)))
  620. if (ST->isNullValue())
  621. NonNullOperand = 2;
  622. // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
  623. if (Constant *ST = dyn_cast<Constant>(SI->getOperand(2)))
  624. if (ST->isNullValue())
  625. NonNullOperand = 1;
  626. if (NonNullOperand == -1)
  627. return false;
  628. Value *SelectCond = SI->getOperand(0);
  629. // Change the div/rem to use 'Y' instead of the select.
  630. I.setOperand(1, SI->getOperand(NonNullOperand));
  631. // Okay, we know we replace the operand of the div/rem with 'Y' with no
  632. // problem. However, the select, or the condition of the select may have
  633. // multiple uses. Based on our knowledge that the operand must be non-zero,
  634. // propagate the known value for the select into other uses of it, and
  635. // propagate a known value of the condition into its other users.
  636. // If the select and condition only have a single use, don't bother with this,
  637. // early exit.
  638. if (SI->use_empty() && SelectCond->hasOneUse())
  639. return true;
  640. // Scan the current block backward, looking for other uses of SI.
  641. BasicBlock::iterator BBI = &I, BBFront = I.getParent()->begin();
  642. while (BBI != BBFront) {
  643. --BBI;
  644. // If we found a call to a function, we can't assume it will return, so
  645. // information from below it cannot be propagated above it.
  646. if (isa<CallInst>(BBI) && !isa<IntrinsicInst>(BBI))
  647. break;
  648. // Replace uses of the select or its condition with the known values.
  649. for (Instruction::op_iterator I = BBI->op_begin(), E = BBI->op_end();
  650. I != E; ++I) {
  651. if (*I == SI) {
  652. *I = SI->getOperand(NonNullOperand);
  653. Worklist.Add(BBI);
  654. } else if (*I == SelectCond) {
  655. *I = Builder->getInt1(NonNullOperand == 1);
  656. Worklist.Add(BBI);
  657. }
  658. }
  659. // If we past the instruction, quit looking for it.
  660. if (&*BBI == SI)
  661. SI = nullptr;
  662. if (&*BBI == SelectCond)
  663. SelectCond = nullptr;
  664. // If we ran out of things to eliminate, break out of the loop.
  665. if (!SelectCond && !SI)
  666. break;
  667. }
  668. return true;
  669. }
  670. /// This function implements the transforms common to both integer division
  671. /// instructions (udiv and sdiv). It is called by the visitors to those integer
  672. /// division instructions.
  673. /// @brief Common integer divide transforms
  674. Instruction *InstCombiner::commonIDivTransforms(BinaryOperator &I) {
  675. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  676. // The RHS is known non-zero.
  677. if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) {
  678. I.setOperand(1, V);
  679. return &I;
  680. }
  681. // Handle cases involving: [su]div X, (select Cond, Y, Z)
  682. // This does not apply for fdiv.
  683. if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
  684. return &I;
  685. if (Instruction *LHS = dyn_cast<Instruction>(Op0)) {
  686. const APInt *C2;
  687. if (match(Op1, m_APInt(C2))) {
  688. Value *X;
  689. const APInt *C1;
  690. bool IsSigned = I.getOpcode() == Instruction::SDiv;
  691. // (X / C1) / C2 -> X / (C1*C2)
  692. if ((IsSigned && match(LHS, m_SDiv(m_Value(X), m_APInt(C1)))) ||
  693. (!IsSigned && match(LHS, m_UDiv(m_Value(X), m_APInt(C1))))) {
  694. APInt Product(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
  695. if (!MultiplyOverflows(*C1, *C2, Product, IsSigned))
  696. return BinaryOperator::Create(I.getOpcode(), X,
  697. ConstantInt::get(I.getType(), Product));
  698. }
  699. if ((IsSigned && match(LHS, m_NSWMul(m_Value(X), m_APInt(C1)))) ||
  700. (!IsSigned && match(LHS, m_NUWMul(m_Value(X), m_APInt(C1))))) {
  701. APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
  702. // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
  703. if (IsMultiple(*C2, *C1, Quotient, IsSigned)) {
  704. BinaryOperator *BO = BinaryOperator::Create(
  705. I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient));
  706. BO->setIsExact(I.isExact());
  707. return BO;
  708. }
  709. // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
  710. if (IsMultiple(*C1, *C2, Quotient, IsSigned)) {
  711. BinaryOperator *BO = BinaryOperator::Create(
  712. Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient));
  713. BO->setHasNoUnsignedWrap(
  714. !IsSigned &&
  715. cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
  716. BO->setHasNoSignedWrap(
  717. cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
  718. return BO;
  719. }
  720. }
  721. if ((IsSigned && match(LHS, m_NSWShl(m_Value(X), m_APInt(C1))) &&
  722. *C1 != C1->getBitWidth() - 1) ||
  723. (!IsSigned && match(LHS, m_NUWShl(m_Value(X), m_APInt(C1))))) {
  724. APInt Quotient(C1->getBitWidth(), /*Val=*/0ULL, IsSigned);
  725. APInt C1Shifted = APInt::getOneBitSet(
  726. C1->getBitWidth(), static_cast<unsigned>(C1->getLimitedValue()));
  727. // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of C1.
  728. if (IsMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
  729. BinaryOperator *BO = BinaryOperator::Create(
  730. I.getOpcode(), X, ConstantInt::get(X->getType(), Quotient));
  731. BO->setIsExact(I.isExact());
  732. return BO;
  733. }
  734. // (X << C1) / C2 -> X * (C2 >> C1) if C1 is a multiple of C2.
  735. if (IsMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
  736. BinaryOperator *BO = BinaryOperator::Create(
  737. Instruction::Mul, X, ConstantInt::get(X->getType(), Quotient));
  738. BO->setHasNoUnsignedWrap(
  739. !IsSigned &&
  740. cast<OverflowingBinaryOperator>(LHS)->hasNoUnsignedWrap());
  741. BO->setHasNoSignedWrap(
  742. cast<OverflowingBinaryOperator>(LHS)->hasNoSignedWrap());
  743. return BO;
  744. }
  745. }
  746. if (*C2 != 0) { // avoid X udiv 0
  747. if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
  748. if (Instruction *R = FoldOpIntoSelect(I, SI))
  749. return R;
  750. if (isa<PHINode>(Op0))
  751. if (Instruction *NV = FoldOpIntoPhi(I))
  752. return NV;
  753. }
  754. }
  755. }
  756. if (ConstantInt *One = dyn_cast<ConstantInt>(Op0)) {
  757. if (One->isOne() && !I.getType()->isIntegerTy(1)) {
  758. bool isSigned = I.getOpcode() == Instruction::SDiv;
  759. if (isSigned) {
  760. // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the
  761. // result is one, if Op1 is -1 then the result is minus one, otherwise
  762. // it's zero.
  763. Value *Inc = Builder->CreateAdd(Op1, One);
  764. Value *Cmp = Builder->CreateICmpULT(
  765. Inc, ConstantInt::get(I.getType(), 3));
  766. return SelectInst::Create(Cmp, Op1, ConstantInt::get(I.getType(), 0));
  767. } else {
  768. // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
  769. // result is one, otherwise it's zero.
  770. return new ZExtInst(Builder->CreateICmpEQ(Op1, One), I.getType());
  771. }
  772. }
  773. }
  774. // See if we can fold away this div instruction.
  775. if (SimplifyDemandedInstructionBits(I))
  776. return &I;
  777. // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
  778. Value *X = nullptr, *Z = nullptr;
  779. if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) { // (X - Z) / Y; Y = Op1
  780. bool isSigned = I.getOpcode() == Instruction::SDiv;
  781. if ((isSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
  782. (!isSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
  783. return BinaryOperator::Create(I.getOpcode(), X, Op1);
  784. }
  785. return nullptr;
  786. }
  787. /// dyn_castZExtVal - Checks if V is a zext or constant that can
  788. /// be truncated to Ty without losing bits.
  789. static Value *dyn_castZExtVal(Value *V, Type *Ty) {
  790. if (ZExtInst *Z = dyn_cast<ZExtInst>(V)) {
  791. if (Z->getSrcTy() == Ty)
  792. return Z->getOperand(0);
  793. } else if (ConstantInt *C = dyn_cast<ConstantInt>(V)) {
  794. if (C->getValue().getActiveBits() <= cast<IntegerType>(Ty)->getBitWidth())
  795. return ConstantExpr::getTrunc(C, Ty);
  796. }
  797. return nullptr;
  798. }
  799. namespace {
  800. const unsigned MaxDepth = 6;
  801. typedef Instruction *(*FoldUDivOperandCb)(Value *Op0, Value *Op1,
  802. const BinaryOperator &I,
  803. InstCombiner &IC);
  804. /// \brief Used to maintain state for visitUDivOperand().
  805. struct UDivFoldAction {
  806. FoldUDivOperandCb FoldAction; ///< Informs visitUDiv() how to fold this
  807. ///< operand. This can be zero if this action
  808. ///< joins two actions together.
  809. Value *OperandToFold; ///< Which operand to fold.
  810. union {
  811. Instruction *FoldResult; ///< The instruction returned when FoldAction is
  812. ///< invoked.
  813. size_t SelectLHSIdx; ///< Stores the LHS action index if this action
  814. ///< joins two actions together.
  815. };
  816. UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand)
  817. : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
  818. UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS)
  819. : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
  820. };
  821. }
  822. // X udiv 2^C -> X >> C
  823. static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1,
  824. const BinaryOperator &I, InstCombiner &IC) {
  825. const APInt &C = cast<Constant>(Op1)->getUniqueInteger();
  826. BinaryOperator *LShr = BinaryOperator::CreateLShr(
  827. Op0, ConstantInt::get(Op0->getType(), C.logBase2()));
  828. if (I.isExact())
  829. LShr->setIsExact();
  830. return LShr;
  831. }
  832. // X udiv C, where C >= signbit
  833. static Instruction *foldUDivNegCst(Value *Op0, Value *Op1,
  834. const BinaryOperator &I, InstCombiner &IC) {
  835. Value *ICI = IC.Builder->CreateICmpULT(Op0, cast<ConstantInt>(Op1));
  836. return SelectInst::Create(ICI, Constant::getNullValue(I.getType()),
  837. ConstantInt::get(I.getType(), 1));
  838. }
  839. // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
  840. static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I,
  841. InstCombiner &IC) {
  842. Instruction *ShiftLeft = cast<Instruction>(Op1);
  843. if (isa<ZExtInst>(ShiftLeft))
  844. ShiftLeft = cast<Instruction>(ShiftLeft->getOperand(0));
  845. const APInt &CI =
  846. cast<Constant>(ShiftLeft->getOperand(0))->getUniqueInteger();
  847. Value *N = ShiftLeft->getOperand(1);
  848. if (CI != 1)
  849. N = IC.Builder->CreateAdd(N, ConstantInt::get(N->getType(), CI.logBase2()));
  850. if (ZExtInst *Z = dyn_cast<ZExtInst>(Op1))
  851. N = IC.Builder->CreateZExt(N, Z->getDestTy());
  852. BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N);
  853. if (I.isExact())
  854. LShr->setIsExact();
  855. return LShr;
  856. }
  857. // \brief Recursively visits the possible right hand operands of a udiv
  858. // instruction, seeing through select instructions, to determine if we can
  859. // replace the udiv with something simpler. If we find that an operand is not
  860. // able to simplify the udiv, we abort the entire transformation.
  861. static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I,
  862. SmallVectorImpl<UDivFoldAction> &Actions,
  863. unsigned Depth = 0) {
  864. // Check to see if this is an unsigned division with an exact power of 2,
  865. // if so, convert to a right shift.
  866. if (match(Op1, m_Power2())) {
  867. Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1));
  868. return Actions.size();
  869. }
  870. if (ConstantInt *C = dyn_cast<ConstantInt>(Op1))
  871. // X udiv C, where C >= signbit
  872. if (C->getValue().isNegative()) {
  873. Actions.push_back(UDivFoldAction(foldUDivNegCst, C));
  874. return Actions.size();
  875. }
  876. // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
  877. if (match(Op1, m_Shl(m_Power2(), m_Value())) ||
  878. match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) {
  879. Actions.push_back(UDivFoldAction(foldUDivShl, Op1));
  880. return Actions.size();
  881. }
  882. // The remaining tests are all recursive, so bail out if we hit the limit.
  883. if (Depth++ == MaxDepth)
  884. return 0;
  885. if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
  886. if (size_t LHSIdx =
  887. visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth))
  888. if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) {
  889. Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1));
  890. return Actions.size();
  891. }
  892. return 0;
  893. }
  894. Instruction *InstCombiner::visitUDiv(BinaryOperator &I) {
  895. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  896. if (Value *V = SimplifyVectorOp(I))
  897. return ReplaceInstUsesWith(I, V);
  898. if (Value *V = SimplifyUDivInst(Op0, Op1, DL, TLI, DT, AC))
  899. return ReplaceInstUsesWith(I, V);
  900. // Handle the integer div common cases
  901. if (Instruction *Common = commonIDivTransforms(I))
  902. return Common;
  903. // (x lshr C1) udiv C2 --> x udiv (C2 << C1)
  904. {
  905. Value *X;
  906. const APInt *C1, *C2;
  907. if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) &&
  908. match(Op1, m_APInt(C2))) {
  909. bool Overflow;
  910. APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow);
  911. if (!Overflow) {
  912. bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value()));
  913. BinaryOperator *BO = BinaryOperator::CreateUDiv(
  914. X, ConstantInt::get(X->getType(), C2ShlC1));
  915. if (IsExact)
  916. BO->setIsExact();
  917. return BO;
  918. }
  919. }
  920. }
  921. // (zext A) udiv (zext B) --> zext (A udiv B)
  922. if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
  923. if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
  924. return new ZExtInst(
  925. Builder->CreateUDiv(ZOp0->getOperand(0), ZOp1, "div", I.isExact()),
  926. I.getType());
  927. // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...))))
  928. SmallVector<UDivFoldAction, 6> UDivActions;
  929. if (visitUDivOperand(Op0, Op1, I, UDivActions))
  930. for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
  931. FoldUDivOperandCb Action = UDivActions[i].FoldAction;
  932. Value *ActionOp1 = UDivActions[i].OperandToFold;
  933. Instruction *Inst;
  934. if (Action)
  935. Inst = Action(Op0, ActionOp1, I, *this);
  936. else {
  937. // This action joins two actions together. The RHS of this action is
  938. // simply the last action we processed, we saved the LHS action index in
  939. // the joining action.
  940. size_t SelectRHSIdx = i - 1;
  941. Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
  942. size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
  943. Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
  944. Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(),
  945. SelectLHS, SelectRHS);
  946. }
  947. // If this is the last action to process, return it to the InstCombiner.
  948. // Otherwise, we insert it before the UDiv and record it so that we may
  949. // use it as part of a joining action (i.e., a SelectInst).
  950. if (e - i != 1) {
  951. Inst->insertBefore(&I);
  952. UDivActions[i].FoldResult = Inst;
  953. } else
  954. return Inst;
  955. }
  956. return nullptr;
  957. }
  958. Instruction *InstCombiner::visitSDiv(BinaryOperator &I) {
  959. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  960. if (Value *V = SimplifyVectorOp(I))
  961. return ReplaceInstUsesWith(I, V);
  962. if (Value *V = SimplifySDivInst(Op0, Op1, DL, TLI, DT, AC))
  963. return ReplaceInstUsesWith(I, V);
  964. // Handle the integer div common cases
  965. if (Instruction *Common = commonIDivTransforms(I))
  966. return Common;
  967. // sdiv X, -1 == -X
  968. if (match(Op1, m_AllOnes()))
  969. return BinaryOperator::CreateNeg(Op0);
  970. if (ConstantInt *RHS = dyn_cast<ConstantInt>(Op1)) {
  971. // sdiv X, C --> ashr exact X, log2(C)
  972. if (I.isExact() && RHS->getValue().isNonNegative() &&
  973. RHS->getValue().isPowerOf2()) {
  974. Value *ShAmt = llvm::ConstantInt::get(RHS->getType(),
  975. RHS->getValue().exactLogBase2());
  976. return BinaryOperator::CreateExactAShr(Op0, ShAmt, I.getName());
  977. }
  978. }
  979. if (Constant *RHS = dyn_cast<Constant>(Op1)) {
  980. // X/INT_MIN -> X == INT_MIN
  981. if (RHS->isMinSignedValue())
  982. return new ZExtInst(Builder->CreateICmpEQ(Op0, Op1), I.getType());
  983. // -X/C --> X/-C provided the negation doesn't overflow.
  984. Value *X;
  985. if (match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) {
  986. auto *BO = BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(RHS));
  987. BO->setIsExact(I.isExact());
  988. return BO;
  989. }
  990. }
  991. // If the sign bits of both operands are zero (i.e. we can prove they are
  992. // unsigned inputs), turn this into a udiv.
  993. if (I.getType()->isIntegerTy()) {
  994. APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
  995. if (MaskedValueIsZero(Op0, Mask, 0, &I)) {
  996. if (MaskedValueIsZero(Op1, Mask, 0, &I)) {
  997. // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
  998. auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
  999. BO->setIsExact(I.isExact());
  1000. return BO;
  1001. }
  1002. if (isKnownToBeAPowerOfTwo(Op1, DL, /*OrZero*/ true, 0, AC, &I, DT)) {
  1003. // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
  1004. // Safe because the only negative value (1 << Y) can take on is
  1005. // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
  1006. // the sign bit set.
  1007. auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
  1008. BO->setIsExact(I.isExact());
  1009. return BO;
  1010. }
  1011. }
  1012. }
  1013. return nullptr;
  1014. }
  1015. /// CvtFDivConstToReciprocal tries to convert X/C into X*1/C if C not a special
  1016. /// FP value and:
  1017. /// 1) 1/C is exact, or
  1018. /// 2) reciprocal is allowed.
  1019. /// If the conversion was successful, the simplified expression "X * 1/C" is
  1020. /// returned; otherwise, NULL is returned.
  1021. ///
  1022. static Instruction *CvtFDivConstToReciprocal(Value *Dividend, Constant *Divisor,
  1023. bool AllowReciprocal) {
  1024. if (!isa<ConstantFP>(Divisor)) // TODO: handle vectors.
  1025. return nullptr;
  1026. const APFloat &FpVal = cast<ConstantFP>(Divisor)->getValueAPF();
  1027. APFloat Reciprocal(FpVal.getSemantics());
  1028. bool Cvt = FpVal.getExactInverse(&Reciprocal);
  1029. if (!Cvt && AllowReciprocal && FpVal.isFiniteNonZero()) {
  1030. Reciprocal = APFloat(FpVal.getSemantics(), 1.0f);
  1031. (void)Reciprocal.divide(FpVal, APFloat::rmNearestTiesToEven);
  1032. Cvt = !Reciprocal.isDenormal();
  1033. }
  1034. if (!Cvt)
  1035. return nullptr;
  1036. ConstantFP *R;
  1037. R = ConstantFP::get(Dividend->getType()->getContext(), Reciprocal);
  1038. return BinaryOperator::CreateFMul(Dividend, R);
  1039. }
  1040. Instruction *InstCombiner::visitFDiv(BinaryOperator &I) {
  1041. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1042. if (Value *V = SimplifyVectorOp(I))
  1043. return ReplaceInstUsesWith(I, V);
  1044. if (Value *V = SimplifyFDivInst(Op0, Op1, I.getFastMathFlags(),
  1045. DL, TLI, DT, AC))
  1046. return ReplaceInstUsesWith(I, V);
  1047. if (isa<Constant>(Op0))
  1048. if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
  1049. if (Instruction *R = FoldOpIntoSelect(I, SI))
  1050. return R;
  1051. bool AllowReassociate = I.hasUnsafeAlgebra();
  1052. bool AllowReciprocal = I.hasAllowReciprocal();
  1053. if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
  1054. if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
  1055. if (Instruction *R = FoldOpIntoSelect(I, SI))
  1056. return R;
  1057. if (AllowReassociate) {
  1058. Constant *C1 = nullptr;
  1059. Constant *C2 = Op1C;
  1060. Value *X;
  1061. Instruction *Res = nullptr;
  1062. if (match(Op0, m_FMul(m_Value(X), m_Constant(C1)))) {
  1063. // (X*C1)/C2 => X * (C1/C2)
  1064. //
  1065. Constant *C = ConstantExpr::getFDiv(C1, C2);
  1066. if (isNormalFp(C))
  1067. Res = BinaryOperator::CreateFMul(X, C);
  1068. } else if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
  1069. // (X/C1)/C2 => X /(C2*C1) [=> X * 1/(C2*C1) if reciprocal is allowed]
  1070. //
  1071. Constant *C = ConstantExpr::getFMul(C1, C2);
  1072. if (isNormalFp(C)) {
  1073. Res = CvtFDivConstToReciprocal(X, C, AllowReciprocal);
  1074. if (!Res)
  1075. Res = BinaryOperator::CreateFDiv(X, C);
  1076. }
  1077. }
  1078. if (Res) {
  1079. Res->setFastMathFlags(I.getFastMathFlags());
  1080. return Res;
  1081. }
  1082. }
  1083. // X / C => X * 1/C
  1084. if (Instruction *T = CvtFDivConstToReciprocal(Op0, Op1C, AllowReciprocal)) {
  1085. T->copyFastMathFlags(&I);
  1086. return T;
  1087. }
  1088. return nullptr;
  1089. }
  1090. if (AllowReassociate && isa<Constant>(Op0)) {
  1091. Constant *C1 = cast<Constant>(Op0), *C2;
  1092. Constant *Fold = nullptr;
  1093. Value *X;
  1094. bool CreateDiv = true;
  1095. // C1 / (X*C2) => (C1/C2) / X
  1096. if (match(Op1, m_FMul(m_Value(X), m_Constant(C2))))
  1097. Fold = ConstantExpr::getFDiv(C1, C2);
  1098. else if (match(Op1, m_FDiv(m_Value(X), m_Constant(C2)))) {
  1099. // C1 / (X/C2) => (C1*C2) / X
  1100. Fold = ConstantExpr::getFMul(C1, C2);
  1101. } else if (match(Op1, m_FDiv(m_Constant(C2), m_Value(X)))) {
  1102. // C1 / (C2/X) => (C1/C2) * X
  1103. Fold = ConstantExpr::getFDiv(C1, C2);
  1104. CreateDiv = false;
  1105. }
  1106. if (Fold && isNormalFp(Fold)) {
  1107. Instruction *R = CreateDiv ? BinaryOperator::CreateFDiv(Fold, X)
  1108. : BinaryOperator::CreateFMul(X, Fold);
  1109. R->setFastMathFlags(I.getFastMathFlags());
  1110. return R;
  1111. }
  1112. return nullptr;
  1113. }
  1114. if (AllowReassociate) {
  1115. Value *X, *Y;
  1116. Value *NewInst = nullptr;
  1117. Instruction *SimpR = nullptr;
  1118. if (Op0->hasOneUse() && match(Op0, m_FDiv(m_Value(X), m_Value(Y)))) {
  1119. // (X/Y) / Z => X / (Y*Z)
  1120. //
  1121. if (!isa<Constant>(Y) || !isa<Constant>(Op1)) {
  1122. NewInst = Builder->CreateFMul(Y, Op1);
  1123. if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
  1124. FastMathFlags Flags = I.getFastMathFlags();
  1125. Flags &= cast<Instruction>(Op0)->getFastMathFlags();
  1126. RI->setFastMathFlags(Flags);
  1127. }
  1128. SimpR = BinaryOperator::CreateFDiv(X, NewInst);
  1129. }
  1130. } else if (Op1->hasOneUse() && match(Op1, m_FDiv(m_Value(X), m_Value(Y)))) {
  1131. // Z / (X/Y) => Z*Y / X
  1132. //
  1133. if (!isa<Constant>(Y) || !isa<Constant>(Op0)) {
  1134. NewInst = Builder->CreateFMul(Op0, Y);
  1135. if (Instruction *RI = dyn_cast<Instruction>(NewInst)) {
  1136. FastMathFlags Flags = I.getFastMathFlags();
  1137. Flags &= cast<Instruction>(Op1)->getFastMathFlags();
  1138. RI->setFastMathFlags(Flags);
  1139. }
  1140. SimpR = BinaryOperator::CreateFDiv(NewInst, X);
  1141. }
  1142. }
  1143. if (NewInst) {
  1144. if (Instruction *T = dyn_cast<Instruction>(NewInst))
  1145. T->setDebugLoc(I.getDebugLoc());
  1146. SimpR->setFastMathFlags(I.getFastMathFlags());
  1147. return SimpR;
  1148. }
  1149. }
  1150. return nullptr;
  1151. }
  1152. /// This function implements the transforms common to both integer remainder
  1153. /// instructions (urem and srem). It is called by the visitors to those integer
  1154. /// remainder instructions.
  1155. /// @brief Common integer remainder transforms
  1156. Instruction *InstCombiner::commonIRemTransforms(BinaryOperator &I) {
  1157. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1158. // The RHS is known non-zero.
  1159. if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I)) {
  1160. I.setOperand(1, V);
  1161. return &I;
  1162. }
  1163. // Handle cases involving: rem X, (select Cond, Y, Z)
  1164. if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
  1165. return &I;
  1166. if (isa<Constant>(Op1)) {
  1167. if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
  1168. if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
  1169. if (Instruction *R = FoldOpIntoSelect(I, SI))
  1170. return R;
  1171. } else if (isa<PHINode>(Op0I)) {
  1172. if (Instruction *NV = FoldOpIntoPhi(I))
  1173. return NV;
  1174. }
  1175. // See if we can fold away this rem instruction.
  1176. if (SimplifyDemandedInstructionBits(I))
  1177. return &I;
  1178. }
  1179. }
  1180. return nullptr;
  1181. }
  1182. Instruction *InstCombiner::visitURem(BinaryOperator &I) {
  1183. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1184. if (Value *V = SimplifyVectorOp(I))
  1185. return ReplaceInstUsesWith(I, V);
  1186. if (Value *V = SimplifyURemInst(Op0, Op1, DL, TLI, DT, AC))
  1187. return ReplaceInstUsesWith(I, V);
  1188. if (Instruction *common = commonIRemTransforms(I))
  1189. return common;
  1190. // (zext A) urem (zext B) --> zext (A urem B)
  1191. if (ZExtInst *ZOp0 = dyn_cast<ZExtInst>(Op0))
  1192. if (Value *ZOp1 = dyn_castZExtVal(Op1, ZOp0->getSrcTy()))
  1193. return new ZExtInst(Builder->CreateURem(ZOp0->getOperand(0), ZOp1),
  1194. I.getType());
  1195. // X urem Y -> X and Y-1, where Y is a power of 2,
  1196. if (isKnownToBeAPowerOfTwo(Op1, DL, /*OrZero*/ true, 0, AC, &I, DT)) {
  1197. Constant *N1 = Constant::getAllOnesValue(I.getType());
  1198. Value *Add = Builder->CreateAdd(Op1, N1);
  1199. return BinaryOperator::CreateAnd(Op0, Add);
  1200. }
  1201. // 1 urem X -> zext(X != 1)
  1202. if (match(Op0, m_One())) {
  1203. Value *Cmp = Builder->CreateICmpNE(Op1, Op0);
  1204. Value *Ext = Builder->CreateZExt(Cmp, I.getType());
  1205. return ReplaceInstUsesWith(I, Ext);
  1206. }
  1207. return nullptr;
  1208. }
  1209. Instruction *InstCombiner::visitSRem(BinaryOperator &I) {
  1210. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1211. if (Value *V = SimplifyVectorOp(I))
  1212. return ReplaceInstUsesWith(I, V);
  1213. if (Value *V = SimplifySRemInst(Op0, Op1, DL, TLI, DT, AC))
  1214. return ReplaceInstUsesWith(I, V);
  1215. // Handle the integer rem common cases
  1216. if (Instruction *Common = commonIRemTransforms(I))
  1217. return Common;
  1218. {
  1219. const APInt *Y;
  1220. // X % -Y -> X % Y
  1221. if (match(Op1, m_APInt(Y)) && Y->isNegative() && !Y->isMinSignedValue()) {
  1222. Worklist.AddValue(I.getOperand(1));
  1223. I.setOperand(1, ConstantInt::get(I.getType(), -*Y));
  1224. return &I;
  1225. }
  1226. }
  1227. // If the sign bits of both operands are zero (i.e. we can prove they are
  1228. // unsigned inputs), turn this into a urem.
  1229. if (I.getType()->isIntegerTy()) {
  1230. APInt Mask(APInt::getSignBit(I.getType()->getPrimitiveSizeInBits()));
  1231. if (MaskedValueIsZero(Op1, Mask, 0, &I) &&
  1232. MaskedValueIsZero(Op0, Mask, 0, &I)) {
  1233. // X srem Y -> X urem Y, iff X and Y don't have sign bit set
  1234. return BinaryOperator::CreateURem(Op0, Op1, I.getName());
  1235. }
  1236. }
  1237. // If it's a constant vector, flip any negative values positive.
  1238. if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
  1239. Constant *C = cast<Constant>(Op1);
  1240. unsigned VWidth = C->getType()->getVectorNumElements();
  1241. bool hasNegative = false;
  1242. bool hasMissing = false;
  1243. for (unsigned i = 0; i != VWidth; ++i) {
  1244. Constant *Elt = C->getAggregateElement(i);
  1245. if (!Elt) {
  1246. hasMissing = true;
  1247. break;
  1248. }
  1249. if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
  1250. if (RHS->isNegative())
  1251. hasNegative = true;
  1252. }
  1253. if (hasNegative && !hasMissing) {
  1254. SmallVector<Constant *, 16> Elts(VWidth);
  1255. for (unsigned i = 0; i != VWidth; ++i) {
  1256. Elts[i] = C->getAggregateElement(i); // Handle undef, etc.
  1257. if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
  1258. if (RHS->isNegative())
  1259. Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
  1260. }
  1261. }
  1262. Constant *NewRHSV = ConstantVector::get(Elts);
  1263. if (NewRHSV != C) { // Don't loop on -MININT
  1264. Worklist.AddValue(I.getOperand(1));
  1265. I.setOperand(1, NewRHSV);
  1266. return &I;
  1267. }
  1268. }
  1269. }
  1270. return nullptr;
  1271. }
  1272. Instruction *InstCombiner::visitFRem(BinaryOperator &I) {
  1273. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1274. if (Value *V = SimplifyVectorOp(I))
  1275. return ReplaceInstUsesWith(I, V);
  1276. if (Value *V = SimplifyFRemInst(Op0, Op1, I.getFastMathFlags(),
  1277. DL, TLI, DT, AC))
  1278. return ReplaceInstUsesWith(I, V);
  1279. // Handle cases involving: rem X, (select Cond, Y, Z)
  1280. if (isa<SelectInst>(Op1) && SimplifyDivRemOfSelect(I))
  1281. return &I;
  1282. return nullptr;
  1283. }