return Constant::getAllOnesValue(DestTy);
// Handle a vector->integer cast.
- if (IntegerType *IT = dyn_cast<IntegerType>(DestTy)) {
- VectorType *VTy = dyn_cast<VectorType>(C->getType());
+ if (auto *IT = dyn_cast<IntegerType>(DestTy)) {
+ auto *VTy = dyn_cast<VectorType>(C->getType());
if (!VTy)
return ConstantExpr::getBitCast(C, DestTy);
}
// The code below only handles casts to vectors currently.
- VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
+ auto *DestVTy = dyn_cast<VectorType>(DestTy);
if (!DestVTy)
return ConstantExpr::getBitCast(C, DestTy);
Constant *Elt = Zero;
unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
for (unsigned j = 0; j != Ratio; ++j) {
- Constant *Src =dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++));
+ Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(SrcElt++));
if (!Src) // Reject constantexpr elements.
return ConstantExpr::getBitCast(C, DestTy);
// Loop over each source value, expanding into multiple results.
for (unsigned i = 0; i != NumSrcElt; ++i) {
- Constant *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i));
+ auto *Src = dyn_cast<ConstantInt>(C->getAggregateElement(i));
if (!Src) // Reject constantexpr elements.
return ConstantExpr::getBitCast(C, DestTy);
}
// Otherwise, if this isn't a constant expr, bail out.
- ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
+ auto *CE = dyn_cast<ConstantExpr>(C);
if (!CE) return false;
// Look through ptr->int and ptr->ptr casts.
return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, DL);
// i32* getelementptr ([5 x i32]* @a, i32 0, i32 5)
- GEPOperator *GEP = dyn_cast<GEPOperator>(CE);
+ auto *GEP = dyn_cast<GEPOperator>(CE);
if (!GEP)
return false;
if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C))
return true;
- if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
+ if (auto *CI = dyn_cast<ConstantInt>(C)) {
if (CI->getBitWidth() > 64 ||
(CI->getBitWidth() & 7) != 0)
return false;
return true;
}
- if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
+ if (auto *CFP = dyn_cast<ConstantFP>(C)) {
if (CFP->getType()->isDoubleTy()) {
C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), DL);
return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, DL);
return false;
}
- if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) {
+ if (auto *CS = dyn_cast<ConstantStruct>(C)) {
const StructLayout *SL = DL.getStructLayout(CS->getType());
unsigned Index = SL->getElementContainingOffset(ByteOffset);
uint64_t CurEltOffset = SL->getElementOffset(Index);
uint64_t Index = ByteOffset / EltSize;
uint64_t Offset = ByteOffset - Index * EltSize;
uint64_t NumElts;
- if (ArrayType *AT = dyn_cast<ArrayType>(C->getType()))
+ if (auto *AT = dyn_cast<ArrayType>(C->getType()))
NumElts = AT->getNumElements();
else
NumElts = C->getType()->getVectorNumElements();
return true;
}
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+ if (auto *CE = dyn_cast<ConstantExpr>(C)) {
if (CE->getOpcode() == Instruction::IntToPtr &&
CE->getOperand(0)->getType() == DL.getIntPtrType(CE->getType())) {
return ReadDataFromGlobal(CE->getOperand(0), ByteOffset, CurPtr,
Constant *FoldReinterpretLoadFromConstPtr(Constant *C, Type *LoadTy,
const DataLayout &DL) {
- PointerType *PTy = cast<PointerType>(C->getType());
- IntegerType *IntType = dyn_cast<IntegerType>(LoadTy);
+ auto *PTy = cast<PointerType>(C->getType());
+ auto *IntType = dyn_cast<IntegerType>(LoadTy);
// If this isn't an integer load we can't fold it directly.
if (!IntType) {
if (!IsConstantOffsetFromGlobal(C, GVal, Offset, DL))
return nullptr;
- GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal);
+ auto *GV = dyn_cast<GlobalVariable>(GVal);
if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
!GV->getInitializer()->getType()->isSized())
return nullptr;
Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
const DataLayout &DL) {
// First, try the easy cases:
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
+ if (auto *GV = dyn_cast<GlobalVariable>(C))
if (GV->isConstant() && GV->hasDefinitiveInitializer())
return GV->getInitializer();
return ConstantFoldLoadFromConstPtr(GA->getAliasee(), Ty, DL);
// If the loaded value isn't a constant expr, we can't handle it.
- ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
+ auto *CE = dyn_cast<ConstantExpr>(C);
if (!CE)
return nullptr;
if (CE->getOpcode() == Instruction::GetElementPtr) {
- if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
+ if (auto *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) {
if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
if (Constant *V =
ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
// If this load comes from anywhere in a constant global, and if the global
// is all undef or zero, we know what it loads.
- if (GlobalVariable *GV =
- dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) {
+ if (auto *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(CE, DL))) {
if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
if (GV->getInitializer()->isNullValue())
return Constant::getNullValue(Ty);
Constant *ConstantFoldLoadInst(const LoadInst *LI, const DataLayout &DL) {
if (LI->isVolatile()) return nullptr;
- if (Constant *C = dyn_cast<Constant>(LI->getOperand(0)))
+ if (auto *C = dyn_cast<Constant>(LI->getOperand(0)))
return ConstantFoldLoadFromConstPtr(C, LI->getType(), DL);
return nullptr;
return nullptr;
Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ops[0], NewIdxs);
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+ if (auto *CE = dyn_cast<ConstantExpr>(C)) {
if (Constant *Folded = ConstantFoldConstantExpression(CE, DL, TLI))
C = Folded;
}
/// Strip the pointer casts, but preserve the address space information.
Constant* StripPtrCastKeepAS(Constant* Ptr, Type *&ElemTy) {
assert(Ptr->getType()->isPointerTy() && "Not a pointer type");
- PointerType *OldPtrTy = cast<PointerType>(Ptr->getType());
+ auto *OldPtrTy = cast<PointerType>(Ptr->getType());
Ptr = Ptr->stripPointerCasts();
- PointerType *NewPtrTy = cast<PointerType>(Ptr->getType());
+ auto *NewPtrTy = cast<PointerType>(Ptr->getType());
ElemTy = NewPtrTy->getPointerElementType();
// If this is "gep i8* Ptr, (sub 0, V)", fold this as:
// "inttoptr (sub (ptrtoint Ptr), V)"
if (Ops.size() == 2 && ResElemTy->isIntegerTy(8)) {
- ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[1]);
+ auto *CE = dyn_cast<ConstantExpr>(Ops[1]);
assert((!CE || CE->getType() == IntPtrTy) &&
"CastGEPIndices didn't canonicalize index types!");
if (CE && CE->getOpcode() == Instruction::Sub &&
Constant *Res = ConstantExpr::getPtrToInt(Ptr, CE->getType());
Res = ConstantExpr::getSub(Res, CE->getOperand(1));
Res = ConstantExpr::getIntToPtr(Res, ResTy);
- if (ConstantExpr *ResCE = dyn_cast<ConstantExpr>(Res))
+ if (auto *ResCE = dyn_cast<ConstantExpr>(Res))
Res = ConstantFoldConstantExpression(ResCE, DL, TLI);
return Res;
}
Ptr = StripPtrCastKeepAS(Ptr, SrcElemTy);
// If this is a GEP of a GEP, fold it all into a single GEP.
- while (GEPOperator *GEP = dyn_cast<GEPOperator>(Ptr)) {
+ while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
// Do not try the incorporate the sub-GEP if some index is not a number.
bool AllConstantInt = true;
- for (unsigned i = 0, e = NestedOps.size(); i != e; ++i)
- if (!isa<ConstantInt>(NestedOps[i])) {
+ for (Value *NestedOp : NestedOps)
+ if (!isa<ConstantInt>(NestedOp)) {
AllConstantInt = false;
break;
}
// If the base value for this address is a literal integer value, fold the
// getelementptr to the resulting integer value casted to the pointer type.
APInt BasePtr(BitWidth, 0);
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
+ if (auto *CE = dyn_cast<ConstantExpr>(Ptr)) {
if (CE->getOpcode() == Instruction::IntToPtr) {
- if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
+ if (auto *Base = dyn_cast<ConstantInt>(CE->getOperand(0)))
BasePtr = Base->getValue().zextOrTrunc(BitWidth);
}
}
NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
}
} else {
- StructType *STy = cast<StructType>(Ty);
+ auto *STy = cast<StructType>(Ty);
// If we end up with an offset that isn't valid for this struct type, we
// can't re-form this GEP in a regular form, so bail out. The pointer
// operand likely went through casts that are necessary to make the GEP
case Instruction::ICmp:
case Instruction::FCmp: llvm_unreachable("Invalid for compares");
case Instruction::Call:
- if (Function *F = dyn_cast<Function>(Ops.back()))
+ if (auto *F = dyn_cast<Function>(Ops.back()))
if (canConstantFoldCallTo(F))
return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
return nullptr;
Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
const TargetLibraryInfo *TLI) {
// Handle PHI nodes quickly here...
- if (PHINode *PN = dyn_cast<PHINode>(I)) {
+ if (auto *PN = dyn_cast<PHINode>(I)) {
Constant *CommonValue = nullptr;
for (Value *Incoming : PN->incoming_values()) {
if (isa<UndefValue>(Incoming))
continue;
// If the incoming value is not a constant, then give up.
- Constant *C = dyn_cast<Constant>(Incoming);
+ auto *C = dyn_cast<Constant>(Incoming);
if (!C)
return nullptr;
// Fold the PHI's operands.
- if (ConstantExpr *NewC = dyn_cast<ConstantExpr>(C))
+ if (auto *NewC = dyn_cast<ConstantExpr>(C))
C = ConstantFoldConstantExpression(NewC, DL, TLI);
// If the incoming value is a different constant to
// the one we saw previously, then give up.
return nullptr;
SmallVector<Constant *, 8> Ops;
- for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) {
- Constant *Op = cast<Constant>(*i);
+ for (const Use &OpU : I->operands()) {
+ auto *Op = cast<Constant>(&OpU);
// Fold the Instruction's operands.
- if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(Op))
+ if (auto *NewCE = dyn_cast<ConstantExpr>(Op))
Op = ConstantFoldConstantExpression(NewCE, DL, TLI);
Ops.push_back(Op);
}
- if (const CmpInst *CI = dyn_cast<CmpInst>(I))
+ if (const auto *CI = dyn_cast<CmpInst>(I))
return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
DL, TLI);
- if (const LoadInst *LI = dyn_cast<LoadInst>(I))
+ if (const auto *LI = dyn_cast<LoadInst>(I))
return ConstantFoldLoadInst(LI, DL);
- if (InsertValueInst *IVI = dyn_cast<InsertValueInst>(I)) {
+ if (auto *IVI = dyn_cast<InsertValueInst>(I)) {
return ConstantExpr::getInsertValue(
cast<Constant>(IVI->getAggregateOperand()),
cast<Constant>(IVI->getInsertedValueOperand()),
IVI->getIndices());
}
- if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(I)) {
+ if (auto *EVI = dyn_cast<ExtractValueInst>(I)) {
return ConstantExpr::getExtractValue(
cast<Constant>(EVI->getAggregateOperand()),
EVI->getIndices());
const TargetLibraryInfo *TLI,
SmallPtrSetImpl<ConstantExpr *> &FoldedOps) {
SmallVector<Constant *, 8> Ops;
- for (User::const_op_iterator i = CE->op_begin(), e = CE->op_end(); i != e;
- ++i) {
- Constant *NewC = cast<Constant>(*i);
+ for (const Use &NewU : CE->operands()) {
+ auto *NewC = cast<Constant>(&NewU);
// Recursively fold the ConstantExpr's operands. If we have already folded
// a ConstantExpr, we don't have to process it again.
- if (ConstantExpr *NewCE = dyn_cast<ConstantExpr>(NewC)) {
+ if (auto *NewCE = dyn_cast<ConstantExpr>(NewC)) {
if (FoldedOps.insert(NewCE).second)
NewC = ConstantFoldConstantExpressionImpl(NewCE, DL, TLI, FoldedOps);
}
// FIXME: The following comment is out of data and the DataLayout is here now.
// ConstantExpr::getCompare cannot do this, because it doesn't have DL
// around to know if bit truncation is happening.
- if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
+ if (auto *CE0 = dyn_cast<ConstantExpr>(Ops0)) {
if (Ops1->isNullValue()) {
if (CE0->getOpcode() == Instruction::IntToPtr) {
Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
}
}
- if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
+ if (auto *CE1 = dyn_cast<ConstantExpr>(Ops1)) {
if (CE0->getOpcode() == CE1->getOpcode()) {
if (CE0->getOpcode() == Instruction::IntToPtr) {
Type *IntPtrTy = DL.getIntPtrType(CE0->getType());
case Instruction::PtrToInt:
// If the input is a inttoptr, eliminate the pair. This requires knowing
// the width of a pointer, so it can't be done in ConstantExpr::getCast.
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+ if (auto *CE = dyn_cast<ConstantExpr>(C)) {
if (CE->getOpcode() == Instruction::IntToPtr) {
Constant *Input = CE->getOperand(0);
unsigned InWidth = Input->getType()->getScalarSizeInBits();
// the int size is >= the ptr size and the address spaces are the same.
// This requires knowing the width of a pointer, so it can't be done in
// ConstantExpr::getCast.
- if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+ if (auto *CE = dyn_cast<ConstantExpr>(C)) {
if (CE->getOpcode() == Instruction::PtrToInt) {
Constant *SrcPtr = CE->getOperand(0);
unsigned SrcPtrSize = DL.getPointerTypeSizeInBits(SrcPtr->getType());
return C;
}
-Constant *llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
- ArrayRef<Constant*> Indices) {
+Constant *
+llvm::ConstantFoldLoadThroughGEPIndices(Constant *C,
+ ArrayRef<Constant *> Indices) {
// Loop over all of the operands, tracking down which value we are
// addressing.
- for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
- C = C->getAggregateElement(Indices[i]);
+ for (Constant *Index : Indices) {
+ C = C->getAggregateElement(Index);
if (!C)
return nullptr;
}
if (IntrinsicID == Intrinsic::cos)
return Constant::getNullValue(Ty);
}
- if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) {
+ if (auto *Op = dyn_cast<ConstantFP>(Operands[0])) {
if (IntrinsicID == Intrinsic::convert_to_fp16) {
APFloat Val(Op->getValueAPF());
return nullptr;
}
- if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) {
+ if (auto *Op = dyn_cast<ConstantInt>(Operands[0])) {
switch (IntrinsicID) {
case Intrinsic::bswap:
return ConstantInt::get(Ty->getContext(), Op->getValue().byteSwap());
// Support ConstantVector in case we have an Undef in the top.
if (isa<ConstantVector>(Operands[0]) ||
isa<ConstantDataVector>(Operands[0])) {
- Constant *Op = cast<Constant>(Operands[0]);
+ auto *Op = cast<Constant>(Operands[0]);
switch (IntrinsicID) {
default: break;
case Intrinsic::x86_sse_cvtss2si:
}
if (Operands.size() == 2) {
- if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
+ if (auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
return nullptr;
double Op1V = getValueAsDouble(Op1);
- if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
+ if (auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
if (Op2->getType() != Op1->getType())
return nullptr;
if ((Name == "atan2" && TLI->has(LibFunc::atan2)) ||
(Name == "atan2f" && TLI->has(LibFunc::atan2f)))
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
- } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
+ } else if (auto *Op2C = dyn_cast<ConstantInt>(Operands[1])) {
if (IntrinsicID == Intrinsic::powi && Ty->isHalfTy())
return ConstantFP::get(Ty->getContext(),
APFloat((float)std::pow((float)Op1V,
return nullptr;
}
- if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
- if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
+ if (auto *Op1 = dyn_cast<ConstantInt>(Operands[0])) {
+ if (auto *Op2 = dyn_cast<ConstantInt>(Operands[1])) {
switch (IntrinsicID) {
default: break;
case Intrinsic::sadd_with_overflow:
if (Operands.size() != 3)
return nullptr;
- if (const ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
- if (const ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
- if (const ConstantFP *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
+ if (const auto *Op1 = dyn_cast<ConstantFP>(Operands[0])) {
+ if (const auto *Op2 = dyn_cast<ConstantFP>(Operands[1])) {
+ if (const auto *Op3 = dyn_cast<ConstantFP>(Operands[2])) {
switch (IntrinsicID) {
default: break;
case Intrinsic::fma:
Type *Ty = F->getReturnType();
- if (VectorType *VTy = dyn_cast<VectorType>(Ty))
+ if (auto *VTy = dyn_cast<VectorType>(Ty))
return ConstantFoldVectorCall(Name, F->getIntrinsicID(), VTy, Operands, TLI);
return ConstantFoldScalarCall(Name, F->getIntrinsicID(), Ty, Operands, TLI);