GISelKnownBits.cpp 27 KB

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