blob: a26bee8f950c444ee0ddc30e52b4212d34b13eae [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//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Owen Anderson53a52212009-07-13 04:09:18 +000027#include "llvm/LLVMContext.h"
Chris Lattner302116a2007-01-31 04:40:28 +000028#include "llvm/ADT/SmallVector.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Torok Edwin56d06592009-07-11 20:10:48 +000030#include "llvm/Support/ErrorHandling.h"
Chris Lattner057083f2006-10-13 17:22:21 +000031#include "llvm/Support/GetElementPtrTypeIterator.h"
32#include "llvm/Support/ManagedStatic.h"
33#include "llvm/Support/MathExtras.h"
Jeff Cohen4e3aede2005-05-03 03:13:01 +000034#include <limits>
Chris Lattner9d9cbcf2003-11-17 19:05:17 +000035using namespace llvm;
Chris Lattner61607ee2001-09-09 21:01:20 +000036
Chris Lattner1dd054c2004-01-12 22:07:24 +000037//===----------------------------------------------------------------------===//
38// ConstantFold*Instruction Implementations
39//===----------------------------------------------------------------------===//
Chris Lattner1dd054c2004-01-12 22:07:24 +000040
Chris Lattner5c6399e2007-12-11 06:07:39 +000041/// BitCastConstantVector - Convert the specified ConstantVector node to the
Reid Spencer09575ba2007-02-15 03:39:18 +000042/// specified vector type. At this point, we know that the elements of the
Dan Gohman06c60b62007-07-16 14:29:03 +000043/// input vector constant are all simple integer or FP values.
Owen Anderson53a52212009-07-13 04:09:18 +000044static Constant *BitCastConstantVector(LLVMContext &Context, ConstantVector *CV,
Owen Anderson0d2de8c2009-06-20 00:24:58 +000045 const VectorType *DstTy) {
Chris Lattner5c6399e2007-12-11 06:07:39 +000046 // If this cast changes element count then we can't handle it here:
47 // doing so requires endianness information. This should be handled by
48 // Analysis/ConstantFolding.cpp
49 unsigned NumElts = DstTy->getNumElements();
50 if (NumElts != CV->getNumOperands())
51 return 0;
Chris Lattner6b3f4752006-04-02 01:38:28 +000052
Chris Lattner5c6399e2007-12-11 06:07:39 +000053 // Check to verify that all elements of the input are simple.
54 for (unsigned i = 0; i != NumElts; ++i) {
55 if (!isa<ConstantInt>(CV->getOperand(i)) &&
56 !isa<ConstantFP>(CV->getOperand(i)))
57 return 0;
Chris Lattner6b3f4752006-04-02 01:38:28 +000058 }
Chris Lattner5c6399e2007-12-11 06:07:39 +000059
60 // Bitcast each element now.
61 std::vector<Constant*> Result;
62 const Type *DstEltTy = DstTy->getElementType();
63 for (unsigned i = 0; i != NumElts; ++i)
Owen Anderson53a52212009-07-13 04:09:18 +000064 Result.push_back(Context.getConstantExprBitCast(CV->getOperand(i),
65 DstEltTy));
66 return Context.getConstantVector(Result);
Chris Lattner6b3f4752006-04-02 01:38:28 +000067}
68
Reid Spencer6c38f0b2006-11-27 01:05:10 +000069/// This function determines which opcode to use to fold two constant cast
70/// expressions together. It uses CastInst::isEliminableCastPair to determine
71/// the opcode. Consequently its just a wrapper around that function.
Reid Spencer05d55b32007-08-05 19:27:01 +000072/// @brief Determine if it is valid to fold a cast of a cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +000073static unsigned
74foldConstantCastPair(
75 unsigned opc, ///< opcode of the second cast constant expression
76 const ConstantExpr*Op, ///< the first cast constant expression
77 const Type *DstTy ///< desintation type of the first cast
78) {
79 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
80 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
81 assert(CastInst::isCast(opc) && "Invalid cast opcode");
82
83 // The the types and opcodes for the two Cast constant expressions
84 const Type *SrcTy = Op->getOperand(0)->getType();
85 const Type *MidTy = Op->getType();
86 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
87 Instruction::CastOps secondOp = Instruction::CastOps(opc);
Chris Lattner6b3f4752006-04-02 01:38:28 +000088
Reid Spencer6c38f0b2006-11-27 01:05:10 +000089 // Let CastInst::isEliminableCastPair do the heavy lifting.
90 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
Reid Spencer8d9336d2006-12-31 05:26:44 +000091 Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +000092}
93
Owen Anderson53a52212009-07-13 04:09:18 +000094static Constant *FoldBitCast(LLVMContext &Context,
95 Constant *V, const Type *DestTy) {
Chris Lattnere8ea0372007-12-11 05:55:02 +000096 const Type *SrcTy = V->getType();
97 if (SrcTy == DestTy)
98 return V; // no-op cast
99
100 // Check to see if we are casting a pointer to an aggregate to a pointer to
101 // the first element. If so, return the appropriate GEP instruction.
102 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
Nate Begemanf2b0b0e2008-03-31 00:22:16 +0000103 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy))
104 if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
105 SmallVector<Value*, 8> IdxList;
Owen Anderson53a52212009-07-13 04:09:18 +0000106 IdxList.push_back(Context.getNullValue(Type::Int32Ty));
Nate Begemanf2b0b0e2008-03-31 00:22:16 +0000107 const Type *ElTy = PTy->getElementType();
108 while (ElTy != DPTy->getElementType()) {
109 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
110 if (STy->getNumElements() == 0) break;
111 ElTy = STy->getElementType(0);
Owen Anderson53a52212009-07-13 04:09:18 +0000112 IdxList.push_back(Context.getNullValue(Type::Int32Ty));
Nate Begemanf2b0b0e2008-03-31 00:22:16 +0000113 } else if (const SequentialType *STy =
114 dyn_cast<SequentialType>(ElTy)) {
115 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
116 ElTy = STy->getElementType();
117 IdxList.push_back(IdxList[0]);
118 } else {
119 break;
120 }
Chris Lattnere8ea0372007-12-11 05:55:02 +0000121 }
Nate Begemanf2b0b0e2008-03-31 00:22:16 +0000122
123 if (ElTy == DPTy->getElementType())
Owen Anderson53a52212009-07-13 04:09:18 +0000124 return Context.getConstantExprGetElementPtr(V, &IdxList[0],
125 IdxList.size());
Chris Lattnere8ea0372007-12-11 05:55:02 +0000126 }
Chris Lattnere8ea0372007-12-11 05:55:02 +0000127
128 // Handle casts from one vector constant to another. We know that the src
129 // and dest type have the same size (otherwise its an illegal cast).
130 if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
131 if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
132 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
133 "Not cast between same sized vectors!");
Devang Pateld26344d2008-11-03 23:20:04 +0000134 SrcTy = NULL;
Chris Lattnere8ea0372007-12-11 05:55:02 +0000135 // First, check for null. Undef is already handled.
136 if (isa<ConstantAggregateZero>(V))
Owen Anderson53a52212009-07-13 04:09:18 +0000137 return Context.getNullValue(DestTy);
Chris Lattnere8ea0372007-12-11 05:55:02 +0000138
Chris Lattner5c6399e2007-12-11 06:07:39 +0000139 if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
Owen Anderson53a52212009-07-13 04:09:18 +0000140 return BitCastConstantVector(Context, CV, DestPTy);
Chris Lattnere8ea0372007-12-11 05:55:02 +0000141 }
Chris Lattner1baace02008-10-16 05:26:51 +0000142
143 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
144 // This allows for other simplifications (although some of them
145 // can only be handled by Analysis/ConstantFolding.cpp).
146 if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
Owen Anderson53a52212009-07-13 04:09:18 +0000147 return Context.getConstantExprBitCast(
148 Context.getConstantVector(&V, 1), DestPTy);
Chris Lattnere8ea0372007-12-11 05:55:02 +0000149 }
150
151 // Finally, implement bitcast folding now. The code below doesn't handle
152 // bitcast right.
153 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
Owen Anderson53a52212009-07-13 04:09:18 +0000154 return Context.getConstantPointerNull(cast<PointerType>(DestTy));
Chris Lattnere8ea0372007-12-11 05:55:02 +0000155
156 // Handle integral constant input.
157 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
158 if (DestTy->isInteger())
159 // Integral -> Integral. This is a no-op because the bit widths must
160 // be the same. Consequently, we just fold to V.
161 return V;
Duncan Sands1ea11732009-02-04 10:17:14 +0000162
163 if (DestTy->isFloatingPoint())
Owen Anderson53a52212009-07-13 04:09:18 +0000164 return Context.getConstantFP(APFloat(CI->getValue(),
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000165 DestTy != Type::PPC_FP128Ty));
Duncan Sands1ea11732009-02-04 10:17:14 +0000166
Chris Lattnere8ea0372007-12-11 05:55:02 +0000167 // Otherwise, can't fold this (vector?)
168 return 0;
169 }
Duncan Sandse7d54792009-02-04 11:17:06 +0000170
Chris Lattnere8ea0372007-12-11 05:55:02 +0000171 // Handle ConstantFP input.
Duncan Sandse7d54792009-02-04 11:17:06 +0000172 if (const ConstantFP *FP = dyn_cast<ConstantFP>(V))
Chris Lattnere8ea0372007-12-11 05:55:02 +0000173 // FP -> Integral.
Owen Andersonedb4a702009-07-24 23:12:02 +0000174 return ConstantInt::get(Context, FP->getValueAPF().bitcastToAPInt());
Duncan Sandse7d54792009-02-04 11:17:06 +0000175
Chris Lattnere8ea0372007-12-11 05:55:02 +0000176 return 0;
177}
178
179
Owen Anderson53a52212009-07-13 04:09:18 +0000180Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context,
181 unsigned opc, const Constant *V,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000182 const Type *DestTy) {
Chris Lattner363485d2007-07-20 22:09:02 +0000183 if (isa<UndefValue>(V)) {
184 // zext(undef) = 0, because the top bits will be zero.
185 // sext(undef) = 0, because the top bits will all be the same.
Chris Lattnerb4c6cc92008-02-19 06:22:12 +0000186 // [us]itofp(undef) = 0, because the result value is bounded.
187 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
188 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
Owen Anderson53a52212009-07-13 04:09:18 +0000189 return Context.getNullValue(DestTy);
190 return Context.getUndef(DestTy);
Chris Lattner363485d2007-07-20 22:09:02 +0000191 }
Dale Johannesen19db0932007-10-14 01:56:47 +0000192 // No compile-time operations on this type yet.
193 if (V->getType() == Type::PPC_FP128Ty || DestTy == Type::PPC_FP128Ty)
194 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000195
196 // If the cast operand is a constant expression, there's a few things we can
197 // do to try to simplify it.
198 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
199 if (CE->isCast()) {
Reid Spencer1a063892006-12-04 02:46:44 +0000200 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000201 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
Owen Anderson53a52212009-07-13 04:09:18 +0000202 return Context.getConstantExprCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000203 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
204 // If all of the indexes in the GEP are null values, there is no pointer
205 // adjustment going on. We might as well cast the source pointer.
206 bool isAllNull = true;
207 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
208 if (!CE->getOperand(i)->isNullValue()) {
209 isAllNull = false;
210 break;
211 }
212 if (isAllNull)
Reid Spencer1a063892006-12-04 02:46:44 +0000213 // This is casting one pointer type to another, always BitCast
Owen Anderson53a52212009-07-13 04:09:18 +0000214 return Context.getConstantExprPointerCast(CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000215 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000216 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000217
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000218 // If the cast operand is a constant vector, perform the cast by
219 // operating on each element. In the cast of bitcasts, the element
220 // count may be mismatched; don't attempt to handle that here.
221 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
222 if (isa<VectorType>(DestTy) &&
223 cast<VectorType>(DestTy)->getNumElements() ==
224 CV->getType()->getNumElements()) {
225 std::vector<Constant*> res;
226 const VectorType *DestVecTy = cast<VectorType>(DestTy);
227 const Type *DstEltTy = DestVecTy->getElementType();
228 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
Owen Anderson53a52212009-07-13 04:09:18 +0000229 res.push_back(Context.getConstantExprCast(opc,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000230 CV->getOperand(i), DstEltTy));
Owen Anderson53a52212009-07-13 04:09:18 +0000231 return Context.getConstantVector(DestVecTy, res);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000232 }
233
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000234 // We actually have to do a cast now. Perform the cast according to the
235 // opcode specified.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000236 switch (opc) {
237 case Instruction::FPTrunc:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000238 case Instruction::FPExt:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000239 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000240 bool ignored;
Dale Johannesenf4bad972007-09-19 14:22:58 +0000241 APFloat Val = FPC->getValueAPF();
242 Val.convert(DestTy == Type::FloatTy ? APFloat::IEEEsingle :
243 DestTy == Type::DoubleTy ? APFloat::IEEEdouble :
244 DestTy == Type::X86_FP80Ty ? APFloat::x87DoubleExtended :
245 DestTy == Type::FP128Ty ? APFloat::IEEEquad :
246 APFloat::Bogus,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000247 APFloat::rmNearestTiesToEven, &ignored);
Owen Anderson53a52212009-07-13 04:09:18 +0000248 return Context.getConstantFP(Val);
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000249 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000250 return 0; // Can't fold.
251 case Instruction::FPToUI:
Reid Spencer8dabca42006-12-19 07:41:40 +0000252 case Instruction::FPToSI:
Reid Spencer81658a82007-02-27 06:23:51 +0000253 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Chris Lattner2b827fd2007-10-15 05:34:10 +0000254 const APFloat &V = FPC->getValueAPF();
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000255 bool ignored;
Dale Johannesenf4bad972007-09-19 14:22:58 +0000256 uint64_t x[2];
Reid Spencer81658a82007-02-27 06:23:51 +0000257 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Dale Johannesen17663f42007-09-25 23:32:20 +0000258 (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000259 APFloat::rmTowardZero, &ignored);
Dale Johannesenf4bad972007-09-19 14:22:58 +0000260 APInt Val(DestBitWidth, 2, x);
Owen Andersonedb4a702009-07-24 23:12:02 +0000261 return ConstantInt::get(Context, Val);
Reid Spencer81658a82007-02-27 06:23:51 +0000262 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000263 return 0; // Can't fold.
264 case Instruction::IntToPtr: //always treated as unsigned
265 if (V->isNullValue()) // Is it an integral null value?
Owen Anderson53a52212009-07-13 04:09:18 +0000266 return Context.getConstantPointerNull(cast<PointerType>(DestTy));
Reid Spencer8dabca42006-12-19 07:41:40 +0000267 return 0; // Other pointer types cannot be casted
268 case Instruction::PtrToInt: // always treated as unsigned
269 if (V->isNullValue()) // is it a null pointer value?
Owen Andersonedb4a702009-07-24 23:12:02 +0000270 return ConstantInt::get(DestTy, 0);
Reid Spencer8dabca42006-12-19 07:41:40 +0000271 return 0; // Other pointer types cannot be casted
272 case Instruction::UIToFP:
Reid Spencer8dabca42006-12-19 07:41:40 +0000273 case Instruction::SIToFP:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000274 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen91506522007-09-30 18:19:03 +0000275 APInt api = CI->getValue();
276 const uint64_t zero[] = {0, 0};
Dale Johannesen91506522007-09-30 18:19:03 +0000277 APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(),
278 2, zero));
Dan Gohman06c45d52008-02-29 01:42:52 +0000279 (void)apf.convertFromAPInt(api,
280 opc==Instruction::SIToFP,
281 APFloat::rmNearestTiesToEven);
Owen Anderson53a52212009-07-13 04:09:18 +0000282 return Context.getConstantFP(apf);
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000283 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000284 return 0;
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000285 case Instruction::ZExt:
Reid Spencer81658a82007-02-27 06:23:51 +0000286 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
287 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
288 APInt Result(CI->getValue());
289 Result.zext(BitWidth);
Owen Andersonedb4a702009-07-24 23:12:02 +0000290 return ConstantInt::get(Context, Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000291 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000292 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000293 case Instruction::SExt:
Reid Spencer81658a82007-02-27 06:23:51 +0000294 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
295 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
296 APInt Result(CI->getValue());
297 Result.sext(BitWidth);
Owen Andersonedb4a702009-07-24 23:12:02 +0000298 return ConstantInt::get(Context, Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000299 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000300 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000301 case Instruction::Trunc:
Reid Spencer81658a82007-02-27 06:23:51 +0000302 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
303 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
304 APInt Result(CI->getValue());
305 Result.trunc(BitWidth);
Owen Andersonedb4a702009-07-24 23:12:02 +0000306 return ConstantInt::get(Context, Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000307 }
Chris Lattner710ebaf2006-12-01 19:22:41 +0000308 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000309 case Instruction::BitCast:
Owen Anderson53a52212009-07-13 04:09:18 +0000310 return FoldBitCast(Context, const_cast<Constant*>(V), DestTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000311 default:
312 assert(!"Invalid CE CastInst opcode");
313 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000314 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000315
Torok Edwinfbcc6632009-07-14 16:55:14 +0000316 llvm_unreachable("Failed to cast constant expression");
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000317 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000318}
319
Owen Anderson53a52212009-07-13 04:09:18 +0000320Constant *llvm::ConstantFoldSelectInstruction(LLVMContext&,
321 const Constant *Cond,
Chris Lattner6ea4b522004-03-12 05:53:32 +0000322 const Constant *V1,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000323 const Constant *V2) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000324 if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
Reid Spencercddc9df2007-01-12 04:24:46 +0000325 return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000326
327 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
328 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
329 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000330 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000331 return 0;
332}
333
Owen Anderson53a52212009-07-13 04:09:18 +0000334Constant *llvm::ConstantFoldExtractElementInstruction(LLVMContext &Context,
335 const Constant *Val,
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000336 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000337 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
Owen Anderson53a52212009-07-13 04:09:18 +0000338 return Context.getUndef(cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000339 if (Val->isNullValue()) // ee(zero, x) -> zero
Owen Anderson53a52212009-07-13 04:09:18 +0000340 return Context.getNullValue(
Reid Spencerd84d35b2007-02-15 02:26:10 +0000341 cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000342
Reid Spencerd84d35b2007-02-15 02:26:10 +0000343 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000344 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000345 return CVal->getOperand(CIdx->getZExtValue());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000346 } else if (isa<UndefValue>(Idx)) {
347 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
Gabor Greiff6caff662008-05-10 08:32:32 +0000348 return CVal->getOperand(0);
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000349 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000350 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000351 return 0;
352}
353
Owen Anderson53a52212009-07-13 04:09:18 +0000354Constant *llvm::ConstantFoldInsertElementInstruction(LLVMContext &Context,
355 const Constant *Val,
Robert Bocchinoca27f032006-01-17 20:07:22 +0000356 const Constant *Elt,
357 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000358 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000359 if (!CIdx) return 0;
Reid Spencer81658a82007-02-27 06:23:51 +0000360 APInt idxVal = CIdx->getValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000361 if (isa<UndefValue>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000362 // Insertion of scalar constant into vector undef
Robert Bocchinoca27f032006-01-17 20:07:22 +0000363 // Optimize away insertion of undef
364 if (isa<UndefValue>(Elt))
365 return const_cast<Constant*>(Val);
366 // Otherwise break the aggregate undef into multiple undefs and do
367 // the insertion
368 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000369 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000370 std::vector<Constant*> Ops;
371 Ops.reserve(numOps);
372 for (unsigned i = 0; i < numOps; ++i) {
373 const Constant *Op =
Owen Anderson53a52212009-07-13 04:09:18 +0000374 (idxVal == i) ? Elt : Context.getUndef(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000375 Ops.push_back(const_cast<Constant*>(Op));
376 }
Owen Anderson53a52212009-07-13 04:09:18 +0000377 return Context.getConstantVector(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000378 }
Reid Spencer3054b142006-11-02 08:18:15 +0000379 if (isa<ConstantAggregateZero>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000380 // Insertion of scalar constant into vector aggregate zero
Robert Bocchinoca27f032006-01-17 20:07:22 +0000381 // Optimize away insertion of zero
382 if (Elt->isNullValue())
383 return const_cast<Constant*>(Val);
384 // Otherwise break the aggregate zero into multiple zeros and do
385 // the insertion
386 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000387 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000388 std::vector<Constant*> Ops;
389 Ops.reserve(numOps);
390 for (unsigned i = 0; i < numOps; ++i) {
391 const Constant *Op =
Owen Anderson53a52212009-07-13 04:09:18 +0000392 (idxVal == i) ? Elt : Context.getNullValue(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000393 Ops.push_back(const_cast<Constant*>(Op));
394 }
Owen Anderson53a52212009-07-13 04:09:18 +0000395 return Context.getConstantVector(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000396 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000397 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000398 // Insertion of scalar constant into vector constant
Robert Bocchinoca27f032006-01-17 20:07:22 +0000399 std::vector<Constant*> Ops;
400 Ops.reserve(CVal->getNumOperands());
401 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
402 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000403 (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
Robert Bocchinoca27f032006-01-17 20:07:22 +0000404 Ops.push_back(const_cast<Constant*>(Op));
405 }
Owen Anderson53a52212009-07-13 04:09:18 +0000406 return Context.getConstantVector(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000407 }
Dan Gohman3db11c22008-06-03 00:15:20 +0000408
Robert Bocchinoca27f032006-01-17 20:07:22 +0000409 return 0;
410}
411
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000412/// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef
413/// return the specified element value. Otherwise return null.
Owen Anderson53a52212009-07-13 04:09:18 +0000414static Constant *GetVectorElement(LLVMContext &Context, const Constant *C,
415 unsigned EltNo) {
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000416 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C))
Gabor Greiff6caff662008-05-10 08:32:32 +0000417 return CV->getOperand(EltNo);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000418
419 const Type *EltTy = cast<VectorType>(C->getType())->getElementType();
420 if (isa<ConstantAggregateZero>(C))
Owen Anderson53a52212009-07-13 04:09:18 +0000421 return Context.getNullValue(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000422 if (isa<UndefValue>(C))
Owen Anderson53a52212009-07-13 04:09:18 +0000423 return Context.getUndef(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000424 return 0;
425}
426
Owen Anderson53a52212009-07-13 04:09:18 +0000427Constant *llvm::ConstantFoldShuffleVectorInstruction(LLVMContext &Context,
428 const Constant *V1,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000429 const Constant *V2,
430 const Constant *Mask) {
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000431 // Undefined shuffle mask -> undefined value.
Owen Anderson53a52212009-07-13 04:09:18 +0000432 if (isa<UndefValue>(Mask)) return Context.getUndef(V1->getType());
Mon P Wang25f01062008-11-10 04:46:22 +0000433
434 unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements();
435 unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements();
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000436 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
Mon P Wang25f01062008-11-10 04:46:22 +0000437
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000438 // Loop over the shuffle mask, evaluating each element.
439 SmallVector<Constant*, 32> Result;
Mon P Wang25f01062008-11-10 04:46:22 +0000440 for (unsigned i = 0; i != MaskNumElts; ++i) {
Owen Anderson53a52212009-07-13 04:09:18 +0000441 Constant *InElt = GetVectorElement(Context, Mask, i);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000442 if (InElt == 0) return 0;
Mon P Wang25f01062008-11-10 04:46:22 +0000443
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000444 if (isa<UndefValue>(InElt))
Owen Anderson53a52212009-07-13 04:09:18 +0000445 InElt = Context.getUndef(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000446 else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) {
447 unsigned Elt = CI->getZExtValue();
Mon P Wang25f01062008-11-10 04:46:22 +0000448 if (Elt >= SrcNumElts*2)
Owen Anderson53a52212009-07-13 04:09:18 +0000449 InElt = Context.getUndef(EltTy);
Mon P Wang25f01062008-11-10 04:46:22 +0000450 else if (Elt >= SrcNumElts)
Owen Anderson53a52212009-07-13 04:09:18 +0000451 InElt = GetVectorElement(Context, V2, Elt - SrcNumElts);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000452 else
Owen Anderson53a52212009-07-13 04:09:18 +0000453 InElt = GetVectorElement(Context, V1, Elt);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000454 if (InElt == 0) return 0;
455 } else {
456 // Unknown value.
457 return 0;
458 }
459 Result.push_back(InElt);
460 }
Mon P Wang25f01062008-11-10 04:46:22 +0000461
Owen Anderson53a52212009-07-13 04:09:18 +0000462 return Context.getConstantVector(&Result[0], Result.size());
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000463}
464
Owen Anderson53a52212009-07-13 04:09:18 +0000465Constant *llvm::ConstantFoldExtractValueInstruction(LLVMContext &Context,
466 const Constant *Agg,
Dan Gohman3db11c22008-06-03 00:15:20 +0000467 const unsigned *Idxs,
468 unsigned NumIdx) {
469 // Base case: no indices, so return the entire value.
470 if (NumIdx == 0)
471 return const_cast<Constant *>(Agg);
472
473 if (isa<UndefValue>(Agg)) // ev(undef, x) -> undef
Owen Anderson53a52212009-07-13 04:09:18 +0000474 return Context.getUndef(ExtractValueInst::getIndexedType(Agg->getType(),
Dan Gohman3db11c22008-06-03 00:15:20 +0000475 Idxs,
476 Idxs + NumIdx));
477
478 if (isa<ConstantAggregateZero>(Agg)) // ev(0, x) -> 0
479 return
Owen Anderson53a52212009-07-13 04:09:18 +0000480 Context.getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
Dan Gohman3db11c22008-06-03 00:15:20 +0000481 Idxs,
482 Idxs + NumIdx));
483
484 // Otherwise recurse.
Owen Anderson53a52212009-07-13 04:09:18 +0000485 return ConstantFoldExtractValueInstruction(Context, Agg->getOperand(*Idxs),
Dan Gohman3db11c22008-06-03 00:15:20 +0000486 Idxs+1, NumIdx-1);
Dan Gohman12fce772008-05-15 19:50:34 +0000487}
488
Owen Anderson53a52212009-07-13 04:09:18 +0000489Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
490 const Constant *Agg,
Dan Gohman3db11c22008-06-03 00:15:20 +0000491 const Constant *Val,
492 const unsigned *Idxs,
493 unsigned NumIdx) {
494 // Base case: no indices, so replace the entire value.
495 if (NumIdx == 0)
496 return const_cast<Constant *>(Val);
497
498 if (isa<UndefValue>(Agg)) {
499 // Insertion of constant into aggregate undef
500 // Optimize away insertion of undef
501 if (isa<UndefValue>(Val))
502 return const_cast<Constant*>(Agg);
503 // Otherwise break the aggregate undef into multiple undefs and do
504 // the insertion
505 const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
506 unsigned numOps;
507 if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
508 numOps = AR->getNumElements();
509 else
510 numOps = cast<StructType>(AggTy)->getNumElements();
511 std::vector<Constant*> Ops(numOps);
512 for (unsigned i = 0; i < numOps; ++i) {
513 const Type *MemberTy = AggTy->getTypeAtIndex(i);
514 const Constant *Op =
515 (*Idxs == i) ?
Owen Anderson53a52212009-07-13 04:09:18 +0000516 ConstantFoldInsertValueInstruction(Context, Context.getUndef(MemberTy),
Dan Gohman3db11c22008-06-03 00:15:20 +0000517 Val, Idxs+1, NumIdx-1) :
Owen Anderson53a52212009-07-13 04:09:18 +0000518 Context.getUndef(MemberTy);
Dan Gohman3db11c22008-06-03 00:15:20 +0000519 Ops[i] = const_cast<Constant*>(Op);
520 }
521 if (isa<StructType>(AggTy))
Owen Anderson53a52212009-07-13 04:09:18 +0000522 return Context.getConstantStruct(Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000523 else
Owen Anderson53a52212009-07-13 04:09:18 +0000524 return Context.getConstantArray(cast<ArrayType>(AggTy), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000525 }
526 if (isa<ConstantAggregateZero>(Agg)) {
527 // Insertion of constant into aggregate zero
528 // Optimize away insertion of zero
529 if (Val->isNullValue())
530 return const_cast<Constant*>(Agg);
531 // Otherwise break the aggregate zero into multiple zeros and do
532 // the insertion
533 const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
534 unsigned numOps;
535 if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
536 numOps = AR->getNumElements();
537 else
538 numOps = cast<StructType>(AggTy)->getNumElements();
539 std::vector<Constant*> Ops(numOps);
540 for (unsigned i = 0; i < numOps; ++i) {
541 const Type *MemberTy = AggTy->getTypeAtIndex(i);
542 const Constant *Op =
543 (*Idxs == i) ?
Owen Anderson53a52212009-07-13 04:09:18 +0000544 ConstantFoldInsertValueInstruction(Context,
545 Context.getNullValue(MemberTy),
Dan Gohman3db11c22008-06-03 00:15:20 +0000546 Val, Idxs+1, NumIdx-1) :
Owen Anderson53a52212009-07-13 04:09:18 +0000547 Context.getNullValue(MemberTy);
Dan Gohman3db11c22008-06-03 00:15:20 +0000548 Ops[i] = const_cast<Constant*>(Op);
549 }
550 if (isa<StructType>(AggTy))
Owen Anderson53a52212009-07-13 04:09:18 +0000551 return Context.getConstantStruct(Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000552 else
Owen Anderson53a52212009-07-13 04:09:18 +0000553 return Context.getConstantArray(cast<ArrayType>(AggTy), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000554 }
555 if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
556 // Insertion of constant into aggregate constant
557 std::vector<Constant*> Ops(Agg->getNumOperands());
558 for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
559 const Constant *Op =
560 (*Idxs == i) ?
Owen Anderson53a52212009-07-13 04:09:18 +0000561 ConstantFoldInsertValueInstruction(Context, Agg->getOperand(i),
Dan Gohman3db11c22008-06-03 00:15:20 +0000562 Val, Idxs+1, NumIdx-1) :
563 Agg->getOperand(i);
564 Ops[i] = const_cast<Constant*>(Op);
565 }
566 Constant *C;
567 if (isa<StructType>(Agg->getType()))
Owen Anderson53a52212009-07-13 04:09:18 +0000568 C = Context.getConstantStruct(Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000569 else
Owen Anderson53a52212009-07-13 04:09:18 +0000570 C = Context.getConstantArray(cast<ArrayType>(Agg->getType()), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000571 return C;
572 }
573
Dan Gohman12fce772008-05-15 19:50:34 +0000574 return 0;
575}
576
Reid Spencer266e42b2006-12-23 06:05:41 +0000577
Owen Anderson53a52212009-07-13 04:09:18 +0000578Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context,
579 unsigned Opcode,
Reid Spencer266e42b2006-12-23 06:05:41 +0000580 const Constant *C1,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000581 const Constant *C2) {
Dale Johannesene5facd52007-10-16 23:38:29 +0000582 // No compile-time operations on this type yet.
583 if (C1->getType() == Type::PPC_FP128Ty)
584 return 0;
585
Reid Spencer266e42b2006-12-23 06:05:41 +0000586 // Handle UndefValue up front
587 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
588 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +0000589 case Instruction::Xor:
590 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
591 // Handle undef ^ undef -> 0 special case. This is a common
592 // idiom (misuse).
Owen Anderson53a52212009-07-13 04:09:18 +0000593 return Context.getNullValue(C1->getType());
Evan Chengdf1690d2008-03-25 20:08:07 +0000594 // Fallthrough
Reid Spencer266e42b2006-12-23 06:05:41 +0000595 case Instruction::Add:
596 case Instruction::Sub:
Owen Anderson53a52212009-07-13 04:09:18 +0000597 return Context.getUndef(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000598 case Instruction::Mul:
599 case Instruction::And:
Owen Anderson53a52212009-07-13 04:09:18 +0000600 return Context.getNullValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000601 case Instruction::UDiv:
602 case Instruction::SDiv:
Reid Spencer266e42b2006-12-23 06:05:41 +0000603 case Instruction::URem:
604 case Instruction::SRem:
Reid Spencer266e42b2006-12-23 06:05:41 +0000605 if (!isa<UndefValue>(C2)) // undef / X -> 0
Owen Anderson53a52212009-07-13 04:09:18 +0000606 return Context.getNullValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000607 return const_cast<Constant*>(C2); // X / undef -> undef
608 case Instruction::Or: // X | undef -> -1
Reid Spencerd84d35b2007-02-15 02:26:10 +0000609 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
Owen Anderson53a52212009-07-13 04:09:18 +0000610 return Context.getAllOnesValue(PTy);
611 return Context.getAllOnesValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000612 case Instruction::LShr:
613 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
614 return const_cast<Constant*>(C1); // undef lshr undef -> undef
Owen Anderson53a52212009-07-13 04:09:18 +0000615 return Context.getNullValue(C1->getType()); // X lshr undef -> 0
Reid Spencer266e42b2006-12-23 06:05:41 +0000616 // undef lshr X -> 0
617 case Instruction::AShr:
618 if (!isa<UndefValue>(C2))
619 return const_cast<Constant*>(C1); // undef ashr X --> undef
620 else if (isa<UndefValue>(C1))
621 return const_cast<Constant*>(C1); // undef ashr undef -> undef
622 else
623 return const_cast<Constant*>(C1); // X ashr undef --> X
624 case Instruction::Shl:
625 // undef << X -> 0 or X << undef -> 0
Owen Anderson53a52212009-07-13 04:09:18 +0000626 return Context.getNullValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000627 }
628 }
629
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000630 // Handle simplifications when the RHS is a constant int.
Chris Lattner334d33c2008-04-19 21:58:19 +0000631 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
632 switch (Opcode) {
633 case Instruction::Add:
634 if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X + 0 == X
635 break;
636 case Instruction::Sub:
637 if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X - 0 == X
638 break;
639 case Instruction::Mul:
640 if (CI2->equalsInt(0)) return const_cast<Constant*>(C2); // X * 0 == 0
641 if (CI2->equalsInt(1))
642 return const_cast<Constant*>(C1); // X * 1 == X
643 break;
644 case Instruction::UDiv:
645 case Instruction::SDiv:
646 if (CI2->equalsInt(1))
647 return const_cast<Constant*>(C1); // X / 1 == X
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000648 if (CI2->equalsInt(0))
Owen Anderson53a52212009-07-13 04:09:18 +0000649 return Context.getUndef(CI2->getType()); // X / 0 == undef
Chris Lattner334d33c2008-04-19 21:58:19 +0000650 break;
651 case Instruction::URem:
652 case Instruction::SRem:
653 if (CI2->equalsInt(1))
Owen Anderson53a52212009-07-13 04:09:18 +0000654 return Context.getNullValue(CI2->getType()); // X % 1 == 0
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000655 if (CI2->equalsInt(0))
Owen Anderson53a52212009-07-13 04:09:18 +0000656 return Context.getUndef(CI2->getType()); // X % 0 == undef
Chris Lattner334d33c2008-04-19 21:58:19 +0000657 break;
658 case Instruction::And:
659 if (CI2->isZero()) return const_cast<Constant*>(C2); // X & 0 == 0
660 if (CI2->isAllOnesValue())
661 return const_cast<Constant*>(C1); // X & -1 == X
662
Chris Lattner334d33c2008-04-19 21:58:19 +0000663 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000664 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
Chris Lattner334d33c2008-04-19 21:58:19 +0000665 if (CE1->getOpcode() == Instruction::ZExt) {
666 unsigned DstWidth = CI2->getType()->getBitWidth();
667 unsigned SrcWidth =
668 CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
669 APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
670 if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
671 return const_cast<Constant*>(C1);
Chris Lattner6d94bb72007-03-25 05:47:04 +0000672 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000673
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000674 // If and'ing the address of a global with a constant, fold it.
Chris Lattner334d33c2008-04-19 21:58:19 +0000675 if (CE1->getOpcode() == Instruction::PtrToInt &&
676 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000677 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
Chris Lattner334d33c2008-04-19 21:58:19 +0000678
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000679 // Functions are at least 4-byte aligned.
680 unsigned GVAlign = GV->getAlignment();
681 if (isa<Function>(GV))
682 GVAlign = std::max(GVAlign, 4U);
683
684 if (GVAlign > 1) {
685 unsigned DstWidth = CI2->getType()->getBitWidth();
Chris Lattner912bec72008-04-20 19:59:12 +0000686 unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000687 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
688
689 // If checking bits we know are clear, return zero.
690 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
Owen Anderson53a52212009-07-13 04:09:18 +0000691 return Context.getNullValue(CI2->getType());
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000692 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000693 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000694 }
695 break;
696 case Instruction::Or:
697 if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X | 0 == X
698 if (CI2->isAllOnesValue())
699 return const_cast<Constant*>(C2); // X | -1 == -1
700 break;
701 case Instruction::Xor:
702 if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X ^ 0 == X
703 break;
704 case Instruction::AShr:
705 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
706 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
Chris Lattner6d94bb72007-03-25 05:47:04 +0000707 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
Owen Anderson53a52212009-07-13 04:09:18 +0000708 return Context.getConstantExprLShr(const_cast<Constant*>(C1),
709 const_cast<Constant*>(C2));
Chris Lattner334d33c2008-04-19 21:58:19 +0000710 break;
Reid Spencer266e42b2006-12-23 06:05:41 +0000711 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000712 }
713
Chris Lattner6b056052008-04-20 18:24:14 +0000714 // At this point we know neither constant is an UndefValue.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000715 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
716 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000717 using namespace APIntOps;
Chris Lattner6b056052008-04-20 18:24:14 +0000718 const APInt &C1V = CI1->getValue();
719 const APInt &C2V = CI2->getValue();
Chris Lattner344da522007-01-12 18:42:52 +0000720 switch (Opcode) {
721 default:
722 break;
723 case Instruction::Add:
Owen Andersonedb4a702009-07-24 23:12:02 +0000724 return ConstantInt::get(Context, C1V + C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000725 case Instruction::Sub:
Owen Andersonedb4a702009-07-24 23:12:02 +0000726 return ConstantInt::get(Context, C1V - C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000727 case Instruction::Mul:
Owen Andersonedb4a702009-07-24 23:12:02 +0000728 return ConstantInt::get(Context, C1V * C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000729 case Instruction::UDiv:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000730 assert(!CI2->isNullValue() && "Div by zero handled above");
Owen Andersonedb4a702009-07-24 23:12:02 +0000731 return ConstantInt::get(Context, C1V.udiv(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000732 case Instruction::SDiv:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000733 assert(!CI2->isNullValue() && "Div by zero handled above");
Reid Spencer81658a82007-02-27 06:23:51 +0000734 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
Owen Anderson53a52212009-07-13 04:09:18 +0000735 return Context.getUndef(CI1->getType()); // MIN_INT / -1 -> undef
Owen Andersonedb4a702009-07-24 23:12:02 +0000736 return ConstantInt::get(Context, C1V.sdiv(C2V));
Reid Spencer81658a82007-02-27 06:23:51 +0000737 case Instruction::URem:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000738 assert(!CI2->isNullValue() && "Div by zero handled above");
Owen Andersonedb4a702009-07-24 23:12:02 +0000739 return ConstantInt::get(Context, C1V.urem(C2V));
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000740 case Instruction::SRem:
741 assert(!CI2->isNullValue() && "Div by zero handled above");
Reid Spencer81658a82007-02-27 06:23:51 +0000742 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
Owen Anderson53a52212009-07-13 04:09:18 +0000743 return Context.getUndef(CI1->getType()); // MIN_INT % -1 -> undef
Owen Andersonedb4a702009-07-24 23:12:02 +0000744 return ConstantInt::get(Context, C1V.srem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000745 case Instruction::And:
Owen Andersonedb4a702009-07-24 23:12:02 +0000746 return ConstantInt::get(Context, C1V & C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000747 case Instruction::Or:
Owen Andersonedb4a702009-07-24 23:12:02 +0000748 return ConstantInt::get(Context, C1V | C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000749 case Instruction::Xor:
Owen Andersonedb4a702009-07-24 23:12:02 +0000750 return ConstantInt::get(Context, C1V ^ C2V);
Chris Lattner6b056052008-04-20 18:24:14 +0000751 case Instruction::Shl: {
752 uint32_t shiftAmt = C2V.getZExtValue();
753 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000754 return ConstantInt::get(Context, C1V.shl(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000755 else
Owen Anderson53a52212009-07-13 04:09:18 +0000756 return Context.getUndef(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000757 }
758 case Instruction::LShr: {
759 uint32_t shiftAmt = C2V.getZExtValue();
760 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000761 return ConstantInt::get(Context, C1V.lshr(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000762 else
Owen Anderson53a52212009-07-13 04:09:18 +0000763 return Context.getUndef(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000764 }
765 case Instruction::AShr: {
766 uint32_t shiftAmt = C2V.getZExtValue();
767 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000768 return ConstantInt::get(Context, C1V.ashr(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000769 else
Owen Anderson53a52212009-07-13 04:09:18 +0000770 return Context.getUndef(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000771 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000772 }
773 }
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000774
775 switch (Opcode) {
776 case Instruction::SDiv:
777 case Instruction::UDiv:
778 case Instruction::URem:
779 case Instruction::SRem:
780 case Instruction::LShr:
781 case Instruction::AShr:
782 case Instruction::Shl:
783 if (CI1->equalsInt(0)) return const_cast<Constant*>(C1);
784 break;
785 default:
786 break;
787 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000788 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
789 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000790 APFloat C1V = CFP1->getValueAPF();
791 APFloat C2V = CFP2->getValueAPF();
792 APFloat C3V = C1V; // copy for modification
Reid Spencer266e42b2006-12-23 06:05:41 +0000793 switch (Opcode) {
794 default:
795 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000796 case Instruction::FAdd:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000797 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson53a52212009-07-13 04:09:18 +0000798 return Context.getConstantFP(C3V);
Dan Gohmana5b96452009-06-04 22:49:04 +0000799 case Instruction::FSub:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000800 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson53a52212009-07-13 04:09:18 +0000801 return Context.getConstantFP(C3V);
Dan Gohmana5b96452009-06-04 22:49:04 +0000802 case Instruction::FMul:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000803 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson53a52212009-07-13 04:09:18 +0000804 return Context.getConstantFP(C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000805 case Instruction::FDiv:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000806 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson53a52212009-07-13 04:09:18 +0000807 return Context.getConstantFP(C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000808 case Instruction::FRem:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000809 (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson53a52212009-07-13 04:09:18 +0000810 return Context.getConstantFP(C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000811 }
812 }
Dan Gohman9f396602007-10-30 19:00:49 +0000813 } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
814 const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
815 const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000816 if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
817 (CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
Owen Anderson85f86dc2009-07-13 22:41:06 +0000818 std::vector<Constant*> Res;
819 const Type* EltTy = VTy->getElementType();
820 const Constant *C1 = 0;
821 const Constant *C2 = 0;
Reid Spencer266e42b2006-12-23 06:05:41 +0000822 switch (Opcode) {
Chris Lattner6072ead2008-04-19 21:13:00 +0000823 default:
824 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000825 case Instruction::Add:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000826 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
827 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
828 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
829 Res.push_back(Context.getConstantExprAdd(const_cast<Constant*>(C1),
830 const_cast<Constant*>(C2)));
831 }
832 return Context.getConstantVector(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000833 case Instruction::FAdd:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000834 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
835 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
836 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
837 Res.push_back(Context.getConstantExprFAdd(const_cast<Constant*>(C1),
838 const_cast<Constant*>(C2)));
839 }
840 return Context.getConstantVector(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000841 case Instruction::Sub:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000842 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
843 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
844 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
845 Res.push_back(Context.getConstantExprSub(const_cast<Constant*>(C1),
846 const_cast<Constant*>(C2)));
847 }
848 return Context.getConstantVector(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000849 case Instruction::FSub:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000850 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
851 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
852 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
853 Res.push_back(Context.getConstantExprFSub(const_cast<Constant*>(C1),
854 const_cast<Constant*>(C2)));
855 }
856 return Context.getConstantVector(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000857 case Instruction::Mul:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000858 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
859 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
860 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
861 Res.push_back(Context.getConstantExprMul(const_cast<Constant*>(C1),
862 const_cast<Constant*>(C2)));
863 }
864 return Context.getConstantVector(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000865 case Instruction::FMul:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000866 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
867 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
868 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
869 Res.push_back(Context.getConstantExprFMul(const_cast<Constant*>(C1),
870 const_cast<Constant*>(C2)));
871 }
872 return Context.getConstantVector(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000873 case Instruction::UDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000874 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
875 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
876 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
877 Res.push_back(Context.getConstantExprUDiv(const_cast<Constant*>(C1),
878 const_cast<Constant*>(C2)));
879 }
880 return Context.getConstantVector(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000881 case Instruction::SDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000882 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
883 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
884 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
885 Res.push_back(Context.getConstantExprSDiv(const_cast<Constant*>(C1),
886 const_cast<Constant*>(C2)));
887 }
888 return Context.getConstantVector(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000889 case Instruction::FDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000890 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
891 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
892 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
893 Res.push_back(Context.getConstantExprFDiv(const_cast<Constant*>(C1),
894 const_cast<Constant*>(C2)));
895 }
896 return Context.getConstantVector(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000897 case Instruction::URem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000898 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
899 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
900 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
901 Res.push_back(Context.getConstantExprURem(const_cast<Constant*>(C1),
902 const_cast<Constant*>(C2)));
903 }
904 return Context.getConstantVector(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000905 case Instruction::SRem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000906 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
907 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
908 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
909 Res.push_back(Context.getConstantExprSRem(const_cast<Constant*>(C1),
910 const_cast<Constant*>(C2)));
911 }
912 return Context.getConstantVector(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000913 case Instruction::FRem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000914 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
915 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
916 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
917 Res.push_back(Context.getConstantExprFRem(const_cast<Constant*>(C1),
918 const_cast<Constant*>(C2)));
919 }
920 return Context.getConstantVector(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000921 case Instruction::And:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000922 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
923 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
924 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
925 Res.push_back(Context.getConstantExprAnd(const_cast<Constant*>(C1),
926 const_cast<Constant*>(C2)));
927 }
928 return Context.getConstantVector(Res);
929 case Instruction::Or:
930 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
931 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
932 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
933 Res.push_back(Context.getConstantExprOr(const_cast<Constant*>(C1),
934 const_cast<Constant*>(C2)));
935 }
936 return Context.getConstantVector(Res);
937 case Instruction::Xor:
938 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
939 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
940 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
941 Res.push_back(Context.getConstantExprXor(const_cast<Constant*>(C1),
942 const_cast<Constant*>(C2)));
943 }
944 return Context.getConstantVector(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000945 case Instruction::LShr:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000946 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
947 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
948 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
949 Res.push_back(Context.getConstantExprLShr(const_cast<Constant*>(C1),
950 const_cast<Constant*>(C2)));
951 }
952 return Context.getConstantVector(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000953 case Instruction::AShr:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000954 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
955 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
956 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
957 Res.push_back(Context.getConstantExprAShr(const_cast<Constant*>(C1),
958 const_cast<Constant*>(C2)));
959 }
960 return Context.getConstantVector(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000961 case Instruction::Shl:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000962 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
963 C1 = CP1 ? CP1->getOperand(i) : Context.getNullValue(EltTy);
964 C2 = CP2 ? CP2->getOperand(i) : Context.getNullValue(EltTy);
965 Res.push_back(Context.getConstantExprShl(const_cast<Constant*>(C1),
966 const_cast<Constant*>(C2)));
967 }
968 return Context.getConstantVector(Res);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000969 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000970 }
971 }
972
Chris Lattner6b056052008-04-20 18:24:14 +0000973 if (isa<ConstantExpr>(C1)) {
974 // There are many possible foldings we could do here. We should probably
975 // at least fold add of a pointer with an integer into the appropriate
976 // getelementptr. This will improve alias analysis a bit.
977 } else if (isa<ConstantExpr>(C2)) {
978 // If C2 is a constant expr and C1 isn't, flop them around and fold the
979 // other way if possible.
980 switch (Opcode) {
981 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000982 case Instruction::FAdd:
Chris Lattner6b056052008-04-20 18:24:14 +0000983 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000984 case Instruction::FMul:
Chris Lattner6b056052008-04-20 18:24:14 +0000985 case Instruction::And:
986 case Instruction::Or:
987 case Instruction::Xor:
988 // No change of opcode required.
Owen Anderson53a52212009-07-13 04:09:18 +0000989 return ConstantFoldBinaryInstruction(Context, Opcode, C2, C1);
Chris Lattner6b056052008-04-20 18:24:14 +0000990
991 case Instruction::Shl:
992 case Instruction::LShr:
993 case Instruction::AShr:
994 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +0000995 case Instruction::FSub:
Chris Lattner6b056052008-04-20 18:24:14 +0000996 case Instruction::SDiv:
997 case Instruction::UDiv:
998 case Instruction::FDiv:
999 case Instruction::URem:
1000 case Instruction::SRem:
1001 case Instruction::FRem:
1002 default: // These instructions cannot be flopped around.
1003 break;
1004 }
1005 }
1006
1007 // We don't know how to fold this.
Reid Spencer266e42b2006-12-23 06:05:41 +00001008 return 0;
1009}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001010
Chris Lattner60c47262005-01-28 19:09:51 +00001011/// isZeroSizedType - This type is zero sized if its an array or structure of
1012/// zero sized types. The only leaf zero sized type is an empty structure.
1013static bool isMaybeZeroSizedType(const Type *Ty) {
1014 if (isa<OpaqueType>(Ty)) return true; // Can't say.
1015 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1016
1017 // If all of elements have zero size, this does too.
1018 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +00001019 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +00001020 return true;
1021
1022 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1023 return isMaybeZeroSizedType(ATy->getElementType());
1024 }
1025 return false;
1026}
Chris Lattner6ea4b522004-03-12 05:53:32 +00001027
Chris Lattner061da2f2004-01-13 05:51:55 +00001028/// IdxCompare - Compare the two constants as though they were getelementptr
1029/// indices. This allows coersion of the types to be the same thing.
1030///
1031/// If the two constants are the "same" (after coersion), return 0. If the
1032/// first is less than the second, return -1, if the second is less than the
1033/// first, return 1. If the constants are not integral, return -2.
1034///
Owen Anderson53a52212009-07-13 04:09:18 +00001035static int IdxCompare(LLVMContext &Context, Constant *C1, Constant *C2,
1036 const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001037 if (C1 == C2) return 0;
1038
Reid Spencerc90cf772006-12-31 21:43:30 +00001039 // Ok, we found a different index. If they are not ConstantInt, we can't do
1040 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +00001041 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1042 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +00001043
Chris Lattner69193f92004-04-05 01:30:19 +00001044 // Ok, we have two differing integer indices. Sign extend them to be the same
1045 // type. Long is always big enough, so we use it.
Reid Spencer8d9336d2006-12-31 05:26:44 +00001046 if (C1->getType() != Type::Int64Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001047 C1 = Context.getConstantExprSExt(C1, Type::Int64Ty);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001048
Reid Spencer8d9336d2006-12-31 05:26:44 +00001049 if (C2->getType() != Type::Int64Ty)
Owen Anderson53a52212009-07-13 04:09:18 +00001050 C2 = Context.getConstantExprSExt(C2, Type::Int64Ty);
Reid Spencer8d9336d2006-12-31 05:26:44 +00001051
1052 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +00001053
Chris Lattner60c47262005-01-28 19:09:51 +00001054 // If the type being indexed over is really just a zero sized type, there is
1055 // no pointer difference being made here.
1056 if (isMaybeZeroSizedType(ElTy))
1057 return -2; // dunno.
1058
Chris Lattner061da2f2004-01-13 05:51:55 +00001059 // If they are really different, now that they are the same type, then we
1060 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +00001061 if (cast<ConstantInt>(C1)->getSExtValue() <
1062 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +00001063 return -1;
1064 else
1065 return 1;
1066}
1067
Chris Lattner858f4e92007-01-04 02:13:20 +00001068/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +00001069/// decide about the two constants provided. This doesn't need to handle simple
1070/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
1071/// If we can determine that the two constants have a particular relation to
1072/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001073/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1074/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +00001075///
1076/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +00001077/// operand is always the most "complex" of the two. We consider ConstantFP
1078/// to be the simplest, and ConstantExprs to be the most complex.
Owen Anderson53a52212009-07-13 04:09:18 +00001079static FCmpInst::Predicate evaluateFCmpRelation(LLVMContext &Context,
1080 const Constant *V1,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001081 const Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001082 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +00001083 "Cannot compare values of different types!");
Dale Johannesen19db0932007-10-14 01:56:47 +00001084
1085 // No compile-time operations on this type yet.
1086 if (V1->getType() == Type::PPC_FP128Ty)
1087 return FCmpInst::BAD_FCMP_PREDICATE;
1088
Reid Spencer9d36acf2006-12-24 18:52:08 +00001089 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +00001090 if (V1 == V2) return FCmpInst::FCMP_OEQ;
1091
Reid Spencer9d36acf2006-12-24 18:52:08 +00001092 if (!isa<ConstantExpr>(V1)) {
1093 if (!isa<ConstantExpr>(V2)) {
1094 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +00001095 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001096 Constant *C1 = const_cast<Constant*>(V1);
1097 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng75b871f2007-01-11 12:24:14 +00001098 R = dyn_cast<ConstantInt>(
Owen Anderson53a52212009-07-13 04:09:18 +00001099 Context.getConstantExprFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001100 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001101 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001102 R = dyn_cast<ConstantInt>(
Owen Anderson53a52212009-07-13 04:09:18 +00001103 Context.getConstantExprFCmp(FCmpInst::FCMP_OLT, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001104 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001105 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001106 R = dyn_cast<ConstantInt>(
Owen Anderson53a52212009-07-13 04:09:18 +00001107 Context.getConstantExprFCmp(FCmpInst::FCMP_OGT, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001108 if (R && !R->isZero())
Reid Spencer9d36acf2006-12-24 18:52:08 +00001109 return FCmpInst::FCMP_OGT;
1110
1111 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +00001112 return FCmpInst::BAD_FCMP_PREDICATE;
1113 }
1114
Reid Spencer9d36acf2006-12-24 18:52:08 +00001115 // If the first operand is simple and second is ConstantExpr, swap operands.
Owen Anderson53a52212009-07-13 04:09:18 +00001116 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(Context, V2, V1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001117 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1118 return FCmpInst::getSwappedPredicate(SwappedRelation);
1119 } else {
1120 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1121 // constantexpr or a simple constant.
1122 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1123 switch (CE1->getOpcode()) {
1124 case Instruction::FPTrunc:
1125 case Instruction::FPExt:
1126 case Instruction::UIToFP:
1127 case Instruction::SIToFP:
1128 // We might be able to do something with these but we don't right now.
1129 break;
1130 default:
1131 break;
1132 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001133 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001134 // There are MANY other foldings that we could perform here. They will
1135 // probably be added on demand, as they seem needed.
1136 return FCmpInst::BAD_FCMP_PREDICATE;
1137}
1138
1139/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +00001140/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +00001141/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +00001142/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +00001143/// particular relation to each other, we should return the corresponding ICmp
1144/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +00001145///
1146/// To simplify this code we canonicalize the relation so that the first
1147/// operand is always the most "complex" of the two. We consider simple
1148/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +00001149/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +00001150///
Owen Anderson53a52212009-07-13 04:09:18 +00001151static ICmpInst::Predicate evaluateICmpRelation(LLVMContext &Context,
1152 const Constant *V1,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001153 const Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +00001154 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001155 assert(V1->getType() == V2->getType() &&
1156 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001157 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001158
Reid Spenceraccd7c72004-07-17 23:47:01 +00001159 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001160 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1161 // We distilled this down to a simple case, use the standard constant
1162 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001163 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001164 Constant *C1 = const_cast<Constant*>(V1);
1165 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001166 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Owen Anderson53a52212009-07-13 04:09:18 +00001167 R = dyn_cast<ConstantInt>(Context.getConstantExprICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001168 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001169 return pred;
1170 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Owen Anderson53a52212009-07-13 04:09:18 +00001171 R = dyn_cast<ConstantInt>(Context.getConstantExprICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001172 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001173 return pred;
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001174 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Owen Anderson53a52212009-07-13 04:09:18 +00001175 R = dyn_cast<ConstantInt>(Context.getConstantExprICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001176 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001177 return pred;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001178
1179 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +00001180 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001181 }
1182
Chris Lattner061da2f2004-01-13 05:51:55 +00001183 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +00001184 ICmpInst::Predicate SwappedRelation =
Owen Anderson53a52212009-07-13 04:09:18 +00001185 evaluateICmpRelation(Context, V2, V1, isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001186 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1187 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001188
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001189 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001190 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +00001191 ICmpInst::Predicate SwappedRelation =
Owen Anderson53a52212009-07-13 04:09:18 +00001192 evaluateICmpRelation(Context, V2, V1, isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001193 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1194 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001195 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001196 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +00001197 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001198
Reid Spenceraccd7c72004-07-17 23:47:01 +00001199 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001200 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001201 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner52fe8692007-09-10 23:42:42 +00001202 // Don't try to decide equality of aliases.
1203 if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2))
1204 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
1205 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001206 } else {
1207 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Chris Lattner52fe8692007-09-10 23:42:42 +00001208 // GlobalVals can never be null. Don't try to evaluate aliases.
1209 if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1))
Reid Spencer266e42b2006-12-23 06:05:41 +00001210 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001211 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001212 } else {
1213 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1214 // constantexpr, a CPR, or a simple constant.
Reid Spencer9d36acf2006-12-24 18:52:08 +00001215 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1216 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +00001217
1218 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001219 case Instruction::Trunc:
1220 case Instruction::FPTrunc:
1221 case Instruction::FPExt:
1222 case Instruction::FPToUI:
1223 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +00001224 break; // We can't evaluate floating point casts or truncations.
1225
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001226 case Instruction::UIToFP:
1227 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001228 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +00001229 case Instruction::ZExt:
1230 case Instruction::SExt:
Chris Lattner061da2f2004-01-13 05:51:55 +00001231 // If the cast is not actually changing bits, and the second operand is a
1232 // null pointer, do the comparison with the pre-casted value.
1233 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +00001234 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Chris Lattnerd2265b42007-12-10 22:53:04 +00001235 bool sgnd = isSigned;
1236 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1237 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
Owen Anderson53a52212009-07-13 04:09:18 +00001238 return evaluateICmpRelation(Context, CE1Op0,
1239 Context.getNullValue(CE1Op0->getType()),
Chris Lattnerd2265b42007-12-10 22:53:04 +00001240 sgnd);
Reid Spencer266e42b2006-12-23 06:05:41 +00001241 }
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001242
1243 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1244 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencer266e42b2006-12-23 06:05:41 +00001245 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001246 // which happens a lot in compilers with tagged integers.
Reid Spencer9d36acf2006-12-24 18:52:08 +00001247 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer266e42b2006-12-23 06:05:41 +00001248 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001249 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
Chris Lattner03c49532007-01-15 02:27:26 +00001250 CE1->getOperand(0)->getType()->isInteger()) {
Chris Lattnerd2265b42007-12-10 22:53:04 +00001251 bool sgnd = isSigned;
1252 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1253 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
Owen Anderson53a52212009-07-13 04:09:18 +00001254 return evaluateICmpRelation(Context, CE1->getOperand(0),
1255 CE2->getOperand(0), sgnd);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001256 }
Chris Lattner192e3262004-04-11 01:29:30 +00001257 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001258
1259 case Instruction::GetElementPtr:
1260 // Ok, since this is a getelementptr, we know that the constant has a
1261 // pointer type. Check the various cases.
1262 if (isa<ConstantPointerNull>(V2)) {
1263 // If we are comparing a GEP to a null pointer, check to see if the base
1264 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +00001265 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001266 if (GV->hasExternalWeakLinkage())
1267 // Weak linkage GVals could be zero or not. We're comparing that
1268 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001269 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +00001270 else
1271 // If its not weak linkage, the GVal must have a non-zero address
1272 // so the result is greater-than
Reid Spencer266e42b2006-12-23 06:05:41 +00001273 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001274 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1275 // If we are indexing from a null pointer, check to see if we have any
1276 // non-zero indices.
1277 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1278 if (!CE1->getOperand(i)->isNullValue())
1279 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001280 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001281 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +00001282 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001283 }
1284 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001285 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001286 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001287 if (CPR2->hasExternalWeakLinkage())
1288 // Weak linkage GVals could be zero or not. We're comparing it to
1289 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001290 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +00001291 else
1292 // If its not weak linkage, the GVal must have a non-zero address
1293 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +00001294 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001295 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001296 if (CPR1 == CPR2) {
1297 // If this is a getelementptr of the same global, then it must be
1298 // different. Because the types must match, the getelementptr could
1299 // only have at most one index, and because we fold getelementptr's
1300 // with a single zero index, it must be nonzero.
1301 assert(CE1->getNumOperands() == 2 &&
1302 !CE1->getOperand(1)->isNullValue() &&
1303 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001304 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001305 } else {
1306 // If they are different globals, we don't know what the value is,
1307 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001308 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001309 }
1310 }
1311 } else {
1312 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1313 const Constant *CE2Op0 = CE2->getOperand(0);
1314
1315 // There are MANY other foldings that we could perform here. They will
1316 // probably be added on demand, as they seem needed.
1317 switch (CE2->getOpcode()) {
1318 default: break;
1319 case Instruction::GetElementPtr:
1320 // By far the most common case to handle is when the base pointers are
1321 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001322 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001323 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001324 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001325 // Ok, we know that both getelementptr instructions are based on the
1326 // same global. From this, we can precisely determine the relative
1327 // ordering of the resultant pointers.
1328 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001329
Chris Lattner061da2f2004-01-13 05:51:55 +00001330 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001331 gep_type_iterator GTI = gep_type_begin(CE1);
1332 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1333 ++i, ++GTI)
Owen Anderson53a52212009-07-13 04:09:18 +00001334 switch (IdxCompare(Context, CE1->getOperand(i),
1335 CE2->getOperand(i), GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001336 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1337 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1338 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001339 }
1340
1341 // Ok, we ran out of things they have in common. If any leftovers
1342 // are non-zero then we have a difference, otherwise we are equal.
1343 for (; i < CE1->getNumOperands(); ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001344 if (!CE1->getOperand(i)->isNullValue()) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001345 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001346 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +00001347 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001348 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001349 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001350
Chris Lattner061da2f2004-01-13 05:51:55 +00001351 for (; i < CE2->getNumOperands(); ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001352 if (!CE2->getOperand(i)->isNullValue()) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001353 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001354 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001355 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001356 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001357 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001358 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001359 }
1360 }
1361 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001362 default:
1363 break;
1364 }
1365 }
1366
Reid Spencer266e42b2006-12-23 06:05:41 +00001367 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001368}
1369
Owen Anderson53a52212009-07-13 04:09:18 +00001370Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context,
1371 unsigned short pred,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001372 const Constant *C1,
1373 const Constant *C2) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001374 const Type *ResultTy;
1375 if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
Owen Anderson53a52212009-07-13 04:09:18 +00001376 ResultTy = Context.getVectorType(Type::Int1Ty, VT->getNumElements());
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001377 else
1378 ResultTy = Type::Int1Ty;
1379
Chris Lattnerd137a082008-07-08 05:46:34 +00001380 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001381 if (pred == FCmpInst::FCMP_FALSE)
Owen Anderson53a52212009-07-13 04:09:18 +00001382 return Context.getNullValue(ResultTy);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001383
1384 if (pred == FCmpInst::FCMP_TRUE)
Owen Anderson53a52212009-07-13 04:09:18 +00001385 return Context.getAllOnesValue(ResultTy);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001386
Reid Spencer266e42b2006-12-23 06:05:41 +00001387 // Handle some degenerate cases first
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001388 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Owen Anderson53a52212009-07-13 04:09:18 +00001389 return Context.getUndef(ResultTy);
Reid Spencer266e42b2006-12-23 06:05:41 +00001390
Dale Johannesen19db0932007-10-14 01:56:47 +00001391 // No compile-time operations on this type yet.
1392 if (C1->getType() == Type::PPC_FP128Ty)
1393 return 0;
1394
Reid Spencer266e42b2006-12-23 06:05:41 +00001395 // icmp eq/ne(null,GV) -> false/true
1396 if (C1->isNullValue()) {
1397 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001398 // Don't try to evaluate aliases. External weak GV can be null.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001399 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001400 if (pred == ICmpInst::ICMP_EQ)
Owen Andersonc37bc692009-07-21 18:03:38 +00001401 return Context.getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001402 else if (pred == ICmpInst::ICMP_NE)
Owen Andersonc37bc692009-07-21 18:03:38 +00001403 return Context.getTrue();
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001404 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001405 // icmp eq/ne(GV,null) -> false/true
1406 } else if (C2->isNullValue()) {
1407 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001408 // Don't try to evaluate aliases. External weak GV can be null.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001409 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001410 if (pred == ICmpInst::ICMP_EQ)
Owen Andersonc37bc692009-07-21 18:03:38 +00001411 return Context.getFalse();
Reid Spencer9d36acf2006-12-24 18:52:08 +00001412 else if (pred == ICmpInst::ICMP_NE)
Owen Andersonc37bc692009-07-21 18:03:38 +00001413 return Context.getTrue();
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001414 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001415 }
1416
Chris Lattner344da522007-01-12 18:42:52 +00001417 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +00001418 APInt V1 = cast<ConstantInt>(C1)->getValue();
1419 APInt V2 = cast<ConstantInt>(C2)->getValue();
1420 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001421 default: llvm_unreachable("Invalid ICmp Predicate"); return 0;
Owen Anderson53a52212009-07-13 04:09:18 +00001422 case ICmpInst::ICMP_EQ:
Owen Andersonedb4a702009-07-24 23:12:02 +00001423 return ConstantInt::get(Type::Int1Ty, V1 == V2);
Owen Anderson53a52212009-07-13 04:09:18 +00001424 case ICmpInst::ICMP_NE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001425 return ConstantInt::get(Type::Int1Ty, V1 != V2);
Owen Anderson53a52212009-07-13 04:09:18 +00001426 case ICmpInst::ICMP_SLT:
Owen Andersonedb4a702009-07-24 23:12:02 +00001427 return ConstantInt::get(Type::Int1Ty, V1.slt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001428 case ICmpInst::ICMP_SGT:
Owen Andersonedb4a702009-07-24 23:12:02 +00001429 return ConstantInt::get(Type::Int1Ty, V1.sgt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001430 case ICmpInst::ICMP_SLE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001431 return ConstantInt::get(Type::Int1Ty, V1.sle(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001432 case ICmpInst::ICMP_SGE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001433 return ConstantInt::get(Type::Int1Ty, V1.sge(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001434 case ICmpInst::ICMP_ULT:
Owen Andersonedb4a702009-07-24 23:12:02 +00001435 return ConstantInt::get(Type::Int1Ty, V1.ult(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001436 case ICmpInst::ICMP_UGT:
Owen Andersonedb4a702009-07-24 23:12:02 +00001437 return ConstantInt::get(Type::Int1Ty, V1.ugt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001438 case ICmpInst::ICMP_ULE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001439 return ConstantInt::get(Type::Int1Ty, V1.ule(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001440 case ICmpInst::ICMP_UGE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001441 return ConstantInt::get(Type::Int1Ty, V1.uge(V2));
Chris Lattner061da2f2004-01-13 05:51:55 +00001442 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001443 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001444 APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
1445 APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
1446 APFloat::cmpResult R = C1V.compare(C2V);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001447 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001448 default: llvm_unreachable("Invalid FCmp Predicate"); return 0;
Owen Andersonc37bc692009-07-21 18:03:38 +00001449 case FCmpInst::FCMP_FALSE: return Context.getFalse();
1450 case FCmpInst::FCMP_TRUE: return Context.getTrue();
Reid Spencer266e42b2006-12-23 06:05:41 +00001451 case FCmpInst::FCMP_UNO:
Owen Andersonedb4a702009-07-24 23:12:02 +00001452 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered);
Reid Spencer74bd0362007-01-11 00:25:45 +00001453 case FCmpInst::FCMP_ORD:
Owen Andersonedb4a702009-07-24 23:12:02 +00001454 return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpUnordered);
Reid Spencer266e42b2006-12-23 06:05:41 +00001455 case FCmpInst::FCMP_UEQ:
Owen Andersonedb4a702009-07-24 23:12:02 +00001456 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001457 R==APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001458 case FCmpInst::FCMP_OEQ:
Owen Andersonedb4a702009-07-24 23:12:02 +00001459 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpEqual);
Reid Spencer74bd0362007-01-11 00:25:45 +00001460 case FCmpInst::FCMP_UNE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001461 return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001462 case FCmpInst::FCMP_ONE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001463 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001464 R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001465 case FCmpInst::FCMP_ULT:
Owen Andersonedb4a702009-07-24 23:12:02 +00001466 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001467 R==APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001468 case FCmpInst::FCMP_OLT:
Owen Andersonedb4a702009-07-24 23:12:02 +00001469 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan);
Reid Spencer266e42b2006-12-23 06:05:41 +00001470 case FCmpInst::FCMP_UGT:
Owen Andersonedb4a702009-07-24 23:12:02 +00001471 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001472 R==APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001473 case FCmpInst::FCMP_OGT:
Owen Andersonedb4a702009-07-24 23:12:02 +00001474 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001475 case FCmpInst::FCMP_ULE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001476 return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001477 case FCmpInst::FCMP_OLE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001478 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpLessThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001479 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001480 case FCmpInst::FCMP_UGE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001481 return ConstantInt::get(Type::Int1Ty, R!=APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001482 case FCmpInst::FCMP_OGE:
Owen Andersonedb4a702009-07-24 23:12:02 +00001483 return ConstantInt::get(Type::Int1Ty, R==APFloat::cmpGreaterThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001484 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001485 }
Chris Lattner67136cf2008-07-10 00:29:28 +00001486 } else if (isa<VectorType>(C1->getType())) {
1487 SmallVector<Constant*, 16> C1Elts, C2Elts;
Owen Anderson53a52212009-07-13 04:09:18 +00001488 C1->getVectorElements(Context, C1Elts);
1489 C2->getVectorElements(Context, C2Elts);
Chris Lattner67136cf2008-07-10 00:29:28 +00001490
1491 // If we can constant fold the comparison of each element, constant fold
1492 // the whole vector comparison.
1493 SmallVector<Constant*, 4> ResElts;
Chris Lattner67136cf2008-07-10 00:29:28 +00001494 for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) {
1495 // Compare the elements, producing an i1 result or constant expr.
Owen Anderson53a52212009-07-13 04:09:18 +00001496 ResElts.push_back(
1497 Context.getConstantExprCompare(pred, C1Elts[i], C2Elts[i]));
Reid Spencer266e42b2006-12-23 06:05:41 +00001498 }
Owen Anderson53a52212009-07-13 04:09:18 +00001499 return Context.getConstantVector(&ResElts[0], ResElts.size());
Reid Spencer266e42b2006-12-23 06:05:41 +00001500 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001501
Reid Spencer9d36acf2006-12-24 18:52:08 +00001502 if (C1->getType()->isFloatingPoint()) {
Chris Lattner350e4172008-07-08 18:47:38 +00001503 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
Owen Anderson53a52212009-07-13 04:09:18 +00001504 switch (evaluateFCmpRelation(Context, C1, C2)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001505 default: llvm_unreachable("Unknown relation!");
Reid Spencer9d36acf2006-12-24 18:52:08 +00001506 case FCmpInst::FCMP_UNO:
1507 case FCmpInst::FCMP_ORD:
1508 case FCmpInst::FCMP_UEQ:
1509 case FCmpInst::FCMP_UNE:
1510 case FCmpInst::FCMP_ULT:
1511 case FCmpInst::FCMP_UGT:
1512 case FCmpInst::FCMP_ULE:
1513 case FCmpInst::FCMP_UGE:
1514 case FCmpInst::FCMP_TRUE:
1515 case FCmpInst::FCMP_FALSE:
1516 case FCmpInst::BAD_FCMP_PREDICATE:
1517 break; // Couldn't determine anything about these constants.
1518 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001519 Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1520 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1521 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1522 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001523 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001524 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1525 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1526 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1527 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001528 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001529 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1530 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1531 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1532 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001533 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1534 // We can only partially decide this relation.
1535 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Chris Lattnerd137a082008-07-08 05:46:34 +00001536 Result = 0;
1537 else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
1538 Result = 1;
Chris Lattner061da2f2004-01-13 05:51:55 +00001539 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001540 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1541 // We can only partially decide this relation.
1542 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Chris Lattnerd137a082008-07-08 05:46:34 +00001543 Result = 0;
1544 else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
1545 Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001546 break;
1547 case ICmpInst::ICMP_NE: // We know that C1 != C2
1548 // We can only partially decide this relation.
1549 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Chris Lattnerd137a082008-07-08 05:46:34 +00001550 Result = 0;
1551 else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
1552 Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001553 break;
1554 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001555
1556 // If we evaluated the result, return it now.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001557 if (Result != -1)
Owen Andersonedb4a702009-07-24 23:12:02 +00001558 return ConstantInt::get(Type::Int1Ty, Result);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001559
Reid Spencer9d36acf2006-12-24 18:52:08 +00001560 } else {
1561 // Evaluate the relation between the two constants, per the predicate.
Chris Lattnerd137a082008-07-08 05:46:34 +00001562 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
Owen Anderson53a52212009-07-13 04:09:18 +00001563 switch (evaluateICmpRelation(Context, C1, C2, CmpInst::isSigned(pred))) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001564 default: llvm_unreachable("Unknown relational!");
Reid Spencer9d36acf2006-12-24 18:52:08 +00001565 case ICmpInst::BAD_ICMP_PREDICATE:
1566 break; // Couldn't determine anything about these constants.
1567 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1568 // If we know the constants are equal, we can decide the result of this
1569 // computation precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001570 Result = (pred == ICmpInst::ICMP_EQ ||
1571 pred == ICmpInst::ICMP_ULE ||
1572 pred == ICmpInst::ICMP_SLE ||
1573 pred == ICmpInst::ICMP_UGE ||
1574 pred == ICmpInst::ICMP_SGE);
1575 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001576 case ICmpInst::ICMP_ULT:
1577 // If we know that C1 < C2, we can decide the result of this computation
1578 // precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001579 Result = (pred == ICmpInst::ICMP_ULT ||
1580 pred == ICmpInst::ICMP_NE ||
1581 pred == ICmpInst::ICMP_ULE);
1582 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001583 case ICmpInst::ICMP_SLT:
1584 // If we know that C1 < C2, we can decide the result of this computation
1585 // precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001586 Result = (pred == ICmpInst::ICMP_SLT ||
1587 pred == ICmpInst::ICMP_NE ||
1588 pred == ICmpInst::ICMP_SLE);
1589 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001590 case ICmpInst::ICMP_UGT:
1591 // If we know that C1 > C2, we can decide the result of this computation
1592 // precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001593 Result = (pred == ICmpInst::ICMP_UGT ||
1594 pred == ICmpInst::ICMP_NE ||
1595 pred == ICmpInst::ICMP_UGE);
1596 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001597 case ICmpInst::ICMP_SGT:
1598 // If we know that C1 > C2, we can decide the result of this computation
1599 // precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001600 Result = (pred == ICmpInst::ICMP_SGT ||
1601 pred == ICmpInst::ICMP_NE ||
1602 pred == ICmpInst::ICMP_SGE);
1603 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001604 case ICmpInst::ICMP_ULE:
1605 // If we know that C1 <= C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001606 if (pred == ICmpInst::ICMP_UGT) Result = 0;
1607 if (pred == ICmpInst::ICMP_ULT) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001608 break;
1609 case ICmpInst::ICMP_SLE:
1610 // If we know that C1 <= C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001611 if (pred == ICmpInst::ICMP_SGT) Result = 0;
1612 if (pred == ICmpInst::ICMP_SLT) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001613 break;
1614
1615 case ICmpInst::ICMP_UGE:
1616 // If we know that C1 >= C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001617 if (pred == ICmpInst::ICMP_ULT) Result = 0;
1618 if (pred == ICmpInst::ICMP_UGT) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001619 break;
1620 case ICmpInst::ICMP_SGE:
1621 // If we know that C1 >= C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001622 if (pred == ICmpInst::ICMP_SLT) Result = 0;
1623 if (pred == ICmpInst::ICMP_SGT) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001624 break;
1625
1626 case ICmpInst::ICMP_NE:
1627 // If we know that C1 != C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001628 if (pred == ICmpInst::ICMP_EQ) Result = 0;
1629 if (pred == ICmpInst::ICMP_NE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001630 break;
1631 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001632
1633 // If we evaluated the result, return it now.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001634 if (Result != -1)
Owen Andersonedb4a702009-07-24 23:12:02 +00001635 return ConstantInt::get(Type::Int1Ty, Result);
Chris Lattnerd137a082008-07-08 05:46:34 +00001636
Reid Spencer9d36acf2006-12-24 18:52:08 +00001637 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001638 // If C2 is a constant expr and C1 isn't, flip them around and fold the
Reid Spencer9d36acf2006-12-24 18:52:08 +00001639 // other way if possible.
1640 switch (pred) {
1641 case ICmpInst::ICMP_EQ:
1642 case ICmpInst::ICMP_NE:
1643 // No change of predicate required.
Owen Anderson53a52212009-07-13 04:09:18 +00001644 return ConstantFoldCompareInstruction(Context, pred, C2, C1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001645
1646 case ICmpInst::ICMP_ULT:
1647 case ICmpInst::ICMP_SLT:
1648 case ICmpInst::ICMP_UGT:
1649 case ICmpInst::ICMP_SGT:
1650 case ICmpInst::ICMP_ULE:
1651 case ICmpInst::ICMP_SLE:
1652 case ICmpInst::ICMP_UGE:
1653 case ICmpInst::ICMP_SGE:
1654 // Change the predicate as necessary to swap the operands.
1655 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
Owen Anderson53a52212009-07-13 04:09:18 +00001656 return ConstantFoldCompareInstruction(Context, pred, C2, C1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001657
1658 default: // These predicates cannot be flopped around.
1659 break;
1660 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001661 }
1662 }
1663 return 0;
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001664 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001665
Owen Anderson53a52212009-07-13 04:09:18 +00001666Constant *llvm::ConstantFoldGetElementPtr(LLVMContext &Context,
1667 const Constant *C,
David Greenec656cbb2007-09-04 15:46:09 +00001668 Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001669 unsigned NumIdx) {
Chris Lattner302116a2007-01-31 04:40:28 +00001670 if (NumIdx == 0 ||
1671 (NumIdx == 1 && Idxs[0]->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001672 return const_cast<Constant*>(C);
1673
Chris Lattnerf6013752004-10-17 21:54:55 +00001674 if (isa<UndefValue>(C)) {
Christopher Lambedf07882007-12-17 01:12:55 +00001675 const PointerType *Ptr = cast<PointerType>(C->getType());
1676 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
David Greenec656cbb2007-09-04 15:46:09 +00001677 (Value **)Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001678 (Value **)Idxs+NumIdx);
Chris Lattnerf6013752004-10-17 21:54:55 +00001679 assert(Ty != 0 && "Invalid indices for GEP!");
Owen Anderson53a52212009-07-13 04:09:18 +00001680 return Context.getUndef(Context.getPointerType(Ty, Ptr->getAddressSpace()));
Chris Lattnerf6013752004-10-17 21:54:55 +00001681 }
1682
Chris Lattner302116a2007-01-31 04:40:28 +00001683 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001684 if (C->isNullValue()) {
1685 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001686 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1687 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001688 isNull = false;
1689 break;
1690 }
1691 if (isNull) {
Christopher Lambedf07882007-12-17 01:12:55 +00001692 const PointerType *Ptr = cast<PointerType>(C->getType());
1693 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
David Greenec656cbb2007-09-04 15:46:09 +00001694 (Value**)Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001695 (Value**)Idxs+NumIdx);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001696 assert(Ty != 0 && "Invalid indices for GEP!");
Owen Anderson53a52212009-07-13 04:09:18 +00001697 return Context.getConstantPointerNull(
1698 Context.getPointerType(Ty,Ptr->getAddressSpace()));
Chris Lattner04b60fe2004-02-16 20:46:13 +00001699 }
1700 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001701
1702 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1703 // Combine Indices - If the source pointer to this getelementptr instruction
1704 // is a getelementptr instruction, combine the indices of the two
1705 // getelementptr instructions into a single instruction.
1706 //
1707 if (CE->getOpcode() == Instruction::GetElementPtr) {
1708 const Type *LastTy = 0;
1709 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1710 I != E; ++I)
1711 LastTy = *I;
1712
Chris Lattner13128ab2004-10-11 22:52:25 +00001713 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001714 SmallVector<Value*, 16> NewIndices;
1715 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001716 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001717 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001718
1719 // Add the last index of the source with the first index of the new GEP.
1720 // Make sure to handle the case when they are actually different types.
1721 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001722 // Otherwise it must be an array.
1723 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001724 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001725 if (IdxTy != Idx0->getType()) {
Owen Anderson53a52212009-07-13 04:09:18 +00001726 Constant *C1 =
1727 Context.getConstantExprSExtOrBitCast(Idx0, Type::Int64Ty);
1728 Constant *C2 = Context.getConstantExprSExtOrBitCast(Combined,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001729 Type::Int64Ty);
Owen Anderson53a52212009-07-13 04:09:18 +00001730 Combined = Context.getConstantExpr(Instruction::Add, C1, C2);
Reid Spencer1a063892006-12-04 02:46:44 +00001731 } else {
1732 Combined =
Owen Anderson53a52212009-07-13 04:09:18 +00001733 Context.getConstantExpr(Instruction::Add, Idx0, Combined);
Reid Spencer1a063892006-12-04 02:46:44 +00001734 }
Chris Lattner71068a02004-07-07 04:45:13 +00001735 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001736
Chris Lattner1dd054c2004-01-12 22:07:24 +00001737 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001738 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
Owen Anderson53a52212009-07-13 04:09:18 +00001739 return Context.getConstantExprGetElementPtr(CE->getOperand(0),
1740 &NewIndices[0],
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001741 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001742 }
1743 }
1744
1745 // Implement folding of:
1746 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1747 // long 0, long 0)
1748 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1749 //
Chris Lattneraadc7782007-08-13 17:09:08 +00001750 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001751 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001752 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1753 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1754 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001755 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001756 if (CAT->getElementType() == SAT->getElementType())
Owen Anderson53a52212009-07-13 04:09:18 +00001757 return Context.getConstantExprGetElementPtr(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001758 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattneraadc7782007-08-13 17:09:08 +00001759 }
1760
1761 // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1)
1762 // Into: inttoptr (i64 0 to i8*)
1763 // This happens with pointers to member functions in C++.
1764 if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 &&
1765 isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) &&
1766 cast<PointerType>(CE->getType())->getElementType() == Type::Int8Ty) {
1767 Constant *Base = CE->getOperand(0);
1768 Constant *Offset = Idxs[0];
1769
1770 // Convert the smaller integer to the larger type.
1771 if (Offset->getType()->getPrimitiveSizeInBits() <
1772 Base->getType()->getPrimitiveSizeInBits())
Owen Anderson53a52212009-07-13 04:09:18 +00001773 Offset = Context.getConstantExprSExt(Offset, Base->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001774 else if (Base->getType()->getPrimitiveSizeInBits() <
1775 Offset->getType()->getPrimitiveSizeInBits())
Owen Anderson53a52212009-07-13 04:09:18 +00001776 Base = Context.getConstantExprZExt(Base, Offset->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001777
Owen Anderson53a52212009-07-13 04:09:18 +00001778 Base = Context.getConstantExprAdd(Base, Offset);
1779 return Context.getConstantExprIntToPtr(Base, CE->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001780 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001781 }
1782 return 0;
1783}
1784