blob: 8641f77b7a611dc0a36656f90affa57003774194 [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
76 const ConstantExpr*Op, ///< the first cast constant expression
77 const Type *DstTy ///< desintation type of the first cast
78) {
79 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
80 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
81 assert(CastInst::isCast(opc) && "Invalid cast opcode");
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.
159 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
160 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.
Duncan Sandse7d54792009-02-04 11:17:06 +0000174 if (const 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,
183 unsigned opc, const 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.
200 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
201 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.
223 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
224 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:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000241 if (const 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:
Reid Spencer81658a82007-02-27 06:23:51 +0000255 if (const 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:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000276 if (const 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:
Reid Spencer81658a82007-02-27 06:23:51 +0000288 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
289 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:
Reid Spencer81658a82007-02-27 06:23:51 +0000296 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
297 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:
Reid Spencer81658a82007-02-27 06:23:51 +0000304 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
305 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:
Owen Anderson53a52212009-07-13 04:09:18 +0000312 return FoldBitCast(Context, const_cast<Constant*>(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&,
323 const Constant *Cond,
Chris Lattner6ea4b522004-03-12 05:53:32 +0000324 const Constant *V1,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000325 const Constant *V2) {
Zhou Sheng75b871f2007-01-11 12:24:14 +0000326 if (const ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
Reid Spencercddc9df2007-01-12 04:24:46 +0000327 return const_cast<Constant*>(CB->getZExtValue() ? V1 : V2);
Chris Lattnerfd7bf722004-10-16 23:31:32 +0000328
329 if (isa<UndefValue>(V1)) return const_cast<Constant*>(V2);
330 if (isa<UndefValue>(V2)) return const_cast<Constant*>(V1);
331 if (isa<UndefValue>(Cond)) return const_cast<Constant*>(V1);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +0000332 if (V1 == V2) return const_cast<Constant*>(V1);
Chris Lattner6ea4b522004-03-12 05:53:32 +0000333 return 0;
334}
335
Owen Anderson53a52212009-07-13 04:09:18 +0000336Constant *llvm::ConstantFoldExtractElementInstruction(LLVMContext &Context,
337 const Constant *Val,
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000338 const Constant *Idx) {
Chris Lattnere52f29b2006-03-31 18:31:40 +0000339 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
Owen Andersonb292b8c2009-07-30 23:03:37 +0000340 return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
Chris Lattnere4f9d7b2006-04-07 04:44:06 +0000341 if (Val->isNullValue()) // ee(zero, x) -> zero
Owen Anderson5a1acd92009-07-31 20:28:14 +0000342 return Constant::getNullValue(
Reid Spencerd84d35b2007-02-15 02:26:10 +0000343 cast<VectorType>(Val->getType())->getElementType());
Dan Gohman062d3782009-08-29 23:35:16 +0000344
Reid Spencerd84d35b2007-02-15 02:26:10 +0000345 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000346 if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000347 return CVal->getOperand(CIdx->getZExtValue());
Chris Lattnere52f29b2006-03-31 18:31:40 +0000348 } else if (isa<UndefValue>(Idx)) {
349 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
Gabor Greiff6caff662008-05-10 08:32:32 +0000350 return CVal->getOperand(0);
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000351 }
Chris Lattnere52f29b2006-03-31 18:31:40 +0000352 }
Robert Bocchinode7f1c92006-01-10 20:03:46 +0000353 return 0;
354}
355
Owen Anderson53a52212009-07-13 04:09:18 +0000356Constant *llvm::ConstantFoldInsertElementInstruction(LLVMContext &Context,
357 const Constant *Val,
Robert Bocchinoca27f032006-01-17 20:07:22 +0000358 const Constant *Elt,
359 const Constant *Idx) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000360 const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000361 if (!CIdx) return 0;
Reid Spencer81658a82007-02-27 06:23:51 +0000362 APInt idxVal = CIdx->getValue();
Reid Spencer3054b142006-11-02 08:18:15 +0000363 if (isa<UndefValue>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000364 // Insertion of scalar constant into vector undef
Robert Bocchinoca27f032006-01-17 20:07:22 +0000365 // Optimize away insertion of undef
366 if (isa<UndefValue>(Elt))
367 return const_cast<Constant*>(Val);
368 // Otherwise break the aggregate undef into multiple undefs and do
369 // the insertion
370 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000371 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000372 std::vector<Constant*> Ops;
373 Ops.reserve(numOps);
374 for (unsigned i = 0; i < numOps; ++i) {
375 const Constant *Op =
Owen Andersonb292b8c2009-07-30 23:03:37 +0000376 (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000377 Ops.push_back(const_cast<Constant*>(Op));
378 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000379 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000380 }
Reid Spencer3054b142006-11-02 08:18:15 +0000381 if (isa<ConstantAggregateZero>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000382 // Insertion of scalar constant into vector aggregate zero
Robert Bocchinoca27f032006-01-17 20:07:22 +0000383 // Optimize away insertion of zero
384 if (Elt->isNullValue())
385 return const_cast<Constant*>(Val);
386 // Otherwise break the aggregate zero into multiple zeros and do
387 // the insertion
388 unsigned numOps =
Reid Spencerd84d35b2007-02-15 02:26:10 +0000389 cast<VectorType>(Val->getType())->getNumElements();
Robert Bocchinoca27f032006-01-17 20:07:22 +0000390 std::vector<Constant*> Ops;
391 Ops.reserve(numOps);
392 for (unsigned i = 0; i < numOps; ++i) {
393 const Constant *Op =
Owen Anderson5a1acd92009-07-31 20:28:14 +0000394 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
Robert Bocchinoca27f032006-01-17 20:07:22 +0000395 Ops.push_back(const_cast<Constant*>(Op));
396 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000397 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000398 }
Reid Spencerd84d35b2007-02-15 02:26:10 +0000399 if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
Dan Gohman06c60b62007-07-16 14:29:03 +0000400 // Insertion of scalar constant into vector constant
Robert Bocchinoca27f032006-01-17 20:07:22 +0000401 std::vector<Constant*> Ops;
402 Ops.reserve(CVal->getNumOperands());
403 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
404 const Constant *Op =
Reid Spencer81658a82007-02-27 06:23:51 +0000405 (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
Robert Bocchinoca27f032006-01-17 20:07:22 +0000406 Ops.push_back(const_cast<Constant*>(Op));
407 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000408 return ConstantVector::get(Ops);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000409 }
Dan Gohman3db11c22008-06-03 00:15:20 +0000410
Robert Bocchinoca27f032006-01-17 20:07:22 +0000411 return 0;
412}
413
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000414/// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef
415/// return the specified element value. Otherwise return null.
Owen Anderson53a52212009-07-13 04:09:18 +0000416static Constant *GetVectorElement(LLVMContext &Context, const Constant *C,
417 unsigned EltNo) {
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000418 if (const ConstantVector *CV = dyn_cast<ConstantVector>(C))
Gabor Greiff6caff662008-05-10 08:32:32 +0000419 return CV->getOperand(EltNo);
Dan Gohman062d3782009-08-29 23:35:16 +0000420
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000421 const Type *EltTy = cast<VectorType>(C->getType())->getElementType();
422 if (isa<ConstantAggregateZero>(C))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000423 return Constant::getNullValue(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000424 if (isa<UndefValue>(C))
Owen Andersonb292b8c2009-07-30 23:03:37 +0000425 return UndefValue::get(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000426 return 0;
427}
428
Owen Anderson53a52212009-07-13 04:09:18 +0000429Constant *llvm::ConstantFoldShuffleVectorInstruction(LLVMContext &Context,
430 const Constant *V1,
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000431 const Constant *V2,
432 const Constant *Mask) {
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000433 // Undefined shuffle mask -> undefined value.
Owen Andersonb292b8c2009-07-30 23:03:37 +0000434 if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType());
Mon P Wang25f01062008-11-10 04:46:22 +0000435
436 unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements();
437 unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements();
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000438 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
Mon P Wang25f01062008-11-10 04:46:22 +0000439
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000440 // Loop over the shuffle mask, evaluating each element.
441 SmallVector<Constant*, 32> Result;
Mon P Wang25f01062008-11-10 04:46:22 +0000442 for (unsigned i = 0; i != MaskNumElts; ++i) {
Owen Anderson53a52212009-07-13 04:09:18 +0000443 Constant *InElt = GetVectorElement(Context, Mask, i);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000444 if (InElt == 0) return 0;
Mon P Wang25f01062008-11-10 04:46:22 +0000445
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000446 if (isa<UndefValue>(InElt))
Owen Andersonb292b8c2009-07-30 23:03:37 +0000447 InElt = UndefValue::get(EltTy);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000448 else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) {
449 unsigned Elt = CI->getZExtValue();
Mon P Wang25f01062008-11-10 04:46:22 +0000450 if (Elt >= SrcNumElts*2)
Owen Andersonb292b8c2009-07-30 23:03:37 +0000451 InElt = UndefValue::get(EltTy);
Mon P Wang25f01062008-11-10 04:46:22 +0000452 else if (Elt >= SrcNumElts)
Owen Anderson53a52212009-07-13 04:09:18 +0000453 InElt = GetVectorElement(Context, V2, Elt - SrcNumElts);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000454 else
Owen Anderson53a52212009-07-13 04:09:18 +0000455 InElt = GetVectorElement(Context, V1, Elt);
Chris Lattner8e6a8f92007-12-11 07:49:37 +0000456 if (InElt == 0) return 0;
457 } else {
458 // Unknown value.
459 return 0;
460 }
461 Result.push_back(InElt);
462 }
Mon P Wang25f01062008-11-10 04:46:22 +0000463
Owen Anderson4aa32952009-07-28 21:19:26 +0000464 return ConstantVector::get(&Result[0], Result.size());
Chris Lattnerbbe0a422006-04-08 01:18:18 +0000465}
466
Owen Anderson53a52212009-07-13 04:09:18 +0000467Constant *llvm::ConstantFoldExtractValueInstruction(LLVMContext &Context,
468 const Constant *Agg,
Dan Gohman3db11c22008-06-03 00:15:20 +0000469 const unsigned *Idxs,
470 unsigned NumIdx) {
471 // Base case: no indices, so return the entire value.
472 if (NumIdx == 0)
473 return const_cast<Constant *>(Agg);
474
475 if (isa<UndefValue>(Agg)) // ev(undef, x) -> undef
Owen Andersonb292b8c2009-07-30 23:03:37 +0000476 return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(),
Dan Gohman3db11c22008-06-03 00:15:20 +0000477 Idxs,
478 Idxs + NumIdx));
479
480 if (isa<ConstantAggregateZero>(Agg)) // ev(0, x) -> 0
481 return
Owen Anderson5a1acd92009-07-31 20:28:14 +0000482 Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
Dan Gohman3db11c22008-06-03 00:15:20 +0000483 Idxs,
484 Idxs + NumIdx));
485
486 // Otherwise recurse.
Owen Anderson53a52212009-07-13 04:09:18 +0000487 return ConstantFoldExtractValueInstruction(Context, Agg->getOperand(*Idxs),
Dan Gohman3db11c22008-06-03 00:15:20 +0000488 Idxs+1, NumIdx-1);
Dan Gohman12fce772008-05-15 19:50:34 +0000489}
490
Owen Anderson53a52212009-07-13 04:09:18 +0000491Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
492 const Constant *Agg,
Dan Gohman3db11c22008-06-03 00:15:20 +0000493 const Constant *Val,
494 const unsigned *Idxs,
495 unsigned NumIdx) {
496 // Base case: no indices, so replace the entire value.
497 if (NumIdx == 0)
498 return const_cast<Constant *>(Val);
499
500 if (isa<UndefValue>(Agg)) {
501 // Insertion of constant into aggregate undef
Chris Lattneree8f74f2009-09-15 06:28:12 +0000502 // Optimize away insertion of undef.
Dan Gohman3db11c22008-06-03 00:15:20 +0000503 if (isa<UndefValue>(Val))
504 return const_cast<Constant*>(Agg);
Chris Lattneree8f74f2009-09-15 06:28:12 +0000505
Dan Gohman3db11c22008-06-03 00:15:20 +0000506 // Otherwise break the aggregate undef into multiple undefs and do
Chris Lattneree8f74f2009-09-15 06:28:12 +0000507 // the insertion.
Dan Gohman3db11c22008-06-03 00:15:20 +0000508 const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
509 unsigned numOps;
510 if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
511 numOps = AR->getNumElements();
512 else
513 numOps = cast<StructType>(AggTy)->getNumElements();
Chris Lattneree8f74f2009-09-15 06:28:12 +0000514
Dan Gohman3db11c22008-06-03 00:15:20 +0000515 std::vector<Constant*> Ops(numOps);
516 for (unsigned i = 0; i < numOps; ++i) {
517 const Type *MemberTy = AggTy->getTypeAtIndex(i);
518 const Constant *Op =
519 (*Idxs == i) ?
Owen Andersonb292b8c2009-07-30 23:03:37 +0000520 ConstantFoldInsertValueInstruction(Context, UndefValue::get(MemberTy),
Dan Gohman3db11c22008-06-03 00:15:20 +0000521 Val, Idxs+1, NumIdx-1) :
Owen Andersonb292b8c2009-07-30 23:03:37 +0000522 UndefValue::get(MemberTy);
Dan Gohman3db11c22008-06-03 00:15:20 +0000523 Ops[i] = const_cast<Constant*>(Op);
524 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000525
526 if (const StructType* ST = dyn_cast<StructType>(AggTy))
527 return ConstantStruct::get(Context, Ops, ST->isPacked());
528 return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000529 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000530
Dan Gohman3db11c22008-06-03 00:15:20 +0000531 if (isa<ConstantAggregateZero>(Agg)) {
532 // Insertion of constant into aggregate zero
Chris Lattneree8f74f2009-09-15 06:28:12 +0000533 // Optimize away insertion of zero.
Dan Gohman3db11c22008-06-03 00:15:20 +0000534 if (Val->isNullValue())
535 return const_cast<Constant*>(Agg);
Chris Lattneree8f74f2009-09-15 06:28:12 +0000536
Dan Gohman3db11c22008-06-03 00:15:20 +0000537 // Otherwise break the aggregate zero into multiple zeros and do
Chris Lattneree8f74f2009-09-15 06:28:12 +0000538 // the insertion.
Dan Gohman3db11c22008-06-03 00:15:20 +0000539 const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
540 unsigned numOps;
541 if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
542 numOps = AR->getNumElements();
543 else
544 numOps = cast<StructType>(AggTy)->getNumElements();
Chris Lattneree8f74f2009-09-15 06:28:12 +0000545
Dan Gohman3db11c22008-06-03 00:15:20 +0000546 std::vector<Constant*> Ops(numOps);
547 for (unsigned i = 0; i < numOps; ++i) {
548 const Type *MemberTy = AggTy->getTypeAtIndex(i);
549 const Constant *Op =
550 (*Idxs == i) ?
Owen Anderson53a52212009-07-13 04:09:18 +0000551 ConstantFoldInsertValueInstruction(Context,
Owen Anderson5a1acd92009-07-31 20:28:14 +0000552 Constant::getNullValue(MemberTy),
Dan Gohman3db11c22008-06-03 00:15:20 +0000553 Val, Idxs+1, NumIdx-1) :
Owen Anderson5a1acd92009-07-31 20:28:14 +0000554 Constant::getNullValue(MemberTy);
Dan Gohman3db11c22008-06-03 00:15:20 +0000555 Ops[i] = const_cast<Constant*>(Op);
556 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000557
558 if (const StructType* ST = dyn_cast<StructType>(AggTy))
559 return ConstantStruct::get(Context, Ops, ST->isPacked());
560 return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000561 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000562
Dan Gohman3db11c22008-06-03 00:15:20 +0000563 if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
Chris Lattneree8f74f2009-09-15 06:28:12 +0000564 // Insertion of constant into aggregate constant.
Dan Gohman3db11c22008-06-03 00:15:20 +0000565 std::vector<Constant*> Ops(Agg->getNumOperands());
566 for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
567 const Constant *Op =
568 (*Idxs == i) ?
Owen Anderson53a52212009-07-13 04:09:18 +0000569 ConstantFoldInsertValueInstruction(Context, Agg->getOperand(i),
Dan Gohman3db11c22008-06-03 00:15:20 +0000570 Val, Idxs+1, NumIdx-1) :
571 Agg->getOperand(i);
572 Ops[i] = const_cast<Constant*>(Op);
573 }
Chris Lattneree8f74f2009-09-15 06:28:12 +0000574
575 if (const StructType* ST = dyn_cast<StructType>(Agg->getType()))
576 return ConstantStruct::get(Context, Ops, ST->isPacked());
577 return ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
Dan Gohman3db11c22008-06-03 00:15:20 +0000578 }
579
Dan Gohman12fce772008-05-15 19:50:34 +0000580 return 0;
581}
582
Reid Spencer266e42b2006-12-23 06:05:41 +0000583
Owen Anderson53a52212009-07-13 04:09:18 +0000584Constant *llvm::ConstantFoldBinaryInstruction(LLVMContext &Context,
585 unsigned Opcode,
Reid Spencer266e42b2006-12-23 06:05:41 +0000586 const Constant *C1,
Owen Anderson0d2de8c2009-06-20 00:24:58 +0000587 const Constant *C2) {
Dale Johannesene5facd52007-10-16 23:38:29 +0000588 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +0000589 if (C1->getType() == Type::getPPC_FP128Ty(Context))
Dale Johannesene5facd52007-10-16 23:38:29 +0000590 return 0;
591
Chris Lattneree8f74f2009-09-15 06:28:12 +0000592 // Handle UndefValue up front.
Reid Spencer266e42b2006-12-23 06:05:41 +0000593 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
594 switch (Opcode) {
Evan Chengdf1690d2008-03-25 20:08:07 +0000595 case Instruction::Xor:
596 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
597 // Handle undef ^ undef -> 0 special case. This is a common
598 // idiom (misuse).
Owen Anderson5a1acd92009-07-31 20:28:14 +0000599 return Constant::getNullValue(C1->getType());
Evan Chengdf1690d2008-03-25 20:08:07 +0000600 // Fallthrough
Reid Spencer266e42b2006-12-23 06:05:41 +0000601 case Instruction::Add:
602 case Instruction::Sub:
Owen Andersonb292b8c2009-07-30 23:03:37 +0000603 return UndefValue::get(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000604 case Instruction::Mul:
605 case Instruction::And:
Owen Anderson5a1acd92009-07-31 20:28:14 +0000606 return Constant::getNullValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000607 case Instruction::UDiv:
608 case Instruction::SDiv:
Reid Spencer266e42b2006-12-23 06:05:41 +0000609 case Instruction::URem:
610 case Instruction::SRem:
Reid Spencer266e42b2006-12-23 06:05:41 +0000611 if (!isa<UndefValue>(C2)) // undef / X -> 0
Owen Anderson5a1acd92009-07-31 20:28:14 +0000612 return Constant::getNullValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000613 return const_cast<Constant*>(C2); // X / undef -> undef
614 case Instruction::Or: // X | undef -> -1
Reid Spencerd84d35b2007-02-15 02:26:10 +0000615 if (const VectorType *PTy = dyn_cast<VectorType>(C1->getType()))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000616 return Constant::getAllOnesValue(PTy);
617 return Constant::getAllOnesValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000618 case Instruction::LShr:
619 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
620 return const_cast<Constant*>(C1); // undef lshr undef -> undef
Owen Anderson5a1acd92009-07-31 20:28:14 +0000621 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
Reid Spencer266e42b2006-12-23 06:05:41 +0000622 // undef lshr X -> 0
623 case Instruction::AShr:
624 if (!isa<UndefValue>(C2))
625 return const_cast<Constant*>(C1); // undef ashr X --> undef
626 else if (isa<UndefValue>(C1))
627 return const_cast<Constant*>(C1); // undef ashr undef -> undef
628 else
629 return const_cast<Constant*>(C1); // X ashr undef --> X
630 case Instruction::Shl:
631 // undef << X -> 0 or X << undef -> 0
Owen Anderson5a1acd92009-07-31 20:28:14 +0000632 return Constant::getNullValue(C1->getType());
Reid Spencer266e42b2006-12-23 06:05:41 +0000633 }
634 }
635
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000636 // Handle simplifications when the RHS is a constant int.
Chris Lattner334d33c2008-04-19 21:58:19 +0000637 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
638 switch (Opcode) {
639 case Instruction::Add:
640 if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X + 0 == X
641 break;
642 case Instruction::Sub:
643 if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X - 0 == X
644 break;
645 case Instruction::Mul:
646 if (CI2->equalsInt(0)) return const_cast<Constant*>(C2); // X * 0 == 0
647 if (CI2->equalsInt(1))
648 return const_cast<Constant*>(C1); // X * 1 == X
649 break;
650 case Instruction::UDiv:
651 case Instruction::SDiv:
652 if (CI2->equalsInt(1))
653 return const_cast<Constant*>(C1); // X / 1 == X
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000654 if (CI2->equalsInt(0))
Owen Andersonb292b8c2009-07-30 23:03:37 +0000655 return UndefValue::get(CI2->getType()); // X / 0 == undef
Chris Lattner334d33c2008-04-19 21:58:19 +0000656 break;
657 case Instruction::URem:
658 case Instruction::SRem:
659 if (CI2->equalsInt(1))
Owen Anderson5a1acd92009-07-31 20:28:14 +0000660 return Constant::getNullValue(CI2->getType()); // X % 1 == 0
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000661 if (CI2->equalsInt(0))
Owen Andersonb292b8c2009-07-30 23:03:37 +0000662 return UndefValue::get(CI2->getType()); // X % 0 == undef
Chris Lattner334d33c2008-04-19 21:58:19 +0000663 break;
664 case Instruction::And:
665 if (CI2->isZero()) return const_cast<Constant*>(C2); // X & 0 == 0
666 if (CI2->isAllOnesValue())
667 return const_cast<Constant*>(C1); // X & -1 == X
Dan Gohman062d3782009-08-29 23:35:16 +0000668
Chris Lattner334d33c2008-04-19 21:58:19 +0000669 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000670 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
Chris Lattner334d33c2008-04-19 21:58:19 +0000671 if (CE1->getOpcode() == Instruction::ZExt) {
672 unsigned DstWidth = CI2->getType()->getBitWidth();
673 unsigned SrcWidth =
674 CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
675 APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
676 if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
677 return const_cast<Constant*>(C1);
Chris Lattner6d94bb72007-03-25 05:47:04 +0000678 }
Dan Gohman062d3782009-08-29 23:35:16 +0000679
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000680 // If and'ing the address of a global with a constant, fold it.
Chris Lattner334d33c2008-04-19 21:58:19 +0000681 if (CE1->getOpcode() == Instruction::PtrToInt &&
682 isa<GlobalValue>(CE1->getOperand(0))) {
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000683 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
Dan Gohman062d3782009-08-29 23:35:16 +0000684
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000685 // Functions are at least 4-byte aligned.
686 unsigned GVAlign = GV->getAlignment();
687 if (isa<Function>(GV))
688 GVAlign = std::max(GVAlign, 4U);
Dan Gohman062d3782009-08-29 23:35:16 +0000689
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000690 if (GVAlign > 1) {
691 unsigned DstWidth = CI2->getType()->getBitWidth();
Chris Lattner912bec72008-04-20 19:59:12 +0000692 unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000693 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
694
695 // If checking bits we know are clear, return zero.
696 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
Owen Anderson5a1acd92009-07-31 20:28:14 +0000697 return Constant::getNullValue(CI2->getType());
Chris Lattnerbc26e1b2008-04-19 22:17:26 +0000698 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000699 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000700 }
701 break;
702 case Instruction::Or:
703 if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X | 0 == X
704 if (CI2->isAllOnesValue())
705 return const_cast<Constant*>(C2); // X | -1 == -1
706 break;
707 case Instruction::Xor:
708 if (CI2->equalsInt(0)) return const_cast<Constant*>(C1); // X ^ 0 == X
709 break;
710 case Instruction::AShr:
711 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
712 if (const ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
Chris Lattner6d94bb72007-03-25 05:47:04 +0000713 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
Owen Anderson487375e2009-07-29 18:55:55 +0000714 return ConstantExpr::getLShr(const_cast<Constant*>(C1),
Owen Anderson53a52212009-07-13 04:09:18 +0000715 const_cast<Constant*>(C2));
Chris Lattner334d33c2008-04-19 21:58:19 +0000716 break;
Reid Spencer266e42b2006-12-23 06:05:41 +0000717 }
Chris Lattner334d33c2008-04-19 21:58:19 +0000718 }
Dan Gohman062d3782009-08-29 23:35:16 +0000719
Chris Lattner6b056052008-04-20 18:24:14 +0000720 // At this point we know neither constant is an UndefValue.
Zhou Sheng75b871f2007-01-11 12:24:14 +0000721 if (const ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
722 if (const ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +0000723 using namespace APIntOps;
Chris Lattner6b056052008-04-20 18:24:14 +0000724 const APInt &C1V = CI1->getValue();
725 const APInt &C2V = CI2->getValue();
Chris Lattner344da522007-01-12 18:42:52 +0000726 switch (Opcode) {
727 default:
728 break;
729 case Instruction::Add:
Owen Andersonedb4a702009-07-24 23:12:02 +0000730 return ConstantInt::get(Context, C1V + C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000731 case Instruction::Sub:
Owen Andersonedb4a702009-07-24 23:12:02 +0000732 return ConstantInt::get(Context, C1V - C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000733 case Instruction::Mul:
Owen Andersonedb4a702009-07-24 23:12:02 +0000734 return ConstantInt::get(Context, C1V * C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000735 case Instruction::UDiv:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000736 assert(!CI2->isNullValue() && "Div by zero handled above");
Owen Andersonedb4a702009-07-24 23:12:02 +0000737 return ConstantInt::get(Context, C1V.udiv(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000738 case Instruction::SDiv:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000739 assert(!CI2->isNullValue() && "Div by zero handled above");
Reid Spencer81658a82007-02-27 06:23:51 +0000740 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000741 return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef
Owen Andersonedb4a702009-07-24 23:12:02 +0000742 return ConstantInt::get(Context, C1V.sdiv(C2V));
Reid Spencer81658a82007-02-27 06:23:51 +0000743 case Instruction::URem:
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000744 assert(!CI2->isNullValue() && "Div by zero handled above");
Owen Andersonedb4a702009-07-24 23:12:02 +0000745 return ConstantInt::get(Context, C1V.urem(C2V));
Chris Lattnerdd8ef1b2009-01-19 21:55:26 +0000746 case Instruction::SRem:
747 assert(!CI2->isNullValue() && "Div by zero handled above");
Reid Spencer81658a82007-02-27 06:23:51 +0000748 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
Owen Andersonb292b8c2009-07-30 23:03:37 +0000749 return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef
Owen Andersonedb4a702009-07-24 23:12:02 +0000750 return ConstantInt::get(Context, C1V.srem(C2V));
Chris Lattner344da522007-01-12 18:42:52 +0000751 case Instruction::And:
Owen Andersonedb4a702009-07-24 23:12:02 +0000752 return ConstantInt::get(Context, C1V & C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000753 case Instruction::Or:
Owen Andersonedb4a702009-07-24 23:12:02 +0000754 return ConstantInt::get(Context, C1V | C2V);
Chris Lattner344da522007-01-12 18:42:52 +0000755 case Instruction::Xor:
Owen Andersonedb4a702009-07-24 23:12:02 +0000756 return ConstantInt::get(Context, C1V ^ C2V);
Chris Lattner6b056052008-04-20 18:24:14 +0000757 case Instruction::Shl: {
758 uint32_t shiftAmt = C2V.getZExtValue();
759 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000760 return ConstantInt::get(Context, C1V.shl(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000761 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000762 return UndefValue::get(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000763 }
764 case Instruction::LShr: {
765 uint32_t shiftAmt = C2V.getZExtValue();
766 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000767 return ConstantInt::get(Context, C1V.lshr(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000768 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000769 return UndefValue::get(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000770 }
771 case Instruction::AShr: {
772 uint32_t shiftAmt = C2V.getZExtValue();
773 if (shiftAmt < C1V.getBitWidth())
Owen Andersonedb4a702009-07-24 23:12:02 +0000774 return ConstantInt::get(Context, C1V.ashr(shiftAmt));
Chris Lattner6b056052008-04-20 18:24:14 +0000775 else
Owen Andersonb292b8c2009-07-30 23:03:37 +0000776 return UndefValue::get(C1->getType()); // too big shift is undef
Chris Lattner6b056052008-04-20 18:24:14 +0000777 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000778 }
779 }
Nick Lewycky6b8320f2009-06-21 01:56:41 +0000780
781 switch (Opcode) {
782 case Instruction::SDiv:
783 case Instruction::UDiv:
784 case Instruction::URem:
785 case Instruction::SRem:
786 case Instruction::LShr:
787 case Instruction::AShr:
788 case Instruction::Shl:
789 if (CI1->equalsInt(0)) return const_cast<Constant*>(C1);
790 break;
791 default:
792 break;
793 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000794 } else if (const ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
795 if (const ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000796 APFloat C1V = CFP1->getValueAPF();
797 APFloat C2V = CFP2->getValueAPF();
798 APFloat C3V = C1V; // copy for modification
Reid Spencer266e42b2006-12-23 06:05:41 +0000799 switch (Opcode) {
800 default:
801 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000802 case Instruction::FAdd:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000803 (void)C3V.add(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::FSub:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000806 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000807 return ConstantFP::get(Context, C3V);
Dan Gohmana5b96452009-06-04 22:49:04 +0000808 case Instruction::FMul:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000809 (void)C3V.multiply(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::FDiv:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000812 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000813 return ConstantFP::get(Context, C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000814 case Instruction::FRem:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000815 (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
Owen Anderson69c464d2009-07-27 20:59:43 +0000816 return ConstantFP::get(Context, C3V);
Reid Spencer266e42b2006-12-23 06:05:41 +0000817 }
818 }
Dan Gohman9f396602007-10-30 19:00:49 +0000819 } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
820 const ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
821 const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000822 if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
823 (CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
Owen Anderson85f86dc2009-07-13 22:41:06 +0000824 std::vector<Constant*> Res;
825 const Type* EltTy = VTy->getElementType();
826 const Constant *C1 = 0;
827 const Constant *C2 = 0;
Reid Spencer266e42b2006-12-23 06:05:41 +0000828 switch (Opcode) {
Chris Lattner6072ead2008-04-19 21:13:00 +0000829 default:
830 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000831 case Instruction::Add:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000832 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000833 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
834 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000835 Res.push_back(ConstantExpr::getAdd(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000836 const_cast<Constant*>(C2)));
837 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000838 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000839 case Instruction::FAdd:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000840 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000841 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
842 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000843 Res.push_back(ConstantExpr::getFAdd(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000844 const_cast<Constant*>(C2)));
845 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000846 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000847 case Instruction::Sub:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000848 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000849 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
850 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000851 Res.push_back(ConstantExpr::getSub(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000852 const_cast<Constant*>(C2)));
853 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000854 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000855 case Instruction::FSub:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000856 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000857 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
858 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000859 Res.push_back(ConstantExpr::getFSub(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000860 const_cast<Constant*>(C2)));
861 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000862 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000863 case Instruction::Mul:
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);
Owen Anderson487375e2009-07-29 18:55:55 +0000867 Res.push_back(ConstantExpr::getMul(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000868 const_cast<Constant*>(C2)));
869 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000870 return ConstantVector::get(Res);
Dan Gohmana5b96452009-06-04 22:49:04 +0000871 case Instruction::FMul:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000872 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000873 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
874 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000875 Res.push_back(ConstantExpr::getFMul(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000876 const_cast<Constant*>(C2)));
877 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000878 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000879 case Instruction::UDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000880 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000881 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
882 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000883 Res.push_back(ConstantExpr::getUDiv(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000884 const_cast<Constant*>(C2)));
885 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000886 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000887 case Instruction::SDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000888 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000889 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
890 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000891 Res.push_back(ConstantExpr::getSDiv(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000892 const_cast<Constant*>(C2)));
893 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000894 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000895 case Instruction::FDiv:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000896 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000897 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
898 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000899 Res.push_back(ConstantExpr::getFDiv(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000900 const_cast<Constant*>(C2)));
901 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000902 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000903 case Instruction::URem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000904 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000905 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
906 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000907 Res.push_back(ConstantExpr::getURem(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000908 const_cast<Constant*>(C2)));
909 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000910 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000911 case Instruction::SRem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000912 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000913 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
914 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000915 Res.push_back(ConstantExpr::getSRem(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000916 const_cast<Constant*>(C2)));
917 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000918 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000919 case Instruction::FRem:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000920 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000921 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
922 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000923 Res.push_back(ConstantExpr::getFRem(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000924 const_cast<Constant*>(C2)));
925 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000926 return ConstantVector::get(Res);
Chris Lattner6072ead2008-04-19 21:13:00 +0000927 case Instruction::And:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000928 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000929 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
930 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000931 Res.push_back(ConstantExpr::getAnd(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000932 const_cast<Constant*>(C2)));
933 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000934 return ConstantVector::get(Res);
Owen Anderson85f86dc2009-07-13 22:41:06 +0000935 case Instruction::Or:
936 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000937 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
938 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000939 Res.push_back(ConstantExpr::getOr(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000940 const_cast<Constant*>(C2)));
941 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000942 return ConstantVector::get(Res);
Owen Anderson85f86dc2009-07-13 22:41:06 +0000943 case Instruction::Xor:
944 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000945 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
946 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000947 Res.push_back(ConstantExpr::getXor(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000948 const_cast<Constant*>(C2)));
949 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000950 return ConstantVector::get(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000951 case Instruction::LShr:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000952 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000953 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
954 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000955 Res.push_back(ConstantExpr::getLShr(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000956 const_cast<Constant*>(C2)));
957 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000958 return ConstantVector::get(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000959 case Instruction::AShr:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000960 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000961 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
962 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000963 Res.push_back(ConstantExpr::getAShr(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000964 const_cast<Constant*>(C2)));
965 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000966 return ConstantVector::get(Res);
Dan Gohman79975d52009-03-14 17:09:17 +0000967 case Instruction::Shl:
Owen Anderson85f86dc2009-07-13 22:41:06 +0000968 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
Owen Anderson5a1acd92009-07-31 20:28:14 +0000969 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
970 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
Owen Anderson487375e2009-07-29 18:55:55 +0000971 Res.push_back(ConstantExpr::getShl(const_cast<Constant*>(C1),
Owen Anderson85f86dc2009-07-13 22:41:06 +0000972 const_cast<Constant*>(C2)));
973 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000974 return ConstantVector::get(Res);
Dan Gohmanb43e0202007-10-31 21:36:31 +0000975 }
Reid Spencer266e42b2006-12-23 06:05:41 +0000976 }
977 }
978
Chris Lattner6b056052008-04-20 18:24:14 +0000979 if (isa<ConstantExpr>(C1)) {
980 // There are many possible foldings we could do here. We should probably
981 // at least fold add of a pointer with an integer into the appropriate
982 // getelementptr. This will improve alias analysis a bit.
983 } else if (isa<ConstantExpr>(C2)) {
984 // If C2 is a constant expr and C1 isn't, flop them around and fold the
985 // other way if possible.
986 switch (Opcode) {
987 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000988 case Instruction::FAdd:
Chris Lattner6b056052008-04-20 18:24:14 +0000989 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000990 case Instruction::FMul:
Chris Lattner6b056052008-04-20 18:24:14 +0000991 case Instruction::And:
992 case Instruction::Or:
993 case Instruction::Xor:
994 // No change of opcode required.
Owen Anderson53a52212009-07-13 04:09:18 +0000995 return ConstantFoldBinaryInstruction(Context, Opcode, C2, C1);
Dan Gohman062d3782009-08-29 23:35:16 +0000996
Chris Lattner6b056052008-04-20 18:24:14 +0000997 case Instruction::Shl:
998 case Instruction::LShr:
999 case Instruction::AShr:
1000 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001001 case Instruction::FSub:
Chris Lattner6b056052008-04-20 18:24:14 +00001002 case Instruction::SDiv:
1003 case Instruction::UDiv:
1004 case Instruction::FDiv:
1005 case Instruction::URem:
1006 case Instruction::SRem:
1007 case Instruction::FRem:
1008 default: // These instructions cannot be flopped around.
1009 break;
1010 }
1011 }
Dan Gohman062d3782009-08-29 23:35:16 +00001012
Chris Lattner6b056052008-04-20 18:24:14 +00001013 // We don't know how to fold this.
Reid Spencer266e42b2006-12-23 06:05:41 +00001014 return 0;
1015}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001016
Chris Lattner60c47262005-01-28 19:09:51 +00001017/// isZeroSizedType - This type is zero sized if its an array or structure of
1018/// zero sized types. The only leaf zero sized type is an empty structure.
1019static bool isMaybeZeroSizedType(const Type *Ty) {
1020 if (isa<OpaqueType>(Ty)) return true; // Can't say.
1021 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1022
1023 // If all of elements have zero size, this does too.
1024 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
Chris Lattnerfeaf92f2005-01-28 23:17:27 +00001025 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
Chris Lattner60c47262005-01-28 19:09:51 +00001026 return true;
1027
1028 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1029 return isMaybeZeroSizedType(ATy->getElementType());
1030 }
1031 return false;
1032}
Chris Lattner6ea4b522004-03-12 05:53:32 +00001033
Chris Lattner061da2f2004-01-13 05:51:55 +00001034/// IdxCompare - Compare the two constants as though they were getelementptr
1035/// indices. This allows coersion of the types to be the same thing.
1036///
1037/// If the two constants are the "same" (after coersion), return 0. If the
1038/// first is less than the second, return -1, if the second is less than the
1039/// first, return 1. If the constants are not integral, return -2.
1040///
Owen Anderson53a52212009-07-13 04:09:18 +00001041static int IdxCompare(LLVMContext &Context, Constant *C1, Constant *C2,
1042 const Type *ElTy) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001043 if (C1 == C2) return 0;
1044
Reid Spencerc90cf772006-12-31 21:43:30 +00001045 // Ok, we found a different index. If they are not ConstantInt, we can't do
1046 // anything with them.
Chris Lattner061da2f2004-01-13 05:51:55 +00001047 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1048 return -2; // don't know!
Misha Brukmanb1c93172005-04-21 23:48:37 +00001049
Chris Lattner69193f92004-04-05 01:30:19 +00001050 // Ok, we have two differing integer indices. Sign extend them to be the same
1051 // type. Long is always big enough, so we use it.
Owen Anderson55f1c092009-08-13 21:58:54 +00001052 if (C1->getType() != Type::getInt64Ty(Context))
1053 C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(Context));
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001054
Owen Anderson55f1c092009-08-13 21:58:54 +00001055 if (C2->getType() != Type::getInt64Ty(Context))
1056 C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(Context));
Reid Spencer8d9336d2006-12-31 05:26:44 +00001057
1058 if (C1 == C2) return 0; // They are equal
Chris Lattner061da2f2004-01-13 05:51:55 +00001059
Chris Lattner60c47262005-01-28 19:09:51 +00001060 // If the type being indexed over is really just a zero sized type, there is
1061 // no pointer difference being made here.
1062 if (isMaybeZeroSizedType(ElTy))
1063 return -2; // dunno.
1064
Chris Lattner061da2f2004-01-13 05:51:55 +00001065 // If they are really different, now that they are the same type, then we
1066 // found a difference!
Reid Spencere0fc4df2006-10-20 07:07:24 +00001067 if (cast<ConstantInt>(C1)->getSExtValue() <
1068 cast<ConstantInt>(C2)->getSExtValue())
Chris Lattner061da2f2004-01-13 05:51:55 +00001069 return -1;
1070 else
1071 return 1;
1072}
1073
Chris Lattner858f4e92007-01-04 02:13:20 +00001074/// evaluateFCmpRelation - This function determines if there is anything we can
Reid Spencer266e42b2006-12-23 06:05:41 +00001075/// decide about the two constants provided. This doesn't need to handle simple
1076/// things like ConstantFP comparisons, but should instead handle ConstantExprs.
1077/// If we can determine that the two constants have a particular relation to
1078/// each other, we should return the corresponding FCmpInst predicate,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001079/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1080/// ConstantFoldCompareInstruction.
Reid Spencer266e42b2006-12-23 06:05:41 +00001081///
1082/// To simplify this code we canonicalize the relation so that the first
Reid Spencer9d36acf2006-12-24 18:52:08 +00001083/// operand is always the most "complex" of the two. We consider ConstantFP
1084/// to be the simplest, and ConstantExprs to be the most complex.
Owen Anderson53a52212009-07-13 04:09:18 +00001085static FCmpInst::Predicate evaluateFCmpRelation(LLVMContext &Context,
1086 const Constant *V1,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001087 const Constant *V2) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001088 assert(V1->getType() == V2->getType() &&
Reid Spencer9d36acf2006-12-24 18:52:08 +00001089 "Cannot compare values of different types!");
Dale Johannesen19db0932007-10-14 01:56:47 +00001090
1091 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +00001092 if (V1->getType() == Type::getPPC_FP128Ty(Context))
Dale Johannesen19db0932007-10-14 01:56:47 +00001093 return FCmpInst::BAD_FCMP_PREDICATE;
1094
Reid Spencer9d36acf2006-12-24 18:52:08 +00001095 // Handle degenerate case quickly
Reid Spencer266e42b2006-12-23 06:05:41 +00001096 if (V1 == V2) return FCmpInst::FCMP_OEQ;
1097
Reid Spencer9d36acf2006-12-24 18:52:08 +00001098 if (!isa<ConstantExpr>(V1)) {
1099 if (!isa<ConstantExpr>(V2)) {
1100 // We distilled thisUse the standard constant folder for a few cases
Zhou Sheng75b871f2007-01-11 12:24:14 +00001101 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001102 Constant *C1 = const_cast<Constant*>(V1);
1103 Constant *C2 = const_cast<Constant*>(V2);
Zhou Sheng75b871f2007-01-11 12:24:14 +00001104 R = dyn_cast<ConstantInt>(
Owen Anderson487375e2009-07-29 18:55:55 +00001105 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001106 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001107 return FCmpInst::FCMP_OEQ;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001108 R = dyn_cast<ConstantInt>(
Owen Anderson487375e2009-07-29 18:55:55 +00001109 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001110 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001111 return FCmpInst::FCMP_OLT;
Zhou Sheng75b871f2007-01-11 12:24:14 +00001112 R = dyn_cast<ConstantInt>(
Owen Anderson487375e2009-07-29 18:55:55 +00001113 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001114 if (R && !R->isZero())
Reid Spencer9d36acf2006-12-24 18:52:08 +00001115 return FCmpInst::FCMP_OGT;
1116
1117 // Nothing more we can do
Reid Spencer266e42b2006-12-23 06:05:41 +00001118 return FCmpInst::BAD_FCMP_PREDICATE;
1119 }
Dan Gohman062d3782009-08-29 23:35:16 +00001120
Reid Spencer9d36acf2006-12-24 18:52:08 +00001121 // If the first operand is simple and second is ConstantExpr, swap operands.
Owen Anderson53a52212009-07-13 04:09:18 +00001122 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(Context, V2, V1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001123 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1124 return FCmpInst::getSwappedPredicate(SwappedRelation);
1125 } else {
1126 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1127 // constantexpr or a simple constant.
1128 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1129 switch (CE1->getOpcode()) {
1130 case Instruction::FPTrunc:
1131 case Instruction::FPExt:
1132 case Instruction::UIToFP:
1133 case Instruction::SIToFP:
1134 // We might be able to do something with these but we don't right now.
1135 break;
1136 default:
1137 break;
1138 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001139 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001140 // There are MANY other foldings that we could perform here. They will
1141 // probably be added on demand, as they seem needed.
1142 return FCmpInst::BAD_FCMP_PREDICATE;
1143}
1144
1145/// evaluateICmpRelation - This function determines if there is anything we can
Chris Lattner061da2f2004-01-13 05:51:55 +00001146/// decide about the two constants provided. This doesn't need to handle simple
Reid Spenceraccd7c72004-07-17 23:47:01 +00001147/// things like integer comparisons, but should instead handle ConstantExprs
Chris Lattner8410beb2006-12-11 02:16:58 +00001148/// and GlobalValues. If we can determine that the two constants have a
Reid Spencer266e42b2006-12-23 06:05:41 +00001149/// particular relation to each other, we should return the corresponding ICmp
1150/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
Chris Lattner061da2f2004-01-13 05:51:55 +00001151///
1152/// To simplify this code we canonicalize the relation so that the first
1153/// operand is always the most "complex" of the two. We consider simple
1154/// constants (like ConstantInt) to be the simplest, followed by
Reid Spenceraccd7c72004-07-17 23:47:01 +00001155/// GlobalValues, followed by ConstantExpr's (the most complex).
Chris Lattner061da2f2004-01-13 05:51:55 +00001156///
Owen Anderson53a52212009-07-13 04:09:18 +00001157static ICmpInst::Predicate evaluateICmpRelation(LLVMContext &Context,
1158 const Constant *V1,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001159 const Constant *V2,
Reid Spencer266e42b2006-12-23 06:05:41 +00001160 bool isSigned) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001161 assert(V1->getType() == V2->getType() &&
1162 "Cannot compare different types of values!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001163 if (V1 == V2) return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001164
Reid Spenceraccd7c72004-07-17 23:47:01 +00001165 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1)) {
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001166 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2)) {
1167 // We distilled this down to a simple case, use the standard constant
1168 // folder.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001169 ConstantInt *R = 0;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001170 Constant *C1 = const_cast<Constant*>(V1);
1171 Constant *C2 = const_cast<Constant*>(V2);
Reid Spencer266e42b2006-12-23 06:05:41 +00001172 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
Owen Anderson487375e2009-07-29 18:55:55 +00001173 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001174 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001175 return pred;
1176 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Owen Anderson487375e2009-07-29 18:55:55 +00001177 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001178 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001179 return pred;
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001180 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Owen Anderson487375e2009-07-29 18:55:55 +00001181 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, C1, C2));
Reid Spencer2e54a152007-03-02 00:28:52 +00001182 if (R && !R->isZero())
Reid Spencer266e42b2006-12-23 06:05:41 +00001183 return pred;
Dan Gohman062d3782009-08-29 23:35:16 +00001184
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001185 // If we couldn't figure it out, bail.
Reid Spencer266e42b2006-12-23 06:05:41 +00001186 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001187 }
Dan Gohman062d3782009-08-29 23:35:16 +00001188
Chris Lattner061da2f2004-01-13 05:51:55 +00001189 // If the first operand is simple, swap operands.
Reid Spencer266e42b2006-12-23 06:05:41 +00001190 ICmpInst::Predicate SwappedRelation =
Owen Anderson53a52212009-07-13 04:09:18 +00001191 evaluateICmpRelation(Context, V2, V1, isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001192 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1193 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner061da2f2004-01-13 05:51:55 +00001194
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001195 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(V1)) {
Chris Lattner125ed542004-02-01 01:23:19 +00001196 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
Reid Spencer266e42b2006-12-23 06:05:41 +00001197 ICmpInst::Predicate SwappedRelation =
Owen Anderson53a52212009-07-13 04:09:18 +00001198 evaluateICmpRelation(Context, V2, V1, isSigned);
Reid Spencer266e42b2006-12-23 06:05:41 +00001199 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1200 return ICmpInst::getSwappedPredicate(SwappedRelation);
Chris Lattner0f7e9f52006-01-05 07:19:51 +00001201 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001202 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner125ed542004-02-01 01:23:19 +00001203 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001204
Reid Spenceraccd7c72004-07-17 23:47:01 +00001205 // Now we know that the RHS is a GlobalValue or simple constant,
Chris Lattner061da2f2004-01-13 05:51:55 +00001206 // which (since the types must match) means that it's a ConstantPointerNull.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001207 if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner52fe8692007-09-10 23:42:42 +00001208 // Don't try to decide equality of aliases.
1209 if (!isa<GlobalAlias>(CPR1) && !isa<GlobalAlias>(CPR2))
1210 if (!CPR1->hasExternalWeakLinkage() || !CPR2->hasExternalWeakLinkage())
1211 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001212 } else {
1213 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
Chris Lattner52fe8692007-09-10 23:42:42 +00001214 // GlobalVals can never be null. Don't try to evaluate aliases.
1215 if (!CPR1->hasExternalWeakLinkage() && !isa<GlobalAlias>(CPR1))
Reid Spencer266e42b2006-12-23 06:05:41 +00001216 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001217 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001218 } else {
1219 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1220 // constantexpr, a CPR, or a simple constant.
Reid Spencer9d36acf2006-12-24 18:52:08 +00001221 const ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1222 const Constant *CE1Op0 = CE1->getOperand(0);
Chris Lattner061da2f2004-01-13 05:51:55 +00001223
1224 switch (CE1->getOpcode()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001225 case Instruction::Trunc:
1226 case Instruction::FPTrunc:
1227 case Instruction::FPExt:
1228 case Instruction::FPToUI:
1229 case Instruction::FPToSI:
Reid Spencer266e42b2006-12-23 06:05:41 +00001230 break; // We can't evaluate floating point casts or truncations.
1231
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001232 case Instruction::UIToFP:
1233 case Instruction::SIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001234 case Instruction::BitCast:
Reid Spencer266e42b2006-12-23 06:05:41 +00001235 case Instruction::ZExt:
1236 case Instruction::SExt:
Chris Lattner061da2f2004-01-13 05:51:55 +00001237 // If the cast is not actually changing bits, and the second operand is a
1238 // null pointer, do the comparison with the pre-casted value.
1239 if (V2->isNullValue() &&
Chris Lattner03c49532007-01-15 02:27:26 +00001240 (isa<PointerType>(CE1->getType()) || CE1->getType()->isInteger())) {
Chris Lattnerd2265b42007-12-10 22:53:04 +00001241 bool sgnd = isSigned;
1242 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1243 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
Owen Anderson53a52212009-07-13 04:09:18 +00001244 return evaluateICmpRelation(Context, CE1Op0,
Owen Anderson5a1acd92009-07-31 20:28:14 +00001245 Constant::getNullValue(CE1Op0->getType()),
Chris Lattnerd2265b42007-12-10 22:53:04 +00001246 sgnd);
Reid Spencer266e42b2006-12-23 06:05:41 +00001247 }
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001248
1249 // If the dest type is a pointer type, and the RHS is a constantexpr cast
1250 // from the same type as the src of the LHS, evaluate the inputs. This is
Reid Spencer266e42b2006-12-23 06:05:41 +00001251 // important for things like "icmp eq (cast 4 to int*), (cast 5 to int*)",
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001252 // which happens a lot in compilers with tagged integers.
Reid Spencer9d36acf2006-12-24 18:52:08 +00001253 if (const ConstantExpr *CE2 = dyn_cast<ConstantExpr>(V2))
Reid Spencer266e42b2006-12-23 06:05:41 +00001254 if (CE2->isCast() && isa<PointerType>(CE1->getType()) &&
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001255 CE1->getOperand(0)->getType() == CE2->getOperand(0)->getType() &&
Chris Lattner03c49532007-01-15 02:27:26 +00001256 CE1->getOperand(0)->getType()->isInteger()) {
Chris Lattnerd2265b42007-12-10 22:53:04 +00001257 bool sgnd = isSigned;
1258 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1259 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
Owen Anderson53a52212009-07-13 04:09:18 +00001260 return evaluateICmpRelation(Context, CE1->getOperand(0),
1261 CE2->getOperand(0), sgnd);
Chris Lattnerfed8ceb2006-01-05 07:49:30 +00001262 }
Chris Lattner192e3262004-04-11 01:29:30 +00001263 break;
Chris Lattner061da2f2004-01-13 05:51:55 +00001264
1265 case Instruction::GetElementPtr:
1266 // Ok, since this is a getelementptr, we know that the constant has a
1267 // pointer type. Check the various cases.
1268 if (isa<ConstantPointerNull>(V2)) {
1269 // If we are comparing a GEP to a null pointer, check to see if the base
1270 // of the GEP equals the null pointer.
Reid Spencer9d36acf2006-12-24 18:52:08 +00001271 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001272 if (GV->hasExternalWeakLinkage())
1273 // Weak linkage GVals could be zero or not. We're comparing that
1274 // to null pointer so its greater-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001275 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
Reid Spencer876f7222006-12-06 00:25:09 +00001276 else
1277 // If its not weak linkage, the GVal must have a non-zero address
1278 // so the result is greater-than
Reid Spencer266e42b2006-12-23 06:05:41 +00001279 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001280 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1281 // If we are indexing from a null pointer, check to see if we have any
1282 // non-zero indices.
1283 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1284 if (!CE1->getOperand(i)->isNullValue())
1285 // Offsetting from null, must not be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001286 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001287 // Only zero indexes from null, must still be zero.
Reid Spencer266e42b2006-12-23 06:05:41 +00001288 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001289 }
1290 // Otherwise, we can't really say if the first operand is null or not.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001291 } else if (const GlobalValue *CPR2 = dyn_cast<GlobalValue>(V2)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001292 if (isa<ConstantPointerNull>(CE1Op0)) {
Reid Spencer876f7222006-12-06 00:25:09 +00001293 if (CPR2->hasExternalWeakLinkage())
1294 // Weak linkage GVals could be zero or not. We're comparing it to
1295 // a null pointer, so its less-or-equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001296 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
Reid Spencer876f7222006-12-06 00:25:09 +00001297 else
1298 // If its not weak linkage, the GVal must have a non-zero address
1299 // so the result is less-than
Reid Spencer266e42b2006-12-23 06:05:41 +00001300 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Reid Spenceraccd7c72004-07-17 23:47:01 +00001301 } else if (const GlobalValue *CPR1 = dyn_cast<GlobalValue>(CE1Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001302 if (CPR1 == CPR2) {
1303 // If this is a getelementptr of the same global, then it must be
1304 // different. Because the types must match, the getelementptr could
1305 // only have at most one index, and because we fold getelementptr's
1306 // with a single zero index, it must be nonzero.
1307 assert(CE1->getNumOperands() == 2 &&
1308 !CE1->getOperand(1)->isNullValue() &&
1309 "Suprising getelementptr!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001310 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner061da2f2004-01-13 05:51:55 +00001311 } else {
1312 // If they are different globals, we don't know what the value is,
1313 // but they can't be equal.
Reid Spencer266e42b2006-12-23 06:05:41 +00001314 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001315 }
1316 }
1317 } else {
1318 const ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1319 const Constant *CE2Op0 = CE2->getOperand(0);
1320
1321 // There are MANY other foldings that we could perform here. They will
1322 // probably be added on demand, as they seem needed.
1323 switch (CE2->getOpcode()) {
1324 default: break;
1325 case Instruction::GetElementPtr:
1326 // By far the most common case to handle is when the base pointers are
1327 // obviously to the same or different globals.
Reid Spenceraccd7c72004-07-17 23:47:01 +00001328 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
Chris Lattner061da2f2004-01-13 05:51:55 +00001329 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
Reid Spencer266e42b2006-12-23 06:05:41 +00001330 return ICmpInst::ICMP_NE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001331 // Ok, we know that both getelementptr instructions are based on the
1332 // same global. From this, we can precisely determine the relative
1333 // ordering of the resultant pointers.
1334 unsigned i = 1;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001335
Dan Gohman7190d482009-09-10 23:37:55 +00001336 // The logic below assumes that the result of the comparison
1337 // can be determined by finding the first index that differs.
1338 // This doesn't work if there is over-indexing in any
1339 // subsequent indices, so check for that case first.
1340 if (!CE1->isGEPWithNoNotionalOverIndexing() ||
1341 !CE2->isGEPWithNoNotionalOverIndexing())
1342 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1343
Chris Lattner061da2f2004-01-13 05:51:55 +00001344 // Compare all of the operands the GEP's have in common.
Chris Lattner60c47262005-01-28 19:09:51 +00001345 gep_type_iterator GTI = gep_type_begin(CE1);
1346 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1347 ++i, ++GTI)
Owen Anderson53a52212009-07-13 04:09:18 +00001348 switch (IdxCompare(Context, CE1->getOperand(i),
1349 CE2->getOperand(i), GTI.getIndexedType())) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001350 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1351 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1352 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001353 }
1354
1355 // Ok, we ran out of things they have in common. If any leftovers
1356 // are non-zero then we have a difference, otherwise we are equal.
1357 for (; i < CE1->getNumOperands(); ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001358 if (!CE1->getOperand(i)->isNullValue()) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001359 if (isa<ConstantInt>(CE1->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001360 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
Chris Lattner60c47262005-01-28 19:09:51 +00001361 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001362 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001363 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001364
Chris Lattner061da2f2004-01-13 05:51:55 +00001365 for (; i < CE2->getNumOperands(); ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001366 if (!CE2->getOperand(i)->isNullValue()) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001367 if (isa<ConstantInt>(CE2->getOperand(i)))
Reid Spencer266e42b2006-12-23 06:05:41 +00001368 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
Chris Lattner60c47262005-01-28 19:09:51 +00001369 else
Reid Spencer266e42b2006-12-23 06:05:41 +00001370 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001371 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001372 return ICmpInst::ICMP_EQ;
Chris Lattner061da2f2004-01-13 05:51:55 +00001373 }
1374 }
1375 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001376 default:
1377 break;
1378 }
1379 }
1380
Reid Spencer266e42b2006-12-23 06:05:41 +00001381 return ICmpInst::BAD_ICMP_PREDICATE;
Chris Lattner061da2f2004-01-13 05:51:55 +00001382}
1383
Owen Anderson53a52212009-07-13 04:09:18 +00001384Constant *llvm::ConstantFoldCompareInstruction(LLVMContext &Context,
1385 unsigned short pred,
Reid Spencer9d36acf2006-12-24 18:52:08 +00001386 const Constant *C1,
1387 const Constant *C2) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001388 const Type *ResultTy;
1389 if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
Owen Anderson55f1c092009-08-13 21:58:54 +00001390 ResultTy = VectorType::get(Type::getInt1Ty(Context), VT->getNumElements());
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001391 else
Owen Anderson55f1c092009-08-13 21:58:54 +00001392 ResultTy = Type::getInt1Ty(Context);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001393
Chris Lattnerd137a082008-07-08 05:46:34 +00001394 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001395 if (pred == FCmpInst::FCMP_FALSE)
Owen Anderson5a1acd92009-07-31 20:28:14 +00001396 return Constant::getNullValue(ResultTy);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001397
1398 if (pred == FCmpInst::FCMP_TRUE)
Owen Anderson5a1acd92009-07-31 20:28:14 +00001399 return Constant::getAllOnesValue(ResultTy);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001400
Reid Spencer266e42b2006-12-23 06:05:41 +00001401 // Handle some degenerate cases first
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001402 if (isa<UndefValue>(C1) || isa<UndefValue>(C2))
Owen Andersonb292b8c2009-07-30 23:03:37 +00001403 return UndefValue::get(ResultTy);
Reid Spencer266e42b2006-12-23 06:05:41 +00001404
Dale Johannesen19db0932007-10-14 01:56:47 +00001405 // No compile-time operations on this type yet.
Owen Anderson55f1c092009-08-13 21:58:54 +00001406 if (C1->getType() == Type::getPPC_FP128Ty(Context))
Dale Johannesen19db0932007-10-14 01:56:47 +00001407 return 0;
1408
Reid Spencer266e42b2006-12-23 06:05:41 +00001409 // icmp eq/ne(null,GV) -> false/true
1410 if (C1->isNullValue()) {
1411 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001412 // Don't try to evaluate aliases. External weak GV can be null.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001413 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001414 if (pred == ICmpInst::ICMP_EQ)
Owen Anderson23a204d2009-07-31 17:39:07 +00001415 return ConstantInt::getFalse(Context);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001416 else if (pred == ICmpInst::ICMP_NE)
Owen Anderson23a204d2009-07-31 17:39:07 +00001417 return ConstantInt::getTrue(Context);
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001418 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001419 // icmp eq/ne(GV,null) -> false/true
1420 } else if (C2->isNullValue()) {
1421 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
Duncan Sandsa38e5272007-09-19 10:16:17 +00001422 // Don't try to evaluate aliases. External weak GV can be null.
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001423 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
Reid Spencer9d36acf2006-12-24 18:52:08 +00001424 if (pred == ICmpInst::ICMP_EQ)
Owen Anderson23a204d2009-07-31 17:39:07 +00001425 return ConstantInt::getFalse(Context);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001426 else if (pred == ICmpInst::ICMP_NE)
Owen Anderson23a204d2009-07-31 17:39:07 +00001427 return ConstantInt::getTrue(Context);
Anton Korobeynikov579f0712008-02-20 11:08:44 +00001428 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001429 }
1430
Chris Lattner344da522007-01-12 18:42:52 +00001431 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
Reid Spencer81658a82007-02-27 06:23:51 +00001432 APInt V1 = cast<ConstantInt>(C1)->getValue();
1433 APInt V2 = cast<ConstantInt>(C2)->getValue();
1434 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001435 default: llvm_unreachable("Invalid ICmp Predicate"); return 0;
Owen Anderson53a52212009-07-13 04:09:18 +00001436 case ICmpInst::ICMP_EQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001437 return ConstantInt::get(Type::getInt1Ty(Context), V1 == V2);
Owen Anderson53a52212009-07-13 04:09:18 +00001438 case ICmpInst::ICMP_NE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001439 return ConstantInt::get(Type::getInt1Ty(Context), V1 != V2);
Owen Anderson53a52212009-07-13 04:09:18 +00001440 case ICmpInst::ICMP_SLT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001441 return ConstantInt::get(Type::getInt1Ty(Context), V1.slt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001442 case ICmpInst::ICMP_SGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001443 return ConstantInt::get(Type::getInt1Ty(Context), V1.sgt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001444 case ICmpInst::ICMP_SLE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001445 return ConstantInt::get(Type::getInt1Ty(Context), V1.sle(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001446 case ICmpInst::ICMP_SGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001447 return ConstantInt::get(Type::getInt1Ty(Context), V1.sge(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001448 case ICmpInst::ICMP_ULT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001449 return ConstantInt::get(Type::getInt1Ty(Context), V1.ult(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001450 case ICmpInst::ICMP_UGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001451 return ConstantInt::get(Type::getInt1Ty(Context), V1.ugt(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001452 case ICmpInst::ICMP_ULE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001453 return ConstantInt::get(Type::getInt1Ty(Context), V1.ule(V2));
Owen Anderson53a52212009-07-13 04:09:18 +00001454 case ICmpInst::ICMP_UGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001455 return ConstantInt::get(Type::getInt1Ty(Context), V1.uge(V2));
Chris Lattner061da2f2004-01-13 05:51:55 +00001456 }
Reid Spencer266e42b2006-12-23 06:05:41 +00001457 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001458 APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
1459 APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
1460 APFloat::cmpResult R = C1V.compare(C2V);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001461 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001462 default: llvm_unreachable("Invalid FCmp Predicate"); return 0;
Owen Anderson23a204d2009-07-31 17:39:07 +00001463 case FCmpInst::FCMP_FALSE: return ConstantInt::getFalse(Context);
1464 case FCmpInst::FCMP_TRUE: return ConstantInt::getTrue(Context);
Reid Spencer266e42b2006-12-23 06:05:41 +00001465 case FCmpInst::FCMP_UNO:
Owen Anderson55f1c092009-08-13 21:58:54 +00001466 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered);
Reid Spencer74bd0362007-01-11 00:25:45 +00001467 case FCmpInst::FCMP_ORD:
Owen Anderson55f1c092009-08-13 21:58:54 +00001468 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpUnordered);
Reid Spencer266e42b2006-12-23 06:05:41 +00001469 case FCmpInst::FCMP_UEQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001470 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001471 R==APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001472 case FCmpInst::FCMP_OEQ:
Owen Anderson55f1c092009-08-13 21:58:54 +00001473 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpEqual);
Reid Spencer74bd0362007-01-11 00:25:45 +00001474 case FCmpInst::FCMP_UNE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001475 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpEqual);
Reid Spencercddc9df2007-01-12 04:24:46 +00001476 case FCmpInst::FCMP_ONE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001477 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001478 R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001479 case FCmpInst::FCMP_ULT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001480 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001481 R==APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001482 case FCmpInst::FCMP_OLT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001483 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan);
Reid Spencer266e42b2006-12-23 06:05:41 +00001484 case FCmpInst::FCMP_UGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001485 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpUnordered ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001486 R==APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001487 case FCmpInst::FCMP_OGT:
Owen Anderson55f1c092009-08-13 21:58:54 +00001488 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpGreaterThan);
Reid Spencer74bd0362007-01-11 00:25:45 +00001489 case FCmpInst::FCMP_ULE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001490 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpGreaterThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001491 case FCmpInst::FCMP_OLE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001492 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpLessThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001493 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001494 case FCmpInst::FCMP_UGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001495 return ConstantInt::get(Type::getInt1Ty(Context), R!=APFloat::cmpLessThan);
Reid Spencercddc9df2007-01-12 04:24:46 +00001496 case FCmpInst::FCMP_OGE:
Owen Anderson55f1c092009-08-13 21:58:54 +00001497 return ConstantInt::get(Type::getInt1Ty(Context), R==APFloat::cmpGreaterThan ||
Dale Johannesenbed9dc42007-09-06 18:13:44 +00001498 R==APFloat::cmpEqual);
Reid Spencer266e42b2006-12-23 06:05:41 +00001499 }
Chris Lattner67136cf2008-07-10 00:29:28 +00001500 } else if (isa<VectorType>(C1->getType())) {
1501 SmallVector<Constant*, 16> C1Elts, C2Elts;
Owen Anderson53a52212009-07-13 04:09:18 +00001502 C1->getVectorElements(Context, C1Elts);
1503 C2->getVectorElements(Context, C2Elts);
Dan Gohman062d3782009-08-29 23:35:16 +00001504
Chris Lattner67136cf2008-07-10 00:29:28 +00001505 // If we can constant fold the comparison of each element, constant fold
1506 // the whole vector comparison.
1507 SmallVector<Constant*, 4> ResElts;
Chris Lattner67136cf2008-07-10 00:29:28 +00001508 for (unsigned i = 0, e = C1Elts.size(); i != e; ++i) {
1509 // Compare the elements, producing an i1 result or constant expr.
Owen Anderson53a52212009-07-13 04:09:18 +00001510 ResElts.push_back(
Owen Anderson487375e2009-07-29 18:55:55 +00001511 ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i]));
Reid Spencer266e42b2006-12-23 06:05:41 +00001512 }
Owen Anderson4aa32952009-07-28 21:19:26 +00001513 return ConstantVector::get(&ResElts[0], ResElts.size());
Reid Spencer266e42b2006-12-23 06:05:41 +00001514 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001515
Reid Spencer9d36acf2006-12-24 18:52:08 +00001516 if (C1->getType()->isFloatingPoint()) {
Chris Lattner350e4172008-07-08 18:47:38 +00001517 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
Owen Anderson53a52212009-07-13 04:09:18 +00001518 switch (evaluateFCmpRelation(Context, C1, C2)) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001519 default: llvm_unreachable("Unknown relation!");
Reid Spencer9d36acf2006-12-24 18:52:08 +00001520 case FCmpInst::FCMP_UNO:
1521 case FCmpInst::FCMP_ORD:
1522 case FCmpInst::FCMP_UEQ:
1523 case FCmpInst::FCMP_UNE:
1524 case FCmpInst::FCMP_ULT:
1525 case FCmpInst::FCMP_UGT:
1526 case FCmpInst::FCMP_ULE:
1527 case FCmpInst::FCMP_UGE:
1528 case FCmpInst::FCMP_TRUE:
1529 case FCmpInst::FCMP_FALSE:
1530 case FCmpInst::BAD_FCMP_PREDICATE:
1531 break; // Couldn't determine anything about these constants.
1532 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001533 Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1534 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1535 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1536 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001537 case FCmpInst::FCMP_OLT: // We know that C1 < C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001538 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1539 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
1540 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
1541 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001542 case FCmpInst::FCMP_OGT: // We know that C1 > C2
Chris Lattnerd137a082008-07-08 05:46:34 +00001543 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
1544 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
1545 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1546 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001547 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
1548 // We can only partially decide this relation.
1549 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
Chris Lattnerd137a082008-07-08 05:46:34 +00001550 Result = 0;
1551 else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
1552 Result = 1;
Chris Lattner061da2f2004-01-13 05:51:55 +00001553 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001554 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
1555 // We can only partially decide this relation.
1556 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
Chris Lattnerd137a082008-07-08 05:46:34 +00001557 Result = 0;
1558 else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
1559 Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001560 break;
1561 case ICmpInst::ICMP_NE: // We know that C1 != C2
1562 // We can only partially decide this relation.
1563 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
Chris Lattnerd137a082008-07-08 05:46:34 +00001564 Result = 0;
1565 else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
1566 Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001567 break;
1568 }
Dan Gohman062d3782009-08-29 23:35:16 +00001569
Chris Lattnerd137a082008-07-08 05:46:34 +00001570 // If we evaluated the result, return it now.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001571 if (Result != -1)
Owen Anderson55f1c092009-08-13 21:58:54 +00001572 return ConstantInt::get(Type::getInt1Ty(Context), Result);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001573
Reid Spencer9d36acf2006-12-24 18:52:08 +00001574 } else {
1575 // Evaluate the relation between the two constants, per the predicate.
Chris Lattnerd137a082008-07-08 05:46:34 +00001576 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
Owen Anderson53a52212009-07-13 04:09:18 +00001577 switch (evaluateICmpRelation(Context, C1, C2, CmpInst::isSigned(pred))) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001578 default: llvm_unreachable("Unknown relational!");
Reid Spencer9d36acf2006-12-24 18:52:08 +00001579 case ICmpInst::BAD_ICMP_PREDICATE:
1580 break; // Couldn't determine anything about these constants.
1581 case ICmpInst::ICMP_EQ: // We know the constants are equal!
1582 // If we know the constants are equal, we can decide the result of this
1583 // computation precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001584 Result = (pred == ICmpInst::ICMP_EQ ||
1585 pred == ICmpInst::ICMP_ULE ||
1586 pred == ICmpInst::ICMP_SLE ||
1587 pred == ICmpInst::ICMP_UGE ||
1588 pred == ICmpInst::ICMP_SGE);
1589 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001590 case ICmpInst::ICMP_ULT:
1591 // If we know that C1 < C2, we can decide the result of this computation
1592 // precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001593 Result = (pred == ICmpInst::ICMP_ULT ||
1594 pred == ICmpInst::ICMP_NE ||
1595 pred == ICmpInst::ICMP_ULE);
1596 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001597 case ICmpInst::ICMP_SLT:
1598 // If we know that C1 < C2, we can decide the result of this computation
1599 // precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001600 Result = (pred == ICmpInst::ICMP_SLT ||
1601 pred == ICmpInst::ICMP_NE ||
1602 pred == ICmpInst::ICMP_SLE);
1603 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001604 case ICmpInst::ICMP_UGT:
1605 // If we know that C1 > C2, we can decide the result of this computation
1606 // precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001607 Result = (pred == ICmpInst::ICMP_UGT ||
1608 pred == ICmpInst::ICMP_NE ||
1609 pred == ICmpInst::ICMP_UGE);
1610 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001611 case ICmpInst::ICMP_SGT:
1612 // If we know that C1 > C2, we can decide the result of this computation
1613 // precisely.
Chris Lattnerd137a082008-07-08 05:46:34 +00001614 Result = (pred == ICmpInst::ICMP_SGT ||
1615 pred == ICmpInst::ICMP_NE ||
1616 pred == ICmpInst::ICMP_SGE);
1617 break;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001618 case ICmpInst::ICMP_ULE:
1619 // If we know that C1 <= C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001620 if (pred == ICmpInst::ICMP_UGT) Result = 0;
1621 if (pred == ICmpInst::ICMP_ULT) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001622 break;
1623 case ICmpInst::ICMP_SLE:
1624 // If we know that C1 <= C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001625 if (pred == ICmpInst::ICMP_SGT) Result = 0;
1626 if (pred == ICmpInst::ICMP_SLT) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001627 break;
1628
1629 case ICmpInst::ICMP_UGE:
1630 // If we know that C1 >= C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001631 if (pred == ICmpInst::ICMP_ULT) Result = 0;
1632 if (pred == ICmpInst::ICMP_UGT) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001633 break;
1634 case ICmpInst::ICMP_SGE:
1635 // If we know that C1 >= C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001636 if (pred == ICmpInst::ICMP_SLT) Result = 0;
1637 if (pred == ICmpInst::ICMP_SGT) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001638 break;
1639
1640 case ICmpInst::ICMP_NE:
1641 // If we know that C1 != C2, we can only partially decide this relation.
Chris Lattnerd137a082008-07-08 05:46:34 +00001642 if (pred == ICmpInst::ICMP_EQ) Result = 0;
1643 if (pred == ICmpInst::ICMP_NE) Result = 1;
Reid Spencer9d36acf2006-12-24 18:52:08 +00001644 break;
1645 }
Dan Gohman062d3782009-08-29 23:35:16 +00001646
Chris Lattnerd137a082008-07-08 05:46:34 +00001647 // If we evaluated the result, return it now.
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001648 if (Result != -1)
Owen Anderson55f1c092009-08-13 21:58:54 +00001649 return ConstantInt::get(Type::getInt1Ty(Context), Result);
Dan Gohman062d3782009-08-29 23:35:16 +00001650
Reid Spencer9d36acf2006-12-24 18:52:08 +00001651 if (!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00001652 // If C2 is a constant expr and C1 isn't, flip them around and fold the
Reid Spencer9d36acf2006-12-24 18:52:08 +00001653 // other way if possible.
1654 switch (pred) {
1655 case ICmpInst::ICMP_EQ:
1656 case ICmpInst::ICMP_NE:
1657 // No change of predicate required.
Owen Anderson53a52212009-07-13 04:09:18 +00001658 return ConstantFoldCompareInstruction(Context, pred, C2, C1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001659
1660 case ICmpInst::ICMP_ULT:
1661 case ICmpInst::ICMP_SLT:
1662 case ICmpInst::ICMP_UGT:
1663 case ICmpInst::ICMP_SGT:
1664 case ICmpInst::ICMP_ULE:
1665 case ICmpInst::ICMP_SLE:
1666 case ICmpInst::ICMP_UGE:
1667 case ICmpInst::ICMP_SGE:
1668 // Change the predicate as necessary to swap the operands.
1669 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
Owen Anderson53a52212009-07-13 04:09:18 +00001670 return ConstantFoldCompareInstruction(Context, pred, C2, C1);
Reid Spencer9d36acf2006-12-24 18:52:08 +00001671
1672 default: // These predicates cannot be flopped around.
1673 break;
1674 }
Chris Lattner061da2f2004-01-13 05:51:55 +00001675 }
1676 }
1677 return 0;
Dan Gohman062d3782009-08-29 23:35:16 +00001678}
Chris Lattner1dd054c2004-01-12 22:07:24 +00001679
Dan Gohman21c62162009-09-11 00:04:14 +00001680/// isInBoundsIndices - Test whether the given sequence of *normalized* indices
1681/// is "inbounds".
1682static bool isInBoundsIndices(Constant *const *Idxs, size_t NumIdx) {
1683 // No indices means nothing that could be out of bounds.
1684 if (NumIdx == 0) return true;
1685
1686 // If the first index is zero, it's in bounds.
1687 if (Idxs[0]->isNullValue()) return true;
1688
1689 // If the first index is one and all the rest are zero, it's in bounds,
1690 // by the one-past-the-end rule.
1691 if (!cast<ConstantInt>(Idxs[0])->isOne())
1692 return false;
1693 for (unsigned i = 1, e = NumIdx; i != e; ++i)
1694 if (!Idxs[i]->isNullValue())
1695 return false;
1696 return true;
1697}
1698
Owen Anderson53a52212009-07-13 04:09:18 +00001699Constant *llvm::ConstantFoldGetElementPtr(LLVMContext &Context,
1700 const Constant *C,
Dan Gohman21c62162009-09-11 00:04:14 +00001701 bool inBounds,
David Greenec656cbb2007-09-04 15:46:09 +00001702 Constant* const *Idxs,
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001703 unsigned NumIdx) {
Chris Lattner302116a2007-01-31 04:40:28 +00001704 if (NumIdx == 0 ||
1705 (NumIdx == 1 && Idxs[0]->isNullValue()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001706 return const_cast<Constant*>(C);
1707
Chris Lattnerf6013752004-10-17 21:54:55 +00001708 if (isa<UndefValue>(C)) {
Christopher Lambedf07882007-12-17 01:12:55 +00001709 const PointerType *Ptr = cast<PointerType>(C->getType());
1710 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
David Greenec656cbb2007-09-04 15:46:09 +00001711 (Value **)Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001712 (Value **)Idxs+NumIdx);
Chris Lattnerf6013752004-10-17 21:54:55 +00001713 assert(Ty != 0 && "Invalid indices for GEP!");
Owen Andersonb292b8c2009-07-30 23:03:37 +00001714 return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
Chris Lattnerf6013752004-10-17 21:54:55 +00001715 }
1716
Chris Lattner302116a2007-01-31 04:40:28 +00001717 Constant *Idx0 = Idxs[0];
Chris Lattner04b60fe2004-02-16 20:46:13 +00001718 if (C->isNullValue()) {
1719 bool isNull = true;
Chris Lattner302116a2007-01-31 04:40:28 +00001720 for (unsigned i = 0, e = NumIdx; i != e; ++i)
1721 if (!Idxs[i]->isNullValue()) {
Chris Lattner04b60fe2004-02-16 20:46:13 +00001722 isNull = false;
1723 break;
1724 }
1725 if (isNull) {
Christopher Lambedf07882007-12-17 01:12:55 +00001726 const PointerType *Ptr = cast<PointerType>(C->getType());
1727 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr,
David Greenec656cbb2007-09-04 15:46:09 +00001728 (Value**)Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001729 (Value**)Idxs+NumIdx);
Chris Lattner04b60fe2004-02-16 20:46:13 +00001730 assert(Ty != 0 && "Invalid indices for GEP!");
Owen Andersonb292b8c2009-07-30 23:03:37 +00001731 return ConstantPointerNull::get(
Owen Anderson4056ca92009-07-29 22:17:13 +00001732 PointerType::get(Ty,Ptr->getAddressSpace()));
Chris Lattner04b60fe2004-02-16 20:46:13 +00001733 }
1734 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001735
1736 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(const_cast<Constant*>(C))) {
1737 // Combine Indices - If the source pointer to this getelementptr instruction
1738 // is a getelementptr instruction, combine the indices of the two
1739 // getelementptr instructions into a single instruction.
1740 //
1741 if (CE->getOpcode() == Instruction::GetElementPtr) {
1742 const Type *LastTy = 0;
1743 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
1744 I != E; ++I)
1745 LastTy = *I;
1746
Chris Lattner13128ab2004-10-11 22:52:25 +00001747 if ((LastTy && isa<ArrayType>(LastTy)) || Idx0->isNullValue()) {
Chris Lattner302116a2007-01-31 04:40:28 +00001748 SmallVector<Value*, 16> NewIndices;
1749 NewIndices.reserve(NumIdx + CE->getNumOperands());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001750 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
Chris Lattner13128ab2004-10-11 22:52:25 +00001751 NewIndices.push_back(CE->getOperand(i));
Chris Lattner1dd054c2004-01-12 22:07:24 +00001752
1753 // Add the last index of the source with the first index of the new GEP.
1754 // Make sure to handle the case when they are actually different types.
1755 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
Chris Lattner13128ab2004-10-11 22:52:25 +00001756 // Otherwise it must be an array.
1757 if (!Idx0->isNullValue()) {
Chris Lattner71068a02004-07-07 04:45:13 +00001758 const Type *IdxTy = Combined->getType();
Reid Spencer1a063892006-12-04 02:46:44 +00001759 if (IdxTy != Idx0->getType()) {
Owen Anderson53a52212009-07-13 04:09:18 +00001760 Constant *C1 =
Owen Anderson55f1c092009-08-13 21:58:54 +00001761 ConstantExpr::getSExtOrBitCast(Idx0, Type::getInt64Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001762 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined,
Owen Anderson55f1c092009-08-13 21:58:54 +00001763 Type::getInt64Ty(Context));
Owen Anderson487375e2009-07-29 18:55:55 +00001764 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
Reid Spencer1a063892006-12-04 02:46:44 +00001765 } else {
1766 Combined =
Owen Anderson487375e2009-07-29 18:55:55 +00001767 ConstantExpr::get(Instruction::Add, Idx0, Combined);
Reid Spencer1a063892006-12-04 02:46:44 +00001768 }
Chris Lattner71068a02004-07-07 04:45:13 +00001769 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001770
Chris Lattner1dd054c2004-01-12 22:07:24 +00001771 NewIndices.push_back(Combined);
Chris Lattner302116a2007-01-31 04:40:28 +00001772 NewIndices.insert(NewIndices.end(), Idxs+1, Idxs+NumIdx);
Dan Gohman21c62162009-09-11 00:04:14 +00001773 return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ?
1774 ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0),
1775 &NewIndices[0],
1776 NewIndices.size()) :
1777 ConstantExpr::getGetElementPtr(CE->getOperand(0),
1778 &NewIndices[0],
1779 NewIndices.size());
Chris Lattner1dd054c2004-01-12 22:07:24 +00001780 }
1781 }
1782
1783 // Implement folding of:
1784 // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
1785 // long 0, long 0)
1786 // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
1787 //
Chris Lattneraadc7782007-08-13 17:09:08 +00001788 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
Misha Brukmanb1c93172005-04-21 23:48:37 +00001789 if (const PointerType *SPT =
Chris Lattner1dd054c2004-01-12 22:07:24 +00001790 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
1791 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
1792 if (const ArrayType *CAT =
Chris Lattner02157b02006-06-28 21:38:54 +00001793 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
Chris Lattner1dd054c2004-01-12 22:07:24 +00001794 if (CAT->getElementType() == SAT->getElementType())
Dan Gohman21c62162009-09-11 00:04:14 +00001795 return inBounds ?
1796 ConstantExpr::getInBoundsGetElementPtr(
1797 (Constant*)CE->getOperand(0), Idxs, NumIdx) :
1798 ConstantExpr::getGetElementPtr(
Owen Anderson0d2de8c2009-06-20 00:24:58 +00001799 (Constant*)CE->getOperand(0), Idxs, NumIdx);
Chris Lattneraadc7782007-08-13 17:09:08 +00001800 }
Dan Gohman062d3782009-08-29 23:35:16 +00001801
Chris Lattneraadc7782007-08-13 17:09:08 +00001802 // Fold: getelementptr (i8* inttoptr (i64 1 to i8*), i32 -1)
1803 // Into: inttoptr (i64 0 to i8*)
1804 // This happens with pointers to member functions in C++.
1805 if (CE->getOpcode() == Instruction::IntToPtr && NumIdx == 1 &&
1806 isa<ConstantInt>(CE->getOperand(0)) && isa<ConstantInt>(Idxs[0]) &&
Owen Anderson55f1c092009-08-13 21:58:54 +00001807 cast<PointerType>(CE->getType())->getElementType() == Type::getInt8Ty(Context)) {
Chris Lattneraadc7782007-08-13 17:09:08 +00001808 Constant *Base = CE->getOperand(0);
1809 Constant *Offset = Idxs[0];
Dan Gohman062d3782009-08-29 23:35:16 +00001810
Chris Lattneraadc7782007-08-13 17:09:08 +00001811 // Convert the smaller integer to the larger type.
1812 if (Offset->getType()->getPrimitiveSizeInBits() <
1813 Base->getType()->getPrimitiveSizeInBits())
Owen Anderson487375e2009-07-29 18:55:55 +00001814 Offset = ConstantExpr::getSExt(Offset, Base->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001815 else if (Base->getType()->getPrimitiveSizeInBits() <
1816 Offset->getType()->getPrimitiveSizeInBits())
Owen Anderson487375e2009-07-29 18:55:55 +00001817 Base = ConstantExpr::getZExt(Base, Offset->getType());
Dan Gohman062d3782009-08-29 23:35:16 +00001818
Owen Anderson487375e2009-07-29 18:55:55 +00001819 Base = ConstantExpr::getAdd(Base, Offset);
1820 return ConstantExpr::getIntToPtr(Base, CE->getType());
Chris Lattneraadc7782007-08-13 17:09:08 +00001821 }
Chris Lattner1dd054c2004-01-12 22:07:24 +00001822 }
Dan Gohman21c62162009-09-11 00:04:14 +00001823
1824 // Check to see if any array indices are not within the corresponding
1825 // notional array bounds. If so, try to determine if they can be factored
1826 // out into preceding dimensions.
1827 bool Unknown = false;
1828 SmallVector<Constant *, 8> NewIdxs;
1829 const Type *Ty = C->getType();
1830 const Type *Prev = 0;
1831 for (unsigned i = 0; i != NumIdx;
1832 Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
1833 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
1834 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
1835 if (ATy->getNumElements() <= INT64_MAX &&
1836 ATy->getNumElements() != 0 &&
1837 CI->getSExtValue() >= (int64_t)ATy->getNumElements()) {
1838 if (isa<SequentialType>(Prev)) {
1839 // It's out of range, but we can factor it into the prior
1840 // dimension.
1841 NewIdxs.resize(NumIdx);
1842 ConstantInt *Factor = ConstantInt::get(CI->getType(),
1843 ATy->getNumElements());
1844 NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
1845
1846 Constant *PrevIdx = Idxs[i-1];
1847 Constant *Div = ConstantExpr::getSDiv(CI, Factor);
1848
1849 // Before adding, extend both operands to i64 to avoid
1850 // overflow trouble.
1851 if (PrevIdx->getType() != Type::getInt64Ty(Context))
1852 PrevIdx = ConstantExpr::getSExt(PrevIdx,
1853 Type::getInt64Ty(Context));
1854 if (Div->getType() != Type::getInt64Ty(Context))
1855 Div = ConstantExpr::getSExt(Div,
1856 Type::getInt64Ty(Context));
1857
1858 NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
1859 } else {
1860 // It's out of range, but the prior dimension is a struct
1861 // so we can't do anything about it.
1862 Unknown = true;
1863 }
1864 }
1865 } else {
1866 // We don't know if it's in range or not.
1867 Unknown = true;
1868 }
1869 }
1870
1871 // If we did any factoring, start over with the adjusted indices.
1872 if (!NewIdxs.empty()) {
1873 for (unsigned i = 0; i != NumIdx; ++i)
1874 if (!NewIdxs[i]) NewIdxs[i] = Idxs[i];
1875 return inBounds ?
1876 ConstantExpr::getGetElementPtr(const_cast<Constant*>(C),
1877 NewIdxs.data(), NewIdxs.size()) :
1878 ConstantExpr::getInBoundsGetElementPtr(const_cast<Constant*>(C),
1879 NewIdxs.data(), NewIdxs.size());
1880 }
1881
1882 // If all indices are known integers and normalized, we can do a simple
1883 // check for the "inbounds" property.
1884 if (!Unknown && !inBounds &&
1885 isa<GlobalVariable>(C) && isInBoundsIndices(Idxs, NumIdx))
1886 return ConstantExpr::getInBoundsGetElementPtr(const_cast<Constant*>(C),
1887 Idxs, NumIdx);
1888
Chris Lattner1dd054c2004-01-12 22:07:24 +00001889 return 0;
1890}