From 802b5ab844070daf5a3e8947124c476b88a5d22c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 8 Mar 2004 06:17:35 +0000 Subject: Implement test/Regression/Assembler/2004-03-07-FunctionAddressAlignment.llx git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12218 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/ConstantFold.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib/VMCore') diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 0d00887..5f7e0ef 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -22,6 +22,7 @@ #include "llvm/Constants.h" #include "llvm/Instructions.h" #include "llvm/DerivedTypes.h" +#include "llvm/Function.h" #include "llvm/Support/GetElementPtrTypeIterator.h" #include using namespace llvm; @@ -523,6 +524,15 @@ Constant *llvm::ConstantFoldCastInstruction(const Constant *V, 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(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(V)) if (CE->getOpcode() == Instruction::Cast) { Constant *Op = const_cast(CE->getOperand(0)); @@ -873,6 +883,16 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, if (cast(V2)->isAllOnesValue()) return const_cast(V1); // X & -1 == X if (V2->isNullValue()) return const_cast(V2); // X & 0 == 0 + if (CE1->getOpcode() == Instruction::Cast && + isa(CE1->getOperand(0))) { + ConstantPointerRef *CPR =cast(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(V2)) + if (CI->getRawValue() < 4 && isa(CPR->getValue())) + return Constant::getNullValue(CI->getType()); + } break; case Instruction::Or: if (V2->isNullValue()) return const_cast(V1); // X | 0 == X -- cgit v1.1