refactor FoldBitCast to reduce nesting and to always return a constantexpr

instead of returning null on failure.  No functionality change.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85038 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2009-10-25 06:02:57 +00:00
parent 4a134afaef
commit ea9d57bc9a

View File

@@ -485,17 +485,24 @@ static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps,
}
/// FoldBitCast - Constant fold bitcast, symbolically evaluating it with
/// targetdata. Return 0 if unfoldable.
/// TargetData. This always returns a non-null constant, but it may be a
/// ConstantExpr if unfoldable.
static Constant *FoldBitCast(Constant *C, const Type *DestTy,
const TargetData &TD, LLVMContext &Context) {
// If this is a bitcast from constant vector -> vector, fold it.
if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) {
if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) {
ConstantVector *CV = dyn_cast<ConstantVector>(C);
if (CV == 0)
return ConstantExpr::getBitCast(C, DestTy);
const VectorType *DestVTy = dyn_cast<VectorType>(DestTy);
if (DestVTy == 0)
return ConstantExpr::getBitCast(C, DestTy);
// If the element types match, VMCore can fold it.
unsigned NumDstElt = DestVTy->getNumElements();
unsigned NumSrcElt = CV->getNumOperands();
if (NumDstElt == NumSrcElt)
return 0;
return ConstantExpr::getBitCast(C, DestTy);
const Type *SrcEltTy = CV->getType()->getElementType();
const Type *DstEltTy = DestVTy->getElementType();
@@ -517,7 +524,7 @@ static Constant *FoldBitCast(Constant *C, const Type *DestTy,
IntegerType::get(Context, FPWidth), NumDstElt);
// Recursively handle this integer conversion, if possible.
C = FoldBitCast(C, DestIVTy, TD, Context);
if (!C) return 0;
if (!C) return ConstantExpr::getBitCast(C, DestTy);
// Finally, VMCore can handle this now that #elts line up.
return ConstantExpr::getBitCast(C, DestTy);
@@ -532,7 +539,8 @@ static Constant *FoldBitCast(Constant *C, const Type *DestTy,
// Ask VMCore to do the conversion now that #elts line up.
C = ConstantExpr::getBitCast(C, SrcIVTy);
CV = dyn_cast<ConstantVector>(C);
if (!CV) return 0; // If VMCore wasn't able to fold it, bail out.
if (!CV) // If VMCore wasn't able to fold it, bail out.
return C;
}
// Now we know that the input and output vectors are both integer vectors
@@ -554,7 +562,8 @@ static Constant *FoldBitCast(Constant *C, const Type *DestTy,
unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1);
for (unsigned j = 0; j != Ratio; ++j) {
Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(SrcElt++));
if (!Src) return 0; // Reject constantexpr elements.
if (!Src) // Reject constantexpr elements.
return ConstantExpr::getBitCast(C, DestTy);
// Zero extend the element to the right size.
Src = ConstantExpr::getZExt(Src, Elt->getType());
@@ -577,7 +586,8 @@ static Constant *FoldBitCast(Constant *C, const Type *DestTy,
// Loop over each source value, expanding into multiple results.
for (unsigned i = 0; i != NumSrcElt; ++i) {
Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(i));
if (!Src) return 0; // Reject constantexpr elements.
if (!Src) // Reject constantexpr elements.
return ConstantExpr::getBitCast(C, DestTy);
unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1);
for (unsigned j = 0; j != Ratio; ++j) {
@@ -594,10 +604,6 @@ static Constant *FoldBitCast(Constant *C, const Type *DestTy,
}
return ConstantVector::get(Result.data(), Result.size());
}
}
return 0;
}
@@ -774,8 +780,7 @@ Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy,
return ConstantExpr::getCast(Opcode, Ops[0], DestTy);
case Instruction::BitCast:
if (TD)
if (Constant *C = FoldBitCast(Ops[0], DestTy, *TD, Context))
return C;
return FoldBitCast(Ops[0], DestTy, *TD, Context);
return ConstantExpr::getBitCast(Ops[0], DestTy);
case Instruction::Select:
return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);