1 //===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements folding of constants for LLVM. This implements the
11 // (internal) ConstantFold.h interface, which is used by the
12 // ConstantExpr::get* methods to automatically fold constants when possible.
14 // The current constant folding implementation is implemented in two pieces: the
15 // pieces that don't need TargetData, and the pieces that do. This is to avoid
16 // a dependence in VMCore on Target.
18 //===----------------------------------------------------------------------===//
20 #include "ConstantFold.h"
21 #include "llvm/Constants.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Function.h"
25 #include "llvm/GlobalAlias.h"
26 #include "llvm/GlobalVariable.h"
27 #include "llvm/Operator.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/GetElementPtrTypeIterator.h"
32 #include "llvm/Support/ManagedStatic.h"
33 #include "llvm/Support/MathExtras.h"
37 //===----------------------------------------------------------------------===//
38 // ConstantFold*Instruction Implementations
39 //===----------------------------------------------------------------------===//
41 /// BitCastConstantVector - Convert the specified ConstantVector node to the
42 /// specified vector type. At this point, we know that the elements of the
43 /// input vector constant are all simple integer or FP values.
44 static Constant *BitCastConstantVector(ConstantVector *CV,
45 const VectorType *DstTy) {
47 if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
48 if (CV->isNullValue()) return Constant::getNullValue(DstTy);
50 // If this cast changes element count then we can't handle it here:
51 // doing so requires endianness information. This should be handled by
52 // Analysis/ConstantFolding.cpp
53 unsigned NumElts = DstTy->getNumElements();
54 if (NumElts != CV->getNumOperands())
57 // Check to verify that all elements of the input are simple.
58 for (unsigned i = 0; i != NumElts; ++i) {
59 if (!isa<ConstantInt>(CV->getOperand(i)) &&
60 !isa<ConstantFP>(CV->getOperand(i)))
64 // Bitcast each element now.
65 std::vector<Constant*> Result;
66 const Type *DstEltTy = DstTy->getElementType();
67 for (unsigned i = 0; i != NumElts; ++i)
68 Result.push_back(ConstantExpr::getBitCast(CV->getOperand(i),
70 return ConstantVector::get(Result);
73 /// This function determines which opcode to use to fold two constant cast
74 /// expressions together. It uses CastInst::isEliminableCastPair to determine
75 /// the opcode. Consequently its just a wrapper around that function.
76 /// @brief Determine if it is valid to fold a cast of a cast
79 unsigned opc, ///< opcode of the second cast constant expression
80 ConstantExpr *Op, ///< the first cast constant expression
81 const Type *DstTy ///< desintation type of the first cast
83 assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
84 assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
85 assert(CastInst::isCast(opc) && "Invalid cast opcode");
87 // The the types and opcodes for the two Cast constant expressions
88 const Type *SrcTy = Op->getOperand(0)->getType();
89 const Type *MidTy = Op->getType();
90 Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
91 Instruction::CastOps secondOp = Instruction::CastOps(opc);
93 // Let CastInst::isEliminableCastPair do the heavy lifting.
94 return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
95 Type::getInt64Ty(DstTy->getContext()));
98 static Constant *FoldBitCast(Constant *V, const Type *DestTy) {
99 const Type *SrcTy = V->getType();
101 return V; // no-op cast
103 // Check to see if we are casting a pointer to an aggregate to a pointer to
104 // the first element. If so, return the appropriate GEP instruction.
105 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType()))
106 if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy))
107 if (PTy->getAddressSpace() == DPTy->getAddressSpace()) {
108 SmallVector<Value*, 8> IdxList;
110 Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
111 IdxList.push_back(Zero);
112 const Type *ElTy = PTy->getElementType();
113 while (ElTy != DPTy->getElementType()) {
114 if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
115 if (STy->getNumElements() == 0) break;
116 ElTy = STy->getElementType(0);
117 IdxList.push_back(Zero);
118 } else if (const SequentialType *STy =
119 dyn_cast<SequentialType>(ElTy)) {
120 if (ElTy->isPointerTy()) break; // Can't index into pointers!
121 ElTy = STy->getElementType();
122 IdxList.push_back(Zero);
128 if (ElTy == DPTy->getElementType())
129 // This GEP is inbounds because all indices are zero.
130 return ConstantExpr::getInBoundsGetElementPtr(V, &IdxList[0],
134 // Handle casts from one vector constant to another. We know that the src
135 // and dest type have the same size (otherwise its an illegal cast).
136 if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
137 if (const VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
138 assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
139 "Not cast between same sized vectors!");
141 // First, check for null. Undef is already handled.
142 if (isa<ConstantAggregateZero>(V))
143 return Constant::getNullValue(DestTy);
145 if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
146 return BitCastConstantVector(CV, DestPTy);
149 // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
150 // This allows for other simplifications (although some of them
151 // can only be handled by Analysis/ConstantFolding.cpp).
152 if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
153 return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
156 // Finally, implement bitcast folding now. The code below doesn't handle
158 if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
159 return ConstantPointerNull::get(cast<PointerType>(DestTy));
161 // Handle integral constant input.
162 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
163 if (DestTy->isIntegerTy())
164 // Integral -> Integral. This is a no-op because the bit widths must
165 // be the same. Consequently, we just fold to V.
168 if (DestTy->isFloatingPointTy())
169 return ConstantFP::get(DestTy->getContext(),
170 APFloat(CI->getValue(),
171 !DestTy->isPPC_FP128Ty()));
173 // Otherwise, can't fold this (vector?)
177 // Handle ConstantFP input: FP -> Integral.
178 if (ConstantFP *FP = dyn_cast<ConstantFP>(V))
179 return ConstantInt::get(FP->getContext(),
180 FP->getValueAPF().bitcastToAPInt());
186 /// ExtractConstantBytes - V is an integer constant which only has a subset of
187 /// its bytes used. The bytes used are indicated by ByteStart (which is the
188 /// first byte used, counting from the least significant byte) and ByteSize,
189 /// which is the number of bytes used.
191 /// This function analyzes the specified constant to see if the specified byte
192 /// range can be returned as a simplified constant. If so, the constant is
193 /// returned, otherwise null is returned.
195 static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
197 assert(C->getType()->isIntegerTy() &&
198 (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
199 "Non-byte sized integer input");
200 unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
201 assert(ByteSize && "Must be accessing some piece");
202 assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
203 assert(ByteSize != CSize && "Should not extract everything");
205 // Constant Integers are simple.
206 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
207 APInt V = CI->getValue();
209 V = V.lshr(ByteStart*8);
210 V = V.trunc(ByteSize*8);
211 return ConstantInt::get(CI->getContext(), V);
214 // In the input is a constant expr, we might be able to recursively simplify.
215 // If not, we definitely can't do anything.
216 ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
217 if (CE == 0) return 0;
219 switch (CE->getOpcode()) {
221 case Instruction::Or: {
222 Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
227 if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
228 if (RHSC->isAllOnesValue())
231 Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
234 return ConstantExpr::getOr(LHS, RHS);
236 case Instruction::And: {
237 Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
242 if (RHS->isNullValue())
245 Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
248 return ConstantExpr::getAnd(LHS, RHS);
250 case Instruction::LShr: {
251 ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
254 unsigned ShAmt = Amt->getZExtValue();
255 // Cannot analyze non-byte shifts.
256 if ((ShAmt & 7) != 0)
260 // If the extract is known to be all zeros, return zero.
261 if (ByteStart >= CSize-ShAmt)
262 return Constant::getNullValue(IntegerType::get(CE->getContext(),
264 // If the extract is known to be fully in the input, extract it.
265 if (ByteStart+ByteSize+ShAmt <= CSize)
266 return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize);
268 // TODO: Handle the 'partially zero' case.
272 case Instruction::Shl: {
273 ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
276 unsigned ShAmt = Amt->getZExtValue();
277 // Cannot analyze non-byte shifts.
278 if ((ShAmt & 7) != 0)
282 // If the extract is known to be all zeros, return zero.
283 if (ByteStart+ByteSize <= ShAmt)
284 return Constant::getNullValue(IntegerType::get(CE->getContext(),
286 // If the extract is known to be fully in the input, extract it.
287 if (ByteStart >= ShAmt)
288 return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize);
290 // TODO: Handle the 'partially zero' case.
294 case Instruction::ZExt: {
295 unsigned SrcBitSize =
296 cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
298 // If extracting something that is completely zero, return 0.
299 if (ByteStart*8 >= SrcBitSize)
300 return Constant::getNullValue(IntegerType::get(CE->getContext(),
303 // If exactly extracting the input, return it.
304 if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
305 return CE->getOperand(0);
307 // If extracting something completely in the input, if if the input is a
308 // multiple of 8 bits, recurse.
309 if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
310 return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
312 // Otherwise, if extracting a subset of the input, which is not multiple of
313 // 8 bits, do a shift and trunc to get the bits.
314 if ((ByteStart+ByteSize)*8 < SrcBitSize) {
315 assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
316 Constant *Res = CE->getOperand(0);
318 Res = ConstantExpr::getLShr(Res,
319 ConstantInt::get(Res->getType(), ByteStart*8));
320 return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
324 // TODO: Handle the 'partially zero' case.
330 /// getFoldedSizeOf - Return a ConstantExpr with type DestTy for sizeof
331 /// on Ty, with any known factors factored out. If Folded is false,
332 /// return null if no factoring was possible, to avoid endlessly
333 /// bouncing an unfoldable expression back into the top-level folder.
335 static Constant *getFoldedSizeOf(const Type *Ty, const Type *DestTy,
337 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
338 Constant *N = ConstantInt::get(DestTy, ATy->getNumElements());
339 Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
340 return ConstantExpr::getNUWMul(E, N);
343 if (const StructType *STy = dyn_cast<StructType>(Ty))
344 if (!STy->isPacked()) {
345 unsigned NumElems = STy->getNumElements();
346 // An empty struct has size zero.
348 return ConstantExpr::getNullValue(DestTy);
349 // Check for a struct with all members having the same size.
350 Constant *MemberSize =
351 getFoldedSizeOf(STy->getElementType(0), DestTy, true);
353 for (unsigned i = 1; i != NumElems; ++i)
355 getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
360 Constant *N = ConstantInt::get(DestTy, NumElems);
361 return ConstantExpr::getNUWMul(MemberSize, N);
365 // Pointer size doesn't depend on the pointee type, so canonicalize them
366 // to an arbitrary pointee.
367 if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
368 if (!PTy->getElementType()->isIntegerTy(1))
370 getFoldedSizeOf(PointerType::get(IntegerType::get(PTy->getContext(), 1),
371 PTy->getAddressSpace()),
374 // If there's no interesting folding happening, bail so that we don't create
375 // a constant that looks like it needs folding but really doesn't.
379 // Base case: Get a regular sizeof expression.
380 Constant *C = ConstantExpr::getSizeOf(Ty);
381 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
387 /// getFoldedAlignOf - Return a ConstantExpr with type DestTy for alignof
388 /// on Ty, with any known factors factored out. If Folded is false,
389 /// return null if no factoring was possible, to avoid endlessly
390 /// bouncing an unfoldable expression back into the top-level folder.
392 static Constant *getFoldedAlignOf(const Type *Ty, const Type *DestTy,
394 // The alignment of an array is equal to the alignment of the
395 // array element. Note that this is not always true for vectors.
396 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
397 Constant *C = ConstantExpr::getAlignOf(ATy->getElementType());
398 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
405 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
406 // Packed structs always have an alignment of 1.
408 return ConstantInt::get(DestTy, 1);
410 // Otherwise, struct alignment is the maximum alignment of any member.
411 // Without target data, we can't compare much, but we can check to see
412 // if all the members have the same alignment.
413 unsigned NumElems = STy->getNumElements();
414 // An empty struct has minimal alignment.
416 return ConstantInt::get(DestTy, 1);
417 // Check for a struct with all members having the same alignment.
418 Constant *MemberAlign =
419 getFoldedAlignOf(STy->getElementType(0), DestTy, true);
421 for (unsigned i = 1; i != NumElems; ++i)
422 if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) {
430 // Pointer alignment doesn't depend on the pointee type, so canonicalize them
431 // to an arbitrary pointee.
432 if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
433 if (!PTy->getElementType()->isIntegerTy(1))
435 getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(),
437 PTy->getAddressSpace()),
440 // If there's no interesting folding happening, bail so that we don't create
441 // a constant that looks like it needs folding but really doesn't.
445 // Base case: Get a regular alignof expression.
446 Constant *C = ConstantExpr::getAlignOf(Ty);
447 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
453 /// getFoldedOffsetOf - Return a ConstantExpr with type DestTy for offsetof
454 /// on Ty and FieldNo, with any known factors factored out. If Folded is false,
455 /// return null if no factoring was possible, to avoid endlessly
456 /// bouncing an unfoldable expression back into the top-level folder.
458 static Constant *getFoldedOffsetOf(const Type *Ty, Constant *FieldNo,
461 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
462 Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false,
465 Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
466 return ConstantExpr::getNUWMul(E, N);
469 if (const StructType *STy = dyn_cast<StructType>(Ty))
470 if (!STy->isPacked()) {
471 unsigned NumElems = STy->getNumElements();
472 // An empty struct has no members.
475 // Check for a struct with all members having the same size.
476 Constant *MemberSize =
477 getFoldedSizeOf(STy->getElementType(0), DestTy, true);
479 for (unsigned i = 1; i != NumElems; ++i)
481 getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
486 Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo,
491 return ConstantExpr::getNUWMul(MemberSize, N);
495 // If there's no interesting folding happening, bail so that we don't create
496 // a constant that looks like it needs folding but really doesn't.
500 // Base case: Get a regular offsetof expression.
501 Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo);
502 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
508 Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
509 const Type *DestTy) {
510 if (isa<UndefValue>(V)) {
511 // zext(undef) = 0, because the top bits will be zero.
512 // sext(undef) = 0, because the top bits will all be the same.
513 // [us]itofp(undef) = 0, because the result value is bounded.
514 if (opc == Instruction::ZExt || opc == Instruction::SExt ||
515 opc == Instruction::UIToFP || opc == Instruction::SIToFP)
516 return Constant::getNullValue(DestTy);
517 return UndefValue::get(DestTy);
520 // No compile-time operations on this type yet.
521 if (V->getType()->isPPC_FP128Ty() || DestTy->isPPC_FP128Ty())
524 if (V->isNullValue() && !DestTy->isX86_MMXTy())
525 return Constant::getNullValue(DestTy);
527 // If the cast operand is a constant expression, there's a few things we can
528 // do to try to simplify it.
529 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
531 // Try hard to fold cast of cast because they are often eliminable.
532 if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
533 return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
534 } else if (CE->getOpcode() == Instruction::GetElementPtr) {
535 // If all of the indexes in the GEP are null values, there is no pointer
536 // adjustment going on. We might as well cast the source pointer.
537 bool isAllNull = true;
538 for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
539 if (!CE->getOperand(i)->isNullValue()) {
544 // This is casting one pointer type to another, always BitCast
545 return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
549 // If the cast operand is a constant vector, perform the cast by
550 // operating on each element. In the cast of bitcasts, the element
551 // count may be mismatched; don't attempt to handle that here.
552 if (ConstantVector *CV = dyn_cast<ConstantVector>(V))
553 if (DestTy->isVectorTy() &&
554 cast<VectorType>(DestTy)->getNumElements() ==
555 CV->getType()->getNumElements()) {
556 std::vector<Constant*> res;
557 const VectorType *DestVecTy = cast<VectorType>(DestTy);
558 const Type *DstEltTy = DestVecTy->getElementType();
559 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
560 res.push_back(ConstantExpr::getCast(opc,
561 CV->getOperand(i), DstEltTy));
562 return ConstantVector::get(res);
565 // We actually have to do a cast now. Perform the cast according to the
569 llvm_unreachable("Failed to cast constant expression");
570 case Instruction::FPTrunc:
571 case Instruction::FPExt:
572 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
574 APFloat Val = FPC->getValueAPF();
575 Val.convert(DestTy->isFloatTy() ? APFloat::IEEEsingle :
576 DestTy->isDoubleTy() ? APFloat::IEEEdouble :
577 DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended :
578 DestTy->isFP128Ty() ? APFloat::IEEEquad :
580 APFloat::rmNearestTiesToEven, &ignored);
581 return ConstantFP::get(V->getContext(), Val);
583 return 0; // Can't fold.
584 case Instruction::FPToUI:
585 case Instruction::FPToSI:
586 if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
587 const APFloat &V = FPC->getValueAPF();
590 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
591 (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
592 APFloat::rmTowardZero, &ignored);
593 APInt Val(DestBitWidth, 2, x);
594 return ConstantInt::get(FPC->getContext(), Val);
596 return 0; // Can't fold.
597 case Instruction::IntToPtr: //always treated as unsigned
598 if (V->isNullValue()) // Is it an integral null value?
599 return ConstantPointerNull::get(cast<PointerType>(DestTy));
600 return 0; // Other pointer types cannot be casted
601 case Instruction::PtrToInt: // always treated as unsigned
602 // Is it a null pointer value?
603 if (V->isNullValue())
604 return ConstantInt::get(DestTy, 0);
605 // If this is a sizeof-like expression, pull out multiplications by
606 // known factors to expose them to subsequent folding. If it's an
607 // alignof-like expression, factor out known factors.
608 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
609 if (CE->getOpcode() == Instruction::GetElementPtr &&
610 CE->getOperand(0)->isNullValue()) {
612 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
613 if (CE->getNumOperands() == 2) {
614 // Handle a sizeof-like expression.
615 Constant *Idx = CE->getOperand(1);
616 bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
617 if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) {
618 Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true,
621 return ConstantExpr::getMul(C, Idx);
623 } else if (CE->getNumOperands() == 3 &&
624 CE->getOperand(1)->isNullValue()) {
625 // Handle an alignof-like expression.
626 if (const StructType *STy = dyn_cast<StructType>(Ty))
627 if (!STy->isPacked()) {
628 ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
630 STy->getNumElements() == 2 &&
631 STy->getElementType(0)->isIntegerTy(1)) {
632 return getFoldedAlignOf(STy->getElementType(1), DestTy, false);
635 // Handle an offsetof-like expression.
636 if (Ty->isStructTy() || Ty->isArrayTy()) {
637 if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2),
643 // Other pointer types cannot be casted
645 case Instruction::UIToFP:
646 case Instruction::SIToFP:
647 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
648 APInt api = CI->getValue();
649 APFloat apf(APInt::getNullValue(DestTy->getPrimitiveSizeInBits()), true);
650 (void)apf.convertFromAPInt(api,
651 opc==Instruction::SIToFP,
652 APFloat::rmNearestTiesToEven);
653 return ConstantFP::get(V->getContext(), apf);
656 case Instruction::ZExt:
657 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
658 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
659 return ConstantInt::get(V->getContext(),
660 CI->getValue().zext(BitWidth));
663 case Instruction::SExt:
664 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
665 uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
666 return ConstantInt::get(V->getContext(),
667 CI->getValue().sext(BitWidth));
670 case Instruction::Trunc: {
671 uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
672 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
673 return ConstantInt::get(V->getContext(),
674 CI->getValue().trunc(DestBitWidth));
677 // The input must be a constantexpr. See if we can simplify this based on
678 // the bytes we are demanding. Only do this if the source and dest are an
679 // even multiple of a byte.
680 if ((DestBitWidth & 7) == 0 &&
681 (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
682 if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
687 case Instruction::BitCast:
688 return FoldBitCast(V, DestTy);
692 Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
693 Constant *V1, Constant *V2) {
694 if (ConstantInt *CB = dyn_cast<ConstantInt>(Cond))
695 return CB->getZExtValue() ? V1 : V2;
697 // Check for zero aggregate and ConstantVector of zeros
698 if (Cond->isNullValue()) return V2;
700 if (ConstantVector* CondV = dyn_cast<ConstantVector>(Cond)) {
702 if (CondV->isAllOnesValue()) return V1;
704 const VectorType *VTy = cast<VectorType>(V1->getType());
705 ConstantVector *CP1 = dyn_cast<ConstantVector>(V1);
706 ConstantVector *CP2 = dyn_cast<ConstantVector>(V2);
708 if ((CP1 || isa<ConstantAggregateZero>(V1)) &&
709 (CP2 || isa<ConstantAggregateZero>(V2))) {
711 // Find the element type of the returned vector
712 const Type *EltTy = VTy->getElementType();
713 unsigned NumElem = VTy->getNumElements();
714 std::vector<Constant*> Res(NumElem);
717 for (unsigned i = 0; i < NumElem; ++i) {
718 ConstantInt* c = dyn_cast<ConstantInt>(CondV->getOperand(i));
723 Constant *C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
724 Constant *C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
725 Res[i] = c->getZExtValue() ? C1 : C2;
727 // If we were able to build the vector, return it
728 if (Valid) return ConstantVector::get(Res);
733 if (isa<UndefValue>(Cond)) {
734 if (isa<UndefValue>(V1)) return V1;
737 if (isa<UndefValue>(V1)) return V2;
738 if (isa<UndefValue>(V2)) return V1;
739 if (V1 == V2) return V1;
741 if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
742 if (TrueVal->getOpcode() == Instruction::Select)
743 if (TrueVal->getOperand(0) == Cond)
744 return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
746 if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
747 if (FalseVal->getOpcode() == Instruction::Select)
748 if (FalseVal->getOperand(0) == Cond)
749 return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
755 Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
757 if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
758 return UndefValue::get(cast<VectorType>(Val->getType())->getElementType());
759 if (Val->isNullValue()) // ee(zero, x) -> zero
760 return Constant::getNullValue(
761 cast<VectorType>(Val->getType())->getElementType());
763 if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
764 if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
765 return CVal->getOperand(CIdx->getZExtValue());
766 } else if (isa<UndefValue>(Idx)) {
767 // ee({w,x,y,z}, undef) -> w (an arbitrary value).
768 return CVal->getOperand(0);
774 Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
777 ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
779 APInt idxVal = CIdx->getValue();
780 if (isa<UndefValue>(Val)) {
781 // Insertion of scalar constant into vector undef
782 // Optimize away insertion of undef
783 if (isa<UndefValue>(Elt))
785 // Otherwise break the aggregate undef into multiple undefs and do
788 cast<VectorType>(Val->getType())->getNumElements();
789 std::vector<Constant*> Ops;
791 for (unsigned i = 0; i < numOps; ++i) {
793 (idxVal == i) ? Elt : UndefValue::get(Elt->getType());
796 return ConstantVector::get(Ops);
798 if (isa<ConstantAggregateZero>(Val)) {
799 // Insertion of scalar constant into vector aggregate zero
800 // Optimize away insertion of zero
801 if (Elt->isNullValue())
803 // Otherwise break the aggregate zero into multiple zeros and do
806 cast<VectorType>(Val->getType())->getNumElements();
807 std::vector<Constant*> Ops;
809 for (unsigned i = 0; i < numOps; ++i) {
811 (idxVal == i) ? Elt : Constant::getNullValue(Elt->getType());
814 return ConstantVector::get(Ops);
816 if (ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) {
817 // Insertion of scalar constant into vector constant
818 std::vector<Constant*> Ops;
819 Ops.reserve(CVal->getNumOperands());
820 for (unsigned i = 0; i < CVal->getNumOperands(); ++i) {
822 (idxVal == i) ? Elt : cast<Constant>(CVal->getOperand(i));
825 return ConstantVector::get(Ops);
831 /// GetVectorElement - If C is a ConstantVector, ConstantAggregateZero or Undef
832 /// return the specified element value. Otherwise return null.
833 static Constant *GetVectorElement(Constant *C, unsigned EltNo) {
834 if (ConstantVector *CV = dyn_cast<ConstantVector>(C))
835 return CV->getOperand(EltNo);
837 const Type *EltTy = cast<VectorType>(C->getType())->getElementType();
838 if (isa<ConstantAggregateZero>(C))
839 return Constant::getNullValue(EltTy);
840 if (isa<UndefValue>(C))
841 return UndefValue::get(EltTy);
845 Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
848 // Undefined shuffle mask -> undefined value.
849 if (isa<UndefValue>(Mask)) return UndefValue::get(V1->getType());
851 unsigned MaskNumElts = cast<VectorType>(Mask->getType())->getNumElements();
852 unsigned SrcNumElts = cast<VectorType>(V1->getType())->getNumElements();
853 const Type *EltTy = cast<VectorType>(V1->getType())->getElementType();
855 // Loop over the shuffle mask, evaluating each element.
856 SmallVector<Constant*, 32> Result;
857 for (unsigned i = 0; i != MaskNumElts; ++i) {
858 Constant *InElt = GetVectorElement(Mask, i);
859 if (InElt == 0) return 0;
861 if (isa<UndefValue>(InElt))
862 InElt = UndefValue::get(EltTy);
863 else if (ConstantInt *CI = dyn_cast<ConstantInt>(InElt)) {
864 unsigned Elt = CI->getZExtValue();
865 if (Elt >= SrcNumElts*2)
866 InElt = UndefValue::get(EltTy);
867 else if (Elt >= SrcNumElts)
868 InElt = GetVectorElement(V2, Elt - SrcNumElts);
870 InElt = GetVectorElement(V1, Elt);
871 if (InElt == 0) return 0;
876 Result.push_back(InElt);
879 return ConstantVector::get(Result);
882 Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
883 ArrayRef<unsigned> Idxs) {
884 // Base case: no indices, so return the entire value.
888 if (isa<UndefValue>(Agg)) // ev(undef, x) -> undef
889 return UndefValue::get(ExtractValueInst::getIndexedType(Agg->getType(),
892 if (isa<ConstantAggregateZero>(Agg)) // ev(0, x) -> 0
894 Constant::getNullValue(ExtractValueInst::getIndexedType(Agg->getType(),
897 // Otherwise recurse.
898 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Agg))
899 return ConstantFoldExtractValueInstruction(CS->getOperand(Idxs[0]),
902 if (ConstantArray *CA = dyn_cast<ConstantArray>(Agg))
903 return ConstantFoldExtractValueInstruction(CA->getOperand(Idxs[0]),
905 ConstantVector *CV = cast<ConstantVector>(Agg);
906 return ConstantFoldExtractValueInstruction(CV->getOperand(Idxs[0]),
910 Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
912 ArrayRef<unsigned> Idxs) {
913 // Base case: no indices, so replace the entire value.
917 if (isa<UndefValue>(Agg)) {
918 // Insertion of constant into aggregate undef
919 // Optimize away insertion of undef.
920 if (isa<UndefValue>(Val))
923 // Otherwise break the aggregate undef into multiple undefs and do
925 const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
927 if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
928 numOps = AR->getNumElements();
930 numOps = cast<StructType>(AggTy)->getNumElements();
932 std::vector<Constant*> Ops(numOps);
933 for (unsigned i = 0; i < numOps; ++i) {
934 const Type *MemberTy = AggTy->getTypeAtIndex(i);
937 ConstantFoldInsertValueInstruction(UndefValue::get(MemberTy),
938 Val, Idxs.slice(1)) :
939 UndefValue::get(MemberTy);
943 if (const StructType* ST = dyn_cast<StructType>(AggTy))
944 return ConstantStruct::get(ST, Ops);
945 return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
948 if (isa<ConstantAggregateZero>(Agg)) {
949 // Insertion of constant into aggregate zero
950 // Optimize away insertion of zero.
951 if (Val->isNullValue())
954 // Otherwise break the aggregate zero into multiple zeros and do
956 const CompositeType *AggTy = cast<CompositeType>(Agg->getType());
958 if (const ArrayType *AR = dyn_cast<ArrayType>(AggTy))
959 numOps = AR->getNumElements();
961 numOps = cast<StructType>(AggTy)->getNumElements();
963 std::vector<Constant*> Ops(numOps);
964 for (unsigned i = 0; i < numOps; ++i) {
965 const Type *MemberTy = AggTy->getTypeAtIndex(i);
968 ConstantFoldInsertValueInstruction(Constant::getNullValue(MemberTy),
969 Val, Idxs.slice(1)) :
970 Constant::getNullValue(MemberTy);
974 if (const StructType *ST = dyn_cast<StructType>(AggTy))
975 return ConstantStruct::get(ST, Ops);
976 return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
979 if (isa<ConstantStruct>(Agg) || isa<ConstantArray>(Agg)) {
980 // Insertion of constant into aggregate constant.
981 std::vector<Constant*> Ops(Agg->getNumOperands());
982 for (unsigned i = 0; i < Agg->getNumOperands(); ++i) {
983 Constant *Op = cast<Constant>(Agg->getOperand(i));
985 Op = ConstantFoldInsertValueInstruction(Op, Val, Idxs.slice(1));
989 if (const StructType* ST = dyn_cast<StructType>(Agg->getType()))
990 return ConstantStruct::get(ST, Ops);
991 return ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
998 Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
999 Constant *C1, Constant *C2) {
1000 // No compile-time operations on this type yet.
1001 if (C1->getType()->isPPC_FP128Ty())
1004 // Handle UndefValue up front.
1005 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1007 case Instruction::Xor:
1008 if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
1009 // Handle undef ^ undef -> 0 special case. This is a common
1011 return Constant::getNullValue(C1->getType());
1013 case Instruction::Add:
1014 case Instruction::Sub:
1015 return UndefValue::get(C1->getType());
1016 case Instruction::And:
1017 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
1019 return Constant::getNullValue(C1->getType()); // undef & X -> 0
1020 case Instruction::Mul: {
1022 // X * undef -> undef if X is odd or undef
1023 if (((CI = dyn_cast<ConstantInt>(C1)) && CI->getValue()[0]) ||
1024 ((CI = dyn_cast<ConstantInt>(C2)) && CI->getValue()[0]) ||
1025 (isa<UndefValue>(C1) && isa<UndefValue>(C2)))
1026 return UndefValue::get(C1->getType());
1028 // X * undef -> 0 otherwise
1029 return Constant::getNullValue(C1->getType());
1031 case Instruction::UDiv:
1032 case Instruction::SDiv:
1033 // undef / 1 -> undef
1034 if (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv)
1035 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2))
1039 case Instruction::URem:
1040 case Instruction::SRem:
1041 if (!isa<UndefValue>(C2)) // undef / X -> 0
1042 return Constant::getNullValue(C1->getType());
1043 return C2; // X / undef -> undef
1044 case Instruction::Or: // X | undef -> -1
1045 if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
1047 return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
1048 case Instruction::LShr:
1049 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
1050 return C1; // undef lshr undef -> undef
1051 return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
1052 // undef lshr X -> 0
1053 case Instruction::AShr:
1054 if (!isa<UndefValue>(C2)) // undef ashr X --> all ones
1055 return Constant::getAllOnesValue(C1->getType());
1056 else if (isa<UndefValue>(C1))
1057 return C1; // undef ashr undef -> undef
1059 return C1; // X ashr undef --> X
1060 case Instruction::Shl:
1061 if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
1062 return C1; // undef shl undef -> undef
1063 // undef << X -> 0 or X << undef -> 0
1064 return Constant::getNullValue(C1->getType());
1068 // Handle simplifications when the RHS is a constant int.
1069 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1071 case Instruction::Add:
1072 if (CI2->equalsInt(0)) return C1; // X + 0 == X
1074 case Instruction::Sub:
1075 if (CI2->equalsInt(0)) return C1; // X - 0 == X
1077 case Instruction::Mul:
1078 if (CI2->equalsInt(0)) return C2; // X * 0 == 0
1079 if (CI2->equalsInt(1))
1080 return C1; // X * 1 == X
1082 case Instruction::UDiv:
1083 case Instruction::SDiv:
1084 if (CI2->equalsInt(1))
1085 return C1; // X / 1 == X
1086 if (CI2->equalsInt(0))
1087 return UndefValue::get(CI2->getType()); // X / 0 == undef
1089 case Instruction::URem:
1090 case Instruction::SRem:
1091 if (CI2->equalsInt(1))
1092 return Constant::getNullValue(CI2->getType()); // X % 1 == 0
1093 if (CI2->equalsInt(0))
1094 return UndefValue::get(CI2->getType()); // X % 0 == undef
1096 case Instruction::And:
1097 if (CI2->isZero()) return C2; // X & 0 == 0
1098 if (CI2->isAllOnesValue())
1099 return C1; // X & -1 == X
1101 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1102 // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
1103 if (CE1->getOpcode() == Instruction::ZExt) {
1104 unsigned DstWidth = CI2->getType()->getBitWidth();
1106 CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
1107 APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
1108 if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
1112 // If and'ing the address of a global with a constant, fold it.
1113 if (CE1->getOpcode() == Instruction::PtrToInt &&
1114 isa<GlobalValue>(CE1->getOperand(0))) {
1115 GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
1117 // Functions are at least 4-byte aligned.
1118 unsigned GVAlign = GV->getAlignment();
1119 if (isa<Function>(GV))
1120 GVAlign = std::max(GVAlign, 4U);
1123 unsigned DstWidth = CI2->getType()->getBitWidth();
1124 unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
1125 APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
1127 // If checking bits we know are clear, return zero.
1128 if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
1129 return Constant::getNullValue(CI2->getType());
1134 case Instruction::Or:
1135 if (CI2->equalsInt(0)) return C1; // X | 0 == X
1136 if (CI2->isAllOnesValue())
1137 return C2; // X | -1 == -1
1139 case Instruction::Xor:
1140 if (CI2->equalsInt(0)) return C1; // X ^ 0 == X
1142 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1143 switch (CE1->getOpcode()) {
1145 case Instruction::ICmp:
1146 case Instruction::FCmp:
1147 // cmp pred ^ true -> cmp !pred
1148 assert(CI2->equalsInt(1));
1149 CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
1150 pred = CmpInst::getInversePredicate(pred);
1151 return ConstantExpr::getCompare(pred, CE1->getOperand(0),
1152 CE1->getOperand(1));
1156 case Instruction::AShr:
1157 // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
1158 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
1159 if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
1160 return ConstantExpr::getLShr(C1, C2);
1163 } else if (isa<ConstantInt>(C1)) {
1164 // If C1 is a ConstantInt and C2 is not, swap the operands.
1165 if (Instruction::isCommutative(Opcode))
1166 return ConstantExpr::get(Opcode, C2, C1);
1169 // At this point we know neither constant is an UndefValue.
1170 if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
1171 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
1172 using namespace APIntOps;
1173 const APInt &C1V = CI1->getValue();
1174 const APInt &C2V = CI2->getValue();
1178 case Instruction::Add:
1179 return ConstantInt::get(CI1->getContext(), C1V + C2V);
1180 case Instruction::Sub:
1181 return ConstantInt::get(CI1->getContext(), C1V - C2V);
1182 case Instruction::Mul:
1183 return ConstantInt::get(CI1->getContext(), C1V * C2V);
1184 case Instruction::UDiv:
1185 assert(!CI2->isNullValue() && "Div by zero handled above");
1186 return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
1187 case Instruction::SDiv:
1188 assert(!CI2->isNullValue() && "Div by zero handled above");
1189 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
1190 return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef
1191 return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
1192 case Instruction::URem:
1193 assert(!CI2->isNullValue() && "Div by zero handled above");
1194 return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
1195 case Instruction::SRem:
1196 assert(!CI2->isNullValue() && "Div by zero handled above");
1197 if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
1198 return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef
1199 return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
1200 case Instruction::And:
1201 return ConstantInt::get(CI1->getContext(), C1V & C2V);
1202 case Instruction::Or:
1203 return ConstantInt::get(CI1->getContext(), C1V | C2V);
1204 case Instruction::Xor:
1205 return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
1206 case Instruction::Shl: {
1207 uint32_t shiftAmt = C2V.getZExtValue();
1208 if (shiftAmt < C1V.getBitWidth())
1209 return ConstantInt::get(CI1->getContext(), C1V.shl(shiftAmt));
1211 return UndefValue::get(C1->getType()); // too big shift is undef
1213 case Instruction::LShr: {
1214 uint32_t shiftAmt = C2V.getZExtValue();
1215 if (shiftAmt < C1V.getBitWidth())
1216 return ConstantInt::get(CI1->getContext(), C1V.lshr(shiftAmt));
1218 return UndefValue::get(C1->getType()); // too big shift is undef
1220 case Instruction::AShr: {
1221 uint32_t shiftAmt = C2V.getZExtValue();
1222 if (shiftAmt < C1V.getBitWidth())
1223 return ConstantInt::get(CI1->getContext(), C1V.ashr(shiftAmt));
1225 return UndefValue::get(C1->getType()); // too big shift is undef
1231 case Instruction::SDiv:
1232 case Instruction::UDiv:
1233 case Instruction::URem:
1234 case Instruction::SRem:
1235 case Instruction::LShr:
1236 case Instruction::AShr:
1237 case Instruction::Shl:
1238 if (CI1->equalsInt(0)) return C1;
1243 } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
1244 if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
1245 APFloat C1V = CFP1->getValueAPF();
1246 APFloat C2V = CFP2->getValueAPF();
1247 APFloat C3V = C1V; // copy for modification
1251 case Instruction::FAdd:
1252 (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
1253 return ConstantFP::get(C1->getContext(), C3V);
1254 case Instruction::FSub:
1255 (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
1256 return ConstantFP::get(C1->getContext(), C3V);
1257 case Instruction::FMul:
1258 (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
1259 return ConstantFP::get(C1->getContext(), C3V);
1260 case Instruction::FDiv:
1261 (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
1262 return ConstantFP::get(C1->getContext(), C3V);
1263 case Instruction::FRem:
1264 (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
1265 return ConstantFP::get(C1->getContext(), C3V);
1268 } else if (const VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
1269 ConstantVector *CP1 = dyn_cast<ConstantVector>(C1);
1270 ConstantVector *CP2 = dyn_cast<ConstantVector>(C2);
1271 if ((CP1 != NULL || isa<ConstantAggregateZero>(C1)) &&
1272 (CP2 != NULL || isa<ConstantAggregateZero>(C2))) {
1273 std::vector<Constant*> Res;
1274 const Type* EltTy = VTy->getElementType();
1280 case Instruction::Add:
1281 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1282 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1283 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1284 Res.push_back(ConstantExpr::getAdd(C1, C2));
1286 return ConstantVector::get(Res);
1287 case Instruction::FAdd:
1288 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1289 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1290 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1291 Res.push_back(ConstantExpr::getFAdd(C1, C2));
1293 return ConstantVector::get(Res);
1294 case Instruction::Sub:
1295 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1296 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1297 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1298 Res.push_back(ConstantExpr::getSub(C1, C2));
1300 return ConstantVector::get(Res);
1301 case Instruction::FSub:
1302 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1303 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1304 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1305 Res.push_back(ConstantExpr::getFSub(C1, C2));
1307 return ConstantVector::get(Res);
1308 case Instruction::Mul:
1309 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1310 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1311 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1312 Res.push_back(ConstantExpr::getMul(C1, C2));
1314 return ConstantVector::get(Res);
1315 case Instruction::FMul:
1316 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1317 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1318 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1319 Res.push_back(ConstantExpr::getFMul(C1, C2));
1321 return ConstantVector::get(Res);
1322 case Instruction::UDiv:
1323 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1324 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1325 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1326 Res.push_back(ConstantExpr::getUDiv(C1, C2));
1328 return ConstantVector::get(Res);
1329 case Instruction::SDiv:
1330 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1331 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1332 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1333 Res.push_back(ConstantExpr::getSDiv(C1, C2));
1335 return ConstantVector::get(Res);
1336 case Instruction::FDiv:
1337 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1338 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1339 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1340 Res.push_back(ConstantExpr::getFDiv(C1, C2));
1342 return ConstantVector::get(Res);
1343 case Instruction::URem:
1344 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1345 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1346 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1347 Res.push_back(ConstantExpr::getURem(C1, C2));
1349 return ConstantVector::get(Res);
1350 case Instruction::SRem:
1351 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1352 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1353 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1354 Res.push_back(ConstantExpr::getSRem(C1, C2));
1356 return ConstantVector::get(Res);
1357 case Instruction::FRem:
1358 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1359 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1360 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1361 Res.push_back(ConstantExpr::getFRem(C1, C2));
1363 return ConstantVector::get(Res);
1364 case Instruction::And:
1365 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1366 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1367 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1368 Res.push_back(ConstantExpr::getAnd(C1, C2));
1370 return ConstantVector::get(Res);
1371 case Instruction::Or:
1372 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1373 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1374 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1375 Res.push_back(ConstantExpr::getOr(C1, C2));
1377 return ConstantVector::get(Res);
1378 case Instruction::Xor:
1379 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1380 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1381 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1382 Res.push_back(ConstantExpr::getXor(C1, C2));
1384 return ConstantVector::get(Res);
1385 case Instruction::LShr:
1386 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1387 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1388 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1389 Res.push_back(ConstantExpr::getLShr(C1, C2));
1391 return ConstantVector::get(Res);
1392 case Instruction::AShr:
1393 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1394 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1395 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1396 Res.push_back(ConstantExpr::getAShr(C1, C2));
1398 return ConstantVector::get(Res);
1399 case Instruction::Shl:
1400 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
1401 C1 = CP1 ? CP1->getOperand(i) : Constant::getNullValue(EltTy);
1402 C2 = CP2 ? CP2->getOperand(i) : Constant::getNullValue(EltTy);
1403 Res.push_back(ConstantExpr::getShl(C1, C2));
1405 return ConstantVector::get(Res);
1410 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
1411 // There are many possible foldings we could do here. We should probably
1412 // at least fold add of a pointer with an integer into the appropriate
1413 // getelementptr. This will improve alias analysis a bit.
1415 // Given ((a + b) + c), if (b + c) folds to something interesting, return
1417 if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
1418 Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
1419 if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
1420 return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
1422 } else if (isa<ConstantExpr>(C2)) {
1423 // If C2 is a constant expr and C1 isn't, flop them around and fold the
1424 // other way if possible.
1425 if (Instruction::isCommutative(Opcode))
1426 return ConstantFoldBinaryInstruction(Opcode, C2, C1);
1429 // i1 can be simplified in many cases.
1430 if (C1->getType()->isIntegerTy(1)) {
1432 case Instruction::Add:
1433 case Instruction::Sub:
1434 return ConstantExpr::getXor(C1, C2);
1435 case Instruction::Mul:
1436 return ConstantExpr::getAnd(C1, C2);
1437 case Instruction::Shl:
1438 case Instruction::LShr:
1439 case Instruction::AShr:
1440 // We can assume that C2 == 0. If it were one the result would be
1441 // undefined because the shift value is as large as the bitwidth.
1443 case Instruction::SDiv:
1444 case Instruction::UDiv:
1445 // We can assume that C2 == 1. If it were zero the result would be
1446 // undefined through division by zero.
1448 case Instruction::URem:
1449 case Instruction::SRem:
1450 // We can assume that C2 == 1. If it were zero the result would be
1451 // undefined through division by zero.
1452 return ConstantInt::getFalse(C1->getContext());
1458 // We don't know how to fold this.
1462 /// isZeroSizedType - This type is zero sized if its an array or structure of
1463 /// zero sized types. The only leaf zero sized type is an empty structure.
1464 static bool isMaybeZeroSizedType(const Type *Ty) {
1465 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1466 if (STy->isOpaque()) return true; // Can't say.
1468 // If all of elements have zero size, this does too.
1469 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1470 if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
1473 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1474 return isMaybeZeroSizedType(ATy->getElementType());
1479 /// IdxCompare - Compare the two constants as though they were getelementptr
1480 /// indices. This allows coersion of the types to be the same thing.
1482 /// If the two constants are the "same" (after coersion), return 0. If the
1483 /// first is less than the second, return -1, if the second is less than the
1484 /// first, return 1. If the constants are not integral, return -2.
1486 static int IdxCompare(Constant *C1, Constant *C2, const Type *ElTy) {
1487 if (C1 == C2) return 0;
1489 // Ok, we found a different index. If they are not ConstantInt, we can't do
1490 // anything with them.
1491 if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
1492 return -2; // don't know!
1494 // Ok, we have two differing integer indices. Sign extend them to be the same
1495 // type. Long is always big enough, so we use it.
1496 if (!C1->getType()->isIntegerTy(64))
1497 C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(C1->getContext()));
1499 if (!C2->getType()->isIntegerTy(64))
1500 C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(C1->getContext()));
1502 if (C1 == C2) return 0; // They are equal
1504 // If the type being indexed over is really just a zero sized type, there is
1505 // no pointer difference being made here.
1506 if (isMaybeZeroSizedType(ElTy))
1507 return -2; // dunno.
1509 // If they are really different, now that they are the same type, then we
1510 // found a difference!
1511 if (cast<ConstantInt>(C1)->getSExtValue() <
1512 cast<ConstantInt>(C2)->getSExtValue())
1518 /// evaluateFCmpRelation - This function determines if there is anything we can
1519 /// decide about the two constants provided. This doesn't need to handle simple
1520 /// things like ConstantFP comparisons, but should instead handle ConstantExprs.
1521 /// If we can determine that the two constants have a particular relation to
1522 /// each other, we should return the corresponding FCmpInst predicate,
1523 /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
1524 /// ConstantFoldCompareInstruction.
1526 /// To simplify this code we canonicalize the relation so that the first
1527 /// operand is always the most "complex" of the two. We consider ConstantFP
1528 /// to be the simplest, and ConstantExprs to be the most complex.
1529 static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
1530 assert(V1->getType() == V2->getType() &&
1531 "Cannot compare values of different types!");
1533 // No compile-time operations on this type yet.
1534 if (V1->getType()->isPPC_FP128Ty())
1535 return FCmpInst::BAD_FCMP_PREDICATE;
1537 // Handle degenerate case quickly
1538 if (V1 == V2) return FCmpInst::FCMP_OEQ;
1540 if (!isa<ConstantExpr>(V1)) {
1541 if (!isa<ConstantExpr>(V2)) {
1542 // We distilled thisUse the standard constant folder for a few cases
1544 R = dyn_cast<ConstantInt>(
1545 ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
1546 if (R && !R->isZero())
1547 return FCmpInst::FCMP_OEQ;
1548 R = dyn_cast<ConstantInt>(
1549 ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
1550 if (R && !R->isZero())
1551 return FCmpInst::FCMP_OLT;
1552 R = dyn_cast<ConstantInt>(
1553 ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
1554 if (R && !R->isZero())
1555 return FCmpInst::FCMP_OGT;
1557 // Nothing more we can do
1558 return FCmpInst::BAD_FCMP_PREDICATE;
1561 // If the first operand is simple and second is ConstantExpr, swap operands.
1562 FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
1563 if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
1564 return FCmpInst::getSwappedPredicate(SwappedRelation);
1566 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1567 // constantexpr or a simple constant.
1568 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1569 switch (CE1->getOpcode()) {
1570 case Instruction::FPTrunc:
1571 case Instruction::FPExt:
1572 case Instruction::UIToFP:
1573 case Instruction::SIToFP:
1574 // We might be able to do something with these but we don't right now.
1580 // There are MANY other foldings that we could perform here. They will
1581 // probably be added on demand, as they seem needed.
1582 return FCmpInst::BAD_FCMP_PREDICATE;
1585 /// evaluateICmpRelation - This function determines if there is anything we can
1586 /// decide about the two constants provided. This doesn't need to handle simple
1587 /// things like integer comparisons, but should instead handle ConstantExprs
1588 /// and GlobalValues. If we can determine that the two constants have a
1589 /// particular relation to each other, we should return the corresponding ICmp
1590 /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
1592 /// To simplify this code we canonicalize the relation so that the first
1593 /// operand is always the most "complex" of the two. We consider simple
1594 /// constants (like ConstantInt) to be the simplest, followed by
1595 /// GlobalValues, followed by ConstantExpr's (the most complex).
1597 static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
1599 assert(V1->getType() == V2->getType() &&
1600 "Cannot compare different types of values!");
1601 if (V1 == V2) return ICmpInst::ICMP_EQ;
1603 if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
1604 !isa<BlockAddress>(V1)) {
1605 if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
1606 !isa<BlockAddress>(V2)) {
1607 // We distilled this down to a simple case, use the standard constant
1610 ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
1611 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1612 if (R && !R->isZero())
1614 pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1615 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1616 if (R && !R->isZero())
1618 pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1619 R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
1620 if (R && !R->isZero())
1623 // If we couldn't figure it out, bail.
1624 return ICmpInst::BAD_ICMP_PREDICATE;
1627 // If the first operand is simple, swap operands.
1628 ICmpInst::Predicate SwappedRelation =
1629 evaluateICmpRelation(V2, V1, isSigned);
1630 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1631 return ICmpInst::getSwappedPredicate(SwappedRelation);
1633 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
1634 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
1635 ICmpInst::Predicate SwappedRelation =
1636 evaluateICmpRelation(V2, V1, isSigned);
1637 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1638 return ICmpInst::getSwappedPredicate(SwappedRelation);
1639 return ICmpInst::BAD_ICMP_PREDICATE;
1642 // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1643 // constant (which, since the types must match, means that it's a
1644 // ConstantPointerNull).
1645 if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1646 // Don't try to decide equality of aliases.
1647 if (!isa<GlobalAlias>(GV) && !isa<GlobalAlias>(GV2))
1648 if (!GV->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage())
1649 return ICmpInst::ICMP_NE;
1650 } else if (isa<BlockAddress>(V2)) {
1651 return ICmpInst::ICMP_NE; // Globals never equal labels.
1653 assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
1654 // GlobalVals can never be null unless they have external weak linkage.
1655 // We don't try to evaluate aliases here.
1656 if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV))
1657 return ICmpInst::ICMP_NE;
1659 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
1660 if (isa<ConstantExpr>(V2)) { // Swap as necessary.
1661 ICmpInst::Predicate SwappedRelation =
1662 evaluateICmpRelation(V2, V1, isSigned);
1663 if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
1664 return ICmpInst::getSwappedPredicate(SwappedRelation);
1665 return ICmpInst::BAD_ICMP_PREDICATE;
1668 // Now we know that the RHS is a GlobalValue, BlockAddress or simple
1669 // constant (which, since the types must match, means that it is a
1670 // ConstantPointerNull).
1671 if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
1672 // Block address in another function can't equal this one, but block
1673 // addresses in the current function might be the same if blocks are
1675 if (BA2->getFunction() != BA->getFunction())
1676 return ICmpInst::ICMP_NE;
1678 // Block addresses aren't null, don't equal the address of globals.
1679 assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
1680 "Canonicalization guarantee!");
1681 return ICmpInst::ICMP_NE;
1684 // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
1685 // constantexpr, a global, block address, or a simple constant.
1686 ConstantExpr *CE1 = cast<ConstantExpr>(V1);
1687 Constant *CE1Op0 = CE1->getOperand(0);
1689 switch (CE1->getOpcode()) {
1690 case Instruction::Trunc:
1691 case Instruction::FPTrunc:
1692 case Instruction::FPExt:
1693 case Instruction::FPToUI:
1694 case Instruction::FPToSI:
1695 break; // We can't evaluate floating point casts or truncations.
1697 case Instruction::UIToFP:
1698 case Instruction::SIToFP:
1699 case Instruction::BitCast:
1700 case Instruction::ZExt:
1701 case Instruction::SExt:
1702 // If the cast is not actually changing bits, and the second operand is a
1703 // null pointer, do the comparison with the pre-casted value.
1704 if (V2->isNullValue() &&
1705 (CE1->getType()->isPointerTy() || CE1->getType()->isIntegerTy())) {
1706 if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
1707 if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
1708 return evaluateICmpRelation(CE1Op0,
1709 Constant::getNullValue(CE1Op0->getType()),
1714 case Instruction::GetElementPtr:
1715 // Ok, since this is a getelementptr, we know that the constant has a
1716 // pointer type. Check the various cases.
1717 if (isa<ConstantPointerNull>(V2)) {
1718 // If we are comparing a GEP to a null pointer, check to see if the base
1719 // of the GEP equals the null pointer.
1720 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1721 if (GV->hasExternalWeakLinkage())
1722 // Weak linkage GVals could be zero or not. We're comparing that
1723 // to null pointer so its greater-or-equal
1724 return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
1726 // If its not weak linkage, the GVal must have a non-zero address
1727 // so the result is greater-than
1728 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1729 } else if (isa<ConstantPointerNull>(CE1Op0)) {
1730 // If we are indexing from a null pointer, check to see if we have any
1731 // non-zero indices.
1732 for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
1733 if (!CE1->getOperand(i)->isNullValue())
1734 // Offsetting from null, must not be equal.
1735 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1736 // Only zero indexes from null, must still be zero.
1737 return ICmpInst::ICMP_EQ;
1739 // Otherwise, we can't really say if the first operand is null or not.
1740 } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
1741 if (isa<ConstantPointerNull>(CE1Op0)) {
1742 if (GV2->hasExternalWeakLinkage())
1743 // Weak linkage GVals could be zero or not. We're comparing it to
1744 // a null pointer, so its less-or-equal
1745 return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
1747 // If its not weak linkage, the GVal must have a non-zero address
1748 // so the result is less-than
1749 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1750 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
1752 // If this is a getelementptr of the same global, then it must be
1753 // different. Because the types must match, the getelementptr could
1754 // only have at most one index, and because we fold getelementptr's
1755 // with a single zero index, it must be nonzero.
1756 assert(CE1->getNumOperands() == 2 &&
1757 !CE1->getOperand(1)->isNullValue() &&
1758 "Surprising getelementptr!");
1759 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1761 // If they are different globals, we don't know what the value is,
1762 // but they can't be equal.
1763 return ICmpInst::ICMP_NE;
1767 ConstantExpr *CE2 = cast<ConstantExpr>(V2);
1768 Constant *CE2Op0 = CE2->getOperand(0);
1770 // There are MANY other foldings that we could perform here. They will
1771 // probably be added on demand, as they seem needed.
1772 switch (CE2->getOpcode()) {
1774 case Instruction::GetElementPtr:
1775 // By far the most common case to handle is when the base pointers are
1776 // obviously to the same or different globals.
1777 if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
1778 if (CE1Op0 != CE2Op0) // Don't know relative ordering, but not equal
1779 return ICmpInst::ICMP_NE;
1780 // Ok, we know that both getelementptr instructions are based on the
1781 // same global. From this, we can precisely determine the relative
1782 // ordering of the resultant pointers.
1785 // The logic below assumes that the result of the comparison
1786 // can be determined by finding the first index that differs.
1787 // This doesn't work if there is over-indexing in any
1788 // subsequent indices, so check for that case first.
1789 if (!CE1->isGEPWithNoNotionalOverIndexing() ||
1790 !CE2->isGEPWithNoNotionalOverIndexing())
1791 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1793 // Compare all of the operands the GEP's have in common.
1794 gep_type_iterator GTI = gep_type_begin(CE1);
1795 for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
1797 switch (IdxCompare(CE1->getOperand(i),
1798 CE2->getOperand(i), GTI.getIndexedType())) {
1799 case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
1800 case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
1801 case -2: return ICmpInst::BAD_ICMP_PREDICATE;
1804 // Ok, we ran out of things they have in common. If any leftovers
1805 // are non-zero then we have a difference, otherwise we are equal.
1806 for (; i < CE1->getNumOperands(); ++i)
1807 if (!CE1->getOperand(i)->isNullValue()) {
1808 if (isa<ConstantInt>(CE1->getOperand(i)))
1809 return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
1811 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1814 for (; i < CE2->getNumOperands(); ++i)
1815 if (!CE2->getOperand(i)->isNullValue()) {
1816 if (isa<ConstantInt>(CE2->getOperand(i)))
1817 return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
1819 return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
1821 return ICmpInst::ICMP_EQ;
1830 return ICmpInst::BAD_ICMP_PREDICATE;
1833 Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
1834 Constant *C1, Constant *C2) {
1835 const Type *ResultTy;
1836 if (const VectorType *VT = dyn_cast<VectorType>(C1->getType()))
1837 ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
1838 VT->getNumElements());
1840 ResultTy = Type::getInt1Ty(C1->getContext());
1842 // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
1843 if (pred == FCmpInst::FCMP_FALSE)
1844 return Constant::getNullValue(ResultTy);
1846 if (pred == FCmpInst::FCMP_TRUE)
1847 return Constant::getAllOnesValue(ResultTy);
1849 // Handle some degenerate cases first
1850 if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
1851 // For EQ and NE, we can always pick a value for the undef to make the
1852 // predicate pass or fail, so we can return undef.
1853 // Also, if both operands are undef, we can return undef.
1854 if (ICmpInst::isEquality(ICmpInst::Predicate(pred)) ||
1855 (isa<UndefValue>(C1) && isa<UndefValue>(C2)))
1856 return UndefValue::get(ResultTy);
1857 // Otherwise, pick the same value as the non-undef operand, and fold
1858 // it to true or false.
1859 return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred));
1862 // No compile-time operations on this type yet.
1863 if (C1->getType()->isPPC_FP128Ty())
1866 // icmp eq/ne(null,GV) -> false/true
1867 if (C1->isNullValue()) {
1868 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
1869 // Don't try to evaluate aliases. External weak GV can be null.
1870 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
1871 if (pred == ICmpInst::ICMP_EQ)
1872 return ConstantInt::getFalse(C1->getContext());
1873 else if (pred == ICmpInst::ICMP_NE)
1874 return ConstantInt::getTrue(C1->getContext());
1876 // icmp eq/ne(GV,null) -> false/true
1877 } else if (C2->isNullValue()) {
1878 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
1879 // Don't try to evaluate aliases. External weak GV can be null.
1880 if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
1881 if (pred == ICmpInst::ICMP_EQ)
1882 return ConstantInt::getFalse(C1->getContext());
1883 else if (pred == ICmpInst::ICMP_NE)
1884 return ConstantInt::getTrue(C1->getContext());
1888 // If the comparison is a comparison between two i1's, simplify it.
1889 if (C1->getType()->isIntegerTy(1)) {
1891 case ICmpInst::ICMP_EQ:
1892 if (isa<ConstantInt>(C2))
1893 return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
1894 return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
1895 case ICmpInst::ICMP_NE:
1896 return ConstantExpr::getXor(C1, C2);
1902 if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
1903 APInt V1 = cast<ConstantInt>(C1)->getValue();
1904 APInt V2 = cast<ConstantInt>(C2)->getValue();
1906 default: llvm_unreachable("Invalid ICmp Predicate"); return 0;
1907 case ICmpInst::ICMP_EQ: return ConstantInt::get(ResultTy, V1 == V2);
1908 case ICmpInst::ICMP_NE: return ConstantInt::get(ResultTy, V1 != V2);
1909 case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2));
1910 case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2));
1911 case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2));
1912 case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2));
1913 case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2));
1914 case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2));
1915 case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2));
1916 case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2));
1918 } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
1919 APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
1920 APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
1921 APFloat::cmpResult R = C1V.compare(C2V);
1923 default: llvm_unreachable("Invalid FCmp Predicate"); return 0;
1924 case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
1925 case FCmpInst::FCMP_TRUE: return Constant::getAllOnesValue(ResultTy);
1926 case FCmpInst::FCMP_UNO:
1927 return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
1928 case FCmpInst::FCMP_ORD:
1929 return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
1930 case FCmpInst::FCMP_UEQ:
1931 return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
1932 R==APFloat::cmpEqual);
1933 case FCmpInst::FCMP_OEQ:
1934 return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
1935 case FCmpInst::FCMP_UNE:
1936 return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
1937 case FCmpInst::FCMP_ONE:
1938 return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
1939 R==APFloat::cmpGreaterThan);
1940 case FCmpInst::FCMP_ULT:
1941 return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
1942 R==APFloat::cmpLessThan);
1943 case FCmpInst::FCMP_OLT:
1944 return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
1945 case FCmpInst::FCMP_UGT:
1946 return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
1947 R==APFloat::cmpGreaterThan);
1948 case FCmpInst::FCMP_OGT:
1949 return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
1950 case FCmpInst::FCMP_ULE:
1951 return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
1952 case FCmpInst::FCMP_OLE:
1953 return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
1954 R==APFloat::cmpEqual);
1955 case FCmpInst::FCMP_UGE:
1956 return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
1957 case FCmpInst::FCMP_OGE:
1958 return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
1959 R==APFloat::cmpEqual);
1961 } else if (C1->getType()->isVectorTy()) {
1962 SmallVector<Constant*, 16> C1Elts, C2Elts;
1963 C1->getVectorElements(C1Elts);
1964 C2->getVectorElements(C2Elts);
1965 if (C1Elts.empty() || C2Elts.empty())
1968 // If we can constant fold the comparison of each element, constant fold
1969 // the whole vector comparison.
1970 SmallVector<Constant*, 4> ResElts;
1971 // Compare the elements, producing an i1 result or constant expr.
1972 for (unsigned i = 0, e = C1Elts.size(); i != e; ++i)
1973 ResElts.push_back(ConstantExpr::getCompare(pred, C1Elts[i], C2Elts[i]));
1975 return ConstantVector::get(ResElts);
1978 if (C1->getType()->isFloatingPointTy()) {
1979 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
1980 switch (evaluateFCmpRelation(C1, C2)) {
1981 default: llvm_unreachable("Unknown relation!");
1982 case FCmpInst::FCMP_UNO:
1983 case FCmpInst::FCMP_ORD:
1984 case FCmpInst::FCMP_UEQ:
1985 case FCmpInst::FCMP_UNE:
1986 case FCmpInst::FCMP_ULT:
1987 case FCmpInst::FCMP_UGT:
1988 case FCmpInst::FCMP_ULE:
1989 case FCmpInst::FCMP_UGE:
1990 case FCmpInst::FCMP_TRUE:
1991 case FCmpInst::FCMP_FALSE:
1992 case FCmpInst::BAD_FCMP_PREDICATE:
1993 break; // Couldn't determine anything about these constants.
1994 case FCmpInst::FCMP_OEQ: // We know that C1 == C2
1995 Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
1996 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
1997 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
1999 case FCmpInst::FCMP_OLT: // We know that C1 < C2
2000 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
2001 pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
2002 pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
2004 case FCmpInst::FCMP_OGT: // We know that C1 > C2
2005 Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
2006 pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
2007 pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
2009 case FCmpInst::FCMP_OLE: // We know that C1 <= C2
2010 // We can only partially decide this relation.
2011 if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
2013 else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
2016 case FCmpInst::FCMP_OGE: // We known that C1 >= C2
2017 // We can only partially decide this relation.
2018 if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
2020 else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
2023 case FCmpInst::FCMP_ONE: // We know that C1 != C2
2024 // We can only partially decide this relation.
2025 if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
2027 else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
2032 // If we evaluated the result, return it now.
2034 return ConstantInt::get(ResultTy, Result);
2037 // Evaluate the relation between the two constants, per the predicate.
2038 int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
2039 switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
2040 default: llvm_unreachable("Unknown relational!");
2041 case ICmpInst::BAD_ICMP_PREDICATE:
2042 break; // Couldn't determine anything about these constants.
2043 case ICmpInst::ICMP_EQ: // We know the constants are equal!
2044 // If we know the constants are equal, we can decide the result of this
2045 // computation precisely.
2046 Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
2048 case ICmpInst::ICMP_ULT:
2050 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
2052 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
2056 case ICmpInst::ICMP_SLT:
2058 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
2060 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
2064 case ICmpInst::ICMP_UGT:
2066 case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
2068 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
2072 case ICmpInst::ICMP_SGT:
2074 case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
2076 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
2080 case ICmpInst::ICMP_ULE:
2081 if (pred == ICmpInst::ICMP_UGT) Result = 0;
2082 if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
2084 case ICmpInst::ICMP_SLE:
2085 if (pred == ICmpInst::ICMP_SGT) Result = 0;
2086 if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
2088 case ICmpInst::ICMP_UGE:
2089 if (pred == ICmpInst::ICMP_ULT) Result = 0;
2090 if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
2092 case ICmpInst::ICMP_SGE:
2093 if (pred == ICmpInst::ICMP_SLT) Result = 0;
2094 if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
2096 case ICmpInst::ICMP_NE:
2097 if (pred == ICmpInst::ICMP_EQ) Result = 0;
2098 if (pred == ICmpInst::ICMP_NE) Result = 1;
2102 // If we evaluated the result, return it now.
2104 return ConstantInt::get(ResultTy, Result);
2106 // If the right hand side is a bitcast, try using its inverse to simplify
2107 // it by moving it to the left hand side. We can't do this if it would turn
2108 // a vector compare into a scalar compare or visa versa.
2109 if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
2110 Constant *CE2Op0 = CE2->getOperand(0);
2111 if (CE2->getOpcode() == Instruction::BitCast &&
2112 CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) {
2113 Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
2114 return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
2118 // If the left hand side is an extension, try eliminating it.
2119 if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
2120 if ((CE1->getOpcode() == Instruction::SExt && ICmpInst::isSigned(pred)) ||
2121 (CE1->getOpcode() == Instruction::ZExt && !ICmpInst::isSigned(pred))){
2122 Constant *CE1Op0 = CE1->getOperand(0);
2123 Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
2124 if (CE1Inverse == CE1Op0) {
2125 // Check whether we can safely truncate the right hand side.
2126 Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
2127 if (ConstantExpr::getZExt(C2Inverse, C2->getType()) == C2) {
2128 return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
2134 if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
2135 (C1->isNullValue() && !C2->isNullValue())) {
2136 // If C2 is a constant expr and C1 isn't, flip them around and fold the
2137 // other way if possible.
2138 // Also, if C1 is null and C2 isn't, flip them around.
2139 pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
2140 return ConstantExpr::getICmp(pred, C2, C1);
2146 /// isInBoundsIndices - Test whether the given sequence of *normalized* indices
2148 template<typename IndexTy>
2149 static bool isInBoundsIndices(IndexTy const *Idxs, size_t NumIdx) {
2150 // No indices means nothing that could be out of bounds.
2151 if (NumIdx == 0) return true;
2153 // If the first index is zero, it's in bounds.
2154 if (cast<Constant>(Idxs[0])->isNullValue()) return true;
2156 // If the first index is one and all the rest are zero, it's in bounds,
2157 // by the one-past-the-end rule.
2158 if (!cast<ConstantInt>(Idxs[0])->isOne())
2160 for (unsigned i = 1, e = NumIdx; i != e; ++i)
2161 if (!cast<Constant>(Idxs[i])->isNullValue())
2166 template<typename IndexTy>
2167 static Constant *ConstantFoldGetElementPtrImpl(Constant *C,
2169 IndexTy const *Idxs,
2171 if (NumIdx == 0) return C;
2172 Constant *Idx0 = cast<Constant>(Idxs[0]);
2173 if ((NumIdx == 1 && Idx0->isNullValue()))
2176 if (isa<UndefValue>(C)) {
2177 const PointerType *Ptr = cast<PointerType>(C->getType());
2178 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs, Idxs+NumIdx);
2179 assert(Ty != 0 && "Invalid indices for GEP!");
2180 return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
2183 if (C->isNullValue()) {
2185 for (unsigned i = 0, e = NumIdx; i != e; ++i)
2186 if (!cast<Constant>(Idxs[i])->isNullValue()) {
2191 const PointerType *Ptr = cast<PointerType>(C->getType());
2192 const Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs,
2194 assert(Ty != 0 && "Invalid indices for GEP!");
2195 return ConstantPointerNull::get(PointerType::get(Ty,
2196 Ptr->getAddressSpace()));
2200 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
2201 // Combine Indices - If the source pointer to this getelementptr instruction
2202 // is a getelementptr instruction, combine the indices of the two
2203 // getelementptr instructions into a single instruction.
2205 if (CE->getOpcode() == Instruction::GetElementPtr) {
2206 const Type *LastTy = 0;
2207 for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
2211 if ((LastTy && LastTy->isArrayTy()) || Idx0->isNullValue()) {
2212 SmallVector<Value*, 16> NewIndices;
2213 NewIndices.reserve(NumIdx + CE->getNumOperands());
2214 for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i)
2215 NewIndices.push_back(CE->getOperand(i));
2217 // Add the last index of the source with the first index of the new GEP.
2218 // Make sure to handle the case when they are actually different types.
2219 Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
2220 // Otherwise it must be an array.
2221 if (!Idx0->isNullValue()) {
2222 const Type *IdxTy = Combined->getType();
2223 if (IdxTy != Idx0->getType()) {
2224 const Type *Int64Ty = Type::getInt64Ty(IdxTy->getContext());
2225 Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Int64Ty);
2226 Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, Int64Ty);
2227 Combined = ConstantExpr::get(Instruction::Add, C1, C2);
2230 ConstantExpr::get(Instruction::Add, Idx0, Combined);
2234 NewIndices.push_back(Combined);
2235 NewIndices.append(Idxs+1, Idxs+NumIdx);
2236 return (inBounds && cast<GEPOperator>(CE)->isInBounds()) ?
2237 ConstantExpr::getInBoundsGetElementPtr(CE->getOperand(0),
2239 NewIndices.size()) :
2240 ConstantExpr::getGetElementPtr(CE->getOperand(0),
2246 // Implement folding of:
2247 // i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
2249 // To: i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
2251 if (CE->isCast() && NumIdx > 1 && Idx0->isNullValue()) {
2252 if (const PointerType *SPT =
2253 dyn_cast<PointerType>(CE->getOperand(0)->getType()))
2254 if (const ArrayType *SAT = dyn_cast<ArrayType>(SPT->getElementType()))
2255 if (const ArrayType *CAT =
2256 dyn_cast<ArrayType>(cast<PointerType>(C->getType())->getElementType()))
2257 if (CAT->getElementType() == SAT->getElementType())
2259 ConstantExpr::getInBoundsGetElementPtr(
2260 (Constant*)CE->getOperand(0), Idxs, NumIdx) :
2261 ConstantExpr::getGetElementPtr(
2262 (Constant*)CE->getOperand(0), Idxs, NumIdx);
2266 // Check to see if any array indices are not within the corresponding
2267 // notional array bounds. If so, try to determine if they can be factored
2268 // out into preceding dimensions.
2269 bool Unknown = false;
2270 SmallVector<Constant *, 8> NewIdxs;
2271 const Type *Ty = C->getType();
2272 const Type *Prev = 0;
2273 for (unsigned i = 0; i != NumIdx;
2274 Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
2275 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
2276 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
2277 if (ATy->getNumElements() <= INT64_MAX &&
2278 ATy->getNumElements() != 0 &&
2279 CI->getSExtValue() >= (int64_t)ATy->getNumElements()) {
2280 if (isa<SequentialType>(Prev)) {
2281 // It's out of range, but we can factor it into the prior
2283 NewIdxs.resize(NumIdx);
2284 ConstantInt *Factor = ConstantInt::get(CI->getType(),
2285 ATy->getNumElements());
2286 NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
2288 Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
2289 Constant *Div = ConstantExpr::getSDiv(CI, Factor);
2291 // Before adding, extend both operands to i64 to avoid
2292 // overflow trouble.
2293 if (!PrevIdx->getType()->isIntegerTy(64))
2294 PrevIdx = ConstantExpr::getSExt(PrevIdx,
2295 Type::getInt64Ty(Div->getContext()));
2296 if (!Div->getType()->isIntegerTy(64))
2297 Div = ConstantExpr::getSExt(Div,
2298 Type::getInt64Ty(Div->getContext()));
2300 NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
2302 // It's out of range, but the prior dimension is a struct
2303 // so we can't do anything about it.
2308 // We don't know if it's in range or not.
2313 // If we did any factoring, start over with the adjusted indices.
2314 if (!NewIdxs.empty()) {
2315 for (unsigned i = 0; i != NumIdx; ++i)
2316 if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
2318 ConstantExpr::getInBoundsGetElementPtr(C, NewIdxs.data(),
2320 ConstantExpr::getGetElementPtr(C, NewIdxs.data(), NewIdxs.size());
2323 // If all indices are known integers and normalized, we can do a simple
2324 // check for the "inbounds" property.
2325 if (!Unknown && !inBounds &&
2326 isa<GlobalVariable>(C) && isInBoundsIndices(Idxs, NumIdx))
2327 return ConstantExpr::getInBoundsGetElementPtr(C, Idxs, NumIdx);
2332 Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
2334 Constant* const *Idxs,
2336 return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs, NumIdx);
2339 Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
2343 return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs, NumIdx);