brintos

brintos / llvm-project-archived public Read only

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