GISelKnownBits.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. //===- lib/CodeGen/GlobalISel/GISelKnownBits.cpp --------------*- C++ *-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// Provides analysis for querying information about KnownBits during GISel
  10. /// passes.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/GlobalISel/GISelKnownBits.h"
  14. #include "llvm/Analysis/ValueTracking.h"
  15. #include "llvm/CodeGen/GlobalISel/Utils.h"
  16. #include "llvm/CodeGen/MachineFrameInfo.h"
  17. #include "llvm/CodeGen/MachineRegisterInfo.h"
  18. #include "llvm/CodeGen/TargetLowering.h"
  19. #include "llvm/CodeGen/TargetOpcodes.h"
  20. #include "llvm/IR/Module.h"
  21. #define DEBUG_TYPE "gisel-known-bits"
  22. using namespace llvm;
  23. char llvm::GISelKnownBitsAnalysis::ID = 0;
  24. INITIALIZE_PASS(GISelKnownBitsAnalysis, DEBUG_TYPE,
  25. "Analysis for ComputingKnownBits", false, true)
  26. GISelKnownBits::GISelKnownBits(MachineFunction &MF, unsigned MaxDepth)
  27. : MF(MF), MRI(MF.getRegInfo()), TL(*MF.getSubtarget().getTargetLowering()),
  28. DL(MF.getFunction().getParent()->getDataLayout()), MaxDepth(MaxDepth) {}
  29. Align GISelKnownBits::computeKnownAlignment(Register R, unsigned Depth) {
  30. const MachineInstr *MI = MRI.getVRegDef(R);
  31. switch (MI->getOpcode()) {
  32. case TargetOpcode::COPY:
  33. return computeKnownAlignment(MI->getOperand(1).getReg(), Depth);
  34. case TargetOpcode::G_ASSERT_ALIGN: {
  35. // TODO: Min with source
  36. return Align(MI->getOperand(2).getImm());
  37. }
  38. case TargetOpcode::G_FRAME_INDEX: {
  39. int FrameIdx = MI->getOperand(1).getIndex();
  40. return MF.getFrameInfo().getObjectAlign(FrameIdx);
  41. }
  42. case TargetOpcode::G_INTRINSIC:
  43. case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
  44. default:
  45. return TL.computeKnownAlignForTargetInstr(*this, R, MRI, Depth + 1);
  46. }
  47. }
  48. KnownBits GISelKnownBits::getKnownBits(MachineInstr &MI) {
  49. assert(MI.getNumExplicitDefs() == 1 &&
  50. "expected single return generic instruction");
  51. return getKnownBits(MI.getOperand(0).getReg());
  52. }
  53. KnownBits GISelKnownBits::getKnownBits(Register R) {
  54. const LLT Ty = MRI.getType(R);
  55. APInt DemandedElts =
  56. Ty.isVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
  57. return getKnownBits(R, DemandedElts);
  58. }
  59. KnownBits GISelKnownBits::getKnownBits(Register R, const APInt &DemandedElts,
  60. unsigned Depth) {
  61. // For now, we only maintain the cache during one request.
  62. assert(ComputeKnownBitsCache.empty() && "Cache should have been cleared");
  63. KnownBits Known;
  64. computeKnownBitsImpl(R, Known, DemandedElts);
  65. ComputeKnownBitsCache.clear();
  66. return Known;
  67. }
  68. bool GISelKnownBits::signBitIsZero(Register R) {
  69. LLT Ty = MRI.getType(R);
  70. unsigned BitWidth = Ty.getScalarSizeInBits();
  71. return maskedValueIsZero(R, APInt::getSignMask(BitWidth));
  72. }
  73. APInt GISelKnownBits::getKnownZeroes(Register R) {
  74. return getKnownBits(R).Zero;
  75. }
  76. APInt GISelKnownBits::getKnownOnes(Register R) { return getKnownBits(R).One; }
  77. LLVM_ATTRIBUTE_UNUSED static void
  78. dumpResult(const MachineInstr &MI, const KnownBits &Known, unsigned Depth) {
  79. dbgs() << "[" << Depth << "] Compute known bits: " << MI << "[" << Depth
  80. << "] Computed for: " << MI << "[" << Depth << "] Known: 0x"
  81. << toString(Known.Zero | Known.One, 16, false) << "\n"
  82. << "[" << Depth << "] Zero: 0x" << toString(Known.Zero, 16, false)
  83. << "\n"
  84. << "[" << Depth << "] One: 0x" << toString(Known.One, 16, false)
  85. << "\n";
  86. }
  87. /// Compute known bits for the intersection of \p Src0 and \p Src1
  88. void GISelKnownBits::computeKnownBitsMin(Register Src0, Register Src1,
  89. KnownBits &Known,
  90. const APInt &DemandedElts,
  91. unsigned Depth) {
  92. // Test src1 first, since we canonicalize simpler expressions to the RHS.
  93. computeKnownBitsImpl(Src1, Known, DemandedElts, Depth);
  94. // If we don't know any bits, early out.
  95. if (Known.isUnknown())
  96. return;
  97. KnownBits Known2;
  98. computeKnownBitsImpl(Src0, Known2, DemandedElts, Depth);
  99. // Only known if known in both the LHS and RHS.
  100. Known = KnownBits::commonBits(Known, Known2);
  101. }
  102. // Bitfield extract is computed as (Src >> Offset) & Mask, where Mask is
  103. // created using Width. Use this function when the inputs are KnownBits
  104. // objects. TODO: Move this KnownBits.h if this is usable in more cases.
  105. static KnownBits extractBits(unsigned BitWidth, const KnownBits &SrcOpKnown,
  106. const KnownBits &OffsetKnown,
  107. const KnownBits &WidthKnown) {
  108. KnownBits Mask(BitWidth);
  109. Mask.Zero = APInt::getBitsSetFrom(
  110. BitWidth, WidthKnown.getMaxValue().getLimitedValue(BitWidth));
  111. Mask.One = APInt::getLowBitsSet(
  112. BitWidth, WidthKnown.getMinValue().getLimitedValue(BitWidth));
  113. return KnownBits::lshr(SrcOpKnown, OffsetKnown) & Mask;
  114. }
  115. void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
  116. const APInt &DemandedElts,
  117. unsigned Depth) {
  118. MachineInstr &MI = *MRI.getVRegDef(R);
  119. unsigned Opcode = MI.getOpcode();
  120. LLT DstTy = MRI.getType(R);
  121. // Handle the case where this is called on a register that does not have a
  122. // type constraint (i.e. it has a register class constraint instead). This is
  123. // unlikely to occur except by looking through copies but it is possible for
  124. // the initial register being queried to be in this state.
  125. if (!DstTy.isValid()) {
  126. Known = KnownBits();
  127. return;
  128. }
  129. unsigned BitWidth = DstTy.getScalarSizeInBits();
  130. auto CacheEntry = ComputeKnownBitsCache.find(R);
  131. if (CacheEntry != ComputeKnownBitsCache.end()) {
  132. Known = CacheEntry->second;
  133. LLVM_DEBUG(dbgs() << "Cache hit at ");
  134. LLVM_DEBUG(dumpResult(MI, Known, Depth));
  135. assert(Known.getBitWidth() == BitWidth && "Cache entry size doesn't match");
  136. return;
  137. }
  138. Known = KnownBits(BitWidth); // Don't know anything
  139. // Depth may get bigger than max depth if it gets passed to a different
  140. // GISelKnownBits object.
  141. // This may happen when say a generic part uses a GISelKnownBits object
  142. // with some max depth, but then we hit TL.computeKnownBitsForTargetInstr
  143. // which creates a new GISelKnownBits object with a different and smaller
  144. // depth. If we just check for equality, we would never exit if the depth
  145. // that is passed down to the target specific GISelKnownBits object is
  146. // already bigger than its max depth.
  147. if (Depth >= getMaxDepth())
  148. return;
  149. if (!DemandedElts)
  150. return; // No demanded elts, better to assume we don't know anything.
  151. KnownBits Known2;
  152. switch (Opcode) {
  153. default:
  154. TL.computeKnownBitsForTargetInstr(*this, R, Known, DemandedElts, MRI,
  155. Depth);
  156. break;
  157. case TargetOpcode::G_BUILD_VECTOR: {
  158. // Collect the known bits that are shared by every demanded vector element.
  159. Known.Zero.setAllBits(); Known.One.setAllBits();
  160. for (unsigned i = 0, e = MI.getNumOperands() - 1; i < e; ++i) {
  161. if (!DemandedElts[i])
  162. continue;
  163. computeKnownBitsImpl(MI.getOperand(i + 1).getReg(), Known2, DemandedElts,
  164. Depth + 1);
  165. // Known bits are the values that are shared by every demanded element.
  166. Known = KnownBits::commonBits(Known, Known2);
  167. // If we don't know any bits, early out.
  168. if (Known.isUnknown())
  169. break;
  170. }
  171. break;
  172. }
  173. case TargetOpcode::COPY:
  174. case TargetOpcode::G_PHI:
  175. case TargetOpcode::PHI: {
  176. Known.One = APInt::getAllOnes(BitWidth);
  177. Known.Zero = APInt::getAllOnes(BitWidth);
  178. // Destination registers should not have subregisters at this
  179. // point of the pipeline, otherwise the main live-range will be
  180. // defined more than once, which is against SSA.
  181. assert(MI.getOperand(0).getSubReg() == 0 && "Is this code in SSA?");
  182. // Record in the cache that we know nothing for MI.
  183. // This will get updated later and in the meantime, if we reach that
  184. // phi again, because of a loop, we will cut the search thanks to this
  185. // cache entry.
  186. // We could actually build up more information on the phi by not cutting
  187. // the search, but that additional information is more a side effect
  188. // than an intended choice.
  189. // Therefore, for now, save on compile time until we derive a proper way
  190. // to derive known bits for PHIs within loops.
  191. ComputeKnownBitsCache[R] = KnownBits(BitWidth);
  192. // PHI's operand are a mix of registers and basic blocks interleaved.
  193. // We only care about the register ones.
  194. for (unsigned Idx = 1; Idx < MI.getNumOperands(); Idx += 2) {
  195. const MachineOperand &Src = MI.getOperand(Idx);
  196. Register SrcReg = Src.getReg();
  197. // Look through trivial copies and phis but don't look through trivial
  198. // copies or phis of the form `%1:(s32) = OP %0:gpr32`, known-bits
  199. // analysis is currently unable to determine the bit width of a
  200. // register class.
  201. //
  202. // We can't use NoSubRegister by name as it's defined by each target but
  203. // it's always defined to be 0 by tablegen.
  204. if (SrcReg.isVirtual() && Src.getSubReg() == 0 /*NoSubRegister*/ &&
  205. MRI.getType(SrcReg).isValid()) {
  206. // For COPYs we don't do anything, don't increase the depth.
  207. computeKnownBitsImpl(SrcReg, Known2, DemandedElts,
  208. Depth + (Opcode != TargetOpcode::COPY));
  209. Known = KnownBits::commonBits(Known, Known2);
  210. // If we reach a point where we don't know anything
  211. // just stop looking through the operands.
  212. if (Known.One == 0 && Known.Zero == 0)
  213. break;
  214. } else {
  215. // We know nothing.
  216. Known = KnownBits(BitWidth);
  217. break;
  218. }
  219. }
  220. break;
  221. }
  222. case TargetOpcode::G_CONSTANT: {
  223. auto CstVal = getIConstantVRegVal(R, MRI);
  224. if (!CstVal)
  225. break;
  226. Known = KnownBits::makeConstant(*CstVal);
  227. break;
  228. }
  229. case TargetOpcode::G_FRAME_INDEX: {
  230. int FrameIdx = MI.getOperand(1).getIndex();
  231. TL.computeKnownBitsForFrameIndex(FrameIdx, Known, MF);
  232. break;
  233. }
  234. case TargetOpcode::G_SUB: {
  235. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
  236. Depth + 1);
  237. computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
  238. Depth + 1);
  239. Known = KnownBits::computeForAddSub(/*Add*/ false, /*NSW*/ false, Known,
  240. Known2);
  241. break;
  242. }
  243. case TargetOpcode::G_XOR: {
  244. computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
  245. Depth + 1);
  246. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
  247. Depth + 1);
  248. Known ^= Known2;
  249. break;
  250. }
  251. case TargetOpcode::G_PTR_ADD: {
  252. if (DstTy.isVector())
  253. break;
  254. // G_PTR_ADD is like G_ADD. FIXME: Is this true for all targets?
  255. LLT Ty = MRI.getType(MI.getOperand(1).getReg());
  256. if (DL.isNonIntegralAddressSpace(Ty.getAddressSpace()))
  257. break;
  258. [[fallthrough]];
  259. }
  260. case TargetOpcode::G_ADD: {
  261. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
  262. Depth + 1);
  263. computeKnownBitsImpl(MI.getOperand(2).getReg(), Known2, DemandedElts,
  264. Depth + 1);
  265. Known =
  266. KnownBits::computeForAddSub(/*Add*/ true, /*NSW*/ false, Known, Known2);
  267. break;
  268. }
  269. case TargetOpcode::G_AND: {
  270. // If either the LHS or the RHS are Zero, the result is zero.
  271. computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
  272. Depth + 1);
  273. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
  274. Depth + 1);
  275. Known &= Known2;
  276. break;
  277. }
  278. case TargetOpcode::G_OR: {
  279. // If either the LHS or the RHS are Zero, the result is zero.
  280. computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
  281. Depth + 1);
  282. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
  283. Depth + 1);
  284. Known |= Known2;
  285. break;
  286. }
  287. case TargetOpcode::G_MUL: {
  288. computeKnownBitsImpl(MI.getOperand(2).getReg(), Known, DemandedElts,
  289. Depth + 1);
  290. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
  291. Depth + 1);
  292. Known = KnownBits::mul(Known, Known2);
  293. break;
  294. }
  295. case TargetOpcode::G_SELECT: {
  296. computeKnownBitsMin(MI.getOperand(2).getReg(), MI.getOperand(3).getReg(),
  297. Known, DemandedElts, Depth + 1);
  298. break;
  299. }
  300. case TargetOpcode::G_SMIN: {
  301. // TODO: Handle clamp pattern with number of sign bits
  302. KnownBits KnownRHS;
  303. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
  304. Depth + 1);
  305. computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
  306. Depth + 1);
  307. Known = KnownBits::smin(Known, KnownRHS);
  308. break;
  309. }
  310. case TargetOpcode::G_SMAX: {
  311. // TODO: Handle clamp pattern with number of sign bits
  312. KnownBits KnownRHS;
  313. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
  314. Depth + 1);
  315. computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS, DemandedElts,
  316. Depth + 1);
  317. Known = KnownBits::smax(Known, KnownRHS);
  318. break;
  319. }
  320. case TargetOpcode::G_UMIN: {
  321. KnownBits KnownRHS;
  322. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known,
  323. DemandedElts, Depth + 1);
  324. computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS,
  325. DemandedElts, Depth + 1);
  326. Known = KnownBits::umin(Known, KnownRHS);
  327. break;
  328. }
  329. case TargetOpcode::G_UMAX: {
  330. KnownBits KnownRHS;
  331. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known,
  332. DemandedElts, Depth + 1);
  333. computeKnownBitsImpl(MI.getOperand(2).getReg(), KnownRHS,
  334. DemandedElts, Depth + 1);
  335. Known = KnownBits::umax(Known, KnownRHS);
  336. break;
  337. }
  338. case TargetOpcode::G_FCMP:
  339. case TargetOpcode::G_ICMP: {
  340. if (DstTy.isVector())
  341. break;
  342. if (TL.getBooleanContents(DstTy.isVector(),
  343. Opcode == TargetOpcode::G_FCMP) ==
  344. TargetLowering::ZeroOrOneBooleanContent &&
  345. BitWidth > 1)
  346. Known.Zero.setBitsFrom(1);
  347. break;
  348. }
  349. case TargetOpcode::G_SEXT: {
  350. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
  351. Depth + 1);
  352. // If the sign bit is known to be zero or one, then sext will extend
  353. // it to the top bits, else it will just zext.
  354. Known = Known.sext(BitWidth);
  355. break;
  356. }
  357. case TargetOpcode::G_ASSERT_SEXT:
  358. case TargetOpcode::G_SEXT_INREG: {
  359. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
  360. Depth + 1);
  361. Known = Known.sextInReg(MI.getOperand(2).getImm());
  362. break;
  363. }
  364. case TargetOpcode::G_ANYEXT: {
  365. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known, DemandedElts,
  366. Depth + 1);
  367. Known = Known.anyext(BitWidth);
  368. break;
  369. }
  370. case TargetOpcode::G_LOAD: {
  371. const MachineMemOperand *MMO = *MI.memoperands_begin();
  372. if (const MDNode *Ranges = MMO->getRanges()) {
  373. computeKnownBitsFromRangeMetadata(*Ranges, Known);
  374. }
  375. break;
  376. }
  377. case TargetOpcode::G_ZEXTLOAD: {
  378. if (DstTy.isVector())
  379. break;
  380. // Everything above the retrieved bits is zero
  381. Known.Zero.setBitsFrom((*MI.memoperands_begin())->getSizeInBits());
  382. break;
  383. }
  384. case TargetOpcode::G_ASHR: {
  385. KnownBits LHSKnown, RHSKnown;
  386. computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
  387. Depth + 1);
  388. computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
  389. Depth + 1);
  390. Known = KnownBits::ashr(LHSKnown, RHSKnown);
  391. break;
  392. }
  393. case TargetOpcode::G_LSHR: {
  394. KnownBits LHSKnown, RHSKnown;
  395. computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
  396. Depth + 1);
  397. computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
  398. Depth + 1);
  399. Known = KnownBits::lshr(LHSKnown, RHSKnown);
  400. break;
  401. }
  402. case TargetOpcode::G_SHL: {
  403. KnownBits LHSKnown, RHSKnown;
  404. computeKnownBitsImpl(MI.getOperand(1).getReg(), LHSKnown, DemandedElts,
  405. Depth + 1);
  406. computeKnownBitsImpl(MI.getOperand(2).getReg(), RHSKnown, DemandedElts,
  407. Depth + 1);
  408. Known = KnownBits::shl(LHSKnown, RHSKnown);
  409. break;
  410. }
  411. case TargetOpcode::G_INTTOPTR:
  412. case TargetOpcode::G_PTRTOINT:
  413. if (DstTy.isVector())
  414. break;
  415. // Fall through and handle them the same as zext/trunc.
  416. [[fallthrough]];
  417. case TargetOpcode::G_ASSERT_ZEXT:
  418. case TargetOpcode::G_ZEXT:
  419. case TargetOpcode::G_TRUNC: {
  420. Register SrcReg = MI.getOperand(1).getReg();
  421. LLT SrcTy = MRI.getType(SrcReg);
  422. unsigned SrcBitWidth;
  423. // G_ASSERT_ZEXT stores the original bitwidth in the immediate operand.
  424. if (Opcode == TargetOpcode::G_ASSERT_ZEXT)
  425. SrcBitWidth = MI.getOperand(2).getImm();
  426. else {
  427. SrcBitWidth = SrcTy.isPointer()
  428. ? DL.getIndexSizeInBits(SrcTy.getAddressSpace())
  429. : SrcTy.getSizeInBits();
  430. }
  431. assert(SrcBitWidth && "SrcBitWidth can't be zero");
  432. Known = Known.zextOrTrunc(SrcBitWidth);
  433. computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
  434. Known = Known.zextOrTrunc(BitWidth);
  435. if (BitWidth > SrcBitWidth)
  436. Known.Zero.setBitsFrom(SrcBitWidth);
  437. break;
  438. }
  439. case TargetOpcode::G_ASSERT_ALIGN: {
  440. int64_t LogOfAlign = Log2_64(MI.getOperand(2).getImm());
  441. // TODO: Should use maximum with source
  442. // If a node is guaranteed to be aligned, set low zero bits accordingly as
  443. // well as clearing one bits.
  444. Known.Zero.setLowBits(LogOfAlign);
  445. Known.One.clearLowBits(LogOfAlign);
  446. break;
  447. }
  448. case TargetOpcode::G_MERGE_VALUES: {
  449. unsigned NumOps = MI.getNumOperands();
  450. unsigned OpSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
  451. for (unsigned I = 0; I != NumOps - 1; ++I) {
  452. KnownBits SrcOpKnown;
  453. computeKnownBitsImpl(MI.getOperand(I + 1).getReg(), SrcOpKnown,
  454. DemandedElts, Depth + 1);
  455. Known.insertBits(SrcOpKnown, I * OpSize);
  456. }
  457. break;
  458. }
  459. case TargetOpcode::G_UNMERGE_VALUES: {
  460. if (DstTy.isVector())
  461. break;
  462. unsigned NumOps = MI.getNumOperands();
  463. Register SrcReg = MI.getOperand(NumOps - 1).getReg();
  464. if (MRI.getType(SrcReg).isVector())
  465. return; // TODO: Handle vectors.
  466. KnownBits SrcOpKnown;
  467. computeKnownBitsImpl(SrcReg, SrcOpKnown, DemandedElts, Depth + 1);
  468. // Figure out the result operand index
  469. unsigned DstIdx = 0;
  470. for (; DstIdx != NumOps - 1 && MI.getOperand(DstIdx).getReg() != R;
  471. ++DstIdx)
  472. ;
  473. Known = SrcOpKnown.extractBits(BitWidth, BitWidth * DstIdx);
  474. break;
  475. }
  476. case TargetOpcode::G_BSWAP: {
  477. Register SrcReg = MI.getOperand(1).getReg();
  478. computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
  479. Known = Known.byteSwap();
  480. break;
  481. }
  482. case TargetOpcode::G_BITREVERSE: {
  483. Register SrcReg = MI.getOperand(1).getReg();
  484. computeKnownBitsImpl(SrcReg, Known, DemandedElts, Depth + 1);
  485. Known = Known.reverseBits();
  486. break;
  487. }
  488. case TargetOpcode::G_CTPOP: {
  489. computeKnownBitsImpl(MI.getOperand(1).getReg(), Known2, DemandedElts,
  490. Depth + 1);
  491. // We can bound the space the count needs. Also, bits known to be zero can't
  492. // contribute to the population.
  493. unsigned BitsPossiblySet = Known2.countMaxPopulation();
  494. unsigned LowBits = llvm::bit_width(BitsPossiblySet);
  495. Known.Zero.setBitsFrom(LowBits);
  496. // TODO: we could bound Known.One using the lower bound on the number of
  497. // bits which might be set provided by popcnt KnownOne2.
  498. break;
  499. }
  500. case TargetOpcode::G_UBFX: {
  501. KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
  502. computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
  503. Depth + 1);
  504. computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
  505. Depth + 1);
  506. computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
  507. Depth + 1);
  508. Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
  509. break;
  510. }
  511. case TargetOpcode::G_SBFX: {
  512. KnownBits SrcOpKnown, OffsetKnown, WidthKnown;
  513. computeKnownBitsImpl(MI.getOperand(1).getReg(), SrcOpKnown, DemandedElts,
  514. Depth + 1);
  515. computeKnownBitsImpl(MI.getOperand(2).getReg(), OffsetKnown, DemandedElts,
  516. Depth + 1);
  517. computeKnownBitsImpl(MI.getOperand(3).getReg(), WidthKnown, DemandedElts,
  518. Depth + 1);
  519. Known = extractBits(BitWidth, SrcOpKnown, OffsetKnown, WidthKnown);
  520. // Sign extend the extracted value using shift left and arithmetic shift
  521. // right.
  522. KnownBits ExtKnown = KnownBits::makeConstant(APInt(BitWidth, BitWidth));
  523. KnownBits ShiftKnown = KnownBits::computeForAddSub(
  524. /*Add*/ false, /*NSW*/ false, ExtKnown, WidthKnown);
  525. Known = KnownBits::ashr(KnownBits::shl(Known, ShiftKnown), ShiftKnown);
  526. break;
  527. }
  528. case TargetOpcode::G_UADDO:
  529. case TargetOpcode::G_UADDE:
  530. case TargetOpcode::G_SADDO:
  531. case TargetOpcode::G_SADDE:
  532. case TargetOpcode::G_USUBO:
  533. case TargetOpcode::G_USUBE:
  534. case TargetOpcode::G_SSUBO:
  535. case TargetOpcode::G_SSUBE:
  536. case TargetOpcode::G_UMULO:
  537. case TargetOpcode::G_SMULO: {
  538. if (MI.getOperand(1).getReg() == R) {
  539. // If we know the result of a compare has the top bits zero, use this
  540. // info.
  541. if (TL.getBooleanContents(DstTy.isVector(), false) ==
  542. TargetLowering::ZeroOrOneBooleanContent &&
  543. BitWidth > 1)
  544. Known.Zero.setBitsFrom(1);
  545. }
  546. break;
  547. }
  548. }
  549. assert(!Known.hasConflict() && "Bits known to be one AND zero?");
  550. LLVM_DEBUG(dumpResult(MI, Known, Depth));
  551. // Update the cache.
  552. ComputeKnownBitsCache[R] = Known;
  553. }
  554. /// Compute number of sign bits for the intersection of \p Src0 and \p Src1
  555. unsigned GISelKnownBits::computeNumSignBitsMin(Register Src0, Register Src1,
  556. const APInt &DemandedElts,
  557. unsigned Depth) {
  558. // Test src1 first, since we canonicalize simpler expressions to the RHS.
  559. unsigned Src1SignBits = computeNumSignBits(Src1, DemandedElts, Depth);
  560. if (Src1SignBits == 1)
  561. return 1;
  562. return std::min(computeNumSignBits(Src0, DemandedElts, Depth), Src1SignBits);
  563. }
  564. unsigned GISelKnownBits::computeNumSignBits(Register R,
  565. const APInt &DemandedElts,
  566. unsigned Depth) {
  567. MachineInstr &MI = *MRI.getVRegDef(R);
  568. unsigned Opcode = MI.getOpcode();
  569. if (Opcode == TargetOpcode::G_CONSTANT)
  570. return MI.getOperand(1).getCImm()->getValue().getNumSignBits();
  571. if (Depth == getMaxDepth())
  572. return 1;
  573. if (!DemandedElts)
  574. return 1; // No demanded elts, better to assume we don't know anything.
  575. LLT DstTy = MRI.getType(R);
  576. const unsigned TyBits = DstTy.getScalarSizeInBits();
  577. // Handle the case where this is called on a register that does not have a
  578. // type constraint. This is unlikely to occur except by looking through copies
  579. // but it is possible for the initial register being queried to be in this
  580. // state.
  581. if (!DstTy.isValid())
  582. return 1;
  583. unsigned FirstAnswer = 1;
  584. switch (Opcode) {
  585. case TargetOpcode::COPY: {
  586. MachineOperand &Src = MI.getOperand(1);
  587. if (Src.getReg().isVirtual() && Src.getSubReg() == 0 &&
  588. MRI.getType(Src.getReg()).isValid()) {
  589. // Don't increment Depth for this one since we didn't do any work.
  590. return computeNumSignBits(Src.getReg(), DemandedElts, Depth);
  591. }
  592. return 1;
  593. }
  594. case TargetOpcode::G_SEXT: {
  595. Register Src = MI.getOperand(1).getReg();
  596. LLT SrcTy = MRI.getType(Src);
  597. unsigned Tmp = DstTy.getScalarSizeInBits() - SrcTy.getScalarSizeInBits();
  598. return computeNumSignBits(Src, DemandedElts, Depth + 1) + Tmp;
  599. }
  600. case TargetOpcode::G_ASSERT_SEXT:
  601. case TargetOpcode::G_SEXT_INREG: {
  602. // Max of the input and what this extends.
  603. Register Src = MI.getOperand(1).getReg();
  604. unsigned SrcBits = MI.getOperand(2).getImm();
  605. unsigned InRegBits = TyBits - SrcBits + 1;
  606. return std::max(computeNumSignBits(Src, DemandedElts, Depth + 1), InRegBits);
  607. }
  608. case TargetOpcode::G_SEXTLOAD: {
  609. // FIXME: We need an in-memory type representation.
  610. if (DstTy.isVector())
  611. return 1;
  612. // e.g. i16->i32 = '17' bits known.
  613. const MachineMemOperand *MMO = *MI.memoperands_begin();
  614. return TyBits - MMO->getSizeInBits() + 1;
  615. }
  616. case TargetOpcode::G_ZEXTLOAD: {
  617. // FIXME: We need an in-memory type representation.
  618. if (DstTy.isVector())
  619. return 1;
  620. // e.g. i16->i32 = '16' bits known.
  621. const MachineMemOperand *MMO = *MI.memoperands_begin();
  622. return TyBits - MMO->getSizeInBits();
  623. }
  624. case TargetOpcode::G_TRUNC: {
  625. Register Src = MI.getOperand(1).getReg();
  626. LLT SrcTy = MRI.getType(Src);
  627. // Check if the sign bits of source go down as far as the truncated value.
  628. unsigned DstTyBits = DstTy.getScalarSizeInBits();
  629. unsigned NumSrcBits = SrcTy.getScalarSizeInBits();
  630. unsigned NumSrcSignBits = computeNumSignBits(Src, DemandedElts, Depth + 1);
  631. if (NumSrcSignBits > (NumSrcBits - DstTyBits))
  632. return NumSrcSignBits - (NumSrcBits - DstTyBits);
  633. break;
  634. }
  635. case TargetOpcode::G_SELECT: {
  636. return computeNumSignBitsMin(MI.getOperand(2).getReg(),
  637. MI.getOperand(3).getReg(), DemandedElts,
  638. Depth + 1);
  639. }
  640. case TargetOpcode::G_SADDO:
  641. case TargetOpcode::G_SADDE:
  642. case TargetOpcode::G_UADDO:
  643. case TargetOpcode::G_UADDE:
  644. case TargetOpcode::G_SSUBO:
  645. case TargetOpcode::G_SSUBE:
  646. case TargetOpcode::G_USUBO:
  647. case TargetOpcode::G_USUBE:
  648. case TargetOpcode::G_SMULO:
  649. case TargetOpcode::G_UMULO: {
  650. // If compares returns 0/-1, all bits are sign bits.
  651. // We know that we have an integer-based boolean since these operations
  652. // are only available for integer.
  653. if (MI.getOperand(1).getReg() == R) {
  654. if (TL.getBooleanContents(DstTy.isVector(), false) ==
  655. TargetLowering::ZeroOrNegativeOneBooleanContent)
  656. return TyBits;
  657. }
  658. break;
  659. }
  660. case TargetOpcode::G_FCMP:
  661. case TargetOpcode::G_ICMP: {
  662. bool IsFP = Opcode == TargetOpcode::G_FCMP;
  663. if (TyBits == 1)
  664. break;
  665. auto BC = TL.getBooleanContents(DstTy.isVector(), IsFP);
  666. if (BC == TargetLoweringBase::ZeroOrNegativeOneBooleanContent)
  667. return TyBits; // All bits are sign bits.
  668. if (BC == TargetLowering::ZeroOrOneBooleanContent)
  669. return TyBits - 1; // Every always-zero bit is a sign bit.
  670. break;
  671. }
  672. case TargetOpcode::G_INTRINSIC:
  673. case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
  674. default: {
  675. unsigned NumBits =
  676. TL.computeNumSignBitsForTargetInstr(*this, R, DemandedElts, MRI, Depth);
  677. if (NumBits > 1)
  678. FirstAnswer = std::max(FirstAnswer, NumBits);
  679. break;
  680. }
  681. }
  682. // Finally, if we can prove that the top bits of the result are 0's or 1's,
  683. // use this information.
  684. KnownBits Known = getKnownBits(R, DemandedElts, Depth);
  685. APInt Mask;
  686. if (Known.isNonNegative()) { // sign bit is 0
  687. Mask = Known.Zero;
  688. } else if (Known.isNegative()) { // sign bit is 1;
  689. Mask = Known.One;
  690. } else {
  691. // Nothing known.
  692. return FirstAnswer;
  693. }
  694. // Okay, we know that the sign bit in Mask is set. Use CLO to determine
  695. // the number of identical bits in the top of the input value.
  696. Mask <<= Mask.getBitWidth() - TyBits;
  697. return std::max(FirstAnswer, Mask.countLeadingOnes());
  698. }
  699. unsigned GISelKnownBits::computeNumSignBits(Register R, unsigned Depth) {
  700. LLT Ty = MRI.getType(R);
  701. APInt DemandedElts =
  702. Ty.isVector() ? APInt::getAllOnes(Ty.getNumElements()) : APInt(1, 1);
  703. return computeNumSignBits(R, DemandedElts, Depth);
  704. }
  705. void GISelKnownBitsAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
  706. AU.setPreservesAll();
  707. MachineFunctionPass::getAnalysisUsage(AU);
  708. }
  709. bool GISelKnownBitsAnalysis::runOnMachineFunction(MachineFunction &MF) {
  710. return false;
  711. }