#include "ConstantFold.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/GlobalAlias.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MathExtras.h"
#include <limits>
using namespace llvm;
static Constant *BitCastConstantVector(ConstantVector *CV,
const VectorType *DstTy) {
unsigned NumElts = DstTy->getNumElements();
if (NumElts != CV->getNumOperands())
return 0;
for (unsigned i = 0; i != NumElts; ++i) {
if (!isa<ConstantInt>(CV->getOperand(i)) &&
!isa<ConstantFP>(CV->getOperand(i)))
return 0;
}
std::vector<Constant*> Result;
const Type *DstEltTy = DstTy->getElementType();
for (unsigned i = 0; i != NumElts; ++i)
Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i), DstEltTy));
return ConstantVector::get(Result);
}
static unsigned
foldConstantCastPair(
unsigned opc, const ConstantExpr*Op, const Type *DstTy ) {
assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
assert(CastInst::isCast(opc) && "Invalid cast opcode");
const Type *SrcTy = Op->getOperand(0)->getType();
const Type *MidTy = Op->getType();
Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
Instruction::CastOps secondOp = Instruction::CastOps(opc);
return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
Type::Int64Ty);
}
static Constant *FoldBitCast(Constant *V, const Type *DestTy) {
const Type *SrcTy = V->getType();
if (SrcTy == DestTy)
return V;
if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy))
if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
SmallVector<Value*, 8> IdxList;
IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
const Type *ElTy = PTy->getElementType();
while (ElTy != DPTy->getElementType()) {
if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
if (STy->getNumElements() == 0) break;
ElTy = STy->getElementType(0);
IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
} else if (const SequentialType *STy =
dyn_cast<SequentialType>(ElTy)) {
if (isa<PointerType>(ElTy)) break; ElTy = STy->getElementType();
IdxList.push_back(IdxList[0]);
} else {
break;
}
}
if (ElTy == DPTy->getElementType())
return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size());
}
if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
"Not cast between same sized vectors!");
SrcTy = NULL;
if (isa<ConstantAggregateZero>(V))
return Constant::getNullValue(DestTy);
if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
return BitCastConstantVector(CV, DestPTy);
}
if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
return ConstantExpr::getBitCast(ConstantVector::get(&V, 1), DestPTy);
}
if (isa<ConstantPointerNull>(V)) return ConstantPointerNull::get(cast<PointerType>(DestTy));
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
if (DestTy->isInteger())
return V;
if (DestTy->isFloatingPoint())
return ConstantFP::get(APFloat(CI->getValue(),
DestTy != Type::PPC_FP128Ty));
return 0;
}
if (const ConstantFP *FP = dyn_cast<ConstantFP>(V))
return ConstantInt::get(FP->getValueAPF().bitcastToAPInt());
return 0;
}
Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
const Type *DestTy) {
if (isa<UndefValue>(V)) {
if (opc == Instruction::ZExt || opc == Instruction::SExt ||
opc == Instruction::UIToFP || opc == Instruction::SIToFP)
return Constant::getNullValue(DestTy);
return UndefValue::get(DestTy);
}
if (V->getType() == Type::PPC_FP128Ty || DestTy == Type::PPC_FP128Ty)
return 0;
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
if (CE->isCast()) {
if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
} else if (CE->getOpcode() == Instruction::GetElementPtr) {
bool isAllNull = true;
for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
if (!CE->getOperand(i)->isNullValue()) {
isAllNull = false;
break;
}
if (isAllNull)
return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
}
}
switch (opc) {
case Instruction::FPTrunc:
case Instruction::FPExt:
if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
bool ignored;
APFloat Val = FPC->getValueAPF();
Val.convert(DestTy == Type::FloatTy ? APFloat::IEEEsingle :
DestTy == Type::DoubleTy ? APFloat::IEEEdouble :
DestTy == Type::X86_FP80Ty ? APFloat::x87DoubleExtended :
DestTy == Type::FP128Ty ? APFloat::IEEEquad :
APFloat::Bogus,
APFloat::rmNearestTiesToEven, &ignored);
return ConstantFP::get(Val);
}
return 0; case Instruction::FPToUI:
case Instruction::FPToSI:
if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
const APFloat &V = FPC->getValueAPF();
bool ignored;
uint64_t x[2];
uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
(void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
APFloat::rmTowardZero, &ignored);
APInt Val(DestBitWidth, 2, x);
return ConstantInt::get(Val);
}
if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
std::vector<Constant*> res;
const VectorType *DestVecTy = cast<VectorType>(DestTy);
const Type *DstEltTy = DestVecTy->getElementType();
for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
res.push_back(ConstantExpr::getCast(opc, CV->getOperand(i), DstEltTy));
return ConstantVector::get(DestVecTy, res);
}
return 0; case Instruction::IntToPtr: if (V->isNullValue()) return ConstantPointerNull::get(cast<PointerType>(DestTy));
return 0; case Instruction::PtrToInt: if (V->isNullValue()) return ConstantInt::get(DestTy, 0);
return 0; case Instruction::UIToFP:
case Instruction::SIToFP:
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
APInt api = CI->getValue();
const uint64_t zero[] = {0, 0};
APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(),
2, zero));
(void)apf.convertFromAPInt(api,
opc==Instruction::SIToFP,
APFloat::rmNearestTiesToEven);
return ConstantFP::get(apf);
}
if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
std::vector<Constant*> res;
const VectorType *DestVecTy = cast<VectorType>(DestTy);
const Type *DstEltTy = DestVecTy->getElementType();
for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
res.push_back(ConstantExpr::getCast(opc, CV->getOperand(i), DstEltTy));
return ConstantVector::get(DestVecTy, res);
}
return 0;
case Instruction::ZExt:
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
APInt Result(CI->getValue());
Result.zext(BitWidth);
return ConstantInt::get(Result);
}
return 0;
case Instruction::SExt:
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
APInt Result(CI->getValue());
Result.sext(BitWidth);
return ConstantInt::get(Result);
}
return 0;
case Instruction::Trunc:
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
APInt Result(CI->getValue());
Result.trunc(BitWidth);
return ConstantInt::get(Result);
}
return 0;
case Instruction::BitCast:
return FoldBitCast(const_cast<Constant*>(V), DestTy);
default:
assert(!"Invalid CE CastInst opcode");
break;
}
assert(0 && "Failed to cast constant expression");
return 0;
}
Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
const Constant *V1,
const Constant *V2) {
if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
if (V1 == V2) return const_cast<Constant*>(V1);
return 0;
}
Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
const Constant *Idx) {
if (isa<UndefValue>(Val)) return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
if (Val->isNullValue()) return Constant::getNullValue(
cast<VectorType>(Val->getType())->getElementType());
if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
return CVal->getOperand(CIdx->getZExtValue());
} else if (isa<UndefValue>(Idx)) {
return CVal->getOperand(0);
}
}
return 0;
}
Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
const Constant *Elt,
const Constant *Idx) {
const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
if (!CIdx) return 0;
APInt idxVal = CIdx->getValue();
if (isa<UndefValue>(Val)) {
if (isa<UndefValue>(Elt))
return const_cast<Constant*>(Val);
unsigned numOps =
cast<VectorType>(Val->getType())->getNumElements();
std::vector<Constant*> Ops;
Ops.reserve(numOps);
for (unsigned i = 0; i < numOps; ++i) {
const Constant *Op =
(idxVal == i) ? Elt : UndefValue::get(Elt->getType());
Ops.push_back(const_cast<Constant*>(Op));
}
return ConstantVector::get(Ops);
}
if (isa<ConstantAggregateZero>(Val)) {
if (Elt->isNullValue())
return const_cast<Constant*>(Val);
unsigned numOps =
cast<VectorType>(Val->getType())->getNumElements();
std::vector<Constant*> Ops;
Ops.reserve(numOps);
for (unsigned i = 0; i < numOps; ++i) {
const Constant *Op =
(idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
Ops.push_back(const_cast<Constant*>(Op));
}
return ConstantVector::get(Ops);
}
if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
std::vector<Constant*> Ops;
Ops.reserve(CVal->getNumOperands());
for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
const Constant *Op =
(idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
Ops.push_back(const_cast<Constant*>(Op));
}
return ConstantVector::get(Ops);
}
return 0;
}
static Constant *GetVectorElement(const Constant *C, unsigned EltNo) {
if (const ConstantVector *CV = dyn_cast<ConstantVector>(C))
return CV->getOperand(EltNo);
const Type *EltTy = cast<VectorType>(C->getType())->getElementType();
if (isa<ConstantAggregateZero>(C))
return Constant::getNullValue(EltTy);
if (isa<UndefValue>(C))
return UndefValue::get(EltTy);
return 0;
}
Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
const Constant *V2,
const Constant *Mask) {
if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType());
unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements();
unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements();
const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
SmallVector<Constant*, 32> Result;
for (unsigned i = 0; i != MaskNumElts; ++i) {
Constant *InElt = GetVectorElement(Mask, i);
if (InElt == 0) return 0;
if (isa<UndefValue>(InElt))
InElt = UndefValue::get(EltTy);
else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) {
unsigned Elt = CI->getZExtValue();
if (Elt >= SrcNumElts*2)
InElt = UndefValue::get(EltTy);
else if (Elt >= SrcNumElts)
InElt = GetVectorElement(V2, Elt - SrcNumElts);
else
InElt = GetVectorElement(V1, Elt);
if (InElt == 0) return 0;
} else {
return 0;
}
Result.push_back(InElt);
}
return ConstantVector::get(&Result[0], Result.size());
}
Constant *llvm::ConstantFoldExtractValueInstruction(const Constant *Agg,
const unsigned *Idxs,
unsigned NumIdx) {
if (NumIdx == 0)
return const_cast<Constant *>(Agg);
if (isa<UndefValue>(Agg)) return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(),
Idxs,
Idxs + NumIdx));
if (isa<ConstantAggregateZero>(Agg)) return
Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
Idxs,
Idxs + NumIdx));
return ConstantFoldExtractValueInstruction(Agg->getOperand(*Idxs),
Idxs+1, NumIdx-1);
}
Constant *llvm::ConstantFoldInsertValueInstruction(const Constant *Agg,
const Constant *Val,
const unsigned *Idxs,
unsigned NumIdx) {
if (NumIdx == 0)
return const_cast<Constant *>(Val);
if (isa<UndefValue>(Agg)) {
if (isa<UndefValue>(Val))
return const_cast<Constant*>(Agg);
const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
unsigned numOps;
if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
numOps = AR->getNumElements();
else
numOps = cast<StructType>(AggTy)->getNumElements();
std::vector<Constant*> Ops(numOps);
for (unsigned i = 0; i < numOps; ++i) {
const Type *MemberTy = AggTy->getTypeAtIndex(i);
const Constant *Op =
(*Idxs == i) ?
ConstantFoldInsertValueInstruction(UndefValue::get(MemberTy),
Val, Idxs+1, NumIdx-1) :
UndefValue::get(MemberTy);
Ops[i] = const_cast<Constant*>(Op);
}
if (isa<StructType>(AggTy))
return ConstantStruct::get(Ops);
else
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
}
if (isa<ConstantAggregateZero>(Agg)) {
if (Val->isNullValue())
return const_cast<Constant*>(Agg);
const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
unsigned numOps;
if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
numOps = AR->getNumElements();
else
numOps = cast<StructType>(AggTy)->getNumElements();
std::vector<Constant*> Ops(numOps);
for (unsigned i = 0; i < numOps; ++i) {
const Type *MemberTy = AggTy->getTypeAtIndex(i);
const Constant *Op =
(*Idxs == i) ?
ConstantFoldInsertValueInstruction(Constant::getNullValue(MemberTy),
Val, Idxs+1, NumIdx-1) :
Constant::getNullValue(MemberTy);
Ops[i] = const_cast<Constant*>(Op);
}
if (isa<StructType>(AggTy))
return ConstantStruct::get(Ops);
else
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
}
if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
std::vector<Constant*> Ops(Agg->getNumOperands());
for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
const Constant *Op =
(*Idxs == i) ?
ConstantFoldInsertValueInstruction(Agg->getOperand(i),
Val, Idxs+1, NumIdx-1) :
Agg->getOperand(i);
Ops[i] = const_cast<Constant*>(Op);
}
Constant *C;
if (isa<StructType>(Agg->getType()))
C = ConstantStruct::get(Ops);
else
C = ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
return C;
}
return 0;
}
static Constant *EvalVectorOp(const ConstantVector *V1,
const ConstantVector *V2,
const VectorType *VTy,
Constant *(*FP)(Constant*, Constant*)) {
std::vector<Constant*> Res;
const Type *EltTy = VTy->getElementType();
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
const Constant *C1 = V1 ? V1->getOperand(i) : Constant::getNullValue(EltTy);
const Constant *C2 = V2 ? V2->getOperand(i) : Constant::getNullValue(EltTy);
Res.push_back(FP(const_cast<Constant*>(C1),
const_cast<Constant*>(C2)));
}
return ConstantVector::get(Res);
}
Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
const Constant *C1,
const Constant *C2) {
if (C1->getType() == Type::PPC_FP128Ty)
return 0;
if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
switch (Opcode) {
case Instruction::Xor:
if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
return Constant::getNullValue(C1->getType());
case Instruction::Add:
case Instruction::Sub:
return UndefValue::get(C1->getType());
case Instruction::Mul:
case Instruction::And:
return Constant::getNullValue(C1->getType());
case Instruction::UDiv:
case Instruction::SDiv:
case Instruction::FDiv:
case Instruction::URem:
case Instruction::SRem:
case Instruction::FRem:
if (!isa<UndefValue>(C2)) return Constant::getNullValue(C1->getType());
return const_cast<Constant*>(C2); case Instruction::Or: if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
return ConstantVector::getAllOnesValue(PTy);
return ConstantInt::getAllOnesValue(C1->getType());
case Instruction::LShr:
if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
return const_cast<Constant*>(C1); return Constant::getNullValue(C1->getType()); case Instruction::AShr:
if (!isa<UndefValue>(C2))
return const_cast<Constant*>(C1); else if (isa<UndefValue>(C1))
return const_cast<Constant*>(C1); else
return const_cast<Constant*>(C1); case Instruction::Shl:
return Constant::getNullValue(C1->getType());
}
}
if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
switch (Opcode) {
case Instruction::Add:
if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); break;
case Instruction::Sub:
if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); break;
case Instruction::Mul:
if (CI2->equalsInt(0)) return const_cast<Constant*>(C2); if (CI2->equalsInt(1))
return const_cast<Constant*>(C1); break;
case Instruction::UDiv:
case Instruction::SDiv:
if (CI2->equalsInt(1))
return const_cast<Constant*>(C1); if (CI2->equalsInt(0))
return UndefValue::get(CI2->getType()); break;
case Instruction::URem:
case Instruction::SRem:
if (CI2->equalsInt(1))
return Constant::getNullValue(CI2->getType()); if (CI2->equalsInt(0))
return UndefValue::get(CI2->getType()); break;
case Instruction::And:
if (CI2->isZero()) return const_cast<Constant*>(C2); if (CI2->isAllOnesValue())
return const_cast<Constant*>(C1);
if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
if (CE1->getOpcode() == Instruction::ZExt) {
unsigned DstWidth = CI2->getType()->getBitWidth();
unsigned SrcWidth =
CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
return const_cast<Constant*>(C1);
}
if (CE1->getOpcode() == Instruction::PtrToInt &&
isa<GlobalValue>(CE1->getOperand(0))) {
GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
unsigned GVAlign = GV->getAlignment();
if (isa<Function>(GV))
GVAlign = std::max(GVAlign, 4U);
if (GVAlign > 1) {
unsigned DstWidth = CI2->getType()->getBitWidth();
unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
return Constant::getNullValue(CI2->getType());
}
}
}
break;
case Instruction::Or:
if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); if (CI2->isAllOnesValue())
return const_cast<Constant*>(C2); break;
case Instruction::Xor:
if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); break;
case Instruction::AShr:
if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
if (CE1->getOpcode() == Instruction::ZExt) return ConstantExpr::getLShr(const_cast<Constant*>(C1),
const_cast<Constant*>(C2));
break;
}
}
if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
using namespace APIntOps;
const APInt &C1V = CI1->getValue();
const APInt &C2V = CI2->getValue();
switch (Opcode) {
default:
break;
case Instruction::Add:
return ConstantInt::get(C1V + C2V);
case Instruction::Sub:
return ConstantInt::get(C1V - C2V);
case Instruction::Mul:
return ConstantInt::get(C1V * C2V);
case Instruction::UDiv:
assert(!CI2->isNullValue() && "Div by zero handled above");
return ConstantInt::get(C1V.udiv(C2V));
case Instruction::SDiv:
assert(!CI2->isNullValue() && "Div by zero handled above");
if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
return UndefValue::get(CI1->getType()); return ConstantInt::get(C1V.sdiv(C2V));
case Instruction::URem:
assert(!CI2->isNullValue() && "Div by zero handled above");
return ConstantInt::get(C1V.urem(C2V));
case Instruction::SRem:
assert(!CI2->isNullValue() && "Div by zero handled above");
if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
return UndefValue::get(CI1->getType()); return ConstantInt::get(C1V.srem(C2V));
case Instruction::And:
return ConstantInt::get(C1V & C2V);
case Instruction::Or:
return ConstantInt::get(C1V | C2V);
case Instruction::Xor:
return ConstantInt::get(C1V ^ C2V);
case Instruction::Shl: {
uint32_t shiftAmt = C2V.getZExtValue();
if (shiftAmt < C1V.getBitWidth())
return ConstantInt::get(C1V.shl(shiftAmt));
else
return UndefValue::get(C1->getType()); }
case Instruction::LShr: {
uint32_t shiftAmt = C2V.getZExtValue();
if (shiftAmt < C1V.getBitWidth())
return ConstantInt::get(C1V.lshr(shiftAmt));
else
return UndefValue::get(C1->getType()); }
case Instruction::AShr: {
uint32_t shiftAmt = C2V.getZExtValue();
if (shiftAmt < C1V.getBitWidth())
return ConstantInt::get(C1V.ashr(shiftAmt));
else
return UndefValue::get(C1->getType()); }
}
}
} else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
APFloat C1V = CFP1->getValueAPF();
APFloat C2V = CFP2->getValueAPF();
APFloat C3V = C1V; switch (Opcode) {
default:
break;
case Instruction::Add:
(void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C3V);
case Instruction::Sub:
(void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C3V);
case Instruction::Mul:
(void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C3V);
case Instruction::FDiv:
(void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C3V);
case Instruction::FRem:
(void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
return ConstantFP::get(C3V);
}
}
} else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
(CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
switch (Opcode) {
default:
break;
case Instruction::Add:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getAdd);
case Instruction::Sub:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSub);
case Instruction::Mul:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getMul);
case Instruction::UDiv:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getUDiv);
case Instruction::SDiv:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSDiv);
case Instruction::FDiv:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getFDiv);
case Instruction::URem:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getURem);
case Instruction::SRem:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSRem);
case Instruction::FRem:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getFRem);
case Instruction::And:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getAnd);
case Instruction::Or:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getOr);
case Instruction::Xor:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getXor);
case Instruction::LShr:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getLShr);
case Instruction::AShr:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getAShr);
case Instruction::Shl:
return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getShl);
}
}
}
if (isa<ConstantExpr>(C1)) {
} else if (isa<ConstantExpr>(C2)) {
switch (Opcode) {
case Instruction::Add:
case Instruction::Mul:
case Instruction::And:
case Instruction::Or:
case Instruction::Xor:
return ConstantFoldBinaryInstruction(Opcode, C2, C1);
case Instruction::Shl:
case Instruction::LShr:
case Instruction::AShr:
case Instruction::Sub:
case Instruction::SDiv:
case Instruction::UDiv:
case Instruction::FDiv:
case Instruction::URem:
case Instruction::SRem:
case Instruction::FRem:
default: break;
}
}
return 0;
}
static bool isMaybeZeroSizedType(const Type *Ty) {
if (isa<OpaqueType>(Ty)) return true; if (const StructType *STy = dyn_cast<StructType>(Ty)) {
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
return true;
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
return isMaybeZeroSizedType(ATy->getElementType());
}
return false;
}
static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
if (C1 == C2) return 0;
if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
return -2;
if (C1->getType() != Type::Int64Ty)
C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
if (C2->getType() != Type::Int64Ty)
C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
if (C1 == C2) return 0;
if (isMaybeZeroSizedType(ElTy))
return -2;
if (cast<ConstantInt>(C1)->getSExtValue() <
cast<ConstantInt>(C2)->getSExtValue())
return -1;
else
return 1;
}
static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1,
const Constant *V2) {
assert(V1->getType() == V2->getType() &&
"Cannot compare values of different types!");
if (V1->getType() == Type::PPC_FP128Ty)
return FCmpInst::BAD_FCMP_PREDICATE;
if (V1 == V2) return FCmpInst::FCMP_OEQ;
if (!isa<ConstantExpr>(V1)) {
if (!isa<ConstantExpr>(V2)) {
ConstantInt *R = 0;
Constant *C1 = const_cast<Constant*>(V1);
Constant *C2 = const_cast<Constant*>(V2);
R = dyn_cast<ConstantInt>(
ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
if (R && !R->isZero())
return FCmpInst::FCMP_OEQ;
R = dyn_cast<ConstantInt>(
ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2));
if (R && !R->isZero())
return FCmpInst::FCMP_OLT;
R = dyn_cast<ConstantInt>(
ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
if (R && !R->isZero())
return FCmpInst::FCMP_OGT;
return FCmpInst::BAD_FCMP_PREDICATE;
}
FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
return FCmpInst::getSwappedPredicate(SwappedRelation);
} else {
const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
switch (CE1->getOpcode()) {
case Instruction::FPTrunc:
case Instruction::FPExt:
case Instruction::UIToFP:
case Instruction::SIToFP:
break;
default:
break;
}
}
return FCmpInst::BAD_FCMP_PREDICATE;
}
static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
const Constant *V2,
bool isSigned) {
assert(V1->getType() == V2->getType() &&
"Cannot compare different types of values!");
if (V1 == V2) return ICmpInst::ICMP_EQ;
if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
ConstantInt *R = 0;
Constant *C1 = const_cast<Constant*>(V1);
Constant *C2 = const_cast<Constant*>(V2);
ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
if (R && !R->isZero())
return pred;
pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
if (R && !R->isZero())
return pred;
pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
if (R && !R->isZero())
return pred;
return ICmpInst::BAD_ICMP_PREDICATE;
}
ICmpInst::Predicate SwappedRelation =
evaluateICmpRelation(V2, V1, isSigned);
if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
return ICmpInst::getSwappedPredicate(SwappedRelation);
} else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
if (isa<ConstantExpr>(V2)) { ICmpInst::Predicate SwappedRelation =
evaluateICmpRelation(V2, V1, isSigned);
if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
return ICmpInst::getSwappedPredicate(SwappedRelation);
else
return ICmpInst::BAD_ICMP_PREDICATE;
}
if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2))
if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
return ICmpInst::ICMP_NE;
} else {
assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1))
return ICmpInst::ICMP_NE;
}
} else {
const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
const Constant *CE1Op0 = CE1->getOperand(0);
switch (CE1->getOpcode()) {
case Instruction::Trunc:
case Instruction::FPTrunc:
case Instruction::FPExt:
case Instruction::FPToUI:
case Instruction::FPToSI:
break;
case Instruction::UIToFP:
case Instruction::SIToFP:
case Instruction::BitCast:
case Instruction::ZExt:
case Instruction::SExt:
if (V2->isNullValue() &&
(isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
bool sgnd = isSigned;
if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
return evaluateICmpRelation(CE1Op0,
Constant::getNullValue(CE1Op0->getType()),
sgnd);
}
if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
CE1->getOperand(0)->getType()->isInteger()) {
bool sgnd = isSigned;
if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0),
sgnd);
}
break;
case Instruction::GetElementPtr:
if (isa<ConstantPointerNull>(V2)) {
if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
if (GV->hasExternalWeakLinkage())
return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
else
return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
} else if (isa<ConstantPointerNull>(CE1Op0)) {
for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
if (!CE1->getOperand(i)->isNullValue())
return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
return ICmpInst::ICMP_EQ;
}
} else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
if (isa<ConstantPointerNull>(CE1Op0)) {
if (CPR2->hasExternalWeakLinkage())
return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
else
return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
} else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
if (CPR1 == CPR2) {
assert(CE1->getNumOperands() == 2 &&
!CE1->getOperand(1)->isNullValue() &&
"Suprising getelementptr!");
return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
} else {
return ICmpInst::ICMP_NE;
}
}
} else {
const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
const Constant *CE2Op0 = CE2->getOperand(0);
switch (CE2->getOpcode()) {
default: break;
case Instruction::GetElementPtr:
if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
if (CE1Op0 != CE2Op0) return ICmpInst::ICMP_NE;
unsigned i = 1;
gep_type_iterator GTI = gep_type_begin(CE1);
for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
++i, ++GTI)
switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
GTI.getIndexedType())) {
case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
case -2: return ICmpInst::BAD_ICMP_PREDICATE;
}
for (; i < CE1->getNumOperands(); ++i)
if (!CE1->getOperand(i)->isNullValue()) {
if (isa<ConstantInt>(CE1->getOperand(i)))
return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
else
return ICmpInst::BAD_ICMP_PREDICATE; }
for (; i < CE2->getNumOperands(); ++i)
if (!CE2->getOperand(i)->isNullValue()) {
if (isa<ConstantInt>(CE2->getOperand(i)))
return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
else
return ICmpInst::BAD_ICMP_PREDICATE; }
return ICmpInst::ICMP_EQ;
}
}
}
default:
break;
}
}
return ICmpInst::BAD_ICMP_PREDICATE;
}
Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
const Constant *C1,
const Constant *C2) {
if (pred == FCmpInst::FCMP_FALSE) {
if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
return Constant::getNullValue(VectorType::getInteger(VT));
else
return ConstantInt::getFalse();
}
if (pred == FCmpInst::FCMP_TRUE) {
if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
return Constant::getAllOnesValue(VectorType::getInteger(VT));
else
return ConstantInt::getTrue();
}
if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType()))
return UndefValue::get(VectorType::getInteger(VTy));
return UndefValue::get(Type::Int1Ty);
}
if (C1->getType() == Type::PPC_FP128Ty)
return 0;
if (C1->isNullValue()) {
if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
if (pred == ICmpInst::ICMP_EQ)
return ConstantInt::getFalse();
else if (pred == ICmpInst::ICMP_NE)
return ConstantInt::getTrue();
}
} else if (C2->isNullValue()) {
if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
if (pred == ICmpInst::ICMP_EQ)
return ConstantInt::getFalse();
else if (pred == ICmpInst::ICMP_NE)
return ConstantInt::getTrue();
}
}
if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
APInt V1 = cast<ConstantInt>(C1)->getValue();
APInt V2 = cast<ConstantInt>(C2)->getValue();
switch (pred) {
default: assert(0 && "Invalid ICmp Predicate"); return 0;
case ICmpInst::ICMP_EQ: return ConstantInt::get(Type::Int1Ty, V1 == V2);
case ICmpInst::ICMP_NE: return ConstantInt::get(Type::Int1Ty, V1 != V2);
case ICmpInst::ICMP_SLT:return ConstantInt::get(Type::Int1Ty, V1.slt(V2));
case ICmpInst::ICMP_SGT:return ConstantInt::get(Type::Int1Ty, V1.sgt(V2));
case ICmpInst::ICMP_SLE:return ConstantInt::get(Type::Int1Ty, V1.sle(V2));
case ICmpInst::ICMP_SGE:return ConstantInt::get(Type::Int1Ty, V1.sge(V2));
case ICmpInst::ICMP_ULT:return ConstantInt::get(Type::Int1Ty, V1.ult(V2));
case ICmpInst::ICMP_UGT:return ConstantInt::get(Type::Int1Ty, V1.ugt(V2));
case ICmpInst::ICMP_ULE:return ConstantInt::get(Type::Int1Ty, V1.ule(V2));
case ICmpInst::ICMP_UGE:return ConstantInt::get(Type::Int1Ty, V1.uge(V2));
}
} else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
APFloat::cmpResult R = C1V.compare(C2V);
switch (pred) {
default: assert(0 && "Invalid FCmp Predicate"); return 0;
case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse();
case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue();
case FCmpInst::FCMP_UNO:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered);
case FCmpInst::FCMP_ORD:
return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpUnordered);
case FCmpInst::FCMP_UEQ:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
R==APFloat::cmpEqual);
case FCmpInst::FCMP_OEQ:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpEqual);
case FCmpInst::FCMP_UNE:
return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpEqual);
case FCmpInst::FCMP_ONE:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan ||
R==APFloat::cmpGreaterThan);
case FCmpInst::FCMP_ULT:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
R==APFloat::cmpLessThan);
case FCmpInst::FCMP_OLT:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan);
case FCmpInst::FCMP_UGT:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
R==APFloat::cmpGreaterThan);
case FCmpInst::FCMP_OGT:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan);
case FCmpInst::FCMP_ULE:
return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpGreaterThan);
case FCmpInst::FCMP_OLE:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan ||
R==APFloat::cmpEqual);
case FCmpInst::FCMP_UGE:
return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpLessThan);
case FCmpInst::FCMP_OGE:
return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan ||
R==APFloat::cmpEqual);
}
} else if (isa<VectorType>(C1->getType())) {
SmallVector<Constant*, 16> C1Elts, C2Elts;
C1->getVectorElements(C1Elts);
C2->getVectorElements(C2Elts);
SmallVector<Constant*, 4> ResElts;
const Type *InEltTy = C1Elts[0]->getType();
bool isFP = InEltTy->isFloatingPoint();
const Type *ResEltTy = InEltTy;
if (isFP)
ResEltTy = IntegerType::get(InEltTy->getPrimitiveSizeInBits());
for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) {
Constant *C;
if (isFP)
C = ConstantExpr::getFCmp(pred, C1Elts[i], C2Elts[i]);
else
C = ConstantExpr::getICmp(pred, C1Elts[i], C2Elts[i]);
if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
if (CI->isZero())
ResElts.push_back(Constant::getNullValue(ResEltTy));
else
ResElts.push_back(Constant::getAllOnesValue(ResEltTy));
} else if (isa<UndefValue>(C)) {
ResElts.push_back(UndefValue::get(ResEltTy));
} else {
break;
}
}
if (ResElts.size() == C1Elts.size())
return ConstantVector::get(&ResElts[0], ResElts.size());
}
if (C1->getType()->isFloatingPoint()) {
int Result = -1; switch (evaluateFCmpRelation(C1, C2)) {
default: assert(0 && "Unknown relation!");
case FCmpInst::FCMP_UNO:
case FCmpInst::FCMP_ORD:
case FCmpInst::FCMP_UEQ:
case FCmpInst::FCMP_UNE:
case FCmpInst::FCMP_ULT:
case FCmpInst::FCMP_UGT:
case FCmpInst::FCMP_ULE:
case FCmpInst::FCMP_UGE:
case FCmpInst::FCMP_TRUE:
case FCmpInst::FCMP_FALSE:
case FCmpInst::BAD_FCMP_PREDICATE:
break; case FCmpInst::FCMP_OEQ: Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
break;
case FCmpInst::FCMP_OLT: Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
break;
case FCmpInst::FCMP_OGT: Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
break;
case FCmpInst::FCMP_OLE: if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Result = 0;
else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Result = 1;
break;
case FCmpInst::FCMP_OGE: if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Result = 0;
else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Result = 1;
break;
case ICmpInst::ICMP_NE: if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Result = 0;
else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
Result = 1;
break;
}
if (Result != -1) {
if (const VectorType *VT = dyn_cast<VectorType>(C1->getType())) {
if (Result == 0)
return Constant::getNullValue(VectorType::getInteger(VT));
else
return Constant::getAllOnesValue(VectorType::getInteger(VT));
}
return ConstantInt::get(Type::Int1Ty, Result);
}
} else {
int Result = -1; switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
default: assert(0 && "Unknown relational!");
case ICmpInst::BAD_ICMP_PREDICATE:
break; case ICmpInst::ICMP_EQ: Result = (pred == ICmpInst::ICMP_EQ ||
pred == ICmpInst::ICMP_ULE ||
pred == ICmpInst::ICMP_SLE ||
pred == ICmpInst::ICMP_UGE ||
pred == ICmpInst::ICMP_SGE);
break;
case ICmpInst::ICMP_ULT:
Result = (pred == ICmpInst::ICMP_ULT ||
pred == ICmpInst::ICMP_NE ||
pred == ICmpInst::ICMP_ULE);
break;
case ICmpInst::ICMP_SLT:
Result = (pred == ICmpInst::ICMP_SLT ||
pred == ICmpInst::ICMP_NE ||
pred == ICmpInst::ICMP_SLE);
break;
case ICmpInst::ICMP_UGT:
Result = (pred == ICmpInst::ICMP_UGT ||
pred == ICmpInst::ICMP_NE ||
pred == ICmpInst::ICMP_UGE);
break;
case ICmpInst::ICMP_SGT:
Result = (pred == ICmpInst::ICMP_SGT ||
pred == ICmpInst::ICMP_NE ||
pred == ICmpInst::ICMP_SGE);
break;
case ICmpInst::ICMP_ULE:
if (pred == ICmpInst::ICMP_UGT) Result = 0;
if (pred == ICmpInst::ICMP_ULT) Result = 1;
break;
case ICmpInst::ICMP_SLE:
if (pred == ICmpInst::ICMP_SGT) Result = 0;
if (pred == ICmpInst::ICMP_SLT) Result = 1;
break;
case ICmpInst::ICMP_UGE:
if (pred == ICmpInst::ICMP_ULT) Result = 0;
if (pred == ICmpInst::ICMP_UGT) Result = 1;
break;
case ICmpInst::ICMP_SGE:
if (pred == ICmpInst::ICMP_SLT) Result = 0;
if (pred == ICmpInst::ICMP_SGT) Result = 1;
break;
case ICmpInst::ICMP_NE:
if (pred == ICmpInst::ICMP_EQ) Result = 0;
if (pred == ICmpInst::ICMP_NE) Result = 1;
break;
}
if (Result != -1) {
if (const VectorType *VT = dyn_cast<VectorType>(C1->getType())) {
if (Result == 0)
return Constant::getNullValue(VT);
else
return Constant::getAllOnesValue(VT);
}
return ConstantInt::get(Type::Int1Ty, Result);
}
if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
switch (pred) {
case ICmpInst::ICMP_EQ:
case ICmpInst::ICMP_NE:
return ConstantFoldCompareInstruction(pred, C2, C1);
case ICmpInst::ICMP_ULT:
case ICmpInst::ICMP_SLT:
case ICmpInst::ICMP_UGT:
case ICmpInst::ICMP_SGT:
case ICmpInst::ICMP_ULE:
case ICmpInst::ICMP_SLE:
case ICmpInst::ICMP_UGE:
case ICmpInst::ICMP_SGE:
pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
return ConstantFoldCompareInstruction(pred, C2, C1);
default: break;
}
}
}
return 0;
}
Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Constant* const *Idxs,
unsigned NumIdx) {
if (NumIdx == 0 ||
(NumIdx == 1 && Idxs[0]->isNullValue()))
return const_cast<Constant*>(C);
if (isa<UndefValue>(C)) {
const PointerType *Ptr = cast<PointerType>(C->getType());
const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
(Value **)Idxs,
(Value **)Idxs+NumIdx);
assert(Ty != 0 && "Invalid indices for GEP!");
return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
}
Constant *Idx0 = Idxs[0];
if (C->isNullValue()) {
bool isNull = true;
for (unsigned i = 0, e = NumIdx; i != e; ++i)
if (!Idxs[i]->isNullValue()) {
isNull = false;
break;
}
if (isNull) {
const PointerType *Ptr = cast<PointerType>(C->getType());
const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
(Value**)Idxs,
(Value**)Idxs+NumIdx);
assert(Ty != 0 && "Invalid indices for GEP!");
return
ConstantPointerNull::get(PointerType::get(Ty,Ptr->getAddressSpace()));
}
}
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
if (CE->getOpcode() == Instruction::GetElementPtr) {
const Type *LastTy = 0;
for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
I != E; ++I)
LastTy = *I;
if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
SmallVector<Value*, 16> NewIndices;
NewIndices.reserve(NumIdx + CE->getNumOperands());
for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
NewIndices.push_back(CE->getOperand(i));
Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
if (!Idx0->isNullValue()) {
const Type *IdxTy = Combined->getType();
if (IdxTy != Idx0->getType()) {
Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Type::Int64Ty);
Combined = ConstantExpr::get(Instruction::Add, C1, C2);
} else {
Combined =
ConstantExpr::get(Instruction::Add, Idx0, Combined);
}
}
NewIndices.push_back(Combined);
NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
NewIndices.size());
}
}
if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
if (const PointerType *SPT =
dyn_cast<PointerType>(CE->getOperand(0)->getType()))
if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
if (const ArrayType *CAT =
dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
if (CAT->getElementType() == SAT->getElementType())
return ConstantExpr::getGetElementPtr(
(Constant*)CE->getOperand(0), Idxs, NumIdx);
}
if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 &&
isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) &&
cast<PointerType>(CE->getType())->getElementType() == Type::Int8Ty) {
Constant *Base = CE->getOperand(0);
Constant *Offset = Idxs[0];
if (Offset->getType()->getPrimitiveSizeInBits() <
Base->getType()->getPrimitiveSizeInBits())
Offset = ConstantExpr::getSExt(Offset, Base->getType());
else if (Base->getType()->getPrimitiveSizeInBits() <
Offset->getType()->getPrimitiveSizeInBits())
Base = ConstantExpr::getZExt(Base, Offset->getType());
Base = ConstantExpr::getAdd(Base, Offset);
return ConstantExpr::getIntToPtr(Base, CE->getType());
}
}
return 0;
}