DemandedBits.cpp 21 KB

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