blob: 85e5992dcf1f3c2ac54710810c58fc615c4f083f [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 Lattner3d27be12006-08-27 12:54:02 +000026#include "llvm/Support/Compiler.h"
Chris Lattner057083f2006-10-13 17:22:21 +000027#include "llvm/Support/GetElementPtrTypeIterator.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/MathExtras.h"
Jeff Cohen4e3aede2005-05-03 03:13:01 +000030#include <limits>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000031using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000032
Chris Lattner1dd054c2004-01-12 22:07:24 +000033//===----------------------------------------------------------------------===//
34// ConstantFold*Instruction Implementations
35//===----------------------------------------------------------------------===//
Chris Lattner1dd054c2004-01-12 22:07:24 +000036
Chris Lattner6b3f4752006-04-02 01:38:28 +000037/// CastConstantPacked - Convert the specified ConstantPacked node to the
38/// specified packed type. At this point, we know that the elements of the
39/// input packed constant are all simple integer or FP values.
40static Constant *CastConstantPacked(ConstantPacked *CP,
41 const PackedType *DstTy) {
42 unsigned SrcNumElts = CP->getType()->getNumElements();
43 unsigned DstNumElts = DstTy->getNumElements();
44 const Type *SrcEltTy = CP->getType()->getElementType();
45 const Type *DstEltTy = DstTy->getElementType();
46
47 // If both vectors have the same number of elements (thus, the elements
48 // are the same size), perform the conversion now.
49 if (SrcNumElts == DstNumElts) {
50 std::vector<Constant*> Result;
51
Reid Spencer6c38f0b2006-11-27 01:05:10 +000052 // If the src and dest elements are both integers, or both floats, we can
53 // just BitCast each element because the elements are the same size.
54 if ((SrcEltTy->isIntegral() && DstEltTy->isIntegral()) ||
55 (SrcEltTy->isFloatingPoint() && DstEltTy->isFloatingPoint())) {
Chris Lattner6b3f4752006-04-02 01:38:28 +000056 for (unsigned i = 0; i != SrcNumElts; ++i)
Reid Spencer6c38f0b2006-11-27 01:05:10 +000057 Result.push_back(
Reid Spencerbb65ebf2006-12-12 23:36:14 +000058 ConstantExpr::getBitCast(CP->getOperand(i), DstEltTy));
Chris Lattner6b3f4752006-04-02 01:38:28 +000059 return ConstantPacked::get(Result);
60 }
61
Reid Spencer6c38f0b2006-11-27 01:05:10 +000062 // If this is an int-to-fp cast ..
Chris Lattner6b3f4752006-04-02 01:38:28 +000063 if (SrcEltTy->isIntegral()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +000064 // Ensure that it is int-to-fp cast
Chris Lattner6b3f4752006-04-02 01:38:28 +000065 assert(DstEltTy->isFloatingPoint());
66 if (DstEltTy->getTypeID() == Type::DoubleTyID) {
67 for (unsigned i = 0; i != SrcNumElts; ++i) {
68 double V =
Reid Spencere0fc4df2006-10-20 07:07:24 +000069 BitsToDouble(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +000070 Result.push_back(ConstantFP::get(Type::DoubleTy, V));
71 }
72 return ConstantPacked::get(Result);
73 }
74 assert(DstEltTy == Type::FloatTy && "Unknown fp type!");
75 for (unsigned i = 0; i != SrcNumElts; ++i) {
76 float V =
Reid Spencere0fc4df2006-10-20 07:07:24 +000077 BitsToFloat(cast<ConstantInt>(CP->getOperand(i))->getZExtValue());
Chris Lattner6b3f4752006-04-02 01:38:28 +000078 Result.push_back(ConstantFP::get(Type::FloatTy, V));
79 }
80 return ConstantPacked::get(Result);
81 }
82
83 // Otherwise, this is an fp-to-int cast.
84 assert(SrcEltTy->isFloatingPoint() && DstEltTy->isIntegral());
85
86 if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
87 for (unsigned i = 0; i != SrcNumElts; ++i) {
88 uint64_t V =
89 DoubleToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
Reid Spencer8d9336d2006-12-31 05:26:44 +000090 Constant *C = ConstantInt::get(Type::Int64Ty, V);
Reid Spencera16f9302006-12-05 07:18:07 +000091 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
Chris Lattner6b3f4752006-04-02 01:38:28 +000092 }
93 return ConstantPacked::get(Result);
94 }
95
96 assert(SrcEltTy->getTypeID() == Type::FloatTyID);
97 for (unsigned i = 0; i != SrcNumElts; ++i) {
Reid Spencere0fc4df2006-10-20 07:07:24 +000098 uint32_t V = FloatToBits(cast<ConstantFP>(CP->getOperand(i))->getValue());
Reid Spencer8d9336d2006-12-31 05:26:44 +000099 Constant *C = ConstantInt::get(Type::Int32Ty, V);
Reid Spencera16f9302006-12-05 07:18:07 +0000100 Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
Chris Lattner6b3f4752006-04-02 01:38:28 +0000101 }
102 return ConstantPacked::get(Result);
103 }
104
105 // Otherwise, this is a cast that changes element count and size. Handle
106 // casts which shrink the elements here.
107
108 // FIXME: We need to know endianness to do this!
109
110 return 0;
111}
112
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000113/// This function determines which opcode to use to fold two constant cast
114/// expressions together. It uses CastInst::isEliminableCastPair to determine
115/// the opcode. Consequently its just a wrapper around that function.
116/// @Determine if it is valid to fold a cast of a cast
117static unsigned
118foldConstantCastPair(
119 unsigned opc, ///< opcode of the second cast constant expression
120 const ConstantExpr*Op, ///< the first cast constant expression
121 const Type *DstTy ///< desintation type of the first cast
122) {
123 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
124 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
125 assert(CastInst::isCast(opc) && "Invalid cast opcode");
126
127 // The the types and opcodes for the two Cast constant expressions
128 const Type *SrcTy = Op->getOperand(0)->getType();
129 const Type *MidTy = Op->getType();
130 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
131 Instruction::CastOps secondOp = Instruction::CastOps(opc);
Chris Lattner6b3f4752006-04-02 01:38:28 +0000132
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000133 // Let CastInst::isEliminableCastPair do the heavy lifting.
134 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
Reid Spencer8d9336d2006-12-31 05:26:44 +0000135 Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000136}
137
138Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
Chris Lattner1dd054c2004-01-12 22:07:24 +0000139 const Type *DestTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000140 const Type *SrcTy = V->getType();
Chris Lattner1dd054c2004-01-12 22:07:24 +0000141
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000142 if (isa<UndefValue>(V))
143 return UndefValue::get(DestTy);
144
145 // If the cast operand is a constant expression, there's a few things we can
146 // do to try to simplify it.
147 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
148 if (CE->isCast()) {
Reid Spencer1a063892006-12-04 02:46:44 +0000149 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000150 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
151 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000152 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
153 // If all of the indexes in the GEP are null values, there is no pointer
154 // adjustment going on. We might as well cast the source pointer.
155 bool isAllNull = true;
156 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
157 if (!CE->getOperand(i)->isNullValue()) {
158 isAllNull = false;
159 break;
160 }
161 if (isAllNull)
Reid Spencer1a063892006-12-04 02:46:44 +0000162 // This is casting one pointer type to another, always BitCast
Reid Spencer27720a92006-12-05 03:30:09 +0000163 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000164 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000165 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000166
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000167 // We actually have to do a cast now. Perform the cast according to the
168 // opcode specified.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000169 switch (opc) {
170 case Instruction::FPTrunc:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000171 case Instruction::FPExt:
Reid Spencer8dabca42006-12-19 07:41:40 +0000172 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
173 return ConstantFP::get(DestTy, FPC->getValue());
174 return 0; // Can't fold.
175 case Instruction::FPToUI:
176 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
Zhou Sheng75b871f2007-01-11 12:24:14 +0000177 return ConstantInt::get(DestTy,(uint64_t) FPC->getValue());
Reid Spencer8dabca42006-12-19 07:41:40 +0000178 return 0; // Can't fold.
179 case Instruction::FPToSI:
180 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V))
Zhou Sheng75b871f2007-01-11 12:24:14 +0000181 return ConstantInt::get(DestTy,(int64_t) FPC->getValue());
Reid Spencer8dabca42006-12-19 07:41:40 +0000182 return 0; // Can't fold.
183 case Instruction::IntToPtr: //always treated as unsigned
184 if (V->isNullValue()) // Is it an integral null value?
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000185 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Reid Spencer8dabca42006-12-19 07:41:40 +0000186 return 0; // Other pointer types cannot be casted
187 case Instruction::PtrToInt: // always treated as unsigned
188 if (V->isNullValue()) // is it a null pointer value?
Zhou Sheng75b871f2007-01-11 12:24:14 +0000189 return ConstantInt::get(DestTy, 0);
Reid Spencer8dabca42006-12-19 07:41:40 +0000190 return 0; // Other pointer types cannot be casted
191 case Instruction::UIToFP:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000192 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer8dabca42006-12-19 07:41:40 +0000193 return ConstantFP::get(DestTy, double(CI->getZExtValue()));
194 return 0;
195 case Instruction::SIToFP:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000196 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer8dabca42006-12-19 07:41:40 +0000197 return ConstantFP::get(DestTy, double(CI->getSExtValue()));
198 return 0;
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000199 case Instruction::ZExt:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000200 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer8dabca42006-12-19 07:41:40 +0000201 return ConstantInt::get(DestTy, CI->getZExtValue());
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000202 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000203 case Instruction::SExt:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000204 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
Reid Spencer8dabca42006-12-19 07:41:40 +0000205 return ConstantInt::get(DestTy, CI->getSExtValue());
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000206 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000207 case Instruction::Trunc:
Reid Spencer8dabca42006-12-19 07:41:40 +0000208 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) // Can't trunc a bool
Zhou Sheng75b871f2007-01-11 12:24:14 +0000209 return ConstantInt::get(DestTy, CI->getZExtValue());
Chris Lattner710ebaf2006-12-01 19:22:41 +0000210 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000211 case Instruction::BitCast:
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000212 if (SrcTy == DestTy)
213 return (Constant*)V; // no-op cast
Chris Lattner4d1da162006-12-11 18:30:27 +0000214
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000215 // Check to see if we are casting a pointer to an aggregate to a pointer to
216 // the first element. If so, return the appropriate GEP instruction.
217 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
218 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
219 std::vector<Value*> IdxList;
Reid Spencer8d9336d2006-12-31 05:26:44 +0000220 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000221 const Type *ElTy = PTy->getElementType();
222 while (ElTy != DPTy->getElementType()) {
223 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
224 if (STy->getNumElements() == 0) break;
225 ElTy = STy->getElementType(0);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000226 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000227 } else if (const SequentialType *STy =
228 dyn_cast<SequentialType>(ElTy)) {
229 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
230 ElTy = STy->getElementType();
231 IdxList.push_back(IdxList[0]);
232 } else {
Chris Lattner6b3f4752006-04-02 01:38:28 +0000233 break;
234 }
235 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000236
237 if (ElTy == DPTy->getElementType())
238 return ConstantExpr::getGetElementPtr(
239 const_cast<Constant*>(V),IdxList);
240 }
241
242 // Handle casts from one packed constant to another. We know that the src
243 // and dest type have the same size (otherwise its an illegal cast).
244 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
245 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
246 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
247 "Not cast between same sized vectors!");
248 // First, check for null and undef
249 if (isa<ConstantAggregateZero>(V))
250 return Constant::getNullValue(DestTy);
251 if (isa<UndefValue>(V))
252 return UndefValue::get(DestTy);
253
254 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
255 // This is a cast from a ConstantPacked of one type to a
256 // ConstantPacked of another type. Check to see if all elements of
257 // the input are simple.
258 bool AllSimpleConstants = true;
259 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) {
260 if (!isa<ConstantInt>(CP->getOperand(i)) &&
261 !isa<ConstantFP>(CP->getOperand(i))) {
262 AllSimpleConstants = false;
263 break;
264 }
265 }
266
267 // If all of the elements are simple constants, we can fold this.
268 if (AllSimpleConstants)
269 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
270 }
Chris Lattner6b3f4752006-04-02 01:38:28 +0000271 }
272 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000273
Chris Lattner4d1da162006-12-11 18:30:27 +0000274 // Finally, implement bitcast folding now. The code below doesn't handle
275 // bitcast right.
276 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
277 return ConstantPointerNull::get(cast<PointerType>(DestTy));
278
279 // Handle integral constant input.
280 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
281 // Integral -> Integral, must be changing sign.
282 if (DestTy->isIntegral())
283 return ConstantInt::get(DestTy, CI->getZExtValue());
284
285 if (DestTy->isFloatingPoint()) {
286 if (DestTy == Type::FloatTy)
287 return ConstantFP::get(DestTy, BitsToFloat(CI->getZExtValue()));
288 assert(DestTy == Type::DoubleTy && "Unknown FP type!");
289 return ConstantFP::get(DestTy, BitsToDouble(CI->getZExtValue()));
290 }
291 // Otherwise, can't fold this (packed?)
292 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000293 }
Chris Lattner4d1da162006-12-11 18:30:27 +0000294
295 // Handle ConstantFP input.
296 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
297 // FP -> Integral.
298 if (DestTy->isIntegral()) {
Reid Spencer8d9336d2006-12-31 05:26:44 +0000299 if (DestTy == Type::Int32Ty)
Chris Lattner4d1da162006-12-11 18:30:27 +0000300 return ConstantInt::get(DestTy, FloatToBits(FP->getValue()));
Reid Spencer8d9336d2006-12-31 05:26:44 +0000301 assert(DestTy == Type::Int64Ty &&
Reid Spencerc90cf772006-12-31 21:43:30 +0000302 "Incorrect integer type for bitcast!");
Chris Lattner4d1da162006-12-11 18:30:27 +0000303 return ConstantInt::get(DestTy, DoubleToBits(FP->getValue()));
304 }
305 }
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 Spencer542964f2007-01-11 18:21:29 +0000320 if (CB->getType() == Type::Int1Ty)
Zhou Sheng75b871f2007-01-11 12:24:14 +0000321 return const_cast<Constant*>(CB->getBoolValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000322
323 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
324 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
325 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000326 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000327 return 0;
328}
329
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000330Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
331 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000332 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
333 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000334 if (Val->isNullValue()) // ee(zero, x) -> zero
335 return Constant::getNullValue(
336 cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000337
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000338 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000339 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
340 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000341 } else if (isa<UndefValue>(Idx)) {
342 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
343 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000344 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000345 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000346 return 0;
347}
348
Robert Bocchinoca27f032006-01-17 20:07:22 +0000349Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
350 const Constant *Elt,
351 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000352 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000353 if (!CIdx) return 0;
Reid Spencere0fc4df2006-10-20 07:07:24 +0000354 uint64_t idxVal = CIdx->getZExtValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000355 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000356 // Insertion of scalar constant into packed undef
357 // Optimize away insertion of undef
358 if (isa<UndefValue>(Elt))
359 return const_cast<Constant*>(Val);
360 // Otherwise break the aggregate undef into multiple undefs and do
361 // the insertion
362 unsigned numOps =
363 cast<PackedType>(Val->getType())->getNumElements();
364 std::vector<Constant*> Ops;
365 Ops.reserve(numOps);
366 for (unsigned i = 0; i < numOps; ++i) {
367 const Constant *Op =
368 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
369 Ops.push_back(const_cast<Constant*>(Op));
370 }
371 return ConstantPacked::get(Ops);
372 }
Reid Spencer3054b142006-11-02 08:18:15 +0000373 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000374 // Insertion of scalar constant into packed aggregate zero
375 // Optimize away insertion of zero
376 if (Elt->isNullValue())
377 return const_cast<Constant*>(Val);
378 // Otherwise break the aggregate zero into multiple zeros and do
379 // the insertion
380 unsigned numOps =
381 cast<PackedType>(Val->getType())->getNumElements();
382 std::vector<Constant*> Ops;
383 Ops.reserve(numOps);
384 for (unsigned i = 0; i < numOps; ++i) {
385 const Constant *Op =
386 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
387 Ops.push_back(const_cast<Constant*>(Op));
388 }
389 return ConstantPacked::get(Ops);
390 }
391 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
392 // Insertion of scalar constant into packed constant
393 std::vector<Constant*> Ops;
394 Ops.reserve(CVal->getNumOperands());
395 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
396 const Constant *Op =
397 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
398 Ops.push_back(const_cast<Constant*>(Op));
399 }
400 return ConstantPacked::get(Ops);
401 }
402 return 0;
403}
404
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000405Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
406 const Constant *V2,
407 const Constant *Mask) {
408 // TODO:
409 return 0;
410}
411
Reid Spencer266e42b2006-12-23 06:05:41 +0000412/// EvalVectorOp - Given two packed constants and a function pointer, apply the
413/// function pointer to each element pair, producing a new ConstantPacked
414/// constant.
415static Constant *EvalVectorOp(const ConstantPacked *V1,
416 const ConstantPacked *V2,
417 Constant *(*FP)(Constant*, Constant*)) {
418 std::vector<Constant*> Res;
419 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
420 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
421 const_cast<Constant*>(V2->getOperand(i))));
422 return ConstantPacked::get(Res);
423}
424
425Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
426 const Constant *C1,
427 const Constant *C2) {
428 // Handle UndefValue up front
429 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
430 switch (Opcode) {
431 case Instruction::Add:
432 case Instruction::Sub:
433 case Instruction::Xor:
434 return UndefValue::get(C1->getType());
435 case Instruction::Mul:
436 case Instruction::And:
437 return Constant::getNullValue(C1->getType());
438 case Instruction::UDiv:
439 case Instruction::SDiv:
440 case Instruction::FDiv:
441 case Instruction::URem:
442 case Instruction::SRem:
443 case Instruction::FRem:
444 if (!isa<UndefValue>(C2)) // undef / X -> 0
445 return Constant::getNullValue(C1->getType());
446 return const_cast<Constant*>(C2); // X / undef -> undef
447 case Instruction::Or: // X | undef -> -1
Chris Lattner26f13eb2007-01-04 01:56:39 +0000448 if (const PackedType *PTy = dyn_cast<PackedType>(C1->getType()))
449 return ConstantPacked::getAllOnesValue(PTy);
Reid Spencer266e42b2006-12-23 06:05:41 +0000450 return ConstantInt::getAllOnesValue(C1->getType());
451 case Instruction::LShr:
452 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
453 return const_cast<Constant*>(C1); // undef lshr undef -> undef
454 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
455 // undef lshr X -> 0
456 case Instruction::AShr:
457 if (!isa<UndefValue>(C2))
458 return const_cast<Constant*>(C1); // undef ashr X --> undef
459 else if (isa<UndefValue>(C1))
460 return const_cast<Constant*>(C1); // undef ashr undef -> undef
461 else
462 return const_cast<Constant*>(C1); // X ashr undef --> X
463 case Instruction::Shl:
464 // undef << X -> 0 or X << undef -> 0
465 return Constant::getNullValue(C1->getType());
466 }
467 }
468
469 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
470 if (isa<ConstantExpr>(C2)) {
471 // There are many possible foldings we could do here. We should probably
472 // at least fold add of a pointer with an integer into the appropriate
473 // getelementptr. This will improve alias analysis a bit.
474 } else {
475 // Just implement a couple of simple identities.
476 switch (Opcode) {
477 case Instruction::Add:
478 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X + 0 == X
479 break;
480 case Instruction::Sub:
481 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X - 0 == X
482 break;
483 case Instruction::Mul:
484 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X * 0 == 0
485 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
486 if (CI->getZExtValue() == 1)
487 return const_cast<Constant*>(C1); // X * 1 == X
488 break;
489 case Instruction::UDiv:
490 case Instruction::SDiv:
491 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
492 if (CI->getZExtValue() == 1)
493 return const_cast<Constant*>(C1); // X / 1 == X
494 break;
495 case Instruction::URem:
496 case Instruction::SRem:
497 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
498 if (CI->getZExtValue() == 1)
499 return Constant::getNullValue(CI->getType()); // X % 1 == 0
500 break;
501 case Instruction::And:
Chris Lattner26f13eb2007-01-04 01:56:39 +0000502 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
503 if (CI->isAllOnesValue())
504 return const_cast<Constant*>(C1); // X & -1 == X
Reid Spencer266e42b2006-12-23 06:05:41 +0000505 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X & 0 == 0
506 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
507 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
508
509 // Functions are at least 4-byte aligned. If and'ing the address of a
510 // function with a constant < 4, fold it to zero.
511 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
512 if (CI->getZExtValue() < 4 && isa<Function>(CPR))
513 return Constant::getNullValue(CI->getType());
514 }
515 break;
516 case Instruction::Or:
517 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X | 0 == X
Chris Lattner26f13eb2007-01-04 01:56:39 +0000518 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
519 if (CI->isAllOnesValue())
520 return const_cast<Constant*>(C2); // X | -1 == -1
Reid Spencer266e42b2006-12-23 06:05:41 +0000521 break;
522 case Instruction::Xor:
523 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X ^ 0 == X
524 break;
525 }
526 }
527 } else if (isa<ConstantExpr>(C2)) {
528 // If C2 is a constant expr and C1 isn't, flop them around and fold the
529 // other way if possible.
530 switch (Opcode) {
531 case Instruction::Add:
532 case Instruction::Mul:
533 case Instruction::And:
534 case Instruction::Or:
535 case Instruction::Xor:
536 // No change of opcode required.
537 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
538
539 case Instruction::Shl:
540 case Instruction::LShr:
541 case Instruction::AShr:
542 case Instruction::Sub:
543 case Instruction::SDiv:
544 case Instruction::UDiv:
545 case Instruction::FDiv:
546 case Instruction::URem:
547 case Instruction::SRem:
548 case Instruction::FRem:
549 default: // These instructions cannot be flopped around.
550 return 0;
551 }
552 }
553
554 // At this point we know neither constant is an UndefValue nor a ConstantExpr
Chris Lattner26f13eb2007-01-04 01:56:39 +0000555 // so look at directly computing the value.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000556 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
557 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer542964f2007-01-11 18:21:29 +0000558 if (CI1->getType() == Type::Int1Ty && CI2->getType() == Type::Int1Ty) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000559 switch (Opcode) {
560 default:
561 break;
562 case Instruction::And:
563 return ConstantInt::get(CI1->getBoolValue() & CI2->getBoolValue());
564 case Instruction::Or:
565 return ConstantInt::get(CI1->getBoolValue() | CI2->getBoolValue());
566 case Instruction::Xor:
567 return ConstantInt::get(CI1->getBoolValue() ^ CI2->getBoolValue());
568 }
569 } else {
570 uint64_t C1Val = CI1->getZExtValue();
571 uint64_t C2Val = CI2->getZExtValue();
572 switch (Opcode) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000573 default:
574 break;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000575 case Instruction::Add:
576 return ConstantInt::get(C1->getType(), C1Val + C2Val);
577 case Instruction::Sub:
578 return ConstantInt::get(C1->getType(), C1Val - C2Val);
579 case Instruction::Mul:
580 return ConstantInt::get(C1->getType(), C1Val * C2Val);
581 case Instruction::UDiv:
582 if (CI2->isNullValue()) // X / 0 -> can't fold
583 return 0;
584 return ConstantInt::get(C1->getType(), C1Val / C2Val);
585 case Instruction::SDiv:
586 if (CI2->isNullValue()) return 0; // X / 0 -> can't fold
587 if (CI2->isAllOnesValue() &&
588 (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
589 (CI1->getSExtValue() == INT64_MIN)) ||
590 (CI1->getSExtValue() == -CI1->getSExtValue())))
591 return 0; // MIN_INT / -1 -> overflow
592 return ConstantInt::get(C1->getType(),
593 CI1->getSExtValue() / CI2->getSExtValue());
594 case Instruction::URem:
595 if (C2->isNullValue()) return 0; // X / 0 -> can't fold
596 return ConstantInt::get(C1->getType(), C1Val % C2Val);
597 case Instruction::SRem:
598 if (CI2->isNullValue()) return 0; // X % 0 -> can't fold
599 if (CI2->isAllOnesValue() &&
600 (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
601 (CI1->getSExtValue() == INT64_MIN)) ||
602 (CI1->getSExtValue() == -CI1->getSExtValue())))
603 return 0; // MIN_INT % -1 -> overflow
604 return ConstantInt::get(C1->getType(),
605 CI1->getSExtValue() % CI2->getSExtValue());
Reid Spencer266e42b2006-12-23 06:05:41 +0000606 case Instruction::And:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000607 return ConstantInt::get(C1->getType(), C1Val & C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +0000608 case Instruction::Or:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000609 return ConstantInt::get(C1->getType(), C1Val | C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +0000610 case Instruction::Xor:
Zhou Sheng75b871f2007-01-11 12:24:14 +0000611 return ConstantInt::get(C1->getType(), C1Val ^ C2Val);
612 case Instruction::Shl:
613 return ConstantInt::get(C1->getType(), C1Val << C2Val);
614 case Instruction::LShr:
615 return ConstantInt::get(C1->getType(), C1Val >> C2Val);
616 case Instruction::AShr:
617 return ConstantInt::get(C1->getType(),
618 CI1->getSExtValue() >> C2Val);
619 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000620 }
621 }
622 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
623 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
624 double C1Val = CFP1->getValue();
625 double C2Val = CFP2->getValue();
626 switch (Opcode) {
627 default:
628 break;
629 case Instruction::Add:
630 return ConstantFP::get(CFP1->getType(), C1Val + C2Val);
631 case Instruction::Sub:
632 return ConstantFP::get(CFP1->getType(), C1Val - C2Val);
633 case Instruction::Mul:
634 return ConstantFP::get(CFP1->getType(), C1Val * C2Val);
635 case Instruction::FDiv:
636 if (CFP2->isExactlyValue(0.0))
637 return ConstantFP::get(CFP1->getType(),
638 std::numeric_limits<double>::infinity());
639 if (CFP2->isExactlyValue(-0.0))
640 return ConstantFP::get(CFP1->getType(),
641 -std::numeric_limits<double>::infinity());
642 return ConstantFP::get(CFP1->getType(), C1Val / C2Val);
643 case Instruction::FRem:
644 if (CFP2->isNullValue())
645 return 0;
646 return ConstantFP::get(CFP1->getType(), std::fmod(C1Val, C2Val));
647 }
648 }
649 } else if (const ConstantPacked *CP1 = dyn_cast<ConstantPacked>(C1)) {
650 if (const ConstantPacked *CP2 = dyn_cast<ConstantPacked>(C2)) {
651 switch (Opcode) {
652 default:
653 break;
654 case Instruction::Add:
655 return EvalVectorOp(CP1, CP2, ConstantExpr::getAdd);
656 case Instruction::Sub:
657 return EvalVectorOp(CP1, CP2, ConstantExpr::getSub);
658 case Instruction::Mul:
659 return EvalVectorOp(CP1, CP2, ConstantExpr::getMul);
660 case Instruction::UDiv:
661 return EvalVectorOp(CP1, CP2, ConstantExpr::getUDiv);
662 case Instruction::SDiv:
663 return EvalVectorOp(CP1, CP2, ConstantExpr::getSDiv);
664 case Instruction::FDiv:
665 return EvalVectorOp(CP1, CP2, ConstantExpr::getFDiv);
666 case Instruction::URem:
667 return EvalVectorOp(CP1, CP2, ConstantExpr::getURem);
668 case Instruction::SRem:
669 return EvalVectorOp(CP1, CP2, ConstantExpr::getSRem);
670 case Instruction::FRem:
671 return EvalVectorOp(CP1, CP2, ConstantExpr::getFRem);
672 case Instruction::And:
673 return EvalVectorOp(CP1, CP2, ConstantExpr::getAnd);
674 case Instruction::Or:
675 return EvalVectorOp(CP1, CP2, ConstantExpr::getOr);
676 case Instruction::Xor:
677 return EvalVectorOp(CP1, CP2, ConstantExpr::getXor);
678 }
679 }
680 }
681
682 // We don't know how to fold this
683 return 0;
684}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000685
Chris Lattner60c47262005-01-28 19:09:51 +0000686/// isZeroSizedType - This type is zero sized if its an array or structure of
687/// zero sized types. The only leaf zero sized type is an empty structure.
688static bool isMaybeZeroSizedType(const Type *Ty) {
689 if (isa<OpaqueType>(Ty)) return true; // Can't say.
690 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
691
692 // If all of elements have zero size, this does too.
693 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000694 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000695 return true;
696
697 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
698 return isMaybeZeroSizedType(ATy->getElementType());
699 }
700 return false;
701}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000702
Chris Lattner061da2f2004-01-13 05:51:55 +0000703/// IdxCompare - Compare the two constants as though they were getelementptr
704/// indices. This allows coersion of the types to be the same thing.
705///
706/// If the two constants are the "same" (after coersion), return 0. If the
707/// first is less than the second, return -1, if the second is less than the
708/// first, return 1. If the constants are not integral, return -2.
709///
Chris Lattner60c47262005-01-28 19:09:51 +0000710static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000711 if (C1 == C2) return 0;
712
Reid Spencerc90cf772006-12-31 21:43:30 +0000713 // Ok, we found a different index. If they are not ConstantInt, we can't do
714 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +0000715 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
716 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000717
Chris Lattner69193f92004-04-05 01:30:19 +0000718 // Ok, we have two differing integer indices. Sign extend them to be the same
719 // type. Long is always big enough, so we use it.
Reid Spencer8d9336d2006-12-31 05:26:44 +0000720 if (C1->getType() != Type::Int64Ty)
721 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000722
Reid Spencer8d9336d2006-12-31 05:26:44 +0000723 if (C2->getType() != Type::Int64Ty)
Reid Spencerc90cf772006-12-31 21:43:30 +0000724 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000725
726 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +0000727
Chris Lattner60c47262005-01-28 19:09:51 +0000728 // If the type being indexed over is really just a zero sized type, there is
729 // no pointer difference being made here.
730 if (isMaybeZeroSizedType(ElTy))
731 return -2; // dunno.
732
Chris Lattner061da2f2004-01-13 05:51:55 +0000733 // If they are really different, now that they are the same type, then we
734 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +0000735 if (cast<ConstantInt>(C1)->getSExtValue() <
736 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +0000737 return -1;
738 else
739 return 1;
740}
741
Chris Lattner858f4e92007-01-04 02:13:20 +0000742/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +0000743/// decide about the two constants provided. This doesn't need to handle simple
744/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
745/// If we can determine that the two constants have a particular relation to
746/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +0000747/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
748/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +0000749///
750/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +0000751/// operand is always the most "complex" of the two. We consider ConstantFP
752/// to be the simplest, and ConstantExprs to be the most complex.
753static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1,
754 const Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000755 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +0000756 "Cannot compare values of different types!");
757 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +0000758 if (V1 == V2) return FCmpInst::FCMP_OEQ;
759
Reid Spencer9d36acf2006-12-24 18:52:08 +0000760 if (!isa<ConstantExpr>(V1)) {
761 if (!isa<ConstantExpr>(V2)) {
762 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +0000763 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000764 Constant *C1 = const_cast<Constant*>(V1);
765 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng75b871f2007-01-11 12:24:14 +0000766 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000767 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Zhou Sheng75b871f2007-01-11 12:24:14 +0000768 if (R && R->getBoolValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000769 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000770 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000771 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2));
Zhou Sheng75b871f2007-01-11 12:24:14 +0000772 if (R && R->getBoolValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000773 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000774 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000775 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
Zhou Sheng75b871f2007-01-11 12:24:14 +0000776 if (R && R->getBoolValue())
Reid Spencer9d36acf2006-12-24 18:52:08 +0000777 return FCmpInst::FCMP_OGT;
778
779 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +0000780 return FCmpInst::BAD_FCMP_PREDICATE;
781 }
782
Reid Spencer9d36acf2006-12-24 18:52:08 +0000783 // If the first operand is simple and second is ConstantExpr, swap operands.
784 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
785 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
786 return FCmpInst::getSwappedPredicate(SwappedRelation);
787 } else {
788 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
789 // constantexpr or a simple constant.
790 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
791 switch (CE1->getOpcode()) {
792 case Instruction::FPTrunc:
793 case Instruction::FPExt:
794 case Instruction::UIToFP:
795 case Instruction::SIToFP:
796 // We might be able to do something with these but we don't right now.
797 break;
798 default:
799 break;
800 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000801 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000802 // There are MANY other foldings that we could perform here. They will
803 // probably be added on demand, as they seem needed.
804 return FCmpInst::BAD_FCMP_PREDICATE;
805}
806
807/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +0000808/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000809/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +0000810/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +0000811/// particular relation to each other, we should return the corresponding ICmp
812/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +0000813///
814/// To simplify this code we canonicalize the relation so that the first
815/// operand is always the most "complex" of the two. We consider simple
816/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000817/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000818///
Reid Spencer9d36acf2006-12-24 18:52:08 +0000819static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
820 const Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000821 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000822 assert(V1->getType() == V2->getType() &&
823 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000824 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000825
Reid Spenceraccd7c72004-07-17 23:47:01 +0000826 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000827 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
828 // We distilled this down to a simple case, use the standard constant
829 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000830 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000831 Constant *C1 = const_cast<Constant*>(V1);
832 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencer266e42b2006-12-23 06:05:41 +0000833 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000834 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
835 if (R && R->getBoolValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000836 return pred;
837 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000838 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
839 if (R && R->getBoolValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000840 return pred;
841 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000842 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
843 if (R && R->getBoolValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000844 return pred;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000845
846 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +0000847 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000848 }
849
Chris Lattner061da2f2004-01-13 05:51:55 +0000850 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +0000851 ICmpInst::Predicate SwappedRelation =
852 evaluateICmpRelation(V2, V1, isSigned);
853 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
854 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000855
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000856 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +0000857 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +0000858 ICmpInst::Predicate SwappedRelation =
859 evaluateICmpRelation(V2, V1, isSigned);
860 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
861 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000862 else
Reid Spencer266e42b2006-12-23 06:05:41 +0000863 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +0000864 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000865
Reid Spenceraccd7c72004-07-17 23:47:01 +0000866 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000867 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000868 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000869 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000870 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000871 } else {
Reid Spencer876f7222006-12-06 00:25:09 +0000872 // GlobalVals can never be null.
Chris Lattner061da2f2004-01-13 05:51:55 +0000873 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer876f7222006-12-06 00:25:09 +0000874 if (!CPR1->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000875 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000876 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000877 } else {
878 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
879 // constantexpr, a CPR, or a simple constant.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000880 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
881 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +0000882
883 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000884 case Instruction::Trunc:
885 case Instruction::FPTrunc:
886 case Instruction::FPExt:
887 case Instruction::FPToUI:
888 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +0000889 break; // We can't evaluate floating point casts or truncations.
890
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000891 case Instruction::UIToFP:
892 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000893 case Instruction::IntToPtr:
894 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +0000895 case Instruction::ZExt:
896 case Instruction::SExt:
897 case Instruction::PtrToInt:
Chris Lattner061da2f2004-01-13 05:51:55 +0000898 // If the cast is not actually changing bits, and the second operand is a
899 // null pointer, do the comparison with the pre-casted value.
900 if (V2->isNullValue() &&
Reid Spencer266e42b2006-12-23 06:05:41 +0000901 (isa<PointerType>(CE1->getType()) || CE1->getType()->isIntegral())) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000902 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000903 (CE1->getOpcode() == Instruction::SExt ? true :
904 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
905 return evaluateICmpRelation(
Reid Spencerccf78ac2006-12-23 10:21:26 +0000906 CE1Op0, Constant::getNullValue(CE1Op0->getType()), sgnd);
Reid Spencer266e42b2006-12-23 06:05:41 +0000907 }
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000908
909 // If the dest type is a pointer type, and the RHS is a constantexpr cast
910 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencer266e42b2006-12-23 06:05:41 +0000911 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000912 // which happens a lot in compilers with tagged integers.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000913 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer266e42b2006-12-23 06:05:41 +0000914 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000915 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
916 CE1->getOperand(0)->getType()->isIntegral()) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000917 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000918 (CE1->getOpcode() == Instruction::SExt ? true :
919 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
920 return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0),
Reid Spencerccf78ac2006-12-23 10:21:26 +0000921 sgnd);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000922 }
Chris Lattner192e3262004-04-11 01:29:30 +0000923 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000924
925 case Instruction::GetElementPtr:
926 // Ok, since this is a getelementptr, we know that the constant has a
927 // pointer type. Check the various cases.
928 if (isa<ConstantPointerNull>(V2)) {
929 // If we are comparing a GEP to a null pointer, check to see if the base
930 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000931 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000932 if (GV->hasExternalWeakLinkage())
933 // Weak linkage GVals could be zero or not. We're comparing that
934 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000935 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +0000936 else
937 // If its not weak linkage, the GVal must have a non-zero address
938 // so the result is greater-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000939 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000940 } else if (isa<ConstantPointerNull>(CE1Op0)) {
941 // If we are indexing from a null pointer, check to see if we have any
942 // non-zero indices.
943 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
944 if (!CE1->getOperand(i)->isNullValue())
945 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000946 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000947 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +0000948 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000949 }
950 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000951 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000952 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000953 if (CPR2->hasExternalWeakLinkage())
954 // Weak linkage GVals could be zero or not. We're comparing it to
955 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000956 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +0000957 else
958 // If its not weak linkage, the GVal must have a non-zero address
959 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000960 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000961 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000962 if (CPR1 == CPR2) {
963 // If this is a getelementptr of the same global, then it must be
964 // different. Because the types must match, the getelementptr could
965 // only have at most one index, and because we fold getelementptr's
966 // with a single zero index, it must be nonzero.
967 assert(CE1->getNumOperands() == 2 &&
968 !CE1->getOperand(1)->isNullValue() &&
969 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000970 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000971 } else {
972 // If they are different globals, we don't know what the value is,
973 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000974 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000975 }
976 }
977 } else {
978 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
979 const Constant *CE2Op0 = CE2->getOperand(0);
980
981 // There are MANY other foldings that we could perform here. They will
982 // probably be added on demand, as they seem needed.
983 switch (CE2->getOpcode()) {
984 default: break;
985 case Instruction::GetElementPtr:
986 // By far the most common case to handle is when the base pointers are
987 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000988 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000989 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000990 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000991 // Ok, we know that both getelementptr instructions are based on the
992 // same global. From this, we can precisely determine the relative
993 // ordering of the resultant pointers.
994 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000995
Chris Lattner061da2f2004-01-13 05:51:55 +0000996 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +0000997 gep_type_iterator GTI = gep_type_begin(CE1);
998 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
999 ++i, ++GTI)
1000 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1001 GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001002 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1003 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1004 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001005 }
1006
1007 // Ok, we ran out of things they have in common. If any leftovers
1008 // are non-zero then we have a difference, otherwise we are equal.
1009 for (; i < CE1->getNumOperands(); ++i)
1010 if (!CE1->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001011 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001012 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +00001013 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001014 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001015
Chris Lattner061da2f2004-01-13 05:51:55 +00001016 for (; i < CE2->getNumOperands(); ++i)
1017 if (!CE2->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001018 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001019 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001020 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001021 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1022 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001023 }
1024 }
1025 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001026 default:
1027 break;
1028 }
1029 }
1030
Reid Spencer266e42b2006-12-23 06:05:41 +00001031 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001032}
1033
Reid Spencer9d36acf2006-12-24 18:52:08 +00001034Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1035 const Constant *C1,
1036 const Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001037
1038 // Handle some degenerate cases first
1039 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Reid Spencer542964f2007-01-11 18:21:29 +00001040 return UndefValue::get(Type::Int1Ty);
Reid Spencer266e42b2006-12-23 06:05:41 +00001041
1042 // icmp eq/ne(null,GV) -> false/true
1043 if (C1->isNullValue()) {
1044 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1045 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001046 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001047 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001048 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001049 return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001050 // icmp eq/ne(GV,null) -> false/true
1051 } else if (C2->isNullValue()) {
1052 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1053 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001054 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001055 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001056 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001057 return ConstantInt::getTrue();
Chris Lattner1dd054c2004-01-12 22:07:24 +00001058 }
1059
Zhou Sheng75b871f2007-01-11 12:24:14 +00001060 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2) &&
Reid Spencer542964f2007-01-11 18:21:29 +00001061 C1->getType() == Type::Int1Ty && C2->getType() == Type::Int1Ty) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001062 bool C1Val = cast<ConstantInt>(C1)->getBoolValue();
1063 bool C2Val = cast<ConstantInt>(C2)->getBoolValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001064 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001065 default: assert(0 && "Invalid ICmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001066 case ICmpInst::ICMP_EQ: return ConstantInt::get(C1Val == C2Val);
1067 case ICmpInst::ICMP_NE: return ConstantInt::get(C1Val != C2Val);
1068 case ICmpInst::ICMP_ULT:return ConstantInt::get(C1Val < C2Val);
1069 case ICmpInst::ICMP_UGT:return ConstantInt::get(C1Val > C2Val);
1070 case ICmpInst::ICMP_ULE:return ConstantInt::get(C1Val <= C2Val);
1071 case ICmpInst::ICMP_UGE:return ConstantInt::get(C1Val >= C2Val);
1072 case ICmpInst::ICMP_SLT:return ConstantInt::get(C1Val < C2Val);
1073 case ICmpInst::ICMP_SGT:return ConstantInt::get(C1Val > C2Val);
1074 case ICmpInst::ICMP_SLE:return ConstantInt::get(C1Val <= C2Val);
1075 case ICmpInst::ICMP_SGE:return ConstantInt::get(C1Val >= C2Val);
Chris Lattner061da2f2004-01-13 05:51:55 +00001076 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001077 } else if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001078 if (ICmpInst::isSignedPredicate(ICmpInst::Predicate(pred))) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001079 int64_t V1 = cast<ConstantInt>(C1)->getSExtValue();
1080 int64_t V2 = cast<ConstantInt>(C2)->getSExtValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001081 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001082 default: assert(0 && "Invalid ICmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001083 case ICmpInst::ICMP_SLT:return ConstantInt::get(V1 < V2);
1084 case ICmpInst::ICMP_SGT:return ConstantInt::get(V1 > V2);
1085 case ICmpInst::ICMP_SLE:return ConstantInt::get(V1 <= V2);
1086 case ICmpInst::ICMP_SGE:return ConstantInt::get(V1 >= V2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001087 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001088 } else {
Reid Spencer266e42b2006-12-23 06:05:41 +00001089 uint64_t V1 = cast<ConstantInt>(C1)->getZExtValue();
1090 uint64_t V2 = cast<ConstantInt>(C2)->getZExtValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001091 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001092 default: assert(0 && "Invalid ICmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001093 case ICmpInst::ICMP_EQ: return ConstantInt::get(V1 == V2);
1094 case ICmpInst::ICMP_NE: return ConstantInt::get(V1 != V2);
1095 case ICmpInst::ICMP_ULT:return ConstantInt::get(V1 < V2);
1096 case ICmpInst::ICMP_UGT:return ConstantInt::get(V1 > V2);
1097 case ICmpInst::ICMP_ULE:return ConstantInt::get(V1 <= V2);
1098 case ICmpInst::ICMP_UGE:return ConstantInt::get(V1 >= V2);
Chris Lattner061da2f2004-01-13 05:51:55 +00001099 }
1100 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001101 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1102 double C1Val = cast<ConstantFP>(C1)->getValue();
1103 double C2Val = cast<ConstantFP>(C2)->getValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001104 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001105 default: assert(0 && "Invalid FCmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001106 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse();
1107 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001108 case FCmpInst::FCMP_UNO:
Zhou Sheng75b871f2007-01-11 12:24:14 +00001109 return ConstantInt::get(C1Val != C1Val || C2Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001110 case FCmpInst::FCMP_ORD:
Zhou Sheng75b871f2007-01-11 12:24:14 +00001111 return ConstantInt::get(C1Val == C1Val && C2Val == C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001112 case FCmpInst::FCMP_UEQ:
Reid Spencer74bd0362007-01-11 00:25:45 +00001113 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 */
Zhou Sheng75b871f2007-01-11 12:24:14 +00001116 case FCmpInst::FCMP_OEQ: return ConstantInt::get(C1Val == C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001117 case FCmpInst::FCMP_UNE:
1118 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001119 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001120 /* FALL THROUGH */
Zhou Sheng75b871f2007-01-11 12:24:14 +00001121 case FCmpInst::FCMP_ONE: return ConstantInt::get(C1Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001122 case FCmpInst::FCMP_ULT:
1123 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001124 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001125 /* FALL THROUGH */
Zhou Sheng75b871f2007-01-11 12:24:14 +00001126 case FCmpInst::FCMP_OLT: return ConstantInt::get(C1Val < C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001127 case FCmpInst::FCMP_UGT:
Reid Spencer74bd0362007-01-11 00:25:45 +00001128 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001129 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001130 /* FALL THROUGH */
Zhou Sheng75b871f2007-01-11 12:24:14 +00001131 case FCmpInst::FCMP_OGT: return ConstantInt::get(C1Val > C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001132 case FCmpInst::FCMP_ULE:
1133 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001134 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001135 /* FALL THROUGH */
Zhou Sheng75b871f2007-01-11 12:24:14 +00001136 case FCmpInst::FCMP_OLE: return ConstantInt::get(C1Val <= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001137 case FCmpInst::FCMP_UGE:
Reid Spencer74bd0362007-01-11 00:25:45 +00001138 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001139 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001140 /* FALL THROUGH */
Zhou Sheng75b871f2007-01-11 12:24:14 +00001141 case FCmpInst::FCMP_OGE: return ConstantInt::get(C1Val >= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001142 }
Reid Spencer9d36acf2006-12-24 18:52:08 +00001143 } else if (const ConstantPacked *CP1 = dyn_cast<ConstantPacked>(C1)) {
1144 if (const ConstantPacked *CP2 = dyn_cast<ConstantPacked>(C2)) {
1145 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
Zhou Sheng75b871f2007-01-11 12:24:14 +00001185 return ConstantInt::get(
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
Zhou Sheng75b871f2007-01-11 12:24:14 +00001190 return ConstantInt::get(
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
Zhou Sheng75b871f2007-01-11 12:24:14 +00001195 return ConstantInt::get(
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.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001230 return ConstantInt::get(pred == ICmpInst::ICMP_EQ ||
1231 pred == ICmpInst::ICMP_ULE ||
1232 pred == ICmpInst::ICMP_SLE ||
1233 pred == ICmpInst::ICMP_UGE ||
1234 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001235 case ICmpInst::ICMP_ULT:
1236 // If we know that C1 < C2, we can decide the result of this computation
1237 // precisely.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001238 return ConstantInt::get(pred == ICmpInst::ICMP_ULT ||
1239 pred == ICmpInst::ICMP_NE ||
1240 pred == ICmpInst::ICMP_ULE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001241 case ICmpInst::ICMP_SLT:
1242 // If we know that C1 < C2, we can decide the result of this computation
1243 // precisely.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001244 return ConstantInt::get(pred == ICmpInst::ICMP_SLT ||
1245 pred == ICmpInst::ICMP_NE ||
1246 pred == ICmpInst::ICMP_SLE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001247 case ICmpInst::ICMP_UGT:
1248 // If we know that C1 > C2, we can decide the result of this computation
1249 // precisely.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001250 return ConstantInt::get(pred == ICmpInst::ICMP_UGT ||
1251 pred == ICmpInst::ICMP_NE ||
1252 pred == ICmpInst::ICMP_UGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001253 case ICmpInst::ICMP_SGT:
1254 // If we know that C1 > C2, we can decide the result of this computation
1255 // precisely.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001256 return ConstantInt::get(pred == ICmpInst::ICMP_SGT ||
1257 pred == ICmpInst::ICMP_NE ||
1258 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001259 case ICmpInst::ICMP_ULE:
1260 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001261 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getFalse();
1262 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001263 break;
1264 case ICmpInst::ICMP_SLE:
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_SGT) return ConstantInt::getFalse();
1267 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001268 break;
1269
1270 case ICmpInst::ICMP_UGE:
1271 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001272 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getFalse();
1273 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001274 break;
1275 case ICmpInst::ICMP_SGE:
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_SLT) return ConstantInt::getFalse();
1278 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001279 break;
1280
1281 case ICmpInst::ICMP_NE:
1282 // If we know that C1 != C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001283 if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse();
1284 if (pred == ICmpInst::ICMP_NE) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001285 break;
1286 }
1287
1288 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
1289 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1290 // other way if possible.
1291 switch (pred) {
1292 case ICmpInst::ICMP_EQ:
1293 case ICmpInst::ICMP_NE:
1294 // No change of predicate required.
1295 return ConstantFoldCompareInstruction(pred, C2, C1);
1296
1297 case ICmpInst::ICMP_ULT:
1298 case ICmpInst::ICMP_SLT:
1299 case ICmpInst::ICMP_UGT:
1300 case ICmpInst::ICMP_SGT:
1301 case ICmpInst::ICMP_ULE:
1302 case ICmpInst::ICMP_SLE:
1303 case ICmpInst::ICMP_UGE:
1304 case ICmpInst::ICMP_SGE:
1305 // Change the predicate as necessary to swap the operands.
1306 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1307 return ConstantFoldCompareInstruction(pred, C2, C1);
1308
1309 default: // These predicates cannot be flopped around.
1310 break;
1311 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001312 }
1313 }
1314 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001315}
1316
1317Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner13128ab2004-10-11 22:52:25 +00001318 const std::vector<Value*> &IdxList) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001319 if (IdxList.size() == 0 ||
Chris Lattner13128ab2004-10-11 22:52:25 +00001320 (IdxList.size() == 1 && cast<Constant>(IdxList[0])->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001321 return const_cast<Constant*>(C);
1322
Chris Lattnerf6013752004-10-17 21:54:55 +00001323 if (isa<UndefValue>(C)) {
1324 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
1325 true);
1326 assert(Ty != 0 && "Invalid indices for GEP!");
1327 return UndefValue::get(PointerType::get(Ty));
1328 }
1329
1330 Constant *Idx0 = cast<Constant>(IdxList[0]);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001331 if (C->isNullValue()) {
1332 bool isNull = true;
1333 for (unsigned i = 0, e = IdxList.size(); i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001334 if (!cast<Constant>(IdxList[i])->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001335 isNull = false;
1336 break;
1337 }
1338 if (isNull) {
Chris Lattner13128ab2004-10-11 22:52:25 +00001339 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(), IdxList,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001340 true);
1341 assert(Ty != 0 && "Invalid indices for GEP!");
1342 return ConstantPointerNull::get(PointerType::get(Ty));
1343 }
Chris Lattner4bbd4092004-07-15 01:16:59 +00001344
1345 if (IdxList.size() == 1) {
1346 const Type *ElTy = cast<PointerType>(C->getType())->getElementType();
Reid Spencere0fc4df2006-10-20 07:07:24 +00001347 if (uint32_t ElSize = ElTy->getPrimitiveSize()) {
Chris Lattner4bbd4092004-07-15 01:16:59 +00001348 // gep null, C is equal to C*sizeof(nullty). If nullty is a known llvm
1349 // type, we can statically fold this.
Reid Spencer8d9336d2006-12-31 05:26:44 +00001350 Constant *R = ConstantInt::get(Type::Int32Ty, ElSize);
Reid Spencer1a063892006-12-04 02:46:44 +00001351 // We know R is unsigned, Idx0 is signed because it must be an index
1352 // through a sequential type (gep pointer operand) which is always
1353 // signed.
Reid Spencer27720a92006-12-05 03:30:09 +00001354 R = ConstantExpr::getSExtOrBitCast(R, Idx0->getType());
Reid Spencer1a063892006-12-04 02:46:44 +00001355 R = ConstantExpr::getMul(R, Idx0); // signed multiply
1356 // R is a signed integer, C is the GEP pointer so -> IntToPtr
Reid Spencerbb65ebf2006-12-12 23:36:14 +00001357 return ConstantExpr::getIntToPtr(R, C->getType());
Chris Lattner4bbd4092004-07-15 01:16:59 +00001358 }
1359 }
Chris Lattner04b60fe2004-02-16 20:46:13 +00001360 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001361
1362 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1363 // Combine Indices - If the source pointer to this getelementptr instruction
1364 // is a getelementptr instruction, combine the indices of the two
1365 // getelementptr instructions into a single instruction.
1366 //
1367 if (CE->getOpcode() == Instruction::GetElementPtr) {
1368 const Type *LastTy = 0;
1369 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1370 I != E; ++I)
1371 LastTy = *I;
1372
Chris Lattner13128ab2004-10-11 22:52:25 +00001373 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
1374 std::vector<Value*> NewIndices;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001375 NewIndices.reserve(IdxList.size() + CE->getNumOperands());
1376 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001377 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001378
1379 // Add the last index of the source with the first index of the new GEP.
1380 // Make sure to handle the case when they are actually different types.
1381 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001382 // Otherwise it must be an array.
1383 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001384 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001385 if (IdxTy != Idx0->getType()) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001386 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Reid Spencer27720a92006-12-05 03:30:09 +00001387 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001388 Type::Int64Ty);
Reid Spencer1a063892006-12-04 02:46:44 +00001389 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1390 } else {
1391 Combined =
1392 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1393 }
Chris Lattner71068a02004-07-07 04:45:13 +00001394 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001395
Chris Lattner1dd054c2004-01-12 22:07:24 +00001396 NewIndices.push_back(Combined);
1397 NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
1398 return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
1399 }
1400 }
1401
1402 // Implement folding of:
1403 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1404 // long 0, long 0)
1405 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1406 //
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001407 if (CE->isCast() && IdxList.size() > 1 && Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001408 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001409 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1410 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1411 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001412 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001413 if (CAT->getElementType() == SAT->getElementType())
1414 return ConstantExpr::getGetElementPtr(
1415 (Constant*)CE->getOperand(0), IdxList);
1416 }
1417 return 0;
1418}
1419