#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/DerivedTypes.h"
+#include "llvm/Function.h"
#include "llvm/Support/GetElementPtrTypeIterator.h"
#include <cmath>
using namespace llvm;
static DirectFPRules <ConstantFP , double , &Type::DoubleTy> DoubleR;
if (isa<ConstantExpr>(V1) || isa<ConstantExpr>(V2) ||
- isa<ConstantPointerRef>(V1) || isa<ConstantPointerRef>(V2))
+ isa<GlobalValue>(V1) || isa<GlobalValue>(V2))
return EmptyR;
- switch (V1->getType()->getPrimitiveID()) {
+ switch (V1->getType()->getTypeID()) {
default: assert(0 && "Unknown value type for constant folding!");
case Type::BoolTyID: return BoolR;
case Type::PointerTyID: return NullPointerR;
const Type *DestTy) {
if (V->getType() == DestTy) return (Constant*)V;
+ // Cast of a global address to boolean is always true.
+ if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
+ if (DestTy == Type::BoolTy)
+ // FIXME: When we support 'external weak' references, we have to prevent
+ // this transformation from happening. In the meantime we avoid folding
+ // any cast of an external symbol.
+ if (!GV->isExternal())
+ return ConstantBool::True;
+
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
if (CE->getOpcode() == Instruction::Cast) {
Constant *Op = const_cast<Constant*>(CE->getOperand(0));
// Try to not produce a cast of a cast, which is almost always redundant.
if (!Op->getType()->isFloatingPoint() &&
!CE->getType()->isFloatingPoint() &&
- !DestTy->getType()->isFloatingPoint()) {
+ !DestTy->isFloatingPoint()) {
unsigned S1 = getSize(Op->getType()), S2 = getSize(CE->getType());
unsigned S3 = getSize(DestTy);
if (Op->getType() == DestTy && S3 >= S2)
ConstRules &Rules = ConstRules::get(V, V);
- switch (DestTy->getPrimitiveID()) {
+ switch (DestTy->getTypeID()) {
case Type::BoolTyID: return Rules.castToBool(V);
case Type::UByteTyID: return Rules.castToUByte(V);
case Type::SByteTyID: return Rules.castToSByte(V);
}
}
+Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
+ const Constant *V1,
+ const Constant *V2) {
+ if (Cond == ConstantBool::True)
+ return const_cast<Constant*>(V1);
+ else if (Cond == ConstantBool::False)
+ return const_cast<Constant*>(V2);
+ return 0;
+}
+
+
/// IdxCompare - Compare the two constants as though they were getelementptr
/// indices. This allows coersion of the types to be the same thing.
///
if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
return -2; // don't know!
- // Ok, we have two differing integer indices. Convert them to
- // be the same type. Long is always big enough, so we use it.
- C1 = ConstantExpr::getCast(C1, Type::LongTy);
- C2 = ConstantExpr::getCast(C2, Type::LongTy);
+ // Ok, we have two differing integer indices. Sign extend them to be the same
+ // type. Long is always big enough, so we use it.
+ C1 = ConstantExpr::getSignExtend(C1, Type::LongTy);
+ C2 = ConstantExpr::getSignExtend(C2, Type::LongTy);
if (C1 == C2) return 0; // Are they just differing types?
// If they are really different, now that they are the same type, then we
/// evaluateRelation - This function determines if there is anything we can
/// decide about the two constants provided. This doesn't need to handle simple
-/// things like integer comparisons, but should instead handle ConstantExpr's
-/// and ConstantPointerRef's. If we can determine that the two constants have a
+/// things like integer comparisons, but should instead handle ConstantExprs
+/// and GlobalValuess. If we can determine that the two constants have a
/// particular relation to each other, we should return the corresponding SetCC
/// code, otherwise return Instruction::BinaryOpsEnd.
///
/// To simplify this code we canonicalize the relation so that the first
/// operand is always the most "complex" of the two. We consider simple
/// constants (like ConstantInt) to be the simplest, followed by
-/// ConstantPointerRef's, followed by ConstantExpr's (the most complex).
+/// GlobalValues, followed by ConstantExpr's (the most complex).
///
static Instruction::BinaryOps evaluateRelation(const Constant *V1,
const Constant *V2) {
"Cannot compare different types of values!");
if (V1 == V2) return Instruction::SetEQ;
- if (!isa<ConstantExpr>(V1) && !isa<ConstantPointerRef>(V1)) {
+ if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
// If the first operand is simple, swap operands.
- assert((isa<ConstantPointerRef>(V2) || isa<ConstantExpr>(V2)) &&
+ assert((isa<GlobalValue>(V2) || isa<ConstantExpr>(V2)) &&
"Simple cases should have been handled by caller!");
Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
if (SwappedRelation != Instruction::BinaryOpsEnd)
return SetCondInst::getSwappedCondition(SwappedRelation);
- } else if (const ConstantPointerRef *CPR1 = dyn_cast<ConstantPointerRef>(V1)){
+ } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)){
if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Instruction::BinaryOps SwappedRelation = evaluateRelation(V2, V1);
if (SwappedRelation != Instruction::BinaryOpsEnd)
return Instruction::BinaryOpsEnd;
}
- // Now we know that the RHS is a ConstantPointerRef or simple constant,
+ // Now we know that the RHS is a GlobalValue or simple constant,
// which (since the types must match) means that it's a ConstantPointerNull.
- if (const ConstantPointerRef *CPR2 = dyn_cast<ConstantPointerRef>(V2)) {
- assert(CPR1->getValue() != CPR2->getValue() &&
- "CPRs for the same value exist at different addresses??");
+ if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
+ assert(CPR1 != CPR2 &&
+ "GVs for the same value exist at different addresses??");
// FIXME: If both globals are external weak, they might both be null!
return Instruction::SetNE;
} else {
CE1->getType()->isLosslesslyConvertibleTo(CE1Op0->getType()))
return evaluateRelation(CE1Op0,
Constant::getNullValue(CE1Op0->getType()));
+ break;
case Instruction::GetElementPtr:
// Ok, since this is a getelementptr, we know that the constant has a
if (isa<ConstantPointerNull>(V2)) {
// If we are comparing a GEP to a null pointer, check to see if the base
// of the GEP equals the null pointer.
- if (isa<ConstantPointerRef>(CE1Op0)) {
+ if (isa<GlobalValue>(CE1Op0)) {
// FIXME: this is not true when we have external weak references!
// No offset can go from a global to a null pointer.
return Instruction::SetGT;
return Instruction::SetEQ;
}
// Otherwise, we can't really say if the first operand is null or not.
- } else if (const ConstantPointerRef *CPR2 =
- dyn_cast<ConstantPointerRef>(V2)) {
+ } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
if (isa<ConstantPointerNull>(CE1Op0)) {
// FIXME: This is not true with external weak references.
return Instruction::SetLT;
- } else if (const ConstantPointerRef *CPR1 =
- dyn_cast<ConstantPointerRef>(CE1Op0)) {
+ } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
if (CPR1 == CPR2) {
// If this is a getelementptr of the same global, then it must be
// different. Because the types must match, the getelementptr could
case Instruction::GetElementPtr:
// By far the most common case to handle is when the base pointers are
// obviously to the same or different globals.
- if (isa<ConstantPointerRef>(CE1Op0) &&
- isa<ConstantPointerRef>(CE2Op0)) {
+ if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
return Instruction::SetNE;
// Ok, we know that both getelementptr instructions are based on the
if (cast<ConstantIntegral>(V2)->isAllOnesValue())
return const_cast<Constant*>(V1); // X & -1 == X
if (V2->isNullValue()) return const_cast<Constant*>(V2); // X & 0 == 0
+ if (CE1->getOpcode() == Instruction::Cast &&
+ isa<GlobalValue>(CE1->getOperand(0))) {
+ GlobalValue *CPR =cast<GlobalValue>(CE1->getOperand(0));
+
+ // Functions are at least 4-byte aligned. If and'ing the address of a
+ // function with a constant < 4, fold it to zero.
+ if (const ConstantInt *CI = dyn_cast<ConstantInt>(V2))
+ if (CI->getRawValue() < 4 && isa<Function>(CPR))
+ return Constant::getNullValue(CI->getType());
+ }
break;
case Instruction::Or:
if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X
assert(Ty != 0 && "Invalid indices for GEP!");
return ConstantPointerNull::get(PointerType::get(Ty));
}
+
+ if (IdxList.size() == 1) {
+ const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
+ if (unsigned ElSize = ElTy->getPrimitiveSize()) {
+ // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
+ // type, we can statically fold this.
+ Constant *R = ConstantUInt::get(Type::UIntTy, ElSize);
+ R = ConstantExpr::getCast(R, IdxList[0]->getType());
+ R = ConstantExpr::getMul(R, IdxList[0]);
+ return ConstantExpr::getCast(R, C->getType());
+ }
+ }
}
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
// Add the last index of the source with the first index of the new GEP.
// Make sure to handle the case when they are actually different types.
Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
- if (!IdxList[0]->isNullValue()) // Otherwise it must be an array
+ if (!IdxList[0]->isNullValue()) { // Otherwise it must be an array
+ const Type *IdxTy = Combined->getType();
+ if (IdxTy != IdxList[0]->getType()) IdxTy = Type::LongTy;
Combined =
ConstantExpr::get(Instruction::Add,
- ConstantExpr::getCast(IdxList[0], Type::LongTy),
- ConstantExpr::getCast(Combined, Type::LongTy));
+ ConstantExpr::getCast(IdxList[0], IdxTy),
+ ConstantExpr::getCast(Combined, IdxTy));
+ }
NewIndices.push_back(Combined);
NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());