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 "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 #include "llvm/Support/ManagedStatic.h" 34 #include "llvm/Support/MathExtras.h" 35 using namespace llvm; 36 using namespace llvm::PatternMatch; 37 38 //===----------------------------------------------------------------------===// 39 // ConstantFold*Instruction Implementations 40 //===----------------------------------------------------------------------===// 41 42 /// Convert the specified vector Constant node to the specified vector type. 43 /// At this point, we know that the elements of the input vector constant are 44 /// all simple integer or FP values. 45 static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) { 46 47 if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy); 48 if (CV->isNullValue()) return Constant::getNullValue(DstTy); 49 50 // Do not iterate on scalable vector. The num of elements is unknown at 51 // compile-time. 52 if (isa<ScalableVectorType>(DstTy)) 53 return nullptr; 54 55 // If this cast changes element count then we can't handle it here: 56 // doing so requires endianness information. This should be handled by 57 // Analysis/ConstantFolding.cpp 58 unsigned NumElts = cast<FixedVectorType>(DstTy)->getNumElements(); 59 if (NumElts != cast<FixedVectorType>(CV->getType())->getNumElements()) 60 return nullptr; 61 62 Type *DstEltTy = DstTy->getElementType(); 63 // Fast path for splatted constants. 64 if (Constant *Splat = CV->getSplatValue()) { 65 return ConstantVector::getSplat(DstTy->getElementCount(), 66 ConstantExpr::getBitCast(Splat, DstEltTy)); 67 } 68 69 SmallVector<Constant*, 16> Result; 70 Type *Ty = IntegerType::get(CV->getContext(), 32); 71 for (unsigned i = 0; i != NumElts; ++i) { 72 Constant *C = 73 ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i)); 74 C = ConstantExpr::getBitCast(C, DstEltTy); 75 Result.push_back(C); 76 } 77 78 return ConstantVector::get(Result); 79 } 80 81 /// This function determines which opcode to use to fold two constant cast 82 /// expressions together. It uses CastInst::isEliminableCastPair to determine 83 /// the opcode. Consequently its just a wrapper around that function. 84 /// Determine if it is valid to fold a cast of a cast 85 static unsigned 86 foldConstantCastPair( 87 unsigned opc, ///< opcode of the second cast constant expression 88 ConstantExpr *Op, ///< the first cast constant expression 89 Type *DstTy ///< destination type of the first cast 90 ) { 91 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!"); 92 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type"); 93 assert(CastInst::isCast(opc) && "Invalid cast opcode"); 94 95 // The types and opcodes for the two Cast constant expressions 96 Type *SrcTy = Op->getOperand(0)->getType(); 97 Type *MidTy = Op->getType(); 98 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode()); 99 Instruction::CastOps secondOp = Instruction::CastOps(opc); 100 101 // Assume that pointers are never more than 64 bits wide, and only use this 102 // for the middle type. Otherwise we could end up folding away illegal 103 // bitcasts between address spaces with different sizes. 104 IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext()); 105 106 // Let CastInst::isEliminableCastPair do the heavy lifting. 107 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, 108 nullptr, FakeIntPtrTy, nullptr); 109 } 110 111 static Constant *FoldBitCast(Constant *V, Type *DestTy) { 112 Type *SrcTy = V->getType(); 113 if (SrcTy == DestTy) 114 return V; // no-op cast 115 116 // Check to see if we are casting a pointer to an aggregate to a pointer to 117 // the first element. If so, return the appropriate GEP instruction. 118 if (PointerType *PTy = dyn_cast<PointerType>(V->getType())) 119 if (PointerType *DPTy = dyn_cast<PointerType>(DestTy)) 120 if (PTy->getAddressSpace() == DPTy->getAddressSpace() 121 && PTy->getElementType()->isSized()) { 122 SmallVector<Value*, 8> IdxList; 123 Value *Zero = 124 Constant::getNullValue(Type::getInt32Ty(DPTy->getContext())); 125 IdxList.push_back(Zero); 126 Type *ElTy = PTy->getElementType(); 127 while (ElTy && ElTy != DPTy->getElementType()) { 128 ElTy = GetElementPtrInst::getTypeAtIndex(ElTy, (uint64_t)0); 129 IdxList.push_back(Zero); 130 } 131 132 if (ElTy == DPTy->getElementType()) 133 // This GEP is inbounds because all indices are zero. 134 return ConstantExpr::getInBoundsGetElementPtr(PTy->getElementType(), 135 V, IdxList); 136 } 137 138 // Handle casts from one vector constant to another. We know that the src 139 // and dest type have the same size (otherwise its an illegal cast). 140 if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { 141 if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) { 142 assert(DestPTy->getPrimitiveSizeInBits() == 143 SrcTy->getPrimitiveSizeInBits() && 144 "Not cast between same sized vectors!"); 145 SrcTy = nullptr; 146 // First, check for null. Undef is already handled. 147 if (isa<ConstantAggregateZero>(V)) 148 return Constant::getNullValue(DestTy); 149 150 // Handle ConstantVector and ConstantAggregateVector. 151 return BitCastConstantVector(V, DestPTy); 152 } 153 154 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts 155 // This allows for other simplifications (although some of them 156 // can only be handled by Analysis/ConstantFolding.cpp). 157 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) 158 return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy); 159 } 160 161 // Finally, implement bitcast folding now. The code below doesn't handle 162 // bitcast right. 163 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast. 164 return ConstantPointerNull::get(cast<PointerType>(DestTy)); 165 166 // Handle integral constant input. 167 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 168 if (DestTy->isIntegerTy()) 169 // Integral -> Integral. This is a no-op because the bit widths must 170 // be the same. Consequently, we just fold to V. 171 return V; 172 173 // See note below regarding the PPC_FP128 restriction. 174 if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty()) 175 return ConstantFP::get(DestTy->getContext(), 176 APFloat(DestTy->getFltSemantics(), 177 CI->getValue())); 178 179 // Otherwise, can't fold this (vector?) 180 return nullptr; 181 } 182 183 // Handle ConstantFP input: FP -> Integral. 184 if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) { 185 // PPC_FP128 is really the sum of two consecutive doubles, where the first 186 // double is always stored first in memory, regardless of the target 187 // endianness. The memory layout of i128, however, depends on the target 188 // endianness, and so we can't fold this without target endianness 189 // information. This should instead be handled by 190 // Analysis/ConstantFolding.cpp 191 if (FP->getType()->isPPC_FP128Ty()) 192 return nullptr; 193 194 // Make sure dest type is compatible with the folded integer constant. 195 if (!DestTy->isIntegerTy()) 196 return nullptr; 197 198 return ConstantInt::get(FP->getContext(), 199 FP->getValueAPF().bitcastToAPInt()); 200 } 201 202 return nullptr; 203 } 204 205 206 /// V is an integer constant which only has a subset of its bytes used. 207 /// The bytes used are indicated by ByteStart (which is the first byte used, 208 /// counting from the least significant byte) and ByteSize, which is the number 209 /// of bytes used. 210 /// 211 /// This function analyzes the specified constant to see if the specified byte 212 /// range can be returned as a simplified constant. If so, the constant is 213 /// returned, otherwise null is returned. 214 static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, 215 unsigned ByteSize) { 216 assert(C->getType()->isIntegerTy() && 217 (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 && 218 "Non-byte sized integer input"); 219 unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8; 220 assert(ByteSize && "Must be accessing some piece"); 221 assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input"); 222 assert(ByteSize != CSize && "Should not extract everything"); 223 224 // Constant Integers are simple. 225 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { 226 APInt V = CI->getValue(); 227 if (ByteStart) 228 V.lshrInPlace(ByteStart*8); 229 V = V.trunc(ByteSize*8); 230 return ConstantInt::get(CI->getContext(), V); 231 } 232 233 // In the input is a constant expr, we might be able to recursively simplify. 234 // If not, we definitely can't do anything. 235 ConstantExpr *CE = dyn_cast<ConstantExpr>(C); 236 if (!CE) return nullptr; 237 238 switch (CE->getOpcode()) { 239 default: return nullptr; 240 case Instruction::Or: { 241 Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize); 242 if (!RHS) 243 return nullptr; 244 245 // X | -1 -> -1. 246 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) 247 if (RHSC->isMinusOne()) 248 return RHSC; 249 250 Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize); 251 if (!LHS) 252 return nullptr; 253 return ConstantExpr::getOr(LHS, RHS); 254 } 255 case Instruction::And: { 256 Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize); 257 if (!RHS) 258 return nullptr; 259 260 // X & 0 -> 0. 261 if (RHS->isNullValue()) 262 return RHS; 263 264 Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize); 265 if (!LHS) 266 return nullptr; 267 return ConstantExpr::getAnd(LHS, RHS); 268 } 269 case Instruction::LShr: { 270 ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1)); 271 if (!Amt) 272 return nullptr; 273 APInt ShAmt = Amt->getValue(); 274 // Cannot analyze non-byte shifts. 275 if ((ShAmt & 7) != 0) 276 return nullptr; 277 ShAmt.lshrInPlace(3); 278 279 // If the extract is known to be all zeros, return zero. 280 if (ShAmt.uge(CSize - ByteStart)) 281 return Constant::getNullValue( 282 IntegerType::get(CE->getContext(), ByteSize * 8)); 283 // If the extract is known to be fully in the input, extract it. 284 if (ShAmt.ule(CSize - (ByteStart + ByteSize))) 285 return ExtractConstantBytes(CE->getOperand(0), 286 ByteStart + ShAmt.getZExtValue(), ByteSize); 287 288 // TODO: Handle the 'partially zero' case. 289 return nullptr; 290 } 291 292 case Instruction::Shl: { 293 ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1)); 294 if (!Amt) 295 return nullptr; 296 APInt ShAmt = Amt->getValue(); 297 // Cannot analyze non-byte shifts. 298 if ((ShAmt & 7) != 0) 299 return nullptr; 300 ShAmt.lshrInPlace(3); 301 302 // If the extract is known to be all zeros, return zero. 303 if (ShAmt.uge(ByteStart + ByteSize)) 304 return Constant::getNullValue( 305 IntegerType::get(CE->getContext(), ByteSize * 8)); 306 // If the extract is known to be fully in the input, extract it. 307 if (ShAmt.ule(ByteStart)) 308 return ExtractConstantBytes(CE->getOperand(0), 309 ByteStart - ShAmt.getZExtValue(), ByteSize); 310 311 // TODO: Handle the 'partially zero' case. 312 return nullptr; 313 } 314 315 case Instruction::ZExt: { 316 unsigned SrcBitSize = 317 cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth(); 318 319 // If extracting something that is completely zero, return 0. 320 if (ByteStart*8 >= SrcBitSize) 321 return Constant::getNullValue(IntegerType::get(CE->getContext(), 322 ByteSize*8)); 323 324 // If exactly extracting the input, return it. 325 if (ByteStart == 0 && ByteSize*8 == SrcBitSize) 326 return CE->getOperand(0); 327 328 // If extracting something completely in the input, if the input is a 329 // multiple of 8 bits, recurse. 330 if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize) 331 return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize); 332 333 // Otherwise, if extracting a subset of the input, which is not multiple of 334 // 8 bits, do a shift and trunc to get the bits. 335 if ((ByteStart+ByteSize)*8 < SrcBitSize) { 336 assert((SrcBitSize&7) && "Shouldn't get byte sized case here"); 337 Constant *Res = CE->getOperand(0); 338 if (ByteStart) 339 Res = ConstantExpr::getLShr(Res, 340 ConstantInt::get(Res->getType(), ByteStart*8)); 341 return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(), 342 ByteSize*8)); 343 } 344 345 // TODO: Handle the 'partially zero' case. 346 return nullptr; 347 } 348 } 349 } 350 351 /// Wrapper around getFoldedSizeOfImpl() that adds caching. 352 static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy, bool Folded, 353 DenseMap<Type *, Constant *> &Cache); 354 355 /// Return a ConstantExpr with type DestTy for sizeof on Ty, with any known 356 /// factors factored out. If Folded is false, return null if no factoring was 357 /// possible, to avoid endlessly bouncing an unfoldable expression back into the 358 /// top-level folder. 359 static Constant *getFoldedSizeOfImpl(Type *Ty, Type *DestTy, bool Folded, 360 DenseMap<Type *, Constant *> &Cache) { 361 // This is the actual implementation of getFoldedSizeOf(). To get the caching 362 // behavior, we need to call getFoldedSizeOf() when we recurse. 363 364 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 365 Constant *N = ConstantInt::get(DestTy, ATy->getNumElements()); 366 Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true, Cache); 367 return ConstantExpr::getNUWMul(E, N); 368 } 369 370 if (StructType *STy = dyn_cast<StructType>(Ty)) 371 if (!STy->isPacked()) { 372 unsigned NumElems = STy->getNumElements(); 373 // An empty struct has size zero. 374 if (NumElems == 0) 375 return ConstantExpr::getNullValue(DestTy); 376 // Check for a struct with all members having the same size. 377 Constant *MemberSize = 378 getFoldedSizeOf(STy->getElementType(0), DestTy, true, Cache); 379 bool AllSame = true; 380 for (unsigned i = 1; i != NumElems; ++i) 381 if (MemberSize != 382 getFoldedSizeOf(STy->getElementType(i), DestTy, true, Cache)) { 383 AllSame = false; 384 break; 385 } 386 if (AllSame) { 387 Constant *N = ConstantInt::get(DestTy, NumElems); 388 return ConstantExpr::getNUWMul(MemberSize, N); 389 } 390 } 391 392 // Pointer size doesn't depend on the pointee type, so canonicalize them 393 // to an arbitrary pointee. 394 if (PointerType *PTy = dyn_cast<PointerType>(Ty)) 395 if (!PTy->getElementType()->isIntegerTy(1)) 396 return getFoldedSizeOf( 397 PointerType::get(IntegerType::get(PTy->getContext(), 1), 398 PTy->getAddressSpace()), 399 DestTy, true, Cache); 400 401 // If there's no interesting folding happening, bail so that we don't create 402 // a constant that looks like it needs folding but really doesn't. 403 if (!Folded) 404 return nullptr; 405 406 // Base case: Get a regular sizeof expression. 407 Constant *C = ConstantExpr::getSizeOf(Ty); 408 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 409 DestTy, false), 410 C, DestTy); 411 return C; 412 } 413 414 static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy, bool Folded, 415 DenseMap<Type *, Constant *> &Cache) { 416 // Check for previously generated folded size constant. 417 auto It = Cache.find(Ty); 418 if (It != Cache.end()) 419 return It->second; 420 return Cache[Ty] = getFoldedSizeOfImpl(Ty, DestTy, Folded, Cache); 421 } 422 423 static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy, bool Folded) { 424 DenseMap<Type *, Constant *> Cache; 425 return getFoldedSizeOf(Ty, DestTy, Folded, Cache); 426 } 427 428 /// Return a ConstantExpr with type DestTy for alignof on Ty, with any known 429 /// factors factored out. If Folded is false, return null if no factoring was 430 /// possible, to avoid endlessly bouncing an unfoldable expression back into the 431 /// top-level folder. 432 static Constant *getFoldedAlignOf(Type *Ty, Type *DestTy, bool Folded) { 433 // The alignment of an array is equal to the alignment of the 434 // array element. Note that this is not always true for vectors. 435 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 436 Constant *C = ConstantExpr::getAlignOf(ATy->getElementType()); 437 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 438 DestTy, 439 false), 440 C, DestTy); 441 return C; 442 } 443 444 if (StructType *STy = dyn_cast<StructType>(Ty)) { 445 // Packed structs always have an alignment of 1. 446 if (STy->isPacked()) 447 return ConstantInt::get(DestTy, 1); 448 449 // Otherwise, struct alignment is the maximum alignment of any member. 450 // Without target data, we can't compare much, but we can check to see 451 // if all the members have the same alignment. 452 unsigned NumElems = STy->getNumElements(); 453 // An empty struct has minimal alignment. 454 if (NumElems == 0) 455 return ConstantInt::get(DestTy, 1); 456 // Check for a struct with all members having the same alignment. 457 Constant *MemberAlign = 458 getFoldedAlignOf(STy->getElementType(0), DestTy, true); 459 bool AllSame = true; 460 for (unsigned i = 1; i != NumElems; ++i) 461 if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) { 462 AllSame = false; 463 break; 464 } 465 if (AllSame) 466 return MemberAlign; 467 } 468 469 // Pointer alignment doesn't depend on the pointee type, so canonicalize them 470 // to an arbitrary pointee. 471 if (PointerType *PTy = dyn_cast<PointerType>(Ty)) 472 if (!PTy->getElementType()->isIntegerTy(1)) 473 return 474 getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(), 475 1), 476 PTy->getAddressSpace()), 477 DestTy, true); 478 479 // If there's no interesting folding happening, bail so that we don't create 480 // a constant that looks like it needs folding but really doesn't. 481 if (!Folded) 482 return nullptr; 483 484 // Base case: Get a regular alignof expression. 485 Constant *C = ConstantExpr::getAlignOf(Ty); 486 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 487 DestTy, false), 488 C, DestTy); 489 return C; 490 } 491 492 /// Return a ConstantExpr with type DestTy for offsetof on Ty and FieldNo, with 493 /// any known factors factored out. If Folded is false, return null if no 494 /// factoring was possible, to avoid endlessly bouncing an unfoldable expression 495 /// back into the top-level folder. 496 static Constant *getFoldedOffsetOf(Type *Ty, Constant *FieldNo, Type *DestTy, 497 bool Folded) { 498 if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 499 Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false, 500 DestTy, false), 501 FieldNo, DestTy); 502 Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true); 503 return ConstantExpr::getNUWMul(E, N); 504 } 505 506 if (StructType *STy = dyn_cast<StructType>(Ty)) 507 if (!STy->isPacked()) { 508 unsigned NumElems = STy->getNumElements(); 509 // An empty struct has no members. 510 if (NumElems == 0) 511 return nullptr; 512 // Check for a struct with all members having the same size. 513 Constant *MemberSize = 514 getFoldedSizeOf(STy->getElementType(0), DestTy, true); 515 bool AllSame = true; 516 for (unsigned i = 1; i != NumElems; ++i) 517 if (MemberSize != 518 getFoldedSizeOf(STy->getElementType(i), DestTy, true)) { 519 AllSame = false; 520 break; 521 } 522 if (AllSame) { 523 Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, 524 false, 525 DestTy, 526 false), 527 FieldNo, DestTy); 528 return ConstantExpr::getNUWMul(MemberSize, N); 529 } 530 } 531 532 // If there's no interesting folding happening, bail so that we don't create 533 // a constant that looks like it needs folding but really doesn't. 534 if (!Folded) 535 return nullptr; 536 537 // Base case: Get a regular offsetof expression. 538 Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo); 539 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 540 DestTy, false), 541 C, DestTy); 542 return C; 543 } 544 545 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, 546 Type *DestTy) { 547 if (isa<PoisonValue>(V)) 548 return PoisonValue::get(DestTy); 549 550 if (isa<UndefValue>(V)) { 551 // zext(undef) = 0, because the top bits will be zero. 552 // sext(undef) = 0, because the top bits will all be the same. 553 // [us]itofp(undef) = 0, because the result value is bounded. 554 if (opc == Instruction::ZExt || opc == Instruction::SExt || 555 opc == Instruction::UIToFP || opc == Instruction::SIToFP) 556 return Constant::getNullValue(DestTy); 557 return UndefValue::get(DestTy); 558 } 559 560 if (V->isNullValue() && !DestTy->isX86_MMXTy() && !DestTy->isX86_AMXTy() && 561 opc != Instruction::AddrSpaceCast) 562 return Constant::getNullValue(DestTy); 563 564 // If the cast operand is a constant expression, there's a few things we can 565 // do to try to simplify it. 566 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 567 if (CE->isCast()) { 568 // Try hard to fold cast of cast because they are often eliminable. 569 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) 570 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy); 571 } else if (CE->getOpcode() == Instruction::GetElementPtr && 572 // Do not fold addrspacecast (gep 0, .., 0). It might make the 573 // addrspacecast uncanonicalized. 574 opc != Instruction::AddrSpaceCast && 575 // Do not fold bitcast (gep) with inrange index, as this loses 576 // information. 577 !cast<GEPOperator>(CE)->getInRangeIndex().hasValue() && 578 // Do not fold if the gep type is a vector, as bitcasting 579 // operand 0 of a vector gep will result in a bitcast between 580 // different sizes. 581 !CE->getType()->isVectorTy()) { 582 // If all of the indexes in the GEP are null values, there is no pointer 583 // adjustment going on. We might as well cast the source pointer. 584 bool isAllNull = true; 585 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) 586 if (!CE->getOperand(i)->isNullValue()) { 587 isAllNull = false; 588 break; 589 } 590 if (isAllNull) 591 // This is casting one pointer type to another, always BitCast 592 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy); 593 } 594 } 595 596 // If the cast operand is a constant vector, perform the cast by 597 // operating on each element. In the cast of bitcasts, the element 598 // count may be mismatched; don't attempt to handle that here. 599 if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) && 600 DestTy->isVectorTy() && 601 cast<FixedVectorType>(DestTy)->getNumElements() == 602 cast<FixedVectorType>(V->getType())->getNumElements()) { 603 VectorType *DestVecTy = cast<VectorType>(DestTy); 604 Type *DstEltTy = DestVecTy->getElementType(); 605 // Fast path for splatted constants. 606 if (Constant *Splat = V->getSplatValue()) { 607 return ConstantVector::getSplat( 608 cast<VectorType>(DestTy)->getElementCount(), 609 ConstantExpr::getCast(opc, Splat, DstEltTy)); 610 } 611 SmallVector<Constant *, 16> res; 612 Type *Ty = IntegerType::get(V->getContext(), 32); 613 for (unsigned i = 0, 614 e = cast<FixedVectorType>(V->getType())->getNumElements(); 615 i != e; ++i) { 616 Constant *C = 617 ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i)); 618 res.push_back(ConstantExpr::getCast(opc, C, DstEltTy)); 619 } 620 return ConstantVector::get(res); 621 } 622 623 // We actually have to do a cast now. Perform the cast according to the 624 // opcode specified. 625 switch (opc) { 626 default: 627 llvm_unreachable("Failed to cast constant expression"); 628 case Instruction::FPTrunc: 629 case Instruction::FPExt: 630 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { 631 bool ignored; 632 APFloat Val = FPC->getValueAPF(); 633 Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf() : 634 DestTy->isFloatTy() ? APFloat::IEEEsingle() : 635 DestTy->isDoubleTy() ? APFloat::IEEEdouble() : 636 DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended() : 637 DestTy->isFP128Ty() ? APFloat::IEEEquad() : 638 DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble() : 639 APFloat::Bogus(), 640 APFloat::rmNearestTiesToEven, &ignored); 641 return ConstantFP::get(V->getContext(), Val); 642 } 643 return nullptr; // Can't fold. 644 case Instruction::FPToUI: 645 case Instruction::FPToSI: 646 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { 647 const APFloat &V = FPC->getValueAPF(); 648 bool ignored; 649 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 650 APSInt IntVal(DestBitWidth, opc == Instruction::FPToUI); 651 if (APFloat::opInvalidOp == 652 V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) { 653 // Undefined behavior invoked - the destination type can't represent 654 // the input constant. 655 return PoisonValue::get(DestTy); 656 } 657 return ConstantInt::get(FPC->getContext(), IntVal); 658 } 659 return nullptr; // Can't fold. 660 case Instruction::IntToPtr: //always treated as unsigned 661 if (V->isNullValue()) // Is it an integral null value? 662 return ConstantPointerNull::get(cast<PointerType>(DestTy)); 663 return nullptr; // Other pointer types cannot be casted 664 case Instruction::PtrToInt: // always treated as unsigned 665 // Is it a null pointer value? 666 if (V->isNullValue()) 667 return ConstantInt::get(DestTy, 0); 668 // If this is a sizeof-like expression, pull out multiplications by 669 // known factors to expose them to subsequent folding. If it's an 670 // alignof-like expression, factor out known factors. 671 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 672 if (CE->getOpcode() == Instruction::GetElementPtr && 673 CE->getOperand(0)->isNullValue()) { 674 // FIXME: Looks like getFoldedSizeOf(), getFoldedOffsetOf() and 675 // getFoldedAlignOf() don't handle the case when DestTy is a vector of 676 // pointers yet. We end up in asserts in CastInst::getCastOpcode (see 677 // test/Analysis/ConstantFolding/cast-vector.ll). I've only seen this 678 // happen in one "real" C-code test case, so it does not seem to be an 679 // important optimization to handle vectors here. For now, simply bail 680 // out. 681 if (DestTy->isVectorTy()) 682 return nullptr; 683 GEPOperator *GEPO = cast<GEPOperator>(CE); 684 Type *Ty = GEPO->getSourceElementType(); 685 if (CE->getNumOperands() == 2) { 686 // Handle a sizeof-like expression. 687 Constant *Idx = CE->getOperand(1); 688 bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne(); 689 if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) { 690 Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true, 691 DestTy, false), 692 Idx, DestTy); 693 return ConstantExpr::getMul(C, Idx); 694 } 695 } else if (CE->getNumOperands() == 3 && 696 CE->getOperand(1)->isNullValue()) { 697 // Handle an alignof-like expression. 698 if (StructType *STy = dyn_cast<StructType>(Ty)) 699 if (!STy->isPacked()) { 700 ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2)); 701 if (CI->isOne() && 702 STy->getNumElements() == 2 && 703 STy->getElementType(0)->isIntegerTy(1)) { 704 return getFoldedAlignOf(STy->getElementType(1), DestTy, false); 705 } 706 } 707 // Handle an offsetof-like expression. 708 if (Ty->isStructTy() || Ty->isArrayTy()) { 709 if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2), 710 DestTy, false)) 711 return C; 712 } 713 } 714 } 715 // Other pointer types cannot be casted 716 return nullptr; 717 case Instruction::UIToFP: 718 case Instruction::SIToFP: 719 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 720 const APInt &api = CI->getValue(); 721 APFloat apf(DestTy->getFltSemantics(), 722 APInt::getNullValue(DestTy->getPrimitiveSizeInBits())); 723 apf.convertFromAPInt(api, opc==Instruction::SIToFP, 724 APFloat::rmNearestTiesToEven); 725 return ConstantFP::get(V->getContext(), apf); 726 } 727 return nullptr; 728 case Instruction::ZExt: 729 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 730 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 731 return ConstantInt::get(V->getContext(), 732 CI->getValue().zext(BitWidth)); 733 } 734 return nullptr; 735 case Instruction::SExt: 736 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 737 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 738 return ConstantInt::get(V->getContext(), 739 CI->getValue().sext(BitWidth)); 740 } 741 return nullptr; 742 case Instruction::Trunc: { 743 if (V->getType()->isVectorTy()) 744 return nullptr; 745 746 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 747 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 748 return ConstantInt::get(V->getContext(), 749 CI->getValue().trunc(DestBitWidth)); 750 } 751 752 // The input must be a constantexpr. See if we can simplify this based on 753 // the bytes we are demanding. Only do this if the source and dest are an 754 // even multiple of a byte. 755 if ((DestBitWidth & 7) == 0 && 756 (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0) 757 if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8)) 758 return Res; 759 760 return nullptr; 761 } 762 case Instruction::BitCast: 763 return FoldBitCast(V, DestTy); 764 case Instruction::AddrSpaceCast: 765 return nullptr; 766 } 767 } 768 769 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond, 770 Constant *V1, Constant *V2) { 771 // Check for i1 and vector true/false conditions. 772 if (Cond->isNullValue()) return V2; 773 if (Cond->isAllOnesValue()) return V1; 774 775 // If the condition is a vector constant, fold the result elementwise. 776 if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) { 777 auto *V1VTy = CondV->getType(); 778 SmallVector<Constant*, 16> Result; 779 Type *Ty = IntegerType::get(CondV->getContext(), 32); 780 for (unsigned i = 0, e = V1VTy->getNumElements(); i != e; ++i) { 781 Constant *V; 782 Constant *V1Element = ConstantExpr::getExtractElement(V1, 783 ConstantInt::get(Ty, i)); 784 Constant *V2Element = ConstantExpr::getExtractElement(V2, 785 ConstantInt::get(Ty, i)); 786 auto *Cond = cast<Constant>(CondV->getOperand(i)); 787 if (isa<PoisonValue>(Cond)) { 788 V = PoisonValue::get(V1Element->getType()); 789 } else if (V1Element == V2Element) { 790 V = V1Element; 791 } else if (isa<UndefValue>(Cond)) { 792 V = isa<UndefValue>(V1Element) ? V1Element : V2Element; 793 } else { 794 if (!isa<ConstantInt>(Cond)) break; 795 V = Cond->isNullValue() ? V2Element : V1Element; 796 } 797 Result.push_back(V); 798 } 799 800 // If we were able to build the vector, return it. 801 if (Result.size() == V1VTy->getNumElements()) 802 return ConstantVector::get(Result); 803 } 804 805 if (isa<PoisonValue>(Cond)) 806 return PoisonValue::get(V1->getType()); 807 808 if (isa<UndefValue>(Cond)) { 809 if (isa<UndefValue>(V1)) return V1; 810 return V2; 811 } 812 813 if (V1 == V2) return V1; 814 815 if (isa<PoisonValue>(V1)) 816 return V2; 817 if (isa<PoisonValue>(V2)) 818 return V1; 819 820 // If the true or false value is undef, we can fold to the other value as 821 // long as the other value isn't poison. 822 auto NotPoison = [](Constant *C) { 823 if (isa<PoisonValue>(C)) 824 return false; 825 826 // TODO: We can analyze ConstExpr by opcode to determine if there is any 827 // possibility of poison. 828 if (isa<ConstantExpr>(C)) 829 return false; 830 831 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) || 832 isa<ConstantPointerNull>(C) || isa<Function>(C)) 833 return true; 834 835 if (C->getType()->isVectorTy()) 836 return !C->containsPoisonElement() && !C->containsConstantExpression(); 837 838 // TODO: Recursively analyze aggregates or other constants. 839 return false; 840 }; 841 if (isa<UndefValue>(V1) && NotPoison(V2)) return V2; 842 if (isa<UndefValue>(V2) && NotPoison(V1)) return V1; 843 844 if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) { 845 if (TrueVal->getOpcode() == Instruction::Select) 846 if (TrueVal->getOperand(0) == Cond) 847 return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2); 848 } 849 if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) { 850 if (FalseVal->getOpcode() == Instruction::Select) 851 if (FalseVal->getOperand(0) == Cond) 852 return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2)); 853 } 854 855 return nullptr; 856 } 857 858 Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val, 859 Constant *Idx) { 860 auto *ValVTy = cast<VectorType>(Val->getType()); 861 862 // extractelt poison, C -> poison 863 // extractelt C, undef -> poison 864 if (isa<PoisonValue>(Val) || isa<UndefValue>(Idx)) 865 return PoisonValue::get(ValVTy->getElementType()); 866 867 // extractelt undef, C -> undef 868 if (isa<UndefValue>(Val)) 869 return UndefValue::get(ValVTy->getElementType()); 870 871 auto *CIdx = dyn_cast<ConstantInt>(Idx); 872 if (!CIdx) 873 return nullptr; 874 875 if (auto *ValFVTy = dyn_cast<FixedVectorType>(Val->getType())) { 876 // ee({w,x,y,z}, wrong_value) -> poison 877 if (CIdx->uge(ValFVTy->getNumElements())) 878 return PoisonValue::get(ValFVTy->getElementType()); 879 } 880 881 // ee (gep (ptr, idx0, ...), idx) -> gep (ee (ptr, idx), ee (idx0, idx), ...) 882 if (auto *CE = dyn_cast<ConstantExpr>(Val)) { 883 if (CE->getOpcode() == Instruction::GetElementPtr) { 884 SmallVector<Constant *, 8> Ops; 885 Ops.reserve(CE->getNumOperands()); 886 for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) { 887 Constant *Op = CE->getOperand(i); 888 if (Op->getType()->isVectorTy()) { 889 Constant *ScalarOp = ConstantExpr::getExtractElement(Op, Idx); 890 if (!ScalarOp) 891 return nullptr; 892 Ops.push_back(ScalarOp); 893 } else 894 Ops.push_back(Op); 895 } 896 return CE->getWithOperands(Ops, ValVTy->getElementType(), false, 897 Ops[0]->getType()->getPointerElementType()); 898 } else if (CE->getOpcode() == Instruction::InsertElement) { 899 if (const auto *IEIdx = dyn_cast<ConstantInt>(CE->getOperand(2))) { 900 if (APSInt::isSameValue(APSInt(IEIdx->getValue()), 901 APSInt(CIdx->getValue()))) { 902 return CE->getOperand(1); 903 } else { 904 return ConstantExpr::getExtractElement(CE->getOperand(0), CIdx); 905 } 906 } 907 } 908 } 909 910 // CAZ of type ScalableVectorType and n < CAZ->getMinNumElements() => 911 // extractelt CAZ, n -> 0 912 if (auto *ValSVTy = dyn_cast<ScalableVectorType>(Val->getType())) { 913 if (!CIdx->uge(ValSVTy->getMinNumElements())) { 914 if (auto *CAZ = dyn_cast<ConstantAggregateZero>(Val)) 915 return CAZ->getElementValue(CIdx->getZExtValue()); 916 } 917 return nullptr; 918 } 919 920 return Val->getAggregateElement(CIdx); 921 } 922 923 Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val, 924 Constant *Elt, 925 Constant *Idx) { 926 if (isa<UndefValue>(Idx)) 927 return PoisonValue::get(Val->getType()); 928 929 ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); 930 if (!CIdx) return nullptr; 931 932 // Do not iterate on scalable vector. The num of elements is unknown at 933 // compile-time. 934 if (isa<ScalableVectorType>(Val->getType())) 935 return nullptr; 936 937 auto *ValTy = cast<FixedVectorType>(Val->getType()); 938 939 unsigned NumElts = ValTy->getNumElements(); 940 if (CIdx->uge(NumElts)) 941 return PoisonValue::get(Val->getType()); 942 943 SmallVector<Constant*, 16> Result; 944 Result.reserve(NumElts); 945 auto *Ty = Type::getInt32Ty(Val->getContext()); 946 uint64_t IdxVal = CIdx->getZExtValue(); 947 for (unsigned i = 0; i != NumElts; ++i) { 948 if (i == IdxVal) { 949 Result.push_back(Elt); 950 continue; 951 } 952 953 Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i)); 954 Result.push_back(C); 955 } 956 957 return ConstantVector::get(Result); 958 } 959 960 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, Constant *V2, 961 ArrayRef<int> Mask) { 962 auto *V1VTy = cast<VectorType>(V1->getType()); 963 unsigned MaskNumElts = Mask.size(); 964 auto MaskEltCount = 965 ElementCount::get(MaskNumElts, isa<ScalableVectorType>(V1VTy)); 966 Type *EltTy = V1VTy->getElementType(); 967 968 // Undefined shuffle mask -> undefined value. 969 if (all_of(Mask, [](int Elt) { return Elt == UndefMaskElem; })) { 970 return UndefValue::get(FixedVectorType::get(EltTy, MaskNumElts)); 971 } 972 973 // If the mask is all zeros this is a splat, no need to go through all 974 // elements. 975 if (all_of(Mask, [](int Elt) { return Elt == 0; }) && 976 !MaskEltCount.isScalable()) { 977 Type *Ty = IntegerType::get(V1->getContext(), 32); 978 Constant *Elt = 979 ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, 0)); 980 return ConstantVector::getSplat(MaskEltCount, Elt); 981 } 982 // Do not iterate on scalable vector. The num of elements is unknown at 983 // compile-time. 984 if (isa<ScalableVectorType>(V1VTy)) 985 return nullptr; 986 987 unsigned SrcNumElts = V1VTy->getElementCount().getKnownMinValue(); 988 989 // Loop over the shuffle mask, evaluating each element. 990 SmallVector<Constant*, 32> Result; 991 for (unsigned i = 0; i != MaskNumElts; ++i) { 992 int Elt = Mask[i]; 993 if (Elt == -1) { 994 Result.push_back(UndefValue::get(EltTy)); 995 continue; 996 } 997 Constant *InElt; 998 if (unsigned(Elt) >= SrcNumElts*2) 999 InElt = UndefValue::get(EltTy); 1000 else if (unsigned(Elt) >= SrcNumElts) { 1001 Type *Ty = IntegerType::get(V2->getContext(), 32); 1002 InElt = 1003 ConstantExpr::getExtractElement(V2, 1004 ConstantInt::get(Ty, Elt - SrcNumElts)); 1005 } else { 1006 Type *Ty = IntegerType::get(V1->getContext(), 32); 1007 InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt)); 1008 } 1009 Result.push_back(InElt); 1010 } 1011 1012 return ConstantVector::get(Result); 1013 } 1014 1015 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg, 1016 ArrayRef<unsigned> Idxs) { 1017 // Base case: no indices, so return the entire value. 1018 if (Idxs.empty()) 1019 return Agg; 1020 1021 if (Constant *C = Agg->getAggregateElement(Idxs[0])) 1022 return ConstantFoldExtractValueInstruction(C, Idxs.slice(1)); 1023 1024 return nullptr; 1025 } 1026 1027 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, 1028 Constant *Val, 1029 ArrayRef<unsigned> Idxs) { 1030 // Base case: no indices, so replace the entire value. 1031 if (Idxs.empty()) 1032 return Val; 1033 1034 unsigned NumElts; 1035 if (StructType *ST = dyn_cast<StructType>(Agg->getType())) 1036 NumElts = ST->getNumElements(); 1037 else 1038 NumElts = cast<ArrayType>(Agg->getType())->getNumElements(); 1039 1040 SmallVector<Constant*, 32> Result; 1041 for (unsigned i = 0; i != NumElts; ++i) { 1042 Constant *C = Agg->getAggregateElement(i); 1043 if (!C) return nullptr; 1044 1045 if (Idxs[0] == i) 1046 C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1)); 1047 1048 Result.push_back(C); 1049 } 1050 1051 if (StructType *ST = dyn_cast<StructType>(Agg->getType())) 1052 return ConstantStruct::get(ST, Result); 1053 return ConstantArray::get(cast<ArrayType>(Agg->getType()), Result); 1054 } 1055 1056 Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) { 1057 assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected"); 1058 1059 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length 1060 // vectors are always evaluated per element. 1061 bool IsScalableVector = isa<ScalableVectorType>(C->getType()); 1062 bool HasScalarUndefOrScalableVectorUndef = 1063 (!C->getType()->isVectorTy() || IsScalableVector) && isa<UndefValue>(C); 1064 1065 if (HasScalarUndefOrScalableVectorUndef) { 1066 switch (static_cast<Instruction::UnaryOps>(Opcode)) { 1067 case Instruction::FNeg: 1068 return C; // -undef -> undef 1069 case Instruction::UnaryOpsEnd: 1070 llvm_unreachable("Invalid UnaryOp"); 1071 } 1072 } 1073 1074 // Constant should not be UndefValue, unless these are vector constants. 1075 assert(!HasScalarUndefOrScalableVectorUndef && "Unexpected UndefValue"); 1076 // We only have FP UnaryOps right now. 1077 assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp"); 1078 1079 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { 1080 const APFloat &CV = CFP->getValueAPF(); 1081 switch (Opcode) { 1082 default: 1083 break; 1084 case Instruction::FNeg: 1085 return ConstantFP::get(C->getContext(), neg(CV)); 1086 } 1087 } else if (auto *VTy = dyn_cast<FixedVectorType>(C->getType())) { 1088 1089 Type *Ty = IntegerType::get(VTy->getContext(), 32); 1090 // Fast path for splatted constants. 1091 if (Constant *Splat = C->getSplatValue()) { 1092 Constant *Elt = ConstantExpr::get(Opcode, Splat); 1093 return ConstantVector::getSplat(VTy->getElementCount(), Elt); 1094 } 1095 1096 // Fold each element and create a vector constant from those constants. 1097 SmallVector<Constant *, 16> Result; 1098 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { 1099 Constant *ExtractIdx = ConstantInt::get(Ty, i); 1100 Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx); 1101 1102 Result.push_back(ConstantExpr::get(Opcode, Elt)); 1103 } 1104 1105 return ConstantVector::get(Result); 1106 } 1107 1108 // We don't know how to fold this. 1109 return nullptr; 1110 } 1111 1112 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, 1113 Constant *C2) { 1114 assert(Instruction::isBinaryOp(Opcode) && "Non-binary instruction detected"); 1115 1116 // Simplify BinOps with their identity values first. They are no-ops and we 1117 // can always return the other value, including undef or poison values. 1118 // FIXME: remove unnecessary duplicated identity patterns below. 1119 // FIXME: Use AllowRHSConstant with getBinOpIdentity to handle additional ops, 1120 // like X << 0 = X. 1121 Constant *Identity = ConstantExpr::getBinOpIdentity(Opcode, C1->getType()); 1122 if (Identity) { 1123 if (C1 == Identity) 1124 return C2; 1125 if (C2 == Identity) 1126 return C1; 1127 } 1128 1129 // Binary operations propagate poison. 1130 // FIXME: Currently, or/and i1 poison aren't folded into poison because 1131 // it causes miscompilation when combined with another optimization in 1132 // InstCombine (select i1 -> and/or). The select fold is wrong, but 1133 // fixing it requires an effort, so temporarily disable this until it is 1134 // fixed. 1135 bool PoisonFold = !C1->getType()->isIntegerTy(1) || 1136 (Opcode != Instruction::Or && Opcode != Instruction::And); 1137 if (PoisonFold && (isa<PoisonValue>(C1) || isa<PoisonValue>(C2))) 1138 return PoisonValue::get(C1->getType()); 1139 1140 // Handle scalar UndefValue and scalable vector UndefValue. Fixed-length 1141 // vectors are always evaluated per element. 1142 bool IsScalableVector = isa<ScalableVectorType>(C1->getType()); 1143 bool HasScalarUndefOrScalableVectorUndef = 1144 (!C1->getType()->isVectorTy() || IsScalableVector) && 1145 (isa<UndefValue>(C1) || isa<UndefValue>(C2)); 1146 if (HasScalarUndefOrScalableVectorUndef) { 1147 switch (static_cast<Instruction::BinaryOps>(Opcode)) { 1148 case Instruction::Xor: 1149 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) 1150 // Handle undef ^ undef -> 0 special case. This is a common 1151 // idiom (misuse). 1152 return Constant::getNullValue(C1->getType()); 1153 LLVM_FALLTHROUGH; 1154 case Instruction::Add: 1155 case Instruction::Sub: 1156 return UndefValue::get(C1->getType()); 1157 case Instruction::And: 1158 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef 1159 return C1; 1160 return Constant::getNullValue(C1->getType()); // undef & X -> 0 1161 case Instruction::Mul: { 1162 // undef * undef -> undef 1163 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) 1164 return C1; 1165 const APInt *CV; 1166 // X * undef -> undef if X is odd 1167 if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV))) 1168 if ((*CV)[0]) 1169 return UndefValue::get(C1->getType()); 1170 1171 // X * undef -> 0 otherwise 1172 return Constant::getNullValue(C1->getType()); 1173 } 1174 case Instruction::SDiv: 1175 case Instruction::UDiv: 1176 // X / undef -> poison 1177 // X / 0 -> poison 1178 if (match(C2, m_CombineOr(m_Undef(), m_Zero()))) 1179 return PoisonValue::get(C2->getType()); 1180 // undef / 1 -> undef 1181 if (match(C2, m_One())) 1182 return C1; 1183 // undef / X -> 0 otherwise 1184 return Constant::getNullValue(C1->getType()); 1185 case Instruction::URem: 1186 case Instruction::SRem: 1187 // X % undef -> poison 1188 // X % 0 -> poison 1189 if (match(C2, m_CombineOr(m_Undef(), m_Zero()))) 1190 return PoisonValue::get(C2->getType()); 1191 // undef % X -> 0 otherwise 1192 return Constant::getNullValue(C1->getType()); 1193 case Instruction::Or: // X | undef -> -1 1194 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef 1195 return C1; 1196 return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 1197 case Instruction::LShr: 1198 // X >>l undef -> poison 1199 if (isa<UndefValue>(C2)) 1200 return PoisonValue::get(C2->getType()); 1201 // undef >>l 0 -> undef 1202 if (match(C2, m_Zero())) 1203 return C1; 1204 // undef >>l X -> 0 1205 return Constant::getNullValue(C1->getType()); 1206 case Instruction::AShr: 1207 // X >>a undef -> poison 1208 if (isa<UndefValue>(C2)) 1209 return PoisonValue::get(C2->getType()); 1210 // undef >>a 0 -> undef 1211 if (match(C2, m_Zero())) 1212 return C1; 1213 // TODO: undef >>a X -> poison if the shift is exact 1214 // undef >>a X -> 0 1215 return Constant::getNullValue(C1->getType()); 1216 case Instruction::Shl: 1217 // X << undef -> undef 1218 if (isa<UndefValue>(C2)) 1219 return PoisonValue::get(C2->getType()); 1220 // undef << 0 -> undef 1221 if (match(C2, m_Zero())) 1222 return C1; 1223 // undef << X -> 0 1224 return Constant::getNullValue(C1->getType()); 1225 case Instruction::FSub: 1226 // -0.0 - undef --> undef (consistent with "fneg undef") 1227 if (match(C1, m_NegZeroFP()) && isa<UndefValue>(C2)) 1228 return C2; 1229 LLVM_FALLTHROUGH; 1230 case Instruction::FAdd: 1231 case Instruction::FMul: 1232 case Instruction::FDiv: 1233 case Instruction::FRem: 1234 // [any flop] undef, undef -> undef 1235 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) 1236 return C1; 1237 // [any flop] C, undef -> NaN 1238 // [any flop] undef, C -> NaN 1239 // We could potentially specialize NaN/Inf constants vs. 'normal' 1240 // constants (possibly differently depending on opcode and operand). This 1241 // would allow returning undef sometimes. But it is always safe to fold to 1242 // NaN because we can choose the undef operand as NaN, and any FP opcode 1243 // with a NaN operand will propagate NaN. 1244 return ConstantFP::getNaN(C1->getType()); 1245 case Instruction::BinaryOpsEnd: 1246 llvm_unreachable("Invalid BinaryOp"); 1247 } 1248 } 1249 1250 // Neither constant should be UndefValue, unless these are vector constants. 1251 assert((!HasScalarUndefOrScalableVectorUndef) && "Unexpected UndefValue"); 1252 1253 // Handle simplifications when the RHS is a constant int. 1254 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { 1255 switch (Opcode) { 1256 case Instruction::Add: 1257 if (CI2->isZero()) return C1; // X + 0 == X 1258 break; 1259 case Instruction::Sub: 1260 if (CI2->isZero()) return C1; // X - 0 == X 1261 break; 1262 case Instruction::Mul: 1263 if (CI2->isZero()) return C2; // X * 0 == 0 1264 if (CI2->isOne()) 1265 return C1; // X * 1 == X 1266 break; 1267 case Instruction::UDiv: 1268 case Instruction::SDiv: 1269 if (CI2->isOne()) 1270 return C1; // X / 1 == X 1271 if (CI2->isZero()) 1272 return PoisonValue::get(CI2->getType()); // X / 0 == poison 1273 break; 1274 case Instruction::URem: 1275 case Instruction::SRem: 1276 if (CI2->isOne()) 1277 return Constant::getNullValue(CI2->getType()); // X % 1 == 0 1278 if (CI2->isZero()) 1279 return PoisonValue::get(CI2->getType()); // X % 0 == poison 1280 break; 1281 case Instruction::And: 1282 if (CI2->isZero()) return C2; // X & 0 == 0 1283 if (CI2->isMinusOne()) 1284 return C1; // X & -1 == X 1285 1286 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 1287 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64) 1288 if (CE1->getOpcode() == Instruction::ZExt) { 1289 unsigned DstWidth = CI2->getType()->getBitWidth(); 1290 unsigned SrcWidth = 1291 CE1->getOperand(0)->getType()->getPrimitiveSizeInBits(); 1292 APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth)); 1293 if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits) 1294 return C1; 1295 } 1296 1297 // If and'ing the address of a global with a constant, fold it. 1298 if (CE1->getOpcode() == Instruction::PtrToInt && 1299 isa<GlobalValue>(CE1->getOperand(0))) { 1300 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0)); 1301 1302 MaybeAlign GVAlign; 1303 1304 if (Module *TheModule = GV->getParent()) { 1305 const DataLayout &DL = TheModule->getDataLayout(); 1306 GVAlign = GV->getPointerAlignment(DL); 1307 1308 // If the function alignment is not specified then assume that it 1309 // is 4. 1310 // This is dangerous; on x86, the alignment of the pointer 1311 // corresponds to the alignment of the function, but might be less 1312 // than 4 if it isn't explicitly specified. 1313 // However, a fix for this behaviour was reverted because it 1314 // increased code size (see https://reviews.llvm.org/D55115) 1315 // FIXME: This code should be deleted once existing targets have 1316 // appropriate defaults 1317 if (isa<Function>(GV) && !DL.getFunctionPtrAlign()) 1318 GVAlign = Align(4); 1319 } else if (isa<Function>(GV)) { 1320 // Without a datalayout we have to assume the worst case: that the 1321 // function pointer isn't aligned at all. 1322 GVAlign = llvm::None; 1323 } else if (isa<GlobalVariable>(GV)) { 1324 GVAlign = cast<GlobalVariable>(GV)->getAlign(); 1325 } 1326 1327 if (GVAlign && *GVAlign > 1) { 1328 unsigned DstWidth = CI2->getType()->getBitWidth(); 1329 unsigned SrcWidth = std::min(DstWidth, Log2(*GVAlign)); 1330 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth)); 1331 1332 // If checking bits we know are clear, return zero. 1333 if ((CI2->getValue() & BitsNotSet) == CI2->getValue()) 1334 return Constant::getNullValue(CI2->getType()); 1335 } 1336 } 1337 } 1338 break; 1339 case Instruction::Or: 1340 if (CI2->isZero()) return C1; // X | 0 == X 1341 if (CI2->isMinusOne()) 1342 return C2; // X | -1 == -1 1343 break; 1344 case Instruction::Xor: 1345 if (CI2->isZero()) return C1; // X ^ 0 == X 1346 1347 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 1348 switch (CE1->getOpcode()) { 1349 default: break; 1350 case Instruction::ICmp: 1351 case Instruction::FCmp: 1352 // cmp pred ^ true -> cmp !pred 1353 assert(CI2->isOne()); 1354 CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate(); 1355 pred = CmpInst::getInversePredicate(pred); 1356 return ConstantExpr::getCompare(pred, CE1->getOperand(0), 1357 CE1->getOperand(1)); 1358 } 1359 } 1360 break; 1361 case Instruction::AShr: 1362 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2 1363 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) 1364 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero. 1365 return ConstantExpr::getLShr(C1, C2); 1366 break; 1367 } 1368 } else if (isa<ConstantInt>(C1)) { 1369 // If C1 is a ConstantInt and C2 is not, swap the operands. 1370 if (Instruction::isCommutative(Opcode)) 1371 return ConstantExpr::get(Opcode, C2, C1); 1372 } 1373 1374 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) { 1375 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { 1376 const APInt &C1V = CI1->getValue(); 1377 const APInt &C2V = CI2->getValue(); 1378 switch (Opcode) { 1379 default: 1380 break; 1381 case Instruction::Add: 1382 return ConstantInt::get(CI1->getContext(), C1V + C2V); 1383 case Instruction::Sub: 1384 return ConstantInt::get(CI1->getContext(), C1V - C2V); 1385 case Instruction::Mul: 1386 return ConstantInt::get(CI1->getContext(), C1V * C2V); 1387 case Instruction::UDiv: 1388 assert(!CI2->isZero() && "Div by zero handled above"); 1389 return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V)); 1390 case Instruction::SDiv: 1391 assert(!CI2->isZero() && "Div by zero handled above"); 1392 if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) 1393 return PoisonValue::get(CI1->getType()); // MIN_INT / -1 -> poison 1394 return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V)); 1395 case Instruction::URem: 1396 assert(!CI2->isZero() && "Div by zero handled above"); 1397 return ConstantInt::get(CI1->getContext(), C1V.urem(C2V)); 1398 case Instruction::SRem: 1399 assert(!CI2->isZero() && "Div by zero handled above"); 1400 if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) 1401 return PoisonValue::get(CI1->getType()); // MIN_INT % -1 -> poison 1402 return ConstantInt::get(CI1->getContext(), C1V.srem(C2V)); 1403 case Instruction::And: 1404 return ConstantInt::get(CI1->getContext(), C1V & C2V); 1405 case Instruction::Or: 1406 return ConstantInt::get(CI1->getContext(), C1V | C2V); 1407 case Instruction::Xor: 1408 return ConstantInt::get(CI1->getContext(), C1V ^ C2V); 1409 case Instruction::Shl: 1410 if (C2V.ult(C1V.getBitWidth())) 1411 return ConstantInt::get(CI1->getContext(), C1V.shl(C2V)); 1412 return PoisonValue::get(C1->getType()); // too big shift is poison 1413 case Instruction::LShr: 1414 if (C2V.ult(C1V.getBitWidth())) 1415 return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V)); 1416 return PoisonValue::get(C1->getType()); // too big shift is poison 1417 case Instruction::AShr: 1418 if (C2V.ult(C1V.getBitWidth())) 1419 return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V)); 1420 return PoisonValue::get(C1->getType()); // too big shift is poison 1421 } 1422 } 1423 1424 switch (Opcode) { 1425 case Instruction::SDiv: 1426 case Instruction::UDiv: 1427 case Instruction::URem: 1428 case Instruction::SRem: 1429 case Instruction::LShr: 1430 case Instruction::AShr: 1431 case Instruction::Shl: 1432 if (CI1->isZero()) return C1; 1433 break; 1434 default: 1435 break; 1436 } 1437 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) { 1438 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) { 1439 const APFloat &C1V = CFP1->getValueAPF(); 1440 const APFloat &C2V = CFP2->getValueAPF(); 1441 APFloat C3V = C1V; // copy for modification 1442 switch (Opcode) { 1443 default: 1444 break; 1445 case Instruction::FAdd: 1446 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven); 1447 return ConstantFP::get(C1->getContext(), C3V); 1448 case Instruction::FSub: 1449 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven); 1450 return ConstantFP::get(C1->getContext(), C3V); 1451 case Instruction::FMul: 1452 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven); 1453 return ConstantFP::get(C1->getContext(), C3V); 1454 case Instruction::FDiv: 1455 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven); 1456 return ConstantFP::get(C1->getContext(), C3V); 1457 case Instruction::FRem: 1458 (void)C3V.mod(C2V); 1459 return ConstantFP::get(C1->getContext(), C3V); 1460 } 1461 } 1462 } else if (auto *VTy = dyn_cast<VectorType>(C1->getType())) { 1463 // Fast path for splatted constants. 1464 if (Constant *C2Splat = C2->getSplatValue()) { 1465 if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue()) 1466 return PoisonValue::get(VTy); 1467 if (Constant *C1Splat = C1->getSplatValue()) { 1468 return ConstantVector::getSplat( 1469 VTy->getElementCount(), 1470 ConstantExpr::get(Opcode, C1Splat, C2Splat)); 1471 } 1472 } 1473 1474 if (auto *FVTy = dyn_cast<FixedVectorType>(VTy)) { 1475 // Fold each element and create a vector constant from those constants. 1476 SmallVector<Constant*, 16> Result; 1477 Type *Ty = IntegerType::get(FVTy->getContext(), 32); 1478 for (unsigned i = 0, e = FVTy->getNumElements(); i != e; ++i) { 1479 Constant *ExtractIdx = ConstantInt::get(Ty, i); 1480 Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx); 1481 Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx); 1482 1483 // If any element of a divisor vector is zero, the whole op is poison. 1484 if (Instruction::isIntDivRem(Opcode) && RHS->isNullValue()) 1485 return PoisonValue::get(VTy); 1486 1487 Result.push_back(ConstantExpr::get(Opcode, LHS, RHS)); 1488 } 1489 1490 return ConstantVector::get(Result); 1491 } 1492 } 1493 1494 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 1495 // There are many possible foldings we could do here. We should probably 1496 // at least fold add of a pointer with an integer into the appropriate 1497 // getelementptr. This will improve alias analysis a bit. 1498 1499 // Given ((a + b) + c), if (b + c) folds to something interesting, return 1500 // (a + (b + c)). 1501 if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) { 1502 Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2); 1503 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode) 1504 return ConstantExpr::get(Opcode, CE1->getOperand(0), T); 1505 } 1506 } else if (isa<ConstantExpr>(C2)) { 1507 // If C2 is a constant expr and C1 isn't, flop them around and fold the 1508 // other way if possible. 1509 if (Instruction::isCommutative(Opcode)) 1510 return ConstantFoldBinaryInstruction(Opcode, C2, C1); 1511 } 1512 1513 // i1 can be simplified in many cases. 1514 if (C1->getType()->isIntegerTy(1)) { 1515 switch (Opcode) { 1516 case Instruction::Add: 1517 case Instruction::Sub: 1518 return ConstantExpr::getXor(C1, C2); 1519 case Instruction::Mul: 1520 return ConstantExpr::getAnd(C1, C2); 1521 case Instruction::Shl: 1522 case Instruction::LShr: 1523 case Instruction::AShr: 1524 // We can assume that C2 == 0. If it were one the result would be 1525 // undefined because the shift value is as large as the bitwidth. 1526 return C1; 1527 case Instruction::SDiv: 1528 case Instruction::UDiv: 1529 // We can assume that C2 == 1. If it were zero the result would be 1530 // undefined through division by zero. 1531 return C1; 1532 case Instruction::URem: 1533 case Instruction::SRem: 1534 // We can assume that C2 == 1. If it were zero the result would be 1535 // undefined through division by zero. 1536 return ConstantInt::getFalse(C1->getContext()); 1537 default: 1538 break; 1539 } 1540 } 1541 1542 // We don't know how to fold this. 1543 return nullptr; 1544 } 1545 1546 /// This type is zero-sized if it's an array or structure of zero-sized types. 1547 /// The only leaf zero-sized type is an empty structure. 1548 static bool isMaybeZeroSizedType(Type *Ty) { 1549 if (StructType *STy = dyn_cast<StructType>(Ty)) { 1550 if (STy->isOpaque()) return true; // Can't say. 1551 1552 // If all of elements have zero size, this does too. 1553 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) 1554 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false; 1555 return true; 1556 1557 } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 1558 return isMaybeZeroSizedType(ATy->getElementType()); 1559 } 1560 return false; 1561 } 1562 1563 /// Compare the two constants as though they were getelementptr indices. 1564 /// This allows coercion of the types to be the same thing. 1565 /// 1566 /// If the two constants are the "same" (after coercion), return 0. If the 1567 /// first is less than the second, return -1, if the second is less than the 1568 /// first, return 1. If the constants are not integral, return -2. 1569 /// 1570 static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) { 1571 if (C1 == C2) return 0; 1572 1573 // Ok, we found a different index. If they are not ConstantInt, we can't do 1574 // anything with them. 1575 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) 1576 return -2; // don't know! 1577 1578 // We cannot compare the indices if they don't fit in an int64_t. 1579 if (cast<ConstantInt>(C1)->getValue().getActiveBits() > 64 || 1580 cast<ConstantInt>(C2)->getValue().getActiveBits() > 64) 1581 return -2; // don't know! 1582 1583 // Ok, we have two differing integer indices. Sign extend them to be the same 1584 // type. 1585 int64_t C1Val = cast<ConstantInt>(C1)->getSExtValue(); 1586 int64_t C2Val = cast<ConstantInt>(C2)->getSExtValue(); 1587 1588 if (C1Val == C2Val) return 0; // They are equal 1589 1590 // If the type being indexed over is really just a zero sized type, there is 1591 // no pointer difference being made here. 1592 if (isMaybeZeroSizedType(ElTy)) 1593 return -2; // dunno. 1594 1595 // If they are really different, now that they are the same type, then we 1596 // found a difference! 1597 if (C1Val < C2Val) 1598 return -1; 1599 else 1600 return 1; 1601 } 1602 1603 /// This function determines if there is anything we can decide about the two 1604 /// constants provided. This doesn't need to handle simple things like 1605 /// ConstantFP comparisons, but should instead handle ConstantExprs. 1606 /// If we can determine that the two constants have a particular relation to 1607 /// each other, we should return the corresponding FCmpInst predicate, 1608 /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in 1609 /// ConstantFoldCompareInstruction. 1610 /// 1611 /// To simplify this code we canonicalize the relation so that the first 1612 /// operand is always the most "complex" of the two. We consider ConstantFP 1613 /// to be the simplest, and ConstantExprs to be the most complex. 1614 static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) { 1615 assert(V1->getType() == V2->getType() && 1616 "Cannot compare values of different types!"); 1617 1618 // We do not know if a constant expression will evaluate to a number or NaN. 1619 // Therefore, we can only say that the relation is unordered or equal. 1620 if (V1 == V2) return FCmpInst::FCMP_UEQ; 1621 1622 if (!isa<ConstantExpr>(V1)) { 1623 if (!isa<ConstantExpr>(V2)) { 1624 // Simple case, use the standard constant folder. 1625 ConstantInt *R = nullptr; 1626 R = dyn_cast<ConstantInt>( 1627 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2)); 1628 if (R && !R->isZero()) 1629 return FCmpInst::FCMP_OEQ; 1630 R = dyn_cast<ConstantInt>( 1631 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2)); 1632 if (R && !R->isZero()) 1633 return FCmpInst::FCMP_OLT; 1634 R = dyn_cast<ConstantInt>( 1635 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2)); 1636 if (R && !R->isZero()) 1637 return FCmpInst::FCMP_OGT; 1638 1639 // Nothing more we can do 1640 return FCmpInst::BAD_FCMP_PREDICATE; 1641 } 1642 1643 // If the first operand is simple and second is ConstantExpr, swap operands. 1644 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1); 1645 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE) 1646 return FCmpInst::getSwappedPredicate(SwappedRelation); 1647 } else { 1648 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a 1649 // constantexpr or a simple constant. 1650 ConstantExpr *CE1 = cast<ConstantExpr>(V1); 1651 switch (CE1->getOpcode()) { 1652 case Instruction::FPTrunc: 1653 case Instruction::FPExt: 1654 case Instruction::UIToFP: 1655 case Instruction::SIToFP: 1656 // We might be able to do something with these but we don't right now. 1657 break; 1658 default: 1659 break; 1660 } 1661 } 1662 // There are MANY other foldings that we could perform here. They will 1663 // probably be added on demand, as they seem needed. 1664 return FCmpInst::BAD_FCMP_PREDICATE; 1665 } 1666 1667 static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1, 1668 const GlobalValue *GV2) { 1669 auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) { 1670 if (GV->isInterposable() || GV->hasGlobalUnnamedAddr()) 1671 return true; 1672 if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) { 1673 Type *Ty = GVar->getValueType(); 1674 // A global with opaque type might end up being zero sized. 1675 if (!Ty->isSized()) 1676 return true; 1677 // A global with an empty type might lie at the address of any other 1678 // global. 1679 if (Ty->isEmptyTy()) 1680 return true; 1681 } 1682 return false; 1683 }; 1684 // Don't try to decide equality of aliases. 1685 if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2)) 1686 if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2)) 1687 return ICmpInst::ICMP_NE; 1688 return ICmpInst::BAD_ICMP_PREDICATE; 1689 } 1690 1691 /// This function determines if there is anything we can decide about the two 1692 /// constants provided. This doesn't need to handle simple things like integer 1693 /// comparisons, but should instead handle ConstantExprs and GlobalValues. 1694 /// If we can determine that the two constants have a particular relation to 1695 /// each other, we should return the corresponding ICmp predicate, otherwise 1696 /// return ICmpInst::BAD_ICMP_PREDICATE. 1697 /// 1698 /// To simplify this code we canonicalize the relation so that the first 1699 /// operand is always the most "complex" of the two. We consider simple 1700 /// constants (like ConstantInt) to be the simplest, followed by 1701 /// GlobalValues, followed by ConstantExpr's (the most complex). 1702 /// 1703 static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2, 1704 bool isSigned) { 1705 assert(V1->getType() == V2->getType() && 1706 "Cannot compare different types of values!"); 1707 if (V1 == V2) return ICmpInst::ICMP_EQ; 1708 1709 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) && 1710 !isa<BlockAddress>(V1)) { 1711 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) && 1712 !isa<BlockAddress>(V2)) { 1713 // We distilled this down to a simple case, use the standard constant 1714 // folder. 1715 ConstantInt *R = nullptr; 1716 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ; 1717 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); 1718 if (R && !R->isZero()) 1719 return pred; 1720 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; 1721 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); 1722 if (R && !R->isZero()) 1723 return pred; 1724 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 1725 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); 1726 if (R && !R->isZero()) 1727 return pred; 1728 1729 // If we couldn't figure it out, bail. 1730 return ICmpInst::BAD_ICMP_PREDICATE; 1731 } 1732 1733 // If the first operand is simple, swap operands. 1734 ICmpInst::Predicate SwappedRelation = 1735 evaluateICmpRelation(V2, V1, isSigned); 1736 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) 1737 return ICmpInst::getSwappedPredicate(SwappedRelation); 1738 1739 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) { 1740 if (isa<ConstantExpr>(V2)) { // Swap as necessary. 1741 ICmpInst::Predicate SwappedRelation = 1742 evaluateICmpRelation(V2, V1, isSigned); 1743 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) 1744 return ICmpInst::getSwappedPredicate(SwappedRelation); 1745 return ICmpInst::BAD_ICMP_PREDICATE; 1746 } 1747 1748 // Now we know that the RHS is a GlobalValue, BlockAddress or simple 1749 // constant (which, since the types must match, means that it's a 1750 // ConstantPointerNull). 1751 if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) { 1752 return areGlobalsPotentiallyEqual(GV, GV2); 1753 } else if (isa<BlockAddress>(V2)) { 1754 return ICmpInst::ICMP_NE; // Globals never equal labels. 1755 } else { 1756 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!"); 1757 // GlobalVals can never be null unless they have external weak linkage. 1758 // We don't try to evaluate aliases here. 1759 // NOTE: We should not be doing this constant folding if null pointer 1760 // is considered valid for the function. But currently there is no way to 1761 // query it from the Constant type. 1762 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV) && 1763 !NullPointerIsDefined(nullptr /* F */, 1764 GV->getType()->getAddressSpace())) 1765 return ICmpInst::ICMP_UGT; 1766 } 1767 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) { 1768 if (isa<ConstantExpr>(V2)) { // Swap as necessary. 1769 ICmpInst::Predicate SwappedRelation = 1770 evaluateICmpRelation(V2, V1, isSigned); 1771 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) 1772 return ICmpInst::getSwappedPredicate(SwappedRelation); 1773 return ICmpInst::BAD_ICMP_PREDICATE; 1774 } 1775 1776 // Now we know that the RHS is a GlobalValue, BlockAddress or simple 1777 // constant (which, since the types must match, means that it is a 1778 // ConstantPointerNull). 1779 if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) { 1780 // Block address in another function can't equal this one, but block 1781 // addresses in the current function might be the same if blocks are 1782 // empty. 1783 if (BA2->getFunction() != BA->getFunction()) 1784 return ICmpInst::ICMP_NE; 1785 } else { 1786 // Block addresses aren't null, don't equal the address of globals. 1787 assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) && 1788 "Canonicalization guarantee!"); 1789 return ICmpInst::ICMP_NE; 1790 } 1791 } else { 1792 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a 1793 // constantexpr, a global, block address, or a simple constant. 1794 ConstantExpr *CE1 = cast<ConstantExpr>(V1); 1795 Constant *CE1Op0 = CE1->getOperand(0); 1796 1797 switch (CE1->getOpcode()) { 1798 case Instruction::Trunc: 1799 case Instruction::FPTrunc: 1800 case Instruction::FPExt: 1801 case Instruction::FPToUI: 1802 case Instruction::FPToSI: 1803 break; // We can't evaluate floating point casts or truncations. 1804 1805 case Instruction::BitCast: 1806 // If this is a global value cast, check to see if the RHS is also a 1807 // GlobalValue. 1808 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) 1809 if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) 1810 return areGlobalsPotentiallyEqual(GV, GV2); 1811 LLVM_FALLTHROUGH; 1812 case Instruction::UIToFP: 1813 case Instruction::SIToFP: 1814 case Instruction::ZExt: 1815 case Instruction::SExt: 1816 // We can't evaluate floating point casts or truncations. 1817 if (CE1Op0->getType()->isFPOrFPVectorTy()) 1818 break; 1819 1820 // If the cast is not actually changing bits, and the second operand is a 1821 // null pointer, do the comparison with the pre-casted value. 1822 if (V2->isNullValue() && CE1->getType()->isIntOrPtrTy()) { 1823 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false; 1824 if (CE1->getOpcode() == Instruction::SExt) isSigned = true; 1825 return evaluateICmpRelation(CE1Op0, 1826 Constant::getNullValue(CE1Op0->getType()), 1827 isSigned); 1828 } 1829 break; 1830 1831 case Instruction::GetElementPtr: { 1832 GEPOperator *CE1GEP = cast<GEPOperator>(CE1); 1833 // Ok, since this is a getelementptr, we know that the constant has a 1834 // pointer type. Check the various cases. 1835 if (isa<ConstantPointerNull>(V2)) { 1836 // If we are comparing a GEP to a null pointer, check to see if the base 1837 // of the GEP equals the null pointer. 1838 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { 1839 // If its not weak linkage, the GVal must have a non-zero address 1840 // so the result is greater-than 1841 if (!GV->hasExternalWeakLinkage()) 1842 return ICmpInst::ICMP_UGT; 1843 } else if (isa<ConstantPointerNull>(CE1Op0)) { 1844 // If we are indexing from a null pointer, check to see if we have any 1845 // non-zero indices. 1846 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) 1847 if (!CE1->getOperand(i)->isNullValue()) 1848 // Offsetting from null, must not be equal. 1849 return ICmpInst::ICMP_UGT; 1850 // Only zero indexes from null, must still be zero. 1851 return ICmpInst::ICMP_EQ; 1852 } 1853 // Otherwise, we can't really say if the first operand is null or not. 1854 } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) { 1855 if (isa<ConstantPointerNull>(CE1Op0)) { 1856 // If its not weak linkage, the GVal must have a non-zero address 1857 // so the result is less-than 1858 if (!GV2->hasExternalWeakLinkage()) 1859 return ICmpInst::ICMP_ULT; 1860 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { 1861 if (GV == GV2) { 1862 // If this is a getelementptr of the same global, then it must be 1863 // different. Because the types must match, the getelementptr could 1864 // only have at most one index, and because we fold getelementptr's 1865 // with a single zero index, it must be nonzero. 1866 assert(CE1->getNumOperands() == 2 && 1867 !CE1->getOperand(1)->isNullValue() && 1868 "Surprising getelementptr!"); 1869 return ICmpInst::ICMP_UGT; 1870 } else { 1871 if (CE1GEP->hasAllZeroIndices()) 1872 return areGlobalsPotentiallyEqual(GV, GV2); 1873 return ICmpInst::BAD_ICMP_PREDICATE; 1874 } 1875 } 1876 } else { 1877 ConstantExpr *CE2 = cast<ConstantExpr>(V2); 1878 Constant *CE2Op0 = CE2->getOperand(0); 1879 1880 // There are MANY other foldings that we could perform here. They will 1881 // probably be added on demand, as they seem needed. 1882 switch (CE2->getOpcode()) { 1883 default: break; 1884 case Instruction::GetElementPtr: 1885 // By far the most common case to handle is when the base pointers are 1886 // obviously to the same global. 1887 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { 1888 // Don't know relative ordering, but check for inequality. 1889 if (CE1Op0 != CE2Op0) { 1890 GEPOperator *CE2GEP = cast<GEPOperator>(CE2); 1891 if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices()) 1892 return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0), 1893 cast<GlobalValue>(CE2Op0)); 1894 return ICmpInst::BAD_ICMP_PREDICATE; 1895 } 1896 // Ok, we know that both getelementptr instructions are based on the 1897 // same global. From this, we can precisely determine the relative 1898 // ordering of the resultant pointers. 1899 unsigned i = 1; 1900 1901 // The logic below assumes that the result of the comparison 1902 // can be determined by finding the first index that differs. 1903 // This doesn't work if there is over-indexing in any 1904 // subsequent indices, so check for that case first. 1905 if (!CE1->isGEPWithNoNotionalOverIndexing() || 1906 !CE2->isGEPWithNoNotionalOverIndexing()) 1907 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. 1908 1909 // Compare all of the operands the GEP's have in common. 1910 gep_type_iterator GTI = gep_type_begin(CE1); 1911 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); 1912 ++i, ++GTI) 1913 switch (IdxCompare(CE1->getOperand(i), 1914 CE2->getOperand(i), GTI.getIndexedType())) { 1915 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT; 1916 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT; 1917 case -2: return ICmpInst::BAD_ICMP_PREDICATE; 1918 } 1919 1920 // Ok, we ran out of things they have in common. If any leftovers 1921 // are non-zero then we have a difference, otherwise we are equal. 1922 for (; i < CE1->getNumOperands(); ++i) 1923 if (!CE1->getOperand(i)->isNullValue()) { 1924 if (isa<ConstantInt>(CE1->getOperand(i))) 1925 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 1926 else 1927 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. 1928 } 1929 1930 for (; i < CE2->getNumOperands(); ++i) 1931 if (!CE2->getOperand(i)->isNullValue()) { 1932 if (isa<ConstantInt>(CE2->getOperand(i))) 1933 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; 1934 else 1935 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. 1936 } 1937 return ICmpInst::ICMP_EQ; 1938 } 1939 } 1940 } 1941 break; 1942 } 1943 default: 1944 break; 1945 } 1946 } 1947 1948 return ICmpInst::BAD_ICMP_PREDICATE; 1949 } 1950 1951 Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, 1952 Constant *C1, Constant *C2) { 1953 Type *ResultTy; 1954 if (VectorType *VT = dyn_cast<VectorType>(C1->getType())) 1955 ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()), 1956 VT->getElementCount()); 1957 else 1958 ResultTy = Type::getInt1Ty(C1->getContext()); 1959 1960 // Fold FCMP_FALSE/FCMP_TRUE unconditionally. 1961 if (pred == FCmpInst::FCMP_FALSE) 1962 return Constant::getNullValue(ResultTy); 1963 1964 if (pred == FCmpInst::FCMP_TRUE) 1965 return Constant::getAllOnesValue(ResultTy); 1966 1967 // Handle some degenerate cases first 1968 if (isa<PoisonValue>(C1) || isa<PoisonValue>(C2)) 1969 return PoisonValue::get(ResultTy); 1970 1971 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) { 1972 CmpInst::Predicate Predicate = CmpInst::Predicate(pred); 1973 bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate); 1974 // For EQ and NE, we can always pick a value for the undef to make the 1975 // predicate pass or fail, so we can return undef. 1976 // Also, if both operands are undef, we can return undef for int comparison. 1977 if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2)) 1978 return UndefValue::get(ResultTy); 1979 1980 // Otherwise, for integer compare, pick the same value as the non-undef 1981 // operand, and fold it to true or false. 1982 if (isIntegerPredicate) 1983 return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(Predicate)); 1984 1985 // Choosing NaN for the undef will always make unordered comparison succeed 1986 // and ordered comparison fails. 1987 return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate)); 1988 } 1989 1990 // icmp eq/ne(null,GV) -> false/true 1991 if (C1->isNullValue()) { 1992 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2)) 1993 // Don't try to evaluate aliases. External weak GV can be null. 1994 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() && 1995 !NullPointerIsDefined(nullptr /* F */, 1996 GV->getType()->getAddressSpace())) { 1997 if (pred == ICmpInst::ICMP_EQ) 1998 return ConstantInt::getFalse(C1->getContext()); 1999 else if (pred == ICmpInst::ICMP_NE) 2000 return ConstantInt::getTrue(C1->getContext()); 2001 } 2002 // icmp eq/ne(GV,null) -> false/true 2003 } else if (C2->isNullValue()) { 2004 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1)) { 2005 // Don't try to evaluate aliases. External weak GV can be null. 2006 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() && 2007 !NullPointerIsDefined(nullptr /* F */, 2008 GV->getType()->getAddressSpace())) { 2009 if (pred == ICmpInst::ICMP_EQ) 2010 return ConstantInt::getFalse(C1->getContext()); 2011 else if (pred == ICmpInst::ICMP_NE) 2012 return ConstantInt::getTrue(C1->getContext()); 2013 } 2014 } 2015 2016 // The caller is expected to commute the operands if the constant expression 2017 // is C2. 2018 // C1 >= 0 --> true 2019 if (pred == ICmpInst::ICMP_UGE) 2020 return Constant::getAllOnesValue(ResultTy); 2021 // C1 < 0 --> false 2022 if (pred == ICmpInst::ICMP_ULT) 2023 return Constant::getNullValue(ResultTy); 2024 } 2025 2026 // If the comparison is a comparison between two i1's, simplify it. 2027 if (C1->getType()->isIntegerTy(1)) { 2028 switch(pred) { 2029 case ICmpInst::ICMP_EQ: 2030 if (isa<ConstantInt>(C2)) 2031 return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2)); 2032 return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2); 2033 case ICmpInst::ICMP_NE: 2034 return ConstantExpr::getXor(C1, C2); 2035 default: 2036 break; 2037 } 2038 } 2039 2040 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) { 2041 const APInt &V1 = cast<ConstantInt>(C1)->getValue(); 2042 const APInt &V2 = cast<ConstantInt>(C2)->getValue(); 2043 switch (pred) { 2044 default: llvm_unreachable("Invalid ICmp Predicate"); 2045 case ICmpInst::ICMP_EQ: return ConstantInt::get(ResultTy, V1 == V2); 2046 case ICmpInst::ICMP_NE: return ConstantInt::get(ResultTy, V1 != V2); 2047 case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2)); 2048 case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2)); 2049 case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2)); 2050 case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2)); 2051 case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2)); 2052 case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2)); 2053 case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2)); 2054 case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2)); 2055 } 2056 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) { 2057 const APFloat &C1V = cast<ConstantFP>(C1)->getValueAPF(); 2058 const APFloat &C2V = cast<ConstantFP>(C2)->getValueAPF(); 2059 APFloat::cmpResult R = C1V.compare(C2V); 2060 switch (pred) { 2061 default: llvm_unreachable("Invalid FCmp Predicate"); 2062 case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy); 2063 case FCmpInst::FCMP_TRUE: return Constant::getAllOnesValue(ResultTy); 2064 case FCmpInst::FCMP_UNO: 2065 return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered); 2066 case FCmpInst::FCMP_ORD: 2067 return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered); 2068 case FCmpInst::FCMP_UEQ: 2069 return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered || 2070 R==APFloat::cmpEqual); 2071 case FCmpInst::FCMP_OEQ: 2072 return ConstantInt::get(ResultTy, R==APFloat::cmpEqual); 2073 case FCmpInst::FCMP_UNE: 2074 return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual); 2075 case FCmpInst::FCMP_ONE: 2076 return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan || 2077 R==APFloat::cmpGreaterThan); 2078 case FCmpInst::FCMP_ULT: 2079 return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered || 2080 R==APFloat::cmpLessThan); 2081 case FCmpInst::FCMP_OLT: 2082 return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan); 2083 case FCmpInst::FCMP_UGT: 2084 return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered || 2085 R==APFloat::cmpGreaterThan); 2086 case FCmpInst::FCMP_OGT: 2087 return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan); 2088 case FCmpInst::FCMP_ULE: 2089 return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan); 2090 case FCmpInst::FCMP_OLE: 2091 return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan || 2092 R==APFloat::cmpEqual); 2093 case FCmpInst::FCMP_UGE: 2094 return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan); 2095 case FCmpInst::FCMP_OGE: 2096 return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan || 2097 R==APFloat::cmpEqual); 2098 } 2099 } else if (auto *C1VTy = dyn_cast<VectorType>(C1->getType())) { 2100 2101 // Fast path for splatted constants. 2102 if (Constant *C1Splat = C1->getSplatValue()) 2103 if (Constant *C2Splat = C2->getSplatValue()) 2104 return ConstantVector::getSplat( 2105 C1VTy->getElementCount(), 2106 ConstantExpr::getCompare(pred, C1Splat, C2Splat)); 2107 2108 // Do not iterate on scalable vector. The number of elements is unknown at 2109 // compile-time. 2110 if (isa<ScalableVectorType>(C1VTy)) 2111 return nullptr; 2112 2113 // If we can constant fold the comparison of each element, constant fold 2114 // the whole vector comparison. 2115 SmallVector<Constant*, 4> ResElts; 2116 Type *Ty = IntegerType::get(C1->getContext(), 32); 2117 // Compare the elements, producing an i1 result or constant expr. 2118 for (unsigned I = 0, E = C1VTy->getElementCount().getKnownMinValue(); 2119 I != E; ++I) { 2120 Constant *C1E = 2121 ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, I)); 2122 Constant *C2E = 2123 ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, I)); 2124 2125 ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E)); 2126 } 2127 2128 return ConstantVector::get(ResElts); 2129 } 2130 2131 if (C1->getType()->isFloatingPointTy() && 2132 // Only call evaluateFCmpRelation if we have a constant expr to avoid 2133 // infinite recursive loop 2134 (isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) { 2135 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. 2136 switch (evaluateFCmpRelation(C1, C2)) { 2137 default: llvm_unreachable("Unknown relation!"); 2138 case FCmpInst::FCMP_UNO: 2139 case FCmpInst::FCMP_ORD: 2140 case FCmpInst::FCMP_UNE: 2141 case FCmpInst::FCMP_ULT: 2142 case FCmpInst::FCMP_UGT: 2143 case FCmpInst::FCMP_ULE: 2144 case FCmpInst::FCMP_UGE: 2145 case FCmpInst::FCMP_TRUE: 2146 case FCmpInst::FCMP_FALSE: 2147 case FCmpInst::BAD_FCMP_PREDICATE: 2148 break; // Couldn't determine anything about these constants. 2149 case FCmpInst::FCMP_OEQ: // We know that C1 == C2 2150 Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ || 2151 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE || 2152 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); 2153 break; 2154 case FCmpInst::FCMP_OLT: // We know that C1 < C2 2155 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || 2156 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT || 2157 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE); 2158 break; 2159 case FCmpInst::FCMP_OGT: // We know that C1 > C2 2160 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || 2161 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT || 2162 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); 2163 break; 2164 case FCmpInst::FCMP_OLE: // We know that C1 <= C2 2165 // We can only partially decide this relation. 2166 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) 2167 Result = 0; 2168 else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) 2169 Result = 1; 2170 break; 2171 case FCmpInst::FCMP_OGE: // We known that C1 >= C2 2172 // We can only partially decide this relation. 2173 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) 2174 Result = 0; 2175 else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) 2176 Result = 1; 2177 break; 2178 case FCmpInst::FCMP_ONE: // We know that C1 != C2 2179 // We can only partially decide this relation. 2180 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) 2181 Result = 0; 2182 else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE) 2183 Result = 1; 2184 break; 2185 case FCmpInst::FCMP_UEQ: // We know that C1 == C2 || isUnordered(C1, C2). 2186 // We can only partially decide this relation. 2187 if (pred == FCmpInst::FCMP_ONE) 2188 Result = 0; 2189 else if (pred == FCmpInst::FCMP_UEQ) 2190 Result = 1; 2191 break; 2192 } 2193 2194 // If we evaluated the result, return it now. 2195 if (Result != -1) 2196 return ConstantInt::get(ResultTy, Result); 2197 2198 } else { 2199 // Evaluate the relation between the two constants, per the predicate. 2200 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. 2201 switch (evaluateICmpRelation(C1, C2, 2202 CmpInst::isSigned((CmpInst::Predicate)pred))) { 2203 default: llvm_unreachable("Unknown relational!"); 2204 case ICmpInst::BAD_ICMP_PREDICATE: 2205 break; // Couldn't determine anything about these constants. 2206 case ICmpInst::ICMP_EQ: // We know the constants are equal! 2207 // If we know the constants are equal, we can decide the result of this 2208 // computation precisely. 2209 Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred); 2210 break; 2211 case ICmpInst::ICMP_ULT: 2212 switch (pred) { 2213 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE: 2214 Result = 1; break; 2215 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE: 2216 Result = 0; break; 2217 } 2218 break; 2219 case ICmpInst::ICMP_SLT: 2220 switch (pred) { 2221 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE: 2222 Result = 1; break; 2223 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE: 2224 Result = 0; break; 2225 } 2226 break; 2227 case ICmpInst::ICMP_UGT: 2228 switch (pred) { 2229 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE: 2230 Result = 1; break; 2231 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE: 2232 Result = 0; break; 2233 } 2234 break; 2235 case ICmpInst::ICMP_SGT: 2236 switch (pred) { 2237 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE: 2238 Result = 1; break; 2239 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE: 2240 Result = 0; break; 2241 } 2242 break; 2243 case ICmpInst::ICMP_ULE: 2244 if (pred == ICmpInst::ICMP_UGT) Result = 0; 2245 if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1; 2246 break; 2247 case ICmpInst::ICMP_SLE: 2248 if (pred == ICmpInst::ICMP_SGT) Result = 0; 2249 if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1; 2250 break; 2251 case ICmpInst::ICMP_UGE: 2252 if (pred == ICmpInst::ICMP_ULT) Result = 0; 2253 if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1; 2254 break; 2255 case ICmpInst::ICMP_SGE: 2256 if (pred == ICmpInst::ICMP_SLT) Result = 0; 2257 if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1; 2258 break; 2259 case ICmpInst::ICMP_NE: 2260 if (pred == ICmpInst::ICMP_EQ) Result = 0; 2261 if (pred == ICmpInst::ICMP_NE) Result = 1; 2262 break; 2263 } 2264 2265 // If we evaluated the result, return it now. 2266 if (Result != -1) 2267 return ConstantInt::get(ResultTy, Result); 2268 2269 // If the right hand side is a bitcast, try using its inverse to simplify 2270 // it by moving it to the left hand side. We can't do this if it would turn 2271 // a vector compare into a scalar compare or visa versa, or if it would turn 2272 // the operands into FP values. 2273 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) { 2274 Constant *CE2Op0 = CE2->getOperand(0); 2275 if (CE2->getOpcode() == Instruction::BitCast && 2276 CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy() && 2277 !CE2Op0->getType()->isFPOrFPVectorTy()) { 2278 Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType()); 2279 return ConstantExpr::getICmp(pred, Inverse, CE2Op0); 2280 } 2281 } 2282 2283 // If the left hand side is an extension, try eliminating it. 2284 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 2285 if ((CE1->getOpcode() == Instruction::SExt && 2286 ICmpInst::isSigned((ICmpInst::Predicate)pred)) || 2287 (CE1->getOpcode() == Instruction::ZExt && 2288 !ICmpInst::isSigned((ICmpInst::Predicate)pred))){ 2289 Constant *CE1Op0 = CE1->getOperand(0); 2290 Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType()); 2291 if (CE1Inverse == CE1Op0) { 2292 // Check whether we can safely truncate the right hand side. 2293 Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType()); 2294 if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse, 2295 C2->getType()) == C2) 2296 return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse); 2297 } 2298 } 2299 } 2300 2301 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) || 2302 (C1->isNullValue() && !C2->isNullValue())) { 2303 // If C2 is a constant expr and C1 isn't, flip them around and fold the 2304 // other way if possible. 2305 // Also, if C1 is null and C2 isn't, flip them around. 2306 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred); 2307 return ConstantExpr::getICmp(pred, C2, C1); 2308 } 2309 } 2310 return nullptr; 2311 } 2312 2313 /// Test whether the given sequence of *normalized* indices is "inbounds". 2314 template<typename IndexTy> 2315 static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) { 2316 // No indices means nothing that could be out of bounds. 2317 if (Idxs.empty()) return true; 2318 2319 // If the first index is zero, it's in bounds. 2320 if (cast<Constant>(Idxs[0])->isNullValue()) return true; 2321 2322 // If the first index is one and all the rest are zero, it's in bounds, 2323 // by the one-past-the-end rule. 2324 if (auto *CI = dyn_cast<ConstantInt>(Idxs[0])) { 2325 if (!CI->isOne()) 2326 return false; 2327 } else { 2328 auto *CV = cast<ConstantDataVector>(Idxs[0]); 2329 CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()); 2330 if (!CI || !CI->isOne()) 2331 return false; 2332 } 2333 2334 for (unsigned i = 1, e = Idxs.size(); i != e; ++i) 2335 if (!cast<Constant>(Idxs[i])->isNullValue()) 2336 return false; 2337 return true; 2338 } 2339 2340 /// Test whether a given ConstantInt is in-range for a SequentialType. 2341 static bool isIndexInRangeOfArrayType(uint64_t NumElements, 2342 const ConstantInt *CI) { 2343 // We cannot bounds check the index if it doesn't fit in an int64_t. 2344 if (CI->getValue().getMinSignedBits() > 64) 2345 return false; 2346 2347 // A negative index or an index past the end of our sequential type is 2348 // considered out-of-range. 2349 int64_t IndexVal = CI->getSExtValue(); 2350 if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements)) 2351 return false; 2352 2353 // Otherwise, it is in-range. 2354 return true; 2355 } 2356 2357 Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, 2358 bool InBounds, 2359 Optional<unsigned> InRangeIndex, 2360 ArrayRef<Value *> Idxs) { 2361 if (Idxs.empty()) return C; 2362 2363 Type *GEPTy = GetElementPtrInst::getGEPReturnType( 2364 PointeeTy, C, makeArrayRef((Value *const *)Idxs.data(), Idxs.size())); 2365 2366 if (isa<PoisonValue>(C)) 2367 return PoisonValue::get(GEPTy); 2368 2369 if (isa<UndefValue>(C)) 2370 // If inbounds, we can choose an out-of-bounds pointer as a base pointer. 2371 return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy); 2372 2373 Constant *Idx0 = cast<Constant>(Idxs[0]); 2374 if (Idxs.size() == 1 && (Idx0->isNullValue() || isa<UndefValue>(Idx0))) 2375 return GEPTy->isVectorTy() && !C->getType()->isVectorTy() 2376 ? ConstantVector::getSplat( 2377 cast<VectorType>(GEPTy)->getElementCount(), C) 2378 : C; 2379 2380 if (C->isNullValue()) { 2381 bool isNull = true; 2382 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) 2383 if (!isa<UndefValue>(Idxs[i]) && 2384 !cast<Constant>(Idxs[i])->isNullValue()) { 2385 isNull = false; 2386 break; 2387 } 2388 if (isNull) { 2389 PointerType *PtrTy = cast<PointerType>(C->getType()->getScalarType()); 2390 Type *Ty = GetElementPtrInst::getIndexedType(PointeeTy, Idxs); 2391 2392 assert(Ty && "Invalid indices for GEP!"); 2393 Type *OrigGEPTy = PointerType::get(Ty, PtrTy->getAddressSpace()); 2394 Type *GEPTy = PointerType::get(Ty, PtrTy->getAddressSpace()); 2395 if (VectorType *VT = dyn_cast<VectorType>(C->getType())) 2396 GEPTy = VectorType::get(OrigGEPTy, VT->getElementCount()); 2397 2398 // The GEP returns a vector of pointers when one of more of 2399 // its arguments is a vector. 2400 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) { 2401 if (auto *VT = dyn_cast<VectorType>(Idxs[i]->getType())) { 2402 assert((!isa<VectorType>(GEPTy) || isa<ScalableVectorType>(GEPTy) == 2403 isa<ScalableVectorType>(VT)) && 2404 "Mismatched GEPTy vector types"); 2405 GEPTy = VectorType::get(OrigGEPTy, VT->getElementCount()); 2406 break; 2407 } 2408 } 2409 2410 return Constant::getNullValue(GEPTy); 2411 } 2412 } 2413 2414 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 2415 // Combine Indices - If the source pointer to this getelementptr instruction 2416 // is a getelementptr instruction, combine the indices of the two 2417 // getelementptr instructions into a single instruction. 2418 // 2419 if (CE->getOpcode() == Instruction::GetElementPtr) { 2420 gep_type_iterator LastI = gep_type_end(CE); 2421 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); 2422 I != E; ++I) 2423 LastI = I; 2424 2425 // We cannot combine indices if doing so would take us outside of an 2426 // array or vector. Doing otherwise could trick us if we evaluated such a 2427 // GEP as part of a load. 2428 // 2429 // e.g. Consider if the original GEP was: 2430 // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c, 2431 // i32 0, i32 0, i64 0) 2432 // 2433 // If we then tried to offset it by '8' to get to the third element, 2434 // an i8, we should *not* get: 2435 // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c, 2436 // i32 0, i32 0, i64 8) 2437 // 2438 // This GEP tries to index array element '8 which runs out-of-bounds. 2439 // Subsequent evaluation would get confused and produce erroneous results. 2440 // 2441 // The following prohibits such a GEP from being formed by checking to see 2442 // if the index is in-range with respect to an array. 2443 // TODO: This code may be extended to handle vectors as well. 2444 bool PerformFold = false; 2445 if (Idx0->isNullValue()) 2446 PerformFold = true; 2447 else if (LastI.isSequential()) 2448 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx0)) 2449 PerformFold = (!LastI.isBoundedSequential() || 2450 isIndexInRangeOfArrayType( 2451 LastI.getSequentialNumElements(), CI)) && 2452 !CE->getOperand(CE->getNumOperands() - 1) 2453 ->getType() 2454 ->isVectorTy(); 2455 2456 if (PerformFold) { 2457 SmallVector<Value*, 16> NewIndices; 2458 NewIndices.reserve(Idxs.size() + CE->getNumOperands()); 2459 NewIndices.append(CE->op_begin() + 1, CE->op_end() - 1); 2460 2461 // Add the last index of the source with the first index of the new GEP. 2462 // Make sure to handle the case when they are actually different types. 2463 Constant *Combined = CE->getOperand(CE->getNumOperands()-1); 2464 // Otherwise it must be an array. 2465 if (!Idx0->isNullValue()) { 2466 Type *IdxTy = Combined->getType(); 2467 if (IdxTy != Idx0->getType()) { 2468 unsigned CommonExtendedWidth = 2469 std::max(IdxTy->getIntegerBitWidth(), 2470 Idx0->getType()->getIntegerBitWidth()); 2471 CommonExtendedWidth = std::max(CommonExtendedWidth, 64U); 2472 2473 Type *CommonTy = 2474 Type::getIntNTy(IdxTy->getContext(), CommonExtendedWidth); 2475 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, CommonTy); 2476 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, CommonTy); 2477 Combined = ConstantExpr::get(Instruction::Add, C1, C2); 2478 } else { 2479 Combined = 2480 ConstantExpr::get(Instruction::Add, Idx0, Combined); 2481 } 2482 } 2483 2484 NewIndices.push_back(Combined); 2485 NewIndices.append(Idxs.begin() + 1, Idxs.end()); 2486 2487 // The combined GEP normally inherits its index inrange attribute from 2488 // the inner GEP, but if the inner GEP's last index was adjusted by the 2489 // outer GEP, any inbounds attribute on that index is invalidated. 2490 Optional<unsigned> IRIndex = cast<GEPOperator>(CE)->getInRangeIndex(); 2491 if (IRIndex && *IRIndex == CE->getNumOperands() - 2 && !Idx0->isNullValue()) 2492 IRIndex = None; 2493 2494 return ConstantExpr::getGetElementPtr( 2495 cast<GEPOperator>(CE)->getSourceElementType(), CE->getOperand(0), 2496 NewIndices, InBounds && cast<GEPOperator>(CE)->isInBounds(), 2497 IRIndex); 2498 } 2499 } 2500 2501 // Attempt to fold casts to the same type away. For example, folding: 2502 // 2503 // i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*), 2504 // i64 0, i64 0) 2505 // into: 2506 // 2507 // i32* getelementptr ([3 x i32]* %X, i64 0, i64 0) 2508 // 2509 // Don't fold if the cast is changing address spaces. 2510 if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) { 2511 PointerType *SrcPtrTy = 2512 dyn_cast<PointerType>(CE->getOperand(0)->getType()); 2513 PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType()); 2514 if (SrcPtrTy && DstPtrTy) { 2515 ArrayType *SrcArrayTy = 2516 dyn_cast<ArrayType>(SrcPtrTy->getElementType()); 2517 ArrayType *DstArrayTy = 2518 dyn_cast<ArrayType>(DstPtrTy->getElementType()); 2519 if (SrcArrayTy && DstArrayTy 2520 && SrcArrayTy->getElementType() == DstArrayTy->getElementType() 2521 && SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) 2522 return ConstantExpr::getGetElementPtr(SrcArrayTy, 2523 (Constant *)CE->getOperand(0), 2524 Idxs, InBounds, InRangeIndex); 2525 } 2526 } 2527 } 2528 2529 // Check to see if any array indices are not within the corresponding 2530 // notional array or vector bounds. If so, try to determine if they can be 2531 // factored out into preceding dimensions. 2532 SmallVector<Constant *, 8> NewIdxs; 2533 Type *Ty = PointeeTy; 2534 Type *Prev = C->getType(); 2535 auto GEPIter = gep_type_begin(PointeeTy, Idxs); 2536 bool Unknown = 2537 !isa<ConstantInt>(Idxs[0]) && !isa<ConstantDataVector>(Idxs[0]); 2538 for (unsigned i = 1, e = Idxs.size(); i != e; 2539 Prev = Ty, Ty = (++GEPIter).getIndexedType(), ++i) { 2540 if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) { 2541 // We don't know if it's in range or not. 2542 Unknown = true; 2543 continue; 2544 } 2545 if (!isa<ConstantInt>(Idxs[i - 1]) && !isa<ConstantDataVector>(Idxs[i - 1])) 2546 // Skip if the type of the previous index is not supported. 2547 continue; 2548 if (InRangeIndex && i == *InRangeIndex + 1) { 2549 // If an index is marked inrange, we cannot apply this canonicalization to 2550 // the following index, as that will cause the inrange index to point to 2551 // the wrong element. 2552 continue; 2553 } 2554 if (isa<StructType>(Ty)) { 2555 // The verify makes sure that GEPs into a struct are in range. 2556 continue; 2557 } 2558 if (isa<VectorType>(Ty)) { 2559 // There can be awkward padding in after a non-power of two vector. 2560 Unknown = true; 2561 continue; 2562 } 2563 auto *STy = cast<ArrayType>(Ty); 2564 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) { 2565 if (isIndexInRangeOfArrayType(STy->getNumElements(), CI)) 2566 // It's in range, skip to the next index. 2567 continue; 2568 if (CI->getSExtValue() < 0) { 2569 // It's out of range and negative, don't try to factor it. 2570 Unknown = true; 2571 continue; 2572 } 2573 } else { 2574 auto *CV = cast<ConstantDataVector>(Idxs[i]); 2575 bool InRange = true; 2576 for (unsigned I = 0, E = CV->getNumElements(); I != E; ++I) { 2577 auto *CI = cast<ConstantInt>(CV->getElementAsConstant(I)); 2578 InRange &= isIndexInRangeOfArrayType(STy->getNumElements(), CI); 2579 if (CI->getSExtValue() < 0) { 2580 Unknown = true; 2581 break; 2582 } 2583 } 2584 if (InRange || Unknown) 2585 // It's in range, skip to the next index. 2586 // It's out of range and negative, don't try to factor it. 2587 continue; 2588 } 2589 if (isa<StructType>(Prev)) { 2590 // It's out of range, but the prior dimension is a struct 2591 // so we can't do anything about it. 2592 Unknown = true; 2593 continue; 2594 } 2595 // It's out of range, but we can factor it into the prior 2596 // dimension. 2597 NewIdxs.resize(Idxs.size()); 2598 // Determine the number of elements in our sequential type. 2599 uint64_t NumElements = STy->getArrayNumElements(); 2600 2601 // Expand the current index or the previous index to a vector from a scalar 2602 // if necessary. 2603 Constant *CurrIdx = cast<Constant>(Idxs[i]); 2604 auto *PrevIdx = 2605 NewIdxs[i - 1] ? NewIdxs[i - 1] : cast<Constant>(Idxs[i - 1]); 2606 bool IsCurrIdxVector = CurrIdx->getType()->isVectorTy(); 2607 bool IsPrevIdxVector = PrevIdx->getType()->isVectorTy(); 2608 bool UseVector = IsCurrIdxVector || IsPrevIdxVector; 2609 2610 if (!IsCurrIdxVector && IsPrevIdxVector) 2611 CurrIdx = ConstantDataVector::getSplat( 2612 cast<FixedVectorType>(PrevIdx->getType())->getNumElements(), CurrIdx); 2613 2614 if (!IsPrevIdxVector && IsCurrIdxVector) 2615 PrevIdx = ConstantDataVector::getSplat( 2616 cast<FixedVectorType>(CurrIdx->getType())->getNumElements(), PrevIdx); 2617 2618 Constant *Factor = 2619 ConstantInt::get(CurrIdx->getType()->getScalarType(), NumElements); 2620 if (UseVector) 2621 Factor = ConstantDataVector::getSplat( 2622 IsPrevIdxVector 2623 ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements() 2624 : cast<FixedVectorType>(CurrIdx->getType())->getNumElements(), 2625 Factor); 2626 2627 NewIdxs[i] = ConstantExpr::getSRem(CurrIdx, Factor); 2628 2629 Constant *Div = ConstantExpr::getSDiv(CurrIdx, Factor); 2630 2631 unsigned CommonExtendedWidth = 2632 std::max(PrevIdx->getType()->getScalarSizeInBits(), 2633 Div->getType()->getScalarSizeInBits()); 2634 CommonExtendedWidth = std::max(CommonExtendedWidth, 64U); 2635 2636 // Before adding, extend both operands to i64 to avoid 2637 // overflow trouble. 2638 Type *ExtendedTy = Type::getIntNTy(Div->getContext(), CommonExtendedWidth); 2639 if (UseVector) 2640 ExtendedTy = FixedVectorType::get( 2641 ExtendedTy, 2642 IsPrevIdxVector 2643 ? cast<FixedVectorType>(PrevIdx->getType())->getNumElements() 2644 : cast<FixedVectorType>(CurrIdx->getType())->getNumElements()); 2645 2646 if (!PrevIdx->getType()->isIntOrIntVectorTy(CommonExtendedWidth)) 2647 PrevIdx = ConstantExpr::getSExt(PrevIdx, ExtendedTy); 2648 2649 if (!Div->getType()->isIntOrIntVectorTy(CommonExtendedWidth)) 2650 Div = ConstantExpr::getSExt(Div, ExtendedTy); 2651 2652 NewIdxs[i - 1] = ConstantExpr::getAdd(PrevIdx, Div); 2653 } 2654 2655 // If we did any factoring, start over with the adjusted indices. 2656 if (!NewIdxs.empty()) { 2657 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) 2658 if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]); 2659 return ConstantExpr::getGetElementPtr(PointeeTy, C, NewIdxs, InBounds, 2660 InRangeIndex); 2661 } 2662 2663 // If all indices are known integers and normalized, we can do a simple 2664 // check for the "inbounds" property. 2665 if (!Unknown && !InBounds) 2666 if (auto *GV = dyn_cast<GlobalVariable>(C)) 2667 if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs)) 2668 return ConstantExpr::getGetElementPtr(PointeeTy, C, Idxs, 2669 /*InBounds=*/true, InRangeIndex); 2670 2671 return nullptr; 2672 } 2673