blob: c950e8d07ffd81da749e602e9e1f47cc2cacf26c [file] [log] [blame]
Reid Spencer9472c372007-02-27 06:23:51 +00001//===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnercbfd4062004-01-12 21:13:12 +000010// This file implements folding of constants for LLVM. This implements the
Reid Spencer9472c372007-02-27 06:23:51 +000011// (internal) ConstantFold.h interface, which is used by the
Chris Lattnercbfd4062004-01-12 21:13:12 +000012// ConstantExpr::get* methods to automatically fold constants when possible.
Chris Lattner00950542001-06-06 20:29:01 +000013//
Chris Lattnereab20b52004-01-12 22:07:24 +000014// The current constant folding implementation is implemented in two pieces: the
15// template-based folder for simple primitive constants like ConstantInt, and
16// the special case hackery that we use to symbolically evaluate expressions
17// that use ConstantExprs.
18//
Chris Lattner00950542001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner92f6fea2007-02-27 03:05:06 +000021#include "ConstantFold.h"
Chris Lattner8b0f0cb2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattnerb228f9c2004-02-22 06:25:38 +000023#include "llvm/Instructions.h"
Chris Lattner27287de2003-04-17 19:24:18 +000024#include "llvm/DerivedTypes.h"
Chris Lattner802b5ab2004-03-08 06:17:35 +000025#include "llvm/Function.h"
Chris Lattner2b9a5da2007-01-31 04:40:28 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattner0eff5ad2006-10-13 17:22:21 +000028#include "llvm/Support/GetElementPtrTypeIterator.h"
29#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/MathExtras.h"
Jeff Cohena97e8db2005-05-03 03:13:01 +000031#include <limits>
Chris Lattner0dc39692003-11-17 19:05:17 +000032using namespace llvm;
Chris Lattner14712a62001-09-09 21:01:20 +000033
Chris Lattnereab20b52004-01-12 22:07:24 +000034//===----------------------------------------------------------------------===//
35// ConstantFold*Instruction Implementations
36//===----------------------------------------------------------------------===//
Chris Lattnereab20b52004-01-12 22:07:24 +000037
Reid Spencer9d6565a2007-02-15 02:26:10 +000038/// CastConstantVector - Convert the specified ConstantVector node to the
Reid Spencerac9dcb92007-02-15 03:39:18 +000039/// specified vector type. At this point, we know that the elements of the
Dan Gohman07a96762007-07-16 14:29:03 +000040/// input vector constant are all simple integer or FP values.
Reid Spencer9472c372007-02-27 06:23:51 +000041static Constant *CastConstantVector(ConstantVector *CV,
Reid Spencer9d6565a2007-02-15 02:26:10 +000042 const VectorType *DstTy) {
Reid Spencer9472c372007-02-27 06:23:51 +000043 unsigned SrcNumElts = CV->getType()->getNumElements();
Chris Lattner4460f402006-04-02 01:38:28 +000044 unsigned DstNumElts = DstTy->getNumElements();
Reid Spencer9472c372007-02-27 06:23:51 +000045 const Type *SrcEltTy = CV->getType()->getElementType();
Chris Lattner4460f402006-04-02 01:38:28 +000046 const Type *DstEltTy = DstTy->getElementType();
47
48 // If both vectors have the same number of elements (thus, the elements
49 // are the same size), perform the conversion now.
50 if (SrcNumElts == DstNumElts) {
51 std::vector<Constant*> Result;
52
Reid Spencer3da59db2006-11-27 01:05:10 +000053 // If the src and dest elements are both integers, or both floats, we can
54 // just BitCast each element because the elements are the same size.
Chris Lattner42a75512007-01-15 02:27:26 +000055 if ((SrcEltTy->isInteger() && DstEltTy->isInteger()) ||
Reid Spencer3da59db2006-11-27 01:05:10 +000056 (SrcEltTy->isFloatingPoint() && DstEltTy->isFloatingPoint())) {
Chris Lattner4460f402006-04-02 01:38:28 +000057 for (unsigned i = 0; i != SrcNumElts; ++i)
Reid Spencer3da59db2006-11-27 01:05:10 +000058 Result.push_back(
Reid Spencer9472c372007-02-27 06:23:51 +000059 ConstantExpr::getBitCast(CV->getOperand(i), DstEltTy));
Reid Spencer9d6565a2007-02-15 02:26:10 +000060 return ConstantVector::get(Result);
Chris Lattner4460f402006-04-02 01:38:28 +000061 }
62
Reid Spencer3da59db2006-11-27 01:05:10 +000063 // If this is an int-to-fp cast ..
Chris Lattner42a75512007-01-15 02:27:26 +000064 if (SrcEltTy->isInteger()) {
Reid Spencer3da59db2006-11-27 01:05:10 +000065 // Ensure that it is int-to-fp cast
Chris Lattner4460f402006-04-02 01:38:28 +000066 assert(DstEltTy->isFloatingPoint());
67 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
68 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencer0d54b7d2007-03-01 20:44:23 +000069 ConstantInt *CI = cast<ConstantInt>(CV->getOperand(i));
70 double V = CI->getValue().bitsToDouble();
Chris Lattner4460f402006-04-02 01:38:28 +000071 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
72 }
Reid Spencer9d6565a2007-02-15 02:26:10 +000073 return ConstantVector::get(Result);
Chris Lattner4460f402006-04-02 01:38:28 +000074 }
75 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
76 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencer0d54b7d2007-03-01 20:44:23 +000077 ConstantInt *CI = cast<ConstantInt>(CV->getOperand(i));
78 float V = CI->getValue().bitsToFloat();
Chris Lattner4460f402006-04-02 01:38:28 +000079 Result.push_back(ConstantFP::get(Type::FloatTy, V));
80 }
Reid Spencer9d6565a2007-02-15 02:26:10 +000081 return ConstantVector::get(Result);
Chris Lattner4460f402006-04-02 01:38:28 +000082 }
83
84 // Otherwise, this is an fp-to-int cast.
Chris Lattner42a75512007-01-15 02:27:26 +000085 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isInteger());
Chris Lattner4460f402006-04-02 01:38:28 +000086
87 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
88 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencera188bbe2007-03-03 08:32:46 +000089 uint64_t V =
Reid Spencer9472c372007-02-27 06:23:51 +000090 DoubleToBits(cast<ConstantFP>(CV->getOperand(i))->getValue());
Reid Spencera188bbe2007-03-03 08:32:46 +000091 Constant *C = ConstantInt::get(Type::Int64Ty, V);
Reid Spencer6f40b832006-12-05 07:18:07 +000092 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
Chris Lattner4460f402006-04-02 01:38:28 +000093 }
Reid Spencer9d6565a2007-02-15 02:26:10 +000094 return ConstantVector::get(Result);
Chris Lattner4460f402006-04-02 01:38:28 +000095 }
96
97 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
98 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencer9472c372007-02-27 06:23:51 +000099 uint32_t V = FloatToBits(cast<ConstantFP>(CV->getOperand(i))->getValue());
Reid Spencer79e21d32006-12-31 05:26:44 +0000100 Constant *C = ConstantInt::get(Type::Int32Ty, V);
Reid Spencer6f40b832006-12-05 07:18:07 +0000101 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
Chris Lattner4460f402006-04-02 01:38:28 +0000102 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000103 return ConstantVector::get(Result);
Chris Lattner4460f402006-04-02 01:38:28 +0000104 }
105
106 // Otherwise, this is a cast that changes element count and size. Handle
107 // casts which shrink the elements here.
108
109 // FIXME: We need to know endianness to do this!
110
111 return 0;
112}
113
Reid Spencer3da59db2006-11-27 01:05:10 +0000114/// This function determines which opcode to use to fold two constant cast
115/// expressions together. It uses CastInst::isEliminableCastPair to determine
116/// the opcode. Consequently its just a wrapper around that function.
Reid Spencerdaa10a42007-08-05 19:27:01 +0000117/// @brief Determine if it is valid to fold a cast of a cast
Reid Spencer3da59db2006-11-27 01:05:10 +0000118static unsigned
119foldConstantCastPair(
120 unsigned opc, ///< opcode of the second cast constant expression
121 const ConstantExpr*Op, ///< the first cast constant expression
122 const Type *DstTy ///< desintation type of the first cast
123) {
124 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
125 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
126 assert(CastInst::isCast(opc) && "Invalid cast opcode");
127
128 // The the types and opcodes for the two Cast constant expressions
129 const Type *SrcTy = Op->getOperand(0)->getType();
130 const Type *MidTy = Op->getType();
131 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
132 Instruction::CastOps secondOp = Instruction::CastOps(opc);
Chris Lattner4460f402006-04-02 01:38:28 +0000133
Reid Spencer3da59db2006-11-27 01:05:10 +0000134 // Let CastInst::isEliminableCastPair do the heavy lifting.
135 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
Reid Spencer79e21d32006-12-31 05:26:44 +0000136 Type::Int64Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +0000137}
138
139Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
Chris Lattnereab20b52004-01-12 22:07:24 +0000140 const Type *DestTy) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000141 const Type *SrcTy = V->getType();
Chris Lattnereab20b52004-01-12 22:07:24 +0000142
Chris Lattnerd2f09962007-07-20 22:09:02 +0000143 if (isa<UndefValue>(V)) {
144 // zext(undef) = 0, because the top bits will be zero.
145 // sext(undef) = 0, because the top bits will all be the same.
146 if (opc == Instruction::ZExt || opc == Instruction::SExt)
147 return Constant::getNullValue(DestTy);
Reid Spencer3da59db2006-11-27 01:05:10 +0000148 return UndefValue::get(DestTy);
Chris Lattnerd2f09962007-07-20 22:09:02 +0000149 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000150
151 // If the cast operand is a constant expression, there's a few things we can
152 // do to try to simplify it.
153 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
154 if (CE->isCast()) {
Reid Spencer575d95c2006-12-04 02:46:44 +0000155 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer3da59db2006-11-27 01:05:10 +0000156 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
157 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattnereab20b52004-01-12 22:07:24 +0000158 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
159 // If all of the indexes in the GEP are null values, there is no pointer
160 // adjustment going on. We might as well cast the source pointer.
161 bool isAllNull = true;
162 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
163 if (!CE->getOperand(i)->isNullValue()) {
164 isAllNull = false;
165 break;
166 }
167 if (isAllNull)
Reid Spencer575d95c2006-12-04 02:46:44 +0000168 // This is casting one pointer type to another, always BitCast
Reid Spencer57c69932006-12-05 03:30:09 +0000169 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
Chris Lattnereab20b52004-01-12 22:07:24 +0000170 }
Chris Lattner71d37782004-10-16 23:31:32 +0000171 }
Chris Lattnereab20b52004-01-12 22:07:24 +0000172
Reid Spencer390437f2006-12-19 03:15:47 +0000173 // We actually have to do a cast now. Perform the cast according to the
174 // opcode specified.
Reid Spencer3da59db2006-11-27 01:05:10 +0000175 switch (opc) {
176 case Instruction::FPTrunc:
Reid Spencer3da59db2006-11-27 01:05:10 +0000177 case Instruction::FPExt:
Reid Spencer85e36e42006-12-19 07:41:40 +0000178 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
179 return ConstantFP::get(DestTy, FPC->getValue());
180 return 0; // Can't fold.
181 case Instruction::FPToUI:
Reid Spencer9472c372007-02-27 06:23:51 +0000182 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Reid Spencer9472c372007-02-27 06:23:51 +0000183 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Reid Spencera5c74722007-02-27 19:29:54 +0000184 APInt Val(APIntOps::RoundDoubleToAPInt(FPC->getValue(), DestBitWidth));
Reid Spenceref2185b2007-03-01 19:31:12 +0000185 return ConstantInt::get(Val);
Reid Spencer9472c372007-02-27 06:23:51 +0000186 }
Reid Spencer85e36e42006-12-19 07:41:40 +0000187 return 0; // Can't fold.
188 case Instruction::FPToSI:
Reid Spencer9472c372007-02-27 06:23:51 +0000189 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Reid Spencer9472c372007-02-27 06:23:51 +0000190 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Reid Spencera5c74722007-02-27 19:29:54 +0000191 APInt Val(APIntOps::RoundDoubleToAPInt(FPC->getValue(), DestBitWidth));
Reid Spenceref2185b2007-03-01 19:31:12 +0000192 return ConstantInt::get(Val);
Reid Spencer9472c372007-02-27 06:23:51 +0000193 }
Reid Spencer85e36e42006-12-19 07:41:40 +0000194 return 0; // Can't fold.
195 case Instruction::IntToPtr: //always treated as unsigned
196 if (V->isNullValue()) // Is it an integral null value?
Reid Spencer390437f2006-12-19 03:15:47 +0000197 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Reid Spencer85e36e42006-12-19 07:41:40 +0000198 return 0; // Other pointer types cannot be casted
199 case Instruction::PtrToInt: // always treated as unsigned
200 if (V->isNullValue()) // is it a null pointer value?
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000201 return ConstantInt::get(DestTy, 0);
Reid Spencer85e36e42006-12-19 07:41:40 +0000202 return 0; // Other pointer types cannot be casted
203 case Instruction::UIToFP:
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000204 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer012d50b2007-02-27 23:33:03 +0000205 return ConstantFP::get(DestTy, CI->getValue().roundToDouble());
Reid Spencer85e36e42006-12-19 07:41:40 +0000206 return 0;
207 case Instruction::SIToFP:
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000208 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer012d50b2007-02-27 23:33:03 +0000209 return ConstantFP::get(DestTy, CI->getValue().signedRoundToDouble());
Reid Spencer85e36e42006-12-19 07:41:40 +0000210 return 0;
Reid Spencer390437f2006-12-19 03:15:47 +0000211 case Instruction::ZExt:
Reid Spencer9472c372007-02-27 06:23:51 +0000212 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
213 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
214 APInt Result(CI->getValue());
215 Result.zext(BitWidth);
Reid Spenceref2185b2007-03-01 19:31:12 +0000216 return ConstantInt::get(Result);
Reid Spencer9472c372007-02-27 06:23:51 +0000217 }
Reid Spencer390437f2006-12-19 03:15:47 +0000218 return 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000219 case Instruction::SExt:
Reid Spencer9472c372007-02-27 06:23:51 +0000220 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
221 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
222 APInt Result(CI->getValue());
223 Result.sext(BitWidth);
Reid Spenceref2185b2007-03-01 19:31:12 +0000224 return ConstantInt::get(Result);
Reid Spencer9472c372007-02-27 06:23:51 +0000225 }
Reid Spencer390437f2006-12-19 03:15:47 +0000226 return 0;
Chris Lattner5be66252006-12-01 19:22:41 +0000227 case Instruction::Trunc:
Reid Spencer9472c372007-02-27 06:23:51 +0000228 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
229 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
230 APInt Result(CI->getValue());
231 Result.trunc(BitWidth);
Reid Spenceref2185b2007-03-01 19:31:12 +0000232 return ConstantInt::get(Result);
Reid Spencer9472c372007-02-27 06:23:51 +0000233 }
Chris Lattner5be66252006-12-01 19:22:41 +0000234 return 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000235 case Instruction::BitCast:
Reid Spencer390437f2006-12-19 03:15:47 +0000236 if (SrcTy == DestTy)
237 return (Constant*)V; // no-op cast
Chris Lattner14f440a2006-12-11 18:30:27 +0000238
Reid Spencer3da59db2006-11-27 01:05:10 +0000239 // Check to see if we are casting a pointer to an aggregate to a pointer to
240 // the first element. If so, return the appropriate GEP instruction.
241 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
242 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
Chris Lattner2b9a5da2007-01-31 04:40:28 +0000243 SmallVector<Value*, 8> IdxList;
Reid Spencer79e21d32006-12-31 05:26:44 +0000244 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer3da59db2006-11-27 01:05:10 +0000245 const Type *ElTy = PTy->getElementType();
246 while (ElTy != DPTy->getElementType()) {
247 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
248 if (STy->getNumElements() == 0) break;
249 ElTy = STy->getElementType(0);
Reid Spencer79e21d32006-12-31 05:26:44 +0000250 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer3da59db2006-11-27 01:05:10 +0000251 } else if (const SequentialType *STy =
252 dyn_cast<SequentialType>(ElTy)) {
253 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
254 ElTy = STy->getElementType();
255 IdxList.push_back(IdxList[0]);
256 } else {
Chris Lattner4460f402006-04-02 01:38:28 +0000257 break;
258 }
259 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000260
261 if (ElTy == DPTy->getElementType())
262 return ConstantExpr::getGetElementPtr(
Chris Lattner2b9a5da2007-01-31 04:40:28 +0000263 const_cast<Constant*>(V), &IdxList[0], IdxList.size());
Reid Spencer3da59db2006-11-27 01:05:10 +0000264 }
265
Dan Gohman07a96762007-07-16 14:29:03 +0000266 // Handle casts from one vector constant to another. We know that the src
Reid Spencer3da59db2006-11-27 01:05:10 +0000267 // and dest type have the same size (otherwise its an illegal cast).
Reid Spencer9d6565a2007-02-15 02:26:10 +0000268 if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
269 if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000270 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
271 "Not cast between same sized vectors!");
272 // First, check for null and undef
273 if (isa<ConstantAggregateZero>(V))
274 return Constant::getNullValue(DestTy);
275 if (isa<UndefValue>(V))
276 return UndefValue::get(DestTy);
277
Reid Spencer9472c372007-02-27 06:23:51 +0000278 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
Reid Spencer9d6565a2007-02-15 02:26:10 +0000279 // This is a cast from a ConstantVector of one type to a
280 // ConstantVector of another type. Check to see if all elements of
Reid Spencer3da59db2006-11-27 01:05:10 +0000281 // the input are simple.
282 bool AllSimpleConstants = true;
Reid Spencer9472c372007-02-27 06:23:51 +0000283 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
284 if (!isa<ConstantInt>(CV->getOperand(i)) &&
285 !isa<ConstantFP>(CV->getOperand(i))) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000286 AllSimpleConstants = false;
287 break;
288 }
289 }
290
291 // If all of the elements are simple constants, we can fold this.
292 if (AllSimpleConstants)
Reid Spencer9472c372007-02-27 06:23:51 +0000293 return CastConstantVector(const_cast<ConstantVector*>(CV), DestPTy);
Reid Spencer3da59db2006-11-27 01:05:10 +0000294 }
Chris Lattner4460f402006-04-02 01:38:28 +0000295 }
296 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000297
Chris Lattner14f440a2006-12-11 18:30:27 +0000298 // Finally, implement bitcast folding now. The code below doesn't handle
299 // bitcast right.
300 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
301 return ConstantPointerNull::get(cast<PointerType>(DestTy));
302
303 // Handle integral constant input.
304 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Chris Lattner42a75512007-01-15 02:27:26 +0000305 if (DestTy->isInteger())
Reid Spencer9472c372007-02-27 06:23:51 +0000306 // Integral -> Integral. This is a no-op because the bit widths must
307 // be the same. Consequently, we just fold to V.
308 return const_cast<Constant*>(V);
Chris Lattner14f440a2006-12-11 18:30:27 +0000309
310 if (DestTy->isFloatingPoint()) {
311 if (DestTy == Type::FloatTy)
Reid Spencer0d54b7d2007-03-01 20:44:23 +0000312 return ConstantFP::get(DestTy, CI->getValue().bitsToFloat());
Chris Lattner14f440a2006-12-11 18:30:27 +0000313 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
Reid Spencer0d54b7d2007-03-01 20:44:23 +0000314 return ConstantFP::get(DestTy, CI->getValue().bitsToDouble());
Chris Lattner14f440a2006-12-11 18:30:27 +0000315 }
Dan Gohman07a96762007-07-16 14:29:03 +0000316 // Otherwise, can't fold this (vector?)
Chris Lattner14f440a2006-12-11 18:30:27 +0000317 return 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000318 }
Chris Lattner14f440a2006-12-11 18:30:27 +0000319
320 // Handle ConstantFP input.
321 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
322 // FP -> Integral.
Chris Lattner775bd832007-02-06 02:22:56 +0000323 if (DestTy == Type::Int32Ty) {
Reid Spencer0d54b7d2007-03-01 20:44:23 +0000324 APInt Val(32, 0);
325 return ConstantInt::get(Val.floatToBits(FP->getValue()));
Chris Lattner775bd832007-02-06 02:22:56 +0000326 } else {
327 assert(DestTy == Type::Int64Ty && "only support f32/f64 for now!");
Reid Spencer0d54b7d2007-03-01 20:44:23 +0000328 APInt Val(64, 0);
329 return ConstantInt::get(Val.doubleToBits(FP->getValue()));
Chris Lattner775bd832007-02-06 02:22:56 +0000330 }
Chris Lattner14f440a2006-12-11 18:30:27 +0000331 }
332 return 0;
Reid Spencer3da59db2006-11-27 01:05:10 +0000333 default:
334 assert(!"Invalid CE CastInst opcode");
335 break;
Chris Lattner4460f402006-04-02 01:38:28 +0000336 }
Chris Lattnerf8d10972004-10-11 03:57:30 +0000337
Reid Spencer390437f2006-12-19 03:15:47 +0000338 assert(0 && "Failed to cast constant expression");
339 return 0;
Chris Lattnereab20b52004-01-12 22:07:24 +0000340}
341
Chris Lattnere9714862004-03-12 05:53:32 +0000342Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
343 const Constant *V1,
344 const Constant *V2) {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000345 if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
Reid Spencer579dca12007-01-12 04:24:46 +0000346 return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
Chris Lattner71d37782004-10-16 23:31:32 +0000347
348 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
349 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
350 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattner2f690c82006-01-05 07:49:30 +0000351 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattnere9714862004-03-12 05:53:32 +0000352 return 0;
353}
354
Robert Bocchinobb90a7a2006-01-10 20:03:46 +0000355Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
356 const Constant *Idx) {
Chris Lattner6fa4cdf2006-03-31 18:31:40 +0000357 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
Reid Spencer9d6565a2007-02-15 02:26:10 +0000358 return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
Chris Lattnerf38d4712006-04-07 04:44:06 +0000359 if (Val->isNullValue()) // ee(zero, x) -> zero
360 return Constant::getNullValue(
Reid Spencer9d6565a2007-02-15 02:26:10 +0000361 cast<VectorType>(Val->getType())->getElementType());
Chris Lattner6fa4cdf2006-03-31 18:31:40 +0000362
Reid Spencer9d6565a2007-02-15 02:26:10 +0000363 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000364 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
365 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattner6fa4cdf2006-03-31 18:31:40 +0000366 } else if (isa<UndefValue>(Idx)) {
367 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
368 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinobb90a7a2006-01-10 20:03:46 +0000369 }
Chris Lattner6fa4cdf2006-03-31 18:31:40 +0000370 }
Robert Bocchinobb90a7a2006-01-10 20:03:46 +0000371 return 0;
372}
373
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000374Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
375 const Constant *Elt,
376 const Constant *Idx) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000377 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000378 if (!CIdx) return 0;
Reid Spencer9472c372007-02-27 06:23:51 +0000379 APInt idxVal = CIdx->getValue();
Reid Spencer4ab09d42006-11-02 08:18:15 +0000380 if (isa<UndefValue>(Val)) {
Dan Gohman07a96762007-07-16 14:29:03 +0000381 // Insertion of scalar constant into vector undef
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000382 // Optimize away insertion of undef
383 if (isa<UndefValue>(Elt))
384 return const_cast<Constant*>(Val);
385 // Otherwise break the aggregate undef into multiple undefs and do
386 // the insertion
387 unsigned numOps =
Reid Spencer9d6565a2007-02-15 02:26:10 +0000388 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000389 std::vector<Constant*> Ops;
390 Ops.reserve(numOps);
391 for (unsigned i = 0; i < numOps; ++i) {
392 const Constant *Op =
Reid Spencer9472c372007-02-27 06:23:51 +0000393 (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000394 Ops.push_back(const_cast<Constant*>(Op));
395 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000396 return ConstantVector::get(Ops);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000397 }
Reid Spencer4ab09d42006-11-02 08:18:15 +0000398 if (isa<ConstantAggregateZero>(Val)) {
Dan Gohman07a96762007-07-16 14:29:03 +0000399 // Insertion of scalar constant into vector aggregate zero
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000400 // Optimize away insertion of zero
401 if (Elt->isNullValue())
402 return const_cast<Constant*>(Val);
403 // Otherwise break the aggregate zero into multiple zeros and do
404 // the insertion
405 unsigned numOps =
Reid Spencer9d6565a2007-02-15 02:26:10 +0000406 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000407 std::vector<Constant*> Ops;
408 Ops.reserve(numOps);
409 for (unsigned i = 0; i < numOps; ++i) {
410 const Constant *Op =
Reid Spencer9472c372007-02-27 06:23:51 +0000411 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000412 Ops.push_back(const_cast<Constant*>(Op));
413 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000414 return ConstantVector::get(Ops);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000415 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000416 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Dan Gohman07a96762007-07-16 14:29:03 +0000417 // Insertion of scalar constant into vector constant
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000418 std::vector<Constant*> Ops;
419 Ops.reserve(CVal->getNumOperands());
420 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
421 const Constant *Op =
Reid Spencer9472c372007-02-27 06:23:51 +0000422 (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000423 Ops.push_back(const_cast<Constant*>(Op));
424 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000425 return ConstantVector::get(Ops);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000426 }
427 return 0;
428}
429
Chris Lattner00f10232006-04-08 01:18:18 +0000430Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
431 const Constant *V2,
432 const Constant *Mask) {
433 // TODO:
434 return 0;
435}
436
Dan Gohman07a96762007-07-16 14:29:03 +0000437/// EvalVectorOp - Given two vector constants and a function pointer, apply the
Reid Spencer9d6565a2007-02-15 02:26:10 +0000438/// function pointer to each element pair, producing a new ConstantVector
Reid Spencere4d87aa2006-12-23 06:05:41 +0000439/// constant.
Reid Spencer9d6565a2007-02-15 02:26:10 +0000440static Constant *EvalVectorOp(const ConstantVector *V1,
441 const ConstantVector *V2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000442 Constant *(*FP)(Constant*, Constant*)) {
443 std::vector<Constant*> Res;
444 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
445 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
446 const_cast<Constant*>(V2->getOperand(i))));
Reid Spencer9d6565a2007-02-15 02:26:10 +0000447 return ConstantVector::get(Res);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000448}
449
450Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
451 const Constant *C1,
452 const Constant *C2) {
453 // Handle UndefValue up front
454 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
455 switch (Opcode) {
456 case Instruction::Add:
457 case Instruction::Sub:
458 case Instruction::Xor:
459 return UndefValue::get(C1->getType());
460 case Instruction::Mul:
461 case Instruction::And:
462 return Constant::getNullValue(C1->getType());
463 case Instruction::UDiv:
464 case Instruction::SDiv:
465 case Instruction::FDiv:
466 case Instruction::URem:
467 case Instruction::SRem:
468 case Instruction::FRem:
469 if (!isa<UndefValue>(C2)) // undef / X -> 0
470 return Constant::getNullValue(C1->getType());
471 return const_cast<Constant*>(C2); // X / undef -> undef
472 case Instruction::Or: // X | undef -> -1
Reid Spencer9d6565a2007-02-15 02:26:10 +0000473 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
474 return ConstantVector::getAllOnesValue(PTy);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000475 return ConstantInt::getAllOnesValue(C1->getType());
476 case Instruction::LShr:
477 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
478 return const_cast<Constant*>(C1); // undef lshr undef -> undef
479 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
480 // undef lshr X -> 0
481 case Instruction::AShr:
482 if (!isa<UndefValue>(C2))
483 return const_cast<Constant*>(C1); // undef ashr X --> undef
484 else if (isa<UndefValue>(C1))
485 return const_cast<Constant*>(C1); // undef ashr undef -> undef
486 else
487 return const_cast<Constant*>(C1); // X ashr undef --> X
488 case Instruction::Shl:
489 // undef << X -> 0 or X << undef -> 0
490 return Constant::getNullValue(C1->getType());
491 }
492 }
493
494 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
495 if (isa<ConstantExpr>(C2)) {
496 // There are many possible foldings we could do here. We should probably
497 // at least fold add of a pointer with an integer into the appropriate
498 // getelementptr. This will improve alias analysis a bit.
499 } else {
500 // Just implement a couple of simple identities.
501 switch (Opcode) {
502 case Instruction::Add:
503 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X + 0 == X
504 break;
505 case Instruction::Sub:
506 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X - 0 == X
507 break;
508 case Instruction::Mul:
509 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X * 0 == 0
510 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer9472c372007-02-27 06:23:51 +0000511 if (CI->equalsInt(1))
Reid Spencere4d87aa2006-12-23 06:05:41 +0000512 return const_cast<Constant*>(C1); // X * 1 == X
513 break;
514 case Instruction::UDiv:
515 case Instruction::SDiv:
516 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer9472c372007-02-27 06:23:51 +0000517 if (CI->equalsInt(1))
Reid Spencere4d87aa2006-12-23 06:05:41 +0000518 return const_cast<Constant*>(C1); // X / 1 == X
519 break;
520 case Instruction::URem:
521 case Instruction::SRem:
522 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer9472c372007-02-27 06:23:51 +0000523 if (CI->equalsInt(1))
Reid Spencere4d87aa2006-12-23 06:05:41 +0000524 return Constant::getNullValue(CI->getType()); // X % 1 == 0
525 break;
526 case Instruction::And:
Chris Lattner2ef14d92007-03-25 05:47:04 +0000527 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2)) {
528 if (CI->isZero()) return const_cast<Constant*>(C2); // X & 0 == 0
Chris Lattnere94d8b22007-01-04 01:56:39 +0000529 if (CI->isAllOnesValue())
530 return const_cast<Constant*>(C1); // X & -1 == X
Chris Lattner2ef14d92007-03-25 05:47:04 +0000531
532 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
533 if (CE1->getOpcode() == Instruction::ZExt) {
534 APInt PossiblySetBits
535 = cast<IntegerType>(CE1->getOperand(0)->getType())->getMask();
536 PossiblySetBits.zext(C1->getType()->getPrimitiveSizeInBits());
537 if ((PossiblySetBits & CI->getValue()) == PossiblySetBits)
538 return const_cast<Constant*>(C1);
539 }
540 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000541 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
542 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
543
544 // Functions are at least 4-byte aligned. If and'ing the address of a
545 // function with a constant < 4, fold it to zero.
546 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer9472c372007-02-27 06:23:51 +0000547 if (CI->getValue().ult(APInt(CI->getType()->getBitWidth(),4)) &&
548 isa<Function>(CPR))
Reid Spencere4d87aa2006-12-23 06:05:41 +0000549 return Constant::getNullValue(CI->getType());
550 }
551 break;
552 case Instruction::Or:
553 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X | 0 == X
Chris Lattnere94d8b22007-01-04 01:56:39 +0000554 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
555 if (CI->isAllOnesValue())
556 return const_cast<Constant*>(C2); // X | -1 == -1
Reid Spencere4d87aa2006-12-23 06:05:41 +0000557 break;
558 case Instruction::Xor:
559 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X ^ 0 == X
560 break;
Chris Lattner2ef14d92007-03-25 05:47:04 +0000561 case Instruction::AShr:
Reid Spencer4ada00d2007-03-26 20:09:02 +0000562 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
Chris Lattner2ef14d92007-03-25 05:47:04 +0000563 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
564 return ConstantExpr::getLShr(const_cast<Constant*>(C1),
565 const_cast<Constant*>(C2));
566 break;
Reid Spencere4d87aa2006-12-23 06:05:41 +0000567 }
568 }
569 } else if (isa<ConstantExpr>(C2)) {
570 // If C2 is a constant expr and C1 isn't, flop them around and fold the
571 // other way if possible.
572 switch (Opcode) {
573 case Instruction::Add:
574 case Instruction::Mul:
575 case Instruction::And:
576 case Instruction::Or:
577 case Instruction::Xor:
578 // No change of opcode required.
579 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
580
581 case Instruction::Shl:
582 case Instruction::LShr:
583 case Instruction::AShr:
584 case Instruction::Sub:
585 case Instruction::SDiv:
586 case Instruction::UDiv:
587 case Instruction::FDiv:
588 case Instruction::URem:
589 case Instruction::SRem:
590 case Instruction::FRem:
591 default: // These instructions cannot be flopped around.
592 return 0;
593 }
594 }
595
596 // At this point we know neither constant is an UndefValue nor a ConstantExpr
Chris Lattnere94d8b22007-01-04 01:56:39 +0000597 // so look at directly computing the value.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000598 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
599 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer9472c372007-02-27 06:23:51 +0000600 using namespace APIntOps;
601 APInt C1V = CI1->getValue();
602 APInt C2V = CI2->getValue();
Chris Lattnerd333d902007-01-12 18:42:52 +0000603 switch (Opcode) {
604 default:
605 break;
606 case Instruction::Add:
Reid Spenceref2185b2007-03-01 19:31:12 +0000607 return ConstantInt::get(C1V + C2V);
Chris Lattnerd333d902007-01-12 18:42:52 +0000608 case Instruction::Sub:
Reid Spenceref2185b2007-03-01 19:31:12 +0000609 return ConstantInt::get(C1V - C2V);
Chris Lattnerd333d902007-01-12 18:42:52 +0000610 case Instruction::Mul:
Reid Spenceref2185b2007-03-01 19:31:12 +0000611 return ConstantInt::get(C1V * C2V);
Chris Lattnerd333d902007-01-12 18:42:52 +0000612 case Instruction::UDiv:
Reid Spencer9472c372007-02-27 06:23:51 +0000613 if (CI2->isNullValue())
614 return 0; // X / 0 -> can't fold
Reid Spenceref2185b2007-03-01 19:31:12 +0000615 return ConstantInt::get(C1V.udiv(C2V));
Chris Lattnerd333d902007-01-12 18:42:52 +0000616 case Instruction::SDiv:
Reid Spencer9472c372007-02-27 06:23:51 +0000617 if (CI2->isNullValue())
618 return 0; // X / 0 -> can't fold
Reid Spencer9472c372007-02-27 06:23:51 +0000619 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
620 return 0; // MIN_INT / -1 -> overflow
Reid Spenceref2185b2007-03-01 19:31:12 +0000621 return ConstantInt::get(C1V.sdiv(C2V));
Reid Spencer9472c372007-02-27 06:23:51 +0000622 case Instruction::URem:
623 if (C2->isNullValue())
624 return 0; // X / 0 -> can't fold
Reid Spenceref2185b2007-03-01 19:31:12 +0000625 return ConstantInt::get(C1V.urem(C2V));
Chris Lattnerd333d902007-01-12 18:42:52 +0000626 case Instruction::SRem:
Reid Spencer9472c372007-02-27 06:23:51 +0000627 if (CI2->isNullValue())
628 return 0; // X % 0 -> can't fold
629 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
630 return 0; // MIN_INT % -1 -> overflow
Reid Spenceref2185b2007-03-01 19:31:12 +0000631 return ConstantInt::get(C1V.srem(C2V));
Chris Lattnerd333d902007-01-12 18:42:52 +0000632 case Instruction::And:
Reid Spenceref2185b2007-03-01 19:31:12 +0000633 return ConstantInt::get(C1V & C2V);
Chris Lattnerd333d902007-01-12 18:42:52 +0000634 case Instruction::Or:
Reid Spenceref2185b2007-03-01 19:31:12 +0000635 return ConstantInt::get(C1V | C2V);
Chris Lattnerd333d902007-01-12 18:42:52 +0000636 case Instruction::Xor:
Reid Spenceref2185b2007-03-01 19:31:12 +0000637 return ConstantInt::get(C1V ^ C2V);
Chris Lattnerd333d902007-01-12 18:42:52 +0000638 case Instruction::Shl:
Reid Spencer9472c372007-02-27 06:23:51 +0000639 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencera5c74722007-02-27 19:29:54 +0000640 if (shiftAmt < C1V.getBitWidth())
Reid Spenceref2185b2007-03-01 19:31:12 +0000641 return ConstantInt::get(C1V.shl(shiftAmt));
Reid Spencer9472c372007-02-27 06:23:51 +0000642 else
643 return UndefValue::get(C1->getType()); // too big shift is undef
644 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Chris Lattnerd333d902007-01-12 18:42:52 +0000645 case Instruction::LShr:
Reid Spencer9472c372007-02-27 06:23:51 +0000646 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencera5c74722007-02-27 19:29:54 +0000647 if (shiftAmt < C1V.getBitWidth())
Reid Spenceref2185b2007-03-01 19:31:12 +0000648 return ConstantInt::get(C1V.lshr(shiftAmt));
Reid Spencer9472c372007-02-27 06:23:51 +0000649 else
650 return UndefValue::get(C1->getType()); // too big shift is undef
651 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Chris Lattnerd333d902007-01-12 18:42:52 +0000652 case Instruction::AShr:
Reid Spencer9472c372007-02-27 06:23:51 +0000653 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencera5c74722007-02-27 19:29:54 +0000654 if (shiftAmt < C1V.getBitWidth())
Reid Spenceref2185b2007-03-01 19:31:12 +0000655 return ConstantInt::get(C1V.ashr(shiftAmt));
Reid Spencer9472c372007-02-27 06:23:51 +0000656 else
657 return UndefValue::get(C1->getType()); // too big shift is undef
658 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Reid Spencere4d87aa2006-12-23 06:05:41 +0000659 }
660 }
661 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
662 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
663 double C1Val = CFP1->getValue();
664 double C2Val = CFP2->getValue();
665 switch (Opcode) {
666 default:
667 break;
668 case Instruction::Add:
669 return ConstantFP::get(CFP1->getType(), C1Val + C2Val);
670 case Instruction::Sub:
671 return ConstantFP::get(CFP1->getType(), C1Val - C2Val);
672 case Instruction::Mul:
673 return ConstantFP::get(CFP1->getType(), C1Val * C2Val);
674 case Instruction::FDiv:
Reid Spencerb8f1c162007-03-23 05:33:23 +0000675 if (CFP2->isExactlyValue(0.0) || CFP2->isExactlyValue(-0.0))
676 if (CFP1->isExactlyValue(0.0) || CFP1->isExactlyValue(-0.0))
677 // IEEE 754, Section 7.1, #4
678 return ConstantFP::get(CFP1->getType(),
679 std::numeric_limits<double>::quiet_NaN());
680 else if (CFP2->isExactlyValue(-0.0) || C1Val < 0.0)
681 // IEEE 754, Section 7.2, negative infinity case
682 return ConstantFP::get(CFP1->getType(),
683 -std::numeric_limits<double>::infinity());
684 else
685 // IEEE 754, Section 7.2, positive infinity case
686 return ConstantFP::get(CFP1->getType(),
687 std::numeric_limits<double>::infinity());
Reid Spencere4d87aa2006-12-23 06:05:41 +0000688 return ConstantFP::get(CFP1->getType(), C1Val / C2Val);
689 case Instruction::FRem:
Reid Spencerb8f1c162007-03-23 05:33:23 +0000690 if (CFP2->isExactlyValue(0.0) || CFP2->isExactlyValue(-0.0))
691 // IEEE 754, Section 7.1, #5
692 return ConstantFP::get(CFP1->getType(),
693 std::numeric_limits<double>::quiet_NaN());
Reid Spencere4d87aa2006-12-23 06:05:41 +0000694 return ConstantFP::get(CFP1->getType(), std::fmod(C1Val, C2Val));
Reid Spencerb8f1c162007-03-23 05:33:23 +0000695
Reid Spencere4d87aa2006-12-23 06:05:41 +0000696 }
697 }
Reid Spencer9d6565a2007-02-15 02:26:10 +0000698 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
699 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000700 switch (Opcode) {
701 default:
702 break;
703 case Instruction::Add:
704 return EvalVectorOp(CP1, CP2, ConstantExpr::getAdd);
705 case Instruction::Sub:
706 return EvalVectorOp(CP1, CP2, ConstantExpr::getSub);
707 case Instruction::Mul:
708 return EvalVectorOp(CP1, CP2, ConstantExpr::getMul);
709 case Instruction::UDiv:
710 return EvalVectorOp(CP1, CP2, ConstantExpr::getUDiv);
711 case Instruction::SDiv:
712 return EvalVectorOp(CP1, CP2, ConstantExpr::getSDiv);
713 case Instruction::FDiv:
714 return EvalVectorOp(CP1, CP2, ConstantExpr::getFDiv);
715 case Instruction::URem:
716 return EvalVectorOp(CP1, CP2, ConstantExpr::getURem);
717 case Instruction::SRem:
718 return EvalVectorOp(CP1, CP2, ConstantExpr::getSRem);
719 case Instruction::FRem:
720 return EvalVectorOp(CP1, CP2, ConstantExpr::getFRem);
721 case Instruction::And:
722 return EvalVectorOp(CP1, CP2, ConstantExpr::getAnd);
723 case Instruction::Or:
724 return EvalVectorOp(CP1, CP2, ConstantExpr::getOr);
725 case Instruction::Xor:
726 return EvalVectorOp(CP1, CP2, ConstantExpr::getXor);
727 }
728 }
729 }
730
731 // We don't know how to fold this
732 return 0;
733}
Chris Lattner00f10232006-04-08 01:18:18 +0000734
Chris Lattnerce04a6d2005-01-28 19:09:51 +0000735/// isZeroSizedType - This type is zero sized if its an array or structure of
736/// zero sized types. The only leaf zero sized type is an empty structure.
737static bool isMaybeZeroSizedType(const Type *Ty) {
738 if (isa<OpaqueType>(Ty)) return true; // Can't say.
739 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
740
741 // If all of elements have zero size, this does too.
742 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerf4aa3352005-01-28 23:17:27 +0000743 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattnerce04a6d2005-01-28 19:09:51 +0000744 return true;
745
746 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
747 return isMaybeZeroSizedType(ATy->getElementType());
748 }
749 return false;
750}
Chris Lattnere9714862004-03-12 05:53:32 +0000751
Chris Lattner504e8fb2004-01-13 05:51:55 +0000752/// IdxCompare - Compare the two constants as though they were getelementptr
753/// indices. This allows coersion of the types to be the same thing.
754///
755/// If the two constants are the "same" (after coersion), return 0. If the
756/// first is less than the second, return -1, if the second is less than the
757/// first, return 1. If the constants are not integral, return -2.
758///
Chris Lattnerce04a6d2005-01-28 19:09:51 +0000759static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner504e8fb2004-01-13 05:51:55 +0000760 if (C1 == C2) return 0;
761
Reid Spencercc615c32006-12-31 21:43:30 +0000762 // Ok, we found a different index. If they are not ConstantInt, we can't do
763 // anything with them.
Chris Lattner504e8fb2004-01-13 05:51:55 +0000764 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
765 return -2; // don't know!
Misha Brukmanfd939082005-04-21 23:48:37 +0000766
Chris Lattner28977af2004-04-05 01:30:19 +0000767 // Ok, we have two differing integer indices. Sign extend them to be the same
768 // type. Long is always big enough, so we use it.
Reid Spencer79e21d32006-12-31 05:26:44 +0000769 if (C1->getType() != Type::Int64Ty)
770 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
Reid Spencer3da59db2006-11-27 01:05:10 +0000771
Reid Spencer79e21d32006-12-31 05:26:44 +0000772 if (C2->getType() != Type::Int64Ty)
Reid Spencercc615c32006-12-31 21:43:30 +0000773 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Reid Spencer79e21d32006-12-31 05:26:44 +0000774
775 if (C1 == C2) return 0; // They are equal
Chris Lattner504e8fb2004-01-13 05:51:55 +0000776
Chris Lattnerce04a6d2005-01-28 19:09:51 +0000777 // If the type being indexed over is really just a zero sized type, there is
778 // no pointer difference being made here.
779 if (isMaybeZeroSizedType(ElTy))
780 return -2; // dunno.
781
Chris Lattner504e8fb2004-01-13 05:51:55 +0000782 // If they are really different, now that they are the same type, then we
783 // found a difference!
Reid Spencerb83eb642006-10-20 07:07:24 +0000784 if (cast<ConstantInt>(C1)->getSExtValue() <
785 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner504e8fb2004-01-13 05:51:55 +0000786 return -1;
787 else
788 return 1;
789}
790
Chris Lattner898b2d52007-01-04 02:13:20 +0000791/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencere4d87aa2006-12-23 06:05:41 +0000792/// decide about the two constants provided. This doesn't need to handle simple
793/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
794/// If we can determine that the two constants have a particular relation to
795/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencerb913bba2006-12-24 18:52:08 +0000796/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
797/// ConstantFoldCompareInstruction.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000798///
799/// To simplify this code we canonicalize the relation so that the first
Reid Spencerb913bba2006-12-24 18:52:08 +0000800/// operand is always the most "complex" of the two. We consider ConstantFP
801/// to be the simplest, and ConstantExprs to be the most complex.
802static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1,
803 const Constant *V2) {
Reid Spencere4d87aa2006-12-23 06:05:41 +0000804 assert(V1->getType() == V2->getType() &&
Reid Spencerb913bba2006-12-24 18:52:08 +0000805 "Cannot compare values of different types!");
806 // Handle degenerate case quickly
Reid Spencere4d87aa2006-12-23 06:05:41 +0000807 if (V1 == V2) return FCmpInst::FCMP_OEQ;
808
Reid Spencerb913bba2006-12-24 18:52:08 +0000809 if (!isa<ConstantExpr>(V1)) {
810 if (!isa<ConstantExpr>(V2)) {
811 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000812 ConstantInt *R = 0;
Reid Spencerb913bba2006-12-24 18:52:08 +0000813 Constant *C1 = const_cast<Constant*>(V1);
814 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000815 R = dyn_cast<ConstantInt>(
Reid Spencerb913bba2006-12-24 18:52:08 +0000816 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Reid Spencercae57542007-03-02 00:28:52 +0000817 if (R && !R->isZero())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000818 return FCmpInst::FCMP_OEQ;
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000819 R = dyn_cast<ConstantInt>(
Reid Spencerb913bba2006-12-24 18:52:08 +0000820 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2));
Reid Spencercae57542007-03-02 00:28:52 +0000821 if (R && !R->isZero())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000822 return FCmpInst::FCMP_OLT;
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000823 R = dyn_cast<ConstantInt>(
Reid Spencerb913bba2006-12-24 18:52:08 +0000824 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
Reid Spencercae57542007-03-02 00:28:52 +0000825 if (R && !R->isZero())
Reid Spencerb913bba2006-12-24 18:52:08 +0000826 return FCmpInst::FCMP_OGT;
827
828 // Nothing more we can do
Reid Spencere4d87aa2006-12-23 06:05:41 +0000829 return FCmpInst::BAD_FCMP_PREDICATE;
830 }
831
Reid Spencerb913bba2006-12-24 18:52:08 +0000832 // If the first operand is simple and second is ConstantExpr, swap operands.
833 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
834 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
835 return FCmpInst::getSwappedPredicate(SwappedRelation);
836 } else {
837 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
838 // constantexpr or a simple constant.
839 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
840 switch (CE1->getOpcode()) {
841 case Instruction::FPTrunc:
842 case Instruction::FPExt:
843 case Instruction::UIToFP:
844 case Instruction::SIToFP:
845 // We might be able to do something with these but we don't right now.
846 break;
847 default:
848 break;
849 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000850 }
Reid Spencere4d87aa2006-12-23 06:05:41 +0000851 // There are MANY other foldings that we could perform here. They will
852 // probably be added on demand, as they seem needed.
853 return FCmpInst::BAD_FCMP_PREDICATE;
854}
855
856/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner504e8fb2004-01-13 05:51:55 +0000857/// decide about the two constants provided. This doesn't need to handle simple
Reid Spencer79703962004-07-17 23:47:01 +0000858/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner36c24512006-12-11 02:16:58 +0000859/// and GlobalValues. If we can determine that the two constants have a
Reid Spencere4d87aa2006-12-23 06:05:41 +0000860/// particular relation to each other, we should return the corresponding ICmp
861/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner504e8fb2004-01-13 05:51:55 +0000862///
863/// To simplify this code we canonicalize the relation so that the first
864/// operand is always the most "complex" of the two. We consider simple
865/// constants (like ConstantInt) to be the simplest, followed by
Reid Spencer79703962004-07-17 23:47:01 +0000866/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner504e8fb2004-01-13 05:51:55 +0000867///
Reid Spencerb913bba2006-12-24 18:52:08 +0000868static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
869 const Constant *V2,
Reid Spencere4d87aa2006-12-23 06:05:41 +0000870 bool isSigned) {
Chris Lattner504e8fb2004-01-13 05:51:55 +0000871 assert(V1->getType() == V2->getType() &&
872 "Cannot compare different types of values!");
Reid Spencere4d87aa2006-12-23 06:05:41 +0000873 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner504e8fb2004-01-13 05:51:55 +0000874
Reid Spencer79703962004-07-17 23:47:01 +0000875 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattner2f690c82006-01-05 07:49:30 +0000876 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
877 // We distilled this down to a simple case, use the standard constant
878 // folder.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000879 ConstantInt *R = 0;
Reid Spencerb913bba2006-12-24 18:52:08 +0000880 Constant *C1 = const_cast<Constant*>(V1);
881 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000882 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000883 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencercae57542007-03-02 00:28:52 +0000884 if (R && !R->isZero())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000885 return pred;
886 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000887 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencercae57542007-03-02 00:28:52 +0000888 if (R && !R->isZero())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000889 return pred;
890 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000891 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencercae57542007-03-02 00:28:52 +0000892 if (R && !R->isZero())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000893 return pred;
Chris Lattner2f690c82006-01-05 07:49:30 +0000894
895 // If we couldn't figure it out, bail.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000896 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner2f690c82006-01-05 07:49:30 +0000897 }
898
Chris Lattner504e8fb2004-01-13 05:51:55 +0000899 // If the first operand is simple, swap operands.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000900 ICmpInst::Predicate SwappedRelation =
901 evaluateICmpRelation(V2, V1, isSigned);
902 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
903 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner504e8fb2004-01-13 05:51:55 +0000904
Chris Lattner2c822cc2006-01-05 07:19:51 +0000905 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattnerb97e2782004-02-01 01:23:19 +0000906 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000907 ICmpInst::Predicate SwappedRelation =
908 evaluateICmpRelation(V2, V1, isSigned);
909 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
910 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner2c822cc2006-01-05 07:19:51 +0000911 else
Reid Spencere4d87aa2006-12-23 06:05:41 +0000912 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerb97e2782004-02-01 01:23:19 +0000913 }
Chris Lattner504e8fb2004-01-13 05:51:55 +0000914
Reid Spencer79703962004-07-17 23:47:01 +0000915 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner504e8fb2004-01-13 05:51:55 +0000916 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spencer79703962004-07-17 23:47:01 +0000917 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer87d5f6c2006-12-06 00:25:09 +0000918 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000919 return ICmpInst::ICMP_NE;
Chris Lattner504e8fb2004-01-13 05:51:55 +0000920 } else {
Reid Spencer87d5f6c2006-12-06 00:25:09 +0000921 // GlobalVals can never be null.
Chris Lattner504e8fb2004-01-13 05:51:55 +0000922 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer87d5f6c2006-12-06 00:25:09 +0000923 if (!CPR1->hasExternalWeakLinkage())
Reid Spencere4d87aa2006-12-23 06:05:41 +0000924 return ICmpInst::ICMP_NE;
Chris Lattner504e8fb2004-01-13 05:51:55 +0000925 }
Chris Lattner504e8fb2004-01-13 05:51:55 +0000926 } else {
927 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
928 // constantexpr, a CPR, or a simple constant.
Reid Spencerb913bba2006-12-24 18:52:08 +0000929 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
930 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner504e8fb2004-01-13 05:51:55 +0000931
932 switch (CE1->getOpcode()) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000933 case Instruction::Trunc:
934 case Instruction::FPTrunc:
935 case Instruction::FPExt:
936 case Instruction::FPToUI:
937 case Instruction::FPToSI:
Reid Spencere4d87aa2006-12-23 06:05:41 +0000938 break; // We can't evaluate floating point casts or truncations.
939
Reid Spencer3da59db2006-11-27 01:05:10 +0000940 case Instruction::UIToFP:
941 case Instruction::SIToFP:
Reid Spencer3da59db2006-11-27 01:05:10 +0000942 case Instruction::IntToPtr:
943 case Instruction::BitCast:
Reid Spencere4d87aa2006-12-23 06:05:41 +0000944 case Instruction::ZExt:
945 case Instruction::SExt:
946 case Instruction::PtrToInt:
Chris Lattner504e8fb2004-01-13 05:51:55 +0000947 // If the cast is not actually changing bits, and the second operand is a
948 // null pointer, do the comparison with the pre-casted value.
949 if (V2->isNullValue() &&
Chris Lattner42a75512007-01-15 02:27:26 +0000950 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Reid Spencer283e2072006-12-23 10:21:26 +0000951 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencere4d87aa2006-12-23 06:05:41 +0000952 (CE1->getOpcode() == Instruction::SExt ? true :
953 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
954 return evaluateICmpRelation(
Reid Spencer283e2072006-12-23 10:21:26 +0000955 CE1Op0, Constant::getNullValue(CE1Op0->getType()), sgnd);
Reid Spencere4d87aa2006-12-23 06:05:41 +0000956 }
Chris Lattner2f690c82006-01-05 07:49:30 +0000957
958 // If the dest type is a pointer type, and the RHS is a constantexpr cast
959 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencere4d87aa2006-12-23 06:05:41 +0000960 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattner2f690c82006-01-05 07:49:30 +0000961 // which happens a lot in compilers with tagged integers.
Reid Spencerb913bba2006-12-24 18:52:08 +0000962 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencere4d87aa2006-12-23 06:05:41 +0000963 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattner2f690c82006-01-05 07:49:30 +0000964 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
Chris Lattner42a75512007-01-15 02:27:26 +0000965 CE1->getOperand(0)->getType()->isInteger()) {
Reid Spencer283e2072006-12-23 10:21:26 +0000966 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencere4d87aa2006-12-23 06:05:41 +0000967 (CE1->getOpcode() == Instruction::SExt ? true :
968 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
969 return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0),
Reid Spencer283e2072006-12-23 10:21:26 +0000970 sgnd);
Chris Lattner2f690c82006-01-05 07:49:30 +0000971 }
Chris Lattnera0ae8192004-04-11 01:29:30 +0000972 break;
Chris Lattner504e8fb2004-01-13 05:51:55 +0000973
974 case Instruction::GetElementPtr:
975 // Ok, since this is a getelementptr, we know that the constant has a
976 // pointer type. Check the various cases.
977 if (isa<ConstantPointerNull>(V2)) {
978 // If we are comparing a GEP to a null pointer, check to see if the base
979 // of the GEP equals the null pointer.
Reid Spencerb913bba2006-12-24 18:52:08 +0000980 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer87d5f6c2006-12-06 00:25:09 +0000981 if (GV->hasExternalWeakLinkage())
982 // Weak linkage GVals could be zero or not. We're comparing that
983 // to null pointer so its greater-or-equal
Reid Spencere4d87aa2006-12-23 06:05:41 +0000984 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer87d5f6c2006-12-06 00:25:09 +0000985 else
986 // If its not weak linkage, the GVal must have a non-zero address
987 // so the result is greater-than
Reid Spencere4d87aa2006-12-23 06:05:41 +0000988 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner504e8fb2004-01-13 05:51:55 +0000989 } else if (isa<ConstantPointerNull>(CE1Op0)) {
990 // If we are indexing from a null pointer, check to see if we have any
991 // non-zero indices.
992 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
993 if (!CE1->getOperand(i)->isNullValue())
994 // Offsetting from null, must not be equal.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000995 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner504e8fb2004-01-13 05:51:55 +0000996 // Only zero indexes from null, must still be zero.
Reid Spencere4d87aa2006-12-23 06:05:41 +0000997 return ICmpInst::ICMP_EQ;
Chris Lattner504e8fb2004-01-13 05:51:55 +0000998 }
999 // Otherwise, we can't really say if the first operand is null or not.
Reid Spencer79703962004-07-17 23:47:01 +00001000 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001001 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer87d5f6c2006-12-06 00:25:09 +00001002 if (CPR2->hasExternalWeakLinkage())
1003 // Weak linkage GVals could be zero or not. We're comparing it to
1004 // a null pointer, so its less-or-equal
Reid Spencere4d87aa2006-12-23 06:05:41 +00001005 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer87d5f6c2006-12-06 00:25:09 +00001006 else
1007 // If its not weak linkage, the GVal must have a non-zero address
1008 // so the result is less-than
Reid Spencere4d87aa2006-12-23 06:05:41 +00001009 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spencer79703962004-07-17 23:47:01 +00001010 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001011 if (CPR1 == CPR2) {
1012 // If this is a getelementptr of the same global, then it must be
1013 // different. Because the types must match, the getelementptr could
1014 // only have at most one index, and because we fold getelementptr's
1015 // with a single zero index, it must be nonzero.
1016 assert(CE1->getNumOperands() == 2 &&
1017 !CE1->getOperand(1)->isNullValue() &&
1018 "Suprising getelementptr!");
Reid Spencere4d87aa2006-12-23 06:05:41 +00001019 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001020 } else {
1021 // If they are different globals, we don't know what the value is,
1022 // but they can't be equal.
Reid Spencere4d87aa2006-12-23 06:05:41 +00001023 return ICmpInst::ICMP_NE;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001024 }
1025 }
1026 } else {
1027 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1028 const Constant *CE2Op0 = CE2->getOperand(0);
1029
1030 // There are MANY other foldings that we could perform here. They will
1031 // probably be added on demand, as they seem needed.
1032 switch (CE2->getOpcode()) {
1033 default: break;
1034 case Instruction::GetElementPtr:
1035 // By far the most common case to handle is when the base pointers are
1036 // obviously to the same or different globals.
Reid Spencer79703962004-07-17 23:47:01 +00001037 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner504e8fb2004-01-13 05:51:55 +00001038 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencere4d87aa2006-12-23 06:05:41 +00001039 return ICmpInst::ICMP_NE;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001040 // Ok, we know that both getelementptr instructions are based on the
1041 // same global. From this, we can precisely determine the relative
1042 // ordering of the resultant pointers.
1043 unsigned i = 1;
Misha Brukmanfd939082005-04-21 23:48:37 +00001044
Chris Lattner504e8fb2004-01-13 05:51:55 +00001045 // Compare all of the operands the GEP's have in common.
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001046 gep_type_iterator GTI = gep_type_begin(CE1);
1047 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1048 ++i, ++GTI)
1049 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1050 GTI.getIndexedType())) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001051 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1052 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1053 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001054 }
1055
1056 // Ok, we ran out of things they have in common. If any leftovers
1057 // are non-zero then we have a difference, otherwise we are equal.
1058 for (; i < CE1->getNumOperands(); ++i)
1059 if (!CE1->getOperand(i)->isNullValue())
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001060 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencere4d87aa2006-12-23 06:05:41 +00001061 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001062 else
Reid Spencere4d87aa2006-12-23 06:05:41 +00001063 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Misha Brukmanfd939082005-04-21 23:48:37 +00001064
Chris Lattner504e8fb2004-01-13 05:51:55 +00001065 for (; i < CE2->getNumOperands(); ++i)
1066 if (!CE2->getOperand(i)->isNullValue())
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001067 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencere4d87aa2006-12-23 06:05:41 +00001068 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattnerce04a6d2005-01-28 19:09:51 +00001069 else
Reid Spencere4d87aa2006-12-23 06:05:41 +00001070 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1071 return ICmpInst::ICMP_EQ;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001072 }
1073 }
1074 }
Chris Lattner504e8fb2004-01-13 05:51:55 +00001075 default:
1076 break;
1077 }
1078 }
1079
Reid Spencere4d87aa2006-12-23 06:05:41 +00001080 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner504e8fb2004-01-13 05:51:55 +00001081}
1082
Reid Spencerb913bba2006-12-24 18:52:08 +00001083Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1084 const Constant *C1,
1085 const Constant *C2) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001086
1087 // Handle some degenerate cases first
1088 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Reid Spencer4fe16d62007-01-11 18:21:29 +00001089 return UndefValue::get(Type::Int1Ty);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001090
1091 // icmp eq/ne(null,GV) -> false/true
1092 if (C1->isNullValue()) {
1093 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1094 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencerb913bba2006-12-24 18:52:08 +00001095 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001096 return ConstantInt::getFalse();
Reid Spencerb913bba2006-12-24 18:52:08 +00001097 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001098 return ConstantInt::getTrue();
Reid Spencere4d87aa2006-12-23 06:05:41 +00001099 // icmp eq/ne(GV,null) -> false/true
1100 } else if (C2->isNullValue()) {
1101 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1102 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencerb913bba2006-12-24 18:52:08 +00001103 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001104 return ConstantInt::getFalse();
Reid Spencerb913bba2006-12-24 18:52:08 +00001105 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001106 return ConstantInt::getTrue();
Chris Lattnereab20b52004-01-12 22:07:24 +00001107 }
1108
Chris Lattnerd333d902007-01-12 18:42:52 +00001109 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer9472c372007-02-27 06:23:51 +00001110 APInt V1 = cast<ConstantInt>(C1)->getValue();
1111 APInt V2 = cast<ConstantInt>(C2)->getValue();
1112 switch (pred) {
1113 default: assert(0 && "Invalid ICmp Predicate"); return 0;
1114 case ICmpInst::ICMP_EQ: return ConstantInt::get(Type::Int1Ty, V1 == V2);
1115 case ICmpInst::ICMP_NE: return ConstantInt::get(Type::Int1Ty, V1 != V2);
1116 case ICmpInst::ICMP_SLT:return ConstantInt::get(Type::Int1Ty, V1.slt(V2));
1117 case ICmpInst::ICMP_SGT:return ConstantInt::get(Type::Int1Ty, V1.sgt(V2));
1118 case ICmpInst::ICMP_SLE:return ConstantInt::get(Type::Int1Ty, V1.sle(V2));
1119 case ICmpInst::ICMP_SGE:return ConstantInt::get(Type::Int1Ty, V1.sge(V2));
1120 case ICmpInst::ICMP_ULT:return ConstantInt::get(Type::Int1Ty, V1.ult(V2));
1121 case ICmpInst::ICMP_UGT:return ConstantInt::get(Type::Int1Ty, V1.ugt(V2));
1122 case ICmpInst::ICMP_ULE:return ConstantInt::get(Type::Int1Ty, V1.ule(V2));
1123 case ICmpInst::ICMP_UGE:return ConstantInt::get(Type::Int1Ty, V1.uge(V2));
Chris Lattner504e8fb2004-01-13 05:51:55 +00001124 }
Reid Spencere4d87aa2006-12-23 06:05:41 +00001125 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1126 double C1Val = cast<ConstantFP>(C1)->getValue();
1127 double C2Val = cast<ConstantFP>(C2)->getValue();
Reid Spencerb913bba2006-12-24 18:52:08 +00001128 switch (pred) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001129 default: assert(0 && "Invalid FCmp Predicate"); return 0;
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001130 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse();
1131 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue();
Reid Spencere4d87aa2006-12-23 06:05:41 +00001132 case FCmpInst::FCMP_UNO:
Reid Spencer579dca12007-01-12 04:24:46 +00001133 return ConstantInt::get(Type::Int1Ty, C1Val != C1Val || C2Val != C2Val);
Reid Spencer53054782007-01-11 00:25:45 +00001134 case FCmpInst::FCMP_ORD:
Reid Spencer579dca12007-01-12 04:24:46 +00001135 return ConstantInt::get(Type::Int1Ty, C1Val == C1Val && C2Val == C2Val);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001136 case FCmpInst::FCMP_UEQ:
Reid Spencer53054782007-01-11 00:25:45 +00001137 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001138 return ConstantInt::getTrue();
Reid Spencer53054782007-01-11 00:25:45 +00001139 /* FALL THROUGH */
Reid Spencer579dca12007-01-12 04:24:46 +00001140 case FCmpInst::FCMP_OEQ:
1141 return ConstantInt::get(Type::Int1Ty, C1Val == C2Val);
Reid Spencer53054782007-01-11 00:25:45 +00001142 case FCmpInst::FCMP_UNE:
1143 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001144 return ConstantInt::getTrue();
Reid Spencer53054782007-01-11 00:25:45 +00001145 /* FALL THROUGH */
Reid Spencer579dca12007-01-12 04:24:46 +00001146 case FCmpInst::FCMP_ONE:
1147 return ConstantInt::get(Type::Int1Ty, C1Val != C2Val);
Reid Spencer53054782007-01-11 00:25:45 +00001148 case FCmpInst::FCMP_ULT:
1149 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001150 return ConstantInt::getTrue();
Reid Spencer53054782007-01-11 00:25:45 +00001151 /* FALL THROUGH */
Reid Spencer579dca12007-01-12 04:24:46 +00001152 case FCmpInst::FCMP_OLT:
1153 return ConstantInt::get(Type::Int1Ty, C1Val < C2Val);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001154 case FCmpInst::FCMP_UGT:
Reid Spencer53054782007-01-11 00:25:45 +00001155 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001156 return ConstantInt::getTrue();
Reid Spencer53054782007-01-11 00:25:45 +00001157 /* FALL THROUGH */
Reid Spencer579dca12007-01-12 04:24:46 +00001158 case FCmpInst::FCMP_OGT:
1159 return ConstantInt::get(Type::Int1Ty, C1Val > C2Val);
Reid Spencer53054782007-01-11 00:25:45 +00001160 case FCmpInst::FCMP_ULE:
1161 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001162 return ConstantInt::getTrue();
Reid Spencer53054782007-01-11 00:25:45 +00001163 /* FALL THROUGH */
Reid Spencer579dca12007-01-12 04:24:46 +00001164 case FCmpInst::FCMP_OLE:
1165 return ConstantInt::get(Type::Int1Ty, C1Val <= C2Val);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001166 case FCmpInst::FCMP_UGE:
Reid Spencer53054782007-01-11 00:25:45 +00001167 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001168 return ConstantInt::getTrue();
Reid Spencer53054782007-01-11 00:25:45 +00001169 /* FALL THROUGH */
Reid Spencer579dca12007-01-12 04:24:46 +00001170 case FCmpInst::FCMP_OGE:
1171 return ConstantInt::get(Type::Int1Ty, C1Val >= C2Val);
Reid Spencere4d87aa2006-12-23 06:05:41 +00001172 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001173 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
1174 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencerb913bba2006-12-24 18:52:08 +00001175 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001176 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1177 Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
1178 const_cast<Constant*>(CP1->getOperand(i)),
1179 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001180 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencere4d87aa2006-12-23 06:05:41 +00001181 return CB;
1182 }
1183 // Otherwise, could not decide from any element pairs.
1184 return 0;
Reid Spencerb913bba2006-12-24 18:52:08 +00001185 } else if (pred == ICmpInst::ICMP_EQ) {
Reid Spencere4d87aa2006-12-23 06:05:41 +00001186 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1187 Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ,
1188 const_cast<Constant*>(CP1->getOperand(i)),
1189 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001190 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencere4d87aa2006-12-23 06:05:41 +00001191 return CB;
1192 }
1193 // Otherwise, could not decide from any element pairs.
1194 return 0;
1195 }
1196 }
1197 }
Chris Lattner504e8fb2004-01-13 05:51:55 +00001198
Reid Spencerb913bba2006-12-24 18:52:08 +00001199 if (C1->getType()->isFloatingPoint()) {
1200 switch (evaluateFCmpRelation(C1, C2)) {
1201 default: assert(0 && "Unknown relation!");
1202 case FCmpInst::FCMP_UNO:
1203 case FCmpInst::FCMP_ORD:
1204 case FCmpInst::FCMP_UEQ:
1205 case FCmpInst::FCMP_UNE:
1206 case FCmpInst::FCMP_ULT:
1207 case FCmpInst::FCMP_UGT:
1208 case FCmpInst::FCMP_ULE:
1209 case FCmpInst::FCMP_UGE:
1210 case FCmpInst::FCMP_TRUE:
1211 case FCmpInst::FCMP_FALSE:
1212 case FCmpInst::BAD_FCMP_PREDICATE:
1213 break; // Couldn't determine anything about these constants.
1214 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Reid Spencer579dca12007-01-12 04:24:46 +00001215 return ConstantInt::get(Type::Int1Ty,
Reid Spencerb913bba2006-12-24 18:52:08 +00001216 pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1217 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1218 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1219 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Reid Spencer579dca12007-01-12 04:24:46 +00001220 return ConstantInt::get(Type::Int1Ty,
Reid Spencerb913bba2006-12-24 18:52:08 +00001221 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1222 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1223 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1224 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Reid Spencer579dca12007-01-12 04:24:46 +00001225 return ConstantInt::get(Type::Int1Ty,
Reid Spencerb913bba2006-12-24 18:52:08 +00001226 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1227 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1228 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1229 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1230 // We can only partially decide this relation.
1231 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001232 return ConstantInt::getFalse();
Reid Spencerb913bba2006-12-24 18:52:08 +00001233 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001234 return ConstantInt::getTrue();
Chris Lattner504e8fb2004-01-13 05:51:55 +00001235 break;
Reid Spencerb913bba2006-12-24 18:52:08 +00001236 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1237 // We can only partially decide this relation.
1238 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001239 return ConstantInt::getFalse();
Reid Spencerb913bba2006-12-24 18:52:08 +00001240 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001241 return ConstantInt::getTrue();
Reid Spencerb913bba2006-12-24 18:52:08 +00001242 break;
1243 case ICmpInst::ICMP_NE: // We know that C1 != C2
1244 // We can only partially decide this relation.
1245 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001246 return ConstantInt::getFalse();
Reid Spencerb913bba2006-12-24 18:52:08 +00001247 if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001248 return ConstantInt::getTrue();
Reid Spencerb913bba2006-12-24 18:52:08 +00001249 break;
1250 }
1251 } else {
1252 // Evaluate the relation between the two constants, per the predicate.
1253 switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
1254 default: assert(0 && "Unknown relational!");
1255 case ICmpInst::BAD_ICMP_PREDICATE:
1256 break; // Couldn't determine anything about these constants.
1257 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1258 // If we know the constants are equal, we can decide the result of this
1259 // computation precisely.
Reid Spencer579dca12007-01-12 04:24:46 +00001260 return ConstantInt::get(Type::Int1Ty,
1261 pred == ICmpInst::ICMP_EQ ||
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001262 pred == ICmpInst::ICMP_ULE ||
1263 pred == ICmpInst::ICMP_SLE ||
1264 pred == ICmpInst::ICMP_UGE ||
1265 pred == ICmpInst::ICMP_SGE);
Reid Spencerb913bba2006-12-24 18:52:08 +00001266 case ICmpInst::ICMP_ULT:
1267 // If we know that C1 < C2, we can decide the result of this computation
1268 // precisely.
Reid Spencer579dca12007-01-12 04:24:46 +00001269 return ConstantInt::get(Type::Int1Ty,
1270 pred == ICmpInst::ICMP_ULT ||
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001271 pred == ICmpInst::ICMP_NE ||
1272 pred == ICmpInst::ICMP_ULE);
Reid Spencerb913bba2006-12-24 18:52:08 +00001273 case ICmpInst::ICMP_SLT:
1274 // If we know that C1 < C2, we can decide the result of this computation
1275 // precisely.
Reid Spencer579dca12007-01-12 04:24:46 +00001276 return ConstantInt::get(Type::Int1Ty,
1277 pred == ICmpInst::ICMP_SLT ||
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001278 pred == ICmpInst::ICMP_NE ||
1279 pred == ICmpInst::ICMP_SLE);
Reid Spencerb913bba2006-12-24 18:52:08 +00001280 case ICmpInst::ICMP_UGT:
1281 // If we know that C1 > C2, we can decide the result of this computation
1282 // precisely.
Reid Spencer579dca12007-01-12 04:24:46 +00001283 return ConstantInt::get(Type::Int1Ty,
1284 pred == ICmpInst::ICMP_UGT ||
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001285 pred == ICmpInst::ICMP_NE ||
1286 pred == ICmpInst::ICMP_UGE);
Reid Spencerb913bba2006-12-24 18:52:08 +00001287 case ICmpInst::ICMP_SGT:
1288 // If we know that C1 > C2, we can decide the result of this computation
1289 // precisely.
Reid Spencer579dca12007-01-12 04:24:46 +00001290 return ConstantInt::get(Type::Int1Ty,
1291 pred == ICmpInst::ICMP_SGT ||
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001292 pred == ICmpInst::ICMP_NE ||
1293 pred == ICmpInst::ICMP_SGE);
Reid Spencerb913bba2006-12-24 18:52:08 +00001294 case ICmpInst::ICMP_ULE:
1295 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001296 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getFalse();
1297 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getTrue();
Reid Spencerb913bba2006-12-24 18:52:08 +00001298 break;
1299 case ICmpInst::ICMP_SLE:
1300 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001301 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getFalse();
1302 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getTrue();
Reid Spencerb913bba2006-12-24 18:52:08 +00001303 break;
1304
1305 case ICmpInst::ICMP_UGE:
1306 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001307 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getFalse();
1308 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getTrue();
Reid Spencerb913bba2006-12-24 18:52:08 +00001309 break;
1310 case ICmpInst::ICMP_SGE:
1311 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001312 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getFalse();
1313 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getTrue();
Reid Spencerb913bba2006-12-24 18:52:08 +00001314 break;
1315
1316 case ICmpInst::ICMP_NE:
1317 // If we know that C1 != C2, we can only partially decide this relation.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001318 if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse();
1319 if (pred == ICmpInst::ICMP_NE) return ConstantInt::getTrue();
Reid Spencerb913bba2006-12-24 18:52:08 +00001320 break;
1321 }
1322
1323 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
1324 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1325 // other way if possible.
1326 switch (pred) {
1327 case ICmpInst::ICMP_EQ:
1328 case ICmpInst::ICMP_NE:
1329 // No change of predicate required.
1330 return ConstantFoldCompareInstruction(pred, C2, C1);
1331
1332 case ICmpInst::ICMP_ULT:
1333 case ICmpInst::ICMP_SLT:
1334 case ICmpInst::ICMP_UGT:
1335 case ICmpInst::ICMP_SGT:
1336 case ICmpInst::ICMP_ULE:
1337 case ICmpInst::ICMP_SLE:
1338 case ICmpInst::ICMP_UGE:
1339 case ICmpInst::ICMP_SGE:
1340 // Change the predicate as necessary to swap the operands.
1341 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1342 return ConstantFoldCompareInstruction(pred, C2, C1);
1343
1344 default: // These predicates cannot be flopped around.
1345 break;
1346 }
Chris Lattner504e8fb2004-01-13 05:51:55 +00001347 }
1348 }
1349 return 0;
Chris Lattnereab20b52004-01-12 22:07:24 +00001350}
1351
1352Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001353 Constant* const *Idxs,
1354 unsigned NumIdx) {
1355 if (NumIdx == 0 ||
1356 (NumIdx == 1 && Idxs[0]->isNullValue()))
Chris Lattnereab20b52004-01-12 22:07:24 +00001357 return const_cast<Constant*>(C);
1358
Chris Lattnercfbf9fa2004-10-17 21:54:55 +00001359 if (isa<UndefValue>(C)) {
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001360 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1361 (Value**)Idxs, NumIdx,
Chris Lattnercfbf9fa2004-10-17 21:54:55 +00001362 true);
1363 assert(Ty != 0 && "Invalid indices for GEP!");
1364 return UndefValue::get(PointerType::get(Ty));
1365 }
1366
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001367 Constant *Idx0 = Idxs[0];
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001368 if (C->isNullValue()) {
1369 bool isNull = true;
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001370 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1371 if (!Idxs[i]->isNullValue()) {
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001372 isNull = false;
1373 break;
1374 }
1375 if (isNull) {
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001376 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1377 (Value**)Idxs, NumIdx,
Chris Lattner2e9bb1a2004-02-16 20:46:13 +00001378 true);
1379 assert(Ty != 0 && "Invalid indices for GEP!");
1380 return ConstantPointerNull::get(PointerType::get(Ty));
1381 }
1382 }
Chris Lattnereab20b52004-01-12 22:07:24 +00001383
1384 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1385 // Combine Indices - If the source pointer to this getelementptr instruction
1386 // is a getelementptr instruction, combine the indices of the two
1387 // getelementptr instructions into a single instruction.
1388 //
1389 if (CE->getOpcode() == Instruction::GetElementPtr) {
1390 const Type *LastTy = 0;
1391 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1392 I != E; ++I)
1393 LastTy = *I;
1394
Chris Lattner7fa6e662004-10-11 22:52:25 +00001395 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001396 SmallVector<Value*, 16> NewIndices;
1397 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattnereab20b52004-01-12 22:07:24 +00001398 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner7fa6e662004-10-11 22:52:25 +00001399 NewIndices.push_back(CE->getOperand(i));
Chris Lattnereab20b52004-01-12 22:07:24 +00001400
1401 // Add the last index of the source with the first index of the new GEP.
1402 // Make sure to handle the case when they are actually different types.
1403 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner7fa6e662004-10-11 22:52:25 +00001404 // Otherwise it must be an array.
1405 if (!Idx0->isNullValue()) {
Chris Lattnerd3408672004-07-07 04:45:13 +00001406 const Type *IdxTy = Combined->getType();
Reid Spencer575d95c2006-12-04 02:46:44 +00001407 if (IdxTy != Idx0->getType()) {
Reid Spencer79e21d32006-12-31 05:26:44 +00001408 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Reid Spencer57c69932006-12-05 03:30:09 +00001409 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Reid Spencer79e21d32006-12-31 05:26:44 +00001410 Type::Int64Ty);
Reid Spencer575d95c2006-12-04 02:46:44 +00001411 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1412 } else {
1413 Combined =
1414 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1415 }
Chris Lattnerd3408672004-07-07 04:45:13 +00001416 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001417
Chris Lattnereab20b52004-01-12 22:07:24 +00001418 NewIndices.push_back(Combined);
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001419 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
1420 return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
1421 NewIndices.size());
Chris Lattnereab20b52004-01-12 22:07:24 +00001422 }
1423 }
1424
1425 // Implement folding of:
1426 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1427 // long 0, long 0)
1428 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1429 //
Chris Lattnerfe9d82a2007-08-13 17:09:08 +00001430 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001431 if (const PointerType *SPT =
Chris Lattnereab20b52004-01-12 22:07:24 +00001432 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1433 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1434 if (const ArrayType *CAT =
Chris Lattnerf190d382006-06-28 21:38:54 +00001435 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattnereab20b52004-01-12 22:07:24 +00001436 if (CAT->getElementType() == SAT->getElementType())
1437 return ConstantExpr::getGetElementPtr(
Chris Lattner2b9a5da2007-01-31 04:40:28 +00001438 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattnerfe9d82a2007-08-13 17:09:08 +00001439 }
1440
1441 // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1)
1442 // Into: inttoptr (i64 0 to i8*)
1443 // This happens with pointers to member functions in C++.
1444 if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 &&
1445 isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) &&
1446 cast<PointerType>(CE->getType())->getElementType() == Type::Int8Ty) {
1447 Constant *Base = CE->getOperand(0);
1448 Constant *Offset = Idxs[0];
1449
1450 // Convert the smaller integer to the larger type.
1451 if (Offset->getType()->getPrimitiveSizeInBits() <
1452 Base->getType()->getPrimitiveSizeInBits())
1453 Offset = ConstantExpr::getSExt(Offset, Base->getType());
1454 else if (Base->getType()->getPrimitiveSizeInBits() <
1455 Offset->getType()->getPrimitiveSizeInBits())
1456 Base = ConstantExpr::getZExt(Base, Base->getType());
1457
1458 Base = ConstantExpr::getAdd(Base, Offset);
1459 return ConstantExpr::getIntToPtr(Base, CE->getType());
1460 }
Chris Lattnereab20b52004-01-12 22:07:24 +00001461 }
1462 return 0;
1463}
1464