blob: bfa7832072a61ae29a91167e751ab5a115335d3b [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
Dan Gohman21c62162009-09-11 00:04:14 +000015// pieces that don't need TargetData, and the pieces that do. This is to avoid
16// a dependence in VMCore on Target.
Chris Lattner1dd054c2004-01-12 22:07:24 +000017//
Chris Lattner2f7c9632001-06-06 20:29:01 +000018//===----------------------------------------------------------------------===//
19
Chris Lattner33e93b82007-02-27 03:05:06 +000020#include "ConstantFold.h"
Chris Lattner6ff6cea2004-01-12 21:02:29 +000021#include "llvm/Constants.h"
Chris Lattnera9eddae2004-02-22 06:25:38 +000022#include "llvm/Instructions.h"
Chris Lattner1f0049c2003-04-17 19:24:18 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerea0789c2004-03-08 06:17:35 +000024#include "llvm/Function.h"
Chris Lattner52fe8692007-09-10 23:42:42 +000025#include "llvm/GlobalAlias.h"
Dan Gohman21c62162009-09-11 00:04:14 +000026#include "llvm/GlobalVariable.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;
Dan Gohman062d3782009-08-29 23:35:16 +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 Anderson487375e2009-07-29 18:55:55 +000064 Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i),
Owen Anderson53a52212009-07-13 04:09:18 +000065 DstEltTy));
Owen Anderson4aa32952009-07-28 21:19:26 +000066 return ConstantVector::get(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
Nick Lewyckye0332982009-09-20 01:35:59 +000076 ConstantExpr *Op, ///< the first cast constant expression
Reid Spencer6c38f0b2006-11-27 01:05:10 +000077 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");
Dan Gohman062d3782009-08-29 23:35:16 +000082
Reid Spencer6c38f0b2006-11-27 01:05:10 +000083 // 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,
Owen Anderson55f1c092009-08-13 21:58:54 +000091 Type::getInt64Ty(DstTy->getContext()));
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
Dan Gohman062d3782009-08-29 23:35:16 +000099
Chris Lattnere8ea0372007-12-11 05:55:02 +0000100 // 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 Anderson55f1c092009-08-13 21:58:54 +0000106 Value *Zero = Constant::getNullValue(Type::getInt32Ty(Context));
Dan Gohman76733742009-08-12 00:32:55 +0000107 IdxList.push_back(Zero);
Nate Begemanf2b0b0e2008-03-31 00:22:16 +0000108 const Type *ElTy = PTy->getElementType();
109 while (ElTy != DPTy->getElementType()) {
110 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
111 if (STy->getNumElements() == 0) break;
112 ElTy = STy->getElementType(0);
Dan Gohman76733742009-08-12 00:32:55 +0000113 IdxList.push_back(Zero);
Nate Begemanf2b0b0e2008-03-31 00:22:16 +0000114 } else if (const SequentialType *STy =
115 dyn_cast<SequentialType>(ElTy)) {
116 if (isa<PointerType>(ElTy)) break; // Can't index into pointers!
117 ElTy = STy->getElementType();
Dan Gohman76733742009-08-12 00:32:55 +0000118 IdxList.push_back(Zero);
Nate Begemanf2b0b0e2008-03-31 00:22:16 +0000119 } else {
120 break;
121 }
Chris Lattnere8ea0372007-12-11 05:55:02 +0000122 }
Dan Gohman062d3782009-08-29 23:35:16 +0000123
Nate Begemanf2b0b0e2008-03-31 00:22:16 +0000124 if (ElTy == DPTy->getElementType())
Dan Gohmane4ca02d2009-09-03 23:34:49 +0000125 // This GEP is inbounds because all indices are zero.
126 return ConstantExpr::getInBoundsGetElementPtr(V, &IdxList[0],
127 IdxList.size());
Chris Lattnere8ea0372007-12-11 05:55:02 +0000128 }
Dan Gohman062d3782009-08-29 23:35:16 +0000129
Chris Lattnere8ea0372007-12-11 05:55:02 +0000130 // Handle casts from one vector constant to another. We know that the src
131 // and dest type have the same size (otherwise its an illegal cast).
132 if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
133 if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
134 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
135 "Not cast between same sized vectors!");
Devang Pateld26344d2008-11-03 23:20:04 +0000136 SrcTy = NULL;
Chris Lattnere8ea0372007-12-11 05:55:02 +0000137 // First, check for null. Undef is already handled.
138 if (isa<ConstantAggregateZero>(V))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000139 return Constant::getNullValue(DestTy);
Dan Gohman062d3782009-08-29 23:35:16 +0000140
Chris Lattner5c6399e2007-12-11 06:07:39 +0000141 if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
Owen Anderson53a52212009-07-13 04:09:18 +0000142 return BitCastConstantVector(Context, CV, DestPTy);
Chris Lattnere8ea0372007-12-11 05:55:02 +0000143 }
Chris Lattner1baace02008-10-16 05:26:51 +0000144
145 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
146 // This allows for other simplifications (although some of them
147 // can only be handled by Analysis/ConstantFolding.cpp).
148 if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
Owen Anderson487375e2009-07-29 18:55:55 +0000149 return ConstantExpr::getBitCast(
Owen Anderson4aa32952009-07-28 21:19:26 +0000150 ConstantVector::get(&V, 1), DestPTy);
Chris Lattnere8ea0372007-12-11 05:55:02 +0000151 }
Dan Gohman062d3782009-08-29 23:35:16 +0000152
Chris Lattnere8ea0372007-12-11 05:55:02 +0000153 // Finally, implement bitcast folding now. The code below doesn't handle
154 // bitcast right.
155 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
Owen Andersonb292b8c2009-07-30 23:03:37 +0000156 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Dan Gohman062d3782009-08-29 23:35:16 +0000157
Chris Lattnere8ea0372007-12-11 05:55:02 +0000158 // Handle integral constant input.
Nick Lewyckye0332982009-09-20 01:35:59 +0000159 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Chris Lattnere8ea0372007-12-11 05:55:02 +0000160 if (DestTy->isInteger())
161 // Integral -> Integral. This is a no-op because the bit widths must
162 // be the same. Consequently, we just fold to V.
163 return V;
Duncan Sands1ea11732009-02-04 10:17:14 +0000164
165 if (DestTy->isFloatingPoint())
Owen Anderson69c464d2009-07-27 20:59:43 +0000166 return ConstantFP::get(Context, APFloat(CI->getValue(),
Owen Anderson55f1c092009-08-13 21:58:54 +0000167 DestTy != Type::getPPC_FP128Ty(Context)));
Duncan Sands1ea11732009-02-04 10:17:14 +0000168
Chris Lattnere8ea0372007-12-11 05:55:02 +0000169 // Otherwise, can't fold this (vector?)
170 return 0;
171 }
Duncan Sandse7d54792009-02-04 11:17:06 +0000172
Chris Lattnere8ea0372007-12-11 05:55:02 +0000173 // Handle ConstantFP input.
Nick Lewyckye0332982009-09-20 01:35:59 +0000174 if (ConstantFP *FP = dyn_cast<ConstantFP>(V))
Chris Lattnere8ea0372007-12-11 05:55:02 +0000175 // FP -> Integral.
Owen Andersonedb4a702009-07-24 23:12:02 +0000176 return ConstantInt::get(Context, FP->getValueAPF().bitcastToAPInt());
Duncan Sandse7d54792009-02-04 11:17:06 +0000177
Chris Lattnere8ea0372007-12-11 05:55:02 +0000178 return 0;
179}
180
181
Owen Anderson53a52212009-07-13 04:09:18 +0000182Constant *llvm::ConstantFoldCastInstruction(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +0000183 unsigned opc, Constant *V,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000184 const Type *DestTy) {
Chris Lattner363485d2007-07-20 22:09:02 +0000185 if (isa<UndefValue>(V)) {
186 // zext(undef) = 0, because the top bits will be zero.
187 // sext(undef) = 0, because the top bits will all be the same.
Chris Lattnerb4c6cc92008-02-19 06:22:12 +0000188 // [us]itofp(undef) = 0, because the result value is bounded.
189 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
190 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
Owen Anderson5a1acd92009-07-31 20:28:14 +0000191 return Constant::getNullValue(DestTy);
Owen Andersonb292b8c2009-07-30 23:03:37 +0000192 return UndefValue::get(DestTy);
Chris Lattner363485d2007-07-20 22:09:02 +0000193 }
Dale Johannesen19db0932007-10-14 01:56:47 +0000194 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +0000195 if (V->getType() == Type::getPPC_FP128Ty(Context) || DestTy == Type::getPPC_FP128Ty(Context))
Dale Johannesen19db0932007-10-14 01:56:47 +0000196 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000197
198 // If the cast operand is a constant expression, there's a few things we can
199 // do to try to simplify it.
Nick Lewyckye0332982009-09-20 01:35:59 +0000200 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000201 if (CE->isCast()) {
Reid Spencer1a063892006-12-04 02:46:44 +0000202 // Try hard to fold cast of cast because they are often eliminable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000203 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
Owen Anderson487375e2009-07-29 18:55:55 +0000204 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000205 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
206 // If all of the indexes in the GEP are null values, there is no pointer
207 // adjustment going on. We might as well cast the source pointer.
208 bool isAllNull = true;
209 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
210 if (!CE->getOperand(i)->isNullValue()) {
211 isAllNull = false;
212 break;
213 }
214 if (isAllNull)
Reid Spencer1a063892006-12-04 02:46:44 +0000215 // This is casting one pointer type to another, always BitCast
Owen Anderson487375e2009-07-29 18:55:55 +0000216 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
Chris Lattner1dd054c2004-01-12 22:07:24 +0000217 }
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000218 }
Chris Lattner1dd054c2004-01-12 22:07:24 +0000219
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000220 // If the cast operand is a constant vector, perform the cast by
221 // operating on each element. In the cast of bitcasts, the element
222 // count may be mismatched; don't attempt to handle that here.
Nick Lewyckye0332982009-09-20 01:35:59 +0000223 if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000224 if (isa<VectorType>(DestTy) &&
225 cast<VectorType>(DestTy)->getNumElements() ==
226 CV->getType()->getNumElements()) {
227 std::vector<Constant*> res;
228 const VectorType *DestVecTy = cast<VectorType>(DestTy);
229 const Type *DstEltTy = DestVecTy->getElementType();
230 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
Owen Anderson487375e2009-07-29 18:55:55 +0000231 res.push_back(ConstantExpr::getCast(opc,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000232 CV->getOperand(i), DstEltTy));
Owen Anderson4aa32952009-07-28 21:19:26 +0000233 return ConstantVector::get(DestVecTy, res);
Dan Gohman7ccc52f2009-06-15 22:12:54 +0000234 }
235
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000236 // We actually have to do a cast now. Perform the cast according to the
237 // opcode specified.
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000238 switch (opc) {
239 case Instruction::FPTrunc:
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000240 case Instruction::FPExt:
Nick Lewyckye0332982009-09-20 01:35:59 +0000241 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000242 bool ignored;
Dale Johannesenf4bad972007-09-19 14:22:58 +0000243 APFloat Val = FPC->getValueAPF();
Owen Anderson55f1c092009-08-13 21:58:54 +0000244 Val.convert(DestTy == Type::getFloatTy(Context) ? APFloat::IEEEsingle :
245 DestTy == Type::getDoubleTy(Context) ? APFloat::IEEEdouble :
246 DestTy == Type::getX86_FP80Ty(Context) ? APFloat::x87DoubleExtended :
247 DestTy == Type::getFP128Ty(Context) ? APFloat::IEEEquad :
Dale Johannesenf4bad972007-09-19 14:22:58 +0000248 APFloat::Bogus,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000249 APFloat::rmNearestTiesToEven, &ignored);
Owen Anderson69c464d2009-07-27 20:59:43 +0000250 return ConstantFP::get(Context, Val);
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000251 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000252 return 0; // Can't fold.
253 case Instruction::FPToUI:
Reid Spencer8dabca42006-12-19 07:41:40 +0000254 case Instruction::FPToSI:
Nick Lewyckye0332982009-09-20 01:35:59 +0000255 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
Chris Lattner2b827fd2007-10-15 05:34:10 +0000256 const APFloat &V = FPC->getValueAPF();
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000257 bool ignored;
Dale Johannesenf4bad972007-09-19 14:22:58 +0000258 uint64_t x[2];
Reid Spencer81658a82007-02-27 06:23:51 +0000259 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
Dale Johannesen17663f42007-09-25 23:32:20 +0000260 (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000261 APFloat::rmTowardZero, &ignored);
Dale Johannesenf4bad972007-09-19 14:22:58 +0000262 APInt Val(DestBitWidth, 2, x);
Owen Andersonedb4a702009-07-24 23:12:02 +0000263 return ConstantInt::get(Context, Val);
Reid Spencer81658a82007-02-27 06:23:51 +0000264 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000265 return 0; // Can't fold.
266 case Instruction::IntToPtr: //always treated as unsigned
267 if (V->isNullValue()) // Is it an integral null value?
Owen Andersonb292b8c2009-07-30 23:03:37 +0000268 return ConstantPointerNull::get(cast<PointerType>(DestTy));
Reid Spencer8dabca42006-12-19 07:41:40 +0000269 return 0; // Other pointer types cannot be casted
270 case Instruction::PtrToInt: // always treated as unsigned
271 if (V->isNullValue()) // is it a null pointer value?
Owen Andersonedb4a702009-07-24 23:12:02 +0000272 return ConstantInt::get(DestTy, 0);
Reid Spencer8dabca42006-12-19 07:41:40 +0000273 return 0; // Other pointer types cannot be casted
274 case Instruction::UIToFP:
Reid Spencer8dabca42006-12-19 07:41:40 +0000275 case Instruction::SIToFP:
Nick Lewyckye0332982009-09-20 01:35:59 +0000276 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Dale Johannesen91506522007-09-30 18:19:03 +0000277 APInt api = CI->getValue();
278 const uint64_t zero[] = {0, 0};
Dale Johannesen91506522007-09-30 18:19:03 +0000279 APFloat apf = APFloat(APInt(DestTy->getPrimitiveSizeInBits(),
280 2, zero));
Dan Gohman06c45d52008-02-29 01:42:52 +0000281 (void)apf.convertFromAPInt(api,
282 opc==Instruction::SIToFP,
283 APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000284 return ConstantFP::get(Context, apf);
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000285 }
Reid Spencer8dabca42006-12-19 07:41:40 +0000286 return 0;
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000287 case Instruction::ZExt:
Nick Lewyckye0332982009-09-20 01:35:59 +0000288 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000289 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
290 APInt Result(CI->getValue());
291 Result.zext(BitWidth);
Owen Andersonedb4a702009-07-24 23:12:02 +0000292 return ConstantInt::get(Context, Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000293 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000294 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000295 case Instruction::SExt:
Nick Lewyckye0332982009-09-20 01:35:59 +0000296 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000297 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
298 APInt Result(CI->getValue());
299 Result.sext(BitWidth);
Owen Andersonedb4a702009-07-24 23:12:02 +0000300 return ConstantInt::get(Context, Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000301 }
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000302 return 0;
Chris Lattner710ebaf2006-12-01 19:22:41 +0000303 case Instruction::Trunc:
Nick Lewyckye0332982009-09-20 01:35:59 +0000304 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000305 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
306 APInt Result(CI->getValue());
307 Result.trunc(BitWidth);
Owen Andersonedb4a702009-07-24 23:12:02 +0000308 return ConstantInt::get(Context, Result);
Reid Spencer81658a82007-02-27 06:23:51 +0000309 }
Chris Lattner710ebaf2006-12-01 19:22:41 +0000310 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000311 case Instruction::BitCast:
Nick Lewyckye0332982009-09-20 01:35:59 +0000312 return FoldBitCast(Context, V, DestTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000313 default:
314 assert(!"Invalid CE CastInst opcode");
315 break;
Chris Lattner6b3f4752006-04-02 01:38:28 +0000316 }
Chris Lattnerb2b7f902004-10-11 03:57:30 +0000317
Torok Edwinfbcc6632009-07-14 16:55:14 +0000318 llvm_unreachable("Failed to cast constant expression");
Reid Spencerf5fc34a2006-12-19 03:15:47 +0000319 return 0;
Chris Lattner1dd054c2004-01-12 22:07:24 +0000320}
321
Owen Anderson53a52212009-07-13 04:09:18 +0000322Constant *llvm::ConstantFoldSelectInstruction(LLVMContext&,
Nick Lewyckye0332982009-09-20 01:35:59 +0000323 Constant *Cond,
324 Constant *V1, Constant *V2) {
325 if (ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
326 return CB->getZExtValue() ? V1 : V2;
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000327
Nick Lewyckye0332982009-09-20 01:35:59 +0000328 if (isa<UndefValue>(V1)) return V2;
329 if (isa<UndefValue>(V2)) return V1;
330 if (isa<UndefValue>(Cond)) return V1;
331 if (V1 == V2) return V1;
Chris Lattner6ea4b522004-03-12 05:53:32 +0000332 return 0;
333}
334
Owen Anderson53a52212009-07-13 04:09:18 +0000335Constant *llvm::ConstantFoldExtractElementInstruction(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +0000336 Constant *Val,
337 Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000338 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
Owen Andersonb292b8c2009-07-30 23:03:37 +0000339 return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000340 if (Val->isNullValue()) // ee(zero, x) -> zero
Owen Anderson5a1acd92009-07-31 20:28:14 +0000341 return Constant::getNullValue(
Reid Spencerd84d35b2007-02-15 02:26:10 +0000342 cast<VectorType>(Val->getType())->getElementType());
Dan Gohman062d3782009-08-29 23:35:16 +0000343
Nick Lewyckye0332982009-09-20 01:35:59 +0000344 if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
345 if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000346 return CVal->getOperand(CIdx->getZExtValue());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000347 } else if (isa<UndefValue>(Idx)) {
348 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
Gabor Greiff6caff662008-05-10 08:32:32 +0000349 return CVal->getOperand(0);
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000350 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000351 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000352 return 0;
353}
354
Owen Anderson53a52212009-07-13 04:09:18 +0000355Constant *llvm::ConstantFoldInsertElementInstruction(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +0000356 Constant *Val,
357 Constant *Elt,
358 Constant *Idx) {
359 ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000360 if (!CIdx) return 0;
Reid Spencer81658a82007-02-27 06:23:51 +0000361 APInt idxVal = CIdx->getValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000362 if (isa<UndefValue>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000363 // Insertion of scalar constant into vector undef
Robert Bocchinoca27f032006-01-17 20:07:22 +0000364 // Optimize away insertion of undef
365 if (isa<UndefValue>(Elt))
Nick Lewyckye0332982009-09-20 01:35:59 +0000366 return Val;
Robert Bocchinoca27f032006-01-17 20:07:22 +0000367 // Otherwise break the aggregate undef into multiple undefs and do
368 // the insertion
369 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000370 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000371 std::vector<Constant*> Ops;
372 Ops.reserve(numOps);
373 for (unsigned i = 0; i < numOps; ++i) {
Nick Lewyckye0332982009-09-20 01:35:59 +0000374 Constant *Op =
Owen Andersonb292b8c2009-07-30 23:03:37 +0000375 (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
Nick Lewyckye0332982009-09-20 01:35:59 +0000376 Ops.push_back(Op);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000377 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000378 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000379 }
Reid Spencer3054b142006-11-02 08:18:15 +0000380 if (isa<ConstantAggregateZero>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000381 // Insertion of scalar constant into vector aggregate zero
Robert Bocchinoca27f032006-01-17 20:07:22 +0000382 // Optimize away insertion of zero
383 if (Elt->isNullValue())
Nick Lewyckye0332982009-09-20 01:35:59 +0000384 return Val;
Robert Bocchinoca27f032006-01-17 20:07:22 +0000385 // Otherwise break the aggregate zero into multiple zeros and do
386 // the insertion
387 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000388 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000389 std::vector<Constant*> Ops;
390 Ops.reserve(numOps);
391 for (unsigned i = 0; i < numOps; ++i) {
Nick Lewyckye0332982009-09-20 01:35:59 +0000392 Constant *Op =
Owen Anderson5a1acd92009-07-31 20:28:14 +0000393 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
Nick Lewyckye0332982009-09-20 01:35:59 +0000394 Ops.push_back(Op);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000395 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000396 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000397 }
Nick Lewyckye0332982009-09-20 01:35:59 +0000398 if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000399 // Insertion of scalar constant into vector constant
Robert Bocchinoca27f032006-01-17 20:07:22 +0000400 std::vector<Constant*> Ops;
401 Ops.reserve(CVal->getNumOperands());
402 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
Nick Lewyckye0332982009-09-20 01:35:59 +0000403 Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000404 (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
Nick Lewyckye0332982009-09-20 01:35:59 +0000405 Ops.push_back(Op);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000406 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000407 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000408 }
Dan Gohman3db11c22008-06-03 00:15:20 +0000409
Robert Bocchinoca27f032006-01-17 20:07:22 +0000410 return 0;
411}
412
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000413/// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef
414/// return the specified element value. Otherwise return null.
Nick Lewyckye0332982009-09-20 01:35:59 +0000415static Constant *GetVectorElement(LLVMContext &Context, Constant *C,
Owen Anderson53a52212009-07-13 04:09:18 +0000416 unsigned EltNo) {
Nick Lewyckye0332982009-09-20 01:35:59 +0000417 if (ConstantVector *CV = dyn_cast<ConstantVector>(C))
Gabor Greiff6caff662008-05-10 08:32:32 +0000418 return CV->getOperand(EltNo);
Dan Gohman062d3782009-08-29 23:35:16 +0000419
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000420 const Type *EltTy = cast<VectorType>(C->getType())->getElementType();
421 if (isa<ConstantAggregateZero>(C))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000422 return Constant::getNullValue(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000423 if (isa<UndefValue>(C))
Owen Andersonb292b8c2009-07-30 23:03:37 +0000424 return UndefValue::get(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000425 return 0;
426}
427
Owen Anderson53a52212009-07-13 04:09:18 +0000428Constant *llvm::ConstantFoldShuffleVectorInstruction(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +0000429 Constant *V1,
430 Constant *V2,
431 Constant *Mask) {
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000432 // Undefined shuffle mask -> undefined value.
Owen Andersonb292b8c2009-07-30 23:03:37 +0000433 if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType());
Mon P Wang25f01062008-11-10 04:46:22 +0000434
435 unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements();
436 unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements();
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000437 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
Mon P Wang25f01062008-11-10 04:46:22 +0000438
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000439 // Loop over the shuffle mask, evaluating each element.
440 SmallVector<Constant*, 32> Result;
Mon P Wang25f01062008-11-10 04:46:22 +0000441 for (unsigned i = 0; i != MaskNumElts; ++i) {
Owen Anderson53a52212009-07-13 04:09:18 +0000442 Constant *InElt = GetVectorElement(Context, Mask, i);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000443 if (InElt == 0) return 0;
Mon P Wang25f01062008-11-10 04:46:22 +0000444
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000445 if (isa<UndefValue>(InElt))
Owen Andersonb292b8c2009-07-30 23:03:37 +0000446 InElt = UndefValue::get(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000447 else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) {
448 unsigned Elt = CI->getZExtValue();
Mon P Wang25f01062008-11-10 04:46:22 +0000449 if (Elt >= SrcNumElts*2)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000450 InElt = UndefValue::get(EltTy);
Mon P Wang25f01062008-11-10 04:46:22 +0000451 else if (Elt >= SrcNumElts)
Owen Anderson53a52212009-07-13 04:09:18 +0000452 InElt = GetVectorElement(Context, V2, Elt - SrcNumElts);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000453 else
Owen Anderson53a52212009-07-13 04:09:18 +0000454 InElt = GetVectorElement(Context, V1, Elt);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000455 if (InElt == 0) return 0;
456 } else {
457 // Unknown value.
458 return 0;
459 }
460 Result.push_back(InElt);
461 }
Mon P Wang25f01062008-11-10 04:46:22 +0000462
Owen Anderson4aa32952009-07-28 21:19:26 +0000463 return ConstantVector::get(&Result[0], Result.size());
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000464}
465
Owen Anderson53a52212009-07-13 04:09:18 +0000466Constant *llvm::ConstantFoldExtractValueInstruction(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +0000467 Constant *Agg,
Dan Gohman3db11c22008-06-03 00:15:20 +0000468 const unsigned *Idxs,
469 unsigned NumIdx) {
470 // Base case: no indices, so return the entire value.
471 if (NumIdx == 0)
Nick Lewyckye0332982009-09-20 01:35:59 +0000472 return Agg;
Dan Gohman3db11c22008-06-03 00:15:20 +0000473
474 if (isa<UndefValue>(Agg)) // ev(undef, x) -> undef
Owen Andersonb292b8c2009-07-30 23:03:37 +0000475 return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(),
Dan Gohman3db11c22008-06-03 00:15:20 +0000476 Idxs,
477 Idxs + NumIdx));
478
479 if (isa<ConstantAggregateZero>(Agg)) // ev(0, x) -> 0
480 return
Owen Anderson5a1acd92009-07-31 20:28:14 +0000481 Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
Dan Gohman3db11c22008-06-03 00:15:20 +0000482 Idxs,
483 Idxs + NumIdx));
484
485 // Otherwise recurse.
Owen Anderson53a52212009-07-13 04:09:18 +0000486 return ConstantFoldExtractValueInstruction(Context, Agg->getOperand(*Idxs),
Dan Gohman3db11c22008-06-03 00:15:20 +0000487 Idxs+1, NumIdx-1);
Dan Gohman12fce772008-05-15 19:50:34 +0000488}
489
Owen Anderson53a52212009-07-13 04:09:18 +0000490Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +0000491 Constant *Agg,
492 Constant *Val,
Dan Gohman3db11c22008-06-03 00:15:20 +0000493 const unsigned *Idxs,
494 unsigned NumIdx) {
495 // Base case: no indices, so replace the entire value.
496 if (NumIdx == 0)
Nick Lewyckye0332982009-09-20 01:35:59 +0000497 return Val;
Dan Gohman3db11c22008-06-03 00:15:20 +0000498
499 if (isa<UndefValue>(Agg)) {
500 // Insertion of constant into aggregate undef
Chris Lattneree8f74f2009-09-15 06:28:12 +0000501 // Optimize away insertion of undef.
Dan Gohman3db11c22008-06-03 00:15:20 +0000502 if (isa<UndefValue>(Val))
Nick Lewyckye0332982009-09-20 01:35:59 +0000503 return Agg;
Chris Lattneree8f74f2009-09-15 06:28:12 +0000504
Dan Gohman3db11c22008-06-03 00:15:20 +0000505 // Otherwise break the aggregate undef into multiple undefs and do
Chris Lattneree8f74f2009-09-15 06:28:12 +0000506 // the insertion.
Dan Gohman3db11c22008-06-03 00:15:20 +0000507 const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
508 unsigned numOps;
509 if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
510 numOps = AR->getNumElements();
511 else
512 numOps = cast<StructType>(AggTy)->getNumElements();
Chris Lattneree8f74f2009-09-15 06:28:12 +0000513
Dan Gohman3db11c22008-06-03 00:15:20 +0000514 std::vector<Constant*> Ops(numOps);
515 for (unsigned i = 0; i < numOps; ++i) {
516 const Type *MemberTy = AggTy->getTypeAtIndex(i);
Nick Lewyckye0332982009-09-20 01:35:59 +0000517 Constant *Op =
Dan Gohman3db11c22008-06-03 00:15:20 +0000518 (*Idxs == i) ?
Owen Andersonb292b8c2009-07-30 23:03:37 +0000519 ConstantFoldInsertValueInstruction(Context, UndefValue::get(MemberTy),
Dan Gohman3db11c22008-06-03 00:15:20 +0000520 Val, Idxs+1, NumIdx-1) :
Owen Andersonb292b8c2009-07-30 23:03:37 +0000521 UndefValue::get(MemberTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000522 Ops[i] = Op;
Dan Gohman3db11c22008-06-03 00:15:20 +0000523 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000524
525 if (const StructType* ST = dyn_cast<StructType>(AggTy))
526 return ConstantStruct::get(Context, Ops, ST->isPacked());
527 return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000528 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000529
Dan Gohman3db11c22008-06-03 00:15:20 +0000530 if (isa<ConstantAggregateZero>(Agg)) {
531 // Insertion of constant into aggregate zero
Chris Lattneree8f74f2009-09-15 06:28:12 +0000532 // Optimize away insertion of zero.
Dan Gohman3db11c22008-06-03 00:15:20 +0000533 if (Val->isNullValue())
Nick Lewyckye0332982009-09-20 01:35:59 +0000534 return Agg;
Chris Lattneree8f74f2009-09-15 06:28:12 +0000535
Dan Gohman3db11c22008-06-03 00:15:20 +0000536 // Otherwise break the aggregate zero into multiple zeros and do
Chris Lattneree8f74f2009-09-15 06:28:12 +0000537 // the insertion.
Dan Gohman3db11c22008-06-03 00:15:20 +0000538 const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
539 unsigned numOps;
540 if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
541 numOps = AR->getNumElements();
542 else
543 numOps = cast<StructType>(AggTy)->getNumElements();
Chris Lattneree8f74f2009-09-15 06:28:12 +0000544
Dan Gohman3db11c22008-06-03 00:15:20 +0000545 std::vector<Constant*> Ops(numOps);
546 for (unsigned i = 0; i < numOps; ++i) {
547 const Type *MemberTy = AggTy->getTypeAtIndex(i);
Nick Lewyckye0332982009-09-20 01:35:59 +0000548 Constant *Op =
Dan Gohman3db11c22008-06-03 00:15:20 +0000549 (*Idxs == i) ?
Owen Anderson53a52212009-07-13 04:09:18 +0000550 ConstantFoldInsertValueInstruction(Context,
Owen Anderson5a1acd92009-07-31 20:28:14 +0000551 Constant::getNullValue(MemberTy),
Dan Gohman3db11c22008-06-03 00:15:20 +0000552 Val, Idxs+1, NumIdx-1) :
Owen Anderson5a1acd92009-07-31 20:28:14 +0000553 Constant::getNullValue(MemberTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000554 Ops[i] = Op;
Dan Gohman3db11c22008-06-03 00:15:20 +0000555 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000556
557 if (const StructType* ST = dyn_cast<StructType>(AggTy))
558 return ConstantStruct::get(Context, Ops, ST->isPacked());
559 return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000560 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000561
Dan Gohman3db11c22008-06-03 00:15:20 +0000562 if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
Chris Lattneree8f74f2009-09-15 06:28:12 +0000563 // Insertion of constant into aggregate constant.
Dan Gohman3db11c22008-06-03 00:15:20 +0000564 std::vector<Constant*> Ops(Agg->getNumOperands());
565 for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
Nick Lewyckye0332982009-09-20 01:35:59 +0000566 Constant *Op =
Dan Gohman3db11c22008-06-03 00:15:20 +0000567 (*Idxs == i) ?
Owen Anderson53a52212009-07-13 04:09:18 +0000568 ConstantFoldInsertValueInstruction(Context, Agg->getOperand(i),
Dan Gohman3db11c22008-06-03 00:15:20 +0000569 Val, Idxs+1, NumIdx-1) :
570 Agg->getOperand(i);
Nick Lewyckye0332982009-09-20 01:35:59 +0000571 Ops[i] = Op;
Dan Gohman3db11c22008-06-03 00:15:20 +0000572 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000573
574 if (const StructType* ST = dyn_cast<StructType>(Agg->getType()))
575 return ConstantStruct::get(Context, Ops, ST->isPacked());
576 return ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000577 }
578
Dan Gohman12fce772008-05-15 19:50:34 +0000579 return 0;
580}
581
Reid Spencer266e42b2006-12-23 06:05:41 +0000582
Owen Anderson53a52212009-07-13 04:09:18 +0000583Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context,
584 unsigned Opcode,
Nick Lewyckye0332982009-09-20 01:35:59 +0000585 Constant *C1, Constant *C2) {
Dale Johannesene5facd52007-10-16 23:38:29 +0000586 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +0000587 if (C1->getType() == Type::getPPC_FP128Ty(Context))
Dale Johannesene5facd52007-10-16 23:38:29 +0000588 return 0;
589
Chris Lattneree8f74f2009-09-15 06:28:12 +0000590 // Handle UndefValue up front.
Reid Spencer266e42b2006-12-23 06:05:41 +0000591 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
592 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +0000593 case Instruction::Xor:
594 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
595 // Handle undef ^ undef -> 0 special case. This is a common
596 // idiom (misuse).
Owen Anderson5a1acd92009-07-31 20:28:14 +0000597 return Constant::getNullValue(C1->getType());
Evan Chengdf1690d2008-03-25 20:08:07 +0000598 // Fallthrough
Reid Spencer266e42b2006-12-23 06:05:41 +0000599 case Instruction::Add:
600 case Instruction::Sub:
Owen Andersonb292b8c2009-07-30 23:03:37 +0000601 return UndefValue::get(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000602 case Instruction::Mul:
603 case Instruction::And:
Owen Anderson5a1acd92009-07-31 20:28:14 +0000604 return Constant::getNullValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000605 case Instruction::UDiv:
606 case Instruction::SDiv:
Reid Spencer266e42b2006-12-23 06:05:41 +0000607 case Instruction::URem:
608 case Instruction::SRem:
Reid Spencer266e42b2006-12-23 06:05:41 +0000609 if (!isa<UndefValue>(C2)) // undef / X -> 0
Owen Anderson5a1acd92009-07-31 20:28:14 +0000610 return Constant::getNullValue(C1->getType());
Nick Lewyckye0332982009-09-20 01:35:59 +0000611 return C2; // X / undef -> undef
Reid Spencer266e42b2006-12-23 06:05:41 +0000612 case Instruction::Or: // X | undef -> -1
Reid Spencerd84d35b2007-02-15 02:26:10 +0000613 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000614 return Constant::getAllOnesValue(PTy);
615 return Constant::getAllOnesValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000616 case Instruction::LShr:
617 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
Nick Lewyckye0332982009-09-20 01:35:59 +0000618 return C1; // undef lshr undef -> undef
Owen Anderson5a1acd92009-07-31 20:28:14 +0000619 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
Reid Spencer266e42b2006-12-23 06:05:41 +0000620 // undef lshr X -> 0
621 case Instruction::AShr:
622 if (!isa<UndefValue>(C2))
Nick Lewyckye0332982009-09-20 01:35:59 +0000623 return C1; // undef ashr X --> undef
Reid Spencer266e42b2006-12-23 06:05:41 +0000624 else if (isa<UndefValue>(C1))
Nick Lewyckye0332982009-09-20 01:35:59 +0000625 return C1; // undef ashr undef -> undef
Reid Spencer266e42b2006-12-23 06:05:41 +0000626 else
Nick Lewyckye0332982009-09-20 01:35:59 +0000627 return C1; // X ashr undef --> X
Reid Spencer266e42b2006-12-23 06:05:41 +0000628 case Instruction::Shl:
629 // undef << X -> 0 or X << undef -> 0
Owen Anderson5a1acd92009-07-31 20:28:14 +0000630 return Constant::getNullValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000631 }
632 }
633
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000634 // Handle simplifications when the RHS is a constant int.
Nick Lewyckye0332982009-09-20 01:35:59 +0000635 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Chris Lattner334d33c2008-04-19 21:58:19 +0000636 switch (Opcode) {
637 case Instruction::Add:
Nick Lewyckye0332982009-09-20 01:35:59 +0000638 if (CI2->equalsInt(0)) return C1; // X + 0 == X
Chris Lattner334d33c2008-04-19 21:58:19 +0000639 break;
640 case Instruction::Sub:
Nick Lewyckye0332982009-09-20 01:35:59 +0000641 if (CI2->equalsInt(0)) return C1; // X - 0 == X
Chris Lattner334d33c2008-04-19 21:58:19 +0000642 break;
643 case Instruction::Mul:
Nick Lewyckye0332982009-09-20 01:35:59 +0000644 if (CI2->equalsInt(0)) return C2; // X * 0 == 0
Chris Lattner334d33c2008-04-19 21:58:19 +0000645 if (CI2->equalsInt(1))
Nick Lewyckye0332982009-09-20 01:35:59 +0000646 return C1; // X * 1 == X
Chris Lattner334d33c2008-04-19 21:58:19 +0000647 break;
648 case Instruction::UDiv:
649 case Instruction::SDiv:
650 if (CI2->equalsInt(1))
Nick Lewyckye0332982009-09-20 01:35:59 +0000651 return C1; // X / 1 == X
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000652 if (CI2->equalsInt(0))
Owen Andersonb292b8c2009-07-30 23:03:37 +0000653 return UndefValue::get(CI2->getType()); // X / 0 == undef
Chris Lattner334d33c2008-04-19 21:58:19 +0000654 break;
655 case Instruction::URem:
656 case Instruction::SRem:
657 if (CI2->equalsInt(1))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000658 return Constant::getNullValue(CI2->getType()); // X % 1 == 0
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000659 if (CI2->equalsInt(0))
Owen Andersonb292b8c2009-07-30 23:03:37 +0000660 return UndefValue::get(CI2->getType()); // X % 0 == undef
Chris Lattner334d33c2008-04-19 21:58:19 +0000661 break;
662 case Instruction::And:
Nick Lewyckye0332982009-09-20 01:35:59 +0000663 if (CI2->isZero()) return C2; // X & 0 == 0
Chris Lattner334d33c2008-04-19 21:58:19 +0000664 if (CI2->isAllOnesValue())
Nick Lewyckye0332982009-09-20 01:35:59 +0000665 return C1; // X & -1 == X
Dan Gohman062d3782009-08-29 23:35:16 +0000666
Nick Lewyckye0332982009-09-20 01:35:59 +0000667 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000668 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
Chris Lattner334d33c2008-04-19 21:58:19 +0000669 if (CE1->getOpcode() == Instruction::ZExt) {
670 unsigned DstWidth = CI2->getType()->getBitWidth();
671 unsigned SrcWidth =
672 CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
673 APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
674 if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
Nick Lewyckye0332982009-09-20 01:35:59 +0000675 return C1;
Chris Lattner6d94bb72007-03-25 05:47:04 +0000676 }
Dan Gohman062d3782009-08-29 23:35:16 +0000677
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000678 // If and'ing the address of a global with a constant, fold it.
Chris Lattner334d33c2008-04-19 21:58:19 +0000679 if (CE1->getOpcode() == Instruction::PtrToInt &&
680 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000681 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
Dan Gohman062d3782009-08-29 23:35:16 +0000682
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000683 // Functions are at least 4-byte aligned.
684 unsigned GVAlign = GV->getAlignment();
685 if (isa<Function>(GV))
686 GVAlign = std::max(GVAlign, 4U);
Dan Gohman062d3782009-08-29 23:35:16 +0000687
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000688 if (GVAlign > 1) {
689 unsigned DstWidth = CI2->getType()->getBitWidth();
Chris Lattner912bec72008-04-20 19:59:12 +0000690 unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000691 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
692
693 // If checking bits we know are clear, return zero.
694 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
Owen Anderson5a1acd92009-07-31 20:28:14 +0000695 return Constant::getNullValue(CI2->getType());
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000696 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000697 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000698 }
699 break;
700 case Instruction::Or:
Nick Lewyckye0332982009-09-20 01:35:59 +0000701 if (CI2->equalsInt(0)) return C1; // X | 0 == X
Chris Lattner334d33c2008-04-19 21:58:19 +0000702 if (CI2->isAllOnesValue())
Nick Lewyckye0332982009-09-20 01:35:59 +0000703 return C2; // X | -1 == -1
Chris Lattner334d33c2008-04-19 21:58:19 +0000704 break;
705 case Instruction::Xor:
Nick Lewyckye0332982009-09-20 01:35:59 +0000706 if (CI2->equalsInt(0)) return C1; // X ^ 0 == X
Nick Lewycky28260402009-09-20 06:24:51 +0000707
708 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
709 switch (CE1->getOpcode()) {
710 default: break;
711 case Instruction::ICmp:
712 case Instruction::FCmp:
713 // icmp pred ^ true -> icmp !pred
714 assert(CI2->equalsInt(1));
Nick Lewycky0f8348e2009-09-20 06:26:34 +0000715 CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
Nick Lewycky28260402009-09-20 06:24:51 +0000716 pred = CmpInst::getInversePredicate(pred);
717 return ConstantExpr::getCompare(pred, CE1->getOperand(0),
718 CE1->getOperand(1));
719 }
720 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000721 break;
722 case Instruction::AShr:
723 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
Nick Lewyckye0332982009-09-20 01:35:59 +0000724 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
Chris Lattner6d94bb72007-03-25 05:47:04 +0000725 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
Nick Lewyckye0332982009-09-20 01:35:59 +0000726 return ConstantExpr::getLShr(C1, C2);
Chris Lattner334d33c2008-04-19 21:58:19 +0000727 break;
Reid Spencer266e42b2006-12-23 06:05:41 +0000728 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000729 }
Dan Gohman062d3782009-08-29 23:35:16 +0000730
Chris Lattner6b056052008-04-20 18:24:14 +0000731 // At this point we know neither constant is an UndefValue.
Nick Lewyckye0332982009-09-20 01:35:59 +0000732 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
733 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000734 using namespace APIntOps;
Chris Lattner6b056052008-04-20 18:24:14 +0000735 const APInt &C1V = CI1->getValue();
736 const APInt &C2V = CI2->getValue();
Chris Lattner344da522007-01-12 18:42:52 +0000737 switch (Opcode) {
738 default:
739 break;
740 case Instruction::Add:
Owen Andersonedb4a702009-07-24 23:12:02 +0000741 return ConstantInt::get(Context, C1V + C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000742 case Instruction::Sub:
Owen Andersonedb4a702009-07-24 23:12:02 +0000743 return ConstantInt::get(Context, C1V - C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000744 case Instruction::Mul:
Owen Andersonedb4a702009-07-24 23:12:02 +0000745 return ConstantInt::get(Context, C1V * C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000746 case Instruction::UDiv:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000747 assert(!CI2->isNullValue() && "Div by zero handled above");
Owen Andersonedb4a702009-07-24 23:12:02 +0000748 return ConstantInt::get(Context, C1V.udiv(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000749 case Instruction::SDiv:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000750 assert(!CI2->isNullValue() && "Div by zero handled above");
Reid Spencer81658a82007-02-27 06:23:51 +0000751 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000752 return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef
Owen Andersonedb4a702009-07-24 23:12:02 +0000753 return ConstantInt::get(Context, C1V.sdiv(C2V));
Reid Spencer81658a82007-02-27 06:23:51 +0000754 case Instruction::URem:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000755 assert(!CI2->isNullValue() && "Div by zero handled above");
Owen Andersonedb4a702009-07-24 23:12:02 +0000756 return ConstantInt::get(Context, C1V.urem(C2V));
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000757 case Instruction::SRem:
758 assert(!CI2->isNullValue() && "Div by zero handled above");
Reid Spencer81658a82007-02-27 06:23:51 +0000759 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000760 return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef
Owen Andersonedb4a702009-07-24 23:12:02 +0000761 return ConstantInt::get(Context, C1V.srem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000762 case Instruction::And:
Owen Andersonedb4a702009-07-24 23:12:02 +0000763 return ConstantInt::get(Context, C1V & C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000764 case Instruction::Or:
Owen Andersonedb4a702009-07-24 23:12:02 +0000765 return ConstantInt::get(Context, C1V | C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000766 case Instruction::Xor:
Owen Andersonedb4a702009-07-24 23:12:02 +0000767 return ConstantInt::get(Context, C1V ^ C2V);
Chris Lattner6b056052008-04-20 18:24:14 +0000768 case Instruction::Shl: {
769 uint32_t shiftAmt = C2V.getZExtValue();
770 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000771 return ConstantInt::get(Context, C1V.shl(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000772 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000773 return UndefValue::get(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000774 }
775 case Instruction::LShr: {
776 uint32_t shiftAmt = C2V.getZExtValue();
777 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000778 return ConstantInt::get(Context, C1V.lshr(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000779 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000780 return UndefValue::get(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000781 }
782 case Instruction::AShr: {
783 uint32_t shiftAmt = C2V.getZExtValue();
784 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000785 return ConstantInt::get(Context, C1V.ashr(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000786 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000787 return UndefValue::get(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000788 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000789 }
790 }
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000791
792 switch (Opcode) {
793 case Instruction::SDiv:
794 case Instruction::UDiv:
795 case Instruction::URem:
796 case Instruction::SRem:
797 case Instruction::LShr:
798 case Instruction::AShr:
799 case Instruction::Shl:
Nick Lewyckye0332982009-09-20 01:35:59 +0000800 if (CI1->equalsInt(0)) return C1;
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000801 break;
802 default:
803 break;
804 }
Nick Lewyckye0332982009-09-20 01:35:59 +0000805 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
806 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000807 APFloat C1V = CFP1->getValueAPF();
808 APFloat C2V = CFP2->getValueAPF();
809 APFloat C3V = C1V; // copy for modification
Reid Spencer266e42b2006-12-23 06:05:41 +0000810 switch (Opcode) {
811 default:
812 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000813 case Instruction::FAdd:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000814 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000815 return ConstantFP::get(Context, C3V);
Dan Gohmana5b96452009-06-04 22:49:04 +0000816 case Instruction::FSub:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000817 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000818 return ConstantFP::get(Context, C3V);
Dan Gohmana5b96452009-06-04 22:49:04 +0000819 case Instruction::FMul:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000820 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000821 return ConstantFP::get(Context, C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000822 case Instruction::FDiv:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000823 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000824 return ConstantFP::get(Context, C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000825 case Instruction::FRem:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000826 (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000827 return ConstantFP::get(Context, C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000828 }
829 }
Dan Gohman9f396602007-10-30 19:00:49 +0000830 } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
Nick Lewyckye0332982009-09-20 01:35:59 +0000831 ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
832 ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000833 if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
834 (CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
Owen Anderson85f86dc2009-07-13 22:41:06 +0000835 std::vector<Constant*> Res;
836 const Type* EltTy = VTy->getElementType();
Nick Lewyckye0332982009-09-20 01:35:59 +0000837 Constant *C1 = 0;
838 Constant *C2 = 0;
Reid Spencer266e42b2006-12-23 06:05:41 +0000839 switch (Opcode) {
Chris Lattner6072ead2008-04-19 21:13:00 +0000840 default:
841 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000842 case Instruction::Add:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000843 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000844 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
845 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000846 Res.push_back(ConstantExpr::getAdd(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000847 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000848 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000849 case Instruction::FAdd:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000850 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000851 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
852 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000853 Res.push_back(ConstantExpr::getFAdd(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000854 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000855 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000856 case Instruction::Sub:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000857 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000858 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
859 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000860 Res.push_back(ConstantExpr::getSub(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000861 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000862 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000863 case Instruction::FSub:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000864 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000865 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
866 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000867 Res.push_back(ConstantExpr::getFSub(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000868 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000869 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000870 case Instruction::Mul:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000871 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000872 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
873 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000874 Res.push_back(ConstantExpr::getMul(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000875 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000876 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000877 case Instruction::FMul:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000878 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000879 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
880 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000881 Res.push_back(ConstantExpr::getFMul(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000882 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000883 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000884 case Instruction::UDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000885 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000886 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
887 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000888 Res.push_back(ConstantExpr::getUDiv(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000889 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000890 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000891 case Instruction::SDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000892 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000893 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
894 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000895 Res.push_back(ConstantExpr::getSDiv(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000896 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000897 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000898 case Instruction::FDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000899 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000900 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
901 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000902 Res.push_back(ConstantExpr::getFDiv(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000903 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000904 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000905 case Instruction::URem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000906 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000907 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
908 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000909 Res.push_back(ConstantExpr::getURem(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000910 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000911 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000912 case Instruction::SRem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000913 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000914 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
915 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000916 Res.push_back(ConstantExpr::getSRem(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000917 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000918 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000919 case Instruction::FRem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000920 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000921 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
922 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000923 Res.push_back(ConstantExpr::getFRem(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000924 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000925 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000926 case Instruction::And:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000927 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000928 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
929 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000930 Res.push_back(ConstantExpr::getAnd(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000931 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000932 return ConstantVector::get(Res);
Owen Anderson85f86dc2009-07-13 22:41:06 +0000933 case Instruction::Or:
934 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000935 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
936 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000937 Res.push_back(ConstantExpr::getOr(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000938 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000939 return ConstantVector::get(Res);
Owen Anderson85f86dc2009-07-13 22:41:06 +0000940 case Instruction::Xor:
941 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000942 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
943 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000944 Res.push_back(ConstantExpr::getXor(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000945 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000946 return ConstantVector::get(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000947 case Instruction::LShr:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000948 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000949 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
950 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000951 Res.push_back(ConstantExpr::getLShr(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000952 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000953 return ConstantVector::get(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000954 case Instruction::AShr:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000955 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000956 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
957 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000958 Res.push_back(ConstantExpr::getAShr(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000959 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000960 return ConstantVector::get(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) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000963 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
964 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000965 Res.push_back(ConstantExpr::getShl(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000966 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000967 return ConstantVector::get(Res);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000968 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000969 }
970 }
971
Chris Lattner6b056052008-04-20 18:24:14 +0000972 if (isa<ConstantExpr>(C1)) {
973 // There are many possible foldings we could do here. We should probably
974 // at least fold add of a pointer with an integer into the appropriate
975 // getelementptr. This will improve alias analysis a bit.
976 } else if (isa<ConstantExpr>(C2)) {
977 // If C2 is a constant expr and C1 isn't, flop them around and fold the
978 // other way if possible.
979 switch (Opcode) {
980 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000981 case Instruction::FAdd:
Chris Lattner6b056052008-04-20 18:24:14 +0000982 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000983 case Instruction::FMul:
Chris Lattner6b056052008-04-20 18:24:14 +0000984 case Instruction::And:
985 case Instruction::Or:
986 case Instruction::Xor:
987 // No change of opcode required.
Owen Anderson53a52212009-07-13 04:09:18 +0000988 return ConstantFoldBinaryInstruction(Context, Opcode, C2, C1);
Dan Gohman062d3782009-08-29 23:35:16 +0000989
Chris Lattner6b056052008-04-20 18:24:14 +0000990 case Instruction::Shl:
991 case Instruction::LShr:
992 case Instruction::AShr:
993 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +0000994 case Instruction::FSub:
Chris Lattner6b056052008-04-20 18:24:14 +0000995 case Instruction::SDiv:
996 case Instruction::UDiv:
997 case Instruction::FDiv:
998 case Instruction::URem:
999 case Instruction::SRem:
1000 case Instruction::FRem:
1001 default: // These instructions cannot be flopped around.
1002 break;
1003 }
1004 }
Dan Gohman062d3782009-08-29 23:35:16 +00001005
Nick Lewycky605109d2009-09-20 00:04:02 +00001006 // i1 can be simplified in many cases.
1007 if (C1->getType() == Type::getInt1Ty(Context)) {
1008 switch (Opcode) {
1009 case Instruction::Add:
1010 case Instruction::Sub:
Nick Lewyckye0332982009-09-20 01:35:59 +00001011 return ConstantExpr::getXor(C1, C2);
Nick Lewycky605109d2009-09-20 00:04:02 +00001012 case Instruction::Mul:
Nick Lewyckye0332982009-09-20 01:35:59 +00001013 return ConstantExpr::getAnd(C1, C2);
Nick Lewycky605109d2009-09-20 00:04:02 +00001014 case Instruction::Shl:
1015 case Instruction::LShr:
1016 case Instruction::AShr:
1017 // We can assume that C2 == 0. If it were one the result would be
1018 // undefined because the shift value is as large as the bitwidth.
Nick Lewyckye0332982009-09-20 01:35:59 +00001019 return C1;
Nick Lewycky605109d2009-09-20 00:04:02 +00001020 case Instruction::SDiv:
1021 case Instruction::UDiv:
1022 // We can assume that C2 == 1. If it were zero the result would be
1023 // undefined through division by zero.
Nick Lewyckye0332982009-09-20 01:35:59 +00001024 return C1;
Nick Lewycky605109d2009-09-20 00:04:02 +00001025 case Instruction::URem:
1026 case Instruction::SRem:
1027 // We can assume that C2 == 1. If it were zero the result would be
1028 // undefined through division by zero.
1029 return ConstantInt::getFalse(Context);
1030 default:
1031 break;
1032 }
1033 }
1034
Chris Lattner6b056052008-04-20 18:24:14 +00001035 // We don't know how to fold this.
Reid Spencer266e42b2006-12-23 06:05:41 +00001036 return 0;
1037}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001038
Chris Lattner60c47262005-01-28 19:09:51 +00001039/// isZeroSizedType - This type is zero sized if its an array or structure of
1040/// zero sized types. The only leaf zero sized type is an empty structure.
1041static bool isMaybeZeroSizedType(const Type *Ty) {
1042 if (isa<OpaqueType>(Ty)) return true; // Can't say.
1043 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1044
1045 // If all of elements have zero size, this does too.
1046 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +00001047 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +00001048 return true;
1049
1050 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1051 return isMaybeZeroSizedType(ATy->getElementType());
1052 }
1053 return false;
1054}
Chris Lattner6ea4b522004-03-12 05:53:32 +00001055
Chris Lattner061da2f2004-01-13 05:51:55 +00001056/// IdxCompare - Compare the two constants as though they were getelementptr
1057/// indices. This allows coersion of the types to be the same thing.
1058///
1059/// If the two constants are the "same" (after coersion), return 0. If the
1060/// first is less than the second, return -1, if the second is less than the
1061/// first, return 1. If the constants are not integral, return -2.
1062///
Owen Anderson53a52212009-07-13 04:09:18 +00001063static int IdxCompare(LLVMContext &Context, Constant *C1, Constant *C2,
1064 const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001065 if (C1 == C2) return 0;
1066
Reid Spencerc90cf772006-12-31 21:43:30 +00001067 // Ok, we found a different index. If they are not ConstantInt, we can't do
1068 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +00001069 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1070 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +00001071
Chris Lattner69193f92004-04-05 01:30:19 +00001072 // Ok, we have two differing integer indices. Sign extend them to be the same
1073 // type. Long is always big enough, so we use it.
Owen Anderson55f1c092009-08-13 21:58:54 +00001074 if (C1->getType() != Type::getInt64Ty(Context))
1075 C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(Context));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001076
Owen Anderson55f1c092009-08-13 21:58:54 +00001077 if (C2->getType() != Type::getInt64Ty(Context))
1078 C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(Context));
Reid Spencer8d9336d2006-12-31 05:26:44 +00001079
1080 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +00001081
Chris Lattner60c47262005-01-28 19:09:51 +00001082 // If the type being indexed over is really just a zero sized type, there is
1083 // no pointer difference being made here.
1084 if (isMaybeZeroSizedType(ElTy))
1085 return -2; // dunno.
1086
Chris Lattner061da2f2004-01-13 05:51:55 +00001087 // If they are really different, now that they are the same type, then we
1088 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +00001089 if (cast<ConstantInt>(C1)->getSExtValue() <
1090 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +00001091 return -1;
1092 else
1093 return 1;
1094}
1095
Chris Lattner858f4e92007-01-04 02:13:20 +00001096/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +00001097/// decide about the two constants provided. This doesn't need to handle simple
1098/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
1099/// If we can determine that the two constants have a particular relation to
1100/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001101/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1102/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +00001103///
1104/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +00001105/// operand is always the most "complex" of the two. We consider ConstantFP
1106/// to be the simplest, and ConstantExprs to be the most complex.
Owen Anderson53a52212009-07-13 04:09:18 +00001107static FCmpInst::Predicate evaluateFCmpRelation(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +00001108 Constant *V1, Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001109 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +00001110 "Cannot compare values of different types!");
Dale Johannesen19db0932007-10-14 01:56:47 +00001111
1112 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +00001113 if (V1->getType() == Type::getPPC_FP128Ty(Context))
Dale Johannesen19db0932007-10-14 01:56:47 +00001114 return FCmpInst::BAD_FCMP_PREDICATE;
1115
Reid Spencer9d36acf2006-12-24 18:52:08 +00001116 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +00001117 if (V1 == V2) return FCmpInst::FCMP_OEQ;
1118
Reid Spencer9d36acf2006-12-24 18:52:08 +00001119 if (!isa<ConstantExpr>(V1)) {
1120 if (!isa<ConstantExpr>(V2)) {
1121 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +00001122 ConstantInt *R = 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001123 R = dyn_cast<ConstantInt>(
Nick Lewyckye0332982009-09-20 01:35:59 +00001124 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001125 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001126 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001127 R = dyn_cast<ConstantInt>(
Nick Lewyckye0332982009-09-20 01:35:59 +00001128 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001129 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001130 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001131 R = dyn_cast<ConstantInt>(
Nick Lewyckye0332982009-09-20 01:35:59 +00001132 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001133 if (R && !R->isZero())
Reid Spencer9d36acf2006-12-24 18:52:08 +00001134 return FCmpInst::FCMP_OGT;
1135
1136 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +00001137 return FCmpInst::BAD_FCMP_PREDICATE;
1138 }
Dan Gohman062d3782009-08-29 23:35:16 +00001139
Reid Spencer9d36acf2006-12-24 18:52:08 +00001140 // If the first operand is simple and second is ConstantExpr, swap operands.
Owen Anderson53a52212009-07-13 04:09:18 +00001141 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(Context, V2, V1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001142 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1143 return FCmpInst::getSwappedPredicate(SwappedRelation);
1144 } else {
1145 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1146 // constantexpr or a simple constant.
Nick Lewyckye0332982009-09-20 01:35:59 +00001147 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001148 switch (CE1->getOpcode()) {
1149 case Instruction::FPTrunc:
1150 case Instruction::FPExt:
1151 case Instruction::UIToFP:
1152 case Instruction::SIToFP:
1153 // We might be able to do something with these but we don't right now.
1154 break;
1155 default:
1156 break;
1157 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001158 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001159 // There are MANY other foldings that we could perform here. They will
1160 // probably be added on demand, as they seem needed.
1161 return FCmpInst::BAD_FCMP_PREDICATE;
1162}
1163
1164/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +00001165/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +00001166/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +00001167/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +00001168/// particular relation to each other, we should return the corresponding ICmp
1169/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +00001170///
1171/// To simplify this code we canonicalize the relation so that the first
1172/// operand is always the most "complex" of the two. We consider simple
1173/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +00001174/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +00001175///
Owen Anderson53a52212009-07-13 04:09:18 +00001176static ICmpInst::Predicate evaluateICmpRelation(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +00001177 Constant *V1,
1178 Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +00001179 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001180 assert(V1->getType() == V2->getType() &&
1181 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001182 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001183
Reid Spenceraccd7c72004-07-17 23:47:01 +00001184 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001185 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1186 // We distilled this down to a simple case, use the standard constant
1187 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001188 ConstantInt *R = 0;
Reid Spencer266e42b2006-12-23 06:05:41 +00001189 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Nick Lewyckye0332982009-09-20 01:35:59 +00001190 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001191 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001192 return pred;
1193 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Nick Lewyckye0332982009-09-20 01:35:59 +00001194 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001195 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001196 return pred;
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001197 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Nick Lewyckye0332982009-09-20 01:35:59 +00001198 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001199 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001200 return pred;
Dan Gohman062d3782009-08-29 23:35:16 +00001201
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001202 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +00001203 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001204 }
Dan Gohman062d3782009-08-29 23:35:16 +00001205
Chris Lattner061da2f2004-01-13 05:51:55 +00001206 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +00001207 ICmpInst::Predicate SwappedRelation =
Owen Anderson53a52212009-07-13 04:09:18 +00001208 evaluateICmpRelation(Context, V2, V1, isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001209 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1210 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001211
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001212 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001213 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +00001214 ICmpInst::Predicate SwappedRelation =
Owen Anderson53a52212009-07-13 04:09:18 +00001215 evaluateICmpRelation(Context, V2, V1, isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001216 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1217 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001218 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001219 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +00001220 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001221
Reid Spenceraccd7c72004-07-17 23:47:01 +00001222 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001223 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001224 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner52fe8692007-09-10 23:42:42 +00001225 // Don't try to decide equality of aliases.
1226 if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2))
1227 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
1228 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001229 } else {
1230 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Chris Lattner52fe8692007-09-10 23:42:42 +00001231 // GlobalVals can never be null. Don't try to evaluate aliases.
1232 if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1))
Reid Spencer266e42b2006-12-23 06:05:41 +00001233 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001234 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001235 } else {
1236 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1237 // constantexpr, a CPR, or a simple constant.
Nick Lewyckye0332982009-09-20 01:35:59 +00001238 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1239 Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +00001240
1241 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001242 case Instruction::Trunc:
1243 case Instruction::FPTrunc:
1244 case Instruction::FPExt:
1245 case Instruction::FPToUI:
1246 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +00001247 break; // We can't evaluate floating point casts or truncations.
1248
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001249 case Instruction::UIToFP:
1250 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001251 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +00001252 case Instruction::ZExt:
1253 case Instruction::SExt:
Chris Lattner061da2f2004-01-13 05:51:55 +00001254 // If the cast is not actually changing bits, and the second operand is a
1255 // null pointer, do the comparison with the pre-casted value.
1256 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +00001257 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Chris Lattnerd2265b42007-12-10 22:53:04 +00001258 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1259 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
Owen Anderson53a52212009-07-13 04:09:18 +00001260 return evaluateICmpRelation(Context, CE1Op0,
Owen Anderson5a1acd92009-07-31 20:28:14 +00001261 Constant::getNullValue(CE1Op0->getType()),
Nick Lewycky2949a232009-09-20 03:48:46 +00001262 isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001263 }
Chris Lattner192e3262004-04-11 01:29:30 +00001264 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001265
1266 case Instruction::GetElementPtr:
1267 // Ok, since this is a getelementptr, we know that the constant has a
1268 // pointer type. Check the various cases.
1269 if (isa<ConstantPointerNull>(V2)) {
1270 // If we are comparing a GEP to a null pointer, check to see if the base
1271 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +00001272 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001273 if (GV->hasExternalWeakLinkage())
1274 // Weak linkage GVals could be zero or not. We're comparing that
1275 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001276 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +00001277 else
1278 // If its not weak linkage, the GVal must have a non-zero address
1279 // so the result is greater-than
Nick Lewycky9e085542009-09-20 04:27:06 +00001280 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001281 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1282 // If we are indexing from a null pointer, check to see if we have any
1283 // non-zero indices.
1284 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1285 if (!CE1->getOperand(i)->isNullValue())
1286 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001287 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001288 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +00001289 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001290 }
1291 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001292 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001293 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001294 if (CPR2->hasExternalWeakLinkage())
1295 // Weak linkage GVals could be zero or not. We're comparing it to
1296 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001297 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +00001298 else
1299 // If its not weak linkage, the GVal must have a non-zero address
1300 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +00001301 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001302 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001303 if (CPR1 == CPR2) {
1304 // If this is a getelementptr of the same global, then it must be
1305 // different. Because the types must match, the getelementptr could
1306 // only have at most one index, and because we fold getelementptr's
1307 // with a single zero index, it must be nonzero.
1308 assert(CE1->getNumOperands() == 2 &&
1309 !CE1->getOperand(1)->isNullValue() &&
1310 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001311 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001312 } else {
1313 // If they are different globals, we don't know what the value is,
1314 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001315 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001316 }
1317 }
1318 } else {
Nick Lewyckye0332982009-09-20 01:35:59 +00001319 ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1320 Constant *CE2Op0 = CE2->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +00001321
1322 // There are MANY other foldings that we could perform here. They will
1323 // probably be added on demand, as they seem needed.
1324 switch (CE2->getOpcode()) {
1325 default: break;
1326 case Instruction::GetElementPtr:
1327 // By far the most common case to handle is when the base pointers are
1328 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001329 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001330 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001331 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001332 // Ok, we know that both getelementptr instructions are based on the
1333 // same global. From this, we can precisely determine the relative
1334 // ordering of the resultant pointers.
1335 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001336
Dan Gohman7190d482009-09-10 23:37:55 +00001337 // The logic below assumes that the result of the comparison
1338 // can be determined by finding the first index that differs.
1339 // This doesn't work if there is over-indexing in any
1340 // subsequent indices, so check for that case first.
1341 if (!CE1->isGEPWithNoNotionalOverIndexing() ||
1342 !CE2->isGEPWithNoNotionalOverIndexing())
1343 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1344
Chris Lattner061da2f2004-01-13 05:51:55 +00001345 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001346 gep_type_iterator GTI = gep_type_begin(CE1);
1347 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1348 ++i, ++GTI)
Owen Anderson53a52212009-07-13 04:09:18 +00001349 switch (IdxCompare(Context, CE1->getOperand(i),
1350 CE2->getOperand(i), GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001351 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1352 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1353 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001354 }
1355
1356 // Ok, we ran out of things they have in common. If any leftovers
1357 // are non-zero then we have a difference, otherwise we are equal.
1358 for (; i < CE1->getNumOperands(); ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001359 if (!CE1->getOperand(i)->isNullValue()) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001360 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001361 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +00001362 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001363 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001364 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001365
Chris Lattner061da2f2004-01-13 05:51:55 +00001366 for (; i < CE2->getNumOperands(); ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001367 if (!CE2->getOperand(i)->isNullValue()) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001368 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001369 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001370 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001371 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001372 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001373 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001374 }
1375 }
1376 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001377 default:
1378 break;
1379 }
1380 }
1381
Reid Spencer266e42b2006-12-23 06:05:41 +00001382 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001383}
1384
Owen Anderson53a52212009-07-13 04:09:18 +00001385Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context,
1386 unsigned short pred,
Nick Lewyckye0332982009-09-20 01:35:59 +00001387 Constant *C1, Constant *C2) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001388 const Type *ResultTy;
1389 if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
Owen Anderson55f1c092009-08-13 21:58:54 +00001390 ResultTy = VectorType::get(Type::getInt1Ty(Context), VT->getNumElements());
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001391 else
Owen Anderson55f1c092009-08-13 21:58:54 +00001392 ResultTy = Type::getInt1Ty(Context);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001393
Chris Lattnerd137a082008-07-08 05:46:34 +00001394 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001395 if (pred == FCmpInst::FCMP_FALSE)
Owen Anderson5a1acd92009-07-31 20:28:14 +00001396 return Constant::getNullValue(ResultTy);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001397
1398 if (pred == FCmpInst::FCMP_TRUE)
Owen Anderson5a1acd92009-07-31 20:28:14 +00001399 return Constant::getAllOnesValue(ResultTy);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001400
Reid Spencer266e42b2006-12-23 06:05:41 +00001401 // Handle some degenerate cases first
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001402 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Owen Andersonb292b8c2009-07-30 23:03:37 +00001403 return UndefValue::get(ResultTy);
Reid Spencer266e42b2006-12-23 06:05:41 +00001404
Dale Johannesen19db0932007-10-14 01:56:47 +00001405 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +00001406 if (C1->getType() == Type::getPPC_FP128Ty(Context))
Dale Johannesen19db0932007-10-14 01:56:47 +00001407 return 0;
1408
Reid Spencer266e42b2006-12-23 06:05:41 +00001409 // icmp eq/ne(null,GV) -> false/true
1410 if (C1->isNullValue()) {
1411 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001412 // Don't try to evaluate aliases. External weak GV can be null.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001413 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001414 if (pred == ICmpInst::ICMP_EQ)
Owen Anderson23a204d2009-07-31 17:39:07 +00001415 return ConstantInt::getFalse(Context);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001416 else if (pred == ICmpInst::ICMP_NE)
Owen Anderson23a204d2009-07-31 17:39:07 +00001417 return ConstantInt::getTrue(Context);
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001418 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001419 // icmp eq/ne(GV,null) -> false/true
1420 } else if (C2->isNullValue()) {
1421 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001422 // Don't try to evaluate aliases. External weak GV can be null.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001423 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001424 if (pred == ICmpInst::ICMP_EQ)
Owen Anderson23a204d2009-07-31 17:39:07 +00001425 return ConstantInt::getFalse(Context);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001426 else if (pred == ICmpInst::ICMP_NE)
Owen Anderson23a204d2009-07-31 17:39:07 +00001427 return ConstantInt::getTrue(Context);
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001428 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001429 }
1430
Chris Lattner344da522007-01-12 18:42:52 +00001431 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +00001432 APInt V1 = cast<ConstantInt>(C1)->getValue();
1433 APInt V2 = cast<ConstantInt>(C2)->getValue();
1434 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001435 default: llvm_unreachable("Invalid ICmp Predicate"); return 0;
Owen Anderson53a52212009-07-13 04:09:18 +00001436 case ICmpInst::ICMP_EQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001437 return ConstantInt::get(Type::getInt1Ty(Context), V1 == V2);
Owen Anderson53a52212009-07-13 04:09:18 +00001438 case ICmpInst::ICMP_NE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001439 return ConstantInt::get(Type::getInt1Ty(Context), V1 != V2);
Owen Anderson53a52212009-07-13 04:09:18 +00001440 case ICmpInst::ICMP_SLT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001441 return ConstantInt::get(Type::getInt1Ty(Context), V1.slt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001442 case ICmpInst::ICMP_SGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001443 return ConstantInt::get(Type::getInt1Ty(Context), V1.sgt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001444 case ICmpInst::ICMP_SLE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001445 return ConstantInt::get(Type::getInt1Ty(Context), V1.sle(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001446 case ICmpInst::ICMP_SGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001447 return ConstantInt::get(Type::getInt1Ty(Context), V1.sge(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001448 case ICmpInst::ICMP_ULT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001449 return ConstantInt::get(Type::getInt1Ty(Context), V1.ult(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001450 case ICmpInst::ICMP_UGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001451 return ConstantInt::get(Type::getInt1Ty(Context), V1.ugt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001452 case ICmpInst::ICMP_ULE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001453 return ConstantInt::get(Type::getInt1Ty(Context), V1.ule(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001454 case ICmpInst::ICMP_UGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001455 return ConstantInt::get(Type::getInt1Ty(Context), V1.uge(V2));
Chris Lattner061da2f2004-01-13 05:51:55 +00001456 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001457 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001458 APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
1459 APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
1460 APFloat::cmpResult R = C1V.compare(C2V);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001461 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001462 default: llvm_unreachable("Invalid FCmp Predicate"); return 0;
Owen Anderson23a204d2009-07-31 17:39:07 +00001463 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse(Context);
1464 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue(Context);
Reid Spencer266e42b2006-12-23 06:05:41 +00001465 case FCmpInst::FCMP_UNO:
Owen Anderson55f1c092009-08-13 21:58:54 +00001466 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered);
Reid Spencer74bd0362007-01-11 00:25:45 +00001467 case FCmpInst::FCMP_ORD:
Owen Anderson55f1c092009-08-13 21:58:54 +00001468 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpUnordered);
Reid Spencer266e42b2006-12-23 06:05:41 +00001469 case FCmpInst::FCMP_UEQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001470 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001471 R==APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001472 case FCmpInst::FCMP_OEQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001473 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpEqual);
Reid Spencer74bd0362007-01-11 00:25:45 +00001474 case FCmpInst::FCMP_UNE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001475 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001476 case FCmpInst::FCMP_ONE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001477 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001478 R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001479 case FCmpInst::FCMP_ULT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001480 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001481 R==APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001482 case FCmpInst::FCMP_OLT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001483 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan);
Reid Spencer266e42b2006-12-23 06:05:41 +00001484 case FCmpInst::FCMP_UGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001485 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001486 R==APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001487 case FCmpInst::FCMP_OGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001488 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001489 case FCmpInst::FCMP_ULE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001490 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001491 case FCmpInst::FCMP_OLE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001492 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001493 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001494 case FCmpInst::FCMP_UGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001495 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001496 case FCmpInst::FCMP_OGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001497 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpGreaterThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001498 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001499 }
Chris Lattner67136cf2008-07-10 00:29:28 +00001500 } else if (isa<VectorType>(C1->getType())) {
1501 SmallVector<Constant*, 16> C1Elts, C2Elts;
Owen Anderson53a52212009-07-13 04:09:18 +00001502 C1->getVectorElements(Context, C1Elts);
1503 C2->getVectorElements(Context, C2Elts);
Dan Gohman062d3782009-08-29 23:35:16 +00001504
Chris Lattner67136cf2008-07-10 00:29:28 +00001505 // If we can constant fold the comparison of each element, constant fold
1506 // the whole vector comparison.
1507 SmallVector<Constant*, 4> ResElts;
Chris Lattner67136cf2008-07-10 00:29:28 +00001508 for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) {
1509 // Compare the elements, producing an i1 result or constant expr.
Owen Anderson53a52212009-07-13 04:09:18 +00001510 ResElts.push_back(
Owen Anderson487375e2009-07-29 18:55:55 +00001511 ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i]));
Reid Spencer266e42b2006-12-23 06:05:41 +00001512 }
Owen Anderson4aa32952009-07-28 21:19:26 +00001513 return ConstantVector::get(&ResElts[0], ResElts.size());
Reid Spencer266e42b2006-12-23 06:05:41 +00001514 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001515
Reid Spencer9d36acf2006-12-24 18:52:08 +00001516 if (C1->getType()->isFloatingPoint()) {
Chris Lattner350e4172008-07-08 18:47:38 +00001517 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
Owen Anderson53a52212009-07-13 04:09:18 +00001518 switch (evaluateFCmpRelation(Context, C1, C2)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001519 default: llvm_unreachable("Unknown relation!");
Reid Spencer9d36acf2006-12-24 18:52:08 +00001520 case FCmpInst::FCMP_UNO:
1521 case FCmpInst::FCMP_ORD:
1522 case FCmpInst::FCMP_UEQ:
1523 case FCmpInst::FCMP_UNE:
1524 case FCmpInst::FCMP_ULT:
1525 case FCmpInst::FCMP_UGT:
1526 case FCmpInst::FCMP_ULE:
1527 case FCmpInst::FCMP_UGE:
1528 case FCmpInst::FCMP_TRUE:
1529 case FCmpInst::FCMP_FALSE:
1530 case FCmpInst::BAD_FCMP_PREDICATE:
1531 break; // Couldn't determine anything about these constants.
1532 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001533 Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1534 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1535 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1536 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001537 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001538 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1539 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1540 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1541 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001542 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001543 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1544 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1545 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1546 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001547 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1548 // We can only partially decide this relation.
1549 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Chris Lattnerd137a082008-07-08 05:46:34 +00001550 Result = 0;
1551 else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
1552 Result = 1;
Chris Lattner061da2f2004-01-13 05:51:55 +00001553 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001554 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1555 // We can only partially decide this relation.
1556 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Chris Lattnerd137a082008-07-08 05:46:34 +00001557 Result = 0;
1558 else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
1559 Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001560 break;
1561 case ICmpInst::ICMP_NE: // We know that C1 != C2
1562 // We can only partially decide this relation.
1563 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Chris Lattnerd137a082008-07-08 05:46:34 +00001564 Result = 0;
1565 else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
1566 Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001567 break;
1568 }
Dan Gohman062d3782009-08-29 23:35:16 +00001569
Chris Lattnerd137a082008-07-08 05:46:34 +00001570 // If we evaluated the result, return it now.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001571 if (Result != -1)
Owen Anderson55f1c092009-08-13 21:58:54 +00001572 return ConstantInt::get(Type::getInt1Ty(Context), Result);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001573
Reid Spencer9d36acf2006-12-24 18:52:08 +00001574 } else {
1575 // Evaluate the relation between the two constants, per the predicate.
Chris Lattnerd137a082008-07-08 05:46:34 +00001576 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
Owen Anderson53a52212009-07-13 04:09:18 +00001577 switch (evaluateICmpRelation(Context, C1, C2, CmpInst::isSigned(pred))) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001578 default: llvm_unreachable("Unknown relational!");
Reid Spencer9d36acf2006-12-24 18:52:08 +00001579 case ICmpInst::BAD_ICMP_PREDICATE:
1580 break; // Couldn't determine anything about these constants.
1581 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1582 // If we know the constants are equal, we can decide the result of this
1583 // computation precisely.
Nick Lewycky9e085542009-09-20 04:27:06 +00001584 Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
Chris Lattnerd137a082008-07-08 05:46:34 +00001585 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001586 case ICmpInst::ICMP_ULT:
Nick Lewycky9e085542009-09-20 04:27:06 +00001587 switch (pred) {
1588 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001589 Result = 1; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001590 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001591 Result = 0; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001592 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001593 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001594 case ICmpInst::ICMP_SLT:
Nick Lewycky9e085542009-09-20 04:27:06 +00001595 switch (pred) {
1596 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001597 Result = 1; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001598 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001599 Result = 0; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001600 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001601 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001602 case ICmpInst::ICMP_UGT:
Nick Lewycky9e085542009-09-20 04:27:06 +00001603 switch (pred) {
1604 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001605 Result = 1; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001606 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001607 Result = 0; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001608 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001609 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001610 case ICmpInst::ICMP_SGT:
Nick Lewycky9e085542009-09-20 04:27:06 +00001611 switch (pred) {
1612 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001613 Result = 1; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001614 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001615 Result = 0; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001616 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001617 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001618 case ICmpInst::ICMP_ULE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001619 if (pred == ICmpInst::ICMP_UGT) Result = 0;
Nick Lewycky9e085542009-09-20 04:27:06 +00001620 if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001621 break;
1622 case ICmpInst::ICMP_SLE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001623 if (pred == ICmpInst::ICMP_SGT) Result = 0;
Nick Lewycky9e085542009-09-20 04:27:06 +00001624 if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001625 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001626 case ICmpInst::ICMP_UGE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001627 if (pred == ICmpInst::ICMP_ULT) Result = 0;
Nick Lewycky9e085542009-09-20 04:27:06 +00001628 if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001629 break;
1630 case ICmpInst::ICMP_SGE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001631 if (pred == ICmpInst::ICMP_SLT) Result = 0;
Nick Lewycky9e085542009-09-20 04:27:06 +00001632 if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001633 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001634 case ICmpInst::ICMP_NE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001635 if (pred == ICmpInst::ICMP_EQ) Result = 0;
1636 if (pred == ICmpInst::ICMP_NE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001637 break;
1638 }
Dan Gohman062d3782009-08-29 23:35:16 +00001639
Chris Lattnerd137a082008-07-08 05:46:34 +00001640 // If we evaluated the result, return it now.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001641 if (Result != -1)
Owen Anderson55f1c092009-08-13 21:58:54 +00001642 return ConstantInt::get(Type::getInt1Ty(Context), Result);
Dan Gohman062d3782009-08-29 23:35:16 +00001643
Nick Lewycky4a034522009-09-20 05:48:50 +00001644 // If the right hand side is a bitcast, try using its inverse to simplify
1645 // it by moving it to the left hand side.
1646 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
1647 if (CE2->getOpcode() == Instruction::BitCast) {
1648 Constant *CE2Op0 = CE2->getOperand(0);
1649 Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
1650 return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
1651 }
1652 }
1653
Reid Spencer9d36acf2006-12-24 18:52:08 +00001654 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001655 // If C2 is a constant expr and C1 isn't, flip them around and fold the
Reid Spencer9d36acf2006-12-24 18:52:08 +00001656 // other way if possible.
1657 switch (pred) {
1658 case ICmpInst::ICMP_EQ:
1659 case ICmpInst::ICMP_NE:
1660 // No change of predicate required.
Owen Anderson53a52212009-07-13 04:09:18 +00001661 return ConstantFoldCompareInstruction(Context, pred, C2, C1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001662
1663 case ICmpInst::ICMP_ULT:
1664 case ICmpInst::ICMP_SLT:
1665 case ICmpInst::ICMP_UGT:
1666 case ICmpInst::ICMP_SGT:
1667 case ICmpInst::ICMP_ULE:
1668 case ICmpInst::ICMP_SLE:
1669 case ICmpInst::ICMP_UGE:
1670 case ICmpInst::ICMP_SGE:
1671 // Change the predicate as necessary to swap the operands.
1672 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
Owen Anderson53a52212009-07-13 04:09:18 +00001673 return ConstantFoldCompareInstruction(Context, pred, C2, C1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001674
1675 default: // These predicates cannot be flopped around.
1676 break;
1677 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001678 }
1679 }
1680 return 0;
Dan Gohman062d3782009-08-29 23:35:16 +00001681}
Chris Lattner1dd054c2004-01-12 22:07:24 +00001682
Dan Gohman21c62162009-09-11 00:04:14 +00001683/// isInBoundsIndices - Test whether the given sequence of *normalized* indices
1684/// is "inbounds".
1685static bool isInBoundsIndices(Constant *const *Idxs, size_t NumIdx) {
1686 // No indices means nothing that could be out of bounds.
1687 if (NumIdx == 0) return true;
1688
1689 // If the first index is zero, it's in bounds.
1690 if (Idxs[0]->isNullValue()) return true;
1691
1692 // If the first index is one and all the rest are zero, it's in bounds,
1693 // by the one-past-the-end rule.
1694 if (!cast<ConstantInt>(Idxs[0])->isOne())
1695 return false;
1696 for (unsigned i = 1, e = NumIdx; i != e; ++i)
1697 if (!Idxs[i]->isNullValue())
1698 return false;
1699 return true;
1700}
1701
Owen Anderson53a52212009-07-13 04:09:18 +00001702Constant *llvm::ConstantFoldGetElementPtr(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +00001703 Constant *C,
Dan Gohman21c62162009-09-11 00:04:14 +00001704 bool inBounds,
David Greenec656cbb2007-09-04 15:46:09 +00001705 Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001706 unsigned NumIdx) {
Chris Lattner302116a2007-01-31 04:40:28 +00001707 if (NumIdx == 0 ||
1708 (NumIdx == 1 && Idxs[0]->isNullValue()))
Nick Lewyckye0332982009-09-20 01:35:59 +00001709 return C;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001710
Chris Lattnerf6013752004-10-17 21:54:55 +00001711 if (isa<UndefValue>(C)) {
Christopher Lambedf07882007-12-17 01:12:55 +00001712 const PointerType *Ptr = cast<PointerType>(C->getType());
1713 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
David Greenec656cbb2007-09-04 15:46:09 +00001714 (Value **)Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001715 (Value **)Idxs+NumIdx);
Chris Lattnerf6013752004-10-17 21:54:55 +00001716 assert(Ty != 0 && "Invalid indices for GEP!");
Owen Andersonb292b8c2009-07-30 23:03:37 +00001717 return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
Chris Lattnerf6013752004-10-17 21:54:55 +00001718 }
1719
Chris Lattner302116a2007-01-31 04:40:28 +00001720 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001721 if (C->isNullValue()) {
1722 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001723 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1724 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001725 isNull = false;
1726 break;
1727 }
1728 if (isNull) {
Christopher Lambedf07882007-12-17 01:12:55 +00001729 const PointerType *Ptr = cast<PointerType>(C->getType());
1730 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
David Greenec656cbb2007-09-04 15:46:09 +00001731 (Value**)Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001732 (Value**)Idxs+NumIdx);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001733 assert(Ty != 0 && "Invalid indices for GEP!");
Owen Andersonb292b8c2009-07-30 23:03:37 +00001734 return ConstantPointerNull::get(
Owen Anderson4056ca92009-07-29 22:17:13 +00001735 PointerType::get(Ty,Ptr->getAddressSpace()));
Chris Lattner04b60fe2004-02-16 20:46:13 +00001736 }
1737 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001738
Nick Lewyckye0332982009-09-20 01:35:59 +00001739 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001740 // Combine Indices - If the source pointer to this getelementptr instruction
1741 // is a getelementptr instruction, combine the indices of the two
1742 // getelementptr instructions into a single instruction.
1743 //
1744 if (CE->getOpcode() == Instruction::GetElementPtr) {
1745 const Type *LastTy = 0;
1746 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1747 I != E; ++I)
1748 LastTy = *I;
1749
Chris Lattner13128ab2004-10-11 22:52:25 +00001750 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001751 SmallVector<Value*, 16> NewIndices;
1752 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001753 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001754 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001755
1756 // Add the last index of the source with the first index of the new GEP.
1757 // Make sure to handle the case when they are actually different types.
1758 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001759 // Otherwise it must be an array.
1760 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001761 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001762 if (IdxTy != Idx0->getType()) {
Owen Anderson53a52212009-07-13 04:09:18 +00001763 Constant *C1 =
Owen Anderson55f1c092009-08-13 21:58:54 +00001764 ConstantExpr::getSExtOrBitCast(Idx0, Type::getInt64Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001765 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Owen Anderson55f1c092009-08-13 21:58:54 +00001766 Type::getInt64Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001767 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
Reid Spencer1a063892006-12-04 02:46:44 +00001768 } else {
1769 Combined =
Owen Anderson487375e2009-07-29 18:55:55 +00001770 ConstantExpr::get(Instruction::Add, Idx0, Combined);
Reid Spencer1a063892006-12-04 02:46:44 +00001771 }
Chris Lattner71068a02004-07-07 04:45:13 +00001772 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001773
Chris Lattner1dd054c2004-01-12 22:07:24 +00001774 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001775 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
Dan Gohman21c62162009-09-11 00:04:14 +00001776 return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ?
1777 ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0),
1778 &NewIndices[0],
1779 NewIndices.size()) :
1780 ConstantExpr::getGetElementPtr(CE->getOperand(0),
1781 &NewIndices[0],
1782 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001783 }
1784 }
1785
1786 // Implement folding of:
1787 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1788 // long 0, long 0)
1789 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1790 //
Chris Lattneraadc7782007-08-13 17:09:08 +00001791 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001792 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001793 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1794 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1795 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001796 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001797 if (CAT->getElementType() == SAT->getElementType())
Dan Gohman21c62162009-09-11 00:04:14 +00001798 return inBounds ?
1799 ConstantExpr::getInBoundsGetElementPtr(
1800 (Constant*)CE->getOperand(0), Idxs, NumIdx) :
1801 ConstantExpr::getGetElementPtr(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001802 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattneraadc7782007-08-13 17:09:08 +00001803 }
Dan Gohman062d3782009-08-29 23:35:16 +00001804
Chris Lattneraadc7782007-08-13 17:09:08 +00001805 // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1)
1806 // Into: inttoptr (i64 0 to i8*)
1807 // This happens with pointers to member functions in C++.
1808 if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 &&
1809 isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) &&
Owen Anderson55f1c092009-08-13 21:58:54 +00001810 cast<PointerType>(CE->getType())->getElementType() == Type::getInt8Ty(Context)) {
Chris Lattneraadc7782007-08-13 17:09:08 +00001811 Constant *Base = CE->getOperand(0);
1812 Constant *Offset = Idxs[0];
Dan Gohman062d3782009-08-29 23:35:16 +00001813
Chris Lattneraadc7782007-08-13 17:09:08 +00001814 // Convert the smaller integer to the larger type.
1815 if (Offset->getType()->getPrimitiveSizeInBits() <
1816 Base->getType()->getPrimitiveSizeInBits())
Owen Anderson487375e2009-07-29 18:55:55 +00001817 Offset = ConstantExpr::getSExt(Offset, Base->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001818 else if (Base->getType()->getPrimitiveSizeInBits() <
1819 Offset->getType()->getPrimitiveSizeInBits())
Owen Anderson487375e2009-07-29 18:55:55 +00001820 Base = ConstantExpr::getZExt(Base, Offset->getType());
Dan Gohman062d3782009-08-29 23:35:16 +00001821
Owen Anderson487375e2009-07-29 18:55:55 +00001822 Base = ConstantExpr::getAdd(Base, Offset);
1823 return ConstantExpr::getIntToPtr(Base, CE->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001824 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001825 }
Dan Gohman21c62162009-09-11 00:04:14 +00001826
1827 // Check to see if any array indices are not within the corresponding
1828 // notional array bounds. If so, try to determine if they can be factored
1829 // out into preceding dimensions.
1830 bool Unknown = false;
1831 SmallVector<Constant *, 8> NewIdxs;
1832 const Type *Ty = C->getType();
1833 const Type *Prev = 0;
1834 for (unsigned i = 0; i != NumIdx;
1835 Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
1836 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
1837 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
1838 if (ATy->getNumElements() <= INT64_MAX &&
1839 ATy->getNumElements() != 0 &&
1840 CI->getSExtValue() >= (int64_t)ATy->getNumElements()) {
1841 if (isa<SequentialType>(Prev)) {
1842 // It's out of range, but we can factor it into the prior
1843 // dimension.
1844 NewIdxs.resize(NumIdx);
1845 ConstantInt *Factor = ConstantInt::get(CI->getType(),
1846 ATy->getNumElements());
1847 NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
1848
1849 Constant *PrevIdx = Idxs[i-1];
1850 Constant *Div = ConstantExpr::getSDiv(CI, Factor);
1851
1852 // Before adding, extend both operands to i64 to avoid
1853 // overflow trouble.
1854 if (PrevIdx->getType() != Type::getInt64Ty(Context))
1855 PrevIdx = ConstantExpr::getSExt(PrevIdx,
1856 Type::getInt64Ty(Context));
1857 if (Div->getType() != Type::getInt64Ty(Context))
1858 Div = ConstantExpr::getSExt(Div,
1859 Type::getInt64Ty(Context));
1860
1861 NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
1862 } else {
1863 // It's out of range, but the prior dimension is a struct
1864 // so we can't do anything about it.
1865 Unknown = true;
1866 }
1867 }
1868 } else {
1869 // We don't know if it's in range or not.
1870 Unknown = true;
1871 }
1872 }
1873
1874 // If we did any factoring, start over with the adjusted indices.
1875 if (!NewIdxs.empty()) {
1876 for (unsigned i = 0; i != NumIdx; ++i)
1877 if (!NewIdxs[i]) NewIdxs[i] = Idxs[i];
1878 return inBounds ?
Nick Lewyckye0332982009-09-20 01:35:59 +00001879 ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(),
1880 NewIdxs.size()) :
1881 ConstantExpr::getGetElementPtr(C, NewIdxs.data(), NewIdxs.size());
Dan Gohman21c62162009-09-11 00:04:14 +00001882 }
1883
1884 // If all indices are known integers and normalized, we can do a simple
1885 // check for the "inbounds" property.
1886 if (!Unknown && !inBounds &&
1887 isa<GlobalVariable>(C) && isInBoundsIndices(Idxs, NumIdx))
Nick Lewyckye0332982009-09-20 01:35:59 +00001888 return ConstantExpr::getInBoundsGetElementPtr(C, Idxs, NumIdx);
Dan Gohman21c62162009-09-11 00:04:14 +00001889
Chris Lattner1dd054c2004-01-12 22:07:24 +00001890 return 0;
1891}