Dan Gohman | 83e3c4f | 2009-09-10 23:07:18 +0000 | [diff] [blame] | 1 | //===-- ConstantFolding.cpp - Fold instructions into constants ------------===// |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Dan Gohman | 83e3c4f | 2009-09-10 23:07:18 +0000 | [diff] [blame] | 10 | // This file defines routines for folding instructions into constants. |
| 11 | // |
| 12 | // Also, to supplement the basic VMCore ConstantExpr simplifications, |
| 13 | // this file defines some additional folding routines that can make use of |
| 14 | // TargetData information. These functions cannot go in VMCore due to library |
| 15 | // dependency issues. |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "llvm/Analysis/ConstantFolding.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 22 | #include "llvm/Function.h" |
Dan Gohman | 9a38e3e | 2009-05-07 19:46:24 +0000 | [diff] [blame] | 23 | #include "llvm/GlobalVariable.h" |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
| 25 | #include "llvm/Intrinsics.h" |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 26 | #include "llvm/LLVMContext.h" |
Chris Lattner | 62d327e | 2009-10-22 06:38:35 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/ValueTracking.h" |
| 28 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringMap.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 32 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 33 | #include "llvm/Support/MathExtras.h" |
| 34 | #include <cerrno> |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 35 | #include <cmath> |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | // Constant Folding internal helper functions |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
Chris Lattner | 6333c39 | 2009-10-25 06:08:26 +0000 | [diff] [blame^] | 42 | /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with |
| 43 | /// TargetData. This always returns a non-null constant, but it may be a |
| 44 | /// ConstantExpr if unfoldable. |
| 45 | static Constant *FoldBitCast(Constant *C, const Type *DestTy, |
| 46 | const TargetData &TD) { |
| 47 | // If this is a bitcast from constant vector -> vector, fold it. |
| 48 | ConstantVector *CV = dyn_cast<ConstantVector>(C); |
| 49 | if (CV == 0) |
| 50 | return ConstantExpr::getBitCast(C, DestTy); |
| 51 | |
| 52 | const VectorType *DestVTy = dyn_cast<VectorType>(DestTy); |
| 53 | if (DestVTy == 0) |
| 54 | return ConstantExpr::getBitCast(C, DestTy); |
| 55 | |
| 56 | // If the element types match, VMCore can fold it. |
| 57 | unsigned NumDstElt = DestVTy->getNumElements(); |
| 58 | unsigned NumSrcElt = CV->getNumOperands(); |
| 59 | if (NumDstElt == NumSrcElt) |
| 60 | return ConstantExpr::getBitCast(C, DestTy); |
| 61 | |
| 62 | const Type *SrcEltTy = CV->getType()->getElementType(); |
| 63 | const Type *DstEltTy = DestVTy->getElementType(); |
| 64 | |
| 65 | // Otherwise, we're changing the number of elements in a vector, which |
| 66 | // requires endianness information to do the right thing. For example, |
| 67 | // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) |
| 68 | // folds to (little endian): |
| 69 | // <4 x i32> <i32 0, i32 0, i32 1, i32 0> |
| 70 | // and to (big endian): |
| 71 | // <4 x i32> <i32 0, i32 0, i32 0, i32 1> |
| 72 | |
| 73 | // First thing is first. We only want to think about integer here, so if |
| 74 | // we have something in FP form, recast it as integer. |
| 75 | if (DstEltTy->isFloatingPoint()) { |
| 76 | // Fold to an vector of integers with same size as our FP type. |
| 77 | unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits(); |
| 78 | const Type *DestIVTy = |
| 79 | VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumDstElt); |
| 80 | // Recursively handle this integer conversion, if possible. |
| 81 | C = FoldBitCast(C, DestIVTy, TD); |
| 82 | if (!C) return ConstantExpr::getBitCast(C, DestTy); |
| 83 | |
| 84 | // Finally, VMCore can handle this now that #elts line up. |
| 85 | return ConstantExpr::getBitCast(C, DestTy); |
| 86 | } |
| 87 | |
| 88 | // Okay, we know the destination is integer, if the input is FP, convert |
| 89 | // it to integer first. |
| 90 | if (SrcEltTy->isFloatingPoint()) { |
| 91 | unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); |
| 92 | const Type *SrcIVTy = |
| 93 | VectorType::get(IntegerType::get(C->getContext(), FPWidth), NumSrcElt); |
| 94 | // Ask VMCore to do the conversion now that #elts line up. |
| 95 | C = ConstantExpr::getBitCast(C, SrcIVTy); |
| 96 | CV = dyn_cast<ConstantVector>(C); |
| 97 | if (!CV) // If VMCore wasn't able to fold it, bail out. |
| 98 | return C; |
| 99 | } |
| 100 | |
| 101 | // Now we know that the input and output vectors are both integer vectors |
| 102 | // of the same size, and that their #elements is not the same. Do the |
| 103 | // conversion here, which depends on whether the input or output has |
| 104 | // more elements. |
| 105 | bool isLittleEndian = TD.isLittleEndian(); |
| 106 | |
| 107 | SmallVector<Constant*, 32> Result; |
| 108 | if (NumDstElt < NumSrcElt) { |
| 109 | // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>) |
| 110 | Constant *Zero = Constant::getNullValue(DstEltTy); |
| 111 | unsigned Ratio = NumSrcElt/NumDstElt; |
| 112 | unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits(); |
| 113 | unsigned SrcElt = 0; |
| 114 | for (unsigned i = 0; i != NumDstElt; ++i) { |
| 115 | // Build each element of the result. |
| 116 | Constant *Elt = Zero; |
| 117 | unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1); |
| 118 | for (unsigned j = 0; j != Ratio; ++j) { |
| 119 | Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(SrcElt++)); |
| 120 | if (!Src) // Reject constantexpr elements. |
| 121 | return ConstantExpr::getBitCast(C, DestTy); |
| 122 | |
| 123 | // Zero extend the element to the right size. |
| 124 | Src = ConstantExpr::getZExt(Src, Elt->getType()); |
| 125 | |
| 126 | // Shift it to the right place, depending on endianness. |
| 127 | Src = ConstantExpr::getShl(Src, |
| 128 | ConstantInt::get(Src->getType(), ShiftAmt)); |
| 129 | ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; |
| 130 | |
| 131 | // Mix it in. |
| 132 | Elt = ConstantExpr::getOr(Elt, Src); |
| 133 | } |
| 134 | Result.push_back(Elt); |
| 135 | } |
| 136 | } else { |
| 137 | // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) |
| 138 | unsigned Ratio = NumDstElt/NumSrcElt; |
| 139 | unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits(); |
| 140 | |
| 141 | // Loop over each source value, expanding into multiple results. |
| 142 | for (unsigned i = 0; i != NumSrcElt; ++i) { |
| 143 | Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(i)); |
| 144 | if (!Src) // Reject constantexpr elements. |
| 145 | return ConstantExpr::getBitCast(C, DestTy); |
| 146 | |
| 147 | unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1); |
| 148 | for (unsigned j = 0; j != Ratio; ++j) { |
| 149 | // Shift the piece of the value into the right place, depending on |
| 150 | // endianness. |
| 151 | Constant *Elt = ConstantExpr::getLShr(Src, |
| 152 | ConstantInt::get(Src->getType(), ShiftAmt)); |
| 153 | ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; |
| 154 | |
| 155 | // Truncate and remember this piece. |
| 156 | Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy)); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return ConstantVector::get(Result.data(), Result.size()); |
| 162 | } |
| 163 | |
| 164 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 165 | /// IsConstantOffsetFromGlobal - If this constant is actually a constant offset |
| 166 | /// from a global, return the global and the constant. Because of |
| 167 | /// constantexprs, this function is recursive. |
| 168 | static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, |
| 169 | int64_t &Offset, const TargetData &TD) { |
| 170 | // Trivial case, constant is the global. |
| 171 | if ((GV = dyn_cast<GlobalValue>(C))) { |
| 172 | Offset = 0; |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | // Otherwise, if this isn't a constant expr, bail out. |
| 177 | ConstantExpr *CE = dyn_cast<ConstantExpr>(C); |
| 178 | if (!CE) return false; |
| 179 | |
| 180 | // Look through ptr->int and ptr->ptr casts. |
| 181 | if (CE->getOpcode() == Instruction::PtrToInt || |
| 182 | CE->getOpcode() == Instruction::BitCast) |
| 183 | return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD); |
| 184 | |
| 185 | // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) |
| 186 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 187 | // Cannot compute this if the element type of the pointer is missing size |
| 188 | // info. |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 189 | if (!cast<PointerType>(CE->getOperand(0)->getType()) |
| 190 | ->getElementType()->isSized()) |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 191 | return false; |
| 192 | |
| 193 | // If the base isn't a global+constant, we aren't either. |
| 194 | if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD)) |
| 195 | return false; |
| 196 | |
| 197 | // Otherwise, add any offset that our operands provide. |
| 198 | gep_type_iterator GTI = gep_type_begin(CE); |
Gabor Greif | de2d74b | 2008-05-22 06:43:33 +0000 | [diff] [blame] | 199 | for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end(); |
Gabor Greif | 785c6af | 2008-05-22 19:24:54 +0000 | [diff] [blame] | 200 | i != e; ++i, ++GTI) { |
Gabor Greif | de2d74b | 2008-05-22 06:43:33 +0000 | [diff] [blame] | 201 | ConstantInt *CI = dyn_cast<ConstantInt>(*i); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 202 | if (!CI) return false; // Index isn't a simple constant? |
| 203 | if (CI->getZExtValue() == 0) continue; // Not adding anything. |
| 204 | |
| 205 | if (const StructType *ST = dyn_cast<StructType>(*GTI)) { |
| 206 | // N = N + Offset |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 207 | Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue()); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 208 | } else { |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 209 | const SequentialType *SQT = cast<SequentialType>(*GTI); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 210 | Offset += TD.getTypeAllocSize(SQT->getElementType())*CI->getSExtValue(); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 219 | /// ReadDataFromGlobal - Recursive helper to read bits out of global. C is the |
| 220 | /// constant being copied out of. ByteOffset is an offset into C. CurPtr is the |
| 221 | /// pointer to copy results into and BytesLeft is the number of bytes left in |
| 222 | /// the CurPtr buffer. TD is the target data. |
| 223 | static bool ReadDataFromGlobal(Constant *C, uint64_t ByteOffset, |
| 224 | unsigned char *CurPtr, unsigned BytesLeft, |
| 225 | const TargetData &TD) { |
| 226 | assert(ByteOffset <= TD.getTypeAllocSize(C->getType()) && |
| 227 | "Out of range access"); |
| 228 | |
Chris Lattner | c7b1382 | 2009-10-24 05:27:19 +0000 | [diff] [blame] | 229 | // If this element is zero or undefined, we can just return since *CurPtr is |
| 230 | // zero initialized. |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 231 | if (isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) |
| 232 | return true; |
| 233 | |
| 234 | if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { |
| 235 | if (CI->getBitWidth() > 64 || |
| 236 | (CI->getBitWidth() & 7) != 0) |
| 237 | return false; |
| 238 | |
| 239 | uint64_t Val = CI->getZExtValue(); |
| 240 | unsigned IntBytes = unsigned(CI->getBitWidth()/8); |
| 241 | |
| 242 | for (unsigned i = 0; i != BytesLeft && ByteOffset != IntBytes; ++i) { |
Chris Lattner | c7b1382 | 2009-10-24 05:27:19 +0000 | [diff] [blame] | 243 | CurPtr[i] = (unsigned char)(Val >> (ByteOffset * 8)); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 244 | ++ByteOffset; |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) { |
| 250 | if (CFP->getType()->isDoubleTy()) { |
Chris Lattner | 6333c39 | 2009-10-25 06:08:26 +0000 | [diff] [blame^] | 251 | C = FoldBitCast(C, Type::getInt64Ty(C->getContext()), TD); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 252 | return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD); |
| 253 | } |
| 254 | if (CFP->getType()->isFloatTy()){ |
Chris Lattner | 6333c39 | 2009-10-25 06:08:26 +0000 | [diff] [blame^] | 255 | C = FoldBitCast(C, Type::getInt32Ty(C->getContext()), TD); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 256 | return ReadDataFromGlobal(C, ByteOffset, CurPtr, BytesLeft, TD); |
| 257 | } |
Chris Lattner | c7b1382 | 2009-10-24 05:27:19 +0000 | [diff] [blame] | 258 | return false; |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 262 | const StructLayout *SL = TD.getStructLayout(CS->getType()); |
| 263 | unsigned Index = SL->getElementContainingOffset(ByteOffset); |
| 264 | uint64_t CurEltOffset = SL->getElementOffset(Index); |
| 265 | ByteOffset -= CurEltOffset; |
| 266 | |
| 267 | while (1) { |
| 268 | // If the element access is to the element itself and not to tail padding, |
| 269 | // read the bytes from the element. |
| 270 | uint64_t EltSize = TD.getTypeAllocSize(CS->getOperand(Index)->getType()); |
| 271 | |
| 272 | if (ByteOffset < EltSize && |
| 273 | !ReadDataFromGlobal(CS->getOperand(Index), ByteOffset, CurPtr, |
| 274 | BytesLeft, TD)) |
| 275 | return false; |
| 276 | |
| 277 | ++Index; |
| 278 | |
| 279 | // Check to see if we read from the last struct element, if so we're done. |
| 280 | if (Index == CS->getType()->getNumElements()) |
| 281 | return true; |
| 282 | |
| 283 | // If we read all of the bytes we needed from this element we're done. |
| 284 | uint64_t NextEltOffset = SL->getElementOffset(Index); |
| 285 | |
| 286 | if (BytesLeft <= NextEltOffset-CurEltOffset-ByteOffset) |
| 287 | return true; |
| 288 | |
| 289 | // Move to the next element of the struct. |
Chris Lattner | c5af649 | 2009-10-24 05:22:15 +0000 | [diff] [blame] | 290 | CurPtr += NextEltOffset-CurEltOffset-ByteOffset; |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 291 | BytesLeft -= NextEltOffset-CurEltOffset-ByteOffset; |
| 292 | ByteOffset = 0; |
| 293 | CurEltOffset = NextEltOffset; |
| 294 | } |
| 295 | // not reached. |
| 296 | } |
| 297 | |
| 298 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) { |
| 299 | uint64_t EltSize = TD.getTypeAllocSize(CA->getType()->getElementType()); |
| 300 | uint64_t Index = ByteOffset / EltSize; |
| 301 | uint64_t Offset = ByteOffset - Index * EltSize; |
| 302 | for (; Index != CA->getType()->getNumElements(); ++Index) { |
| 303 | if (!ReadDataFromGlobal(CA->getOperand(Index), Offset, CurPtr, |
| 304 | BytesLeft, TD)) |
| 305 | return false; |
| 306 | if (EltSize >= BytesLeft) |
| 307 | return true; |
| 308 | |
| 309 | Offset = 0; |
| 310 | BytesLeft -= EltSize; |
| 311 | CurPtr += EltSize; |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) { |
| 317 | uint64_t EltSize = TD.getTypeAllocSize(CV->getType()->getElementType()); |
| 318 | uint64_t Index = ByteOffset / EltSize; |
| 319 | uint64_t Offset = ByteOffset - Index * EltSize; |
| 320 | for (; Index != CV->getType()->getNumElements(); ++Index) { |
| 321 | if (!ReadDataFromGlobal(CV->getOperand(Index), Offset, CurPtr, |
| 322 | BytesLeft, TD)) |
| 323 | return false; |
| 324 | if (EltSize >= BytesLeft) |
| 325 | return true; |
| 326 | |
| 327 | Offset = 0; |
| 328 | BytesLeft -= EltSize; |
| 329 | CurPtr += EltSize; |
| 330 | } |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | // Otherwise, unknown initializer type. |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | static Constant *FoldReinterpretLoadFromConstPtr(Constant *C, |
| 339 | const TargetData &TD) { |
Chris Lattner | 17f0cd3 | 2009-10-23 06:57:37 +0000 | [diff] [blame] | 340 | const Type *LoadTy = cast<PointerType>(C->getType())->getElementType(); |
| 341 | const IntegerType *IntType = dyn_cast<IntegerType>(LoadTy); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 342 | |
| 343 | // If this isn't an integer load we can't fold it directly. |
| 344 | if (!IntType) { |
| 345 | // If this is a float/double load, we can try folding it as an int32/64 load |
Chris Lattner | 17f0cd3 | 2009-10-23 06:57:37 +0000 | [diff] [blame] | 346 | // and then bitcast the result. This can be useful for union cases. Note |
| 347 | // that address spaces don't matter here since we're not going to result in |
| 348 | // an actual new load. |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 349 | const Type *MapTy; |
Chris Lattner | 17f0cd3 | 2009-10-23 06:57:37 +0000 | [diff] [blame] | 350 | if (LoadTy->isFloatTy()) |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 351 | MapTy = Type::getInt32PtrTy(C->getContext()); |
Chris Lattner | 17f0cd3 | 2009-10-23 06:57:37 +0000 | [diff] [blame] | 352 | else if (LoadTy->isDoubleTy()) |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 353 | MapTy = Type::getInt64PtrTy(C->getContext()); |
Chris Lattner | 17f0cd3 | 2009-10-23 06:57:37 +0000 | [diff] [blame] | 354 | else if (isa<VectorType>(LoadTy)) { |
| 355 | MapTy = IntegerType::get(C->getContext(), |
| 356 | TD.getTypeAllocSizeInBits(LoadTy)); |
| 357 | MapTy = PointerType::getUnqual(MapTy); |
| 358 | } else |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 359 | return 0; |
| 360 | |
Chris Lattner | 6333c39 | 2009-10-25 06:08:26 +0000 | [diff] [blame^] | 361 | C = FoldBitCast(C, MapTy, TD); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 362 | if (Constant *Res = FoldReinterpretLoadFromConstPtr(C, TD)) |
Chris Lattner | 6333c39 | 2009-10-25 06:08:26 +0000 | [diff] [blame^] | 363 | return FoldBitCast(Res, LoadTy, TD); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | unsigned BytesLoaded = (IntType->getBitWidth() + 7) / 8; |
Chris Lattner | 739208a | 2009-10-23 06:50:36 +0000 | [diff] [blame] | 368 | if (BytesLoaded > 32 || BytesLoaded == 0) return 0; |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 369 | |
| 370 | GlobalValue *GVal; |
| 371 | int64_t Offset; |
| 372 | if (!IsConstantOffsetFromGlobal(C, GVal, Offset, TD)) |
| 373 | return 0; |
| 374 | |
| 375 | GlobalVariable *GV = dyn_cast<GlobalVariable>(GVal); |
Chris Lattner | c7b1382 | 2009-10-24 05:27:19 +0000 | [diff] [blame] | 376 | if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 377 | !GV->getInitializer()->getType()->isSized()) |
| 378 | return 0; |
| 379 | |
| 380 | // If we're loading off the beginning of the global, some bytes may be valid, |
| 381 | // but we don't try to handle this. |
| 382 | if (Offset < 0) return 0; |
| 383 | |
| 384 | // If we're not accessing anything in this constant, the result is undefined. |
| 385 | if (uint64_t(Offset) >= TD.getTypeAllocSize(GV->getInitializer()->getType())) |
| 386 | return UndefValue::get(IntType); |
| 387 | |
Chris Lattner | 739208a | 2009-10-23 06:50:36 +0000 | [diff] [blame] | 388 | unsigned char RawBytes[32] = {0}; |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 389 | if (!ReadDataFromGlobal(GV->getInitializer(), Offset, RawBytes, |
| 390 | BytesLoaded, TD)) |
| 391 | return 0; |
| 392 | |
Chris Lattner | 739208a | 2009-10-23 06:50:36 +0000 | [diff] [blame] | 393 | APInt ResultVal(IntType->getBitWidth(), 0); |
| 394 | for (unsigned i = 0; i != BytesLoaded; ++i) { |
| 395 | ResultVal <<= 8; |
| 396 | ResultVal |= APInt(IntType->getBitWidth(), RawBytes[BytesLoaded-1-i]); |
| 397 | } |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 739208a | 2009-10-23 06:50:36 +0000 | [diff] [blame] | 399 | return ConstantInt::get(IntType->getContext(), ResultVal); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Chris Lattner | 878e494 | 2009-10-22 06:25:11 +0000 | [diff] [blame] | 402 | /// ConstantFoldLoadFromConstPtr - Return the value that a load from C would |
| 403 | /// produce if it is constant and determinable. If this is not determinable, |
| 404 | /// return null. |
| 405 | Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, |
| 406 | const TargetData *TD) { |
| 407 | // First, try the easy cases: |
| 408 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) |
| 409 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
| 410 | return GV->getInitializer(); |
| 411 | |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 412 | // If the loaded value isn't a constant expr, we can't handle it. |
| 413 | ConstantExpr *CE = dyn_cast<ConstantExpr>(C); |
| 414 | if (!CE) return 0; |
| 415 | |
| 416 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 417 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0))) |
| 418 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) |
| 419 | if (Constant *V = |
| 420 | ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) |
| 421 | return V; |
| 422 | } |
| 423 | |
| 424 | // Instead of loading constant c string, use corresponding integer value |
| 425 | // directly if string length is small enough. |
| 426 | std::string Str; |
| 427 | if (TD && GetConstantStringInfo(CE->getOperand(0), Str) && !Str.empty()) { |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 428 | unsigned StrLen = Str.length(); |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 429 | const Type *Ty = cast<PointerType>(CE->getType())->getElementType(); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 430 | unsigned NumBits = Ty->getPrimitiveSizeInBits(); |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 431 | // Replace LI with immediate integer store. |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 432 | if ((NumBits >> 3) == StrLen + 1) { |
| 433 | APInt StrVal(NumBits, 0); |
| 434 | APInt SingleChar(NumBits, 0); |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 435 | if (TD->isLittleEndian()) { |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 436 | for (signed i = StrLen-1; i >= 0; i--) { |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 437 | SingleChar = (uint64_t) Str[i] & UCHAR_MAX; |
Chris Lattner | 62d327e | 2009-10-22 06:38:35 +0000 | [diff] [blame] | 438 | StrVal = (StrVal << 8) | SingleChar; |
| 439 | } |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 440 | } else { |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 441 | for (unsigned i = 0; i < StrLen; i++) { |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 442 | SingleChar = (uint64_t) Str[i] & UCHAR_MAX; |
| 443 | StrVal = (StrVal << 8) | SingleChar; |
| 444 | } |
| 445 | // Append NULL at the end. |
| 446 | SingleChar = 0; |
| 447 | StrVal = (StrVal << 8) | SingleChar; |
Chris Lattner | 62d327e | 2009-10-22 06:38:35 +0000 | [diff] [blame] | 448 | } |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 449 | return ConstantInt::get(CE->getContext(), StrVal); |
Chris Lattner | 62d327e | 2009-10-22 06:38:35 +0000 | [diff] [blame] | 450 | } |
Chris Lattner | 878e494 | 2009-10-22 06:25:11 +0000 | [diff] [blame] | 451 | } |
Chris Lattner | e00c43f | 2009-10-22 06:44:07 +0000 | [diff] [blame] | 452 | |
| 453 | // If this load comes from anywhere in a constant global, and if the global |
| 454 | // is all undef or zero, we know what it loads. |
| 455 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getUnderlyingObject())){ |
| 456 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) { |
| 457 | const Type *ResTy = cast<PointerType>(C->getType())->getElementType(); |
| 458 | if (GV->getInitializer()->isNullValue()) |
| 459 | return Constant::getNullValue(ResTy); |
| 460 | if (isa<UndefValue>(GV->getInitializer())) |
| 461 | return UndefValue::get(ResTy); |
| 462 | } |
| 463 | } |
| 464 | |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 465 | // Try hard to fold loads from bitcasted strange and non-type-safe things. We |
| 466 | // currently don't do any of this for big endian systems. It can be |
| 467 | // generalized in the future if someone is interested. |
| 468 | if (TD && TD->isLittleEndian()) |
| 469 | return FoldReinterpretLoadFromConstPtr(CE, *TD); |
Chris Lattner | 878e494 | 2009-10-22 06:25:11 +0000 | [diff] [blame] | 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static Constant *ConstantFoldLoadInst(const LoadInst *LI, const TargetData *TD){ |
| 474 | if (LI->isVolatile()) return 0; |
| 475 | |
| 476 | if (Constant *C = dyn_cast<Constant>(LI->getOperand(0))) |
| 477 | return ConstantFoldLoadFromConstPtr(C, TD); |
Chris Lattner | fe8c7c8 | 2009-10-23 06:23:49 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 878e494 | 2009-10-22 06:25:11 +0000 | [diff] [blame] | 479 | return 0; |
| 480 | } |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 481 | |
| 482 | /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression. |
Nick Lewycky | 67e3566 | 2008-12-15 01:35:36 +0000 | [diff] [blame] | 483 | /// Attempt to symbolically evaluate the result of a binary operator merging |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 484 | /// these together. If target data info is available, it is provided as TD, |
| 485 | /// otherwise TD is null. |
| 486 | static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 487 | Constant *Op1, const TargetData *TD, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 488 | LLVMContext &Context){ |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 489 | // SROA |
| 490 | |
| 491 | // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. |
| 492 | // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute |
| 493 | // bits. |
| 494 | |
| 495 | |
| 496 | // If the constant expr is something like &A[123] - &A[4].f, fold this into a |
| 497 | // constant. This happens frequently when iterating over a global array. |
| 498 | if (Opc == Instruction::Sub && TD) { |
| 499 | GlobalValue *GV1, *GV2; |
| 500 | int64_t Offs1, Offs2; |
| 501 | |
| 502 | if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD)) |
| 503 | if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) && |
| 504 | GV1 == GV2) { |
| 505 | // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow. |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 506 | return ConstantInt::get(Op0->getType(), Offs1-Offs2); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP |
| 514 | /// constant expression, do so. |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 515 | static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps, |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 516 | const Type *ResultTy, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 517 | LLVMContext &Context, |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 518 | const TargetData *TD) { |
| 519 | Constant *Ptr = Ops[0]; |
Chris Lattner | 268e7d7 | 2008-05-08 04:54:43 +0000 | [diff] [blame] | 520 | if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized()) |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 521 | return 0; |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 522 | |
| 523 | unsigned BitWidth = TD->getTypeSizeInBits(TD->getIntPtrType(Context)); |
| 524 | APInt BasePtr(BitWidth, 0); |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 525 | bool BaseIsInt = true; |
Chris Lattner | 268e7d7 | 2008-05-08 04:54:43 +0000 | [diff] [blame] | 526 | if (!Ptr->isNullValue()) { |
| 527 | // If this is a inttoptr from a constant int, we can fold this as the base, |
| 528 | // otherwise we can't. |
| 529 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 530 | if (CE->getOpcode() == Instruction::IntToPtr) |
Dan Gohman | 7178010 | 2009-08-21 18:27:26 +0000 | [diff] [blame] | 531 | if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) { |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 532 | BasePtr = Base->getValue(); |
Dan Gohman | 7178010 | 2009-08-21 18:27:26 +0000 | [diff] [blame] | 533 | BasePtr.zextOrTrunc(BitWidth); |
| 534 | } |
Chris Lattner | 268e7d7 | 2008-05-08 04:54:43 +0000 | [diff] [blame] | 535 | |
| 536 | if (BasePtr == 0) |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 537 | BaseIsInt = false; |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 538 | } |
Chris Lattner | 268e7d7 | 2008-05-08 04:54:43 +0000 | [diff] [blame] | 539 | |
| 540 | // If this is a constant expr gep that is effectively computing an |
| 541 | // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' |
| 542 | for (unsigned i = 1; i != NumOps; ++i) |
| 543 | if (!isa<ConstantInt>(Ops[i])) |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 544 | return 0; |
Chris Lattner | 268e7d7 | 2008-05-08 04:54:43 +0000 | [diff] [blame] | 545 | |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 546 | APInt Offset = APInt(BitWidth, |
| 547 | TD->getIndexedOffset(Ptr->getType(), |
| 548 | (Value**)Ops+1, NumOps-1)); |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 549 | // If the base value for this address is a literal integer value, fold the |
| 550 | // getelementptr to the resulting integer value casted to the pointer type. |
| 551 | if (BaseIsInt) { |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 552 | Constant *C = ConstantInt::get(Context, Offset+BasePtr); |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 553 | return ConstantExpr::getIntToPtr(C, ResultTy); |
| 554 | } |
| 555 | |
| 556 | // Otherwise form a regular getelementptr. Recompute the indices so that |
| 557 | // we eliminate over-indexing of the notional static type array bounds. |
| 558 | // This makes it easy to determine if the getelementptr is "inbounds". |
| 559 | // Also, this helps GlobalOpt do SROA on GlobalVariables. |
| 560 | const Type *Ty = Ptr->getType(); |
| 561 | SmallVector<Constant*, 32> NewIdxs; |
Dan Gohman | 3d01334 | 2009-08-19 22:46:59 +0000 | [diff] [blame] | 562 | do { |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 563 | if (const SequentialType *ATy = dyn_cast<SequentialType>(Ty)) { |
Dan Gohman | 3d01334 | 2009-08-19 22:46:59 +0000 | [diff] [blame] | 564 | // The only pointer indexing we'll do is on the first index of the GEP. |
Chris Lattner | f19f934 | 2009-09-02 05:35:45 +0000 | [diff] [blame] | 565 | if (isa<PointerType>(ATy) && !NewIdxs.empty()) |
Dan Gohman | 3d01334 | 2009-08-19 22:46:59 +0000 | [diff] [blame] | 566 | break; |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 567 | // Determine which element of the array the offset points into. |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 568 | APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType())); |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 569 | if (ElemSize == 0) |
| 570 | return 0; |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 571 | APInt NewIdx = Offset.udiv(ElemSize); |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 572 | Offset -= NewIdx * ElemSize; |
| 573 | NewIdxs.push_back(ConstantInt::get(TD->getIntPtrType(Context), NewIdx)); |
| 574 | Ty = ATy->getElementType(); |
| 575 | } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 576 | // Determine which field of the struct the offset points into. The |
| 577 | // getZExtValue is at least as safe as the StructLayout API because we |
| 578 | // know the offset is within the struct at this point. |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 579 | const StructLayout &SL = *TD->getStructLayout(STy); |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 580 | unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue()); |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 581 | NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), ElIdx)); |
Dan Gohman | cda9706 | 2009-08-21 16:52:54 +0000 | [diff] [blame] | 582 | Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx)); |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 583 | Ty = STy->getTypeAtIndex(ElIdx); |
| 584 | } else { |
Dan Gohman | 3d01334 | 2009-08-19 22:46:59 +0000 | [diff] [blame] | 585 | // We've reached some non-indexable type. |
| 586 | break; |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 587 | } |
Dan Gohman | 3d01334 | 2009-08-19 22:46:59 +0000 | [diff] [blame] | 588 | } while (Ty != cast<PointerType>(ResultTy)->getElementType()); |
| 589 | |
| 590 | // If we haven't used up the entire offset by descending the static |
| 591 | // type, then the offset is pointing into the middle of an indivisible |
| 592 | // member, so we can't simplify it. |
| 593 | if (Offset != 0) |
| 594 | return 0; |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 595 | |
Dan Gohman | 3bfbc45 | 2009-09-11 00:04:14 +0000 | [diff] [blame] | 596 | // Create a GEP. |
| 597 | Constant *C = |
Dan Gohman | 6e7ad95 | 2009-09-03 23:34:49 +0000 | [diff] [blame] | 598 | ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size()); |
| 599 | assert(cast<PointerType>(C->getType())->getElementType() == Ty && |
| 600 | "Computed GetElementPtr has unexpected type!"); |
Dan Gohman | de0e587 | 2009-08-19 18:18:36 +0000 | [diff] [blame] | 601 | |
Dan Gohman | 3d01334 | 2009-08-19 22:46:59 +0000 | [diff] [blame] | 602 | // If we ended up indexing a member with a type that doesn't match |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 603 | // the type of what the original indices indexed, add a cast. |
Dan Gohman | 3d01334 | 2009-08-19 22:46:59 +0000 | [diff] [blame] | 604 | if (Ty != cast<PointerType>(ResultTy)->getElementType()) |
Chris Lattner | 6333c39 | 2009-10-25 06:08:26 +0000 | [diff] [blame^] | 605 | C = FoldBitCast(C, ResultTy, *TD); |
Dan Gohman | 3d01334 | 2009-08-19 22:46:59 +0000 | [diff] [blame] | 606 | |
| 607 | return C; |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Chris Lattner | 1afab9c | 2007-12-11 07:29:44 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 611 | |
| 612 | //===----------------------------------------------------------------------===// |
| 613 | // Constant Folding public APIs |
| 614 | //===----------------------------------------------------------------------===// |
| 615 | |
| 616 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 617 | /// ConstantFoldInstruction - Attempt to constant fold the specified |
| 618 | /// instruction. If successful, the constant result is returned, if not, null |
| 619 | /// is returned. Note that this function can only fail when attempting to fold |
| 620 | /// instructions like loads and stores, which have no constant expression form. |
| 621 | /// |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 622 | Constant *llvm::ConstantFoldInstruction(Instruction *I, LLVMContext &Context, |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 623 | const TargetData *TD) { |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 624 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 625 | if (PN->getNumIncomingValues() == 0) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 626 | return UndefValue::get(PN->getType()); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 628 | Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0)); |
| 629 | if (Result == 0) return 0; |
| 630 | |
| 631 | // Handle PHI nodes specially here... |
| 632 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
| 633 | if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN) |
| 634 | return 0; // Not all the same incoming constants... |
| 635 | |
| 636 | // If we reach here, all incoming values are the same constant. |
| 637 | return Result; |
| 638 | } |
| 639 | |
| 640 | // Scan the operand list, checking to see if they are all constants, if so, |
| 641 | // hand off to ConstantFoldInstOperands. |
| 642 | SmallVector<Constant*, 8> Ops; |
Gabor Greif | de2d74b | 2008-05-22 06:43:33 +0000 | [diff] [blame] | 643 | for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) |
| 644 | if (Constant *Op = dyn_cast<Constant>(*i)) |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 645 | Ops.push_back(Op); |
| 646 | else |
| 647 | return 0; // All operands not constant! |
| 648 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 649 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 650 | return ConstantFoldCompareInstOperands(CI->getPredicate(), |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 651 | Ops.data(), Ops.size(), |
| 652 | Context, TD); |
Chris Lattner | 58665d4 | 2009-09-16 00:08:07 +0000 | [diff] [blame] | 653 | |
Chris Lattner | 878e494 | 2009-10-22 06:25:11 +0000 | [diff] [blame] | 654 | if (const LoadInst *LI = dyn_cast<LoadInst>(I)) |
| 655 | return ConstantFoldLoadInst(LI, TD); |
| 656 | |
Chris Lattner | 58665d4 | 2009-09-16 00:08:07 +0000 | [diff] [blame] | 657 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 658 | Ops.data(), Ops.size(), Context, TD); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 661 | /// ConstantFoldConstantExpression - Attempt to fold the constant expression |
| 662 | /// using the specified TargetData. If successful, the constant result is |
| 663 | /// result is returned, if not, null is returned. |
| 664 | Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 665 | LLVMContext &Context, |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 666 | const TargetData *TD) { |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 667 | SmallVector<Constant*, 8> Ops; |
| 668 | for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) |
| 669 | Ops.push_back(cast<Constant>(*i)); |
| 670 | |
| 671 | if (CE->isCompare()) |
| 672 | return ConstantFoldCompareInstOperands(CE->getPredicate(), |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 673 | Ops.data(), Ops.size(), |
| 674 | Context, TD); |
Chris Lattner | 58665d4 | 2009-09-16 00:08:07 +0000 | [diff] [blame] | 675 | return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), |
| 676 | Ops.data(), Ops.size(), Context, TD); |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 679 | /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the |
| 680 | /// specified opcode and operands. If successful, the constant result is |
| 681 | /// returned, if not, null is returned. Note that this function can fail when |
| 682 | /// attempting to fold instructions like loads and stores, which have no |
| 683 | /// constant expression form. |
| 684 | /// |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 685 | Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy, |
| 686 | Constant* const* Ops, unsigned NumOps, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 687 | LLVMContext &Context, |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 688 | const TargetData *TD) { |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 689 | // Handle easy binops first. |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 690 | if (Instruction::isBinaryOp(Opcode)) { |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 691 | if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 692 | if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD, |
| 693 | Context)) |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 694 | return C; |
| 695 | |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 696 | return ConstantExpr::get(Opcode, Ops[0], Ops[1]); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 697 | } |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 698 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 699 | switch (Opcode) { |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 700 | default: return 0; |
| 701 | case Instruction::Call: |
| 702 | if (Function *F = dyn_cast<Function>(Ops[0])) |
| 703 | if (canConstantFoldCallTo(F)) |
Chris Lattner | ad58eb3 | 2007-01-31 18:04:55 +0000 | [diff] [blame] | 704 | return ConstantFoldCall(F, Ops+1, NumOps-1); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 705 | return 0; |
| 706 | case Instruction::ICmp: |
| 707 | case Instruction::FCmp: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 708 | llvm_unreachable("This function is invalid for compares: no predicate specified"); |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame] | 709 | case Instruction::PtrToInt: |
| 710 | // If the input is a inttoptr, eliminate the pair. This requires knowing |
| 711 | // the width of a pointer, so it can't be done in ConstantExpr::getCast. |
| 712 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { |
| 713 | if (TD && CE->getOpcode() == Instruction::IntToPtr) { |
| 714 | Constant *Input = CE->getOperand(0); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 715 | unsigned InWidth = Input->getType()->getScalarSizeInBits(); |
Nick Lewycky | 04aa2c3 | 2008-10-24 06:14:27 +0000 | [diff] [blame] | 716 | if (TD->getPointerSizeInBits() < InWidth) { |
| 717 | Constant *Mask = |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 718 | ConstantInt::get(Context, APInt::getLowBitsSet(InWidth, |
Nick Lewycky | 04aa2c3 | 2008-10-24 06:14:27 +0000 | [diff] [blame] | 719 | TD->getPointerSizeInBits())); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 720 | Input = ConstantExpr::getAnd(Input, Mask); |
Nick Lewycky | 04aa2c3 | 2008-10-24 06:14:27 +0000 | [diff] [blame] | 721 | } |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame] | 722 | // Do a zext or trunc to get to the dest size. |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 723 | return ConstantExpr::getIntegerCast(Input, DestTy, false); |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame] | 724 | } |
| 725 | } |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 726 | return ConstantExpr::getCast(Opcode, Ops[0], DestTy); |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame] | 727 | case Instruction::IntToPtr: |
Duncan Sands | 81b06be | 2008-08-13 20:20:35 +0000 | [diff] [blame] | 728 | // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if |
| 729 | // the int size is >= the ptr size. This requires knowing the width of a |
| 730 | // pointer, so it can't be done in ConstantExpr::getCast. |
| 731 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { |
Dan Gohman | 9a38e3e | 2009-05-07 19:46:24 +0000 | [diff] [blame] | 732 | if (TD && |
Duncan Sands | 81b06be | 2008-08-13 20:20:35 +0000 | [diff] [blame] | 733 | TD->getPointerSizeInBits() <= |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 734 | CE->getType()->getScalarSizeInBits()) { |
Chris Lattner | 6333c39 | 2009-10-25 06:08:26 +0000 | [diff] [blame^] | 735 | if (CE->getOpcode() == Instruction::PtrToInt) |
| 736 | return FoldBitCast(CE->getOperand(0), DestTy, *TD); |
| 737 | |
Dan Gohman | 9a38e3e | 2009-05-07 19:46:24 +0000 | [diff] [blame] | 738 | // If there's a constant offset added to the integer value before |
| 739 | // it is casted back to a pointer, see if the expression can be |
| 740 | // converted into a GEP. |
| 741 | if (CE->getOpcode() == Instruction::Add) |
| 742 | if (ConstantInt *L = dyn_cast<ConstantInt>(CE->getOperand(0))) |
| 743 | if (ConstantExpr *R = dyn_cast<ConstantExpr>(CE->getOperand(1))) |
| 744 | if (R->getOpcode() == Instruction::PtrToInt) |
| 745 | if (GlobalVariable *GV = |
| 746 | dyn_cast<GlobalVariable>(R->getOperand(0))) { |
| 747 | const PointerType *GVTy = cast<PointerType>(GV->getType()); |
| 748 | if (const ArrayType *AT = |
| 749 | dyn_cast<ArrayType>(GVTy->getElementType())) { |
| 750 | const Type *ElTy = AT->getElementType(); |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 751 | uint64_t AllocSize = TD->getTypeAllocSize(ElTy); |
| 752 | APInt PSA(L->getValue().getBitWidth(), AllocSize); |
Dan Gohman | 9a38e3e | 2009-05-07 19:46:24 +0000 | [diff] [blame] | 753 | if (ElTy == cast<PointerType>(DestTy)->getElementType() && |
| 754 | L->getValue().urem(PSA) == 0) { |
| 755 | APInt ElemIdx = L->getValue().udiv(PSA); |
| 756 | if (ElemIdx.ult(APInt(ElemIdx.getBitWidth(), |
| 757 | AT->getNumElements()))) { |
| 758 | Constant *Index[] = { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 759 | Constant::getNullValue(CE->getType()), |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 760 | ConstantInt::get(Context, ElemIdx) |
Dan Gohman | 9a38e3e | 2009-05-07 19:46:24 +0000 | [diff] [blame] | 761 | }; |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 762 | return |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 763 | ConstantExpr::getGetElementPtr(GV, &Index[0], 2); |
Dan Gohman | 9a38e3e | 2009-05-07 19:46:24 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
Duncan Sands | 81b06be | 2008-08-13 20:20:35 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 770 | return ConstantExpr::getCast(Opcode, Ops[0], DestTy); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 771 | case Instruction::Trunc: |
| 772 | case Instruction::ZExt: |
| 773 | case Instruction::SExt: |
| 774 | case Instruction::FPTrunc: |
| 775 | case Instruction::FPExt: |
| 776 | case Instruction::UIToFP: |
| 777 | case Instruction::SIToFP: |
| 778 | case Instruction::FPToUI: |
| 779 | case Instruction::FPToSI: |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 780 | return ConstantExpr::getCast(Opcode, Ops[0], DestTy); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 781 | case Instruction::BitCast: |
Chris Lattner | 1afab9c | 2007-12-11 07:29:44 +0000 | [diff] [blame] | 782 | if (TD) |
Chris Lattner | 6333c39 | 2009-10-25 06:08:26 +0000 | [diff] [blame^] | 783 | return FoldBitCast(Ops[0], DestTy, *TD); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 784 | return ConstantExpr::getBitCast(Ops[0], DestTy); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 785 | case Instruction::Select: |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 786 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 787 | case Instruction::ExtractElement: |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 788 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 789 | case Instruction::InsertElement: |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 790 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 791 | case Instruction::ShuffleVector: |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 792 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 793 | case Instruction::GetElementPtr: |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 794 | if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, Context, TD)) |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 795 | return C; |
| 796 | |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 797 | return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 801 | /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare |
| 802 | /// instruction (icmp/fcmp) with the specified operands. If it fails, it |
| 803 | /// returns a constant expression of the specified operands. |
| 804 | /// |
| 805 | Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, |
| 806 | Constant*const * Ops, |
| 807 | unsigned NumOps, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 808 | LLVMContext &Context, |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 809 | const TargetData *TD) { |
| 810 | // fold: icmp (inttoptr x), null -> icmp x, 0 |
| 811 | // fold: icmp (ptrtoint x), 0 -> icmp x, null |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 812 | // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 813 | // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y |
| 814 | // |
| 815 | // ConstantExpr::getCompare cannot do this, because it doesn't have TD |
| 816 | // around to know if bit truncation is happening. |
| 817 | if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops[0])) { |
| 818 | if (TD && Ops[1]->isNullValue()) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 819 | const Type *IntPtrTy = TD->getIntPtrType(Context); |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 820 | if (CE0->getOpcode() == Instruction::IntToPtr) { |
| 821 | // Convert the integer value to the right size to ensure we get the |
| 822 | // proper extension or truncation. |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 823 | Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 824 | IntPtrTy, false); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 825 | Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 826 | return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, |
| 827 | Context, TD); |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | // Only do this transformation if the int is intptrty in size, otherwise |
| 831 | // there is a truncation or extension that we aren't modeling. |
| 832 | if (CE0->getOpcode() == Instruction::PtrToInt && |
| 833 | CE0->getType() == IntPtrTy) { |
| 834 | Constant *C = CE0->getOperand(0); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 835 | Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 836 | // FIXME! |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 837 | return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, |
| 838 | Context, TD); |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 842 | if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops[1])) { |
| 843 | if (TD && CE0->getOpcode() == CE1->getOpcode()) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 844 | const Type *IntPtrTy = TD->getIntPtrType(Context); |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 845 | |
| 846 | if (CE0->getOpcode() == Instruction::IntToPtr) { |
| 847 | // Convert the integer value to the right size to ensure we get the |
| 848 | // proper extension or truncation. |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 849 | Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0), |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 850 | IntPtrTy, false); |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 851 | Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0), |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 852 | IntPtrTy, false); |
| 853 | Constant *NewOps[] = { C0, C1 }; |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 854 | return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, |
| 855 | Context, TD); |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | // Only do this transformation if the int is intptrty in size, otherwise |
| 859 | // there is a truncation or extension that we aren't modeling. |
| 860 | if ((CE0->getOpcode() == Instruction::PtrToInt && |
| 861 | CE0->getType() == IntPtrTy && |
| 862 | CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType())) { |
| 863 | Constant *NewOps[] = { |
| 864 | CE0->getOperand(0), CE1->getOperand(0) |
| 865 | }; |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 866 | return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, |
| 867 | Context, TD); |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 868 | } |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | } |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 872 | return ConstantExpr::getCompare(Predicate, Ops[0], Ops[1]); |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 876 | /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a |
| 877 | /// getelementptr constantexpr, return the constant value being addressed by the |
| 878 | /// constant expression, or null if something is funny and we can't decide. |
| 879 | Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, |
Dan Gohman | c6f69e9 | 2009-10-05 16:36:26 +0000 | [diff] [blame] | 880 | ConstantExpr *CE) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 881 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 882 | return 0; // Do not allow stepping over the value! |
| 883 | |
| 884 | // Loop over all of the operands, tracking down which value we are |
| 885 | // addressing... |
| 886 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 887 | for (++I; I != E; ++I) |
| 888 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 889 | ConstantInt *CU = cast<ConstantInt>(I.getOperand()); |
| 890 | assert(CU->getZExtValue() < STy->getNumElements() && |
| 891 | "Struct index out of range!"); |
| 892 | unsigned El = (unsigned)CU->getZExtValue(); |
| 893 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 894 | C = CS->getOperand(El); |
| 895 | } else if (isa<ConstantAggregateZero>(C)) { |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 896 | C = Constant::getNullValue(STy->getElementType(El)); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 897 | } else if (isa<UndefValue>(C)) { |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 898 | C = UndefValue::get(STy->getElementType(El)); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 899 | } else { |
| 900 | return 0; |
| 901 | } |
| 902 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
| 903 | if (const ArrayType *ATy = dyn_cast<ArrayType>(*I)) { |
| 904 | if (CI->getZExtValue() >= ATy->getNumElements()) |
| 905 | return 0; |
| 906 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
| 907 | C = CA->getOperand(CI->getZExtValue()); |
| 908 | else if (isa<ConstantAggregateZero>(C)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 909 | C = Constant::getNullValue(ATy->getElementType()); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 910 | else if (isa<UndefValue>(C)) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 911 | C = UndefValue::get(ATy->getElementType()); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 912 | else |
| 913 | return 0; |
Chris Lattner | 62d327e | 2009-10-22 06:38:35 +0000 | [diff] [blame] | 914 | } else if (const VectorType *VTy = dyn_cast<VectorType>(*I)) { |
| 915 | if (CI->getZExtValue() >= VTy->getNumElements()) |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 916 | return 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 917 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 918 | C = CP->getOperand(CI->getZExtValue()); |
| 919 | else if (isa<ConstantAggregateZero>(C)) |
Chris Lattner | 62d327e | 2009-10-22 06:38:35 +0000 | [diff] [blame] | 920 | C = Constant::getNullValue(VTy->getElementType()); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 921 | else if (isa<UndefValue>(C)) |
Chris Lattner | 62d327e | 2009-10-22 06:38:35 +0000 | [diff] [blame] | 922 | C = UndefValue::get(VTy->getElementType()); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 923 | else |
| 924 | return 0; |
| 925 | } else { |
| 926 | return 0; |
| 927 | } |
| 928 | } else { |
| 929 | return 0; |
| 930 | } |
| 931 | return C; |
| 932 | } |
| 933 | |
| 934 | |
| 935 | //===----------------------------------------------------------------------===// |
| 936 | // Constant Folding for Calls |
| 937 | // |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 938 | |
| 939 | /// canConstantFoldCallTo - Return true if its even possible to fold a call to |
| 940 | /// the specified function. |
| 941 | bool |
Dan Gohman | fa9b80e | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 942 | llvm::canConstantFoldCallTo(const Function *F) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 943 | switch (F->getIntrinsicID()) { |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 944 | case Intrinsic::sqrt: |
| 945 | case Intrinsic::powi: |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 946 | case Intrinsic::bswap: |
| 947 | case Intrinsic::ctpop: |
| 948 | case Intrinsic::ctlz: |
| 949 | case Intrinsic::cttz: |
Chris Lattner | e65cd40 | 2009-10-05 05:26:04 +0000 | [diff] [blame] | 950 | case Intrinsic::uadd_with_overflow: |
| 951 | case Intrinsic::usub_with_overflow: |
Evan Phoenix | 1614e50 | 2009-10-05 22:53:52 +0000 | [diff] [blame] | 952 | case Intrinsic::sadd_with_overflow: |
| 953 | case Intrinsic::ssub_with_overflow: |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 954 | return true; |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 955 | default: |
| 956 | return false; |
| 957 | case 0: break; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Chris Lattner | 6f532a9 | 2009-04-03 00:02:39 +0000 | [diff] [blame] | 960 | if (!F->hasName()) return false; |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 961 | StringRef Name = F->getName(); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 962 | |
| 963 | // In these cases, the check of the length is required. We don't want to |
| 964 | // return true for a name like "cos\0blah" which strcmp would return equal to |
| 965 | // "cos", but has length 8. |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 966 | switch (Name[0]) { |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 967 | default: return false; |
| 968 | case 'a': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 969 | return Name == "acos" || Name == "asin" || |
| 970 | Name == "atan" || Name == "atan2"; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 971 | case 'c': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 972 | return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh"; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 973 | case 'e': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 974 | return Name == "exp"; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 975 | case 'f': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 976 | return Name == "fabs" || Name == "fmod" || Name == "floor"; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 977 | case 'l': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 978 | return Name == "log" || Name == "log10"; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 979 | case 'p': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 980 | return Name == "pow"; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 981 | case 's': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 982 | return Name == "sin" || Name == "sinh" || Name == "sqrt" || |
| 983 | Name == "sinf" || Name == "sqrtf"; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 984 | case 't': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 985 | return Name == "tan" || Name == "tanh"; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 989 | static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 990 | const Type *Ty, LLVMContext &Context) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 991 | errno = 0; |
| 992 | V = NativeFP(V); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 993 | if (errno != 0) { |
| 994 | errno = 0; |
| 995 | return 0; |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 996 | } |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 997 | |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 998 | if (Ty->isFloatTy()) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 999 | return ConstantFP::get(Context, APFloat((float)V)); |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 1000 | if (Ty->isDoubleTy()) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1001 | return ConstantFP::get(Context, APFloat(V)); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1002 | llvm_unreachable("Can only constant fold float/double"); |
Gabor Greif | 33e456d | 2008-05-21 14:07:30 +0000 | [diff] [blame] | 1003 | return 0; // dummy return to suppress warning |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 1006 | static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), |
| 1007 | double V, double W, |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1008 | const Type *Ty, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1009 | LLVMContext &Context) { |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 1010 | errno = 0; |
| 1011 | V = NativeFP(V, W); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 1012 | if (errno != 0) { |
| 1013 | errno = 0; |
| 1014 | return 0; |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1015 | } |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 1017 | if (Ty->isFloatTy()) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1018 | return ConstantFP::get(Context, APFloat((float)V)); |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 1019 | if (Ty->isDoubleTy()) |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1020 | return ConstantFP::get(Context, APFloat(V)); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1021 | llvm_unreachable("Can only constant fold float/double"); |
Gabor Greif | 33e456d | 2008-05-21 14:07:30 +0000 | [diff] [blame] | 1022 | return 0; // dummy return to suppress warning |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1025 | /// ConstantFoldCall - Attempt to constant fold a call to the specified function |
| 1026 | /// with the specified arguments, returning null if unsuccessful. |
| 1027 | Constant * |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 1028 | llvm::ConstantFoldCall(Function *F, |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1029 | Constant *const *Operands, unsigned NumOperands) { |
Chris Lattner | 6f532a9 | 2009-04-03 00:02:39 +0000 | [diff] [blame] | 1030 | if (!F->hasName()) return 0; |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 1031 | LLVMContext &Context = F->getContext(); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1032 | StringRef Name = F->getName(); |
Chris Lattner | e65cd40 | 2009-10-05 05:26:04 +0000 | [diff] [blame] | 1033 | |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1034 | const Type *Ty = F->getReturnType(); |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 1035 | if (NumOperands == 1) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1036 | if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 1037 | if (!Ty->isFloatTy() && !Ty->isDoubleTy()) |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1038 | return 0; |
| 1039 | /// Currently APFloat versions of these functions do not exist, so we use |
| 1040 | /// the host native double versions. Float versions are not called |
| 1041 | /// directly but for all these it is true (float)(f((double)arg)) == |
| 1042 | /// f(arg). Long double not supported yet. |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 1043 | double V = Ty->isFloatTy() ? (double)Op->getValueAPF().convertToFloat() : |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1044 | Op->getValueAPF().convertToDouble(); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1045 | switch (Name[0]) { |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1046 | case 'a': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1047 | if (Name == "acos") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1048 | return ConstantFoldFP(acos, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1049 | else if (Name == "asin") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1050 | return ConstantFoldFP(asin, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1051 | else if (Name == "atan") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1052 | return ConstantFoldFP(atan, V, Ty, Context); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1053 | break; |
| 1054 | case 'c': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1055 | if (Name == "ceil") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1056 | return ConstantFoldFP(ceil, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1057 | else if (Name == "cos") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1058 | return ConstantFoldFP(cos, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1059 | else if (Name == "cosh") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1060 | return ConstantFoldFP(cosh, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1061 | else if (Name == "cosf") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1062 | return ConstantFoldFP(cos, V, Ty, Context); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | case 'e': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1065 | if (Name == "exp") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1066 | return ConstantFoldFP(exp, V, Ty, Context); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1067 | break; |
| 1068 | case 'f': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1069 | if (Name == "fabs") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1070 | return ConstantFoldFP(fabs, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1071 | else if (Name == "floor") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1072 | return ConstantFoldFP(floor, V, Ty, Context); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1073 | break; |
| 1074 | case 'l': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1075 | if (Name == "log" && V > 0) |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1076 | return ConstantFoldFP(log, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1077 | else if (Name == "log10" && V > 0) |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1078 | return ConstantFoldFP(log10, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1079 | else if (Name == "llvm.sqrt.f32" || |
| 1080 | Name == "llvm.sqrt.f64") { |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1081 | if (V >= -0.0) |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1082 | return ConstantFoldFP(sqrt, V, Ty, Context); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1083 | else // Undefined |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1084 | return Constant::getNullValue(Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1085 | } |
| 1086 | break; |
| 1087 | case 's': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1088 | if (Name == "sin") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1089 | return ConstantFoldFP(sin, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1090 | else if (Name == "sinh") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1091 | return ConstantFoldFP(sinh, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1092 | else if (Name == "sqrt" && V >= 0) |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1093 | return ConstantFoldFP(sqrt, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1094 | else if (Name == "sqrtf" && V >= 0) |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1095 | return ConstantFoldFP(sqrt, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1096 | else if (Name == "sinf") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1097 | return ConstantFoldFP(sin, V, Ty, Context); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1098 | break; |
| 1099 | case 't': |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1100 | if (Name == "tan") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1101 | return ConstantFoldFP(tan, V, Ty, Context); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1102 | else if (Name == "tanh") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1103 | return ConstantFoldFP(tanh, V, Ty, Context); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 1104 | break; |
| 1105 | default: |
| 1106 | break; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1107 | } |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | |
| 1112 | if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) { |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1113 | if (Name.startswith("llvm.bswap")) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1114 | return ConstantInt::get(Context, Op->getValue().byteSwap()); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1115 | else if (Name.startswith("llvm.ctpop")) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1116 | return ConstantInt::get(Ty, Op->getValue().countPopulation()); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1117 | else if (Name.startswith("llvm.cttz")) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1118 | return ConstantInt::get(Ty, Op->getValue().countTrailingZeros()); |
Daniel Dunbar | f0443c1 | 2009-07-26 08:34:35 +0000 | [diff] [blame] | 1119 | else if (Name.startswith("llvm.ctlz")) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 1120 | return ConstantInt::get(Ty, Op->getValue().countLeadingZeros()); |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1121 | return 0; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1122 | } |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | if (NumOperands == 2) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1128 | if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) { |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 1129 | if (!Ty->isFloatTy() && !Ty->isDoubleTy()) |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 1130 | return 0; |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 1131 | double Op1V = Ty->isFloatTy() ? |
| 1132 | (double)Op1->getValueAPF().convertToFloat() : |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1133 | Op1->getValueAPF().convertToDouble(); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1134 | if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { |
Chris Lattner | d0806a1 | 2009-10-05 05:06:24 +0000 | [diff] [blame] | 1135 | if (Op2->getType() != Op1->getType()) |
| 1136 | return 0; |
| 1137 | |
| 1138 | double Op2V = Ty->isFloatTy() ? |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 1139 | (double)Op2->getValueAPF().convertToFloat(): |
| 1140 | Op2->getValueAPF().convertToDouble(); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1141 | |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1142 | if (Name == "pow") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1143 | return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty, Context); |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1144 | if (Name == "fmod") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1145 | return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty, Context); |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1146 | if (Name == "atan2") |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 1147 | return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty, Context); |
Chris Lattner | b5282dc | 2007-01-15 06:27:37 +0000 | [diff] [blame] | 1148 | } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) { |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1149 | if (Name == "llvm.powi.f32") |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1150 | return ConstantFP::get(Context, APFloat((float)std::pow((float)Op1V, |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 1151 | (int)Op2C->getZExtValue()))); |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1152 | if (Name == "llvm.powi.f64") |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1153 | return ConstantFP::get(Context, APFloat((double)std::pow((double)Op1V, |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 1154 | (int)Op2C->getZExtValue()))); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1155 | } |
Chris Lattner | 68a0603 | 2009-10-05 05:00:35 +0000 | [diff] [blame] | 1156 | return 0; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1157 | } |
Chris Lattner | e65cd40 | 2009-10-05 05:26:04 +0000 | [diff] [blame] | 1158 | |
| 1159 | |
| 1160 | if (ConstantInt *Op1 = dyn_cast<ConstantInt>(Operands[0])) { |
| 1161 | if (ConstantInt *Op2 = dyn_cast<ConstantInt>(Operands[1])) { |
| 1162 | switch (F->getIntrinsicID()) { |
| 1163 | default: break; |
| 1164 | case Intrinsic::uadd_with_overflow: { |
| 1165 | Constant *Res = ConstantExpr::getAdd(Op1, Op2); // result. |
| 1166 | Constant *Ops[] = { |
| 1167 | Res, ConstantExpr::getICmp(CmpInst::ICMP_ULT, Res, Op1) // overflow. |
| 1168 | }; |
| 1169 | return ConstantStruct::get(F->getContext(), Ops, 2, false); |
| 1170 | } |
| 1171 | case Intrinsic::usub_with_overflow: { |
| 1172 | Constant *Res = ConstantExpr::getSub(Op1, Op2); // result. |
| 1173 | Constant *Ops[] = { |
| 1174 | Res, ConstantExpr::getICmp(CmpInst::ICMP_UGT, Res, Op1) // overflow. |
| 1175 | }; |
| 1176 | return ConstantStruct::get(F->getContext(), Ops, 2, false); |
| 1177 | } |
Evan Phoenix | 1614e50 | 2009-10-05 22:53:52 +0000 | [diff] [blame] | 1178 | case Intrinsic::sadd_with_overflow: { |
| 1179 | Constant *Res = ConstantExpr::getAdd(Op1, Op2); // result. |
| 1180 | Constant *Overflow = ConstantExpr::getSelect( |
| 1181 | ConstantExpr::getICmp(CmpInst::ICMP_SGT, |
| 1182 | ConstantInt::get(Op1->getType(), 0), Op1), |
| 1183 | ConstantExpr::getICmp(CmpInst::ICMP_SGT, Res, Op2), |
| 1184 | ConstantExpr::getICmp(CmpInst::ICMP_SLT, Res, Op2)); // overflow. |
| 1185 | |
| 1186 | Constant *Ops[] = { Res, Overflow }; |
| 1187 | return ConstantStruct::get(F->getContext(), Ops, 2, false); |
| 1188 | } |
| 1189 | case Intrinsic::ssub_with_overflow: { |
| 1190 | Constant *Res = ConstantExpr::getSub(Op1, Op2); // result. |
| 1191 | Constant *Overflow = ConstantExpr::getSelect( |
| 1192 | ConstantExpr::getICmp(CmpInst::ICMP_SGT, |
| 1193 | ConstantInt::get(Op2->getType(), 0), Op2), |
| 1194 | ConstantExpr::getICmp(CmpInst::ICMP_SLT, Res, Op1), |
| 1195 | ConstantExpr::getICmp(CmpInst::ICMP_SGT, Res, Op1)); // overflow. |
| 1196 | |
| 1197 | Constant *Ops[] = { Res, Overflow }; |
| 1198 | return ConstantStruct::get(F->getContext(), Ops, 2, false); |
| 1199 | } |
Chris Lattner | e65cd40 | 2009-10-05 05:26:04 +0000 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
| 1205 | return 0; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1206 | } |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |