blob: 6660c8d3e2f3168242aaaf29c3d02c96384810aa [file] [log] [blame]
Reid Spencer81658a82007-02-27 06:23:51 +00001//===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner5a945e32004-01-12 21:13:12 +000010// This file implements folding of constants for LLVM. This implements the
Reid Spencer81658a82007-02-27 06:23:51 +000011// (internal) ConstantFold.h interface, which is used by the
Chris Lattner5a945e32004-01-12 21:13:12 +000012// ConstantExpr::get* methods to automatically fold constants when possible.
Chris Lattner2f7c9632001-06-06 20:29:01 +000013//
Chris Lattner1dd054c2004-01-12 22:07:24 +000014// The current constant folding implementation is implemented in two pieces: the
15// template-based folder for simple primitive constants like ConstantInt, and
16// the special case hackery that we use to symbolically evaluate expressions
17// that use ConstantExprs.
18//
Chris Lattner2f7c9632001-06-06 20:29:01 +000019//===----------------------------------------------------------------------===//
20
Chris Lattner33e93b82007-02-27 03:05:06 +000021#include "ConstantFold.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000022#include "llvm/Constants.h"
Chris Lattnera9eddae2004-02-22 06:25:38 +000023#include "llvm/Instructions.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerea0789c2004-03-08 06:17:35 +000025#include "llvm/Function.h"
Chris Lattner52fe8692007-09-10 23:42:42 +000026#include "llvm/GlobalAlias.h"
Chris Lattner302116a2007-01-31 04:40:28 +000027#include "llvm/ADT/SmallVector.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattner057083f2006-10-13 17:22:21 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
30#include "llvm/Support/ManagedStatic.h"
31#include "llvm/Support/MathExtras.h"
Jeff Cohen4e3aede2005-05-03 03:13:01 +000032#include <limits>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000033using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000034
Chris Lattner1dd054c2004-01-12 22:07:24 +000035//===----------------------------------------------------------------------===//
36// ConstantFold*Instruction Implementations
37//===----------------------------------------------------------------------===//
Chris Lattner1dd054c2004-01-12 22:07:24 +000038
Chris Lattner5c6399e2007-12-11 06:07:39 +000039/// BitCastConstantVector - Convert the specified ConstantVector node to the
Reid Spencer09575ba2007-02-15 03:39:18 +000040/// specified vector type. At this point, we know that the elements of the
Dan Gohman06c60b62007-07-16 14:29:03 +000041/// input vector constant are all simple integer or FP values.
Chris Lattner5c6399e2007-12-11 06:07:39 +000042static Constant *BitCastConstantVector(ConstantVector *CV,
43 const VectorType *DstTy) {
44 // If this cast changes element count then we can't handle it here:
45 // doing so requires endianness information. This should be handled by
46 // Analysis/ConstantFolding.cpp
47 unsigned NumElts = DstTy->getNumElements();
48 if (NumElts != CV->getNumOperands())
49 return 0;
Chris Lattner6b3f4752006-04-02 01:38:28 +000050
Chris Lattner5c6399e2007-12-11 06:07:39 +000051 // Check to verify that all elements of the input are simple.
52 for (unsigned i = 0; i != NumElts; ++i) {
53 if (!isa<ConstantInt>(CV->getOperand(i)) &&
54 !isa<ConstantFP>(CV->getOperand(i)))
55 return 0;
Chris Lattner6b3f4752006-04-02 01:38:28 +000056 }
Chris Lattner5c6399e2007-12-11 06:07:39 +000057
58 // Bitcast each element now.
59 std::vector<Constant*> Result;
60 const Type *DstEltTy = DstTy->getElementType();
61 for (unsigned i = 0; i != NumElts; ++i)
62 Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i), DstEltTy));
63 return ConstantVector::get(Result);
Chris Lattner6b3f4752006-04-02 01:38:28 +000064}
65
Reid Spencer6c38f0b2006-11-27 01:05:10 +000066/// This function determines which opcode to use to fold two constant cast
67/// expressions together. It uses CastInst::isEliminableCastPair to determine
68/// the opcode. Consequently its just a wrapper around that function.
Reid Spencer05d55b32007-08-05 19:27:01 +000069/// @brief Determine if it is valid to fold a cast of a cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +000070static unsigned
71foldConstantCastPair(
72 unsigned opc, ///< opcode of the second cast constant expression
73 const ConstantExpr*Op, ///< the first cast constant expression
74 const Type *DstTy ///< desintation type of the first cast
75) {
76 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
77 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
78 assert(CastInst::isCast(opc) && "Invalid cast opcode");
79
80 // The the types and opcodes for the two Cast constant expressions
81 const Type *SrcTy = Op->getOperand(0)->getType();
82 const Type *MidTy = Op->getType();
83 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
84 Instruction::CastOps secondOp = Instruction::CastOps(opc);
Chris Lattner6b3f4752006-04-02 01:38:28 +000085
Reid Spencer6c38f0b2006-11-27 01:05:10 +000086 // Let CastInst::isEliminableCastPair do the heavy lifting.
87 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
Reid Spencer8d9336d2006-12-31 05:26:44 +000088 Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +000089}
90
Chris Lattnere8ea0372007-12-11 05:55:02 +000091static Constant *FoldBitCast(Constant *V, const Type *DestTy) {
92 const Type *SrcTy = V->getType();
93 if (SrcTy == DestTy)
94 return V; // no-op cast
95
96 // Check to see if we are casting a pointer to an aggregate to a pointer to
97 // the first element. If so, return the appropriate GEP instruction.
98 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
99 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) {
100 SmallVector<Value*, 8> IdxList;
101 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
102 const Type *ElTy = PTy->getElementType();
103 while (ElTy != DPTy->getElementType()) {
104 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
105 if (STy->getNumElements() == 0) break;
106 ElTy = STy->getElementType(0);
107 IdxList.push_back(Constant::getNullValue(Type::Int32Ty));
108 } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) {
109 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
110 ElTy = STy->getElementType();
111 IdxList.push_back(IdxList[0]);
112 } else {
113 break;
114 }
115 }
116
117 if (ElTy == DPTy->getElementType())
118 return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size());
119 }
120
121 // Handle casts from one vector constant to another. We know that the src
122 // and dest type have the same size (otherwise its an illegal cast).
123 if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
124 if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
125 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
126 "Not cast between same sized vectors!");
127 // First, check for null. Undef is already handled.
128 if (isa<ConstantAggregateZero>(V))
129 return Constant::getNullValue(DestTy);
130
Chris Lattner5c6399e2007-12-11 06:07:39 +0000131 if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
132 return BitCastConstantVector(CV, DestPTy);
Chris Lattnere8ea0372007-12-11 05:55:02 +0000133 }
134 }
135
136 // Finally, implement bitcast folding now. The code below doesn't handle
137 // bitcast right.
138 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
139 return ConstantPointerNull::get(cast<PointerType>(DestTy));
140
141 // Handle integral constant input.
142 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
143 if (DestTy->isInteger())
144 // Integral -> Integral. This is a no-op because the bit widths must
145 // be the same. Consequently, we just fold to V.
146 return V;
147
148 if (DestTy->isFloatingPoint()) {
149 assert((DestTy == Type::DoubleTy || DestTy == Type::FloatTy) &&
150 "Unknown FP type!");
151 return ConstantFP::get(DestTy, APFloat(CI->getValue()));
152 }
153 // Otherwise, can't fold this (vector?)
154 return 0;
155 }
156
157 // Handle ConstantFP input.
158 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
159 // FP -> Integral.
160 if (DestTy == Type::Int32Ty) {
161 return ConstantInt::get(FP->getValueAPF().convertToAPInt());
162 } else {
163 assert(DestTy == Type::Int64Ty && "only support f32/f64 for now!");
164 return ConstantInt::get(FP->getValueAPF().convertToAPInt());
165 }
166 }
167 return 0;
168}
169
170
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000171Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
Chris Lattner1dd054c2004-01-12 22:07:24 +0000172 const Type *DestTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000173 const Type *SrcTy = V->getType();
Chris Lattner1dd054c2004-01-12 22:07:24 +0000174
Chris Lattner363485d2007-07-20 22:09:02 +0000175 if (isa<UndefValue>(V)) {
176 // zext(undef) = 0, because the top bits will be zero.
177 // sext(undef) = 0, because the top bits will all be the same.
178 if (opc == Instruction::ZExt || opc == Instruction::SExt)
179 return Constant::getNullValue(DestTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000180 return UndefValue::get(DestTy);
Chris Lattner363485d2007-07-20 22:09:02 +0000181 }
Dale Johannesen19db0932007-10-14 01:56:47 +0000182 // No compile-time operations on this type yet.
183 if (V->getType() == Type::PPC_FP128Ty || DestTy == Type::PPC_FP128Ty)
184 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000185
186 // If the cast operand is a constant expression, there's a few things we can
187 // do to try to simplify it.
188 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
189 if (CE->isCast()) {
Reid Spencer1a063892006-12-04 02:46:44 +0000190 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000191 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
192 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000193 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
194 // If all of the indexes in the GEP are null values, there is no pointer
195 // adjustment going on. We might as well cast the source pointer.
196 bool isAllNull = true;
197 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
198 if (!CE->getOperand(i)->isNullValue()) {
199 isAllNull = false;
200 break;
201 }
202 if (isAllNull)
Reid Spencer1a063892006-12-04 02:46:44 +0000203 // This is casting one pointer type to another, always BitCast
Reid Spencer27720a92006-12-05 03:30:09 +0000204 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000205 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000206 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000207
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000208 // We actually have to do a cast now. Perform the cast according to the
209 // opcode specified.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000210 switch (opc) {
211 case Instruction::FPTrunc:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000212 case Instruction::FPExt:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000213 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Dale Johannesenf4bad972007-09-19 14:22:58 +0000214 APFloat Val = FPC->getValueAPF();
215 Val.convert(DestTy == Type::FloatTy ? APFloat::IEEEsingle :
216 DestTy == Type::DoubleTy ? APFloat::IEEEdouble :
217 DestTy == Type::X86_FP80Ty ? APFloat::x87DoubleExtended :
218 DestTy == Type::FP128Ty ? APFloat::IEEEquad :
219 APFloat::Bogus,
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000220 APFloat::rmNearestTiesToEven);
221 return ConstantFP::get(DestTy, Val);
222 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000223 return 0; // Can't fold.
224 case Instruction::FPToUI:
Reid Spencer8dabca42006-12-19 07:41:40 +0000225 case Instruction::FPToSI:
Reid Spencer81658a82007-02-27 06:23:51 +0000226 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Chris Lattner2b827fd2007-10-15 05:34:10 +0000227 const APFloat &V = FPC->getValueAPF();
Dale Johannesenf4bad972007-09-19 14:22:58 +0000228 uint64_t x[2];
Reid Spencer81658a82007-02-27 06:23:51 +0000229 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Dale Johannesen17663f42007-09-25 23:32:20 +0000230 (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
231 APFloat::rmTowardZero);
Dale Johannesenf4bad972007-09-19 14:22:58 +0000232 APInt Val(DestBitWidth, 2, x);
Reid Spencera1276332007-03-01 19:31:12 +0000233 return ConstantInt::get(Val);
Reid Spencer81658a82007-02-27 06:23:51 +0000234 }
Nate Begemand4d45c22007-11-17 03:58:34 +0000235 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
236 std::vector<Constant*> res;
237 const VectorType *DestVecTy = cast<VectorType>(DestTy);
238 const Type *DstEltTy = DestVecTy->getElementType();
239 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
240 res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i),
241 DstEltTy));
242 return ConstantVector::get(DestVecTy, res);
243 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000244 return 0; // Can't fold.
245 case Instruction::IntToPtr: //always treated as unsigned
246 if (V->isNullValue()) // Is it an integral null value?
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000247 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Reid Spencer8dabca42006-12-19 07:41:40 +0000248 return 0; // Other pointer types cannot be casted
249 case Instruction::PtrToInt: // always treated as unsigned
250 if (V->isNullValue()) // is it a null pointer value?
Zhou Sheng75b871f2007-01-11 12:24:14 +0000251 return ConstantInt::get(DestTy, 0);
Reid Spencer8dabca42006-12-19 07:41:40 +0000252 return 0; // Other pointer types cannot be casted
253 case Instruction::UIToFP:
Reid Spencer8dabca42006-12-19 07:41:40 +0000254 case Instruction::SIToFP:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000255 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen91506522007-09-30 18:19:03 +0000256 APInt api = CI->getValue();
257 const uint64_t zero[] = {0, 0};
258 uint32_t BitWidth = cast<IntegerType>(SrcTy)->getBitWidth();
259 APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(),
260 2, zero));
Neil Booth5f009732007-10-07 11:45:55 +0000261 (void)apf.convertFromZeroExtendedInteger(api.getRawData(), BitWidth,
Dale Johannesen91506522007-09-30 18:19:03 +0000262 opc==Instruction::SIToFP,
263 APFloat::rmNearestTiesToEven);
264 return ConstantFP::get(DestTy, apf);
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000265 }
Nate Begemand4d45c22007-11-17 03:58:34 +0000266 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) {
267 std::vector<Constant*> res;
268 const VectorType *DestVecTy = cast<VectorType>(DestTy);
269 const Type *DstEltTy = DestVecTy->getElementType();
270 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
271 res.push_back(ConstantFoldCastInstruction(opc, V->getOperand(i),
272 DstEltTy));
273 return ConstantVector::get(DestVecTy, res);
274 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000275 return 0;
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000276 case Instruction::ZExt:
Reid Spencer81658a82007-02-27 06:23:51 +0000277 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
278 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
279 APInt Result(CI->getValue());
280 Result.zext(BitWidth);
Reid Spencera1276332007-03-01 19:31:12 +0000281 return ConstantInt::get(Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000282 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000283 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000284 case Instruction::SExt:
Reid Spencer81658a82007-02-27 06:23:51 +0000285 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
286 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
287 APInt Result(CI->getValue());
288 Result.sext(BitWidth);
Reid Spencera1276332007-03-01 19:31:12 +0000289 return ConstantInt::get(Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000290 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000291 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000292 case Instruction::Trunc:
Reid Spencer81658a82007-02-27 06:23:51 +0000293 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
294 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
295 APInt Result(CI->getValue());
296 Result.trunc(BitWidth);
Reid Spencera1276332007-03-01 19:31:12 +0000297 return ConstantInt::get(Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000298 }
Chris Lattner710ebaf2006-12-01 19:22:41 +0000299 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000300 case Instruction::BitCast:
Chris Lattnere8ea0372007-12-11 05:55:02 +0000301 return FoldBitCast(const_cast<Constant*>(V), DestTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000302 default:
303 assert(!"Invalid CE CastInst opcode");
304 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000305 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000306
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000307 assert(0 && "Failed to cast constant expression");
308 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000309}
310
Chris Lattner6ea4b522004-03-12 05:53:32 +0000311Constant *llvm::ConstantFoldSelectInstruction(const Constant *Cond,
312 const Constant *V1,
313 const Constant *V2) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000314 if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
Reid Spencercddc9df2007-01-12 04:24:46 +0000315 return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000316
317 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
318 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
319 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000320 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000321 return 0;
322}
323
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000324Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val,
325 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000326 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
Reid Spencerd84d35b2007-02-15 02:26:10 +0000327 return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000328 if (Val->isNullValue()) // ee(zero, x) -> zero
329 return Constant::getNullValue(
Reid Spencerd84d35b2007-02-15 02:26:10 +0000330 cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000331
Reid Spencerd84d35b2007-02-15 02:26:10 +0000332 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000333 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
334 return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue()));
Chris Lattnere52f29b2006-03-31 18:31:40 +0000335 } else if (isa<UndefValue>(Idx)) {
336 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
337 return const_cast<Constant*>(CVal->getOperand(0));
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000338 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000339 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000340 return 0;
341}
342
Robert Bocchinoca27f032006-01-17 20:07:22 +0000343Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val,
344 const Constant *Elt,
345 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000346 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000347 if (!CIdx) return 0;
Reid Spencer81658a82007-02-27 06:23:51 +0000348 APInt idxVal = CIdx->getValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000349 if (isa<UndefValue>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000350 // Insertion of scalar constant into vector undef
Robert Bocchinoca27f032006-01-17 20:07:22 +0000351 // Optimize away insertion of undef
352 if (isa<UndefValue>(Elt))
353 return const_cast<Constant*>(Val);
354 // Otherwise break the aggregate undef into multiple undefs and do
355 // the insertion
356 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000357 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000358 std::vector<Constant*> Ops;
359 Ops.reserve(numOps);
360 for (unsigned i = 0; i < numOps; ++i) {
361 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000362 (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000363 Ops.push_back(const_cast<Constant*>(Op));
364 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000365 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000366 }
Reid Spencer3054b142006-11-02 08:18:15 +0000367 if (isa<ConstantAggregateZero>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000368 // Insertion of scalar constant into vector aggregate zero
Robert Bocchinoca27f032006-01-17 20:07:22 +0000369 // Optimize away insertion of zero
370 if (Elt->isNullValue())
371 return const_cast<Constant*>(Val);
372 // Otherwise break the aggregate zero into multiple zeros and do
373 // the insertion
374 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000375 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000376 std::vector<Constant*> Ops;
377 Ops.reserve(numOps);
378 for (unsigned i = 0; i < numOps; ++i) {
379 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000380 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000381 Ops.push_back(const_cast<Constant*>(Op));
382 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000383 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000384 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000385 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000386 // Insertion of scalar constant into vector constant
Robert Bocchinoca27f032006-01-17 20:07:22 +0000387 std::vector<Constant*> Ops;
388 Ops.reserve(CVal->getNumOperands());
389 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
390 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000391 (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
Robert Bocchinoca27f032006-01-17 20:07:22 +0000392 Ops.push_back(const_cast<Constant*>(Op));
393 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000394 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000395 }
396 return 0;
397}
398
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000399Constant *llvm::ConstantFoldShuffleVectorInstruction(const Constant *V1,
400 const Constant *V2,
401 const Constant *Mask) {
402 // TODO:
403 return 0;
404}
405
Dan Gohman06c60b62007-07-16 14:29:03 +0000406/// EvalVectorOp - Given two vector constants and a function pointer, apply the
Reid Spencerd84d35b2007-02-15 02:26:10 +0000407/// function pointer to each element pair, producing a new ConstantVector
Dan Gohman9f396602007-10-30 19:00:49 +0000408/// constant. Either or both of V1 and V2 may be NULL, meaning a
409/// ConstantAggregateZero operand.
Reid Spencerd84d35b2007-02-15 02:26:10 +0000410static Constant *EvalVectorOp(const ConstantVector *V1,
411 const ConstantVector *V2,
Dan Gohman9f396602007-10-30 19:00:49 +0000412 const VectorType *VTy,
Reid Spencer266e42b2006-12-23 06:05:41 +0000413 Constant *(*FP)(Constant*, Constant*)) {
414 std::vector<Constant*> Res;
Dan Gohman9f396602007-10-30 19:00:49 +0000415 const Type *EltTy = VTy->getElementType();
416 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
417 const Constant *C1 = V1 ? V1->getOperand(i) : Constant::getNullValue(EltTy);
418 const Constant *C2 = V2 ? V2->getOperand(i) : Constant::getNullValue(EltTy);
419 Res.push_back(FP(const_cast<Constant*>(C1),
420 const_cast<Constant*>(C2)));
421 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000422 return ConstantVector::get(Res);
Reid Spencer266e42b2006-12-23 06:05:41 +0000423}
424
425Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
426 const Constant *C1,
427 const Constant *C2) {
Dale Johannesene5facd52007-10-16 23:38:29 +0000428 // No compile-time operations on this type yet.
429 if (C1->getType() == Type::PPC_FP128Ty)
430 return 0;
431
Reid Spencer266e42b2006-12-23 06:05:41 +0000432 // Handle UndefValue up front
433 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
434 switch (Opcode) {
435 case Instruction::Add:
436 case Instruction::Sub:
437 case Instruction::Xor:
438 return UndefValue::get(C1->getType());
439 case Instruction::Mul:
440 case Instruction::And:
441 return Constant::getNullValue(C1->getType());
442 case Instruction::UDiv:
443 case Instruction::SDiv:
444 case Instruction::FDiv:
445 case Instruction::URem:
446 case Instruction::SRem:
447 case Instruction::FRem:
448 if (!isa<UndefValue>(C2)) // undef / X -> 0
449 return Constant::getNullValue(C1->getType());
450 return const_cast<Constant*>(C2); // X / undef -> undef
451 case Instruction::Or: // X | undef -> -1
Reid Spencerd84d35b2007-02-15 02:26:10 +0000452 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
453 return ConstantVector::getAllOnesValue(PTy);
Reid Spencer266e42b2006-12-23 06:05:41 +0000454 return ConstantInt::getAllOnesValue(C1->getType());
455 case Instruction::LShr:
456 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
457 return const_cast<Constant*>(C1); // undef lshr undef -> undef
458 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
459 // undef lshr X -> 0
460 case Instruction::AShr:
461 if (!isa<UndefValue>(C2))
462 return const_cast<Constant*>(C1); // undef ashr X --> undef
463 else if (isa<UndefValue>(C1))
464 return const_cast<Constant*>(C1); // undef ashr undef -> undef
465 else
466 return const_cast<Constant*>(C1); // X ashr undef --> X
467 case Instruction::Shl:
468 // undef << X -> 0 or X << undef -> 0
469 return Constant::getNullValue(C1->getType());
470 }
471 }
472
473 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
474 if (isa<ConstantExpr>(C2)) {
475 // There are many possible foldings we could do here. We should probably
476 // at least fold add of a pointer with an integer into the appropriate
477 // getelementptr. This will improve alias analysis a bit.
478 } else {
479 // Just implement a couple of simple identities.
480 switch (Opcode) {
481 case Instruction::Add:
482 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X + 0 == X
483 break;
484 case Instruction::Sub:
485 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X - 0 == X
486 break;
487 case Instruction::Mul:
488 if (C2->isNullValue()) return const_cast<Constant*>(C2); // X * 0 == 0
489 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000490 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000491 return const_cast<Constant*>(C1); // X * 1 == X
492 break;
493 case Instruction::UDiv:
494 case Instruction::SDiv:
495 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000496 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000497 return const_cast<Constant*>(C1); // X / 1 == X
498 break;
499 case Instruction::URem:
500 case Instruction::SRem:
501 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000502 if (CI->equalsInt(1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000503 return Constant::getNullValue(CI->getType()); // X % 1 == 0
504 break;
505 case Instruction::And:
Chris Lattner6d94bb72007-03-25 05:47:04 +0000506 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2)) {
507 if (CI->isZero()) return const_cast<Constant*>(C2); // X & 0 == 0
Chris Lattner26f13eb2007-01-04 01:56:39 +0000508 if (CI->isAllOnesValue())
509 return const_cast<Constant*>(C1); // X & -1 == X
Chris Lattner6d94bb72007-03-25 05:47:04 +0000510
511 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
512 if (CE1->getOpcode() == Instruction::ZExt) {
513 APInt PossiblySetBits
514 = cast<IntegerType>(CE1->getOperand(0)->getType())->getMask();
515 PossiblySetBits.zext(C1->getType()->getPrimitiveSizeInBits());
516 if ((PossiblySetBits & CI->getValue()) == PossiblySetBits)
517 return const_cast<Constant*>(C1);
518 }
519 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000520 if (CE1->isCast() && isa<GlobalValue>(CE1->getOperand(0))) {
521 GlobalValue *CPR = cast<GlobalValue>(CE1->getOperand(0));
522
523 // Functions are at least 4-byte aligned. If and'ing the address of a
524 // function with a constant < 4, fold it to zero.
525 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
Reid Spencer81658a82007-02-27 06:23:51 +0000526 if (CI->getValue().ult(APInt(CI->getType()->getBitWidth(),4)) &&
527 isa<Function>(CPR))
Reid Spencer266e42b2006-12-23 06:05:41 +0000528 return Constant::getNullValue(CI->getType());
529 }
530 break;
531 case Instruction::Or:
532 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X | 0 == X
Chris Lattner26f13eb2007-01-04 01:56:39 +0000533 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C2))
534 if (CI->isAllOnesValue())
535 return const_cast<Constant*>(C2); // X | -1 == -1
Reid Spencer266e42b2006-12-23 06:05:41 +0000536 break;
537 case Instruction::Xor:
538 if (C2->isNullValue()) return const_cast<Constant*>(C1); // X ^ 0 == X
539 break;
Chris Lattner6d94bb72007-03-25 05:47:04 +0000540 case Instruction::AShr:
Reid Spencere20090b2007-03-26 20:09:02 +0000541 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
Chris Lattner6d94bb72007-03-25 05:47:04 +0000542 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
543 return ConstantExpr::getLShr(const_cast<Constant*>(C1),
544 const_cast<Constant*>(C2));
545 break;
Reid Spencer266e42b2006-12-23 06:05:41 +0000546 }
547 }
548 } else if (isa<ConstantExpr>(C2)) {
549 // If C2 is a constant expr and C1 isn't, flop them around and fold the
550 // other way if possible.
551 switch (Opcode) {
552 case Instruction::Add:
553 case Instruction::Mul:
554 case Instruction::And:
555 case Instruction::Or:
556 case Instruction::Xor:
557 // No change of opcode required.
558 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
559
560 case Instruction::Shl:
561 case Instruction::LShr:
562 case Instruction::AShr:
563 case Instruction::Sub:
564 case Instruction::SDiv:
565 case Instruction::UDiv:
566 case Instruction::FDiv:
567 case Instruction::URem:
568 case Instruction::SRem:
569 case Instruction::FRem:
570 default: // These instructions cannot be flopped around.
571 return 0;
572 }
573 }
574
575 // At this point we know neither constant is an UndefValue nor a ConstantExpr
Chris Lattner26f13eb2007-01-04 01:56:39 +0000576 // so look at directly computing the value.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000577 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
578 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000579 using namespace APIntOps;
580 APInt C1V = CI1->getValue();
581 APInt C2V = CI2->getValue();
Chris Lattner344da522007-01-12 18:42:52 +0000582 switch (Opcode) {
583 default:
584 break;
585 case Instruction::Add:
Reid Spencera1276332007-03-01 19:31:12 +0000586 return ConstantInt::get(C1V + C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000587 case Instruction::Sub:
Reid Spencera1276332007-03-01 19:31:12 +0000588 return ConstantInt::get(C1V - C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000589 case Instruction::Mul:
Reid Spencera1276332007-03-01 19:31:12 +0000590 return ConstantInt::get(C1V * C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000591 case Instruction::UDiv:
Reid Spencer81658a82007-02-27 06:23:51 +0000592 if (CI2->isNullValue())
593 return 0; // X / 0 -> can't fold
Reid Spencera1276332007-03-01 19:31:12 +0000594 return ConstantInt::get(C1V.udiv(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000595 case Instruction::SDiv:
Reid Spencer81658a82007-02-27 06:23:51 +0000596 if (CI2->isNullValue())
597 return 0; // X / 0 -> can't fold
Reid Spencer81658a82007-02-27 06:23:51 +0000598 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
599 return 0; // MIN_INT / -1 -> overflow
Reid Spencera1276332007-03-01 19:31:12 +0000600 return ConstantInt::get(C1V.sdiv(C2V));
Reid Spencer81658a82007-02-27 06:23:51 +0000601 case Instruction::URem:
602 if (C2->isNullValue())
603 return 0; // X / 0 -> can't fold
Reid Spencera1276332007-03-01 19:31:12 +0000604 return ConstantInt::get(C1V.urem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000605 case Instruction::SRem:
Reid Spencer81658a82007-02-27 06:23:51 +0000606 if (CI2->isNullValue())
607 return 0; // X % 0 -> can't fold
608 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
609 return 0; // MIN_INT % -1 -> overflow
Reid Spencera1276332007-03-01 19:31:12 +0000610 return ConstantInt::get(C1V.srem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000611 case Instruction::And:
Reid Spencera1276332007-03-01 19:31:12 +0000612 return ConstantInt::get(C1V & C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000613 case Instruction::Or:
Reid Spencera1276332007-03-01 19:31:12 +0000614 return ConstantInt::get(C1V | C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000615 case Instruction::Xor:
Reid Spencera1276332007-03-01 19:31:12 +0000616 return ConstantInt::get(C1V ^ C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000617 case Instruction::Shl:
Reid Spencer81658a82007-02-27 06:23:51 +0000618 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000619 if (shiftAmt < C1V.getBitWidth())
Reid Spencera1276332007-03-01 19:31:12 +0000620 return ConstantInt::get(C1V.shl(shiftAmt));
Reid Spencer81658a82007-02-27 06:23:51 +0000621 else
622 return UndefValue::get(C1->getType()); // too big shift is undef
623 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Chris Lattner344da522007-01-12 18:42:52 +0000624 case Instruction::LShr:
Reid Spencer81658a82007-02-27 06:23:51 +0000625 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000626 if (shiftAmt < C1V.getBitWidth())
Reid Spencera1276332007-03-01 19:31:12 +0000627 return ConstantInt::get(C1V.lshr(shiftAmt));
Reid Spencer81658a82007-02-27 06:23:51 +0000628 else
629 return UndefValue::get(C1->getType()); // too big shift is undef
630 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Chris Lattner344da522007-01-12 18:42:52 +0000631 case Instruction::AShr:
Reid Spencer81658a82007-02-27 06:23:51 +0000632 if (uint32_t shiftAmt = C2V.getZExtValue())
Reid Spencerac419b52007-02-27 19:29:54 +0000633 if (shiftAmt < C1V.getBitWidth())
Reid Spencera1276332007-03-01 19:31:12 +0000634 return ConstantInt::get(C1V.ashr(shiftAmt));
Reid Spencer81658a82007-02-27 06:23:51 +0000635 else
636 return UndefValue::get(C1->getType()); // too big shift is undef
637 return const_cast<ConstantInt*>(CI1); // Zero shift is identity
Reid Spencer266e42b2006-12-23 06:05:41 +0000638 }
639 }
640 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
641 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000642 APFloat C1V = CFP1->getValueAPF();
643 APFloat C2V = CFP2->getValueAPF();
644 APFloat C3V = C1V; // copy for modification
645 bool isDouble = CFP1->getType()==Type::DoubleTy;
Reid Spencer266e42b2006-12-23 06:05:41 +0000646 switch (Opcode) {
647 default:
648 break;
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000649 case Instruction::Add:
650 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
651 return ConstantFP::get(CFP1->getType(), C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000652 case Instruction::Sub:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000653 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
654 return ConstantFP::get(CFP1->getType(), C3V);
655 case Instruction::Mul:
656 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
657 return ConstantFP::get(CFP1->getType(), C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000658 case Instruction::FDiv:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000659 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
660 return ConstantFP::get(CFP1->getType(), C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000661 case Instruction::FRem:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000662 if (C2V.isZero())
Reid Spencerd96dc902007-03-23 05:33:23 +0000663 // IEEE 754, Section 7.1, #5
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000664 return ConstantFP::get(CFP1->getType(), isDouble ?
665 APFloat(std::numeric_limits<double>::quiet_NaN()) :
666 APFloat(std::numeric_limits<float>::quiet_NaN()));
667 (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
668 return ConstantFP::get(CFP1->getType(), C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000669 }
670 }
Dan Gohman9f396602007-10-30 19:00:49 +0000671 } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
672 const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
673 const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000674 if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
675 (CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000676 switch (Opcode) {
677 default:
678 break;
679 case Instruction::Add:
Dan Gohman9f396602007-10-30 19:00:49 +0000680 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getAdd);
Reid Spencer266e42b2006-12-23 06:05:41 +0000681 case Instruction::Sub:
Dan Gohman9f396602007-10-30 19:00:49 +0000682 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSub);
Reid Spencer266e42b2006-12-23 06:05:41 +0000683 case Instruction::Mul:
Dan Gohman9f396602007-10-30 19:00:49 +0000684 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getMul);
Reid Spencer266e42b2006-12-23 06:05:41 +0000685 case Instruction::UDiv:
Dan Gohman9f396602007-10-30 19:00:49 +0000686 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getUDiv);
Reid Spencer266e42b2006-12-23 06:05:41 +0000687 case Instruction::SDiv:
Dan Gohman9f396602007-10-30 19:00:49 +0000688 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSDiv);
Reid Spencer266e42b2006-12-23 06:05:41 +0000689 case Instruction::FDiv:
Dan Gohman9f396602007-10-30 19:00:49 +0000690 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getFDiv);
Reid Spencer266e42b2006-12-23 06:05:41 +0000691 case Instruction::URem:
Dan Gohman9f396602007-10-30 19:00:49 +0000692 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getURem);
Reid Spencer266e42b2006-12-23 06:05:41 +0000693 case Instruction::SRem:
Dan Gohman9f396602007-10-30 19:00:49 +0000694 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getSRem);
Reid Spencer266e42b2006-12-23 06:05:41 +0000695 case Instruction::FRem:
Dan Gohman9f396602007-10-30 19:00:49 +0000696 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getFRem);
Reid Spencer266e42b2006-12-23 06:05:41 +0000697 case Instruction::And:
Dan Gohman9f396602007-10-30 19:00:49 +0000698 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getAnd);
Reid Spencer266e42b2006-12-23 06:05:41 +0000699 case Instruction::Or:
Dan Gohman9f396602007-10-30 19:00:49 +0000700 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getOr);
Reid Spencer266e42b2006-12-23 06:05:41 +0000701 case Instruction::Xor:
Dan Gohman9f396602007-10-30 19:00:49 +0000702 return EvalVectorOp(CP1, CP2, VTy, ConstantExpr::getXor);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000703 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000704 }
705 }
706
707 // We don't know how to fold this
708 return 0;
709}
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000710
Chris Lattner60c47262005-01-28 19:09:51 +0000711/// isZeroSizedType - This type is zero sized if its an array or structure of
712/// zero sized types. The only leaf zero sized type is an empty structure.
713static bool isMaybeZeroSizedType(const Type *Ty) {
714 if (isa<OpaqueType>(Ty)) return true; // Can't say.
715 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
716
717 // If all of elements have zero size, this does too.
718 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +0000719 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +0000720 return true;
721
722 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
723 return isMaybeZeroSizedType(ATy->getElementType());
724 }
725 return false;
726}
Chris Lattner6ea4b522004-03-12 05:53:32 +0000727
Chris Lattner061da2f2004-01-13 05:51:55 +0000728/// IdxCompare - Compare the two constants as though they were getelementptr
729/// indices. This allows coersion of the types to be the same thing.
730///
731/// If the two constants are the "same" (after coersion), return 0. If the
732/// first is less than the second, return -1, if the second is less than the
733/// first, return 1. If the constants are not integral, return -2.
734///
Chris Lattner60c47262005-01-28 19:09:51 +0000735static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000736 if (C1 == C2) return 0;
737
Reid Spencerc90cf772006-12-31 21:43:30 +0000738 // Ok, we found a different index. If they are not ConstantInt, we can't do
739 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +0000740 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
741 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +0000742
Chris Lattner69193f92004-04-05 01:30:19 +0000743 // Ok, we have two differing integer indices. Sign extend them to be the same
744 // type. Long is always big enough, so we use it.
Reid Spencer8d9336d2006-12-31 05:26:44 +0000745 if (C1->getType() != Type::Int64Ty)
746 C1 = ConstantExpr::getSExt(C1, Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000747
Reid Spencer8d9336d2006-12-31 05:26:44 +0000748 if (C2->getType() != Type::Int64Ty)
Reid Spencerc90cf772006-12-31 21:43:30 +0000749 C2 = ConstantExpr::getSExt(C2, Type::Int64Ty);
Reid Spencer8d9336d2006-12-31 05:26:44 +0000750
751 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +0000752
Chris Lattner60c47262005-01-28 19:09:51 +0000753 // If the type being indexed over is really just a zero sized type, there is
754 // no pointer difference being made here.
755 if (isMaybeZeroSizedType(ElTy))
756 return -2; // dunno.
757
Chris Lattner061da2f2004-01-13 05:51:55 +0000758 // If they are really different, now that they are the same type, then we
759 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +0000760 if (cast<ConstantInt>(C1)->getSExtValue() <
761 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +0000762 return -1;
763 else
764 return 1;
765}
766
Chris Lattner858f4e92007-01-04 02:13:20 +0000767/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +0000768/// decide about the two constants provided. This doesn't need to handle simple
769/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
770/// If we can determine that the two constants have a particular relation to
771/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +0000772/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
773/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +0000774///
775/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +0000776/// operand is always the most "complex" of the two. We consider ConstantFP
777/// to be the simplest, and ConstantExprs to be the most complex.
778static FCmpInst::Predicate evaluateFCmpRelation(const Constant *V1,
779 const Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +0000780 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +0000781 "Cannot compare values of different types!");
Dale Johannesen19db0932007-10-14 01:56:47 +0000782
783 // No compile-time operations on this type yet.
784 if (V1->getType() == Type::PPC_FP128Ty)
785 return FCmpInst::BAD_FCMP_PREDICATE;
786
Reid Spencer9d36acf2006-12-24 18:52:08 +0000787 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +0000788 if (V1 == V2) return FCmpInst::FCMP_OEQ;
789
Reid Spencer9d36acf2006-12-24 18:52:08 +0000790 if (!isa<ConstantExpr>(V1)) {
791 if (!isa<ConstantExpr>(V2)) {
792 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +0000793 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000794 Constant *C1 = const_cast<Constant*>(V1);
795 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng75b871f2007-01-11 12:24:14 +0000796 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000797 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +0000798 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +0000799 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000800 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000801 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +0000802 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +0000803 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000804 R = dyn_cast<ConstantInt>(
Reid Spencer9d36acf2006-12-24 18:52:08 +0000805 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +0000806 if (R && !R->isZero())
Reid Spencer9d36acf2006-12-24 18:52:08 +0000807 return FCmpInst::FCMP_OGT;
808
809 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +0000810 return FCmpInst::BAD_FCMP_PREDICATE;
811 }
812
Reid Spencer9d36acf2006-12-24 18:52:08 +0000813 // If the first operand is simple and second is ConstantExpr, swap operands.
814 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
815 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
816 return FCmpInst::getSwappedPredicate(SwappedRelation);
817 } else {
818 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
819 // constantexpr or a simple constant.
820 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
821 switch (CE1->getOpcode()) {
822 case Instruction::FPTrunc:
823 case Instruction::FPExt:
824 case Instruction::UIToFP:
825 case Instruction::SIToFP:
826 // We might be able to do something with these but we don't right now.
827 break;
828 default:
829 break;
830 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000831 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000832 // There are MANY other foldings that we could perform here. They will
833 // probably be added on demand, as they seem needed.
834 return FCmpInst::BAD_FCMP_PREDICATE;
835}
836
837/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +0000838/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +0000839/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +0000840/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +0000841/// particular relation to each other, we should return the corresponding ICmp
842/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +0000843///
844/// To simplify this code we canonicalize the relation so that the first
845/// operand is always the most "complex" of the two. We consider simple
846/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +0000847/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +0000848///
Reid Spencer9d36acf2006-12-24 18:52:08 +0000849static ICmpInst::Predicate evaluateICmpRelation(const Constant *V1,
850 const Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +0000851 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000852 assert(V1->getType() == V2->getType() &&
853 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +0000854 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000855
Reid Spenceraccd7c72004-07-17 23:47:01 +0000856 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000857 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
858 // We distilled this down to a simple case, use the standard constant
859 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000860 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +0000861 Constant *C1 = const_cast<Constant*>(V1);
862 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencer266e42b2006-12-23 06:05:41 +0000863 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000864 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +0000865 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +0000866 return pred;
867 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000868 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +0000869 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +0000870 return pred;
871 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Zhou Sheng75b871f2007-01-11 12:24:14 +0000872 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +0000873 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +0000874 return pred;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000875
876 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +0000877 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000878 }
879
Chris Lattner061da2f2004-01-13 05:51:55 +0000880 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +0000881 ICmpInst::Predicate SwappedRelation =
882 evaluateICmpRelation(V2, V1, isSigned);
883 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
884 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +0000885
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000886 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +0000887 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +0000888 ICmpInst::Predicate SwappedRelation =
889 evaluateICmpRelation(V2, V1, isSigned);
890 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
891 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +0000892 else
Reid Spencer266e42b2006-12-23 06:05:41 +0000893 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +0000894 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000895
Reid Spenceraccd7c72004-07-17 23:47:01 +0000896 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +0000897 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000898 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner52fe8692007-09-10 23:42:42 +0000899 // Don't try to decide equality of aliases.
900 if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2))
901 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
902 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000903 } else {
904 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Chris Lattner52fe8692007-09-10 23:42:42 +0000905 // GlobalVals can never be null. Don't try to evaluate aliases.
906 if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1))
Reid Spencer266e42b2006-12-23 06:05:41 +0000907 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +0000908 }
Chris Lattner061da2f2004-01-13 05:51:55 +0000909 } else {
910 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
911 // constantexpr, a CPR, or a simple constant.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000912 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
913 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +0000914
915 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000916 case Instruction::Trunc:
917 case Instruction::FPTrunc:
918 case Instruction::FPExt:
919 case Instruction::FPToUI:
920 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +0000921 break; // We can't evaluate floating point casts or truncations.
922
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000923 case Instruction::UIToFP:
924 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000925 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +0000926 case Instruction::ZExt:
927 case Instruction::SExt:
Chris Lattner061da2f2004-01-13 05:51:55 +0000928 // If the cast is not actually changing bits, and the second operand is a
929 // null pointer, do the comparison with the pre-casted value.
930 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000931 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Chris Lattnerd2265b42007-12-10 22:53:04 +0000932 bool sgnd = isSigned;
933 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
934 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
935 return evaluateICmpRelation(CE1Op0,
936 Constant::getNullValue(CE1Op0->getType()),
937 sgnd);
Reid Spencer266e42b2006-12-23 06:05:41 +0000938 }
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000939
940 // If the dest type is a pointer type, and the RHS is a constantexpr cast
941 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencer266e42b2006-12-23 06:05:41 +0000942 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000943 // which happens a lot in compilers with tagged integers.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000944 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer266e42b2006-12-23 06:05:41 +0000945 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000946 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
Chris Lattner03c49532007-01-15 02:27:26 +0000947 CE1->getOperand(0)->getType()->isInteger()) {
Chris Lattnerd2265b42007-12-10 22:53:04 +0000948 bool sgnd = isSigned;
949 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
950 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
Reid Spencer266e42b2006-12-23 06:05:41 +0000951 return evaluateICmpRelation(CE1->getOperand(0), CE2->getOperand(0),
Chris Lattnerd2265b42007-12-10 22:53:04 +0000952 sgnd);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000953 }
Chris Lattner192e3262004-04-11 01:29:30 +0000954 break;
Chris Lattner061da2f2004-01-13 05:51:55 +0000955
956 case Instruction::GetElementPtr:
957 // Ok, since this is a getelementptr, we know that the constant has a
958 // pointer type. Check the various cases.
959 if (isa<ConstantPointerNull>(V2)) {
960 // If we are comparing a GEP to a null pointer, check to see if the base
961 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +0000962 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000963 if (GV->hasExternalWeakLinkage())
964 // Weak linkage GVals could be zero or not. We're comparing that
965 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000966 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +0000967 else
968 // If its not weak linkage, the GVal must have a non-zero address
969 // so the result is greater-than
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 if (isa<ConstantPointerNull>(CE1Op0)) {
972 // If we are indexing from a null pointer, check to see if we have any
973 // non-zero indices.
974 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
975 if (!CE1->getOperand(i)->isNullValue())
976 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +0000977 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +0000978 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +0000979 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +0000980 }
981 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +0000982 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000983 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +0000984 if (CPR2->hasExternalWeakLinkage())
985 // Weak linkage GVals could be zero or not. We're comparing it to
986 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +0000987 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +0000988 else
989 // If its not weak linkage, the GVal must have a non-zero address
990 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +0000991 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +0000992 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +0000993 if (CPR1 == CPR2) {
994 // If this is a getelementptr of the same global, then it must be
995 // different. Because the types must match, the getelementptr could
996 // only have at most one index, and because we fold getelementptr's
997 // with a single zero index, it must be nonzero.
998 assert(CE1->getNumOperands() == 2 &&
999 !CE1->getOperand(1)->isNullValue() &&
1000 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001001 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001002 } else {
1003 // If they are different globals, we don't know what the value is,
1004 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001005 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001006 }
1007 }
1008 } else {
1009 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1010 const Constant *CE2Op0 = CE2->getOperand(0);
1011
1012 // There are MANY other foldings that we could perform here. They will
1013 // probably be added on demand, as they seem needed.
1014 switch (CE2->getOpcode()) {
1015 default: break;
1016 case Instruction::GetElementPtr:
1017 // By far the most common case to handle is when the base pointers are
1018 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001019 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001020 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001021 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001022 // Ok, we know that both getelementptr instructions are based on the
1023 // same global. From this, we can precisely determine the relative
1024 // ordering of the resultant pointers.
1025 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001026
Chris Lattner061da2f2004-01-13 05:51:55 +00001027 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001028 gep_type_iterator GTI = gep_type_begin(CE1);
1029 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1030 ++i, ++GTI)
1031 switch (IdxCompare(CE1->getOperand(i), CE2->getOperand(i),
1032 GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001033 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1034 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1035 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001036 }
1037
1038 // Ok, we ran out of things they have in common. If any leftovers
1039 // are non-zero then we have a difference, otherwise we are equal.
1040 for (; i < CE1->getNumOperands(); ++i)
1041 if (!CE1->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001042 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001043 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +00001044 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001045 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Misha Brukmanb1c93172005-04-21 23:48:37 +00001046
Chris Lattner061da2f2004-01-13 05:51:55 +00001047 for (; i < CE2->getNumOperands(); ++i)
1048 if (!CE2->getOperand(i)->isNullValue())
Zhou Sheng75b871f2007-01-11 12:24:14 +00001049 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001050 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001051 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001052 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1053 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001054 }
1055 }
1056 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001057 default:
1058 break;
1059 }
1060 }
1061
Reid Spencer266e42b2006-12-23 06:05:41 +00001062 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001063}
1064
Reid Spencer9d36acf2006-12-24 18:52:08 +00001065Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1066 const Constant *C1,
1067 const Constant *C2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001068
1069 // Handle some degenerate cases first
1070 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Reid Spencer542964f2007-01-11 18:21:29 +00001071 return UndefValue::get(Type::Int1Ty);
Reid Spencer266e42b2006-12-23 06:05:41 +00001072
Dale Johannesen19db0932007-10-14 01:56:47 +00001073 // No compile-time operations on this type yet.
1074 if (C1->getType() == Type::PPC_FP128Ty)
1075 return 0;
1076
Reid Spencer266e42b2006-12-23 06:05:41 +00001077 // icmp eq/ne(null,GV) -> false/true
1078 if (C1->isNullValue()) {
1079 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001080 // Don't try to evaluate aliases. External weak GV can be null.
1081 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage())
Reid Spencer9d36acf2006-12-24 18:52:08 +00001082 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001083 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001084 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001085 return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001086 // icmp eq/ne(GV,null) -> false/true
1087 } else if (C2->isNullValue()) {
1088 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001089 // Don't try to evaluate aliases. External weak GV can be null.
1090 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage())
Reid Spencer9d36acf2006-12-24 18:52:08 +00001091 if (pred == ICmpInst::ICMP_EQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001092 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001093 else if (pred == ICmpInst::ICMP_NE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001094 return ConstantInt::getTrue();
Chris Lattner1dd054c2004-01-12 22:07:24 +00001095 }
1096
Chris Lattner344da522007-01-12 18:42:52 +00001097 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +00001098 APInt V1 = cast<ConstantInt>(C1)->getValue();
1099 APInt V2 = cast<ConstantInt>(C2)->getValue();
1100 switch (pred) {
1101 default: assert(0 && "Invalid ICmp Predicate"); return 0;
1102 case ICmpInst::ICMP_EQ: return ConstantInt::get(Type::Int1Ty, V1 == V2);
1103 case ICmpInst::ICMP_NE: return ConstantInt::get(Type::Int1Ty, V1 != V2);
1104 case ICmpInst::ICMP_SLT:return ConstantInt::get(Type::Int1Ty, V1.slt(V2));
1105 case ICmpInst::ICMP_SGT:return ConstantInt::get(Type::Int1Ty, V1.sgt(V2));
1106 case ICmpInst::ICMP_SLE:return ConstantInt::get(Type::Int1Ty, V1.sle(V2));
1107 case ICmpInst::ICMP_SGE:return ConstantInt::get(Type::Int1Ty, V1.sge(V2));
1108 case ICmpInst::ICMP_ULT:return ConstantInt::get(Type::Int1Ty, V1.ult(V2));
1109 case ICmpInst::ICMP_UGT:return ConstantInt::get(Type::Int1Ty, V1.ugt(V2));
1110 case ICmpInst::ICMP_ULE:return ConstantInt::get(Type::Int1Ty, V1.ule(V2));
1111 case ICmpInst::ICMP_UGE:return ConstantInt::get(Type::Int1Ty, V1.uge(V2));
Chris Lattner061da2f2004-01-13 05:51:55 +00001112 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001113 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001114 APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
1115 APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
1116 APFloat::cmpResult R = C1V.compare(C2V);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001117 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001118 default: assert(0 && "Invalid FCmp Predicate"); return 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001119 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse();
1120 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001121 case FCmpInst::FCMP_UNO:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001122 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered);
Reid Spencer74bd0362007-01-11 00:25:45 +00001123 case FCmpInst::FCMP_ORD:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001124 return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpUnordered);
Reid Spencer266e42b2006-12-23 06:05:41 +00001125 case FCmpInst::FCMP_UEQ:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001126 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
1127 R==APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001128 case FCmpInst::FCMP_OEQ:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001129 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpEqual);
Reid Spencer74bd0362007-01-11 00:25:45 +00001130 case FCmpInst::FCMP_UNE:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001131 return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001132 case FCmpInst::FCMP_ONE:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001133 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan ||
1134 R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001135 case FCmpInst::FCMP_ULT:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001136 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
1137 R==APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001138 case FCmpInst::FCMP_OLT:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001139 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan);
Reid Spencer266e42b2006-12-23 06:05:41 +00001140 case FCmpInst::FCMP_UGT:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001141 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
1142 R==APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001143 case FCmpInst::FCMP_OGT:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001144 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001145 case FCmpInst::FCMP_ULE:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001146 return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001147 case FCmpInst::FCMP_OLE:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001148 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan ||
1149 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001150 case FCmpInst::FCMP_UGE:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001151 return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001152 case FCmpInst::FCMP_OGE:
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001153 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan ||
1154 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001155 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001156 } else if (const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1)) {
1157 if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001158 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001159 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1160 Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ,
1161 const_cast<Constant*>(CP1->getOperand(i)),
1162 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001163 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001164 return CB;
1165 }
1166 // Otherwise, could not decide from any element pairs.
1167 return 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001168 } else if (pred == ICmpInst::ICMP_EQ) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001169 for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) {
1170 Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ,
1171 const_cast<Constant*>(CP1->getOperand(i)),
1172 const_cast<Constant*>(CP2->getOperand(i)));
Zhou Sheng75b871f2007-01-11 12:24:14 +00001173 if (ConstantInt *CB = dyn_cast<ConstantInt>(C))
Reid Spencer266e42b2006-12-23 06:05:41 +00001174 return CB;
1175 }
1176 // Otherwise, could not decide from any element pairs.
1177 return 0;
1178 }
1179 }
1180 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001181
Reid Spencer9d36acf2006-12-24 18:52:08 +00001182 if (C1->getType()->isFloatingPoint()) {
1183 switch (evaluateFCmpRelation(C1, C2)) {
1184 default: assert(0 && "Unknown relation!");
1185 case FCmpInst::FCMP_UNO:
1186 case FCmpInst::FCMP_ORD:
1187 case FCmpInst::FCMP_UEQ:
1188 case FCmpInst::FCMP_UNE:
1189 case FCmpInst::FCMP_ULT:
1190 case FCmpInst::FCMP_UGT:
1191 case FCmpInst::FCMP_ULE:
1192 case FCmpInst::FCMP_UGE:
1193 case FCmpInst::FCMP_TRUE:
1194 case FCmpInst::FCMP_FALSE:
1195 case FCmpInst::BAD_FCMP_PREDICATE:
1196 break; // Couldn't determine anything about these constants.
1197 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001198 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001199 pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1200 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1201 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1202 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001203 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001204 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1205 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1206 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1207 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Reid Spencercddc9df2007-01-12 04:24:46 +00001208 return ConstantInt::get(Type::Int1Ty,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001209 pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1210 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1211 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1212 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1213 // We can only partially decide this relation.
1214 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001215 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001216 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001217 return ConstantInt::getTrue();
Chris Lattner061da2f2004-01-13 05:51:55 +00001218 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001219 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1220 // We can only partially decide this relation.
1221 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001222 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001223 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001224 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001225 break;
1226 case ICmpInst::ICMP_NE: // We know that C1 != C2
1227 // We can only partially decide this relation.
1228 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001229 return ConstantInt::getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001230 if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
Zhou Sheng75b871f2007-01-11 12:24:14 +00001231 return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001232 break;
1233 }
1234 } else {
1235 // Evaluate the relation between the two constants, per the predicate.
1236 switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
1237 default: assert(0 && "Unknown relational!");
1238 case ICmpInst::BAD_ICMP_PREDICATE:
1239 break; // Couldn't determine anything about these constants.
1240 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1241 // If we know the constants are equal, we can decide the result of this
1242 // computation precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001243 return ConstantInt::get(Type::Int1Ty,
1244 pred == ICmpInst::ICMP_EQ ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001245 pred == ICmpInst::ICMP_ULE ||
1246 pred == ICmpInst::ICMP_SLE ||
1247 pred == ICmpInst::ICMP_UGE ||
1248 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001249 case ICmpInst::ICMP_ULT:
1250 // If we know that C1 < C2, we can decide the result of this computation
1251 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001252 return ConstantInt::get(Type::Int1Ty,
1253 pred == ICmpInst::ICMP_ULT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001254 pred == ICmpInst::ICMP_NE ||
1255 pred == ICmpInst::ICMP_ULE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001256 case ICmpInst::ICMP_SLT:
1257 // If we know that C1 < C2, we can decide the result of this computation
1258 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001259 return ConstantInt::get(Type::Int1Ty,
1260 pred == ICmpInst::ICMP_SLT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001261 pred == ICmpInst::ICMP_NE ||
1262 pred == ICmpInst::ICMP_SLE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001263 case ICmpInst::ICMP_UGT:
1264 // If we know that C1 > C2, we can decide the result of this computation
1265 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001266 return ConstantInt::get(Type::Int1Ty,
1267 pred == ICmpInst::ICMP_UGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001268 pred == ICmpInst::ICMP_NE ||
1269 pred == ICmpInst::ICMP_UGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001270 case ICmpInst::ICMP_SGT:
1271 // If we know that C1 > C2, we can decide the result of this computation
1272 // precisely.
Reid Spencercddc9df2007-01-12 04:24:46 +00001273 return ConstantInt::get(Type::Int1Ty,
1274 pred == ICmpInst::ICMP_SGT ||
Zhou Sheng75b871f2007-01-11 12:24:14 +00001275 pred == ICmpInst::ICMP_NE ||
1276 pred == ICmpInst::ICMP_SGE);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001277 case ICmpInst::ICMP_ULE:
1278 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001279 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getFalse();
1280 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001281 break;
1282 case ICmpInst::ICMP_SLE:
1283 // If we know that C1 <= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001284 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getFalse();
1285 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001286 break;
1287
1288 case ICmpInst::ICMP_UGE:
1289 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001290 if (pred == ICmpInst::ICMP_ULT) return ConstantInt::getFalse();
1291 if (pred == ICmpInst::ICMP_UGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001292 break;
1293 case ICmpInst::ICMP_SGE:
1294 // If we know that C1 >= C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001295 if (pred == ICmpInst::ICMP_SLT) return ConstantInt::getFalse();
1296 if (pred == ICmpInst::ICMP_SGT) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001297 break;
1298
1299 case ICmpInst::ICMP_NE:
1300 // If we know that C1 != C2, we can only partially decide this relation.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001301 if (pred == ICmpInst::ICMP_EQ) return ConstantInt::getFalse();
1302 if (pred == ICmpInst::ICMP_NE) return ConstantInt::getTrue();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001303 break;
1304 }
1305
1306 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
1307 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1308 // other way if possible.
1309 switch (pred) {
1310 case ICmpInst::ICMP_EQ:
1311 case ICmpInst::ICMP_NE:
1312 // No change of predicate required.
1313 return ConstantFoldCompareInstruction(pred, C2, C1);
1314
1315 case ICmpInst::ICMP_ULT:
1316 case ICmpInst::ICMP_SLT:
1317 case ICmpInst::ICMP_UGT:
1318 case ICmpInst::ICMP_SGT:
1319 case ICmpInst::ICMP_ULE:
1320 case ICmpInst::ICMP_SLE:
1321 case ICmpInst::ICMP_UGE:
1322 case ICmpInst::ICMP_SGE:
1323 // Change the predicate as necessary to swap the operands.
1324 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
1325 return ConstantFoldCompareInstruction(pred, C2, C1);
1326
1327 default: // These predicates cannot be flopped around.
1328 break;
1329 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001330 }
1331 }
1332 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001333}
1334
1335Constant *llvm::ConstantFoldGetElementPtr(const Constant *C,
David Greenec656cbb2007-09-04 15:46:09 +00001336 Constant* const *Idxs,
Chris Lattner302116a2007-01-31 04:40:28 +00001337 unsigned NumIdx) {
1338 if (NumIdx == 0 ||
1339 (NumIdx == 1 && Idxs[0]->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001340 return const_cast<Constant*>(C);
1341
Chris Lattnerf6013752004-10-17 21:54:55 +00001342 if (isa<UndefValue>(C)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001343 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
David Greenec656cbb2007-09-04 15:46:09 +00001344 (Value **)Idxs,
1345 (Value **)Idxs+NumIdx,
Chris Lattnerf6013752004-10-17 21:54:55 +00001346 true);
1347 assert(Ty != 0 && "Invalid indices for GEP!");
1348 return UndefValue::get(PointerType::get(Ty));
1349 }
1350
Chris Lattner302116a2007-01-31 04:40:28 +00001351 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001352 if (C->isNullValue()) {
1353 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001354 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1355 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001356 isNull = false;
1357 break;
1358 }
1359 if (isNull) {
Chris Lattner302116a2007-01-31 04:40:28 +00001360 const Type *Ty = GetElementPtrInst::getIndexedType(C->getType(),
David Greenec656cbb2007-09-04 15:46:09 +00001361 (Value**)Idxs,
1362 (Value**)Idxs+NumIdx,
Chris Lattner04b60fe2004-02-16 20:46:13 +00001363 true);
1364 assert(Ty != 0 && "Invalid indices for GEP!");
1365 return ConstantPointerNull::get(PointerType::get(Ty));
1366 }
1367 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001368
1369 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1370 // Combine Indices - If the source pointer to this getelementptr instruction
1371 // is a getelementptr instruction, combine the indices of the two
1372 // getelementptr instructions into a single instruction.
1373 //
1374 if (CE->getOpcode() == Instruction::GetElementPtr) {
1375 const Type *LastTy = 0;
1376 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1377 I != E; ++I)
1378 LastTy = *I;
1379
Chris Lattner13128ab2004-10-11 22:52:25 +00001380 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001381 SmallVector<Value*, 16> NewIndices;
1382 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001383 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001384 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001385
1386 // Add the last index of the source with the first index of the new GEP.
1387 // Make sure to handle the case when they are actually different types.
1388 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001389 // Otherwise it must be an array.
1390 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001391 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001392 if (IdxTy != Idx0->getType()) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001393 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Type::Int64Ty);
Reid Spencer27720a92006-12-05 03:30:09 +00001394 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Reid Spencer8d9336d2006-12-31 05:26:44 +00001395 Type::Int64Ty);
Reid Spencer1a063892006-12-04 02:46:44 +00001396 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
1397 } else {
1398 Combined =
1399 ConstantExpr::get(Instruction::Add, Idx0, Combined);
1400 }
Chris Lattner71068a02004-07-07 04:45:13 +00001401 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001402
Chris Lattner1dd054c2004-01-12 22:07:24 +00001403 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001404 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
1405 return ConstantExpr::getGetElementPtr(CE->getOperand(0), &NewIndices[0],
1406 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001407 }
1408 }
1409
1410 // Implement folding of:
1411 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1412 // long 0, long 0)
1413 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1414 //
Chris Lattneraadc7782007-08-13 17:09:08 +00001415 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001416 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001417 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1418 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1419 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001420 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001421 if (CAT->getElementType() == SAT->getElementType())
1422 return ConstantExpr::getGetElementPtr(
Chris Lattner302116a2007-01-31 04:40:28 +00001423 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattneraadc7782007-08-13 17:09:08 +00001424 }
1425
1426 // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1)
1427 // Into: inttoptr (i64 0 to i8*)
1428 // This happens with pointers to member functions in C++.
1429 if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 &&
1430 isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) &&
1431 cast<PointerType>(CE->getType())->getElementType() == Type::Int8Ty) {
1432 Constant *Base = CE->getOperand(0);
1433 Constant *Offset = Idxs[0];
1434
1435 // Convert the smaller integer to the larger type.
1436 if (Offset->getType()->getPrimitiveSizeInBits() <
1437 Base->getType()->getPrimitiveSizeInBits())
1438 Offset = ConstantExpr::getSExt(Offset, Base->getType());
1439 else if (Base->getType()->getPrimitiveSizeInBits() <
1440 Offset->getType()->getPrimitiveSizeInBits())
1441 Base = ConstantExpr::getZExt(Base, Base->getType());
1442
1443 Base = ConstantExpr::getAdd(Base, Offset);
1444 return ConstantExpr::getIntToPtr(Base, CE->getType());
1445 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001446 }
1447 return 0;
1448}
1449