//===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements folding of constants for LLVM. This implements the
// (internal) ConstantFold.h interface, which is used by the
// ConstantExpr::get* methods to automatically fold constants when possible.
//
// The current constant folding implementation is implemented in two pieces: the
// template-based folder for simple primitive constants like ConstantInt, and
// the special case hackery that we use to symbolically evaluate expressions
// that use ConstantExprs.
//
//===----------------------------------------------------------------------===//
#include "ConstantFold.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.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;
//===----------------------------------------------------------------------===//
// ConstantFold*Instruction Implementations
//===----------------------------------------------------------------------===//
/// CastConstantVector - Convert the specified ConstantVector node to the
/// specified vector type. At this point, we know that the elements of the
/// input packed constant are all simple integer or FP values.
static Constant *CastConstantVector(ConstantVector *CV,
const VectorType *DstTy) {
unsigned SrcNumElts = CV->getType()->getNumElements();
unsigned DstNumElts = DstTy->getNumElements();
const Type *SrcEltTy = CV->getType()->getElementType();
const Type *DstEltTy = DstTy->getElementType();
// If both vectors have the same number of elements (thus, the elements
// are the same size), perform the conversion now.
if (SrcNumElts == DstNumElts) {
std::vector<Constant*> Result;
// If the src and dest elements are both integers, or both floats, we can
// just BitCast each element becaus