blob: 2e78bc9062d4171cd0f8173a03d01ca644338df3 [file] [log] [blame]
Reid Spencer81658a82007-02-27 06:23:51 +00001//===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner5a945e32004-01-12 21:13:12 +000010// This file implements folding of constants for LLVM. This implements the
Reid Spencer81658a82007-02-27 06:23:51 +000011// (internal) ConstantFold.h interface, which is used by the
Chris Lattner5a945e32004-01-12 21:13:12 +000012// ConstantExpr::get* methods to automatically fold constants when possible.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner1dd054c2004-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 Lattner2f7c9632001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner33e93b82007-02-27 03:05:06 +000021#include "ConstantFold.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattnera9eddae2004-02-22 06:25:38 +000023#include "llvm/Instructions.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerea0789c2004-03-08 06:17:35 +000025#include "llvm/Function.h"
Chris Lattner302116a2007-01-31 04:40:28 +000026#include "llvm/ADT/SmallVector.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Chris Lattner057083f2006-10-13 17:22:21 +000028#include "llvm/Support/GetElementPtrTypeIterator.h"
29#include "llvm/Support/ManagedStatic.h"
30#include "llvm/Support/MathExtras.h"
Jeff Cohen4e3aede2005-05-03 03:13:01 +000031#include <limits>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000032using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000033
Chris Lattner1dd054c2004-01-12 22:07:24 +000034//===----------------------------------------------------------------------===//
35// ConstantFold*Instruction Implementations
36//===----------------------------------------------------------------------===//
Chris Lattner1dd054c2004-01-12 22:07:24 +000037
Reid Spencerd84d35b2007-02-15 02:26:10 +000038/// CastConstantVector - Convert the specified ConstantVector node to the
Reid Spencer09575ba2007-02-15 03:39:18 +000039/// specified vector type. At this point, we know that the elements of the
Chris Lattner6b3f4752006-04-02 01:38:28 +000040/// input packed constant are all simple integer or FP values.
Reid Spencer81658a82007-02-27 06:23:51 +000041static Constant *CastConstantVector(ConstantVector *CV,
Reid Spencerd84d35b2007-02-15 02:26:10 +000042 const VectorType *DstTy) {
Reid Spencer81658a82007-02-27 06:23:51 +000043 unsigned SrcNumElts = CV->getType()->getNumElements();
Chris Lattner6b3f4752006-04-02 01:38:28 +000044 unsigned DstNumElts = DstTy->getNumElements();
Reid Spencer81658a82007-02-27 06:23:51 +000045 const Type *SrcEltTy = CV->getType()->getElementType();
Chris Lattner6b3f4752006-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 Spencer6c38f0b2006-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 Lattner03c49532007-01-15 02:27:26 +000055 if ((SrcEltTy->isInteger() && DstEltTy->isInteger()) ||
Reid Spencer6c38f0b2006-11-27 01:05:10 +000056 (SrcEltTy->isFloatingPoint() && DstEltTy->isFloatingPoint())) {
Chris Lattner6b3f4752006-04-02 01:38:28 +000057 for (unsigned i = 0; i != SrcNumElts; ++i)
Reid Spencer6c38f0b2006-11-27 01:05:10 +000058 Result.push_back(
Reid Spencer81658a82007-02-27 06:23:51 +000059 ConstantExpr::getBitCast(CV->getOperand(i), DstEltTy));
Reid Spencerd84d35b2007-02-15 02:26:10 +000060 return ConstantVector::get(Result);
Chris Lattner6b3f4752006-04-02 01:38:28 +000061 }
62
Reid Spencer6c38f0b2006-11-27 01:05:10 +000063 // If this is an int-to-fp cast ..
Chris Lattner03c49532007-01-15 02:27:26 +000064 if (SrcEltTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +000065 // Ensure that it is int-to-fp cast
Chris Lattner6b3f4752006-04-02 01:38:28 +000066 assert(DstEltTy->isFloatingPoint());
67 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
68 for (unsigned i = 0; i != SrcNumElts; ++i) {
69 double V =
Reid Spencer81658a82007-02-27 06:23:51 +000070 BitsToDouble(cast<ConstantInt>(CV->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +000071 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
72 }
Reid Spencerd84d35b2007-02-15 02:26:10 +000073 return ConstantVector::get(Result);
Chris Lattner6b3f4752006-04-02 01:38:28 +000074 }
75 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
76 for (unsigned i = 0; i != SrcNumElts; ++i) {
77 float V =
Reid Spencer81658a82007-02-27 06:23:51 +000078 BitsToFloat(cast<ConstantInt>(CV->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +000079 Result.push_back(ConstantFP::get(Type::FloatTy, V));
80 }
Reid Spencerd84d35b2007-02-15 02:26:10 +000081 return ConstantVector::get(Result);
Chris Lattner6b3f4752006-04-02 01:38:28 +000082 }
83
84 // Otherwise, this is an fp-to-int cast.
Chris Lattner03c49532007-01-15 02:27:26 +000085 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isInteger());
Chris Lattner6b3f4752006-04-02 01:38:28 +000086
87 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
88 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencer81658a82007-02-27 06:23:51 +000089 double V =
90 DoubleToBits(cast<ConstantFP>(CV->getOperand(i))->getValue());
Reid Spencera1276332007-03-01 19:31:12 +000091 Constant *C = ConstantInt::get(APIntOps::RoundDoubleToAPInt(V));
Reid Spencera16f9302006-12-05 07:18:07 +000092 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
Chris Lattner6b3f4752006-04-02 01:38:28 +000093 }
Reid Spencerd84d35b2007-02-15 02:26:10 +000094 return ConstantVector::get(Result);
Chris Lattner6b3f4752006-04-02 01:38:28 +000095 }
96
97 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
98 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencer81658a82007-02-27 06:23:51 +000099 uint32_t V = FloatToBits(cast<ConstantFP>(CV->getOperand(i))->getValue());
Reid Spencer8d9336d2006-12-31 05:26:44 +0000100 Constant *C = ConstantInt::get(Type::Int32Ty, V);
Reid Spencera16f9302006-12-05 07:18:07 +0000101 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000102 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000103 return ConstantVector::get(Result);
Chris Lattner6b3f4752006-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 Spencer6c38f0b2006-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.
117/// @Determine if it is valid to fold a cast of a cast
118static 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 Lattner6b3f4752006-04-02 01:38:28 +0000133
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000134 // Let CastInst::isEliminableCastPair do the heavy lifting.
135 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
Reid Spencer8d9336d2006-12-31 05:26:44 +0000136 Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000137}
138
139Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
Chris Lattner1dd054c2004-01-12 22:07:24 +0000140 const Type *DestTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000141 const Type *SrcTy = V->getType();
Chris Lattner1dd054c2004-01-12 22:07:24 +0000142
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000143 if (isa<UndefValue>(V))
144 return UndefValue::get(DestTy);
145
146 // If the cast operand is a constant expression, there's a few things we can
147 // do to try to simplify it.
148 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
149 if (CE->isCast()) {
Reid Spencer1a063892006-12-04 02:46:44 +0000150 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000151 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
152 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000153 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
154 // If all of the indexes in the GEP are null values, there is no pointer
155 // adjustment going on. We might as well cast the source pointer.
156 bool isAllNull = true;
157 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
158 if (!CE->getOperand(i)->isNullValue()) {
159 isAllNull = false;
160 break;
161 }
162 if (isAllNull)
Reid Spencer1a063892006-12-04 02:46:44 +0000163 // This is casting one pointer type to another, always BitCast
Reid Spencer27720a92006-12-05 03:30:09 +0000164 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000165 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000166 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000167
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000168 // We actually have to do a cast now. Perform the cast according to the
169 // opcode specified.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000170 switch (opc) {
171 case Instruction::FPTrunc:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000172 case Instruction::FPExt:
Reid Spencer8dabca42006-12-19 07:41:40 +0000173 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
174 return ConstantFP::get(DestTy, FPC->getValue());
175 return 0; // Can't fold.
176 case Instruction::FPToUI:
Reid Spencer81658a82007-02-27 06:23:51 +0000177 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000178 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Reid Spencerac419b52007-02-27 19:29:54 +0000179 APInt Val(APIntOps::RoundDoubleToAPInt(FPC->getValue(), DestBitWidth));
Reid Spencera1276332007-03-01 19:31:12 +0000180 return ConstantInt::get(Val);
Reid Spencer81658a82007-02-27 06:23:51 +0000181 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000182 return 0; // Can't fold.
183 case Instruction::FPToSI:
Reid Spencer81658a82007-02-27 06:23:51 +0000184 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000185 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Reid Spencerac419b52007-02-27 19:29:54 +0000186 APInt Val(APIntOps::RoundDoubleToAPInt(FPC->getValue(), DestBitWidth));
Reid Spencera1276332007-03-01 19:31:12 +0000187 return ConstantInt::get(Val);
Reid Spencer81658a82007-02-27 06:23:51 +0000188 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000189 return 0; // Can't fold.
190 case Instruction::IntToPtr: //always treated as unsigned
191 if (V->isNullValue()) // Is it an integral null value?
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000192 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Reid Spencer8dabca42006-12-19 07:41:40 +0000193 return 0; // Other pointer types cannot be casted
194 case Instruction::PtrToInt: // always treated as unsigned
195 if (V->isNullValue()) // is it a null pointer value?
Zhou Sheng75b871f2007-01-11 12:24:14 +0000196 return ConstantInt::get(DestTy, 0);
Reid Spencer8dabca42006-12-19 07:41:40 +0000197 return 0; // Other pointer types cannot be casted
198 case Instruction::UIToFP:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000199 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer4aeaeaf2007-02-27 23:33:03 +0000200 return ConstantFP::get(DestTy, CI->getValue().roundToDouble());
Reid Spencer8dabca42006-12-19 07:41:40 +0000201 return 0;
202 case Instruction::SIToFP:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000203 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer4aeaeaf2007-02-27 23:33:03 +0000204 return ConstantFP::get(DestTy, CI->getValue().signedRoundToDouble());
Reid Spencer8dabca42006-12-19 07:41:40 +0000205 return 0;
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000206 case Instruction::ZExt:
Reid Spencer81658a82007-02-27 06:23:51 +0000207 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
208 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
209 APInt Result(CI->getValue());
210 Result.zext(BitWidth);
Reid Spencera1276332007-03-01 19:31:12 +0000211 return ConstantInt::get(Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000212 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000213 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000214 case Instruction::SExt:
Reid Spencer81658a82007-02-27 06:23:51 +0000215 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
216 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
217 APInt Result(CI->getValue());
218 Result.sext(BitWidth);
Reid Spencera1276332007-03-01 19:31:12 +0000219 return ConstantInt::get(Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000220 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000221 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000222 case Instruction::Trunc:
Reid Spencer81658a82007-02-27 06:23:51 +0000223 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
224 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
225 APInt Result(CI->getValue());
226 Result.trunc(BitWidth);
Reid Spencera1276332007-03-01 19:31:12 +0000227 return ConstantInt::get(Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000228 }
Chris Lattner710ebaf2006-12-01 19:22:41 +0000229 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000230 case Instruction::BitCast:
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000231 if (SrcTy == DestTy)
232 return (Constant*)V; // no-op cast
Chris Lattner4d1da162006-12-11 18:30:27 +0000233
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000234 // Check to see if we are casting a pointer to an aggregate to a pointer to
235 // the first element. If so, return the appropriate GEP instruction.
236 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
237 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000238 SmallVector<Value*, 8> IdxList;
Reid Spencer8d9336d2006-12-31 05:26:44 +0000239 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000240 const Type *ElTy = PTy->getElementType();
241 while (ElTy != DPTy->getElementType()) {
242 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
243 if (STy->getNumElements() == 0) break;
244 ElTy = STy->getElementType(0);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000245 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000246 } else if (const SequentialType *STy =
247 dyn_cast<SequentialType>(ElTy)) {
248 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
249 ElTy = STy->getElementType();
250 IdxList.push_back(IdxList[0]);
251 } else {
Chris Lattner6b3f4752006-04-02 01:38:28 +0000252 break;
253 }
254 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000255
256 if (ElTy == DPTy->getElementType())
257 return ConstantExpr::getGetElementPtr(
Chris Lattner302116a2007-01-31 04:40:28 +0000258 const_cast<Constant*>(V), &IdxList[0], IdxList.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000259 }
260
261 // Handle casts from one packed constant to another. We know that the src
262 // and dest type have the same size (otherwise its an illegal cast).
Reid Spencerd84d35b2007-02-15 02:26:10 +0000263 if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
264 if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000265 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
266 "Not cast between same sized vectors!");
267 // First, check for null and undef
268 if (isa<ConstantAggregateZero>(V))
269 return Constant::getNullValue(DestTy);
270 if (isa<UndefValue>(V))
271 return UndefValue::get(DestTy);
272
Reid Spencer81658a82007-02-27 06:23:51 +0000273 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
Reid Spencerd84d35b2007-02-15 02:26:10 +0000274 // This is a cast from a ConstantVector of one type to a
275 // ConstantVector of another type. Check to see if all elements of
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000276 // the input are simple.
277 bool AllSimpleConstants = true;
Reid Spencer81658a82007-02-27 06:23:51 +0000278 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
279 if (!isa<ConstantInt>(CV->getOperand(i)) &&
280 !isa<ConstantFP>(CV->getOperand(i))) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000281 AllSimpleConstants = false;
282 break;
283 }
284 }
285
286 // If all of the elements are simple constants, we can fold this.
287 if (AllSimpleConstants)
Reid Spencer81658a82007-02-27 06:23:51 +0000288 return CastConstantVector(const_cast<ConstantVector*>(CV), DestPTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000289 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000290 }
291 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000292
Chris Lattner4d1da162006-12-11 18:30:27 +0000293 // Finally, implement bitcast folding now. The code below doesn't handle
294 // bitcast right.
295 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
296 return ConstantPointerNull::get(cast<PointerType>(DestTy));
297
298 // Handle integral constant input.
299 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Chris Lattner03c49532007-01-15 02:27:26 +0000300 if (DestTy->isInteger())
Reid Spencer81658a82007-02-27 06:23:51 +0000301 // Integral -> Integral. This is a no-op because the bit widths must
302 // be the same. Consequently, we just fold to V.
303 return const_cast<Constant*>(V);
Chris Lattner4d1da162006-12-11 18:30:27 +0000304
305 if (DestTy->isFloatingPoint()) {
306 if (DestTy == Type::FloatTy)
307 return ConstantFP::get(DestTy, BitsToFloat(CI->getZExtValue()));
308 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
309 return ConstantFP::get(DestTy, BitsToDouble(CI->getZExtValue()));
310 }
311 // Otherwise, can't fold this (packed?)
312 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000313 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000314
315 // Handle ConstantFP input.
316 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
317 // FP -> Integral.
Chris Lattnere62c89a2007-02-06 02:22:56 +0000318 if (DestTy == Type::Int32Ty) {
319 return ConstantInt::get(DestTy, FloatToBits(FP->getValue()));
320 } else {
321 assert(DestTy == Type::Int64Ty && "only support f32/f64 for now!");
Chris Lattner4d1da162006-12-11 18:30:27 +0000322 return ConstantInt::get(DestTy, DoubleToBits(FP->getValue()));
Chris Lattnere62c89a2007-02-06 02:22:56 +0000323 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000324 }
325 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000326 default:
327 assert(!"Invalid CE CastInst opcode");
328 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000329 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000330
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000331 assert(0 && "Failed to cast constant expression");
332 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000333}
334
Chris Lattner6ea4b522004-03-12 05:53:32 +0000335Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
336 const Constant *V1,
337 const Constant *V2) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000338 if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
Reid Spencercddc9df2007-01-12 04:24:46 +0000339 return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000340
341 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
342 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
343 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000344 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000345 return 0;
346}
347
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000348Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
349 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000350 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
Reid Spencerd84d35b2007-02-15 02:26:10 +0000351 return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000352 if (Val->isNullValue()) // ee(zero, x) -> zero
353 return Constant::getNullValue(
Reid Spencerd84d35b2007-02-15 02:26:10 +0000354 cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000355
Reid Spencerd84d35b2007-02-15 02:26:10 +0000356 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000357 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
358 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000359 } else if (isa<UndefValue>(Idx)) {
360 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
361 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000362 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000363 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000364 return 0;
365}
366
Robert Bocchinoca27f032006-01-17 20:07:22 +0000367Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
368 const Constant *Elt,
369 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000370 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000371 if (!CIdx) return 0;
Reid Spencer81658a82007-02-27 06:23:51 +0000372 APInt idxVal = CIdx->getValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000373 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000374 // Insertion of scalar constant into packed undef
375 // Optimize away insertion of undef
376 if (isa<UndefValue>(Elt))
377 return const_cast<Constant*>(Val);
378 // Otherwise break the aggregate undef into multiple undefs and do
379 // the insertion
380 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000381 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000382 std::vector<Constant*> Ops;
383 Ops.reserve(numOps);
384 for (unsigned i = 0; i < numOps; ++i) {
385 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000386 (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000387 Ops.push_back(const_cast<Constant*>(Op));
388 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000389 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000390 }
Reid Spencer3054b142006-11-02 08:18:15 +0000391 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000392 // Insertion of scalar constant into packed aggregate zero
393 // Optimize away insertion of zero
394 if (Elt->isNullValue())
395 return const_cast<Constant*>(Val);
396 // Otherwise break the aggregate zero into multiple zeros and do
397 // the insertion
398 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000399 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000400 std::vector<Constant*> Ops;
401 Ops.reserve(numOps);
402 for (unsigned i = 0; i < numOps; ++i) {
403 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000404 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000405 Ops.push_back(const_cast<Constant*>(Op));
406 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000407 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000408 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000409 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000410 // Insertion of scalar constant into packed constant
411 std::vector<Constant*> Ops;
412 Ops.reserve(CVal->getNumOperands());
413 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
414 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000415 (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
Robert Bocchinoca27f032006-01-17 20:07:22 +0000416 Ops.push_back(const_cast<Constant*>(Op));
417 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000418 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000419 }
420 return 0;
421}
422
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000423Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
424 const Constant *V2,
425 const Constant *Mask) {
426 // TODO:
427 return 0;
428}
429
Reid Spencer266e42b2006-12-23 06:05:41 +0000430/// EvalVectorOp - Given two packed constants and a function pointer, apply the
Reid Spencerd84d35b2007-02-15 02:26:10 +0000431/// function pointer to each element pair, producing a new ConstantVector
Reid Spencer266e42b2006-12-23 06:05:41 +0000432/// constant.
Reid Spencerd84d35b2007-02-15 02:26:10 +0000433static Constant *EvalVectorOp(const ConstantVector *V1,
434 const ConstantVector *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000435 Constant *(*FP)(Constant*, Constant*)) {
436 std::vector<Constant*> Res;
437 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
438 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
439 const_cast<Constant*>(V2->getOperand(i))));
Reid Spencerd84d35b2007-02-15 02:26:10 +0000440 return ConstantVector::get(Res);
Reid Spencer266e42b2006-12-23 06:05:41 +0000441}
442
443Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
444 const Constant *C1,
445 const Constant *C2) {
446 // Handle UndefValue up front
447 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
448 switch (Opcode) {
449 case Instruction::Add:
450 case Instruction::Sub:
451 case Instruction::Xor:
452 return UndefValue::get(C1->getType());
453 case Instruction::Mul:
454 case Instruction::And:
455 return Constant::getNullValue(C1->getType());
456 case Instruction::UDiv:
457 case Instruction::SDiv:
458 case Instruction::FDiv:
459 case Instruction::URem:
460 case Instruction::SRem:
461 case Instruction::FRem:
462 if (!isa<UndefValue>(C2)) // undef / X -> 0
463 return Constant::getNullValue(C1->getType());
464 return const_cast<Constant*>(C2); // X / undef -> undef
465 case Instruction::Or: // X | undef -> -1
Reid Spencerd84d35b2007-02-15 02:26:10 +0000466 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
467 return ConstantVector::getAllOnesValue(PTy);
Reid Spencer266e42b2006-12-23 06:05:41 +0000468 return ConstantInt::getAllOnesValue(C1->getType());
469 case Instruction::LShr:
470 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
471 return const_cast<Constant*>(C1); // undef lshr undef -> undef
472 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
473 // undef lshr X -> 0
474 case Instruction::AShr:
475 if (!isa<UndefValue>(C2))
476 return const_cast<Constant*>(C1); // undef ashr X --> undef
477 else if (isa<UndefValue>(C1))
478 return const_cast<Constant*>(C1); // undef ashr undef -> undef
479 else
480 return const_cast<Constant*>(C1); // X ashr undef --> X
481 case Instruction::Shl:
482 // undef << X -> 0 or X << undef -> 0
483 return Constant::getNullValue(C1->getType());
484 }
485 }
486
487 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
488 if (isa<ConstantExpr>(C2)) {
489 // There are many possible foldings we could do here. We should probably
490 // at least fold add of a pointer with an integer into the appropriate
491 // getelementptr. This will improve alias analysis a bit.
492 } else {
493 // Just implement a couple of simple identities.
494 switch (Opcode) {
495 case Instruction::Add:
496 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X + 0 == X
497 break;
498 case Instruction::Sub:
499 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X - 0 == X
500 break;
501 case Instruction::Mul:
502 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X * 0 == 0
503 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000504 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000505 return const_cast<Constant*>(C1); // X * 1 == X
506 break;
507 case Instruction::UDiv:
508 case Instruction::SDiv:
509 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000510 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000511 return const_cast<Constant*>(C1); // X / 1 == X
512 break;
513 case Instruction::URem:
514 case Instruction::SRem:
515 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000516 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000517 return Constant::getNullValue(CI->getType()); // X % 1 == 0
518 break;
519 case Instruction::And:
Chris Lattner26f13eb2007-01-04 01:56:39 +0000520 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
521 if (CI->isAllOnesValue())
522 return const_cast<Constant*>(C1); // X & -1 == X
Reid Spencer266e42b2006-12-23 06:05:41 +0000523 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X & 0 == 0
524 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
525 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
526
527 // Functions are at least 4-byte aligned. If and'ing the address of a
528 // function with a constant < 4, fold it to zero.
529 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000530 if (CI->getValue().ult(APInt(CI->getType()->getBitWidth(),4)) &&
531 isa<Function>(CPR))
Reid Spencer266e42b2006-12-23 06:05:41 +0000532 return Constant::getNullValue(CI->getType());
533 }
534 break;
535 case Instruction::Or:
536 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X | 0 == X
Chris Lattner26f13eb2007-01-04 01:56:39 +0000537 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
538 if (CI->isAllOnesValue())
539 return const_cast<Constant*>(C2); // X | -1 == -1
Reid Spencer266e42b2006-12-23 06:05:41 +0000540 break;
541 case Instruction::Xor:
542 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X ^ 0 == X
543 break;
544 }
545 }
546 } else if (isa<ConstantExpr>(C2)) {
547 // If C2 is a constant expr and C1 isn't, flop them around and fold the
548 // other way if possible.
549 switch (Opcode) {
550 case Instruction::Add:
551 case Instruction::Mul:
552 case Instruction::And:
553 case Instruction::Or:
554 case Instruction::Xor:
555 // No change of opcode required.
556 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
557
558 case Instruction::Shl:
559 case Instruction::LShr:
560 case Instruction::AShr:
561 case Instruction::Sub:
562 case Instruction::SDiv:
563 case Instruction::UDiv:
564 case Instruction::FDiv:
565 case Instruction::URem:
566 case Instruction::SRem:
567 case Instruction::FRem:
568 default: // These instructions cannot be flopped around.
569 return 0;
570 }
571 }
572
573 // At this point we know neither constant is an UndefValue nor a ConstantExpr
Chris Lattner26f13eb2007-01-04 01:56:39 +0000574 // so look at directly computing the value.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000575 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
576 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000577 using namespace APIntOps;
578 APInt C1V = CI1->getValue();
579 APInt C2V = CI2->getValue();
Chris Lattner344da522007-01-12 18:42:52 +0000580 switch (Opcode) {
581 default:
582 break;
583 case Instruction::Add:
Reid Spencera1276332007-03-01 19:31:12 +0000584 return ConstantInt::get(C1V + C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000585 case Instruction::Sub:
Reid Spencera1276332007-03-01 19:31:12 +0000586 return ConstantInt::get(C1V - C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000587 case Instruction::Mul:
Reid Spencera1276332007-03-01 19:31:12 +0000588 return ConstantInt::get(C1V * C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000589 case Instruction::UDiv:
Reid Spencer81658a82007-02-27 06:23:51 +0000590 if (CI2->isNullValue())
591 return 0; // X / 0 -> can't fold
Reid Spencera1276332007-03-01 19:31:12 +0000592 return ConstantInt::get(C1V.udiv(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000593 case Instruction::SDiv:
Reid Spencer81658a82007-02-27 06:23:51 +0000594 if (CI2->isNullValue())
595 return 0; // X / 0 -> can't fold
Reid Spencer81658a82007-02-27 06:23:51 +0000596 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
597 return 0; // MIN_INT / -1 -> overflow
Reid Spencera1276332007-03-01 19:31:12 +0000598 return ConstantInt::get(C1V.sdiv(C2V));
Reid Spencer81658a82007-02-27 06:23:51 +0000599 case Instruction::URem:
600 if (C2->isNullValue())
601 return 0; // X / 0 -> can't fold
Reid Spencera1276332007-03-01 19:31:12 +0000602 return ConstantInt::get(C1V.urem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000603 case Instruction::SRem:
Reid Spencer81658a82007-02-27 06:23:51 +0000604 if (CI2->isNullValue())
605 return 0; // X % 0 -> can't fold
606 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
607 return 0; // MIN_INT % -1 -> overflow
Reid Spencera1276332007-03-01 19:31:12 +0000608 return ConstantInt::get(C1V.srem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000609 case Instruction::And:
Reid Spencera1276332007-03-01 19:31:12 +0000610 return ConstantInt::get(C1V & C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000611 case Instruction::Or:
Reid Spencera1276332007-03-01 19:31:12 +0000612 return ConstantInt::get(C1V | C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000613 case Instruction::Xor:
Reid Spencera1276332007-03-01 19:31:12 +0000614 return ConstantInt::get(C1V ^ C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000615 case Instruction::Shl:
Reid Spencer81658a82007-02-27 06:23:51 +0000616 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000617 if (shiftAmt < C1V.getBitWidth())
Reid Spencera1276332007-03-01 19:31:12 +0000618 return ConstantInt::get(C1V.shl(shiftAmt));
Reid Spencer81658a82007-02-27 06:23:51 +0000619 else
620 return UndefValue::get(C1->getType()); // too big shift is undef
621 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Chris Lattner344da522007-01-12 18:42:52 +0000622 case Instruction::LShr:
Reid Spencer81658a82007-02-27 06:23:51 +0000623 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000624 if (shiftAmt < C1V.getBitWidth())
Reid Spencera1276332007-03-01 19:31:12 +0000625 return ConstantInt::get(C1V.lshr(shiftAmt));
Reid Spencer81658a82007-02-27 06:23:51 +0000626 else
627 return UndefValue::get(C1->getType()); // too big shift is undef
628 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Chris Lattner344da522007-01-12 18:42:52 +0000629 case Instruction::AShr:
Reid Spencer81658a82007-02-27 06:23:51 +0000630 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000631 if (shiftAmt < C1V.getBitWidth())
Reid Spencera1276332007-03-01 19:31:12 +0000632 return ConstantInt::get(C1V.ashr(shiftAmt));
Reid Spencer81658a82007-02-27 06:23:51 +0000633 else
634 return UndefValue::get(C1->getType()); // too big shift is undef
635 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Reid Spencer266e42b2006-12-23 06:05:41 +0000636 }
637 }
638 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
639 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
640 double C1Val = CFP1->getValue();
641 double C2Val = CFP2->getValue();
642 switch (Opcode) {
643 default:
644 break;
645 case Instruction::Add:
646 return ConstantFP::get(CFP1->getType(), C1Val + C2Val);
647 case Instruction::Sub:
648 return ConstantFP::get(CFP1->getType(), C1Val - C2Val);
649 case Instruction::Mul:
650 return ConstantFP::get(CFP1->getType(), C1Val * C2Val);
651 case Instruction::FDiv:
652 if (CFP2->isExactlyValue(0.0))
653 return ConstantFP::get(CFP1->getType(),
654 std::numeric_limits<double>::infinity());
655 if (CFP2->isExactlyValue(-0.0))
656 return ConstantFP::get(CFP1->getType(),
657 -std::numeric_limits<double>::infinity());
658 return ConstantFP::get(CFP1->getType(), C1Val / C2Val);
659 case Instruction::FRem:
660 if (CFP2->isNullValue())
661 return 0;
662 return ConstantFP::get(CFP1->getType(), std::fmod(C1Val, C2Val));
663 }
664 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000665 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
666 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000667 switch (Opcode) {
668 default:
669 break;
670 case Instruction::Add:
671 return EvalVectorOp(CP1, CP2, ConstantExpr::getAdd);
672 case Instruction::Sub:
673 return EvalVectorOp(CP1, CP2, ConstantExpr::getSub);
674 case Instruction::Mul:
675 return EvalVectorOp(CP1, CP2, ConstantExpr::getMul);
676 case Instruction::UDiv:
677 return EvalVectorOp(CP1, CP2, ConstantExpr::getUDiv);
678 case Instruction::SDiv:
679 return EvalVectorOp(CP1, CP2, ConstantExpr::getSDiv);
680 case Instruction::FDiv:
681 return EvalVectorOp(CP1, CP2, ConstantExpr::getFDiv);
682 case Instruction::URem:
683 return EvalVectorOp(CP1, CP2, ConstantExpr::getURem);
684 case Instruction::SRem:
685 return EvalVectorOp(CP1, CP2, ConstantExpr::getSRem);
686 case Instruction::FRem:
687 return EvalVectorOp(CP1, CP2, ConstantExpr::getFRem);
688 case Instruction::And:
689 return EvalVectorOp(CP1, CP2, ConstantExpr::getAnd);
690 case Instruction::Or:
691 return EvalVectorOp(CP1, CP2, ConstantExpr::getOr);
692 case Instruction::Xor:
693 return EvalVectorOp(CP1, CP2, ConstantExpr::getXor);
694 }
695 }
696 }
697
698 // We don't know how to fold this
699 return 0;
700}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000701
Chris Lattner60c47262005-01-28 19:09:51 +0000702/// isZeroSizedType - This type is zero sized if its an array or structure of
703/// zero sized types. The only leaf zero sized type is an empty structure.
704static bool isMaybeZeroSizedType(const Type *Ty) {
705 if (isa<OpaqueType>(Ty)) return true; // Can't say.
706 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
707
708 // If all of elements have zero size, this does too.
709 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000710 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000711 return true;
712
713 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
714 return isMaybeZeroSizedType(ATy->getElementType());
715 }
716 return false;
717}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000718
Chris Lattner061da2f2004-01-13 05:51:55 +0000719/// IdxCompare - Compare the two constants as though they were getelementptr
720/// indices. This allows coersion of the types to be the same thing.
721///
722/// If the two constants are the "same" (after coersion), return 0. If the
723/// first is less than the second, return -1, if the second is less than the
724/// first, return 1. If the constants are not integral, return -2.
725///
Chris Lattner60c47262005-01-28 19:09:51 +0000726static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000727 if (C1 == C2) return 0;
728
Reid Spencerc90cf772006-12-31 21:43:30 +0000729 // Ok, we found a different index. If they are not ConstantInt, we can't do
730 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +0000731 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
732 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000733
Chris Lattner69193f92004-04-05 01:30:19 +0000734 // Ok, we have two differing integer indices. Sign extend them to be the same
735 // type. Long is always big enough, so we use it.
Reid Spencer8d9336d2006-12-31 05:26:44 +0000736 if (C1->getType() != Type::Int64Ty)
737 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000738
Reid Spencer8d9336d2006-12-31 05:26:44 +0000739 if (C2->getType() != Type::Int64Ty)
Reid Spencerc90cf772006-12-31 21:43:30 +0000740 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000741
742 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +0000743
Chris Lattner60c47262005-01-28 19:09:51 +0000744 // If the type being indexed over is really just a zero sized type, there is
745 // no pointer difference being made here.
746 if (isMaybeZeroSizedType(ElTy))
747 return -2; // dunno.
748
Chris Lattner061da2f2004-01-13 05:51:55 +0000749 // If they are really different, now that they are the same type, then we
750 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +0000751 if (cast<ConstantInt>(C1)->getSExtValue() <
752 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +0000753 return -1;
754 else
755 return 1;
756}
757
Chris Lattner858f4e92007-01-04 02:13:20 +0000758/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +0000759/// decide about the two constants provided. This doesn't need to handle simple
760/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
761/// If we can determine that the two constants have a particular relation to
762/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +0000763/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
764/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +0000765///
766/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +0000767/// operand is always the most "complex" of the two. We consider ConstantFP
768/// to be the simplest, and ConstantExprs to be the most complex.
769static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1,
770 const Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000771 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +0000772 "Cannot compare values of different types!");
773 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +0000774 if (V1 == V2) return FCmpInst::FCMP_OEQ;
775
Reid Spencer9d36acf2006-12-24 18:52:08 +0000776 if (!isa<ConstantExpr>(V1)) {
777 if (!isa<ConstantExpr>(V2)) {
778 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +0000779 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000780 Constant *C1 = const_cast<Constant*>(V1);
781 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng75b871f2007-01-11 12:24:14 +0000782 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000783 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000784 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000785 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000786 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000787 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000788 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000789 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000790 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000791 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000792 if (R && !R->isNullValue())
Reid Spencer9d36acf2006-12-24 18:52:08 +0000793 return FCmpInst::FCMP_OGT;
794
795 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +0000796 return FCmpInst::BAD_FCMP_PREDICATE;
797 }
798
Reid Spencer9d36acf2006-12-24 18:52:08 +0000799 // If the first operand is simple and second is ConstantExpr, swap operands.
800 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
801 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
802 return FCmpInst::getSwappedPredicate(SwappedRelation);
803 } else {
804 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
805 // constantexpr or a simple constant.
806 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
807 switch (CE1->getOpcode()) {
808 case Instruction::FPTrunc:
809 case Instruction::FPExt:
810 case Instruction::UIToFP:
811 case Instruction::SIToFP:
812 // We might be able to do something with these but we don't right now.
813 break;
814 default:
815 break;
816 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000817 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000818 // There are MANY other foldings that we could perform here. They will
819 // probably be added on demand, as they seem needed.
820 return FCmpInst::BAD_FCMP_PREDICATE;
821}
822
823/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +0000824/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000825/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +0000826/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +0000827/// particular relation to each other, we should return the corresponding ICmp
828/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +0000829///
830/// To simplify this code we canonicalize the relation so that the first
831/// operand is always the most "complex" of the two. We consider simple
832/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000833/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000834///
Reid Spencer9d36acf2006-12-24 18:52:08 +0000835static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
836 const Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000837 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000838 assert(V1->getType() == V2->getType() &&
839 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000840 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000841
Reid Spenceraccd7c72004-07-17 23:47:01 +0000842 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000843 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
844 // We distilled this down to a simple case, use the standard constant
845 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000846 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000847 Constant *C1 = const_cast<Constant*>(V1);
848 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencer266e42b2006-12-23 06:05:41 +0000849 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000850 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000851 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000852 return pred;
853 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000854 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000855 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000856 return pred;
857 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000858 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000859 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000860 return pred;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000861
862 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +0000863 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000864 }
865
Chris Lattner061da2f2004-01-13 05:51:55 +0000866 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +0000867 ICmpInst::Predicate SwappedRelation =
868 evaluateICmpRelation(V2, V1, isSigned);
869 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
870 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000871
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000872 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +0000873 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +0000874 ICmpInst::Predicate SwappedRelation =
875 evaluateICmpRelation(V2, V1, isSigned);
876 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
877 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000878 else
Reid Spencer266e42b2006-12-23 06:05:41 +0000879 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +0000880 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000881
Reid Spenceraccd7c72004-07-17 23:47:01 +0000882 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000883 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000884 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000885 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000886 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000887 } else {
Reid Spencer876f7222006-12-06 00:25:09 +0000888 // GlobalVals can never be null.
Chris Lattner061da2f2004-01-13 05:51:55 +0000889 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer876f7222006-12-06 00:25:09 +0000890 if (!CPR1->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000891 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000892 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000893 } else {
894 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
895 // constantexpr, a CPR, or a simple constant.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000896 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
897 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +0000898
899 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000900 case Instruction::Trunc:
901 case Instruction::FPTrunc:
902 case Instruction::FPExt:
903 case Instruction::FPToUI:
904 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +0000905 break; // We can't evaluate floating point casts or truncations.
906
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000907 case Instruction::UIToFP:
908 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000909 case Instruction::IntToPtr:
910 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +0000911 case Instruction::ZExt:
912 case Instruction::SExt:
913 case Instruction::PtrToInt:
Chris Lattner061da2f2004-01-13 05:51:55 +0000914 // If the cast is not actually changing bits, and the second operand is a
915 // null pointer, do the comparison with the pre-casted value.
916 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000917 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000918 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000919 (CE1->getOpcode() == Instruction::SExt ? true :
920 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
921 return evaluateICmpRelation(
Reid Spencerccf78ac2006-12-23 10:21:26 +0000922 CE1Op0, Constant::getNullValue(CE1Op0->getType()), sgnd);
Reid Spencer266e42b2006-12-23 06:05:41 +0000923 }
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000924
925 // If the dest type is a pointer type, and the RHS is a constantexpr cast
926 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencer266e42b2006-12-23 06:05:41 +0000927 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000928 // which happens a lot in compilers with tagged integers.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000929 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer266e42b2006-12-23 06:05:41 +0000930 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000931 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000932 CE1->getOperand(0)->getType()->isInteger()) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000933 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000934 (CE1->getOpcode() == Instruction::SExt ? true :
935 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
936 return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0),
Reid Spencerccf78ac2006-12-23 10:21:26 +0000937 sgnd);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000938 }
Chris Lattner192e3262004-04-11 01:29:30 +0000939 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000940
941 case Instruction::GetElementPtr:
942 // Ok, since this is a getelementptr, we know that the constant has a
943 // pointer type. Check the various cases.
944 if (isa<ConstantPointerNull>(V2)) {
945 // If we are comparing a GEP to a null pointer, check to see if the base
946 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000947 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000948 if (GV->hasExternalWeakLinkage())
949 // Weak linkage GVals could be zero or not. We're comparing that
950 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000951 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +0000952 else
953 // If its not weak linkage, the GVal must have a non-zero address
954 // so the result is greater-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000955 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000956 } else if (isa<ConstantPointerNull>(CE1Op0)) {
957 // If we are indexing from a null pointer, check to see if we have any
958 // non-zero indices.
959 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
960 if (!CE1->getOperand(i)->isNullValue())
961 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000962 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000963 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +0000964 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000965 }
966 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000967 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000968 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000969 if (CPR2->hasExternalWeakLinkage())
970 // Weak linkage GVals could be zero or not. We're comparing it to
971 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000972 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +0000973 else
974 // If its not weak linkage, the GVal must have a non-zero address
975 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000976 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000977 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000978 if (CPR1 == CPR2) {
979 // If this is a getelementptr of the same global, then it must be
980 // different. Because the types must match, the getelementptr could
981 // only have at most one index, and because we fold getelementptr's
982 // with a single zero index, it must be nonzero.
983 assert(CE1->getNumOperands() == 2 &&
984 !CE1->getOperand(1)->isNullValue() &&
985 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000986 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000987 } else {
988 // If they are different globals, we don't know what the value is,
989 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000990 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000991 }
992 }
993 } else {
994 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
995 const Constant *CE2Op0 = CE2->getOperand(0);
996
997 // There are MANY other foldings that we could perform here. They will
998 // probably be added on demand, as they seem needed.
999 switch (CE2->getOpcode()) {
1000 default: break;
1001 case Instruction::GetElementPtr:
1002 // By far the most common case to handle is when the base pointers are
1003 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001004 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001005 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001006 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001007 // Ok, we know that both getelementptr instructions are based on the
1008 // same global. From this, we can precisely determine the relative
1009 // ordering of the resultant pointers.
1010 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001011
Chris Lattner061da2f2004-01-13 05:51:55 +00001012 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001013 gep_type_iterator GTI = gep_type_begin(CE1);
1014 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1015 ++i, ++GTI)
1016 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1017 GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001018 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1019 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1020 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001021 }
1022
1023 // Ok, we ran out of things they have in common. If any leftovers
1024 // are non-zero then we have a difference, otherwise we are equal.
1025 for (; i < CE1->getNumOperands(); ++i)
1026 if (!CE1->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001027 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001028 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +00001029 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001030 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001031
Chris Lattner061da2f2004-01-13 05:51:55 +00001032 for (; i < CE2->getNumOperands(); ++i)
1033 if (!CE2->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001034 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001035 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001036 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001037 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1038 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001039 }
1040 }
1041 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001042 default:
1043 break;
1044 }
1045 }
1046
Reid Spencer266e42b2006-12-23 06:05:41 +00001047 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001048}
1049
Reid Spencer9d36acf2006-12-24 18:52:08 +00001050Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1051 const Constant *C1,
1052 const Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001053
1054 // Handle some degenerate cases first
1055 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Reid Spencer542964f2007-01-11 18:21:29 +00001056 return UndefValue::get(Type::Int1Ty);
Reid Spencer266e42b2006-12-23 06:05:41 +00001057
1058 // icmp eq/ne(null,GV) -> false/true
1059 if (C1->isNullValue()) {
1060 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1061 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001062 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001063 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001064 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001065 return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001066 // icmp eq/ne(GV,null) -> false/true
1067 } else if (C2->isNullValue()) {
1068 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1069 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001070 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001071 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001072 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001073 return ConstantInt::getTrue();
Chris Lattner1dd054c2004-01-12 22:07:24 +00001074 }
1075
Chris Lattner344da522007-01-12 18:42:52 +00001076 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +00001077 APInt V1 = cast<ConstantInt>(C1)->getValue();
1078 APInt V2 = cast<ConstantInt>(C2)->getValue();
1079 switch (pred) {
1080 default: assert(0 && "Invalid ICmp Predicate"); return 0;
1081 case ICmpInst::ICMP_EQ: return ConstantInt::get(Type::Int1Ty, V1 == V2);
1082 case ICmpInst::ICMP_NE: return ConstantInt::get(Type::Int1Ty, V1 != V2);
1083 case ICmpInst::ICMP_SLT:return ConstantInt::get(Type::Int1Ty, V1.slt(V2));
1084 case ICmpInst::ICMP_SGT:return ConstantInt::get(Type::Int1Ty, V1.sgt(V2));
1085 case ICmpInst::ICMP_SLE:return ConstantInt::get(Type::Int1Ty, V1.sle(V2));
1086 case ICmpInst::ICMP_SGE:return ConstantInt::get(Type::Int1Ty, V1.sge(V2));
1087 case ICmpInst::ICMP_ULT:return ConstantInt::get(Type::Int1Ty, V1.ult(V2));
1088 case ICmpInst::ICMP_UGT:return ConstantInt::get(Type::Int1Ty, V1.ugt(V2));
1089 case ICmpInst::ICMP_ULE:return ConstantInt::get(Type::Int1Ty, V1.ule(V2));
1090 case ICmpInst::ICMP_UGE:return ConstantInt::get(Type::Int1Ty, V1.uge(V2));
Chris Lattner061da2f2004-01-13 05:51:55 +00001091 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001092 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1093 double C1Val = cast<ConstantFP>(C1)->getValue();
1094 double C2Val = cast<ConstantFP>(C2)->getValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001095 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001096 default: assert(0 && "Invalid FCmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001097 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse();
1098 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001099 case FCmpInst::FCMP_UNO:
Reid Spencercddc9df2007-01-12 04:24:46 +00001100 return ConstantInt::get(Type::Int1Ty, C1Val != C1Val || C2Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001101 case FCmpInst::FCMP_ORD:
Reid Spencercddc9df2007-01-12 04:24:46 +00001102 return ConstantInt::get(Type::Int1Ty, C1Val == C1Val && C2Val == C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001103 case FCmpInst::FCMP_UEQ:
Reid Spencer74bd0362007-01-11 00:25:45 +00001104 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001105 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001106 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001107 case FCmpInst::FCMP_OEQ:
1108 return ConstantInt::get(Type::Int1Ty, C1Val == C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001109 case FCmpInst::FCMP_UNE:
1110 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001111 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001112 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001113 case FCmpInst::FCMP_ONE:
1114 return ConstantInt::get(Type::Int1Ty, C1Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001115 case FCmpInst::FCMP_ULT:
1116 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001117 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001118 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001119 case FCmpInst::FCMP_OLT:
1120 return ConstantInt::get(Type::Int1Ty, C1Val < C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001121 case FCmpInst::FCMP_UGT:
Reid Spencer74bd0362007-01-11 00:25:45 +00001122 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001123 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001124 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001125 case FCmpInst::FCMP_OGT:
1126 return ConstantInt::get(Type::Int1Ty, C1Val > C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001127 case FCmpInst::FCMP_ULE:
1128 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001129 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001130 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001131 case FCmpInst::FCMP_OLE:
1132 return ConstantInt::get(Type::Int1Ty, C1Val <= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001133 case FCmpInst::FCMP_UGE:
Reid Spencer74bd0362007-01-11 00:25:45 +00001134 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001135 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001136 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001137 case FCmpInst::FCMP_OGE:
1138 return ConstantInt::get(Type::Int1Ty, C1Val >= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001139 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001140 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
1141 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001142 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001143 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1144 Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
1145 const_cast<Constant*>(CP1->getOperand(i)),
1146 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001147 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001148 return CB;
1149 }
1150 // Otherwise, could not decide from any element pairs.
1151 return 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001152 } else if (pred == ICmpInst::ICMP_EQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001153 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1154 Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ,
1155 const_cast<Constant*>(CP1->getOperand(i)),
1156 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001157 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001158 return CB;
1159 }
1160 // Otherwise, could not decide from any element pairs.
1161 return 0;
1162 }
1163 }
1164 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001165
Reid Spencer9d36acf2006-12-24 18:52:08 +00001166 if (C1->getType()->isFloatingPoint()) {
1167 switch (evaluateFCmpRelation(C1, C2)) {
1168 default: assert(0 && "Unknown relation!");
1169 case FCmpInst::FCMP_UNO:
1170 case FCmpInst::FCMP_ORD:
1171 case FCmpInst::FCMP_UEQ:
1172 case FCmpInst::FCMP_UNE:
1173 case FCmpInst::FCMP_ULT:
1174 case FCmpInst::FCMP_UGT:
1175 case FCmpInst::FCMP_ULE:
1176 case FCmpInst::FCMP_UGE:
1177 case FCmpInst::FCMP_TRUE:
1178 case FCmpInst::FCMP_FALSE:
1179 case FCmpInst::BAD_FCMP_PREDICATE:
1180 break; // Couldn't determine anything about these constants.
1181 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001182 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001183 pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1184 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1185 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1186 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001187 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001188 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1189 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1190 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1191 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001192 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001193 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1194 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1195 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1196 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1197 // We can only partially decide this relation.
1198 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001199 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001200 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001201 return ConstantInt::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001202 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001203 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1204 // We can only partially decide this relation.
1205 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001206 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001207 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001208 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001209 break;
1210 case ICmpInst::ICMP_NE: // We know that C1 != C2
1211 // We can only partially decide this relation.
1212 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001213 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001214 if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001215 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001216 break;
1217 }
1218 } else {
1219 // Evaluate the relation between the two constants, per the predicate.
1220 switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
1221 default: assert(0 && "Unknown relational!");
1222 case ICmpInst::BAD_ICMP_PREDICATE:
1223 break; // Couldn't determine anything about these constants.
1224 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1225 // If we know the constants are equal, we can decide the result of this
1226 // computation precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001227 return ConstantInt::get(Type::Int1Ty,
1228 pred == ICmpInst::ICMP_EQ ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001229 pred == ICmpInst::ICMP_ULE ||
1230 pred == ICmpInst::ICMP_SLE ||
1231 pred == ICmpInst::ICMP_UGE ||
1232 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001233 case ICmpInst::ICMP_ULT:
1234 // If we know that C1 < C2, we can decide the result of this computation
1235 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001236 return ConstantInt::get(Type::Int1Ty,
1237 pred == ICmpInst::ICMP_ULT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001238 pred == ICmpInst::ICMP_NE ||
1239 pred == ICmpInst::ICMP_ULE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001240 case ICmpInst::ICMP_SLT:
1241 // If we know that C1 < C2, we can decide the result of this computation
1242 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001243 return ConstantInt::get(Type::Int1Ty,
1244 pred == ICmpInst::ICMP_SLT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001245 pred == ICmpInst::ICMP_NE ||
1246 pred == ICmpInst::ICMP_SLE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001247 case ICmpInst::ICMP_UGT:
1248 // If we know that C1 > C2, we can decide the result of this computation
1249 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001250 return ConstantInt::get(Type::Int1Ty,
1251 pred == ICmpInst::ICMP_UGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001252 pred == ICmpInst::ICMP_NE ||
1253 pred == ICmpInst::ICMP_UGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001254 case ICmpInst::ICMP_SGT:
1255 // If we know that C1 > C2, we can decide the result of this computation
1256 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001257 return ConstantInt::get(Type::Int1Ty,
1258 pred == ICmpInst::ICMP_SGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001259 pred == ICmpInst::ICMP_NE ||
1260 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001261 case ICmpInst::ICMP_ULE:
1262 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001263 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getFalse();
1264 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001265 break;
1266 case ICmpInst::ICMP_SLE:
1267 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001268 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getFalse();
1269 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001270 break;
1271
1272 case ICmpInst::ICMP_UGE:
1273 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001274 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getFalse();
1275 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001276 break;
1277 case ICmpInst::ICMP_SGE:
1278 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001279 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getFalse();
1280 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001281 break;
1282
1283 case ICmpInst::ICMP_NE:
1284 // If we know that C1 != C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001285 if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse();
1286 if (pred == ICmpInst::ICMP_NE) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001287 break;
1288 }
1289
1290 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
1291 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1292 // other way if possible.
1293 switch (pred) {
1294 case ICmpInst::ICMP_EQ:
1295 case ICmpInst::ICMP_NE:
1296 // No change of predicate required.
1297 return ConstantFoldCompareInstruction(pred, C2, C1);
1298
1299 case ICmpInst::ICMP_ULT:
1300 case ICmpInst::ICMP_SLT:
1301 case ICmpInst::ICMP_UGT:
1302 case ICmpInst::ICMP_SGT:
1303 case ICmpInst::ICMP_ULE:
1304 case ICmpInst::ICMP_SLE:
1305 case ICmpInst::ICMP_UGE:
1306 case ICmpInst::ICMP_SGE:
1307 // Change the predicate as necessary to swap the operands.
1308 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1309 return ConstantFoldCompareInstruction(pred, C2, C1);
1310
1311 default: // These predicates cannot be flopped around.
1312 break;
1313 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001314 }
1315 }
1316 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001317}
1318
1319Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001320 Constant* const *Idxs,
1321 unsigned NumIdx) {
1322 if (NumIdx == 0 ||
1323 (NumIdx == 1 && Idxs[0]->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001324 return const_cast<Constant*>(C);
1325
Chris Lattnerf6013752004-10-17 21:54:55 +00001326 if (isa<UndefValue>(C)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001327 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1328 (Value**)Idxs, NumIdx,
Chris Lattnerf6013752004-10-17 21:54:55 +00001329 true);
1330 assert(Ty != 0 && "Invalid indices for GEP!");
1331 return UndefValue::get(PointerType::get(Ty));
1332 }
1333
Chris Lattner302116a2007-01-31 04:40:28 +00001334 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001335 if (C->isNullValue()) {
1336 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001337 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1338 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001339 isNull = false;
1340 break;
1341 }
1342 if (isNull) {
Chris Lattner302116a2007-01-31 04:40:28 +00001343 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1344 (Value**)Idxs, NumIdx,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001345 true);
1346 assert(Ty != 0 && "Invalid indices for GEP!");
1347 return ConstantPointerNull::get(PointerType::get(Ty));
1348 }
1349 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001350
1351 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1352 // Combine Indices - If the source pointer to this getelementptr instruction
1353 // is a getelementptr instruction, combine the indices of the two
1354 // getelementptr instructions into a single instruction.
1355 //
1356 if (CE->getOpcode() == Instruction::GetElementPtr) {
1357 const Type *LastTy = 0;
1358 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1359 I != E; ++I)
1360 LastTy = *I;
1361
Chris Lattner13128ab2004-10-11 22:52:25 +00001362 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001363 SmallVector<Value*, 16> NewIndices;
1364 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001365 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001366 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001367
1368 // Add the last index of the source with the first index of the new GEP.
1369 // Make sure to handle the case when they are actually different types.
1370 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001371 // Otherwise it must be an array.
1372 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001373 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001374 if (IdxTy != Idx0->getType()) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001375 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Reid Spencer27720a92006-12-05 03:30:09 +00001376 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001377 Type::Int64Ty);
Reid Spencer1a063892006-12-04 02:46:44 +00001378 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1379 } else {
1380 Combined =
1381 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1382 }
Chris Lattner71068a02004-07-07 04:45:13 +00001383 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001384
Chris Lattner1dd054c2004-01-12 22:07:24 +00001385 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001386 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
1387 return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
1388 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001389 }
1390 }
1391
1392 // Implement folding of:
1393 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1394 // long 0, long 0)
1395 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1396 //
Chris Lattner302116a2007-01-31 04:40:28 +00001397 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001398 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001399 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1400 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1401 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001402 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001403 if (CAT->getElementType() == SAT->getElementType())
1404 return ConstantExpr::getGetElementPtr(
Chris Lattner302116a2007-01-31 04:40:28 +00001405 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001406 }
1407 return 0;
1408}
1409