blob: 8c65b56357029cfb9ea1be43075448fcc955c254 [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
Chris Lattner334d33c2008-04-19 21:58:19 +0000707 break;
708 case Instruction::AShr:
709 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
Nick Lewyckye0332982009-09-20 01:35:59 +0000710 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
Chris Lattner6d94bb72007-03-25 05:47:04 +0000711 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
Nick Lewyckye0332982009-09-20 01:35:59 +0000712 return ConstantExpr::getLShr(C1, C2);
Chris Lattner334d33c2008-04-19 21:58:19 +0000713 break;
Reid Spencer266e42b2006-12-23 06:05:41 +0000714 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000715 }
Dan Gohman062d3782009-08-29 23:35:16 +0000716
Chris Lattner6b056052008-04-20 18:24:14 +0000717 // At this point we know neither constant is an UndefValue.
Nick Lewyckye0332982009-09-20 01:35:59 +0000718 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
719 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000720 using namespace APIntOps;
Chris Lattner6b056052008-04-20 18:24:14 +0000721 const APInt &C1V = CI1->getValue();
722 const APInt &C2V = CI2->getValue();
Chris Lattner344da522007-01-12 18:42:52 +0000723 switch (Opcode) {
724 default:
725 break;
726 case Instruction::Add:
Owen Andersonedb4a702009-07-24 23:12:02 +0000727 return ConstantInt::get(Context, C1V + C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000728 case Instruction::Sub:
Owen Andersonedb4a702009-07-24 23:12:02 +0000729 return ConstantInt::get(Context, C1V - C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000730 case Instruction::Mul:
Owen Andersonedb4a702009-07-24 23:12:02 +0000731 return ConstantInt::get(Context, C1V * C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000732 case Instruction::UDiv:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000733 assert(!CI2->isNullValue() && "Div by zero handled above");
Owen Andersonedb4a702009-07-24 23:12:02 +0000734 return ConstantInt::get(Context, C1V.udiv(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000735 case Instruction::SDiv:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000736 assert(!CI2->isNullValue() && "Div by zero handled above");
Reid Spencer81658a82007-02-27 06:23:51 +0000737 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000738 return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef
Owen Andersonedb4a702009-07-24 23:12:02 +0000739 return ConstantInt::get(Context, C1V.sdiv(C2V));
Reid Spencer81658a82007-02-27 06:23:51 +0000740 case Instruction::URem:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000741 assert(!CI2->isNullValue() && "Div by zero handled above");
Owen Andersonedb4a702009-07-24 23:12:02 +0000742 return ConstantInt::get(Context, C1V.urem(C2V));
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000743 case Instruction::SRem:
744 assert(!CI2->isNullValue() && "Div by zero handled above");
Reid Spencer81658a82007-02-27 06:23:51 +0000745 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000746 return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef
Owen Andersonedb4a702009-07-24 23:12:02 +0000747 return ConstantInt::get(Context, C1V.srem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000748 case Instruction::And:
Owen Andersonedb4a702009-07-24 23:12:02 +0000749 return ConstantInt::get(Context, C1V & C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000750 case Instruction::Or:
Owen Andersonedb4a702009-07-24 23:12:02 +0000751 return ConstantInt::get(Context, C1V | C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000752 case Instruction::Xor:
Owen Andersonedb4a702009-07-24 23:12:02 +0000753 return ConstantInt::get(Context, C1V ^ C2V);
Chris Lattner6b056052008-04-20 18:24:14 +0000754 case Instruction::Shl: {
755 uint32_t shiftAmt = C2V.getZExtValue();
756 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000757 return ConstantInt::get(Context, C1V.shl(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000758 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000759 return UndefValue::get(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000760 }
761 case Instruction::LShr: {
762 uint32_t shiftAmt = C2V.getZExtValue();
763 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000764 return ConstantInt::get(Context, C1V.lshr(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000765 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000766 return UndefValue::get(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000767 }
768 case Instruction::AShr: {
769 uint32_t shiftAmt = C2V.getZExtValue();
770 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000771 return ConstantInt::get(Context, C1V.ashr(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 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000775 }
776 }
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000777
778 switch (Opcode) {
779 case Instruction::SDiv:
780 case Instruction::UDiv:
781 case Instruction::URem:
782 case Instruction::SRem:
783 case Instruction::LShr:
784 case Instruction::AShr:
785 case Instruction::Shl:
Nick Lewyckye0332982009-09-20 01:35:59 +0000786 if (CI1->equalsInt(0)) return C1;
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000787 break;
788 default:
789 break;
790 }
Nick Lewyckye0332982009-09-20 01:35:59 +0000791 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
792 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000793 APFloat C1V = CFP1->getValueAPF();
794 APFloat C2V = CFP2->getValueAPF();
795 APFloat C3V = C1V; // copy for modification
Reid Spencer266e42b2006-12-23 06:05:41 +0000796 switch (Opcode) {
797 default:
798 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000799 case Instruction::FAdd:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000800 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000801 return ConstantFP::get(Context, C3V);
Dan Gohmana5b96452009-06-04 22:49:04 +0000802 case Instruction::FSub:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000803 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000804 return ConstantFP::get(Context, C3V);
Dan Gohmana5b96452009-06-04 22:49:04 +0000805 case Instruction::FMul:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000806 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000807 return ConstantFP::get(Context, C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000808 case Instruction::FDiv:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000809 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000810 return ConstantFP::get(Context, C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000811 case Instruction::FRem:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000812 (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000813 return ConstantFP::get(Context, C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000814 }
815 }
Dan Gohman9f396602007-10-30 19:00:49 +0000816 } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
Nick Lewyckye0332982009-09-20 01:35:59 +0000817 ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
818 ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000819 if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
820 (CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
Owen Anderson85f86dc2009-07-13 22:41:06 +0000821 std::vector<Constant*> Res;
822 const Type* EltTy = VTy->getElementType();
Nick Lewyckye0332982009-09-20 01:35:59 +0000823 Constant *C1 = 0;
824 Constant *C2 = 0;
Reid Spencer266e42b2006-12-23 06:05:41 +0000825 switch (Opcode) {
Chris Lattner6072ead2008-04-19 21:13:00 +0000826 default:
827 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000828 case Instruction::Add:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000829 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000830 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
831 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000832 Res.push_back(ConstantExpr::getAdd(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000833 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000834 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000835 case Instruction::FAdd:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000836 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000837 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
838 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Nick Lewyckye0332982009-09-20 01:35:59 +0000839 Res.push_back(ConstantExpr::getFAdd(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000840 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000841 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000842 case Instruction::Sub:
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::getSub(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::FSub:
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::getFSub(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::Mul:
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::getMul(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::FMul:
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::getFMul(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000868 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000869 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000870 case Instruction::UDiv:
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::getUDiv(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000875 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000876 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000877 case Instruction::SDiv:
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::getSDiv(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::FDiv:
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::getFDiv(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::URem:
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::getURem(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::SRem:
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::getSRem(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::FRem:
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::getFRem(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::And:
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::getAnd(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000917 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000918 return ConstantVector::get(Res);
Owen Anderson85f86dc2009-07-13 22:41:06 +0000919 case Instruction::Or:
920 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::getOr(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000924 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000925 return ConstantVector::get(Res);
Owen Anderson85f86dc2009-07-13 22:41:06 +0000926 case Instruction::Xor:
927 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::getXor(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000931 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000932 return ConstantVector::get(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000933 case Instruction::LShr:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000934 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::getLShr(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000938 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000939 return ConstantVector::get(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000940 case Instruction::AShr:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000941 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::getAShr(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::Shl:
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::getShl(C1, C2));
Owen Anderson85f86dc2009-07-13 22:41:06 +0000952 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000953 return ConstantVector::get(Res);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000954 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000955 }
956 }
957
Chris Lattner6b056052008-04-20 18:24:14 +0000958 if (isa<ConstantExpr>(C1)) {
959 // There are many possible foldings we could do here. We should probably
960 // at least fold add of a pointer with an integer into the appropriate
961 // getelementptr. This will improve alias analysis a bit.
962 } else if (isa<ConstantExpr>(C2)) {
963 // If C2 is a constant expr and C1 isn't, flop them around and fold the
964 // other way if possible.
965 switch (Opcode) {
966 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000967 case Instruction::FAdd:
Chris Lattner6b056052008-04-20 18:24:14 +0000968 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000969 case Instruction::FMul:
Chris Lattner6b056052008-04-20 18:24:14 +0000970 case Instruction::And:
971 case Instruction::Or:
972 case Instruction::Xor:
973 // No change of opcode required.
Owen Anderson53a52212009-07-13 04:09:18 +0000974 return ConstantFoldBinaryInstruction(Context, Opcode, C2, C1);
Dan Gohman062d3782009-08-29 23:35:16 +0000975
Chris Lattner6b056052008-04-20 18:24:14 +0000976 case Instruction::Shl:
977 case Instruction::LShr:
978 case Instruction::AShr:
979 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +0000980 case Instruction::FSub:
Chris Lattner6b056052008-04-20 18:24:14 +0000981 case Instruction::SDiv:
982 case Instruction::UDiv:
983 case Instruction::FDiv:
984 case Instruction::URem:
985 case Instruction::SRem:
986 case Instruction::FRem:
987 default: // These instructions cannot be flopped around.
988 break;
989 }
990 }
Dan Gohman062d3782009-08-29 23:35:16 +0000991
Nick Lewycky605109d2009-09-20 00:04:02 +0000992 // i1 can be simplified in many cases.
993 if (C1->getType() == Type::getInt1Ty(Context)) {
994 switch (Opcode) {
995 case Instruction::Add:
996 case Instruction::Sub:
Nick Lewyckye0332982009-09-20 01:35:59 +0000997 return ConstantExpr::getXor(C1, C2);
Nick Lewycky605109d2009-09-20 00:04:02 +0000998 case Instruction::Mul:
Nick Lewyckye0332982009-09-20 01:35:59 +0000999 return ConstantExpr::getAnd(C1, C2);
Nick Lewycky605109d2009-09-20 00:04:02 +00001000 case Instruction::Shl:
1001 case Instruction::LShr:
1002 case Instruction::AShr:
1003 // We can assume that C2 == 0. If it were one the result would be
1004 // undefined because the shift value is as large as the bitwidth.
Nick Lewyckye0332982009-09-20 01:35:59 +00001005 return C1;
Nick Lewycky605109d2009-09-20 00:04:02 +00001006 case Instruction::SDiv:
1007 case Instruction::UDiv:
1008 // We can assume that C2 == 1. If it were zero the result would be
1009 // undefined through division by zero.
Nick Lewyckye0332982009-09-20 01:35:59 +00001010 return C1;
Nick Lewycky605109d2009-09-20 00:04:02 +00001011 case Instruction::URem:
1012 case Instruction::SRem:
1013 // We can assume that C2 == 1. If it were zero the result would be
1014 // undefined through division by zero.
1015 return ConstantInt::getFalse(Context);
1016 default:
1017 break;
1018 }
1019 }
1020
Chris Lattner6b056052008-04-20 18:24:14 +00001021 // We don't know how to fold this.
Reid Spencer266e42b2006-12-23 06:05:41 +00001022 return 0;
1023}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001024
Chris Lattner60c47262005-01-28 19:09:51 +00001025/// isZeroSizedType - This type is zero sized if its an array or structure of
1026/// zero sized types. The only leaf zero sized type is an empty structure.
1027static bool isMaybeZeroSizedType(const Type *Ty) {
1028 if (isa<OpaqueType>(Ty)) return true; // Can't say.
1029 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1030
1031 // If all of elements have zero size, this does too.
1032 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +00001033 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +00001034 return true;
1035
1036 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1037 return isMaybeZeroSizedType(ATy->getElementType());
1038 }
1039 return false;
1040}
Chris Lattner6ea4b522004-03-12 05:53:32 +00001041
Chris Lattner061da2f2004-01-13 05:51:55 +00001042/// IdxCompare - Compare the two constants as though they were getelementptr
1043/// indices. This allows coersion of the types to be the same thing.
1044///
1045/// If the two constants are the "same" (after coersion), return 0. If the
1046/// first is less than the second, return -1, if the second is less than the
1047/// first, return 1. If the constants are not integral, return -2.
1048///
Owen Anderson53a52212009-07-13 04:09:18 +00001049static int IdxCompare(LLVMContext &Context, Constant *C1, Constant *C2,
1050 const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001051 if (C1 == C2) return 0;
1052
Reid Spencerc90cf772006-12-31 21:43:30 +00001053 // Ok, we found a different index. If they are not ConstantInt, we can't do
1054 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +00001055 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1056 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +00001057
Chris Lattner69193f92004-04-05 01:30:19 +00001058 // Ok, we have two differing integer indices. Sign extend them to be the same
1059 // type. Long is always big enough, so we use it.
Owen Anderson55f1c092009-08-13 21:58:54 +00001060 if (C1->getType() != Type::getInt64Ty(Context))
1061 C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(Context));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001062
Owen Anderson55f1c092009-08-13 21:58:54 +00001063 if (C2->getType() != Type::getInt64Ty(Context))
1064 C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(Context));
Reid Spencer8d9336d2006-12-31 05:26:44 +00001065
1066 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +00001067
Chris Lattner60c47262005-01-28 19:09:51 +00001068 // If the type being indexed over is really just a zero sized type, there is
1069 // no pointer difference being made here.
1070 if (isMaybeZeroSizedType(ElTy))
1071 return -2; // dunno.
1072
Chris Lattner061da2f2004-01-13 05:51:55 +00001073 // If they are really different, now that they are the same type, then we
1074 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +00001075 if (cast<ConstantInt>(C1)->getSExtValue() <
1076 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +00001077 return -1;
1078 else
1079 return 1;
1080}
1081
Chris Lattner858f4e92007-01-04 02:13:20 +00001082/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +00001083/// decide about the two constants provided. This doesn't need to handle simple
1084/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
1085/// If we can determine that the two constants have a particular relation to
1086/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001087/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1088/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +00001089///
1090/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +00001091/// operand is always the most "complex" of the two. We consider ConstantFP
1092/// to be the simplest, and ConstantExprs to be the most complex.
Owen Anderson53a52212009-07-13 04:09:18 +00001093static FCmpInst::Predicate evaluateFCmpRelation(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +00001094 Constant *V1, Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001095 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +00001096 "Cannot compare values of different types!");
Dale Johannesen19db0932007-10-14 01:56:47 +00001097
1098 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +00001099 if (V1->getType() == Type::getPPC_FP128Ty(Context))
Dale Johannesen19db0932007-10-14 01:56:47 +00001100 return FCmpInst::BAD_FCMP_PREDICATE;
1101
Reid Spencer9d36acf2006-12-24 18:52:08 +00001102 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +00001103 if (V1 == V2) return FCmpInst::FCMP_OEQ;
1104
Reid Spencer9d36acf2006-12-24 18:52:08 +00001105 if (!isa<ConstantExpr>(V1)) {
1106 if (!isa<ConstantExpr>(V2)) {
1107 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +00001108 ConstantInt *R = 0;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001109 R = dyn_cast<ConstantInt>(
Nick Lewyckye0332982009-09-20 01:35:59 +00001110 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001111 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001112 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001113 R = dyn_cast<ConstantInt>(
Nick Lewyckye0332982009-09-20 01:35:59 +00001114 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001115 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001116 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001117 R = dyn_cast<ConstantInt>(
Nick Lewyckye0332982009-09-20 01:35:59 +00001118 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001119 if (R && !R->isZero())
Reid Spencer9d36acf2006-12-24 18:52:08 +00001120 return FCmpInst::FCMP_OGT;
1121
1122 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +00001123 return FCmpInst::BAD_FCMP_PREDICATE;
1124 }
Dan Gohman062d3782009-08-29 23:35:16 +00001125
Reid Spencer9d36acf2006-12-24 18:52:08 +00001126 // If the first operand is simple and second is ConstantExpr, swap operands.
Owen Anderson53a52212009-07-13 04:09:18 +00001127 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(Context, V2, V1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001128 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1129 return FCmpInst::getSwappedPredicate(SwappedRelation);
1130 } else {
1131 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1132 // constantexpr or a simple constant.
Nick Lewyckye0332982009-09-20 01:35:59 +00001133 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001134 switch (CE1->getOpcode()) {
1135 case Instruction::FPTrunc:
1136 case Instruction::FPExt:
1137 case Instruction::UIToFP:
1138 case Instruction::SIToFP:
1139 // We might be able to do something with these but we don't right now.
1140 break;
1141 default:
1142 break;
1143 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001144 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001145 // There are MANY other foldings that we could perform here. They will
1146 // probably be added on demand, as they seem needed.
1147 return FCmpInst::BAD_FCMP_PREDICATE;
1148}
1149
1150/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +00001151/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +00001152/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +00001153/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +00001154/// particular relation to each other, we should return the corresponding ICmp
1155/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +00001156///
1157/// To simplify this code we canonicalize the relation so that the first
1158/// operand is always the most "complex" of the two. We consider simple
1159/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +00001160/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +00001161///
Owen Anderson53a52212009-07-13 04:09:18 +00001162static ICmpInst::Predicate evaluateICmpRelation(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +00001163 Constant *V1,
1164 Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +00001165 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001166 assert(V1->getType() == V2->getType() &&
1167 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001168 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001169
Reid Spenceraccd7c72004-07-17 23:47:01 +00001170 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001171 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1172 // We distilled this down to a simple case, use the standard constant
1173 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001174 ConstantInt *R = 0;
Reid Spencer266e42b2006-12-23 06:05:41 +00001175 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Nick Lewyckye0332982009-09-20 01:35:59 +00001176 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001177 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001178 return pred;
1179 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Nick Lewyckye0332982009-09-20 01:35:59 +00001180 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001181 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001182 return pred;
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001183 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Nick Lewyckye0332982009-09-20 01:35:59 +00001184 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001185 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001186 return pred;
Dan Gohman062d3782009-08-29 23:35:16 +00001187
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001188 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +00001189 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001190 }
Dan Gohman062d3782009-08-29 23:35:16 +00001191
Chris Lattner061da2f2004-01-13 05:51:55 +00001192 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +00001193 ICmpInst::Predicate SwappedRelation =
Owen Anderson53a52212009-07-13 04:09:18 +00001194 evaluateICmpRelation(Context, V2, V1, isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001195 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1196 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001197
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001198 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001199 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +00001200 ICmpInst::Predicate SwappedRelation =
Owen Anderson53a52212009-07-13 04:09:18 +00001201 evaluateICmpRelation(Context, V2, V1, isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001202 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1203 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001204 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001205 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +00001206 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001207
Reid Spenceraccd7c72004-07-17 23:47:01 +00001208 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001209 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001210 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner52fe8692007-09-10 23:42:42 +00001211 // Don't try to decide equality of aliases.
1212 if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2))
1213 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
1214 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001215 } else {
1216 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Chris Lattner52fe8692007-09-10 23:42:42 +00001217 // GlobalVals can never be null. Don't try to evaluate aliases.
1218 if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1))
Reid Spencer266e42b2006-12-23 06:05:41 +00001219 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001220 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001221 } else {
1222 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1223 // constantexpr, a CPR, or a simple constant.
Nick Lewyckye0332982009-09-20 01:35:59 +00001224 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1225 Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +00001226
1227 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001228 case Instruction::Trunc:
1229 case Instruction::FPTrunc:
1230 case Instruction::FPExt:
1231 case Instruction::FPToUI:
1232 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +00001233 break; // We can't evaluate floating point casts or truncations.
1234
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001235 case Instruction::UIToFP:
1236 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001237 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +00001238 case Instruction::ZExt:
1239 case Instruction::SExt:
Chris Lattner061da2f2004-01-13 05:51:55 +00001240 // If the cast is not actually changing bits, and the second operand is a
1241 // null pointer, do the comparison with the pre-casted value.
1242 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +00001243 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Chris Lattnerd2265b42007-12-10 22:53:04 +00001244 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1245 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
Owen Anderson53a52212009-07-13 04:09:18 +00001246 return evaluateICmpRelation(Context, CE1Op0,
Owen Anderson5a1acd92009-07-31 20:28:14 +00001247 Constant::getNullValue(CE1Op0->getType()),
Nick Lewycky2949a232009-09-20 03:48:46 +00001248 isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001249 }
Chris Lattner192e3262004-04-11 01:29:30 +00001250 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001251
1252 case Instruction::GetElementPtr:
1253 // Ok, since this is a getelementptr, we know that the constant has a
1254 // pointer type. Check the various cases.
1255 if (isa<ConstantPointerNull>(V2)) {
1256 // If we are comparing a GEP to a null pointer, check to see if the base
1257 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +00001258 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001259 if (GV->hasExternalWeakLinkage())
1260 // Weak linkage GVals could be zero or not. We're comparing that
1261 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001262 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +00001263 else
1264 // If its not weak linkage, the GVal must have a non-zero address
1265 // so the result is greater-than
Nick Lewycky9e085542009-09-20 04:27:06 +00001266 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001267 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1268 // If we are indexing from a null pointer, check to see if we have any
1269 // non-zero indices.
1270 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1271 if (!CE1->getOperand(i)->isNullValue())
1272 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001273 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001274 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +00001275 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001276 }
1277 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001278 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001279 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001280 if (CPR2->hasExternalWeakLinkage())
1281 // Weak linkage GVals could be zero or not. We're comparing it to
1282 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001283 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +00001284 else
1285 // If its not weak linkage, the GVal must have a non-zero address
1286 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +00001287 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001288 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001289 if (CPR1 == CPR2) {
1290 // If this is a getelementptr of the same global, then it must be
1291 // different. Because the types must match, the getelementptr could
1292 // only have at most one index, and because we fold getelementptr's
1293 // with a single zero index, it must be nonzero.
1294 assert(CE1->getNumOperands() == 2 &&
1295 !CE1->getOperand(1)->isNullValue() &&
1296 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001297 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001298 } else {
1299 // If they are different globals, we don't know what the value is,
1300 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001301 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001302 }
1303 }
1304 } else {
Nick Lewyckye0332982009-09-20 01:35:59 +00001305 ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1306 Constant *CE2Op0 = CE2->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +00001307
1308 // There are MANY other foldings that we could perform here. They will
1309 // probably be added on demand, as they seem needed.
1310 switch (CE2->getOpcode()) {
1311 default: break;
1312 case Instruction::GetElementPtr:
1313 // By far the most common case to handle is when the base pointers are
1314 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001315 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001316 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001317 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001318 // Ok, we know that both getelementptr instructions are based on the
1319 // same global. From this, we can precisely determine the relative
1320 // ordering of the resultant pointers.
1321 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001322
Dan Gohman7190d482009-09-10 23:37:55 +00001323 // The logic below assumes that the result of the comparison
1324 // can be determined by finding the first index that differs.
1325 // This doesn't work if there is over-indexing in any
1326 // subsequent indices, so check for that case first.
1327 if (!CE1->isGEPWithNoNotionalOverIndexing() ||
1328 !CE2->isGEPWithNoNotionalOverIndexing())
1329 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1330
Chris Lattner061da2f2004-01-13 05:51:55 +00001331 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001332 gep_type_iterator GTI = gep_type_begin(CE1);
1333 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1334 ++i, ++GTI)
Owen Anderson53a52212009-07-13 04:09:18 +00001335 switch (IdxCompare(Context, CE1->getOperand(i),
1336 CE2->getOperand(i), GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001337 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1338 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1339 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001340 }
1341
1342 // Ok, we ran out of things they have in common. If any leftovers
1343 // are non-zero then we have a difference, otherwise we are equal.
1344 for (; i < CE1->getNumOperands(); ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001345 if (!CE1->getOperand(i)->isNullValue()) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001346 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001347 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +00001348 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001349 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001350 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001351
Chris Lattner061da2f2004-01-13 05:51:55 +00001352 for (; i < CE2->getNumOperands(); ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001353 if (!CE2->getOperand(i)->isNullValue()) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001354 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001355 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001356 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001357 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001358 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001359 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001360 }
1361 }
1362 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001363 default:
1364 break;
1365 }
1366 }
1367
Reid Spencer266e42b2006-12-23 06:05:41 +00001368 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001369}
1370
Owen Anderson53a52212009-07-13 04:09:18 +00001371Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context,
1372 unsigned short pred,
Nick Lewyckye0332982009-09-20 01:35:59 +00001373 Constant *C1, Constant *C2) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001374 const Type *ResultTy;
1375 if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
Owen Anderson55f1c092009-08-13 21:58:54 +00001376 ResultTy = VectorType::get(Type::getInt1Ty(Context), VT->getNumElements());
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001377 else
Owen Anderson55f1c092009-08-13 21:58:54 +00001378 ResultTy = Type::getInt1Ty(Context);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001379
Chris Lattnerd137a082008-07-08 05:46:34 +00001380 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001381 if (pred == FCmpInst::FCMP_FALSE)
Owen Anderson5a1acd92009-07-31 20:28:14 +00001382 return Constant::getNullValue(ResultTy);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001383
1384 if (pred == FCmpInst::FCMP_TRUE)
Owen Anderson5a1acd92009-07-31 20:28:14 +00001385 return Constant::getAllOnesValue(ResultTy);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001386
Reid Spencer266e42b2006-12-23 06:05:41 +00001387 // Handle some degenerate cases first
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001388 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Owen Andersonb292b8c2009-07-30 23:03:37 +00001389 return UndefValue::get(ResultTy);
Reid Spencer266e42b2006-12-23 06:05:41 +00001390
Dale Johannesen19db0932007-10-14 01:56:47 +00001391 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +00001392 if (C1->getType() == Type::getPPC_FP128Ty(Context))
Dale Johannesen19db0932007-10-14 01:56:47 +00001393 return 0;
1394
Reid Spencer266e42b2006-12-23 06:05:41 +00001395 // icmp eq/ne(null,GV) -> false/true
1396 if (C1->isNullValue()) {
1397 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001398 // Don't try to evaluate aliases. External weak GV can be null.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001399 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001400 if (pred == ICmpInst::ICMP_EQ)
Owen Anderson23a204d2009-07-31 17:39:07 +00001401 return ConstantInt::getFalse(Context);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001402 else if (pred == ICmpInst::ICMP_NE)
Owen Anderson23a204d2009-07-31 17:39:07 +00001403 return ConstantInt::getTrue(Context);
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001404 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001405 // icmp eq/ne(GV,null) -> false/true
1406 } else if (C2->isNullValue()) {
1407 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001408 // Don't try to evaluate aliases. External weak GV can be null.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001409 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001410 if (pred == ICmpInst::ICMP_EQ)
Owen Anderson23a204d2009-07-31 17:39:07 +00001411 return ConstantInt::getFalse(Context);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001412 else if (pred == ICmpInst::ICMP_NE)
Owen Anderson23a204d2009-07-31 17:39:07 +00001413 return ConstantInt::getTrue(Context);
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001414 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001415 }
1416
Chris Lattner344da522007-01-12 18:42:52 +00001417 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +00001418 APInt V1 = cast<ConstantInt>(C1)->getValue();
1419 APInt V2 = cast<ConstantInt>(C2)->getValue();
1420 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001421 default: llvm_unreachable("Invalid ICmp Predicate"); return 0;
Owen Anderson53a52212009-07-13 04:09:18 +00001422 case ICmpInst::ICMP_EQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001423 return ConstantInt::get(Type::getInt1Ty(Context), V1 == V2);
Owen Anderson53a52212009-07-13 04:09:18 +00001424 case ICmpInst::ICMP_NE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001425 return ConstantInt::get(Type::getInt1Ty(Context), V1 != V2);
Owen Anderson53a52212009-07-13 04:09:18 +00001426 case ICmpInst::ICMP_SLT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001427 return ConstantInt::get(Type::getInt1Ty(Context), V1.slt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001428 case ICmpInst::ICMP_SGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001429 return ConstantInt::get(Type::getInt1Ty(Context), V1.sgt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001430 case ICmpInst::ICMP_SLE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001431 return ConstantInt::get(Type::getInt1Ty(Context), V1.sle(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001432 case ICmpInst::ICMP_SGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001433 return ConstantInt::get(Type::getInt1Ty(Context), V1.sge(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001434 case ICmpInst::ICMP_ULT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001435 return ConstantInt::get(Type::getInt1Ty(Context), V1.ult(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001436 case ICmpInst::ICMP_UGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001437 return ConstantInt::get(Type::getInt1Ty(Context), V1.ugt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001438 case ICmpInst::ICMP_ULE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001439 return ConstantInt::get(Type::getInt1Ty(Context), V1.ule(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001440 case ICmpInst::ICMP_UGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001441 return ConstantInt::get(Type::getInt1Ty(Context), V1.uge(V2));
Chris Lattner061da2f2004-01-13 05:51:55 +00001442 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001443 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001444 APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
1445 APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
1446 APFloat::cmpResult R = C1V.compare(C2V);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001447 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001448 default: llvm_unreachable("Invalid FCmp Predicate"); return 0;
Owen Anderson23a204d2009-07-31 17:39:07 +00001449 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse(Context);
1450 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue(Context);
Reid Spencer266e42b2006-12-23 06:05:41 +00001451 case FCmpInst::FCMP_UNO:
Owen Anderson55f1c092009-08-13 21:58:54 +00001452 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered);
Reid Spencer74bd0362007-01-11 00:25:45 +00001453 case FCmpInst::FCMP_ORD:
Owen Anderson55f1c092009-08-13 21:58:54 +00001454 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpUnordered);
Reid Spencer266e42b2006-12-23 06:05:41 +00001455 case FCmpInst::FCMP_UEQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001456 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001457 R==APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001458 case FCmpInst::FCMP_OEQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001459 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpEqual);
Reid Spencer74bd0362007-01-11 00:25:45 +00001460 case FCmpInst::FCMP_UNE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001461 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001462 case FCmpInst::FCMP_ONE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001463 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001464 R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001465 case FCmpInst::FCMP_ULT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001466 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001467 R==APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001468 case FCmpInst::FCMP_OLT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001469 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan);
Reid Spencer266e42b2006-12-23 06:05:41 +00001470 case FCmpInst::FCMP_UGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001471 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001472 R==APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001473 case FCmpInst::FCMP_OGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001474 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001475 case FCmpInst::FCMP_ULE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001476 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001477 case FCmpInst::FCMP_OLE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001478 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001479 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001480 case FCmpInst::FCMP_UGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001481 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001482 case FCmpInst::FCMP_OGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001483 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpGreaterThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001484 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001485 }
Chris Lattner67136cf2008-07-10 00:29:28 +00001486 } else if (isa<VectorType>(C1->getType())) {
1487 SmallVector<Constant*, 16> C1Elts, C2Elts;
Owen Anderson53a52212009-07-13 04:09:18 +00001488 C1->getVectorElements(Context, C1Elts);
1489 C2->getVectorElements(Context, C2Elts);
Dan Gohman062d3782009-08-29 23:35:16 +00001490
Chris Lattner67136cf2008-07-10 00:29:28 +00001491 // If we can constant fold the comparison of each element, constant fold
1492 // the whole vector comparison.
1493 SmallVector<Constant*, 4> ResElts;
Chris Lattner67136cf2008-07-10 00:29:28 +00001494 for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) {
1495 // Compare the elements, producing an i1 result or constant expr.
Owen Anderson53a52212009-07-13 04:09:18 +00001496 ResElts.push_back(
Owen Anderson487375e2009-07-29 18:55:55 +00001497 ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i]));
Reid Spencer266e42b2006-12-23 06:05:41 +00001498 }
Owen Anderson4aa32952009-07-28 21:19:26 +00001499 return ConstantVector::get(&ResElts[0], ResElts.size());
Reid Spencer266e42b2006-12-23 06:05:41 +00001500 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001501
Reid Spencer9d36acf2006-12-24 18:52:08 +00001502 if (C1->getType()->isFloatingPoint()) {
Chris Lattner350e4172008-07-08 18:47:38 +00001503 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
Owen Anderson53a52212009-07-13 04:09:18 +00001504 switch (evaluateFCmpRelation(Context, C1, C2)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001505 default: llvm_unreachable("Unknown relation!");
Reid Spencer9d36acf2006-12-24 18:52:08 +00001506 case FCmpInst::FCMP_UNO:
1507 case FCmpInst::FCMP_ORD:
1508 case FCmpInst::FCMP_UEQ:
1509 case FCmpInst::FCMP_UNE:
1510 case FCmpInst::FCMP_ULT:
1511 case FCmpInst::FCMP_UGT:
1512 case FCmpInst::FCMP_ULE:
1513 case FCmpInst::FCMP_UGE:
1514 case FCmpInst::FCMP_TRUE:
1515 case FCmpInst::FCMP_FALSE:
1516 case FCmpInst::BAD_FCMP_PREDICATE:
1517 break; // Couldn't determine anything about these constants.
1518 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001519 Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1520 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1521 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1522 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001523 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001524 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1525 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1526 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1527 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001528 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001529 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1530 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1531 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1532 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001533 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1534 // We can only partially decide this relation.
1535 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Chris Lattnerd137a082008-07-08 05:46:34 +00001536 Result = 0;
1537 else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
1538 Result = 1;
Chris Lattner061da2f2004-01-13 05:51:55 +00001539 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001540 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1541 // We can only partially decide this relation.
1542 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Chris Lattnerd137a082008-07-08 05:46:34 +00001543 Result = 0;
1544 else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
1545 Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001546 break;
1547 case ICmpInst::ICMP_NE: // We know that C1 != C2
1548 // We can only partially decide this relation.
1549 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Chris Lattnerd137a082008-07-08 05:46:34 +00001550 Result = 0;
1551 else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
1552 Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001553 break;
1554 }
Dan Gohman062d3782009-08-29 23:35:16 +00001555
Chris Lattnerd137a082008-07-08 05:46:34 +00001556 // If we evaluated the result, return it now.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001557 if (Result != -1)
Owen Anderson55f1c092009-08-13 21:58:54 +00001558 return ConstantInt::get(Type::getInt1Ty(Context), Result);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001559
Reid Spencer9d36acf2006-12-24 18:52:08 +00001560 } else {
1561 // Evaluate the relation between the two constants, per the predicate.
Chris Lattnerd137a082008-07-08 05:46:34 +00001562 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
Owen Anderson53a52212009-07-13 04:09:18 +00001563 switch (evaluateICmpRelation(Context, C1, C2, CmpInst::isSigned(pred))) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001564 default: llvm_unreachable("Unknown relational!");
Reid Spencer9d36acf2006-12-24 18:52:08 +00001565 case ICmpInst::BAD_ICMP_PREDICATE:
1566 break; // Couldn't determine anything about these constants.
1567 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1568 // If we know the constants are equal, we can decide the result of this
1569 // computation precisely.
Nick Lewycky9e085542009-09-20 04:27:06 +00001570 Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
Chris Lattnerd137a082008-07-08 05:46:34 +00001571 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001572 case ICmpInst::ICMP_ULT:
Nick Lewycky9e085542009-09-20 04:27:06 +00001573 switch (pred) {
1574 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001575 Result = 1; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001576 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001577 Result = 0; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001578 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001579 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001580 case ICmpInst::ICMP_SLT:
Nick Lewycky9e085542009-09-20 04:27:06 +00001581 switch (pred) {
1582 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001583 Result = 1; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001584 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001585 Result = 0; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001586 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001587 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001588 case ICmpInst::ICMP_UGT:
Nick Lewycky9e085542009-09-20 04:27:06 +00001589 switch (pred) {
1590 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001591 Result = 1; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001592 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001593 Result = 0; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001594 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001595 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001596 case ICmpInst::ICMP_SGT:
Nick Lewycky9e085542009-09-20 04:27:06 +00001597 switch (pred) {
1598 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001599 Result = 1; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001600 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
Nick Lewycky2b31b532009-09-20 05:47:45 +00001601 Result = 0; break;
Nick Lewycky9e085542009-09-20 04:27:06 +00001602 }
Chris Lattnerd137a082008-07-08 05:46:34 +00001603 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001604 case ICmpInst::ICMP_ULE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001605 if (pred == ICmpInst::ICMP_UGT) Result = 0;
Nick Lewycky9e085542009-09-20 04:27:06 +00001606 if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001607 break;
1608 case ICmpInst::ICMP_SLE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001609 if (pred == ICmpInst::ICMP_SGT) Result = 0;
Nick Lewycky9e085542009-09-20 04:27:06 +00001610 if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001611 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001612 case ICmpInst::ICMP_UGE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001613 if (pred == ICmpInst::ICMP_ULT) Result = 0;
Nick Lewycky9e085542009-09-20 04:27:06 +00001614 if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001615 break;
1616 case ICmpInst::ICMP_SGE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001617 if (pred == ICmpInst::ICMP_SLT) Result = 0;
Nick Lewycky9e085542009-09-20 04:27:06 +00001618 if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001619 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001620 case ICmpInst::ICMP_NE:
Chris Lattnerd137a082008-07-08 05:46:34 +00001621 if (pred == ICmpInst::ICMP_EQ) Result = 0;
1622 if (pred == ICmpInst::ICMP_NE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001623 break;
1624 }
Dan Gohman062d3782009-08-29 23:35:16 +00001625
Chris Lattnerd137a082008-07-08 05:46:34 +00001626 // If we evaluated the result, return it now.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001627 if (Result != -1)
Owen Anderson55f1c092009-08-13 21:58:54 +00001628 return ConstantInt::get(Type::getInt1Ty(Context), Result);
Dan Gohman062d3782009-08-29 23:35:16 +00001629
Reid Spencer9d36acf2006-12-24 18:52:08 +00001630 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001631 // If C2 is a constant expr and C1 isn't, flip them around and fold the
Reid Spencer9d36acf2006-12-24 18:52:08 +00001632 // other way if possible.
1633 switch (pred) {
1634 case ICmpInst::ICMP_EQ:
1635 case ICmpInst::ICMP_NE:
1636 // No change of predicate required.
Owen Anderson53a52212009-07-13 04:09:18 +00001637 return ConstantFoldCompareInstruction(Context, pred, C2, C1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001638
1639 case ICmpInst::ICMP_ULT:
1640 case ICmpInst::ICMP_SLT:
1641 case ICmpInst::ICMP_UGT:
1642 case ICmpInst::ICMP_SGT:
1643 case ICmpInst::ICMP_ULE:
1644 case ICmpInst::ICMP_SLE:
1645 case ICmpInst::ICMP_UGE:
1646 case ICmpInst::ICMP_SGE:
1647 // Change the predicate as necessary to swap the operands.
1648 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
Owen Anderson53a52212009-07-13 04:09:18 +00001649 return ConstantFoldCompareInstruction(Context, pred, C2, C1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001650
1651 default: // These predicates cannot be flopped around.
1652 break;
1653 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001654 }
1655 }
1656 return 0;
Dan Gohman062d3782009-08-29 23:35:16 +00001657}
Chris Lattner1dd054c2004-01-12 22:07:24 +00001658
Dan Gohman21c62162009-09-11 00:04:14 +00001659/// isInBoundsIndices - Test whether the given sequence of *normalized* indices
1660/// is "inbounds".
1661static bool isInBoundsIndices(Constant *const *Idxs, size_t NumIdx) {
1662 // No indices means nothing that could be out of bounds.
1663 if (NumIdx == 0) return true;
1664
1665 // If the first index is zero, it's in bounds.
1666 if (Idxs[0]->isNullValue()) return true;
1667
1668 // If the first index is one and all the rest are zero, it's in bounds,
1669 // by the one-past-the-end rule.
1670 if (!cast<ConstantInt>(Idxs[0])->isOne())
1671 return false;
1672 for (unsigned i = 1, e = NumIdx; i != e; ++i)
1673 if (!Idxs[i]->isNullValue())
1674 return false;
1675 return true;
1676}
1677
Owen Anderson53a52212009-07-13 04:09:18 +00001678Constant *llvm::ConstantFoldGetElementPtr(LLVMContext &Context,
Nick Lewyckye0332982009-09-20 01:35:59 +00001679 Constant *C,
Dan Gohman21c62162009-09-11 00:04:14 +00001680 bool inBounds,
David Greenec656cbb2007-09-04 15:46:09 +00001681 Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001682 unsigned NumIdx) {
Chris Lattner302116a2007-01-31 04:40:28 +00001683 if (NumIdx == 0 ||
1684 (NumIdx == 1 && Idxs[0]->isNullValue()))
Nick Lewyckye0332982009-09-20 01:35:59 +00001685 return C;
Chris Lattner1dd054c2004-01-12 22:07:24 +00001686
Chris Lattnerf6013752004-10-17 21:54:55 +00001687 if (isa<UndefValue>(C)) {
Christopher Lambedf07882007-12-17 01:12:55 +00001688 const PointerType *Ptr = cast<PointerType>(C->getType());
1689 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
David Greenec656cbb2007-09-04 15:46:09 +00001690 (Value **)Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001691 (Value **)Idxs+NumIdx);
Chris Lattnerf6013752004-10-17 21:54:55 +00001692 assert(Ty != 0 && "Invalid indices for GEP!");
Owen Andersonb292b8c2009-07-30 23:03:37 +00001693 return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
Chris Lattnerf6013752004-10-17 21:54:55 +00001694 }
1695
Chris Lattner302116a2007-01-31 04:40:28 +00001696 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001697 if (C->isNullValue()) {
1698 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001699 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1700 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001701 isNull = false;
1702 break;
1703 }
1704 if (isNull) {
Christopher Lambedf07882007-12-17 01:12:55 +00001705 const PointerType *Ptr = cast<PointerType>(C->getType());
1706 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
David Greenec656cbb2007-09-04 15:46:09 +00001707 (Value**)Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001708 (Value**)Idxs+NumIdx);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001709 assert(Ty != 0 && "Invalid indices for GEP!");
Owen Andersonb292b8c2009-07-30 23:03:37 +00001710 return ConstantPointerNull::get(
Owen Anderson4056ca92009-07-29 22:17:13 +00001711 PointerType::get(Ty,Ptr->getAddressSpace()));
Chris Lattner04b60fe2004-02-16 20:46:13 +00001712 }
1713 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001714
Nick Lewyckye0332982009-09-20 01:35:59 +00001715 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Chris Lattner1dd054c2004-01-12 22:07:24 +00001716 // Combine Indices - If the source pointer to this getelementptr instruction
1717 // is a getelementptr instruction, combine the indices of the two
1718 // getelementptr instructions into a single instruction.
1719 //
1720 if (CE->getOpcode() == Instruction::GetElementPtr) {
1721 const Type *LastTy = 0;
1722 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1723 I != E; ++I)
1724 LastTy = *I;
1725
Chris Lattner13128ab2004-10-11 22:52:25 +00001726 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001727 SmallVector<Value*, 16> NewIndices;
1728 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001729 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001730 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001731
1732 // Add the last index of the source with the first index of the new GEP.
1733 // Make sure to handle the case when they are actually different types.
1734 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001735 // Otherwise it must be an array.
1736 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001737 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001738 if (IdxTy != Idx0->getType()) {
Owen Anderson53a52212009-07-13 04:09:18 +00001739 Constant *C1 =
Owen Anderson55f1c092009-08-13 21:58:54 +00001740 ConstantExpr::getSExtOrBitCast(Idx0, Type::getInt64Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001741 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Owen Anderson55f1c092009-08-13 21:58:54 +00001742 Type::getInt64Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001743 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
Reid Spencer1a063892006-12-04 02:46:44 +00001744 } else {
1745 Combined =
Owen Anderson487375e2009-07-29 18:55:55 +00001746 ConstantExpr::get(Instruction::Add, Idx0, Combined);
Reid Spencer1a063892006-12-04 02:46:44 +00001747 }
Chris Lattner71068a02004-07-07 04:45:13 +00001748 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001749
Chris Lattner1dd054c2004-01-12 22:07:24 +00001750 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001751 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
Dan Gohman21c62162009-09-11 00:04:14 +00001752 return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ?
1753 ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0),
1754 &NewIndices[0],
1755 NewIndices.size()) :
1756 ConstantExpr::getGetElementPtr(CE->getOperand(0),
1757 &NewIndices[0],
1758 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001759 }
1760 }
1761
1762 // Implement folding of:
1763 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1764 // long 0, long 0)
1765 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1766 //
Chris Lattneraadc7782007-08-13 17:09:08 +00001767 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001768 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001769 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1770 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1771 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001772 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001773 if (CAT->getElementType() == SAT->getElementType())
Dan Gohman21c62162009-09-11 00:04:14 +00001774 return inBounds ?
1775 ConstantExpr::getInBoundsGetElementPtr(
1776 (Constant*)CE->getOperand(0), Idxs, NumIdx) :
1777 ConstantExpr::getGetElementPtr(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001778 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattneraadc7782007-08-13 17:09:08 +00001779 }
Dan Gohman062d3782009-08-29 23:35:16 +00001780
Chris Lattneraadc7782007-08-13 17:09:08 +00001781 // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1)
1782 // Into: inttoptr (i64 0 to i8*)
1783 // This happens with pointers to member functions in C++.
1784 if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 &&
1785 isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) &&
Owen Anderson55f1c092009-08-13 21:58:54 +00001786 cast<PointerType>(CE->getType())->getElementType() == Type::getInt8Ty(Context)) {
Chris Lattneraadc7782007-08-13 17:09:08 +00001787 Constant *Base = CE->getOperand(0);
1788 Constant *Offset = Idxs[0];
Dan Gohman062d3782009-08-29 23:35:16 +00001789
Chris Lattneraadc7782007-08-13 17:09:08 +00001790 // Convert the smaller integer to the larger type.
1791 if (Offset->getType()->getPrimitiveSizeInBits() <
1792 Base->getType()->getPrimitiveSizeInBits())
Owen Anderson487375e2009-07-29 18:55:55 +00001793 Offset = ConstantExpr::getSExt(Offset, Base->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001794 else if (Base->getType()->getPrimitiveSizeInBits() <
1795 Offset->getType()->getPrimitiveSizeInBits())
Owen Anderson487375e2009-07-29 18:55:55 +00001796 Base = ConstantExpr::getZExt(Base, Offset->getType());
Dan Gohman062d3782009-08-29 23:35:16 +00001797
Owen Anderson487375e2009-07-29 18:55:55 +00001798 Base = ConstantExpr::getAdd(Base, Offset);
1799 return ConstantExpr::getIntToPtr(Base, CE->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001800 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001801 }
Dan Gohman21c62162009-09-11 00:04:14 +00001802
1803 // Check to see if any array indices are not within the corresponding
1804 // notional array bounds. If so, try to determine if they can be factored
1805 // out into preceding dimensions.
1806 bool Unknown = false;
1807 SmallVector<Constant *, 8> NewIdxs;
1808 const Type *Ty = C->getType();
1809 const Type *Prev = 0;
1810 for (unsigned i = 0; i != NumIdx;
1811 Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
1812 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
1813 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
1814 if (ATy->getNumElements() <= INT64_MAX &&
1815 ATy->getNumElements() != 0 &&
1816 CI->getSExtValue() >= (int64_t)ATy->getNumElements()) {
1817 if (isa<SequentialType>(Prev)) {
1818 // It's out of range, but we can factor it into the prior
1819 // dimension.
1820 NewIdxs.resize(NumIdx);
1821 ConstantInt *Factor = ConstantInt::get(CI->getType(),
1822 ATy->getNumElements());
1823 NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
1824
1825 Constant *PrevIdx = Idxs[i-1];
1826 Constant *Div = ConstantExpr::getSDiv(CI, Factor);
1827
1828 // Before adding, extend both operands to i64 to avoid
1829 // overflow trouble.
1830 if (PrevIdx->getType() != Type::getInt64Ty(Context))
1831 PrevIdx = ConstantExpr::getSExt(PrevIdx,
1832 Type::getInt64Ty(Context));
1833 if (Div->getType() != Type::getInt64Ty(Context))
1834 Div = ConstantExpr::getSExt(Div,
1835 Type::getInt64Ty(Context));
1836
1837 NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
1838 } else {
1839 // It's out of range, but the prior dimension is a struct
1840 // so we can't do anything about it.
1841 Unknown = true;
1842 }
1843 }
1844 } else {
1845 // We don't know if it's in range or not.
1846 Unknown = true;
1847 }
1848 }
1849
1850 // If we did any factoring, start over with the adjusted indices.
1851 if (!NewIdxs.empty()) {
1852 for (unsigned i = 0; i != NumIdx; ++i)
1853 if (!NewIdxs[i]) NewIdxs[i] = Idxs[i];
1854 return inBounds ?
Nick Lewyckye0332982009-09-20 01:35:59 +00001855 ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(),
1856 NewIdxs.size()) :
1857 ConstantExpr::getGetElementPtr(C, NewIdxs.data(), NewIdxs.size());
Dan Gohman21c62162009-09-11 00:04:14 +00001858 }
1859
1860 // If all indices are known integers and normalized, we can do a simple
1861 // check for the "inbounds" property.
1862 if (!Unknown && !inBounds &&
1863 isa<GlobalVariable>(C) && isInBoundsIndices(Idxs, NumIdx))
Nick Lewyckye0332982009-09-20 01:35:59 +00001864 return ConstantExpr::getInBoundsGetElementPtr(C, Idxs, NumIdx);
Dan Gohman21c62162009-09-11 00:04:14 +00001865
Chris Lattner1dd054c2004-01-12 22:07:24 +00001866 return 0;
1867}