blob: d4b02d9915566d39750cd37748a940d5f67de148 [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());
91 Constant *C = ConstantInt::get(Type::Int64Ty,
92 APIntOps::RoundDoubleToAPInt(V));
Reid Spencera16f9302006-12-05 07:18:07 +000093 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
Chris Lattner6b3f4752006-04-02 01:38:28 +000094 }
Reid Spencerd84d35b2007-02-15 02:26:10 +000095 return ConstantVector::get(Result);
Chris Lattner6b3f4752006-04-02 01:38:28 +000096 }
97
98 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
99 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencer81658a82007-02-27 06:23:51 +0000100 uint32_t V = FloatToBits(cast<ConstantFP>(CV->getOperand(i))->getValue());
Reid Spencer8d9336d2006-12-31 05:26:44 +0000101 Constant *C = ConstantInt::get(Type::Int32Ty, V);
Reid Spencera16f9302006-12-05 07:18:07 +0000102 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000103 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000104 return ConstantVector::get(Result);
Chris Lattner6b3f4752006-04-02 01:38:28 +0000105 }
106
107 // Otherwise, this is a cast that changes element count and size. Handle
108 // casts which shrink the elements here.
109
110 // FIXME: We need to know endianness to do this!
111
112 return 0;
113}
114
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000115/// This function determines which opcode to use to fold two constant cast
116/// expressions together. It uses CastInst::isEliminableCastPair to determine
117/// the opcode. Consequently its just a wrapper around that function.
118/// @Determine if it is valid to fold a cast of a cast
119static unsigned
120foldConstantCastPair(
121 unsigned opc, ///< opcode of the second cast constant expression
122 const ConstantExpr*Op, ///< the first cast constant expression
123 const Type *DstTy ///< desintation type of the first cast
124) {
125 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
126 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
127 assert(CastInst::isCast(opc) && "Invalid cast opcode");
128
129 // The the types and opcodes for the two Cast constant expressions
130 const Type *SrcTy = Op->getOperand(0)->getType();
131 const Type *MidTy = Op->getType();
132 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
133 Instruction::CastOps secondOp = Instruction::CastOps(opc);
Chris Lattner6b3f4752006-04-02 01:38:28 +0000134
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000135 // Let CastInst::isEliminableCastPair do the heavy lifting.
136 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
Reid Spencer8d9336d2006-12-31 05:26:44 +0000137 Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000138}
139
140Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
Chris Lattner1dd054c2004-01-12 22:07:24 +0000141 const Type *DestTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000142 const Type *SrcTy = V->getType();
Chris Lattner1dd054c2004-01-12 22:07:24 +0000143
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000144 if (isa<UndefValue>(V))
145 return UndefValue::get(DestTy);
146
147 // If the cast operand is a constant expression, there's a few things we can
148 // do to try to simplify it.
149 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
150 if (CE->isCast()) {
Reid Spencer1a063892006-12-04 02:46:44 +0000151 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000152 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
153 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000154 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
155 // If all of the indexes in the GEP are null values, there is no pointer
156 // adjustment going on. We might as well cast the source pointer.
157 bool isAllNull = true;
158 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
159 if (!CE->getOperand(i)->isNullValue()) {
160 isAllNull = false;
161 break;
162 }
163 if (isAllNull)
Reid Spencer1a063892006-12-04 02:46:44 +0000164 // This is casting one pointer type to another, always BitCast
Reid Spencer27720a92006-12-05 03:30:09 +0000165 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000166 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000167 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000168
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000169 // We actually have to do a cast now. Perform the cast according to the
170 // opcode specified.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000171 switch (opc) {
172 case Instruction::FPTrunc:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000173 case Instruction::FPExt:
Reid Spencer8dabca42006-12-19 07:41:40 +0000174 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
175 return ConstantFP::get(DestTy, FPC->getValue());
176 return 0; // Can't fold.
177 case Instruction::FPToUI:
Reid Spencer81658a82007-02-27 06:23:51 +0000178 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000179 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Reid Spencerac419b52007-02-27 19:29:54 +0000180 APInt Val(APIntOps::RoundDoubleToAPInt(FPC->getValue(), DestBitWidth));
Reid Spencer81658a82007-02-27 06:23:51 +0000181 return ConstantInt::get(DestTy, Val);
182 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000183 return 0; // Can't fold.
184 case Instruction::FPToSI:
Reid Spencer81658a82007-02-27 06:23:51 +0000185 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000186 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Reid Spencerac419b52007-02-27 19:29:54 +0000187 APInt Val(APIntOps::RoundDoubleToAPInt(FPC->getValue(), DestBitWidth));
Reid Spencer81658a82007-02-27 06:23:51 +0000188 return ConstantInt::get(DestTy, Val);
189 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000190 return 0; // Can't fold.
191 case Instruction::IntToPtr: //always treated as unsigned
192 if (V->isNullValue()) // Is it an integral null value?
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000193 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Reid Spencer8dabca42006-12-19 07:41:40 +0000194 return 0; // Other pointer types cannot be casted
195 case Instruction::PtrToInt: // always treated as unsigned
196 if (V->isNullValue()) // is it a null pointer value?
Zhou Sheng75b871f2007-01-11 12:24:14 +0000197 return ConstantInt::get(DestTy, 0);
Reid Spencer8dabca42006-12-19 07:41:40 +0000198 return 0; // Other pointer types cannot be casted
199 case Instruction::UIToFP:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000200 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer81658a82007-02-27 06:23:51 +0000201 if (CI->getType()->getBitWidth() <= APInt::APINT_BITS_PER_WORD)
202 return ConstantFP::get(DestTy, CI->getValue().roundToDouble(false));
Reid Spencer8dabca42006-12-19 07:41:40 +0000203 return 0;
204 case Instruction::SIToFP:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000205 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer81658a82007-02-27 06:23:51 +0000206 if (CI->getType()->getBitWidth() <= APInt::APINT_BITS_PER_WORD)
207 return ConstantFP::get(DestTy, CI->getValue().roundToDouble(true));
Reid Spencer8dabca42006-12-19 07:41:40 +0000208 return 0;
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000209 case Instruction::ZExt:
Reid Spencer81658a82007-02-27 06:23:51 +0000210 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
211 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
212 APInt Result(CI->getValue());
213 Result.zext(BitWidth);
214 return ConstantInt::get(DestTy, Result);
215 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000216 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000217 case Instruction::SExt:
Reid Spencer81658a82007-02-27 06:23:51 +0000218 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
219 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
220 APInt Result(CI->getValue());
221 Result.sext(BitWidth);
222 return ConstantInt::get(DestTy, Result);
223 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000224 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000225 case Instruction::Trunc:
Reid Spencer81658a82007-02-27 06:23:51 +0000226 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
227 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
228 APInt Result(CI->getValue());
229 Result.trunc(BitWidth);
230 return ConstantInt::get(DestTy, Result);
231 }
Chris Lattner710ebaf2006-12-01 19:22:41 +0000232 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000233 case Instruction::BitCast:
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000234 if (SrcTy == DestTy)
235 return (Constant*)V; // no-op cast
Chris Lattner4d1da162006-12-11 18:30:27 +0000236
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000237 // Check to see if we are casting a pointer to an aggregate to a pointer to
238 // the first element. If so, return the appropriate GEP instruction.
239 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
240 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000241 SmallVector<Value*, 8> IdxList;
Reid Spencer8d9336d2006-12-31 05:26:44 +0000242 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000243 const Type *ElTy = PTy->getElementType();
244 while (ElTy != DPTy->getElementType()) {
245 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
246 if (STy->getNumElements() == 0) break;
247 ElTy = STy->getElementType(0);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000248 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000249 } else if (const SequentialType *STy =
250 dyn_cast<SequentialType>(ElTy)) {
251 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
252 ElTy = STy->getElementType();
253 IdxList.push_back(IdxList[0]);
254 } else {
Chris Lattner6b3f4752006-04-02 01:38:28 +0000255 break;
256 }
257 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000258
259 if (ElTy == DPTy->getElementType())
260 return ConstantExpr::getGetElementPtr(
Chris Lattner302116a2007-01-31 04:40:28 +0000261 const_cast<Constant*>(V), &IdxList[0], IdxList.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000262 }
263
264 // Handle casts from one packed constant to another. We know that the src
265 // and dest type have the same size (otherwise its an illegal cast).
Reid Spencerd84d35b2007-02-15 02:26:10 +0000266 if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
267 if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000268 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
269 "Not cast between same sized vectors!");
270 // First, check for null and undef
271 if (isa<ConstantAggregateZero>(V))
272 return Constant::getNullValue(DestTy);
273 if (isa<UndefValue>(V))
274 return UndefValue::get(DestTy);
275
Reid Spencer81658a82007-02-27 06:23:51 +0000276 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
Reid Spencerd84d35b2007-02-15 02:26:10 +0000277 // This is a cast from a ConstantVector of one type to a
278 // ConstantVector of another type. Check to see if all elements of
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000279 // the input are simple.
280 bool AllSimpleConstants = true;
Reid Spencer81658a82007-02-27 06:23:51 +0000281 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
282 if (!isa<ConstantInt>(CV->getOperand(i)) &&
283 !isa<ConstantFP>(CV->getOperand(i))) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000284 AllSimpleConstants = false;
285 break;
286 }
287 }
288
289 // If all of the elements are simple constants, we can fold this.
290 if (AllSimpleConstants)
Reid Spencer81658a82007-02-27 06:23:51 +0000291 return CastConstantVector(const_cast<ConstantVector*>(CV), DestPTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000292 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000293 }
294 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000295
Chris Lattner4d1da162006-12-11 18:30:27 +0000296 // Finally, implement bitcast folding now. The code below doesn't handle
297 // bitcast right.
298 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
299 return ConstantPointerNull::get(cast<PointerType>(DestTy));
300
301 // Handle integral constant input.
302 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Chris Lattner03c49532007-01-15 02:27:26 +0000303 if (DestTy->isInteger())
Reid Spencer81658a82007-02-27 06:23:51 +0000304 // Integral -> Integral. This is a no-op because the bit widths must
305 // be the same. Consequently, we just fold to V.
306 return const_cast<Constant*>(V);
Chris Lattner4d1da162006-12-11 18:30:27 +0000307
308 if (DestTy->isFloatingPoint()) {
309 if (DestTy == Type::FloatTy)
310 return ConstantFP::get(DestTy, BitsToFloat(CI->getZExtValue()));
311 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
312 return ConstantFP::get(DestTy, BitsToDouble(CI->getZExtValue()));
313 }
314 // Otherwise, can't fold this (packed?)
315 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000316 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000317
318 // Handle ConstantFP input.
319 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
320 // FP -> Integral.
Chris Lattnere62c89a2007-02-06 02:22:56 +0000321 if (DestTy == Type::Int32Ty) {
322 return ConstantInt::get(DestTy, FloatToBits(FP->getValue()));
323 } else {
324 assert(DestTy == Type::Int64Ty && "only support f32/f64 for now!");
Chris Lattner4d1da162006-12-11 18:30:27 +0000325 return ConstantInt::get(DestTy, DoubleToBits(FP->getValue()));
Chris Lattnere62c89a2007-02-06 02:22:56 +0000326 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000327 }
328 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000329 default:
330 assert(!"Invalid CE CastInst opcode");
331 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000332 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000333
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000334 assert(0 && "Failed to cast constant expression");
335 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000336}
337
Chris Lattner6ea4b522004-03-12 05:53:32 +0000338Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
339 const Constant *V1,
340 const Constant *V2) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000341 if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
Reid Spencercddc9df2007-01-12 04:24:46 +0000342 return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000343
344 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
345 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
346 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000347 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000348 return 0;
349}
350
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000351Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
352 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000353 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
Reid Spencerd84d35b2007-02-15 02:26:10 +0000354 return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000355 if (Val->isNullValue()) // ee(zero, x) -> zero
356 return Constant::getNullValue(
Reid Spencerd84d35b2007-02-15 02:26:10 +0000357 cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000358
Reid Spencerd84d35b2007-02-15 02:26:10 +0000359 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000360 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
361 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000362 } else if (isa<UndefValue>(Idx)) {
363 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
364 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000365 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000366 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000367 return 0;
368}
369
Robert Bocchinoca27f032006-01-17 20:07:22 +0000370Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
371 const Constant *Elt,
372 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000373 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000374 if (!CIdx) return 0;
Reid Spencer81658a82007-02-27 06:23:51 +0000375 APInt idxVal = CIdx->getValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000376 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000377 // Insertion of scalar constant into packed undef
378 // Optimize away insertion of undef
379 if (isa<UndefValue>(Elt))
380 return const_cast<Constant*>(Val);
381 // Otherwise break the aggregate undef into multiple undefs and do
382 // the insertion
383 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000384 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000385 std::vector<Constant*> Ops;
386 Ops.reserve(numOps);
387 for (unsigned i = 0; i < numOps; ++i) {
388 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000389 (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000390 Ops.push_back(const_cast<Constant*>(Op));
391 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000392 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000393 }
Reid Spencer3054b142006-11-02 08:18:15 +0000394 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000395 // Insertion of scalar constant into packed aggregate zero
396 // Optimize away insertion of zero
397 if (Elt->isNullValue())
398 return const_cast<Constant*>(Val);
399 // Otherwise break the aggregate zero into multiple zeros and do
400 // the insertion
401 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000402 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000403 std::vector<Constant*> Ops;
404 Ops.reserve(numOps);
405 for (unsigned i = 0; i < numOps; ++i) {
406 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000407 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000408 Ops.push_back(const_cast<Constant*>(Op));
409 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000410 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000411 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000412 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000413 // Insertion of scalar constant into packed constant
414 std::vector<Constant*> Ops;
415 Ops.reserve(CVal->getNumOperands());
416 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
417 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000418 (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
Robert Bocchinoca27f032006-01-17 20:07:22 +0000419 Ops.push_back(const_cast<Constant*>(Op));
420 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000421 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000422 }
423 return 0;
424}
425
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000426Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
427 const Constant *V2,
428 const Constant *Mask) {
429 // TODO:
430 return 0;
431}
432
Reid Spencer266e42b2006-12-23 06:05:41 +0000433/// EvalVectorOp - Given two packed constants and a function pointer, apply the
Reid Spencerd84d35b2007-02-15 02:26:10 +0000434/// function pointer to each element pair, producing a new ConstantVector
Reid Spencer266e42b2006-12-23 06:05:41 +0000435/// constant.
Reid Spencerd84d35b2007-02-15 02:26:10 +0000436static Constant *EvalVectorOp(const ConstantVector *V1,
437 const ConstantVector *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000438 Constant *(*FP)(Constant*, Constant*)) {
439 std::vector<Constant*> Res;
440 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
441 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
442 const_cast<Constant*>(V2->getOperand(i))));
Reid Spencerd84d35b2007-02-15 02:26:10 +0000443 return ConstantVector::get(Res);
Reid Spencer266e42b2006-12-23 06:05:41 +0000444}
445
446Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
447 const Constant *C1,
448 const Constant *C2) {
449 // Handle UndefValue up front
450 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
451 switch (Opcode) {
452 case Instruction::Add:
453 case Instruction::Sub:
454 case Instruction::Xor:
455 return UndefValue::get(C1->getType());
456 case Instruction::Mul:
457 case Instruction::And:
458 return Constant::getNullValue(C1->getType());
459 case Instruction::UDiv:
460 case Instruction::SDiv:
461 case Instruction::FDiv:
462 case Instruction::URem:
463 case Instruction::SRem:
464 case Instruction::FRem:
465 if (!isa<UndefValue>(C2)) // undef / X -> 0
466 return Constant::getNullValue(C1->getType());
467 return const_cast<Constant*>(C2); // X / undef -> undef
468 case Instruction::Or: // X | undef -> -1
Reid Spencerd84d35b2007-02-15 02:26:10 +0000469 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
470 return ConstantVector::getAllOnesValue(PTy);
Reid Spencer266e42b2006-12-23 06:05:41 +0000471 return ConstantInt::getAllOnesValue(C1->getType());
472 case Instruction::LShr:
473 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
474 return const_cast<Constant*>(C1); // undef lshr undef -> undef
475 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
476 // undef lshr X -> 0
477 case Instruction::AShr:
478 if (!isa<UndefValue>(C2))
479 return const_cast<Constant*>(C1); // undef ashr X --> undef
480 else if (isa<UndefValue>(C1))
481 return const_cast<Constant*>(C1); // undef ashr undef -> undef
482 else
483 return const_cast<Constant*>(C1); // X ashr undef --> X
484 case Instruction::Shl:
485 // undef << X -> 0 or X << undef -> 0
486 return Constant::getNullValue(C1->getType());
487 }
488 }
489
490 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
491 if (isa<ConstantExpr>(C2)) {
492 // There are many possible foldings we could do here. We should probably
493 // at least fold add of a pointer with an integer into the appropriate
494 // getelementptr. This will improve alias analysis a bit.
495 } else {
496 // Just implement a couple of simple identities.
497 switch (Opcode) {
498 case Instruction::Add:
499 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X + 0 == X
500 break;
501 case Instruction::Sub:
502 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X - 0 == X
503 break;
504 case Instruction::Mul:
505 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X * 0 == 0
506 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000507 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000508 return const_cast<Constant*>(C1); // X * 1 == X
509 break;
510 case Instruction::UDiv:
511 case Instruction::SDiv:
512 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000513 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000514 return const_cast<Constant*>(C1); // X / 1 == X
515 break;
516 case Instruction::URem:
517 case Instruction::SRem:
518 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000519 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000520 return Constant::getNullValue(CI->getType()); // X % 1 == 0
521 break;
522 case Instruction::And:
Chris Lattner26f13eb2007-01-04 01:56:39 +0000523 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
524 if (CI->isAllOnesValue())
525 return const_cast<Constant*>(C1); // X & -1 == X
Reid Spencer266e42b2006-12-23 06:05:41 +0000526 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X & 0 == 0
527 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
528 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
529
530 // Functions are at least 4-byte aligned. If and'ing the address of a
531 // function with a constant < 4, fold it to zero.
532 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000533 if (CI->getValue().ult(APInt(CI->getType()->getBitWidth(),4)) &&
534 isa<Function>(CPR))
Reid Spencer266e42b2006-12-23 06:05:41 +0000535 return Constant::getNullValue(CI->getType());
536 }
537 break;
538 case Instruction::Or:
539 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X | 0 == X
Chris Lattner26f13eb2007-01-04 01:56:39 +0000540 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
541 if (CI->isAllOnesValue())
542 return const_cast<Constant*>(C2); // X | -1 == -1
Reid Spencer266e42b2006-12-23 06:05:41 +0000543 break;
544 case Instruction::Xor:
545 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X ^ 0 == X
546 break;
547 }
548 }
549 } else if (isa<ConstantExpr>(C2)) {
550 // If C2 is a constant expr and C1 isn't, flop them around and fold the
551 // other way if possible.
552 switch (Opcode) {
553 case Instruction::Add:
554 case Instruction::Mul:
555 case Instruction::And:
556 case Instruction::Or:
557 case Instruction::Xor:
558 // No change of opcode required.
559 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
560
561 case Instruction::Shl:
562 case Instruction::LShr:
563 case Instruction::AShr:
564 case Instruction::Sub:
565 case Instruction::SDiv:
566 case Instruction::UDiv:
567 case Instruction::FDiv:
568 case Instruction::URem:
569 case Instruction::SRem:
570 case Instruction::FRem:
571 default: // These instructions cannot be flopped around.
572 return 0;
573 }
574 }
575
576 // At this point we know neither constant is an UndefValue nor a ConstantExpr
Chris Lattner26f13eb2007-01-04 01:56:39 +0000577 // so look at directly computing the value.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000578 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
579 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000580 using namespace APIntOps;
581 APInt C1V = CI1->getValue();
582 APInt C2V = CI2->getValue();
Chris Lattner344da522007-01-12 18:42:52 +0000583 switch (Opcode) {
584 default:
585 break;
586 case Instruction::Add:
Reid Spencer81658a82007-02-27 06:23:51 +0000587 return ConstantInt::get(C1->getType(), C1V + C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000588 case Instruction::Sub:
Reid Spencer81658a82007-02-27 06:23:51 +0000589 return ConstantInt::get(C1->getType(), C1V - C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000590 case Instruction::Mul:
Reid Spencer81658a82007-02-27 06:23:51 +0000591 return ConstantInt::get(C1->getType(), C1V * C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000592 case Instruction::UDiv:
Reid Spencer81658a82007-02-27 06:23:51 +0000593 if (CI2->isNullValue())
594 return 0; // X / 0 -> can't fold
595 return ConstantInt::get(C1->getType(), C1V.udiv(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000596 case Instruction::SDiv:
Reid Spencer81658a82007-02-27 06:23:51 +0000597 if (CI2->isNullValue())
598 return 0; // X / 0 -> can't fold
Reid Spencer81658a82007-02-27 06:23:51 +0000599 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
600 return 0; // MIN_INT / -1 -> overflow
601 return ConstantInt::get(C1->getType(), C1V.sdiv(C2V));
602 case Instruction::URem:
603 if (C2->isNullValue())
604 return 0; // X / 0 -> can't fold
605 return ConstantInt::get(C1->getType(), C1V.urem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000606 case Instruction::SRem:
Reid Spencer81658a82007-02-27 06:23:51 +0000607 if (CI2->isNullValue())
608 return 0; // X % 0 -> can't fold
609 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
610 return 0; // MIN_INT % -1 -> overflow
611 return ConstantInt::get(C1->getType(), C1V.srem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000612 case Instruction::And:
Reid Spencer81658a82007-02-27 06:23:51 +0000613 return ConstantInt::get(C1->getType(), C1V & C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000614 case Instruction::Or:
Reid Spencer81658a82007-02-27 06:23:51 +0000615 return ConstantInt::get(C1->getType(), C1V | C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000616 case Instruction::Xor:
Reid Spencer81658a82007-02-27 06:23:51 +0000617 return ConstantInt::get(C1->getType(), C1V ^ C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000618 case Instruction::Shl:
Reid Spencer81658a82007-02-27 06:23:51 +0000619 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000620 if (shiftAmt < C1V.getBitWidth())
Reid Spencer81658a82007-02-27 06:23:51 +0000621 return ConstantInt::get(C1->getType(), C1V.shl(shiftAmt));
622 else
623 return UndefValue::get(C1->getType()); // too big shift is undef
624 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Chris Lattner344da522007-01-12 18:42:52 +0000625 case Instruction::LShr:
Reid Spencer81658a82007-02-27 06:23:51 +0000626 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000627 if (shiftAmt < C1V.getBitWidth())
Reid Spencer81658a82007-02-27 06:23:51 +0000628 return ConstantInt::get(C1->getType(), C1V.lshr(shiftAmt));
629 else
630 return UndefValue::get(C1->getType()); // too big shift is undef
631 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Chris Lattner344da522007-01-12 18:42:52 +0000632 case Instruction::AShr:
Reid Spencer81658a82007-02-27 06:23:51 +0000633 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000634 if (shiftAmt < C1V.getBitWidth())
Reid Spencer81658a82007-02-27 06:23:51 +0000635 return ConstantInt::get(C1->getType(), C1V.ashr(shiftAmt));
636 else
637 return UndefValue::get(C1->getType()); // too big shift is undef
638 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Reid Spencer266e42b2006-12-23 06:05:41 +0000639 }
640 }
641 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
642 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
643 double C1Val = CFP1->getValue();
644 double C2Val = CFP2->getValue();
645 switch (Opcode) {
646 default:
647 break;
648 case Instruction::Add:
649 return ConstantFP::get(CFP1->getType(), C1Val + C2Val);
650 case Instruction::Sub:
651 return ConstantFP::get(CFP1->getType(), C1Val - C2Val);
652 case Instruction::Mul:
653 return ConstantFP::get(CFP1->getType(), C1Val * C2Val);
654 case Instruction::FDiv:
655 if (CFP2->isExactlyValue(0.0))
656 return ConstantFP::get(CFP1->getType(),
657 std::numeric_limits<double>::infinity());
658 if (CFP2->isExactlyValue(-0.0))
659 return ConstantFP::get(CFP1->getType(),
660 -std::numeric_limits<double>::infinity());
661 return ConstantFP::get(CFP1->getType(), C1Val / C2Val);
662 case Instruction::FRem:
663 if (CFP2->isNullValue())
664 return 0;
665 return ConstantFP::get(CFP1->getType(), std::fmod(C1Val, C2Val));
666 }
667 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000668 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
669 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000670 switch (Opcode) {
671 default:
672 break;
673 case Instruction::Add:
674 return EvalVectorOp(CP1, CP2, ConstantExpr::getAdd);
675 case Instruction::Sub:
676 return EvalVectorOp(CP1, CP2, ConstantExpr::getSub);
677 case Instruction::Mul:
678 return EvalVectorOp(CP1, CP2, ConstantExpr::getMul);
679 case Instruction::UDiv:
680 return EvalVectorOp(CP1, CP2, ConstantExpr::getUDiv);
681 case Instruction::SDiv:
682 return EvalVectorOp(CP1, CP2, ConstantExpr::getSDiv);
683 case Instruction::FDiv:
684 return EvalVectorOp(CP1, CP2, ConstantExpr::getFDiv);
685 case Instruction::URem:
686 return EvalVectorOp(CP1, CP2, ConstantExpr::getURem);
687 case Instruction::SRem:
688 return EvalVectorOp(CP1, CP2, ConstantExpr::getSRem);
689 case Instruction::FRem:
690 return EvalVectorOp(CP1, CP2, ConstantExpr::getFRem);
691 case Instruction::And:
692 return EvalVectorOp(CP1, CP2, ConstantExpr::getAnd);
693 case Instruction::Or:
694 return EvalVectorOp(CP1, CP2, ConstantExpr::getOr);
695 case Instruction::Xor:
696 return EvalVectorOp(CP1, CP2, ConstantExpr::getXor);
697 }
698 }
699 }
700
701 // We don't know how to fold this
702 return 0;
703}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000704
Chris Lattner60c47262005-01-28 19:09:51 +0000705/// isZeroSizedType - This type is zero sized if its an array or structure of
706/// zero sized types. The only leaf zero sized type is an empty structure.
707static bool isMaybeZeroSizedType(const Type *Ty) {
708 if (isa<OpaqueType>(Ty)) return true; // Can't say.
709 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
710
711 // If all of elements have zero size, this does too.
712 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000713 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000714 return true;
715
716 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
717 return isMaybeZeroSizedType(ATy->getElementType());
718 }
719 return false;
720}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000721
Chris Lattner061da2f2004-01-13 05:51:55 +0000722/// IdxCompare - Compare the two constants as though they were getelementptr
723/// indices. This allows coersion of the types to be the same thing.
724///
725/// If the two constants are the "same" (after coersion), return 0. If the
726/// first is less than the second, return -1, if the second is less than the
727/// first, return 1. If the constants are not integral, return -2.
728///
Chris Lattner60c47262005-01-28 19:09:51 +0000729static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000730 if (C1 == C2) return 0;
731
Reid Spencerc90cf772006-12-31 21:43:30 +0000732 // Ok, we found a different index. If they are not ConstantInt, we can't do
733 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +0000734 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
735 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000736
Chris Lattner69193f92004-04-05 01:30:19 +0000737 // Ok, we have two differing integer indices. Sign extend them to be the same
738 // type. Long is always big enough, so we use it.
Reid Spencer8d9336d2006-12-31 05:26:44 +0000739 if (C1->getType() != Type::Int64Ty)
740 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000741
Reid Spencer8d9336d2006-12-31 05:26:44 +0000742 if (C2->getType() != Type::Int64Ty)
Reid Spencerc90cf772006-12-31 21:43:30 +0000743 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000744
745 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +0000746
Chris Lattner60c47262005-01-28 19:09:51 +0000747 // If the type being indexed over is really just a zero sized type, there is
748 // no pointer difference being made here.
749 if (isMaybeZeroSizedType(ElTy))
750 return -2; // dunno.
751
Chris Lattner061da2f2004-01-13 05:51:55 +0000752 // If they are really different, now that they are the same type, then we
753 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +0000754 if (cast<ConstantInt>(C1)->getSExtValue() <
755 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +0000756 return -1;
757 else
758 return 1;
759}
760
Chris Lattner858f4e92007-01-04 02:13:20 +0000761/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +0000762/// decide about the two constants provided. This doesn't need to handle simple
763/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
764/// If we can determine that the two constants have a particular relation to
765/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +0000766/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
767/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +0000768///
769/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +0000770/// operand is always the most "complex" of the two. We consider ConstantFP
771/// to be the simplest, and ConstantExprs to be the most complex.
772static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1,
773 const Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000774 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +0000775 "Cannot compare values of different types!");
776 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +0000777 if (V1 == V2) return FCmpInst::FCMP_OEQ;
778
Reid Spencer9d36acf2006-12-24 18:52:08 +0000779 if (!isa<ConstantExpr>(V1)) {
780 if (!isa<ConstantExpr>(V2)) {
781 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +0000782 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000783 Constant *C1 = const_cast<Constant*>(V1);
784 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng75b871f2007-01-11 12:24:14 +0000785 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000786 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000787 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000788 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000789 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000790 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000791 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000792 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000793 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000794 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000795 if (R && !R->isNullValue())
Reid Spencer9d36acf2006-12-24 18:52:08 +0000796 return FCmpInst::FCMP_OGT;
797
798 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +0000799 return FCmpInst::BAD_FCMP_PREDICATE;
800 }
801
Reid Spencer9d36acf2006-12-24 18:52:08 +0000802 // If the first operand is simple and second is ConstantExpr, swap operands.
803 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
804 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
805 return FCmpInst::getSwappedPredicate(SwappedRelation);
806 } else {
807 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
808 // constantexpr or a simple constant.
809 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
810 switch (CE1->getOpcode()) {
811 case Instruction::FPTrunc:
812 case Instruction::FPExt:
813 case Instruction::UIToFP:
814 case Instruction::SIToFP:
815 // We might be able to do something with these but we don't right now.
816 break;
817 default:
818 break;
819 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000820 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000821 // There are MANY other foldings that we could perform here. They will
822 // probably be added on demand, as they seem needed.
823 return FCmpInst::BAD_FCMP_PREDICATE;
824}
825
826/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +0000827/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000828/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +0000829/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +0000830/// particular relation to each other, we should return the corresponding ICmp
831/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +0000832///
833/// To simplify this code we canonicalize the relation so that the first
834/// operand is always the most "complex" of the two. We consider simple
835/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000836/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000837///
Reid Spencer9d36acf2006-12-24 18:52:08 +0000838static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
839 const Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000840 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000841 assert(V1->getType() == V2->getType() &&
842 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000843 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000844
Reid Spenceraccd7c72004-07-17 23:47:01 +0000845 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000846 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
847 // We distilled this down to a simple case, use the standard constant
848 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000849 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000850 Constant *C1 = const_cast<Constant*>(V1);
851 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencer266e42b2006-12-23 06:05:41 +0000852 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000853 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000854 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000855 return pred;
856 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000857 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000858 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000859 return pred;
860 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000861 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer81658a82007-02-27 06:23:51 +0000862 if (R && !R->isNullValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000863 return pred;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000864
865 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +0000866 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000867 }
868
Chris Lattner061da2f2004-01-13 05:51:55 +0000869 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +0000870 ICmpInst::Predicate SwappedRelation =
871 evaluateICmpRelation(V2, V1, isSigned);
872 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
873 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000874
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000875 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +0000876 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +0000877 ICmpInst::Predicate SwappedRelation =
878 evaluateICmpRelation(V2, V1, isSigned);
879 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
880 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000881 else
Reid Spencer266e42b2006-12-23 06:05:41 +0000882 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +0000883 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000884
Reid Spenceraccd7c72004-07-17 23:47:01 +0000885 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000886 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000887 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000888 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000889 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000890 } else {
Reid Spencer876f7222006-12-06 00:25:09 +0000891 // GlobalVals can never be null.
Chris Lattner061da2f2004-01-13 05:51:55 +0000892 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer876f7222006-12-06 00:25:09 +0000893 if (!CPR1->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000894 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000895 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000896 } else {
897 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
898 // constantexpr, a CPR, or a simple constant.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000899 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
900 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +0000901
902 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000903 case Instruction::Trunc:
904 case Instruction::FPTrunc:
905 case Instruction::FPExt:
906 case Instruction::FPToUI:
907 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +0000908 break; // We can't evaluate floating point casts or truncations.
909
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000910 case Instruction::UIToFP:
911 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000912 case Instruction::IntToPtr:
913 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +0000914 case Instruction::ZExt:
915 case Instruction::SExt:
916 case Instruction::PtrToInt:
Chris Lattner061da2f2004-01-13 05:51:55 +0000917 // If the cast is not actually changing bits, and the second operand is a
918 // null pointer, do the comparison with the pre-casted value.
919 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000920 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000921 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000922 (CE1->getOpcode() == Instruction::SExt ? true :
923 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
924 return evaluateICmpRelation(
Reid Spencerccf78ac2006-12-23 10:21:26 +0000925 CE1Op0, Constant::getNullValue(CE1Op0->getType()), sgnd);
Reid Spencer266e42b2006-12-23 06:05:41 +0000926 }
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000927
928 // If the dest type is a pointer type, and the RHS is a constantexpr cast
929 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencer266e42b2006-12-23 06:05:41 +0000930 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000931 // which happens a lot in compilers with tagged integers.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000932 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer266e42b2006-12-23 06:05:41 +0000933 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000934 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000935 CE1->getOperand(0)->getType()->isInteger()) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000936 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000937 (CE1->getOpcode() == Instruction::SExt ? true :
938 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
939 return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0),
Reid Spencerccf78ac2006-12-23 10:21:26 +0000940 sgnd);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000941 }
Chris Lattner192e3262004-04-11 01:29:30 +0000942 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000943
944 case Instruction::GetElementPtr:
945 // Ok, since this is a getelementptr, we know that the constant has a
946 // pointer type. Check the various cases.
947 if (isa<ConstantPointerNull>(V2)) {
948 // If we are comparing a GEP to a null pointer, check to see if the base
949 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000950 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000951 if (GV->hasExternalWeakLinkage())
952 // Weak linkage GVals could be zero or not. We're comparing that
953 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000954 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +0000955 else
956 // If its not weak linkage, the GVal must have a non-zero address
957 // so the result is greater-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000958 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000959 } else if (isa<ConstantPointerNull>(CE1Op0)) {
960 // If we are indexing from a null pointer, check to see if we have any
961 // non-zero indices.
962 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
963 if (!CE1->getOperand(i)->isNullValue())
964 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000965 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000966 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +0000967 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000968 }
969 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000970 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000971 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000972 if (CPR2->hasExternalWeakLinkage())
973 // Weak linkage GVals could be zero or not. We're comparing it to
974 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000975 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +0000976 else
977 // If its not weak linkage, the GVal must have a non-zero address
978 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000979 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000980 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000981 if (CPR1 == CPR2) {
982 // If this is a getelementptr of the same global, then it must be
983 // different. Because the types must match, the getelementptr could
984 // only have at most one index, and because we fold getelementptr's
985 // with a single zero index, it must be nonzero.
986 assert(CE1->getNumOperands() == 2 &&
987 !CE1->getOperand(1)->isNullValue() &&
988 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000989 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000990 } else {
991 // If they are different globals, we don't know what the value is,
992 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000993 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000994 }
995 }
996 } else {
997 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
998 const Constant *CE2Op0 = CE2->getOperand(0);
999
1000 // There are MANY other foldings that we could perform here. They will
1001 // probably be added on demand, as they seem needed.
1002 switch (CE2->getOpcode()) {
1003 default: break;
1004 case Instruction::GetElementPtr:
1005 // By far the most common case to handle is when the base pointers are
1006 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001007 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001008 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001009 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001010 // Ok, we know that both getelementptr instructions are based on the
1011 // same global. From this, we can precisely determine the relative
1012 // ordering of the resultant pointers.
1013 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001014
Chris Lattner061da2f2004-01-13 05:51:55 +00001015 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001016 gep_type_iterator GTI = gep_type_begin(CE1);
1017 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1018 ++i, ++GTI)
1019 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1020 GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001021 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1022 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1023 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001024 }
1025
1026 // Ok, we ran out of things they have in common. If any leftovers
1027 // are non-zero then we have a difference, otherwise we are equal.
1028 for (; i < CE1->getNumOperands(); ++i)
1029 if (!CE1->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001030 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001031 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +00001032 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001033 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001034
Chris Lattner061da2f2004-01-13 05:51:55 +00001035 for (; i < CE2->getNumOperands(); ++i)
1036 if (!CE2->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001037 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001038 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001039 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001040 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1041 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001042 }
1043 }
1044 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001045 default:
1046 break;
1047 }
1048 }
1049
Reid Spencer266e42b2006-12-23 06:05:41 +00001050 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001051}
1052
Reid Spencer9d36acf2006-12-24 18:52:08 +00001053Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1054 const Constant *C1,
1055 const Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001056
1057 // Handle some degenerate cases first
1058 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Reid Spencer542964f2007-01-11 18:21:29 +00001059 return UndefValue::get(Type::Int1Ty);
Reid Spencer266e42b2006-12-23 06:05:41 +00001060
1061 // icmp eq/ne(null,GV) -> false/true
1062 if (C1->isNullValue()) {
1063 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1064 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001065 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001066 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001067 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001068 return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001069 // icmp eq/ne(GV,null) -> false/true
1070 } else if (C2->isNullValue()) {
1071 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1072 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001073 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001074 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001075 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001076 return ConstantInt::getTrue();
Chris Lattner1dd054c2004-01-12 22:07:24 +00001077 }
1078
Chris Lattner344da522007-01-12 18:42:52 +00001079 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +00001080 APInt V1 = cast<ConstantInt>(C1)->getValue();
1081 APInt V2 = cast<ConstantInt>(C2)->getValue();
1082 switch (pred) {
1083 default: assert(0 && "Invalid ICmp Predicate"); return 0;
1084 case ICmpInst::ICMP_EQ: return ConstantInt::get(Type::Int1Ty, V1 == V2);
1085 case ICmpInst::ICMP_NE: return ConstantInt::get(Type::Int1Ty, V1 != V2);
1086 case ICmpInst::ICMP_SLT:return ConstantInt::get(Type::Int1Ty, V1.slt(V2));
1087 case ICmpInst::ICMP_SGT:return ConstantInt::get(Type::Int1Ty, V1.sgt(V2));
1088 case ICmpInst::ICMP_SLE:return ConstantInt::get(Type::Int1Ty, V1.sle(V2));
1089 case ICmpInst::ICMP_SGE:return ConstantInt::get(Type::Int1Ty, V1.sge(V2));
1090 case ICmpInst::ICMP_ULT:return ConstantInt::get(Type::Int1Ty, V1.ult(V2));
1091 case ICmpInst::ICMP_UGT:return ConstantInt::get(Type::Int1Ty, V1.ugt(V2));
1092 case ICmpInst::ICMP_ULE:return ConstantInt::get(Type::Int1Ty, V1.ule(V2));
1093 case ICmpInst::ICMP_UGE:return ConstantInt::get(Type::Int1Ty, V1.uge(V2));
Chris Lattner061da2f2004-01-13 05:51:55 +00001094 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001095 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1096 double C1Val = cast<ConstantFP>(C1)->getValue();
1097 double C2Val = cast<ConstantFP>(C2)->getValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001098 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001099 default: assert(0 && "Invalid FCmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001100 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse();
1101 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001102 case FCmpInst::FCMP_UNO:
Reid Spencercddc9df2007-01-12 04:24:46 +00001103 return ConstantInt::get(Type::Int1Ty, C1Val != C1Val || C2Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001104 case FCmpInst::FCMP_ORD:
Reid Spencercddc9df2007-01-12 04:24:46 +00001105 return ConstantInt::get(Type::Int1Ty, C1Val == C1Val && C2Val == C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001106 case FCmpInst::FCMP_UEQ:
Reid Spencer74bd0362007-01-11 00:25:45 +00001107 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001108 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001109 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001110 case FCmpInst::FCMP_OEQ:
1111 return ConstantInt::get(Type::Int1Ty, C1Val == C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001112 case FCmpInst::FCMP_UNE:
1113 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001114 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001115 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001116 case FCmpInst::FCMP_ONE:
1117 return ConstantInt::get(Type::Int1Ty, C1Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001118 case FCmpInst::FCMP_ULT:
1119 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001120 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001121 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001122 case FCmpInst::FCMP_OLT:
1123 return ConstantInt::get(Type::Int1Ty, C1Val < C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001124 case FCmpInst::FCMP_UGT:
Reid Spencer74bd0362007-01-11 00:25:45 +00001125 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001126 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001127 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001128 case FCmpInst::FCMP_OGT:
1129 return ConstantInt::get(Type::Int1Ty, C1Val > C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001130 case FCmpInst::FCMP_ULE:
1131 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001132 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001133 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001134 case FCmpInst::FCMP_OLE:
1135 return ConstantInt::get(Type::Int1Ty, C1Val <= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001136 case FCmpInst::FCMP_UGE:
Reid Spencer74bd0362007-01-11 00:25:45 +00001137 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001138 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001139 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001140 case FCmpInst::FCMP_OGE:
1141 return ConstantInt::get(Type::Int1Ty, C1Val >= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001142 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001143 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
1144 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001145 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001146 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1147 Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
1148 const_cast<Constant*>(CP1->getOperand(i)),
1149 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001150 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001151 return CB;
1152 }
1153 // Otherwise, could not decide from any element pairs.
1154 return 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001155 } else if (pred == ICmpInst::ICMP_EQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001156 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1157 Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ,
1158 const_cast<Constant*>(CP1->getOperand(i)),
1159 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001160 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001161 return CB;
1162 }
1163 // Otherwise, could not decide from any element pairs.
1164 return 0;
1165 }
1166 }
1167 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001168
Reid Spencer9d36acf2006-12-24 18:52:08 +00001169 if (C1->getType()->isFloatingPoint()) {
1170 switch (evaluateFCmpRelation(C1, C2)) {
1171 default: assert(0 && "Unknown relation!");
1172 case FCmpInst::FCMP_UNO:
1173 case FCmpInst::FCMP_ORD:
1174 case FCmpInst::FCMP_UEQ:
1175 case FCmpInst::FCMP_UNE:
1176 case FCmpInst::FCMP_ULT:
1177 case FCmpInst::FCMP_UGT:
1178 case FCmpInst::FCMP_ULE:
1179 case FCmpInst::FCMP_UGE:
1180 case FCmpInst::FCMP_TRUE:
1181 case FCmpInst::FCMP_FALSE:
1182 case FCmpInst::BAD_FCMP_PREDICATE:
1183 break; // Couldn't determine anything about these constants.
1184 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001185 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001186 pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1187 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1188 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1189 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001190 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001191 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1192 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1193 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1194 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001195 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001196 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1197 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1198 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1199 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1200 // We can only partially decide this relation.
1201 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001202 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001203 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001204 return ConstantInt::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001205 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001206 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1207 // We can only partially decide this relation.
1208 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001209 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001210 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001211 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001212 break;
1213 case ICmpInst::ICMP_NE: // We know that C1 != C2
1214 // We can only partially decide this relation.
1215 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001216 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001217 if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001218 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001219 break;
1220 }
1221 } else {
1222 // Evaluate the relation between the two constants, per the predicate.
1223 switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
1224 default: assert(0 && "Unknown relational!");
1225 case ICmpInst::BAD_ICMP_PREDICATE:
1226 break; // Couldn't determine anything about these constants.
1227 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1228 // If we know the constants are equal, we can decide the result of this
1229 // computation precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001230 return ConstantInt::get(Type::Int1Ty,
1231 pred == ICmpInst::ICMP_EQ ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001232 pred == ICmpInst::ICMP_ULE ||
1233 pred == ICmpInst::ICMP_SLE ||
1234 pred == ICmpInst::ICMP_UGE ||
1235 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001236 case ICmpInst::ICMP_ULT:
1237 // If we know that C1 < C2, we can decide the result of this computation
1238 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001239 return ConstantInt::get(Type::Int1Ty,
1240 pred == ICmpInst::ICMP_ULT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001241 pred == ICmpInst::ICMP_NE ||
1242 pred == ICmpInst::ICMP_ULE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001243 case ICmpInst::ICMP_SLT:
1244 // If we know that C1 < C2, we can decide the result of this computation
1245 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001246 return ConstantInt::get(Type::Int1Ty,
1247 pred == ICmpInst::ICMP_SLT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001248 pred == ICmpInst::ICMP_NE ||
1249 pred == ICmpInst::ICMP_SLE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001250 case ICmpInst::ICMP_UGT:
1251 // If we know that C1 > C2, we can decide the result of this computation
1252 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001253 return ConstantInt::get(Type::Int1Ty,
1254 pred == ICmpInst::ICMP_UGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001255 pred == ICmpInst::ICMP_NE ||
1256 pred == ICmpInst::ICMP_UGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001257 case ICmpInst::ICMP_SGT:
1258 // If we know that C1 > C2, we can decide the result of this computation
1259 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001260 return ConstantInt::get(Type::Int1Ty,
1261 pred == ICmpInst::ICMP_SGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001262 pred == ICmpInst::ICMP_NE ||
1263 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001264 case ICmpInst::ICMP_ULE:
1265 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001266 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getFalse();
1267 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001268 break;
1269 case ICmpInst::ICMP_SLE:
1270 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001271 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getFalse();
1272 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001273 break;
1274
1275 case ICmpInst::ICMP_UGE:
1276 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001277 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getFalse();
1278 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001279 break;
1280 case ICmpInst::ICMP_SGE:
1281 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001282 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getFalse();
1283 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001284 break;
1285
1286 case ICmpInst::ICMP_NE:
1287 // If we know that C1 != C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001288 if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse();
1289 if (pred == ICmpInst::ICMP_NE) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001290 break;
1291 }
1292
1293 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
1294 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1295 // other way if possible.
1296 switch (pred) {
1297 case ICmpInst::ICMP_EQ:
1298 case ICmpInst::ICMP_NE:
1299 // No change of predicate required.
1300 return ConstantFoldCompareInstruction(pred, C2, C1);
1301
1302 case ICmpInst::ICMP_ULT:
1303 case ICmpInst::ICMP_SLT:
1304 case ICmpInst::ICMP_UGT:
1305 case ICmpInst::ICMP_SGT:
1306 case ICmpInst::ICMP_ULE:
1307 case ICmpInst::ICMP_SLE:
1308 case ICmpInst::ICMP_UGE:
1309 case ICmpInst::ICMP_SGE:
1310 // Change the predicate as necessary to swap the operands.
1311 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1312 return ConstantFoldCompareInstruction(pred, C2, C1);
1313
1314 default: // These predicates cannot be flopped around.
1315 break;
1316 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001317 }
1318 }
1319 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001320}
1321
1322Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001323 Constant* const *Idxs,
1324 unsigned NumIdx) {
1325 if (NumIdx == 0 ||
1326 (NumIdx == 1 && Idxs[0]->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001327 return const_cast<Constant*>(C);
1328
Chris Lattnerf6013752004-10-17 21:54:55 +00001329 if (isa<UndefValue>(C)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001330 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1331 (Value**)Idxs, NumIdx,
Chris Lattnerf6013752004-10-17 21:54:55 +00001332 true);
1333 assert(Ty != 0 && "Invalid indices for GEP!");
1334 return UndefValue::get(PointerType::get(Ty));
1335 }
1336
Chris Lattner302116a2007-01-31 04:40:28 +00001337 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001338 if (C->isNullValue()) {
1339 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001340 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1341 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001342 isNull = false;
1343 break;
1344 }
1345 if (isNull) {
Chris Lattner302116a2007-01-31 04:40:28 +00001346 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1347 (Value**)Idxs, NumIdx,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001348 true);
1349 assert(Ty != 0 && "Invalid indices for GEP!");
1350 return ConstantPointerNull::get(PointerType::get(Ty));
1351 }
1352 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001353
1354 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1355 // Combine Indices - If the source pointer to this getelementptr instruction
1356 // is a getelementptr instruction, combine the indices of the two
1357 // getelementptr instructions into a single instruction.
1358 //
1359 if (CE->getOpcode() == Instruction::GetElementPtr) {
1360 const Type *LastTy = 0;
1361 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1362 I != E; ++I)
1363 LastTy = *I;
1364
Chris Lattner13128ab2004-10-11 22:52:25 +00001365 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001366 SmallVector<Value*, 16> NewIndices;
1367 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001368 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001369 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001370
1371 // Add the last index of the source with the first index of the new GEP.
1372 // Make sure to handle the case when they are actually different types.
1373 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001374 // Otherwise it must be an array.
1375 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001376 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001377 if (IdxTy != Idx0->getType()) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001378 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Reid Spencer27720a92006-12-05 03:30:09 +00001379 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001380 Type::Int64Ty);
Reid Spencer1a063892006-12-04 02:46:44 +00001381 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1382 } else {
1383 Combined =
1384 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1385 }
Chris Lattner71068a02004-07-07 04:45:13 +00001386 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001387
Chris Lattner1dd054c2004-01-12 22:07:24 +00001388 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001389 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
1390 return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
1391 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001392 }
1393 }
1394
1395 // Implement folding of:
1396 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1397 // long 0, long 0)
1398 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1399 //
Chris Lattner302116a2007-01-31 04:40:28 +00001400 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001401 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001402 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1403 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1404 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001405 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001406 if (CAT->getElementType() == SAT->getElementType())
1407 return ConstantExpr::getGetElementPtr(
Chris Lattner302116a2007-01-31 04:40:28 +00001408 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001409 }
1410 return 0;
1411}
1412