Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 1 | //===- DemandedBits.cpp - Determine demanded bits -------------------------===// |
James Molloy | 87405c7 | 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 | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/APInt.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallPtrSet.h" |
| 25 | #include "llvm/ADT/SmallVector.h" |
James Molloy | bcd7f0a | 2015-10-08 12:39:59 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AssumptionCache.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ValueTracking.h" |
| 29 | #include "llvm/IR/BasicBlock.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Constants.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DataLayout.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DerivedTypes.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Dominators.h" |
| 34 | #include "llvm/IR/InstIterator.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 35 | #include "llvm/IR/InstrTypes.h" |
| 36 | #include "llvm/IR/Instruction.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 37 | #include "llvm/IR/IntrinsicInst.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Intrinsics.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 39 | #include "llvm/IR/Module.h" |
| 40 | #include "llvm/IR/Operator.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 41 | #include "llvm/IR/PassManager.h" |
| 42 | #include "llvm/IR/Type.h" |
| 43 | #include "llvm/IR/Use.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 44 | #include "llvm/Pass.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Casting.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Debug.h" |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 47 | #include "llvm/Support/KnownBits.h" |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 48 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 49 | #include <algorithm> |
| 50 | #include <cstdint> |
| 51 | |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 52 | using namespace llvm; |
| 53 | |
| 54 | #define DEBUG_TYPE "demanded-bits" |
| 55 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 56 | char DemandedBitsWrapperPass::ID = 0; |
Eugene Zelenko | 38c02bc | 2017-07-21 21:37:46 +0000 | [diff] [blame] | 57 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 58 | INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits", |
| 59 | "Demanded bits analysis", false, false) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 60 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 61 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 62 | INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits", |
| 63 | "Demanded bits analysis", false, false) |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 64 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 65 | DemandedBitsWrapperPass::DemandedBitsWrapperPass() : FunctionPass(ID) { |
| 66 | initializeDemandedBitsWrapperPassPass(*PassRegistry::getPassRegistry()); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 69 | void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 70 | AU.setPreservesCFG(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 71 | AU.addRequired<AssumptionCacheTracker>(); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 72 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 73 | AU.setPreservesAll(); |
| 74 | } |
| 75 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 76 | void DemandedBitsWrapperPass::print(raw_ostream &OS, const Module *M) const { |
| 77 | DB->print(OS); |
| 78 | } |
| 79 | |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 80 | static bool isAlwaysLive(Instruction *I) { |
Chandler Carruth | 9ae926b | 2018-08-26 09:51:22 +0000 | [diff] [blame] | 81 | return I->isTerminator() || isa<DbgInfoIntrinsic>(I) || I->isEHPad() || |
| 82 | I->mayHaveSideEffects(); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 83 | } |
| 84 | |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 85 | void DemandedBits::determineLiveOperandBits( |
| 86 | const Instruction *UserI, const Instruction *I, unsigned OperandNo, |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 87 | const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2) { |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 88 | unsigned BitWidth = AB.getBitWidth(); |
| 89 | |
| 90 | // We're called once per operand, but for some instructions, we need to |
| 91 | // compute known bits of both operands in order to determine the live bits of |
| 92 | // either (when both operands are instructions themselves). We don't, |
| 93 | // however, want to do this twice, so we cache the result in APInts that live |
| 94 | // in the caller. For the two-relevant-operands case, both operand values are |
| 95 | // provided here. |
| 96 | auto ComputeKnownBits = |
| 97 | [&](unsigned BitWidth, const Value *V1, const Value *V2) { |
| 98 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 99 | Known = KnownBits(BitWidth); |
Craig Topper | 9fe3579 | 2017-05-13 17:22:16 +0000 | [diff] [blame] | 100 | computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 101 | |
| 102 | if (V2) { |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 103 | Known2 = KnownBits(BitWidth); |
Craig Topper | 9fe3579 | 2017-05-13 17:22:16 +0000 | [diff] [blame] | 104 | computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
| 108 | switch (UserI->getOpcode()) { |
| 109 | default: break; |
| 110 | case Instruction::Call: |
| 111 | case Instruction::Invoke: |
| 112 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(UserI)) |
| 113 | switch (II->getIntrinsicID()) { |
| 114 | default: break; |
| 115 | case Intrinsic::bswap: |
| 116 | // The alive bits of the input are the swapped alive bits of |
| 117 | // the output. |
| 118 | AB = AOut.byteSwap(); |
| 119 | break; |
Brian Gesiak | 0a7894d | 2017-04-13 16:44:25 +0000 | [diff] [blame] | 120 | case Intrinsic::bitreverse: |
Xin Tong | bb8dbcf | 2017-06-19 20:10:41 +0000 | [diff] [blame] | 121 | // The alive bits of the input are the reversed alive bits of |
| 122 | // the output. |
Brian Gesiak | 0a7894d | 2017-04-13 16:44:25 +0000 | [diff] [blame] | 123 | AB = AOut.reverseBits(); |
| 124 | break; |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 125 | case Intrinsic::ctlz: |
| 126 | if (OperandNo == 0) { |
| 127 | // We need some output bits, so we need all bits of the |
| 128 | // input to the left of, and including, the leftmost bit |
| 129 | // known to be one. |
| 130 | ComputeKnownBits(BitWidth, I, nullptr); |
| 131 | AB = APInt::getHighBitsSet(BitWidth, |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 132 | std::min(BitWidth, Known.countMaxLeadingZeros()+1)); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 133 | } |
| 134 | break; |
| 135 | case Intrinsic::cttz: |
| 136 | if (OperandNo == 0) { |
| 137 | // We need some output bits, so we need all bits of the |
| 138 | // input to the right of, and including, the rightmost bit |
| 139 | // known to be one. |
| 140 | ComputeKnownBits(BitWidth, I, nullptr); |
| 141 | AB = APInt::getLowBitsSet(BitWidth, |
Craig Topper | 8df66c6 | 2017-05-12 17:20:30 +0000 | [diff] [blame] | 142 | std::min(BitWidth, Known.countMaxTrailingZeros()+1)); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 143 | } |
| 144 | break; |
Nikita Popov | f94c8f0 | 2018-11-26 15:36:57 +0000 | [diff] [blame] | 145 | case Intrinsic::fshl: |
| 146 | case Intrinsic::fshr: |
| 147 | if (OperandNo == 2) { |
| 148 | // Shift amount is modulo the bitwidth. For powers of two we have |
| 149 | // SA % BW == SA & (BW - 1). |
| 150 | if (isPowerOf2_32(BitWidth)) |
| 151 | AB = BitWidth - 1; |
| 152 | } else if (auto *SA = dyn_cast<ConstantInt>(II->getOperand(2))) { |
| 153 | // TODO: Support vectors. |
| 154 | // Normalize to funnel shift left. APInt shifts of BitWidth are well- |
| 155 | // defined, so no need to special-case zero shifts here. |
| 156 | uint64_t ShiftAmt = SA->getValue().urem(BitWidth); |
| 157 | if (II->getIntrinsicID() == Intrinsic::fshr) |
| 158 | ShiftAmt = BitWidth - ShiftAmt; |
| 159 | |
| 160 | if (OperandNo == 0) |
| 161 | AB = AOut.lshr(ShiftAmt); |
| 162 | else if (OperandNo == 1) |
| 163 | AB = AOut.shl(BitWidth - ShiftAmt); |
| 164 | } |
| 165 | break; |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 166 | } |
| 167 | break; |
| 168 | case Instruction::Add: |
| 169 | case Instruction::Sub: |
James Molloy | bcd7f0a | 2015-10-08 12:39:59 +0000 | [diff] [blame] | 170 | case Instruction::Mul: |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 171 | // Find the highest live output bit. We don't need any more input |
| 172 | // bits than that (adds, and thus subtracts, ripple only to the |
| 173 | // left). |
| 174 | AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits()); |
| 175 | break; |
| 176 | case Instruction::Shl: |
| 177 | if (OperandNo == 0) |
Sanjay Patel | 1bbdf4e | 2017-07-07 14:39:26 +0000 | [diff] [blame] | 178 | if (auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->getOperand(1))) { |
| 179 | uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 180 | AB = AOut.lshr(ShiftAmt); |
| 181 | |
| 182 | // If the shift is nuw/nsw, then the high bits are not dead |
| 183 | // (because we've promised that they *must* be zero). |
| 184 | const ShlOperator *S = cast<ShlOperator>(UserI); |
| 185 | if (S->hasNoSignedWrap()) |
| 186 | AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1); |
| 187 | else if (S->hasNoUnsignedWrap()) |
| 188 | AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt); |
| 189 | } |
| 190 | break; |
| 191 | case Instruction::LShr: |
| 192 | if (OperandNo == 0) |
Sanjay Patel | 1bbdf4e | 2017-07-07 14:39:26 +0000 | [diff] [blame] | 193 | if (auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->getOperand(1))) { |
| 194 | uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 195 | AB = AOut.shl(ShiftAmt); |
| 196 | |
| 197 | // If the shift is exact, then the low bits are not dead |
| 198 | // (they must be zero). |
| 199 | if (cast<LShrOperator>(UserI)->isExact()) |
| 200 | AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
| 201 | } |
| 202 | break; |
| 203 | case Instruction::AShr: |
| 204 | if (OperandNo == 0) |
Sanjay Patel | 1bbdf4e | 2017-07-07 14:39:26 +0000 | [diff] [blame] | 205 | if (auto *ShiftAmtC = dyn_cast<ConstantInt>(UserI->getOperand(1))) { |
| 206 | uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 207 | AB = AOut.shl(ShiftAmt); |
| 208 | // Because the high input bit is replicated into the |
| 209 | // high-order bits of the result, if we need any of those |
| 210 | // bits, then we must keep the highest input bit. |
| 211 | if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt)) |
| 212 | .getBoolValue()) |
Craig Topper | 24db6b8 | 2017-04-28 16:58:05 +0000 | [diff] [blame] | 213 | AB.setSignBit(); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 214 | |
| 215 | // If the shift is exact, then the low bits are not dead |
| 216 | // (they must be zero). |
| 217 | if (cast<AShrOperator>(UserI)->isExact()) |
| 218 | AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt); |
| 219 | } |
| 220 | break; |
| 221 | case Instruction::And: |
| 222 | AB = AOut; |
| 223 | |
| 224 | // For bits that are known zero, the corresponding bits in the |
| 225 | // other operand are dead (unless they're both zero, in which |
| 226 | // case they can't both be dead, so just mark the LHS bits as |
| 227 | // dead). |
| 228 | if (OperandNo == 0) { |
| 229 | ComputeKnownBits(BitWidth, I, UserI->getOperand(1)); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 230 | AB &= ~Known2.Zero; |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 231 | } else { |
| 232 | if (!isa<Instruction>(UserI->getOperand(0))) |
| 233 | ComputeKnownBits(BitWidth, UserI->getOperand(0), I); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 234 | AB &= ~(Known.Zero & ~Known2.Zero); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 235 | } |
| 236 | break; |
| 237 | case Instruction::Or: |
| 238 | AB = AOut; |
| 239 | |
| 240 | // For bits that are known one, the corresponding bits in the |
| 241 | // other operand are dead (unless they're both one, in which |
| 242 | // case they can't both be dead, so just mark the LHS bits as |
| 243 | // dead). |
| 244 | if (OperandNo == 0) { |
| 245 | ComputeKnownBits(BitWidth, I, UserI->getOperand(1)); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 246 | AB &= ~Known2.One; |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 247 | } else { |
| 248 | if (!isa<Instruction>(UserI->getOperand(0))) |
| 249 | ComputeKnownBits(BitWidth, UserI->getOperand(0), I); |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 250 | AB &= ~(Known.One & ~Known2.One); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 251 | } |
| 252 | break; |
| 253 | case Instruction::Xor: |
| 254 | case Instruction::PHI: |
| 255 | AB = AOut; |
| 256 | break; |
| 257 | case Instruction::Trunc: |
| 258 | AB = AOut.zext(BitWidth); |
| 259 | break; |
| 260 | case Instruction::ZExt: |
| 261 | AB = AOut.trunc(BitWidth); |
| 262 | break; |
| 263 | case Instruction::SExt: |
| 264 | AB = AOut.trunc(BitWidth); |
| 265 | // Because the high input bit is replicated into the |
| 266 | // high-order bits of the result, if we need any of those |
| 267 | // bits, then we must keep the highest input bit. |
| 268 | if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(), |
| 269 | AOut.getBitWidth() - BitWidth)) |
| 270 | .getBoolValue()) |
Craig Topper | 24db6b8 | 2017-04-28 16:58:05 +0000 | [diff] [blame] | 271 | AB.setSignBit(); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 272 | break; |
| 273 | case Instruction::Select: |
| 274 | if (OperandNo != 0) |
| 275 | AB = AOut; |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 280 | bool DemandedBitsWrapperPass::runOnFunction(Function &F) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 281 | auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 282 | auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 283 | DB.emplace(F, AC, DT); |
James Molloy | ab9fdb9 | 2015-10-08 12:39:50 +0000 | [diff] [blame] | 284 | return false; |
| 285 | } |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 286 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 287 | void DemandedBitsWrapperPass::releaseMemory() { |
| 288 | DB.reset(); |
| 289 | } |
| 290 | |
James Molloy | ab9fdb9 | 2015-10-08 12:39:50 +0000 | [diff] [blame] | 291 | void DemandedBits::performAnalysis() { |
| 292 | if (Analyzed) |
| 293 | // Analysis already completed for this function. |
| 294 | return; |
| 295 | Analyzed = true; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 296 | |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 297 | Visited.clear(); |
| 298 | AliveBits.clear(); |
| 299 | |
| 300 | SmallVector<Instruction*, 128> Worklist; |
| 301 | |
| 302 | // Collect the set of "root" instructions that are known live. |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 303 | for (Instruction &I : instructions(F)) { |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 304 | if (!isAlwaysLive(&I)) |
| 305 | continue; |
| 306 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 307 | LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n"); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 308 | // For integer-valued instructions, set up an initial empty set of alive |
| 309 | // bits and add the instruction to the work list. For other instructions |
| 310 | // add their operands to the work list (for integer values operands, mark |
| 311 | // all bits as live). |
| 312 | if (IntegerType *IT = dyn_cast<IntegerType>(I.getType())) { |
Benjamin Kramer | a9e477b | 2016-07-21 13:37:55 +0000 | [diff] [blame] | 313 | if (AliveBits.try_emplace(&I, IT->getBitWidth(), 0).second) |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 314 | Worklist.push_back(&I); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 315 | |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | // Non-integer-typed instructions... |
| 320 | for (Use &OI : I.operands()) { |
| 321 | if (Instruction *J = dyn_cast<Instruction>(OI)) { |
| 322 | if (IntegerType *IT = dyn_cast<IntegerType>(J->getType())) |
| 323 | AliveBits[J] = APInt::getAllOnesValue(IT->getBitWidth()); |
| 324 | Worklist.push_back(J); |
| 325 | } |
| 326 | } |
| 327 | // To save memory, we don't add I to the Visited set here. Instead, we |
| 328 | // check isAlwaysLive on every instruction when searching for dead |
| 329 | // instructions later (we need to check isAlwaysLive for the |
| 330 | // integer-typed instructions anyway). |
| 331 | } |
| 332 | |
| 333 | // Propagate liveness backwards to operands. |
| 334 | while (!Worklist.empty()) { |
| 335 | Instruction *UserI = Worklist.pop_back_val(); |
| 336 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 337 | LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 338 | APInt AOut; |
| 339 | if (UserI->getType()->isIntegerTy()) { |
| 340 | AOut = AliveBits[UserI]; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 341 | LLVM_DEBUG(dbgs() << " Alive Out: " << AOut); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 342 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 343 | LLVM_DEBUG(dbgs() << "\n"); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 344 | |
| 345 | if (!UserI->getType()->isIntegerTy()) |
| 346 | Visited.insert(UserI); |
| 347 | |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 348 | KnownBits Known, Known2; |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 349 | // Compute the set of alive bits for each operand. These are anded into the |
| 350 | // existing set, if any, and if that changes the set of alive bits, the |
| 351 | // operand is added to the work-list. |
| 352 | for (Use &OI : UserI->operands()) { |
| 353 | if (Instruction *I = dyn_cast<Instruction>(OI)) { |
| 354 | if (IntegerType *IT = dyn_cast<IntegerType>(I->getType())) { |
| 355 | unsigned BitWidth = IT->getBitWidth(); |
| 356 | APInt AB = APInt::getAllOnesValue(BitWidth); |
| 357 | if (UserI->getType()->isIntegerTy() && !AOut && |
| 358 | !isAlwaysLive(UserI)) { |
| 359 | AB = APInt(BitWidth, 0); |
| 360 | } else { |
NAKAMURA Takumi | 8496503 | 2015-09-22 11:14:12 +0000 | [diff] [blame] | 361 | // If all bits of the output are dead, then all bits of the input |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 362 | // Bits of each operand that are used to compute alive bits of the |
| 363 | // output are alive, all others are dead. |
| 364 | determineLiveOperandBits(UserI, I, OI.getOperandNo(), AOut, AB, |
Craig Topper | b45eabc | 2017-04-26 16:39:58 +0000 | [diff] [blame] | 365 | Known, Known2); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // If we've added to the set of alive bits (or the operand has not |
| 369 | // been previously visited), then re-queue the operand to be visited |
| 370 | // again. |
| 371 | APInt ABPrev(BitWidth, 0); |
| 372 | auto ABI = AliveBits.find(I); |
| 373 | if (ABI != AliveBits.end()) |
| 374 | ABPrev = ABI->second; |
| 375 | |
| 376 | APInt ABNew = AB | ABPrev; |
| 377 | if (ABNew != ABPrev || ABI == AliveBits.end()) { |
| 378 | AliveBits[I] = std::move(ABNew); |
| 379 | Worklist.push_back(I); |
| 380 | } |
| 381 | } else if (!Visited.count(I)) { |
| 382 | Worklist.push_back(I); |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | APInt DemandedBits::getDemandedBits(Instruction *I) { |
James Molloy | ab9fdb9 | 2015-10-08 12:39:50 +0000 | [diff] [blame] | 390 | performAnalysis(); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 391 | |
Sanjay Patel | 042a536 | 2017-08-16 14:28:23 +0000 | [diff] [blame] | 392 | const DataLayout &DL = I->getModule()->getDataLayout(); |
Benjamin Kramer | a9e477b | 2016-07-21 13:37:55 +0000 | [diff] [blame] | 393 | auto Found = AliveBits.find(I); |
| 394 | if (Found != AliveBits.end()) |
| 395 | return Found->second; |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 396 | return APInt::getAllOnesValue(DL.getTypeSizeInBits(I->getType())); |
| 397 | } |
| 398 | |
| 399 | bool DemandedBits::isInstructionDead(Instruction *I) { |
James Molloy | ab9fdb9 | 2015-10-08 12:39:50 +0000 | [diff] [blame] | 400 | performAnalysis(); |
| 401 | |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 402 | return !Visited.count(I) && AliveBits.find(I) == AliveBits.end() && |
| 403 | !isAlwaysLive(I); |
| 404 | } |
| 405 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 406 | void DemandedBits::print(raw_ostream &OS) { |
| 407 | performAnalysis(); |
James Molloy | bcd7f0a | 2015-10-08 12:39:59 +0000 | [diff] [blame] | 408 | for (auto &KV : AliveBits) { |
Benjamin Kramer | 3a13ed6 | 2017-12-28 16:58:54 +0000 | [diff] [blame] | 409 | OS << "DemandedBits: 0x" << Twine::utohexstr(KV.second.getLimitedValue()) |
| 410 | << " for " << *KV.first << '\n'; |
James Molloy | bcd7f0a | 2015-10-08 12:39:59 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 414 | FunctionPass *llvm::createDemandedBitsWrapperPass() { |
| 415 | return new DemandedBitsWrapperPass(); |
| 416 | } |
| 417 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 418 | AnalysisKey DemandedBitsAnalysis::Key; |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 419 | |
| 420 | DemandedBits DemandedBitsAnalysis::run(Function &F, |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 421 | FunctionAnalysisManager &AM) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 422 | auto &AC = AM.getResult<AssumptionAnalysis>(F); |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 423 | auto &DT = AM.getResult<DominatorTreeAnalysis>(F); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 424 | return DemandedBits(F, AC, DT); |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | PreservedAnalyses DemandedBitsPrinterPass::run(Function &F, |
| 428 | FunctionAnalysisManager &AM) { |
| 429 | AM.getResult<DemandedBitsAnalysis>(F).print(OS); |
| 430 | return PreservedAnalyses::all(); |
James Molloy | 87405c7 | 2015-08-14 11:09:09 +0000 | [diff] [blame] | 431 | } |