1 //===- InstCombineSimplifyDemanded.cpp ------------------------------------===// 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 file contains logic for simplifying instructions based on information 10 // about how they are used. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "InstCombineInternal.h" 15 #include "llvm/Analysis/TargetTransformInfo.h" 16 #include "llvm/Analysis/ValueTracking.h" 17 #include "llvm/IR/GetElementPtrTypeIterator.h" 18 #include "llvm/IR/IntrinsicInst.h" 19 #include "llvm/IR/PatternMatch.h" 20 #include "llvm/Support/KnownBits.h" 21 #include "llvm/Transforms/InstCombine/InstCombiner.h" 22 23 using namespace llvm; 24 using namespace llvm::PatternMatch; 25 26 #define DEBUG_TYPE "instcombine" 27 28 /// Check to see if the specified operand of the specified instruction is a 29 /// constant integer. If so, check to see if there are any bits set in the 30 /// constant that are not demanded. If so, shrink the constant and return true. 31 static bool ShrinkDemandedConstant(Instruction *I, unsigned OpNo, 32 const APInt &Demanded) { 33 assert(I && "No instruction?"); 34 assert(OpNo < I->getNumOperands() && "Operand index too large"); 35 36 // The operand must be a constant integer or splat integer. 37 Value *Op = I->getOperand(OpNo); 38 const APInt *C; 39 if (!match(Op, m_APInt(C))) 40 return false; 41 42 // If there are no bits set that aren't demanded, nothing to do. 43 if (C->isSubsetOf(Demanded)) 44 return false; 45 46 // This instruction is producing bits that are not demanded. Shrink the RHS. 47 I->setOperand(OpNo, ConstantInt::get(Op->getType(), *C & Demanded)); 48 49 return true; 50 } 51 52 53 54 /// Inst is an integer instruction that SimplifyDemandedBits knows about. See if 55 /// the instruction has any properties that allow us to simplify its operands. 56 bool InstCombinerImpl::SimplifyDemandedInstructionBits(Instruction &Inst) { 57 unsigned BitWidth = Inst.getType()->getScalarSizeInBits(); 58 KnownBits Known(BitWidth); 59 APInt DemandedMask(APInt::getAllOnes(BitWidth)); 60 61 Value *V = SimplifyDemandedUseBits(&Inst, DemandedMask, Known, 62 0, &Inst); 63 if (!V) return false; 64 if (V == &Inst) return true; 65 replaceInstUsesWith(Inst, V); 66 return true; 67 } 68 69 /// This form of SimplifyDemandedBits simplifies the specified instruction 70 /// operand if possible, updating it in place. It returns true if it made any 71 /// change and false otherwise. 72 bool InstCombinerImpl::SimplifyDemandedBits(Instruction *I, unsigned OpNo, 73 const APInt &DemandedMask, 74 KnownBits &Known, unsigned Depth) { 75 Use &U = I->getOperandUse(OpNo); 76 Value *NewVal = SimplifyDemandedUseBits(U.get(), DemandedMask, Known, 77 Depth, I); 78 if (!NewVal) return false; 79 if (Instruction* OpInst = dyn_cast<Instruction>(U)) 80 salvageDebugInfo(*OpInst); 81 82 replaceUse(U, NewVal); 83 return true; 84 } 85 86 /// This function attempts to replace V with a simpler value based on the 87 /// demanded bits. When this function is called, it is known that only the bits 88 /// set in DemandedMask of the result of V are ever used downstream. 89 /// Consequently, depending on the mask and V, it may be possible to replace V 90 /// with a constant or one of its operands. In such cases, this function does 91 /// the replacement and returns true. In all other cases, it returns false after 92 /// analyzing the expression and setting KnownOne and known to be one in the 93 /// expression. Known.Zero contains all the bits that are known to be zero in 94 /// the expression. These are provided to potentially allow the caller (which 95 /// might recursively be SimplifyDemandedBits itself) to simplify the 96 /// expression. 97 /// Known.One and Known.Zero always follow the invariant that: 98 /// Known.One & Known.Zero == 0. 99 /// That is, a bit can't be both 1 and 0. Note that the bits in Known.One and 100 /// Known.Zero may only be accurate for those bits set in DemandedMask. Note 101 /// also that the bitwidth of V, DemandedMask, Known.Zero and Known.One must all 102 /// be the same. 103 /// 104 /// This returns null if it did not change anything and it permits no 105 /// simplification. This returns V itself if it did some simplification of V's 106 /// operands based on the information about what bits are demanded. This returns 107 /// some other non-null value if it found out that V is equal to another value 108 /// in the context where the specified bits are demanded, but not for all users. 109 Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, APInt DemandedMask, 110 KnownBits &Known, 111 unsigned Depth, 112 Instruction *CxtI) { 113 assert(V != nullptr && "Null pointer of Value???"); 114 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth"); 115 uint32_t BitWidth = DemandedMask.getBitWidth(); 116 Type *VTy = V->getType(); 117 assert( 118 (!VTy->isIntOrIntVectorTy() || VTy->getScalarSizeInBits() == BitWidth) && 119 Known.getBitWidth() == BitWidth && 120 "Value *V, DemandedMask and Known must have same BitWidth"); 121 122 if (isa<Constant>(V)) { 123 computeKnownBits(V, Known, Depth, CxtI); 124 return nullptr; 125 } 126 127 Known.resetAll(); 128 if (DemandedMask.isZero()) // Not demanding any bits from V. 129 return UndefValue::get(VTy); 130 131 if (Depth == MaxAnalysisRecursionDepth) 132 return nullptr; 133 134 if (isa<ScalableVectorType>(VTy)) 135 return nullptr; 136 137 Instruction *I = dyn_cast<Instruction>(V); 138 if (!I) { 139 computeKnownBits(V, Known, Depth, CxtI); 140 return nullptr; // Only analyze instructions. 141 } 142 143 // If there are multiple uses of this value and we aren't at the root, then 144 // we can't do any simplifications of the operands, because DemandedMask 145 // only reflects the bits demanded by *one* of the users. 146 if (Depth != 0 && !I->hasOneUse()) 147 return SimplifyMultipleUseDemandedBits(I, DemandedMask, Known, Depth, CxtI); 148 149 KnownBits LHSKnown(BitWidth), RHSKnown(BitWidth); 150 151 // If this is the root being simplified, allow it to have multiple uses, 152 // just set the DemandedMask to all bits so that we can try to simplify the 153 // operands. This allows visitTruncInst (for example) to simplify the 154 // operand of a trunc without duplicating all the logic below. 155 if (Depth == 0 && !V->hasOneUse()) 156 DemandedMask.setAllBits(); 157 158 // If the high-bits of an ADD/SUB/MUL are not demanded, then we do not care 159 // about the high bits of the operands. 160 auto simplifyOperandsBasedOnUnusedHighBits = [&](APInt &DemandedFromOps) { 161 unsigned NLZ = DemandedMask.countLeadingZeros(); 162 // Right fill the mask of bits for the operands to demand the most 163 // significant bit and all those below it. 164 DemandedFromOps = APInt::getLowBitsSet(BitWidth, BitWidth - NLZ); 165 if (ShrinkDemandedConstant(I, 0, DemandedFromOps) || 166 SimplifyDemandedBits(I, 0, DemandedFromOps, LHSKnown, Depth + 1) || 167 ShrinkDemandedConstant(I, 1, DemandedFromOps) || 168 SimplifyDemandedBits(I, 1, DemandedFromOps, RHSKnown, Depth + 1)) { 169 if (NLZ > 0) { 170 // Disable the nsw and nuw flags here: We can no longer guarantee that 171 // we won't wrap after simplification. Removing the nsw/nuw flags is 172 // legal here because the top bit is not demanded. 173 I->setHasNoSignedWrap(false); 174 I->setHasNoUnsignedWrap(false); 175 } 176 return true; 177 } 178 return false; 179 }; 180 181 switch (I->getOpcode()) { 182 default: 183 computeKnownBits(I, Known, Depth, CxtI); 184 break; 185 case Instruction::And: { 186 // If either the LHS or the RHS are Zero, the result is zero. 187 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) || 188 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.Zero, LHSKnown, 189 Depth + 1)) 190 return I; 191 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?"); 192 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?"); 193 194 Known = LHSKnown & RHSKnown; 195 196 // If the client is only demanding bits that we know, return the known 197 // constant. 198 if (DemandedMask.isSubsetOf(Known.Zero | Known.One)) 199 return Constant::getIntegerValue(VTy, Known.One); 200 201 // If all of the demanded bits are known 1 on one side, return the other. 202 // These bits cannot contribute to the result of the 'and'. 203 if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One)) 204 return I->getOperand(0); 205 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One)) 206 return I->getOperand(1); 207 208 // If the RHS is a constant, see if we can simplify it. 209 if (ShrinkDemandedConstant(I, 1, DemandedMask & ~LHSKnown.Zero)) 210 return I; 211 212 break; 213 } 214 case Instruction::Or: { 215 // If either the LHS or the RHS are One, the result is One. 216 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) || 217 SimplifyDemandedBits(I, 0, DemandedMask & ~RHSKnown.One, LHSKnown, 218 Depth + 1)) 219 return I; 220 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?"); 221 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?"); 222 223 Known = LHSKnown | RHSKnown; 224 225 // If the client is only demanding bits that we know, return the known 226 // constant. 227 if (DemandedMask.isSubsetOf(Known.Zero | Known.One)) 228 return Constant::getIntegerValue(VTy, Known.One); 229 230 // If all of the demanded bits are known zero on one side, return the other. 231 // These bits cannot contribute to the result of the 'or'. 232 if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero)) 233 return I->getOperand(0); 234 if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero)) 235 return I->getOperand(1); 236 237 // If the RHS is a constant, see if we can simplify it. 238 if (ShrinkDemandedConstant(I, 1, DemandedMask)) 239 return I; 240 241 break; 242 } 243 case Instruction::Xor: { 244 if (SimplifyDemandedBits(I, 1, DemandedMask, RHSKnown, Depth + 1) || 245 SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1)) 246 return I; 247 Value *LHS, *RHS; 248 if (DemandedMask == 1 && 249 match(I->getOperand(0), m_Intrinsic<Intrinsic::ctpop>(m_Value(LHS))) && 250 match(I->getOperand(1), m_Intrinsic<Intrinsic::ctpop>(m_Value(RHS)))) { 251 // (ctpop(X) ^ ctpop(Y)) & 1 --> ctpop(X^Y) & 1 252 IRBuilderBase::InsertPointGuard Guard(Builder); 253 Builder.SetInsertPoint(I); 254 auto *Xor = Builder.CreateXor(LHS, RHS); 255 return Builder.CreateUnaryIntrinsic(Intrinsic::ctpop, Xor); 256 } 257 258 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?"); 259 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?"); 260 261 Known = LHSKnown ^ RHSKnown; 262 263 // If the client is only demanding bits that we know, return the known 264 // constant. 265 if (DemandedMask.isSubsetOf(Known.Zero | Known.One)) 266 return Constant::getIntegerValue(VTy, Known.One); 267 268 // If all of the demanded bits are known zero on one side, return the other. 269 // These bits cannot contribute to the result of the 'xor'. 270 if (DemandedMask.isSubsetOf(RHSKnown.Zero)) 271 return I->getOperand(0); 272 if (DemandedMask.isSubsetOf(LHSKnown.Zero)) 273 return I->getOperand(1); 274 275 // If all of the demanded bits are known to be zero on one side or the 276 // other, turn this into an *inclusive* or. 277 // e.g. (A & C1)^(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0 278 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.Zero)) { 279 Instruction *Or = 280 BinaryOperator::CreateOr(I->getOperand(0), I->getOperand(1), 281 I->getName()); 282 return InsertNewInstWith(Or, *I); 283 } 284 285 // If all of the demanded bits on one side are known, and all of the set 286 // bits on that side are also known to be set on the other side, turn this 287 // into an AND, as we know the bits will be cleared. 288 // e.g. (X | C1) ^ C2 --> (X | C1) & ~C2 iff (C1&C2) == C2 289 if (DemandedMask.isSubsetOf(RHSKnown.Zero|RHSKnown.One) && 290 RHSKnown.One.isSubsetOf(LHSKnown.One)) { 291 Constant *AndC = Constant::getIntegerValue(VTy, 292 ~RHSKnown.One & DemandedMask); 293 Instruction *And = BinaryOperator::CreateAnd(I->getOperand(0), AndC); 294 return InsertNewInstWith(And, *I); 295 } 296 297 // If the RHS is a constant, see if we can change it. Don't alter a -1 298 // constant because that's a canonical 'not' op, and that is better for 299 // combining, SCEV, and codegen. 300 const APInt *C; 301 if (match(I->getOperand(1), m_APInt(C)) && !C->isAllOnes()) { 302 if ((*C | ~DemandedMask).isAllOnes()) { 303 // Force bits to 1 to create a 'not' op. 304 I->setOperand(1, ConstantInt::getAllOnesValue(VTy)); 305 return I; 306 } 307 // If we can't turn this into a 'not', try to shrink the constant. 308 if (ShrinkDemandedConstant(I, 1, DemandedMask)) 309 return I; 310 } 311 312 // If our LHS is an 'and' and if it has one use, and if any of the bits we 313 // are flipping are known to be set, then the xor is just resetting those 314 // bits to zero. We can just knock out bits from the 'and' and the 'xor', 315 // simplifying both of them. 316 if (Instruction *LHSInst = dyn_cast<Instruction>(I->getOperand(0))) { 317 ConstantInt *AndRHS, *XorRHS; 318 if (LHSInst->getOpcode() == Instruction::And && LHSInst->hasOneUse() && 319 match(I->getOperand(1), m_ConstantInt(XorRHS)) && 320 match(LHSInst->getOperand(1), m_ConstantInt(AndRHS)) && 321 (LHSKnown.One & RHSKnown.One & DemandedMask) != 0) { 322 APInt NewMask = ~(LHSKnown.One & RHSKnown.One & DemandedMask); 323 324 Constant *AndC = 325 ConstantInt::get(I->getType(), NewMask & AndRHS->getValue()); 326 Instruction *NewAnd = BinaryOperator::CreateAnd(I->getOperand(0), AndC); 327 InsertNewInstWith(NewAnd, *I); 328 329 Constant *XorC = 330 ConstantInt::get(I->getType(), NewMask & XorRHS->getValue()); 331 Instruction *NewXor = BinaryOperator::CreateXor(NewAnd, XorC); 332 return InsertNewInstWith(NewXor, *I); 333 } 334 } 335 break; 336 } 337 case Instruction::Select: { 338 if (SimplifyDemandedBits(I, 2, DemandedMask, RHSKnown, Depth + 1) || 339 SimplifyDemandedBits(I, 1, DemandedMask, LHSKnown, Depth + 1)) 340 return I; 341 assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?"); 342 assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?"); 343 344 // If the operands are constants, see if we can simplify them. 345 // This is similar to ShrinkDemandedConstant, but for a select we want to 346 // try to keep the selected constants the same as icmp value constants, if 347 // we can. This helps not break apart (or helps put back together) 348 // canonical patterns like min and max. 349 auto CanonicalizeSelectConstant = [](Instruction *I, unsigned OpNo, 350 const APInt &DemandedMask) { 351 const APInt *SelC; 352 if (!match(I->getOperand(OpNo), m_APInt(SelC))) 353 return false; 354 355 // Get the constant out of the ICmp, if there is one. 356 // Only try this when exactly 1 operand is a constant (if both operands 357 // are constant, the icmp should eventually simplify). Otherwise, we may 358 // invert the transform that reduces set bits and infinite-loop. 359 Value *X; 360 const APInt *CmpC; 361 ICmpInst::Predicate Pred; 362 if (!match(I->getOperand(0), m_ICmp(Pred, m_Value(X), m_APInt(CmpC))) || 363 isa<Constant>(X) || CmpC->getBitWidth() != SelC->getBitWidth()) 364 return ShrinkDemandedConstant(I, OpNo, DemandedMask); 365 366 // If the constant is already the same as the ICmp, leave it as-is. 367 if (*CmpC == *SelC) 368 return false; 369 // If the constants are not already the same, but can be with the demand 370 // mask, use the constant value from the ICmp. 371 if ((*CmpC & DemandedMask) == (*SelC & DemandedMask)) { 372 I->setOperand(OpNo, ConstantInt::get(I->getType(), *CmpC)); 373 return true; 374 } 375 return ShrinkDemandedConstant(I, OpNo, DemandedMask); 376 }; 377 if (CanonicalizeSelectConstant(I, 1, DemandedMask) || 378 CanonicalizeSelectConstant(I, 2, DemandedMask)) 379 return I; 380 381 // Only known if known in both the LHS and RHS. 382 Known = KnownBits::commonBits(LHSKnown, RHSKnown); 383 break; 384 } 385 case Instruction::Trunc: { 386 // If we do not demand the high bits of a right-shifted and truncated value, 387 // then we may be able to truncate it before the shift. 388 Value *X; 389 const APInt *C; 390 if (match(I->getOperand(0), m_OneUse(m_LShr(m_Value(X), m_APInt(C))))) { 391 // The shift amount must be valid (not poison) in the narrow type, and 392 // it must not be greater than the high bits demanded of the result. 393 if (C->ult(I->getType()->getScalarSizeInBits()) && 394 C->ule(DemandedMask.countLeadingZeros())) { 395 // trunc (lshr X, C) --> lshr (trunc X), C 396 IRBuilderBase::InsertPointGuard Guard(Builder); 397 Builder.SetInsertPoint(I); 398 Value *Trunc = Builder.CreateTrunc(X, I->getType()); 399 return Builder.CreateLShr(Trunc, C->getZExtValue()); 400 } 401 } 402 } 403 LLVM_FALLTHROUGH; 404 case Instruction::ZExt: { 405 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); 406 407 APInt InputDemandedMask = DemandedMask.zextOrTrunc(SrcBitWidth); 408 KnownBits InputKnown(SrcBitWidth); 409 if (SimplifyDemandedBits(I, 0, InputDemandedMask, InputKnown, Depth + 1)) 410 return I; 411 assert(InputKnown.getBitWidth() == SrcBitWidth && "Src width changed?"); 412 Known = InputKnown.zextOrTrunc(BitWidth); 413 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 414 break; 415 } 416 case Instruction::BitCast: 417 if (!I->getOperand(0)->getType()->isIntOrIntVectorTy()) 418 return nullptr; // vector->int or fp->int? 419 420 if (VectorType *DstVTy = dyn_cast<VectorType>(I->getType())) { 421 if (VectorType *SrcVTy = 422 dyn_cast<VectorType>(I->getOperand(0)->getType())) { 423 if (cast<FixedVectorType>(DstVTy)->getNumElements() != 424 cast<FixedVectorType>(SrcVTy)->getNumElements()) 425 // Don't touch a bitcast between vectors of different element counts. 426 return nullptr; 427 } else 428 // Don't touch a scalar-to-vector bitcast. 429 return nullptr; 430 } else if (I->getOperand(0)->getType()->isVectorTy()) 431 // Don't touch a vector-to-scalar bitcast. 432 return nullptr; 433 434 if (SimplifyDemandedBits(I, 0, DemandedMask, Known, Depth + 1)) 435 return I; 436 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 437 break; 438 case Instruction::SExt: { 439 // Compute the bits in the result that are not present in the input. 440 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits(); 441 442 APInt InputDemandedBits = DemandedMask.trunc(SrcBitWidth); 443 444 // If any of the sign extended bits are demanded, we know that the sign 445 // bit is demanded. 446 if (DemandedMask.getActiveBits() > SrcBitWidth) 447 InputDemandedBits.setBit(SrcBitWidth-1); 448 449 KnownBits InputKnown(SrcBitWidth); 450 if (SimplifyDemandedBits(I, 0, InputDemandedBits, InputKnown, Depth + 1)) 451 return I; 452 453 // If the input sign bit is known zero, or if the NewBits are not demanded 454 // convert this into a zero extension. 455 if (InputKnown.isNonNegative() || 456 DemandedMask.getActiveBits() <= SrcBitWidth) { 457 // Convert to ZExt cast. 458 CastInst *NewCast = new ZExtInst(I->getOperand(0), VTy, I->getName()); 459 return InsertNewInstWith(NewCast, *I); 460 } 461 462 // If the sign bit of the input is known set or clear, then we know the 463 // top bits of the result. 464 Known = InputKnown.sext(BitWidth); 465 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 466 break; 467 } 468 case Instruction::Add: 469 if ((DemandedMask & 1) == 0) { 470 // If we do not need the low bit, try to convert bool math to logic: 471 // add iN (zext i1 X), (sext i1 Y) --> sext (~X & Y) to iN 472 Value *X, *Y; 473 if (match(I, m_c_Add(m_OneUse(m_ZExt(m_Value(X))), 474 m_OneUse(m_SExt(m_Value(Y))))) && 475 X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) { 476 // Truth table for inputs and output signbits: 477 // X:0 | X:1 478 // ---------- 479 // Y:0 | 0 | 0 | 480 // Y:1 | -1 | 0 | 481 // ---------- 482 IRBuilderBase::InsertPointGuard Guard(Builder); 483 Builder.SetInsertPoint(I); 484 Value *AndNot = Builder.CreateAnd(Builder.CreateNot(X), Y); 485 return Builder.CreateSExt(AndNot, VTy); 486 } 487 488 // add iN (sext i1 X), (sext i1 Y) --> sext (X | Y) to iN 489 // TODO: Relax the one-use checks because we are removing an instruction? 490 if (match(I, m_Add(m_OneUse(m_SExt(m_Value(X))), 491 m_OneUse(m_SExt(m_Value(Y))))) && 492 X->getType()->isIntOrIntVectorTy(1) && X->getType() == Y->getType()) { 493 // Truth table for inputs and output signbits: 494 // X:0 | X:1 495 // ----------- 496 // Y:0 | -1 | -1 | 497 // Y:1 | -1 | 0 | 498 // ----------- 499 IRBuilderBase::InsertPointGuard Guard(Builder); 500 Builder.SetInsertPoint(I); 501 Value *Or = Builder.CreateOr(X, Y); 502 return Builder.CreateSExt(Or, VTy); 503 } 504 } 505 LLVM_FALLTHROUGH; 506 case Instruction::Sub: { 507 APInt DemandedFromOps; 508 if (simplifyOperandsBasedOnUnusedHighBits(DemandedFromOps)) 509 return I; 510 511 // If we are known to be adding/subtracting zeros to every bit below 512 // the highest demanded bit, we just return the other side. 513 if (DemandedFromOps.isSubsetOf(RHSKnown.Zero)) 514 return I->getOperand(0); 515 // We can't do this with the LHS for subtraction, unless we are only 516 // demanding the LSB. 517 if ((I->getOpcode() == Instruction::Add || DemandedFromOps.isOne()) && 518 DemandedFromOps.isSubsetOf(LHSKnown.Zero)) 519 return I->getOperand(1); 520 521 // Otherwise just compute the known bits of the result. 522 bool NSW = cast<OverflowingBinaryOperator>(I)->hasNoSignedWrap(); 523 Known = KnownBits::computeForAddSub(I->getOpcode() == Instruction::Add, 524 NSW, LHSKnown, RHSKnown); 525 break; 526 } 527 case Instruction::Mul: { 528 APInt DemandedFromOps; 529 if (simplifyOperandsBasedOnUnusedHighBits(DemandedFromOps)) 530 return I; 531 532 if (DemandedMask.isPowerOf2()) { 533 // The LSB of X*Y is set only if (X & 1) == 1 and (Y & 1) == 1. 534 // If we demand exactly one bit N and we have "X * (C' << N)" where C' is 535 // odd (has LSB set), then the left-shifted low bit of X is the answer. 536 unsigned CTZ = DemandedMask.countTrailingZeros(); 537 const APInt *C; 538 if (match(I->getOperand(1), m_APInt(C)) && 539 C->countTrailingZeros() == CTZ) { 540 Constant *ShiftC = ConstantInt::get(I->getType(), CTZ); 541 Instruction *Shl = BinaryOperator::CreateShl(I->getOperand(0), ShiftC); 542 return InsertNewInstWith(Shl, *I); 543 } 544 } 545 // For a squared value "X * X", the bottom 2 bits are 0 and X[0] because: 546 // X * X is odd iff X is odd. 547 // 'Quadratic Reciprocity': X * X -> 0 for bit[1] 548 if (I->getOperand(0) == I->getOperand(1) && DemandedMask.ult(4)) { 549 Constant *One = ConstantInt::get(VTy, 1); 550 Instruction *And1 = BinaryOperator::CreateAnd(I->getOperand(0), One); 551 return InsertNewInstWith(And1, *I); 552 } 553 554 computeKnownBits(I, Known, Depth, CxtI); 555 break; 556 } 557 case Instruction::Shl: { 558 const APInt *SA; 559 if (match(I->getOperand(1), m_APInt(SA))) { 560 const APInt *ShrAmt; 561 if (match(I->getOperand(0), m_Shr(m_Value(), m_APInt(ShrAmt)))) 562 if (Instruction *Shr = dyn_cast<Instruction>(I->getOperand(0))) 563 if (Value *R = simplifyShrShlDemandedBits(Shr, *ShrAmt, I, *SA, 564 DemandedMask, Known)) 565 return R; 566 567 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1); 568 APInt DemandedMaskIn(DemandedMask.lshr(ShiftAmt)); 569 570 // If the shift is NUW/NSW, then it does demand the high bits. 571 ShlOperator *IOp = cast<ShlOperator>(I); 572 if (IOp->hasNoSignedWrap()) 573 DemandedMaskIn.setHighBits(ShiftAmt+1); 574 else if (IOp->hasNoUnsignedWrap()) 575 DemandedMaskIn.setHighBits(ShiftAmt); 576 577 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1)) 578 return I; 579 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 580 581 bool SignBitZero = Known.Zero.isSignBitSet(); 582 bool SignBitOne = Known.One.isSignBitSet(); 583 Known.Zero <<= ShiftAmt; 584 Known.One <<= ShiftAmt; 585 // low bits known zero. 586 if (ShiftAmt) 587 Known.Zero.setLowBits(ShiftAmt); 588 589 // If this shift has "nsw" keyword, then the result is either a poison 590 // value or has the same sign bit as the first operand. 591 if (IOp->hasNoSignedWrap()) { 592 if (SignBitZero) 593 Known.Zero.setSignBit(); 594 else if (SignBitOne) 595 Known.One.setSignBit(); 596 if (Known.hasConflict()) 597 return UndefValue::get(I->getType()); 598 } 599 } else { 600 // This is a variable shift, so we can't shift the demand mask by a known 601 // amount. But if we are not demanding high bits, then we are not 602 // demanding those bits from the pre-shifted operand either. 603 if (unsigned CTLZ = DemandedMask.countLeadingZeros()) { 604 APInt DemandedFromOp(APInt::getLowBitsSet(BitWidth, BitWidth - CTLZ)); 605 if (SimplifyDemandedBits(I, 0, DemandedFromOp, Known, Depth + 1)) { 606 // We can't guarantee that nsw/nuw hold after simplifying the operand. 607 I->dropPoisonGeneratingFlags(); 608 return I; 609 } 610 } 611 computeKnownBits(I, Known, Depth, CxtI); 612 } 613 break; 614 } 615 case Instruction::LShr: { 616 const APInt *SA; 617 if (match(I->getOperand(1), m_APInt(SA))) { 618 uint64_t ShiftAmt = SA->getLimitedValue(BitWidth-1); 619 620 // Unsigned shift right. 621 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); 622 623 // If the shift is exact, then it does demand the low bits (and knows that 624 // they are zero). 625 if (cast<LShrOperator>(I)->isExact()) 626 DemandedMaskIn.setLowBits(ShiftAmt); 627 628 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1)) 629 return I; 630 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 631 Known.Zero.lshrInPlace(ShiftAmt); 632 Known.One.lshrInPlace(ShiftAmt); 633 if (ShiftAmt) 634 Known.Zero.setHighBits(ShiftAmt); // high bits known zero. 635 } else { 636 computeKnownBits(I, Known, Depth, CxtI); 637 } 638 break; 639 } 640 case Instruction::AShr: { 641 // If this is an arithmetic shift right and only the low-bit is set, we can 642 // always convert this into a logical shr, even if the shift amount is 643 // variable. The low bit of the shift cannot be an input sign bit unless 644 // the shift amount is >= the size of the datatype, which is undefined. 645 if (DemandedMask.isOne()) { 646 // Perform the logical shift right. 647 Instruction *NewVal = BinaryOperator::CreateLShr( 648 I->getOperand(0), I->getOperand(1), I->getName()); 649 return InsertNewInstWith(NewVal, *I); 650 } 651 652 // If the sign bit is the only bit demanded by this ashr, then there is no 653 // need to do it, the shift doesn't change the high bit. 654 if (DemandedMask.isSignMask()) 655 return I->getOperand(0); 656 657 const APInt *SA; 658 if (match(I->getOperand(1), m_APInt(SA))) { 659 uint32_t ShiftAmt = SA->getLimitedValue(BitWidth-1); 660 661 // Signed shift right. 662 APInt DemandedMaskIn(DemandedMask.shl(ShiftAmt)); 663 // If any of the high bits are demanded, we should set the sign bit as 664 // demanded. 665 if (DemandedMask.countLeadingZeros() <= ShiftAmt) 666 DemandedMaskIn.setSignBit(); 667 668 // If the shift is exact, then it does demand the low bits (and knows that 669 // they are zero). 670 if (cast<AShrOperator>(I)->isExact()) 671 DemandedMaskIn.setLowBits(ShiftAmt); 672 673 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, Known, Depth + 1)) 674 return I; 675 676 unsigned SignBits = ComputeNumSignBits(I->getOperand(0), Depth + 1, CxtI); 677 678 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 679 // Compute the new bits that are at the top now plus sign bits. 680 APInt HighBits(APInt::getHighBitsSet( 681 BitWidth, std::min(SignBits + ShiftAmt - 1, BitWidth))); 682 Known.Zero.lshrInPlace(ShiftAmt); 683 Known.One.lshrInPlace(ShiftAmt); 684 685 // If the input sign bit is known to be zero, or if none of the top bits 686 // are demanded, turn this into an unsigned shift right. 687 assert(BitWidth > ShiftAmt && "Shift amount not saturated?"); 688 if (Known.Zero[BitWidth-ShiftAmt-1] || 689 !DemandedMask.intersects(HighBits)) { 690 BinaryOperator *LShr = BinaryOperator::CreateLShr(I->getOperand(0), 691 I->getOperand(1)); 692 LShr->setIsExact(cast<BinaryOperator>(I)->isExact()); 693 return InsertNewInstWith(LShr, *I); 694 } else if (Known.One[BitWidth-ShiftAmt-1]) { // New bits are known one. 695 Known.One |= HighBits; 696 } 697 } else { 698 computeKnownBits(I, Known, Depth, CxtI); 699 } 700 break; 701 } 702 case Instruction::UDiv: { 703 // UDiv doesn't demand low bits that are zero in the divisor. 704 const APInt *SA; 705 if (match(I->getOperand(1), m_APInt(SA))) { 706 // If the shift is exact, then it does demand the low bits. 707 if (cast<UDivOperator>(I)->isExact()) 708 break; 709 710 // FIXME: Take the demanded mask of the result into account. 711 unsigned RHSTrailingZeros = SA->countTrailingZeros(); 712 APInt DemandedMaskIn = 713 APInt::getHighBitsSet(BitWidth, BitWidth - RHSTrailingZeros); 714 if (SimplifyDemandedBits(I, 0, DemandedMaskIn, LHSKnown, Depth + 1)) 715 return I; 716 717 // Propagate zero bits from the input. 718 Known.Zero.setHighBits(std::min( 719 BitWidth, LHSKnown.Zero.countLeadingOnes() + RHSTrailingZeros)); 720 } else { 721 computeKnownBits(I, Known, Depth, CxtI); 722 } 723 break; 724 } 725 case Instruction::SRem: { 726 ConstantInt *Rem; 727 if (match(I->getOperand(1), m_ConstantInt(Rem))) { 728 // X % -1 demands all the bits because we don't want to introduce 729 // INT_MIN % -1 (== undef) by accident. 730 if (Rem->isMinusOne()) 731 break; 732 APInt RA = Rem->getValue().abs(); 733 if (RA.isPowerOf2()) { 734 if (DemandedMask.ult(RA)) // srem won't affect demanded bits 735 return I->getOperand(0); 736 737 APInt LowBits = RA - 1; 738 APInt Mask2 = LowBits | APInt::getSignMask(BitWidth); 739 if (SimplifyDemandedBits(I, 0, Mask2, LHSKnown, Depth + 1)) 740 return I; 741 742 // The low bits of LHS are unchanged by the srem. 743 Known.Zero = LHSKnown.Zero & LowBits; 744 Known.One = LHSKnown.One & LowBits; 745 746 // If LHS is non-negative or has all low bits zero, then the upper bits 747 // are all zero. 748 if (LHSKnown.isNonNegative() || LowBits.isSubsetOf(LHSKnown.Zero)) 749 Known.Zero |= ~LowBits; 750 751 // If LHS is negative and not all low bits are zero, then the upper bits 752 // are all one. 753 if (LHSKnown.isNegative() && LowBits.intersects(LHSKnown.One)) 754 Known.One |= ~LowBits; 755 756 assert(!Known.hasConflict() && "Bits known to be one AND zero?"); 757 break; 758 } 759 } 760 761 // The sign bit is the LHS's sign bit, except when the result of the 762 // remainder is zero. 763 if (DemandedMask.isSignBitSet()) { 764 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, CxtI); 765 // If it's known zero, our sign bit is also zero. 766 if (LHSKnown.isNonNegative()) 767 Known.makeNonNegative(); 768 } 769 break; 770 } 771 case Instruction::URem: { 772 KnownBits Known2(BitWidth); 773 APInt AllOnes = APInt::getAllOnes(BitWidth); 774 if (SimplifyDemandedBits(I, 0, AllOnes, Known2, Depth + 1) || 775 SimplifyDemandedBits(I, 1, AllOnes, Known2, Depth + 1)) 776 return I; 777 778 unsigned Leaders = Known2.countMinLeadingZeros(); 779 Known.Zero = APInt::getHighBitsSet(BitWidth, Leaders) & DemandedMask; 780 break; 781 } 782 case Instruction::Call: { 783 bool KnownBitsComputed = false; 784 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) { 785 switch (II->getIntrinsicID()) { 786 case Intrinsic::abs: { 787 if (DemandedMask == 1) 788 return II->getArgOperand(0); 789 break; 790 } 791 case Intrinsic::ctpop: { 792 // Checking if the number of clear bits is odd (parity)? If the type has 793 // an even number of bits, that's the same as checking if the number of 794 // set bits is odd, so we can eliminate the 'not' op. 795 Value *X; 796 if (DemandedMask == 1 && VTy->getScalarSizeInBits() % 2 == 0 && 797 match(II->getArgOperand(0), m_Not(m_Value(X)))) { 798 Function *Ctpop = Intrinsic::getDeclaration( 799 II->getModule(), Intrinsic::ctpop, II->getType()); 800 return InsertNewInstWith(CallInst::Create(Ctpop, {X}), *I); 801 } 802 break; 803 } 804 case Intrinsic::bswap: { 805 // If the only bits demanded come from one byte of the bswap result, 806 // just shift the input byte into position to eliminate the bswap. 807 unsigned NLZ = DemandedMask.countLeadingZeros(); 808 unsigned NTZ = DemandedMask.countTrailingZeros(); 809 810 // Round NTZ down to the next byte. If we have 11 trailing zeros, then 811 // we need all the bits down to bit 8. Likewise, round NLZ. If we 812 // have 14 leading zeros, round to 8. 813 NLZ = alignDown(NLZ, 8); 814 NTZ = alignDown(NTZ, 8); 815 // If we need exactly one byte, we can do this transformation. 816 if (BitWidth - NLZ - NTZ == 8) { 817 // Replace this with either a left or right shift to get the byte into 818 // the right place. 819 Instruction *NewVal; 820 if (NLZ > NTZ) 821 NewVal = BinaryOperator::CreateLShr( 822 II->getArgOperand(0), 823 ConstantInt::get(I->getType(), NLZ - NTZ)); 824 else 825 NewVal = BinaryOperator::CreateShl( 826 II->getArgOperand(0), 827 ConstantInt::get(I->getType(), NTZ - NLZ)); 828 NewVal->takeName(I); 829 return InsertNewInstWith(NewVal, *I); 830 } 831 break; 832 } 833 case Intrinsic::fshr: 834 case Intrinsic::fshl: { 835 const APInt *SA; 836 if (!match(I->getOperand(2), m_APInt(SA))) 837 break; 838 839 // Normalize to funnel shift left. APInt shifts of BitWidth are well- 840 // defined, so no need to special-case zero shifts here. 841 uint64_t ShiftAmt = SA->urem(BitWidth); 842 if (II->getIntrinsicID() == Intrinsic::fshr) 843 ShiftAmt = BitWidth - ShiftAmt; 844 845 APInt DemandedMaskLHS(DemandedMask.lshr(ShiftAmt)); 846 APInt DemandedMaskRHS(DemandedMask.shl(BitWidth - ShiftAmt)); 847 if (SimplifyDemandedBits(I, 0, DemandedMaskLHS, LHSKnown, Depth + 1) || 848 SimplifyDemandedBits(I, 1, DemandedMaskRHS, RHSKnown, Depth + 1)) 849 return I; 850 851 Known.Zero = LHSKnown.Zero.shl(ShiftAmt) | 852 RHSKnown.Zero.lshr(BitWidth - ShiftAmt); 853 Known.One = LHSKnown.One.shl(ShiftAmt) | 854 RHSKnown.One.lshr(BitWidth - ShiftAmt); 855 KnownBitsComputed = true; 856 break; 857 } 858 case Intrinsic::umax: { 859 // UMax(A, C) == A if ... 860 // The lowest non-zero bit of DemandMask is higher than the highest 861 // non-zero bit of C. 862 const APInt *C; 863 unsigned CTZ = DemandedMask.countTrailingZeros(); 864 if (match(II->getArgOperand(1), m_APInt(C)) && 865 CTZ >= C->getActiveBits()) 866 return II->getArgOperand(0); 867 break; 868 } 869 case Intrinsic::umin: { 870 // UMin(A, C) == A if ... 871 // The lowest non-zero bit of DemandMask is higher than the highest 872 // non-one bit of C. 873 // This comes from using DeMorgans on the above umax example. 874 const APInt *C; 875 unsigned CTZ = DemandedMask.countTrailingZeros(); 876 if (match(II->getArgOperand(1), m_APInt(C)) && 877 CTZ >= C->getBitWidth() - C->countLeadingOnes()) 878 return II->getArgOperand(0); 879 break; 880 } 881 default: { 882 // Handle target specific intrinsics 883 Optional<Value *> V = targetSimplifyDemandedUseBitsIntrinsic( 884 *II, DemandedMask, Known, KnownBitsComputed); 885 if (V.hasValue()) 886 return V.getValue(); 887 break; 888 } 889 } 890 } 891 892 if (!KnownBitsComputed) 893 computeKnownBits(V, Known, Depth, CxtI); 894 break; 895 } 896 } 897 898 // If the client is only demanding bits that we know, return the known 899 // constant. 900 if (DemandedMask.isSubsetOf(Known.Zero|Known.One)) 901 return Constant::getIntegerValue(VTy, Known.One); 902 return nullptr; 903 } 904 905 /// Helper routine of SimplifyDemandedUseBits. It computes Known 906 /// bits. It also tries to handle simplifications that can be done based on 907 /// DemandedMask, but without modifying the Instruction. 908 Value *InstCombinerImpl::SimplifyMultipleUseDemandedBits( 909 Instruction *I, const APInt &DemandedMask, KnownBits &Known, unsigned Depth, 910 Instruction *CxtI) { 911 unsigned BitWidth = DemandedMask.getBitWidth(); 912 Type *ITy = I->getType(); 913 914 KnownBits LHSKnown(BitWidth); 915 KnownBits RHSKnown(BitWidth); 916 917 // Despite the fact that we can't simplify this instruction in all User's 918 // context, we can at least compute the known bits, and we can 919 // do simplifications that apply to *just* the one user if we know that 920 // this instruction has a simpler value in that context. 921 switch (I->getOpcode()) { 922 case Instruction::And: { 923 // If either the LHS or the RHS are Zero, the result is zero. 924 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI); 925 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, 926 CxtI); 927 928 Known = LHSKnown & RHSKnown; 929 930 // If the client is only demanding bits that we know, return the known 931 // constant. 932 if (DemandedMask.isSubsetOf(Known.Zero | Known.One)) 933 return Constant::getIntegerValue(ITy, Known.One); 934 935 // If all of the demanded bits are known 1 on one side, return the other. 936 // These bits cannot contribute to the result of the 'and' in this 937 // context. 938 if (DemandedMask.isSubsetOf(LHSKnown.Zero | RHSKnown.One)) 939 return I->getOperand(0); 940 if (DemandedMask.isSubsetOf(RHSKnown.Zero | LHSKnown.One)) 941 return I->getOperand(1); 942 943 break; 944 } 945 case Instruction::Or: { 946 // We can simplify (X|Y) -> X or Y in the user's context if we know that 947 // only bits from X or Y are demanded. 948 949 // If either the LHS or the RHS are One, the result is One. 950 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI); 951 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, 952 CxtI); 953 954 Known = LHSKnown | RHSKnown; 955 956 // If the client is only demanding bits that we know, return the known 957 // constant. 958 if (DemandedMask.isSubsetOf(Known.Zero | Known.One)) 959 return Constant::getIntegerValue(ITy, Known.One); 960 961 // If all of the demanded bits are known zero on one side, return the 962 // other. These bits cannot contribute to the result of the 'or' in this 963 // context. 964 if (DemandedMask.isSubsetOf(LHSKnown.One | RHSKnown.Zero)) 965 return I->getOperand(0); 966 if (DemandedMask.isSubsetOf(RHSKnown.One | LHSKnown.Zero)) 967 return I->getOperand(1); 968 969 break; 970 } 971 case Instruction::Xor: { 972 // We can simplify (X^Y) -> X or Y in the user's context if we know that 973 // only bits from X or Y are demanded. 974 975 computeKnownBits(I->getOperand(1), RHSKnown, Depth + 1, CxtI); 976 computeKnownBits(I->getOperand(0), LHSKnown, Depth + 1, 977 CxtI); 978 979 Known = LHSKnown ^ RHSKnown; 980 981 // If the client is only demanding bits that we know, return the known 982 // constant. 983 if (DemandedMask.isSubsetOf(Known.Zero | Known.One)) 984 return Constant::getIntegerValue(ITy, Known.One); 985 986 // If all of the demanded bits are known zero on one side, return the 987 // other. 988 if (DemandedMask.isSubsetOf(RHSKnown.Zero)) 989 return I->getOperand(0); 990 if (DemandedMask.isSubsetOf(LHSKnown.Zero)) 991 return I->getOperand(1); 992 993 break; 994 } 995 case Instruction::AShr: { 996 // Compute the Known bits to simplify things downstream. 997 computeKnownBits(I, Known, Depth, CxtI); 998 999 // If this user is only demanding bits that we know, return the known 1000 // constant. 1001 if (DemandedMask.isSubsetOf(Known.Zero | Known.One)) 1002 return Constant::getIntegerValue(ITy, Known.One); 1003 1004 // If the right shift operand 0 is a result of a left shift by the same 1005 // amount, this is probably a zero/sign extension, which may be unnecessary, 1006 // if we do not demand any of the new sign bits. So, return the original 1007 // operand instead. 1008 const APInt *ShiftRC; 1009 const APInt *ShiftLC; 1010 Value *X; 1011 unsigned BitWidth = DemandedMask.getBitWidth(); 1012 if (match(I, 1013 m_AShr(m_Shl(m_Value(X), m_APInt(ShiftLC)), m_APInt(ShiftRC))) && 1014 ShiftLC == ShiftRC && ShiftLC->ult(BitWidth) && 1015 DemandedMask.isSubsetOf(APInt::getLowBitsSet( 1016 BitWidth, BitWidth - ShiftRC->getZExtValue()))) { 1017 return X; 1018 } 1019 1020 break; 1021 } 1022 default: 1023 // Compute the Known bits to simplify things downstream. 1024 computeKnownBits(I, Known, Depth, CxtI); 1025 1026 // If this user is only demanding bits that we know, return the known 1027 // constant. 1028 if (DemandedMask.isSubsetOf(Known.Zero|Known.One)) 1029 return Constant::getIntegerValue(ITy, Known.One); 1030 1031 break; 1032 } 1033 1034 return nullptr; 1035 } 1036 1037 /// Helper routine of SimplifyDemandedUseBits. It tries to simplify 1038 /// "E1 = (X lsr C1) << C2", where the C1 and C2 are constant, into 1039 /// "E2 = X << (C2 - C1)" or "E2 = X >> (C1 - C2)", depending on the sign 1040 /// of "C2-C1". 1041 /// 1042 /// Suppose E1 and E2 are generally different in bits S={bm, bm+1, 1043 /// ..., bn}, without considering the specific value X is holding. 1044 /// This transformation is legal iff one of following conditions is hold: 1045 /// 1) All the bit in S are 0, in this case E1 == E2. 1046 /// 2) We don't care those bits in S, per the input DemandedMask. 1047 /// 3) Combination of 1) and 2). Some bits in S are 0, and we don't care the 1048 /// rest bits. 1049 /// 1050 /// Currently we only test condition 2). 1051 /// 1052 /// As with SimplifyDemandedUseBits, it returns NULL if the simplification was 1053 /// not successful. 1054 Value *InstCombinerImpl::simplifyShrShlDemandedBits( 1055 Instruction *Shr, const APInt &ShrOp1, Instruction *Shl, 1056 const APInt &ShlOp1, const APInt &DemandedMask, KnownBits &Known) { 1057 if (!ShlOp1 || !ShrOp1) 1058 return nullptr; // No-op. 1059 1060 Value *VarX = Shr->getOperand(0); 1061 Type *Ty = VarX->getType(); 1062 unsigned BitWidth = Ty->getScalarSizeInBits(); 1063 if (ShlOp1.uge(BitWidth) || ShrOp1.uge(BitWidth)) 1064 return nullptr; // Undef. 1065 1066 unsigned ShlAmt = ShlOp1.getZExtValue(); 1067 unsigned ShrAmt = ShrOp1.getZExtValue(); 1068 1069 Known.One.clearAllBits(); 1070 Known.Zero.setLowBits(ShlAmt - 1); 1071 Known.Zero &= DemandedMask; 1072 1073 APInt BitMask1(APInt::getAllOnes(BitWidth)); 1074 APInt BitMask2(APInt::getAllOnes(BitWidth)); 1075 1076 bool isLshr = (Shr->getOpcode() == Instruction::LShr); 1077 BitMask1 = isLshr ? (BitMask1.lshr(ShrAmt) << ShlAmt) : 1078 (BitMask1.ashr(ShrAmt) << ShlAmt); 1079 1080 if (ShrAmt <= ShlAmt) { 1081 BitMask2 <<= (ShlAmt - ShrAmt); 1082 } else { 1083 BitMask2 = isLshr ? BitMask2.lshr(ShrAmt - ShlAmt): 1084 BitMask2.ashr(ShrAmt - ShlAmt); 1085 } 1086 1087 // Check if condition-2 (see the comment to this function) is satified. 1088 if ((BitMask1 & DemandedMask) == (BitMask2 & DemandedMask)) { 1089 if (ShrAmt == ShlAmt) 1090 return VarX; 1091 1092 if (!Shr->hasOneUse()) 1093 return nullptr; 1094 1095 BinaryOperator *New; 1096 if (ShrAmt < ShlAmt) { 1097 Constant *Amt = ConstantInt::get(VarX->getType(), ShlAmt - ShrAmt); 1098 New = BinaryOperator::CreateShl(VarX, Amt); 1099 BinaryOperator *Orig = cast<BinaryOperator>(Shl); 1100 New->setHasNoSignedWrap(Orig->hasNoSignedWrap()); 1101 New->setHasNoUnsignedWrap(Orig->hasNoUnsignedWrap()); 1102 } else { 1103 Constant *Amt = ConstantInt::get(VarX->getType(), ShrAmt - ShlAmt); 1104 New = isLshr ? BinaryOperator::CreateLShr(VarX, Amt) : 1105 BinaryOperator::CreateAShr(VarX, Amt); 1106 if (cast<BinaryOperator>(Shr)->isExact()) 1107 New->setIsExact(true); 1108 } 1109 1110 return InsertNewInstWith(New, *Shl); 1111 } 1112 1113 return nullptr; 1114 } 1115 1116 /// The specified value produces a vector with any number of elements. 1117 /// This method analyzes which elements of the operand are undef or poison and 1118 /// returns that information in UndefElts. 1119 /// 1120 /// DemandedElts contains the set of elements that are actually used by the 1121 /// caller, and by default (AllowMultipleUsers equals false) the value is 1122 /// simplified only if it has a single caller. If AllowMultipleUsers is set 1123 /// to true, DemandedElts refers to the union of sets of elements that are 1124 /// used by all callers. 1125 /// 1126 /// If the information about demanded elements can be used to simplify the 1127 /// operation, the operation is simplified, then the resultant value is 1128 /// returned. This returns null if no change was made. 1129 Value *InstCombinerImpl::SimplifyDemandedVectorElts(Value *V, 1130 APInt DemandedElts, 1131 APInt &UndefElts, 1132 unsigned Depth, 1133 bool AllowMultipleUsers) { 1134 // Cannot analyze scalable type. The number of vector elements is not a 1135 // compile-time constant. 1136 if (isa<ScalableVectorType>(V->getType())) 1137 return nullptr; 1138 1139 unsigned VWidth = cast<FixedVectorType>(V->getType())->getNumElements(); 1140 APInt EltMask(APInt::getAllOnes(VWidth)); 1141 assert((DemandedElts & ~EltMask) == 0 && "Invalid DemandedElts!"); 1142 1143 if (match(V, m_Undef())) { 1144 // If the entire vector is undef or poison, just return this info. 1145 UndefElts = EltMask; 1146 return nullptr; 1147 } 1148 1149 if (DemandedElts.isZero()) { // If nothing is demanded, provide poison. 1150 UndefElts = EltMask; 1151 return PoisonValue::get(V->getType()); 1152 } 1153 1154 UndefElts = 0; 1155 1156 if (auto *C = dyn_cast<Constant>(V)) { 1157 // Check if this is identity. If so, return 0 since we are not simplifying 1158 // anything. 1159 if (DemandedElts.isAllOnes()) 1160 return nullptr; 1161 1162 Type *EltTy = cast<VectorType>(V->getType())->getElementType(); 1163 Constant *Poison = PoisonValue::get(EltTy); 1164 SmallVector<Constant*, 16> Elts; 1165 for (unsigned i = 0; i != VWidth; ++i) { 1166 if (!DemandedElts[i]) { // If not demanded, set to poison. 1167 Elts.push_back(Poison); 1168 UndefElts.setBit(i); 1169 continue; 1170 } 1171 1172 Constant *Elt = C->getAggregateElement(i); 1173 if (!Elt) return nullptr; 1174 1175 Elts.push_back(Elt); 1176 if (isa<UndefValue>(Elt)) // Already undef or poison. 1177 UndefElts.setBit(i); 1178 } 1179 1180 // If we changed the constant, return it. 1181 Constant *NewCV = ConstantVector::get(Elts); 1182 return NewCV != C ? NewCV : nullptr; 1183 } 1184 1185 // Limit search depth. 1186 if (Depth == 10) 1187 return nullptr; 1188 1189 if (!AllowMultipleUsers) { 1190 // If multiple users are using the root value, proceed with 1191 // simplification conservatively assuming that all elements 1192 // are needed. 1193 if (!V->hasOneUse()) { 1194 // Quit if we find multiple users of a non-root value though. 1195 // They'll be handled when it's their turn to be visited by 1196 // the main instcombine process. 1197 if (Depth != 0) 1198 // TODO: Just compute the UndefElts information recursively. 1199 return nullptr; 1200 1201 // Conservatively assume that all elements are needed. 1202 DemandedElts = EltMask; 1203 } 1204 } 1205 1206 Instruction *I = dyn_cast<Instruction>(V); 1207 if (!I) return nullptr; // Only analyze instructions. 1208 1209 bool MadeChange = false; 1210 auto simplifyAndSetOp = [&](Instruction *Inst, unsigned OpNum, 1211 APInt Demanded, APInt &Undef) { 1212 auto *II = dyn_cast<IntrinsicInst>(Inst); 1213 Value *Op = II ? II->getArgOperand(OpNum) : Inst->getOperand(OpNum); 1214 if (Value *V = SimplifyDemandedVectorElts(Op, Demanded, Undef, Depth + 1)) { 1215 replaceOperand(*Inst, OpNum, V); 1216 MadeChange = true; 1217 } 1218 }; 1219 1220 APInt UndefElts2(VWidth, 0); 1221 APInt UndefElts3(VWidth, 0); 1222 switch (I->getOpcode()) { 1223 default: break; 1224 1225 case Instruction::GetElementPtr: { 1226 // The LangRef requires that struct geps have all constant indices. As 1227 // such, we can't convert any operand to partial undef. 1228 auto mayIndexStructType = [](GetElementPtrInst &GEP) { 1229 for (auto I = gep_type_begin(GEP), E = gep_type_end(GEP); 1230 I != E; I++) 1231 if (I.isStruct()) 1232 return true; 1233 return false; 1234 }; 1235 if (mayIndexStructType(cast<GetElementPtrInst>(*I))) 1236 break; 1237 1238 // Conservatively track the demanded elements back through any vector 1239 // operands we may have. We know there must be at least one, or we 1240 // wouldn't have a vector result to get here. Note that we intentionally 1241 // merge the undef bits here since gepping with either an poison base or 1242 // index results in poison. 1243 for (unsigned i = 0; i < I->getNumOperands(); i++) { 1244 if (i == 0 ? match(I->getOperand(i), m_Undef()) 1245 : match(I->getOperand(i), m_Poison())) { 1246 // If the entire vector is undefined, just return this info. 1247 UndefElts = EltMask; 1248 return nullptr; 1249 } 1250 if (I->getOperand(i)->getType()->isVectorTy()) { 1251 APInt UndefEltsOp(VWidth, 0); 1252 simplifyAndSetOp(I, i, DemandedElts, UndefEltsOp); 1253 // gep(x, undef) is not undef, so skip considering idx ops here 1254 // Note that we could propagate poison, but we can't distinguish between 1255 // undef & poison bits ATM 1256 if (i == 0) 1257 UndefElts |= UndefEltsOp; 1258 } 1259 } 1260 1261 break; 1262 } 1263 case Instruction::InsertElement: { 1264 // If this is a variable index, we don't know which element it overwrites. 1265 // demand exactly the same input as we produce. 1266 ConstantInt *Idx = dyn_cast<ConstantInt>(I->getOperand(2)); 1267 if (!Idx) { 1268 // Note that we can't propagate undef elt info, because we don't know 1269 // which elt is getting updated. 1270 simplifyAndSetOp(I, 0, DemandedElts, UndefElts2); 1271 break; 1272 } 1273 1274 // The element inserted overwrites whatever was there, so the input demanded 1275 // set is simpler than the output set. 1276 unsigned IdxNo = Idx->getZExtValue(); 1277 APInt PreInsertDemandedElts = DemandedElts; 1278 if (IdxNo < VWidth) 1279 PreInsertDemandedElts.clearBit(IdxNo); 1280 1281 // If we only demand the element that is being inserted and that element 1282 // was extracted from the same index in another vector with the same type, 1283 // replace this insert with that other vector. 1284 // Note: This is attempted before the call to simplifyAndSetOp because that 1285 // may change UndefElts to a value that does not match with Vec. 1286 Value *Vec; 1287 if (PreInsertDemandedElts == 0 && 1288 match(I->getOperand(1), 1289 m_ExtractElt(m_Value(Vec), m_SpecificInt(IdxNo))) && 1290 Vec->getType() == I->getType()) { 1291 return Vec; 1292 } 1293 1294 simplifyAndSetOp(I, 0, PreInsertDemandedElts, UndefElts); 1295 1296 // If this is inserting an element that isn't demanded, remove this 1297 // insertelement. 1298 if (IdxNo >= VWidth || !DemandedElts[IdxNo]) { 1299 Worklist.push(I); 1300 return I->getOperand(0); 1301 } 1302 1303 // The inserted element is defined. 1304 UndefElts.clearBit(IdxNo); 1305 break; 1306 } 1307 case Instruction::ShuffleVector: { 1308 auto *Shuffle = cast<ShuffleVectorInst>(I); 1309 assert(Shuffle->getOperand(0)->getType() == 1310 Shuffle->getOperand(1)->getType() && 1311 "Expected shuffle operands to have same type"); 1312 unsigned OpWidth = cast<FixedVectorType>(Shuffle->getOperand(0)->getType()) 1313 ->getNumElements(); 1314 // Handle trivial case of a splat. Only check the first element of LHS 1315 // operand. 1316 if (all_of(Shuffle->getShuffleMask(), [](int Elt) { return Elt == 0; }) && 1317 DemandedElts.isAllOnes()) { 1318 if (!match(I->getOperand(1), m_Undef())) { 1319 I->setOperand(1, PoisonValue::get(I->getOperand(1)->getType())); 1320 MadeChange = true; 1321 } 1322 APInt LeftDemanded(OpWidth, 1); 1323 APInt LHSUndefElts(OpWidth, 0); 1324 simplifyAndSetOp(I, 0, LeftDemanded, LHSUndefElts); 1325 if (LHSUndefElts[0]) 1326 UndefElts = EltMask; 1327 else 1328 UndefElts.clearAllBits(); 1329 break; 1330 } 1331 1332 APInt LeftDemanded(OpWidth, 0), RightDemanded(OpWidth, 0); 1333 for (unsigned i = 0; i < VWidth; i++) { 1334 if (DemandedElts[i]) { 1335 unsigned MaskVal = Shuffle->getMaskValue(i); 1336 if (MaskVal != -1u) { 1337 assert(MaskVal < OpWidth * 2 && 1338 "shufflevector mask index out of range!"); 1339 if (MaskVal < OpWidth) 1340 LeftDemanded.setBit(MaskVal); 1341 else 1342 RightDemanded.setBit(MaskVal - OpWidth); 1343 } 1344 } 1345 } 1346 1347 APInt LHSUndefElts(OpWidth, 0); 1348 simplifyAndSetOp(I, 0, LeftDemanded, LHSUndefElts); 1349 1350 APInt RHSUndefElts(OpWidth, 0); 1351 simplifyAndSetOp(I, 1, RightDemanded, RHSUndefElts); 1352 1353 // If this shuffle does not change the vector length and the elements 1354 // demanded by this shuffle are an identity mask, then this shuffle is 1355 // unnecessary. 1356 // 1357 // We are assuming canonical form for the mask, so the source vector is 1358 // operand 0 and operand 1 is not used. 1359 // 1360 // Note that if an element is demanded and this shuffle mask is undefined 1361 // for that element, then the shuffle is not considered an identity 1362 // operation. The shuffle prevents poison from the operand vector from 1363 // leaking to the result by replacing poison with an undefined value. 1364 if (VWidth == OpWidth) { 1365 bool IsIdentityShuffle = true; 1366 for (unsigned i = 0; i < VWidth; i++) { 1367 unsigned MaskVal = Shuffle->getMaskValue(i); 1368 if (DemandedElts[i] && i != MaskVal) { 1369 IsIdentityShuffle = false; 1370 break; 1371 } 1372 } 1373 if (IsIdentityShuffle) 1374 return Shuffle->getOperand(0); 1375 } 1376 1377 bool NewUndefElts = false; 1378 unsigned LHSIdx = -1u, LHSValIdx = -1u; 1379 unsigned RHSIdx = -1u, RHSValIdx = -1u; 1380 bool LHSUniform = true; 1381 bool RHSUniform = true; 1382 for (unsigned i = 0; i < VWidth; i++) { 1383 unsigned MaskVal = Shuffle->getMaskValue(i); 1384 if (MaskVal == -1u) { 1385 UndefElts.setBit(i); 1386 } else if (!DemandedElts[i]) { 1387 NewUndefElts = true; 1388 UndefElts.setBit(i); 1389 } else if (MaskVal < OpWidth) { 1390 if (LHSUndefElts[MaskVal]) { 1391 NewUndefElts = true; 1392 UndefElts.setBit(i); 1393 } else { 1394 LHSIdx = LHSIdx == -1u ? i : OpWidth; 1395 LHSValIdx = LHSValIdx == -1u ? MaskVal : OpWidth; 1396 LHSUniform = LHSUniform && (MaskVal == i); 1397 } 1398 } else { 1399 if (RHSUndefElts[MaskVal - OpWidth]) { 1400 NewUndefElts = true; 1401 UndefElts.setBit(i); 1402 } else { 1403 RHSIdx = RHSIdx == -1u ? i : OpWidth; 1404 RHSValIdx = RHSValIdx == -1u ? MaskVal - OpWidth : OpWidth; 1405 RHSUniform = RHSUniform && (MaskVal - OpWidth == i); 1406 } 1407 } 1408 } 1409 1410 // Try to transform shuffle with constant vector and single element from 1411 // this constant vector to single insertelement instruction. 1412 // shufflevector V, C, <v1, v2, .., ci, .., vm> -> 1413 // insertelement V, C[ci], ci-n 1414 if (OpWidth == 1415 cast<FixedVectorType>(Shuffle->getType())->getNumElements()) { 1416 Value *Op = nullptr; 1417 Constant *Value = nullptr; 1418 unsigned Idx = -1u; 1419 1420 // Find constant vector with the single element in shuffle (LHS or RHS). 1421 if (LHSIdx < OpWidth && RHSUniform) { 1422 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(0))) { 1423 Op = Shuffle->getOperand(1); 1424 Value = CV->getOperand(LHSValIdx); 1425 Idx = LHSIdx; 1426 } 1427 } 1428 if (RHSIdx < OpWidth && LHSUniform) { 1429 if (auto *CV = dyn_cast<ConstantVector>(Shuffle->getOperand(1))) { 1430 Op = Shuffle->getOperand(0); 1431 Value = CV->getOperand(RHSValIdx); 1432 Idx = RHSIdx; 1433 } 1434 } 1435 // Found constant vector with single element - convert to insertelement. 1436 if (Op && Value) { 1437 Instruction *New = InsertElementInst::Create( 1438 Op, Value, ConstantInt::get(Type::getInt32Ty(I->getContext()), Idx), 1439 Shuffle->getName()); 1440 InsertNewInstWith(New, *Shuffle); 1441 return New; 1442 } 1443 } 1444 if (NewUndefElts) { 1445 // Add additional discovered undefs. 1446 SmallVector<int, 16> Elts; 1447 for (unsigned i = 0; i < VWidth; ++i) { 1448 if (UndefElts[i]) 1449 Elts.push_back(UndefMaskElem); 1450 else 1451 Elts.push_back(Shuffle->getMaskValue(i)); 1452 } 1453 Shuffle->setShuffleMask(Elts); 1454 MadeChange = true; 1455 } 1456 break; 1457 } 1458 case Instruction::Select: { 1459 // If this is a vector select, try to transform the select condition based 1460 // on the current demanded elements. 1461 SelectInst *Sel = cast<SelectInst>(I); 1462 if (Sel->getCondition()->getType()->isVectorTy()) { 1463 // TODO: We are not doing anything with UndefElts based on this call. 1464 // It is overwritten below based on the other select operands. If an 1465 // element of the select condition is known undef, then we are free to 1466 // choose the output value from either arm of the select. If we know that 1467 // one of those values is undef, then the output can be undef. 1468 simplifyAndSetOp(I, 0, DemandedElts, UndefElts); 1469 } 1470 1471 // Next, see if we can transform the arms of the select. 1472 APInt DemandedLHS(DemandedElts), DemandedRHS(DemandedElts); 1473 if (auto *CV = dyn_cast<ConstantVector>(Sel->getCondition())) { 1474 for (unsigned i = 0; i < VWidth; i++) { 1475 // isNullValue() always returns false when called on a ConstantExpr. 1476 // Skip constant expressions to avoid propagating incorrect information. 1477 Constant *CElt = CV->getAggregateElement(i); 1478 if (isa<ConstantExpr>(CElt)) 1479 continue; 1480 // TODO: If a select condition element is undef, we can demand from 1481 // either side. If one side is known undef, choosing that side would 1482 // propagate undef. 1483 if (CElt->isNullValue()) 1484 DemandedLHS.clearBit(i); 1485 else 1486 DemandedRHS.clearBit(i); 1487 } 1488 } 1489 1490 simplifyAndSetOp(I, 1, DemandedLHS, UndefElts2); 1491 simplifyAndSetOp(I, 2, DemandedRHS, UndefElts3); 1492 1493 // Output elements are undefined if the element from each arm is undefined. 1494 // TODO: This can be improved. See comment in select condition handling. 1495 UndefElts = UndefElts2 & UndefElts3; 1496 break; 1497 } 1498 case Instruction::BitCast: { 1499 // Vector->vector casts only. 1500 VectorType *VTy = dyn_cast<VectorType>(I->getOperand(0)->getType()); 1501 if (!VTy) break; 1502 unsigned InVWidth = cast<FixedVectorType>(VTy)->getNumElements(); 1503 APInt InputDemandedElts(InVWidth, 0); 1504 UndefElts2 = APInt(InVWidth, 0); 1505 unsigned Ratio; 1506 1507 if (VWidth == InVWidth) { 1508 // If we are converting from <4 x i32> -> <4 x f32>, we demand the same 1509 // elements as are demanded of us. 1510 Ratio = 1; 1511 InputDemandedElts = DemandedElts; 1512 } else if ((VWidth % InVWidth) == 0) { 1513 // If the number of elements in the output is a multiple of the number of 1514 // elements in the input then an input element is live if any of the 1515 // corresponding output elements are live. 1516 Ratio = VWidth / InVWidth; 1517 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) 1518 if (DemandedElts[OutIdx]) 1519 InputDemandedElts.setBit(OutIdx / Ratio); 1520 } else if ((InVWidth % VWidth) == 0) { 1521 // If the number of elements in the input is a multiple of the number of 1522 // elements in the output then an input element is live if the 1523 // corresponding output element is live. 1524 Ratio = InVWidth / VWidth; 1525 for (unsigned InIdx = 0; InIdx != InVWidth; ++InIdx) 1526 if (DemandedElts[InIdx / Ratio]) 1527 InputDemandedElts.setBit(InIdx); 1528 } else { 1529 // Unsupported so far. 1530 break; 1531 } 1532 1533 simplifyAndSetOp(I, 0, InputDemandedElts, UndefElts2); 1534 1535 if (VWidth == InVWidth) { 1536 UndefElts = UndefElts2; 1537 } else if ((VWidth % InVWidth) == 0) { 1538 // If the number of elements in the output is a multiple of the number of 1539 // elements in the input then an output element is undef if the 1540 // corresponding input element is undef. 1541 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) 1542 if (UndefElts2[OutIdx / Ratio]) 1543 UndefElts.setBit(OutIdx); 1544 } else if ((InVWidth % VWidth) == 0) { 1545 // If the number of elements in the input is a multiple of the number of 1546 // elements in the output then an output element is undef if all of the 1547 // corresponding input elements are undef. 1548 for (unsigned OutIdx = 0; OutIdx != VWidth; ++OutIdx) { 1549 APInt SubUndef = UndefElts2.lshr(OutIdx * Ratio).zextOrTrunc(Ratio); 1550 if (SubUndef.countPopulation() == Ratio) 1551 UndefElts.setBit(OutIdx); 1552 } 1553 } else { 1554 llvm_unreachable("Unimp"); 1555 } 1556 break; 1557 } 1558 case Instruction::FPTrunc: 1559 case Instruction::FPExt: 1560 simplifyAndSetOp(I, 0, DemandedElts, UndefElts); 1561 break; 1562 1563 case Instruction::Call: { 1564 IntrinsicInst *II = dyn_cast<IntrinsicInst>(I); 1565 if (!II) break; 1566 switch (II->getIntrinsicID()) { 1567 case Intrinsic::masked_gather: // fallthrough 1568 case Intrinsic::masked_load: { 1569 // Subtlety: If we load from a pointer, the pointer must be valid 1570 // regardless of whether the element is demanded. Doing otherwise risks 1571 // segfaults which didn't exist in the original program. 1572 APInt DemandedPtrs(APInt::getAllOnes(VWidth)), 1573 DemandedPassThrough(DemandedElts); 1574 if (auto *CV = dyn_cast<ConstantVector>(II->getOperand(2))) 1575 for (unsigned i = 0; i < VWidth; i++) { 1576 Constant *CElt = CV->getAggregateElement(i); 1577 if (CElt->isNullValue()) 1578 DemandedPtrs.clearBit(i); 1579 else if (CElt->isAllOnesValue()) 1580 DemandedPassThrough.clearBit(i); 1581 } 1582 if (II->getIntrinsicID() == Intrinsic::masked_gather) 1583 simplifyAndSetOp(II, 0, DemandedPtrs, UndefElts2); 1584 simplifyAndSetOp(II, 3, DemandedPassThrough, UndefElts3); 1585 1586 // Output elements are undefined if the element from both sources are. 1587 // TODO: can strengthen via mask as well. 1588 UndefElts = UndefElts2 & UndefElts3; 1589 break; 1590 } 1591 default: { 1592 // Handle target specific intrinsics 1593 Optional<Value *> V = targetSimplifyDemandedVectorEltsIntrinsic( 1594 *II, DemandedElts, UndefElts, UndefElts2, UndefElts3, 1595 simplifyAndSetOp); 1596 if (V.hasValue()) 1597 return V.getValue(); 1598 break; 1599 } 1600 } // switch on IntrinsicID 1601 break; 1602 } // case Call 1603 } // switch on Opcode 1604 1605 // TODO: We bail completely on integer div/rem and shifts because they have 1606 // UB/poison potential, but that should be refined. 1607 BinaryOperator *BO; 1608 if (match(I, m_BinOp(BO)) && !BO->isIntDivRem() && !BO->isShift()) { 1609 simplifyAndSetOp(I, 0, DemandedElts, UndefElts); 1610 simplifyAndSetOp(I, 1, DemandedElts, UndefElts2); 1611 1612 // Output elements are undefined if both are undefined. Consider things 1613 // like undef & 0. The result is known zero, not undef. 1614 UndefElts &= UndefElts2; 1615 } 1616 1617 // If we've proven all of the lanes undef, return an undef value. 1618 // TODO: Intersect w/demanded lanes 1619 if (UndefElts.isAllOnes()) 1620 return UndefValue::get(I->getType());; 1621 1622 return MadeChange ? I : nullptr; 1623 } 1624