InstCombineMulDivRem.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. //===- InstCombineMulDivRem.cpp -------------------------------------------===//
  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. // This file implements the visit functions for mul, fmul, sdiv, udiv, fdiv,
  10. // srem, urem, frem.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "InstCombineInternal.h"
  14. #include "llvm/ADT/APFloat.h"
  15. #include "llvm/ADT/APInt.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/Analysis/InstructionSimplify.h"
  18. #include "llvm/IR/BasicBlock.h"
  19. #include "llvm/IR/Constant.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/InstrTypes.h"
  22. #include "llvm/IR/Instruction.h"
  23. #include "llvm/IR/Instructions.h"
  24. #include "llvm/IR/IntrinsicInst.h"
  25. #include "llvm/IR/Intrinsics.h"
  26. #include "llvm/IR/Operator.h"
  27. #include "llvm/IR/PatternMatch.h"
  28. #include "llvm/IR/Type.h"
  29. #include "llvm/IR/Value.h"
  30. #include "llvm/Support/Casting.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/KnownBits.h"
  33. #include "llvm/Transforms/InstCombine/InstCombiner.h"
  34. #include "llvm/Transforms/Utils/BuildLibCalls.h"
  35. #include <cassert>
  36. #include <cstddef>
  37. #include <cstdint>
  38. #include <utility>
  39. #define DEBUG_TYPE "instcombine"
  40. #include "llvm/Transforms/Utils/InstructionWorklist.h"
  41. using namespace llvm;
  42. using namespace PatternMatch;
  43. /// The specific integer value is used in a context where it is known to be
  44. /// non-zero. If this allows us to simplify the computation, do so and return
  45. /// the new operand, otherwise return null.
  46. static Value *simplifyValueKnownNonZero(Value *V, InstCombinerImpl &IC,
  47. Instruction &CxtI) {
  48. // If V has multiple uses, then we would have to do more analysis to determine
  49. // if this is safe. For example, the use could be in dynamically unreached
  50. // code.
  51. if (!V->hasOneUse()) return nullptr;
  52. bool MadeChange = false;
  53. // ((1 << A) >>u B) --> (1 << (A-B))
  54. // Because V cannot be zero, we know that B is less than A.
  55. Value *A = nullptr, *B = nullptr, *One = nullptr;
  56. if (match(V, m_LShr(m_OneUse(m_Shl(m_Value(One), m_Value(A))), m_Value(B))) &&
  57. match(One, m_One())) {
  58. A = IC.Builder.CreateSub(A, B);
  59. return IC.Builder.CreateShl(One, A);
  60. }
  61. // (PowerOfTwo >>u B) --> isExact since shifting out the result would make it
  62. // inexact. Similarly for <<.
  63. BinaryOperator *I = dyn_cast<BinaryOperator>(V);
  64. if (I && I->isLogicalShift() &&
  65. IC.isKnownToBeAPowerOfTwo(I->getOperand(0), false, 0, &CxtI)) {
  66. // We know that this is an exact/nuw shift and that the input is a
  67. // non-zero context as well.
  68. if (Value *V2 = simplifyValueKnownNonZero(I->getOperand(0), IC, CxtI)) {
  69. IC.replaceOperand(*I, 0, V2);
  70. MadeChange = true;
  71. }
  72. if (I->getOpcode() == Instruction::LShr && !I->isExact()) {
  73. I->setIsExact();
  74. MadeChange = true;
  75. }
  76. if (I->getOpcode() == Instruction::Shl && !I->hasNoUnsignedWrap()) {
  77. I->setHasNoUnsignedWrap();
  78. MadeChange = true;
  79. }
  80. }
  81. // TODO: Lots more we could do here:
  82. // If V is a phi node, we can call this on each of its operands.
  83. // "select cond, X, 0" can simplify to "X".
  84. return MadeChange ? V : nullptr;
  85. }
  86. // TODO: This is a specific form of a much more general pattern.
  87. // We could detect a select with any binop identity constant, or we
  88. // could use SimplifyBinOp to see if either arm of the select reduces.
  89. // But that needs to be done carefully and/or while removing potential
  90. // reverse canonicalizations as in InstCombiner::foldSelectIntoOp().
  91. static Value *foldMulSelectToNegate(BinaryOperator &I,
  92. InstCombiner::BuilderTy &Builder) {
  93. Value *Cond, *OtherOp;
  94. // mul (select Cond, 1, -1), OtherOp --> select Cond, OtherOp, -OtherOp
  95. // mul OtherOp, (select Cond, 1, -1) --> select Cond, OtherOp, -OtherOp
  96. if (match(&I, m_c_Mul(m_OneUse(m_Select(m_Value(Cond), m_One(), m_AllOnes())),
  97. m_Value(OtherOp)))) {
  98. bool HasAnyNoWrap = I.hasNoSignedWrap() || I.hasNoUnsignedWrap();
  99. Value *Neg = Builder.CreateNeg(OtherOp, "", false, HasAnyNoWrap);
  100. return Builder.CreateSelect(Cond, OtherOp, Neg);
  101. }
  102. // mul (select Cond, -1, 1), OtherOp --> select Cond, -OtherOp, OtherOp
  103. // mul OtherOp, (select Cond, -1, 1) --> select Cond, -OtherOp, OtherOp
  104. if (match(&I, m_c_Mul(m_OneUse(m_Select(m_Value(Cond), m_AllOnes(), m_One())),
  105. m_Value(OtherOp)))) {
  106. bool HasAnyNoWrap = I.hasNoSignedWrap() || I.hasNoUnsignedWrap();
  107. Value *Neg = Builder.CreateNeg(OtherOp, "", false, HasAnyNoWrap);
  108. return Builder.CreateSelect(Cond, Neg, OtherOp);
  109. }
  110. // fmul (select Cond, 1.0, -1.0), OtherOp --> select Cond, OtherOp, -OtherOp
  111. // fmul OtherOp, (select Cond, 1.0, -1.0) --> select Cond, OtherOp, -OtherOp
  112. if (match(&I, m_c_FMul(m_OneUse(m_Select(m_Value(Cond), m_SpecificFP(1.0),
  113. m_SpecificFP(-1.0))),
  114. m_Value(OtherOp)))) {
  115. IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
  116. Builder.setFastMathFlags(I.getFastMathFlags());
  117. return Builder.CreateSelect(Cond, OtherOp, Builder.CreateFNeg(OtherOp));
  118. }
  119. // fmul (select Cond, -1.0, 1.0), OtherOp --> select Cond, -OtherOp, OtherOp
  120. // fmul OtherOp, (select Cond, -1.0, 1.0) --> select Cond, -OtherOp, OtherOp
  121. if (match(&I, m_c_FMul(m_OneUse(m_Select(m_Value(Cond), m_SpecificFP(-1.0),
  122. m_SpecificFP(1.0))),
  123. m_Value(OtherOp)))) {
  124. IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
  125. Builder.setFastMathFlags(I.getFastMathFlags());
  126. return Builder.CreateSelect(Cond, Builder.CreateFNeg(OtherOp), OtherOp);
  127. }
  128. return nullptr;
  129. }
  130. Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
  131. if (Value *V = SimplifyMulInst(I.getOperand(0), I.getOperand(1),
  132. SQ.getWithInstruction(&I)))
  133. return replaceInstUsesWith(I, V);
  134. if (SimplifyAssociativeOrCommutative(I))
  135. return &I;
  136. if (Instruction *X = foldVectorBinop(I))
  137. return X;
  138. if (Instruction *Phi = foldBinopWithPhiOperands(I))
  139. return Phi;
  140. if (Value *V = SimplifyUsingDistributiveLaws(I))
  141. return replaceInstUsesWith(I, V);
  142. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  143. unsigned BitWidth = I.getType()->getScalarSizeInBits();
  144. // X * -1 == 0 - X
  145. if (match(Op1, m_AllOnes())) {
  146. BinaryOperator *BO = BinaryOperator::CreateNeg(Op0, I.getName());
  147. if (I.hasNoSignedWrap())
  148. BO->setHasNoSignedWrap();
  149. return BO;
  150. }
  151. // Also allow combining multiply instructions on vectors.
  152. {
  153. Value *NewOp;
  154. Constant *C1, *C2;
  155. const APInt *IVal;
  156. if (match(&I, m_Mul(m_Shl(m_Value(NewOp), m_Constant(C2)),
  157. m_Constant(C1))) &&
  158. match(C1, m_APInt(IVal))) {
  159. // ((X << C2)*C1) == (X * (C1 << C2))
  160. Constant *Shl = ConstantExpr::getShl(C1, C2);
  161. BinaryOperator *Mul = cast<BinaryOperator>(I.getOperand(0));
  162. BinaryOperator *BO = BinaryOperator::CreateMul(NewOp, Shl);
  163. if (I.hasNoUnsignedWrap() && Mul->hasNoUnsignedWrap())
  164. BO->setHasNoUnsignedWrap();
  165. if (I.hasNoSignedWrap() && Mul->hasNoSignedWrap() &&
  166. Shl->isNotMinSignedValue())
  167. BO->setHasNoSignedWrap();
  168. return BO;
  169. }
  170. if (match(&I, m_Mul(m_Value(NewOp), m_Constant(C1)))) {
  171. // Replace X*(2^C) with X << C, where C is either a scalar or a vector.
  172. if (Constant *NewCst = ConstantExpr::getExactLogBase2(C1)) {
  173. BinaryOperator *Shl = BinaryOperator::CreateShl(NewOp, NewCst);
  174. if (I.hasNoUnsignedWrap())
  175. Shl->setHasNoUnsignedWrap();
  176. if (I.hasNoSignedWrap()) {
  177. const APInt *V;
  178. if (match(NewCst, m_APInt(V)) && *V != V->getBitWidth() - 1)
  179. Shl->setHasNoSignedWrap();
  180. }
  181. return Shl;
  182. }
  183. }
  184. }
  185. if (Op0->hasOneUse() && match(Op1, m_NegatedPower2())) {
  186. // Interpret X * (-1<<C) as (-X) * (1<<C) and try to sink the negation.
  187. // The "* (1<<C)" thus becomes a potential shifting opportunity.
  188. if (Value *NegOp0 = Negator::Negate(/*IsNegation*/ true, Op0, *this))
  189. return BinaryOperator::CreateMul(
  190. NegOp0, ConstantExpr::getNeg(cast<Constant>(Op1)), I.getName());
  191. }
  192. if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
  193. return FoldedMul;
  194. if (Value *FoldedMul = foldMulSelectToNegate(I, Builder))
  195. return replaceInstUsesWith(I, FoldedMul);
  196. // Simplify mul instructions with a constant RHS.
  197. if (isa<Constant>(Op1)) {
  198. // Canonicalize (X+C1)*CI -> X*CI+C1*CI.
  199. Value *X;
  200. Constant *C1;
  201. if (match(Op0, m_OneUse(m_Add(m_Value(X), m_Constant(C1))))) {
  202. Value *Mul = Builder.CreateMul(C1, Op1);
  203. // Only go forward with the transform if C1*CI simplifies to a tidier
  204. // constant.
  205. if (!match(Mul, m_Mul(m_Value(), m_Value())))
  206. return BinaryOperator::CreateAdd(Builder.CreateMul(X, Op1), Mul);
  207. }
  208. }
  209. // abs(X) * abs(X) -> X * X
  210. // nabs(X) * nabs(X) -> X * X
  211. if (Op0 == Op1) {
  212. Value *X, *Y;
  213. SelectPatternFlavor SPF = matchSelectPattern(Op0, X, Y).Flavor;
  214. if (SPF == SPF_ABS || SPF == SPF_NABS)
  215. return BinaryOperator::CreateMul(X, X);
  216. if (match(Op0, m_Intrinsic<Intrinsic::abs>(m_Value(X))))
  217. return BinaryOperator::CreateMul(X, X);
  218. }
  219. // -X * C --> X * -C
  220. Value *X, *Y;
  221. Constant *Op1C;
  222. if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Constant(Op1C)))
  223. return BinaryOperator::CreateMul(X, ConstantExpr::getNeg(Op1C));
  224. // -X * -Y --> X * Y
  225. if (match(Op0, m_Neg(m_Value(X))) && match(Op1, m_Neg(m_Value(Y)))) {
  226. auto *NewMul = BinaryOperator::CreateMul(X, Y);
  227. if (I.hasNoSignedWrap() &&
  228. cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap() &&
  229. cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap())
  230. NewMul->setHasNoSignedWrap();
  231. return NewMul;
  232. }
  233. // -X * Y --> -(X * Y)
  234. // X * -Y --> -(X * Y)
  235. if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))
  236. return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y));
  237. // (X / Y) * Y = X - (X % Y)
  238. // (X / Y) * -Y = (X % Y) - X
  239. {
  240. Value *Y = Op1;
  241. BinaryOperator *Div = dyn_cast<BinaryOperator>(Op0);
  242. if (!Div || (Div->getOpcode() != Instruction::UDiv &&
  243. Div->getOpcode() != Instruction::SDiv)) {
  244. Y = Op0;
  245. Div = dyn_cast<BinaryOperator>(Op1);
  246. }
  247. Value *Neg = dyn_castNegVal(Y);
  248. if (Div && Div->hasOneUse() &&
  249. (Div->getOperand(1) == Y || Div->getOperand(1) == Neg) &&
  250. (Div->getOpcode() == Instruction::UDiv ||
  251. Div->getOpcode() == Instruction::SDiv)) {
  252. Value *X = Div->getOperand(0), *DivOp1 = Div->getOperand(1);
  253. // If the division is exact, X % Y is zero, so we end up with X or -X.
  254. if (Div->isExact()) {
  255. if (DivOp1 == Y)
  256. return replaceInstUsesWith(I, X);
  257. return BinaryOperator::CreateNeg(X);
  258. }
  259. auto RemOpc = Div->getOpcode() == Instruction::UDiv ? Instruction::URem
  260. : Instruction::SRem;
  261. Value *Rem = Builder.CreateBinOp(RemOpc, X, DivOp1);
  262. if (DivOp1 == Y)
  263. return BinaryOperator::CreateSub(X, Rem);
  264. return BinaryOperator::CreateSub(Rem, X);
  265. }
  266. }
  267. /// i1 mul -> i1 and.
  268. if (I.getType()->isIntOrIntVectorTy(1))
  269. return BinaryOperator::CreateAnd(Op0, Op1);
  270. // X*(1 << Y) --> X << Y
  271. // (1 << Y)*X --> X << Y
  272. {
  273. Value *Y;
  274. BinaryOperator *BO = nullptr;
  275. bool ShlNSW = false;
  276. if (match(Op0, m_Shl(m_One(), m_Value(Y)))) {
  277. BO = BinaryOperator::CreateShl(Op1, Y);
  278. ShlNSW = cast<ShlOperator>(Op0)->hasNoSignedWrap();
  279. } else if (match(Op1, m_Shl(m_One(), m_Value(Y)))) {
  280. BO = BinaryOperator::CreateShl(Op0, Y);
  281. ShlNSW = cast<ShlOperator>(Op1)->hasNoSignedWrap();
  282. }
  283. if (BO) {
  284. if (I.hasNoUnsignedWrap())
  285. BO->setHasNoUnsignedWrap();
  286. if (I.hasNoSignedWrap() && ShlNSW)
  287. BO->setHasNoSignedWrap();
  288. return BO;
  289. }
  290. }
  291. // (zext bool X) * (zext bool Y) --> zext (and X, Y)
  292. // (sext bool X) * (sext bool Y) --> zext (and X, Y)
  293. // Note: -1 * -1 == 1 * 1 == 1 (if the extends match, the result is the same)
  294. if (((match(Op0, m_ZExt(m_Value(X))) && match(Op1, m_ZExt(m_Value(Y)))) ||
  295. (match(Op0, m_SExt(m_Value(X))) && match(Op1, m_SExt(m_Value(Y))))) &&
  296. X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
  297. (Op0->hasOneUse() || Op1->hasOneUse() || X == Y)) {
  298. Value *And = Builder.CreateAnd(X, Y, "mulbool");
  299. return CastInst::Create(Instruction::ZExt, And, I.getType());
  300. }
  301. // (sext bool X) * (zext bool Y) --> sext (and X, Y)
  302. // (zext bool X) * (sext bool Y) --> sext (and X, Y)
  303. // Note: -1 * 1 == 1 * -1 == -1
  304. if (((match(Op0, m_SExt(m_Value(X))) && match(Op1, m_ZExt(m_Value(Y)))) ||
  305. (match(Op0, m_ZExt(m_Value(X))) && match(Op1, m_SExt(m_Value(Y))))) &&
  306. X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType() &&
  307. (Op0->hasOneUse() || Op1->hasOneUse())) {
  308. Value *And = Builder.CreateAnd(X, Y, "mulbool");
  309. return CastInst::Create(Instruction::SExt, And, I.getType());
  310. }
  311. // (zext bool X) * Y --> X ? Y : 0
  312. // Y * (zext bool X) --> X ? Y : 0
  313. if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
  314. return SelectInst::Create(X, Op1, ConstantInt::get(I.getType(), 0));
  315. if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
  316. return SelectInst::Create(X, Op0, ConstantInt::get(I.getType(), 0));
  317. // (sext bool X) * C --> X ? -C : 0
  318. Constant *ImmC;
  319. if (match(Op0, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1) &&
  320. match(Op1, m_ImmConstant(ImmC))) {
  321. Constant *NegC = ConstantExpr::getNeg(ImmC);
  322. return SelectInst::Create(X, NegC, ConstantInt::getNullValue(I.getType()));
  323. }
  324. // (lshr X, 31) * Y --> (ashr X, 31) & Y
  325. // Y * (lshr X, 31) --> (ashr X, 31) & Y
  326. // TODO: We are not checking one-use because the elimination of the multiply
  327. // is better for analysis?
  328. // TODO: Should we canonicalize to '(X < 0) ? Y : 0' instead? That would be
  329. // more similar to what we're doing above.
  330. const APInt *C;
  331. if (match(Op0, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1)
  332. return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op1);
  333. if (match(Op1, m_LShr(m_Value(X), m_APInt(C))) && *C == C->getBitWidth() - 1)
  334. return BinaryOperator::CreateAnd(Builder.CreateAShr(X, *C), Op0);
  335. // ((ashr X, 31) | 1) * X --> abs(X)
  336. // X * ((ashr X, 31) | 1) --> abs(X)
  337. if (match(&I, m_c_BinOp(m_Or(m_AShr(m_Value(X),
  338. m_SpecificIntAllowUndef(BitWidth - 1)),
  339. m_One()),
  340. m_Deferred(X)))) {
  341. Value *Abs = Builder.CreateBinaryIntrinsic(
  342. Intrinsic::abs, X,
  343. ConstantInt::getBool(I.getContext(), I.hasNoSignedWrap()));
  344. Abs->takeName(&I);
  345. return replaceInstUsesWith(I, Abs);
  346. }
  347. if (Instruction *Ext = narrowMathIfNoOverflow(I))
  348. return Ext;
  349. bool Changed = false;
  350. if (!I.hasNoSignedWrap() && willNotOverflowSignedMul(Op0, Op1, I)) {
  351. Changed = true;
  352. I.setHasNoSignedWrap(true);
  353. }
  354. if (!I.hasNoUnsignedWrap() && willNotOverflowUnsignedMul(Op0, Op1, I)) {
  355. Changed = true;
  356. I.setHasNoUnsignedWrap(true);
  357. }
  358. return Changed ? &I : nullptr;
  359. }
  360. Instruction *InstCombinerImpl::foldFPSignBitOps(BinaryOperator &I) {
  361. BinaryOperator::BinaryOps Opcode = I.getOpcode();
  362. assert((Opcode == Instruction::FMul || Opcode == Instruction::FDiv) &&
  363. "Expected fmul or fdiv");
  364. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  365. Value *X, *Y;
  366. // -X * -Y --> X * Y
  367. // -X / -Y --> X / Y
  368. if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_FNeg(m_Value(Y))))
  369. return BinaryOperator::CreateWithCopiedFlags(Opcode, X, Y, &I);
  370. // fabs(X) * fabs(X) -> X * X
  371. // fabs(X) / fabs(X) -> X / X
  372. if (Op0 == Op1 && match(Op0, m_FAbs(m_Value(X))))
  373. return BinaryOperator::CreateWithCopiedFlags(Opcode, X, X, &I);
  374. // fabs(X) * fabs(Y) --> fabs(X * Y)
  375. // fabs(X) / fabs(Y) --> fabs(X / Y)
  376. if (match(Op0, m_FAbs(m_Value(X))) && match(Op1, m_FAbs(m_Value(Y))) &&
  377. (Op0->hasOneUse() || Op1->hasOneUse())) {
  378. IRBuilder<>::FastMathFlagGuard FMFGuard(Builder);
  379. Builder.setFastMathFlags(I.getFastMathFlags());
  380. Value *XY = Builder.CreateBinOp(Opcode, X, Y);
  381. Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, XY);
  382. Fabs->takeName(&I);
  383. return replaceInstUsesWith(I, Fabs);
  384. }
  385. return nullptr;
  386. }
  387. Instruction *InstCombinerImpl::visitFMul(BinaryOperator &I) {
  388. if (Value *V = SimplifyFMulInst(I.getOperand(0), I.getOperand(1),
  389. I.getFastMathFlags(),
  390. SQ.getWithInstruction(&I)))
  391. return replaceInstUsesWith(I, V);
  392. if (SimplifyAssociativeOrCommutative(I))
  393. return &I;
  394. if (Instruction *X = foldVectorBinop(I))
  395. return X;
  396. if (Instruction *Phi = foldBinopWithPhiOperands(I))
  397. return Phi;
  398. if (Instruction *FoldedMul = foldBinOpIntoSelectOrPhi(I))
  399. return FoldedMul;
  400. if (Value *FoldedMul = foldMulSelectToNegate(I, Builder))
  401. return replaceInstUsesWith(I, FoldedMul);
  402. if (Instruction *R = foldFPSignBitOps(I))
  403. return R;
  404. // X * -1.0 --> -X
  405. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  406. if (match(Op1, m_SpecificFP(-1.0)))
  407. return UnaryOperator::CreateFNegFMF(Op0, &I);
  408. // -X * C --> X * -C
  409. Value *X, *Y;
  410. Constant *C;
  411. if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Constant(C)))
  412. return BinaryOperator::CreateFMulFMF(X, ConstantExpr::getFNeg(C), &I);
  413. // (select A, B, C) * (select A, D, E) --> select A, (B*D), (C*E)
  414. if (Value *V = SimplifySelectsFeedingBinaryOp(I, Op0, Op1))
  415. return replaceInstUsesWith(I, V);
  416. if (I.hasAllowReassoc()) {
  417. // Reassociate constant RHS with another constant to form constant
  418. // expression.
  419. if (match(Op1, m_Constant(C)) && C->isFiniteNonZeroFP()) {
  420. Constant *C1;
  421. if (match(Op0, m_OneUse(m_FDiv(m_Constant(C1), m_Value(X))))) {
  422. // (C1 / X) * C --> (C * C1) / X
  423. Constant *CC1 = ConstantExpr::getFMul(C, C1);
  424. if (CC1->isNormalFP())
  425. return BinaryOperator::CreateFDivFMF(CC1, X, &I);
  426. }
  427. if (match(Op0, m_FDiv(m_Value(X), m_Constant(C1)))) {
  428. // (X / C1) * C --> X * (C / C1)
  429. Constant *CDivC1 = ConstantExpr::getFDiv(C, C1);
  430. if (CDivC1->isNormalFP())
  431. return BinaryOperator::CreateFMulFMF(X, CDivC1, &I);
  432. // If the constant was a denormal, try reassociating differently.
  433. // (X / C1) * C --> X / (C1 / C)
  434. Constant *C1DivC = ConstantExpr::getFDiv(C1, C);
  435. if (Op0->hasOneUse() && C1DivC->isNormalFP())
  436. return BinaryOperator::CreateFDivFMF(X, C1DivC, &I);
  437. }
  438. // We do not need to match 'fadd C, X' and 'fsub X, C' because they are
  439. // canonicalized to 'fadd X, C'. Distributing the multiply may allow
  440. // further folds and (X * C) + C2 is 'fma'.
  441. if (match(Op0, m_OneUse(m_FAdd(m_Value(X), m_Constant(C1))))) {
  442. // (X + C1) * C --> (X * C) + (C * C1)
  443. Constant *CC1 = ConstantExpr::getFMul(C, C1);
  444. Value *XC = Builder.CreateFMulFMF(X, C, &I);
  445. return BinaryOperator::CreateFAddFMF(XC, CC1, &I);
  446. }
  447. if (match(Op0, m_OneUse(m_FSub(m_Constant(C1), m_Value(X))))) {
  448. // (C1 - X) * C --> (C * C1) - (X * C)
  449. Constant *CC1 = ConstantExpr::getFMul(C, C1);
  450. Value *XC = Builder.CreateFMulFMF(X, C, &I);
  451. return BinaryOperator::CreateFSubFMF(CC1, XC, &I);
  452. }
  453. }
  454. Value *Z;
  455. if (match(&I, m_c_FMul(m_OneUse(m_FDiv(m_Value(X), m_Value(Y))),
  456. m_Value(Z)))) {
  457. // Sink division: (X / Y) * Z --> (X * Z) / Y
  458. Value *NewFMul = Builder.CreateFMulFMF(X, Z, &I);
  459. return BinaryOperator::CreateFDivFMF(NewFMul, Y, &I);
  460. }
  461. // sqrt(X) * sqrt(Y) -> sqrt(X * Y)
  462. // nnan disallows the possibility of returning a number if both operands are
  463. // negative (in that case, we should return NaN).
  464. if (I.hasNoNaNs() &&
  465. match(Op0, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(X)))) &&
  466. match(Op1, m_OneUse(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) {
  467. Value *XY = Builder.CreateFMulFMF(X, Y, &I);
  468. Value *Sqrt = Builder.CreateUnaryIntrinsic(Intrinsic::sqrt, XY, &I);
  469. return replaceInstUsesWith(I, Sqrt);
  470. }
  471. // The following transforms are done irrespective of the number of uses
  472. // for the expression "1.0/sqrt(X)".
  473. // 1) 1.0/sqrt(X) * X -> X/sqrt(X)
  474. // 2) X * 1.0/sqrt(X) -> X/sqrt(X)
  475. // We always expect the backend to reduce X/sqrt(X) to sqrt(X), if it
  476. // has the necessary (reassoc) fast-math-flags.
  477. if (I.hasNoSignedZeros() &&
  478. match(Op0, (m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) &&
  479. match(Y, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))) && Op1 == X)
  480. return BinaryOperator::CreateFDivFMF(X, Y, &I);
  481. if (I.hasNoSignedZeros() &&
  482. match(Op1, (m_FDiv(m_SpecificFP(1.0), m_Value(Y)))) &&
  483. match(Y, m_Intrinsic<Intrinsic::sqrt>(m_Value(X))) && Op0 == X)
  484. return BinaryOperator::CreateFDivFMF(X, Y, &I);
  485. // Like the similar transform in instsimplify, this requires 'nsz' because
  486. // sqrt(-0.0) = -0.0, and -0.0 * -0.0 does not simplify to -0.0.
  487. if (I.hasNoNaNs() && I.hasNoSignedZeros() && Op0 == Op1 &&
  488. Op0->hasNUses(2)) {
  489. // Peek through fdiv to find squaring of square root:
  490. // (X / sqrt(Y)) * (X / sqrt(Y)) --> (X * X) / Y
  491. if (match(Op0, m_FDiv(m_Value(X),
  492. m_Intrinsic<Intrinsic::sqrt>(m_Value(Y))))) {
  493. Value *XX = Builder.CreateFMulFMF(X, X, &I);
  494. return BinaryOperator::CreateFDivFMF(XX, Y, &I);
  495. }
  496. // (sqrt(Y) / X) * (sqrt(Y) / X) --> Y / (X * X)
  497. if (match(Op0, m_FDiv(m_Intrinsic<Intrinsic::sqrt>(m_Value(Y)),
  498. m_Value(X)))) {
  499. Value *XX = Builder.CreateFMulFMF(X, X, &I);
  500. return BinaryOperator::CreateFDivFMF(Y, XX, &I);
  501. }
  502. }
  503. if (I.isOnlyUserOfAnyOperand()) {
  504. // pow(x, y) * pow(x, z) -> pow(x, y + z)
  505. if (match(Op0, m_Intrinsic<Intrinsic::pow>(m_Value(X), m_Value(Y))) &&
  506. match(Op1, m_Intrinsic<Intrinsic::pow>(m_Specific(X), m_Value(Z)))) {
  507. auto *YZ = Builder.CreateFAddFMF(Y, Z, &I);
  508. auto *NewPow = Builder.CreateBinaryIntrinsic(Intrinsic::pow, X, YZ, &I);
  509. return replaceInstUsesWith(I, NewPow);
  510. }
  511. // powi(x, y) * powi(x, z) -> powi(x, y + z)
  512. if (match(Op0, m_Intrinsic<Intrinsic::powi>(m_Value(X), m_Value(Y))) &&
  513. match(Op1, m_Intrinsic<Intrinsic::powi>(m_Specific(X), m_Value(Z))) &&
  514. Y->getType() == Z->getType()) {
  515. auto *YZ = Builder.CreateAdd(Y, Z);
  516. auto *NewPow = Builder.CreateIntrinsic(
  517. Intrinsic::powi, {X->getType(), YZ->getType()}, {X, YZ}, &I);
  518. return replaceInstUsesWith(I, NewPow);
  519. }
  520. // exp(X) * exp(Y) -> exp(X + Y)
  521. if (match(Op0, m_Intrinsic<Intrinsic::exp>(m_Value(X))) &&
  522. match(Op1, m_Intrinsic<Intrinsic::exp>(m_Value(Y)))) {
  523. Value *XY = Builder.CreateFAddFMF(X, Y, &I);
  524. Value *Exp = Builder.CreateUnaryIntrinsic(Intrinsic::exp, XY, &I);
  525. return replaceInstUsesWith(I, Exp);
  526. }
  527. // exp2(X) * exp2(Y) -> exp2(X + Y)
  528. if (match(Op0, m_Intrinsic<Intrinsic::exp2>(m_Value(X))) &&
  529. match(Op1, m_Intrinsic<Intrinsic::exp2>(m_Value(Y)))) {
  530. Value *XY = Builder.CreateFAddFMF(X, Y, &I);
  531. Value *Exp2 = Builder.CreateUnaryIntrinsic(Intrinsic::exp2, XY, &I);
  532. return replaceInstUsesWith(I, Exp2);
  533. }
  534. }
  535. // (X*Y) * X => (X*X) * Y where Y != X
  536. // The purpose is two-fold:
  537. // 1) to form a power expression (of X).
  538. // 2) potentially shorten the critical path: After transformation, the
  539. // latency of the instruction Y is amortized by the expression of X*X,
  540. // and therefore Y is in a "less critical" position compared to what it
  541. // was before the transformation.
  542. if (match(Op0, m_OneUse(m_c_FMul(m_Specific(Op1), m_Value(Y)))) &&
  543. Op1 != Y) {
  544. Value *XX = Builder.CreateFMulFMF(Op1, Op1, &I);
  545. return BinaryOperator::CreateFMulFMF(XX, Y, &I);
  546. }
  547. if (match(Op1, m_OneUse(m_c_FMul(m_Specific(Op0), m_Value(Y)))) &&
  548. Op0 != Y) {
  549. Value *XX = Builder.CreateFMulFMF(Op0, Op0, &I);
  550. return BinaryOperator::CreateFMulFMF(XX, Y, &I);
  551. }
  552. }
  553. // log2(X * 0.5) * Y = log2(X) * Y - Y
  554. if (I.isFast()) {
  555. IntrinsicInst *Log2 = nullptr;
  556. if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::log2>(
  557. m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) {
  558. Log2 = cast<IntrinsicInst>(Op0);
  559. Y = Op1;
  560. }
  561. if (match(Op1, m_OneUse(m_Intrinsic<Intrinsic::log2>(
  562. m_OneUse(m_FMul(m_Value(X), m_SpecificFP(0.5))))))) {
  563. Log2 = cast<IntrinsicInst>(Op1);
  564. Y = Op0;
  565. }
  566. if (Log2) {
  567. Value *Log2 = Builder.CreateUnaryIntrinsic(Intrinsic::log2, X, &I);
  568. Value *LogXTimesY = Builder.CreateFMulFMF(Log2, Y, &I);
  569. return BinaryOperator::CreateFSubFMF(LogXTimesY, Y, &I);
  570. }
  571. }
  572. return nullptr;
  573. }
  574. /// Fold a divide or remainder with a select instruction divisor when one of the
  575. /// select operands is zero. In that case, we can use the other select operand
  576. /// because div/rem by zero is undefined.
  577. bool InstCombinerImpl::simplifyDivRemOfSelectWithZeroOp(BinaryOperator &I) {
  578. SelectInst *SI = dyn_cast<SelectInst>(I.getOperand(1));
  579. if (!SI)
  580. return false;
  581. int NonNullOperand;
  582. if (match(SI->getTrueValue(), m_Zero()))
  583. // div/rem X, (Cond ? 0 : Y) -> div/rem X, Y
  584. NonNullOperand = 2;
  585. else if (match(SI->getFalseValue(), m_Zero()))
  586. // div/rem X, (Cond ? Y : 0) -> div/rem X, Y
  587. NonNullOperand = 1;
  588. else
  589. return false;
  590. // Change the div/rem to use 'Y' instead of the select.
  591. replaceOperand(I, 1, SI->getOperand(NonNullOperand));
  592. // Okay, we know we replace the operand of the div/rem with 'Y' with no
  593. // problem. However, the select, or the condition of the select may have
  594. // multiple uses. Based on our knowledge that the operand must be non-zero,
  595. // propagate the known value for the select into other uses of it, and
  596. // propagate a known value of the condition into its other users.
  597. // If the select and condition only have a single use, don't bother with this,
  598. // early exit.
  599. Value *SelectCond = SI->getCondition();
  600. if (SI->use_empty() && SelectCond->hasOneUse())
  601. return true;
  602. // Scan the current block backward, looking for other uses of SI.
  603. BasicBlock::iterator BBI = I.getIterator(), BBFront = I.getParent()->begin();
  604. Type *CondTy = SelectCond->getType();
  605. while (BBI != BBFront) {
  606. --BBI;
  607. // If we found an instruction that we can't assume will return, so
  608. // information from below it cannot be propagated above it.
  609. if (!isGuaranteedToTransferExecutionToSuccessor(&*BBI))
  610. break;
  611. // Replace uses of the select or its condition with the known values.
  612. for (Use &Op : BBI->operands()) {
  613. if (Op == SI) {
  614. replaceUse(Op, SI->getOperand(NonNullOperand));
  615. Worklist.push(&*BBI);
  616. } else if (Op == SelectCond) {
  617. replaceUse(Op, NonNullOperand == 1 ? ConstantInt::getTrue(CondTy)
  618. : ConstantInt::getFalse(CondTy));
  619. Worklist.push(&*BBI);
  620. }
  621. }
  622. // If we past the instruction, quit looking for it.
  623. if (&*BBI == SI)
  624. SI = nullptr;
  625. if (&*BBI == SelectCond)
  626. SelectCond = nullptr;
  627. // If we ran out of things to eliminate, break out of the loop.
  628. if (!SelectCond && !SI)
  629. break;
  630. }
  631. return true;
  632. }
  633. /// True if the multiply can not be expressed in an int this size.
  634. static bool multiplyOverflows(const APInt &C1, const APInt &C2, APInt &Product,
  635. bool IsSigned) {
  636. bool Overflow;
  637. Product = IsSigned ? C1.smul_ov(C2, Overflow) : C1.umul_ov(C2, Overflow);
  638. return Overflow;
  639. }
  640. /// True if C1 is a multiple of C2. Quotient contains C1/C2.
  641. static bool isMultiple(const APInt &C1, const APInt &C2, APInt &Quotient,
  642. bool IsSigned) {
  643. assert(C1.getBitWidth() == C2.getBitWidth() && "Constant widths not equal");
  644. // Bail if we will divide by zero.
  645. if (C2.isZero())
  646. return false;
  647. // Bail if we would divide INT_MIN by -1.
  648. if (IsSigned && C1.isMinSignedValue() && C2.isAllOnes())
  649. return false;
  650. APInt Remainder(C1.getBitWidth(), /*val=*/0ULL, IsSigned);
  651. if (IsSigned)
  652. APInt::sdivrem(C1, C2, Quotient, Remainder);
  653. else
  654. APInt::udivrem(C1, C2, Quotient, Remainder);
  655. return Remainder.isMinValue();
  656. }
  657. /// This function implements the transforms common to both integer division
  658. /// instructions (udiv and sdiv). It is called by the visitors to those integer
  659. /// division instructions.
  660. /// Common integer divide transforms
  661. Instruction *InstCombinerImpl::commonIDivTransforms(BinaryOperator &I) {
  662. if (Instruction *Phi = foldBinopWithPhiOperands(I))
  663. return Phi;
  664. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  665. bool IsSigned = I.getOpcode() == Instruction::SDiv;
  666. Type *Ty = I.getType();
  667. // The RHS is known non-zero.
  668. if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I))
  669. return replaceOperand(I, 1, V);
  670. // Handle cases involving: [su]div X, (select Cond, Y, Z)
  671. // This does not apply for fdiv.
  672. if (simplifyDivRemOfSelectWithZeroOp(I))
  673. return &I;
  674. // If the divisor is a select-of-constants, try to constant fold all div ops:
  675. // C / (select Cond, TrueC, FalseC) --> select Cond, (C / TrueC), (C / FalseC)
  676. // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds.
  677. if (match(Op0, m_ImmConstant()) &&
  678. match(Op1, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) {
  679. if (Instruction *R = FoldOpIntoSelect(I, cast<SelectInst>(Op1)))
  680. return R;
  681. }
  682. const APInt *C2;
  683. if (match(Op1, m_APInt(C2))) {
  684. Value *X;
  685. const APInt *C1;
  686. // (X / C1) / C2 -> X / (C1*C2)
  687. if ((IsSigned && match(Op0, m_SDiv(m_Value(X), m_APInt(C1)))) ||
  688. (!IsSigned && match(Op0, m_UDiv(m_Value(X), m_APInt(C1))))) {
  689. APInt Product(C1->getBitWidth(), /*val=*/0ULL, IsSigned);
  690. if (!multiplyOverflows(*C1, *C2, Product, IsSigned))
  691. return BinaryOperator::Create(I.getOpcode(), X,
  692. ConstantInt::get(Ty, Product));
  693. }
  694. if ((IsSigned && match(Op0, m_NSWMul(m_Value(X), m_APInt(C1)))) ||
  695. (!IsSigned && match(Op0, m_NUWMul(m_Value(X), m_APInt(C1))))) {
  696. APInt Quotient(C1->getBitWidth(), /*val=*/0ULL, IsSigned);
  697. // (X * C1) / C2 -> X / (C2 / C1) if C2 is a multiple of C1.
  698. if (isMultiple(*C2, *C1, Quotient, IsSigned)) {
  699. auto *NewDiv = BinaryOperator::Create(I.getOpcode(), X,
  700. ConstantInt::get(Ty, Quotient));
  701. NewDiv->setIsExact(I.isExact());
  702. return NewDiv;
  703. }
  704. // (X * C1) / C2 -> X * (C1 / C2) if C1 is a multiple of C2.
  705. if (isMultiple(*C1, *C2, Quotient, IsSigned)) {
  706. auto *Mul = BinaryOperator::Create(Instruction::Mul, X,
  707. ConstantInt::get(Ty, Quotient));
  708. auto *OBO = cast<OverflowingBinaryOperator>(Op0);
  709. Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
  710. Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
  711. return Mul;
  712. }
  713. }
  714. if ((IsSigned && match(Op0, m_NSWShl(m_Value(X), m_APInt(C1))) &&
  715. C1->ult(C1->getBitWidth() - 1)) ||
  716. (!IsSigned && match(Op0, m_NUWShl(m_Value(X), m_APInt(C1))) &&
  717. C1->ult(C1->getBitWidth()))) {
  718. APInt Quotient(C1->getBitWidth(), /*val=*/0ULL, IsSigned);
  719. APInt C1Shifted = APInt::getOneBitSet(
  720. C1->getBitWidth(), static_cast<unsigned>(C1->getZExtValue()));
  721. // (X << C1) / C2 -> X / (C2 >> C1) if C2 is a multiple of 1 << C1.
  722. if (isMultiple(*C2, C1Shifted, Quotient, IsSigned)) {
  723. auto *BO = BinaryOperator::Create(I.getOpcode(), X,
  724. ConstantInt::get(Ty, Quotient));
  725. BO->setIsExact(I.isExact());
  726. return BO;
  727. }
  728. // (X << C1) / C2 -> X * ((1 << C1) / C2) if 1 << C1 is a multiple of C2.
  729. if (isMultiple(C1Shifted, *C2, Quotient, IsSigned)) {
  730. auto *Mul = BinaryOperator::Create(Instruction::Mul, X,
  731. ConstantInt::get(Ty, Quotient));
  732. auto *OBO = cast<OverflowingBinaryOperator>(Op0);
  733. Mul->setHasNoUnsignedWrap(!IsSigned && OBO->hasNoUnsignedWrap());
  734. Mul->setHasNoSignedWrap(OBO->hasNoSignedWrap());
  735. return Mul;
  736. }
  737. }
  738. if (!C2->isZero()) // avoid X udiv 0
  739. if (Instruction *FoldedDiv = foldBinOpIntoSelectOrPhi(I))
  740. return FoldedDiv;
  741. }
  742. if (match(Op0, m_One())) {
  743. assert(!Ty->isIntOrIntVectorTy(1) && "i1 divide not removed?");
  744. if (IsSigned) {
  745. // If Op1 is 0 then it's undefined behaviour, if Op1 is 1 then the
  746. // result is one, if Op1 is -1 then the result is minus one, otherwise
  747. // it's zero.
  748. Value *Inc = Builder.CreateAdd(Op1, Op0);
  749. Value *Cmp = Builder.CreateICmpULT(Inc, ConstantInt::get(Ty, 3));
  750. return SelectInst::Create(Cmp, Op1, ConstantInt::get(Ty, 0));
  751. } else {
  752. // If Op1 is 0 then it's undefined behaviour. If Op1 is 1 then the
  753. // result is one, otherwise it's zero.
  754. return new ZExtInst(Builder.CreateICmpEQ(Op1, Op0), Ty);
  755. }
  756. }
  757. // See if we can fold away this div instruction.
  758. if (SimplifyDemandedInstructionBits(I))
  759. return &I;
  760. // (X - (X rem Y)) / Y -> X / Y; usually originates as ((X / Y) * Y) / Y
  761. Value *X, *Z;
  762. if (match(Op0, m_Sub(m_Value(X), m_Value(Z)))) // (X - Z) / Y; Y = Op1
  763. if ((IsSigned && match(Z, m_SRem(m_Specific(X), m_Specific(Op1)))) ||
  764. (!IsSigned && match(Z, m_URem(m_Specific(X), m_Specific(Op1)))))
  765. return BinaryOperator::Create(I.getOpcode(), X, Op1);
  766. // (X << Y) / X -> 1 << Y
  767. Value *Y;
  768. if (IsSigned && match(Op0, m_NSWShl(m_Specific(Op1), m_Value(Y))))
  769. return BinaryOperator::CreateNSWShl(ConstantInt::get(Ty, 1), Y);
  770. if (!IsSigned && match(Op0, m_NUWShl(m_Specific(Op1), m_Value(Y))))
  771. return BinaryOperator::CreateNUWShl(ConstantInt::get(Ty, 1), Y);
  772. // X / (X * Y) -> 1 / Y if the multiplication does not overflow.
  773. if (match(Op1, m_c_Mul(m_Specific(Op0), m_Value(Y)))) {
  774. bool HasNSW = cast<OverflowingBinaryOperator>(Op1)->hasNoSignedWrap();
  775. bool HasNUW = cast<OverflowingBinaryOperator>(Op1)->hasNoUnsignedWrap();
  776. if ((IsSigned && HasNSW) || (!IsSigned && HasNUW)) {
  777. replaceOperand(I, 0, ConstantInt::get(Ty, 1));
  778. replaceOperand(I, 1, Y);
  779. return &I;
  780. }
  781. }
  782. return nullptr;
  783. }
  784. static const unsigned MaxDepth = 6;
  785. namespace {
  786. using FoldUDivOperandCb = Instruction *(*)(Value *Op0, Value *Op1,
  787. const BinaryOperator &I,
  788. InstCombinerImpl &IC);
  789. /// Used to maintain state for visitUDivOperand().
  790. struct UDivFoldAction {
  791. /// Informs visitUDiv() how to fold this operand. This can be zero if this
  792. /// action joins two actions together.
  793. FoldUDivOperandCb FoldAction;
  794. /// Which operand to fold.
  795. Value *OperandToFold;
  796. union {
  797. /// The instruction returned when FoldAction is invoked.
  798. Instruction *FoldResult;
  799. /// Stores the LHS action index if this action joins two actions together.
  800. size_t SelectLHSIdx;
  801. };
  802. UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand)
  803. : FoldAction(FA), OperandToFold(InputOperand), FoldResult(nullptr) {}
  804. UDivFoldAction(FoldUDivOperandCb FA, Value *InputOperand, size_t SLHS)
  805. : FoldAction(FA), OperandToFold(InputOperand), SelectLHSIdx(SLHS) {}
  806. };
  807. } // end anonymous namespace
  808. // X udiv 2^C -> X >> C
  809. static Instruction *foldUDivPow2Cst(Value *Op0, Value *Op1,
  810. const BinaryOperator &I,
  811. InstCombinerImpl &IC) {
  812. Constant *C1 = ConstantExpr::getExactLogBase2(cast<Constant>(Op1));
  813. if (!C1)
  814. llvm_unreachable("Failed to constant fold udiv -> logbase2");
  815. BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, C1);
  816. if (I.isExact())
  817. LShr->setIsExact();
  818. return LShr;
  819. }
  820. // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
  821. // X udiv (zext (C1 << N)), where C1 is "1<<C2" --> X >> (N+C2)
  822. static Instruction *foldUDivShl(Value *Op0, Value *Op1, const BinaryOperator &I,
  823. InstCombinerImpl &IC) {
  824. Value *ShiftLeft;
  825. if (!match(Op1, m_ZExt(m_Value(ShiftLeft))))
  826. ShiftLeft = Op1;
  827. Constant *CI;
  828. Value *N;
  829. if (!match(ShiftLeft, m_Shl(m_Constant(CI), m_Value(N))))
  830. llvm_unreachable("match should never fail here!");
  831. Constant *Log2Base = ConstantExpr::getExactLogBase2(CI);
  832. if (!Log2Base)
  833. llvm_unreachable("getLogBase2 should never fail here!");
  834. N = IC.Builder.CreateAdd(N, Log2Base);
  835. if (Op1 != ShiftLeft)
  836. N = IC.Builder.CreateZExt(N, Op1->getType());
  837. BinaryOperator *LShr = BinaryOperator::CreateLShr(Op0, N);
  838. if (I.isExact())
  839. LShr->setIsExact();
  840. return LShr;
  841. }
  842. // Recursively visits the possible right hand operands of a udiv
  843. // instruction, seeing through select instructions, to determine if we can
  844. // replace the udiv with something simpler. If we find that an operand is not
  845. // able to simplify the udiv, we abort the entire transformation.
  846. static size_t visitUDivOperand(Value *Op0, Value *Op1, const BinaryOperator &I,
  847. SmallVectorImpl<UDivFoldAction> &Actions,
  848. unsigned Depth = 0) {
  849. // FIXME: assert that Op1 isn't/doesn't contain undef.
  850. // Check to see if this is an unsigned division with an exact power of 2,
  851. // if so, convert to a right shift.
  852. if (match(Op1, m_Power2())) {
  853. Actions.push_back(UDivFoldAction(foldUDivPow2Cst, Op1));
  854. return Actions.size();
  855. }
  856. // X udiv (C1 << N), where C1 is "1<<C2" --> X >> (N+C2)
  857. if (match(Op1, m_Shl(m_Power2(), m_Value())) ||
  858. match(Op1, m_ZExt(m_Shl(m_Power2(), m_Value())))) {
  859. Actions.push_back(UDivFoldAction(foldUDivShl, Op1));
  860. return Actions.size();
  861. }
  862. // The remaining tests are all recursive, so bail out if we hit the limit.
  863. if (Depth++ == MaxDepth)
  864. return 0;
  865. if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
  866. // FIXME: missed optimization: if one of the hands of select is/contains
  867. // undef, just directly pick the other one.
  868. // FIXME: can both hands contain undef?
  869. if (size_t LHSIdx =
  870. visitUDivOperand(Op0, SI->getOperand(1), I, Actions, Depth))
  871. if (visitUDivOperand(Op0, SI->getOperand(2), I, Actions, Depth)) {
  872. Actions.push_back(UDivFoldAction(nullptr, Op1, LHSIdx - 1));
  873. return Actions.size();
  874. }
  875. return 0;
  876. }
  877. /// If we have zero-extended operands of an unsigned div or rem, we may be able
  878. /// to narrow the operation (sink the zext below the math).
  879. static Instruction *narrowUDivURem(BinaryOperator &I,
  880. InstCombiner::BuilderTy &Builder) {
  881. Instruction::BinaryOps Opcode = I.getOpcode();
  882. Value *N = I.getOperand(0);
  883. Value *D = I.getOperand(1);
  884. Type *Ty = I.getType();
  885. Value *X, *Y;
  886. if (match(N, m_ZExt(m_Value(X))) && match(D, m_ZExt(m_Value(Y))) &&
  887. X->getType() == Y->getType() && (N->hasOneUse() || D->hasOneUse())) {
  888. // udiv (zext X), (zext Y) --> zext (udiv X, Y)
  889. // urem (zext X), (zext Y) --> zext (urem X, Y)
  890. Value *NarrowOp = Builder.CreateBinOp(Opcode, X, Y);
  891. return new ZExtInst(NarrowOp, Ty);
  892. }
  893. Constant *C;
  894. if ((match(N, m_OneUse(m_ZExt(m_Value(X)))) && match(D, m_Constant(C))) ||
  895. (match(D, m_OneUse(m_ZExt(m_Value(X)))) && match(N, m_Constant(C)))) {
  896. // If the constant is the same in the smaller type, use the narrow version.
  897. Constant *TruncC = ConstantExpr::getTrunc(C, X->getType());
  898. if (ConstantExpr::getZExt(TruncC, Ty) != C)
  899. return nullptr;
  900. // udiv (zext X), C --> zext (udiv X, C')
  901. // urem (zext X), C --> zext (urem X, C')
  902. // udiv C, (zext X) --> zext (udiv C', X)
  903. // urem C, (zext X) --> zext (urem C', X)
  904. Value *NarrowOp = isa<Constant>(D) ? Builder.CreateBinOp(Opcode, X, TruncC)
  905. : Builder.CreateBinOp(Opcode, TruncC, X);
  906. return new ZExtInst(NarrowOp, Ty);
  907. }
  908. return nullptr;
  909. }
  910. Instruction *InstCombinerImpl::visitUDiv(BinaryOperator &I) {
  911. if (Value *V = SimplifyUDivInst(I.getOperand(0), I.getOperand(1),
  912. SQ.getWithInstruction(&I)))
  913. return replaceInstUsesWith(I, V);
  914. if (Instruction *X = foldVectorBinop(I))
  915. return X;
  916. // Handle the integer div common cases
  917. if (Instruction *Common = commonIDivTransforms(I))
  918. return Common;
  919. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  920. Value *X;
  921. const APInt *C1, *C2;
  922. if (match(Op0, m_LShr(m_Value(X), m_APInt(C1))) && match(Op1, m_APInt(C2))) {
  923. // (X lshr C1) udiv C2 --> X udiv (C2 << C1)
  924. bool Overflow;
  925. APInt C2ShlC1 = C2->ushl_ov(*C1, Overflow);
  926. if (!Overflow) {
  927. bool IsExact = I.isExact() && match(Op0, m_Exact(m_Value()));
  928. BinaryOperator *BO = BinaryOperator::CreateUDiv(
  929. X, ConstantInt::get(X->getType(), C2ShlC1));
  930. if (IsExact)
  931. BO->setIsExact();
  932. return BO;
  933. }
  934. }
  935. // Op0 / C where C is large (negative) --> zext (Op0 >= C)
  936. // TODO: Could use isKnownNegative() to handle non-constant values.
  937. Type *Ty = I.getType();
  938. if (match(Op1, m_Negative())) {
  939. Value *Cmp = Builder.CreateICmpUGE(Op0, Op1);
  940. return CastInst::CreateZExtOrBitCast(Cmp, Ty);
  941. }
  942. // Op0 / (sext i1 X) --> zext (Op0 == -1) (if X is 0, the div is undefined)
  943. if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
  944. Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty));
  945. return CastInst::CreateZExtOrBitCast(Cmp, Ty);
  946. }
  947. if (Instruction *NarrowDiv = narrowUDivURem(I, Builder))
  948. return NarrowDiv;
  949. // If the udiv operands are non-overflowing multiplies with a common operand,
  950. // then eliminate the common factor:
  951. // (A * B) / (A * X) --> B / X (and commuted variants)
  952. // TODO: The code would be reduced if we had m_c_NUWMul pattern matching.
  953. // TODO: If -reassociation handled this generally, we could remove this.
  954. Value *A, *B;
  955. if (match(Op0, m_NUWMul(m_Value(A), m_Value(B)))) {
  956. if (match(Op1, m_NUWMul(m_Specific(A), m_Value(X))) ||
  957. match(Op1, m_NUWMul(m_Value(X), m_Specific(A))))
  958. return BinaryOperator::CreateUDiv(B, X);
  959. if (match(Op1, m_NUWMul(m_Specific(B), m_Value(X))) ||
  960. match(Op1, m_NUWMul(m_Value(X), m_Specific(B))))
  961. return BinaryOperator::CreateUDiv(A, X);
  962. }
  963. // (LHS udiv (select (select (...)))) -> (LHS >> (select (select (...))))
  964. SmallVector<UDivFoldAction, 6> UDivActions;
  965. if (visitUDivOperand(Op0, Op1, I, UDivActions))
  966. for (unsigned i = 0, e = UDivActions.size(); i != e; ++i) {
  967. FoldUDivOperandCb Action = UDivActions[i].FoldAction;
  968. Value *ActionOp1 = UDivActions[i].OperandToFold;
  969. Instruction *Inst;
  970. if (Action)
  971. Inst = Action(Op0, ActionOp1, I, *this);
  972. else {
  973. // This action joins two actions together. The RHS of this action is
  974. // simply the last action we processed, we saved the LHS action index in
  975. // the joining action.
  976. size_t SelectRHSIdx = i - 1;
  977. Value *SelectRHS = UDivActions[SelectRHSIdx].FoldResult;
  978. size_t SelectLHSIdx = UDivActions[i].SelectLHSIdx;
  979. Value *SelectLHS = UDivActions[SelectLHSIdx].FoldResult;
  980. Inst = SelectInst::Create(cast<SelectInst>(ActionOp1)->getCondition(),
  981. SelectLHS, SelectRHS);
  982. }
  983. // If this is the last action to process, return it to the InstCombiner.
  984. // Otherwise, we insert it before the UDiv and record it so that we may
  985. // use it as part of a joining action (i.e., a SelectInst).
  986. if (e - i != 1) {
  987. Inst->insertBefore(&I);
  988. UDivActions[i].FoldResult = Inst;
  989. } else
  990. return Inst;
  991. }
  992. return nullptr;
  993. }
  994. Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
  995. if (Value *V = SimplifySDivInst(I.getOperand(0), I.getOperand(1),
  996. SQ.getWithInstruction(&I)))
  997. return replaceInstUsesWith(I, V);
  998. if (Instruction *X = foldVectorBinop(I))
  999. return X;
  1000. // Handle the integer div common cases
  1001. if (Instruction *Common = commonIDivTransforms(I))
  1002. return Common;
  1003. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1004. Type *Ty = I.getType();
  1005. Value *X;
  1006. // sdiv Op0, -1 --> -Op0
  1007. // sdiv Op0, (sext i1 X) --> -Op0 (because if X is 0, the op is undefined)
  1008. if (match(Op1, m_AllOnes()) ||
  1009. (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)))
  1010. return BinaryOperator::CreateNeg(Op0);
  1011. // X / INT_MIN --> X == INT_MIN
  1012. if (match(Op1, m_SignMask()))
  1013. return new ZExtInst(Builder.CreateICmpEQ(Op0, Op1), Ty);
  1014. // sdiv exact X, 1<<C --> ashr exact X, C iff 1<<C is non-negative
  1015. // sdiv exact X, -1<<C --> -(ashr exact X, C)
  1016. if (I.isExact() && ((match(Op1, m_Power2()) && match(Op1, m_NonNegative())) ||
  1017. match(Op1, m_NegatedPower2()))) {
  1018. bool DivisorWasNegative = match(Op1, m_NegatedPower2());
  1019. if (DivisorWasNegative)
  1020. Op1 = ConstantExpr::getNeg(cast<Constant>(Op1));
  1021. auto *AShr = BinaryOperator::CreateExactAShr(
  1022. Op0, ConstantExpr::getExactLogBase2(cast<Constant>(Op1)), I.getName());
  1023. if (!DivisorWasNegative)
  1024. return AShr;
  1025. Builder.Insert(AShr);
  1026. AShr->setName(I.getName() + ".neg");
  1027. return BinaryOperator::CreateNeg(AShr, I.getName());
  1028. }
  1029. const APInt *Op1C;
  1030. if (match(Op1, m_APInt(Op1C))) {
  1031. // If the dividend is sign-extended and the constant divisor is small enough
  1032. // to fit in the source type, shrink the division to the narrower type:
  1033. // (sext X) sdiv C --> sext (X sdiv C)
  1034. Value *Op0Src;
  1035. if (match(Op0, m_OneUse(m_SExt(m_Value(Op0Src)))) &&
  1036. Op0Src->getType()->getScalarSizeInBits() >= Op1C->getMinSignedBits()) {
  1037. // In the general case, we need to make sure that the dividend is not the
  1038. // minimum signed value because dividing that by -1 is UB. But here, we
  1039. // know that the -1 divisor case is already handled above.
  1040. Constant *NarrowDivisor =
  1041. ConstantExpr::getTrunc(cast<Constant>(Op1), Op0Src->getType());
  1042. Value *NarrowOp = Builder.CreateSDiv(Op0Src, NarrowDivisor);
  1043. return new SExtInst(NarrowOp, Ty);
  1044. }
  1045. // -X / C --> X / -C (if the negation doesn't overflow).
  1046. // TODO: This could be enhanced to handle arbitrary vector constants by
  1047. // checking if all elements are not the min-signed-val.
  1048. if (!Op1C->isMinSignedValue() &&
  1049. match(Op0, m_NSWSub(m_Zero(), m_Value(X)))) {
  1050. Constant *NegC = ConstantInt::get(Ty, -(*Op1C));
  1051. Instruction *BO = BinaryOperator::CreateSDiv(X, NegC);
  1052. BO->setIsExact(I.isExact());
  1053. return BO;
  1054. }
  1055. }
  1056. // -X / Y --> -(X / Y)
  1057. Value *Y;
  1058. if (match(&I, m_SDiv(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y))))
  1059. return BinaryOperator::CreateNSWNeg(
  1060. Builder.CreateSDiv(X, Y, I.getName(), I.isExact()));
  1061. // abs(X) / X --> X > -1 ? 1 : -1
  1062. // X / abs(X) --> X > -1 ? 1 : -1
  1063. if (match(&I, m_c_BinOp(
  1064. m_OneUse(m_Intrinsic<Intrinsic::abs>(m_Value(X), m_One())),
  1065. m_Deferred(X)))) {
  1066. Constant *NegOne = ConstantInt::getAllOnesValue(Ty);
  1067. Value *Cond = Builder.CreateICmpSGT(X, NegOne);
  1068. return SelectInst::Create(Cond, ConstantInt::get(Ty, 1), NegOne);
  1069. }
  1070. // If the sign bits of both operands are zero (i.e. we can prove they are
  1071. // unsigned inputs), turn this into a udiv.
  1072. APInt Mask(APInt::getSignMask(Ty->getScalarSizeInBits()));
  1073. if (MaskedValueIsZero(Op0, Mask, 0, &I)) {
  1074. if (MaskedValueIsZero(Op1, Mask, 0, &I)) {
  1075. // X sdiv Y -> X udiv Y, iff X and Y don't have sign bit set
  1076. auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
  1077. BO->setIsExact(I.isExact());
  1078. return BO;
  1079. }
  1080. if (match(Op1, m_NegatedPower2())) {
  1081. // X sdiv (-(1 << C)) -> -(X sdiv (1 << C)) ->
  1082. // -> -(X udiv (1 << C)) -> -(X u>> C)
  1083. return BinaryOperator::CreateNeg(Builder.Insert(foldUDivPow2Cst(
  1084. Op0, ConstantExpr::getNeg(cast<Constant>(Op1)), I, *this)));
  1085. }
  1086. if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
  1087. // X sdiv (1 << Y) -> X udiv (1 << Y) ( -> X u>> Y)
  1088. // Safe because the only negative value (1 << Y) can take on is
  1089. // INT_MIN, and X sdiv INT_MIN == X udiv INT_MIN == 0 if X doesn't have
  1090. // the sign bit set.
  1091. auto *BO = BinaryOperator::CreateUDiv(Op0, Op1, I.getName());
  1092. BO->setIsExact(I.isExact());
  1093. return BO;
  1094. }
  1095. }
  1096. return nullptr;
  1097. }
  1098. /// Remove negation and try to convert division into multiplication.
  1099. static Instruction *foldFDivConstantDivisor(BinaryOperator &I) {
  1100. Constant *C;
  1101. if (!match(I.getOperand(1), m_Constant(C)))
  1102. return nullptr;
  1103. // -X / C --> X / -C
  1104. Value *X;
  1105. if (match(I.getOperand(0), m_FNeg(m_Value(X))))
  1106. return BinaryOperator::CreateFDivFMF(X, ConstantExpr::getFNeg(C), &I);
  1107. // If the constant divisor has an exact inverse, this is always safe. If not,
  1108. // then we can still create a reciprocal if fast-math-flags allow it and the
  1109. // constant is a regular number (not zero, infinite, or denormal).
  1110. if (!(C->hasExactInverseFP() || (I.hasAllowReciprocal() && C->isNormalFP())))
  1111. return nullptr;
  1112. // Disallow denormal constants because we don't know what would happen
  1113. // on all targets.
  1114. // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
  1115. // denorms are flushed?
  1116. auto *RecipC = ConstantExpr::getFDiv(ConstantFP::get(I.getType(), 1.0), C);
  1117. if (!RecipC->isNormalFP())
  1118. return nullptr;
  1119. // X / C --> X * (1 / C)
  1120. return BinaryOperator::CreateFMulFMF(I.getOperand(0), RecipC, &I);
  1121. }
  1122. /// Remove negation and try to reassociate constant math.
  1123. static Instruction *foldFDivConstantDividend(BinaryOperator &I) {
  1124. Constant *C;
  1125. if (!match(I.getOperand(0), m_Constant(C)))
  1126. return nullptr;
  1127. // C / -X --> -C / X
  1128. Value *X;
  1129. if (match(I.getOperand(1), m_FNeg(m_Value(X))))
  1130. return BinaryOperator::CreateFDivFMF(ConstantExpr::getFNeg(C), X, &I);
  1131. if (!I.hasAllowReassoc() || !I.hasAllowReciprocal())
  1132. return nullptr;
  1133. // Try to reassociate C / X expressions where X includes another constant.
  1134. Constant *C2, *NewC = nullptr;
  1135. if (match(I.getOperand(1), m_FMul(m_Value(X), m_Constant(C2)))) {
  1136. // C / (X * C2) --> (C / C2) / X
  1137. NewC = ConstantExpr::getFDiv(C, C2);
  1138. } else if (match(I.getOperand(1), m_FDiv(m_Value(X), m_Constant(C2)))) {
  1139. // C / (X / C2) --> (C * C2) / X
  1140. NewC = ConstantExpr::getFMul(C, C2);
  1141. }
  1142. // Disallow denormal constants because we don't know what would happen
  1143. // on all targets.
  1144. // TODO: Use Intrinsic::canonicalize or let function attributes tell us that
  1145. // denorms are flushed?
  1146. if (!NewC || !NewC->isNormalFP())
  1147. return nullptr;
  1148. return BinaryOperator::CreateFDivFMF(NewC, X, &I);
  1149. }
  1150. /// Negate the exponent of pow/exp to fold division-by-pow() into multiply.
  1151. static Instruction *foldFDivPowDivisor(BinaryOperator &I,
  1152. InstCombiner::BuilderTy &Builder) {
  1153. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1154. auto *II = dyn_cast<IntrinsicInst>(Op1);
  1155. if (!II || !II->hasOneUse() || !I.hasAllowReassoc() ||
  1156. !I.hasAllowReciprocal())
  1157. return nullptr;
  1158. // Z / pow(X, Y) --> Z * pow(X, -Y)
  1159. // Z / exp{2}(Y) --> Z * exp{2}(-Y)
  1160. // In the general case, this creates an extra instruction, but fmul allows
  1161. // for better canonicalization and optimization than fdiv.
  1162. Intrinsic::ID IID = II->getIntrinsicID();
  1163. SmallVector<Value *> Args;
  1164. switch (IID) {
  1165. case Intrinsic::pow:
  1166. Args.push_back(II->getArgOperand(0));
  1167. Args.push_back(Builder.CreateFNegFMF(II->getArgOperand(1), &I));
  1168. break;
  1169. case Intrinsic::powi: {
  1170. // Require 'ninf' assuming that makes powi(X, -INT_MIN) acceptable.
  1171. // That is, X ** (huge negative number) is 0.0, ~1.0, or INF and so
  1172. // dividing by that is INF, ~1.0, or 0.0. Code that uses powi allows
  1173. // non-standard results, so this corner case should be acceptable if the
  1174. // code rules out INF values.
  1175. if (!I.hasNoInfs())
  1176. return nullptr;
  1177. Args.push_back(II->getArgOperand(0));
  1178. Args.push_back(Builder.CreateNeg(II->getArgOperand(1)));
  1179. Type *Tys[] = {I.getType(), II->getArgOperand(1)->getType()};
  1180. Value *Pow = Builder.CreateIntrinsic(IID, Tys, Args, &I);
  1181. return BinaryOperator::CreateFMulFMF(Op0, Pow, &I);
  1182. }
  1183. case Intrinsic::exp:
  1184. case Intrinsic::exp2:
  1185. Args.push_back(Builder.CreateFNegFMF(II->getArgOperand(0), &I));
  1186. break;
  1187. default:
  1188. return nullptr;
  1189. }
  1190. Value *Pow = Builder.CreateIntrinsic(IID, I.getType(), Args, &I);
  1191. return BinaryOperator::CreateFMulFMF(Op0, Pow, &I);
  1192. }
  1193. Instruction *InstCombinerImpl::visitFDiv(BinaryOperator &I) {
  1194. if (Value *V = SimplifyFDivInst(I.getOperand(0), I.getOperand(1),
  1195. I.getFastMathFlags(),
  1196. SQ.getWithInstruction(&I)))
  1197. return replaceInstUsesWith(I, V);
  1198. if (Instruction *X = foldVectorBinop(I))
  1199. return X;
  1200. if (Instruction *Phi = foldBinopWithPhiOperands(I))
  1201. return Phi;
  1202. if (Instruction *R = foldFDivConstantDivisor(I))
  1203. return R;
  1204. if (Instruction *R = foldFDivConstantDividend(I))
  1205. return R;
  1206. if (Instruction *R = foldFPSignBitOps(I))
  1207. return R;
  1208. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1209. if (isa<Constant>(Op0))
  1210. if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
  1211. if (Instruction *R = FoldOpIntoSelect(I, SI))
  1212. return R;
  1213. if (isa<Constant>(Op1))
  1214. if (SelectInst *SI = dyn_cast<SelectInst>(Op0))
  1215. if (Instruction *R = FoldOpIntoSelect(I, SI))
  1216. return R;
  1217. if (I.hasAllowReassoc() && I.hasAllowReciprocal()) {
  1218. Value *X, *Y;
  1219. if (match(Op0, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
  1220. (!isa<Constant>(Y) || !isa<Constant>(Op1))) {
  1221. // (X / Y) / Z => X / (Y * Z)
  1222. Value *YZ = Builder.CreateFMulFMF(Y, Op1, &I);
  1223. return BinaryOperator::CreateFDivFMF(X, YZ, &I);
  1224. }
  1225. if (match(Op1, m_OneUse(m_FDiv(m_Value(X), m_Value(Y)))) &&
  1226. (!isa<Constant>(Y) || !isa<Constant>(Op0))) {
  1227. // Z / (X / Y) => (Y * Z) / X
  1228. Value *YZ = Builder.CreateFMulFMF(Y, Op0, &I);
  1229. return BinaryOperator::CreateFDivFMF(YZ, X, &I);
  1230. }
  1231. // Z / (1.0 / Y) => (Y * Z)
  1232. //
  1233. // This is a special case of Z / (X / Y) => (Y * Z) / X, with X = 1.0. The
  1234. // m_OneUse check is avoided because even in the case of the multiple uses
  1235. // for 1.0/Y, the number of instructions remain the same and a division is
  1236. // replaced by a multiplication.
  1237. if (match(Op1, m_FDiv(m_SpecificFP(1.0), m_Value(Y))))
  1238. return BinaryOperator::CreateFMulFMF(Y, Op0, &I);
  1239. }
  1240. if (I.hasAllowReassoc() && Op0->hasOneUse() && Op1->hasOneUse()) {
  1241. // sin(X) / cos(X) -> tan(X)
  1242. // cos(X) / sin(X) -> 1/tan(X) (cotangent)
  1243. Value *X;
  1244. bool IsTan = match(Op0, m_Intrinsic<Intrinsic::sin>(m_Value(X))) &&
  1245. match(Op1, m_Intrinsic<Intrinsic::cos>(m_Specific(X)));
  1246. bool IsCot =
  1247. !IsTan && match(Op0, m_Intrinsic<Intrinsic::cos>(m_Value(X))) &&
  1248. match(Op1, m_Intrinsic<Intrinsic::sin>(m_Specific(X)));
  1249. if ((IsTan || IsCot) &&
  1250. hasFloatFn(&TLI, I.getType(), LibFunc_tan, LibFunc_tanf, LibFunc_tanl)) {
  1251. IRBuilder<> B(&I);
  1252. IRBuilder<>::FastMathFlagGuard FMFGuard(B);
  1253. B.setFastMathFlags(I.getFastMathFlags());
  1254. AttributeList Attrs =
  1255. cast<CallBase>(Op0)->getCalledFunction()->getAttributes();
  1256. Value *Res = emitUnaryFloatFnCall(X, &TLI, LibFunc_tan, LibFunc_tanf,
  1257. LibFunc_tanl, B, Attrs);
  1258. if (IsCot)
  1259. Res = B.CreateFDiv(ConstantFP::get(I.getType(), 1.0), Res);
  1260. return replaceInstUsesWith(I, Res);
  1261. }
  1262. }
  1263. // X / (X * Y) --> 1.0 / Y
  1264. // Reassociate to (X / X -> 1.0) is legal when NaNs are not allowed.
  1265. // We can ignore the possibility that X is infinity because INF/INF is NaN.
  1266. Value *X, *Y;
  1267. if (I.hasNoNaNs() && I.hasAllowReassoc() &&
  1268. match(Op1, m_c_FMul(m_Specific(Op0), m_Value(Y)))) {
  1269. replaceOperand(I, 0, ConstantFP::get(I.getType(), 1.0));
  1270. replaceOperand(I, 1, Y);
  1271. return &I;
  1272. }
  1273. // X / fabs(X) -> copysign(1.0, X)
  1274. // fabs(X) / X -> copysign(1.0, X)
  1275. if (I.hasNoNaNs() && I.hasNoInfs() &&
  1276. (match(&I, m_FDiv(m_Value(X), m_FAbs(m_Deferred(X)))) ||
  1277. match(&I, m_FDiv(m_FAbs(m_Value(X)), m_Deferred(X))))) {
  1278. Value *V = Builder.CreateBinaryIntrinsic(
  1279. Intrinsic::copysign, ConstantFP::get(I.getType(), 1.0), X, &I);
  1280. return replaceInstUsesWith(I, V);
  1281. }
  1282. if (Instruction *Mul = foldFDivPowDivisor(I, Builder))
  1283. return Mul;
  1284. return nullptr;
  1285. }
  1286. /// This function implements the transforms common to both integer remainder
  1287. /// instructions (urem and srem). It is called by the visitors to those integer
  1288. /// remainder instructions.
  1289. /// Common integer remainder transforms
  1290. Instruction *InstCombinerImpl::commonIRemTransforms(BinaryOperator &I) {
  1291. if (Instruction *Phi = foldBinopWithPhiOperands(I))
  1292. return Phi;
  1293. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1294. // The RHS is known non-zero.
  1295. if (Value *V = simplifyValueKnownNonZero(I.getOperand(1), *this, I))
  1296. return replaceOperand(I, 1, V);
  1297. // Handle cases involving: rem X, (select Cond, Y, Z)
  1298. if (simplifyDivRemOfSelectWithZeroOp(I))
  1299. return &I;
  1300. // If the divisor is a select-of-constants, try to constant fold all rem ops:
  1301. // C % (select Cond, TrueC, FalseC) --> select Cond, (C % TrueC), (C % FalseC)
  1302. // TODO: Adapt simplifyDivRemOfSelectWithZeroOp to allow this and other folds.
  1303. if (match(Op0, m_ImmConstant()) &&
  1304. match(Op1, m_Select(m_Value(), m_ImmConstant(), m_ImmConstant()))) {
  1305. if (Instruction *R = FoldOpIntoSelect(I, cast<SelectInst>(Op1)))
  1306. return R;
  1307. }
  1308. if (isa<Constant>(Op1)) {
  1309. if (Instruction *Op0I = dyn_cast<Instruction>(Op0)) {
  1310. if (SelectInst *SI = dyn_cast<SelectInst>(Op0I)) {
  1311. if (Instruction *R = FoldOpIntoSelect(I, SI))
  1312. return R;
  1313. } else if (auto *PN = dyn_cast<PHINode>(Op0I)) {
  1314. const APInt *Op1Int;
  1315. if (match(Op1, m_APInt(Op1Int)) && !Op1Int->isMinValue() &&
  1316. (I.getOpcode() == Instruction::URem ||
  1317. !Op1Int->isMinSignedValue())) {
  1318. // foldOpIntoPhi will speculate instructions to the end of the PHI's
  1319. // predecessor blocks, so do this only if we know the srem or urem
  1320. // will not fault.
  1321. if (Instruction *NV = foldOpIntoPhi(I, PN))
  1322. return NV;
  1323. }
  1324. }
  1325. // See if we can fold away this rem instruction.
  1326. if (SimplifyDemandedInstructionBits(I))
  1327. return &I;
  1328. }
  1329. }
  1330. return nullptr;
  1331. }
  1332. Instruction *InstCombinerImpl::visitURem(BinaryOperator &I) {
  1333. if (Value *V = SimplifyURemInst(I.getOperand(0), I.getOperand(1),
  1334. SQ.getWithInstruction(&I)))
  1335. return replaceInstUsesWith(I, V);
  1336. if (Instruction *X = foldVectorBinop(I))
  1337. return X;
  1338. if (Instruction *common = commonIRemTransforms(I))
  1339. return common;
  1340. if (Instruction *NarrowRem = narrowUDivURem(I, Builder))
  1341. return NarrowRem;
  1342. // X urem Y -> X and Y-1, where Y is a power of 2,
  1343. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1344. Type *Ty = I.getType();
  1345. if (isKnownToBeAPowerOfTwo(Op1, /*OrZero*/ true, 0, &I)) {
  1346. // This may increase instruction count, we don't enforce that Y is a
  1347. // constant.
  1348. Constant *N1 = Constant::getAllOnesValue(Ty);
  1349. Value *Add = Builder.CreateAdd(Op1, N1);
  1350. return BinaryOperator::CreateAnd(Op0, Add);
  1351. }
  1352. // 1 urem X -> zext(X != 1)
  1353. if (match(Op0, m_One())) {
  1354. Value *Cmp = Builder.CreateICmpNE(Op1, ConstantInt::get(Ty, 1));
  1355. return CastInst::CreateZExtOrBitCast(Cmp, Ty);
  1356. }
  1357. // X urem C -> X < C ? X : X - C, where C >= signbit.
  1358. if (match(Op1, m_Negative())) {
  1359. Value *Cmp = Builder.CreateICmpULT(Op0, Op1);
  1360. Value *Sub = Builder.CreateSub(Op0, Op1);
  1361. return SelectInst::Create(Cmp, Op0, Sub);
  1362. }
  1363. // If the divisor is a sext of a boolean, then the divisor must be max
  1364. // unsigned value (-1). Therefore, the remainder is Op0 unless Op0 is also
  1365. // max unsigned value. In that case, the remainder is 0:
  1366. // urem Op0, (sext i1 X) --> (Op0 == -1) ? 0 : Op0
  1367. Value *X;
  1368. if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
  1369. Value *Cmp = Builder.CreateICmpEQ(Op0, ConstantInt::getAllOnesValue(Ty));
  1370. return SelectInst::Create(Cmp, ConstantInt::getNullValue(Ty), Op0);
  1371. }
  1372. return nullptr;
  1373. }
  1374. Instruction *InstCombinerImpl::visitSRem(BinaryOperator &I) {
  1375. if (Value *V = SimplifySRemInst(I.getOperand(0), I.getOperand(1),
  1376. SQ.getWithInstruction(&I)))
  1377. return replaceInstUsesWith(I, V);
  1378. if (Instruction *X = foldVectorBinop(I))
  1379. return X;
  1380. // Handle the integer rem common cases
  1381. if (Instruction *Common = commonIRemTransforms(I))
  1382. return Common;
  1383. Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
  1384. {
  1385. const APInt *Y;
  1386. // X % -Y -> X % Y
  1387. if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue())
  1388. return replaceOperand(I, 1, ConstantInt::get(I.getType(), -*Y));
  1389. }
  1390. // -X srem Y --> -(X srem Y)
  1391. Value *X, *Y;
  1392. if (match(&I, m_SRem(m_OneUse(m_NSWSub(m_Zero(), m_Value(X))), m_Value(Y))))
  1393. return BinaryOperator::CreateNSWNeg(Builder.CreateSRem(X, Y));
  1394. // If the sign bits of both operands are zero (i.e. we can prove they are
  1395. // unsigned inputs), turn this into a urem.
  1396. APInt Mask(APInt::getSignMask(I.getType()->getScalarSizeInBits()));
  1397. if (MaskedValueIsZero(Op1, Mask, 0, &I) &&
  1398. MaskedValueIsZero(Op0, Mask, 0, &I)) {
  1399. // X srem Y -> X urem Y, iff X and Y don't have sign bit set
  1400. return BinaryOperator::CreateURem(Op0, Op1, I.getName());
  1401. }
  1402. // If it's a constant vector, flip any negative values positive.
  1403. if (isa<ConstantVector>(Op1) || isa<ConstantDataVector>(Op1)) {
  1404. Constant *C = cast<Constant>(Op1);
  1405. unsigned VWidth = cast<FixedVectorType>(C->getType())->getNumElements();
  1406. bool hasNegative = false;
  1407. bool hasMissing = false;
  1408. for (unsigned i = 0; i != VWidth; ++i) {
  1409. Constant *Elt = C->getAggregateElement(i);
  1410. if (!Elt) {
  1411. hasMissing = true;
  1412. break;
  1413. }
  1414. if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elt))
  1415. if (RHS->isNegative())
  1416. hasNegative = true;
  1417. }
  1418. if (hasNegative && !hasMissing) {
  1419. SmallVector<Constant *, 16> Elts(VWidth);
  1420. for (unsigned i = 0; i != VWidth; ++i) {
  1421. Elts[i] = C->getAggregateElement(i); // Handle undef, etc.
  1422. if (ConstantInt *RHS = dyn_cast<ConstantInt>(Elts[i])) {
  1423. if (RHS->isNegative())
  1424. Elts[i] = cast<ConstantInt>(ConstantExpr::getNeg(RHS));
  1425. }
  1426. }
  1427. Constant *NewRHSV = ConstantVector::get(Elts);
  1428. if (NewRHSV != C) // Don't loop on -MININT
  1429. return replaceOperand(I, 1, NewRHSV);
  1430. }
  1431. }
  1432. return nullptr;
  1433. }
  1434. Instruction *InstCombinerImpl::visitFRem(BinaryOperator &I) {
  1435. if (Value *V = SimplifyFRemInst(I.getOperand(0), I.getOperand(1),
  1436. I.getFastMathFlags(),
  1437. SQ.getWithInstruction(&I)))
  1438. return replaceInstUsesWith(I, V);
  1439. if (Instruction *X = foldVectorBinop(I))
  1440. return X;
  1441. if (Instruction *Phi = foldBinopWithPhiOperands(I))
  1442. return Phi;
  1443. return nullptr;
  1444. }