2007-02-27 06:23:51 +00:00
|
|
|
//===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2004-01-12 21:13:12 +00:00
|
|
|
// This file implements folding of constants for LLVM. This implements the
|
2007-02-27 06:23:51 +00:00
|
|
|
// (internal) ConstantFold.h interface, which is used by the
|
2004-01-12 21:13:12 +00:00
|
|
|
// ConstantExpr::get* methods to automatically fold constants when possible.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2004-01-12 22:07:24 +00:00
|
|
|
// The current constant folding implementation is implemented in two pieces: the
|
2009-09-11 00:04:14 +00:00
|
|
|
// pieces that don't need TargetData, and the pieces that do. This is to avoid
|
|
|
|
// a dependence in VMCore on Target.
|
2004-01-12 22:07:24 +00:00
|
|
|
//
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-27 03:05:06 +00:00
|
|
|
#include "ConstantFold.h"
|
2004-01-12 21:02:29 +00:00
|
|
|
#include "llvm/Constants.h"
|
2004-02-22 06:25:38 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2003-04-17 19:24:18 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-03-08 06:17:35 +00:00
|
|
|
#include "llvm/Function.h"
|
2007-09-10 23:42:42 +00:00
|
|
|
#include "llvm/GlobalAlias.h"
|
2009-09-11 00:04:14 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2007-01-31 04:40:28 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2006-10-13 17:22:21 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2003-11-25 21:21:46 +00:00
|
|
|
#include "llvm/Support/GetElementPtrTypeIterator.h"
|
2006-10-13 17:22:21 +00:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2006-04-02 01:38:28 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2005-05-03 03:13:01 +00:00
|
|
|
#include <limits>
|
2003-11-17 19:05:17 +00:00
|
|
|
using namespace llvm;
|
2001-09-09 21:01:20 +00:00
|
|
|
|
2004-01-12 22:07:24 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ConstantFold*Instruction Implementations
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-11 06:07:39 +00:00
|
|
|
/// BitCastConstantVector - Convert the specified ConstantVector node to the
|
2007-02-15 03:39:18 +00:00
|
|
|
/// specified vector type. At this point, we know that the elements of the
|
2007-07-16 14:29:03 +00:00
|
|
|
/// input vector constant are all simple integer or FP values.
|
2010-02-01 20:48:08 +00:00
|
|
|
static Constant *BitCastConstantVector(ConstantVector *CV,
|
2009-06-20 00:24:58 +00:00
|
|
|
const VectorType *DstTy) {
|
2007-12-11 06:07:39 +00:00
|
|
|
// If this cast changes element count then we can't handle it here:
|
|
|
|
// doing so requires endianness information. This should be handled by
|
|
|
|
// Analysis/ConstantFolding.cpp
|
|
|
|
unsigned NumElts = DstTy->getNumElements();
|
|
|
|
if (NumElts != CV->getNumOperands())
|
|
|
|
return 0;
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2007-12-11 06:07:39 +00:00
|
|
|
// Check to verify that all elements of the input are simple.
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
if (!isa<ConstantInt>(CV->getOperand(i)) &&
|
|
|
|
!isa<ConstantFP>(CV->getOperand(i)))
|
|
|
|
return 0;
|
2006-04-02 01:38:28 +00:00
|
|
|
}
|
2007-12-11 06:07:39 +00:00
|
|
|
|
|
|
|
// Bitcast each element now.
|
|
|
|
std::vector<Constant*> Result;
|
|
|
|
const Type *DstEltTy = DstTy->getElementType();
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i)
|
2009-07-29 18:55:55 +00:00
|
|
|
Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i),
|
2009-07-13 04:09:18 +00:00
|
|
|
DstEltTy));
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Result);
|
2006-04-02 01:38:28 +00:00
|
|
|
}
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
/// This function determines which opcode to use to fold two constant cast
|
|
|
|
/// expressions together. It uses CastInst::isEliminableCastPair to determine
|
|
|
|
/// the opcode. Consequently its just a wrapper around that function.
|
2007-08-05 19:27:01 +00:00
|
|
|
/// @brief Determine if it is valid to fold a cast of a cast
|
2006-11-27 01:05:10 +00:00
|
|
|
static unsigned
|
|
|
|
foldConstantCastPair(
|
|
|
|
unsigned opc, ///< opcode of the second cast constant expression
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantExpr *Op, ///< the first cast constant expression
|
2006-11-27 01:05:10 +00:00
|
|
|
const Type *DstTy ///< desintation type of the first cast
|
|
|
|
) {
|
|
|
|
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");
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// The the types and opcodes for the two Cast constant expressions
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Let CastInst::isEliminableCastPair do the heavy lifting.
|
|
|
|
return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
|
2009-08-13 21:58:54 +00:00
|
|
|
Type::getInt64Ty(DstTy->getContext()));
|
2006-11-27 01:05:10 +00:00
|
|
|
}
|
2006-04-02 01:38:28 +00:00
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
static Constant *FoldBitCast(Constant *V, const Type *DestTy) {
|
2007-12-11 05:55:02 +00:00
|
|
|
const Type *SrcTy = V->getType();
|
|
|
|
if (SrcTy == DestTy)
|
|
|
|
return V; // no-op cast
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2007-12-11 05:55:02 +00:00
|
|
|
// Check to see if we are casting a pointer to an aggregate to a pointer to
|
|
|
|
// the first element. If so, return the appropriate GEP instruction.
|
|
|
|
if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
|
2008-03-31 00:22:16 +00:00
|
|
|
if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy))
|
|
|
|
if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
|
|
|
|
SmallVector<Value*, 8> IdxList;
|
2010-02-01 20:48:08 +00:00
|
|
|
Value *Zero =
|
|
|
|
Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
|
2009-08-12 00:32:55 +00:00
|
|
|
IdxList.push_back(Zero);
|
2008-03-31 00:22:16 +00:00
|
|
|
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);
|
2009-08-12 00:32:55 +00:00
|
|
|
IdxList.push_back(Zero);
|
2008-03-31 00:22:16 +00:00
|
|
|
} else if (const SequentialType *STy =
|
|
|
|
dyn_cast<SequentialType>(ElTy)) {
|
2010-02-16 11:11:14 +00:00
|
|
|
if (ElTy->isPointerTy()) break; // Can't index into pointers!
|
2008-03-31 00:22:16 +00:00
|
|
|
ElTy = STy->getElementType();
|
2009-08-12 00:32:55 +00:00
|
|
|
IdxList.push_back(Zero);
|
2008-03-31 00:22:16 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2007-12-11 05:55:02 +00:00
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2008-03-31 00:22:16 +00:00
|
|
|
if (ElTy == DPTy->getElementType())
|
2009-09-03 23:34:49 +00:00
|
|
|
// This GEP is inbounds because all indices are zero.
|
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(V, &IdxList[0],
|
|
|
|
IdxList.size());
|
2007-12-11 05:55:02 +00:00
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2007-12-11 05:55:02 +00:00
|
|
|
// Handle casts from one vector constant to another. We know that the src
|
|
|
|
// and dest type have the same size (otherwise its an illegal cast).
|
|
|
|
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!");
|
2008-11-03 23:20:04 +00:00
|
|
|
SrcTy = NULL;
|
2007-12-11 05:55:02 +00:00
|
|
|
// First, check for null. Undef is already handled.
|
|
|
|
if (isa<ConstantAggregateZero>(V))
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(DestTy);
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2007-12-11 06:07:39 +00:00
|
|
|
if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
|
2010-02-01 20:48:08 +00:00
|
|
|
return BitCastConstantVector(CV, DestPTy);
|
2007-12-11 05:55:02 +00:00
|
|
|
}
|
2008-10-16 05:26:51 +00:00
|
|
|
|
|
|
|
// Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
|
|
|
|
// This allows for other simplifications (although some of them
|
|
|
|
// can only be handled by Analysis/ConstantFolding.cpp).
|
|
|
|
if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantExpr::getBitCast(ConstantVector::get(&V, 1), DestPTy);
|
2007-12-11 05:55:02 +00:00
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2007-12-11 05:55:02 +00:00
|
|
|
// Finally, implement bitcast folding now. The code below doesn't handle
|
|
|
|
// bitcast right.
|
|
|
|
if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
|
2009-07-30 23:03:37 +00:00
|
|
|
return ConstantPointerNull::get(cast<PointerType>(DestTy));
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2007-12-11 05:55:02 +00:00
|
|
|
// Handle integral constant input.
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2010-02-15 16:12:20 +00:00
|
|
|
if (DestTy->isIntegerTy())
|
2007-12-11 05:55:02 +00:00
|
|
|
// Integral -> Integral. This is a no-op because the bit widths must
|
|
|
|
// be the same. Consequently, we just fold to V.
|
|
|
|
return V;
|
2009-02-04 10:17:14 +00:00
|
|
|
|
2010-02-15 16:12:20 +00:00
|
|
|
if (DestTy->isFloatingPointTy())
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFP::get(DestTy->getContext(),
|
|
|
|
APFloat(CI->getValue(),
|
|
|
|
!DestTy->isPPC_FP128Ty()));
|
2009-02-04 10:17:14 +00:00
|
|
|
|
2007-12-11 05:55:02 +00:00
|
|
|
// Otherwise, can't fold this (vector?)
|
|
|
|
return 0;
|
|
|
|
}
|
2009-02-04 11:17:06 +00:00
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
// Handle ConstantFP input: FP -> Integral.
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantFP *FP = dyn_cast<ConstantFP>(V))
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(FP->getContext(),
|
|
|
|
FP->getValueAPF().bitcastToAPInt());
|
2009-02-04 11:17:06 +00:00
|
|
|
|
2007-12-11 05:55:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-17 21:53:27 +00:00
|
|
|
/// ExtractConstantBytes - V is an integer constant which only has a subset of
|
|
|
|
/// its bytes used. The bytes used are indicated by ByteStart (which is the
|
|
|
|
/// first byte used, counting from the least significant byte) and ByteSize,
|
|
|
|
/// which is the number of bytes used.
|
|
|
|
///
|
|
|
|
/// This function analyzes the specified constant to see if the specified byte
|
|
|
|
/// range can be returned as a simplified constant. If so, the constant is
|
|
|
|
/// returned, otherwise null is returned.
|
|
|
|
///
|
|
|
|
static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
|
|
|
|
unsigned ByteSize) {
|
2010-02-16 11:11:14 +00:00
|
|
|
assert(C->getType()->isIntegerTy() &&
|
2009-10-17 21:53:27 +00:00
|
|
|
(cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
|
|
|
|
"Non-byte sized integer input");
|
|
|
|
unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
|
|
|
|
assert(ByteSize && "Must be accessing some piece");
|
|
|
|
assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
|
|
|
|
assert(ByteSize != CSize && "Should not extract everything");
|
|
|
|
|
|
|
|
// Constant Integers are simple.
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
|
|
|
|
APInt V = CI->getValue();
|
|
|
|
if (ByteStart)
|
|
|
|
V = V.lshr(ByteStart*8);
|
2010-12-07 08:25:19 +00:00
|
|
|
V = V.trunc(ByteSize*8);
|
2009-10-17 21:53:27 +00:00
|
|
|
return ConstantInt::get(CI->getContext(), V);
|
|
|
|
}
|
|
|
|
|
|
|
|
// In the input is a constant expr, we might be able to recursively simplify.
|
|
|
|
// If not, we definitely can't do anything.
|
|
|
|
ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
|
|
|
|
if (CE == 0) return 0;
|
|
|
|
|
|
|
|
switch (CE->getOpcode()) {
|
|
|
|
default: return 0;
|
|
|
|
case Instruction::Or: {
|
2009-10-28 05:14:34 +00:00
|
|
|
Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
|
2009-10-17 21:53:27 +00:00
|
|
|
if (RHS == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// X | -1 -> -1.
|
|
|
|
if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
|
|
|
|
if (RHSC->isAllOnesValue())
|
|
|
|
return RHSC;
|
|
|
|
|
2009-10-28 05:14:34 +00:00
|
|
|
Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
|
2009-10-17 21:53:27 +00:00
|
|
|
if (LHS == 0)
|
|
|
|
return 0;
|
|
|
|
return ConstantExpr::getOr(LHS, RHS);
|
|
|
|
}
|
|
|
|
case Instruction::And: {
|
2009-10-28 05:14:34 +00:00
|
|
|
Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
|
2009-10-17 21:53:27 +00:00
|
|
|
if (RHS == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// X & 0 -> 0.
|
|
|
|
if (RHS->isNullValue())
|
|
|
|
return RHS;
|
|
|
|
|
2009-10-28 05:14:34 +00:00
|
|
|
Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
|
2009-10-17 21:53:27 +00:00
|
|
|
if (LHS == 0)
|
|
|
|
return 0;
|
|
|
|
return ConstantExpr::getAnd(LHS, RHS);
|
|
|
|
}
|
|
|
|
case Instruction::LShr: {
|
|
|
|
ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
|
|
|
|
if (Amt == 0)
|
|
|
|
return 0;
|
|
|
|
unsigned ShAmt = Amt->getZExtValue();
|
|
|
|
// Cannot analyze non-byte shifts.
|
|
|
|
if ((ShAmt & 7) != 0)
|
|
|
|
return 0;
|
|
|
|
ShAmt >>= 3;
|
|
|
|
|
|
|
|
// If the extract is known to be all zeros, return zero.
|
|
|
|
if (ByteStart >= CSize-ShAmt)
|
|
|
|
return Constant::getNullValue(IntegerType::get(CE->getContext(),
|
|
|
|
ByteSize*8));
|
|
|
|
// If the extract is known to be fully in the input, extract it.
|
|
|
|
if (ByteStart+ByteSize+ShAmt <= CSize)
|
2009-10-28 05:14:34 +00:00
|
|
|
return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize);
|
2009-10-17 21:53:27 +00:00
|
|
|
|
|
|
|
// TODO: Handle the 'partially zero' case.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::Shl: {
|
|
|
|
ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
|
|
|
|
if (Amt == 0)
|
|
|
|
return 0;
|
|
|
|
unsigned ShAmt = Amt->getZExtValue();
|
|
|
|
// Cannot analyze non-byte shifts.
|
|
|
|
if ((ShAmt & 7) != 0)
|
|
|
|
return 0;
|
|
|
|
ShAmt >>= 3;
|
|
|
|
|
|
|
|
// If the extract is known to be all zeros, return zero.
|
|
|
|
if (ByteStart+ByteSize <= ShAmt)
|
|
|
|
return Constant::getNullValue(IntegerType::get(CE->getContext(),
|
|
|
|
ByteSize*8));
|
|
|
|
// If the extract is known to be fully in the input, extract it.
|
|
|
|
if (ByteStart >= ShAmt)
|
2009-10-28 05:14:34 +00:00
|
|
|
return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize);
|
2009-10-17 21:53:27 +00:00
|
|
|
|
|
|
|
// TODO: Handle the 'partially zero' case.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Instruction::ZExt: {
|
|
|
|
unsigned SrcBitSize =
|
2009-10-28 05:14:34 +00:00
|
|
|
cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
|
2009-10-17 21:53:27 +00:00
|
|
|
|
|
|
|
// If extracting something that is completely zero, return 0.
|
|
|
|
if (ByteStart*8 >= SrcBitSize)
|
|
|
|
return Constant::getNullValue(IntegerType::get(CE->getContext(),
|
|
|
|
ByteSize*8));
|
|
|
|
|
|
|
|
// If exactly extracting the input, return it.
|
|
|
|
if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
|
2009-10-28 05:14:34 +00:00
|
|
|
return CE->getOperand(0);
|
2009-10-17 21:53:27 +00:00
|
|
|
|
|
|
|
// If extracting something completely in the input, if if the input is a
|
|
|
|
// multiple of 8 bits, recurse.
|
|
|
|
if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
|
2009-10-28 05:14:34 +00:00
|
|
|
return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
|
2009-10-17 21:53:27 +00:00
|
|
|
|
|
|
|
// Otherwise, if extracting a subset of the input, which is not multiple of
|
|
|
|
// 8 bits, do a shift and trunc to get the bits.
|
|
|
|
if ((ByteStart+ByteSize)*8 < SrcBitSize) {
|
|
|
|
assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
|
2009-10-28 05:14:34 +00:00
|
|
|
Constant *Res = CE->getOperand(0);
|
2009-10-17 21:53:27 +00:00
|
|
|
if (ByteStart)
|
|
|
|
Res = ConstantExpr::getLShr(Res,
|
|
|
|
ConstantInt::get(Res->getType(), ByteStart*8));
|
|
|
|
return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
|
|
|
|
ByteSize*8));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle the 'partially zero' case.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-01 18:27:38 +00:00
|
|
|
/// getFoldedSizeOf - Return a ConstantExpr with type DestTy for sizeof
|
|
|
|
/// on Ty, with any known factors factored out. If Folded is false,
|
|
|
|
/// return null if no factoring was possible, to avoid endlessly
|
|
|
|
/// bouncing an unfoldable expression back into the top-level folder.
|
|
|
|
///
|
|
|
|
static Constant *getFoldedSizeOf(const Type *Ty, const Type *DestTy,
|
|
|
|
bool Folded) {
|
|
|
|
if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
Constant *N = ConstantInt::get(DestTy, ATy->getNumElements());
|
|
|
|
Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
|
|
|
|
return ConstantExpr::getNUWMul(E, N);
|
|
|
|
}
|
2010-02-25 16:05:33 +00:00
|
|
|
|
2010-02-01 18:27:38 +00:00
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Ty))
|
|
|
|
if (!STy->isPacked()) {
|
|
|
|
unsigned NumElems = STy->getNumElements();
|
|
|
|
// An empty struct has size zero.
|
|
|
|
if (NumElems == 0)
|
|
|
|
return ConstantExpr::getNullValue(DestTy);
|
2010-02-02 01:41:39 +00:00
|
|
|
// Check for a struct with all members having the same size.
|
|
|
|
Constant *MemberSize =
|
|
|
|
getFoldedSizeOf(STy->getElementType(0), DestTy, true);
|
2010-02-01 18:27:38 +00:00
|
|
|
bool AllSame = true;
|
|
|
|
for (unsigned i = 1; i != NumElems; ++i)
|
2010-02-02 01:41:39 +00:00
|
|
|
if (MemberSize !=
|
|
|
|
getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
|
2010-02-01 18:27:38 +00:00
|
|
|
AllSame = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (AllSame) {
|
|
|
|
Constant *N = ConstantInt::get(DestTy, NumElems);
|
2010-02-02 01:41:39 +00:00
|
|
|
return ConstantExpr::getNUWMul(MemberSize, N);
|
2010-02-01 18:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 06:13:07 +00:00
|
|
|
// Pointer size doesn't depend on the pointee type, so canonicalize them
|
|
|
|
// to an arbitrary pointee.
|
|
|
|
if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
|
2010-02-15 16:12:20 +00:00
|
|
|
if (!PTy->getElementType()->isIntegerTy(1))
|
2010-02-10 06:13:07 +00:00
|
|
|
return
|
|
|
|
getFoldedSizeOf(PointerType::get(IntegerType::get(PTy->getContext(), 1),
|
|
|
|
PTy->getAddressSpace()),
|
|
|
|
DestTy, true);
|
|
|
|
|
2010-02-01 18:27:38 +00:00
|
|
|
// If there's no interesting folding happening, bail so that we don't create
|
|
|
|
// a constant that looks like it needs folding but really doesn't.
|
|
|
|
if (!Folded)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Base case: Get a regular sizeof expression.
|
|
|
|
Constant *C = ConstantExpr::getSizeOf(Ty);
|
|
|
|
C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
|
|
|
|
DestTy, false),
|
|
|
|
C, DestTy);
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2010-02-02 01:41:39 +00:00
|
|
|
/// getFoldedAlignOf - Return a ConstantExpr with type DestTy for alignof
|
|
|
|
/// on Ty, with any known factors factored out. If Folded is false,
|
|
|
|
/// return null if no factoring was possible, to avoid endlessly
|
|
|
|
/// bouncing an unfoldable expression back into the top-level folder.
|
|
|
|
///
|
|
|
|
static Constant *getFoldedAlignOf(const Type *Ty, const Type *DestTy,
|
|
|
|
bool Folded) {
|
|
|
|
// The alignment of an array is equal to the alignment of the
|
|
|
|
// array element. Note that this is not always true for vectors.
|
|
|
|
if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
Constant *C = ConstantExpr::getAlignOf(ATy->getElementType());
|
|
|
|
C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
|
|
|
|
DestTy,
|
|
|
|
false),
|
|
|
|
C, DestTy);
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
|
|
|
|
// Packed structs always have an alignment of 1.
|
|
|
|
if (STy->isPacked())
|
|
|
|
return ConstantInt::get(DestTy, 1);
|
|
|
|
|
|
|
|
// Otherwise, struct alignment is the maximum alignment of any member.
|
|
|
|
// Without target data, we can't compare much, but we can check to see
|
|
|
|
// if all the members have the same alignment.
|
|
|
|
unsigned NumElems = STy->getNumElements();
|
|
|
|
// An empty struct has minimal alignment.
|
|
|
|
if (NumElems == 0)
|
|
|
|
return ConstantInt::get(DestTy, 1);
|
|
|
|
// Check for a struct with all members having the same alignment.
|
|
|
|
Constant *MemberAlign =
|
|
|
|
getFoldedAlignOf(STy->getElementType(0), DestTy, true);
|
|
|
|
bool AllSame = true;
|
|
|
|
for (unsigned i = 1; i != NumElems; ++i)
|
|
|
|
if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) {
|
|
|
|
AllSame = false;
|
2010-02-25 16:45:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (AllSame)
|
|
|
|
return MemberAlign;
|
|
|
|
}
|
|
|
|
|
2010-02-10 06:13:07 +00:00
|
|
|
// Pointer alignment doesn't depend on the pointee type, so canonicalize them
|
|
|
|
// to an arbitrary pointee.
|
|
|
|
if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
|
2010-02-15 16:12:20 +00:00
|
|
|
if (!PTy->getElementType()->isIntegerTy(1))
|
2010-02-10 06:13:07 +00:00
|
|
|
return
|
|
|
|
getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(),
|
|
|
|
1),
|
|
|
|
PTy->getAddressSpace()),
|
|
|
|
DestTy, true);
|
|
|
|
|
2010-02-02 01:41:39 +00:00
|
|
|
// If there's no interesting folding happening, bail so that we don't create
|
|
|
|
// a constant that looks like it needs folding but really doesn't.
|
|
|
|
if (!Folded)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Base case: Get a regular alignof expression.
|
|
|
|
Constant *C = ConstantExpr::getAlignOf(Ty);
|
|
|
|
C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
|
|
|
|
DestTy, false),
|
|
|
|
C, DestTy);
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2010-02-01 18:27:38 +00:00
|
|
|
/// getFoldedOffsetOf - Return a ConstantExpr with type DestTy for offsetof
|
|
|
|
/// on Ty and FieldNo, with any known factors factored out. If Folded is false,
|
|
|
|
/// return null if no factoring was possible, to avoid endlessly
|
|
|
|
/// bouncing an unfoldable expression back into the top-level folder.
|
|
|
|
///
|
|
|
|
static Constant *getFoldedOffsetOf(const Type *Ty, Constant *FieldNo,
|
|
|
|
const Type *DestTy,
|
|
|
|
bool Folded) {
|
|
|
|
if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false,
|
|
|
|
DestTy, false),
|
|
|
|
FieldNo, DestTy);
|
|
|
|
Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
|
|
|
|
return ConstantExpr::getNUWMul(E, N);
|
|
|
|
}
|
2010-02-25 16:05:33 +00:00
|
|
|
|
2010-02-01 18:27:38 +00:00
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Ty))
|
|
|
|
if (!STy->isPacked()) {
|
|
|
|
unsigned NumElems = STy->getNumElements();
|
|
|
|
// An empty struct has no members.
|
|
|
|
if (NumElems == 0)
|
|
|
|
return 0;
|
2010-02-02 01:41:39 +00:00
|
|
|
// Check for a struct with all members having the same size.
|
|
|
|
Constant *MemberSize =
|
|
|
|
getFoldedSizeOf(STy->getElementType(0), DestTy, true);
|
2010-02-01 18:27:38 +00:00
|
|
|
bool AllSame = true;
|
|
|
|
for (unsigned i = 1; i != NumElems; ++i)
|
2010-02-02 01:41:39 +00:00
|
|
|
if (MemberSize !=
|
|
|
|
getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
|
2010-02-01 18:27:38 +00:00
|
|
|
AllSame = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (AllSame) {
|
|
|
|
Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo,
|
|
|
|
false,
|
|
|
|
DestTy,
|
|
|
|
false),
|
|
|
|
FieldNo, DestTy);
|
2010-02-02 01:41:39 +00:00
|
|
|
return ConstantExpr::getNUWMul(MemberSize, N);
|
2010-02-01 18:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no interesting folding happening, bail so that we don't create
|
|
|
|
// a constant that looks like it needs folding but really doesn't.
|
|
|
|
if (!Folded)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Base case: Get a regular offsetof expression.
|
|
|
|
Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo);
|
|
|
|
C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
|
|
|
|
DestTy, false),
|
|
|
|
C, DestTy);
|
|
|
|
return C;
|
|
|
|
}
|
2009-10-17 21:53:27 +00:00
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
|
2009-06-20 00:24:58 +00:00
|
|
|
const Type *DestTy) {
|
2007-07-20 22:09:02 +00:00
|
|
|
if (isa<UndefValue>(V)) {
|
|
|
|
// zext(undef) = 0, because the top bits will be zero.
|
|
|
|
// sext(undef) = 0, because the top bits will all be the same.
|
2008-02-19 06:22:12 +00:00
|
|
|
// [us]itofp(undef) = 0, because the result value is bounded.
|
|
|
|
if (opc == Instruction::ZExt || opc == Instruction::SExt ||
|
|
|
|
opc == Instruction::UIToFP || opc == Instruction::SIToFP)
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(DestTy);
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(DestTy);
|
2007-07-20 22:09:02 +00:00
|
|
|
}
|
2011-01-21 01:12:09 +00:00
|
|
|
|
2007-10-14 01:56:47 +00:00
|
|
|
// No compile-time operations on this type yet.
|
2009-10-05 05:54:46 +00:00
|
|
|
if (V->getType()->isPPC_FP128Ty() || DestTy->isPPC_FP128Ty())
|
2007-10-14 01:56:47 +00:00
|
|
|
return 0;
|
2006-11-27 01:05:10 +00:00
|
|
|
|
2011-01-21 01:12:09 +00:00
|
|
|
if (V->isNullValue() && !DestTy->isX86_MMXTy())
|
|
|
|
return Constant::getNullValue(DestTy);
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
// If the cast operand is a constant expression, there's a few things we can
|
|
|
|
// do to try to simplify it.
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
|
2006-11-27 01:05:10 +00:00
|
|
|
if (CE->isCast()) {
|
2006-12-04 02:46:44 +00:00
|
|
|
// Try hard to fold cast of cast because they are often eliminable.
|
2006-11-27 01:05:10 +00:00
|
|
|
if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
|
2004-01-12 22:07:24 +00:00
|
|
|
} else if (CE->getOpcode() == Instruction::GetElementPtr) {
|
|
|
|
// If all of the indexes in the GEP are null values, there is no pointer
|
|
|
|
// adjustment going on. We might as well cast the source pointer.
|
|
|
|
bool isAllNull = true;
|
|
|
|
for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
|
|
|
|
if (!CE->getOperand(i)->isNullValue()) {
|
|
|
|
isAllNull = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isAllNull)
|
2006-12-04 02:46:44 +00:00
|
|
|
// This is casting one pointer type to another, always BitCast
|
2009-07-29 18:55:55 +00:00
|
|
|
return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
|
2004-01-12 22:07:24 +00:00
|
|
|
}
|
2004-10-16 23:31:32 +00:00
|
|
|
}
|
2004-01-12 22:07:24 +00:00
|
|
|
|
2009-06-15 22:12:54 +00:00
|
|
|
// If the cast operand is a constant vector, perform the cast by
|
|
|
|
// operating on each element. In the cast of bitcasts, the element
|
|
|
|
// count may be mismatched; don't attempt to handle that here.
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
|
2010-02-16 11:11:14 +00:00
|
|
|
if (DestTy->isVectorTy() &&
|
2009-06-15 22:12:54 +00:00
|
|
|
cast<VectorType>(DestTy)->getNumElements() ==
|
|
|
|
CV->getType()->getNumElements()) {
|
|
|
|
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)
|
2009-07-29 18:55:55 +00:00
|
|
|
res.push_back(ConstantExpr::getCast(opc,
|
2009-06-20 00:24:58 +00:00
|
|
|
CV->getOperand(i), DstEltTy));
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(DestVecTy, res);
|
2009-06-15 22:12:54 +00:00
|
|
|
}
|
|
|
|
|
2006-12-19 03:15:47 +00:00
|
|
|
// We actually have to do a cast now. Perform the cast according to the
|
|
|
|
// opcode specified.
|
2006-11-27 01:05:10 +00:00
|
|
|
switch (opc) {
|
2009-10-17 21:53:27 +00:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Failed to cast constant expression");
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
|
2008-10-09 23:00:39 +00:00
|
|
|
bool ignored;
|
2007-09-19 14:22:58 +00:00
|
|
|
APFloat Val = FPC->getValueAPF();
|
2009-10-05 05:54:46 +00:00
|
|
|
Val.convert(DestTy->isFloatTy() ? APFloat::IEEEsingle :
|
|
|
|
DestTy->isDoubleTy() ? APFloat::IEEEdouble :
|
|
|
|
DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended :
|
|
|
|
DestTy->isFP128Ty() ? APFloat::IEEEquad :
|
2007-09-19 14:22:58 +00:00
|
|
|
APFloat::Bogus,
|
2008-10-09 23:00:39 +00:00
|
|
|
APFloat::rmNearestTiesToEven, &ignored);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFP::get(V->getContext(), Val);
|
2007-09-06 18:13:44 +00:00
|
|
|
}
|
2006-12-19 07:41:40 +00:00
|
|
|
return 0; // Can't fold.
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
|
2007-10-15 05:34:10 +00:00
|
|
|
const APFloat &V = FPC->getValueAPF();
|
2008-10-09 23:00:39 +00:00
|
|
|
bool ignored;
|
2007-09-19 14:22:58 +00:00
|
|
|
uint64_t x[2];
|
2007-02-27 06:23:51 +00:00
|
|
|
uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
|
2007-09-25 23:32:20 +00:00
|
|
|
(void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
|
2008-10-09 23:00:39 +00:00
|
|
|
APFloat::rmTowardZero, &ignored);
|
2007-09-19 14:22:58 +00:00
|
|
|
APInt Val(DestBitWidth, 2, x);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(FPC->getContext(), Val);
|
2007-02-27 06:23:51 +00:00
|
|
|
}
|
2006-12-19 07:41:40 +00:00
|
|
|
return 0; // Can't fold.
|
|
|
|
case Instruction::IntToPtr: //always treated as unsigned
|
|
|
|
if (V->isNullValue()) // Is it an integral null value?
|
2009-07-30 23:03:37 +00:00
|
|
|
return ConstantPointerNull::get(cast<PointerType>(DestTy));
|
2006-12-19 07:41:40 +00:00
|
|
|
return 0; // Other pointer types cannot be casted
|
|
|
|
case Instruction::PtrToInt: // always treated as unsigned
|
2010-01-28 02:15:55 +00:00
|
|
|
// Is it a null pointer value?
|
|
|
|
if (V->isNullValue())
|
2009-07-24 23:12:02 +00:00
|
|
|
return ConstantInt::get(DestTy, 0);
|
2010-02-01 18:27:38 +00:00
|
|
|
// If this is a sizeof-like expression, pull out multiplications by
|
|
|
|
// known factors to expose them to subsequent folding. If it's an
|
|
|
|
// alignof-like expression, factor out known factors.
|
2010-01-28 02:15:55 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
|
|
|
|
if (CE->getOpcode() == Instruction::GetElementPtr &&
|
2010-02-01 18:27:38 +00:00
|
|
|
CE->getOperand(0)->isNullValue()) {
|
|
|
|
const Type *Ty =
|
|
|
|
cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
|
|
|
|
if (CE->getNumOperands() == 2) {
|
|
|
|
// Handle a sizeof-like expression.
|
|
|
|
Constant *Idx = CE->getOperand(1);
|
|
|
|
bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
|
|
|
|
if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) {
|
|
|
|
Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true,
|
2010-01-28 02:15:55 +00:00
|
|
|
DestTy, false),
|
2010-02-01 18:27:38 +00:00
|
|
|
Idx, DestTy);
|
|
|
|
return ConstantExpr::getMul(C, Idx);
|
|
|
|
}
|
|
|
|
} else if (CE->getNumOperands() == 3 &&
|
|
|
|
CE->getOperand(1)->isNullValue()) {
|
|
|
|
// Handle an alignof-like expression.
|
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Ty))
|
|
|
|
if (!STy->isPacked()) {
|
|
|
|
ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
|
|
|
|
if (CI->isOne() &&
|
|
|
|
STy->getNumElements() == 2 &&
|
2010-02-15 16:12:20 +00:00
|
|
|
STy->getElementType(0)->isIntegerTy(1)) {
|
2010-02-02 01:41:39 +00:00
|
|
|
return getFoldedAlignOf(STy->getElementType(1), DestTy, false);
|
2010-02-01 18:27:38 +00:00
|
|
|
}
|
2010-01-28 02:15:55 +00:00
|
|
|
}
|
2010-02-01 18:27:38 +00:00
|
|
|
// Handle an offsetof-like expression.
|
2010-06-05 00:47:34 +00:00
|
|
|
if (Ty->isStructTy() || Ty->isArrayTy()) {
|
2010-02-01 18:27:38 +00:00
|
|
|
if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2),
|
|
|
|
DestTy, false))
|
|
|
|
return C;
|
2010-01-28 02:15:55 +00:00
|
|
|
}
|
2010-02-01 18:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-28 02:15:55 +00:00
|
|
|
// Other pointer types cannot be casted
|
|
|
|
return 0;
|
2006-12-19 07:41:40 +00:00
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2007-09-30 18:19:03 +00:00
|
|
|
APInt api = CI->getValue();
|
2010-12-29 01:33:36 +00:00
|
|
|
APFloat apf(APInt::getNullValue(DestTy->getPrimitiveSizeInBits()), true);
|
2008-02-29 01:42:52 +00:00
|
|
|
(void)apf.convertFromAPInt(api,
|
|
|
|
opc==Instruction::SIToFP,
|
|
|
|
APFloat::rmNearestTiesToEven);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFP::get(V->getContext(), apf);
|
2007-09-06 18:13:44 +00:00
|
|
|
}
|
2006-12-19 07:41:40 +00:00
|
|
|
return 0;
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::ZExt:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2007-02-27 06:23:51 +00:00
|
|
|
uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
|
2010-12-07 08:25:19 +00:00
|
|
|
return ConstantInt::get(V->getContext(),
|
|
|
|
CI->getValue().zext(BitWidth));
|
2007-02-27 06:23:51 +00:00
|
|
|
}
|
2006-12-19 03:15:47 +00:00
|
|
|
return 0;
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::SExt:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2007-02-27 06:23:51 +00:00
|
|
|
uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
|
2010-12-07 08:25:19 +00:00
|
|
|
return ConstantInt::get(V->getContext(),
|
|
|
|
CI->getValue().sext(BitWidth));
|
2007-02-27 06:23:51 +00:00
|
|
|
}
|
2006-12-19 03:15:47 +00:00
|
|
|
return 0;
|
2009-10-17 21:53:27 +00:00
|
|
|
case Instruction::Trunc: {
|
|
|
|
uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
|
2010-12-07 08:25:19 +00:00
|
|
|
return ConstantInt::get(V->getContext(),
|
|
|
|
CI->getValue().trunc(DestBitWidth));
|
2007-02-27 06:23:51 +00:00
|
|
|
}
|
2009-10-17 21:53:27 +00:00
|
|
|
|
|
|
|
// The input must be a constantexpr. See if we can simplify this based on
|
|
|
|
// the bytes we are demanding. Only do this if the source and dest are an
|
|
|
|
// even multiple of a byte.
|
|
|
|
if ((DestBitWidth & 7) == 0 &&
|
|
|
|
(cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
|
|
|
|
if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
|
|
|
|
return Res;
|
|
|
|
|
2006-12-01 19:22:41 +00:00
|
|
|
return 0;
|
2009-10-17 21:53:27 +00:00
|
|
|
}
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::BitCast:
|
2010-02-01 20:48:08 +00:00
|
|
|
return FoldBitCast(V, DestTy);
|
2006-04-02 01:38:28 +00:00
|
|
|
}
|
2004-01-12 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *V1, Constant *V2) {
|
|
|
|
if (ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
|
|
|
|
return CB->getZExtValue() ? V1 : V2;
|
|
|
|
|
|
|
|
if (isa<UndefValue>(V1)) return V2;
|
|
|
|
if (isa<UndefValue>(V2)) return V1;
|
|
|
|
if (isa<UndefValue>(Cond)) return V1;
|
|
|
|
if (V1 == V2) return V1;
|
2011-01-29 20:35:06 +00:00
|
|
|
|
|
|
|
if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
|
|
|
|
if (TrueVal->getOpcode() == Instruction::Select)
|
|
|
|
if (TrueVal->getOperand(0) == Cond)
|
|
|
|
return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
|
|
|
|
}
|
|
|
|
if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
|
|
|
|
if (FalseVal->getOpcode() == Instruction::Select)
|
|
|
|
if (FalseVal->getOperand(0) == Cond)
|
|
|
|
return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
|
|
|
|
}
|
|
|
|
|
2004-03-12 05:53:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *Idx) {
|
2006-03-31 18:31:40 +00:00
|
|
|
if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
|
2006-04-07 04:44:06 +00:00
|
|
|
if (Val->isNullValue()) // ee(zero, x) -> zero
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(
|
2007-02-15 02:26:10 +00:00
|
|
|
cast<VectorType>(Val->getType())->getElementType());
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
|
|
|
|
if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
|
2008-05-10 08:32:32 +00:00
|
|
|
return CVal->getOperand(CIdx->getZExtValue());
|
2006-03-31 18:31:40 +00:00
|
|
|
} else if (isa<UndefValue>(Idx)) {
|
|
|
|
// ee({w,x,y,z}, undef) -> w (an arbitrary value).
|
2008-05-10 08:32:32 +00:00
|
|
|
return CVal->getOperand(0);
|
2006-01-10 20:03:46 +00:00
|
|
|
}
|
2006-03-31 18:31:40 +00:00
|
|
|
}
|
2006-01-10 20:03:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *Elt,
|
|
|
|
Constant *Idx) {
|
|
|
|
ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
|
2006-01-17 20:07:22 +00:00
|
|
|
if (!CIdx) return 0;
|
2007-02-27 06:23:51 +00:00
|
|
|
APInt idxVal = CIdx->getValue();
|
2006-11-02 08:18:15 +00:00
|
|
|
if (isa<UndefValue>(Val)) {
|
2007-07-16 14:29:03 +00:00
|
|
|
// Insertion of scalar constant into vector undef
|
2006-01-17 20:07:22 +00:00
|
|
|
// Optimize away insertion of undef
|
|
|
|
if (isa<UndefValue>(Elt))
|
2009-09-20 01:35:59 +00:00
|
|
|
return Val;
|
2006-01-17 20:07:22 +00:00
|
|
|
// Otherwise break the aggregate undef into multiple undefs and do
|
|
|
|
// the insertion
|
|
|
|
unsigned numOps =
|
2007-02-15 02:26:10 +00:00
|
|
|
cast<VectorType>(Val->getType())->getNumElements();
|
2006-01-17 20:07:22 +00:00
|
|
|
std::vector<Constant*> Ops;
|
|
|
|
Ops.reserve(numOps);
|
|
|
|
for (unsigned i = 0; i < numOps; ++i) {
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *Op =
|
2009-07-30 23:03:37 +00:00
|
|
|
(idxVal == i) ? Elt : UndefValue::get(Elt->getType());
|
2009-09-20 01:35:59 +00:00
|
|
|
Ops.push_back(Op);
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Ops);
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
2006-11-02 08:18:15 +00:00
|
|
|
if (isa<ConstantAggregateZero>(Val)) {
|
2007-07-16 14:29:03 +00:00
|
|
|
// Insertion of scalar constant into vector aggregate zero
|
2006-01-17 20:07:22 +00:00
|
|
|
// Optimize away insertion of zero
|
|
|
|
if (Elt->isNullValue())
|
2009-09-20 01:35:59 +00:00
|
|
|
return Val;
|
2006-01-17 20:07:22 +00:00
|
|
|
// Otherwise break the aggregate zero into multiple zeros and do
|
|
|
|
// the insertion
|
|
|
|
unsigned numOps =
|
2007-02-15 02:26:10 +00:00
|
|
|
cast<VectorType>(Val->getType())->getNumElements();
|
2006-01-17 20:07:22 +00:00
|
|
|
std::vector<Constant*> Ops;
|
|
|
|
Ops.reserve(numOps);
|
|
|
|
for (unsigned i = 0; i < numOps; ++i) {
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *Op =
|
2009-07-31 20:28:14 +00:00
|
|
|
(idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
|
2009-09-20 01:35:59 +00:00
|
|
|
Ops.push_back(Op);
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Ops);
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
|
2007-07-16 14:29:03 +00:00
|
|
|
// Insertion of scalar constant into vector constant
|
2006-01-17 20:07:22 +00:00
|
|
|
std::vector<Constant*> Ops;
|
|
|
|
Ops.reserve(CVal->getNumOperands());
|
|
|
|
for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *Op =
|
2007-02-27 06:23:51 +00:00
|
|
|
(idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
|
2009-09-20 01:35:59 +00:00
|
|
|
Ops.push_back(Op);
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Ops);
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
2008-06-03 00:15:20 +00:00
|
|
|
|
2006-01-17 20:07:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
/// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef
|
|
|
|
/// return the specified element value. Otherwise return null.
|
2010-02-01 20:48:08 +00:00
|
|
|
static Constant *GetVectorElement(Constant *C, unsigned EltNo) {
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantVector *CV = dyn_cast<ConstantVector>(C))
|
2008-05-10 08:32:32 +00:00
|
|
|
return CV->getOperand(EltNo);
|
2009-08-29 23:35:16 +00:00
|
|
|
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
const Type *EltTy = cast<VectorType>(C->getType())->getElementType();
|
|
|
|
if (isa<ConstantAggregateZero>(C))
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(EltTy);
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
if (isa<UndefValue>(C))
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(EltTy);
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *V2,
|
|
|
|
Constant *Mask) {
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
// Undefined shuffle mask -> undefined value.
|
2009-07-30 23:03:37 +00:00
|
|
|
if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType());
|
2008-11-10 04:46:22 +00:00
|
|
|
|
|
|
|
unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements();
|
|
|
|
unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements();
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
|
2008-11-10 04:46:22 +00:00
|
|
|
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
// Loop over the shuffle mask, evaluating each element.
|
|
|
|
SmallVector<Constant*, 32> Result;
|
2008-11-10 04:46:22 +00:00
|
|
|
for (unsigned i = 0; i != MaskNumElts; ++i) {
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *InElt = GetVectorElement(Mask, i);
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
if (InElt == 0) return 0;
|
2008-11-10 04:46:22 +00:00
|
|
|
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
if (isa<UndefValue>(InElt))
|
2009-07-30 23:03:37 +00:00
|
|
|
InElt = UndefValue::get(EltTy);
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) {
|
|
|
|
unsigned Elt = CI->getZExtValue();
|
2008-11-10 04:46:22 +00:00
|
|
|
if (Elt >= SrcNumElts*2)
|
2009-07-30 23:03:37 +00:00
|
|
|
InElt = UndefValue::get(EltTy);
|
2008-11-10 04:46:22 +00:00
|
|
|
else if (Elt >= SrcNumElts)
|
2010-02-01 20:48:08 +00:00
|
|
|
InElt = GetVectorElement(V2, Elt - SrcNumElts);
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
else
|
2010-02-01 20:48:08 +00:00
|
|
|
InElt = GetVectorElement(V1, Elt);
|
Teach VMCore to constant fold shufflevectors with constant operands.
This allows us to compile:
#include <emmintrin.h>
typedef __m128i VSInt16;
typedef short vSInt16 __attribute__ ((__vector_size__ (16)));
VSInt16 t3() {
return (VSInt16)((vSInt16)_mm_set1_epi16(6518));
}
into:
_t3:
movaps LCPI1_0, %xmm0
ret
instead of:
_t3:
movl $6518, %eax
movd %eax, %xmm0
pextrw $0, %xmm0, %eax
xorps %xmm0, %xmm0
pinsrw $0, %eax, %xmm0
punpcklwd %xmm0, %xmm0
pshufd $0, %xmm0, %xmm0
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44856 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-11 07:49:37 +00:00
|
|
|
if (InElt == 0) return 0;
|
|
|
|
} else {
|
|
|
|
// Unknown value.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Result.push_back(InElt);
|
|
|
|
}
|
2008-11-10 04:46:22 +00:00
|
|
|
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(&Result[0], Result.size());
|
2006-04-08 01:18:18 +00:00
|
|
|
}
|
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
|
2008-06-03 00:15:20 +00:00
|
|
|
const unsigned *Idxs,
|
|
|
|
unsigned NumIdx) {
|
|
|
|
// Base case: no indices, so return the entire value.
|
|
|
|
if (NumIdx == 0)
|
2009-09-20 01:35:59 +00:00
|
|
|
return Agg;
|
2008-06-03 00:15:20 +00:00
|
|
|
|
|
|
|
if (isa<UndefValue>(Agg)) // ev(undef, x) -> undef
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(),
|
2008-06-03 00:15:20 +00:00
|
|
|
Idxs,
|
|
|
|
Idxs + NumIdx));
|
|
|
|
|
|
|
|
if (isa<ConstantAggregateZero>(Agg)) // ev(0, x) -> 0
|
|
|
|
return
|
2009-07-31 20:28:14 +00:00
|
|
|
Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
|
2008-06-03 00:15:20 +00:00
|
|
|
Idxs,
|
|
|
|
Idxs + NumIdx));
|
|
|
|
|
|
|
|
// Otherwise recurse.
|
2009-10-28 05:14:34 +00:00
|
|
|
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg))
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFoldExtractValueInstruction(CS->getOperand(*Idxs),
|
2009-10-28 05:14:34 +00:00
|
|
|
Idxs+1, NumIdx-1);
|
|
|
|
|
|
|
|
if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg))
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFoldExtractValueInstruction(CA->getOperand(*Idxs),
|
2009-10-28 05:14:34 +00:00
|
|
|
Idxs+1, NumIdx-1);
|
|
|
|
ConstantVector *CV = cast<ConstantVector>(Agg);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFoldExtractValueInstruction(CV->getOperand(*Idxs),
|
2008-06-03 00:15:20 +00:00
|
|
|
Idxs+1, NumIdx-1);
|
2008-05-15 19:50:34 +00:00
|
|
|
}
|
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *Val,
|
2008-06-03 00:15:20 +00:00
|
|
|
const unsigned *Idxs,
|
|
|
|
unsigned NumIdx) {
|
|
|
|
// Base case: no indices, so replace the entire value.
|
|
|
|
if (NumIdx == 0)
|
2009-09-20 01:35:59 +00:00
|
|
|
return Val;
|
2008-06-03 00:15:20 +00:00
|
|
|
|
|
|
|
if (isa<UndefValue>(Agg)) {
|
|
|
|
// Insertion of constant into aggregate undef
|
2009-09-15 06:28:12 +00:00
|
|
|
// Optimize away insertion of undef.
|
2008-06-03 00:15:20 +00:00
|
|
|
if (isa<UndefValue>(Val))
|
2009-09-20 01:35:59 +00:00
|
|
|
return Agg;
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2008-06-03 00:15:20 +00:00
|
|
|
// Otherwise break the aggregate undef into multiple undefs and do
|
2009-09-15 06:28:12 +00:00
|
|
|
// the insertion.
|
2008-06-03 00:15:20 +00:00
|
|
|
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();
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2008-06-03 00:15:20 +00:00
|
|
|
std::vector<Constant*> Ops(numOps);
|
|
|
|
for (unsigned i = 0; i < numOps; ++i) {
|
|
|
|
const Type *MemberTy = AggTy->getTypeAtIndex(i);
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *Op =
|
2008-06-03 00:15:20 +00:00
|
|
|
(*Idxs == i) ?
|
2010-02-01 20:48:08 +00:00
|
|
|
ConstantFoldInsertValueInstruction(UndefValue::get(MemberTy),
|
2008-06-03 00:15:20 +00:00
|
|
|
Val, Idxs+1, NumIdx-1) :
|
2009-07-30 23:03:37 +00:00
|
|
|
UndefValue::get(MemberTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Ops[i] = Op;
|
2008-06-03 00:15:20 +00:00
|
|
|
}
|
2009-09-15 06:28:12 +00:00
|
|
|
|
|
|
|
if (const StructType* ST = dyn_cast<StructType>(AggTy))
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantStruct::get(ST->getContext(), Ops, ST->isPacked());
|
2009-09-15 06:28:12 +00:00
|
|
|
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
|
2008-06-03 00:15:20 +00:00
|
|
|
}
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2008-06-03 00:15:20 +00:00
|
|
|
if (isa<ConstantAggregateZero>(Agg)) {
|
|
|
|
// Insertion of constant into aggregate zero
|
2009-09-15 06:28:12 +00:00
|
|
|
// Optimize away insertion of zero.
|
2008-06-03 00:15:20 +00:00
|
|
|
if (Val->isNullValue())
|
2009-09-20 01:35:59 +00:00
|
|
|
return Agg;
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2008-06-03 00:15:20 +00:00
|
|
|
// Otherwise break the aggregate zero into multiple zeros and do
|
2009-09-15 06:28:12 +00:00
|
|
|
// the insertion.
|
2008-06-03 00:15:20 +00:00
|
|
|
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();
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2008-06-03 00:15:20 +00:00
|
|
|
std::vector<Constant*> Ops(numOps);
|
|
|
|
for (unsigned i = 0; i < numOps; ++i) {
|
|
|
|
const Type *MemberTy = AggTy->getTypeAtIndex(i);
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *Op =
|
2008-06-03 00:15:20 +00:00
|
|
|
(*Idxs == i) ?
|
2010-02-01 20:48:08 +00:00
|
|
|
ConstantFoldInsertValueInstruction(Constant::getNullValue(MemberTy),
|
2008-06-03 00:15:20 +00:00
|
|
|
Val, Idxs+1, NumIdx-1) :
|
2009-07-31 20:28:14 +00:00
|
|
|
Constant::getNullValue(MemberTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Ops[i] = Op;
|
2008-06-03 00:15:20 +00:00
|
|
|
}
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
if (const StructType *ST = dyn_cast<StructType>(AggTy))
|
|
|
|
return ConstantStruct::get(ST->getContext(), Ops, ST->isPacked());
|
2009-09-15 06:28:12 +00:00
|
|
|
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
|
2008-06-03 00:15:20 +00:00
|
|
|
}
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2008-06-03 00:15:20 +00:00
|
|
|
if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
|
2009-09-15 06:28:12 +00:00
|
|
|
// Insertion of constant into aggregate constant.
|
2008-06-03 00:15:20 +00:00
|
|
|
std::vector<Constant*> Ops(Agg->getNumOperands());
|
|
|
|
for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
|
2009-10-28 05:14:34 +00:00
|
|
|
Constant *Op = cast<Constant>(Agg->getOperand(i));
|
|
|
|
if (*Idxs == i)
|
2010-02-01 20:48:08 +00:00
|
|
|
Op = ConstantFoldInsertValueInstruction(Op, Val, Idxs+1, NumIdx-1);
|
2009-09-20 01:35:59 +00:00
|
|
|
Ops[i] = Op;
|
2008-06-03 00:15:20 +00:00
|
|
|
}
|
2009-09-15 06:28:12 +00:00
|
|
|
|
|
|
|
if (const StructType* ST = dyn_cast<StructType>(Agg->getType()))
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantStruct::get(ST->getContext(), Ops, ST->isPacked());
|
2009-09-15 06:28:12 +00:00
|
|
|
return ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
|
2008-06-03 00:15:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 19:50:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *C1, Constant *C2) {
|
2007-10-16 23:38:29 +00:00
|
|
|
// No compile-time operations on this type yet.
|
2009-10-05 05:54:46 +00:00
|
|
|
if (C1->getType()->isPPC_FP128Ty())
|
2007-10-16 23:38:29 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-09-15 06:28:12 +00:00
|
|
|
// Handle UndefValue up front.
|
2006-12-23 06:05:41 +00:00
|
|
|
if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
|
|
|
|
switch (Opcode) {
|
2008-03-25 20:08:07 +00:00
|
|
|
case Instruction::Xor:
|
|
|
|
if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
|
|
|
|
// Handle undef ^ undef -> 0 special case. This is a common
|
|
|
|
// idiom (misuse).
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(C1->getType());
|
2008-03-25 20:08:07 +00:00
|
|
|
// Fallthrough
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(C1->getType());
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::And:
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(C1->getType());
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
if (!isa<UndefValue>(C2)) // undef / X -> 0
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(C1->getType());
|
2009-09-20 01:35:59 +00:00
|
|
|
return C2; // X / undef -> undef
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::Or: // X | undef -> -1
|
2007-02-15 02:26:10 +00:00
|
|
|
if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getAllOnesValue(PTy);
|
|
|
|
return Constant::getAllOnesValue(C1->getType());
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::LShr:
|
|
|
|
if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1; // undef lshr undef -> undef
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
|
2006-12-23 06:05:41 +00:00
|
|
|
// undef lshr X -> 0
|
|
|
|
case Instruction::AShr:
|
2011-01-14 00:37:45 +00:00
|
|
|
if (!isa<UndefValue>(C2)) // undef ashr X --> all ones
|
|
|
|
return Constant::getAllOnesValue(C1->getType());
|
2006-12-23 06:05:41 +00:00
|
|
|
else if (isa<UndefValue>(C1))
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1; // undef ashr undef -> undef
|
2006-12-23 06:05:41 +00:00
|
|
|
else
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1; // X ashr undef --> X
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::Shl:
|
|
|
|
// undef << X -> 0 or X << undef -> 0
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(C1->getType());
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-21 01:56:41 +00:00
|
|
|
// Handle simplifications when the RHS is a constant int.
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
|
2008-04-19 21:58:19 +00:00
|
|
|
switch (Opcode) {
|
|
|
|
case Instruction::Add:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (CI2->equalsInt(0)) return C1; // X + 0 == X
|
2008-04-19 21:58:19 +00:00
|
|
|
break;
|
|
|
|
case Instruction::Sub:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (CI2->equalsInt(0)) return C1; // X - 0 == X
|
2008-04-19 21:58:19 +00:00
|
|
|
break;
|
|
|
|
case Instruction::Mul:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (CI2->equalsInt(0)) return C2; // X * 0 == 0
|
2008-04-19 21:58:19 +00:00
|
|
|
if (CI2->equalsInt(1))
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1; // X * 1 == X
|
2008-04-19 21:58:19 +00:00
|
|
|
break;
|
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
if (CI2->equalsInt(1))
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1; // X / 1 == X
|
2009-01-19 21:55:26 +00:00
|
|
|
if (CI2->equalsInt(0))
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(CI2->getType()); // X / 0 == undef
|
2008-04-19 21:58:19 +00:00
|
|
|
break;
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
if (CI2->equalsInt(1))
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(CI2->getType()); // X % 1 == 0
|
2009-01-19 21:55:26 +00:00
|
|
|
if (CI2->equalsInt(0))
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(CI2->getType()); // X % 0 == undef
|
2008-04-19 21:58:19 +00:00
|
|
|
break;
|
|
|
|
case Instruction::And:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (CI2->isZero()) return C2; // X & 0 == 0
|
2008-04-19 21:58:19 +00:00
|
|
|
if (CI2->isAllOnesValue())
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1; // X & -1 == X
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
|
2008-04-19 22:17:26 +00:00
|
|
|
// (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
|
2008-04-19 21:58:19 +00:00
|
|
|
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)
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1;
|
2007-03-25 05:47:04 +00:00
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2008-04-19 22:17:26 +00:00
|
|
|
// If and'ing the address of a global with a constant, fold it.
|
2008-04-19 21:58:19 +00:00
|
|
|
if (CE1->getOpcode() == Instruction::PtrToInt &&
|
|
|
|
isa<GlobalValue>(CE1->getOperand(0))) {
|
2008-04-19 22:17:26 +00:00
|
|
|
GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2008-04-19 22:17:26 +00:00
|
|
|
// Functions are at least 4-byte aligned.
|
|
|
|
unsigned GVAlign = GV->getAlignment();
|
|
|
|
if (isa<Function>(GV))
|
|
|
|
GVAlign = std::max(GVAlign, 4U);
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2008-04-19 22:17:26 +00:00
|
|
|
if (GVAlign > 1) {
|
|
|
|
unsigned DstWidth = CI2->getType()->getBitWidth();
|
2008-04-20 19:59:12 +00:00
|
|
|
unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
|
2008-04-19 22:17:26 +00:00
|
|
|
APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
|
|
|
|
|
|
|
|
// If checking bits we know are clear, return zero.
|
|
|
|
if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(CI2->getType());
|
2008-04-19 22:17:26 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
2008-04-19 21:58:19 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Instruction::Or:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (CI2->equalsInt(0)) return C1; // X | 0 == X
|
2008-04-19 21:58:19 +00:00
|
|
|
if (CI2->isAllOnesValue())
|
2009-09-20 01:35:59 +00:00
|
|
|
return C2; // X | -1 == -1
|
2008-04-19 21:58:19 +00:00
|
|
|
break;
|
|
|
|
case Instruction::Xor:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (CI2->equalsInt(0)) return C1; // X ^ 0 == X
|
2009-09-20 06:24:51 +00:00
|
|
|
|
|
|
|
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
|
|
|
|
switch (CE1->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case Instruction::ICmp:
|
|
|
|
case Instruction::FCmp:
|
2009-09-20 06:27:35 +00:00
|
|
|
// cmp pred ^ true -> cmp !pred
|
2009-09-20 06:24:51 +00:00
|
|
|
assert(CI2->equalsInt(1));
|
2009-09-20 06:26:34 +00:00
|
|
|
CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
|
2009-09-20 06:24:51 +00:00
|
|
|
pred = CmpInst::getInversePredicate(pred);
|
|
|
|
return ConstantExpr::getCompare(pred, CE1->getOperand(0),
|
|
|
|
CE1->getOperand(1));
|
|
|
|
}
|
|
|
|
}
|
2008-04-19 21:58:19 +00:00
|
|
|
break;
|
|
|
|
case Instruction::AShr:
|
|
|
|
// ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
|
2007-03-25 05:47:04 +00:00
|
|
|
if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
|
2009-09-20 01:35:59 +00:00
|
|
|
return ConstantExpr::getLShr(C1, C2);
|
2008-04-19 21:58:19 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
2010-02-22 22:43:23 +00:00
|
|
|
} else if (isa<ConstantInt>(C1)) {
|
|
|
|
// If C1 is a ConstantInt and C2 is not, swap the operands.
|
|
|
|
if (Instruction::isCommutative(Opcode))
|
|
|
|
return ConstantExpr::get(Opcode, C2, C1);
|
2008-04-19 21:58:19 +00:00
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2008-04-20 18:24:14 +00:00
|
|
|
// At this point we know neither constant is an UndefValue.
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
|
|
|
|
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
|
2007-02-27 06:23:51 +00:00
|
|
|
using namespace APIntOps;
|
2008-04-20 18:24:14 +00:00
|
|
|
const APInt &C1V = CI1->getValue();
|
|
|
|
const APInt &C2V = CI2->getValue();
|
2007-01-12 18:42:52 +00:00
|
|
|
switch (Opcode) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case Instruction::Add:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V + C2V);
|
2007-01-12 18:42:52 +00:00
|
|
|
case Instruction::Sub:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V - C2V);
|
2007-01-12 18:42:52 +00:00
|
|
|
case Instruction::Mul:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V * C2V);
|
2007-01-12 18:42:52 +00:00
|
|
|
case Instruction::UDiv:
|
2009-01-19 21:55:26 +00:00
|
|
|
assert(!CI2->isNullValue() && "Div by zero handled above");
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
|
2007-01-12 18:42:52 +00:00
|
|
|
case Instruction::SDiv:
|
2009-01-19 21:55:26 +00:00
|
|
|
assert(!CI2->isNullValue() && "Div by zero handled above");
|
2007-02-27 06:23:51 +00:00
|
|
|
if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
|
2007-02-27 06:23:51 +00:00
|
|
|
case Instruction::URem:
|
2009-01-19 21:55:26 +00:00
|
|
|
assert(!CI2->isNullValue() && "Div by zero handled above");
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
|
2009-01-19 21:55:26 +00:00
|
|
|
case Instruction::SRem:
|
|
|
|
assert(!CI2->isNullValue() && "Div by zero handled above");
|
2007-02-27 06:23:51 +00:00
|
|
|
if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
|
2007-01-12 18:42:52 +00:00
|
|
|
case Instruction::And:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V & C2V);
|
2007-01-12 18:42:52 +00:00
|
|
|
case Instruction::Or:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V | C2V);
|
2007-01-12 18:42:52 +00:00
|
|
|
case Instruction::Xor:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
|
2008-04-20 18:24:14 +00:00
|
|
|
case Instruction::Shl: {
|
|
|
|
uint32_t shiftAmt = C2V.getZExtValue();
|
|
|
|
if (shiftAmt < C1V.getBitWidth())
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V.shl(shiftAmt));
|
2008-04-20 18:24:14 +00:00
|
|
|
else
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(C1->getType()); // too big shift is undef
|
2008-04-20 18:24:14 +00:00
|
|
|
}
|
|
|
|
case Instruction::LShr: {
|
|
|
|
uint32_t shiftAmt = C2V.getZExtValue();
|
|
|
|
if (shiftAmt < C1V.getBitWidth())
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V.lshr(shiftAmt));
|
2008-04-20 18:24:14 +00:00
|
|
|
else
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(C1->getType()); // too big shift is undef
|
2008-04-20 18:24:14 +00:00
|
|
|
}
|
|
|
|
case Instruction::AShr: {
|
|
|
|
uint32_t shiftAmt = C2V.getZExtValue();
|
|
|
|
if (shiftAmt < C1V.getBitWidth())
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(CI1->getContext(), C1V.ashr(shiftAmt));
|
2008-04-20 18:24:14 +00:00
|
|
|
else
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(C1->getType()); // too big shift is undef
|
2008-04-20 18:24:14 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-21 01:56:41 +00:00
|
|
|
|
|
|
|
switch (Opcode) {
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
|
|
|
case Instruction::Shl:
|
2009-09-20 01:35:59 +00:00
|
|
|
if (CI1->equalsInt(0)) return C1;
|
2009-06-21 01:56:41 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-09-20 01:35:59 +00:00
|
|
|
} else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
|
|
|
|
if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
|
2007-09-06 18:13:44 +00:00
|
|
|
APFloat C1V = CFP1->getValueAPF();
|
|
|
|
APFloat C2V = CFP2->getValueAPF();
|
|
|
|
APFloat C3V = C1V; // copy for modification
|
2006-12-23 06:05:41 +00:00
|
|
|
switch (Opcode) {
|
|
|
|
default:
|
|
|
|
break;
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FAdd:
|
2007-09-06 18:13:44 +00:00
|
|
|
(void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFP::get(C1->getContext(), C3V);
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FSub:
|
2007-09-06 18:13:44 +00:00
|
|
|
(void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFP::get(C1->getContext(), C3V);
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FMul:
|
2007-09-06 18:13:44 +00:00
|
|
|
(void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFP::get(C1->getContext(), C3V);
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::FDiv:
|
2007-09-06 18:13:44 +00:00
|
|
|
(void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFP::get(C1->getContext(), C3V);
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::FRem:
|
2007-09-06 18:13:44 +00:00
|
|
|
(void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFP::get(C1->getContext(), C3V);
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-30 19:00:49 +00:00
|
|
|
} else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
|
|
|
|
ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
|
2007-10-31 21:36:31 +00:00
|
|
|
if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
|
|
|
|
(CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
|
2009-07-13 22:41:06 +00:00
|
|
|
std::vector<Constant*> Res;
|
|
|
|
const Type* EltTy = VTy->getElementType();
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *C1 = 0;
|
|
|
|
Constant *C2 = 0;
|
2006-12-23 06:05:41 +00:00
|
|
|
switch (Opcode) {
|
2008-04-19 21:13:00 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::Add:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getAdd(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FAdd:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getFAdd(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::Sub:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getSub(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FSub:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getFSub(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::Mul:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getMul(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-06-04 22:49:04 +00:00
|
|
|
case Instruction::FMul:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getFMul(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2008-04-19 21:13:00 +00:00
|
|
|
case Instruction::UDiv:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getUDiv(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2008-04-19 21:13:00 +00:00
|
|
|
case Instruction::SDiv:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getSDiv(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2008-04-19 21:13:00 +00:00
|
|
|
case Instruction::FDiv:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getFDiv(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2008-04-19 21:13:00 +00:00
|
|
|
case Instruction::URem:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getURem(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2008-04-19 21:13:00 +00:00
|
|
|
case Instruction::SRem:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getSRem(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2008-04-19 21:13:00 +00:00
|
|
|
case Instruction::FRem:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getFRem(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2008-04-19 21:13:00 +00:00
|
|
|
case Instruction::And:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getAnd(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-07-13 22:41:06 +00:00
|
|
|
case Instruction::Or:
|
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getOr(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-07-13 22:41:06 +00:00
|
|
|
case Instruction::Xor:
|
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getXor(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-03-14 17:09:17 +00:00
|
|
|
case Instruction::LShr:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getLShr(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-03-14 17:09:17 +00:00
|
|
|
case Instruction::AShr:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getAShr(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2009-03-14 17:09:17 +00:00
|
|
|
case Instruction::Shl:
|
2009-07-13 22:41:06 +00:00
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
2009-07-31 20:28:14 +00:00
|
|
|
C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
|
|
|
|
C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
|
2009-09-20 01:35:59 +00:00
|
|
|
Res.push_back(ConstantExpr::getShl(C1, C2));
|
2009-07-13 22:41:06 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(Res);
|
2007-10-31 21:36:31 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-01 18:27:38 +00:00
|
|
|
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
|
2008-04-20 18:24:14 +00:00
|
|
|
// There are many possible foldings we could do here. We should probably
|
|
|
|
// at least fold add of a pointer with an integer into the appropriate
|
|
|
|
// getelementptr. This will improve alias analysis a bit.
|
2010-02-01 18:27:38 +00:00
|
|
|
|
|
|
|
// Given ((a + b) + c), if (b + c) folds to something interesting, return
|
|
|
|
// (a + (b + c)).
|
2010-12-20 13:10:23 +00:00
|
|
|
if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
|
2010-02-01 18:27:38 +00:00
|
|
|
Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
|
|
|
|
if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
|
|
|
|
return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
|
|
|
|
}
|
2008-04-20 18:24:14 +00:00
|
|
|
} else if (isa<ConstantExpr>(C2)) {
|
|
|
|
// If C2 is a constant expr and C1 isn't, flop them around and fold the
|
|
|
|
// other way if possible.
|
2010-02-22 22:05:18 +00:00
|
|
|
if (Instruction::isCommutative(Opcode))
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantFoldBinaryInstruction(Opcode, C2, C1);
|
2008-04-20 18:24:14 +00:00
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2009-09-20 00:04:02 +00:00
|
|
|
// i1 can be simplified in many cases.
|
2010-02-15 16:12:20 +00:00
|
|
|
if (C1->getType()->isIntegerTy(1)) {
|
2009-09-20 00:04:02 +00:00
|
|
|
switch (Opcode) {
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
2009-09-20 01:35:59 +00:00
|
|
|
return ConstantExpr::getXor(C1, C2);
|
2009-09-20 00:04:02 +00:00
|
|
|
case Instruction::Mul:
|
2009-09-20 01:35:59 +00:00
|
|
|
return ConstantExpr::getAnd(C1, C2);
|
2009-09-20 00:04:02 +00:00
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
|
|
|
// We can assume that C2 == 0. If it were one the result would be
|
|
|
|
// undefined because the shift value is as large as the bitwidth.
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1;
|
2009-09-20 00:04:02 +00:00
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
// We can assume that C2 == 1. If it were zero the result would be
|
|
|
|
// undefined through division by zero.
|
2009-09-20 01:35:59 +00:00
|
|
|
return C1;
|
2009-09-20 00:04:02 +00:00
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
// We can assume that C2 == 1. If it were zero the result would be
|
|
|
|
// undefined through division by zero.
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::getFalse(C1->getContext());
|
2009-09-20 00:04:02 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-20 18:24:14 +00:00
|
|
|
// We don't know how to fold this.
|
2006-12-23 06:05:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-04-08 01:18:18 +00:00
|
|
|
|
2005-01-28 19:09:51 +00:00
|
|
|
/// isZeroSizedType - This type is zero sized if its an array or structure of
|
|
|
|
/// zero sized types. The only leaf zero sized type is an empty structure.
|
|
|
|
static bool isMaybeZeroSizedType(const Type *Ty) {
|
2010-02-16 14:50:09 +00:00
|
|
|
if (Ty->isOpaqueTy()) return true; // Can't say.
|
2005-01-28 19:09:51 +00:00
|
|
|
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
|
|
|
|
|
|
|
|
// If all of elements have zero size, this does too.
|
|
|
|
for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
|
2005-01-28 23:17:27 +00:00
|
|
|
if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
|
2005-01-28 19:09:51 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
return isMaybeZeroSizedType(ATy->getElementType());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2004-03-12 05:53:32 +00:00
|
|
|
|
2004-01-13 05:51:55 +00:00
|
|
|
/// IdxCompare - Compare the two constants as though they were getelementptr
|
|
|
|
/// indices. This allows coersion of the types to be the same thing.
|
|
|
|
///
|
|
|
|
/// If the two constants are the "same" (after coersion), return 0. If the
|
|
|
|
/// first is less than the second, return -1, if the second is less than the
|
|
|
|
/// first, return 1. If the constants are not integral, return -2.
|
|
|
|
///
|
2011-01-30 01:48:50 +00:00
|
|
|
static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
|
2004-01-13 05:51:55 +00:00
|
|
|
if (C1 == C2) return 0;
|
|
|
|
|
2006-12-31 21:43:30 +00:00
|
|
|
// Ok, we found a different index. If they are not ConstantInt, we can't do
|
|
|
|
// anything with them.
|
2004-01-13 05:51:55 +00:00
|
|
|
if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
|
|
|
|
return -2; // don't know!
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-04-05 01:30:19 +00:00
|
|
|
// Ok, we have two differing integer indices. Sign extend them to be the same
|
|
|
|
// type. Long is always big enough, so we use it.
|
2010-02-15 16:12:20 +00:00
|
|
|
if (!C1->getType()->isIntegerTy(64))
|
2010-02-01 20:48:08 +00:00
|
|
|
C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(C1->getContext()));
|
2006-12-31 05:26:44 +00:00
|
|
|
|
2010-02-15 16:12:20 +00:00
|
|
|
if (!C2->getType()->isIntegerTy(64))
|
2010-02-01 20:48:08 +00:00
|
|
|
C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(C1->getContext()));
|
2006-11-27 01:05:10 +00:00
|
|
|
|
2006-12-31 05:26:44 +00:00
|
|
|
if (C1 == C2) return 0; // They are equal
|
2004-01-13 05:51:55 +00:00
|
|
|
|
2005-01-28 19:09:51 +00:00
|
|
|
// If the type being indexed over is really just a zero sized type, there is
|
|
|
|
// no pointer difference being made here.
|
|
|
|
if (isMaybeZeroSizedType(ElTy))
|
|
|
|
return -2; // dunno.
|
|
|
|
|
2004-01-13 05:51:55 +00:00
|
|
|
// If they are really different, now that they are the same type, then we
|
|
|
|
// found a difference!
|
2006-10-20 07:07:24 +00:00
|
|
|
if (cast<ConstantInt>(C1)->getSExtValue() <
|
|
|
|
cast<ConstantInt>(C2)->getSExtValue())
|
2004-01-13 05:51:55 +00:00
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-01-04 02:13:20 +00:00
|
|
|
/// evaluateFCmpRelation - This function determines if there is anything we can
|
2006-12-23 06:05:41 +00:00
|
|
|
/// decide about the two constants provided. This doesn't need to handle simple
|
|
|
|
/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
|
|
|
|
/// If we can determine that the two constants have a particular relation to
|
|
|
|
/// each other, we should return the corresponding FCmpInst predicate,
|
2006-12-24 18:52:08 +00:00
|
|
|
/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
|
|
|
|
/// ConstantFoldCompareInstruction.
|
2006-12-23 06:05:41 +00:00
|
|
|
///
|
|
|
|
/// To simplify this code we canonicalize the relation so that the first
|
2006-12-24 18:52:08 +00:00
|
|
|
/// operand is always the most "complex" of the two. We consider ConstantFP
|
|
|
|
/// to be the simplest, and ConstantExprs to be the most complex.
|
2010-02-01 20:48:08 +00:00
|
|
|
static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
|
2006-12-23 06:05:41 +00:00
|
|
|
assert(V1->getType() == V2->getType() &&
|
2006-12-24 18:52:08 +00:00
|
|
|
"Cannot compare values of different types!");
|
2007-10-14 01:56:47 +00:00
|
|
|
|
|
|
|
// No compile-time operations on this type yet.
|
2009-10-05 05:54:46 +00:00
|
|
|
if (V1->getType()->isPPC_FP128Ty())
|
2007-10-14 01:56:47 +00:00
|
|
|
return FCmpInst::BAD_FCMP_PREDICATE;
|
|
|
|
|
2006-12-24 18:52:08 +00:00
|
|
|
// Handle degenerate case quickly
|
2006-12-23 06:05:41 +00:00
|
|
|
if (V1 == V2) return FCmpInst::FCMP_OEQ;
|
|
|
|
|
2006-12-24 18:52:08 +00:00
|
|
|
if (!isa<ConstantExpr>(V1)) {
|
|
|
|
if (!isa<ConstantExpr>(V2)) {
|
|
|
|
// We distilled thisUse the standard constant folder for a few cases
|
2007-01-11 12:24:14 +00:00
|
|
|
ConstantInt *R = 0;
|
|
|
|
R = dyn_cast<ConstantInt>(
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
|
2007-03-02 00:28:52 +00:00
|
|
|
if (R && !R->isZero())
|
2006-12-23 06:05:41 +00:00
|
|
|
return FCmpInst::FCMP_OEQ;
|
2007-01-11 12:24:14 +00:00
|
|
|
R = dyn_cast<ConstantInt>(
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
|
2007-03-02 00:28:52 +00:00
|
|
|
if (R && !R->isZero())
|
2006-12-23 06:05:41 +00:00
|
|
|
return FCmpInst::FCMP_OLT;
|
2007-01-11 12:24:14 +00:00
|
|
|
R = dyn_cast<ConstantInt>(
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
|
2007-03-02 00:28:52 +00:00
|
|
|
if (R && !R->isZero())
|
2006-12-24 18:52:08 +00:00
|
|
|
return FCmpInst::FCMP_OGT;
|
|
|
|
|
|
|
|
// Nothing more we can do
|
2006-12-23 06:05:41 +00:00
|
|
|
return FCmpInst::BAD_FCMP_PREDICATE;
|
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2006-12-24 18:52:08 +00:00
|
|
|
// If the first operand is simple and second is ConstantExpr, swap operands.
|
2010-02-01 20:48:08 +00:00
|
|
|
FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
|
2006-12-24 18:52:08 +00:00
|
|
|
if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
|
|
|
|
return FCmpInst::getSwappedPredicate(SwappedRelation);
|
|
|
|
} else {
|
|
|
|
// Ok, the LHS is known to be a constantexpr. The RHS can be any of a
|
|
|
|
// constantexpr or a simple constant.
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantExpr *CE1 = cast<ConstantExpr>(V1);
|
2006-12-24 18:52:08 +00:00
|
|
|
switch (CE1->getOpcode()) {
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
// We might be able to do something with these but we don't right now.
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
|
|
|
// There are MANY other foldings that we could perform here. They will
|
|
|
|
// probably be added on demand, as they seem needed.
|
|
|
|
return FCmpInst::BAD_FCMP_PREDICATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// evaluateICmpRelation - This function determines if there is anything we can
|
2004-01-13 05:51:55 +00:00
|
|
|
/// decide about the two constants provided. This doesn't need to handle simple
|
2004-07-17 23:47:01 +00:00
|
|
|
/// things like integer comparisons, but should instead handle ConstantExprs
|
2006-12-11 02:16:58 +00:00
|
|
|
/// and GlobalValues. If we can determine that the two constants have a
|
2006-12-23 06:05:41 +00:00
|
|
|
/// particular relation to each other, we should return the corresponding ICmp
|
|
|
|
/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
|
2004-01-13 05:51:55 +00:00
|
|
|
///
|
|
|
|
/// 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
|
2004-07-17 23:47:01 +00:00
|
|
|
/// GlobalValues, followed by ConstantExpr's (the most complex).
|
2004-01-13 05:51:55 +00:00
|
|
|
///
|
2010-02-01 20:48:08 +00:00
|
|
|
static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
|
2006-12-23 06:05:41 +00:00
|
|
|
bool isSigned) {
|
2004-01-13 05:51:55 +00:00
|
|
|
assert(V1->getType() == V2->getType() &&
|
|
|
|
"Cannot compare different types of values!");
|
2006-12-23 06:05:41 +00:00
|
|
|
if (V1 == V2) return ICmpInst::ICMP_EQ;
|
2004-01-13 05:51:55 +00:00
|
|
|
|
2010-02-01 19:35:08 +00:00
|
|
|
if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
|
|
|
|
!isa<BlockAddress>(V1)) {
|
|
|
|
if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
|
|
|
|
!isa<BlockAddress>(V2)) {
|
2006-01-05 07:49:30 +00:00
|
|
|
// We distilled this down to a simple case, use the standard constant
|
|
|
|
// folder.
|
2007-01-11 12:24:14 +00:00
|
|
|
ConstantInt *R = 0;
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
|
2009-09-20 01:35:59 +00:00
|
|
|
R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
|
2007-03-02 00:28:52 +00:00
|
|
|
if (R && !R->isZero())
|
2006-12-23 06:05:41 +00:00
|
|
|
return pred;
|
|
|
|
pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
|
2009-09-20 01:35:59 +00:00
|
|
|
R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
|
2007-03-02 00:28:52 +00:00
|
|
|
if (R && !R->isZero())
|
2006-12-23 06:05:41 +00:00
|
|
|
return pred;
|
2009-07-08 03:04:38 +00:00
|
|
|
pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
|
2009-09-20 01:35:59 +00:00
|
|
|
R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
|
2007-03-02 00:28:52 +00:00
|
|
|
if (R && !R->isZero())
|
2006-12-23 06:05:41 +00:00
|
|
|
return pred;
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2006-01-05 07:49:30 +00:00
|
|
|
// If we couldn't figure it out, bail.
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::BAD_ICMP_PREDICATE;
|
2006-01-05 07:49:30 +00:00
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2004-01-13 05:51:55 +00:00
|
|
|
// If the first operand is simple, swap operands.
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::Predicate SwappedRelation =
|
2010-02-01 20:48:08 +00:00
|
|
|
evaluateICmpRelation(V2, V1, isSigned);
|
2006-12-23 06:05:41 +00:00
|
|
|
if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
|
|
|
|
return ICmpInst::getSwappedPredicate(SwappedRelation);
|
2004-01-13 05:51:55 +00:00
|
|
|
|
2010-02-01 19:35:08 +00:00
|
|
|
} else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
|
2004-02-01 01:23:19 +00:00
|
|
|
if (isa<ConstantExpr>(V2)) { // Swap as necessary.
|
2006-12-23 06:05:41 +00:00
|
|
|
ICmpInst::Predicate SwappedRelation =
|
2010-02-01 20:48:08 +00:00
|
|
|
evaluateICmpRelation(V2, V1, isSigned);
|
2006-12-23 06:05:41 +00:00
|
|
|
if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
|
|
|
|
return ICmpInst::getSwappedPredicate(SwappedRelation);
|
2010-02-01 19:35:08 +00:00
|
|
|
return ICmpInst::BAD_ICMP_PREDICATE;
|
2004-02-01 01:23:19 +00:00
|
|
|
}
|
2004-01-13 05:51:55 +00:00
|
|
|
|
2010-02-01 19:35:08 +00:00
|
|
|
// Now we know that the RHS is a GlobalValue, BlockAddress or simple
|
|
|
|
// constant (which, since the types must match, means that it's a
|
|
|
|
// ConstantPointerNull).
|
|
|
|
if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
|
2007-09-10 23:42:42 +00:00
|
|
|
// Don't try to decide equality of aliases.
|
2010-02-01 19:35:08 +00:00
|
|
|
if (!isa<GlobalAlias>(GV) && !isa<GlobalAlias>(GV2))
|
|
|
|
if (!GV->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage())
|
2007-09-10 23:42:42 +00:00
|
|
|
return ICmpInst::ICMP_NE;
|
2010-02-01 19:35:08 +00:00
|
|
|
} else if (isa<BlockAddress>(V2)) {
|
|
|
|
return ICmpInst::ICMP_NE; // Globals never equal labels.
|
2004-01-13 05:51:55 +00:00
|
|
|
} else {
|
|
|
|
assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
|
2010-02-01 19:35:08 +00:00
|
|
|
// GlobalVals can never be null unless they have external weak linkage.
|
|
|
|
// We don't try to evaluate aliases here.
|
|
|
|
if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV))
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::ICMP_NE;
|
2004-01-13 05:51:55 +00:00
|
|
|
}
|
2010-02-01 19:35:08 +00:00
|
|
|
} else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
|
|
|
|
if (isa<ConstantExpr>(V2)) { // Swap as necessary.
|
|
|
|
ICmpInst::Predicate SwappedRelation =
|
2010-02-01 20:48:08 +00:00
|
|
|
evaluateICmpRelation(V2, V1, isSigned);
|
2010-02-01 19:35:08 +00:00
|
|
|
if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
|
|
|
|
return ICmpInst::getSwappedPredicate(SwappedRelation);
|
|
|
|
return ICmpInst::BAD_ICMP_PREDICATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know that the RHS is a GlobalValue, BlockAddress or simple
|
|
|
|
// constant (which, since the types must match, means that it is a
|
|
|
|
// ConstantPointerNull).
|
|
|
|
if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
|
|
|
|
// Block address in another function can't equal this one, but block
|
|
|
|
// addresses in the current function might be the same if blocks are
|
|
|
|
// empty.
|
|
|
|
if (BA2->getFunction() != BA->getFunction())
|
|
|
|
return ICmpInst::ICMP_NE;
|
|
|
|
} else {
|
|
|
|
// Block addresses aren't null, don't equal the address of globals.
|
|
|
|
assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
|
|
|
|
"Canonicalization guarantee!");
|
|
|
|
return ICmpInst::ICMP_NE;
|
|
|
|
}
|
2004-01-13 05:51:55 +00:00
|
|
|
} else {
|
|
|
|
// Ok, the LHS is known to be a constantexpr. The RHS can be any of a
|
2010-02-01 19:35:08 +00:00
|
|
|
// constantexpr, a global, block address, or a simple constant.
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantExpr *CE1 = cast<ConstantExpr>(V1);
|
|
|
|
Constant *CE1Op0 = CE1->getOperand(0);
|
2004-01-13 05:51:55 +00:00
|
|
|
|
|
|
|
switch (CE1->getOpcode()) {
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
2006-12-23 06:05:41 +00:00
|
|
|
break; // We can't evaluate floating point casts or truncations.
|
|
|
|
|
2006-11-27 01:05:10 +00:00
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::BitCast:
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
2004-01-13 05:51:55 +00:00
|
|
|
// If the cast is not actually changing bits, and the second operand is a
|
|
|
|
// null pointer, do the comparison with the pre-casted value.
|
|
|
|
if (V2->isNullValue() &&
|
2010-02-16 11:11:14 +00:00
|
|
|
(CE1->getType()->isPointerTy() || CE1->getType()->isIntegerTy())) {
|
2007-12-10 22:53:04 +00:00
|
|
|
if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
|
|
|
|
if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
|
2010-02-01 20:48:08 +00:00
|
|
|
return evaluateICmpRelation(CE1Op0,
|
2009-07-31 20:28:14 +00:00
|
|
|
Constant::getNullValue(CE1Op0->getType()),
|
2009-09-20 03:48:46 +00:00
|
|
|
isSigned);
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
2004-04-11 01:29:30 +00:00
|
|
|
break;
|
2004-01-13 05:51:55 +00:00
|
|
|
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
// Ok, since this is a getelementptr, we know that the constant has a
|
|
|
|
// pointer type. Check the various cases.
|
|
|
|
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.
|
2006-12-24 18:52:08 +00:00
|
|
|
if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
|
2006-12-06 00:25:09 +00:00
|
|
|
if (GV->hasExternalWeakLinkage())
|
|
|
|
// Weak linkage GVals could be zero or not. We're comparing that
|
|
|
|
// to null pointer so its greater-or-equal
|
2006-12-23 06:05:41 +00:00
|
|
|
return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
|
2006-12-06 00:25:09 +00:00
|
|
|
else
|
|
|
|
// If its not weak linkage, the GVal must have a non-zero address
|
|
|
|
// so the result is greater-than
|
2009-09-20 04:27:06 +00:00
|
|
|
return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
|
2004-01-13 05:51:55 +00:00
|
|
|
} else if (isa<ConstantPointerNull>(CE1Op0)) {
|
|
|
|
// If we are indexing from a null pointer, check to see if we have any
|
|
|
|
// non-zero indices.
|
|
|
|
for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
|
|
|
|
if (!CE1->getOperand(i)->isNullValue())
|
|
|
|
// Offsetting from null, must not be equal.
|
2006-12-23 06:05:41 +00:00
|
|
|
return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
|
2004-01-13 05:51:55 +00:00
|
|
|
// Only zero indexes from null, must still be zero.
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::ICMP_EQ;
|
2004-01-13 05:51:55 +00:00
|
|
|
}
|
|
|
|
// Otherwise, we can't really say if the first operand is null or not.
|
2010-02-01 19:35:08 +00:00
|
|
|
} else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
|
2004-01-13 05:51:55 +00:00
|
|
|
if (isa<ConstantPointerNull>(CE1Op0)) {
|
2010-02-01 19:35:08 +00:00
|
|
|
if (GV2->hasExternalWeakLinkage())
|
2006-12-06 00:25:09 +00:00
|
|
|
// Weak linkage GVals could be zero or not. We're comparing it to
|
|
|
|
// a null pointer, so its less-or-equal
|
2006-12-23 06:05:41 +00:00
|
|
|
return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
|
2006-12-06 00:25:09 +00:00
|
|
|
else
|
|
|
|
// If its not weak linkage, the GVal must have a non-zero address
|
|
|
|
// so the result is less-than
|
2006-12-23 06:05:41 +00:00
|
|
|
return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
|
2010-02-01 19:35:08 +00:00
|
|
|
} else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
|
|
|
|
if (GV == GV2) {
|
2004-01-13 05:51:55 +00:00
|
|
|
// If this is a getelementptr of the same global, then it must be
|
|
|
|
// different. Because the types must match, the getelementptr could
|
|
|
|
// only have at most one index, and because we fold getelementptr's
|
|
|
|
// with a single zero index, it must be nonzero.
|
|
|
|
assert(CE1->getNumOperands() == 2 &&
|
|
|
|
!CE1->getOperand(1)->isNullValue() &&
|
|
|
|
"Suprising getelementptr!");
|
2006-12-23 06:05:41 +00:00
|
|
|
return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
|
2004-01-13 05:51:55 +00:00
|
|
|
} else {
|
|
|
|
// If they are different globals, we don't know what the value is,
|
|
|
|
// but they can't be equal.
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::ICMP_NE;
|
2004-01-13 05:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantExpr *CE2 = cast<ConstantExpr>(V2);
|
|
|
|
Constant *CE2Op0 = CE2->getOperand(0);
|
2004-01-13 05:51:55 +00:00
|
|
|
|
|
|
|
// There are MANY other foldings that we could perform here. They will
|
|
|
|
// probably be added on demand, as they seem needed.
|
|
|
|
switch (CE2->getOpcode()) {
|
|
|
|
default: break;
|
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
// By far the most common case to handle is when the base pointers are
|
|
|
|
// obviously to the same or different globals.
|
2004-07-17 23:47:01 +00:00
|
|
|
if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
|
2004-01-13 05:51:55 +00:00
|
|
|
if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::ICMP_NE;
|
2004-01-13 05:51:55 +00:00
|
|
|
// Ok, we know that both getelementptr instructions are based on the
|
|
|
|
// same global. From this, we can precisely determine the relative
|
|
|
|
// ordering of the resultant pointers.
|
|
|
|
unsigned i = 1;
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2009-09-10 23:37:55 +00:00
|
|
|
// The logic below assumes that the result of the comparison
|
|
|
|
// can be determined by finding the first index that differs.
|
|
|
|
// This doesn't work if there is over-indexing in any
|
|
|
|
// subsequent indices, so check for that case first.
|
|
|
|
if (!CE1->isGEPWithNoNotionalOverIndexing() ||
|
|
|
|
!CE2->isGEPWithNoNotionalOverIndexing())
|
|
|
|
return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
|
|
|
|
|
2004-01-13 05:51:55 +00:00
|
|
|
// Compare all of the operands the GEP's have in common.
|
2005-01-28 19:09:51 +00:00
|
|
|
gep_type_iterator GTI = gep_type_begin(CE1);
|
|
|
|
for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
|
|
|
|
++i, ++GTI)
|
2010-02-01 20:48:08 +00:00
|
|
|
switch (IdxCompare(CE1->getOperand(i),
|
2009-07-13 04:09:18 +00:00
|
|
|
CE2->getOperand(i), GTI.getIndexedType())) {
|
2006-12-23 06:05:41 +00:00
|
|
|
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;
|
2004-01-13 05:51:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, we ran out of things they have in common. If any leftovers
|
|
|
|
// are non-zero then we have a difference, otherwise we are equal.
|
|
|
|
for (; i < CE1->getNumOperands(); ++i)
|
2008-02-20 11:08:44 +00:00
|
|
|
if (!CE1->getOperand(i)->isNullValue()) {
|
2007-01-11 12:24:14 +00:00
|
|
|
if (isa<ConstantInt>(CE1->getOperand(i)))
|
2006-12-23 06:05:41 +00:00
|
|
|
return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
|
2005-01-28 19:09:51 +00:00
|
|
|
else
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
|
2008-02-20 11:08:44 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-01-13 05:51:55 +00:00
|
|
|
for (; i < CE2->getNumOperands(); ++i)
|
2008-02-20 11:08:44 +00:00
|
|
|
if (!CE2->getOperand(i)->isNullValue()) {
|
2007-01-11 12:24:14 +00:00
|
|
|
if (isa<ConstantInt>(CE2->getOperand(i)))
|
2006-12-23 06:05:41 +00:00
|
|
|
return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
|
2005-01-28 19:09:51 +00:00
|
|
|
else
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
|
2008-02-20 11:08:44 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::ICMP_EQ;
|
2004-01-13 05:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
return ICmpInst::BAD_ICMP_PREDICATE;
|
2004-01-13 05:51:55 +00:00
|
|
|
}
|
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
|
2009-09-20 01:35:59 +00:00
|
|
|
Constant *C1, Constant *C2) {
|
2009-07-08 03:04:38 +00:00
|
|
|
const Type *ResultTy;
|
|
|
|
if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
|
2010-02-01 20:48:08 +00:00
|
|
|
ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
|
|
|
|
VT->getNumElements());
|
2009-07-08 03:04:38 +00:00
|
|
|
else
|
2010-02-01 20:48:08 +00:00
|
|
|
ResultTy = Type::getInt1Ty(C1->getContext());
|
2009-07-08 03:04:38 +00:00
|
|
|
|
2008-07-08 05:46:34 +00:00
|
|
|
// Fold FCMP_FALSE/FCMP_TRUE unconditionally.
|
2009-07-08 03:04:38 +00:00
|
|
|
if (pred == FCmpInst::FCMP_FALSE)
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(ResultTy);
|
2009-07-08 03:04:38 +00:00
|
|
|
|
|
|
|
if (pred == FCmpInst::FCMP_TRUE)
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getAllOnesValue(ResultTy);
|
2009-07-08 03:04:38 +00:00
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// Handle some degenerate cases first
|
2010-06-28 21:30:07 +00:00
|
|
|
if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
|
|
|
|
// For EQ and NE, we can always pick a value for the undef to make the
|
|
|
|
// predicate pass or fail, so we can return undef.
|
|
|
|
if (ICmpInst::isEquality(ICmpInst::Predicate(pred)))
|
|
|
|
return UndefValue::get(ResultTy);
|
|
|
|
// Otherwise, pick the same value as the non-undef operand, and fold
|
|
|
|
// it to true or false.
|
2010-03-03 19:46:03 +00:00
|
|
|
return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred));
|
2010-06-28 21:30:07 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
|
2007-10-14 01:56:47 +00:00
|
|
|
// No compile-time operations on this type yet.
|
2009-10-05 05:54:46 +00:00
|
|
|
if (C1->getType()->isPPC_FP128Ty())
|
2007-10-14 01:56:47 +00:00
|
|
|
return 0;
|
|
|
|
|
2006-12-23 06:05:41 +00:00
|
|
|
// icmp eq/ne(null,GV) -> false/true
|
|
|
|
if (C1->isNullValue()) {
|
|
|
|
if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
|
2007-09-19 10:16:17 +00:00
|
|
|
// Don't try to evaluate aliases. External weak GV can be null.
|
2008-02-20 11:08:44 +00:00
|
|
|
if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
|
2006-12-24 18:52:08 +00:00
|
|
|
if (pred == ICmpInst::ICMP_EQ)
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::getFalse(C1->getContext());
|
2006-12-24 18:52:08 +00:00
|
|
|
else if (pred == ICmpInst::ICMP_NE)
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::getTrue(C1->getContext());
|
2008-02-20 11:08:44 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
// icmp eq/ne(GV,null) -> false/true
|
|
|
|
} else if (C2->isNullValue()) {
|
|
|
|
if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
|
2007-09-19 10:16:17 +00:00
|
|
|
// Don't try to evaluate aliases. External weak GV can be null.
|
2008-02-20 11:08:44 +00:00
|
|
|
if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
|
2006-12-24 18:52:08 +00:00
|
|
|
if (pred == ICmpInst::ICMP_EQ)
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::getFalse(C1->getContext());
|
2006-12-24 18:52:08 +00:00
|
|
|
else if (pred == ICmpInst::ICMP_NE)
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::getTrue(C1->getContext());
|
2008-02-20 11:08:44 +00:00
|
|
|
}
|
2004-01-12 22:07:24 +00:00
|
|
|
}
|
|
|
|
|
2009-09-20 07:21:39 +00:00
|
|
|
// If the comparison is a comparison between two i1's, simplify it.
|
2010-02-15 16:12:20 +00:00
|
|
|
if (C1->getType()->isIntegerTy(1)) {
|
2009-09-20 07:21:39 +00:00
|
|
|
switch(pred) {
|
|
|
|
case ICmpInst::ICMP_EQ:
|
|
|
|
if (isa<ConstantInt>(C2))
|
|
|
|
return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
|
|
|
|
return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
|
|
|
|
case ICmpInst::ICMP_NE:
|
|
|
|
return ConstantExpr::getXor(C1, C2);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-12 18:42:52 +00:00
|
|
|
if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
|
2007-02-27 06:23:51 +00:00
|
|
|
APInt V1 = cast<ConstantInt>(C1)->getValue();
|
|
|
|
APInt V2 = cast<ConstantInt>(C2)->getValue();
|
|
|
|
switch (pred) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Invalid ICmp Predicate"); return 0;
|
2010-02-01 20:48:08 +00:00
|
|
|
case ICmpInst::ICMP_EQ: return ConstantInt::get(ResultTy, V1 == V2);
|
|
|
|
case ICmpInst::ICMP_NE: return ConstantInt::get(ResultTy, V1 != V2);
|
|
|
|
case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2));
|
|
|
|
case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2));
|
|
|
|
case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2));
|
|
|
|
case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2));
|
|
|
|
case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2));
|
|
|
|
case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2));
|
|
|
|
case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2));
|
|
|
|
case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2));
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
|
|
|
} else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
|
2007-09-06 18:13:44 +00:00
|
|
|
APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
|
|
|
|
APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
|
|
|
|
APFloat::cmpResult R = C1V.compare(C2V);
|
2006-12-24 18:52:08 +00:00
|
|
|
switch (pred) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Invalid FCmp Predicate"); return 0;
|
2010-02-01 20:48:08 +00:00
|
|
|
case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
|
|
|
|
case FCmpInst::FCMP_TRUE: return Constant::getAllOnesValue(ResultTy);
|
2006-12-23 06:05:41 +00:00
|
|
|
case FCmpInst::FCMP_UNO:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
|
2007-01-11 00:25:45 +00:00
|
|
|
case FCmpInst::FCMP_ORD:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
|
2006-12-23 06:05:41 +00:00
|
|
|
case FCmpInst::FCMP_UEQ:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
|
|
|
|
R==APFloat::cmpEqual);
|
2007-01-12 04:24:46 +00:00
|
|
|
case FCmpInst::FCMP_OEQ:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
|
2007-01-11 00:25:45 +00:00
|
|
|
case FCmpInst::FCMP_UNE:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
|
2007-01-12 04:24:46 +00:00
|
|
|
case FCmpInst::FCMP_ONE:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
|
|
|
|
R==APFloat::cmpGreaterThan);
|
2007-01-11 00:25:45 +00:00
|
|
|
case FCmpInst::FCMP_ULT:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
|
|
|
|
R==APFloat::cmpLessThan);
|
2007-01-12 04:24:46 +00:00
|
|
|
case FCmpInst::FCMP_OLT:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
|
2006-12-23 06:05:41 +00:00
|
|
|
case FCmpInst::FCMP_UGT:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
|
|
|
|
R==APFloat::cmpGreaterThan);
|
2007-01-12 04:24:46 +00:00
|
|
|
case FCmpInst::FCMP_OGT:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
|
2007-01-11 00:25:45 +00:00
|
|
|
case FCmpInst::FCMP_ULE:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
|
2007-01-12 04:24:46 +00:00
|
|
|
case FCmpInst::FCMP_OLE:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
|
|
|
|
R==APFloat::cmpEqual);
|
2006-12-23 06:05:41 +00:00
|
|
|
case FCmpInst::FCMP_UGE:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
|
2007-01-12 04:24:46 +00:00
|
|
|
case FCmpInst::FCMP_OGE:
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
|
|
|
|
R==APFloat::cmpEqual);
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
2010-02-16 11:11:14 +00:00
|
|
|
} else if (C1->getType()->isVectorTy()) {
|
2008-07-10 00:29:28 +00:00
|
|
|
SmallVector<Constant*, 16> C1Elts, C2Elts;
|
2010-02-01 20:48:08 +00:00
|
|
|
C1->getVectorElements(C1Elts);
|
|
|
|
C2->getVectorElements(C2Elts);
|
2010-01-21 07:03:21 +00:00
|
|
|
if (C1Elts.empty() || C2Elts.empty())
|
|
|
|
return 0;
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2008-07-10 00:29:28 +00:00
|
|
|
// If we can constant fold the comparison of each element, constant fold
|
|
|
|
// the whole vector comparison.
|
|
|
|
SmallVector<Constant*, 4> ResElts;
|
|
|
|
for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) {
|
|
|
|
// Compare the elements, producing an i1 result or constant expr.
|
2010-01-21 07:03:21 +00:00
|
|
|
ResElts.push_back(ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i]));
|
2004-01-13 05:51:55 +00:00
|
|
|
}
|
2009-07-28 21:19:26 +00:00
|
|
|
return ConstantVector::get(&ResElts[0], ResElts.size());
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
2004-01-13 05:51:55 +00:00
|
|
|
|
2010-02-15 16:12:20 +00:00
|
|
|
if (C1->getType()->isFloatingPointTy()) {
|
2008-07-08 18:47:38 +00:00
|
|
|
int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
|
2010-02-01 20:48:08 +00:00
|
|
|
switch (evaluateFCmpRelation(C1, C2)) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Unknown relation!");
|
2006-12-24 18:52:08 +00:00
|
|
|
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; // Couldn't determine anything about these constants.
|
|
|
|
case FCmpInst::FCMP_OEQ: // We know that C1 == C2
|
2008-07-08 05:46:34 +00:00
|
|
|
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;
|
2006-12-24 18:52:08 +00:00
|
|
|
case FCmpInst::FCMP_OLT: // We know that C1 < C2
|
2008-07-08 05:46:34 +00:00
|
|
|
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;
|
2006-12-24 18:52:08 +00:00
|
|
|
case FCmpInst::FCMP_OGT: // We know that C1 > C2
|
2008-07-08 05:46:34 +00:00
|
|
|
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;
|
2006-12-24 18:52:08 +00:00
|
|
|
case FCmpInst::FCMP_OLE: // We know that C1 <= C2
|
|
|
|
// We can only partially decide this relation.
|
|
|
|
if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
|
2008-07-08 05:46:34 +00:00
|
|
|
Result = 0;
|
|
|
|
else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
|
|
|
|
Result = 1;
|
2006-12-24 18:52:08 +00:00
|
|
|
break;
|
|
|
|
case FCmpInst::FCMP_OGE: // We known that C1 >= C2
|
|
|
|
// We can only partially decide this relation.
|
|
|
|
if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
|
2008-07-08 05:46:34 +00:00
|
|
|
Result = 0;
|
|
|
|
else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
|
|
|
|
Result = 1;
|
2006-12-24 18:52:08 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE: // We know that C1 != C2
|
|
|
|
// We can only partially decide this relation.
|
|
|
|
if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
|
2008-07-08 05:46:34 +00:00
|
|
|
Result = 0;
|
|
|
|
else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
|
|
|
|
Result = 1;
|
2006-12-24 18:52:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2008-07-08 05:46:34 +00:00
|
|
|
// If we evaluated the result, return it now.
|
2009-07-08 03:04:38 +00:00
|
|
|
if (Result != -1)
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, Result);
|
2009-07-08 03:04:38 +00:00
|
|
|
|
2006-12-24 18:52:08 +00:00
|
|
|
} else {
|
|
|
|
// Evaluate the relation between the two constants, per the predicate.
|
2008-07-08 05:46:34 +00:00
|
|
|
int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
|
2010-02-01 20:48:08 +00:00
|
|
|
switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Unknown relational!");
|
2006-12-24 18:52:08 +00:00
|
|
|
case ICmpInst::BAD_ICMP_PREDICATE:
|
|
|
|
break; // Couldn't determine anything about these constants.
|
|
|
|
case ICmpInst::ICMP_EQ: // We know the constants are equal!
|
|
|
|
// If we know the constants are equal, we can decide the result of this
|
|
|
|
// computation precisely.
|
2009-09-20 04:27:06 +00:00
|
|
|
Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
|
2008-07-08 05:46:34 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULT:
|
2009-09-20 04:27:06 +00:00
|
|
|
switch (pred) {
|
|
|
|
case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
|
2009-09-20 05:47:45 +00:00
|
|
|
Result = 1; break;
|
2009-09-20 04:27:06 +00:00
|
|
|
case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
|
2009-09-20 05:47:45 +00:00
|
|
|
Result = 0; break;
|
2009-09-20 04:27:06 +00:00
|
|
|
}
|
2008-07-08 05:46:34 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SLT:
|
2009-09-20 04:27:06 +00:00
|
|
|
switch (pred) {
|
|
|
|
case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
|
2009-09-20 05:47:45 +00:00
|
|
|
Result = 1; break;
|
2009-09-20 04:27:06 +00:00
|
|
|
case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
|
2009-09-20 05:47:45 +00:00
|
|
|
Result = 0; break;
|
2009-09-20 04:27:06 +00:00
|
|
|
}
|
2008-07-08 05:46:34 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_UGT:
|
2009-09-20 04:27:06 +00:00
|
|
|
switch (pred) {
|
|
|
|
case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
|
2009-09-20 05:47:45 +00:00
|
|
|
Result = 1; break;
|
2009-09-20 04:27:06 +00:00
|
|
|
case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
|
2009-09-20 05:47:45 +00:00
|
|
|
Result = 0; break;
|
2009-09-20 04:27:06 +00:00
|
|
|
}
|
2008-07-08 05:46:34 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SGT:
|
2009-09-20 04:27:06 +00:00
|
|
|
switch (pred) {
|
|
|
|
case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
|
2009-09-20 05:47:45 +00:00
|
|
|
Result = 1; break;
|
2009-09-20 04:27:06 +00:00
|
|
|
case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
|
2009-09-20 05:47:45 +00:00
|
|
|
Result = 0; break;
|
2009-09-20 04:27:06 +00:00
|
|
|
}
|
2008-07-08 05:46:34 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_ULE:
|
2008-07-08 05:46:34 +00:00
|
|
|
if (pred == ICmpInst::ICMP_UGT) Result = 0;
|
2009-09-20 04:27:06 +00:00
|
|
|
if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
|
2006-12-24 18:52:08 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SLE:
|
2008-07-08 05:46:34 +00:00
|
|
|
if (pred == ICmpInst::ICMP_SGT) Result = 0;
|
2009-09-20 04:27:06 +00:00
|
|
|
if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
|
2006-12-24 18:52:08 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_UGE:
|
2008-07-08 05:46:34 +00:00
|
|
|
if (pred == ICmpInst::ICMP_ULT) Result = 0;
|
2009-09-20 04:27:06 +00:00
|
|
|
if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
|
2006-12-24 18:52:08 +00:00
|
|
|
break;
|
2006-12-23 06:05:41 +00:00
|
|
|
case ICmpInst::ICMP_SGE:
|
2008-07-08 05:46:34 +00:00
|
|
|
if (pred == ICmpInst::ICMP_SLT) Result = 0;
|
2009-09-20 04:27:06 +00:00
|
|
|
if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
|
2006-12-24 18:52:08 +00:00
|
|
|
break;
|
|
|
|
case ICmpInst::ICMP_NE:
|
2008-07-08 05:46:34 +00:00
|
|
|
if (pred == ICmpInst::ICMP_EQ) Result = 0;
|
|
|
|
if (pred == ICmpInst::ICMP_NE) Result = 1;
|
2004-01-13 05:51:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2008-07-08 05:46:34 +00:00
|
|
|
// If we evaluated the result, return it now.
|
2009-07-08 03:04:38 +00:00
|
|
|
if (Result != -1)
|
2010-02-01 20:48:08 +00:00
|
|
|
return ConstantInt::get(ResultTy, Result);
|
2009-08-29 23:35:16 +00:00
|
|
|
|
2009-09-20 05:48:50 +00:00
|
|
|
// If the right hand side is a bitcast, try using its inverse to simplify
|
2010-02-01 20:04:40 +00:00
|
|
|
// it by moving it to the left hand side. We can't do this if it would turn
|
2010-02-01 20:42:02 +00:00
|
|
|
// a vector compare into a scalar compare or visa versa.
|
2009-09-20 05:48:50 +00:00
|
|
|
if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
|
2010-02-01 20:04:40 +00:00
|
|
|
Constant *CE2Op0 = CE2->getOperand(0);
|
|
|
|
if (CE2->getOpcode() == Instruction::BitCast &&
|
2010-03-04 06:54:10 +00:00
|
|
|
CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) {
|
2009-09-20 05:48:50 +00:00
|
|
|
Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
|
|
|
|
return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-20 07:31:25 +00:00
|
|
|
// If the left hand side is an extension, try eliminating it.
|
|
|
|
if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
|
2010-03-04 06:54:10 +00:00
|
|
|
if ((CE1->getOpcode() == Instruction::SExt && ICmpInst::isSigned(pred)) ||
|
|
|
|
(CE1->getOpcode() == Instruction::ZExt && !ICmpInst::isSigned(pred))){
|
2009-09-20 07:31:25 +00:00
|
|
|
Constant *CE1Op0 = CE1->getOperand(0);
|
|
|
|
Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
|
|
|
|
if (CE1Inverse == CE1Op0) {
|
|
|
|
// Check whether we can safely truncate the right hand side.
|
|
|
|
Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
|
|
|
|
if (ConstantExpr::getZExt(C2Inverse, C2->getType()) == C2) {
|
|
|
|
return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 06:07:04 +00:00
|
|
|
if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
|
|
|
|
(C1->isNullValue() && !C2->isNullValue())) {
|
2009-07-08 03:04:38 +00:00
|
|
|
// If C2 is a constant expr and C1 isn't, flip them around and fold the
|
2006-12-24 18:52:08 +00:00
|
|
|
// other way if possible.
|
2009-12-17 06:07:04 +00:00
|
|
|
// Also, if C1 is null and C2 isn't, flip them around.
|
2010-03-04 06:54:10 +00:00
|
|
|
pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
|
|
|
|
return ConstantExpr::getICmp(pred, C2, C1);
|
2006-12-24 18:52:08 +00:00
|
|
|
}
|
2004-01-13 05:51:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2009-08-29 23:35:16 +00:00
|
|
|
}
|
2004-01-12 22:07:24 +00:00
|
|
|
|
2009-09-11 00:04:14 +00:00
|
|
|
/// isInBoundsIndices - Test whether the given sequence of *normalized* indices
|
|
|
|
/// is "inbounds".
|
2011-01-14 08:07:43 +00:00
|
|
|
template<typename IndexTy>
|
|
|
|
static bool isInBoundsIndices(IndexTy const *Idxs, size_t NumIdx) {
|
2009-09-11 00:04:14 +00:00
|
|
|
// No indices means nothing that could be out of bounds.
|
|
|
|
if (NumIdx == 0) return true;
|
|
|
|
|
|
|
|
// If the first index is zero, it's in bounds.
|
2011-01-14 08:07:43 +00:00
|
|
|
if (cast<Constant>(Idxs[0])->isNullValue()) return true;
|
2009-09-11 00:04:14 +00:00
|
|
|
|
|
|
|
// If the first index is one and all the rest are zero, it's in bounds,
|
|
|
|
// by the one-past-the-end rule.
|
|
|
|
if (!cast<ConstantInt>(Idxs[0])->isOne())
|
|
|
|
return false;
|
|
|
|
for (unsigned i = 1, e = NumIdx; i != e; ++i)
|
2011-01-14 08:07:43 +00:00
|
|
|
if (!cast<Constant>(Idxs[i])->isNullValue())
|
2009-09-11 00:04:14 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-14 08:07:43 +00:00
|
|
|
template<typename IndexTy>
|
|
|
|
static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
|
|
|
|
bool inBounds,
|
|
|
|
IndexTy const *Idxs,
|
|
|
|
unsigned NumIdx) {
|
|
|
|
Constant *Idx0 = cast<Constant>(Idxs[0]);
|
2007-01-31 04:40:28 +00:00
|
|
|
if (NumIdx == 0 ||
|
2011-01-14 08:07:43 +00:00
|
|
|
(NumIdx == 1 && Idx0->isNullValue()))
|
2009-09-20 01:35:59 +00:00
|
|
|
return C;
|
2004-01-12 22:07:24 +00:00
|
|
|
|
2004-10-17 21:54:55 +00:00
|
|
|
if (isa<UndefValue>(C)) {
|
2007-12-17 01:12:55 +00:00
|
|
|
const PointerType *Ptr = cast<PointerType>(C->getType());
|
2011-01-14 08:07:43 +00:00
|
|
|
const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs, Idxs+NumIdx);
|
2004-10-17 21:54:55 +00:00
|
|
|
assert(Ty != 0 && "Invalid indices for GEP!");
|
2009-07-30 23:03:37 +00:00
|
|
|
return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
|
2004-10-17 21:54:55 +00:00
|
|
|
}
|
|
|
|
|
2004-02-16 20:46:13 +00:00
|
|
|
if (C->isNullValue()) {
|
|
|
|
bool isNull = true;
|
2007-01-31 04:40:28 +00:00
|
|
|
for (unsigned i = 0, e = NumIdx; i != e; ++i)
|
2011-01-14 08:07:43 +00:00
|
|
|
if (!cast<Constant>(Idxs[i])->isNullValue()) {
|
2004-02-16 20:46:13 +00:00
|
|
|
isNull = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isNull) {
|
2007-12-17 01:12:55 +00:00
|
|
|
const PointerType *Ptr = cast<PointerType>(C->getType());
|
2011-01-14 08:07:43 +00:00
|
|
|
const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs,
|
|
|
|
Idxs+NumIdx);
|
2004-02-16 20:46:13 +00:00
|
|
|
assert(Ty != 0 && "Invalid indices for GEP!");
|
2011-01-30 01:48:50 +00:00
|
|
|
return ConstantPointerNull::get(PointerType::get(Ty,
|
|
|
|
Ptr->getAddressSpace()));
|
2004-02-16 20:46:13 +00:00
|
|
|
}
|
|
|
|
}
|
2004-01-12 22:07:24 +00:00
|
|
|
|
2009-09-20 01:35:59 +00:00
|
|
|
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
|
2004-01-12 22:07:24 +00:00
|
|
|
// Combine Indices - If the source pointer to this getelementptr instruction
|
|
|
|
// is a getelementptr instruction, combine the indices of the two
|
|
|
|
// getelementptr instructions into a single instruction.
|
|
|
|
//
|
|
|
|
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;
|
|
|
|
|
2010-02-16 11:11:14 +00:00
|
|
|
if ((LastTy && LastTy->isArrayTy()) || Idx0->isNullValue()) {
|
2007-01-31 04:40:28 +00:00
|
|
|
SmallVector<Value*, 16> NewIndices;
|
|
|
|
NewIndices.reserve(NumIdx + CE->getNumOperands());
|
2004-01-12 22:07:24 +00:00
|
|
|
for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
|
2004-10-11 22:52:25 +00:00
|
|
|
NewIndices.push_back(CE->getOperand(i));
|
2004-01-12 22:07:24 +00:00
|
|
|
|
|
|
|
// 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);
|
2004-10-11 22:52:25 +00:00
|
|
|
// Otherwise it must be an array.
|
|
|
|
if (!Idx0->isNullValue()) {
|
2004-07-07 04:45:13 +00:00
|
|
|
const Type *IdxTy = Combined->getType();
|
2006-12-04 02:46:44 +00:00
|
|
|
if (IdxTy != Idx0->getType()) {
|
2010-02-01 20:48:08 +00:00
|
|
|
const Type *Int64Ty = Type::getInt64Ty(IdxTy->getContext());
|
|
|
|
Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Int64Ty);
|
|
|
|
Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, Int64Ty);
|
2009-07-29 18:55:55 +00:00
|
|
|
Combined = ConstantExpr::get(Instruction::Add, C1, C2);
|
2006-12-04 02:46:44 +00:00
|
|
|
} else {
|
|
|
|
Combined =
|
2009-07-29 18:55:55 +00:00
|
|
|
ConstantExpr::get(Instruction::Add, Idx0, Combined);
|
2006-12-04 02:46:44 +00:00
|
|
|
}
|
2004-07-07 04:45:13 +00:00
|
|
|
}
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-01-12 22:07:24 +00:00
|
|
|
NewIndices.push_back(Combined);
|
2010-06-21 19:47:52 +00:00
|
|
|
NewIndices.append(Idxs+1, Idxs+NumIdx);
|
2009-09-11 00:04:14 +00:00
|
|
|
return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ?
|
|
|
|
ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0),
|
|
|
|
&NewIndices[0],
|
|
|
|
NewIndices.size()) :
|
|
|
|
ConstantExpr::getGetElementPtr(CE->getOperand(0),
|
|
|
|
&NewIndices[0],
|
|
|
|
NewIndices.size());
|
2004-01-12 22:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement folding of:
|
2011-01-30 01:48:50 +00:00
|
|
|
// i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
|
|
|
|
// i64 0, i64 0)
|
|
|
|
// To: i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
|
2004-01-12 22:07:24 +00:00
|
|
|
//
|
2007-08-13 17:09:08 +00:00
|
|
|
if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
|
2005-04-21 23:48:37 +00:00
|
|
|
if (const PointerType *SPT =
|
2004-01-12 22:07:24 +00:00
|
|
|
dyn_cast<PointerType>(CE->getOperand(0)->getType()))
|
|
|
|
if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
|
|
|
|
if (const ArrayType *CAT =
|
2006-06-28 21:38:54 +00:00
|
|
|
dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
|
2004-01-12 22:07:24 +00:00
|
|
|
if (CAT->getElementType() == SAT->getElementType())
|
2009-09-11 00:04:14 +00:00
|
|
|
return inBounds ?
|
|
|
|
ConstantExpr::getInBoundsGetElementPtr(
|
|
|
|
(Constant*)CE->getOperand(0), Idxs, NumIdx) :
|
|
|
|
ConstantExpr::getGetElementPtr(
|
2009-06-20 00:24:58 +00:00
|
|
|
(Constant*)CE->getOperand(0), Idxs, NumIdx);
|
2007-08-13 17:09:08 +00:00
|
|
|
}
|
2004-01-12 22:07:24 +00:00
|
|
|
}
|
2009-09-11 00:04:14 +00:00
|
|
|
|
|
|
|
// Check to see if any array indices are not within the corresponding
|
|
|
|
// notional array bounds. If so, try to determine if they can be factored
|
|
|
|
// out into preceding dimensions.
|
|
|
|
bool Unknown = false;
|
|
|
|
SmallVector<Constant *, 8> NewIdxs;
|
|
|
|
const Type *Ty = C->getType();
|
|
|
|
const Type *Prev = 0;
|
|
|
|
for (unsigned i = 0; i != NumIdx;
|
|
|
|
Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
|
|
|
|
if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
|
|
|
|
if (ATy->getNumElements() <= INT64_MAX &&
|
|
|
|
ATy->getNumElements() != 0 &&
|
|
|
|
CI->getSExtValue() >= (int64_t)ATy->getNumElements()) {
|
|
|
|
if (isa<SequentialType>(Prev)) {
|
|
|
|
// It's out of range, but we can factor it into the prior
|
|
|
|
// dimension.
|
|
|
|
NewIdxs.resize(NumIdx);
|
|
|
|
ConstantInt *Factor = ConstantInt::get(CI->getType(),
|
|
|
|
ATy->getNumElements());
|
|
|
|
NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
|
|
|
|
|
2011-01-14 08:07:43 +00:00
|
|
|
Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
|
2009-09-11 00:04:14 +00:00
|
|
|
Constant *Div = ConstantExpr::getSDiv(CI, Factor);
|
|
|
|
|
|
|
|
// Before adding, extend both operands to i64 to avoid
|
|
|
|
// overflow trouble.
|
2010-02-15 16:12:20 +00:00
|
|
|
if (!PrevIdx->getType()->isIntegerTy(64))
|
2009-09-11 00:04:14 +00:00
|
|
|
PrevIdx = ConstantExpr::getSExt(PrevIdx,
|
2010-02-01 20:48:08 +00:00
|
|
|
Type::getInt64Ty(Div->getContext()));
|
2010-02-15 16:12:20 +00:00
|
|
|
if (!Div->getType()->isIntegerTy(64))
|
2009-09-11 00:04:14 +00:00
|
|
|
Div = ConstantExpr::getSExt(Div,
|
2010-02-01 20:48:08 +00:00
|
|
|
Type::getInt64Ty(Div->getContext()));
|
2009-09-11 00:04:14 +00:00
|
|
|
|
|
|
|
NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
|
|
|
|
} else {
|
|
|
|
// It's out of range, but the prior dimension is a struct
|
|
|
|
// so we can't do anything about it.
|
|
|
|
Unknown = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't know if it's in range or not.
|
|
|
|
Unknown = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we did any factoring, start over with the adjusted indices.
|
|
|
|
if (!NewIdxs.empty()) {
|
|
|
|
for (unsigned i = 0; i != NumIdx; ++i)
|
2011-01-14 08:07:43 +00:00
|
|
|
if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
|
2009-09-11 00:04:14 +00:00
|
|
|
return inBounds ?
|
2009-09-20 01:35:59 +00:00
|
|
|
ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(),
|
|
|
|
NewIdxs.size()) :
|
|
|
|
ConstantExpr::getGetElementPtr(C, NewIdxs.data(), NewIdxs.size());
|
2009-09-11 00:04:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If all indices are known integers and normalized, we can do a simple
|
|
|
|
// check for the "inbounds" property.
|
|
|
|
if (!Unknown && !inBounds &&
|
|
|
|
isa<GlobalVariable>(C) && isInBoundsIndices(Idxs, NumIdx))
|
2009-09-20 01:35:59 +00:00
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(C, Idxs, NumIdx);
|
2009-09-11 00:04:14 +00:00
|
|
|
|
2004-01-12 22:07:24 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2011-01-14 08:07:43 +00:00
|
|
|
|
|
|
|
Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
|
|
|
|
bool inBounds,
|
|
|
|
Constant* const *Idxs,
|
|
|
|
unsigned NumIdx) {
|
|
|
|
return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs, NumIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
|
|
|
|
bool inBounds,
|
|
|
|
Value* const *Idxs,
|
|
|
|
unsigned NumIdx) {
|
|
|
|
return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs, NumIdx);
|
|
|
|
}
|