1 //===- InstCombineSelect.cpp ----------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the visitSelect function. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "InstCombineInternal.h" 15 #include "llvm/ADT/APInt.h" 16 #include "llvm/ADT/Optional.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/Analysis/AssumptionCache.h" 20 #include "llvm/Analysis/CmpInstAnalysis.h" 21 #include "llvm/Analysis/InstructionSimplify.h" 22 #include "llvm/Analysis/ValueTracking.h" 23 #include "llvm/IR/BasicBlock.h" 24 #include "llvm/IR/Constant.h" 25 #include "llvm/IR/Constants.h" 26 #include "llvm/IR/DerivedTypes.h" 27 #include "llvm/IR/IRBuilder.h" 28 #include "llvm/IR/InstrTypes.h" 29 #include "llvm/IR/Instruction.h" 30 #include "llvm/IR/Instructions.h" 31 #include "llvm/IR/IntrinsicInst.h" 32 #include "llvm/IR/Intrinsics.h" 33 #include "llvm/IR/Operator.h" 34 #include "llvm/IR/PatternMatch.h" 35 #include "llvm/IR/Type.h" 36 #include "llvm/IR/User.h" 37 #include "llvm/IR/Value.h" 38 #include "llvm/Support/Casting.h" 39 #include "llvm/Support/ErrorHandling.h" 40 #include "llvm/Support/KnownBits.h" 41 #include "llvm/Transforms/InstCombine/InstCombineWorklist.h" 42 #include <cassert> 43 #include <utility> 44 45 using namespace llvm; 46 using namespace PatternMatch; 47 48 #define DEBUG_TYPE "instcombine" 49 50 static SelectPatternFlavor 51 getInverseMinMaxSelectPattern(SelectPatternFlavor SPF) { 52 switch (SPF) { 53 default: 54 llvm_unreachable("unhandled!"); 55 56 case SPF_SMIN: 57 return SPF_SMAX; 58 case SPF_UMIN: 59 return SPF_UMAX; 60 case SPF_SMAX: 61 return SPF_SMIN; 62 case SPF_UMAX: 63 return SPF_UMIN; 64 } 65 } 66 67 static CmpInst::Predicate getCmpPredicateForMinMax(SelectPatternFlavor SPF, 68 bool Ordered=false) { 69 switch (SPF) { 70 default: 71 llvm_unreachable("unhandled!"); 72 73 case SPF_SMIN: 74 return ICmpInst::ICMP_SLT; 75 case SPF_UMIN: 76 return ICmpInst::ICMP_ULT; 77 case SPF_SMAX: 78 return ICmpInst::ICMP_SGT; 79 case SPF_UMAX: 80 return ICmpInst::ICMP_UGT; 81 case SPF_FMINNUM: 82 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT; 83 case SPF_FMAXNUM: 84 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT; 85 } 86 } 87 88 static Value *generateMinMaxSelectPattern(InstCombiner::BuilderTy &Builder, 89 SelectPatternFlavor SPF, Value *A, 90 Value *B) { 91 CmpInst::Predicate Pred = getCmpPredicateForMinMax(SPF); 92 assert(CmpInst::isIntPredicate(Pred)); 93 return Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B); 94 } 95 96 /// If one of the constants is zero (we know they can't both be) and we have an 97 /// icmp instruction with zero, and we have an 'and' with the non-constant value 98 /// and a power of two we can turn the select into a shift on the result of the 99 /// 'and'. 100 /// This folds: 101 /// select (icmp eq (and X, C1)), C2, C3 102 /// iff C1 is a power 2 and the difference between C2 and C3 is a power of 2. 103 /// To something like: 104 /// (shr (and (X, C1)), (log2(C1) - log2(C2-C3))) + C3 105 /// Or: 106 /// (shl (and (X, C1)), (log2(C2-C3) - log2(C1))) + C3 107 /// With some variations depending if C3 is larger than C2, or the shift 108 /// isn't needed, or the bit widths don't match. 109 static Value *foldSelectICmpAnd(Type *SelType, const ICmpInst *IC, 110 APInt TrueVal, APInt FalseVal, 111 InstCombiner::BuilderTy &Builder) { 112 assert(SelType->isIntOrIntVectorTy() && "Not an integer select?"); 113 114 // If this is a vector select, we need a vector compare. 115 if (SelType->isVectorTy() != IC->getType()->isVectorTy()) 116 return nullptr; 117 118 Value *V; 119 APInt AndMask; 120 bool CreateAnd = false; 121 ICmpInst::Predicate Pred = IC->getPredicate(); 122 if (ICmpInst::isEquality(Pred)) { 123 if (!match(IC->getOperand(1), m_Zero())) 124 return nullptr; 125 126 V = IC->getOperand(0); 127 128 const APInt *AndRHS; 129 if (!match(V, m_And(m_Value(), m_Power2(AndRHS)))) 130 return nullptr; 131 132 AndMask = *AndRHS; 133 } else if (decomposeBitTestICmp(IC->getOperand(0), IC->getOperand(1), 134 Pred, V, AndMask)) { 135 assert(ICmpInst::isEquality(Pred) && "Not equality test?"); 136 137 if (!AndMask.isPowerOf2()) 138 return nullptr; 139 140 CreateAnd = true; 141 } else { 142 return nullptr; 143 } 144 145 // If both select arms are non-zero see if we have a select of the form 146 // 'x ? 2^n + C : C'. Then we can offset both arms by C, use the logic 147 // for 'x ? 2^n : 0' and fix the thing up at the end. 148 APInt Offset(TrueVal.getBitWidth(), 0); 149 if (!TrueVal.isNullValue() && !FalseVal.isNullValue()) { 150 if ((TrueVal - FalseVal).isPowerOf2()) 151 Offset = FalseVal; 152 else if ((FalseVal - TrueVal).isPowerOf2()) 153 Offset = TrueVal; 154 else 155 return nullptr; 156 157 // Adjust TrueVal and FalseVal to the offset. 158 TrueVal -= Offset; 159 FalseVal -= Offset; 160 } 161 162 // Make sure one of the select arms is a power of 2. 163 if (!TrueVal.isPowerOf2() && !FalseVal.isPowerOf2()) 164 return nullptr; 165 166 // Determine which shift is needed to transform result of the 'and' into the 167 // desired result. 168 const APInt &ValC = !TrueVal.isNullValue() ? TrueVal : FalseVal; 169 unsigned ValZeros = ValC.logBase2(); 170 unsigned AndZeros = AndMask.logBase2(); 171 172 if (CreateAnd) { 173 // Insert the AND instruction on the input to the truncate. 174 V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), AndMask)); 175 } 176 177 // If types don't match we can still convert the select by introducing a zext 178 // or a trunc of the 'and'. 179 if (ValZeros > AndZeros) { 180 V = Builder.CreateZExtOrTrunc(V, SelType); 181 V = Builder.CreateShl(V, ValZeros - AndZeros); 182 } else if (ValZeros < AndZeros) { 183 V = Builder.CreateLShr(V, AndZeros - ValZeros); 184 V = Builder.CreateZExtOrTrunc(V, SelType); 185 } else 186 V = Builder.CreateZExtOrTrunc(V, SelType); 187 188 // Okay, now we know that everything is set up, we just don't know whether we 189 // have a icmp_ne or icmp_eq and whether the true or false val is the zero. 190 bool ShouldNotVal = !TrueVal.isNullValue(); 191 ShouldNotVal ^= Pred == ICmpInst::ICMP_NE; 192 if (ShouldNotVal) 193 V = Builder.CreateXor(V, ValC); 194 195 // Apply an offset if needed. 196 if (!Offset.isNullValue()) 197 V = Builder.CreateAdd(V, ConstantInt::get(V->getType(), Offset)); 198 return V; 199 } 200 201 /// We want to turn code that looks like this: 202 /// %C = or %A, %B 203 /// %D = select %cond, %C, %A 204 /// into: 205 /// %C = select %cond, %B, 0 206 /// %D = or %A, %C 207 /// 208 /// Assuming that the specified instruction is an operand to the select, return 209 /// a bitmask indicating which operands of this instruction are foldable if they 210 /// equal the other incoming value of the select. 211 static unsigned getSelectFoldableOperands(BinaryOperator *I) { 212 switch (I->getOpcode()) { 213 case Instruction::Add: 214 case Instruction::Mul: 215 case Instruction::And: 216 case Instruction::Or: 217 case Instruction::Xor: 218 return 3; // Can fold through either operand. 219 case Instruction::Sub: // Can only fold on the amount subtracted. 220 case Instruction::Shl: // Can only fold on the shift amount. 221 case Instruction::LShr: 222 case Instruction::AShr: 223 return 1; 224 default: 225 return 0; // Cannot fold 226 } 227 } 228 229 /// For the same transformation as the previous function, return the identity 230 /// constant that goes into the select. 231 static APInt getSelectFoldableConstant(BinaryOperator *I) { 232 switch (I->getOpcode()) { 233 default: llvm_unreachable("This cannot happen!"); 234 case Instruction::Add: 235 case Instruction::Sub: 236 case Instruction::Or: 237 case Instruction::Xor: 238 case Instruction::Shl: 239 case Instruction::LShr: 240 case Instruction::AShr: 241 return APInt::getNullValue(I->getType()->getScalarSizeInBits()); 242 case Instruction::And: 243 return APInt::getAllOnesValue(I->getType()->getScalarSizeInBits()); 244 case Instruction::Mul: 245 return APInt(I->getType()->getScalarSizeInBits(), 1); 246 } 247 } 248 249 /// We have (select c, TI, FI), and we know that TI and FI have the same opcode. 250 Instruction *InstCombiner::foldSelectOpOp(SelectInst &SI, Instruction *TI, 251 Instruction *FI) { 252 // Don't break up min/max patterns. The hasOneUse checks below prevent that 253 // for most cases, but vector min/max with bitcasts can be transformed. If the 254 // one-use restrictions are eased for other patterns, we still don't want to 255 // obfuscate min/max. 256 if ((match(&SI, m_SMin(m_Value(), m_Value())) || 257 match(&SI, m_SMax(m_Value(), m_Value())) || 258 match(&SI, m_UMin(m_Value(), m_Value())) || 259 match(&SI, m_UMax(m_Value(), m_Value())))) 260 return nullptr; 261 262 // If this is a cast from the same type, merge. 263 if (TI->getNumOperands() == 1 && TI->isCast()) { 264 Type *FIOpndTy = FI->getOperand(0)->getType(); 265 if (TI->getOperand(0)->getType() != FIOpndTy) 266 return nullptr; 267 268 // The select condition may be a vector. We may only change the operand 269 // type if the vector width remains the same (and matches the condition). 270 Type *CondTy = SI.getCondition()->getType(); 271 if (CondTy->isVectorTy()) { 272 if (!FIOpndTy->isVectorTy()) 273 return nullptr; 274 if (CondTy->getVectorNumElements() != FIOpndTy->getVectorNumElements()) 275 return nullptr; 276 277 // TODO: If the backend knew how to deal with casts better, we could 278 // remove this limitation. For now, there's too much potential to create 279 // worse codegen by promoting the select ahead of size-altering casts 280 // (PR28160). 281 // 282 // Note that ValueTracking's matchSelectPattern() looks through casts 283 // without checking 'hasOneUse' when it matches min/max patterns, so this 284 // transform may end up happening anyway. 285 if (TI->getOpcode() != Instruction::BitCast && 286 (!TI->hasOneUse() || !FI->hasOneUse())) 287 return nullptr; 288 } else if (!TI->hasOneUse() || !FI->hasOneUse()) { 289 // TODO: The one-use restrictions for a scalar select could be eased if 290 // the fold of a select in visitLoadInst() was enhanced to match a pattern 291 // that includes a cast. 292 return nullptr; 293 } 294 295 // Fold this by inserting a select from the input values. 296 Value *NewSI = 297 Builder.CreateSelect(SI.getCondition(), TI->getOperand(0), 298 FI->getOperand(0), SI.getName() + ".v", &SI); 299 return CastInst::Create(Instruction::CastOps(TI->getOpcode()), NewSI, 300 TI->getType()); 301 } 302 303 // Only handle binary operators (including two-operand getelementptr) with 304 // one-use here. As with the cast case above, it may be possible to relax the 305 // one-use constraint, but that needs be examined carefully since it may not 306 // reduce the total number of instructions. 307 if (TI->getNumOperands() != 2 || FI->getNumOperands() != 2 || 308 (!isa<BinaryOperator>(TI) && !isa<GetElementPtrInst>(TI)) || 309 !TI->hasOneUse() || !FI->hasOneUse()) 310 return nullptr; 311 312 // Figure out if the operations have any operands in common. 313 Value *MatchOp, *OtherOpT, *OtherOpF; 314 bool MatchIsOpZero; 315 if (TI->getOperand(0) == FI->getOperand(0)) { 316 MatchOp = TI->getOperand(0); 317 OtherOpT = TI->getOperand(1); 318 OtherOpF = FI->getOperand(1); 319 MatchIsOpZero = true; 320 } else if (TI->getOperand(1) == FI->getOperand(1)) { 321 MatchOp = TI->getOperand(1); 322 OtherOpT = TI->getOperand(0); 323 OtherOpF = FI->getOperand(0); 324 MatchIsOpZero = false; 325 } else if (!TI->isCommutative()) { 326 return nullptr; 327 } else if (TI->getOperand(0) == FI->getOperand(1)) { 328 MatchOp = TI->getOperand(0); 329 OtherOpT = TI->getOperand(1); 330 OtherOpF = FI->getOperand(0); 331 MatchIsOpZero = true; 332 } else if (TI->getOperand(1) == FI->getOperand(0)) { 333 MatchOp = TI->getOperand(1); 334 OtherOpT = TI->getOperand(0); 335 OtherOpF = FI->getOperand(1); 336 MatchIsOpZero = true; 337 } else { 338 return nullptr; 339 } 340 341 // If we reach here, they do have operations in common. 342 Value *NewSI = Builder.CreateSelect(SI.getCondition(), OtherOpT, OtherOpF, 343 SI.getName() + ".v", &SI); 344 Value *Op0 = MatchIsOpZero ? MatchOp : NewSI; 345 Value *Op1 = MatchIsOpZero ? NewSI : MatchOp; 346 if (auto *BO = dyn_cast<BinaryOperator>(TI)) { 347 return BinaryOperator::Create(BO->getOpcode(), Op0, Op1); 348 } 349 if (auto *TGEP = dyn_cast<GetElementPtrInst>(TI)) { 350 auto *FGEP = cast<GetElementPtrInst>(FI); 351 Type *ElementType = TGEP->getResultElementType(); 352 return TGEP->isInBounds() && FGEP->isInBounds() 353 ? GetElementPtrInst::CreateInBounds(ElementType, Op0, {Op1}) 354 : GetElementPtrInst::Create(ElementType, Op0, {Op1}); 355 } 356 llvm_unreachable("Expected BinaryOperator or GEP"); 357 return nullptr; 358 } 359 360 static bool isSelect01(const APInt &C1I, const APInt &C2I) { 361 if (!C1I.isNullValue() && !C2I.isNullValue()) // One side must be zero. 362 return false; 363 return C1I.isOneValue() || C1I.isAllOnesValue() || 364 C2I.isOneValue() || C2I.isAllOnesValue(); 365 } 366 367 /// Try to fold the select into one of the operands to allow further 368 /// optimization. 369 Instruction *InstCombiner::foldSelectIntoOp(SelectInst &SI, Value *TrueVal, 370 Value *FalseVal) { 371 // See the comment above GetSelectFoldableOperands for a description of the 372 // transformation we are doing here. 373 if (auto *TVI = dyn_cast<BinaryOperator>(TrueVal)) { 374 if (TVI->hasOneUse() && !isa<Constant>(FalseVal)) { 375 if (unsigned SFO = getSelectFoldableOperands(TVI)) { 376 unsigned OpToFold = 0; 377 if ((SFO & 1) && FalseVal == TVI->getOperand(0)) { 378 OpToFold = 1; 379 } else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) { 380 OpToFold = 2; 381 } 382 383 if (OpToFold) { 384 APInt CI = getSelectFoldableConstant(TVI); 385 Value *OOp = TVI->getOperand(2-OpToFold); 386 // Avoid creating select between 2 constants unless it's selecting 387 // between 0, 1 and -1. 388 const APInt *OOpC; 389 bool OOpIsAPInt = match(OOp, m_APInt(OOpC)); 390 if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) { 391 Value *C = ConstantInt::get(OOp->getType(), CI); 392 Value *NewSel = Builder.CreateSelect(SI.getCondition(), OOp, C); 393 NewSel->takeName(TVI); 394 BinaryOperator *BO = BinaryOperator::Create(TVI->getOpcode(), 395 FalseVal, NewSel); 396 BO->copyIRFlags(TVI); 397 return BO; 398 } 399 } 400 } 401 } 402 } 403 404 if (auto *FVI = dyn_cast<BinaryOperator>(FalseVal)) { 405 if (FVI->hasOneUse() && !isa<Constant>(TrueVal)) { 406 if (unsigned SFO = getSelectFoldableOperands(FVI)) { 407 unsigned OpToFold = 0; 408 if ((SFO & 1) && TrueVal == FVI->getOperand(0)) { 409 OpToFold = 1; 410 } else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) { 411 OpToFold = 2; 412 } 413 414 if (OpToFold) { 415 APInt CI = getSelectFoldableConstant(FVI); 416 Value *OOp = FVI->getOperand(2-OpToFold); 417 // Avoid creating select between 2 constants unless it's selecting 418 // between 0, 1 and -1. 419 const APInt *OOpC; 420 bool OOpIsAPInt = match(OOp, m_APInt(OOpC)); 421 if (!isa<Constant>(OOp) || (OOpIsAPInt && isSelect01(CI, *OOpC))) { 422 Value *C = ConstantInt::get(OOp->getType(), CI); 423 Value *NewSel = Builder.CreateSelect(SI.getCondition(), C, OOp); 424 NewSel->takeName(FVI); 425 BinaryOperator *BO = BinaryOperator::Create(FVI->getOpcode(), 426 TrueVal, NewSel); 427 BO->copyIRFlags(FVI); 428 return BO; 429 } 430 } 431 } 432 } 433 } 434 435 return nullptr; 436 } 437 438 /// We want to turn: 439 /// (select (icmp eq (and X, C1), 0), Y, (or Y, C2)) 440 /// into: 441 /// (or (shl (and X, C1), C3), Y) 442 /// iff: 443 /// C1 and C2 are both powers of 2 444 /// where: 445 /// C3 = Log(C2) - Log(C1) 446 /// 447 /// This transform handles cases where: 448 /// 1. The icmp predicate is inverted 449 /// 2. The select operands are reversed 450 /// 3. The magnitude of C2 and C1 are flipped 451 static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal, 452 Value *FalseVal, 453 InstCombiner::BuilderTy &Builder) { 454 // Only handle integer compares. Also, if this is a vector select, we need a 455 // vector compare. 456 if (!TrueVal->getType()->isIntOrIntVectorTy() || 457 TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy()) 458 return nullptr; 459 460 Value *CmpLHS = IC->getOperand(0); 461 Value *CmpRHS = IC->getOperand(1); 462 463 Value *V; 464 unsigned C1Log; 465 bool IsEqualZero; 466 bool NeedAnd = false; 467 if (IC->isEquality()) { 468 if (!match(CmpRHS, m_Zero())) 469 return nullptr; 470 471 const APInt *C1; 472 if (!match(CmpLHS, m_And(m_Value(), m_Power2(C1)))) 473 return nullptr; 474 475 V = CmpLHS; 476 C1Log = C1->logBase2(); 477 IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_EQ; 478 } else if (IC->getPredicate() == ICmpInst::ICMP_SLT || 479 IC->getPredicate() == ICmpInst::ICMP_SGT) { 480 // We also need to recognize (icmp slt (trunc (X)), 0) and 481 // (icmp sgt (trunc (X)), -1). 482 IsEqualZero = IC->getPredicate() == ICmpInst::ICMP_SGT; 483 if ((IsEqualZero && !match(CmpRHS, m_AllOnes())) || 484 (!IsEqualZero && !match(CmpRHS, m_Zero()))) 485 return nullptr; 486 487 if (!match(CmpLHS, m_OneUse(m_Trunc(m_Value(V))))) 488 return nullptr; 489 490 C1Log = CmpLHS->getType()->getScalarSizeInBits() - 1; 491 NeedAnd = true; 492 } else { 493 return nullptr; 494 } 495 496 const APInt *C2; 497 bool OrOnTrueVal = false; 498 bool OrOnFalseVal = match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2))); 499 if (!OrOnFalseVal) 500 OrOnTrueVal = match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2))); 501 502 if (!OrOnFalseVal && !OrOnTrueVal) 503 return nullptr; 504 505 Value *Y = OrOnFalseVal ? TrueVal : FalseVal; 506 507 unsigned C2Log = C2->logBase2(); 508 509 bool NeedXor = (!IsEqualZero && OrOnFalseVal) || (IsEqualZero && OrOnTrueVal); 510 bool NeedShift = C1Log != C2Log; 511 bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() != 512 V->getType()->getScalarSizeInBits(); 513 514 // Make sure we don't create more instructions than we save. 515 Value *Or = OrOnFalseVal ? FalseVal : TrueVal; 516 if ((NeedShift + NeedXor + NeedZExtTrunc) > 517 (IC->hasOneUse() + Or->hasOneUse())) 518 return nullptr; 519 520 if (NeedAnd) { 521 // Insert the AND instruction on the input to the truncate. 522 APInt C1 = APInt::getOneBitSet(V->getType()->getScalarSizeInBits(), C1Log); 523 V = Builder.CreateAnd(V, ConstantInt::get(V->getType(), C1)); 524 } 525 526 if (C2Log > C1Log) { 527 V = Builder.CreateZExtOrTrunc(V, Y->getType()); 528 V = Builder.CreateShl(V, C2Log - C1Log); 529 } else if (C1Log > C2Log) { 530 V = Builder.CreateLShr(V, C1Log - C2Log); 531 V = Builder.CreateZExtOrTrunc(V, Y->getType()); 532 } else 533 V = Builder.CreateZExtOrTrunc(V, Y->getType()); 534 535 if (NeedXor) 536 V = Builder.CreateXor(V, *C2); 537 538 return Builder.CreateOr(V, Y); 539 } 540 541 /// Transform patterns such as: (a > b) ? a - b : 0 542 /// into: ((a > b) ? a : b) - b) 543 /// This produces a canonical max pattern that is more easily recognized by the 544 /// backend and converted into saturated subtraction instructions if those 545 /// exist. 546 /// There are 8 commuted/swapped variants of this pattern. 547 /// TODO: Also support a - UMIN(a,b) patterns. 548 static Value *canonicalizeSaturatedSubtract(const ICmpInst *ICI, 549 const Value *TrueVal, 550 const Value *FalseVal, 551 InstCombiner::BuilderTy &Builder) { 552 ICmpInst::Predicate Pred = ICI->getPredicate(); 553 if (!ICmpInst::isUnsigned(Pred)) 554 return nullptr; 555 556 // (b > a) ? 0 : a - b -> (b <= a) ? a - b : 0 557 if (match(TrueVal, m_Zero())) { 558 Pred = ICmpInst::getInversePredicate(Pred); 559 std::swap(TrueVal, FalseVal); 560 } 561 if (!match(FalseVal, m_Zero())) 562 return nullptr; 563 564 Value *A = ICI->getOperand(0); 565 Value *B = ICI->getOperand(1); 566 if (Pred == ICmpInst::ICMP_ULE || Pred == ICmpInst::ICMP_ULT) { 567 // (b < a) ? a - b : 0 -> (a > b) ? a - b : 0 568 std::swap(A, B); 569 Pred = ICmpInst::getSwappedPredicate(Pred); 570 } 571 572 assert((Pred == ICmpInst::ICMP_UGE || Pred == ICmpInst::ICMP_UGT) && 573 "Unexpected isUnsigned predicate!"); 574 575 // Account for swapped form of subtraction: ((a > b) ? b - a : 0). 576 bool IsNegative = false; 577 if (match(TrueVal, m_Sub(m_Specific(B), m_Specific(A)))) 578 IsNegative = true; 579 else if (!match(TrueVal, m_Sub(m_Specific(A), m_Specific(B)))) 580 return nullptr; 581 582 // If sub is used anywhere else, we wouldn't be able to eliminate it 583 // afterwards. 584 if (!TrueVal->hasOneUse()) 585 return nullptr; 586 587 // All checks passed, convert to canonical unsigned saturated subtraction 588 // form: sub(max()). 589 // (a > b) ? a - b : 0 -> ((a > b) ? a : b) - b) 590 Value *Max = Builder.CreateSelect(Builder.CreateICmp(Pred, A, B), A, B); 591 return IsNegative ? Builder.CreateSub(B, Max) : Builder.CreateSub(Max, B); 592 } 593 594 /// Attempt to fold a cttz/ctlz followed by a icmp plus select into a single 595 /// call to cttz/ctlz with flag 'is_zero_undef' cleared. 596 /// 597 /// For example, we can fold the following code sequence: 598 /// \code 599 /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 true) 600 /// %1 = icmp ne i32 %x, 0 601 /// %2 = select i1 %1, i32 %0, i32 32 602 /// \code 603 /// 604 /// into: 605 /// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false) 606 static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal, 607 InstCombiner::BuilderTy &Builder) { 608 ICmpInst::Predicate Pred = ICI->getPredicate(); 609 Value *CmpLHS = ICI->getOperand(0); 610 Value *CmpRHS = ICI->getOperand(1); 611 612 // Check if the condition value compares a value for equality against zero. 613 if (!ICI->isEquality() || !match(CmpRHS, m_Zero())) 614 return nullptr; 615 616 Value *Count = FalseVal; 617 Value *ValueOnZero = TrueVal; 618 if (Pred == ICmpInst::ICMP_NE) 619 std::swap(Count, ValueOnZero); 620 621 // Skip zero extend/truncate. 622 Value *V = nullptr; 623 if (match(Count, m_ZExt(m_Value(V))) || 624 match(Count, m_Trunc(m_Value(V)))) 625 Count = V; 626 627 // Check if the value propagated on zero is a constant number equal to the 628 // sizeof in bits of 'Count'. 629 unsigned SizeOfInBits = Count->getType()->getScalarSizeInBits(); 630 if (!match(ValueOnZero, m_SpecificInt(SizeOfInBits))) 631 return nullptr; 632 633 // Check that 'Count' is a call to intrinsic cttz/ctlz. Also check that the 634 // input to the cttz/ctlz is used as LHS for the compare instruction. 635 if (match(Count, m_Intrinsic<Intrinsic::cttz>(m_Specific(CmpLHS))) || 636 match(Count, m_Intrinsic<Intrinsic::ctlz>(m_Specific(CmpLHS)))) { 637 IntrinsicInst *II = cast<IntrinsicInst>(Count); 638 // Explicitly clear the 'undef_on_zero' flag. 639 IntrinsicInst *NewI = cast<IntrinsicInst>(II->clone()); 640 NewI->setArgOperand(1, ConstantInt::getFalse(NewI->getContext())); 641 Builder.Insert(NewI); 642 return Builder.CreateZExtOrTrunc(NewI, ValueOnZero->getType()); 643 } 644 645 return nullptr; 646 } 647 648 /// Return true if we find and adjust an icmp+select pattern where the compare 649 /// is with a constant that can be incremented or decremented to match the 650 /// minimum or maximum idiom. 651 static bool adjustMinMax(SelectInst &Sel, ICmpInst &Cmp) { 652 ICmpInst::Predicate Pred = Cmp.getPredicate(); 653 Value *CmpLHS = Cmp.getOperand(0); 654 Value *CmpRHS = Cmp.getOperand(1); 655 Value *TrueVal = Sel.getTrueValue(); 656 Value *FalseVal = Sel.getFalseValue(); 657 658 // We may move or edit the compare, so make sure the select is the only user. 659 const APInt *CmpC; 660 if (!Cmp.hasOneUse() || !match(CmpRHS, m_APInt(CmpC))) 661 return false; 662 663 // These transforms only work for selects of integers or vector selects of 664 // integer vectors. 665 Type *SelTy = Sel.getType(); 666 auto *SelEltTy = dyn_cast<IntegerType>(SelTy->getScalarType()); 667 if (!SelEltTy || SelTy->isVectorTy() != Cmp.getType()->isVectorTy()) 668 return false; 669 670 Constant *AdjustedRHS; 671 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_SGT) 672 AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC + 1); 673 else if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) 674 AdjustedRHS = ConstantInt::get(CmpRHS->getType(), *CmpC - 1); 675 else 676 return false; 677 678 // X > C ? X : C+1 --> X < C+1 ? C+1 : X 679 // X < C ? X : C-1 --> X > C-1 ? C-1 : X 680 if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) || 681 (CmpLHS == FalseVal && AdjustedRHS == TrueVal)) { 682 ; // Nothing to do here. Values match without any sign/zero extension. 683 } 684 // Types do not match. Instead of calculating this with mixed types, promote 685 // all to the larger type. This enables scalar evolution to analyze this 686 // expression. 687 else if (CmpRHS->getType()->getScalarSizeInBits() < SelEltTy->getBitWidth()) { 688 Constant *SextRHS = ConstantExpr::getSExt(AdjustedRHS, SelTy); 689 690 // X = sext x; x >s c ? X : C+1 --> X = sext x; X <s C+1 ? C+1 : X 691 // X = sext x; x <s c ? X : C-1 --> X = sext x; X >s C-1 ? C-1 : X 692 // X = sext x; x >u c ? X : C+1 --> X = sext x; X <u C+1 ? C+1 : X 693 // X = sext x; x <u c ? X : C-1 --> X = sext x; X >u C-1 ? C-1 : X 694 if (match(TrueVal, m_SExt(m_Specific(CmpLHS))) && SextRHS == FalseVal) { 695 CmpLHS = TrueVal; 696 AdjustedRHS = SextRHS; 697 } else if (match(FalseVal, m_SExt(m_Specific(CmpLHS))) && 698 SextRHS == TrueVal) { 699 CmpLHS = FalseVal; 700 AdjustedRHS = SextRHS; 701 } else if (Cmp.isUnsigned()) { 702 Constant *ZextRHS = ConstantExpr::getZExt(AdjustedRHS, SelTy); 703 // X = zext x; x >u c ? X : C+1 --> X = zext x; X <u C+1 ? C+1 : X 704 // X = zext x; x <u c ? X : C-1 --> X = zext x; X >u C-1 ? C-1 : X 705 // zext + signed compare cannot be changed: 706 // 0xff <s 0x00, but 0x00ff >s 0x0000 707 if (match(TrueVal, m_ZExt(m_Specific(CmpLHS))) && ZextRHS == FalseVal) { 708 CmpLHS = TrueVal; 709 AdjustedRHS = ZextRHS; 710 } else if (match(FalseVal, m_ZExt(m_Specific(CmpLHS))) && 711 ZextRHS == TrueVal) { 712 CmpLHS = FalseVal; 713 AdjustedRHS = ZextRHS; 714 } else { 715 return false; 716 } 717 } else { 718 return false; 719 } 720 } else { 721 return false; 722 } 723 724 Pred = ICmpInst::getSwappedPredicate(Pred); 725 CmpRHS = AdjustedRHS; 726 std::swap(FalseVal, TrueVal); 727 Cmp.setPredicate(Pred); 728 Cmp.setOperand(0, CmpLHS); 729 Cmp.setOperand(1, CmpRHS); 730 Sel.setOperand(1, TrueVal); 731 Sel.setOperand(2, FalseVal); 732 Sel.swapProfMetadata(); 733 734 // Move the compare instruction right before the select instruction. Otherwise 735 // the sext/zext value may be defined after the compare instruction uses it. 736 Cmp.moveBefore(&Sel); 737 738 return true; 739 } 740 741 /// If this is an integer min/max (icmp + select) with a constant operand, 742 /// create the canonical icmp for the min/max operation and canonicalize the 743 /// constant to the 'false' operand of the select: 744 /// select (icmp Pred X, C1), C2, X --> select (icmp Pred' X, C2), X, C2 745 /// Note: if C1 != C2, this will change the icmp constant to the existing 746 /// constant operand of the select. 747 static Instruction * 748 canonicalizeMinMaxWithConstant(SelectInst &Sel, ICmpInst &Cmp, 749 InstCombiner::BuilderTy &Builder) { 750 if (!Cmp.hasOneUse() || !isa<Constant>(Cmp.getOperand(1))) 751 return nullptr; 752 753 // Canonicalize the compare predicate based on whether we have min or max. 754 Value *LHS, *RHS; 755 ICmpInst::Predicate NewPred; 756 SelectPatternResult SPR = matchSelectPattern(&Sel, LHS, RHS); 757 switch (SPR.Flavor) { 758 case SPF_SMIN: NewPred = ICmpInst::ICMP_SLT; break; 759 case SPF_UMIN: NewPred = ICmpInst::ICMP_ULT; break; 760 case SPF_SMAX: NewPred = ICmpInst::ICMP_SGT; break; 761 case SPF_UMAX: NewPred = ICmpInst::ICMP_UGT; break; 762 default: return nullptr; 763 } 764 765 // Is this already canonical? 766 if (Cmp.getOperand(0) == LHS && Cmp.getOperand(1) == RHS && 767 Cmp.getPredicate() == NewPred) 768 return nullptr; 769 770 // Create the canonical compare and plug it into the select. 771 Sel.setCondition(Builder.CreateICmp(NewPred, LHS, RHS)); 772 773 // If the select operands did not change, we're done. 774 if (Sel.getTrueValue() == LHS && Sel.getFalseValue() == RHS) 775 return &Sel; 776 777 // If we are swapping the select operands, swap the metadata too. 778 assert(Sel.getTrueValue() == RHS && Sel.getFalseValue() == LHS && 779 "Unexpected results from matchSelectPattern"); 780 Sel.setTrueValue(LHS); 781 Sel.setFalseValue(RHS); 782 Sel.swapProfMetadata(); 783 return &Sel; 784 } 785 786 /// Visit a SelectInst that has an ICmpInst as its first operand. 787 Instruction *InstCombiner::foldSelectInstWithICmp(SelectInst &SI, 788 ICmpInst *ICI) { 789 Value *TrueVal = SI.getTrueValue(); 790 Value *FalseVal = SI.getFalseValue(); 791 792 if (Instruction *NewSel = canonicalizeMinMaxWithConstant(SI, *ICI, Builder)) 793 return NewSel; 794 795 bool Changed = adjustMinMax(SI, *ICI); 796 797 ICmpInst::Predicate Pred = ICI->getPredicate(); 798 Value *CmpLHS = ICI->getOperand(0); 799 Value *CmpRHS = ICI->getOperand(1); 800 801 // Transform (X >s -1) ? C1 : C2 --> ((X >>s 31) & (C2 - C1)) + C1 802 // and (X <s 0) ? C2 : C1 --> ((X >>s 31) & (C2 - C1)) + C1 803 // FIXME: Type and constness constraints could be lifted, but we have to 804 // watch code size carefully. We should consider xor instead of 805 // sub/add when we decide to do that. 806 // TODO: Merge this with foldSelectICmpAnd somehow. 807 if (CmpLHS->getType()->isIntOrIntVectorTy() && 808 CmpLHS->getType() == TrueVal->getType()) { 809 const APInt *C1, *C2; 810 if (match(TrueVal, m_APInt(C1)) && match(FalseVal, m_APInt(C2))) { 811 ICmpInst::Predicate Pred = ICI->getPredicate(); 812 Value *X; 813 APInt Mask; 814 if (decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, X, Mask, false)) { 815 if (Mask.isSignMask()) { 816 assert(X == CmpLHS && "Expected to use the compare input directly"); 817 assert(ICmpInst::isEquality(Pred) && "Expected equality predicate"); 818 819 if (Pred == ICmpInst::ICMP_NE) 820 std::swap(C1, C2); 821 822 // This shift results in either -1 or 0. 823 Value *AShr = Builder.CreateAShr(X, Mask.getBitWidth() - 1); 824 825 // Check if we can express the operation with a single or. 826 if (C2->isAllOnesValue()) 827 return replaceInstUsesWith(SI, Builder.CreateOr(AShr, *C1)); 828 829 Value *And = Builder.CreateAnd(AShr, *C2 - *C1); 830 return replaceInstUsesWith(SI, Builder.CreateAdd(And, 831 ConstantInt::get(And->getType(), *C1))); 832 } 833 } 834 } 835 } 836 837 { 838 const APInt *TrueValC, *FalseValC; 839 if (match(TrueVal, m_APInt(TrueValC)) && 840 match(FalseVal, m_APInt(FalseValC))) 841 if (Value *V = foldSelectICmpAnd(SI.getType(), ICI, *TrueValC, 842 *FalseValC, Builder)) 843 return replaceInstUsesWith(SI, V); 844 } 845 846 // NOTE: if we wanted to, this is where to detect integer MIN/MAX 847 848 if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) { 849 if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) { 850 // Transform (X == C) ? X : Y -> (X == C) ? C : Y 851 SI.setOperand(1, CmpRHS); 852 Changed = true; 853 } else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) { 854 // Transform (X != C) ? Y : X -> (X != C) ? Y : C 855 SI.setOperand(2, CmpRHS); 856 Changed = true; 857 } 858 } 859 860 // FIXME: This code is nearly duplicated in InstSimplify. Using/refactoring 861 // decomposeBitTestICmp() might help. 862 { 863 unsigned BitWidth = 864 DL.getTypeSizeInBits(TrueVal->getType()->getScalarType()); 865 APInt MinSignedValue = APInt::getSignedMinValue(BitWidth); 866 Value *X; 867 const APInt *Y, *C; 868 bool TrueWhenUnset; 869 bool IsBitTest = false; 870 if (ICmpInst::isEquality(Pred) && 871 match(CmpLHS, m_And(m_Value(X), m_Power2(Y))) && 872 match(CmpRHS, m_Zero())) { 873 IsBitTest = true; 874 TrueWhenUnset = Pred == ICmpInst::ICMP_EQ; 875 } else if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, m_Zero())) { 876 X = CmpLHS; 877 Y = &MinSignedValue; 878 IsBitTest = true; 879 TrueWhenUnset = false; 880 } else if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, m_AllOnes())) { 881 X = CmpLHS; 882 Y = &MinSignedValue; 883 IsBitTest = true; 884 TrueWhenUnset = true; 885 } 886 if (IsBitTest) { 887 Value *V = nullptr; 888 // (X & Y) == 0 ? X : X ^ Y --> X & ~Y 889 if (TrueWhenUnset && TrueVal == X && 890 match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) 891 V = Builder.CreateAnd(X, ~(*Y)); 892 // (X & Y) != 0 ? X ^ Y : X --> X & ~Y 893 else if (!TrueWhenUnset && FalseVal == X && 894 match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) 895 V = Builder.CreateAnd(X, ~(*Y)); 896 // (X & Y) == 0 ? X ^ Y : X --> X | Y 897 else if (TrueWhenUnset && FalseVal == X && 898 match(TrueVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) 899 V = Builder.CreateOr(X, *Y); 900 // (X & Y) != 0 ? X : X ^ Y --> X | Y 901 else if (!TrueWhenUnset && TrueVal == X && 902 match(FalseVal, m_Xor(m_Specific(X), m_APInt(C))) && *Y == *C) 903 V = Builder.CreateOr(X, *Y); 904 905 if (V) 906 return replaceInstUsesWith(SI, V); 907 } 908 } 909 910 if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder)) 911 return replaceInstUsesWith(SI, V); 912 913 if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder)) 914 return replaceInstUsesWith(SI, V); 915 916 if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder)) 917 return replaceInstUsesWith(SI, V); 918 919 return Changed ? &SI : nullptr; 920 } 921 922 /// SI is a select whose condition is a PHI node (but the two may be in 923 /// different blocks). See if the true/false values (V) are live in all of the 924 /// predecessor blocks of the PHI. For example, cases like this can't be mapped: 925 /// 926 /// X = phi [ C1, BB1], [C2, BB2] 927 /// Y = add 928 /// Z = select X, Y, 0 929 /// 930 /// because Y is not live in BB1/BB2. 931 static bool canSelectOperandBeMappingIntoPredBlock(const Value *V, 932 const SelectInst &SI) { 933 // If the value is a non-instruction value like a constant or argument, it 934 // can always be mapped. 935 const Instruction *I = dyn_cast<Instruction>(V); 936 if (!I) return true; 937 938 // If V is a PHI node defined in the same block as the condition PHI, we can 939 // map the arguments. 940 const PHINode *CondPHI = cast<PHINode>(SI.getCondition()); 941 942 if (const PHINode *VP = dyn_cast<PHINode>(I)) 943 if (VP->getParent() == CondPHI->getParent()) 944 return true; 945 946 // Otherwise, if the PHI and select are defined in the same block and if V is 947 // defined in a different block, then we can transform it. 948 if (SI.getParent() == CondPHI->getParent() && 949 I->getParent() != CondPHI->getParent()) 950 return true; 951 952 // Otherwise we have a 'hard' case and we can't tell without doing more 953 // detailed dominator based analysis, punt. 954 return false; 955 } 956 957 /// We have an SPF (e.g. a min or max) of an SPF of the form: 958 /// SPF2(SPF1(A, B), C) 959 Instruction *InstCombiner::foldSPFofSPF(Instruction *Inner, 960 SelectPatternFlavor SPF1, 961 Value *A, Value *B, 962 Instruction &Outer, 963 SelectPatternFlavor SPF2, Value *C) { 964 if (Outer.getType() != Inner->getType()) 965 return nullptr; 966 967 if (C == A || C == B) { 968 // MAX(MAX(A, B), B) -> MAX(A, B) 969 // MIN(MIN(a, b), a) -> MIN(a, b) 970 if (SPF1 == SPF2) 971 return replaceInstUsesWith(Outer, Inner); 972 973 // MAX(MIN(a, b), a) -> a 974 // MIN(MAX(a, b), a) -> a 975 if ((SPF1 == SPF_SMIN && SPF2 == SPF_SMAX) || 976 (SPF1 == SPF_SMAX && SPF2 == SPF_SMIN) || 977 (SPF1 == SPF_UMIN && SPF2 == SPF_UMAX) || 978 (SPF1 == SPF_UMAX && SPF2 == SPF_UMIN)) 979 return replaceInstUsesWith(Outer, C); 980 } 981 982 if (SPF1 == SPF2) { 983 const APInt *CB, *CC; 984 if (match(B, m_APInt(CB)) && match(C, m_APInt(CC))) { 985 // MIN(MIN(A, 23), 97) -> MIN(A, 23) 986 // MAX(MAX(A, 97), 23) -> MAX(A, 97) 987 if ((SPF1 == SPF_UMIN && CB->ule(*CC)) || 988 (SPF1 == SPF_SMIN && CB->sle(*CC)) || 989 (SPF1 == SPF_UMAX && CB->uge(*CC)) || 990 (SPF1 == SPF_SMAX && CB->sge(*CC))) 991 return replaceInstUsesWith(Outer, Inner); 992 993 // MIN(MIN(A, 97), 23) -> MIN(A, 23) 994 // MAX(MAX(A, 23), 97) -> MAX(A, 97) 995 if ((SPF1 == SPF_UMIN && CB->ugt(*CC)) || 996 (SPF1 == SPF_SMIN && CB->sgt(*CC)) || 997 (SPF1 == SPF_UMAX && CB->ult(*CC)) || 998 (SPF1 == SPF_SMAX && CB->slt(*CC))) { 999 Outer.replaceUsesOfWith(Inner, A); 1000 return &Outer; 1001 } 1002 } 1003 } 1004 1005 // ABS(ABS(X)) -> ABS(X) 1006 // NABS(NABS(X)) -> NABS(X) 1007 if (SPF1 == SPF2 && (SPF1 == SPF_ABS || SPF1 == SPF_NABS)) { 1008 return replaceInstUsesWith(Outer, Inner); 1009 } 1010 1011 // ABS(NABS(X)) -> ABS(X) 1012 // NABS(ABS(X)) -> NABS(X) 1013 if ((SPF1 == SPF_ABS && SPF2 == SPF_NABS) || 1014 (SPF1 == SPF_NABS && SPF2 == SPF_ABS)) { 1015 SelectInst *SI = cast<SelectInst>(Inner); 1016 Value *NewSI = 1017 Builder.CreateSelect(SI->getCondition(), SI->getFalseValue(), 1018 SI->getTrueValue(), SI->getName(), SI); 1019 return replaceInstUsesWith(Outer, NewSI); 1020 } 1021 1022 auto IsFreeOrProfitableToInvert = 1023 [&](Value *V, Value *&NotV, bool &ElidesXor) { 1024 if (match(V, m_Not(m_Value(NotV)))) { 1025 // If V has at most 2 uses then we can get rid of the xor operation 1026 // entirely. 1027 ElidesXor |= !V->hasNUsesOrMore(3); 1028 return true; 1029 } 1030 1031 if (IsFreeToInvert(V, !V->hasNUsesOrMore(3))) { 1032 NotV = nullptr; 1033 return true; 1034 } 1035 1036 return false; 1037 }; 1038 1039 Value *NotA, *NotB, *NotC; 1040 bool ElidesXor = false; 1041 1042 // MIN(MIN(~A, ~B), ~C) == ~MAX(MAX(A, B), C) 1043 // MIN(MAX(~A, ~B), ~C) == ~MAX(MIN(A, B), C) 1044 // MAX(MIN(~A, ~B), ~C) == ~MIN(MAX(A, B), C) 1045 // MAX(MAX(~A, ~B), ~C) == ~MIN(MIN(A, B), C) 1046 // 1047 // This transform is performance neutral if we can elide at least one xor from 1048 // the set of three operands, since we'll be tacking on an xor at the very 1049 // end. 1050 if (SelectPatternResult::isMinOrMax(SPF1) && 1051 SelectPatternResult::isMinOrMax(SPF2) && 1052 IsFreeOrProfitableToInvert(A, NotA, ElidesXor) && 1053 IsFreeOrProfitableToInvert(B, NotB, ElidesXor) && 1054 IsFreeOrProfitableToInvert(C, NotC, ElidesXor) && ElidesXor) { 1055 if (!NotA) 1056 NotA = Builder.CreateNot(A); 1057 if (!NotB) 1058 NotB = Builder.CreateNot(B); 1059 if (!NotC) 1060 NotC = Builder.CreateNot(C); 1061 1062 Value *NewInner = generateMinMaxSelectPattern( 1063 Builder, getInverseMinMaxSelectPattern(SPF1), NotA, NotB); 1064 Value *NewOuter = Builder.CreateNot(generateMinMaxSelectPattern( 1065 Builder, getInverseMinMaxSelectPattern(SPF2), NewInner, NotC)); 1066 return replaceInstUsesWith(Outer, NewOuter); 1067 } 1068 1069 return nullptr; 1070 } 1071 1072 /// Turn select C, (X + Y), (X - Y) --> (X + (select C, Y, (-Y))). 1073 /// This is even legal for FP. 1074 static Instruction *foldAddSubSelect(SelectInst &SI, 1075 InstCombiner::BuilderTy &Builder) { 1076 Value *CondVal = SI.getCondition(); 1077 Value *TrueVal = SI.getTrueValue(); 1078 Value *FalseVal = SI.getFalseValue(); 1079 auto *TI = dyn_cast<Instruction>(TrueVal); 1080 auto *FI = dyn_cast<Instruction>(FalseVal); 1081 if (!TI || !FI || !TI->hasOneUse() || !FI->hasOneUse()) 1082 return nullptr; 1083 1084 Instruction *AddOp = nullptr, *SubOp = nullptr; 1085 if ((TI->getOpcode() == Instruction::Sub && 1086 FI->getOpcode() == Instruction::Add) || 1087 (TI->getOpcode() == Instruction::FSub && 1088 FI->getOpcode() == Instruction::FAdd)) { 1089 AddOp = FI; 1090 SubOp = TI; 1091 } else if ((FI->getOpcode() == Instruction::Sub && 1092 TI->getOpcode() == Instruction::Add) || 1093 (FI->getOpcode() == Instruction::FSub && 1094 TI->getOpcode() == Instruction::FAdd)) { 1095 AddOp = TI; 1096 SubOp = FI; 1097 } 1098 1099 if (AddOp) { 1100 Value *OtherAddOp = nullptr; 1101 if (SubOp->getOperand(0) == AddOp->getOperand(0)) { 1102 OtherAddOp = AddOp->getOperand(1); 1103 } else if (SubOp->getOperand(0) == AddOp->getOperand(1)) { 1104 OtherAddOp = AddOp->getOperand(0); 1105 } 1106 1107 if (OtherAddOp) { 1108 // So at this point we know we have (Y -> OtherAddOp): 1109 // select C, (add X, Y), (sub X, Z) 1110 Value *NegVal; // Compute -Z 1111 if (SI.getType()->isFPOrFPVectorTy()) { 1112 NegVal = Builder.CreateFNeg(SubOp->getOperand(1)); 1113 if (Instruction *NegInst = dyn_cast<Instruction>(NegVal)) { 1114 FastMathFlags Flags = AddOp->getFastMathFlags(); 1115 Flags &= SubOp->getFastMathFlags(); 1116 NegInst->setFastMathFlags(Flags); 1117 } 1118 } else { 1119 NegVal = Builder.CreateNeg(SubOp->getOperand(1)); 1120 } 1121 1122 Value *NewTrueOp = OtherAddOp; 1123 Value *NewFalseOp = NegVal; 1124 if (AddOp != TI) 1125 std::swap(NewTrueOp, NewFalseOp); 1126 Value *NewSel = Builder.CreateSelect(CondVal, NewTrueOp, NewFalseOp, 1127 SI.getName() + ".p", &SI); 1128 1129 if (SI.getType()->isFPOrFPVectorTy()) { 1130 Instruction *RI = 1131 BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel); 1132 1133 FastMathFlags Flags = AddOp->getFastMathFlags(); 1134 Flags &= SubOp->getFastMathFlags(); 1135 RI->setFastMathFlags(Flags); 1136 return RI; 1137 } else 1138 return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel); 1139 } 1140 } 1141 return nullptr; 1142 } 1143 1144 Instruction *InstCombiner::foldSelectExtConst(SelectInst &Sel) { 1145 Instruction *ExtInst; 1146 if (!match(Sel.getTrueValue(), m_Instruction(ExtInst)) && 1147 !match(Sel.getFalseValue(), m_Instruction(ExtInst))) 1148 return nullptr; 1149 1150 auto ExtOpcode = ExtInst->getOpcode(); 1151 if (ExtOpcode != Instruction::ZExt && ExtOpcode != Instruction::SExt) 1152 return nullptr; 1153 1154 // TODO: Handle larger types? That requires adjusting FoldOpIntoSelect too. 1155 Value *X = ExtInst->getOperand(0); 1156 Type *SmallType = X->getType(); 1157 if (!SmallType->isIntOrIntVectorTy(1)) 1158 return nullptr; 1159 1160 Constant *C; 1161 if (!match(Sel.getTrueValue(), m_Constant(C)) && 1162 !match(Sel.getFalseValue(), m_Constant(C))) 1163 return nullptr; 1164 1165 // If the constant is the same after truncation to the smaller type and 1166 // extension to the original type, we can narrow the select. 1167 Value *Cond = Sel.getCondition(); 1168 Type *SelType = Sel.getType(); 1169 Constant *TruncC = ConstantExpr::getTrunc(C, SmallType); 1170 Constant *ExtC = ConstantExpr::getCast(ExtOpcode, TruncC, SelType); 1171 if (ExtC == C) { 1172 Value *TruncCVal = cast<Value>(TruncC); 1173 if (ExtInst == Sel.getFalseValue()) 1174 std::swap(X, TruncCVal); 1175 1176 // select Cond, (ext X), C --> ext(select Cond, X, C') 1177 // select Cond, C, (ext X) --> ext(select Cond, C', X) 1178 Value *NewSel = Builder.CreateSelect(Cond, X, TruncCVal, "narrow", &Sel); 1179 return CastInst::Create(Instruction::CastOps(ExtOpcode), NewSel, SelType); 1180 } 1181 1182 // If one arm of the select is the extend of the condition, replace that arm 1183 // with the extension of the appropriate known bool value. 1184 if (Cond == X) { 1185 if (ExtInst == Sel.getTrueValue()) { 1186 // select X, (sext X), C --> select X, -1, C 1187 // select X, (zext X), C --> select X, 1, C 1188 Constant *One = ConstantInt::getTrue(SmallType); 1189 Constant *AllOnesOrOne = ConstantExpr::getCast(ExtOpcode, One, SelType); 1190 return SelectInst::Create(Cond, AllOnesOrOne, C, "", nullptr, &Sel); 1191 } else { 1192 // select X, C, (sext X) --> select X, C, 0 1193 // select X, C, (zext X) --> select X, C, 0 1194 Constant *Zero = ConstantInt::getNullValue(SelType); 1195 return SelectInst::Create(Cond, C, Zero, "", nullptr, &Sel); 1196 } 1197 } 1198 1199 return nullptr; 1200 } 1201 1202 /// Try to transform a vector select with a constant condition vector into a 1203 /// shuffle for easier combining with other shuffles and insert/extract. 1204 static Instruction *canonicalizeSelectToShuffle(SelectInst &SI) { 1205 Value *CondVal = SI.getCondition(); 1206 Constant *CondC; 1207 if (!CondVal->getType()->isVectorTy() || !match(CondVal, m_Constant(CondC))) 1208 return nullptr; 1209 1210 unsigned NumElts = CondVal->getType()->getVectorNumElements(); 1211 SmallVector<Constant *, 16> Mask; 1212 Mask.reserve(NumElts); 1213 Type *Int32Ty = Type::getInt32Ty(CondVal->getContext()); 1214 for (unsigned i = 0; i != NumElts; ++i) { 1215 Constant *Elt = CondC->getAggregateElement(i); 1216 if (!Elt) 1217 return nullptr; 1218 1219 if (Elt->isOneValue()) { 1220 // If the select condition element is true, choose from the 1st vector. 1221 Mask.push_back(ConstantInt::get(Int32Ty, i)); 1222 } else if (Elt->isNullValue()) { 1223 // If the select condition element is false, choose from the 2nd vector. 1224 Mask.push_back(ConstantInt::get(Int32Ty, i + NumElts)); 1225 } else if (isa<UndefValue>(Elt)) { 1226 // Undef in a select condition (choose one of the operands) does not mean 1227 // the same thing as undef in a shuffle mask (any value is acceptable), so 1228 // give up. 1229 return nullptr; 1230 } else { 1231 // Bail out on a constant expression. 1232 return nullptr; 1233 } 1234 } 1235 1236 return new ShuffleVectorInst(SI.getTrueValue(), SI.getFalseValue(), 1237 ConstantVector::get(Mask)); 1238 } 1239 1240 /// Reuse bitcasted operands between a compare and select: 1241 /// select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) --> 1242 /// bitcast (select (cmp (bitcast C), (bitcast D)), (bitcast C), (bitcast D)) 1243 static Instruction *foldSelectCmpBitcasts(SelectInst &Sel, 1244 InstCombiner::BuilderTy &Builder) { 1245 Value *Cond = Sel.getCondition(); 1246 Value *TVal = Sel.getTrueValue(); 1247 Value *FVal = Sel.getFalseValue(); 1248 1249 CmpInst::Predicate Pred; 1250 Value *A, *B; 1251 if (!match(Cond, m_Cmp(Pred, m_Value(A), m_Value(B)))) 1252 return nullptr; 1253 1254 // The select condition is a compare instruction. If the select's true/false 1255 // values are already the same as the compare operands, there's nothing to do. 1256 if (TVal == A || TVal == B || FVal == A || FVal == B) 1257 return nullptr; 1258 1259 Value *C, *D; 1260 if (!match(A, m_BitCast(m_Value(C))) || !match(B, m_BitCast(m_Value(D)))) 1261 return nullptr; 1262 1263 // select (cmp (bitcast C), (bitcast D)), (bitcast TSrc), (bitcast FSrc) 1264 Value *TSrc, *FSrc; 1265 if (!match(TVal, m_BitCast(m_Value(TSrc))) || 1266 !match(FVal, m_BitCast(m_Value(FSrc)))) 1267 return nullptr; 1268 1269 // If the select true/false values are *different bitcasts* of the same source 1270 // operands, make the select operands the same as the compare operands and 1271 // cast the result. This is the canonical select form for min/max. 1272 Value *NewSel; 1273 if (TSrc == C && FSrc == D) { 1274 // select (cmp (bitcast C), (bitcast D)), (bitcast' C), (bitcast' D) --> 1275 // bitcast (select (cmp A, B), A, B) 1276 NewSel = Builder.CreateSelect(Cond, A, B, "", &Sel); 1277 } else if (TSrc == D && FSrc == C) { 1278 // select (cmp (bitcast C), (bitcast D)), (bitcast' D), (bitcast' C) --> 1279 // bitcast (select (cmp A, B), B, A) 1280 NewSel = Builder.CreateSelect(Cond, B, A, "", &Sel); 1281 } else { 1282 return nullptr; 1283 } 1284 return CastInst::CreateBitOrPointerCast(NewSel, Sel.getType()); 1285 } 1286 1287 /// Try to eliminate select instructions that test the returned flag of cmpxchg 1288 /// instructions. 1289 /// 1290 /// If a select instruction tests the returned flag of a cmpxchg instruction and 1291 /// selects between the returned value of the cmpxchg instruction its compare 1292 /// operand, the result of the select will always be equal to its false value. 1293 /// For example: 1294 /// 1295 /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst 1296 /// %1 = extractvalue { i64, i1 } %0, 1 1297 /// %2 = extractvalue { i64, i1 } %0, 0 1298 /// %3 = select i1 %1, i64 %compare, i64 %2 1299 /// ret i64 %3 1300 /// 1301 /// The returned value of the cmpxchg instruction (%2) is the original value 1302 /// located at %ptr prior to any update. If the cmpxchg operation succeeds, %2 1303 /// must have been equal to %compare. Thus, the result of the select is always 1304 /// equal to %2, and the code can be simplified to: 1305 /// 1306 /// %0 = cmpxchg i64* %ptr, i64 %compare, i64 %new_value seq_cst seq_cst 1307 /// %1 = extractvalue { i64, i1 } %0, 0 1308 /// ret i64 %1 1309 /// 1310 static Instruction *foldSelectCmpXchg(SelectInst &SI) { 1311 // A helper that determines if V is an extractvalue instruction whose 1312 // aggregate operand is a cmpxchg instruction and whose single index is equal 1313 // to I. If such conditions are true, the helper returns the cmpxchg 1314 // instruction; otherwise, a nullptr is returned. 1315 auto isExtractFromCmpXchg = [](Value *V, unsigned I) -> AtomicCmpXchgInst * { 1316 auto *Extract = dyn_cast<ExtractValueInst>(V); 1317 if (!Extract) 1318 return nullptr; 1319 if (Extract->getIndices()[0] != I) 1320 return nullptr; 1321 return dyn_cast<AtomicCmpXchgInst>(Extract->getAggregateOperand()); 1322 }; 1323 1324 // If the select has a single user, and this user is a select instruction that 1325 // we can simplify, skip the cmpxchg simplification for now. 1326 if (SI.hasOneUse()) 1327 if (auto *Select = dyn_cast<SelectInst>(SI.user_back())) 1328 if (Select->getCondition() == SI.getCondition()) 1329 if (Select->getFalseValue() == SI.getTrueValue() || 1330 Select->getTrueValue() == SI.getFalseValue()) 1331 return nullptr; 1332 1333 // Ensure the select condition is the returned flag of a cmpxchg instruction. 1334 auto *CmpXchg = isExtractFromCmpXchg(SI.getCondition(), 1); 1335 if (!CmpXchg) 1336 return nullptr; 1337 1338 // Check the true value case: The true value of the select is the returned 1339 // value of the same cmpxchg used by the condition, and the false value is the 1340 // cmpxchg instruction's compare operand. 1341 if (auto *X = isExtractFromCmpXchg(SI.getTrueValue(), 0)) 1342 if (X == CmpXchg && X->getCompareOperand() == SI.getFalseValue()) { 1343 SI.setTrueValue(SI.getFalseValue()); 1344 return &SI; 1345 } 1346 1347 // Check the false value case: The false value of the select is the returned 1348 // value of the same cmpxchg used by the condition, and the true value is the 1349 // cmpxchg instruction's compare operand. 1350 if (auto *X = isExtractFromCmpXchg(SI.getFalseValue(), 0)) 1351 if (X == CmpXchg && X->getCompareOperand() == SI.getTrueValue()) { 1352 SI.setTrueValue(SI.getFalseValue()); 1353 return &SI; 1354 } 1355 1356 return nullptr; 1357 } 1358 1359 /// Reduce a sequence of min/max with a common operand. 1360 static Instruction *factorizeMinMaxTree(SelectPatternFlavor SPF, Value *LHS, 1361 Value *RHS, 1362 InstCombiner::BuilderTy &Builder) { 1363 assert(SelectPatternResult::isMinOrMax(SPF) && "Expected a min/max"); 1364 // TODO: Allow FP min/max with nnan/nsz. 1365 if (!LHS->getType()->isIntOrIntVectorTy()) 1366 return nullptr; 1367 1368 // Match 3 of the same min/max ops. Example: umin(umin(), umin()). 1369 Value *A, *B, *C, *D; 1370 SelectPatternResult L = matchSelectPattern(LHS, A, B); 1371 SelectPatternResult R = matchSelectPattern(RHS, C, D); 1372 if (SPF != L.Flavor || L.Flavor != R.Flavor) 1373 return nullptr; 1374 1375 // Look for a common operand. The use checks are different than usual because 1376 // a min/max pattern typically has 2 uses of each op: 1 by the cmp and 1 by 1377 // the select. 1378 Value *MinMaxOp = nullptr; 1379 Value *ThirdOp = nullptr; 1380 if (LHS->getNumUses() <= 2 && RHS->getNumUses() > 2) { 1381 // If the LHS is only used in this chain and the RHS is used outside of it, 1382 // reuse the RHS min/max because that will eliminate the LHS. 1383 if (D == A || C == A) { 1384 // min(min(a, b), min(c, a)) --> min(min(c, a), b) 1385 // min(min(a, b), min(a, d)) --> min(min(a, d), b) 1386 MinMaxOp = RHS; 1387 ThirdOp = B; 1388 } else if (D == B || C == B) { 1389 // min(min(a, b), min(c, b)) --> min(min(c, b), a) 1390 // min(min(a, b), min(b, d)) --> min(min(b, d), a) 1391 MinMaxOp = RHS; 1392 ThirdOp = A; 1393 } 1394 } else if (RHS->getNumUses() <= 2) { 1395 // Reuse the LHS. This will eliminate the RHS. 1396 if (D == A || D == B) { 1397 // min(min(a, b), min(c, a)) --> min(min(a, b), c) 1398 // min(min(a, b), min(c, b)) --> min(min(a, b), c) 1399 MinMaxOp = LHS; 1400 ThirdOp = C; 1401 } else if (C == A || C == B) { 1402 // min(min(a, b), min(b, d)) --> min(min(a, b), d) 1403 // min(min(a, b), min(c, b)) --> min(min(a, b), d) 1404 MinMaxOp = LHS; 1405 ThirdOp = D; 1406 } 1407 } 1408 if (!MinMaxOp || !ThirdOp) 1409 return nullptr; 1410 1411 CmpInst::Predicate P = getCmpPredicateForMinMax(SPF); 1412 Value *CmpABC = Builder.CreateICmp(P, MinMaxOp, ThirdOp); 1413 return SelectInst::Create(CmpABC, MinMaxOp, ThirdOp); 1414 } 1415 1416 Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { 1417 Value *CondVal = SI.getCondition(); 1418 Value *TrueVal = SI.getTrueValue(); 1419 Value *FalseVal = SI.getFalseValue(); 1420 Type *SelType = SI.getType(); 1421 1422 // FIXME: Remove this workaround when freeze related patches are done. 1423 // For select with undef operand which feeds into an equality comparison, 1424 // don't simplify it so loop unswitch can know the equality comparison 1425 // may have an undef operand. This is a workaround for PR31652 caused by 1426 // descrepancy about branch on undef between LoopUnswitch and GVN. 1427 if (isa<UndefValue>(TrueVal) || isa<UndefValue>(FalseVal)) { 1428 if (llvm::any_of(SI.users(), [&](User *U) { 1429 ICmpInst *CI = dyn_cast<ICmpInst>(U); 1430 if (CI && CI->isEquality()) 1431 return true; 1432 return false; 1433 })) { 1434 return nullptr; 1435 } 1436 } 1437 1438 if (Value *V = SimplifySelectInst(CondVal, TrueVal, FalseVal, 1439 SQ.getWithInstruction(&SI))) 1440 return replaceInstUsesWith(SI, V); 1441 1442 if (Instruction *I = canonicalizeSelectToShuffle(SI)) 1443 return I; 1444 1445 // Canonicalize a one-use integer compare with a non-canonical predicate by 1446 // inverting the predicate and swapping the select operands. This matches a 1447 // compare canonicalization for conditional branches. 1448 // TODO: Should we do the same for FP compares? 1449 CmpInst::Predicate Pred; 1450 if (match(CondVal, m_OneUse(m_ICmp(Pred, m_Value(), m_Value()))) && 1451 !isCanonicalPredicate(Pred)) { 1452 // Swap true/false values and condition. 1453 CmpInst *Cond = cast<CmpInst>(CondVal); 1454 Cond->setPredicate(CmpInst::getInversePredicate(Pred)); 1455 SI.setOperand(1, FalseVal); 1456 SI.setOperand(2, TrueVal); 1457 SI.swapProfMetadata(); 1458 Worklist.Add(Cond); 1459 return &SI; 1460 } 1461 1462 if (SelType->isIntOrIntVectorTy(1) && 1463 TrueVal->getType() == CondVal->getType()) { 1464 if (match(TrueVal, m_One())) { 1465 // Change: A = select B, true, C --> A = or B, C 1466 return BinaryOperator::CreateOr(CondVal, FalseVal); 1467 } 1468 if (match(TrueVal, m_Zero())) { 1469 // Change: A = select B, false, C --> A = and !B, C 1470 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); 1471 return BinaryOperator::CreateAnd(NotCond, FalseVal); 1472 } 1473 if (match(FalseVal, m_Zero())) { 1474 // Change: A = select B, C, false --> A = and B, C 1475 return BinaryOperator::CreateAnd(CondVal, TrueVal); 1476 } 1477 if (match(FalseVal, m_One())) { 1478 // Change: A = select B, C, true --> A = or !B, C 1479 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); 1480 return BinaryOperator::CreateOr(NotCond, TrueVal); 1481 } 1482 1483 // select a, a, b -> a | b 1484 // select a, b, a -> a & b 1485 if (CondVal == TrueVal) 1486 return BinaryOperator::CreateOr(CondVal, FalseVal); 1487 if (CondVal == FalseVal) 1488 return BinaryOperator::CreateAnd(CondVal, TrueVal); 1489 1490 // select a, ~a, b -> (~a) & b 1491 // select a, b, ~a -> (~a) | b 1492 if (match(TrueVal, m_Not(m_Specific(CondVal)))) 1493 return BinaryOperator::CreateAnd(TrueVal, FalseVal); 1494 if (match(FalseVal, m_Not(m_Specific(CondVal)))) 1495 return BinaryOperator::CreateOr(TrueVal, FalseVal); 1496 } 1497 1498 // Selecting between two integer or vector splat integer constants? 1499 // 1500 // Note that we don't handle a scalar select of vectors: 1501 // select i1 %c, <2 x i8> <1, 1>, <2 x i8> <0, 0> 1502 // because that may need 3 instructions to splat the condition value: 1503 // extend, insertelement, shufflevector. 1504 if (SelType->isIntOrIntVectorTy() && 1505 CondVal->getType()->isVectorTy() == SelType->isVectorTy()) { 1506 // select C, 1, 0 -> zext C to int 1507 if (match(TrueVal, m_One()) && match(FalseVal, m_Zero())) 1508 return new ZExtInst(CondVal, SelType); 1509 1510 // select C, -1, 0 -> sext C to int 1511 if (match(TrueVal, m_AllOnes()) && match(FalseVal, m_Zero())) 1512 return new SExtInst(CondVal, SelType); 1513 1514 // select C, 0, 1 -> zext !C to int 1515 if (match(TrueVal, m_Zero()) && match(FalseVal, m_One())) { 1516 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); 1517 return new ZExtInst(NotCond, SelType); 1518 } 1519 1520 // select C, 0, -1 -> sext !C to int 1521 if (match(TrueVal, m_Zero()) && match(FalseVal, m_AllOnes())) { 1522 Value *NotCond = Builder.CreateNot(CondVal, "not." + CondVal->getName()); 1523 return new SExtInst(NotCond, SelType); 1524 } 1525 } 1526 1527 // See if we are selecting two values based on a comparison of the two values. 1528 if (FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) { 1529 if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) { 1530 // Transform (X == Y) ? X : Y -> Y 1531 if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { 1532 // This is not safe in general for floating point: 1533 // consider X== -0, Y== +0. 1534 // It becomes safe if either operand is a nonzero constant. 1535 ConstantFP *CFPt, *CFPf; 1536 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && 1537 !CFPt->getValueAPF().isZero()) || 1538 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && 1539 !CFPf->getValueAPF().isZero())) 1540 return replaceInstUsesWith(SI, FalseVal); 1541 } 1542 // Transform (X une Y) ? X : Y -> X 1543 if (FCI->getPredicate() == FCmpInst::FCMP_UNE) { 1544 // This is not safe in general for floating point: 1545 // consider X== -0, Y== +0. 1546 // It becomes safe if either operand is a nonzero constant. 1547 ConstantFP *CFPt, *CFPf; 1548 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && 1549 !CFPt->getValueAPF().isZero()) || 1550 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && 1551 !CFPf->getValueAPF().isZero())) 1552 return replaceInstUsesWith(SI, TrueVal); 1553 } 1554 1555 // Canonicalize to use ordered comparisons by swapping the select 1556 // operands. 1557 // 1558 // e.g. 1559 // (X ugt Y) ? X : Y -> (X ole Y) ? Y : X 1560 if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) { 1561 FCmpInst::Predicate InvPred = FCI->getInversePredicate(); 1562 IRBuilder<>::FastMathFlagGuard FMFG(Builder); 1563 Builder.setFastMathFlags(FCI->getFastMathFlags()); 1564 Value *NewCond = Builder.CreateFCmp(InvPred, TrueVal, FalseVal, 1565 FCI->getName() + ".inv"); 1566 1567 return SelectInst::Create(NewCond, FalseVal, TrueVal, 1568 SI.getName() + ".p"); 1569 } 1570 1571 // NOTE: if we wanted to, this is where to detect MIN/MAX 1572 } else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){ 1573 // Transform (X == Y) ? Y : X -> X 1574 if (FCI->getPredicate() == FCmpInst::FCMP_OEQ) { 1575 // This is not safe in general for floating point: 1576 // consider X== -0, Y== +0. 1577 // It becomes safe if either operand is a nonzero constant. 1578 ConstantFP *CFPt, *CFPf; 1579 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && 1580 !CFPt->getValueAPF().isZero()) || 1581 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && 1582 !CFPf->getValueAPF().isZero())) 1583 return replaceInstUsesWith(SI, FalseVal); 1584 } 1585 // Transform (X une Y) ? Y : X -> Y 1586 if (FCI->getPredicate() == FCmpInst::FCMP_UNE) { 1587 // This is not safe in general for floating point: 1588 // consider X== -0, Y== +0. 1589 // It becomes safe if either operand is a nonzero constant. 1590 ConstantFP *CFPt, *CFPf; 1591 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) && 1592 !CFPt->getValueAPF().isZero()) || 1593 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) && 1594 !CFPf->getValueAPF().isZero())) 1595 return replaceInstUsesWith(SI, TrueVal); 1596 } 1597 1598 // Canonicalize to use ordered comparisons by swapping the select 1599 // operands. 1600 // 1601 // e.g. 1602 // (X ugt Y) ? X : Y -> (X ole Y) ? X : Y 1603 if (FCI->hasOneUse() && FCmpInst::isUnordered(FCI->getPredicate())) { 1604 FCmpInst::Predicate InvPred = FCI->getInversePredicate(); 1605 IRBuilder<>::FastMathFlagGuard FMFG(Builder); 1606 Builder.setFastMathFlags(FCI->getFastMathFlags()); 1607 Value *NewCond = Builder.CreateFCmp(InvPred, FalseVal, TrueVal, 1608 FCI->getName() + ".inv"); 1609 1610 return SelectInst::Create(NewCond, FalseVal, TrueVal, 1611 SI.getName() + ".p"); 1612 } 1613 1614 // NOTE: if we wanted to, this is where to detect MIN/MAX 1615 } 1616 // NOTE: if we wanted to, this is where to detect ABS 1617 } 1618 1619 // See if we are selecting two values based on a comparison of the two values. 1620 if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal)) 1621 if (Instruction *Result = foldSelectInstWithICmp(SI, ICI)) 1622 return Result; 1623 1624 if (Instruction *Add = foldAddSubSelect(SI, Builder)) 1625 return Add; 1626 1627 // Turn (select C, (op X, Y), (op X, Z)) -> (op X, (select C, Y, Z)) 1628 auto *TI = dyn_cast<Instruction>(TrueVal); 1629 auto *FI = dyn_cast<Instruction>(FalseVal); 1630 if (TI && FI && TI->getOpcode() == FI->getOpcode()) 1631 if (Instruction *IV = foldSelectOpOp(SI, TI, FI)) 1632 return IV; 1633 1634 if (Instruction *I = foldSelectExtConst(SI)) 1635 return I; 1636 1637 // See if we can fold the select into one of our operands. 1638 if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) { 1639 if (Instruction *FoldI = foldSelectIntoOp(SI, TrueVal, FalseVal)) 1640 return FoldI; 1641 1642 Value *LHS, *RHS, *LHS2, *RHS2; 1643 Instruction::CastOps CastOp; 1644 SelectPatternResult SPR = matchSelectPattern(&SI, LHS, RHS, &CastOp); 1645 auto SPF = SPR.Flavor; 1646 1647 if (SelectPatternResult::isMinOrMax(SPF)) { 1648 // Canonicalize so that 1649 // - type casts are outside select patterns. 1650 // - float clamp is transformed to min/max pattern 1651 1652 bool IsCastNeeded = LHS->getType() != SelType; 1653 Value *CmpLHS = cast<CmpInst>(CondVal)->getOperand(0); 1654 Value *CmpRHS = cast<CmpInst>(CondVal)->getOperand(1); 1655 if (IsCastNeeded || 1656 (LHS->getType()->isFPOrFPVectorTy() && 1657 ((CmpLHS != LHS && CmpLHS != RHS) || 1658 (CmpRHS != LHS && CmpRHS != RHS)))) { 1659 CmpInst::Predicate Pred = getCmpPredicateForMinMax(SPF, SPR.Ordered); 1660 1661 Value *Cmp; 1662 if (CmpInst::isIntPredicate(Pred)) { 1663 Cmp = Builder.CreateICmp(Pred, LHS, RHS); 1664 } else { 1665 IRBuilder<>::FastMathFlagGuard FMFG(Builder); 1666 auto FMF = cast<FPMathOperator>(SI.getCondition())->getFastMathFlags(); 1667 Builder.setFastMathFlags(FMF); 1668 Cmp = Builder.CreateFCmp(Pred, LHS, RHS); 1669 } 1670 1671 Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI); 1672 if (!IsCastNeeded) 1673 return replaceInstUsesWith(SI, NewSI); 1674 1675 Value *NewCast = Builder.CreateCast(CastOp, NewSI, SelType); 1676 return replaceInstUsesWith(SI, NewCast); 1677 } 1678 1679 // MAX(~a, ~b) -> ~MIN(a, b) 1680 // MIN(~a, ~b) -> ~MAX(a, b) 1681 Value *A, *B; 1682 if (match(LHS, m_Not(m_Value(A))) && match(RHS, m_Not(m_Value(B))) && 1683 (LHS->getNumUses() <= 2 || RHS->getNumUses() <= 2)) { 1684 CmpInst::Predicate InvertedPred = 1685 getCmpPredicateForMinMax(getInverseMinMaxSelectPattern(SPF)); 1686 Value *InvertedCmp = Builder.CreateICmp(InvertedPred, A, B); 1687 Value *NewSel = Builder.CreateSelect(InvertedCmp, A, B); 1688 return BinaryOperator::CreateNot(NewSel); 1689 } 1690 1691 if (Instruction *I = factorizeMinMaxTree(SPF, LHS, RHS, Builder)) 1692 return I; 1693 } 1694 1695 if (SPF) { 1696 // MAX(MAX(a, b), a) -> MAX(a, b) 1697 // MIN(MIN(a, b), a) -> MIN(a, b) 1698 // MAX(MIN(a, b), a) -> a 1699 // MIN(MAX(a, b), a) -> a 1700 // ABS(ABS(a)) -> ABS(a) 1701 // NABS(NABS(a)) -> NABS(a) 1702 if (SelectPatternFlavor SPF2 = matchSelectPattern(LHS, LHS2, RHS2).Flavor) 1703 if (Instruction *R = foldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2, 1704 SI, SPF, RHS)) 1705 return R; 1706 if (SelectPatternFlavor SPF2 = matchSelectPattern(RHS, LHS2, RHS2).Flavor) 1707 if (Instruction *R = foldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2, 1708 SI, SPF, LHS)) 1709 return R; 1710 } 1711 1712 // TODO. 1713 // ABS(-X) -> ABS(X) 1714 } 1715 1716 // See if we can fold the select into a phi node if the condition is a select. 1717 if (auto *PN = dyn_cast<PHINode>(SI.getCondition())) 1718 // The true/false values have to be live in the PHI predecessor's blocks. 1719 if (canSelectOperandBeMappingIntoPredBlock(TrueVal, SI) && 1720 canSelectOperandBeMappingIntoPredBlock(FalseVal, SI)) 1721 if (Instruction *NV = foldOpIntoPhi(SI, PN)) 1722 return NV; 1723 1724 if (SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) { 1725 if (TrueSI->getCondition()->getType() == CondVal->getType()) { 1726 // select(C, select(C, a, b), c) -> select(C, a, c) 1727 if (TrueSI->getCondition() == CondVal) { 1728 if (SI.getTrueValue() == TrueSI->getTrueValue()) 1729 return nullptr; 1730 SI.setOperand(1, TrueSI->getTrueValue()); 1731 return &SI; 1732 } 1733 // select(C0, select(C1, a, b), b) -> select(C0&C1, a, b) 1734 // We choose this as normal form to enable folding on the And and shortening 1735 // paths for the values (this helps GetUnderlyingObjects() for example). 1736 if (TrueSI->getFalseValue() == FalseVal && TrueSI->hasOneUse()) { 1737 Value *And = Builder.CreateAnd(CondVal, TrueSI->getCondition()); 1738 SI.setOperand(0, And); 1739 SI.setOperand(1, TrueSI->getTrueValue()); 1740 return &SI; 1741 } 1742 } 1743 } 1744 if (SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) { 1745 if (FalseSI->getCondition()->getType() == CondVal->getType()) { 1746 // select(C, a, select(C, b, c)) -> select(C, a, c) 1747 if (FalseSI->getCondition() == CondVal) { 1748 if (SI.getFalseValue() == FalseSI->getFalseValue()) 1749 return nullptr; 1750 SI.setOperand(2, FalseSI->getFalseValue()); 1751 return &SI; 1752 } 1753 // select(C0, a, select(C1, a, b)) -> select(C0|C1, a, b) 1754 if (FalseSI->getTrueValue() == TrueVal && FalseSI->hasOneUse()) { 1755 Value *Or = Builder.CreateOr(CondVal, FalseSI->getCondition()); 1756 SI.setOperand(0, Or); 1757 SI.setOperand(2, FalseSI->getFalseValue()); 1758 return &SI; 1759 } 1760 } 1761 } 1762 1763 auto canMergeSelectThroughBinop = [](BinaryOperator *BO) { 1764 // The select might be preventing a division by 0. 1765 switch (BO->getOpcode()) { 1766 default: 1767 return true; 1768 case Instruction::SRem: 1769 case Instruction::URem: 1770 case Instruction::SDiv: 1771 case Instruction::UDiv: 1772 return false; 1773 } 1774 }; 1775 1776 // Try to simplify a binop sandwiched between 2 selects with the same 1777 // condition. 1778 // select(C, binop(select(C, X, Y), W), Z) -> select(C, binop(X, W), Z) 1779 BinaryOperator *TrueBO; 1780 if (match(TrueVal, m_OneUse(m_BinOp(TrueBO))) && 1781 canMergeSelectThroughBinop(TrueBO)) { 1782 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(0))) { 1783 if (TrueBOSI->getCondition() == CondVal) { 1784 TrueBO->setOperand(0, TrueBOSI->getTrueValue()); 1785 Worklist.Add(TrueBO); 1786 return &SI; 1787 } 1788 } 1789 if (auto *TrueBOSI = dyn_cast<SelectInst>(TrueBO->getOperand(1))) { 1790 if (TrueBOSI->getCondition() == CondVal) { 1791 TrueBO->setOperand(1, TrueBOSI->getTrueValue()); 1792 Worklist.Add(TrueBO); 1793 return &SI; 1794 } 1795 } 1796 } 1797 1798 // select(C, Z, binop(select(C, X, Y), W)) -> select(C, Z, binop(Y, W)) 1799 BinaryOperator *FalseBO; 1800 if (match(FalseVal, m_OneUse(m_BinOp(FalseBO))) && 1801 canMergeSelectThroughBinop(FalseBO)) { 1802 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(0))) { 1803 if (FalseBOSI->getCondition() == CondVal) { 1804 FalseBO->setOperand(0, FalseBOSI->getFalseValue()); 1805 Worklist.Add(FalseBO); 1806 return &SI; 1807 } 1808 } 1809 if (auto *FalseBOSI = dyn_cast<SelectInst>(FalseBO->getOperand(1))) { 1810 if (FalseBOSI->getCondition() == CondVal) { 1811 FalseBO->setOperand(1, FalseBOSI->getFalseValue()); 1812 Worklist.Add(FalseBO); 1813 return &SI; 1814 } 1815 } 1816 } 1817 1818 if (BinaryOperator::isNot(CondVal)) { 1819 SI.setOperand(0, BinaryOperator::getNotArgument(CondVal)); 1820 SI.setOperand(1, FalseVal); 1821 SI.setOperand(2, TrueVal); 1822 return &SI; 1823 } 1824 1825 if (VectorType *VecTy = dyn_cast<VectorType>(SelType)) { 1826 unsigned VWidth = VecTy->getNumElements(); 1827 APInt UndefElts(VWidth, 0); 1828 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); 1829 if (Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) { 1830 if (V != &SI) 1831 return replaceInstUsesWith(SI, V); 1832 return &SI; 1833 } 1834 } 1835 1836 // See if we can determine the result of this select based on a dominating 1837 // condition. 1838 BasicBlock *Parent = SI.getParent(); 1839 if (BasicBlock *Dom = Parent->getSinglePredecessor()) { 1840 auto *PBI = dyn_cast_or_null<BranchInst>(Dom->getTerminator()); 1841 if (PBI && PBI->isConditional() && 1842 PBI->getSuccessor(0) != PBI->getSuccessor(1) && 1843 (PBI->getSuccessor(0) == Parent || PBI->getSuccessor(1) == Parent)) { 1844 bool CondIsTrue = PBI->getSuccessor(0) == Parent; 1845 Optional<bool> Implication = isImpliedCondition( 1846 PBI->getCondition(), SI.getCondition(), DL, CondIsTrue); 1847 if (Implication) { 1848 Value *V = *Implication ? TrueVal : FalseVal; 1849 return replaceInstUsesWith(SI, V); 1850 } 1851 } 1852 } 1853 1854 // If we can compute the condition, there's no need for a select. 1855 // Like the above fold, we are attempting to reduce compile-time cost by 1856 // putting this fold here with limitations rather than in InstSimplify. 1857 // The motivation for this call into value tracking is to take advantage of 1858 // the assumption cache, so make sure that is populated. 1859 if (!CondVal->getType()->isVectorTy() && !AC.assumptions().empty()) { 1860 KnownBits Known(1); 1861 computeKnownBits(CondVal, Known, 0, &SI); 1862 if (Known.One.isOneValue()) 1863 return replaceInstUsesWith(SI, TrueVal); 1864 if (Known.Zero.isOneValue()) 1865 return replaceInstUsesWith(SI, FalseVal); 1866 } 1867 1868 if (Instruction *BitCastSel = foldSelectCmpBitcasts(SI, Builder)) 1869 return BitCastSel; 1870 1871 // Simplify selects that test the returned flag of cmpxchg instructions. 1872 if (Instruction *Select = foldSelectCmpXchg(SI)) 1873 return Select; 1874 1875 return nullptr; 1876 } 1877