#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;
const Type *DestTy) {
if (V->getType() == DestTy) return (Constant*)V;
+ // Cast of a global address to boolean is always true.
+ if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(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 (!CPR->getValue()->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));
}
}
+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
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<ConstantPointerRef>(CE1->getOperand(0))) {
+ ConstantPointerRef *CPR =cast<ConstantPointerRef>(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->getValue()))
+ return Constant::getNullValue(CI->getType());
+ }
break;
case Instruction::Or:
if (V2->isNullValue()) return const_cast<Constant*>(V1); // X | 0 == X