1 //===- InstCombineCasts.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 implements the visit functions for cast operations. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "InstCombineInternal.h" 14 #include "llvm/ADT/SetVector.h" 15 #include "llvm/Analysis/ConstantFolding.h" 16 #include "llvm/IR/DataLayout.h" 17 #include "llvm/IR/DebugInfo.h" 18 #include "llvm/IR/PatternMatch.h" 19 #include "llvm/Support/KnownBits.h" 20 #include "llvm/Transforms/InstCombine/InstCombiner.h" 21 using namespace llvm; 22 using namespace PatternMatch; 23 24 #define DEBUG_TYPE "instcombine" 25 26 /// Analyze 'Val', seeing if it is a simple linear expression. 27 /// If so, decompose it, returning some value X, such that Val is 28 /// X*Scale+Offset. 29 /// 30 static Value *decomposeSimpleLinearExpr(Value *Val, unsigned &Scale, 31 uint64_t &Offset) { 32 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { 33 Offset = CI->getZExtValue(); 34 Scale = 0; 35 return ConstantInt::get(Val->getType(), 0); 36 } 37 38 if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { 39 // Cannot look past anything that might overflow. 40 // We specifically require nuw because we store the Scale in an unsigned 41 // and perform an unsigned divide on it. 42 OverflowingBinaryOperator *OBI = dyn_cast<OverflowingBinaryOperator>(Val); 43 if (OBI && !OBI->hasNoUnsignedWrap()) { 44 Scale = 1; 45 Offset = 0; 46 return Val; 47 } 48 49 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { 50 if (I->getOpcode() == Instruction::Shl) { 51 // This is a value scaled by '1 << the shift amt'. 52 Scale = UINT64_C(1) << RHS->getZExtValue(); 53 Offset = 0; 54 return I->getOperand(0); 55 } 56 57 if (I->getOpcode() == Instruction::Mul) { 58 // This value is scaled by 'RHS'. 59 Scale = RHS->getZExtValue(); 60 Offset = 0; 61 return I->getOperand(0); 62 } 63 64 if (I->getOpcode() == Instruction::Add) { 65 // We have X+C. Check to see if we really have (X*C2)+C1, 66 // where C1 is divisible by C2. 67 unsigned SubScale; 68 Value *SubVal = 69 decomposeSimpleLinearExpr(I->getOperand(0), SubScale, Offset); 70 Offset += RHS->getZExtValue(); 71 Scale = SubScale; 72 return SubVal; 73 } 74 } 75 } 76 77 // Otherwise, we can't look past this. 78 Scale = 1; 79 Offset = 0; 80 return Val; 81 } 82 83 /// If we find a cast of an allocation instruction, try to eliminate the cast by 84 /// moving the type information into the alloc. 85 Instruction *InstCombinerImpl::PromoteCastOfAllocation(BitCastInst &CI, 86 AllocaInst &AI) { 87 PointerType *PTy = cast<PointerType>(CI.getType()); 88 // Opaque pointers don't have an element type we could replace with. 89 if (PTy->isOpaque()) 90 return nullptr; 91 92 IRBuilderBase::InsertPointGuard Guard(Builder); 93 Builder.SetInsertPoint(&AI); 94 95 // Get the type really allocated and the type casted to. 96 Type *AllocElTy = AI.getAllocatedType(); 97 Type *CastElTy = PTy->getNonOpaquePointerElementType(); 98 if (!AllocElTy->isSized() || !CastElTy->isSized()) return nullptr; 99 100 // This optimisation does not work for cases where the cast type 101 // is scalable and the allocated type is not. This because we need to 102 // know how many times the casted type fits into the allocated type. 103 // For the opposite case where the allocated type is scalable and the 104 // cast type is not this leads to poor code quality due to the 105 // introduction of 'vscale' into the calculations. It seems better to 106 // bail out for this case too until we've done a proper cost-benefit 107 // analysis. 108 bool AllocIsScalable = isa<ScalableVectorType>(AllocElTy); 109 bool CastIsScalable = isa<ScalableVectorType>(CastElTy); 110 if (AllocIsScalable != CastIsScalable) return nullptr; 111 112 Align AllocElTyAlign = DL.getABITypeAlign(AllocElTy); 113 Align CastElTyAlign = DL.getABITypeAlign(CastElTy); 114 if (CastElTyAlign < AllocElTyAlign) return nullptr; 115 116 // If the allocation has multiple uses, only promote it if we are strictly 117 // increasing the alignment of the resultant allocation. If we keep it the 118 // same, we open the door to infinite loops of various kinds. 119 if (!AI.hasOneUse() && CastElTyAlign == AllocElTyAlign) return nullptr; 120 121 // The alloc and cast types should be either both fixed or both scalable. 122 uint64_t AllocElTySize = DL.getTypeAllocSize(AllocElTy).getKnownMinSize(); 123 uint64_t CastElTySize = DL.getTypeAllocSize(CastElTy).getKnownMinSize(); 124 if (CastElTySize == 0 || AllocElTySize == 0) return nullptr; 125 126 // If the allocation has multiple uses, only promote it if we're not 127 // shrinking the amount of memory being allocated. 128 uint64_t AllocElTyStoreSize = DL.getTypeStoreSize(AllocElTy).getKnownMinSize(); 129 uint64_t CastElTyStoreSize = DL.getTypeStoreSize(CastElTy).getKnownMinSize(); 130 if (!AI.hasOneUse() && CastElTyStoreSize < AllocElTyStoreSize) return nullptr; 131 132 // See if we can satisfy the modulus by pulling a scale out of the array 133 // size argument. 134 unsigned ArraySizeScale; 135 uint64_t ArrayOffset; 136 Value *NumElements = // See if the array size is a decomposable linear expr. 137 decomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); 138 139 // If we can now satisfy the modulus, by using a non-1 scale, we really can 140 // do the xform. 141 if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0 || 142 (AllocElTySize*ArrayOffset ) % CastElTySize != 0) return nullptr; 143 144 // We don't currently support arrays of scalable types. 145 assert(!AllocIsScalable || (ArrayOffset == 1 && ArraySizeScale == 0)); 146 147 unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; 148 Value *Amt = nullptr; 149 if (Scale == 1) { 150 Amt = NumElements; 151 } else { 152 Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale); 153 // Insert before the alloca, not before the cast. 154 Amt = Builder.CreateMul(Amt, NumElements); 155 } 156 157 if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { 158 Value *Off = ConstantInt::get(AI.getArraySize()->getType(), 159 Offset, true); 160 Amt = Builder.CreateAdd(Amt, Off); 161 } 162 163 AllocaInst *New = Builder.CreateAlloca(CastElTy, AI.getAddressSpace(), Amt); 164 New->setAlignment(AI.getAlign()); 165 New->takeName(&AI); 166 New->setUsedWithInAlloca(AI.isUsedWithInAlloca()); 167 New->setMetadata(LLVMContext::MD_DIAssignID, 168 AI.getMetadata(LLVMContext::MD_DIAssignID)); 169 170 replaceAllDbgUsesWith(AI, *New, *New, DT); 171 172 // If the allocation has multiple real uses, insert a cast and change all 173 // things that used it to use the new cast. This will also hack on CI, but it 174 // will die soon. 175 if (!AI.hasOneUse()) { 176 // New is the allocation instruction, pointer typed. AI is the original 177 // allocation instruction, also pointer typed. Thus, cast to use is BitCast. 178 Value *NewCast = Builder.CreateBitCast(New, AI.getType(), "tmpcast"); 179 replaceInstUsesWith(AI, NewCast); 180 eraseInstFromFunction(AI); 181 } 182 return replaceInstUsesWith(CI, New); 183 } 184 185 /// Given an expression that CanEvaluateTruncated or CanEvaluateSExtd returns 186 /// true for, actually insert the code to evaluate the expression. 187 Value *InstCombinerImpl::EvaluateInDifferentType(Value *V, Type *Ty, 188 bool isSigned) { 189 if (Constant *C = dyn_cast<Constant>(V)) { 190 C = ConstantExpr::getIntegerCast(C, Ty, isSigned /*Sext or ZExt*/); 191 // If we got a constantexpr back, try to simplify it with DL info. 192 return ConstantFoldConstant(C, DL, &TLI); 193 } 194 195 // Otherwise, it must be an instruction. 196 Instruction *I = cast<Instruction>(V); 197 Instruction *Res = nullptr; 198 unsigned Opc = I->getOpcode(); 199 switch (Opc) { 200 case Instruction::Add: 201 case Instruction::Sub: 202 case Instruction::Mul: 203 case Instruction::And: 204 case Instruction::Or: 205 case Instruction::Xor: 206 case Instruction::AShr: 207 case Instruction::LShr: 208 case Instruction::Shl: 209 case Instruction::UDiv: 210 case Instruction::URem: { 211 Value *LHS = EvaluateInDifferentType(I->getOperand(0), Ty, isSigned); 212 Value *RHS = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); 213 Res = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS); 214 break; 215 } 216 case Instruction::Trunc: 217 case Instruction::ZExt: 218 case Instruction::SExt: 219 // If the source type of the cast is the type we're trying for then we can 220 // just return the source. There's no need to insert it because it is not 221 // new. 222 if (I->getOperand(0)->getType() == Ty) 223 return I->getOperand(0); 224 225 // Otherwise, must be the same type of cast, so just reinsert a new one. 226 // This also handles the case of zext(trunc(x)) -> zext(x). 227 Res = CastInst::CreateIntegerCast(I->getOperand(0), Ty, 228 Opc == Instruction::SExt); 229 break; 230 case Instruction::Select: { 231 Value *True = EvaluateInDifferentType(I->getOperand(1), Ty, isSigned); 232 Value *False = EvaluateInDifferentType(I->getOperand(2), Ty, isSigned); 233 Res = SelectInst::Create(I->getOperand(0), True, False); 234 break; 235 } 236 case Instruction::PHI: { 237 PHINode *OPN = cast<PHINode>(I); 238 PHINode *NPN = PHINode::Create(Ty, OPN->getNumIncomingValues()); 239 for (unsigned i = 0, e = OPN->getNumIncomingValues(); i != e; ++i) { 240 Value *V = 241 EvaluateInDifferentType(OPN->getIncomingValue(i), Ty, isSigned); 242 NPN->addIncoming(V, OPN->getIncomingBlock(i)); 243 } 244 Res = NPN; 245 break; 246 } 247 default: 248 // TODO: Can handle more cases here. 249 llvm_unreachable("Unreachable!"); 250 } 251 252 Res->takeName(I); 253 return InsertNewInstWith(Res, *I); 254 } 255 256 Instruction::CastOps 257 InstCombinerImpl::isEliminableCastPair(const CastInst *CI1, 258 const CastInst *CI2) { 259 Type *SrcTy = CI1->getSrcTy(); 260 Type *MidTy = CI1->getDestTy(); 261 Type *DstTy = CI2->getDestTy(); 262 263 Instruction::CastOps firstOp = CI1->getOpcode(); 264 Instruction::CastOps secondOp = CI2->getOpcode(); 265 Type *SrcIntPtrTy = 266 SrcTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(SrcTy) : nullptr; 267 Type *MidIntPtrTy = 268 MidTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(MidTy) : nullptr; 269 Type *DstIntPtrTy = 270 DstTy->isPtrOrPtrVectorTy() ? DL.getIntPtrType(DstTy) : nullptr; 271 unsigned Res = CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, 272 DstTy, SrcIntPtrTy, MidIntPtrTy, 273 DstIntPtrTy); 274 275 // We don't want to form an inttoptr or ptrtoint that converts to an integer 276 // type that differs from the pointer size. 277 if ((Res == Instruction::IntToPtr && SrcTy != DstIntPtrTy) || 278 (Res == Instruction::PtrToInt && DstTy != SrcIntPtrTy)) 279 Res = 0; 280 281 return Instruction::CastOps(Res); 282 } 283 284 /// Implement the transforms common to all CastInst visitors. 285 Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) { 286 Value *Src = CI.getOperand(0); 287 Type *Ty = CI.getType(); 288 289 // Try to eliminate a cast of a cast. 290 if (auto *CSrc = dyn_cast<CastInst>(Src)) { // A->B->C cast 291 if (Instruction::CastOps NewOpc = isEliminableCastPair(CSrc, &CI)) { 292 // The first cast (CSrc) is eliminable so we need to fix up or replace 293 // the second cast (CI). CSrc will then have a good chance of being dead. 294 auto *Res = CastInst::Create(NewOpc, CSrc->getOperand(0), Ty); 295 // Point debug users of the dying cast to the new one. 296 if (CSrc->hasOneUse()) 297 replaceAllDbgUsesWith(*CSrc, *Res, CI, DT); 298 return Res; 299 } 300 } 301 302 if (auto *Sel = dyn_cast<SelectInst>(Src)) { 303 // We are casting a select. Try to fold the cast into the select if the 304 // select does not have a compare instruction with matching operand types 305 // or the select is likely better done in a narrow type. 306 // Creating a select with operands that are different sizes than its 307 // condition may inhibit other folds and lead to worse codegen. 308 auto *Cmp = dyn_cast<CmpInst>(Sel->getCondition()); 309 if (!Cmp || Cmp->getOperand(0)->getType() != Sel->getType() || 310 (CI.getOpcode() == Instruction::Trunc && 311 shouldChangeType(CI.getSrcTy(), CI.getType()))) { 312 if (Instruction *NV = FoldOpIntoSelect(CI, Sel)) { 313 replaceAllDbgUsesWith(*Sel, *NV, CI, DT); 314 return NV; 315 } 316 } 317 } 318 319 // If we are casting a PHI, then fold the cast into the PHI. 320 if (auto *PN = dyn_cast<PHINode>(Src)) { 321 // Don't do this if it would create a PHI node with an illegal type from a 322 // legal type. 323 if (!Src->getType()->isIntegerTy() || !CI.getType()->isIntegerTy() || 324 shouldChangeType(CI.getSrcTy(), CI.getType())) 325 if (Instruction *NV = foldOpIntoPhi(CI, PN)) 326 return NV; 327 } 328 329 // Canonicalize a unary shuffle after the cast if neither operation changes 330 // the size or element size of the input vector. 331 // TODO: We could allow size-changing ops if that doesn't harm codegen. 332 // cast (shuffle X, Mask) --> shuffle (cast X), Mask 333 Value *X; 334 ArrayRef<int> Mask; 335 if (match(Src, m_OneUse(m_Shuffle(m_Value(X), m_Undef(), m_Mask(Mask))))) { 336 // TODO: Allow scalable vectors? 337 auto *SrcTy = dyn_cast<FixedVectorType>(X->getType()); 338 auto *DestTy = dyn_cast<FixedVectorType>(Ty); 339 if (SrcTy && DestTy && 340 SrcTy->getNumElements() == DestTy->getNumElements() && 341 SrcTy->getPrimitiveSizeInBits() == DestTy->getPrimitiveSizeInBits()) { 342 Value *CastX = Builder.CreateCast(CI.getOpcode(), X, DestTy); 343 return new ShuffleVectorInst(CastX, Mask); 344 } 345 } 346 347 return nullptr; 348 } 349 350 /// Constants and extensions/truncates from the destination type are always 351 /// free to be evaluated in that type. This is a helper for canEvaluate*. 352 static bool canAlwaysEvaluateInType(Value *V, Type *Ty) { 353 if (isa<Constant>(V)) 354 return true; 355 Value *X; 356 if ((match(V, m_ZExtOrSExt(m_Value(X))) || match(V, m_Trunc(m_Value(X)))) && 357 X->getType() == Ty) 358 return true; 359 360 return false; 361 } 362 363 /// Filter out values that we can not evaluate in the destination type for free. 364 /// This is a helper for canEvaluate*. 365 static bool canNotEvaluateInType(Value *V, Type *Ty) { 366 assert(!isa<Constant>(V) && "Constant should already be handled."); 367 if (!isa<Instruction>(V)) 368 return true; 369 // We don't extend or shrink something that has multiple uses -- doing so 370 // would require duplicating the instruction which isn't profitable. 371 if (!V->hasOneUse()) 372 return true; 373 374 return false; 375 } 376 377 /// Return true if we can evaluate the specified expression tree as type Ty 378 /// instead of its larger type, and arrive with the same value. 379 /// This is used by code that tries to eliminate truncates. 380 /// 381 /// Ty will always be a type smaller than V. We should return true if trunc(V) 382 /// can be computed by computing V in the smaller type. If V is an instruction, 383 /// then trunc(inst(x,y)) can be computed as inst(trunc(x),trunc(y)), which only 384 /// makes sense if x and y can be efficiently truncated. 385 /// 386 /// This function works on both vectors and scalars. 387 /// 388 static bool canEvaluateTruncated(Value *V, Type *Ty, InstCombinerImpl &IC, 389 Instruction *CxtI) { 390 if (canAlwaysEvaluateInType(V, Ty)) 391 return true; 392 if (canNotEvaluateInType(V, Ty)) 393 return false; 394 395 auto *I = cast<Instruction>(V); 396 Type *OrigTy = V->getType(); 397 switch (I->getOpcode()) { 398 case Instruction::Add: 399 case Instruction::Sub: 400 case Instruction::Mul: 401 case Instruction::And: 402 case Instruction::Or: 403 case Instruction::Xor: 404 // These operators can all arbitrarily be extended or truncated. 405 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) && 406 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI); 407 408 case Instruction::UDiv: 409 case Instruction::URem: { 410 // UDiv and URem can be truncated if all the truncated bits are zero. 411 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); 412 uint32_t BitWidth = Ty->getScalarSizeInBits(); 413 assert(BitWidth < OrigBitWidth && "Unexpected bitwidths!"); 414 APInt Mask = APInt::getBitsSetFrom(OrigBitWidth, BitWidth); 415 if (IC.MaskedValueIsZero(I->getOperand(0), Mask, 0, CxtI) && 416 IC.MaskedValueIsZero(I->getOperand(1), Mask, 0, CxtI)) { 417 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) && 418 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI); 419 } 420 break; 421 } 422 case Instruction::Shl: { 423 // If we are truncating the result of this SHL, and if it's a shift of an 424 // inrange amount, we can always perform a SHL in a smaller type. 425 uint32_t BitWidth = Ty->getScalarSizeInBits(); 426 KnownBits AmtKnownBits = 427 llvm::computeKnownBits(I->getOperand(1), IC.getDataLayout()); 428 if (AmtKnownBits.getMaxValue().ult(BitWidth)) 429 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) && 430 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI); 431 break; 432 } 433 case Instruction::LShr: { 434 // If this is a truncate of a logical shr, we can truncate it to a smaller 435 // lshr iff we know that the bits we would otherwise be shifting in are 436 // already zeros. 437 // TODO: It is enough to check that the bits we would be shifting in are 438 // zero - use AmtKnownBits.getMaxValue(). 439 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); 440 uint32_t BitWidth = Ty->getScalarSizeInBits(); 441 KnownBits AmtKnownBits = 442 llvm::computeKnownBits(I->getOperand(1), IC.getDataLayout()); 443 APInt ShiftedBits = APInt::getBitsSetFrom(OrigBitWidth, BitWidth); 444 if (AmtKnownBits.getMaxValue().ult(BitWidth) && 445 IC.MaskedValueIsZero(I->getOperand(0), ShiftedBits, 0, CxtI)) { 446 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) && 447 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI); 448 } 449 break; 450 } 451 case Instruction::AShr: { 452 // If this is a truncate of an arithmetic shr, we can truncate it to a 453 // smaller ashr iff we know that all the bits from the sign bit of the 454 // original type and the sign bit of the truncate type are similar. 455 // TODO: It is enough to check that the bits we would be shifting in are 456 // similar to sign bit of the truncate type. 457 uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); 458 uint32_t BitWidth = Ty->getScalarSizeInBits(); 459 KnownBits AmtKnownBits = 460 llvm::computeKnownBits(I->getOperand(1), IC.getDataLayout()); 461 unsigned ShiftedBits = OrigBitWidth - BitWidth; 462 if (AmtKnownBits.getMaxValue().ult(BitWidth) && 463 ShiftedBits < IC.ComputeNumSignBits(I->getOperand(0), 0, CxtI)) 464 return canEvaluateTruncated(I->getOperand(0), Ty, IC, CxtI) && 465 canEvaluateTruncated(I->getOperand(1), Ty, IC, CxtI); 466 break; 467 } 468 case Instruction::Trunc: 469 // trunc(trunc(x)) -> trunc(x) 470 return true; 471 case Instruction::ZExt: 472 case Instruction::SExt: 473 // trunc(ext(x)) -> ext(x) if the source type is smaller than the new dest 474 // trunc(ext(x)) -> trunc(x) if the source type is larger than the new dest 475 return true; 476 case Instruction::Select: { 477 SelectInst *SI = cast<SelectInst>(I); 478 return canEvaluateTruncated(SI->getTrueValue(), Ty, IC, CxtI) && 479 canEvaluateTruncated(SI->getFalseValue(), Ty, IC, CxtI); 480 } 481 case Instruction::PHI: { 482 // We can change a phi if we can change all operands. Note that we never 483 // get into trouble with cyclic PHIs here because we only consider 484 // instructions with a single use. 485 PHINode *PN = cast<PHINode>(I); 486 for (Value *IncValue : PN->incoming_values()) 487 if (!canEvaluateTruncated(IncValue, Ty, IC, CxtI)) 488 return false; 489 return true; 490 } 491 default: 492 // TODO: Can handle more cases here. 493 break; 494 } 495 496 return false; 497 } 498 499 /// Given a vector that is bitcast to an integer, optionally logically 500 /// right-shifted, and truncated, convert it to an extractelement. 501 /// Example (big endian): 502 /// trunc (lshr (bitcast <4 x i32> %X to i128), 32) to i32 503 /// ---> 504 /// extractelement <4 x i32> %X, 1 505 static Instruction *foldVecTruncToExtElt(TruncInst &Trunc, 506 InstCombinerImpl &IC) { 507 Value *TruncOp = Trunc.getOperand(0); 508 Type *DestType = Trunc.getType(); 509 if (!TruncOp->hasOneUse() || !isa<IntegerType>(DestType)) 510 return nullptr; 511 512 Value *VecInput = nullptr; 513 ConstantInt *ShiftVal = nullptr; 514 if (!match(TruncOp, m_CombineOr(m_BitCast(m_Value(VecInput)), 515 m_LShr(m_BitCast(m_Value(VecInput)), 516 m_ConstantInt(ShiftVal)))) || 517 !isa<VectorType>(VecInput->getType())) 518 return nullptr; 519 520 VectorType *VecType = cast<VectorType>(VecInput->getType()); 521 unsigned VecWidth = VecType->getPrimitiveSizeInBits(); 522 unsigned DestWidth = DestType->getPrimitiveSizeInBits(); 523 unsigned ShiftAmount = ShiftVal ? ShiftVal->getZExtValue() : 0; 524 525 if ((VecWidth % DestWidth != 0) || (ShiftAmount % DestWidth != 0)) 526 return nullptr; 527 528 // If the element type of the vector doesn't match the result type, 529 // bitcast it to a vector type that we can extract from. 530 unsigned NumVecElts = VecWidth / DestWidth; 531 if (VecType->getElementType() != DestType) { 532 VecType = FixedVectorType::get(DestType, NumVecElts); 533 VecInput = IC.Builder.CreateBitCast(VecInput, VecType, "bc"); 534 } 535 536 unsigned Elt = ShiftAmount / DestWidth; 537 if (IC.getDataLayout().isBigEndian()) 538 Elt = NumVecElts - 1 - Elt; 539 540 return ExtractElementInst::Create(VecInput, IC.Builder.getInt32(Elt)); 541 } 542 543 /// Funnel/Rotate left/right may occur in a wider type than necessary because of 544 /// type promotion rules. Try to narrow the inputs and convert to funnel shift. 545 Instruction *InstCombinerImpl::narrowFunnelShift(TruncInst &Trunc) { 546 assert((isa<VectorType>(Trunc.getSrcTy()) || 547 shouldChangeType(Trunc.getSrcTy(), Trunc.getType())) && 548 "Don't narrow to an illegal scalar type"); 549 550 // Bail out on strange types. It is possible to handle some of these patterns 551 // even with non-power-of-2 sizes, but it is not a likely scenario. 552 Type *DestTy = Trunc.getType(); 553 unsigned NarrowWidth = DestTy->getScalarSizeInBits(); 554 unsigned WideWidth = Trunc.getSrcTy()->getScalarSizeInBits(); 555 if (!isPowerOf2_32(NarrowWidth)) 556 return nullptr; 557 558 // First, find an or'd pair of opposite shifts: 559 // trunc (or (lshr ShVal0, ShAmt0), (shl ShVal1, ShAmt1)) 560 BinaryOperator *Or0, *Or1; 561 if (!match(Trunc.getOperand(0), m_OneUse(m_Or(m_BinOp(Or0), m_BinOp(Or1))))) 562 return nullptr; 563 564 Value *ShVal0, *ShVal1, *ShAmt0, *ShAmt1; 565 if (!match(Or0, m_OneUse(m_LogicalShift(m_Value(ShVal0), m_Value(ShAmt0)))) || 566 !match(Or1, m_OneUse(m_LogicalShift(m_Value(ShVal1), m_Value(ShAmt1)))) || 567 Or0->getOpcode() == Or1->getOpcode()) 568 return nullptr; 569 570 // Canonicalize to or(shl(ShVal0, ShAmt0), lshr(ShVal1, ShAmt1)). 571 if (Or0->getOpcode() == BinaryOperator::LShr) { 572 std::swap(Or0, Or1); 573 std::swap(ShVal0, ShVal1); 574 std::swap(ShAmt0, ShAmt1); 575 } 576 assert(Or0->getOpcode() == BinaryOperator::Shl && 577 Or1->getOpcode() == BinaryOperator::LShr && 578 "Illegal or(shift,shift) pair"); 579 580 // Match the shift amount operands for a funnel/rotate pattern. This always 581 // matches a subtraction on the R operand. 582 auto matchShiftAmount = [&](Value *L, Value *R, unsigned Width) -> Value * { 583 // The shift amounts may add up to the narrow bit width: 584 // (shl ShVal0, L) | (lshr ShVal1, Width - L) 585 // If this is a funnel shift (different operands are shifted), then the 586 // shift amount can not over-shift (create poison) in the narrow type. 587 unsigned MaxShiftAmountWidth = Log2_32(NarrowWidth); 588 APInt HiBitMask = ~APInt::getLowBitsSet(WideWidth, MaxShiftAmountWidth); 589 if (ShVal0 == ShVal1 || MaskedValueIsZero(L, HiBitMask)) 590 if (match(R, m_OneUse(m_Sub(m_SpecificInt(Width), m_Specific(L))))) 591 return L; 592 593 // The following patterns currently only work for rotation patterns. 594 // TODO: Add more general funnel-shift compatible patterns. 595 if (ShVal0 != ShVal1) 596 return nullptr; 597 598 // The shift amount may be masked with negation: 599 // (shl ShVal0, (X & (Width - 1))) | (lshr ShVal1, ((-X) & (Width - 1))) 600 Value *X; 601 unsigned Mask = Width - 1; 602 if (match(L, m_And(m_Value(X), m_SpecificInt(Mask))) && 603 match(R, m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask)))) 604 return X; 605 606 // Same as above, but the shift amount may be extended after masking: 607 if (match(L, m_ZExt(m_And(m_Value(X), m_SpecificInt(Mask)))) && 608 match(R, m_ZExt(m_And(m_Neg(m_Specific(X)), m_SpecificInt(Mask))))) 609 return X; 610 611 return nullptr; 612 }; 613 614 Value *ShAmt = matchShiftAmount(ShAmt0, ShAmt1, NarrowWidth); 615 bool IsFshl = true; // Sub on LSHR. 616 if (!ShAmt) { 617 ShAmt = matchShiftAmount(ShAmt1, ShAmt0, NarrowWidth); 618 IsFshl = false; // Sub on SHL. 619 } 620 if (!ShAmt) 621 return nullptr; 622 623 // The right-shifted value must have high zeros in the wide type (for example 624 // from 'zext', 'and' or 'shift'). High bits of the left-shifted value are 625 // truncated, so those do not matter. 626 APInt HiBitMask = APInt::getHighBitsSet(WideWidth, WideWidth - NarrowWidth); 627 if (!MaskedValueIsZero(ShVal1, HiBitMask, 0, &Trunc)) 628 return nullptr; 629 630 // We have an unnecessarily wide rotate! 631 // trunc (or (shl ShVal0, ShAmt), (lshr ShVal1, BitWidth - ShAmt)) 632 // Narrow the inputs and convert to funnel shift intrinsic: 633 // llvm.fshl.i8(trunc(ShVal), trunc(ShVal), trunc(ShAmt)) 634 Value *NarrowShAmt = Builder.CreateTrunc(ShAmt, DestTy); 635 Value *X, *Y; 636 X = Y = Builder.CreateTrunc(ShVal0, DestTy); 637 if (ShVal0 != ShVal1) 638 Y = Builder.CreateTrunc(ShVal1, DestTy); 639 Intrinsic::ID IID = IsFshl ? Intrinsic::fshl : Intrinsic::fshr; 640 Function *F = Intrinsic::getDeclaration(Trunc.getModule(), IID, DestTy); 641 return CallInst::Create(F, {X, Y, NarrowShAmt}); 642 } 643 644 /// Try to narrow the width of math or bitwise logic instructions by pulling a 645 /// truncate ahead of binary operators. 646 Instruction *InstCombinerImpl::narrowBinOp(TruncInst &Trunc) { 647 Type *SrcTy = Trunc.getSrcTy(); 648 Type *DestTy = Trunc.getType(); 649 unsigned SrcWidth = SrcTy->getScalarSizeInBits(); 650 unsigned DestWidth = DestTy->getScalarSizeInBits(); 651 652 if (!isa<VectorType>(SrcTy) && !shouldChangeType(SrcTy, DestTy)) 653 return nullptr; 654 655 BinaryOperator *BinOp; 656 if (!match(Trunc.getOperand(0), m_OneUse(m_BinOp(BinOp)))) 657 return nullptr; 658 659 Value *BinOp0 = BinOp->getOperand(0); 660 Value *BinOp1 = BinOp->getOperand(1); 661 switch (BinOp->getOpcode()) { 662 case Instruction::And: 663 case Instruction::Or: 664 case Instruction::Xor: 665 case Instruction::Add: 666 case Instruction::Sub: 667 case Instruction::Mul: { 668 Constant *C; 669 if (match(BinOp0, m_Constant(C))) { 670 // trunc (binop C, X) --> binop (trunc C', X) 671 Constant *NarrowC = ConstantExpr::getTrunc(C, DestTy); 672 Value *TruncX = Builder.CreateTrunc(BinOp1, DestTy); 673 return BinaryOperator::Create(BinOp->getOpcode(), NarrowC, TruncX); 674 } 675 if (match(BinOp1, m_Constant(C))) { 676 // trunc (binop X, C) --> binop (trunc X, C') 677 Constant *NarrowC = ConstantExpr::getTrunc(C, DestTy); 678 Value *TruncX = Builder.CreateTrunc(BinOp0, DestTy); 679 return BinaryOperator::Create(BinOp->getOpcode(), TruncX, NarrowC); 680 } 681 Value *X; 682 if (match(BinOp0, m_ZExtOrSExt(m_Value(X))) && X->getType() == DestTy) { 683 // trunc (binop (ext X), Y) --> binop X, (trunc Y) 684 Value *NarrowOp1 = Builder.CreateTrunc(BinOp1, DestTy); 685 return BinaryOperator::Create(BinOp->getOpcode(), X, NarrowOp1); 686 } 687 if (match(BinOp1, m_ZExtOrSExt(m_Value(X))) && X->getType() == DestTy) { 688 // trunc (binop Y, (ext X)) --> binop (trunc Y), X 689 Value *NarrowOp0 = Builder.CreateTrunc(BinOp0, DestTy); 690 return BinaryOperator::Create(BinOp->getOpcode(), NarrowOp0, X); 691 } 692 break; 693 } 694 case Instruction::LShr: 695 case Instruction::AShr: { 696 // trunc (*shr (trunc A), C) --> trunc(*shr A, C) 697 Value *A; 698 Constant *C; 699 if (match(BinOp0, m_Trunc(m_Value(A))) && match(BinOp1, m_Constant(C))) { 700 unsigned MaxShiftAmt = SrcWidth - DestWidth; 701 // If the shift is small enough, all zero/sign bits created by the shift 702 // are removed by the trunc. 703 if (match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULE, 704 APInt(SrcWidth, MaxShiftAmt)))) { 705 auto *OldShift = cast<Instruction>(Trunc.getOperand(0)); 706 bool IsExact = OldShift->isExact(); 707 auto *ShAmt = ConstantExpr::getIntegerCast(C, A->getType(), true); 708 ShAmt = Constant::mergeUndefsWith(ShAmt, C); 709 Value *Shift = 710 OldShift->getOpcode() == Instruction::AShr 711 ? Builder.CreateAShr(A, ShAmt, OldShift->getName(), IsExact) 712 : Builder.CreateLShr(A, ShAmt, OldShift->getName(), IsExact); 713 return CastInst::CreateTruncOrBitCast(Shift, DestTy); 714 } 715 } 716 break; 717 } 718 default: break; 719 } 720 721 if (Instruction *NarrowOr = narrowFunnelShift(Trunc)) 722 return NarrowOr; 723 724 return nullptr; 725 } 726 727 /// Try to narrow the width of a splat shuffle. This could be generalized to any 728 /// shuffle with a constant operand, but we limit the transform to avoid 729 /// creating a shuffle type that targets may not be able to lower effectively. 730 static Instruction *shrinkSplatShuffle(TruncInst &Trunc, 731 InstCombiner::BuilderTy &Builder) { 732 auto *Shuf = dyn_cast<ShuffleVectorInst>(Trunc.getOperand(0)); 733 if (Shuf && Shuf->hasOneUse() && match(Shuf->getOperand(1), m_Undef()) && 734 all_equal(Shuf->getShuffleMask()) && 735 Shuf->getType() == Shuf->getOperand(0)->getType()) { 736 // trunc (shuf X, Undef, SplatMask) --> shuf (trunc X), Poison, SplatMask 737 // trunc (shuf X, Poison, SplatMask) --> shuf (trunc X), Poison, SplatMask 738 Value *NarrowOp = Builder.CreateTrunc(Shuf->getOperand(0), Trunc.getType()); 739 return new ShuffleVectorInst(NarrowOp, Shuf->getShuffleMask()); 740 } 741 742 return nullptr; 743 } 744 745 /// Try to narrow the width of an insert element. This could be generalized for 746 /// any vector constant, but we limit the transform to insertion into undef to 747 /// avoid potential backend problems from unsupported insertion widths. This 748 /// could also be extended to handle the case of inserting a scalar constant 749 /// into a vector variable. 750 static Instruction *shrinkInsertElt(CastInst &Trunc, 751 InstCombiner::BuilderTy &Builder) { 752 Instruction::CastOps Opcode = Trunc.getOpcode(); 753 assert((Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) && 754 "Unexpected instruction for shrinking"); 755 756 auto *InsElt = dyn_cast<InsertElementInst>(Trunc.getOperand(0)); 757 if (!InsElt || !InsElt->hasOneUse()) 758 return nullptr; 759 760 Type *DestTy = Trunc.getType(); 761 Type *DestScalarTy = DestTy->getScalarType(); 762 Value *VecOp = InsElt->getOperand(0); 763 Value *ScalarOp = InsElt->getOperand(1); 764 Value *Index = InsElt->getOperand(2); 765 766 if (match(VecOp, m_Undef())) { 767 // trunc (inselt undef, X, Index) --> inselt undef, (trunc X), Index 768 // fptrunc (inselt undef, X, Index) --> inselt undef, (fptrunc X), Index 769 UndefValue *NarrowUndef = UndefValue::get(DestTy); 770 Value *NarrowOp = Builder.CreateCast(Opcode, ScalarOp, DestScalarTy); 771 return InsertElementInst::Create(NarrowUndef, NarrowOp, Index); 772 } 773 774 return nullptr; 775 } 776 777 Instruction *InstCombinerImpl::visitTrunc(TruncInst &Trunc) { 778 if (Instruction *Result = commonCastTransforms(Trunc)) 779 return Result; 780 781 Value *Src = Trunc.getOperand(0); 782 Type *DestTy = Trunc.getType(), *SrcTy = Src->getType(); 783 unsigned DestWidth = DestTy->getScalarSizeInBits(); 784 unsigned SrcWidth = SrcTy->getScalarSizeInBits(); 785 786 // Attempt to truncate the entire input expression tree to the destination 787 // type. Only do this if the dest type is a simple type, don't convert the 788 // expression tree to something weird like i93 unless the source is also 789 // strange. 790 if ((DestTy->isVectorTy() || shouldChangeType(SrcTy, DestTy)) && 791 canEvaluateTruncated(Src, DestTy, *this, &Trunc)) { 792 793 // If this cast is a truncate, evaluting in a different type always 794 // eliminates the cast, so it is always a win. 795 LLVM_DEBUG( 796 dbgs() << "ICE: EvaluateInDifferentType converting expression type" 797 " to avoid cast: " 798 << Trunc << '\n'); 799 Value *Res = EvaluateInDifferentType(Src, DestTy, false); 800 assert(Res->getType() == DestTy); 801 return replaceInstUsesWith(Trunc, Res); 802 } 803 804 // For integer types, check if we can shorten the entire input expression to 805 // DestWidth * 2, which won't allow removing the truncate, but reducing the 806 // width may enable further optimizations, e.g. allowing for larger 807 // vectorization factors. 808 if (auto *DestITy = dyn_cast<IntegerType>(DestTy)) { 809 if (DestWidth * 2 < SrcWidth) { 810 auto *NewDestTy = DestITy->getExtendedType(); 811 if (shouldChangeType(SrcTy, NewDestTy) && 812 canEvaluateTruncated(Src, NewDestTy, *this, &Trunc)) { 813 LLVM_DEBUG( 814 dbgs() << "ICE: EvaluateInDifferentType converting expression type" 815 " to reduce the width of operand of" 816 << Trunc << '\n'); 817 Value *Res = EvaluateInDifferentType(Src, NewDestTy, false); 818 return new TruncInst(Res, DestTy); 819 } 820 } 821 } 822 823 // Test if the trunc is the user of a select which is part of a 824 // minimum or maximum operation. If so, don't do any more simplification. 825 // Even simplifying demanded bits can break the canonical form of a 826 // min/max. 827 Value *LHS, *RHS; 828 if (SelectInst *Sel = dyn_cast<SelectInst>(Src)) 829 if (matchSelectPattern(Sel, LHS, RHS).Flavor != SPF_UNKNOWN) 830 return nullptr; 831 832 // See if we can simplify any instructions used by the input whose sole 833 // purpose is to compute bits we don't care about. 834 if (SimplifyDemandedInstructionBits(Trunc)) 835 return &Trunc; 836 837 if (DestWidth == 1) { 838 Value *Zero = Constant::getNullValue(SrcTy); 839 if (DestTy->isIntegerTy()) { 840 // Canonicalize trunc x to i1 -> icmp ne (and x, 1), 0 (scalar only). 841 // TODO: We canonicalize to more instructions here because we are probably 842 // lacking equivalent analysis for trunc relative to icmp. There may also 843 // be codegen concerns. If those trunc limitations were removed, we could 844 // remove this transform. 845 Value *And = Builder.CreateAnd(Src, ConstantInt::get(SrcTy, 1)); 846 return new ICmpInst(ICmpInst::ICMP_NE, And, Zero); 847 } 848 849 // For vectors, we do not canonicalize all truncs to icmp, so optimize 850 // patterns that would be covered within visitICmpInst. 851 Value *X; 852 Constant *C; 853 if (match(Src, m_OneUse(m_LShr(m_Value(X), m_Constant(C))))) { 854 // trunc (lshr X, C) to i1 --> icmp ne (and X, C'), 0 855 Constant *One = ConstantInt::get(SrcTy, APInt(SrcWidth, 1)); 856 Constant *MaskC = ConstantExpr::getShl(One, C); 857 Value *And = Builder.CreateAnd(X, MaskC); 858 return new ICmpInst(ICmpInst::ICMP_NE, And, Zero); 859 } 860 if (match(Src, m_OneUse(m_c_Or(m_LShr(m_Value(X), m_Constant(C)), 861 m_Deferred(X))))) { 862 // trunc (or (lshr X, C), X) to i1 --> icmp ne (and X, C'), 0 863 Constant *One = ConstantInt::get(SrcTy, APInt(SrcWidth, 1)); 864 Constant *MaskC = ConstantExpr::getShl(One, C); 865 MaskC = ConstantExpr::getOr(MaskC, One); 866 Value *And = Builder.CreateAnd(X, MaskC); 867 return new ICmpInst(ICmpInst::ICMP_NE, And, Zero); 868 } 869 } 870 871 Value *A, *B; 872 Constant *C; 873 if (match(Src, m_LShr(m_SExt(m_Value(A)), m_Constant(C)))) { 874 unsigned AWidth = A->getType()->getScalarSizeInBits(); 875 unsigned MaxShiftAmt = SrcWidth - std::max(DestWidth, AWidth); 876 auto *OldSh = cast<Instruction>(Src); 877 bool IsExact = OldSh->isExact(); 878 879 // If the shift is small enough, all zero bits created by the shift are 880 // removed by the trunc. 881 if (match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULE, 882 APInt(SrcWidth, MaxShiftAmt)))) { 883 // trunc (lshr (sext A), C) --> ashr A, C 884 if (A->getType() == DestTy) { 885 Constant *MaxAmt = ConstantInt::get(SrcTy, DestWidth - 1, false); 886 Constant *ShAmt = ConstantExpr::getUMin(C, MaxAmt); 887 ShAmt = ConstantExpr::getTrunc(ShAmt, A->getType()); 888 ShAmt = Constant::mergeUndefsWith(ShAmt, C); 889 return IsExact ? BinaryOperator::CreateExactAShr(A, ShAmt) 890 : BinaryOperator::CreateAShr(A, ShAmt); 891 } 892 // The types are mismatched, so create a cast after shifting: 893 // trunc (lshr (sext A), C) --> sext/trunc (ashr A, C) 894 if (Src->hasOneUse()) { 895 Constant *MaxAmt = ConstantInt::get(SrcTy, AWidth - 1, false); 896 Constant *ShAmt = ConstantExpr::getUMin(C, MaxAmt); 897 ShAmt = ConstantExpr::getTrunc(ShAmt, A->getType()); 898 Value *Shift = Builder.CreateAShr(A, ShAmt, "", IsExact); 899 return CastInst::CreateIntegerCast(Shift, DestTy, true); 900 } 901 } 902 // TODO: Mask high bits with 'and'. 903 } 904 905 if (Instruction *I = narrowBinOp(Trunc)) 906 return I; 907 908 if (Instruction *I = shrinkSplatShuffle(Trunc, Builder)) 909 return I; 910 911 if (Instruction *I = shrinkInsertElt(Trunc, Builder)) 912 return I; 913 914 if (Src->hasOneUse() && 915 (isa<VectorType>(SrcTy) || shouldChangeType(SrcTy, DestTy))) { 916 // Transform "trunc (shl X, cst)" -> "shl (trunc X), cst" so long as the 917 // dest type is native and cst < dest size. 918 if (match(Src, m_Shl(m_Value(A), m_Constant(C))) && 919 !match(A, m_Shr(m_Value(), m_Constant()))) { 920 // Skip shifts of shift by constants. It undoes a combine in 921 // FoldShiftByConstant and is the extend in reg pattern. 922 APInt Threshold = APInt(C->getType()->getScalarSizeInBits(), DestWidth); 923 if (match(C, m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold))) { 924 Value *NewTrunc = Builder.CreateTrunc(A, DestTy, A->getName() + ".tr"); 925 return BinaryOperator::Create(Instruction::Shl, NewTrunc, 926 ConstantExpr::getTrunc(C, DestTy)); 927 } 928 } 929 } 930 931 if (Instruction *I = foldVecTruncToExtElt(Trunc, *this)) 932 return I; 933 934 // Whenever an element is extracted from a vector, and then truncated, 935 // canonicalize by converting it to a bitcast followed by an 936 // extractelement. 937 // 938 // Example (little endian): 939 // trunc (extractelement <4 x i64> %X, 0) to i32 940 // ---> 941 // extractelement <8 x i32> (bitcast <4 x i64> %X to <8 x i32>), i32 0 942 Value *VecOp; 943 ConstantInt *Cst; 944 if (match(Src, m_OneUse(m_ExtractElt(m_Value(VecOp), m_ConstantInt(Cst))))) { 945 auto *VecOpTy = cast<VectorType>(VecOp->getType()); 946 auto VecElts = VecOpTy->getElementCount(); 947 948 // A badly fit destination size would result in an invalid cast. 949 if (SrcWidth % DestWidth == 0) { 950 uint64_t TruncRatio = SrcWidth / DestWidth; 951 uint64_t BitCastNumElts = VecElts.getKnownMinValue() * TruncRatio; 952 uint64_t VecOpIdx = Cst->getZExtValue(); 953 uint64_t NewIdx = DL.isBigEndian() ? (VecOpIdx + 1) * TruncRatio - 1 954 : VecOpIdx * TruncRatio; 955 assert(BitCastNumElts <= std::numeric_limits<uint32_t>::max() && 956 "overflow 32-bits"); 957 958 auto *BitCastTo = 959 VectorType::get(DestTy, BitCastNumElts, VecElts.isScalable()); 960 Value *BitCast = Builder.CreateBitCast(VecOp, BitCastTo); 961 return ExtractElementInst::Create(BitCast, Builder.getInt32(NewIdx)); 962 } 963 } 964 965 // trunc (ctlz_i32(zext(A), B) --> add(ctlz_i16(A, B), C) 966 if (match(Src, m_OneUse(m_Intrinsic<Intrinsic::ctlz>(m_ZExt(m_Value(A)), 967 m_Value(B))))) { 968 unsigned AWidth = A->getType()->getScalarSizeInBits(); 969 if (AWidth == DestWidth && AWidth > Log2_32(SrcWidth)) { 970 Value *WidthDiff = ConstantInt::get(A->getType(), SrcWidth - AWidth); 971 Value *NarrowCtlz = 972 Builder.CreateIntrinsic(Intrinsic::ctlz, {Trunc.getType()}, {A, B}); 973 return BinaryOperator::CreateAdd(NarrowCtlz, WidthDiff); 974 } 975 } 976 977 if (match(Src, m_VScale(DL))) { 978 if (Trunc.getFunction() && 979 Trunc.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { 980 Attribute Attr = 981 Trunc.getFunction()->getFnAttribute(Attribute::VScaleRange); 982 if (Optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) { 983 if (Log2_32(*MaxVScale) < DestWidth) { 984 Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1)); 985 return replaceInstUsesWith(Trunc, VScale); 986 } 987 } 988 } 989 } 990 991 return nullptr; 992 } 993 994 Instruction *InstCombinerImpl::transformZExtICmp(ICmpInst *Cmp, ZExtInst &Zext) { 995 // If we are just checking for a icmp eq of a single bit and zext'ing it 996 // to an integer, then shift the bit to the appropriate place and then 997 // cast to integer to avoid the comparison. 998 999 // FIXME: This set of transforms does not check for extra uses and/or creates 1000 // an extra instruction (an optional final cast is not included 1001 // in the transform comments). We may also want to favor icmp over 1002 // shifts in cases of equal instructions because icmp has better 1003 // analysis in general (invert the transform). 1004 1005 const APInt *Op1CV; 1006 if (match(Cmp->getOperand(1), m_APInt(Op1CV))) { 1007 1008 // zext (x <s 0) to i32 --> x>>u31 true if signbit set. 1009 if (Cmp->getPredicate() == ICmpInst::ICMP_SLT && Op1CV->isZero()) { 1010 Value *In = Cmp->getOperand(0); 1011 Value *Sh = ConstantInt::get(In->getType(), 1012 In->getType()->getScalarSizeInBits() - 1); 1013 In = Builder.CreateLShr(In, Sh, In->getName() + ".lobit"); 1014 if (In->getType() != Zext.getType()) 1015 In = Builder.CreateIntCast(In, Zext.getType(), false /*ZExt*/); 1016 1017 return replaceInstUsesWith(Zext, In); 1018 } 1019 1020 // zext (X == 0) to i32 --> X^1 iff X has only the low bit set. 1021 // zext (X == 0) to i32 --> (X>>1)^1 iff X has only the 2nd bit set. 1022 // zext (X != 0) to i32 --> X iff X has only the low bit set. 1023 // zext (X != 0) to i32 --> X>>1 iff X has only the 2nd bit set. 1024 if (Op1CV->isZero() && Cmp->isEquality()) { 1025 // If Op1C some other power of two, convert: 1026 KnownBits Known = computeKnownBits(Cmp->getOperand(0), 0, &Zext); 1027 1028 APInt KnownZeroMask(~Known.Zero); 1029 if (KnownZeroMask.isPowerOf2()) { // Exactly 1 possible 1? 1030 bool isNE = Cmp->getPredicate() == ICmpInst::ICMP_NE; 1031 uint32_t ShAmt = KnownZeroMask.logBase2(); 1032 Value *In = Cmp->getOperand(0); 1033 if (ShAmt) { 1034 // Perform a logical shr by shiftamt. 1035 // Insert the shift to put the result in the low bit. 1036 In = Builder.CreateLShr(In, ConstantInt::get(In->getType(), ShAmt), 1037 In->getName() + ".lobit"); 1038 } 1039 1040 if (!isNE) { // Toggle the low bit. 1041 Constant *One = ConstantInt::get(In->getType(), 1); 1042 In = Builder.CreateXor(In, One); 1043 } 1044 1045 if (Zext.getType() == In->getType()) 1046 return replaceInstUsesWith(Zext, In); 1047 1048 Value *IntCast = Builder.CreateIntCast(In, Zext.getType(), false); 1049 return replaceInstUsesWith(Zext, IntCast); 1050 } 1051 } 1052 } 1053 1054 if (Cmp->isEquality() && Zext.getType() == Cmp->getOperand(0)->getType()) { 1055 // Test if a bit is clear/set using a shifted-one mask: 1056 // zext (icmp eq (and X, (1 << ShAmt)), 0) --> and (lshr (not X), ShAmt), 1 1057 // zext (icmp ne (and X, (1 << ShAmt)), 0) --> and (lshr X, ShAmt), 1 1058 Value *X, *ShAmt; 1059 if (Cmp->hasOneUse() && match(Cmp->getOperand(1), m_ZeroInt()) && 1060 match(Cmp->getOperand(0), 1061 m_OneUse(m_c_And(m_Shl(m_One(), m_Value(ShAmt)), m_Value(X))))) { 1062 if (Cmp->getPredicate() == ICmpInst::ICMP_EQ) 1063 X = Builder.CreateNot(X); 1064 Value *Lshr = Builder.CreateLShr(X, ShAmt); 1065 Value *And1 = Builder.CreateAnd(Lshr, ConstantInt::get(X->getType(), 1)); 1066 return replaceInstUsesWith(Zext, And1); 1067 } 1068 1069 // icmp ne A, B is equal to xor A, B when A and B only really have one bit. 1070 // It is also profitable to transform icmp eq into not(xor(A, B)) because 1071 // that may lead to additional simplifications. 1072 if (IntegerType *ITy = dyn_cast<IntegerType>(Zext.getType())) { 1073 Value *LHS = Cmp->getOperand(0); 1074 Value *RHS = Cmp->getOperand(1); 1075 1076 KnownBits KnownLHS = computeKnownBits(LHS, 0, &Zext); 1077 KnownBits KnownRHS = computeKnownBits(RHS, 0, &Zext); 1078 1079 if (KnownLHS == KnownRHS) { 1080 APInt KnownBits = KnownLHS.Zero | KnownLHS.One; 1081 APInt UnknownBit = ~KnownBits; 1082 if (UnknownBit.countPopulation() == 1) { 1083 Value *Result = Builder.CreateXor(LHS, RHS); 1084 1085 // Mask off any bits that are set and won't be shifted away. 1086 if (KnownLHS.One.uge(UnknownBit)) 1087 Result = Builder.CreateAnd(Result, 1088 ConstantInt::get(ITy, UnknownBit)); 1089 1090 // Shift the bit we're testing down to the lsb. 1091 Result = Builder.CreateLShr( 1092 Result, ConstantInt::get(ITy, UnknownBit.countTrailingZeros())); 1093 1094 if (Cmp->getPredicate() == ICmpInst::ICMP_EQ) 1095 Result = Builder.CreateXor(Result, ConstantInt::get(ITy, 1)); 1096 Result->takeName(Cmp); 1097 return replaceInstUsesWith(Zext, Result); 1098 } 1099 } 1100 } 1101 } 1102 1103 return nullptr; 1104 } 1105 1106 /// Determine if the specified value can be computed in the specified wider type 1107 /// and produce the same low bits. If not, return false. 1108 /// 1109 /// If this function returns true, it can also return a non-zero number of bits 1110 /// (in BitsToClear) which indicates that the value it computes is correct for 1111 /// the zero extend, but that the additional BitsToClear bits need to be zero'd 1112 /// out. For example, to promote something like: 1113 /// 1114 /// %B = trunc i64 %A to i32 1115 /// %C = lshr i32 %B, 8 1116 /// %E = zext i32 %C to i64 1117 /// 1118 /// CanEvaluateZExtd for the 'lshr' will return true, and BitsToClear will be 1119 /// set to 8 to indicate that the promoted value needs to have bits 24-31 1120 /// cleared in addition to bits 32-63. Since an 'and' will be generated to 1121 /// clear the top bits anyway, doing this has no extra cost. 1122 /// 1123 /// This function works on both vectors and scalars. 1124 static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear, 1125 InstCombinerImpl &IC, Instruction *CxtI) { 1126 BitsToClear = 0; 1127 if (canAlwaysEvaluateInType(V, Ty)) 1128 return true; 1129 if (canNotEvaluateInType(V, Ty)) 1130 return false; 1131 1132 auto *I = cast<Instruction>(V); 1133 unsigned Tmp; 1134 switch (I->getOpcode()) { 1135 case Instruction::ZExt: // zext(zext(x)) -> zext(x). 1136 case Instruction::SExt: // zext(sext(x)) -> sext(x). 1137 case Instruction::Trunc: // zext(trunc(x)) -> trunc(x) or zext(x) 1138 return true; 1139 case Instruction::And: 1140 case Instruction::Or: 1141 case Instruction::Xor: 1142 case Instruction::Add: 1143 case Instruction::Sub: 1144 case Instruction::Mul: 1145 if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI) || 1146 !canEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI)) 1147 return false; 1148 // These can all be promoted if neither operand has 'bits to clear'. 1149 if (BitsToClear == 0 && Tmp == 0) 1150 return true; 1151 1152 // If the operation is an AND/OR/XOR and the bits to clear are zero in the 1153 // other side, BitsToClear is ok. 1154 if (Tmp == 0 && I->isBitwiseLogicOp()) { 1155 // We use MaskedValueIsZero here for generality, but the case we care 1156 // about the most is constant RHS. 1157 unsigned VSize = V->getType()->getScalarSizeInBits(); 1158 if (IC.MaskedValueIsZero(I->getOperand(1), 1159 APInt::getHighBitsSet(VSize, BitsToClear), 1160 0, CxtI)) { 1161 // If this is an And instruction and all of the BitsToClear are 1162 // known to be zero we can reset BitsToClear. 1163 if (I->getOpcode() == Instruction::And) 1164 BitsToClear = 0; 1165 return true; 1166 } 1167 } 1168 1169 // Otherwise, we don't know how to analyze this BitsToClear case yet. 1170 return false; 1171 1172 case Instruction::Shl: { 1173 // We can promote shl(x, cst) if we can promote x. Since shl overwrites the 1174 // upper bits we can reduce BitsToClear by the shift amount. 1175 const APInt *Amt; 1176 if (match(I->getOperand(1), m_APInt(Amt))) { 1177 if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI)) 1178 return false; 1179 uint64_t ShiftAmt = Amt->getZExtValue(); 1180 BitsToClear = ShiftAmt < BitsToClear ? BitsToClear - ShiftAmt : 0; 1181 return true; 1182 } 1183 return false; 1184 } 1185 case Instruction::LShr: { 1186 // We can promote lshr(x, cst) if we can promote x. This requires the 1187 // ultimate 'and' to clear out the high zero bits we're clearing out though. 1188 const APInt *Amt; 1189 if (match(I->getOperand(1), m_APInt(Amt))) { 1190 if (!canEvaluateZExtd(I->getOperand(0), Ty, BitsToClear, IC, CxtI)) 1191 return false; 1192 BitsToClear += Amt->getZExtValue(); 1193 if (BitsToClear > V->getType()->getScalarSizeInBits()) 1194 BitsToClear = V->getType()->getScalarSizeInBits(); 1195 return true; 1196 } 1197 // Cannot promote variable LSHR. 1198 return false; 1199 } 1200 case Instruction::Select: 1201 if (!canEvaluateZExtd(I->getOperand(1), Ty, Tmp, IC, CxtI) || 1202 !canEvaluateZExtd(I->getOperand(2), Ty, BitsToClear, IC, CxtI) || 1203 // TODO: If important, we could handle the case when the BitsToClear are 1204 // known zero in the disagreeing side. 1205 Tmp != BitsToClear) 1206 return false; 1207 return true; 1208 1209 case Instruction::PHI: { 1210 // We can change a phi if we can change all operands. Note that we never 1211 // get into trouble with cyclic PHIs here because we only consider 1212 // instructions with a single use. 1213 PHINode *PN = cast<PHINode>(I); 1214 if (!canEvaluateZExtd(PN->getIncomingValue(0), Ty, BitsToClear, IC, CxtI)) 1215 return false; 1216 for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) 1217 if (!canEvaluateZExtd(PN->getIncomingValue(i), Ty, Tmp, IC, CxtI) || 1218 // TODO: If important, we could handle the case when the BitsToClear 1219 // are known zero in the disagreeing input. 1220 Tmp != BitsToClear) 1221 return false; 1222 return true; 1223 } 1224 default: 1225 // TODO: Can handle more cases here. 1226 return false; 1227 } 1228 } 1229 1230 Instruction *InstCombinerImpl::visitZExt(ZExtInst &CI) { 1231 // If this zero extend is only used by a truncate, let the truncate be 1232 // eliminated before we try to optimize this zext. 1233 if (CI.hasOneUse() && isa<TruncInst>(CI.user_back())) 1234 return nullptr; 1235 1236 // If one of the common conversion will work, do it. 1237 if (Instruction *Result = commonCastTransforms(CI)) 1238 return Result; 1239 1240 Value *Src = CI.getOperand(0); 1241 Type *SrcTy = Src->getType(), *DestTy = CI.getType(); 1242 1243 // Try to extend the entire expression tree to the wide destination type. 1244 unsigned BitsToClear; 1245 if (shouldChangeType(SrcTy, DestTy) && 1246 canEvaluateZExtd(Src, DestTy, BitsToClear, *this, &CI)) { 1247 assert(BitsToClear <= SrcTy->getScalarSizeInBits() && 1248 "Can't clear more bits than in SrcTy"); 1249 1250 // Okay, we can transform this! Insert the new expression now. 1251 LLVM_DEBUG( 1252 dbgs() << "ICE: EvaluateInDifferentType converting expression type" 1253 " to avoid zero extend: " 1254 << CI << '\n'); 1255 Value *Res = EvaluateInDifferentType(Src, DestTy, false); 1256 assert(Res->getType() == DestTy); 1257 1258 // Preserve debug values referring to Src if the zext is its last use. 1259 if (auto *SrcOp = dyn_cast<Instruction>(Src)) 1260 if (SrcOp->hasOneUse()) 1261 replaceAllDbgUsesWith(*SrcOp, *Res, CI, DT); 1262 1263 uint32_t SrcBitsKept = SrcTy->getScalarSizeInBits()-BitsToClear; 1264 uint32_t DestBitSize = DestTy->getScalarSizeInBits(); 1265 1266 // If the high bits are already filled with zeros, just replace this 1267 // cast with the result. 1268 if (MaskedValueIsZero(Res, 1269 APInt::getHighBitsSet(DestBitSize, 1270 DestBitSize-SrcBitsKept), 1271 0, &CI)) 1272 return replaceInstUsesWith(CI, Res); 1273 1274 // We need to emit an AND to clear the high bits. 1275 Constant *C = ConstantInt::get(Res->getType(), 1276 APInt::getLowBitsSet(DestBitSize, SrcBitsKept)); 1277 return BinaryOperator::CreateAnd(Res, C); 1278 } 1279 1280 // If this is a TRUNC followed by a ZEXT then we are dealing with integral 1281 // types and if the sizes are just right we can convert this into a logical 1282 // 'and' which will be much cheaper than the pair of casts. 1283 if (TruncInst *CSrc = dyn_cast<TruncInst>(Src)) { // A->B->C cast 1284 // TODO: Subsume this into EvaluateInDifferentType. 1285 1286 // Get the sizes of the types involved. We know that the intermediate type 1287 // will be smaller than A or C, but don't know the relation between A and C. 1288 Value *A = CSrc->getOperand(0); 1289 unsigned SrcSize = A->getType()->getScalarSizeInBits(); 1290 unsigned MidSize = CSrc->getType()->getScalarSizeInBits(); 1291 unsigned DstSize = CI.getType()->getScalarSizeInBits(); 1292 // If we're actually extending zero bits, then if 1293 // SrcSize < DstSize: zext(a & mask) 1294 // SrcSize == DstSize: a & mask 1295 // SrcSize > DstSize: trunc(a) & mask 1296 if (SrcSize < DstSize) { 1297 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); 1298 Constant *AndConst = ConstantInt::get(A->getType(), AndValue); 1299 Value *And = Builder.CreateAnd(A, AndConst, CSrc->getName() + ".mask"); 1300 return new ZExtInst(And, CI.getType()); 1301 } 1302 1303 if (SrcSize == DstSize) { 1304 APInt AndValue(APInt::getLowBitsSet(SrcSize, MidSize)); 1305 return BinaryOperator::CreateAnd(A, ConstantInt::get(A->getType(), 1306 AndValue)); 1307 } 1308 if (SrcSize > DstSize) { 1309 Value *Trunc = Builder.CreateTrunc(A, CI.getType()); 1310 APInt AndValue(APInt::getLowBitsSet(DstSize, MidSize)); 1311 return BinaryOperator::CreateAnd(Trunc, 1312 ConstantInt::get(Trunc->getType(), 1313 AndValue)); 1314 } 1315 } 1316 1317 if (ICmpInst *Cmp = dyn_cast<ICmpInst>(Src)) 1318 return transformZExtICmp(Cmp, CI); 1319 1320 // zext(trunc(X) & C) -> (X & zext(C)). 1321 Constant *C; 1322 Value *X; 1323 if (match(Src, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Constant(C)))) && 1324 X->getType() == CI.getType()) 1325 return BinaryOperator::CreateAnd(X, ConstantExpr::getZExt(C, CI.getType())); 1326 1327 // zext((trunc(X) & C) ^ C) -> ((X & zext(C)) ^ zext(C)). 1328 Value *And; 1329 if (match(Src, m_OneUse(m_Xor(m_Value(And), m_Constant(C)))) && 1330 match(And, m_OneUse(m_And(m_Trunc(m_Value(X)), m_Specific(C)))) && 1331 X->getType() == CI.getType()) { 1332 Constant *ZC = ConstantExpr::getZExt(C, CI.getType()); 1333 return BinaryOperator::CreateXor(Builder.CreateAnd(X, ZC), ZC); 1334 } 1335 1336 // If we are truncating, masking, and then zexting back to the original type, 1337 // that's just a mask. This is not handled by canEvaluateZextd if the 1338 // intermediate values have extra uses. This could be generalized further for 1339 // a non-constant mask operand. 1340 // zext (and (trunc X), C) --> and X, (zext C) 1341 if (match(Src, m_And(m_Trunc(m_Value(X)), m_Constant(C))) && 1342 X->getType() == DestTy) { 1343 Constant *ZextC = ConstantExpr::getZExt(C, DestTy); 1344 return BinaryOperator::CreateAnd(X, ZextC); 1345 } 1346 1347 if (match(Src, m_VScale(DL))) { 1348 if (CI.getFunction() && 1349 CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { 1350 Attribute Attr = CI.getFunction()->getFnAttribute(Attribute::VScaleRange); 1351 if (Optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) { 1352 unsigned TypeWidth = Src->getType()->getScalarSizeInBits(); 1353 if (Log2_32(*MaxVScale) < TypeWidth) { 1354 Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1)); 1355 return replaceInstUsesWith(CI, VScale); 1356 } 1357 } 1358 } 1359 } 1360 1361 return nullptr; 1362 } 1363 1364 /// Transform (sext icmp) to bitwise / integer operations to eliminate the icmp. 1365 Instruction *InstCombinerImpl::transformSExtICmp(ICmpInst *ICI, 1366 Instruction &CI) { 1367 Value *Op0 = ICI->getOperand(0), *Op1 = ICI->getOperand(1); 1368 ICmpInst::Predicate Pred = ICI->getPredicate(); 1369 1370 // Don't bother if Op1 isn't of vector or integer type. 1371 if (!Op1->getType()->isIntOrIntVectorTy()) 1372 return nullptr; 1373 1374 if ((Pred == ICmpInst::ICMP_SLT && match(Op1, m_ZeroInt())) || 1375 (Pred == ICmpInst::ICMP_SGT && match(Op1, m_AllOnes()))) { 1376 // (x <s 0) ? -1 : 0 -> ashr x, 31 -> all ones if negative 1377 // (x >s -1) ? -1 : 0 -> not (ashr x, 31) -> all ones if positive 1378 Value *Sh = ConstantInt::get(Op0->getType(), 1379 Op0->getType()->getScalarSizeInBits() - 1); 1380 Value *In = Builder.CreateAShr(Op0, Sh, Op0->getName() + ".lobit"); 1381 if (In->getType() != CI.getType()) 1382 In = Builder.CreateIntCast(In, CI.getType(), true /*SExt*/); 1383 1384 if (Pred == ICmpInst::ICMP_SGT) 1385 In = Builder.CreateNot(In, In->getName() + ".not"); 1386 return replaceInstUsesWith(CI, In); 1387 } 1388 1389 if (ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) { 1390 // If we know that only one bit of the LHS of the icmp can be set and we 1391 // have an equality comparison with zero or a power of 2, we can transform 1392 // the icmp and sext into bitwise/integer operations. 1393 if (ICI->hasOneUse() && 1394 ICI->isEquality() && (Op1C->isZero() || Op1C->getValue().isPowerOf2())){ 1395 KnownBits Known = computeKnownBits(Op0, 0, &CI); 1396 1397 APInt KnownZeroMask(~Known.Zero); 1398 if (KnownZeroMask.isPowerOf2()) { 1399 Value *In = ICI->getOperand(0); 1400 1401 // If the icmp tests for a known zero bit we can constant fold it. 1402 if (!Op1C->isZero() && Op1C->getValue() != KnownZeroMask) { 1403 Value *V = Pred == ICmpInst::ICMP_NE ? 1404 ConstantInt::getAllOnesValue(CI.getType()) : 1405 ConstantInt::getNullValue(CI.getType()); 1406 return replaceInstUsesWith(CI, V); 1407 } 1408 1409 if (!Op1C->isZero() == (Pred == ICmpInst::ICMP_NE)) { 1410 // sext ((x & 2^n) == 0) -> (x >> n) - 1 1411 // sext ((x & 2^n) != 2^n) -> (x >> n) - 1 1412 unsigned ShiftAmt = KnownZeroMask.countTrailingZeros(); 1413 // Perform a right shift to place the desired bit in the LSB. 1414 if (ShiftAmt) 1415 In = Builder.CreateLShr(In, 1416 ConstantInt::get(In->getType(), ShiftAmt)); 1417 1418 // At this point "In" is either 1 or 0. Subtract 1 to turn 1419 // {1, 0} -> {0, -1}. 1420 In = Builder.CreateAdd(In, 1421 ConstantInt::getAllOnesValue(In->getType()), 1422 "sext"); 1423 } else { 1424 // sext ((x & 2^n) != 0) -> (x << bitwidth-n) a>> bitwidth-1 1425 // sext ((x & 2^n) == 2^n) -> (x << bitwidth-n) a>> bitwidth-1 1426 unsigned ShiftAmt = KnownZeroMask.countLeadingZeros(); 1427 // Perform a left shift to place the desired bit in the MSB. 1428 if (ShiftAmt) 1429 In = Builder.CreateShl(In, 1430 ConstantInt::get(In->getType(), ShiftAmt)); 1431 1432 // Distribute the bit over the whole bit width. 1433 In = Builder.CreateAShr(In, ConstantInt::get(In->getType(), 1434 KnownZeroMask.getBitWidth() - 1), "sext"); 1435 } 1436 1437 if (CI.getType() == In->getType()) 1438 return replaceInstUsesWith(CI, In); 1439 return CastInst::CreateIntegerCast(In, CI.getType(), true/*SExt*/); 1440 } 1441 } 1442 } 1443 1444 return nullptr; 1445 } 1446 1447 /// Return true if we can take the specified value and return it as type Ty 1448 /// without inserting any new casts and without changing the value of the common 1449 /// low bits. This is used by code that tries to promote integer operations to 1450 /// a wider types will allow us to eliminate the extension. 1451 /// 1452 /// This function works on both vectors and scalars. 1453 /// 1454 static bool canEvaluateSExtd(Value *V, Type *Ty) { 1455 assert(V->getType()->getScalarSizeInBits() < Ty->getScalarSizeInBits() && 1456 "Can't sign extend type to a smaller type"); 1457 if (canAlwaysEvaluateInType(V, Ty)) 1458 return true; 1459 if (canNotEvaluateInType(V, Ty)) 1460 return false; 1461 1462 auto *I = cast<Instruction>(V); 1463 switch (I->getOpcode()) { 1464 case Instruction::SExt: // sext(sext(x)) -> sext(x) 1465 case Instruction::ZExt: // sext(zext(x)) -> zext(x) 1466 case Instruction::Trunc: // sext(trunc(x)) -> trunc(x) or sext(x) 1467 return true; 1468 case Instruction::And: 1469 case Instruction::Or: 1470 case Instruction::Xor: 1471 case Instruction::Add: 1472 case Instruction::Sub: 1473 case Instruction::Mul: 1474 // These operators can all arbitrarily be extended if their inputs can. 1475 return canEvaluateSExtd(I->getOperand(0), Ty) && 1476 canEvaluateSExtd(I->getOperand(1), Ty); 1477 1478 //case Instruction::Shl: TODO 1479 //case Instruction::LShr: TODO 1480 1481 case Instruction::Select: 1482 return canEvaluateSExtd(I->getOperand(1), Ty) && 1483 canEvaluateSExtd(I->getOperand(2), Ty); 1484 1485 case Instruction::PHI: { 1486 // We can change a phi if we can change all operands. Note that we never 1487 // get into trouble with cyclic PHIs here because we only consider 1488 // instructions with a single use. 1489 PHINode *PN = cast<PHINode>(I); 1490 for (Value *IncValue : PN->incoming_values()) 1491 if (!canEvaluateSExtd(IncValue, Ty)) return false; 1492 return true; 1493 } 1494 default: 1495 // TODO: Can handle more cases here. 1496 break; 1497 } 1498 1499 return false; 1500 } 1501 1502 Instruction *InstCombinerImpl::visitSExt(SExtInst &CI) { 1503 // If this sign extend is only used by a truncate, let the truncate be 1504 // eliminated before we try to optimize this sext. 1505 if (CI.hasOneUse() && isa<TruncInst>(CI.user_back())) 1506 return nullptr; 1507 1508 if (Instruction *I = commonCastTransforms(CI)) 1509 return I; 1510 1511 Value *Src = CI.getOperand(0); 1512 Type *SrcTy = Src->getType(), *DestTy = CI.getType(); 1513 unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); 1514 unsigned DestBitSize = DestTy->getScalarSizeInBits(); 1515 1516 // If the value being extended is zero or positive, use a zext instead. 1517 if (isKnownNonNegative(Src, DL, 0, &AC, &CI, &DT)) 1518 return CastInst::Create(Instruction::ZExt, Src, DestTy); 1519 1520 // Try to extend the entire expression tree to the wide destination type. 1521 if (shouldChangeType(SrcTy, DestTy) && canEvaluateSExtd(Src, DestTy)) { 1522 // Okay, we can transform this! Insert the new expression now. 1523 LLVM_DEBUG( 1524 dbgs() << "ICE: EvaluateInDifferentType converting expression type" 1525 " to avoid sign extend: " 1526 << CI << '\n'); 1527 Value *Res = EvaluateInDifferentType(Src, DestTy, true); 1528 assert(Res->getType() == DestTy); 1529 1530 // If the high bits are already filled with sign bit, just replace this 1531 // cast with the result. 1532 if (ComputeNumSignBits(Res, 0, &CI) > DestBitSize - SrcBitSize) 1533 return replaceInstUsesWith(CI, Res); 1534 1535 // We need to emit a shl + ashr to do the sign extend. 1536 Value *ShAmt = ConstantInt::get(DestTy, DestBitSize-SrcBitSize); 1537 return BinaryOperator::CreateAShr(Builder.CreateShl(Res, ShAmt, "sext"), 1538 ShAmt); 1539 } 1540 1541 Value *X; 1542 if (match(Src, m_Trunc(m_Value(X)))) { 1543 // If the input has more sign bits than bits truncated, then convert 1544 // directly to final type. 1545 unsigned XBitSize = X->getType()->getScalarSizeInBits(); 1546 if (ComputeNumSignBits(X, 0, &CI) > XBitSize - SrcBitSize) 1547 return CastInst::CreateIntegerCast(X, DestTy, /* isSigned */ true); 1548 1549 // If input is a trunc from the destination type, then convert into shifts. 1550 if (Src->hasOneUse() && X->getType() == DestTy) { 1551 // sext (trunc X) --> ashr (shl X, C), C 1552 Constant *ShAmt = ConstantInt::get(DestTy, DestBitSize - SrcBitSize); 1553 return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShAmt), ShAmt); 1554 } 1555 1556 // If we are replacing shifted-in high zero bits with sign bits, convert 1557 // the logic shift to arithmetic shift and eliminate the cast to 1558 // intermediate type: 1559 // sext (trunc (lshr Y, C)) --> sext/trunc (ashr Y, C) 1560 Value *Y; 1561 if (Src->hasOneUse() && 1562 match(X, m_LShr(m_Value(Y), 1563 m_SpecificIntAllowUndef(XBitSize - SrcBitSize)))) { 1564 Value *Ashr = Builder.CreateAShr(Y, XBitSize - SrcBitSize); 1565 return CastInst::CreateIntegerCast(Ashr, DestTy, /* isSigned */ true); 1566 } 1567 } 1568 1569 if (ICmpInst *ICI = dyn_cast<ICmpInst>(Src)) 1570 return transformSExtICmp(ICI, CI); 1571 1572 // If the input is a shl/ashr pair of a same constant, then this is a sign 1573 // extension from a smaller value. If we could trust arbitrary bitwidth 1574 // integers, we could turn this into a truncate to the smaller bit and then 1575 // use a sext for the whole extension. Since we don't, look deeper and check 1576 // for a truncate. If the source and dest are the same type, eliminate the 1577 // trunc and extend and just do shifts. For example, turn: 1578 // %a = trunc i32 %i to i8 1579 // %b = shl i8 %a, C 1580 // %c = ashr i8 %b, C 1581 // %d = sext i8 %c to i32 1582 // into: 1583 // %a = shl i32 %i, 32-(8-C) 1584 // %d = ashr i32 %a, 32-(8-C) 1585 Value *A = nullptr; 1586 // TODO: Eventually this could be subsumed by EvaluateInDifferentType. 1587 Constant *BA = nullptr, *CA = nullptr; 1588 if (match(Src, m_AShr(m_Shl(m_Trunc(m_Value(A)), m_Constant(BA)), 1589 m_Constant(CA))) && 1590 BA->isElementWiseEqual(CA) && A->getType() == DestTy) { 1591 Constant *WideCurrShAmt = ConstantExpr::getSExt(CA, DestTy); 1592 Constant *NumLowbitsLeft = ConstantExpr::getSub( 1593 ConstantInt::get(DestTy, SrcTy->getScalarSizeInBits()), WideCurrShAmt); 1594 Constant *NewShAmt = ConstantExpr::getSub( 1595 ConstantInt::get(DestTy, DestTy->getScalarSizeInBits()), 1596 NumLowbitsLeft); 1597 NewShAmt = 1598 Constant::mergeUndefsWith(Constant::mergeUndefsWith(NewShAmt, BA), CA); 1599 A = Builder.CreateShl(A, NewShAmt, CI.getName()); 1600 return BinaryOperator::CreateAShr(A, NewShAmt); 1601 } 1602 1603 // Splatting a bit of constant-index across a value: 1604 // sext (ashr (trunc iN X to iM), M-1) to iN --> ashr (shl X, N-M), N-1 1605 // If the dest type is different, use a cast (adjust use check). 1606 if (match(Src, m_OneUse(m_AShr(m_Trunc(m_Value(X)), 1607 m_SpecificInt(SrcBitSize - 1))))) { 1608 Type *XTy = X->getType(); 1609 unsigned XBitSize = XTy->getScalarSizeInBits(); 1610 Constant *ShlAmtC = ConstantInt::get(XTy, XBitSize - SrcBitSize); 1611 Constant *AshrAmtC = ConstantInt::get(XTy, XBitSize - 1); 1612 if (XTy == DestTy) 1613 return BinaryOperator::CreateAShr(Builder.CreateShl(X, ShlAmtC), 1614 AshrAmtC); 1615 if (cast<BinaryOperator>(Src)->getOperand(0)->hasOneUse()) { 1616 Value *Ashr = Builder.CreateAShr(Builder.CreateShl(X, ShlAmtC), AshrAmtC); 1617 return CastInst::CreateIntegerCast(Ashr, DestTy, /* isSigned */ true); 1618 } 1619 } 1620 1621 if (match(Src, m_VScale(DL))) { 1622 if (CI.getFunction() && 1623 CI.getFunction()->hasFnAttribute(Attribute::VScaleRange)) { 1624 Attribute Attr = CI.getFunction()->getFnAttribute(Attribute::VScaleRange); 1625 if (Optional<unsigned> MaxVScale = Attr.getVScaleRangeMax()) { 1626 if (Log2_32(*MaxVScale) < (SrcBitSize - 1)) { 1627 Value *VScale = Builder.CreateVScale(ConstantInt::get(DestTy, 1)); 1628 return replaceInstUsesWith(CI, VScale); 1629 } 1630 } 1631 } 1632 } 1633 1634 return nullptr; 1635 } 1636 1637 /// Return a Constant* for the specified floating-point constant if it fits 1638 /// in the specified FP type without changing its value. 1639 static bool fitsInFPType(ConstantFP *CFP, const fltSemantics &Sem) { 1640 bool losesInfo; 1641 APFloat F = CFP->getValueAPF(); 1642 (void)F.convert(Sem, APFloat::rmNearestTiesToEven, &losesInfo); 1643 return !losesInfo; 1644 } 1645 1646 static Type *shrinkFPConstant(ConstantFP *CFP) { 1647 if (CFP->getType() == Type::getPPC_FP128Ty(CFP->getContext())) 1648 return nullptr; // No constant folding of this. 1649 // See if the value can be truncated to half and then reextended. 1650 if (fitsInFPType(CFP, APFloat::IEEEhalf())) 1651 return Type::getHalfTy(CFP->getContext()); 1652 // See if the value can be truncated to float and then reextended. 1653 if (fitsInFPType(CFP, APFloat::IEEEsingle())) 1654 return Type::getFloatTy(CFP->getContext()); 1655 if (CFP->getType()->isDoubleTy()) 1656 return nullptr; // Won't shrink. 1657 if (fitsInFPType(CFP, APFloat::IEEEdouble())) 1658 return Type::getDoubleTy(CFP->getContext()); 1659 // Don't try to shrink to various long double types. 1660 return nullptr; 1661 } 1662 1663 // Determine if this is a vector of ConstantFPs and if so, return the minimal 1664 // type we can safely truncate all elements to. 1665 static Type *shrinkFPConstantVector(Value *V) { 1666 auto *CV = dyn_cast<Constant>(V); 1667 auto *CVVTy = dyn_cast<FixedVectorType>(V->getType()); 1668 if (!CV || !CVVTy) 1669 return nullptr; 1670 1671 Type *MinType = nullptr; 1672 1673 unsigned NumElts = CVVTy->getNumElements(); 1674 1675 // For fixed-width vectors we find the minimal type by looking 1676 // through the constant values of the vector. 1677 for (unsigned i = 0; i != NumElts; ++i) { 1678 if (isa<UndefValue>(CV->getAggregateElement(i))) 1679 continue; 1680 1681 auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i)); 1682 if (!CFP) 1683 return nullptr; 1684 1685 Type *T = shrinkFPConstant(CFP); 1686 if (!T) 1687 return nullptr; 1688 1689 // If we haven't found a type yet or this type has a larger mantissa than 1690 // our previous type, this is our new minimal type. 1691 if (!MinType || T->getFPMantissaWidth() > MinType->getFPMantissaWidth()) 1692 MinType = T; 1693 } 1694 1695 // Make a vector type from the minimal type. 1696 return MinType ? FixedVectorType::get(MinType, NumElts) : nullptr; 1697 } 1698 1699 /// Find the minimum FP type we can safely truncate to. 1700 static Type *getMinimumFPType(Value *V) { 1701 if (auto *FPExt = dyn_cast<FPExtInst>(V)) 1702 return FPExt->getOperand(0)->getType(); 1703 1704 // If this value is a constant, return the constant in the smallest FP type 1705 // that can accurately represent it. This allows us to turn 1706 // (float)((double)X+2.0) into x+2.0f. 1707 if (auto *CFP = dyn_cast<ConstantFP>(V)) 1708 if (Type *T = shrinkFPConstant(CFP)) 1709 return T; 1710 1711 // We can only correctly find a minimum type for a scalable vector when it is 1712 // a splat. For splats of constant values the fpext is wrapped up as a 1713 // ConstantExpr. 1714 if (auto *FPCExt = dyn_cast<ConstantExpr>(V)) 1715 if (FPCExt->getOpcode() == Instruction::FPExt) 1716 return FPCExt->getOperand(0)->getType(); 1717 1718 // Try to shrink a vector of FP constants. This returns nullptr on scalable 1719 // vectors 1720 if (Type *T = shrinkFPConstantVector(V)) 1721 return T; 1722 1723 return V->getType(); 1724 } 1725 1726 /// Return true if the cast from integer to FP can be proven to be exact for all 1727 /// possible inputs (the conversion does not lose any precision). 1728 static bool isKnownExactCastIntToFP(CastInst &I, InstCombinerImpl &IC) { 1729 CastInst::CastOps Opcode = I.getOpcode(); 1730 assert((Opcode == CastInst::SIToFP || Opcode == CastInst::UIToFP) && 1731 "Unexpected cast"); 1732 Value *Src = I.getOperand(0); 1733 Type *SrcTy = Src->getType(); 1734 Type *FPTy = I.getType(); 1735 bool IsSigned = Opcode == Instruction::SIToFP; 1736 int SrcSize = (int)SrcTy->getScalarSizeInBits() - IsSigned; 1737 1738 // Easy case - if the source integer type has less bits than the FP mantissa, 1739 // then the cast must be exact. 1740 int DestNumSigBits = FPTy->getFPMantissaWidth(); 1741 if (SrcSize <= DestNumSigBits) 1742 return true; 1743 1744 // Cast from FP to integer and back to FP is independent of the intermediate 1745 // integer width because of poison on overflow. 1746 Value *F; 1747 if (match(Src, m_FPToSI(m_Value(F))) || match(Src, m_FPToUI(m_Value(F)))) { 1748 // If this is uitofp (fptosi F), the source needs an extra bit to avoid 1749 // potential rounding of negative FP input values. 1750 int SrcNumSigBits = F->getType()->getFPMantissaWidth(); 1751 if (!IsSigned && match(Src, m_FPToSI(m_Value()))) 1752 SrcNumSigBits++; 1753 1754 // [su]itofp (fpto[su]i F) --> exact if the source type has less or equal 1755 // significant bits than the destination (and make sure neither type is 1756 // weird -- ppc_fp128). 1757 if (SrcNumSigBits > 0 && DestNumSigBits > 0 && 1758 SrcNumSigBits <= DestNumSigBits) 1759 return true; 1760 } 1761 1762 // TODO: 1763 // Try harder to find if the source integer type has less significant bits. 1764 // For example, compute number of sign bits. 1765 KnownBits SrcKnown = IC.computeKnownBits(Src, 0, &I); 1766 int SigBits = (int)SrcTy->getScalarSizeInBits() - 1767 SrcKnown.countMinLeadingZeros() - 1768 SrcKnown.countMinTrailingZeros(); 1769 if (SigBits <= DestNumSigBits) 1770 return true; 1771 1772 return false; 1773 } 1774 1775 Instruction *InstCombinerImpl::visitFPTrunc(FPTruncInst &FPT) { 1776 if (Instruction *I = commonCastTransforms(FPT)) 1777 return I; 1778 1779 // If we have fptrunc(OpI (fpextend x), (fpextend y)), we would like to 1780 // simplify this expression to avoid one or more of the trunc/extend 1781 // operations if we can do so without changing the numerical results. 1782 // 1783 // The exact manner in which the widths of the operands interact to limit 1784 // what we can and cannot do safely varies from operation to operation, and 1785 // is explained below in the various case statements. 1786 Type *Ty = FPT.getType(); 1787 auto *BO = dyn_cast<BinaryOperator>(FPT.getOperand(0)); 1788 if (BO && BO->hasOneUse()) { 1789 Type *LHSMinType = getMinimumFPType(BO->getOperand(0)); 1790 Type *RHSMinType = getMinimumFPType(BO->getOperand(1)); 1791 unsigned OpWidth = BO->getType()->getFPMantissaWidth(); 1792 unsigned LHSWidth = LHSMinType->getFPMantissaWidth(); 1793 unsigned RHSWidth = RHSMinType->getFPMantissaWidth(); 1794 unsigned SrcWidth = std::max(LHSWidth, RHSWidth); 1795 unsigned DstWidth = Ty->getFPMantissaWidth(); 1796 switch (BO->getOpcode()) { 1797 default: break; 1798 case Instruction::FAdd: 1799 case Instruction::FSub: 1800 // For addition and subtraction, the infinitely precise result can 1801 // essentially be arbitrarily wide; proving that double rounding 1802 // will not occur because the result of OpI is exact (as we will for 1803 // FMul, for example) is hopeless. However, we *can* nonetheless 1804 // frequently know that double rounding cannot occur (or that it is 1805 // innocuous) by taking advantage of the specific structure of 1806 // infinitely-precise results that admit double rounding. 1807 // 1808 // Specifically, if OpWidth >= 2*DstWdith+1 and DstWidth is sufficient 1809 // to represent both sources, we can guarantee that the double 1810 // rounding is innocuous (See p50 of Figueroa's 2000 PhD thesis, 1811 // "A Rigorous Framework for Fully Supporting the IEEE Standard ..." 1812 // for proof of this fact). 1813 // 1814 // Note: Figueroa does not consider the case where DstFormat != 1815 // SrcFormat. It's possible (likely even!) that this analysis 1816 // could be tightened for those cases, but they are rare (the main 1817 // case of interest here is (float)((double)float + float)). 1818 if (OpWidth >= 2*DstWidth+1 && DstWidth >= SrcWidth) { 1819 Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty); 1820 Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty); 1821 Instruction *RI = BinaryOperator::Create(BO->getOpcode(), LHS, RHS); 1822 RI->copyFastMathFlags(BO); 1823 return RI; 1824 } 1825 break; 1826 case Instruction::FMul: 1827 // For multiplication, the infinitely precise result has at most 1828 // LHSWidth + RHSWidth significant bits; if OpWidth is sufficient 1829 // that such a value can be exactly represented, then no double 1830 // rounding can possibly occur; we can safely perform the operation 1831 // in the destination format if it can represent both sources. 1832 if (OpWidth >= LHSWidth + RHSWidth && DstWidth >= SrcWidth) { 1833 Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty); 1834 Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty); 1835 return BinaryOperator::CreateFMulFMF(LHS, RHS, BO); 1836 } 1837 break; 1838 case Instruction::FDiv: 1839 // For division, we use again use the bound from Figueroa's 1840 // dissertation. I am entirely certain that this bound can be 1841 // tightened in the unbalanced operand case by an analysis based on 1842 // the diophantine rational approximation bound, but the well-known 1843 // condition used here is a good conservative first pass. 1844 // TODO: Tighten bound via rigorous analysis of the unbalanced case. 1845 if (OpWidth >= 2*DstWidth && DstWidth >= SrcWidth) { 1846 Value *LHS = Builder.CreateFPTrunc(BO->getOperand(0), Ty); 1847 Value *RHS = Builder.CreateFPTrunc(BO->getOperand(1), Ty); 1848 return BinaryOperator::CreateFDivFMF(LHS, RHS, BO); 1849 } 1850 break; 1851 case Instruction::FRem: { 1852 // Remainder is straightforward. Remainder is always exact, so the 1853 // type of OpI doesn't enter into things at all. We simply evaluate 1854 // in whichever source type is larger, then convert to the 1855 // destination type. 1856 if (SrcWidth == OpWidth) 1857 break; 1858 Value *LHS, *RHS; 1859 if (LHSWidth == SrcWidth) { 1860 LHS = Builder.CreateFPTrunc(BO->getOperand(0), LHSMinType); 1861 RHS = Builder.CreateFPTrunc(BO->getOperand(1), LHSMinType); 1862 } else { 1863 LHS = Builder.CreateFPTrunc(BO->getOperand(0), RHSMinType); 1864 RHS = Builder.CreateFPTrunc(BO->getOperand(1), RHSMinType); 1865 } 1866 1867 Value *ExactResult = Builder.CreateFRemFMF(LHS, RHS, BO); 1868 return CastInst::CreateFPCast(ExactResult, Ty); 1869 } 1870 } 1871 } 1872 1873 // (fptrunc (fneg x)) -> (fneg (fptrunc x)) 1874 Value *X; 1875 Instruction *Op = dyn_cast<Instruction>(FPT.getOperand(0)); 1876 if (Op && Op->hasOneUse()) { 1877 // FIXME: The FMF should propagate from the fptrunc, not the source op. 1878 IRBuilder<>::FastMathFlagGuard FMFG(Builder); 1879 if (isa<FPMathOperator>(Op)) 1880 Builder.setFastMathFlags(Op->getFastMathFlags()); 1881 1882 if (match(Op, m_FNeg(m_Value(X)))) { 1883 Value *InnerTrunc = Builder.CreateFPTrunc(X, Ty); 1884 1885 return UnaryOperator::CreateFNegFMF(InnerTrunc, Op); 1886 } 1887 1888 // If we are truncating a select that has an extended operand, we can 1889 // narrow the other operand and do the select as a narrow op. 1890 Value *Cond, *X, *Y; 1891 if (match(Op, m_Select(m_Value(Cond), m_FPExt(m_Value(X)), m_Value(Y))) && 1892 X->getType() == Ty) { 1893 // fptrunc (select Cond, (fpext X), Y --> select Cond, X, (fptrunc Y) 1894 Value *NarrowY = Builder.CreateFPTrunc(Y, Ty); 1895 Value *Sel = Builder.CreateSelect(Cond, X, NarrowY, "narrow.sel", Op); 1896 return replaceInstUsesWith(FPT, Sel); 1897 } 1898 if (match(Op, m_Select(m_Value(Cond), m_Value(Y), m_FPExt(m_Value(X)))) && 1899 X->getType() == Ty) { 1900 // fptrunc (select Cond, Y, (fpext X) --> select Cond, (fptrunc Y), X 1901 Value *NarrowY = Builder.CreateFPTrunc(Y, Ty); 1902 Value *Sel = Builder.CreateSelect(Cond, NarrowY, X, "narrow.sel", Op); 1903 return replaceInstUsesWith(FPT, Sel); 1904 } 1905 } 1906 1907 if (auto *II = dyn_cast<IntrinsicInst>(FPT.getOperand(0))) { 1908 switch (II->getIntrinsicID()) { 1909 default: break; 1910 case Intrinsic::ceil: 1911 case Intrinsic::fabs: 1912 case Intrinsic::floor: 1913 case Intrinsic::nearbyint: 1914 case Intrinsic::rint: 1915 case Intrinsic::round: 1916 case Intrinsic::roundeven: 1917 case Intrinsic::trunc: { 1918 Value *Src = II->getArgOperand(0); 1919 if (!Src->hasOneUse()) 1920 break; 1921 1922 // Except for fabs, this transformation requires the input of the unary FP 1923 // operation to be itself an fpext from the type to which we're 1924 // truncating. 1925 if (II->getIntrinsicID() != Intrinsic::fabs) { 1926 FPExtInst *FPExtSrc = dyn_cast<FPExtInst>(Src); 1927 if (!FPExtSrc || FPExtSrc->getSrcTy() != Ty) 1928 break; 1929 } 1930 1931 // Do unary FP operation on smaller type. 1932 // (fptrunc (fabs x)) -> (fabs (fptrunc x)) 1933 Value *InnerTrunc = Builder.CreateFPTrunc(Src, Ty); 1934 Function *Overload = Intrinsic::getDeclaration(FPT.getModule(), 1935 II->getIntrinsicID(), Ty); 1936 SmallVector<OperandBundleDef, 1> OpBundles; 1937 II->getOperandBundlesAsDefs(OpBundles); 1938 CallInst *NewCI = 1939 CallInst::Create(Overload, {InnerTrunc}, OpBundles, II->getName()); 1940 NewCI->copyFastMathFlags(II); 1941 return NewCI; 1942 } 1943 } 1944 } 1945 1946 if (Instruction *I = shrinkInsertElt(FPT, Builder)) 1947 return I; 1948 1949 Value *Src = FPT.getOperand(0); 1950 if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) { 1951 auto *FPCast = cast<CastInst>(Src); 1952 if (isKnownExactCastIntToFP(*FPCast, *this)) 1953 return CastInst::Create(FPCast->getOpcode(), FPCast->getOperand(0), Ty); 1954 } 1955 1956 return nullptr; 1957 } 1958 1959 Instruction *InstCombinerImpl::visitFPExt(CastInst &FPExt) { 1960 // If the source operand is a cast from integer to FP and known exact, then 1961 // cast the integer operand directly to the destination type. 1962 Type *Ty = FPExt.getType(); 1963 Value *Src = FPExt.getOperand(0); 1964 if (isa<SIToFPInst>(Src) || isa<UIToFPInst>(Src)) { 1965 auto *FPCast = cast<CastInst>(Src); 1966 if (isKnownExactCastIntToFP(*FPCast, *this)) 1967 return CastInst::Create(FPCast->getOpcode(), FPCast->getOperand(0), Ty); 1968 } 1969 1970 return commonCastTransforms(FPExt); 1971 } 1972 1973 /// fpto{s/u}i({u/s}itofp(X)) --> X or zext(X) or sext(X) or trunc(X) 1974 /// This is safe if the intermediate type has enough bits in its mantissa to 1975 /// accurately represent all values of X. For example, this won't work with 1976 /// i64 -> float -> i64. 1977 Instruction *InstCombinerImpl::foldItoFPtoI(CastInst &FI) { 1978 if (!isa<UIToFPInst>(FI.getOperand(0)) && !isa<SIToFPInst>(FI.getOperand(0))) 1979 return nullptr; 1980 1981 auto *OpI = cast<CastInst>(FI.getOperand(0)); 1982 Value *X = OpI->getOperand(0); 1983 Type *XType = X->getType(); 1984 Type *DestType = FI.getType(); 1985 bool IsOutputSigned = isa<FPToSIInst>(FI); 1986 1987 // Since we can assume the conversion won't overflow, our decision as to 1988 // whether the input will fit in the float should depend on the minimum 1989 // of the input range and output range. 1990 1991 // This means this is also safe for a signed input and unsigned output, since 1992 // a negative input would lead to undefined behavior. 1993 if (!isKnownExactCastIntToFP(*OpI, *this)) { 1994 // The first cast may not round exactly based on the source integer width 1995 // and FP width, but the overflow UB rules can still allow this to fold. 1996 // If the destination type is narrow, that means the intermediate FP value 1997 // must be large enough to hold the source value exactly. 1998 // For example, (uint8_t)((float)(uint32_t 16777217) is undefined behavior. 1999 int OutputSize = (int)DestType->getScalarSizeInBits(); 2000 if (OutputSize > OpI->getType()->getFPMantissaWidth()) 2001 return nullptr; 2002 } 2003 2004 if (DestType->getScalarSizeInBits() > XType->getScalarSizeInBits()) { 2005 bool IsInputSigned = isa<SIToFPInst>(OpI); 2006 if (IsInputSigned && IsOutputSigned) 2007 return new SExtInst(X, DestType); 2008 return new ZExtInst(X, DestType); 2009 } 2010 if (DestType->getScalarSizeInBits() < XType->getScalarSizeInBits()) 2011 return new TruncInst(X, DestType); 2012 2013 assert(XType == DestType && "Unexpected types for int to FP to int casts"); 2014 return replaceInstUsesWith(FI, X); 2015 } 2016 2017 Instruction *InstCombinerImpl::visitFPToUI(FPToUIInst &FI) { 2018 if (Instruction *I = foldItoFPtoI(FI)) 2019 return I; 2020 2021 return commonCastTransforms(FI); 2022 } 2023 2024 Instruction *InstCombinerImpl::visitFPToSI(FPToSIInst &FI) { 2025 if (Instruction *I = foldItoFPtoI(FI)) 2026 return I; 2027 2028 return commonCastTransforms(FI); 2029 } 2030 2031 Instruction *InstCombinerImpl::visitUIToFP(CastInst &CI) { 2032 return commonCastTransforms(CI); 2033 } 2034 2035 Instruction *InstCombinerImpl::visitSIToFP(CastInst &CI) { 2036 return commonCastTransforms(CI); 2037 } 2038 2039 Instruction *InstCombinerImpl::visitIntToPtr(IntToPtrInst &CI) { 2040 // If the source integer type is not the intptr_t type for this target, do a 2041 // trunc or zext to the intptr_t type, then inttoptr of it. This allows the 2042 // cast to be exposed to other transforms. 2043 unsigned AS = CI.getAddressSpace(); 2044 if (CI.getOperand(0)->getType()->getScalarSizeInBits() != 2045 DL.getPointerSizeInBits(AS)) { 2046 Type *Ty = CI.getOperand(0)->getType()->getWithNewType( 2047 DL.getIntPtrType(CI.getContext(), AS)); 2048 Value *P = Builder.CreateZExtOrTrunc(CI.getOperand(0), Ty); 2049 return new IntToPtrInst(P, CI.getType()); 2050 } 2051 2052 if (Instruction *I = commonCastTransforms(CI)) 2053 return I; 2054 2055 return nullptr; 2056 } 2057 2058 /// Implement the transforms for cast of pointer (bitcast/ptrtoint) 2059 Instruction *InstCombinerImpl::commonPointerCastTransforms(CastInst &CI) { 2060 Value *Src = CI.getOperand(0); 2061 2062 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Src)) { 2063 // If casting the result of a getelementptr instruction with no offset, turn 2064 // this into a cast of the original pointer! 2065 if (GEP->hasAllZeroIndices() && 2066 // If CI is an addrspacecast and GEP changes the poiner type, merging 2067 // GEP into CI would undo canonicalizing addrspacecast with different 2068 // pointer types, causing infinite loops. 2069 (!isa<AddrSpaceCastInst>(CI) || 2070 GEP->getType() == GEP->getPointerOperandType())) { 2071 // Changing the cast operand is usually not a good idea but it is safe 2072 // here because the pointer operand is being replaced with another 2073 // pointer operand so the opcode doesn't need to change. 2074 return replaceOperand(CI, 0, GEP->getOperand(0)); 2075 } 2076 } 2077 2078 return commonCastTransforms(CI); 2079 } 2080 2081 Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) { 2082 // If the destination integer type is not the intptr_t type for this target, 2083 // do a ptrtoint to intptr_t then do a trunc or zext. This allows the cast 2084 // to be exposed to other transforms. 2085 Value *SrcOp = CI.getPointerOperand(); 2086 Type *SrcTy = SrcOp->getType(); 2087 Type *Ty = CI.getType(); 2088 unsigned AS = CI.getPointerAddressSpace(); 2089 unsigned TySize = Ty->getScalarSizeInBits(); 2090 unsigned PtrSize = DL.getPointerSizeInBits(AS); 2091 if (TySize != PtrSize) { 2092 Type *IntPtrTy = 2093 SrcTy->getWithNewType(DL.getIntPtrType(CI.getContext(), AS)); 2094 Value *P = Builder.CreatePtrToInt(SrcOp, IntPtrTy); 2095 return CastInst::CreateIntegerCast(P, Ty, /*isSigned=*/false); 2096 } 2097 2098 if (auto *GEP = dyn_cast<GetElementPtrInst>(SrcOp)) { 2099 // Fold ptrtoint(gep null, x) to multiply + constant if the GEP has one use. 2100 // While this can increase the number of instructions it doesn't actually 2101 // increase the overall complexity since the arithmetic is just part of 2102 // the GEP otherwise. 2103 if (GEP->hasOneUse() && 2104 isa<ConstantPointerNull>(GEP->getPointerOperand())) { 2105 return replaceInstUsesWith(CI, 2106 Builder.CreateIntCast(EmitGEPOffset(GEP), Ty, 2107 /*isSigned=*/false)); 2108 } 2109 } 2110 2111 Value *Vec, *Scalar, *Index; 2112 if (match(SrcOp, m_OneUse(m_InsertElt(m_IntToPtr(m_Value(Vec)), 2113 m_Value(Scalar), m_Value(Index)))) && 2114 Vec->getType() == Ty) { 2115 assert(Vec->getType()->getScalarSizeInBits() == PtrSize && "Wrong type"); 2116 // Convert the scalar to int followed by insert to eliminate one cast: 2117 // p2i (ins (i2p Vec), Scalar, Index --> ins Vec, (p2i Scalar), Index 2118 Value *NewCast = Builder.CreatePtrToInt(Scalar, Ty->getScalarType()); 2119 return InsertElementInst::Create(Vec, NewCast, Index); 2120 } 2121 2122 return commonPointerCastTransforms(CI); 2123 } 2124 2125 /// This input value (which is known to have vector type) is being zero extended 2126 /// or truncated to the specified vector type. Since the zext/trunc is done 2127 /// using an integer type, we have a (bitcast(cast(bitcast))) pattern, 2128 /// endianness will impact which end of the vector that is extended or 2129 /// truncated. 2130 /// 2131 /// A vector is always stored with index 0 at the lowest address, which 2132 /// corresponds to the most significant bits for a big endian stored integer and 2133 /// the least significant bits for little endian. A trunc/zext of an integer 2134 /// impacts the big end of the integer. Thus, we need to add/remove elements at 2135 /// the front of the vector for big endian targets, and the back of the vector 2136 /// for little endian targets. 2137 /// 2138 /// Try to replace it with a shuffle (and vector/vector bitcast) if possible. 2139 /// 2140 /// The source and destination vector types may have different element types. 2141 static Instruction * 2142 optimizeVectorResizeWithIntegerBitCasts(Value *InVal, VectorType *DestTy, 2143 InstCombinerImpl &IC) { 2144 // We can only do this optimization if the output is a multiple of the input 2145 // element size, or the input is a multiple of the output element size. 2146 // Convert the input type to have the same element type as the output. 2147 VectorType *SrcTy = cast<VectorType>(InVal->getType()); 2148 2149 if (SrcTy->getElementType() != DestTy->getElementType()) { 2150 // The input types don't need to be identical, but for now they must be the 2151 // same size. There is no specific reason we couldn't handle things like 2152 // <4 x i16> -> <4 x i32> by bitcasting to <2 x i32> but haven't gotten 2153 // there yet. 2154 if (SrcTy->getElementType()->getPrimitiveSizeInBits() != 2155 DestTy->getElementType()->getPrimitiveSizeInBits()) 2156 return nullptr; 2157 2158 SrcTy = 2159 FixedVectorType::get(DestTy->getElementType(), 2160 cast<FixedVectorType>(SrcTy)->getNumElements()); 2161 InVal = IC.Builder.CreateBitCast(InVal, SrcTy); 2162 } 2163 2164 bool IsBigEndian = IC.getDataLayout().isBigEndian(); 2165 unsigned SrcElts = cast<FixedVectorType>(SrcTy)->getNumElements(); 2166 unsigned DestElts = cast<FixedVectorType>(DestTy)->getNumElements(); 2167 2168 assert(SrcElts != DestElts && "Element counts should be different."); 2169 2170 // Now that the element types match, get the shuffle mask and RHS of the 2171 // shuffle to use, which depends on whether we're increasing or decreasing the 2172 // size of the input. 2173 auto ShuffleMaskStorage = llvm::to_vector<16>(llvm::seq<int>(0, SrcElts)); 2174 ArrayRef<int> ShuffleMask; 2175 Value *V2; 2176 2177 if (SrcElts > DestElts) { 2178 // If we're shrinking the number of elements (rewriting an integer 2179 // truncate), just shuffle in the elements corresponding to the least 2180 // significant bits from the input and use poison as the second shuffle 2181 // input. 2182 V2 = PoisonValue::get(SrcTy); 2183 // Make sure the shuffle mask selects the "least significant bits" by 2184 // keeping elements from back of the src vector for big endian, and from the 2185 // front for little endian. 2186 ShuffleMask = ShuffleMaskStorage; 2187 if (IsBigEndian) 2188 ShuffleMask = ShuffleMask.take_back(DestElts); 2189 else 2190 ShuffleMask = ShuffleMask.take_front(DestElts); 2191 } else { 2192 // If we're increasing the number of elements (rewriting an integer zext), 2193 // shuffle in all of the elements from InVal. Fill the rest of the result 2194 // elements with zeros from a constant zero. 2195 V2 = Constant::getNullValue(SrcTy); 2196 // Use first elt from V2 when indicating zero in the shuffle mask. 2197 uint32_t NullElt = SrcElts; 2198 // Extend with null values in the "most significant bits" by adding elements 2199 // in front of the src vector for big endian, and at the back for little 2200 // endian. 2201 unsigned DeltaElts = DestElts - SrcElts; 2202 if (IsBigEndian) 2203 ShuffleMaskStorage.insert(ShuffleMaskStorage.begin(), DeltaElts, NullElt); 2204 else 2205 ShuffleMaskStorage.append(DeltaElts, NullElt); 2206 ShuffleMask = ShuffleMaskStorage; 2207 } 2208 2209 return new ShuffleVectorInst(InVal, V2, ShuffleMask); 2210 } 2211 2212 static bool isMultipleOfTypeSize(unsigned Value, Type *Ty) { 2213 return Value % Ty->getPrimitiveSizeInBits() == 0; 2214 } 2215 2216 static unsigned getTypeSizeIndex(unsigned Value, Type *Ty) { 2217 return Value / Ty->getPrimitiveSizeInBits(); 2218 } 2219 2220 /// V is a value which is inserted into a vector of VecEltTy. 2221 /// Look through the value to see if we can decompose it into 2222 /// insertions into the vector. See the example in the comment for 2223 /// OptimizeIntegerToVectorInsertions for the pattern this handles. 2224 /// The type of V is always a non-zero multiple of VecEltTy's size. 2225 /// Shift is the number of bits between the lsb of V and the lsb of 2226 /// the vector. 2227 /// 2228 /// This returns false if the pattern can't be matched or true if it can, 2229 /// filling in Elements with the elements found here. 2230 static bool collectInsertionElements(Value *V, unsigned Shift, 2231 SmallVectorImpl<Value *> &Elements, 2232 Type *VecEltTy, bool isBigEndian) { 2233 assert(isMultipleOfTypeSize(Shift, VecEltTy) && 2234 "Shift should be a multiple of the element type size"); 2235 2236 // Undef values never contribute useful bits to the result. 2237 if (isa<UndefValue>(V)) return true; 2238 2239 // If we got down to a value of the right type, we win, try inserting into the 2240 // right element. 2241 if (V->getType() == VecEltTy) { 2242 // Inserting null doesn't actually insert any elements. 2243 if (Constant *C = dyn_cast<Constant>(V)) 2244 if (C->isNullValue()) 2245 return true; 2246 2247 unsigned ElementIndex = getTypeSizeIndex(Shift, VecEltTy); 2248 if (isBigEndian) 2249 ElementIndex = Elements.size() - ElementIndex - 1; 2250 2251 // Fail if multiple elements are inserted into this slot. 2252 if (Elements[ElementIndex]) 2253 return false; 2254 2255 Elements[ElementIndex] = V; 2256 return true; 2257 } 2258 2259 if (Constant *C = dyn_cast<Constant>(V)) { 2260 // Figure out the # elements this provides, and bitcast it or slice it up 2261 // as required. 2262 unsigned NumElts = getTypeSizeIndex(C->getType()->getPrimitiveSizeInBits(), 2263 VecEltTy); 2264 // If the constant is the size of a vector element, we just need to bitcast 2265 // it to the right type so it gets properly inserted. 2266 if (NumElts == 1) 2267 return collectInsertionElements(ConstantExpr::getBitCast(C, VecEltTy), 2268 Shift, Elements, VecEltTy, isBigEndian); 2269 2270 // Okay, this is a constant that covers multiple elements. Slice it up into 2271 // pieces and insert each element-sized piece into the vector. 2272 if (!isa<IntegerType>(C->getType())) 2273 C = ConstantExpr::getBitCast(C, IntegerType::get(V->getContext(), 2274 C->getType()->getPrimitiveSizeInBits())); 2275 unsigned ElementSize = VecEltTy->getPrimitiveSizeInBits(); 2276 Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize); 2277 2278 for (unsigned i = 0; i != NumElts; ++i) { 2279 unsigned ShiftI = Shift+i*ElementSize; 2280 Constant *Piece = ConstantExpr::getLShr(C, ConstantInt::get(C->getType(), 2281 ShiftI)); 2282 Piece = ConstantExpr::getTrunc(Piece, ElementIntTy); 2283 if (!collectInsertionElements(Piece, ShiftI, Elements, VecEltTy, 2284 isBigEndian)) 2285 return false; 2286 } 2287 return true; 2288 } 2289 2290 if (!V->hasOneUse()) return false; 2291 2292 Instruction *I = dyn_cast<Instruction>(V); 2293 if (!I) return false; 2294 switch (I->getOpcode()) { 2295 default: return false; // Unhandled case. 2296 case Instruction::BitCast: 2297 if (I->getOperand(0)->getType()->isVectorTy()) 2298 return false; 2299 return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy, 2300 isBigEndian); 2301 case Instruction::ZExt: 2302 if (!isMultipleOfTypeSize( 2303 I->getOperand(0)->getType()->getPrimitiveSizeInBits(), 2304 VecEltTy)) 2305 return false; 2306 return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy, 2307 isBigEndian); 2308 case Instruction::Or: 2309 return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy, 2310 isBigEndian) && 2311 collectInsertionElements(I->getOperand(1), Shift, Elements, VecEltTy, 2312 isBigEndian); 2313 case Instruction::Shl: { 2314 // Must be shifting by a constant that is a multiple of the element size. 2315 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1)); 2316 if (!CI) return false; 2317 Shift += CI->getZExtValue(); 2318 if (!isMultipleOfTypeSize(Shift, VecEltTy)) return false; 2319 return collectInsertionElements(I->getOperand(0), Shift, Elements, VecEltTy, 2320 isBigEndian); 2321 } 2322 2323 } 2324 } 2325 2326 2327 /// If the input is an 'or' instruction, we may be doing shifts and ors to 2328 /// assemble the elements of the vector manually. 2329 /// Try to rip the code out and replace it with insertelements. This is to 2330 /// optimize code like this: 2331 /// 2332 /// %tmp37 = bitcast float %inc to i32 2333 /// %tmp38 = zext i32 %tmp37 to i64 2334 /// %tmp31 = bitcast float %inc5 to i32 2335 /// %tmp32 = zext i32 %tmp31 to i64 2336 /// %tmp33 = shl i64 %tmp32, 32 2337 /// %ins35 = or i64 %tmp33, %tmp38 2338 /// %tmp43 = bitcast i64 %ins35 to <2 x float> 2339 /// 2340 /// Into two insertelements that do "buildvector{%inc, %inc5}". 2341 static Value *optimizeIntegerToVectorInsertions(BitCastInst &CI, 2342 InstCombinerImpl &IC) { 2343 auto *DestVecTy = cast<FixedVectorType>(CI.getType()); 2344 Value *IntInput = CI.getOperand(0); 2345 2346 SmallVector<Value*, 8> Elements(DestVecTy->getNumElements()); 2347 if (!collectInsertionElements(IntInput, 0, Elements, 2348 DestVecTy->getElementType(), 2349 IC.getDataLayout().isBigEndian())) 2350 return nullptr; 2351 2352 // If we succeeded, we know that all of the element are specified by Elements 2353 // or are zero if Elements has a null entry. Recast this as a set of 2354 // insertions. 2355 Value *Result = Constant::getNullValue(CI.getType()); 2356 for (unsigned i = 0, e = Elements.size(); i != e; ++i) { 2357 if (!Elements[i]) continue; // Unset element. 2358 2359 Result = IC.Builder.CreateInsertElement(Result, Elements[i], 2360 IC.Builder.getInt32(i)); 2361 } 2362 2363 return Result; 2364 } 2365 2366 /// Canonicalize scalar bitcasts of extracted elements into a bitcast of the 2367 /// vector followed by extract element. The backend tends to handle bitcasts of 2368 /// vectors better than bitcasts of scalars because vector registers are 2369 /// usually not type-specific like scalar integer or scalar floating-point. 2370 static Instruction *canonicalizeBitCastExtElt(BitCastInst &BitCast, 2371 InstCombinerImpl &IC) { 2372 Value *VecOp, *Index; 2373 if (!match(BitCast.getOperand(0), 2374 m_OneUse(m_ExtractElt(m_Value(VecOp), m_Value(Index))))) 2375 return nullptr; 2376 2377 // The bitcast must be to a vectorizable type, otherwise we can't make a new 2378 // type to extract from. 2379 Type *DestType = BitCast.getType(); 2380 VectorType *VecType = cast<VectorType>(VecOp->getType()); 2381 if (VectorType::isValidElementType(DestType)) { 2382 auto *NewVecType = VectorType::get(DestType, VecType); 2383 auto *NewBC = IC.Builder.CreateBitCast(VecOp, NewVecType, "bc"); 2384 return ExtractElementInst::Create(NewBC, Index); 2385 } 2386 2387 // Only solve DestType is vector to avoid inverse transform in visitBitCast. 2388 // bitcast (extractelement <1 x elt>, dest) -> bitcast(<1 x elt>, dest) 2389 auto *FixedVType = dyn_cast<FixedVectorType>(VecType); 2390 if (DestType->isVectorTy() && FixedVType && FixedVType->getNumElements() == 1) 2391 return CastInst::Create(Instruction::BitCast, VecOp, DestType); 2392 2393 return nullptr; 2394 } 2395 2396 /// Change the type of a bitwise logic operation if we can eliminate a bitcast. 2397 static Instruction *foldBitCastBitwiseLogic(BitCastInst &BitCast, 2398 InstCombiner::BuilderTy &Builder) { 2399 Type *DestTy = BitCast.getType(); 2400 BinaryOperator *BO; 2401 2402 if (!match(BitCast.getOperand(0), m_OneUse(m_BinOp(BO))) || 2403 !BO->isBitwiseLogicOp()) 2404 return nullptr; 2405 2406 // FIXME: This transform is restricted to vector types to avoid backend 2407 // problems caused by creating potentially illegal operations. If a fix-up is 2408 // added to handle that situation, we can remove this check. 2409 if (!DestTy->isVectorTy() || !BO->getType()->isVectorTy()) 2410 return nullptr; 2411 2412 if (DestTy->isFPOrFPVectorTy()) { 2413 Value *X, *Y; 2414 // bitcast(logic(bitcast(X), bitcast(Y))) -> bitcast'(logic(bitcast'(X), Y)) 2415 if (match(BO->getOperand(0), m_OneUse(m_BitCast(m_Value(X)))) && 2416 match(BO->getOperand(1), m_OneUse(m_BitCast(m_Value(Y))))) { 2417 if (X->getType()->isFPOrFPVectorTy() && 2418 Y->getType()->isIntOrIntVectorTy()) { 2419 Value *CastedOp = 2420 Builder.CreateBitCast(BO->getOperand(0), Y->getType()); 2421 Value *NewBO = Builder.CreateBinOp(BO->getOpcode(), CastedOp, Y); 2422 return CastInst::CreateBitOrPointerCast(NewBO, DestTy); 2423 } 2424 if (X->getType()->isIntOrIntVectorTy() && 2425 Y->getType()->isFPOrFPVectorTy()) { 2426 Value *CastedOp = 2427 Builder.CreateBitCast(BO->getOperand(1), X->getType()); 2428 Value *NewBO = Builder.CreateBinOp(BO->getOpcode(), CastedOp, X); 2429 return CastInst::CreateBitOrPointerCast(NewBO, DestTy); 2430 } 2431 } 2432 return nullptr; 2433 } 2434 2435 if (!DestTy->isIntOrIntVectorTy()) 2436 return nullptr; 2437 2438 Value *X; 2439 if (match(BO->getOperand(0), m_OneUse(m_BitCast(m_Value(X)))) && 2440 X->getType() == DestTy && !isa<Constant>(X)) { 2441 // bitcast(logic(bitcast(X), Y)) --> logic'(X, bitcast(Y)) 2442 Value *CastedOp1 = Builder.CreateBitCast(BO->getOperand(1), DestTy); 2443 return BinaryOperator::Create(BO->getOpcode(), X, CastedOp1); 2444 } 2445 2446 if (match(BO->getOperand(1), m_OneUse(m_BitCast(m_Value(X)))) && 2447 X->getType() == DestTy && !isa<Constant>(X)) { 2448 // bitcast(logic(Y, bitcast(X))) --> logic'(bitcast(Y), X) 2449 Value *CastedOp0 = Builder.CreateBitCast(BO->getOperand(0), DestTy); 2450 return BinaryOperator::Create(BO->getOpcode(), CastedOp0, X); 2451 } 2452 2453 // Canonicalize vector bitcasts to come before vector bitwise logic with a 2454 // constant. This eases recognition of special constants for later ops. 2455 // Example: 2456 // icmp u/s (a ^ signmask), (b ^ signmask) --> icmp s/u a, b 2457 Constant *C; 2458 if (match(BO->getOperand(1), m_Constant(C))) { 2459 // bitcast (logic X, C) --> logic (bitcast X, C') 2460 Value *CastedOp0 = Builder.CreateBitCast(BO->getOperand(0), DestTy); 2461 Value *CastedC = Builder.CreateBitCast(C, DestTy); 2462 return BinaryOperator::Create(BO->getOpcode(), CastedOp0, CastedC); 2463 } 2464 2465 return nullptr; 2466 } 2467 2468 /// Change the type of a select if we can eliminate a bitcast. 2469 static Instruction *foldBitCastSelect(BitCastInst &BitCast, 2470 InstCombiner::BuilderTy &Builder) { 2471 Value *Cond, *TVal, *FVal; 2472 if (!match(BitCast.getOperand(0), 2473 m_OneUse(m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal))))) 2474 return nullptr; 2475 2476 // A vector select must maintain the same number of elements in its operands. 2477 Type *CondTy = Cond->getType(); 2478 Type *DestTy = BitCast.getType(); 2479 if (auto *CondVTy = dyn_cast<VectorType>(CondTy)) 2480 if (!DestTy->isVectorTy() || 2481 CondVTy->getElementCount() != 2482 cast<VectorType>(DestTy)->getElementCount()) 2483 return nullptr; 2484 2485 // FIXME: This transform is restricted from changing the select between 2486 // scalars and vectors to avoid backend problems caused by creating 2487 // potentially illegal operations. If a fix-up is added to handle that 2488 // situation, we can remove this check. 2489 if (DestTy->isVectorTy() != TVal->getType()->isVectorTy()) 2490 return nullptr; 2491 2492 auto *Sel = cast<Instruction>(BitCast.getOperand(0)); 2493 Value *X; 2494 if (match(TVal, m_OneUse(m_BitCast(m_Value(X)))) && X->getType() == DestTy && 2495 !isa<Constant>(X)) { 2496 // bitcast(select(Cond, bitcast(X), Y)) --> select'(Cond, X, bitcast(Y)) 2497 Value *CastedVal = Builder.CreateBitCast(FVal, DestTy); 2498 return SelectInst::Create(Cond, X, CastedVal, "", nullptr, Sel); 2499 } 2500 2501 if (match(FVal, m_OneUse(m_BitCast(m_Value(X)))) && X->getType() == DestTy && 2502 !isa<Constant>(X)) { 2503 // bitcast(select(Cond, Y, bitcast(X))) --> select'(Cond, bitcast(Y), X) 2504 Value *CastedVal = Builder.CreateBitCast(TVal, DestTy); 2505 return SelectInst::Create(Cond, CastedVal, X, "", nullptr, Sel); 2506 } 2507 2508 return nullptr; 2509 } 2510 2511 /// Check if all users of CI are StoreInsts. 2512 static bool hasStoreUsersOnly(CastInst &CI) { 2513 for (User *U : CI.users()) { 2514 if (!isa<StoreInst>(U)) 2515 return false; 2516 } 2517 return true; 2518 } 2519 2520 /// This function handles following case 2521 /// 2522 /// A -> B cast 2523 /// PHI 2524 /// B -> A cast 2525 /// 2526 /// All the related PHI nodes can be replaced by new PHI nodes with type A. 2527 /// The uses of \p CI can be changed to the new PHI node corresponding to \p PN. 2528 Instruction *InstCombinerImpl::optimizeBitCastFromPhi(CastInst &CI, 2529 PHINode *PN) { 2530 // BitCast used by Store can be handled in InstCombineLoadStoreAlloca.cpp. 2531 if (hasStoreUsersOnly(CI)) 2532 return nullptr; 2533 2534 Value *Src = CI.getOperand(0); 2535 Type *SrcTy = Src->getType(); // Type B 2536 Type *DestTy = CI.getType(); // Type A 2537 2538 SmallVector<PHINode *, 4> PhiWorklist; 2539 SmallSetVector<PHINode *, 4> OldPhiNodes; 2540 2541 // Find all of the A->B casts and PHI nodes. 2542 // We need to inspect all related PHI nodes, but PHIs can be cyclic, so 2543 // OldPhiNodes is used to track all known PHI nodes, before adding a new 2544 // PHI to PhiWorklist, it is checked against and added to OldPhiNodes first. 2545 PhiWorklist.push_back(PN); 2546 OldPhiNodes.insert(PN); 2547 while (!PhiWorklist.empty()) { 2548 auto *OldPN = PhiWorklist.pop_back_val(); 2549 for (Value *IncValue : OldPN->incoming_values()) { 2550 if (isa<Constant>(IncValue)) 2551 continue; 2552 2553 if (auto *LI = dyn_cast<LoadInst>(IncValue)) { 2554 // If there is a sequence of one or more load instructions, each loaded 2555 // value is used as address of later load instruction, bitcast is 2556 // necessary to change the value type, don't optimize it. For 2557 // simplicity we give up if the load address comes from another load. 2558 Value *Addr = LI->getOperand(0); 2559 if (Addr == &CI || isa<LoadInst>(Addr)) 2560 return nullptr; 2561 // Don't tranform "load <256 x i32>, <256 x i32>*" to 2562 // "load x86_amx, x86_amx*", because x86_amx* is invalid. 2563 // TODO: Remove this check when bitcast between vector and x86_amx 2564 // is replaced with a specific intrinsic. 2565 if (DestTy->isX86_AMXTy()) 2566 return nullptr; 2567 if (LI->hasOneUse() && LI->isSimple()) 2568 continue; 2569 // If a LoadInst has more than one use, changing the type of loaded 2570 // value may create another bitcast. 2571 return nullptr; 2572 } 2573 2574 if (auto *PNode = dyn_cast<PHINode>(IncValue)) { 2575 if (OldPhiNodes.insert(PNode)) 2576 PhiWorklist.push_back(PNode); 2577 continue; 2578 } 2579 2580 auto *BCI = dyn_cast<BitCastInst>(IncValue); 2581 // We can't handle other instructions. 2582 if (!BCI) 2583 return nullptr; 2584 2585 // Verify it's a A->B cast. 2586 Type *TyA = BCI->getOperand(0)->getType(); 2587 Type *TyB = BCI->getType(); 2588 if (TyA != DestTy || TyB != SrcTy) 2589 return nullptr; 2590 } 2591 } 2592 2593 // Check that each user of each old PHI node is something that we can 2594 // rewrite, so that all of the old PHI nodes can be cleaned up afterwards. 2595 for (auto *OldPN : OldPhiNodes) { 2596 for (User *V : OldPN->users()) { 2597 if (auto *SI = dyn_cast<StoreInst>(V)) { 2598 if (!SI->isSimple() || SI->getOperand(0) != OldPN) 2599 return nullptr; 2600 } else if (auto *BCI = dyn_cast<BitCastInst>(V)) { 2601 // Verify it's a B->A cast. 2602 Type *TyB = BCI->getOperand(0)->getType(); 2603 Type *TyA = BCI->getType(); 2604 if (TyA != DestTy || TyB != SrcTy) 2605 return nullptr; 2606 } else if (auto *PHI = dyn_cast<PHINode>(V)) { 2607 // As long as the user is another old PHI node, then even if we don't 2608 // rewrite it, the PHI web we're considering won't have any users 2609 // outside itself, so it'll be dead. 2610 if (!OldPhiNodes.contains(PHI)) 2611 return nullptr; 2612 } else { 2613 return nullptr; 2614 } 2615 } 2616 } 2617 2618 // For each old PHI node, create a corresponding new PHI node with a type A. 2619 SmallDenseMap<PHINode *, PHINode *> NewPNodes; 2620 for (auto *OldPN : OldPhiNodes) { 2621 Builder.SetInsertPoint(OldPN); 2622 PHINode *NewPN = Builder.CreatePHI(DestTy, OldPN->getNumOperands()); 2623 NewPNodes[OldPN] = NewPN; 2624 } 2625 2626 // Fill in the operands of new PHI nodes. 2627 for (auto *OldPN : OldPhiNodes) { 2628 PHINode *NewPN = NewPNodes[OldPN]; 2629 for (unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) { 2630 Value *V = OldPN->getOperand(j); 2631 Value *NewV = nullptr; 2632 if (auto *C = dyn_cast<Constant>(V)) { 2633 NewV = ConstantExpr::getBitCast(C, DestTy); 2634 } else if (auto *LI = dyn_cast<LoadInst>(V)) { 2635 // Explicitly perform load combine to make sure no opposing transform 2636 // can remove the bitcast in the meantime and trigger an infinite loop. 2637 Builder.SetInsertPoint(LI); 2638 NewV = combineLoadToNewType(*LI, DestTy); 2639 // Remove the old load and its use in the old phi, which itself becomes 2640 // dead once the whole transform finishes. 2641 replaceInstUsesWith(*LI, PoisonValue::get(LI->getType())); 2642 eraseInstFromFunction(*LI); 2643 } else if (auto *BCI = dyn_cast<BitCastInst>(V)) { 2644 NewV = BCI->getOperand(0); 2645 } else if (auto *PrevPN = dyn_cast<PHINode>(V)) { 2646 NewV = NewPNodes[PrevPN]; 2647 } 2648 assert(NewV); 2649 NewPN->addIncoming(NewV, OldPN->getIncomingBlock(j)); 2650 } 2651 } 2652 2653 // Traverse all accumulated PHI nodes and process its users, 2654 // which are Stores and BitcCasts. Without this processing 2655 // NewPHI nodes could be replicated and could lead to extra 2656 // moves generated after DeSSA. 2657 // If there is a store with type B, change it to type A. 2658 2659 2660 // Replace users of BitCast B->A with NewPHI. These will help 2661 // later to get rid off a closure formed by OldPHI nodes. 2662 Instruction *RetVal = nullptr; 2663 for (auto *OldPN : OldPhiNodes) { 2664 PHINode *NewPN = NewPNodes[OldPN]; 2665 for (User *V : make_early_inc_range(OldPN->users())) { 2666 if (auto *SI = dyn_cast<StoreInst>(V)) { 2667 assert(SI->isSimple() && SI->getOperand(0) == OldPN); 2668 Builder.SetInsertPoint(SI); 2669 auto *NewBC = 2670 cast<BitCastInst>(Builder.CreateBitCast(NewPN, SrcTy)); 2671 SI->setOperand(0, NewBC); 2672 Worklist.push(SI); 2673 assert(hasStoreUsersOnly(*NewBC)); 2674 } 2675 else if (auto *BCI = dyn_cast<BitCastInst>(V)) { 2676 Type *TyB = BCI->getOperand(0)->getType(); 2677 Type *TyA = BCI->getType(); 2678 assert(TyA == DestTy && TyB == SrcTy); 2679 (void) TyA; 2680 (void) TyB; 2681 Instruction *I = replaceInstUsesWith(*BCI, NewPN); 2682 if (BCI == &CI) 2683 RetVal = I; 2684 } else if (auto *PHI = dyn_cast<PHINode>(V)) { 2685 assert(OldPhiNodes.contains(PHI)); 2686 (void) PHI; 2687 } else { 2688 llvm_unreachable("all uses should be handled"); 2689 } 2690 } 2691 } 2692 2693 return RetVal; 2694 } 2695 2696 static Instruction *convertBitCastToGEP(BitCastInst &CI, IRBuilderBase &Builder, 2697 const DataLayout &DL) { 2698 Value *Src = CI.getOperand(0); 2699 PointerType *SrcPTy = cast<PointerType>(Src->getType()); 2700 PointerType *DstPTy = cast<PointerType>(CI.getType()); 2701 2702 // Bitcasts involving opaque pointers cannot be converted into a GEP. 2703 if (SrcPTy->isOpaque() || DstPTy->isOpaque()) 2704 return nullptr; 2705 2706 Type *DstElTy = DstPTy->getNonOpaquePointerElementType(); 2707 Type *SrcElTy = SrcPTy->getNonOpaquePointerElementType(); 2708 2709 // When the type pointed to is not sized the cast cannot be 2710 // turned into a gep. 2711 if (!SrcElTy->isSized()) 2712 return nullptr; 2713 2714 // If the source and destination are pointers, and this cast is equivalent 2715 // to a getelementptr X, 0, 0, 0... turn it into the appropriate gep. 2716 // This can enhance SROA and other transforms that want type-safe pointers. 2717 unsigned NumZeros = 0; 2718 while (SrcElTy && SrcElTy != DstElTy) { 2719 SrcElTy = GetElementPtrInst::getTypeAtIndex(SrcElTy, (uint64_t)0); 2720 ++NumZeros; 2721 } 2722 2723 // If we found a path from the src to dest, create the getelementptr now. 2724 if (SrcElTy == DstElTy) { 2725 SmallVector<Value *, 8> Idxs(NumZeros + 1, Builder.getInt32(0)); 2726 GetElementPtrInst *GEP = GetElementPtrInst::Create( 2727 SrcPTy->getNonOpaquePointerElementType(), Src, Idxs); 2728 2729 // If the source pointer is dereferenceable, then assume it points to an 2730 // allocated object and apply "inbounds" to the GEP. 2731 bool CanBeNull, CanBeFreed; 2732 if (Src->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed)) { 2733 // In a non-default address space (not 0), a null pointer can not be 2734 // assumed inbounds, so ignore that case (dereferenceable_or_null). 2735 // The reason is that 'null' is not treated differently in these address 2736 // spaces, and we consequently ignore the 'gep inbounds' special case 2737 // for 'null' which allows 'inbounds' on 'null' if the indices are 2738 // zeros. 2739 if (SrcPTy->getAddressSpace() == 0 || !CanBeNull) 2740 GEP->setIsInBounds(); 2741 } 2742 return GEP; 2743 } 2744 return nullptr; 2745 } 2746 2747 Instruction *InstCombinerImpl::visitBitCast(BitCastInst &CI) { 2748 // If the operands are integer typed then apply the integer transforms, 2749 // otherwise just apply the common ones. 2750 Value *Src = CI.getOperand(0); 2751 Type *SrcTy = Src->getType(); 2752 Type *DestTy = CI.getType(); 2753 2754 // Get rid of casts from one type to the same type. These are useless and can 2755 // be replaced by the operand. 2756 if (DestTy == Src->getType()) 2757 return replaceInstUsesWith(CI, Src); 2758 2759 if (isa<PointerType>(SrcTy) && isa<PointerType>(DestTy)) { 2760 // If we are casting a alloca to a pointer to a type of the same 2761 // size, rewrite the allocation instruction to allocate the "right" type. 2762 // There is no need to modify malloc calls because it is their bitcast that 2763 // needs to be cleaned up. 2764 if (AllocaInst *AI = dyn_cast<AllocaInst>(Src)) 2765 if (Instruction *V = PromoteCastOfAllocation(CI, *AI)) 2766 return V; 2767 2768 if (Instruction *I = convertBitCastToGEP(CI, Builder, DL)) 2769 return I; 2770 } 2771 2772 if (FixedVectorType *DestVTy = dyn_cast<FixedVectorType>(DestTy)) { 2773 // Beware: messing with this target-specific oddity may cause trouble. 2774 if (DestVTy->getNumElements() == 1 && SrcTy->isX86_MMXTy()) { 2775 Value *Elem = Builder.CreateBitCast(Src, DestVTy->getElementType()); 2776 return InsertElementInst::Create(PoisonValue::get(DestTy), Elem, 2777 Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); 2778 } 2779 2780 if (isa<IntegerType>(SrcTy)) { 2781 // If this is a cast from an integer to vector, check to see if the input 2782 // is a trunc or zext of a bitcast from vector. If so, we can replace all 2783 // the casts with a shuffle and (potentially) a bitcast. 2784 if (isa<TruncInst>(Src) || isa<ZExtInst>(Src)) { 2785 CastInst *SrcCast = cast<CastInst>(Src); 2786 if (BitCastInst *BCIn = dyn_cast<BitCastInst>(SrcCast->getOperand(0))) 2787 if (isa<VectorType>(BCIn->getOperand(0)->getType())) 2788 if (Instruction *I = optimizeVectorResizeWithIntegerBitCasts( 2789 BCIn->getOperand(0), cast<VectorType>(DestTy), *this)) 2790 return I; 2791 } 2792 2793 // If the input is an 'or' instruction, we may be doing shifts and ors to 2794 // assemble the elements of the vector manually. Try to rip the code out 2795 // and replace it with insertelements. 2796 if (Value *V = optimizeIntegerToVectorInsertions(CI, *this)) 2797 return replaceInstUsesWith(CI, V); 2798 } 2799 } 2800 2801 if (FixedVectorType *SrcVTy = dyn_cast<FixedVectorType>(SrcTy)) { 2802 if (SrcVTy->getNumElements() == 1) { 2803 // If our destination is not a vector, then make this a straight 2804 // scalar-scalar cast. 2805 if (!DestTy->isVectorTy()) { 2806 Value *Elem = 2807 Builder.CreateExtractElement(Src, 2808 Constant::getNullValue(Type::getInt32Ty(CI.getContext()))); 2809 return CastInst::Create(Instruction::BitCast, Elem, DestTy); 2810 } 2811 2812 // Otherwise, see if our source is an insert. If so, then use the scalar 2813 // component directly: 2814 // bitcast (inselt <1 x elt> V, X, 0) to <n x m> --> bitcast X to <n x m> 2815 if (auto *InsElt = dyn_cast<InsertElementInst>(Src)) 2816 return new BitCastInst(InsElt->getOperand(1), DestTy); 2817 } 2818 2819 // Convert an artificial vector insert into more analyzable bitwise logic. 2820 unsigned BitWidth = DestTy->getScalarSizeInBits(); 2821 Value *X, *Y; 2822 uint64_t IndexC; 2823 if (match(Src, m_OneUse(m_InsertElt(m_OneUse(m_BitCast(m_Value(X))), 2824 m_Value(Y), m_ConstantInt(IndexC)))) && 2825 DestTy->isIntegerTy() && X->getType() == DestTy && 2826 Y->getType()->isIntegerTy() && isDesirableIntType(BitWidth)) { 2827 // Adjust for big endian - the LSBs are at the high index. 2828 if (DL.isBigEndian()) 2829 IndexC = SrcVTy->getNumElements() - 1 - IndexC; 2830 2831 // We only handle (endian-normalized) insert to index 0. Any other insert 2832 // would require a left-shift, so that is an extra instruction. 2833 if (IndexC == 0) { 2834 // bitcast (inselt (bitcast X), Y, 0) --> or (and X, MaskC), (zext Y) 2835 unsigned EltWidth = Y->getType()->getScalarSizeInBits(); 2836 APInt MaskC = APInt::getHighBitsSet(BitWidth, BitWidth - EltWidth); 2837 Value *AndX = Builder.CreateAnd(X, MaskC); 2838 Value *ZextY = Builder.CreateZExt(Y, DestTy); 2839 return BinaryOperator::CreateOr(AndX, ZextY); 2840 } 2841 } 2842 } 2843 2844 if (auto *Shuf = dyn_cast<ShuffleVectorInst>(Src)) { 2845 // Okay, we have (bitcast (shuffle ..)). Check to see if this is 2846 // a bitcast to a vector with the same # elts. 2847 Value *ShufOp0 = Shuf->getOperand(0); 2848 Value *ShufOp1 = Shuf->getOperand(1); 2849 auto ShufElts = cast<VectorType>(Shuf->getType())->getElementCount(); 2850 auto SrcVecElts = cast<VectorType>(ShufOp0->getType())->getElementCount(); 2851 if (Shuf->hasOneUse() && DestTy->isVectorTy() && 2852 cast<VectorType>(DestTy)->getElementCount() == ShufElts && 2853 ShufElts == SrcVecElts) { 2854 BitCastInst *Tmp; 2855 // If either of the operands is a cast from CI.getType(), then 2856 // evaluating the shuffle in the casted destination's type will allow 2857 // us to eliminate at least one cast. 2858 if (((Tmp = dyn_cast<BitCastInst>(ShufOp0)) && 2859 Tmp->getOperand(0)->getType() == DestTy) || 2860 ((Tmp = dyn_cast<BitCastInst>(ShufOp1)) && 2861 Tmp->getOperand(0)->getType() == DestTy)) { 2862 Value *LHS = Builder.CreateBitCast(ShufOp0, DestTy); 2863 Value *RHS = Builder.CreateBitCast(ShufOp1, DestTy); 2864 // Return a new shuffle vector. Use the same element ID's, as we 2865 // know the vector types match #elts. 2866 return new ShuffleVectorInst(LHS, RHS, Shuf->getShuffleMask()); 2867 } 2868 } 2869 2870 // A bitcasted-to-scalar and byte/bit reversing shuffle is better recognized 2871 // as a byte/bit swap: 2872 // bitcast <N x i8> (shuf X, undef, <N, N-1,...0>) -> bswap (bitcast X) 2873 // bitcast <N x i1> (shuf X, undef, <N, N-1,...0>) -> bitreverse (bitcast X) 2874 if (DestTy->isIntegerTy() && ShufElts.getKnownMinValue() % 2 == 0 && 2875 Shuf->hasOneUse() && Shuf->isReverse()) { 2876 unsigned IntrinsicNum = 0; 2877 if (DL.isLegalInteger(DestTy->getScalarSizeInBits()) && 2878 SrcTy->getScalarSizeInBits() == 8) { 2879 IntrinsicNum = Intrinsic::bswap; 2880 } else if (SrcTy->getScalarSizeInBits() == 1) { 2881 IntrinsicNum = Intrinsic::bitreverse; 2882 } 2883 if (IntrinsicNum != 0) { 2884 assert(ShufOp0->getType() == SrcTy && "Unexpected shuffle mask"); 2885 assert(match(ShufOp1, m_Undef()) && "Unexpected shuffle op"); 2886 Function *BswapOrBitreverse = 2887 Intrinsic::getDeclaration(CI.getModule(), IntrinsicNum, DestTy); 2888 Value *ScalarX = Builder.CreateBitCast(ShufOp0, DestTy); 2889 return CallInst::Create(BswapOrBitreverse, {ScalarX}); 2890 } 2891 } 2892 } 2893 2894 // Handle the A->B->A cast, and there is an intervening PHI node. 2895 if (PHINode *PN = dyn_cast<PHINode>(Src)) 2896 if (Instruction *I = optimizeBitCastFromPhi(CI, PN)) 2897 return I; 2898 2899 if (Instruction *I = canonicalizeBitCastExtElt(CI, *this)) 2900 return I; 2901 2902 if (Instruction *I = foldBitCastBitwiseLogic(CI, Builder)) 2903 return I; 2904 2905 if (Instruction *I = foldBitCastSelect(CI, Builder)) 2906 return I; 2907 2908 if (SrcTy->isPointerTy()) 2909 return commonPointerCastTransforms(CI); 2910 return commonCastTransforms(CI); 2911 } 2912 2913 Instruction *InstCombinerImpl::visitAddrSpaceCast(AddrSpaceCastInst &CI) { 2914 // If the destination pointer element type is not the same as the source's 2915 // first do a bitcast to the destination type, and then the addrspacecast. 2916 // This allows the cast to be exposed to other transforms. 2917 Value *Src = CI.getOperand(0); 2918 PointerType *SrcTy = cast<PointerType>(Src->getType()->getScalarType()); 2919 PointerType *DestTy = cast<PointerType>(CI.getType()->getScalarType()); 2920 2921 if (!SrcTy->hasSameElementTypeAs(DestTy)) { 2922 Type *MidTy = 2923 PointerType::getWithSamePointeeType(DestTy, SrcTy->getAddressSpace()); 2924 // Handle vectors of pointers. 2925 if (VectorType *VT = dyn_cast<VectorType>(CI.getType())) 2926 MidTy = VectorType::get(MidTy, VT->getElementCount()); 2927 2928 Value *NewBitCast = Builder.CreateBitCast(Src, MidTy); 2929 return new AddrSpaceCastInst(NewBitCast, CI.getType()); 2930 } 2931 2932 return commonPointerCastTransforms(CI); 2933 } 2934