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"
|
2011-04-11 09:35:34 +00:00
|
|
|
#include "llvm/Operator.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
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-01-26 02:32:04 +00:00
|
|
|
/// BitCastConstantVector - Convert the specified vector Constant 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.
|
2012-01-26 02:32:04 +00:00
|
|
|
static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
|
2011-02-11 19:37:55 +00:00
|
|
|
|
2011-02-17 21:22:27 +00:00
|
|
|
if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
|
2011-02-11 19:37:55 +00:00
|
|
|
if (CV->isNullValue()) return Constant::getNullValue(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();
|
2012-01-26 02:32:04 +00:00
|
|
|
if (NumElts != CV->getType()->getVectorNumElements())
|
2007-12-11 06:07:39 +00:00
|
|
|
return 0;
|
2012-01-26 02:32:04 +00:00
|
|
|
|
|
|
|
Type *DstEltTy = DstTy->getElementType();
|
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.
|
2012-01-26 02:32:04 +00:00
|
|
|
SmallVector<Constant*, 16> Result;
|
2007-12-11 06:07:39 +00:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
2012-01-26 02:32:04 +00:00
|
|
|
Constant *C = CV->getAggregateElement(i);
|
|
|
|
if (C == 0) return 0;
|
|
|
|
C = ConstantExpr::getBitCast(C, DstEltTy);
|
|
|
|
if (isa<ConstantExpr>(C)) return 0;
|
|
|
|
Result.push_back(C);
|
2006-04-02 01:38:28 +00:00
|
|
|
}
|
2007-12-11 06:07:39 +00:00
|
|
|
|
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
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *DstTy ///< desintation type of the first cast
|
2006-11-27 01:05:10 +00:00
|
|
|
) {
|
|
|
|
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
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *SrcTy = Op->getOperand(0)->getType();
|
|
|
|
Type *MidTy = Op->getType();
|
2006-11-27 01:05:10 +00:00
|
|
|
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
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
static Constant *FoldBitCast(Constant *V, Type *DestTy) {
|
|
|
|
Type *SrcTy = V->getType();
|
2007-12-11 05:55:02 +00:00
|
|
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
|
|
|
|
if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
|
2012-01-11 12:20:08 +00:00
|
|
|
if (PTy->getAddressSpace() == DPTy->getAddressSpace()
|
|
|
|
&& DPTy->getElementType()->isSized()) {
|
2008-03-31 00:22:16 +00:00
|
|
|
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);
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *ElTy = PTy->getElementType();
|
2008-03-31 00:22:16 +00:00
|
|
|
while (ElTy != DPTy->getElementType()) {
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(ElTy)) {
|
2008-03-31 00:22:16 +00:00
|
|
|
if (STy->getNumElements() == 0) break;
|
|
|
|
ElTy = STy->getElementType(0);
|
2009-08-12 00:32:55 +00:00
|
|
|
IdxList.push_back(Zero);
|
2011-07-18 04:54:35 +00:00
|
|
|
} else if (SequentialType *STy =
|
2008-03-31 00:22:16 +00:00
|
|
|
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.
|
2011-07-21 14:31:17 +00:00
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(V, IdxList);
|
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).
|
2011-07-18 04:54:35 +00:00
|
|
|
if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
|
|
|
|
if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
|
2007-12-11 05:55:02 +00:00
|
|
|
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
|
|
|
|
2012-01-26 02:32:04 +00:00
|
|
|
// Handle ConstantVector and ConstantAggregateVector.
|
|
|
|
return BitCastConstantVector(V, 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))
|
2011-02-15 00:14:00 +00:00
|
|
|
return ConstantExpr::getBitCast(ConstantVector::get(V), 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.
|
|
|
|
///
|
2011-07-18 04:54:35 +00:00
|
|
|
static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy,
|
2010-02-01 18:27:38 +00:00
|
|
|
bool Folded) {
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
2010-02-01 18:27:38 +00:00
|
|
|
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
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty))
|
2010-02-01 18:27:38 +00:00
|
|
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
if (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.
|
|
|
|
///
|
2011-07-18 04:54:35 +00:00
|
|
|
static Constant *getFoldedAlignOf(Type *Ty, Type *DestTy,
|
2010-02-02 01:41:39 +00:00
|
|
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
2010-02-02 01:41:39 +00:00
|
|
|
Constant *C = ConstantExpr::getAlignOf(ATy->getElementType());
|
|
|
|
C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
|
|
|
|
DestTy,
|
|
|
|
false),
|
|
|
|
C, DestTy);
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty)) {
|
2010-02-02 01:41:39 +00:00
|
|
|
// 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.
|
2011-07-18 04:54:35 +00:00
|
|
|
if (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.
|
|
|
|
///
|
2011-07-18 04:54:35 +00:00
|
|
|
static Constant *getFoldedOffsetOf(Type *Ty, Constant *FieldNo,
|
|
|
|
Type *DestTy,
|
2010-02-01 18:27:38 +00:00
|
|
|
bool Folded) {
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
2010-02-01 18:27:38 +00:00
|
|
|
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
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty))
|
2010-02-01 18:27:38 +00:00
|
|
|
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,
|
2011-07-18 04:54:35 +00:00
|
|
|
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.
|
2012-01-27 03:08:05 +00:00
|
|
|
if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
|
|
|
|
DestTy->isVectorTy() &&
|
|
|
|
DestTy->getVectorNumElements() == V->getType()->getVectorNumElements()) {
|
|
|
|
SmallVector<Constant*, 16> res;
|
|
|
|
VectorType *DestVecTy = cast<VectorType>(DestTy);
|
|
|
|
Type *DstEltTy = DestVecTy->getElementType();
|
|
|
|
for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i)
|
|
|
|
res.push_back(ConstantExpr::getCast(opc,
|
|
|
|
V->getAggregateElement(i), DstEltTy));
|
|
|
|
return ConstantVector::get(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();
|
2011-12-17 00:04:22 +00:00
|
|
|
Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf :
|
|
|
|
DestTy->isFloatTy() ? APFloat::IEEEsingle :
|
2009-10-05 05:54:46 +00:00
|
|
|
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);
|
2011-07-18 21:45:40 +00:00
|
|
|
APInt Val(DestBitWidth, 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()) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty =
|
2010-02-01 18:27:38 +00:00
|
|
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty))
|
2010-02-01 18:27:38 +00:00
|
|
|
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) {
|
2012-01-26 02:32:04 +00:00
|
|
|
// Check for i1 and vector true/false conditions.
|
2011-02-11 19:37:55 +00:00
|
|
|
if (Cond->isNullValue()) return V2;
|
2012-01-26 02:32:04 +00:00
|
|
|
if (Cond->isAllOnesValue()) return V1;
|
|
|
|
|
|
|
|
// If the condition is a vector constant, fold the result elementwise.
|
|
|
|
if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
|
|
|
|
SmallVector<Constant*, 16> Result;
|
|
|
|
for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
|
|
|
|
ConstantInt *Cond = dyn_cast<ConstantInt>(CondV->getOperand(i));
|
|
|
|
if (Cond == 0) break;
|
|
|
|
|
|
|
|
Constant *Res = (Cond->getZExtValue() ? V2 : V1)->getAggregateElement(i);
|
|
|
|
if (Res == 0) break;
|
|
|
|
Result.push_back(Res);
|
2011-02-11 19:37:55 +00:00
|
|
|
}
|
2012-01-26 02:32:04 +00:00
|
|
|
|
|
|
|
// If we were able to build the vector, return it.
|
|
|
|
if (Result.size() == V1->getType()->getVectorNumElements())
|
|
|
|
return ConstantVector::get(Result);
|
2011-02-11 19:37:55 +00:00
|
|
|
}
|
|
|
|
|
2011-07-01 01:03:43 +00:00
|
|
|
if (isa<UndefValue>(Cond)) {
|
|
|
|
if (isa<UndefValue>(V1)) return V1;
|
|
|
|
return V2;
|
|
|
|
}
|
2009-09-20 01:35:59 +00:00
|
|
|
if (isa<UndefValue>(V1)) return V2;
|
|
|
|
if (isa<UndefValue>(V2)) 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
|
2012-01-27 03:08:05 +00:00
|
|
|
return UndefValue::get(Val->getType()->getVectorElementType());
|
2006-04-07 04:44:06 +00:00
|
|
|
if (Val->isNullValue()) // ee(zero, x) -> zero
|
2012-01-27 03:08:05 +00:00
|
|
|
return Constant::getNullValue(Val->getType()->getVectorElementType());
|
|
|
|
// ee({w,x,y,z}, undef) -> undef
|
|
|
|
if (isa<UndefValue>(Idx))
|
|
|
|
return UndefValue::get(Val->getType()->getVectorElementType());
|
|
|
|
|
|
|
|
if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
|
|
|
|
uint64_t Index = CIdx->getZExtValue();
|
|
|
|
// ee({w,x,y,z}, wrong_value) -> undef
|
|
|
|
if (Index >= Val->getType()->getVectorNumElements())
|
|
|
|
return UndefValue::get(Val->getType()->getVectorElementType());
|
|
|
|
return Val->getAggregateElement(Index);
|
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;
|
2012-01-26 02:32:04 +00:00
|
|
|
const APInt &IdxVal = CIdx->getValue();
|
|
|
|
|
|
|
|
SmallVector<Constant*, 16> Result;
|
|
|
|
for (unsigned i = 0, e = Val->getType()->getVectorNumElements(); i != e; ++i){
|
|
|
|
if (i == IdxVal) {
|
|
|
|
Result.push_back(Elt);
|
|
|
|
continue;
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
2012-01-26 02:32:04 +00:00
|
|
|
|
|
|
|
if (Constant *C = Val->getAggregateElement(i))
|
|
|
|
Result.push_back(C);
|
|
|
|
else
|
|
|
|
return 0;
|
2006-01-17 20:07:22 +00:00
|
|
|
}
|
2012-01-26 02:32:04 +00:00
|
|
|
|
|
|
|
return ConstantVector::get(Result);
|
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
llvm-svn: 44856
2007-12-11 07:49:37 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2012-01-30 05:34:13 +00:00
|
|
|
unsigned MaskNumElts = Mask->getType()->getVectorNumElements();
|
|
|
|
Type *EltTy = V1->getType()->getVectorElementType();
|
|
|
|
|
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
llvm-svn: 44856
2007-12-11 07:49:37 +00:00
|
|
|
// Undefined shuffle mask -> undefined value.
|
2012-01-30 05:34:13 +00:00
|
|
|
if (isa<UndefValue>(Mask))
|
|
|
|
return UndefValue::get(VectorType::get(EltTy, MaskNumElts));
|
2008-11-10 04:46:22 +00:00
|
|
|
|
2012-01-26 03:10:45 +00:00
|
|
|
// Don't break the bitcode reader hack.
|
|
|
|
if (isa<ConstantExpr>(Mask)) return 0;
|
|
|
|
|
2012-01-26 02:54:54 +00:00
|
|
|
unsigned SrcNumElts = V1->getType()->getVectorNumElements();
|
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
llvm-svn: 44856
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) {
|
2012-01-26 02:54:54 +00:00
|
|
|
int Elt = ShuffleVectorInst::getMaskValue(Mask, i);
|
|
|
|
if (Elt == -1) {
|
2012-01-26 02:32:04 +00:00
|
|
|
Result.push_back(UndefValue::get(EltTy));
|
|
|
|
continue;
|
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
llvm-svn: 44856
2007-12-11 07:49:37 +00:00
|
|
|
}
|
2012-01-26 02:54:54 +00:00
|
|
|
Constant *InElt;
|
|
|
|
if (unsigned(Elt) >= SrcNumElts*2)
|
2012-01-26 02:32:04 +00:00
|
|
|
InElt = UndefValue::get(EltTy);
|
2012-01-26 02:54:54 +00:00
|
|
|
else if (unsigned(Elt) >= SrcNumElts)
|
2012-01-26 02:32:04 +00:00
|
|
|
InElt = V2->getAggregateElement(Elt - SrcNumElts);
|
|
|
|
else
|
|
|
|
InElt = V1->getAggregateElement(Elt);
|
|
|
|
if (InElt == 0) 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
llvm-svn: 44856
2007-12-11 07:49:37 +00:00
|
|
|
Result.push_back(InElt);
|
|
|
|
}
|
2008-11-10 04:46:22 +00:00
|
|
|
|
2011-02-15 00:14:00 +00:00
|
|
|
return ConstantVector::get(Result);
|
2006-04-08 01:18:18 +00:00
|
|
|
}
|
|
|
|
|
2010-02-01 20:48:08 +00:00
|
|
|
Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
|
2011-07-13 10:26:04 +00:00
|
|
|
ArrayRef<unsigned> Idxs) {
|
2008-06-03 00:15:20 +00:00
|
|
|
// Base case: no indices, so return the entire value.
|
2011-07-13 10:26:04 +00:00
|
|
|
if (Idxs.empty())
|
2009-09-20 01:35:59 +00:00
|
|
|
return Agg;
|
2008-06-03 00:15:20 +00:00
|
|
|
|
2012-01-26 02:32:04 +00:00
|
|
|
if (Constant *C = Agg->getAggregateElement(Idxs[0]))
|
|
|
|
return ConstantFoldExtractValueInstruction(C, Idxs.slice(1));
|
|
|
|
|
|
|
|
return 0;
|
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,
|
2011-07-13 10:26:04 +00:00
|
|
|
ArrayRef<unsigned> Idxs) {
|
2008-06-03 00:15:20 +00:00
|
|
|
// Base case: no indices, so replace the entire value.
|
2011-07-13 10:26:04 +00:00
|
|
|
if (Idxs.empty())
|
2009-09-20 01:35:59 +00:00
|
|
|
return Val;
|
2008-06-03 00:15:20 +00:00
|
|
|
|
2012-01-26 02:32:04 +00:00
|
|
|
unsigned NumElts;
|
|
|
|
if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
|
|
|
|
NumElts = ST->getNumElements();
|
|
|
|
else if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
|
|
|
|
NumElts = AT->getNumElements();
|
|
|
|
else
|
|
|
|
NumElts = AT->getVectorNumElements();
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2012-01-26 02:32:04 +00:00
|
|
|
SmallVector<Constant*, 32> Result;
|
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
Constant *C = Agg->getAggregateElement(i);
|
|
|
|
if (C == 0) return 0;
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2012-01-26 02:32:04 +00:00
|
|
|
if (Idxs[0] == i)
|
|
|
|
C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1));
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2012-01-26 02:32:04 +00:00
|
|
|
Result.push_back(C);
|
2008-06-03 00:15:20 +00:00
|
|
|
}
|
2009-09-15 06:28:12 +00:00
|
|
|
|
2012-01-26 02:32:04 +00:00
|
|
|
if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
|
|
|
|
return ConstantStruct::get(ST, Result);
|
|
|
|
if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
|
|
|
|
return ConstantArray::get(AT, Result);
|
|
|
|
return ConstantVector::get(Result);
|
2008-05-15 19:50:34 +00:00
|
|
|
}
|
|
|
|
|
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::And:
|
2011-07-01 00:42:17 +00:00
|
|
|
if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
|
|
|
|
return C1;
|
|
|
|
return Constant::getNullValue(C1->getType()); // undef & X -> 0
|
|
|
|
case Instruction::Mul: {
|
|
|
|
ConstantInt *CI;
|
|
|
|
// X * undef -> undef if X is odd or undef
|
|
|
|
if (((CI = dyn_cast<ConstantInt>(C1)) && CI->getValue()[0]) ||
|
|
|
|
((CI = dyn_cast<ConstantInt>(C2)) && CI->getValue()[0]) ||
|
|
|
|
(isa<UndefValue>(C1) && isa<UndefValue>(C2)))
|
|
|
|
return UndefValue::get(C1->getType());
|
|
|
|
|
|
|
|
// X * undef -> 0 otherwise
|
2009-07-31 20:28:14 +00:00
|
|
|
return Constant::getNullValue(C1->getType());
|
2011-07-01 00:42:17 +00:00
|
|
|
}
|
2006-12-23 06:05:41 +00:00
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
2011-07-01 00:42:17 +00:00
|
|
|
// undef / 1 -> undef
|
|
|
|
if (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv)
|
|
|
|
if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2))
|
|
|
|
if (CI2->isOne())
|
|
|
|
return C1;
|
|
|
|
// FALL THROUGH
|
2006-12-23 06:05:41 +00:00
|
|
|
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
|
2011-07-01 00:42:17 +00:00
|
|
|
if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
|
|
|
|
return C1;
|
|
|
|
return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
|
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:
|
2011-07-01 00:42:17 +00:00
|
|
|
if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
|
|
|
|
return C1; // undef shl undef -> undef
|
2006-12-23 06:05:41 +00:00
|
|
|
// 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)) {
|
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
|
|
|
}
|
|
|
|
}
|
2011-07-18 04:54:35 +00:00
|
|
|
} else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
|
2012-01-26 02:32:04 +00:00
|
|
|
// Perform elementwise folding.
|
|
|
|
SmallVector<Constant*, 16> Result;
|
|
|
|
for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
|
|
|
|
Constant *LHS = C1->getAggregateElement(i);
|
|
|
|
Constant *RHS = C2->getAggregateElement(i);
|
|
|
|
if (LHS == 0 || RHS == 0) break;
|
|
|
|
|
|
|
|
Result.push_back(ConstantExpr::get(Opcode, LHS, RHS));
|
2006-12-23 06:05:41 +00:00
|
|
|
}
|
2012-01-26 02:32:04 +00:00
|
|
|
|
|
|
|
if (Result.size() == VTy->getNumElements())
|
|
|
|
return ConstantVector::get(Result);
|
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.
|
2011-07-18 04:54:35 +00:00
|
|
|
static bool isMaybeZeroSizedType(Type *Ty) {
|
|
|
|
if (StructType *STy = dyn_cast<StructType>(Ty)) {
|
Land the long talked about "type system rewrite" patch. This
patch brings numerous advantages to LLVM. One way to look at it
is through diffstat:
109 files changed, 3005 insertions(+), 5906 deletions(-)
Removing almost 3K lines of code is a good thing. Other advantages
include:
1. Value::getType() is a simple load that can be CSE'd, not a mutating
union-find operation.
2. Types a uniqued and never move once created, defining away PATypeHolder.
3. Structs can be "named" now, and their name is part of the identity that
uniques them. This means that the compiler doesn't merge them structurally
which makes the IR much less confusing.
4. Now that there is no way to get a cycle in a type graph without a named
struct type, "upreferences" go away.
5. Type refinement is completely gone, which should make LTO much MUCH faster
in some common cases with C++ code.
6. Types are now generally immutable, so we can use "Type *" instead
"const Type *" everywhere.
Downsides of this patch are that it removes some functions from the C API,
so people using those will have to upgrade to (not yet added) new API.
"LLVM 3.0" is the right time to do this.
There are still some cleanups pending after this, this patch is large enough
as-is.
llvm-svn: 134829
2011-07-09 17:41:24 +00:00
|
|
|
if (STy->isOpaque()) return true; // Can't say.
|
2005-01-28 19:09:51 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
} else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
2005-01-28 19:09:51 +00:00
|
|
|
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-07-18 04:54:35 +00:00
|
|
|
static int IdxCompare(Constant *C1, Constant *C2, 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() &&
|
2011-04-15 05:18:47 +00:00
|
|
|
"Surprising 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) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *ResultTy;
|
|
|
|
if (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.
|
2011-07-01 01:03:43 +00:00
|
|
|
// Also, if both operands are undef, we can return undef.
|
|
|
|
if (ICmpInst::isEquality(ICmpInst::Predicate(pred)) ||
|
|
|
|
(isa<UndefValue>(C1) && isa<UndefValue>(C2)))
|
2010-06-28 21:30:07 +00:00
|
|
|
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) {
|
2012-01-20 21:51:11 +00:00
|
|
|
default: llvm_unreachable("Invalid ICmp Predicate");
|
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) {
|
2012-01-20 21:51:11 +00:00
|
|
|
default: llvm_unreachable("Invalid FCmp Predicate");
|
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
|
|
|
// If we can constant fold the comparison of each element, constant fold
|
|
|
|
// the whole vector comparison.
|
|
|
|
SmallVector<Constant*, 4> ResElts;
|
2011-02-15 00:14:00 +00:00
|
|
|
// Compare the elements, producing an i1 result or constant expr.
|
2012-01-26 02:51:13 +00:00
|
|
|
for (unsigned i = 0, e = C1->getType()->getVectorNumElements(); i != e;++i){
|
|
|
|
Constant *C1E = C1->getAggregateElement(i);
|
|
|
|
Constant *C2E = C2->getAggregateElement(i);
|
|
|
|
if (C1E == 0 || C2E == 0) break;
|
|
|
|
|
|
|
|
ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ResElts.size() == C1->getType()->getVectorNumElements())
|
|
|
|
return ConstantVector::get(ResElts);
|
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;
|
2011-01-30 01:49:58 +00:00
|
|
|
case FCmpInst::FCMP_ONE: // We know that C1 != C2
|
2006-12-24 18:52:08 +00:00
|
|
|
// 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>
|
2011-07-21 14:31:17 +00:00
|
|
|
static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) {
|
2009-09-11 00:04:14 +00:00
|
|
|
// No indices means nothing that could be out of bounds.
|
2011-07-21 14:31:17 +00:00
|
|
|
if (Idxs.empty()) return true;
|
2009-09-11 00:04:14 +00:00
|
|
|
|
|
|
|
// 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;
|
2011-07-21 14:31:17 +00:00
|
|
|
for (unsigned i = 1, e = Idxs.size(); 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,
|
2011-07-19 15:30:30 +00:00
|
|
|
ArrayRef<IndexTy> Idxs) {
|
|
|
|
if (Idxs.empty()) return C;
|
2011-01-14 08:07:43 +00:00
|
|
|
Constant *Idx0 = cast<Constant>(Idxs[0]);
|
2011-07-19 15:30:30 +00:00
|
|
|
if ((Idxs.size() == 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)) {
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *Ptr = cast<PointerType>(C->getType());
|
2011-07-25 09:48:08 +00:00
|
|
|
Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs);
|
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;
|
2011-07-19 15:30:30 +00:00
|
|
|
for (unsigned i = 0, e = Idxs.size(); 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) {
|
2011-07-18 04:54:35 +00:00
|
|
|
PointerType *Ptr = cast<PointerType>(C->getType());
|
2011-07-25 09:48:08 +00:00
|
|
|
Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs);
|
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) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *LastTy = 0;
|
2004-01-12 22:07:24 +00:00
|
|
|
for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
|
|
|
|
I != E; ++I)
|
|
|
|
LastTy = *I;
|
|
|
|
|
2011-12-15 04:33:48 +00:00
|
|
|
if ((LastTy && isa<SequentialType>(LastTy)) || Idx0->isNullValue()) {
|
2007-01-31 04:40:28 +00:00
|
|
|
SmallVector<Value*, 16> NewIndices;
|
2011-07-19 15:30:30 +00:00
|
|
|
NewIndices.reserve(Idxs.size() + 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()) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *IdxTy = Combined->getType();
|
2006-12-04 02:46:44 +00:00
|
|
|
if (IdxTy != Idx0->getType()) {
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Int64Ty = Type::getInt64Ty(IdxTy->getContext());
|
2010-02-01 20:48:08 +00:00
|
|
|
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);
|
2011-07-19 15:30:30 +00:00
|
|
|
NewIndices.append(Idxs.begin() + 1, Idxs.end());
|
2011-07-21 15:15:37 +00:00
|
|
|
return
|
|
|
|
ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices,
|
|
|
|
inBounds &&
|
|
|
|
cast<GEPOperator>(CE)->isInBounds());
|
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
|
|
|
//
|
2011-07-19 15:30:30 +00:00
|
|
|
if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
|
2011-07-18 04:54:35 +00:00
|
|
|
if (PointerType *SPT =
|
2004-01-12 22:07:24 +00:00
|
|
|
dyn_cast<PointerType>(CE->getOperand(0)->getType()))
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
|
|
|
|
if (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())
|
2011-07-21 15:15:37 +00:00
|
|
|
return
|
|
|
|
ConstantExpr::getGetElementPtr((Constant*)CE->getOperand(0),
|
|
|
|
Idxs, inBounds);
|
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;
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty = C->getType();
|
|
|
|
Type *Prev = 0;
|
2011-07-19 15:30:30 +00:00
|
|
|
for (unsigned i = 0, e = Idxs.size(); i != e;
|
2009-09-11 00:04:14 +00:00
|
|
|
Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
|
|
|
|
if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
|
2011-07-18 04:54:35 +00:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(Ty))
|
2009-09-11 00:04:14 +00:00
|
|
|
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.
|
2011-07-19 15:30:30 +00:00
|
|
|
NewIdxs.resize(Idxs.size());
|
2009-09-11 00:04:14 +00:00
|
|
|
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()) {
|
2011-07-19 15:30:30 +00:00
|
|
|
for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
|
2011-01-14 08:07:43 +00:00
|
|
|
if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
|
2011-07-21 15:15:37 +00:00
|
|
|
return ConstantExpr::getGetElementPtr(C, NewIdxs, inBounds);
|
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 &&
|
2011-07-21 14:31:17 +00:00
|
|
|
isa<GlobalVariable>(C) && isInBoundsIndices(Idxs))
|
|
|
|
return ConstantExpr::getInBoundsGetElementPtr(C, Idxs);
|
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,
|
2011-07-19 15:30:30 +00:00
|
|
|
ArrayRef<Constant *> Idxs) {
|
|
|
|
return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs);
|
2011-01-14 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
|
|
|
|
bool inBounds,
|
2011-07-19 15:30:30 +00:00
|
|
|
ArrayRef<Value *> Idxs) {
|
|
|
|
return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs);
|
2011-01-14 08:07:43 +00:00
|
|
|
}
|