Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 1 | //===- DemandedBits.cpp - Determine demanded bits -------------------------===// |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass implements a demanded bits analysis. A demanded bit is one that |
| 11 | // contributes to a result; bits that are not demanded can be either zero or |
| 12 | // one without affecting control or data flow. For example in this sequence: |
| 13 | // |
| 14 | // %1 = add i32 %x, %y |
| 15 | // %2 = trunc i32 %1 to i16 |
| 16 | // |
| 17 | // Only the lowest 16 bits of %1 are demanded; the rest are removed by the |
| 18 | // trunc. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "llvm/Analysis/DemandedBits.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/APInt.h" |
Nikita Popov | 14b956f | 2019-01-12 09:09:15 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SetVector.h" |
James Molloy | 6f819bd | 2015-10-08 12:39:59 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/AssumptionCache.h" |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ValueTracking.h" |
| 28 | #include "llvm/IR/BasicBlock.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Constants.h" |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DerivedTypes.h" |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Dominators.h" |
| 33 | #include "llvm/IR/InstIterator.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 34 | #include "llvm/IR/InstrTypes.h" |
| 35 | #include "llvm/IR/Instruction.h" |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 36 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Intrinsics.h" |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Module.h" |
| 39 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 40 | #include "llvm/IR/PassManager.h" |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 41 | #include "llvm/IR/PatternMatch.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 42 | #include "llvm/IR/Type.h" |
| 43 | #include "llvm/IR/Use.h" |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 44 | #include "llvm/Pass.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Casting.h" |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 47 | #include "llvm/Support/KnownBits.h" |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 48 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 49 | #include <algorithm> |
| 50 | #include <cstdint> |
| 51 | |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 52 | using namespace llvm; |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 53 | using namespace llvm::PatternMatch; |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 54 | |
| 55 | #define DEBUG_TYPE "demanded-bits" |
| 56 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 57 | char DemandedBitsWrapperPass::ID = 0; |
Eugene Zelenko | 15a56d4 | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 58 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 59 | INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits", |
| 60 | "Demanded bits analysis", false, false) |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 61 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 62 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 63 | INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits", |
| 64 | "Demanded bits analysis", false, false) |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 65 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 66 | DemandedBitsWrapperPass::DemandedBitsWrapperPass() : FunctionPass(ID) { |
| 67 | initializeDemandedBitsWrapperPassPass(*PassRegistry::getPassRegistry()); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 70 | void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 71 | AU.setPreservesCFG(); |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 72 | AU.addRequired<AssumptionCacheTracker>(); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 73 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 74 | AU.setPreservesAll(); |
| 75 | } |
| 76 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 77 | void DemandedBitsWrapperPass::print(raw_ostream &OS, const Module *M) const { |
| 78 | DB->print(OS); |
| 79 | } |
| 80 | |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 81 | static bool isAlwaysLive(Instruction *I) { |
Chandler Carruth | 9179aee | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 82 | return I->isTerminator() || isa<DbgInfoIntrinsic>(I) || I->isEHPad() || |
| 83 | I->mayHaveSideEffects(); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 84 | } |
| 85 | |
NAKAMURA Takumi | 09c0ea5 | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 86 | void DemandedBits::determineLiveOperandBits( |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 87 | const Instruction *UserI, const Value *Val, unsigned OperandNo, |
| 88 | const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2, |
| 89 | bool &KnownBitsComputed) { |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 90 | unsigned BitWidth = AB.getBitWidth(); |
| 91 | |
| 92 | // We're called once per operand, but for some instructions, we need to |
| 93 | // compute known bits of both operands in order to determine the live bits of |
| 94 | // either (when both operands are instructions themselves). We don't, |
| 95 | // however, want to do this twice, so we cache the result in APInts that live |
| 96 | // in the caller. For the two-relevant-operands case, both operand values are |
| 97 | // provided here. |
| 98 | auto ComputeKnownBits = |
| 99 | [&](unsigned BitWidth, const Value *V1, const Value *V2) { |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 100 | if (KnownBitsComputed) |
| 101 | return; |
| 102 | KnownBitsComputed = true; |
| 103 | |
| 104 | const DataLayout &DL = UserI->getModule()->getDataLayout(); |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 105 | Known = KnownBits(BitWidth); |
Craig Topper | a7f9de4 | 2017-05-13 17:22:16 +0000 | [diff] [blame] | 106 | computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 107 | |
| 108 | if (V2) { |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 109 | Known2 = KnownBits(BitWidth); |
Craig Topper | a7f9de4 | 2017-05-13 17:22:16 +0000 | [diff] [blame] | 110 | computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 111 | } |
| 112 | }; |
| 113 | |
| 114 | switch (UserI->getOpcode()) { |
| 115 | default: break; |
| 116 | case Instruction::Call: |
| 117 | case Instruction::Invoke: |
| 118 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI)) |
| 119 | switch (II->getIntrinsicID()) { |
| 120 | default: break; |
| 121 | case Intrinsic::bswap: |
| 122 | // The alive bits of the input are the swapped alive bits of |
| 123 | // the output. |
| 124 | AB = AOut.byteSwap(); |
| 125 | break; |
Brian Gesiak | b2b7c4b | 2017-04-13 16:44:25 +0000 | [diff] [blame] | 126 | case Intrinsic::bitreverse: |
Xin Tong | b06e406 | 2017-06-19 20:10:41 +0000 | [diff] [blame] | 127 | // The alive bits of the input are the reversed alive bits of |
| 128 | // the output. |
Brian Gesiak | b2b7c4b | 2017-04-13 16:44:25 +0000 | [diff] [blame] | 129 | AB = AOut.reverseBits(); |
| 130 | break; |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 131 | case Intrinsic::ctlz: |
| 132 | if (OperandNo == 0) { |
| 133 | // We need some output bits, so we need all bits of the |
| 134 | // input to the left of, and including, the leftmost bit |
| 135 | // known to be one. |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 136 | ComputeKnownBits(BitWidth, Val, nullptr); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 137 | AB = APInt::getHighBitsSet(BitWidth, |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 138 | std::min(BitWidth, Known.countMaxLeadingZeros()+1)); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 139 | } |
| 140 | break; |
| 141 | case Intrinsic::cttz: |
| 142 | if (OperandNo == 0) { |
| 143 | // We need some output bits, so we need all bits of the |
| 144 | // input to the right of, and including, the rightmost bit |
| 145 | // known to be one. |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 146 | ComputeKnownBits(BitWidth, Val, nullptr); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 147 | AB = APInt::getLowBitsSet(BitWidth, |
Craig Topper | d493444 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 148 | std::min(BitWidth, Known.countMaxTrailingZeros()+1)); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 149 | } |
| 150 | break; |
Nikita Popov | 753cb24 | 2018-11-26 15:36:57 +0000 | [diff] [blame] | 151 | case Intrinsic::fshl: |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 152 | case Intrinsic::fshr: { |
| 153 | const APInt *SA; |
Nikita Popov | 753cb24 | 2018-11-26 15:36:57 +0000 | [diff] [blame] | 154 | if (OperandNo == 2) { |
| 155 | // Shift amount is modulo the bitwidth. For powers of two we have |
| 156 | // SA % BW == SA & (BW - 1). |
| 157 | if (isPowerOf2_32(BitWidth)) |
| 158 | AB = BitWidth - 1; |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 159 | } else if (match(II->getOperand(2), m_APInt(SA))) { |
Nikita Popov | 753cb24 | 2018-11-26 15:36:57 +0000 | [diff] [blame] | 160 | // Normalize to funnel shift left. APInt shifts of BitWidth are well- |
| 161 | // defined, so no need to special-case zero shifts here. |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 162 | uint64_t ShiftAmt = SA->urem(BitWidth); |
Nikita Popov | 753cb24 | 2018-11-26 15:36:57 +0000 | [diff] [blame] | 163 | if (II->getIntrinsicID() == Intrinsic::fshr) |
| 164 | ShiftAmt = BitWidth - ShiftAmt; |
| 165 | |
| 166 | if (OperandNo == 0) |
| 167 | AB = AOut.lshr(ShiftAmt); |
| 168 | else if (OperandNo == 1) |
| 169 | AB = AOut.shl(BitWidth - ShiftAmt); |
| 170 | } |
| 171 | break; |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 172 | } |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 173 | } |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 174 | break; |
| 175 | case Instruction::Add: |
| 176 | case Instruction::Sub: |
James Molloy | 6f819bd | 2015-10-08 12:39:59 +0000 | [diff] [blame] | 177 | case Instruction::Mul: |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 178 | // Find the highest live output bit. We don't need any more input |
| 179 | // bits than that (adds, and thus subtracts, ripple only to the |
| 180 | // left). |
| 181 | AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits()); |
| 182 | break; |
| 183 | case Instruction::Shl: |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 184 | if (OperandNo == 0) { |
| 185 | const APInt *ShiftAmtC; |
| 186 | if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) { |
Sanjay Patel | 28bebe4 | 2017-07-07 14:39:26 +0000 | [diff] [blame] | 187 | uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 188 | AB = AOut.lshr(ShiftAmt); |
| 189 | |
| 190 | // If the shift is nuw/nsw, then the high bits are not dead |
| 191 | // (because we've promised that they *must* be zero). |
| 192 | const ShlOperator *S = cast<ShlOperator>(UserI); |
| 193 | if (S->hasNoSignedWrap()) |
| 194 | AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1); |
| 195 | else if (S->hasNoUnsignedWrap()) |
| 196 | AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
| 197 | } |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 198 | } |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 199 | break; |
| 200 | case Instruction::LShr: |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 201 | if (OperandNo == 0) { |
| 202 | const APInt *ShiftAmtC; |
| 203 | if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) { |
Sanjay Patel | 28bebe4 | 2017-07-07 14:39:26 +0000 | [diff] [blame] | 204 | uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 205 | AB = AOut.shl(ShiftAmt); |
| 206 | |
| 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 | } |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 212 | } |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 213 | break; |
| 214 | case Instruction::AShr: |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 215 | if (OperandNo == 0) { |
| 216 | const APInt *ShiftAmtC; |
| 217 | if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) { |
Sanjay Patel | 28bebe4 | 2017-07-07 14:39:26 +0000 | [diff] [blame] | 218 | uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 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()) |
Craig Topper | 78b412f | 2017-04-28 16:58:05 +0000 | [diff] [blame] | 225 | AB.setSignBit(); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 226 | |
| 227 | // If the shift is exact, then the low bits are not dead |
| 228 | // (they must be zero). |
| 229 | if (cast<AShrOperator>(UserI)->isExact()) |
| 230 | AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
| 231 | } |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 232 | } |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 233 | break; |
| 234 | case Instruction::And: |
| 235 | AB = AOut; |
| 236 | |
| 237 | // For bits that are known zero, the corresponding bits in the |
| 238 | // other operand are dead (unless they're both zero, in which |
| 239 | // case they can't both be dead, so just mark the LHS bits as |
| 240 | // dead). |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 241 | ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1)); |
| 242 | if (OperandNo == 0) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 243 | AB &= ~Known2.Zero; |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 244 | else |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 245 | AB &= ~(Known.Zero & ~Known2.Zero); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 246 | break; |
| 247 | case Instruction::Or: |
| 248 | AB = AOut; |
| 249 | |
| 250 | // For bits that are known one, the corresponding bits in the |
| 251 | // other operand are dead (unless they're both one, in which |
| 252 | // case they can't both be dead, so just mark the LHS bits as |
| 253 | // dead). |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 254 | ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1)); |
| 255 | if (OperandNo == 0) |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 256 | AB &= ~Known2.One; |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 257 | else |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 258 | AB &= ~(Known.One & ~Known2.One); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 259 | break; |
| 260 | case Instruction::Xor: |
| 261 | case Instruction::PHI: |
| 262 | AB = AOut; |
| 263 | break; |
| 264 | case Instruction::Trunc: |
| 265 | AB = AOut.zext(BitWidth); |
| 266 | break; |
| 267 | case Instruction::ZExt: |
| 268 | AB = AOut.trunc(BitWidth); |
| 269 | break; |
| 270 | case Instruction::SExt: |
| 271 | AB = AOut.trunc(BitWidth); |
| 272 | // Because the high input bit is replicated into the |
| 273 | // high-order bits of the result, if we need any of those |
| 274 | // bits, then we must keep the highest input bit. |
| 275 | if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(), |
| 276 | AOut.getBitWidth() - BitWidth)) |
| 277 | .getBoolValue()) |
Craig Topper | 78b412f | 2017-04-28 16:58:05 +0000 | [diff] [blame] | 278 | AB.setSignBit(); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 279 | break; |
| 280 | case Instruction::Select: |
| 281 | if (OperandNo != 0) |
| 282 | AB = AOut; |
| 283 | break; |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 284 | case Instruction::ExtractElement: |
| 285 | if (OperandNo == 0) |
| 286 | AB = AOut; |
| 287 | break; |
| 288 | case Instruction::InsertElement: |
| 289 | case Instruction::ShuffleVector: |
| 290 | if (OperandNo == 0 || OperandNo == 1) |
| 291 | AB = AOut; |
| 292 | break; |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 296 | bool DemandedBitsWrapperPass::runOnFunction(Function &F) { |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 297 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 298 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 299 | DB.emplace(F, AC, DT); |
James Molloy | cbaa853 | 2015-10-08 12:39:50 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 302 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 303 | void DemandedBitsWrapperPass::releaseMemory() { |
| 304 | DB.reset(); |
| 305 | } |
| 306 | |
James Molloy | cbaa853 | 2015-10-08 12:39:50 +0000 | [diff] [blame] | 307 | void DemandedBits::performAnalysis() { |
| 308 | if (Analyzed) |
| 309 | // Analysis already completed for this function. |
| 310 | return; |
| 311 | Analyzed = true; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 312 | |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 313 | Visited.clear(); |
| 314 | AliveBits.clear(); |
Nikita Popov | e002075 | 2019-01-01 10:05:26 +0000 | [diff] [blame] | 315 | DeadUses.clear(); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 316 | |
Nikita Popov | 14b956f | 2019-01-12 09:09:15 +0000 | [diff] [blame] | 317 | SmallSetVector<Instruction*, 16> Worklist; |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 318 | |
| 319 | // Collect the set of "root" instructions that are known live. |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 320 | for (Instruction &I : instructions(F)) { |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 321 | if (!isAlwaysLive(&I)) |
| 322 | continue; |
| 323 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 324 | LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n"); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 325 | // For integer-valued instructions, set up an initial empty set of alive |
| 326 | // bits and add the instruction to the work list. For other instructions |
| 327 | // add their operands to the work list (for integer values operands, mark |
| 328 | // all bits as live). |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 329 | Type *T = I.getType(); |
| 330 | if (T->isIntOrIntVectorTy()) { |
| 331 | if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second) |
Nikita Popov | 14b956f | 2019-01-12 09:09:15 +0000 | [diff] [blame] | 332 | Worklist.insert(&I); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 333 | |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | // Non-integer-typed instructions... |
| 338 | for (Use &OI : I.operands()) { |
| 339 | if (Instruction *J = dyn_cast<Instruction>(OI)) { |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 340 | Type *T = J->getType(); |
| 341 | if (T->isIntOrIntVectorTy()) |
| 342 | AliveBits[J] = APInt::getAllOnesValue(T->getScalarSizeInBits()); |
Nikita Popov | 14b956f | 2019-01-12 09:09:15 +0000 | [diff] [blame] | 343 | Worklist.insert(J); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | // To save memory, we don't add I to the Visited set here. Instead, we |
| 347 | // check isAlwaysLive on every instruction when searching for dead |
| 348 | // instructions later (we need to check isAlwaysLive for the |
| 349 | // integer-typed instructions anyway). |
| 350 | } |
| 351 | |
| 352 | // Propagate liveness backwards to operands. |
| 353 | while (!Worklist.empty()) { |
| 354 | Instruction *UserI = Worklist.pop_back_val(); |
| 355 | |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 356 | LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 357 | APInt AOut; |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 358 | if (UserI->getType()->isIntOrIntVectorTy()) { |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 359 | AOut = AliveBits[UserI]; |
Nikita Popov | e002075 | 2019-01-01 10:05:26 +0000 | [diff] [blame] | 360 | LLVM_DEBUG(dbgs() << " Alive Out: 0x" |
| 361 | << Twine::utohexstr(AOut.getLimitedValue())); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 362 | } |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 363 | LLVM_DEBUG(dbgs() << "\n"); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 364 | |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 365 | if (!UserI->getType()->isIntOrIntVectorTy()) |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 366 | Visited.insert(UserI); |
| 367 | |
Craig Topper | 58c7fe6 | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 368 | KnownBits Known, Known2; |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 369 | bool KnownBitsComputed = false; |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 370 | // Compute the set of alive bits for each operand. These are anded into the |
| 371 | // existing set, if any, and if that changes the set of alive bits, the |
| 372 | // operand is added to the work-list. |
| 373 | for (Use &OI : UserI->operands()) { |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 374 | // We also want to detect dead uses of arguments, but will only store |
| 375 | // demanded bits for instructions. |
| 376 | Instruction *I = dyn_cast<Instruction>(OI); |
| 377 | if (!I && !isa<Argument>(OI)) |
| 378 | continue; |
Nikita Popov | e002075 | 2019-01-01 10:05:26 +0000 | [diff] [blame] | 379 | |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 380 | Type *T = OI->getType(); |
| 381 | if (T->isIntOrIntVectorTy()) { |
| 382 | unsigned BitWidth = T->getScalarSizeInBits(); |
| 383 | APInt AB = APInt::getAllOnesValue(BitWidth); |
| 384 | if (UserI->getType()->isIntOrIntVectorTy() && !AOut && |
| 385 | !isAlwaysLive(UserI)) { |
| 386 | // If all bits of the output are dead, then all bits of the input |
| 387 | // are also dead. |
| 388 | AB = APInt(BitWidth, 0); |
| 389 | } else { |
| 390 | // Bits of each operand that are used to compute alive bits of the |
| 391 | // output are alive, all others are dead. |
| 392 | determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB, |
| 393 | Known, Known2, KnownBitsComputed); |
Nikita Popov | c93c383 | 2018-12-19 19:56:21 +0000 | [diff] [blame] | 394 | |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 395 | // Keep track of uses which have no demanded bits. |
| 396 | if (AB.isNullValue()) |
| 397 | DeadUses.insert(&OI); |
| 398 | else |
| 399 | DeadUses.erase(&OI); |
| 400 | } |
| 401 | |
| 402 | if (I) { |
Nikita Popov | c93c383 | 2018-12-19 19:56:21 +0000 | [diff] [blame] | 403 | // If we've added to the set of alive bits (or the operand has not |
| 404 | // been previously visited), then re-queue the operand to be visited |
| 405 | // again. |
Nikita Popov | 992a545 | 2018-12-19 22:09:02 +0000 | [diff] [blame] | 406 | APInt ABPrev(BitWidth, 0); |
| 407 | auto ABI = AliveBits.find(I); |
| 408 | if (ABI != AliveBits.end()) |
| 409 | ABPrev = ABI->second; |
| 410 | |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 411 | APInt ABNew = AB | ABPrev; |
| 412 | if (ABNew != ABPrev || ABI == AliveBits.end()) { |
| 413 | AliveBits[I] = std::move(ABNew); |
Nikita Popov | 14b956f | 2019-01-12 09:09:15 +0000 | [diff] [blame] | 414 | Worklist.insert(I); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 415 | } |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 416 | } |
Nikita Popov | ef0f0e1 | 2019-01-04 21:21:43 +0000 | [diff] [blame] | 417 | } else if (I && !Visited.count(I)) { |
Nikita Popov | 14b956f | 2019-01-12 09:09:15 +0000 | [diff] [blame] | 418 | Worklist.insert(I); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | } |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | APInt DemandedBits::getDemandedBits(Instruction *I) { |
Nikita Popov | 6e9e89f | 2018-12-06 23:50:32 +0000 | [diff] [blame] | 425 | performAnalysis(); |
Nikita Popov | 0ac91b6 | 2018-12-07 00:42:03 +0000 | [diff] [blame] | 426 | |
Benjamin Kramer | ffecbde | 2016-07-21 13:37:55 +0000 | [diff] [blame] | 427 | auto Found = AliveBits.find(I); |
| 428 | if (Found != AliveBits.end()) |
| 429 | return Found->second; |
Nikita Popov | 38880e6 | 2018-12-07 15:38:13 +0000 | [diff] [blame] | 430 | |
| 431 | const DataLayout &DL = I->getModule()->getDataLayout(); |
| 432 | return APInt::getAllOnesValue( |
| 433 | DL.getTypeSizeInBits(I->getType()->getScalarType())); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | bool DemandedBits::isInstructionDead(Instruction *I) { |
James Molloy | cbaa853 | 2015-10-08 12:39:50 +0000 | [diff] [blame] | 437 | performAnalysis(); |
| 438 | |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 439 | return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() && |
| 440 | !isAlwaysLive(I); |
| 441 | } |
| 442 | |
Nikita Popov | e002075 | 2019-01-01 10:05:26 +0000 | [diff] [blame] | 443 | bool DemandedBits::isUseDead(Use *U) { |
| 444 | // We only track integer uses, everything else is assumed live. |
| 445 | if (!(*U)->getType()->isIntOrIntVectorTy()) |
| 446 | return false; |
| 447 | |
| 448 | // Uses by always-live instructions are never dead. |
| 449 | Instruction *UserI = cast<Instruction>(U->getUser()); |
| 450 | if (isAlwaysLive(UserI)) |
| 451 | return false; |
| 452 | |
| 453 | performAnalysis(); |
| 454 | if (DeadUses.count(U)) |
| 455 | return true; |
| 456 | |
| 457 | // If no output bits are demanded, no input bits are demanded and the use |
| 458 | // is dead. These uses might not be explicitly present in the DeadUses map. |
| 459 | if (UserI->getType()->isIntOrIntVectorTy()) { |
| 460 | auto Found = AliveBits.find(UserI); |
| 461 | if (Found != AliveBits.end() && Found->second.isNullValue()) |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | return false; |
| 466 | } |
| 467 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 468 | void DemandedBits::print(raw_ostream &OS) { |
| 469 | performAnalysis(); |
James Molloy | 6f819bd | 2015-10-08 12:39:59 +0000 | [diff] [blame] | 470 | for (auto &KV : AliveBits) { |
Benjamin Kramer | ca5092a | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 471 | OS << "DemandedBits: 0x" << Twine::utohexstr(KV.second.getLimitedValue()) |
| 472 | << " for " << *KV.first << '\n'; |
James Molloy | 6f819bd | 2015-10-08 12:39:59 +0000 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 476 | FunctionPass *llvm::createDemandedBitsWrapperPass() { |
| 477 | return new DemandedBitsWrapperPass(); |
| 478 | } |
| 479 | |
Chandler Carruth | 33d5681 | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 480 | AnalysisKey DemandedBitsAnalysis::Key; |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 481 | |
| 482 | DemandedBits DemandedBitsAnalysis::run(Function &F, |
Sean Silva | 20b343c | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 483 | FunctionAnalysisManager &AM) { |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 484 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 485 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
Daniel Jasper | 8de3a54 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 486 | return DemandedBits(F, AC, DT); |
Michael Kuperstein | 3445cc7 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | PreservedAnalyses DemandedBitsPrinterPass::run(Function &F, |
| 490 | FunctionAnalysisManager &AM) { |
| 491 | AM.getResult<DemandedBitsAnalysis>(F).print(OS); |
| 492 | return PreservedAnalyses::all(); |
James Molloy | 26e1739 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 493 | } |