blob: 41078d97f1d8cb0eaf91b54830f1172a9fac6e48 [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
Chris Lattner6b3f4752006-04-02 01:38:28 +000038/// CastConstantPacked - Convert the specified ConstantPacked node to the
39/// specified packed type. At this point, we know that the elements of the
40/// input packed constant are all simple integer or FP values.
41static Constant *CastConstantPacked(ConstantPacked *CP,
42 const PackedType *DstTy) {
43 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));
Chris Lattner6b3f4752006-04-02 01:38:28 +000060 return ConstantPacked::get(Result);
61 }
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 }
73 return ConstantPacked::get(Result);
74 }
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 }
81 return ConstantPacked::get(Result);
82 }
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 }
94 return ConstantPacked::get(Result);
95 }
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 }
103 return ConstantPacked::get(Result);
104 }
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).
245 if (const PackedType *DestPTy = dyn_cast<PackedType>(DestTy)) {
246 if (const PackedType *SrcTy = dyn_cast<PackedType>(V->getType())) {
247 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
255 if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(V)) {
256 // This is a cast from a ConstantPacked of one type to a
257 // ConstantPacked of another type. Check to see if all elements of
258 // 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)
270 return CastConstantPacked(const_cast<ConstantPacked*>(CP), DestPTy);
271 }
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.
Reid Spencera94d3942007-01-19 21:13:56 +0000299 if (DestTy->isInteger())
Chris Lattner4d1da162006-12-11 18:30:27 +0000300 return ConstantInt::get(DestTy, DoubleToBits(FP->getValue()));
Chris Lattner4d1da162006-12-11 18:30:27 +0000301 }
302 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000303 default:
304 assert(!"Invalid CE CastInst opcode");
305 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000306 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000307
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000308 assert(0 && "Failed to cast constant expression");
309 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000310}
311
Chris Lattner6ea4b522004-03-12 05:53:32 +0000312Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
313 const Constant *V1,
314 const Constant *V2) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000315 if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
Reid Spencercddc9df2007-01-12 04:24:46 +0000316 return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000317
318 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
319 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
320 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000321 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000322 return 0;
323}
324
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000325Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
326 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000327 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
328 return UndefValue::get(cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000329 if (Val->isNullValue()) // ee(zero, x) -> zero
330 return Constant::getNullValue(
331 cast<PackedType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000332
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000333 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000334 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
335 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000336 } else if (isa<UndefValue>(Idx)) {
337 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
338 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000339 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000340 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000341 return 0;
342}
343
Robert Bocchinoca27f032006-01-17 20:07:22 +0000344Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
345 const Constant *Elt,
346 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000347 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000348 if (!CIdx) return 0;
Reid Spencere0fc4df2006-10-20 07:07:24 +0000349 uint64_t idxVal = CIdx->getZExtValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000350 if (isa<UndefValue>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000351 // Insertion of scalar constant into packed undef
352 // Optimize away insertion of undef
353 if (isa<UndefValue>(Elt))
354 return const_cast<Constant*>(Val);
355 // Otherwise break the aggregate undef into multiple undefs and do
356 // the insertion
357 unsigned numOps =
358 cast<PackedType>(Val->getType())->getNumElements();
359 std::vector<Constant*> Ops;
360 Ops.reserve(numOps);
361 for (unsigned i = 0; i < numOps; ++i) {
362 const Constant *Op =
363 (i == idxVal) ? Elt : UndefValue::get(Elt->getType());
364 Ops.push_back(const_cast<Constant*>(Op));
365 }
366 return ConstantPacked::get(Ops);
367 }
Reid Spencer3054b142006-11-02 08:18:15 +0000368 if (isa<ConstantAggregateZero>(Val)) {
Robert Bocchinoca27f032006-01-17 20:07:22 +0000369 // Insertion of scalar constant into packed aggregate zero
370 // Optimize away insertion of zero
371 if (Elt->isNullValue())
372 return const_cast<Constant*>(Val);
373 // Otherwise break the aggregate zero into multiple zeros and do
374 // the insertion
375 unsigned numOps =
376 cast<PackedType>(Val->getType())->getNumElements();
377 std::vector<Constant*> Ops;
378 Ops.reserve(numOps);
379 for (unsigned i = 0; i < numOps; ++i) {
380 const Constant *Op =
381 (i == idxVal) ? Elt : Constant::getNullValue(Elt->getType());
382 Ops.push_back(const_cast<Constant*>(Op));
383 }
384 return ConstantPacked::get(Ops);
385 }
386 if (const ConstantPacked *CVal = dyn_cast<ConstantPacked>(Val)) {
387 // Insertion of scalar constant into packed constant
388 std::vector<Constant*> Ops;
389 Ops.reserve(CVal->getNumOperands());
390 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
391 const Constant *Op =
392 (i == idxVal) ? Elt : cast<Constant>(CVal->getOperand(i));
393 Ops.push_back(const_cast<Constant*>(Op));
394 }
395 return ConstantPacked::get(Ops);
396 }
397 return 0;
398}
399
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000400Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
401 const Constant *V2,
402 const Constant *Mask) {
403 // TODO:
404 return 0;
405}
406
Reid Spencer266e42b2006-12-23 06:05:41 +0000407/// EvalVectorOp - Given two packed constants and a function pointer, apply the
408/// function pointer to each element pair, producing a new ConstantPacked
409/// constant.
410static Constant *EvalVectorOp(const ConstantPacked *V1,
411 const ConstantPacked *V2,
412 Constant *(*FP)(Constant*, Constant*)) {
413 std::vector<Constant*> Res;
414 for (unsigned i = 0, e = V1->getNumOperands(); i != e; ++i)
415 Res.push_back(FP(const_cast<Constant*>(V1->getOperand(i)),
416 const_cast<Constant*>(V2->getOperand(i))));
417 return ConstantPacked::get(Res);
418}
419
420Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
421 const Constant *C1,
422 const Constant *C2) {
423 // Handle UndefValue up front
424 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
425 switch (Opcode) {
426 case Instruction::Add:
427 case Instruction::Sub:
428 case Instruction::Xor:
429 return UndefValue::get(C1->getType());
430 case Instruction::Mul:
431 case Instruction::And:
432 return Constant::getNullValue(C1->getType());
433 case Instruction::UDiv:
434 case Instruction::SDiv:
435 case Instruction::FDiv:
436 case Instruction::URem:
437 case Instruction::SRem:
438 case Instruction::FRem:
439 if (!isa<UndefValue>(C2)) // undef / X -> 0
440 return Constant::getNullValue(C1->getType());
441 return const_cast<Constant*>(C2); // X / undef -> undef
442 case Instruction::Or: // X | undef -> -1
Chris Lattner26f13eb2007-01-04 01:56:39 +0000443 if (const PackedType *PTy = dyn_cast<PackedType>(C1->getType()))
444 return ConstantPacked::getAllOnesValue(PTy);
Reid Spencer266e42b2006-12-23 06:05:41 +0000445 return ConstantInt::getAllOnesValue(C1->getType());
446 case Instruction::LShr:
447 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
448 return const_cast<Constant*>(C1); // undef lshr undef -> undef
449 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
450 // undef lshr X -> 0
451 case Instruction::AShr:
452 if (!isa<UndefValue>(C2))
453 return const_cast<Constant*>(C1); // undef ashr X --> undef
454 else if (isa<UndefValue>(C1))
455 return const_cast<Constant*>(C1); // undef ashr undef -> undef
456 else
457 return const_cast<Constant*>(C1); // X ashr undef --> X
458 case Instruction::Shl:
459 // undef << X -> 0 or X << undef -> 0
460 return Constant::getNullValue(C1->getType());
461 }
462 }
463
464 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
465 if (isa<ConstantExpr>(C2)) {
466 // There are many possible foldings we could do here. We should probably
467 // at least fold add of a pointer with an integer into the appropriate
468 // getelementptr. This will improve alias analysis a bit.
469 } else {
470 // Just implement a couple of simple identities.
471 switch (Opcode) {
472 case Instruction::Add:
473 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X + 0 == X
474 break;
475 case Instruction::Sub:
476 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X - 0 == X
477 break;
478 case Instruction::Mul:
479 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X * 0 == 0
480 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
481 if (CI->getZExtValue() == 1)
482 return const_cast<Constant*>(C1); // X * 1 == X
483 break;
484 case Instruction::UDiv:
485 case Instruction::SDiv:
486 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
487 if (CI->getZExtValue() == 1)
488 return const_cast<Constant*>(C1); // X / 1 == X
489 break;
490 case Instruction::URem:
491 case Instruction::SRem:
492 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
493 if (CI->getZExtValue() == 1)
494 return Constant::getNullValue(CI->getType()); // X % 1 == 0
495 break;
496 case Instruction::And:
Chris Lattner26f13eb2007-01-04 01:56:39 +0000497 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
498 if (CI->isAllOnesValue())
499 return const_cast<Constant*>(C1); // X & -1 == X
Reid Spencer266e42b2006-12-23 06:05:41 +0000500 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X & 0 == 0
501 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
502 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
503
504 // Functions are at least 4-byte aligned. If and'ing the address of a
505 // function with a constant < 4, fold it to zero.
506 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
507 if (CI->getZExtValue() < 4 && isa<Function>(CPR))
508 return Constant::getNullValue(CI->getType());
509 }
510 break;
511 case Instruction::Or:
512 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X | 0 == X
Chris Lattner26f13eb2007-01-04 01:56:39 +0000513 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
514 if (CI->isAllOnesValue())
515 return const_cast<Constant*>(C2); // X | -1 == -1
Reid Spencer266e42b2006-12-23 06:05:41 +0000516 break;
517 case Instruction::Xor:
518 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X ^ 0 == X
519 break;
520 }
521 }
522 } else if (isa<ConstantExpr>(C2)) {
523 // If C2 is a constant expr and C1 isn't, flop them around and fold the
524 // other way if possible.
525 switch (Opcode) {
526 case Instruction::Add:
527 case Instruction::Mul:
528 case Instruction::And:
529 case Instruction::Or:
530 case Instruction::Xor:
531 // No change of opcode required.
532 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
533
534 case Instruction::Shl:
535 case Instruction::LShr:
536 case Instruction::AShr:
537 case Instruction::Sub:
538 case Instruction::SDiv:
539 case Instruction::UDiv:
540 case Instruction::FDiv:
541 case Instruction::URem:
542 case Instruction::SRem:
543 case Instruction::FRem:
544 default: // These instructions cannot be flopped around.
545 return 0;
546 }
547 }
548
549 // At this point we know neither constant is an UndefValue nor a ConstantExpr
Chris Lattner26f13eb2007-01-04 01:56:39 +0000550 // so look at directly computing the value.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000551 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
552 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Chris Lattner344da522007-01-12 18:42:52 +0000553 uint64_t C1Val = CI1->getZExtValue();
554 uint64_t C2Val = CI2->getZExtValue();
555 switch (Opcode) {
556 default:
557 break;
558 case Instruction::Add:
559 return ConstantInt::get(C1->getType(), C1Val + C2Val);
560 case Instruction::Sub:
561 return ConstantInt::get(C1->getType(), C1Val - C2Val);
562 case Instruction::Mul:
563 return ConstantInt::get(C1->getType(), C1Val * C2Val);
564 case Instruction::UDiv:
565 if (CI2->isNullValue()) // X / 0 -> can't fold
566 return 0;
567 return ConstantInt::get(C1->getType(), C1Val / C2Val);
568 case Instruction::SDiv:
569 if (CI2->isNullValue()) return 0; // X / 0 -> can't fold
570 if (CI2->isAllOnesValue() &&
571 (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
572 (CI1->getSExtValue() == INT64_MIN)) ||
573 (CI1->getSExtValue() == -CI1->getSExtValue())))
574 return 0; // MIN_INT / -1 -> overflow
575 return ConstantInt::get(C1->getType(),
576 CI1->getSExtValue() / CI2->getSExtValue());
577 case Instruction::URem:
578 if (C2->isNullValue()) return 0; // X / 0 -> can't fold
579 return ConstantInt::get(C1->getType(), C1Val % C2Val);
580 case Instruction::SRem:
581 if (CI2->isNullValue()) return 0; // X % 0 -> can't fold
582 if (CI2->isAllOnesValue() &&
583 (((CI1->getType()->getPrimitiveSizeInBits() == 64) &&
584 (CI1->getSExtValue() == INT64_MIN)) ||
585 (CI1->getSExtValue() == -CI1->getSExtValue())))
586 return 0; // MIN_INT % -1 -> overflow
587 return ConstantInt::get(C1->getType(),
588 CI1->getSExtValue() % CI2->getSExtValue());
589 case Instruction::And:
590 return ConstantInt::get(C1->getType(), C1Val & C2Val);
591 case Instruction::Or:
592 return ConstantInt::get(C1->getType(), C1Val | C2Val);
593 case Instruction::Xor:
594 return ConstantInt::get(C1->getType(), C1Val ^ C2Val);
595 case Instruction::Shl:
596 return ConstantInt::get(C1->getType(), C1Val << C2Val);
597 case Instruction::LShr:
598 return ConstantInt::get(C1->getType(), C1Val >> C2Val);
599 case Instruction::AShr:
600 return ConstantInt::get(C1->getType(),
601 CI1->getSExtValue() >> C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +0000602 }
603 }
604 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
605 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
606 double C1Val = CFP1->getValue();
607 double C2Val = CFP2->getValue();
608 switch (Opcode) {
609 default:
610 break;
611 case Instruction::Add:
612 return ConstantFP::get(CFP1->getType(), C1Val + C2Val);
613 case Instruction::Sub:
614 return ConstantFP::get(CFP1->getType(), C1Val - C2Val);
615 case Instruction::Mul:
616 return ConstantFP::get(CFP1->getType(), C1Val * C2Val);
617 case Instruction::FDiv:
618 if (CFP2->isExactlyValue(0.0))
619 return ConstantFP::get(CFP1->getType(),
620 std::numeric_limits<double>::infinity());
621 if (CFP2->isExactlyValue(-0.0))
622 return ConstantFP::get(CFP1->getType(),
623 -std::numeric_limits<double>::infinity());
624 return ConstantFP::get(CFP1->getType(), C1Val / C2Val);
625 case Instruction::FRem:
626 if (CFP2->isNullValue())
627 return 0;
628 return ConstantFP::get(CFP1->getType(), std::fmod(C1Val, C2Val));
629 }
630 }
631 } else if (const ConstantPacked *CP1 = dyn_cast<ConstantPacked>(C1)) {
632 if (const ConstantPacked *CP2 = dyn_cast<ConstantPacked>(C2)) {
633 switch (Opcode) {
634 default:
635 break;
636 case Instruction::Add:
637 return EvalVectorOp(CP1, CP2, ConstantExpr::getAdd);
638 case Instruction::Sub:
639 return EvalVectorOp(CP1, CP2, ConstantExpr::getSub);
640 case Instruction::Mul:
641 return EvalVectorOp(CP1, CP2, ConstantExpr::getMul);
642 case Instruction::UDiv:
643 return EvalVectorOp(CP1, CP2, ConstantExpr::getUDiv);
644 case Instruction::SDiv:
645 return EvalVectorOp(CP1, CP2, ConstantExpr::getSDiv);
646 case Instruction::FDiv:
647 return EvalVectorOp(CP1, CP2, ConstantExpr::getFDiv);
648 case Instruction::URem:
649 return EvalVectorOp(CP1, CP2, ConstantExpr::getURem);
650 case Instruction::SRem:
651 return EvalVectorOp(CP1, CP2, ConstantExpr::getSRem);
652 case Instruction::FRem:
653 return EvalVectorOp(CP1, CP2, ConstantExpr::getFRem);
654 case Instruction::And:
655 return EvalVectorOp(CP1, CP2, ConstantExpr::getAnd);
656 case Instruction::Or:
657 return EvalVectorOp(CP1, CP2, ConstantExpr::getOr);
658 case Instruction::Xor:
659 return EvalVectorOp(CP1, CP2, ConstantExpr::getXor);
660 }
661 }
662 }
663
664 // We don't know how to fold this
665 return 0;
666}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000667
Chris Lattner60c47262005-01-28 19:09:51 +0000668/// isZeroSizedType - This type is zero sized if its an array or structure of
669/// zero sized types. The only leaf zero sized type is an empty structure.
670static bool isMaybeZeroSizedType(const Type *Ty) {
671 if (isa<OpaqueType>(Ty)) return true; // Can't say.
672 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
673
674 // If all of elements have zero size, this does too.
675 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000676 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000677 return true;
678
679 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
680 return isMaybeZeroSizedType(ATy->getElementType());
681 }
682 return false;
683}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000684
Chris Lattner061da2f2004-01-13 05:51:55 +0000685/// IdxCompare - Compare the two constants as though they were getelementptr
686/// indices. This allows coersion of the types to be the same thing.
687///
688/// If the two constants are the "same" (after coersion), return 0. If the
689/// first is less than the second, return -1, if the second is less than the
690/// first, return 1. If the constants are not integral, return -2.
691///
Chris Lattner60c47262005-01-28 19:09:51 +0000692static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000693 if (C1 == C2) return 0;
694
Reid Spencerc90cf772006-12-31 21:43:30 +0000695 // Ok, we found a different index. If they are not ConstantInt, we can't do
696 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +0000697 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
698 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000699
Chris Lattner69193f92004-04-05 01:30:19 +0000700 // Ok, we have two differing integer indices. Sign extend them to be the same
701 // type. Long is always big enough, so we use it.
Reid Spencer8d9336d2006-12-31 05:26:44 +0000702 if (C1->getType() != Type::Int64Ty)
703 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000704
Reid Spencer8d9336d2006-12-31 05:26:44 +0000705 if (C2->getType() != Type::Int64Ty)
Reid Spencerc90cf772006-12-31 21:43:30 +0000706 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000707
708 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +0000709
Chris Lattner60c47262005-01-28 19:09:51 +0000710 // If the type being indexed over is really just a zero sized type, there is
711 // no pointer difference being made here.
712 if (isMaybeZeroSizedType(ElTy))
713 return -2; // dunno.
714
Chris Lattner061da2f2004-01-13 05:51:55 +0000715 // If they are really different, now that they are the same type, then we
716 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +0000717 if (cast<ConstantInt>(C1)->getSExtValue() <
718 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +0000719 return -1;
720 else
721 return 1;
722}
723
Chris Lattner858f4e92007-01-04 02:13:20 +0000724/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +0000725/// decide about the two constants provided. This doesn't need to handle simple
726/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
727/// If we can determine that the two constants have a particular relation to
728/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +0000729/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
730/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +0000731///
732/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +0000733/// operand is always the most "complex" of the two. We consider ConstantFP
734/// to be the simplest, and ConstantExprs to be the most complex.
735static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1,
736 const Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000737 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +0000738 "Cannot compare values of different types!");
739 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +0000740 if (V1 == V2) return FCmpInst::FCMP_OEQ;
741
Reid Spencer9d36acf2006-12-24 18:52:08 +0000742 if (!isa<ConstantExpr>(V1)) {
743 if (!isa<ConstantExpr>(V2)) {
744 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +0000745 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000746 Constant *C1 = const_cast<Constant*>(V1);
747 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng75b871f2007-01-11 12:24:14 +0000748 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000749 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000750 if (R && R->getZExtValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000751 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000752 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000753 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, 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_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000756 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000757 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000758 if (R && R->getZExtValue())
Reid Spencer9d36acf2006-12-24 18:52:08 +0000759 return FCmpInst::FCMP_OGT;
760
761 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +0000762 return FCmpInst::BAD_FCMP_PREDICATE;
763 }
764
Reid Spencer9d36acf2006-12-24 18:52:08 +0000765 // If the first operand is simple and second is ConstantExpr, swap operands.
766 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
767 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
768 return FCmpInst::getSwappedPredicate(SwappedRelation);
769 } else {
770 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
771 // constantexpr or a simple constant.
772 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
773 switch (CE1->getOpcode()) {
774 case Instruction::FPTrunc:
775 case Instruction::FPExt:
776 case Instruction::UIToFP:
777 case Instruction::SIToFP:
778 // We might be able to do something with these but we don't right now.
779 break;
780 default:
781 break;
782 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000783 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000784 // There are MANY other foldings that we could perform here. They will
785 // probably be added on demand, as they seem needed.
786 return FCmpInst::BAD_FCMP_PREDICATE;
787}
788
789/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +0000790/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000791/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +0000792/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +0000793/// particular relation to each other, we should return the corresponding ICmp
794/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +0000795///
796/// To simplify this code we canonicalize the relation so that the first
797/// operand is always the most "complex" of the two. We consider simple
798/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000799/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000800///
Reid Spencer9d36acf2006-12-24 18:52:08 +0000801static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
802 const Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000803 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000804 assert(V1->getType() == V2->getType() &&
805 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000806 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000807
Reid Spenceraccd7c72004-07-17 23:47:01 +0000808 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000809 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
810 // We distilled this down to a simple case, use the standard constant
811 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000812 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000813 Constant *C1 = const_cast<Constant*>(V1);
814 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencer266e42b2006-12-23 06:05:41 +0000815 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000816 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencercddc9df2007-01-12 04:24:46 +0000817 if (R && R->getZExtValue())
Reid Spencer266e42b2006-12-23 06:05:41 +0000818 return pred;
819 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
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_SGT : ICmpInst::ICMP_UGT;
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;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000827
828 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +0000829 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000830 }
831
Chris Lattner061da2f2004-01-13 05:51:55 +0000832 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +0000833 ICmpInst::Predicate SwappedRelation =
834 evaluateICmpRelation(V2, V1, isSigned);
835 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
836 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000837
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000838 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +0000839 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +0000840 ICmpInst::Predicate SwappedRelation =
841 evaluateICmpRelation(V2, V1, isSigned);
842 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
843 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000844 else
Reid Spencer266e42b2006-12-23 06:05:41 +0000845 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +0000846 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000847
Reid Spenceraccd7c72004-07-17 23:47:01 +0000848 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000849 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000850 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000851 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000852 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000853 } else {
Reid Spencer876f7222006-12-06 00:25:09 +0000854 // GlobalVals can never be null.
Chris Lattner061da2f2004-01-13 05:51:55 +0000855 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Reid Spencer876f7222006-12-06 00:25:09 +0000856 if (!CPR1->hasExternalWeakLinkage())
Reid Spencer266e42b2006-12-23 06:05:41 +0000857 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000858 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000859 } else {
860 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
861 // constantexpr, a CPR, or a simple constant.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000862 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
863 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +0000864
865 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000866 case Instruction::Trunc:
867 case Instruction::FPTrunc:
868 case Instruction::FPExt:
869 case Instruction::FPToUI:
870 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +0000871 break; // We can't evaluate floating point casts or truncations.
872
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000873 case Instruction::UIToFP:
874 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000875 case Instruction::IntToPtr:
876 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +0000877 case Instruction::ZExt:
878 case Instruction::SExt:
879 case Instruction::PtrToInt:
Chris Lattner061da2f2004-01-13 05:51:55 +0000880 // If the cast is not actually changing bits, and the second operand is a
881 // null pointer, do the comparison with the pre-casted value.
882 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000883 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000884 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000885 (CE1->getOpcode() == Instruction::SExt ? true :
886 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
887 return evaluateICmpRelation(
Reid Spencerccf78ac2006-12-23 10:21:26 +0000888 CE1Op0, Constant::getNullValue(CE1Op0->getType()), sgnd);
Reid Spencer266e42b2006-12-23 06:05:41 +0000889 }
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000890
891 // If the dest type is a pointer type, and the RHS is a constantexpr cast
892 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencer266e42b2006-12-23 06:05:41 +0000893 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000894 // which happens a lot in compilers with tagged integers.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000895 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer266e42b2006-12-23 06:05:41 +0000896 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000897 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000898 CE1->getOperand(0)->getType()->isInteger()) {
Reid Spencerccf78ac2006-12-23 10:21:26 +0000899 bool sgnd = CE1->getOpcode() == Instruction::ZExt ? false :
Reid Spencer266e42b2006-12-23 06:05:41 +0000900 (CE1->getOpcode() == Instruction::SExt ? true :
901 (CE1->getOpcode() == Instruction::PtrToInt ? false : isSigned));
902 return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0),
Reid Spencerccf78ac2006-12-23 10:21:26 +0000903 sgnd);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000904 }
Chris Lattner192e3262004-04-11 01:29:30 +0000905 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000906
907 case Instruction::GetElementPtr:
908 // Ok, since this is a getelementptr, we know that the constant has a
909 // pointer type. Check the various cases.
910 if (isa<ConstantPointerNull>(V2)) {
911 // If we are comparing a GEP to a null pointer, check to see if the base
912 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000913 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000914 if (GV->hasExternalWeakLinkage())
915 // Weak linkage GVals could be zero or not. We're comparing that
916 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000917 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +0000918 else
919 // If its not weak linkage, the GVal must have a non-zero address
920 // so the result is greater-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000921 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000922 } else if (isa<ConstantPointerNull>(CE1Op0)) {
923 // If we are indexing from a null pointer, check to see if we have any
924 // non-zero indices.
925 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
926 if (!CE1->getOperand(i)->isNullValue())
927 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000928 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000929 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +0000930 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000931 }
932 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000933 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000934 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000935 if (CPR2->hasExternalWeakLinkage())
936 // Weak linkage GVals could be zero or not. We're comparing it to
937 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000938 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +0000939 else
940 // If its not weak linkage, the GVal must have a non-zero address
941 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000942 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000943 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000944 if (CPR1 == CPR2) {
945 // If this is a getelementptr of the same global, then it must be
946 // different. Because the types must match, the getelementptr could
947 // only have at most one index, and because we fold getelementptr's
948 // with a single zero index, it must be nonzero.
949 assert(CE1->getNumOperands() == 2 &&
950 !CE1->getOperand(1)->isNullValue() &&
951 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000952 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000953 } else {
954 // If they are different globals, we don't know what the value is,
955 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000956 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000957 }
958 }
959 } else {
960 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
961 const Constant *CE2Op0 = CE2->getOperand(0);
962
963 // There are MANY other foldings that we could perform here. They will
964 // probably be added on demand, as they seem needed.
965 switch (CE2->getOpcode()) {
966 default: break;
967 case Instruction::GetElementPtr:
968 // By far the most common case to handle is when the base pointers are
969 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000970 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000971 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000972 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000973 // Ok, we know that both getelementptr instructions are based on the
974 // same global. From this, we can precisely determine the relative
975 // ordering of the resultant pointers.
976 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000977
Chris Lattner061da2f2004-01-13 05:51:55 +0000978 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +0000979 gep_type_iterator GTI = gep_type_begin(CE1);
980 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
981 ++i, ++GTI)
982 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
983 GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000984 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
985 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
986 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000987 }
988
989 // Ok, we ran out of things they have in common. If any leftovers
990 // are non-zero then we have a difference, otherwise we are equal.
991 for (; i < CE1->getNumOperands(); ++i)
992 if (!CE1->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +0000993 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +0000994 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +0000995 else
Reid Spencer266e42b2006-12-23 06:05:41 +0000996 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000997
Chris Lattner061da2f2004-01-13 05:51:55 +0000998 for (; i < CE2->getNumOperands(); ++i)
999 if (!CE2->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001000 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001001 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001002 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001003 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1004 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001005 }
1006 }
1007 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001008 default:
1009 break;
1010 }
1011 }
1012
Reid Spencer266e42b2006-12-23 06:05:41 +00001013 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001014}
1015
Reid Spencer9d36acf2006-12-24 18:52:08 +00001016Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1017 const Constant *C1,
1018 const Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001019
1020 // Handle some degenerate cases first
1021 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Reid Spencer542964f2007-01-11 18:21:29 +00001022 return UndefValue::get(Type::Int1Ty);
Reid Spencer266e42b2006-12-23 06:05:41 +00001023
1024 // icmp eq/ne(null,GV) -> false/true
1025 if (C1->isNullValue()) {
1026 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1027 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001028 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001029 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001030 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001031 return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001032 // icmp eq/ne(GV,null) -> false/true
1033 } else if (C2->isNullValue()) {
1034 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1035 if (!GV->hasExternalWeakLinkage()) // External weak GV can be null
Reid Spencer9d36acf2006-12-24 18:52:08 +00001036 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001037 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001038 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001039 return ConstantInt::getTrue();
Chris Lattner1dd054c2004-01-12 22:07:24 +00001040 }
1041
Chris Lattner344da522007-01-12 18:42:52 +00001042 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001043 if (ICmpInst::isSignedPredicate(ICmpInst::Predicate(pred))) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001044 int64_t V1 = cast<ConstantInt>(C1)->getSExtValue();
1045 int64_t V2 = cast<ConstantInt>(C2)->getSExtValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001046 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001047 default: assert(0 && "Invalid ICmp Predicate"); return 0;
Reid Spencercddc9df2007-01-12 04:24:46 +00001048 case ICmpInst::ICMP_SLT:return ConstantInt::get(Type::Int1Ty, V1 < V2);
1049 case ICmpInst::ICMP_SGT:return ConstantInt::get(Type::Int1Ty, V1 > V2);
1050 case ICmpInst::ICMP_SLE:return ConstantInt::get(Type::Int1Ty, V1 <= V2);
1051 case ICmpInst::ICMP_SGE:return ConstantInt::get(Type::Int1Ty, V1 >= V2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001052 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001053 } else {
Reid Spencer266e42b2006-12-23 06:05:41 +00001054 uint64_t V1 = cast<ConstantInt>(C1)->getZExtValue();
1055 uint64_t V2 = cast<ConstantInt>(C2)->getZExtValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001056 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001057 default: assert(0 && "Invalid ICmp Predicate"); return 0;
Reid Spencercddc9df2007-01-12 04:24:46 +00001058 case ICmpInst::ICMP_EQ: return ConstantInt::get(Type::Int1Ty, V1 == V2);
1059 case ICmpInst::ICMP_NE: return ConstantInt::get(Type::Int1Ty, V1 != V2);
1060 case ICmpInst::ICMP_ULT:return ConstantInt::get(Type::Int1Ty, V1 < V2);
1061 case ICmpInst::ICMP_UGT:return ConstantInt::get(Type::Int1Ty, V1 > V2);
1062 case ICmpInst::ICMP_ULE:return ConstantInt::get(Type::Int1Ty, V1 <= V2);
1063 case ICmpInst::ICMP_UGE:return ConstantInt::get(Type::Int1Ty, V1 >= V2);
Chris Lattner061da2f2004-01-13 05:51:55 +00001064 }
1065 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001066 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1067 double C1Val = cast<ConstantFP>(C1)->getValue();
1068 double C2Val = cast<ConstantFP>(C2)->getValue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001069 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001070 default: assert(0 && "Invalid FCmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001071 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse();
1072 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001073 case FCmpInst::FCMP_UNO:
Reid Spencercddc9df2007-01-12 04:24:46 +00001074 return ConstantInt::get(Type::Int1Ty, C1Val != C1Val || C2Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001075 case FCmpInst::FCMP_ORD:
Reid Spencercddc9df2007-01-12 04:24:46 +00001076 return ConstantInt::get(Type::Int1Ty, C1Val == C1Val && C2Val == C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001077 case FCmpInst::FCMP_UEQ:
Reid Spencer74bd0362007-01-11 00:25:45 +00001078 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001079 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001080 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001081 case FCmpInst::FCMP_OEQ:
1082 return ConstantInt::get(Type::Int1Ty, C1Val == C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001083 case FCmpInst::FCMP_UNE:
1084 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001085 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001086 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001087 case FCmpInst::FCMP_ONE:
1088 return ConstantInt::get(Type::Int1Ty, C1Val != C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001089 case FCmpInst::FCMP_ULT:
1090 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001091 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001092 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001093 case FCmpInst::FCMP_OLT:
1094 return ConstantInt::get(Type::Int1Ty, C1Val < C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001095 case FCmpInst::FCMP_UGT:
Reid Spencer74bd0362007-01-11 00:25:45 +00001096 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001097 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001098 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001099 case FCmpInst::FCMP_OGT:
1100 return ConstantInt::get(Type::Int1Ty, C1Val > C2Val);
Reid Spencer74bd0362007-01-11 00:25:45 +00001101 case FCmpInst::FCMP_ULE:
1102 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001103 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001104 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001105 case FCmpInst::FCMP_OLE:
1106 return ConstantInt::get(Type::Int1Ty, C1Val <= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001107 case FCmpInst::FCMP_UGE:
Reid Spencer74bd0362007-01-11 00:25:45 +00001108 if (C1Val != C1Val || C2Val != C2Val)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001109 return ConstantInt::getTrue();
Reid Spencer74bd0362007-01-11 00:25:45 +00001110 /* FALL THROUGH */
Reid Spencercddc9df2007-01-12 04:24:46 +00001111 case FCmpInst::FCMP_OGE:
1112 return ConstantInt::get(Type::Int1Ty, C1Val >= C2Val);
Reid Spencer266e42b2006-12-23 06:05:41 +00001113 }
Reid Spencer9d36acf2006-12-24 18:52:08 +00001114 } else if (const ConstantPacked *CP1 = dyn_cast<ConstantPacked>(C1)) {
1115 if (const ConstantPacked *CP2 = dyn_cast<ConstantPacked>(C2)) {
1116 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001117 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1118 Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
1119 const_cast<Constant*>(CP1->getOperand(i)),
1120 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001121 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001122 return CB;
1123 }
1124 // Otherwise, could not decide from any element pairs.
1125 return 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001126 } else if (pred == ICmpInst::ICMP_EQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001127 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1128 Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ,
1129 const_cast<Constant*>(CP1->getOperand(i)),
1130 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001131 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001132 return CB;
1133 }
1134 // Otherwise, could not decide from any element pairs.
1135 return 0;
1136 }
1137 }
1138 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001139
Reid Spencer9d36acf2006-12-24 18:52:08 +00001140 if (C1->getType()->isFloatingPoint()) {
1141 switch (evaluateFCmpRelation(C1, C2)) {
1142 default: assert(0 && "Unknown relation!");
1143 case FCmpInst::FCMP_UNO:
1144 case FCmpInst::FCMP_ORD:
1145 case FCmpInst::FCMP_UEQ:
1146 case FCmpInst::FCMP_UNE:
1147 case FCmpInst::FCMP_ULT:
1148 case FCmpInst::FCMP_UGT:
1149 case FCmpInst::FCMP_ULE:
1150 case FCmpInst::FCMP_UGE:
1151 case FCmpInst::FCMP_TRUE:
1152 case FCmpInst::FCMP_FALSE:
1153 case FCmpInst::BAD_FCMP_PREDICATE:
1154 break; // Couldn't determine anything about these constants.
1155 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001156 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001157 pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1158 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1159 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1160 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001161 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001162 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1163 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1164 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1165 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001166 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001167 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1168 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1169 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1170 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1171 // We can only partially decide this relation.
1172 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001173 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001174 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001175 return ConstantInt::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001176 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001177 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1178 // We can only partially decide this relation.
1179 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001180 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001181 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001182 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001183 break;
1184 case ICmpInst::ICMP_NE: // We know that C1 != C2
1185 // We can only partially decide this relation.
1186 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001187 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001188 if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001189 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001190 break;
1191 }
1192 } else {
1193 // Evaluate the relation between the two constants, per the predicate.
1194 switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
1195 default: assert(0 && "Unknown relational!");
1196 case ICmpInst::BAD_ICMP_PREDICATE:
1197 break; // Couldn't determine anything about these constants.
1198 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1199 // If we know the constants are equal, we can decide the result of this
1200 // computation precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001201 return ConstantInt::get(Type::Int1Ty,
1202 pred == ICmpInst::ICMP_EQ ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001203 pred == ICmpInst::ICMP_ULE ||
1204 pred == ICmpInst::ICMP_SLE ||
1205 pred == ICmpInst::ICMP_UGE ||
1206 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001207 case ICmpInst::ICMP_ULT:
1208 // If we know that C1 < C2, we can decide the result of this computation
1209 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001210 return ConstantInt::get(Type::Int1Ty,
1211 pred == ICmpInst::ICMP_ULT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001212 pred == ICmpInst::ICMP_NE ||
1213 pred == ICmpInst::ICMP_ULE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001214 case ICmpInst::ICMP_SLT:
1215 // If we know that C1 < C2, we can decide the result of this computation
1216 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001217 return ConstantInt::get(Type::Int1Ty,
1218 pred == ICmpInst::ICMP_SLT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001219 pred == ICmpInst::ICMP_NE ||
1220 pred == ICmpInst::ICMP_SLE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001221 case ICmpInst::ICMP_UGT:
1222 // If we know that C1 > C2, we can decide the result of this computation
1223 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001224 return ConstantInt::get(Type::Int1Ty,
1225 pred == ICmpInst::ICMP_UGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001226 pred == ICmpInst::ICMP_NE ||
1227 pred == ICmpInst::ICMP_UGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001228 case ICmpInst::ICMP_SGT:
1229 // If we know that C1 > C2, we can decide the result of this computation
1230 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001231 return ConstantInt::get(Type::Int1Ty,
1232 pred == ICmpInst::ICMP_SGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001233 pred == ICmpInst::ICMP_NE ||
1234 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001235 case ICmpInst::ICMP_ULE:
1236 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001237 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getFalse();
1238 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001239 break;
1240 case ICmpInst::ICMP_SLE:
1241 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001242 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getFalse();
1243 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001244 break;
1245
1246 case ICmpInst::ICMP_UGE:
1247 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001248 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getFalse();
1249 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001250 break;
1251 case ICmpInst::ICMP_SGE:
1252 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001253 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getFalse();
1254 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001255 break;
1256
1257 case ICmpInst::ICMP_NE:
1258 // If we know that C1 != C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001259 if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse();
1260 if (pred == ICmpInst::ICMP_NE) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001261 break;
1262 }
1263
1264 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
1265 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1266 // other way if possible.
1267 switch (pred) {
1268 case ICmpInst::ICMP_EQ:
1269 case ICmpInst::ICMP_NE:
1270 // No change of predicate required.
1271 return ConstantFoldCompareInstruction(pred, C2, C1);
1272
1273 case ICmpInst::ICMP_ULT:
1274 case ICmpInst::ICMP_SLT:
1275 case ICmpInst::ICMP_UGT:
1276 case ICmpInst::ICMP_SGT:
1277 case ICmpInst::ICMP_ULE:
1278 case ICmpInst::ICMP_SLE:
1279 case ICmpInst::ICMP_UGE:
1280 case ICmpInst::ICMP_SGE:
1281 // Change the predicate as necessary to swap the operands.
1282 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1283 return ConstantFoldCompareInstruction(pred, C2, C1);
1284
1285 default: // These predicates cannot be flopped around.
1286 break;
1287 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001288 }
1289 }
1290 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001291}
1292
1293Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
Chris Lattner302116a2007-01-31 04:40:28 +00001294 Constant* const *Idxs,
1295 unsigned NumIdx) {
1296 if (NumIdx == 0 ||
1297 (NumIdx == 1 && Idxs[0]->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001298 return const_cast<Constant*>(C);
1299
Chris Lattnerf6013752004-10-17 21:54:55 +00001300 if (isa<UndefValue>(C)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001301 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1302 (Value**)Idxs, NumIdx,
Chris Lattnerf6013752004-10-17 21:54:55 +00001303 true);
1304 assert(Ty != 0 && "Invalid indices for GEP!");
1305 return UndefValue::get(PointerType::get(Ty));
1306 }
1307
Chris Lattner302116a2007-01-31 04:40:28 +00001308 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001309 if (C->isNullValue()) {
1310 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001311 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1312 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001313 isNull = false;
1314 break;
1315 }
1316 if (isNull) {
Chris Lattner302116a2007-01-31 04:40:28 +00001317 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
1318 (Value**)Idxs, NumIdx,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001319 true);
1320 assert(Ty != 0 && "Invalid indices for GEP!");
1321 return ConstantPointerNull::get(PointerType::get(Ty));
1322 }
1323 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001324
1325 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1326 // Combine Indices - If the source pointer to this getelementptr instruction
1327 // is a getelementptr instruction, combine the indices of the two
1328 // getelementptr instructions into a single instruction.
1329 //
1330 if (CE->getOpcode() == Instruction::GetElementPtr) {
1331 const Type *LastTy = 0;
1332 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1333 I != E; ++I)
1334 LastTy = *I;
1335
Chris Lattner13128ab2004-10-11 22:52:25 +00001336 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001337 SmallVector<Value*, 16> NewIndices;
1338 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001339 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001340 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001341
1342 // Add the last index of the source with the first index of the new GEP.
1343 // Make sure to handle the case when they are actually different types.
1344 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001345 // Otherwise it must be an array.
1346 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001347 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001348 if (IdxTy != Idx0->getType()) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001349 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Reid Spencer27720a92006-12-05 03:30:09 +00001350 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001351 Type::Int64Ty);
Reid Spencer1a063892006-12-04 02:46:44 +00001352 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1353 } else {
1354 Combined =
1355 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1356 }
Chris Lattner71068a02004-07-07 04:45:13 +00001357 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001358
Chris Lattner1dd054c2004-01-12 22:07:24 +00001359 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001360 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
1361 return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
1362 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001363 }
1364 }
1365
1366 // Implement folding of:
1367 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1368 // long 0, long 0)
1369 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1370 //
Chris Lattner302116a2007-01-31 04:40:28 +00001371 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue())
Misha Brukmanb1c93172005-04-21 23:48:37 +00001372 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001373 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1374 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1375 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001376 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001377 if (CAT->getElementType() == SAT->getElementType())
1378 return ConstantExpr::getGetElementPtr(
Chris Lattner302116a2007-01-31 04:40:28 +00001379 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattner1dd054c2004-01-12 22:07:24 +00001380 }
1381 return 0;
1382}
1383