1 //===- ConstantFold.cpp - LLVM constant folder ----------------------------===// 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 folding of constants for LLVM. This implements the 10 // (internal) ConstantFold.h interface, which is used by the 11 // ConstantExpr::get* methods to automatically fold constants when possible. 12 // 13 // The current constant folding implementation is implemented in two pieces: the 14 // pieces that don't need DataLayout, and the pieces that do. This is to avoid 15 // a dependence in IR on Target. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "llvm/IR/ConstantFold.h" 20 #include "llvm/ADT/APSInt.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/DerivedTypes.h" 24 #include "llvm/IR/Function.h" 25 #include "llvm/IR/GetElementPtrTypeIterator.h" 26 #include "llvm/IR/GlobalAlias.h" 27 #include "llvm/IR/GlobalVariable.h" 28 #include "llvm/IR/Instructions.h" 29 #include "llvm/IR/Module.h" 30 #include "llvm/IR/Operator.h" 31 #include "llvm/IR/PatternMatch.h" 32 #include "llvm/Support/ErrorHandling.h" 33 using namespace llvm; 34 using namespace llvm::PatternMatch; 35 36 //===----------------------------------------------------------------------===// 37 // ConstantFold*Instruction Implementations 38 //===----------------------------------------------------------------------===// 39 40 /// This function determines which opcode to use to fold two constant cast 41 /// expressions together. It uses CastInst::isEliminableCastPair to determine 42 /// the opcode. Consequently its just a wrapper around that function. 43 /// Determine if it is valid to fold a cast of a cast 44 static unsigned 45 foldConstantCastPair( 46 unsigned opc, ///< opcode of the second cast constant expression 47 ConstantExpr *Op, ///< the first cast constant expression 48 Type *DstTy ///< destination type of the first cast 49 ) { 50 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!"); 51 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type"); 52 assert(CastInst::isCast(opc) && "Invalid cast opcode"); 53 54 // The types and opcodes for the two Cast constant expressions 55 Type *SrcTy = Op->getOperand(0)->getType(); 56 Type *MidTy = Op->getType(); 57 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode()); 58 Instruction::CastOps secondOp = Instruction::CastOps(opc); 59 60 // Assume that pointers are never more than 64 bits wide, and only use this 61 // for the middle type. Otherwise we could end up folding away illegal 62 // bitcasts between address spaces with different sizes. 63 IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext()); 64 65 // Let CastInst::isEliminableCastPair do the heavy lifting. 66 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, 67 nullptr, FakeIntPtrTy, nullptr); 68 } 69 70 static Constant *FoldBitCast(Constant *V, Type *DestTy) { 71 Type *SrcTy = V->getType(); 72 if (SrcTy == DestTy) 73 return V; // no-op cast 74 75 // Handle casts from one vector constant to another. We know that the src 76 // and dest type have the same size (otherwise its an illegal cast). 77 if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { 78 if (V->isAllOnesValue()) 79 return Constant::getAllOnesValue(DestTy); 80 81 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts 82 // This allows for other simplifications (although some of them 83 // can only be handled by Analysis/ConstantFolding.cpp). 84 if (!isa<VectorType>(SrcTy)) 85 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) 86 return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy); 87 return nullptr; 88 } 89 90 // Handle integral constant input. 91 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 92 // See note below regarding the PPC_FP128 restriction. 93 if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty()) 94 return ConstantFP::get(DestTy->getContext(), 95 APFloat(DestTy->getFltSemantics(), 96 CI->getValue())); 97 98 // Otherwise, can't fold this (vector?) 99 return nullptr; 100 } 101 102 // Handle ConstantFP input: FP -> Integral. 103 if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) { 104 // PPC_FP128 is really the sum of two consecutive doubles, where the first 105 // double is always stored first in memory, regardless of the target 106 // endianness. The memory layout of i128, however, depends on the target 107 // endianness, and so we can't fold this without target endianness 108 // information. This should instead be handled by 109 // Analysis/ConstantFolding.cpp 110 if (FP->getType()->isPPC_FP128Ty()) 111 return nullptr; 112 113 // Make sure dest type is compatible with the folded integer constant. 114 if (!DestTy->isIntegerTy()) 115 return nullptr; 116 117 return ConstantInt::get(FP->getContext(), 118 FP->getValueAPF().bitcastToAPInt()); 119 } 120 121 return nullptr; 122 } 123 124 static Constant *foldMaybeUndesirableCast(unsigned opc, Constant *V, 125 Type *DestTy) { 126 return ConstantExpr::isDesirableCastOp(opc) 127 ? ConstantExpr::getCast(opc, V, DestTy) 128 : ConstantFoldCastInstruction(opc, V, DestTy); 129 } 130 131 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, 132 Type *DestTy) { 133 if (isa<PoisonValue>(V)) 134 return PoisonValue::get(DestTy); 135 136 if (isa<UndefValue>(V)) { 137 // zext(undef) = 0, because the top bits will be zero. 138 // sext(undef) = 0, because the top bits will all be the same. 139 // [us]itofp(undef) = 0, because the result value is bounded. 140 if (opc == Instruction::ZExt || opc == Instruction::SExt || 141 opc == Instruction::UIToFP || opc == Instruction::SIToFP) 142 return Constant::getNullValue(DestTy); 143 return UndefValue::get(DestTy); 144 } 145 146 if (V->isNullValue() && !DestTy->isX86_AMXTy() && 147 opc != Instruction::AddrSpaceCast) 148 return Constant::getNullValue(DestTy); 149 150 // If the cast operand is a constant expression, there's a few things we can 151 // do to try to simplify it. 152 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 153 if (CE->isCast()) { 154 // Try hard to fold cast of cast because they are often eliminable. 155 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) 156 return foldMaybeUndesirableCast(newOpc, CE->getOperand(0), DestTy); 157 } 158 } 159 160 // If the cast operand is a constant vector, perform the cast by 161 // operating on each element. In the cast of bitcasts, the element 162 // count may be mismatched; don't attempt to handle that here. 163 if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) && 164 DestTy->isVectorTy() && 165 cast<FixedVectorType>(DestTy)->getNumElements() == 166 cast<FixedVectorType>(V->getType())->getNumElements()) { 167 VectorType *DestVecTy = cast<VectorType>(DestTy); 168 Type *DstEltTy = DestVecTy->getElementType(); 169 // Fast path for splatted constants. 170 if (Constant *Splat = V->getSplatValue()) { 171 Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy); 172 if (!Res) 173 return nullptr; 174 return ConstantVector::getSplat( 175 cast<VectorType>(DestTy)->getElementCount(), Res); 176 } 177 SmallVector<Constant *, 16> res; 178 Type *Ty = IntegerType::get(V->getContext(), 32); 179 for (unsigned i = 0, 180 e = cast<FixedVectorType>(V->getType())->getNumElements(); 181 i != e; ++i) { 182 Constant *C = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i)); 183 Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy); 184 if (!Casted) 185 return nullptr; 186 res.push_back(Casted); 187 } 188 return ConstantVector::get(res); 189 } 190 191 // We actually have to do a cast now. Perform the cast according to the 192 // opcode specified. 193 switch (opc) { 194 default: 195 llvm_unreachable("Failed to cast constant expression"); 196 case Instruction::FPTrunc: 197 case Instruction::FPExt: 198 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { 199 bool ignored; 200 APFloat Val = FPC->getValueAPF(); 201 Val.convert(DestTy->getFltSemantics(), APFloat::rmNearestTiesToEven, 202 &ignored); 203 return ConstantFP::get(V->getContext(), Val); 204 } 205 return nullptr; // Can't fold. 206 case Instruction::FPToUI: 207 case Instruction::FPToSI: 208 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { 209 const APFloat &V = FPC->getValueAPF(); 210 bool ignored; 211 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 212 APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI); 213 if (APFloat::opInvalidOp == 214 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) { 215 // Undefined behavior invoked - the destination type can't represent 216 // the input constant. 217 return PoisonValue::get(DestTy); 218 } 219 return ConstantInt::get(FPC->getContext(), IntVal); 220 } 221 return nullptr; // Can't fold. 222 case Instruction::UIToFP: 223 case Instruction::SIToFP: 224 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 225 const APInt &api = CI->getValue(); 226 APFloat apf(DestTy->getFltSemantics(), 227 APInt::getZero(DestTy->getPrimitiveSizeInBits())); 228 apf.convertFromAPInt(api, opc==Instruction::SIToFP, 229 APFloat::rmNearestTiesToEven); 230 return ConstantFP::get(V->getContext(), apf); 231 } 232 return nullptr; 233 case Instruction::ZExt: 234 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 235 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 236 return ConstantInt::get(V->getContext(), 237 CI->getValue().zext(BitWidth)); 238 } 239 return nullptr; 240 case Instruction::SExt: 241 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 242 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 243 return ConstantInt::get(V->getContext(), 244 CI->getValue().sext(BitWidth)); 245 } 246 return nullptr; 247 case Instruction::Trunc: { 248 if (V->getType()->isVectorTy()) 249 return nullptr; 250 251 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 252 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 253 return ConstantInt::get(V->getContext(), 254 CI->getValue().trunc(DestBitWidth)); 255 } 256 257 return nullptr; 258 } 259 case Instruction::BitCast: 260 return FoldBitCast(V, DestTy); 261 case Instruction::AddrSpaceCast: 262 case Instruction::IntToPtr: 263 case Instruction::PtrToInt: 264 return nullptr; 265 } 266 } 267 268 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, 269 Constant *V1, Constant *V2) { 270 // Check for i1 and vector true/false conditions. 271 if (Cond->isNullValue()) return V2; 272 if (Cond->isAllOnesValue()) return V1; 273 274 // If the condition is a vector constant, fold the result elementwise. 275 if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) { 276 auto *V1VTy = CondV->getType(); 277 SmallVector<Constant*, 16> Result; 278 Type *Ty = IntegerType::get(CondV->getContext(), 32); 279 for (unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) { 280 Constant *V; 281 Constant *V1Element = ConstantExpr::getExtractElement(V1, 282 ConstantInt::get(Ty, i)); 283 Constant *V2Element = ConstantExpr::getExtractElement(V2, 284 ConstantInt::get(Ty, i)); 285 auto *Cond = cast<Constant>(CondV->getOperand(i)); 286 if (isa<PoisonValue>(Cond)) { 287 V = PoisonValue::get(V1Element->getType()); 288 } else if (V1Element == V2Element) { 289 V = V1Element; 290 } else if (isa<UndefValue>(Cond)) { 291 V = isa<UndefValue>(V1Element) ? V1Element : V2Element; 292 } else { 293 if (!isa<ConstantInt>(Cond)) break; 294 V = Cond->isNullValue() ? V2Element : V1Element; 295 } 296 Result.push_back(V); 297 } 298 299 // If we were able to build the vector, return it. 300 if (Result.size() == V1VTy->getNumElements()) 301 return ConstantVector::get(Result); 302 } 303 304 if (isa<PoisonValue>(Cond)) 305 return PoisonValue::get(V1->getType()); 306 307 if (isa<UndefValue>(Cond)) { 308 if (isa<UndefValue>(V1)) return V1; 309 return V2; 310 } 311 312 if (V1 == V2) return V1; 313 314 if (isa<PoisonValue>(V1)) 315 return V2; 316 if (isa<PoisonValue>(V2)) 317 return V1; 318 319 // If the true or false value is undef, we can fold to the other value as 320 // long as the other value isn't poison. 321 auto NotPoison = [](Constant *C) { 322 if (isa<PoisonValue>(C)) 323 return false; 324 325 // TODO: We can analyze ConstExpr by opcode to determine if there is any 326 // possibility of poison. 327 if (isa<ConstantExpr>(C)) 328 return false; 329 330 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) || 331 isa<ConstantPointerNull>(C) || isa<Function>(C)) 332 return true; 333 334 if (C->getType()->isVectorTy()) 335 return !C->containsPoisonElement() && !C->containsConstantExpression(); 336 337 // TODO: Recursively analyze aggregates or other constants. 338 return false; 339 }; 340 if (isa<UndefValue>(V1) && NotPoison(V2)) return V2; 341 if (isa<UndefValue>(V2) && NotPoison(V1)) return V1; 342 343 return nullptr; 344 } 345 346 Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val, 347 Constant *Idx) { 348 auto *ValVTy = cast<VectorType>(Val->getType()); 349 350 // extractelt poison, C -> poison 351 // extractelt C, undef -> poison 352 if (isa<PoisonValue>(Val) || isa<UndefValue>(Idx)) 353 return PoisonValue::get(ValVTy->getElementType()); 354 355 // extractelt undef, C -> undef 356 if (isa<UndefValue>(Val)) 357 return UndefValue::get(ValVTy->getElementType()); 358 359 auto *CIdx = dyn_cast<ConstantInt>(Idx); 360 if (!CIdx) 361 return nullptr; 362 363 if (auto *ValFVTy = dyn_cast<FixedVectorType>(Val->getType())) { 364 // ee({w,x,y,z}, wrong_value) -> poison 365 if (CIdx->uge(ValFVTy->getNumElements())) 366 return PoisonValue::get(ValFVTy->getElementType()); 367 } 368 369 // ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...) 370 if (auto *CE = dyn_cast<ConstantExpr>(Val)) { 371 if (auto *GEP = dyn_cast<GEPOperator>(CE)) { 372 SmallVector<Constant *, 8> Ops; 373 Ops.reserve(CE->getNumOperands()); 374 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 375 Constant *Op = CE->getOperand(i); 376 if (Op->getType()->isVectorTy()) { 377 Constant *ScalarOp = ConstantExpr::getExtractElement(Op, Idx); 378 if (!ScalarOp) 379 return nullptr; 380 Ops.push_back(ScalarOp); 381 } else 382 Ops.push_back(Op); 383 } 384 return CE->getWithOperands(Ops, ValVTy->getElementType(), false, 385 GEP->getSourceElementType()); 386 } else if (CE->getOpcode() == Instruction::InsertElement) { 387 if (const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) { 388 if (APSInt::isSameValue(APSInt(IEIdx->getValue()), 389 APSInt(CIdx->getValue()))) { 390 return CE->getOperand(1); 391 } else { 392 return ConstantExpr::getExtractElement(CE->getOperand(0), CIdx); 393 } 394 } 395 } 396 } 397 398 if (Constant *C = Val->getAggregateElement(CIdx)) 399 return C; 400 401 // Lane < Splat minimum vector width => extractelt Splat(x), Lane -> x 402 if (CIdx->getValue().ult(ValVTy->getElementCount().getKnownMinValue())) { 403 if (Constant *SplatVal = Val->getSplatValue()) 404 return SplatVal; 405 } 406 407 return nullptr; 408 } 409 410 Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val, 411 Constant *Elt, 412 Constant *Idx) { 413 if (isa<UndefValue>(Idx)) 414 return PoisonValue::get(Val->getType()); 415 416 // Inserting null into all zeros is still all zeros. 417 // TODO: This is true for undef and poison splats too. 418 if (isa<ConstantAggregateZero>(Val) && Elt->isNullValue()) 419 return Val; 420 421 ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); 422 if (!CIdx) return nullptr; 423 424 // Do not iterate on scalable vector. The num of elements is unknown at 425 // compile-time. 426 if (isa<ScalableVectorType>(Val->getType())) 427 return nullptr; 428 429 auto *ValTy = cast<FixedVectorType>(Val->getType()); 430 431 unsigned NumElts = ValTy->getNumElements(); 432 if (CIdx->uge(NumElts)) 433 return PoisonValue::get(Val->getType()); 434 435 SmallVector<Constant*, 16> Result; 436 Result.reserve(NumElts); 437 auto *Ty = Type::getInt32Ty(Val->getContext()); 438 uint64_t IdxVal = CIdx->getZExtValue(); 439 for (unsigned i = 0; i != NumElts; ++i) { 440 if (i == IdxVal) { 441 Result.push_back(Elt); 442 continue; 443 } 444 445 Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i)); 446 Result.push_back(C); 447 } 448 449 return ConstantVector::get(Result); 450 } 451 452 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, 453 ArrayRef<int> Mask) { 454 auto *V1VTy = cast<VectorType>(V1->getType()); 455 unsigned MaskNumElts = Mask.size(); 456 auto MaskEltCount = 457 ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy)); 458 Type *EltTy = V1VTy->getElementType(); 459 460 // Poison shuffle mask -> poison value. 461 if (all_of(Mask, [](int Elt) { return Elt == PoisonMaskElem; })) { 462 return PoisonValue::get(VectorType::get(EltTy, MaskEltCount)); 463 } 464 465 // If the mask is all zeros this is a splat, no need to go through all 466 // elements. 467 if (all_of(Mask, [](int Elt) { return Elt == 0; })) { 468 Type *Ty = IntegerType::get(V1->getContext(), 32); 469 Constant *Elt = 470 ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, 0)); 471 472 if (Elt->isNullValue()) { 473 auto *VTy = VectorType::get(EltTy, MaskEltCount); 474 return ConstantAggregateZero::get(VTy); 475 } else if (!MaskEltCount.isScalable()) 476 return ConstantVector::getSplat(MaskEltCount, Elt); 477 } 478 479 // Do not iterate on scalable vector. The num of elements is unknown at 480 // compile-time. 481 if (isa<ScalableVectorType>(V1VTy)) 482 return nullptr; 483 484 unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue(); 485 486 // Loop over the shuffle mask, evaluating each element. 487 SmallVector<Constant*, 32> Result; 488 for (unsigned i = 0; i != MaskNumElts; ++i) { 489 int Elt = Mask[i]; 490 if (Elt == -1) { 491 Result.push_back(UndefValue::get(EltTy)); 492 continue; 493 } 494 Constant *InElt; 495 if (unsigned(Elt) >= SrcNumElts*2) 496 InElt = UndefValue::get(EltTy); 497 else if (unsigned(Elt) >= SrcNumElts) { 498 Type *Ty = IntegerType::get(V2->getContext(), 32); 499 InElt = 500 ConstantExpr::getExtractElement(V2, 501 ConstantInt::get(Ty, Elt - SrcNumElts)); 502 } else { 503 Type *Ty = IntegerType::get(V1->getContext(), 32); 504 InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt)); 505 } 506 Result.push_back(InElt); 507 } 508 509 return ConstantVector::get(Result); 510 } 511 512 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg, 513 ArrayRef<unsigned> Idxs) { 514 // Base case: no indices, so return the entire value. 515 if (Idxs.empty()) 516 return Agg; 517 518 if (Constant *C = Agg->getAggregateElement(Idxs[0])) 519 return ConstantFoldExtractValueInstruction(C, Idxs.slice(1)); 520 521 return nullptr; 522 } 523 524 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, 525 Constant *Val, 526 ArrayRef<unsigned> Idxs) { 527 // Base case: no indices, so replace the entire value. 528 if (Idxs.empty()) 529 return Val; 530 531 unsigned NumElts; 532 if (StructType *ST = dyn_cast<StructType>(Agg->getType())) 533 NumElts = ST->getNumElements(); 534 else 535 NumElts = cast<ArrayType>(Agg->getType())->getNumElements(); 536 537 SmallVector<Constant*, 32> Result; 538 for (unsigned i = 0; i != NumElts; ++i) { 539 Constant *C = Agg->getAggregateElement(i); 540 if (!C) return nullptr; 541 542 if (Idxs[0] == i) 543 C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1)); 544 545 Result.push_back(C); 546 } 547 548 if (StructType *ST = dyn_cast<StructType>(Agg->getType())) 549 return ConstantStruct::get(ST, Result); 550 return ConstantArray::get(cast<ArrayType>(Agg->getType()), Result); 551 } 552 553 Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) { 554 assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected"); 555 556 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length 557 // vectors are always evaluated per element. 558 bool IsScalableVector = isa<ScalableVectorType>(C->getType()); 559 bool HasScalarUndefOrScalableVectorUndef = 560 (!C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(C); 561 562 if (HasScalarUndefOrScalableVectorUndef) { 563 switch (static_cast<Instruction::UnaryOps>(Opcode)) { 564 case Instruction::FNeg: 565 return C; // -undef -> undef 566 case Instruction::UnaryOpsEnd: 567 llvm_unreachable("Invalid UnaryOp"); 568 } 569 } 570 571 // Constant should not be UndefValue, unless these are vector constants. 572 assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue"); 573 // We only have FP UnaryOps right now. 574 assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp"); 575 576 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 577 const APFloat &CV = CFP->getValueAPF(); 578 switch (Opcode) { 579 default: 580 break; 581 case Instruction::FNeg: 582 return ConstantFP::get(C->getContext(), neg(CV)); 583 } 584 } else if (auto *VTy = dyn_cast<VectorType>(C->getType())) { 585 // Fast path for splatted constants. 586 if (Constant *Splat = C->getSplatValue()) 587 if (Constant *Elt = ConstantFoldUnaryInstruction(Opcode, Splat)) 588 return ConstantVector::getSplat(VTy->getElementCount(), Elt); 589 590 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) { 591 // Fold each element and create a vector constant from those constants. 592 Type *Ty = IntegerType::get(FVTy->getContext(), 32); 593 SmallVector<Constant *, 16> Result; 594 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) { 595 Constant *ExtractIdx = ConstantInt::get(Ty, i); 596 Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx); 597 Constant *Res = ConstantFoldUnaryInstruction(Opcode, Elt); 598 if (!Res) 599 return nullptr; 600 Result.push_back(Res); 601 } 602 603 return ConstantVector::get(Result); 604 } 605 } 606 607 // We don't know how to fold this. 608 return nullptr; 609 } 610 611 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, 612 Constant *C2) { 613 assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected"); 614 615 // Simplify BinOps with their identity values first. They are no-ops and we 616 // can always return the other value, including undef or poison values. 617 if (Constant *Identity = ConstantExpr::getBinOpIdentity( 618 Opcode, C1->getType(), /*AllowRHSIdentity*/ false)) { 619 if (C1 == Identity) 620 return C2; 621 if (C2 == Identity) 622 return C1; 623 } else if (Constant *Identity = ConstantExpr::getBinOpIdentity( 624 Opcode, C1->getType(), /*AllowRHSIdentity*/ true)) { 625 if (C2 == Identity) 626 return C1; 627 } 628 629 // Binary operations propagate poison. 630 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2)) 631 return PoisonValue::get(C1->getType()); 632 633 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length 634 // vectors are always evaluated per element. 635 bool IsScalableVector = isa<ScalableVectorType>(C1->getType()); 636 bool HasScalarUndefOrScalableVectorUndef = 637 (!C1->getType()->isVectorTy() || IsScalableVector) && 638 (isa<UndefValue>(C1) || isa<UndefValue>(C2)); 639 if (HasScalarUndefOrScalableVectorUndef) { 640 switch (static_cast<Instruction::BinaryOps>(Opcode)) { 641 case Instruction::Xor: 642 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) 643 // Handle undef ^ undef -> 0 special case. This is a common 644 // idiom (misuse). 645 return Constant::getNullValue(C1->getType()); 646 [[fallthrough]]; 647 case Instruction::Add: 648 case Instruction::Sub: 649 return UndefValue::get(C1->getType()); 650 case Instruction::And: 651 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef 652 return C1; 653 return Constant::getNullValue(C1->getType()); // undef & X -> 0 654 case Instruction::Mul: { 655 // undef * undef -> undef 656 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) 657 return C1; 658 const APInt *CV; 659 // X * undef -> undef if X is odd 660 if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV))) 661 if ((*CV)[0]) 662 return UndefValue::get(C1->getType()); 663 664 // X * undef -> 0 otherwise 665 return Constant::getNullValue(C1->getType()); 666 } 667 case Instruction::SDiv: 668 case Instruction::UDiv: 669 // X / undef -> poison 670 // X / 0 -> poison 671 if (match(C2, m_CombineOr(m_Undef(), m_Zero()))) 672 return PoisonValue::get(C2->getType()); 673 // undef / X -> 0 otherwise 674 return Constant::getNullValue(C1->getType()); 675 case Instruction::URem: 676 case Instruction::SRem: 677 // X % undef -> poison 678 // X % 0 -> poison 679 if (match(C2, m_CombineOr(m_Undef(), m_Zero()))) 680 return PoisonValue::get(C2->getType()); 681 // undef % X -> 0 otherwise 682 return Constant::getNullValue(C1->getType()); 683 case Instruction::Or: // X | undef -> -1 684 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef 685 return C1; 686 return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 687 case Instruction::LShr: 688 // X >>l undef -> poison 689 if (isa<UndefValue>(C2)) 690 return PoisonValue::get(C2->getType()); 691 // undef >>l X -> 0 692 return Constant::getNullValue(C1->getType()); 693 case Instruction::AShr: 694 // X >>a undef -> poison 695 if (isa<UndefValue>(C2)) 696 return PoisonValue::get(C2->getType()); 697 // TODO: undef >>a X -> poison if the shift is exact 698 // undef >>a X -> 0 699 return Constant::getNullValue(C1->getType()); 700 case Instruction::Shl: 701 // X << undef -> undef 702 if (isa<UndefValue>(C2)) 703 return PoisonValue::get(C2->getType()); 704 // undef << X -> 0 705 return Constant::getNullValue(C1->getType()); 706 case Instruction::FSub: 707 // -0.0 - undef --> undef (consistent with "fneg undef") 708 if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2)) 709 return C2; 710 [[fallthrough]]; 711 case Instruction::FAdd: 712 case Instruction::FMul: 713 case Instruction::FDiv: 714 case Instruction::FRem: 715 // [any flop] undef, undef -> undef 716 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) 717 return C1; 718 // [any flop] C, undef -> NaN 719 // [any flop] undef, C -> NaN 720 // We could potentially specialize NaN/Inf constants vs. 'normal' 721 // constants (possibly differently depending on opcode and operand). This 722 // would allow returning undef sometimes. But it is always safe to fold to 723 // NaN because we can choose the undef operand as NaN, and any FP opcode 724 // with a NaN operand will propagate NaN. 725 return ConstantFP::getNaN(C1->getType()); 726 case Instruction::BinaryOpsEnd: 727 llvm_unreachable("Invalid BinaryOp"); 728 } 729 } 730 731 // Neither constant should be UndefValue, unless these are vector constants. 732 assert((!HasScalarUndefOrScalableVectorUndef) && "Unexpected UndefValue"); 733 734 // Handle simplifications when the RHS is a constant int. 735 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { 736 if (C2 == ConstantExpr::getBinOpAbsorber(Opcode, C2->getType(), 737 /*AllowLHSConstant*/ false)) 738 return C2; 739 740 switch (Opcode) { 741 case Instruction::UDiv: 742 case Instruction::SDiv: 743 if (CI2->isZero()) 744 return PoisonValue::get(CI2->getType()); // X / 0 == poison 745 break; 746 case Instruction::URem: 747 case Instruction::SRem: 748 if (CI2->isOne()) 749 return Constant::getNullValue(CI2->getType()); // X % 1 == 0 750 if (CI2->isZero()) 751 return PoisonValue::get(CI2->getType()); // X % 0 == poison 752 break; 753 case Instruction::And: 754 assert(!CI2->isZero() && "And zero handled above"); 755 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 756 // If and'ing the address of a global with a constant, fold it. 757 if (CE1->getOpcode() == Instruction::PtrToInt && 758 isa<GlobalValue>(CE1->getOperand(0))) { 759 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0)); 760 761 Align GVAlign; // defaults to 1 762 763 if (Module *TheModule = GV->getParent()) { 764 const DataLayout &DL = TheModule->getDataLayout(); 765 GVAlign = GV->getPointerAlignment(DL); 766 767 // If the function alignment is not specified then assume that it 768 // is 4. 769 // This is dangerous; on x86, the alignment of the pointer 770 // corresponds to the alignment of the function, but might be less 771 // than 4 if it isn't explicitly specified. 772 // However, a fix for this behaviour was reverted because it 773 // increased code size (see https://reviews.llvm.org/D55115) 774 // FIXME: This code should be deleted once existing targets have 775 // appropriate defaults 776 if (isa<Function>(GV) && !DL.getFunctionPtrAlign()) 777 GVAlign = Align(4); 778 } else if (isa<GlobalVariable>(GV)) { 779 GVAlign = cast<GlobalVariable>(GV)->getAlign().valueOrOne(); 780 } 781 782 if (GVAlign > 1) { 783 unsigned DstWidth = CI2->getBitWidth(); 784 unsigned SrcWidth = std::min(DstWidth, Log2(GVAlign)); 785 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth)); 786 787 // If checking bits we know are clear, return zero. 788 if ((CI2->getValue() & BitsNotSet) == CI2->getValue()) 789 return Constant::getNullValue(CI2->getType()); 790 } 791 } 792 } 793 break; 794 } 795 } else if (isa<ConstantInt>(C1)) { 796 // If C1 is a ConstantInt and C2 is not, swap the operands. 797 if (Instruction::isCommutative(Opcode)) 798 return ConstantExpr::isDesirableBinOp(Opcode) 799 ? ConstantExpr::get(Opcode, C2, C1) 800 : ConstantFoldBinaryInstruction(Opcode, C2, C1); 801 } 802 803 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) { 804 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { 805 const APInt &C1V = CI1->getValue(); 806 const APInt &C2V = CI2->getValue(); 807 switch (Opcode) { 808 default: 809 break; 810 case Instruction::Add: 811 return ConstantInt::get(CI1->getContext(), C1V + C2V); 812 case Instruction::Sub: 813 return ConstantInt::get(CI1->getContext(), C1V - C2V); 814 case Instruction::Mul: 815 return ConstantInt::get(CI1->getContext(), C1V * C2V); 816 case Instruction::UDiv: 817 assert(!CI2->isZero() && "Div by zero handled above"); 818 return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V)); 819 case Instruction::SDiv: 820 assert(!CI2->isZero() && "Div by zero handled above"); 821 if (C2V.isAllOnes() && C1V.isMinSignedValue()) 822 return PoisonValue::get(CI1->getType()); // MIN_INT / -1 -> poison 823 return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V)); 824 case Instruction::URem: 825 assert(!CI2->isZero() && "Div by zero handled above"); 826 return ConstantInt::get(CI1->getContext(), C1V.urem(C2V)); 827 case Instruction::SRem: 828 assert(!CI2->isZero() && "Div by zero handled above"); 829 if (C2V.isAllOnes() && C1V.isMinSignedValue()) 830 return PoisonValue::get(CI1->getType()); // MIN_INT % -1 -> poison 831 return ConstantInt::get(CI1->getContext(), C1V.srem(C2V)); 832 case Instruction::And: 833 return ConstantInt::get(CI1->getContext(), C1V & C2V); 834 case Instruction::Or: 835 return ConstantInt::get(CI1->getContext(), C1V | C2V); 836 case Instruction::Xor: 837 return ConstantInt::get(CI1->getContext(), C1V ^ C2V); 838 case Instruction::Shl: 839 if (C2V.ult(C1V.getBitWidth())) 840 return ConstantInt::get(CI1->getContext(), C1V.shl(C2V)); 841 return PoisonValue::get(C1->getType()); // too big shift is poison 842 case Instruction::LShr: 843 if (C2V.ult(C1V.getBitWidth())) 844 return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V)); 845 return PoisonValue::get(C1->getType()); // too big shift is poison 846 case Instruction::AShr: 847 if (C2V.ult(C1V.getBitWidth())) 848 return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V)); 849 return PoisonValue::get(C1->getType()); // too big shift is poison 850 } 851 } 852 853 if (C1 == ConstantExpr::getBinOpAbsorber(Opcode, C1->getType(), 854 /*AllowLHSConstant*/ true)) 855 return C1; 856 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) { 857 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) { 858 const APFloat &C1V = CFP1->getValueAPF(); 859 const APFloat &C2V = CFP2->getValueAPF(); 860 APFloat C3V = C1V; // copy for modification 861 switch (Opcode) { 862 default: 863 break; 864 case Instruction::FAdd: 865 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven); 866 return ConstantFP::get(C1->getContext(), C3V); 867 case Instruction::FSub: 868 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven); 869 return ConstantFP::get(C1->getContext(), C3V); 870 case Instruction::FMul: 871 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven); 872 return ConstantFP::get(C1->getContext(), C3V); 873 case Instruction::FDiv: 874 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven); 875 return ConstantFP::get(C1->getContext(), C3V); 876 case Instruction::FRem: 877 (void)C3V.mod(C2V); 878 return ConstantFP::get(C1->getContext(), C3V); 879 } 880 } 881 } else if (auto *VTy = dyn_cast<VectorType>(C1->getType())) { 882 // Fast path for splatted constants. 883 if (Constant *C2Splat = C2->getSplatValue()) { 884 if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue()) 885 return PoisonValue::get(VTy); 886 if (Constant *C1Splat = C1->getSplatValue()) { 887 Constant *Res = 888 ConstantExpr::isDesirableBinOp(Opcode) 889 ? ConstantExpr::get(Opcode, C1Splat, C2Splat) 890 : ConstantFoldBinaryInstruction(Opcode, C1Splat, C2Splat); 891 if (!Res) 892 return nullptr; 893 return ConstantVector::getSplat(VTy->getElementCount(), Res); 894 } 895 } 896 897 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) { 898 // Fold each element and create a vector constant from those constants. 899 SmallVector<Constant*, 16> Result; 900 Type *Ty = IntegerType::get(FVTy->getContext(), 32); 901 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) { 902 Constant *ExtractIdx = ConstantInt::get(Ty, i); 903 Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx); 904 Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx); 905 906 // If any element of a divisor vector is zero, the whole op is poison. 907 if (Instruction::isIntDivRem(Opcode) && RHS->isNullValue()) 908 return PoisonValue::get(VTy); 909 910 Constant *Res = ConstantExpr::isDesirableBinOp(Opcode) 911 ? ConstantExpr::get(Opcode, LHS, RHS) 912 : ConstantFoldBinaryInstruction(Opcode, LHS, RHS); 913 if (!Res) 914 return nullptr; 915 Result.push_back(Res); 916 } 917 918 return ConstantVector::get(Result); 919 } 920 } 921 922 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 923 // There are many possible foldings we could do here. We should probably 924 // at least fold add of a pointer with an integer into the appropriate 925 // getelementptr. This will improve alias analysis a bit. 926 927 // Given ((a + b) + c), if (b + c) folds to something interesting, return 928 // (a + (b + c)). 929 if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) { 930 Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2); 931 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode) 932 return ConstantExpr::get(Opcode, CE1->getOperand(0), T); 933 } 934 } else if (isa<ConstantExpr>(C2)) { 935 // If C2 is a constant expr and C1 isn't, flop them around and fold the 936 // other way if possible. 937 if (Instruction::isCommutative(Opcode)) 938 return ConstantFoldBinaryInstruction(Opcode, C2, C1); 939 } 940 941 // i1 can be simplified in many cases. 942 if (C1->getType()->isIntegerTy(1)) { 943 switch (Opcode) { 944 case Instruction::Add: 945 case Instruction::Sub: 946 return ConstantExpr::getXor(C1, C2); 947 case Instruction::Shl: 948 case Instruction::LShr: 949 case Instruction::AShr: 950 // We can assume that C2 == 0. If it were one the result would be 951 // undefined because the shift value is as large as the bitwidth. 952 return C1; 953 case Instruction::SDiv: 954 case Instruction::UDiv: 955 // We can assume that C2 == 1. If it were zero the result would be 956 // undefined through division by zero. 957 return C1; 958 case Instruction::URem: 959 case Instruction::SRem: 960 // We can assume that C2 == 1. If it were zero the result would be 961 // undefined through division by zero. 962 return ConstantInt::getFalse(C1->getContext()); 963 default: 964 break; 965 } 966 } 967 968 // We don't know how to fold this. 969 return nullptr; 970 } 971 972 static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, 973 const GlobalValue *GV2) { 974 auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) { 975 if (GV->isInterposable() || GV->hasGlobalUnnamedAddr()) 976 return true; 977 if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) { 978 Type *Ty = GVar->getValueType(); 979 // A global with opaque type might end up being zero sized. 980 if (!Ty->isSized()) 981 return true; 982 // A global with an empty type might lie at the address of any other 983 // global. 984 if (Ty->isEmptyTy()) 985 return true; 986 } 987 return false; 988 }; 989 // Don't try to decide equality of aliases. 990 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2)) 991 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2)) 992 return ICmpInst::ICMP_NE; 993 return ICmpInst::BAD_ICMP_PREDICATE; 994 } 995 996 /// This function determines if there is anything we can decide about the two 997 /// constants provided. This doesn't need to handle simple things like integer 998 /// comparisons, but should instead handle ConstantExprs and GlobalValues. 999 /// If we can determine that the two constants have a particular relation to 1000 /// each other, we should return the corresponding ICmp predicate, otherwise 1001 /// return ICmpInst::BAD_ICMP_PREDICATE. 1002 static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) { 1003 assert(V1->getType() == V2->getType() && 1004 "Cannot compare different types of values!"); 1005 if (V1 == V2) return ICmpInst::ICMP_EQ; 1006 1007 // The following folds only apply to pointers. 1008 if (!V1->getType()->isPointerTy()) 1009 return ICmpInst::BAD_ICMP_PREDICATE; 1010 1011 // To simplify this code we canonicalize the relation so that the first 1012 // operand is always the most "complex" of the two. We consider simple 1013 // constants (like ConstantPointerNull) to be the simplest, followed by 1014 // BlockAddress, GlobalValues, and ConstantExpr's (the most complex). 1015 auto GetComplexity = [](Constant *V) { 1016 if (isa<ConstantExpr>(V)) 1017 return 3; 1018 if (isa<GlobalValue>(V)) 1019 return 2; 1020 if (isa<BlockAddress>(V)) 1021 return 1; 1022 return 0; 1023 }; 1024 if (GetComplexity(V1) < GetComplexity(V2)) { 1025 ICmpInst::Predicate SwappedRelation = evaluateICmpRelation(V2, V1); 1026 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) 1027 return ICmpInst::getSwappedPredicate(SwappedRelation); 1028 return ICmpInst::BAD_ICMP_PREDICATE; 1029 } 1030 1031 if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) { 1032 // Now we know that the RHS is a BlockAddress or simple constant. 1033 if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) { 1034 // Block address in another function can't equal this one, but block 1035 // addresses in the current function might be the same if blocks are 1036 // empty. 1037 if (BA2->getFunction() != BA->getFunction()) 1038 return ICmpInst::ICMP_NE; 1039 } else if (isa<ConstantPointerNull>(V2)) { 1040 return ICmpInst::ICMP_NE; 1041 } 1042 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) { 1043 // Now we know that the RHS is a GlobalValue, BlockAddress or simple 1044 // constant. 1045 if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) { 1046 return areGlobalsPotentiallyEqual(GV, GV2); 1047 } else if (isa<BlockAddress>(V2)) { 1048 return ICmpInst::ICMP_NE; // Globals never equal labels. 1049 } else if (isa<ConstantPointerNull>(V2)) { 1050 // GlobalVals can never be null unless they have external weak linkage. 1051 // We don't try to evaluate aliases here. 1052 // NOTE: We should not be doing this constant folding if null pointer 1053 // is considered valid for the function. But currently there is no way to 1054 // query it from the Constant type. 1055 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) && 1056 !NullPointerIsDefined(nullptr /* F */, 1057 GV->getType()->getAddressSpace())) 1058 return ICmpInst::ICMP_UGT; 1059 } 1060 } else if (auto *CE1 = dyn_cast<ConstantExpr>(V1)) { 1061 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a 1062 // constantexpr, a global, block address, or a simple constant. 1063 Constant *CE1Op0 = CE1->getOperand(0); 1064 1065 switch (CE1->getOpcode()) { 1066 case Instruction::GetElementPtr: { 1067 GEPOperator *CE1GEP = cast<GEPOperator>(CE1); 1068 // Ok, since this is a getelementptr, we know that the constant has a 1069 // pointer type. Check the various cases. 1070 if (isa<ConstantPointerNull>(V2)) { 1071 // If we are comparing a GEP to a null pointer, check to see if the base 1072 // of the GEP equals the null pointer. 1073 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { 1074 // If its not weak linkage, the GVal must have a non-zero address 1075 // so the result is greater-than 1076 if (!GV->hasExternalWeakLinkage() && CE1GEP->isInBounds()) 1077 return ICmpInst::ICMP_UGT; 1078 } 1079 } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) { 1080 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { 1081 if (GV != GV2) { 1082 if (CE1GEP->hasAllZeroIndices()) 1083 return areGlobalsPotentiallyEqual(GV, GV2); 1084 return ICmpInst::BAD_ICMP_PREDICATE; 1085 } 1086 } 1087 } else if (const auto *CE2GEP = dyn_cast<GEPOperator>(V2)) { 1088 // By far the most common case to handle is when the base pointers are 1089 // obviously to the same global. 1090 const Constant *CE2Op0 = cast<Constant>(CE2GEP->getPointerOperand()); 1091 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { 1092 // Don't know relative ordering, but check for inequality. 1093 if (CE1Op0 != CE2Op0) { 1094 if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices()) 1095 return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0), 1096 cast<GlobalValue>(CE2Op0)); 1097 return ICmpInst::BAD_ICMP_PREDICATE; 1098 } 1099 } 1100 } 1101 break; 1102 } 1103 default: 1104 break; 1105 } 1106 } 1107 1108 return ICmpInst::BAD_ICMP_PREDICATE; 1109 } 1110 1111 Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, 1112 Constant *C1, Constant *C2) { 1113 Type *ResultTy; 1114 if (VectorType *VT = dyn_cast<VectorType>(C1->getType())) 1115 ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()), 1116 VT->getElementCount()); 1117 else 1118 ResultTy = Type::getInt1Ty(C1->getContext()); 1119 1120 // Fold FCMP_FALSE/FCMP_TRUE unconditionally. 1121 if (Predicate == FCmpInst::FCMP_FALSE) 1122 return Constant::getNullValue(ResultTy); 1123 1124 if (Predicate == FCmpInst::FCMP_TRUE) 1125 return Constant::getAllOnesValue(ResultTy); 1126 1127 // Handle some degenerate cases first 1128 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2)) 1129 return PoisonValue::get(ResultTy); 1130 1131 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) { 1132 bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate); 1133 // For EQ and NE, we can always pick a value for the undef to make the 1134 // predicate pass or fail, so we can return undef. 1135 // Also, if both operands are undef, we can return undef for int comparison. 1136 if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2)) 1137 return UndefValue::get(ResultTy); 1138 1139 // Otherwise, for integer compare, pick the same value as the non-undef 1140 // operand, and fold it to true or false. 1141 if (isIntegerPredicate) 1142 return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate)); 1143 1144 // Choosing NaN for the undef will always make unordered comparison succeed 1145 // and ordered comparison fails. 1146 return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate)); 1147 } 1148 1149 if (C2->isNullValue()) { 1150 // The caller is expected to commute the operands if the constant expression 1151 // is C2. 1152 // C1 >= 0 --> true 1153 if (Predicate == ICmpInst::ICMP_UGE) 1154 return Constant::getAllOnesValue(ResultTy); 1155 // C1 < 0 --> false 1156 if (Predicate == ICmpInst::ICMP_ULT) 1157 return Constant::getNullValue(ResultTy); 1158 } 1159 1160 // If the comparison is a comparison between two i1's, simplify it. 1161 if (C1->getType()->isIntegerTy(1)) { 1162 switch (Predicate) { 1163 case ICmpInst::ICMP_EQ: 1164 if (isa<ConstantInt>(C2)) 1165 return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2)); 1166 return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2); 1167 case ICmpInst::ICMP_NE: 1168 return ConstantExpr::getXor(C1, C2); 1169 default: 1170 break; 1171 } 1172 } 1173 1174 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) { 1175 const APInt &V1 = cast<ConstantInt>(C1)->getValue(); 1176 const APInt &V2 = cast<ConstantInt>(C2)->getValue(); 1177 return ConstantInt::get(ResultTy, ICmpInst::compare(V1, V2, Predicate)); 1178 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) { 1179 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF(); 1180 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF(); 1181 return ConstantInt::get(ResultTy, FCmpInst::compare(C1V, C2V, Predicate)); 1182 } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) { 1183 1184 // Fast path for splatted constants. 1185 if (Constant *C1Splat = C1->getSplatValue()) 1186 if (Constant *C2Splat = C2->getSplatValue()) 1187 if (Constant *Elt = 1188 ConstantFoldCompareInstruction(Predicate, C1Splat, C2Splat)) 1189 return ConstantVector::getSplat(C1VTy->getElementCount(), Elt); 1190 1191 // Do not iterate on scalable vector. The number of elements is unknown at 1192 // compile-time. 1193 if (isa<ScalableVectorType>(C1VTy)) 1194 return nullptr; 1195 1196 // If we can constant fold the comparison of each element, constant fold 1197 // the whole vector comparison. 1198 SmallVector<Constant*, 4> ResElts; 1199 Type *Ty = IntegerType::get(C1->getContext(), 32); 1200 // Compare the elements, producing an i1 result or constant expr. 1201 for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue(); 1202 I != E; ++I) { 1203 Constant *C1E = 1204 ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, I)); 1205 Constant *C2E = 1206 ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, I)); 1207 Constant *Elt = ConstantFoldCompareInstruction(Predicate, C1E, C2E); 1208 if (!Elt) 1209 return nullptr; 1210 1211 ResElts.push_back(Elt); 1212 } 1213 1214 return ConstantVector::get(ResElts); 1215 } 1216 1217 if (C1->getType()->isFPOrFPVectorTy()) { 1218 if (C1 == C2) { 1219 // We know that C1 == C2 || isUnordered(C1, C2). 1220 if (Predicate == FCmpInst::FCMP_ONE) 1221 return ConstantInt::getFalse(ResultTy); 1222 else if (Predicate == FCmpInst::FCMP_UEQ) 1223 return ConstantInt::getTrue(ResultTy); 1224 } 1225 } else { 1226 // Evaluate the relation between the two constants, per the predicate. 1227 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. 1228 switch (evaluateICmpRelation(C1, C2)) { 1229 default: llvm_unreachable("Unknown relational!"); 1230 case ICmpInst::BAD_ICMP_PREDICATE: 1231 break; // Couldn't determine anything about these constants. 1232 case ICmpInst::ICMP_EQ: // We know the constants are equal! 1233 // If we know the constants are equal, we can decide the result of this 1234 // computation precisely. 1235 Result = ICmpInst::isTrueWhenEqual(Predicate); 1236 break; 1237 case ICmpInst::ICMP_ULT: 1238 switch (Predicate) { 1239 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE: 1240 Result = 1; break; 1241 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE: 1242 Result = 0; break; 1243 default: 1244 break; 1245 } 1246 break; 1247 case ICmpInst::ICMP_SLT: 1248 switch (Predicate) { 1249 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE: 1250 Result = 1; break; 1251 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE: 1252 Result = 0; break; 1253 default: 1254 break; 1255 } 1256 break; 1257 case ICmpInst::ICMP_UGT: 1258 switch (Predicate) { 1259 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE: 1260 Result = 1; break; 1261 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE: 1262 Result = 0; break; 1263 default: 1264 break; 1265 } 1266 break; 1267 case ICmpInst::ICMP_SGT: 1268 switch (Predicate) { 1269 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE: 1270 Result = 1; break; 1271 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE: 1272 Result = 0; break; 1273 default: 1274 break; 1275 } 1276 break; 1277 case ICmpInst::ICMP_ULE: 1278 if (Predicate == ICmpInst::ICMP_UGT) 1279 Result = 0; 1280 if (Predicate == ICmpInst::ICMP_ULT || Predicate == ICmpInst::ICMP_ULE) 1281 Result = 1; 1282 break; 1283 case ICmpInst::ICMP_SLE: 1284 if (Predicate == ICmpInst::ICMP_SGT) 1285 Result = 0; 1286 if (Predicate == ICmpInst::ICMP_SLT || Predicate == ICmpInst::ICMP_SLE) 1287 Result = 1; 1288 break; 1289 case ICmpInst::ICMP_UGE: 1290 if (Predicate == ICmpInst::ICMP_ULT) 1291 Result = 0; 1292 if (Predicate == ICmpInst::ICMP_UGT || Predicate == ICmpInst::ICMP_UGE) 1293 Result = 1; 1294 break; 1295 case ICmpInst::ICMP_SGE: 1296 if (Predicate == ICmpInst::ICMP_SLT) 1297 Result = 0; 1298 if (Predicate == ICmpInst::ICMP_SGT || Predicate == ICmpInst::ICMP_SGE) 1299 Result = 1; 1300 break; 1301 case ICmpInst::ICMP_NE: 1302 if (Predicate == ICmpInst::ICMP_EQ) 1303 Result = 0; 1304 if (Predicate == ICmpInst::ICMP_NE) 1305 Result = 1; 1306 break; 1307 } 1308 1309 // If we evaluated the result, return it now. 1310 if (Result != -1) 1311 return ConstantInt::get(ResultTy, Result); 1312 1313 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) || 1314 (C1->isNullValue() && !C2->isNullValue())) { 1315 // If C2 is a constant expr and C1 isn't, flip them around and fold the 1316 // other way if possible. 1317 // Also, if C1 is null and C2 isn't, flip them around. 1318 Predicate = ICmpInst::getSwappedPredicate(Predicate); 1319 return ConstantFoldCompareInstruction(Predicate, C2, C1); 1320 } 1321 } 1322 return nullptr; 1323 } 1324 1325 Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, 1326 std::optional<ConstantRange> InRange, 1327 ArrayRef<Value *> Idxs) { 1328 if (Idxs.empty()) return C; 1329 1330 Type *GEPTy = GetElementPtrInst::getGEPReturnType( 1331 C, ArrayRef((Value *const *)Idxs.data(), Idxs.size())); 1332 1333 if (isa<PoisonValue>(C)) 1334 return PoisonValue::get(GEPTy); 1335 1336 if (isa<UndefValue>(C)) 1337 return UndefValue::get(GEPTy); 1338 1339 auto IsNoOp = [&]() { 1340 // Avoid losing inrange information. 1341 if (InRange) 1342 return false; 1343 1344 return all_of(Idxs, [](Value *Idx) { 1345 Constant *IdxC = cast<Constant>(Idx); 1346 return IdxC->isNullValue() || isa<UndefValue>(IdxC); 1347 }); 1348 }; 1349 if (IsNoOp()) 1350 return GEPTy->isVectorTy() && !C->getType()->isVectorTy() 1351 ? ConstantVector::getSplat( 1352 cast<VectorType>(GEPTy)->getElementCount(), C) 1353 : C; 1354 1355 return nullptr; 1356 } 1357