blob: 7cb4b7bfbe51d58a13937ad3a650b24134f36d26 [file] [log] [blame]
Chris Lattner5a945e32004-01-12 21:13:12 +00001//===- ConstantFolding.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
11// (internal) ConstantFolding.h interface, which is used by the
12// 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 Lattner5a945e32004-01-12 21:13:12 +000021#include "ConstantFolding.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 Spencerd84d35b2007-02-15 02:26:10 +000041static Constant *CastConstantVector(ConstantVector *CP,
42 const VectorType *DstTy) {
Chris Lattner6b3f4752006-04-02 01:38:28 +000043 unsigned SrcNumElts = CP->getType()->getNumElements();
44 unsigned DstNumElts = DstTy->getNumElements();
45 const Type *SrcEltTy = CP->getType()->getElementType();
46 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 Spencerbb65ebf2006-12-12 23:36:14 +000059 ConstantExpr::getBitCast(CP->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 Spencere0fc4df2006-10-20 07:07:24 +000070 BitsToDouble(cast<ConstantInt>(CP->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 Spencere0fc4df2006-10-20 07:07:24 +000078 BitsToFloat(cast<ConstantInt>(CP->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) {
89 uint64_t V =
90 DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
Reid Spencer8d9336d2006-12-31 05:26:44 +000091 Constant *C = ConstantInt::get(Type::Int64Ty, 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 Spencere0fc4df2006-10-20 07:07:24 +000099 uint32_t V = FloatToBits(cast<ConstantFP>(CP->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:
177 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
Zhou Sheng75b871f2007-01-11 12:24:14 +0000178 return ConstantInt::get(DestTy,(uint64_t) FPC->getValue());
Reid Spencer8dabca42006-12-19 07:41:40 +0000179 return 0; // Can't fold.
180 case Instruction::FPToSI:
181 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
Zhou Sheng75b871f2007-01-11 12:24:14 +0000182 return ConstantInt::get(DestTy,(int64_t) FPC->getValue());
Reid Spencer8dabca42006-12-19 07:41:40 +0000183 return 0; // Can't fold.
184 case Instruction::IntToPtr: //always treated as unsigned
185 if (V->isNullValue()) // Is it an integral null value?
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000186 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Reid Spencer8dabca42006-12-19 07:41:40 +0000187 return 0; // Other pointer types cannot be casted
188 case Instruction::PtrToInt: // always treated as unsigned
189 if (V->isNullValue()) // is it a null pointer value?
Zhou Sheng75b871f2007-01-11 12:24:14 +0000190 return ConstantInt::get(DestTy, 0);
Reid Spencer8dabca42006-12-19 07:41:40 +0000191 return 0; // Other pointer types cannot be casted
192 case Instruction::UIToFP:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000193 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer8dabca42006-12-19 07:41:40 +0000194 return ConstantFP::get(DestTy, double(CI->getZExtValue()));
195 return 0;
196 case Instruction::SIToFP:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000197 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer8dabca42006-12-19 07:41:40 +0000198 return ConstantFP::get(DestTy, double(CI->getSExtValue()));
199 return 0;
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000200 case Instruction::ZExt:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000201 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer8dabca42006-12-19 07:41:40 +0000202 return ConstantInt::get(DestTy, CI->getZExtValue());
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000203 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000204 case Instruction::SExt:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000205 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer8dabca42006-12-19 07:41:40 +0000206 return ConstantInt::get(DestTy, CI->getSExtValue());
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000207 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000208 case Instruction::Trunc:
Reid Spencer8dabca42006-12-19 07:41:40 +0000209 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) // Can't trunc a bool
Zhou Sheng75b871f2007-01-11 12:24:14 +0000210 return ConstantInt::get(DestTy, CI->getZExtValue());
Chris Lattner710ebaf2006-12-01 19:22:41 +0000211 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000212 case Instruction::BitCast:
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000213 if (SrcTy == DestTy)
214 return (Constant*)V; // no-op cast
Chris Lattner4d1da162006-12-11 18:30:27 +0000215
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000216 // Check to see if we are casting a pointer to an aggregate to a pointer to
217 // the first element. If so, return the appropriate GEP instruction.
218 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
219 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000220 SmallVector<Value*, 8> IdxList;
Reid Spencer8d9336d2006-12-31 05:26:44 +0000221 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000222 const Type *ElTy = PTy->getElementType();
223 while (ElTy != DPTy->getElementType()) {
224 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
225 if (STy->getNumElements() == 0) break;
226 ElTy = STy->getElementType(0);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000227 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000228 } else if (const SequentialType *STy =
229 dyn_cast<SequentialType>(ElTy)) {
230 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
231 ElTy = STy->getElementType();
232 IdxList.push_back(IdxList[0]);
233 } else {
Chris Lattner6b3f4752006-04-02 01:38:28 +0000234 break;
235 }
236 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000237
238 if (ElTy == DPTy->getElementType())
239 return ConstantExpr::getGetElementPtr(
Chris Lattner302116a2007-01-31 04:40:28 +0000240 const_cast<Constant*>(V), &IdxList[0], IdxList.size());
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000241 }
242
243 // Handle casts from one packed constant to another. We know that the src
244 // and dest type have the same size (otherwise its an illegal cast).
Reid Spencerd84d35b2007-02-15 02:26:10 +0000245 if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
246 if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000247 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
248 "Not cast between same sized vectors!");
249 // First, check for null and undef
250 if (isa<ConstantAggregateZero>(V))
251 return Constant::getNullValue(DestTy);
252 if (isa<UndefValue>(V))
253 return UndefValue::get(DestTy);
254
Reid Spencerd84d35b2007-02-15 02:26:10 +0000255 if (const ConstantVector *CP = dyn_cast<ConstantVector>(V)) {
256 // This is a cast from a ConstantVector of one type to a
257 // ConstantVector of another type. Check to see if all elements of
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000258 // the input are simple.
259 bool AllSimpleConstants = true;
260 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
261 if (!isa<ConstantInt>(CP->getOperand(i)) &&
262 !isa<ConstantFP>(CP->getOperand(i))) {
263 AllSimpleConstants = false;
264 break;
265 }
266 }
267
268 // If all of the elements are simple constants, we can fold this.
269 if (AllSimpleConstants)
Reid Spencerd84d35b2007-02-15 02:26:10 +0000270 return CastConstantVector(const_cast<ConstantVector*>(CP), DestPTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000271 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000272 }
273 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000274
Chris Lattner4d1da162006-12-11 18:30:27 +0000275 // Finally, implement bitcast folding now. The code below doesn't handle
276 // bitcast right.
277 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
278 return ConstantPointerNull::get(cast<PointerType>(DestTy));
279
280 // Handle integral constant input.
281 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
282 // Integral -> Integral, must be changing sign.
Chris Lattner03c49532007-01-15 02:27:26 +0000283 if (DestTy->isInteger())
Chris Lattner4d1da162006-12-11 18:30:27 +0000284 return ConstantInt::get(DestTy, CI->getZExtValue());
285
286 if (DestTy->isFloatingPoint()) {
287 if (DestTy == Type::FloatTy)
288 return ConstantFP::get(DestTy, BitsToFloat(CI->getZExtValue()));
289 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
290 return ConstantFP::get(DestTy, BitsToDouble(CI->getZExtValue()));
291 }
292 // Otherwise, can't fold this (packed?)
293 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000294 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000295
296 // Handle ConstantFP input.
297 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
298 // FP -> Integral.
Chris Lattnere62c89a2007-02-06 02:22:56 +0000299 if (DestTy == Type::Int32Ty) {
300 return ConstantInt::get(DestTy, FloatToBits(FP->getValue()));
301 } else {
302 assert(DestTy == Type::Int64Ty && "only support f32/f64 for now!");
Chris Lattner4d1da162006-12-11 18:30:27 +0000303 return ConstantInt::get(DestTy, DoubleToBits(FP->getValue()));
Chris Lattnere62c89a2007-02-06 02:22:56 +0000304 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000305 }
306 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000307 default:
308 assert(!"Invalid CE CastInst opcode");
309 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000310 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000311
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000312 assert(0 && "Failed to cast constant expression");
313 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000314}
315
Chris Lattner6ea4b522004-03-12 05:53:32 +0000316Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
317 const Constant *V1,
318 const Constant *V2) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000319 if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
Reid Spencercddc9df2007-01-12 04:24:46 +0000320 return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000321
322 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
323 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
324 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000325 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000326 return 0;
327}
328
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000329Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
330 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000331 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
Reid Spencerd84d35b2007-02-15 02:26:10 +0000332 return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000333 if (Val->isNullValue()) // ee(zero, x) -> zero
334 return Constant::getNullValue(
Reid Spencerd84d35b2007-02-15 02:26:10 +0000335 cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000336
Reid Spencerd84d35b2007-02-15 02:26:10 +0000337 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000338 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
339 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000340 } else if (isa<UndefValue>(Idx)) {
341 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
342 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000343 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000344 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000345 return 0;
346}
347
Robert Bocchinoca27f032006-01-17 20:07:22 +0000348Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
349 const Constant *Elt,
350 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000351 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000352 if (!CIdx) return 0;
Reid Spencere0fc4df2006-10-20 07:07:24 +0000353 uint64_t idxVal = CIdx->getZExtValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000354 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000355 // Insertion of scalar constant into packed undef
356 // Optimize away insertion of undef
357 if (isa<UndefValue>(Elt))
358 return const_cast<Constant*>(Val);
359 // Otherwise break the aggregate undef into multiple undefs and do
360 // the insertion
361 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000362 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000363 std::vector<Constant*> Ops;
364 Ops.reserve(numOps);
365 for (unsigned i = 0; i < numOps; ++i) {
366 const Constant *Op =
367 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
368 Ops.push_back(const_cast<Constant*>(Op));
369 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000370 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000371 }
Reid Spencer3054b142006-11-02 08:18:15 +0000372 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000373 // Insertion of scalar constant into packed aggregate zero
374 // Optimize away insertion of zero
375 if (Elt->isNullValue())
376 return const_cast<Constant*>(Val);
377 // Otherwise break the aggregate zero into multiple zeros and do
378 // the insertion
379 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000380 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000381 std::vector<Constant*> Ops;
382 Ops.reserve(numOps);
383 for (unsigned i = 0; i < numOps; ++i) {
384 const Constant *Op =
385 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
386 Ops.push_back(const_cast<Constant*>(Op));
387 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000388 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000389 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000390 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000391 // Insertion of scalar constant into packed constant
392 std::vector<Constant*> Ops;
393 Ops.reserve(CVal->getNumOperands());
394 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
395 const Constant *Op =
396 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
397 Ops.push_back(const_cast<Constant*>(Op));
398 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000399 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000400 }
401 return 0;
402}
403
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000404Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
405 const Constant *V2,
406 const Constant *Mask) {
407 // TODO:
408 return 0;
409}
410
Reid Spencer266e42b2006-12-23 06:05:41 +0000411/// EvalVectorOp - Given two packed constants and a function pointer, apply the
Reid Spencerd84d35b2007-02-15 02:26:10 +0000412/// function pointer to each element pair, producing a new ConstantVector
Reid Spencer266e42b2006-12-23 06:05:41 +0000413/// constant.
Reid Spencerd84d35b2007-02-15 02:26:10 +0000414static Constant *EvalVectorOp(const ConstantVector *V1,
415 const ConstantVector *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000416 Constant *(*FP)(Constant*, Constant*)) {
417 std::vector<Constant*> Res;
418 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
419 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
420 const_cast<Constant*>(V2->getOperand(i))));
Reid Spencerd84d35b2007-02-15 02:26:10 +0000421 return ConstantVector::get(Res);
Reid Spencer266e42b2006-12-23 06:05:41 +0000422}
423
424Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
425 const Constant *C1,
426 const Constant *C2) {
427 // Handle UndefValue up front
428 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
429 switch (Opcode) {
430 case Instruction::Add:
431 case Instruction::Sub:
432 case Instruction::Xor:
433 return UndefValue::get(C1->getType());
434 case Instruction::Mul:
435 case Instruction::And:
436 return Constant::getNullValue(C1->getType());
437 case Instruction::UDiv:
438 case Instruction::SDiv:
439 case Instruction::FDiv:
440 case Instruction::URem:
441 case Instruction::SRem:
442 case Instruction::FRem:
443 if (!isa<UndefValue>(C2)) // undef / X -> 0
444 return Constant::getNullValue(C1->getType());
445 return const_cast<Constant*>(C2); // X / undef -> undef
446 case Instruction::Or: // X | undef -> -1
Reid Spencerd84d35b2007-02-15 02:26:10 +0000447 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
448 return ConstantVector::getAllOnesValue(PTy);
Reid Spencer266e42b2006-12-23 06:05:41 +0000449 return ConstantInt::getAllOnesValue(C1->getType());
450 case Instruction::LShr:
451 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
452 return const_cast<Constant*>(C1); // undef lshr undef -> undef
453 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
454 // undef lshr X -> 0
455 case Instruction::AShr:
456 if (!isa<UndefValue>(C2))
457 return const_cast<Constant*>(C1); // undef ashr X --> undef
458 else if (isa<UndefValue>(C1))
459 return const_cast<Constant*>(C1); // undef ashr undef -> undef
460 else
461 return const_cast<Constant*>(C1); // X ashr undef --> X
462 case Instruction::Shl:
463 // undef << X -> 0 or X << undef -> 0
464 return Constant::getNullValue(C1->getType());
465 }
466 }
467
468 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
469 if (isa<ConstantExpr>(C2)) {
470 // There are many possible foldings we could do here. We should probably
471 // at least fold add of a pointer with an integer into the appropriate
472 // getelementptr. This will improve alias analysis a bit.
473 } else {
474 // Just implement a couple of simple identities.
475 switch (Opcode) {
476 case Instruction::Add:
477 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X + 0 == X
478 break;
479 case Instruction::Sub:
480 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X - 0 == X
481 break;
482 case Instruction::Mul:
483 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X * 0 == 0
484 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
485 if (CI->getZExtValue() == 1)
486 return const_cast<Constant*>(C1); // X * 1 == X
487 break;
488 case Instruction::UDiv:
489 case Instruction::SDiv:
490 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
491 if (CI->getZExtValue() == 1)
492 return const_cast<Constant*>(C1); // X / 1 == X
493 break;
494 case Instruction::URem:
495 case Instruction::SRem:
496 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
497 if (CI->getZExtValue() == 1)
498 return Constant::getNullValue(CI->getType()); // X % 1 == 0
499 break;
500 case Instruction::And:
Chris Lattner26f13eb2007-01-04 01:56:39 +0000501 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
502 if (CI->isAllOnesValue())
503 return const_cast<Constant*>(C1); // X & -1 == X
Reid Spencer266e42b2006-12-23 06:05:41 +0000504 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X & 0 == 0
505 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
506 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
507
508 // Functions are at least 4-byte aligned. If and'ing the address of a
509 // function with a constant < 4, fold it to zero.
510 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
511 if (CI->getZExtValue() < 4 && isa<Function>(CPR))
512 return Constant::getNullValue(CI->getType());
513 }
514 break;
515 case Instruction::Or:
516 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X | 0 == X
Chris Lattner26f13eb2007-01-04 01:56:39 +0000517 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
518 if (CI->isAllOnesValue())
519 return const_cast<Constant*>(C2); // X | -1 == -1
Reid Spencer266e42b2006-12-23 06:05:41 +0000520 break;
521 case Instruction::Xor:
522 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X ^ 0 == X
523 break;
524 }
525 }
526 } else if (isa<ConstantExpr>(C2)) {
527 // If C2 is a constant expr and C1 isn't, flop them around and fold the
528 // other way if possible.
529 switch (Opcode) {
530 case Instruction::Add:
531 case Instruction::Mul:
532 case Instruction::And:
533 case Instruction::Or:
534 case Instruction::Xor:
535 // No change of opcode required.
536 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
537
538 case Instruction::Shl:
539 case Instruction::LShr:
540 case Instruction::AShr:
541 case Instruction::Sub:
542 case Instruction::SDiv:
543 case Instruction::UDiv:
544 case Instruction::FDiv:
545 case Instruction::URem:
546 case Instruction::SRem:
547 case Instruction::FRem:
548 default: // These instructions cannot be flopped around.
549 return 0;
550 }
551 }
552
553 // At this point we know neither constant is an UndefValue nor a ConstantExpr
Chris Lattner26f13eb2007-01-04 01:56:39 +0000554 // so look at directly computing the value.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000555 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
556 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Chris Lattner344da522007-01-12 18:42:52 +0000557 uint64_t C1Val = CI1->getZExtValue();
558 uint64_t C2Val = CI2->getZExtValue();
559 switch (Opcode) {
560 default:
561 break;
562 case Instruction::Add:
563 return ConstantInt::get(C1->getType(), C1Val + C2Val);
564 case Instruction::Sub:
565 return ConstantInt::get(C1->getType(), C1Val - C2Val);
566 case Instruction::Mul:
567 return ConstantInt::get(C1->getType(), C1Val * C2Val);
568 case Instruction::UDiv:
569 if (CI2->isNullValue()) // X / 0 -> can't fold
570 return 0;
571 return ConstantInt::get(C1->getType(), C1Val / C2Val);
572 case Instruction::SDiv:
573 if (CI2->isNullValue()) return 0; // X / 0 -> can't fold
574 if (CI2->isAllOnesValue() &&
575 (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
576 (CI1->getSExtValue() == INT64_MIN)) ||
577 (CI1->getSExtValue() == -CI1->getSExtValue())))
578 return 0; // MIN_INT / -1 -> overflow
579 return ConstantInt::get(C1->getType(),
580 CI1->getSExtValue() / CI2->getSExtValue());
581 case Instruction::URem:
582 if (C2->isNullValue()) return 0; // X / 0 -> can't fold
583 return ConstantInt::get(C1->getType(), C1Val % C2Val);
584 case Instruction::SRem:
585 if (CI2->isNullValue()) return 0; // X % 0 -> can't fold
586 if (CI2->isAllOnesValue() &&
587 (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
588 (CI1->getSExtValue() == INT64_MIN)) ||
589 (CI1->getSExtValue() == -CI1->getSExtValue())))
590 return 0; // MIN_INT % -1 -> overflow
591 return ConstantInt::get(C1->getType(),
592 CI1->getSExtValue() % CI2->getSExtValue());
593 case Instruction::And:
594 return ConstantInt::get(C1->getType(), C1Val & C2Val);
595 case Instruction::Or:
596 return ConstantInt::get(C1->getType(), C1Val | C2Val);
597 case Instruction::Xor:
598 return ConstantInt::get(C1->getType(), C1Val ^ C2Val);
599 case Instruction::Shl:
600 return ConstantInt::get(C1->getType(), C1Val << C2Val);
601 case Instruction::LShr:
602 return ConstantInt::get(C1->getType(), C1Val >> C2Val);
603 case Instruction::AShr:
604 return ConstantInt::get(C1->getType(),
605 CI1->getSExtValue() >> C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +0000606 }
607 }
608 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
609 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
610 double C1Val = CFP1->getValue();
611 double C2Val = CFP2->getValue();
612 switch (Opcode) {
613 default:
614 break;
615 case Instruction::Add:
616 return ConstantFP::get(CFP1->getType(), C1Val + C2Val);
617 case Instruction::Sub:
618 return ConstantFP::get(CFP1->getType(), C1Val - C2Val);
619 case Instruction::Mul:
620 return ConstantFP::get(CFP1->getType(), C1Val * C2Val);
621 case Instruction::FDiv:
622 if (CFP2->isExactlyValue(0.0))
623 return ConstantFP::get(CFP1->getType(),
624 std::numeric_limits<double>::infinity());
625 if (CFP2->isExactlyValue(-0.0))
626 return ConstantFP::get(CFP1->getType(),
627 -std::numeric_limits<double>::infinity());
628 return ConstantFP::get(CFP1->getType(), C1Val / C2Val);
629 case Instruction::FRem:
630 if (CFP2->isNullValue())
631 return 0;
632 return ConstantFP::get(CFP1->getType(), std::fmod(C1Val, C2Val));
633 }
634 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000635 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
636 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000637 switch (Opcode) {
638 default:
639 break;
640 case Instruction::Add:
641 return EvalVectorOp(CP1, CP2, ConstantExpr::getAdd);
642 case Instruction::Sub:
643 return EvalVectorOp(CP1, CP2, ConstantExpr::getSub);
644 case Instruction::Mul:
645 return EvalVectorOp(CP1, CP2, ConstantExpr::getMul);
646 case Instruction::UDiv:
647 return EvalVectorOp(CP1, CP2, ConstantExpr::getUDiv);
648 case Instruction::SDiv:
649 return EvalVectorOp(CP1, CP2, ConstantExpr::getSDiv);
650 case Instruction::FDiv:
651 return EvalVectorOp(CP1, CP2, ConstantExpr::getFDiv);
652 case Instruction::URem:
653 return EvalVectorOp(CP1, CP2, ConstantExpr::getURem);
654 case Instruction::SRem:
655 return EvalVectorOp(CP1, CP2, ConstantExpr::getSRem);
656 case Instruction::FRem:
657 return EvalVectorOp(CP1, CP2, ConstantExpr::getFRem);
658 case Instruction::And:
659 return EvalVectorOp(CP1, CP2, ConstantExpr::getAnd);
660 case Instruction::Or:
661 return EvalVectorOp(CP1, CP2, ConstantExpr::getOr);
662 case Instruction::Xor:
663 return EvalVectorOp(CP1, CP2, ConstantExpr::getXor);
664 }
665 }
666 }
667
668 // We don't know how to fold this
669 return 0;
670}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000671
Chris Lattner60c47262005-01-28 19:09:51 +0000672/// isZeroSizedType - This type is zero sized if its an array or structure of
673/// zero sized types. The only leaf zero sized type is an empty structure.
674static bool isMaybeZeroSizedType(const Type *Ty) {
675 if (isa<OpaqueType>(Ty)) return true; // Can't say.
676 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
677
678 // If all of elements have zero size, this does too.
679 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000680 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000681 return true;
682
683 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
684 return isMaybeZeroSizedType(ATy->getElementType());
685 }
686 return false;
687}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000688
Chris Lattner061da2f2004-01-13 05:51:55 +0000689/// IdxCompare - Compare the two constants as though they were getelementptr
690/// indices. This allows coersion of the types to be the same thing.
691///
692/// If the two constants are the "same" (after coersion), return 0. If the
693/// first is less than the second, return -1, if the second is less than the
694/// first, return 1. If the constants are not integral, return -2.
695///
Chris Lattner60c47262005-01-28 19:09:51 +0000696static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000697 if (C1 == C2) return 0;
698
Reid Spencerc90cf772006-12-31 21:43:30 +0000699 // Ok, we found a different index. If they are not ConstantInt, we can't do
700 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +0000701 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
702 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000703
Chris Lattner69193f92004-04-05 01:30:19 +0000704 // Ok, we have two differing integer indices. Sign extend them to be the same
705 // type. Long is always big enough, so we use it.
Reid Spencer8d9336d2006-12-31 05:26:44 +0000706 if (C1->getType() != Type::Int64Ty)
707 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000708
Reid Spencer8d9336d2006-12-31 05:26:44 +0000709 if (C2->getType() != Type::Int64Ty)
Reid Spencerc90cf772006-12-31 21:43:30 +0000710 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000711
712 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +0000713
Chris Lattner60c47262005-01-28 19:09:51 +0000714 // If the type being indexed over is really just a zero sized type, there is
715 // no pointer difference being made here.
716 if (isMaybeZeroSizedType(ElTy))
717 return -2; // dunno.
718
Chris Lattner061da2f2004-01-13 05:51:55 +0000719 // If they are really different, now that they are the same type, then we
720 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +0000721 if (cast<ConstantInt>(C1)->getSExtValue() <
722 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +0000723 return -1;
724 else
725 return 1;
726}
727
Chris Lattner858f4e92007-01-04 02:13:20 +0000728/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +0000729/// decide about the two constants provided. This doesn't need to handle simple
730/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
731/// If we can determine that the two constants have a particular relation to
732/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +0000733/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
734/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +0000735///
736/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +0000737/// operand is always the most "complex" of the two. We consider ConstantFP
738/// to be the simplest, and ConstantExprs to be the most complex.
739static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1,
740 const Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000741 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +0000742 "Cannot compare values of different types!");
743 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +0000744 if (V1 == V2) return FCmpInst::FCMP_OEQ;
745
Reid Spencer9d36acf2006-12-24 18:52:08 +0000746 if (!isa<ConstantExpr>(V1)) {
747 if (!isa<ConstantExpr>(V2)) {
748 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +0000749 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000750 Constant *C1 = const_cast<Constant*>(V1);
751 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng75b871f2007-01-11 12:24:14 +0000752 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000753 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000754 if (R && R->getZExtValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000755 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000756 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000757 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000758 if (R && R->getZExtValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000759 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000760 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000761 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000762 if (R && R->getZExtValue())
Reid Spencer9d36acf2006-12-24 18:52:08 +0000763 return FCmpInst::FCMP_OGT;
764
765 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +0000766 return FCmpInst::BAD_FCMP_PREDICATE;
767 }
768
Reid Spencer9d36acf2006-12-24 18:52:08 +0000769 // If the first operand is simple and second is ConstantExpr, swap operands.
770 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
771 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
772 return FCmpInst::getSwappedPredicate(SwappedRelation);
773 } else {
774 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
775 // constantexpr or a simple constant.
776 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
777 switch (CE1->getOpcode()) {
778 case Instruction::FPTrunc:
779 case Instruction::FPExt:
780 case Instruction::UIToFP:
781 case Instruction::SIToFP:
782 // We might be able to do something with these but we don't right now.
783 break;
784 default:
785 break;
786 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000787 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000788 // There are MANY other foldings that we could perform here. They will
789 // probably be added on demand, as they seem needed.
790 return FCmpInst::BAD_FCMP_PREDICATE;
791}
792
793/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +0000794/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000795/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +0000796/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +0000797/// particular relation to each other, we should return the corresponding ICmp
798/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +0000799///
800/// To simplify this code we canonicalize the relation so that the first
801/// operand is always the most "complex" of the two. We consider simple
802/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000803/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000804///
Reid Spencer9d36acf2006-12-24 18:52:08 +0000805static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
806 const Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000807 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000808 assert(V1->getType() == V2->getType() &&
809 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000810 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000811
Reid Spenceraccd7c72004-07-17 23:47:01 +0000812 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000813 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
814 // We distilled this down to a simple case, use the standard constant
815 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000816 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000817 Constant *C1 = const_cast<Constant*>(V1);
818 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencer266e42b2006-12-23 06:05:41 +0000819 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000820 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000821 if (R && R->getZExtValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000822 return pred;
823 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000824 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000825 if (R && R->getZExtValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000826 return pred;
827 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000828 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000829 if (R && R->getZExtValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000830 return pred;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000831
832 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +0000833 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000834 }
835
Chris Lattner061da2f2004-01-13 05:51:55 +0000836 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +0000837 ICmpInst::Predicate SwappedRelation =
838 evaluateICmpRelation(V2, V1, isSigned);
839 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
840 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000841
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000842 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +0000843 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +0000844 ICmpInst::Predicate SwappedRelation =
845 evaluateICmpRelation(V2, V1, isSigned);
846 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
847 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000848 else
Reid Spencer266e42b2006-12-23 06:05:41 +0000849 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +0000850 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000851
Reid Spenceraccd7c72004-07-17 23:47:01 +0000852 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000853 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000854 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000855 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000856 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000857 } else {
Reid Spencer876f7222006-12-06 00:25:09 +0000858 // GlobalVals can never be null.
Chris Lattner061da2f2004-01-13 05:51:55 +0000859 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer876f7222006-12-06 00:25:09 +0000860 if (!CPR1->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000861 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000862 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000863 } else {
864 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
865 // constantexpr, a CPR, or a simple constant.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000866 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
867 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +0000868
869 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000870 case Instruction::Trunc:
871 case Instruction::FPTrunc:
872 case Instruction::FPExt:
873 case Instruction::FPToUI:
874 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +0000875 break; // We can't evaluate floating point casts or truncations.
876
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000877 case Instruction::UIToFP:
878 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000879 case Instruction::IntToPtr:
880 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +0000881 case Instruction::ZExt:
882 case Instruction::SExt:
883 case Instruction::PtrToInt:
Chris Lattner061da2f2004-01-13 05:51:55 +0000884 // If the cast is not actually changing bits, and the second operand is a
885 // null pointer, do the comparison with the pre-casted value.
886 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000887 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000888 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000889 (CE1->getOpcode() == Instruction::SExt ? true :
890 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
891 return evaluateICmpRelation(
Reid Spencerccf78ac2006-12-23 10:21:26 +0000892 CE1Op0, Constant::getNullValue(CE1Op0->getType()), sgnd);
Reid Spencer266e42b2006-12-23 06:05:41 +0000893 }
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000894
895 // If the dest type is a pointer type, and the RHS is a constantexpr cast
896 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencer266e42b2006-12-23 06:05:41 +0000897 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000898 // which happens a lot in compilers with tagged integers.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000899 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer266e42b2006-12-23 06:05:41 +0000900 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000901 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000902 CE1->getOperand(0)->getType()->isInteger()) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000903 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000904 (CE1->getOpcode() == Instruction::SExt ? true :
905 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
906 return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0),
Reid Spencerccf78ac2006-12-23 10:21:26 +0000907 sgnd);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000908 }
Chris Lattner192e3262004-04-11 01:29:30 +0000909 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000910
911 case Instruction::GetElementPtr:
912 // Ok, since this is a getelementptr, we know that the constant has a
913 // pointer type. Check the various cases.
914 if (isa<ConstantPointerNull>(V2)) {
915 // If we are comparing a GEP to a null pointer, check to see if the base
916 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000917 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000918 if (GV->hasExternalWeakLinkage())
919 // Weak linkage GVals could be zero or not. We're comparing that
920 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000921 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +0000922 else
923 // If its not weak linkage, the GVal must have a non-zero address
924 // so the result is greater-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000925 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000926 } else if (isa<ConstantPointerNull>(CE1Op0)) {
927 // If we are indexing from a null pointer, check to see if we have any
928 // non-zero indices.
929 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
930 if (!CE1->getOperand(i)->isNullValue())
931 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000932 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000933 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +0000934 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000935 }
936 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000937 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000938 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000939 if (CPR2->hasExternalWeakLinkage())
940 // Weak linkage GVals could be zero or not. We're comparing it to
941 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000942 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +0000943 else
944 // If its not weak linkage, the GVal must have a non-zero address
945 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000946 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000947 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000948 if (CPR1 == CPR2) {
949 // If this is a getelementptr of the same global, then it must be
950 // different. Because the types must match, the getelementptr could
951 // only have at most one index, and because we fold getelementptr's
952 // with a single zero index, it must be nonzero.
953 assert(CE1->getNumOperands() == 2 &&
954 !CE1->getOperand(1)->isNullValue() &&
955 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000956 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000957 } else {
958 // If they are different globals, we don't know what the value is,
959 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000960 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000961 }
962 }
963 } else {
964 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
965 const Constant *CE2Op0 = CE2->getOperand(0);
966
967 // There are MANY other foldings that we could perform here. They will
968 // probably be added on demand, as they seem needed.
969 switch (CE2->getOpcode()) {
970 default: break;
971 case Instruction::GetElementPtr:
972 // By far the most common case to handle is when the base pointers are
973 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000974 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000975 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000976 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000977 // Ok, we know that both getelementptr instructions are based on the
978 // same global. From this, we can precisely determine the relative
979 // ordering of the resultant pointers.
980 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000981
Chris Lattner061da2f2004-01-13 05:51:55 +0000982 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +0000983 gep_type_iterator GTI = gep_type_begin(CE1);
984 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
985 ++i, ++GTI)
986 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
987 GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000988 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
989 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
990 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000991 }
992
993 // Ok, we ran out of things they have in common. If any leftovers
994 // are non-zero then we have a difference, otherwise we are equal.
995 for (; i < CE1->getNumOperands(); ++i)
996 if (!CE1->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +0000997 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +0000998 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +0000999 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001000 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001001
Chris Lattner061da2f2004-01-13 05:51:55 +00001002 for (; i < CE2->getNumOperands(); ++i)
1003 if (!CE2->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001004 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001005 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001006 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001007 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1008 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001009 }
1010 }
1011 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001012 default:
1013 break;
1014 }
1015 }
1016
Reid Spencer266e42b2006-12-23 06:05:41 +00001017 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001018}
1019
Reid Spencer9d36acf2006-12-24 18:52:08 +00001020Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1021 const Constant *C1,
1022 const Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001023
1024 // Handle some degenerate cases first
1025 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Reid Spencer542964f2007-01-11 18:21:29 +00001026 return UndefValue::get(Type::Int1Ty);
Reid Spencer266e42b2006-12-23 06:05:41 +00001027
1028 // icmp eq/ne(null,GV) -> false/true
1029 if (C1->isNullValue()) {
1030 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1031 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001032 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001033 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001034 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001035 return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001036 // icmp eq/ne(GV,null) -> false/true
1037 } else if (C2->isNullValue()) {
1038 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1039 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001040 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001041 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001042 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001043 return ConstantInt::getTrue();
Chris Lattner1dd054c2004-01-12 22:07:24 +00001044 }
1045
Chris Lattner344da522007-01-12 18:42:52 +00001046 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001047 if (ICmpInst::isSignedPredicate(ICmpInst::Predicate(pred))) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001048 int64_t V1 = cast<ConstantInt>(C1)->getSExtValue();
1049 int64_t V2 = cast<ConstantInt>(C2)->getSExtValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001050 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001051 default: assert(0 && "Invalid ICmp Predicate"); return 0;
Reid Spencercddc9df2007-01-12 04:24:46 +00001052 case ICmpInst::ICMP_SLT:return ConstantInt::get(Type::Int1Ty, V1 < V2);
1053 case ICmpInst::ICMP_SGT:return ConstantInt::get(Type::Int1Ty, V1 > V2);
1054 case ICmpInst::ICMP_SLE:return ConstantInt::get(Type::Int1Ty, V1 <= V2);
1055 case ICmpInst::ICMP_SGE:return ConstantInt::get(Type::Int1Ty, V1 >= V2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001056 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001057 } else {
Reid Spencer266e42b2006-12-23 06:05:41 +00001058 uint64_t V1 = cast<ConstantInt>(C1)->getZExtValue();
1059 uint64_t V2 = cast<ConstantInt>(C2)->getZExtValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001060 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001061 default: assert(0 && "Invalid ICmp Predicate"); return 0;
Reid Spencercddc9df2007-01-12 04:24:46 +00001062 case ICmpInst::ICMP_EQ: return ConstantInt::get(Type::Int1Ty, V1 == V2);
1063 case ICmpInst::ICMP_NE: return ConstantInt::get(Type::Int1Ty, V1 != V2);
1064 case ICmpInst::ICMP_ULT:return ConstantInt::get(Type::Int1Ty, V1 < V2);
1065 case ICmpInst::ICMP_UGT:return ConstantInt::get(Type::Int1Ty, V1 > V2);
1066 case ICmpInst::ICMP_ULE:return ConstantInt::get(Type::Int1Ty, V1 <= V2);
1067 case ICmpInst::ICMP_UGE:return ConstantInt::get(Type::Int1Ty, V1 >= V2);
Chris Lattner061da2f2004-01-13 05:51:55 +00001068 }
1069 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001070 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1071 double C1Val = cast<ConstantFP>(C1)->getValue();
1072 double C2Val = cast<ConstantFP>(C2)->getValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001073 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001074 default: assert(0 && "Invalid FCmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001075 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse();
1076 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001077 case FCmpInst::FCMP_UNO:
Reid Spencercddc9df2007-01-12 04:24:46 +00001078 return ConstantInt::get(Type::Int1Ty, C1Val != C1Val || C2Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001079 case FCmpInst::FCMP_ORD:
Reid Spencercddc9df2007-01-12 04:24:46 +00001080 return ConstantInt::get(Type::Int1Ty, C1Val == C1Val && C2Val == C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001081 case FCmpInst::FCMP_UEQ:
Reid Spencer74bd0362007-01-11 00:25:45 +00001082 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001083 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001084 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001085 case FCmpInst::FCMP_OEQ:
1086 return ConstantInt::get(Type::Int1Ty, C1Val == C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001087 case FCmpInst::FCMP_UNE:
1088 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001089 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001090 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001091 case FCmpInst::FCMP_ONE:
1092 return ConstantInt::get(Type::Int1Ty, C1Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001093 case FCmpInst::FCMP_ULT:
1094 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001095 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001096 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001097 case FCmpInst::FCMP_OLT:
1098 return ConstantInt::get(Type::Int1Ty, C1Val < C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001099 case FCmpInst::FCMP_UGT:
Reid Spencer74bd0362007-01-11 00:25:45 +00001100 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001101 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001102 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001103 case FCmpInst::FCMP_OGT:
1104 return ConstantInt::get(Type::Int1Ty, C1Val > C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001105 case FCmpInst::FCMP_ULE:
1106 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001107 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001108 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001109 case FCmpInst::FCMP_OLE:
1110 return ConstantInt::get(Type::Int1Ty, C1Val <= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001111 case FCmpInst::FCMP_UGE:
Reid Spencer74bd0362007-01-11 00:25:45 +00001112 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001113 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001114 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001115 case FCmpInst::FCMP_OGE:
1116 return ConstantInt::get(Type::Int1Ty, C1Val >= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001117 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001118 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
1119 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001120 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001121 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1122 Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
1123 const_cast<Constant*>(CP1->getOperand(i)),
1124 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001125 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001126 return CB;
1127 }
1128 // Otherwise, could not decide from any element pairs.
1129 return 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001130 } else if (pred == ICmpInst::ICMP_EQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001131 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1132 Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ,
1133 const_cast<Constant*>(CP1->getOperand(i)),
1134 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001135 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001136 return CB;
1137 }
1138 // Otherwise, could not decide from any element pairs.
1139 return 0;
1140 }
1141 }
1142 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001143
Reid Spencer9d36acf2006-12-24 18:52:08 +00001144 if (C1->getType()->isFloatingPoint()) {
1145 switch (evaluateFCmpRelation(C1, C2)) {
1146 default: assert(0 && "Unknown relation!");
1147 case FCmpInst::FCMP_UNO:
1148 case FCmpInst::FCMP_ORD:
1149 case FCmpInst::FCMP_UEQ:
1150 case FCmpInst::FCMP_UNE:
1151 case FCmpInst::FCMP_ULT:
1152 case FCmpInst::FCMP_UGT:
1153 case FCmpInst::FCMP_ULE:
1154 case FCmpInst::FCMP_UGE:
1155 case FCmpInst::FCMP_TRUE:
1156 case FCmpInst::FCMP_FALSE:
1157 case FCmpInst::BAD_FCMP_PREDICATE:
1158 break; // Couldn't determine anything about these constants.
1159 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001160 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001161 pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1162 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1163 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1164 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001165 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001166 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1167 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1168 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1169 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001170 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001171 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1172 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1173 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1174 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1175 // We can only partially decide this relation.
1176 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001177 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001178 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001179 return ConstantInt::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001180 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001181 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1182 // We can only partially decide this relation.
1183 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001184 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001185 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001186 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001187 break;
1188 case ICmpInst::ICMP_NE: // We know that C1 != C2
1189 // We can only partially decide this relation.
1190 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001191 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001192 if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001193 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001194 break;
1195 }
1196 } else {
1197 // Evaluate the relation between the two constants, per the predicate.
1198 switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
1199 default: assert(0 && "Unknown relational!");
1200 case ICmpInst::BAD_ICMP_PREDICATE:
1201 break; // Couldn't determine anything about these constants.
1202 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1203 // If we know the constants are equal, we can decide the result of this
1204 // computation precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001205 return ConstantInt::get(Type::Int1Ty,
1206 pred == ICmpInst::ICMP_EQ ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001207 pred == ICmpInst::ICMP_ULE ||
1208 pred == ICmpInst::ICMP_SLE ||
1209 pred == ICmpInst::ICMP_UGE ||
1210 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001211 case ICmpInst::ICMP_ULT:
1212 // If we know that C1 < C2, we can decide the result of this computation
1213 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001214 return ConstantInt::get(Type::Int1Ty,
1215 pred == ICmpInst::ICMP_ULT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001216 pred == ICmpInst::ICMP_NE ||
1217 pred == ICmpInst::ICMP_ULE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001218 case ICmpInst::ICMP_SLT:
1219 // If we know that C1 < C2, we can decide the result of this computation
1220 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001221 return ConstantInt::get(Type::Int1Ty,
1222 pred == ICmpInst::ICMP_SLT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001223 pred == ICmpInst::ICMP_NE ||
1224 pred == ICmpInst::ICMP_SLE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001225 case ICmpInst::ICMP_UGT:
1226 // If we know that C1 > C2, we can decide the result of this computation
1227 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001228 return ConstantInt::get(Type::Int1Ty,
1229 pred == ICmpInst::ICMP_UGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001230 pred == ICmpInst::ICMP_NE ||
1231 pred == ICmpInst::ICMP_UGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001232 case ICmpInst::ICMP_SGT:
1233 // If we know that C1 > C2, we can decide the result of this computation
1234 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001235 return ConstantInt::get(Type::Int1Ty,
1236 pred == ICmpInst::ICMP_SGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001237 pred == ICmpInst::ICMP_NE ||
1238 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001239 case ICmpInst::ICMP_ULE:
1240 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001241 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getFalse();
1242 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001243 break;
1244 case ICmpInst::ICMP_SLE:
1245 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001246 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getFalse();
1247 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001248 break;
1249
1250 case ICmpInst::ICMP_UGE:
1251 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001252 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getFalse();
1253 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001254 break;
1255 case ICmpInst::ICMP_SGE:
1256 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001257 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getFalse();
1258 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001259 break;
1260
1261 case ICmpInst::ICMP_NE:
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_EQ) return ConstantInt::getFalse();
1264 if (pred == ICmpInst::ICMP_NE) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001265 break;
1266 }
1267
1268 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
1269 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1270 // other way if possible.
1271 switch (pred) {
1272 case ICmpInst::ICMP_EQ:
1273 case ICmpInst::ICMP_NE:
1274 // No change of predicate required.
1275 return ConstantFoldCompareInstruction(pred, C2, C1);
1276
1277 case ICmpInst::ICMP_ULT:
1278 case ICmpInst::ICMP_SLT:
1279 case ICmpInst::ICMP_UGT:
1280 case ICmpInst::ICMP_SGT:
1281 case ICmpInst::ICMP_ULE:
1282 case ICmpInst::ICMP_SLE:
1283 case ICmpInst::ICMP_UGE:
1284 case ICmpInst::ICMP_SGE:
1285 // Change the predicate as necessary to swap the operands.
1286 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1287 return ConstantFoldCompareInstruction(pred, C2, C1);
1288
1289 default: // These predicates cannot be flopped around.
1290 break;
1291 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001292 }
1293 }
1294 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001295}
1296
1297Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001298 Constant* const *Idxs,
1299 unsigned NumIdx) {
1300 if (NumIdx == 0 ||
1301 (NumIdx == 1 && Idxs[0]->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001302 return const_cast<Constant*>(C);
1303
Chris Lattnerf6013752004-10-17 21:54:55 +00001304 if (isa<UndefValue>(C)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001305 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1306 (Value**)Idxs, NumIdx,
Chris Lattnerf6013752004-10-17 21:54:55 +00001307 true);
1308 assert(Ty != 0 && "Invalid indices for GEP!");
1309 return UndefValue::get(PointerType::get(Ty));
1310 }
1311
Chris Lattner302116a2007-01-31 04:40:28 +00001312 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001313 if (C->isNullValue()) {
1314 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001315 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1316 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001317 isNull = false;
1318 break;
1319 }
1320 if (isNull) {
Chris Lattner302116a2007-01-31 04:40:28 +00001321 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1322 (Value**)Idxs, NumIdx,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001323 true);
1324 assert(Ty != 0 && "Invalid indices for GEP!");
1325 return ConstantPointerNull::get(PointerType::get(Ty));
1326 }
1327 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001328
1329 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1330 // Combine Indices - If the source pointer to this getelementptr instruction
1331 // is a getelementptr instruction, combine the indices of the two
1332 // getelementptr instructions into a single instruction.
1333 //
1334 if (CE->getOpcode() == Instruction::GetElementPtr) {
1335 const Type *LastTy = 0;
1336 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1337 I != E; ++I)
1338 LastTy = *I;
1339
Chris Lattner13128ab2004-10-11 22:52:25 +00001340 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001341 SmallVector<Value*, 16> NewIndices;
1342 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001343 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001344 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001345
1346 // Add the last index of the source with the first index of the new GEP.
1347 // Make sure to handle the case when they are actually different types.
1348 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001349 // Otherwise it must be an array.
1350 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001351 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001352 if (IdxTy != Idx0->getType()) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001353 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Reid Spencer27720a92006-12-05 03:30:09 +00001354 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001355 Type::Int64Ty);
Reid Spencer1a063892006-12-04 02:46:44 +00001356 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1357 } else {
1358 Combined =
1359 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1360 }
Chris Lattner71068a02004-07-07 04:45:13 +00001361 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001362
Chris Lattner1dd054c2004-01-12 22:07:24 +00001363 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001364 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
1365 return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
1366 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001367 }
1368 }
1369
1370 // Implement folding of:
1371 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1372 // long 0, long 0)
1373 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1374 //
Chris Lattner302116a2007-01-31 04:40:28 +00001375 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001376 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001377 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1378 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1379 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001380 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001381 if (CAT->getElementType() == SAT->getElementType())
1382 return ConstantExpr::getGetElementPtr(
Chris Lattner302116a2007-01-31 04:40:28 +00001383 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001384 }
1385 return 0;
1386}
1387