DemandedBits.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. //===- DemandedBits.cpp - Determine demanded bits -------------------------===//
  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 pass implements a demanded bits analysis. A demanded bit is one that
  10. // contributes to a result; bits that are not demanded can be either zero or
  11. // one without affecting control or data flow. For example in this sequence:
  12. //
  13. // %1 = add i32 %x, %y
  14. // %2 = trunc i32 %1 to i16
  15. //
  16. // Only the lowest 16 bits of %1 are demanded; the rest are removed by the
  17. // trunc.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #include "llvm/Analysis/DemandedBits.h"
  21. #include "llvm/ADT/APInt.h"
  22. #include "llvm/ADT/SetVector.h"
  23. #include "llvm/ADT/StringExtras.h"
  24. #include "llvm/Analysis/AssumptionCache.h"
  25. #include "llvm/Analysis/ValueTracking.h"
  26. #include "llvm/IR/BasicBlock.h"
  27. #include "llvm/IR/Constants.h"
  28. #include "llvm/IR/DataLayout.h"
  29. #include "llvm/IR/DerivedTypes.h"
  30. #include "llvm/IR/Dominators.h"
  31. #include "llvm/IR/InstIterator.h"
  32. #include "llvm/IR/InstrTypes.h"
  33. #include "llvm/IR/Instruction.h"
  34. #include "llvm/IR/IntrinsicInst.h"
  35. #include "llvm/IR/Intrinsics.h"
  36. #include "llvm/IR/Module.h"
  37. #include "llvm/IR/Operator.h"
  38. #include "llvm/IR/PassManager.h"
  39. #include "llvm/IR/PatternMatch.h"
  40. #include "llvm/IR/Type.h"
  41. #include "llvm/IR/Use.h"
  42. #include "llvm/InitializePasses.h"
  43. #include "llvm/Pass.h"
  44. #include "llvm/Support/Casting.h"
  45. #include "llvm/Support/Debug.h"
  46. #include "llvm/Support/KnownBits.h"
  47. #include "llvm/Support/raw_ostream.h"
  48. #include <algorithm>
  49. #include <cstdint>
  50. using namespace llvm;
  51. using namespace llvm::PatternMatch;
  52. #define DEBUG_TYPE "demanded-bits"
  53. char DemandedBitsWrapperPass::ID = 0;
  54. INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits",
  55. "Demanded bits analysis", false, false)
  56. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  57. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  58. INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits",
  59. "Demanded bits analysis", false, false)
  60. DemandedBitsWrapperPass::DemandedBitsWrapperPass() : FunctionPass(ID) {
  61. initializeDemandedBitsWrapperPassPass(*PassRegistry::getPassRegistry());
  62. }
  63. void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
  64. AU.setPreservesCFG();
  65. AU.addRequired<AssumptionCacheTracker>();
  66. AU.addRequired<DominatorTreeWrapperPass>();
  67. AU.setPreservesAll();
  68. }
  69. void DemandedBitsWrapperPass::print(raw_ostream &OS, const Module *M) const {
  70. DB->print(OS);
  71. }
  72. static bool isAlwaysLive(Instruction *I) {
  73. return I->isTerminator() || isa<DbgInfoIntrinsic>(I) || I->isEHPad() ||
  74. I->mayHaveSideEffects();
  75. }
  76. void DemandedBits::determineLiveOperandBits(
  77. const Instruction *UserI, const Value *Val, unsigned OperandNo,
  78. const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2,
  79. bool &KnownBitsComputed) {
  80. unsigned BitWidth = AB.getBitWidth();
  81. // We're called once per operand, but for some instructions, we need to
  82. // compute known bits of both operands in order to determine the live bits of
  83. // either (when both operands are instructions themselves). We don't,
  84. // however, want to do this twice, so we cache the result in APInts that live
  85. // in the caller. For the two-relevant-operands case, both operand values are
  86. // provided here.
  87. auto ComputeKnownBits =
  88. [&](unsigned BitWidth, const Value *V1, const Value *V2) {
  89. if (KnownBitsComputed)
  90. return;
  91. KnownBitsComputed = true;
  92. const DataLayout &DL = UserI->getModule()->getDataLayout();
  93. Known = KnownBits(BitWidth);
  94. computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
  95. if (V2) {
  96. Known2 = KnownBits(BitWidth);
  97. computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
  98. }
  99. };
  100. switch (UserI->getOpcode()) {
  101. default: break;
  102. case Instruction::Call:
  103. case Instruction::Invoke:
  104. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI)) {
  105. switch (II->getIntrinsicID()) {
  106. default: break;
  107. case Intrinsic::bswap:
  108. // The alive bits of the input are the swapped alive bits of
  109. // the output.
  110. AB = AOut.byteSwap();
  111. break;
  112. case Intrinsic::bitreverse:
  113. // The alive bits of the input are the reversed alive bits of
  114. // the output.
  115. AB = AOut.reverseBits();
  116. break;
  117. case Intrinsic::ctlz:
  118. if (OperandNo == 0) {
  119. // We need some output bits, so we need all bits of the
  120. // input to the left of, and including, the leftmost bit
  121. // known to be one.
  122. ComputeKnownBits(BitWidth, Val, nullptr);
  123. AB = APInt::getHighBitsSet(BitWidth,
  124. std::min(BitWidth, Known.countMaxLeadingZeros()+1));
  125. }
  126. break;
  127. case Intrinsic::cttz:
  128. if (OperandNo == 0) {
  129. // We need some output bits, so we need all bits of the
  130. // input to the right of, and including, the rightmost bit
  131. // known to be one.
  132. ComputeKnownBits(BitWidth, Val, nullptr);
  133. AB = APInt::getLowBitsSet(BitWidth,
  134. std::min(BitWidth, Known.countMaxTrailingZeros()+1));
  135. }
  136. break;
  137. case Intrinsic::fshl:
  138. case Intrinsic::fshr: {
  139. const APInt *SA;
  140. if (OperandNo == 2) {
  141. // Shift amount is modulo the bitwidth. For powers of two we have
  142. // SA % BW == SA & (BW - 1).
  143. if (isPowerOf2_32(BitWidth))
  144. AB = BitWidth - 1;
  145. } else if (match(II->getOperand(2), m_APInt(SA))) {
  146. // Normalize to funnel shift left. APInt shifts of BitWidth are well-
  147. // defined, so no need to special-case zero shifts here.
  148. uint64_t ShiftAmt = SA->urem(BitWidth);
  149. if (II->getIntrinsicID() == Intrinsic::fshr)
  150. ShiftAmt = BitWidth - ShiftAmt;
  151. if (OperandNo == 0)
  152. AB = AOut.lshr(ShiftAmt);
  153. else if (OperandNo == 1)
  154. AB = AOut.shl(BitWidth - ShiftAmt);
  155. }
  156. break;
  157. }
  158. case Intrinsic::umax:
  159. case Intrinsic::umin:
  160. case Intrinsic::smax:
  161. case Intrinsic::smin:
  162. // If low bits of result are not demanded, they are also not demanded
  163. // for the min/max operands.
  164. AB = APInt::getBitsSetFrom(BitWidth, AOut.countTrailingZeros());
  165. break;
  166. }
  167. }
  168. break;
  169. case Instruction::Add:
  170. if (AOut.isMask()) {
  171. AB = AOut;
  172. } else {
  173. ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
  174. AB = determineLiveOperandBitsAdd(OperandNo, AOut, Known, Known2);
  175. }
  176. break;
  177. case Instruction::Sub:
  178. if (AOut.isMask()) {
  179. AB = AOut;
  180. } else {
  181. ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
  182. AB = determineLiveOperandBitsSub(OperandNo, AOut, Known, Known2);
  183. }
  184. break;
  185. case Instruction::Mul:
  186. // Find the highest live output bit. We don't need any more input
  187. // bits than that (adds, and thus subtracts, ripple only to the
  188. // left).
  189. AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits());
  190. break;
  191. case Instruction::Shl:
  192. if (OperandNo == 0) {
  193. const APInt *ShiftAmtC;
  194. if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
  195. uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
  196. AB = AOut.lshr(ShiftAmt);
  197. // If the shift is nuw/nsw, then the high bits are not dead
  198. // (because we've promised that they *must* be zero).
  199. const ShlOperator *S = cast<ShlOperator>(UserI);
  200. if (S->hasNoSignedWrap())
  201. AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
  202. else if (S->hasNoUnsignedWrap())
  203. AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
  204. }
  205. }
  206. break;
  207. case Instruction::LShr:
  208. if (OperandNo == 0) {
  209. const APInt *ShiftAmtC;
  210. if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
  211. uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
  212. AB = AOut.shl(ShiftAmt);
  213. // If the shift is exact, then the low bits are not dead
  214. // (they must be zero).
  215. if (cast<LShrOperator>(UserI)->isExact())
  216. AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
  217. }
  218. }
  219. break;
  220. case Instruction::AShr:
  221. if (OperandNo == 0) {
  222. const APInt *ShiftAmtC;
  223. if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
  224. uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
  225. AB = AOut.shl(ShiftAmt);
  226. // Because the high input bit is replicated into the
  227. // high-order bits of the result, if we need any of those
  228. // bits, then we must keep the highest input bit.
  229. if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))
  230. .getBoolValue())
  231. AB.setSignBit();
  232. // If the shift is exact, then the low bits are not dead
  233. // (they must be zero).
  234. if (cast<AShrOperator>(UserI)->isExact())
  235. AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
  236. }
  237. }
  238. break;
  239. case Instruction::And:
  240. AB = AOut;
  241. // For bits that are known zero, the corresponding bits in the
  242. // other operand are dead (unless they're both zero, in which
  243. // case they can't both be dead, so just mark the LHS bits as
  244. // dead).
  245. ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
  246. if (OperandNo == 0)
  247. AB &= ~Known2.Zero;
  248. else
  249. AB &= ~(Known.Zero & ~Known2.Zero);
  250. break;
  251. case Instruction::Or:
  252. AB = AOut;
  253. // For bits that are known one, the corresponding bits in the
  254. // other operand are dead (unless they're both one, in which
  255. // case they can't both be dead, so just mark the LHS bits as
  256. // dead).
  257. ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
  258. if (OperandNo == 0)
  259. AB &= ~Known2.One;
  260. else
  261. AB &= ~(Known.One & ~Known2.One);
  262. break;
  263. case Instruction::Xor:
  264. case Instruction::PHI:
  265. AB = AOut;
  266. break;
  267. case Instruction::Trunc:
  268. AB = AOut.zext(BitWidth);
  269. break;
  270. case Instruction::ZExt:
  271. AB = AOut.trunc(BitWidth);
  272. break;
  273. case Instruction::SExt:
  274. AB = AOut.trunc(BitWidth);
  275. // Because the high input bit is replicated into the
  276. // high-order bits of the result, if we need any of those
  277. // bits, then we must keep the highest input bit.
  278. if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),
  279. AOut.getBitWidth() - BitWidth))
  280. .getBoolValue())
  281. AB.setSignBit();
  282. break;
  283. case Instruction::Select:
  284. if (OperandNo != 0)
  285. AB = AOut;
  286. break;
  287. case Instruction::ExtractElement:
  288. if (OperandNo == 0)
  289. AB = AOut;
  290. break;
  291. case Instruction::InsertElement:
  292. case Instruction::ShuffleVector:
  293. if (OperandNo == 0 || OperandNo == 1)
  294. AB = AOut;
  295. break;
  296. }
  297. }
  298. bool DemandedBitsWrapperPass::runOnFunction(Function &F) {
  299. auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
  300. auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  301. DB.emplace(F, AC, DT);
  302. return false;
  303. }
  304. void DemandedBitsWrapperPass::releaseMemory() {
  305. DB.reset();
  306. }
  307. void DemandedBits::performAnalysis() {
  308. if (Analyzed)
  309. // Analysis already completed for this function.
  310. return;
  311. Analyzed = true;
  312. Visited.clear();
  313. AliveBits.clear();
  314. DeadUses.clear();
  315. SmallSetVector<Instruction*, 16> Worklist;
  316. // Collect the set of "root" instructions that are known live.
  317. for (Instruction &I : instructions(F)) {
  318. if (!isAlwaysLive(&I))
  319. continue;
  320. LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");
  321. // For integer-valued instructions, set up an initial empty set of alive
  322. // bits and add the instruction to the work list. For other instructions
  323. // add their operands to the work list (for integer values operands, mark
  324. // all bits as live).
  325. Type *T = I.getType();
  326. if (T->isIntOrIntVectorTy()) {
  327. if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second)
  328. Worklist.insert(&I);
  329. continue;
  330. }
  331. // Non-integer-typed instructions...
  332. for (Use &OI : I.operands()) {
  333. if (Instruction *J = dyn_cast<Instruction>(OI)) {
  334. Type *T = J->getType();
  335. if (T->isIntOrIntVectorTy())
  336. AliveBits[J] = APInt::getAllOnes(T->getScalarSizeInBits());
  337. else
  338. Visited.insert(J);
  339. Worklist.insert(J);
  340. }
  341. }
  342. // To save memory, we don't add I to the Visited set here. Instead, we
  343. // check isAlwaysLive on every instruction when searching for dead
  344. // instructions later (we need to check isAlwaysLive for the
  345. // integer-typed instructions anyway).
  346. }
  347. // Propagate liveness backwards to operands.
  348. while (!Worklist.empty()) {
  349. Instruction *UserI = Worklist.pop_back_val();
  350. LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);
  351. APInt AOut;
  352. bool InputIsKnownDead = false;
  353. if (UserI->getType()->isIntOrIntVectorTy()) {
  354. AOut = AliveBits[UserI];
  355. LLVM_DEBUG(dbgs() << " Alive Out: 0x"
  356. << Twine::utohexstr(AOut.getLimitedValue()));
  357. // If all bits of the output are dead, then all bits of the input
  358. // are also dead.
  359. InputIsKnownDead = !AOut && !isAlwaysLive(UserI);
  360. }
  361. LLVM_DEBUG(dbgs() << "\n");
  362. KnownBits Known, Known2;
  363. bool KnownBitsComputed = false;
  364. // Compute the set of alive bits for each operand. These are anded into the
  365. // existing set, if any, and if that changes the set of alive bits, the
  366. // operand is added to the work-list.
  367. for (Use &OI : UserI->operands()) {
  368. // We also want to detect dead uses of arguments, but will only store
  369. // demanded bits for instructions.
  370. Instruction *I = dyn_cast<Instruction>(OI);
  371. if (!I && !isa<Argument>(OI))
  372. continue;
  373. Type *T = OI->getType();
  374. if (T->isIntOrIntVectorTy()) {
  375. unsigned BitWidth = T->getScalarSizeInBits();
  376. APInt AB = APInt::getAllOnes(BitWidth);
  377. if (InputIsKnownDead) {
  378. AB = APInt(BitWidth, 0);
  379. } else {
  380. // Bits of each operand that are used to compute alive bits of the
  381. // output are alive, all others are dead.
  382. determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB,
  383. Known, Known2, KnownBitsComputed);
  384. // Keep track of uses which have no demanded bits.
  385. if (AB.isZero())
  386. DeadUses.insert(&OI);
  387. else
  388. DeadUses.erase(&OI);
  389. }
  390. if (I) {
  391. // If we've added to the set of alive bits (or the operand has not
  392. // been previously visited), then re-queue the operand to be visited
  393. // again.
  394. auto Res = AliveBits.try_emplace(I);
  395. if (Res.second || (AB |= Res.first->second) != Res.first->second) {
  396. Res.first->second = std::move(AB);
  397. Worklist.insert(I);
  398. }
  399. }
  400. } else if (I && Visited.insert(I).second) {
  401. Worklist.insert(I);
  402. }
  403. }
  404. }
  405. }
  406. APInt DemandedBits::getDemandedBits(Instruction *I) {
  407. performAnalysis();
  408. auto Found = AliveBits.find(I);
  409. if (Found != AliveBits.end())
  410. return Found->second;
  411. const DataLayout &DL = I->getModule()->getDataLayout();
  412. return APInt::getAllOnes(DL.getTypeSizeInBits(I->getType()->getScalarType()));
  413. }
  414. APInt DemandedBits::getDemandedBits(Use *U) {
  415. Type *T = (*U)->getType();
  416. Instruction *UserI = cast<Instruction>(U->getUser());
  417. const DataLayout &DL = UserI->getModule()->getDataLayout();
  418. unsigned BitWidth = DL.getTypeSizeInBits(T->getScalarType());
  419. // We only track integer uses, everything else produces a mask with all bits
  420. // set
  421. if (!T->isIntOrIntVectorTy())
  422. return APInt::getAllOnes(BitWidth);
  423. if (isUseDead(U))
  424. return APInt(BitWidth, 0);
  425. performAnalysis();
  426. APInt AOut = getDemandedBits(UserI);
  427. APInt AB = APInt::getAllOnes(BitWidth);
  428. KnownBits Known, Known2;
  429. bool KnownBitsComputed = false;
  430. determineLiveOperandBits(UserI, *U, U->getOperandNo(), AOut, AB, Known,
  431. Known2, KnownBitsComputed);
  432. return AB;
  433. }
  434. bool DemandedBits::isInstructionDead(Instruction *I) {
  435. performAnalysis();
  436. return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() &&
  437. !isAlwaysLive(I);
  438. }
  439. bool DemandedBits::isUseDead(Use *U) {
  440. // We only track integer uses, everything else is assumed live.
  441. if (!(*U)->getType()->isIntOrIntVectorTy())
  442. return false;
  443. // Uses by always-live instructions are never dead.
  444. Instruction *UserI = cast<Instruction>(U->getUser());
  445. if (isAlwaysLive(UserI))
  446. return false;
  447. performAnalysis();
  448. if (DeadUses.count(U))
  449. return true;
  450. // If no output bits are demanded, no input bits are demanded and the use
  451. // is dead. These uses might not be explicitly present in the DeadUses map.
  452. if (UserI->getType()->isIntOrIntVectorTy()) {
  453. auto Found = AliveBits.find(UserI);
  454. if (Found != AliveBits.end() && Found->second.isZero())
  455. return true;
  456. }
  457. return false;
  458. }
  459. void DemandedBits::print(raw_ostream &OS) {
  460. auto PrintDB = [&](const Instruction *I, const APInt &A, Value *V = nullptr) {
  461. OS << "DemandedBits: 0x" << Twine::utohexstr(A.getLimitedValue())
  462. << " for ";
  463. if (V) {
  464. V->printAsOperand(OS, false);
  465. OS << " in ";
  466. }
  467. OS << *I << '\n';
  468. };
  469. performAnalysis();
  470. for (auto &KV : AliveBits) {
  471. Instruction *I = KV.first;
  472. PrintDB(I, KV.second);
  473. for (Use &OI : I->operands()) {
  474. PrintDB(I, getDemandedBits(&OI), OI);
  475. }
  476. }
  477. }
  478. static APInt determineLiveOperandBitsAddCarry(unsigned OperandNo,
  479. const APInt &AOut,
  480. const KnownBits &LHS,
  481. const KnownBits &RHS,
  482. bool CarryZero, bool CarryOne) {
  483. assert(!(CarryZero && CarryOne) &&
  484. "Carry can't be zero and one at the same time");
  485. // The following check should be done by the caller, as it also indicates
  486. // that LHS and RHS don't need to be computed.
  487. //
  488. // if (AOut.isMask())
  489. // return AOut;
  490. // Boundary bits' carry out is unaffected by their carry in.
  491. APInt Bound = (LHS.Zero & RHS.Zero) | (LHS.One & RHS.One);
  492. // First, the alive carry bits are determined from the alive output bits:
  493. // Let demand ripple to the right but only up to any set bit in Bound.
  494. // AOut = -1----
  495. // Bound = ----1-
  496. // ACarry&~AOut = --111-
  497. APInt RBound = Bound.reverseBits();
  498. APInt RAOut = AOut.reverseBits();
  499. APInt RProp = RAOut + (RAOut | ~RBound);
  500. APInt RACarry = RProp ^ ~RBound;
  501. APInt ACarry = RACarry.reverseBits();
  502. // Then, the alive input bits are determined from the alive carry bits:
  503. APInt NeededToMaintainCarryZero;
  504. APInt NeededToMaintainCarryOne;
  505. if (OperandNo == 0) {
  506. NeededToMaintainCarryZero = LHS.Zero | ~RHS.Zero;
  507. NeededToMaintainCarryOne = LHS.One | ~RHS.One;
  508. } else {
  509. NeededToMaintainCarryZero = RHS.Zero | ~LHS.Zero;
  510. NeededToMaintainCarryOne = RHS.One | ~LHS.One;
  511. }
  512. // As in computeForAddCarry
  513. APInt PossibleSumZero = ~LHS.Zero + ~RHS.Zero + !CarryZero;
  514. APInt PossibleSumOne = LHS.One + RHS.One + CarryOne;
  515. // The below is simplified from
  516. //
  517. // APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
  518. // APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
  519. // APInt CarryUnknown = ~(CarryKnownZero | CarryKnownOne);
  520. //
  521. // APInt NeededToMaintainCarry =
  522. // (CarryKnownZero & NeededToMaintainCarryZero) |
  523. // (CarryKnownOne & NeededToMaintainCarryOne) |
  524. // CarryUnknown;
  525. APInt NeededToMaintainCarry = (~PossibleSumZero | NeededToMaintainCarryZero) &
  526. (PossibleSumOne | NeededToMaintainCarryOne);
  527. APInt AB = AOut | (ACarry & NeededToMaintainCarry);
  528. return AB;
  529. }
  530. APInt DemandedBits::determineLiveOperandBitsAdd(unsigned OperandNo,
  531. const APInt &AOut,
  532. const KnownBits &LHS,
  533. const KnownBits &RHS) {
  534. return determineLiveOperandBitsAddCarry(OperandNo, AOut, LHS, RHS, true,
  535. false);
  536. }
  537. APInt DemandedBits::determineLiveOperandBitsSub(unsigned OperandNo,
  538. const APInt &AOut,
  539. const KnownBits &LHS,
  540. const KnownBits &RHS) {
  541. KnownBits NRHS;
  542. NRHS.Zero = RHS.One;
  543. NRHS.One = RHS.Zero;
  544. return determineLiveOperandBitsAddCarry(OperandNo, AOut, LHS, NRHS, false,
  545. true);
  546. }
  547. FunctionPass *llvm::createDemandedBitsWrapperPass() {
  548. return new DemandedBitsWrapperPass();
  549. }
  550. AnalysisKey DemandedBitsAnalysis::Key;
  551. DemandedBits DemandedBitsAnalysis::run(Function &F,
  552. FunctionAnalysisManager &AM) {
  553. auto &AC = AM.getResult<AssumptionAnalysis>(F);
  554. auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
  555. return DemandedBits(F, AC, DT);
  556. }
  557. PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
  558. FunctionAnalysisManager &AM) {
  559. AM.getResult<DemandedBitsAnalysis>(F).print(OS);
  560. return PreservedAnalyses::all();
  561. }