John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 1 | //===-- ConstantFolding.cpp - Analyze constant folding possibilities ------===// |
| 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 | // |
| 10 | // This family of functions determines the possibility of performing constant |
| 11 | // folding. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/ConstantFolding.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Intrinsics.h" |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 24 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 25 | #include "llvm/Support/MathExtras.h" |
| 26 | #include <cerrno> |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 27 | #include <cmath> |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Constant Folding internal helper functions |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | /// IsConstantOffsetFromGlobal - If this constant is actually a constant offset |
| 35 | /// from a global, return the global and the constant. Because of |
| 36 | /// constantexprs, this function is recursive. |
| 37 | static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, |
| 38 | int64_t &Offset, const TargetData &TD) { |
| 39 | // Trivial case, constant is the global. |
| 40 | if ((GV = dyn_cast<GlobalValue>(C))) { |
| 41 | Offset = 0; |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | // Otherwise, if this isn't a constant expr, bail out. |
| 46 | ConstantExpr *CE = dyn_cast<ConstantExpr>(C); |
| 47 | if (!CE) return false; |
| 48 | |
| 49 | // Look through ptr->int and ptr->ptr casts. |
| 50 | if (CE->getOpcode() == Instruction::PtrToInt || |
| 51 | CE->getOpcode() == Instruction::BitCast) |
| 52 | return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD); |
| 53 | |
| 54 | // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) |
| 55 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 56 | // Cannot compute this if the element type of the pointer is missing size |
| 57 | // info. |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 58 | if (!cast<PointerType>(CE->getOperand(0)->getType()) |
| 59 | ->getElementType()->isSized()) |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 60 | return false; |
| 61 | |
| 62 | // If the base isn't a global+constant, we aren't either. |
| 63 | if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD)) |
| 64 | return false; |
| 65 | |
| 66 | // Otherwise, add any offset that our operands provide. |
| 67 | gep_type_iterator GTI = gep_type_begin(CE); |
Gabor Greif | de2d74b | 2008-05-22 06:43:33 +0000 | [diff] [blame] | 68 | 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] | 69 | i != e; ++i, ++GTI) { |
Gabor Greif | de2d74b | 2008-05-22 06:43:33 +0000 | [diff] [blame] | 70 | ConstantInt *CI = dyn_cast<ConstantInt>(*i); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 71 | if (!CI) return false; // Index isn't a simple constant? |
| 72 | if (CI->getZExtValue() == 0) continue; // Not adding anything. |
| 73 | |
| 74 | if (const StructType *ST = dyn_cast<StructType>(*GTI)) { |
| 75 | // N = N + Offset |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 76 | Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue()); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 77 | } else { |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 78 | const SequentialType *SQT = cast<SequentialType>(*GTI); |
Duncan Sands | ceb4d1a | 2009-01-12 20:38:59 +0000 | [diff] [blame] | 79 | Offset += TD.getTypePaddedSize(SQT->getElementType())*CI->getSExtValue(); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression. |
Nick Lewycky | 67e3566 | 2008-12-15 01:35:36 +0000 | [diff] [blame] | 90 | /// Attempt to symbolically evaluate the result of a binary operator merging |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 91 | /// these together. If target data info is available, it is provided as TD, |
| 92 | /// otherwise TD is null. |
| 93 | static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, |
| 94 | Constant *Op1, const TargetData *TD){ |
| 95 | // SROA |
| 96 | |
| 97 | // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. |
| 98 | // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute |
| 99 | // bits. |
| 100 | |
| 101 | |
| 102 | // If the constant expr is something like &A[123] - &A[4].f, fold this into a |
| 103 | // constant. This happens frequently when iterating over a global array. |
| 104 | if (Opc == Instruction::Sub && TD) { |
| 105 | GlobalValue *GV1, *GV2; |
| 106 | int64_t Offs1, Offs2; |
| 107 | |
| 108 | if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD)) |
| 109 | if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) && |
| 110 | GV1 == GV2) { |
| 111 | // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow. |
| 112 | return ConstantInt::get(Op0->getType(), Offs1-Offs2); |
| 113 | } |
| 114 | } |
| 115 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP |
| 120 | /// constant expression, do so. |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 121 | static Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps, |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 122 | const Type *ResultTy, |
| 123 | const TargetData *TD) { |
| 124 | Constant *Ptr = Ops[0]; |
Chris Lattner | 268e7d7 | 2008-05-08 04:54:43 +0000 | [diff] [blame] | 125 | if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized()) |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 126 | return 0; |
| 127 | |
Chris Lattner | 268e7d7 | 2008-05-08 04:54:43 +0000 | [diff] [blame] | 128 | uint64_t BasePtr = 0; |
| 129 | if (!Ptr->isNullValue()) { |
| 130 | // If this is a inttoptr from a constant int, we can fold this as the base, |
| 131 | // otherwise we can't. |
| 132 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) |
| 133 | if (CE->getOpcode() == Instruction::IntToPtr) |
| 134 | if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) |
| 135 | BasePtr = Base->getZExtValue(); |
| 136 | |
| 137 | if (BasePtr == 0) |
| 138 | return 0; |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | 268e7d7 | 2008-05-08 04:54:43 +0000 | [diff] [blame] | 140 | |
| 141 | // If this is a constant expr gep that is effectively computing an |
| 142 | // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' |
| 143 | for (unsigned i = 1; i != NumOps; ++i) |
| 144 | if (!isa<ConstantInt>(Ops[i])) |
| 145 | return false; |
| 146 | |
| 147 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), |
| 148 | (Value**)Ops+1, NumOps-1); |
| 149 | Constant *C = ConstantInt::get(TD->getIntPtrType(), Offset+BasePtr); |
| 150 | return ConstantExpr::getIntToPtr(C, ResultTy); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Chris Lattner | 1afab9c | 2007-12-11 07:29:44 +0000 | [diff] [blame] | 153 | /// FoldBitCast - Constant fold bitcast, symbolically evaluating it with |
| 154 | /// targetdata. Return 0 if unfoldable. |
| 155 | static Constant *FoldBitCast(Constant *C, const Type *DestTy, |
| 156 | const TargetData &TD) { |
| 157 | // If this is a bitcast from constant vector -> vector, fold it. |
| 158 | if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) { |
| 159 | if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) { |
| 160 | // If the element types match, VMCore can fold it. |
| 161 | unsigned NumDstElt = DestVTy->getNumElements(); |
| 162 | unsigned NumSrcElt = CV->getNumOperands(); |
| 163 | if (NumDstElt == NumSrcElt) |
| 164 | return 0; |
| 165 | |
| 166 | const Type *SrcEltTy = CV->getType()->getElementType(); |
| 167 | const Type *DstEltTy = DestVTy->getElementType(); |
| 168 | |
| 169 | // Otherwise, we're changing the number of elements in a vector, which |
| 170 | // requires endianness information to do the right thing. For example, |
| 171 | // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) |
| 172 | // folds to (little endian): |
| 173 | // <4 x i32> <i32 0, i32 0, i32 1, i32 0> |
| 174 | // and to (big endian): |
| 175 | // <4 x i32> <i32 0, i32 0, i32 0, i32 1> |
| 176 | |
| 177 | // First thing is first. We only want to think about integer here, so if |
| 178 | // we have something in FP form, recast it as integer. |
| 179 | if (DstEltTy->isFloatingPoint()) { |
| 180 | // Fold to an vector of integers with same size as our FP type. |
| 181 | unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits(); |
| 182 | const Type *DestIVTy = VectorType::get(IntegerType::get(FPWidth), |
| 183 | NumDstElt); |
| 184 | // Recursively handle this integer conversion, if possible. |
| 185 | C = FoldBitCast(C, DestIVTy, TD); |
| 186 | if (!C) return 0; |
| 187 | |
| 188 | // Finally, VMCore can handle this now that #elts line up. |
| 189 | return ConstantExpr::getBitCast(C, DestTy); |
| 190 | } |
| 191 | |
| 192 | // Okay, we know the destination is integer, if the input is FP, convert |
| 193 | // it to integer first. |
| 194 | if (SrcEltTy->isFloatingPoint()) { |
| 195 | unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); |
| 196 | const Type *SrcIVTy = VectorType::get(IntegerType::get(FPWidth), |
| 197 | NumSrcElt); |
| 198 | // Ask VMCore to do the conversion now that #elts line up. |
| 199 | C = ConstantExpr::getBitCast(C, SrcIVTy); |
| 200 | CV = dyn_cast<ConstantVector>(C); |
| 201 | if (!CV) return 0; // If VMCore wasn't able to fold it, bail out. |
| 202 | } |
| 203 | |
| 204 | // Now we know that the input and output vectors are both integer vectors |
| 205 | // of the same size, and that their #elements is not the same. Do the |
| 206 | // conversion here, which depends on whether the input or output has |
| 207 | // more elements. |
| 208 | bool isLittleEndian = TD.isLittleEndian(); |
| 209 | |
| 210 | SmallVector<Constant*, 32> Result; |
| 211 | if (NumDstElt < NumSrcElt) { |
| 212 | // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>) |
| 213 | Constant *Zero = Constant::getNullValue(DstEltTy); |
| 214 | unsigned Ratio = NumSrcElt/NumDstElt; |
| 215 | unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits(); |
| 216 | unsigned SrcElt = 0; |
| 217 | for (unsigned i = 0; i != NumDstElt; ++i) { |
| 218 | // Build each element of the result. |
| 219 | Constant *Elt = Zero; |
| 220 | unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1); |
| 221 | for (unsigned j = 0; j != Ratio; ++j) { |
| 222 | Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(SrcElt++)); |
| 223 | if (!Src) return 0; // Reject constantexpr elements. |
| 224 | |
| 225 | // Zero extend the element to the right size. |
| 226 | Src = ConstantExpr::getZExt(Src, Elt->getType()); |
| 227 | |
| 228 | // Shift it to the right place, depending on endianness. |
| 229 | Src = ConstantExpr::getShl(Src, |
| 230 | ConstantInt::get(Src->getType(), ShiftAmt)); |
| 231 | ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; |
| 232 | |
| 233 | // Mix it in. |
| 234 | Elt = ConstantExpr::getOr(Elt, Src); |
| 235 | } |
| 236 | Result.push_back(Elt); |
| 237 | } |
| 238 | } else { |
| 239 | // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) |
| 240 | unsigned Ratio = NumDstElt/NumSrcElt; |
| 241 | unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits(); |
| 242 | |
| 243 | // Loop over each source value, expanding into multiple results. |
| 244 | for (unsigned i = 0; i != NumSrcElt; ++i) { |
| 245 | Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(i)); |
| 246 | if (!Src) return 0; // Reject constantexpr elements. |
| 247 | |
| 248 | unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1); |
| 249 | for (unsigned j = 0; j != Ratio; ++j) { |
| 250 | // Shift the piece of the value into the right place, depending on |
| 251 | // endianness. |
| 252 | Constant *Elt = ConstantExpr::getLShr(Src, |
| 253 | ConstantInt::get(Src->getType(), ShiftAmt)); |
| 254 | ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; |
| 255 | |
| 256 | // Truncate and remember this piece. |
| 257 | Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy)); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return ConstantVector::get(&Result[0], Result.size()); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 269 | |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | // Constant Folding public APIs |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
| 274 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 275 | /// ConstantFoldInstruction - Attempt to constant fold the specified |
| 276 | /// instruction. If successful, the constant result is returned, if not, null |
| 277 | /// is returned. Note that this function can only fail when attempting to fold |
| 278 | /// instructions like loads and stores, which have no constant expression form. |
| 279 | /// |
| 280 | Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) { |
| 281 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 282 | if (PN->getNumIncomingValues() == 0) |
Nick Lewycky | d433904 | 2008-11-20 04:36:13 +0000 | [diff] [blame] | 283 | return UndefValue::get(PN->getType()); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 285 | Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0)); |
| 286 | if (Result == 0) return 0; |
| 287 | |
| 288 | // Handle PHI nodes specially here... |
| 289 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
| 290 | if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN) |
| 291 | return 0; // Not all the same incoming constants... |
| 292 | |
| 293 | // If we reach here, all incoming values are the same constant. |
| 294 | return Result; |
| 295 | } |
| 296 | |
| 297 | // Scan the operand list, checking to see if they are all constants, if so, |
| 298 | // hand off to ConstantFoldInstOperands. |
| 299 | SmallVector<Constant*, 8> Ops; |
Gabor Greif | de2d74b | 2008-05-22 06:43:33 +0000 | [diff] [blame] | 300 | for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) |
| 301 | if (Constant *Op = dyn_cast<Constant>(*i)) |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 302 | Ops.push_back(Op); |
| 303 | else |
| 304 | return 0; // All operands not constant! |
| 305 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 306 | if (const CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 307 | return ConstantFoldCompareInstOperands(CI->getPredicate(), |
| 308 | &Ops[0], Ops.size(), TD); |
| 309 | else |
| 310 | return ConstantFoldInstOperands(I->getOpcode(), I->getType(), |
| 311 | &Ops[0], Ops.size(), TD); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 314 | /// ConstantFoldConstantExpression - Attempt to fold the constant expression |
| 315 | /// using the specified TargetData. If successful, the constant result is |
| 316 | /// result is returned, if not, null is returned. |
| 317 | Constant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE, |
| 318 | const TargetData *TD) { |
| 319 | assert(TD && "ConstantFoldConstantExpression requires a valid TargetData."); |
| 320 | |
| 321 | SmallVector<Constant*, 8> Ops; |
| 322 | for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) |
| 323 | Ops.push_back(cast<Constant>(*i)); |
| 324 | |
| 325 | if (CE->isCompare()) |
| 326 | return ConstantFoldCompareInstOperands(CE->getPredicate(), |
| 327 | &Ops[0], Ops.size(), TD); |
| 328 | else |
| 329 | return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), |
| 330 | &Ops[0], Ops.size(), TD); |
| 331 | } |
| 332 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 333 | /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the |
| 334 | /// specified opcode and operands. If successful, the constant result is |
| 335 | /// returned, if not, null is returned. Note that this function can fail when |
| 336 | /// attempting to fold instructions like loads and stores, which have no |
| 337 | /// constant expression form. |
| 338 | /// |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 339 | Constant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy, |
| 340 | Constant* const* Ops, unsigned NumOps, |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 341 | const TargetData *TD) { |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 342 | // Handle easy binops first. |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 343 | if (Instruction::isBinaryOp(Opcode)) { |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 344 | if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 345 | if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD)) |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 346 | return C; |
| 347 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 348 | return ConstantExpr::get(Opcode, Ops[0], Ops[1]); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 349 | } |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 350 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 351 | switch (Opcode) { |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 352 | default: return 0; |
| 353 | case Instruction::Call: |
| 354 | if (Function *F = dyn_cast<Function>(Ops[0])) |
| 355 | if (canConstantFoldCallTo(F)) |
Chris Lattner | ad58eb3 | 2007-01-31 18:04:55 +0000 | [diff] [blame] | 356 | return ConstantFoldCall(F, Ops+1, NumOps-1); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 357 | return 0; |
| 358 | case Instruction::ICmp: |
| 359 | case Instruction::FCmp: |
Nate Begeman | b5557ab | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 360 | case Instruction::VICmp: |
| 361 | case Instruction::VFCmp: |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 362 | assert(0 &&"This function is invalid for compares: no predicate specified"); |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame] | 363 | case Instruction::PtrToInt: |
| 364 | // If the input is a inttoptr, eliminate the pair. This requires knowing |
| 365 | // the width of a pointer, so it can't be done in ConstantExpr::getCast. |
| 366 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { |
| 367 | if (TD && CE->getOpcode() == Instruction::IntToPtr) { |
| 368 | Constant *Input = CE->getOperand(0); |
| 369 | unsigned InWidth = Input->getType()->getPrimitiveSizeInBits(); |
Nick Lewycky | 04aa2c3 | 2008-10-24 06:14:27 +0000 | [diff] [blame] | 370 | if (TD->getPointerSizeInBits() < InWidth) { |
| 371 | Constant *Mask = |
| 372 | ConstantInt::get(APInt::getLowBitsSet(InWidth, |
| 373 | TD->getPointerSizeInBits())); |
| 374 | Input = ConstantExpr::getAnd(Input, Mask); |
| 375 | } |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame] | 376 | // Do a zext or trunc to get to the dest size. |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 377 | return ConstantExpr::getIntegerCast(Input, DestTy, false); |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame] | 378 | } |
| 379 | } |
Chris Lattner | 1afab9c | 2007-12-11 07:29:44 +0000 | [diff] [blame] | 380 | return ConstantExpr::getCast(Opcode, Ops[0], DestTy); |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame] | 381 | case Instruction::IntToPtr: |
Duncan Sands | 81b06be | 2008-08-13 20:20:35 +0000 | [diff] [blame] | 382 | // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if |
| 383 | // the int size is >= the ptr size. This requires knowing the width of a |
| 384 | // pointer, so it can't be done in ConstantExpr::getCast. |
| 385 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { |
| 386 | if (TD && CE->getOpcode() == Instruction::PtrToInt && |
| 387 | TD->getPointerSizeInBits() <= |
| 388 | CE->getType()->getPrimitiveSizeInBits()) { |
| 389 | Constant *Input = CE->getOperand(0); |
| 390 | Constant *C = FoldBitCast(Input, DestTy, *TD); |
| 391 | return C ? C : ConstantExpr::getBitCast(Input, DestTy); |
| 392 | } |
| 393 | } |
| 394 | return ConstantExpr::getCast(Opcode, Ops[0], DestTy); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 395 | case Instruction::Trunc: |
| 396 | case Instruction::ZExt: |
| 397 | case Instruction::SExt: |
| 398 | case Instruction::FPTrunc: |
| 399 | case Instruction::FPExt: |
| 400 | case Instruction::UIToFP: |
| 401 | case Instruction::SIToFP: |
| 402 | case Instruction::FPToUI: |
| 403 | case Instruction::FPToSI: |
Chris Lattner | 1afab9c | 2007-12-11 07:29:44 +0000 | [diff] [blame] | 404 | return ConstantExpr::getCast(Opcode, Ops[0], DestTy); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 405 | case Instruction::BitCast: |
Chris Lattner | 1afab9c | 2007-12-11 07:29:44 +0000 | [diff] [blame] | 406 | if (TD) |
| 407 | if (Constant *C = FoldBitCast(Ops[0], DestTy, *TD)) |
| 408 | return C; |
| 409 | return ConstantExpr::getBitCast(Ops[0], DestTy); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 410 | case Instruction::Select: |
| 411 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 412 | case Instruction::ExtractElement: |
| 413 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
| 414 | case Instruction::InsertElement: |
| 415 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 416 | case Instruction::ShuffleVector: |
| 417 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
| 418 | case Instruction::GetElementPtr: |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 419 | if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, TD)) |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 420 | return C; |
| 421 | |
Chris Lattner | d917fe5 | 2007-01-31 04:42:05 +0000 | [diff] [blame] | 422 | return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 426 | /// ConstantFoldCompareInstOperands - Attempt to constant fold a compare |
| 427 | /// instruction (icmp/fcmp) with the specified operands. If it fails, it |
| 428 | /// returns a constant expression of the specified operands. |
| 429 | /// |
| 430 | Constant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, |
| 431 | Constant*const * Ops, |
| 432 | unsigned NumOps, |
| 433 | const TargetData *TD) { |
| 434 | // fold: icmp (inttoptr x), null -> icmp x, 0 |
| 435 | // fold: icmp (ptrtoint x), 0 -> icmp x, null |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 436 | // 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] | 437 | // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y |
| 438 | // |
| 439 | // ConstantExpr::getCompare cannot do this, because it doesn't have TD |
| 440 | // around to know if bit truncation is happening. |
| 441 | if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops[0])) { |
| 442 | if (TD && Ops[1]->isNullValue()) { |
| 443 | const Type *IntPtrTy = TD->getIntPtrType(); |
| 444 | if (CE0->getOpcode() == Instruction::IntToPtr) { |
| 445 | // Convert the integer value to the right size to ensure we get the |
| 446 | // proper extension or truncation. |
| 447 | Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), |
| 448 | IntPtrTy, false); |
| 449 | Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; |
| 450 | return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD); |
| 451 | } |
| 452 | |
| 453 | // Only do this transformation if the int is intptrty in size, otherwise |
| 454 | // there is a truncation or extension that we aren't modeling. |
| 455 | if (CE0->getOpcode() == Instruction::PtrToInt && |
| 456 | CE0->getType() == IntPtrTy) { |
| 457 | Constant *C = CE0->getOperand(0); |
| 458 | Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; |
| 459 | // FIXME! |
| 460 | return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD); |
| 461 | } |
| 462 | } |
| 463 | |
Nick Lewycky | 3dfd7bf | 2008-05-25 20:56:15 +0000 | [diff] [blame] | 464 | if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops[1])) { |
| 465 | if (TD && CE0->getOpcode() == CE1->getOpcode()) { |
| 466 | const Type *IntPtrTy = TD->getIntPtrType(); |
| 467 | |
| 468 | if (CE0->getOpcode() == Instruction::IntToPtr) { |
| 469 | // Convert the integer value to the right size to ensure we get the |
| 470 | // proper extension or truncation. |
| 471 | Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0), |
| 472 | IntPtrTy, false); |
| 473 | Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0), |
| 474 | IntPtrTy, false); |
| 475 | Constant *NewOps[] = { C0, C1 }; |
| 476 | return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD); |
| 477 | } |
| 478 | |
| 479 | // Only do this transformation if the int is intptrty in size, otherwise |
| 480 | // there is a truncation or extension that we aren't modeling. |
| 481 | if ((CE0->getOpcode() == Instruction::PtrToInt && |
| 482 | CE0->getType() == IntPtrTy && |
| 483 | CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType())) { |
| 484 | Constant *NewOps[] = { |
| 485 | CE0->getOperand(0), CE1->getOperand(0) |
| 486 | }; |
| 487 | return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, TD); |
| 488 | } |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | } |
Nate Begeman | b5557ab | 2008-07-25 17:35:37 +0000 | [diff] [blame] | 492 | return ConstantExpr::getCompare(Predicate, Ops[0], Ops[1]); |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 496 | /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a |
| 497 | /// getelementptr constantexpr, return the constant value being addressed by the |
| 498 | /// constant expression, or null if something is funny and we can't decide. |
| 499 | Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, |
| 500 | ConstantExpr *CE) { |
| 501 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
| 502 | return 0; // Do not allow stepping over the value! |
| 503 | |
| 504 | // Loop over all of the operands, tracking down which value we are |
| 505 | // addressing... |
| 506 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 507 | for (++I; I != E; ++I) |
| 508 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 509 | ConstantInt *CU = cast<ConstantInt>(I.getOperand()); |
| 510 | assert(CU->getZExtValue() < STy->getNumElements() && |
| 511 | "Struct index out of range!"); |
| 512 | unsigned El = (unsigned)CU->getZExtValue(); |
| 513 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 514 | C = CS->getOperand(El); |
| 515 | } else if (isa<ConstantAggregateZero>(C)) { |
| 516 | C = Constant::getNullValue(STy->getElementType(El)); |
| 517 | } else if (isa<UndefValue>(C)) { |
| 518 | C = UndefValue::get(STy->getElementType(El)); |
| 519 | } else { |
| 520 | return 0; |
| 521 | } |
| 522 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
| 523 | if (const ArrayType *ATy = dyn_cast<ArrayType>(*I)) { |
| 524 | if (CI->getZExtValue() >= ATy->getNumElements()) |
| 525 | return 0; |
| 526 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
| 527 | C = CA->getOperand(CI->getZExtValue()); |
| 528 | else if (isa<ConstantAggregateZero>(C)) |
| 529 | C = Constant::getNullValue(ATy->getElementType()); |
| 530 | else if (isa<UndefValue>(C)) |
| 531 | C = UndefValue::get(ATy->getElementType()); |
| 532 | else |
| 533 | return 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 534 | } else if (const VectorType *PTy = dyn_cast<VectorType>(*I)) { |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 535 | if (CI->getZExtValue() >= PTy->getNumElements()) |
| 536 | return 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 537 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 538 | C = CP->getOperand(CI->getZExtValue()); |
| 539 | else if (isa<ConstantAggregateZero>(C)) |
| 540 | C = Constant::getNullValue(PTy->getElementType()); |
| 541 | else if (isa<UndefValue>(C)) |
| 542 | C = UndefValue::get(PTy->getElementType()); |
| 543 | else |
| 544 | return 0; |
| 545 | } else { |
| 546 | return 0; |
| 547 | } |
| 548 | } else { |
| 549 | return 0; |
| 550 | } |
| 551 | return C; |
| 552 | } |
| 553 | |
| 554 | |
| 555 | //===----------------------------------------------------------------------===// |
| 556 | // Constant Folding for Calls |
| 557 | // |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 558 | |
| 559 | /// canConstantFoldCallTo - Return true if its even possible to fold a call to |
| 560 | /// the specified function. |
| 561 | bool |
Dan Gohman | fa9b80e | 2008-01-31 01:05:10 +0000 | [diff] [blame] | 562 | llvm::canConstantFoldCallTo(const Function *F) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 563 | switch (F->getIntrinsicID()) { |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 564 | case Intrinsic::sqrt: |
| 565 | case Intrinsic::powi: |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 566 | case Intrinsic::bswap: |
| 567 | case Intrinsic::ctpop: |
| 568 | case Intrinsic::ctlz: |
| 569 | case Intrinsic::cttz: |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 570 | return true; |
| 571 | default: break; |
| 572 | } |
| 573 | |
Chris Lattner | 6f532a9 | 2009-04-03 00:02:39 +0000 | [diff] [blame^] | 574 | if (!F->hasName()) return false; |
| 575 | const char *Str = F->getNameStart(); |
| 576 | unsigned Len = F->getNameLen(); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 577 | |
| 578 | // In these cases, the check of the length is required. We don't want to |
| 579 | // return true for a name like "cos\0blah" which strcmp would return equal to |
| 580 | // "cos", but has length 8. |
| 581 | switch (Str[0]) { |
| 582 | default: return false; |
| 583 | case 'a': |
| 584 | if (Len == 4) |
| 585 | return !strcmp(Str, "acos") || !strcmp(Str, "asin") || |
| 586 | !strcmp(Str, "atan"); |
| 587 | else if (Len == 5) |
| 588 | return !strcmp(Str, "atan2"); |
| 589 | return false; |
| 590 | case 'c': |
| 591 | if (Len == 3) |
| 592 | return !strcmp(Str, "cos"); |
| 593 | else if (Len == 4) |
| 594 | return !strcmp(Str, "ceil") || !strcmp(Str, "cosf") || |
| 595 | !strcmp(Str, "cosh"); |
| 596 | return false; |
| 597 | case 'e': |
| 598 | if (Len == 3) |
| 599 | return !strcmp(Str, "exp"); |
| 600 | return false; |
| 601 | case 'f': |
| 602 | if (Len == 4) |
| 603 | return !strcmp(Str, "fabs") || !strcmp(Str, "fmod"); |
| 604 | else if (Len == 5) |
| 605 | return !strcmp(Str, "floor"); |
| 606 | return false; |
| 607 | break; |
| 608 | case 'l': |
| 609 | if (Len == 3 && !strcmp(Str, "log")) |
| 610 | return true; |
| 611 | if (Len == 5 && !strcmp(Str, "log10")) |
| 612 | return true; |
| 613 | return false; |
| 614 | case 'p': |
| 615 | if (Len == 3 && !strcmp(Str, "pow")) |
| 616 | return true; |
| 617 | return false; |
| 618 | case 's': |
| 619 | if (Len == 3) |
| 620 | return !strcmp(Str, "sin"); |
| 621 | if (Len == 4) |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 622 | return !strcmp(Str, "sinh") || !strcmp(Str, "sqrt") || |
| 623 | !strcmp(Str, "sinf"); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 624 | if (Len == 5) |
| 625 | return !strcmp(Str, "sqrtf"); |
| 626 | return false; |
| 627 | case 't': |
| 628 | if (Len == 3 && !strcmp(Str, "tan")) |
| 629 | return true; |
| 630 | else if (Len == 4 && !strcmp(Str, "tanh")) |
| 631 | return true; |
| 632 | return false; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 636 | static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, |
| 637 | const Type *Ty) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 638 | errno = 0; |
| 639 | V = NativeFP(V); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 640 | if (errno != 0) { |
| 641 | errno = 0; |
| 642 | return 0; |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 643 | } |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 644 | |
| 645 | if (Ty == Type::FloatTy) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 646 | return ConstantFP::get(APFloat((float)V)); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 647 | if (Ty == Type::DoubleTy) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 648 | return ConstantFP::get(APFloat(V)); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 649 | assert(0 && "Can only constant fold float/double"); |
Gabor Greif | 33e456d | 2008-05-21 14:07:30 +0000 | [diff] [blame] | 650 | return 0; // dummy return to suppress warning |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 653 | static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), |
| 654 | double V, double W, |
| 655 | const Type *Ty) { |
| 656 | errno = 0; |
| 657 | V = NativeFP(V, W); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 658 | if (errno != 0) { |
| 659 | errno = 0; |
| 660 | return 0; |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 661 | } |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 662 | |
| 663 | if (Ty == Type::FloatTy) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 664 | return ConstantFP::get(APFloat((float)V)); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 665 | if (Ty == Type::DoubleTy) |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 666 | return ConstantFP::get(APFloat(V)); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 667 | assert(0 && "Can only constant fold float/double"); |
Gabor Greif | 33e456d | 2008-05-21 14:07:30 +0000 | [diff] [blame] | 668 | return 0; // dummy return to suppress warning |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 669 | } |
| 670 | |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 671 | /// ConstantFoldCall - Attempt to constant fold a call to the specified function |
| 672 | /// with the specified arguments, returning null if unsuccessful. |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 673 | |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 674 | Constant * |
Chris Lattner | f286f6f | 2007-12-10 22:53:04 +0000 | [diff] [blame] | 675 | llvm::ConstantFoldCall(Function *F, |
| 676 | Constant* const* Operands, unsigned NumOperands) { |
Chris Lattner | 6f532a9 | 2009-04-03 00:02:39 +0000 | [diff] [blame^] | 677 | if (!F->hasName()) return 0; |
| 678 | const char *Str = F->getNameStart(); |
| 679 | unsigned Len = F->getNameLen(); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 680 | |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 681 | const Type *Ty = F->getReturnType(); |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 682 | if (NumOperands == 1) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 683 | if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 684 | if (Ty!=Type::FloatTy && Ty!=Type::DoubleTy) |
| 685 | return 0; |
| 686 | /// Currently APFloat versions of these functions do not exist, so we use |
| 687 | /// the host native double versions. Float versions are not called |
| 688 | /// directly but for all these it is true (float)(f((double)arg)) == |
| 689 | /// f(arg). Long double not supported yet. |
| 690 | double V = Ty==Type::FloatTy ? (double)Op->getValueAPF().convertToFloat(): |
| 691 | Op->getValueAPF().convertToDouble(); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 692 | switch (Str[0]) { |
| 693 | case 'a': |
| 694 | if (Len == 4 && !strcmp(Str, "acos")) |
| 695 | return ConstantFoldFP(acos, V, Ty); |
| 696 | else if (Len == 4 && !strcmp(Str, "asin")) |
| 697 | return ConstantFoldFP(asin, V, Ty); |
| 698 | else if (Len == 4 && !strcmp(Str, "atan")) |
| 699 | return ConstantFoldFP(atan, V, Ty); |
| 700 | break; |
| 701 | case 'c': |
| 702 | if (Len == 4 && !strcmp(Str, "ceil")) |
| 703 | return ConstantFoldFP(ceil, V, Ty); |
| 704 | else if (Len == 3 && !strcmp(Str, "cos")) |
| 705 | return ConstantFoldFP(cos, V, Ty); |
| 706 | else if (Len == 4 && !strcmp(Str, "cosh")) |
| 707 | return ConstantFoldFP(cosh, V, Ty); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 708 | else if (Len == 4 && !strcmp(Str, "cosf")) |
| 709 | return ConstantFoldFP(cos, V, Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 710 | break; |
| 711 | case 'e': |
| 712 | if (Len == 3 && !strcmp(Str, "exp")) |
| 713 | return ConstantFoldFP(exp, V, Ty); |
| 714 | break; |
| 715 | case 'f': |
| 716 | if (Len == 4 && !strcmp(Str, "fabs")) |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 717 | return ConstantFoldFP(fabs, V, Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 718 | else if (Len == 5 && !strcmp(Str, "floor")) |
| 719 | return ConstantFoldFP(floor, V, Ty); |
| 720 | break; |
| 721 | case 'l': |
| 722 | if (Len == 3 && !strcmp(Str, "log") && V > 0) |
| 723 | return ConstantFoldFP(log, V, Ty); |
| 724 | else if (Len == 5 && !strcmp(Str, "log10") && V > 0) |
| 725 | return ConstantFoldFP(log10, V, Ty); |
| 726 | else if (!strcmp(Str, "llvm.sqrt.f32") || |
| 727 | !strcmp(Str, "llvm.sqrt.f64")) { |
| 728 | if (V >= -0.0) |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 729 | return ConstantFoldFP(sqrt, V, Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 730 | else // Undefined |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 731 | return Constant::getNullValue(Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 732 | } |
| 733 | break; |
| 734 | case 's': |
| 735 | if (Len == 3 && !strcmp(Str, "sin")) |
| 736 | return ConstantFoldFP(sin, V, Ty); |
| 737 | else if (Len == 4 && !strcmp(Str, "sinh")) |
| 738 | return ConstantFoldFP(sinh, V, Ty); |
| 739 | else if (Len == 4 && !strcmp(Str, "sqrt") && V >= 0) |
| 740 | return ConstantFoldFP(sqrt, V, Ty); |
| 741 | else if (Len == 5 && !strcmp(Str, "sqrtf") && V >= 0) |
| 742 | return ConstantFoldFP(sqrt, V, Ty); |
Chris Lattner | f19f58a | 2008-03-30 18:02:00 +0000 | [diff] [blame] | 743 | else if (Len == 4 && !strcmp(Str, "sinf")) |
| 744 | return ConstantFoldFP(sin, V, Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 745 | break; |
| 746 | case 't': |
| 747 | if (Len == 3 && !strcmp(Str, "tan")) |
| 748 | return ConstantFoldFP(tan, V, Ty); |
| 749 | else if (Len == 4 && !strcmp(Str, "tanh")) |
| 750 | return ConstantFoldFP(tanh, V, Ty); |
| 751 | break; |
| 752 | default: |
| 753 | break; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 754 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 755 | } else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) { |
Chris Lattner | ecc0274 | 2007-11-23 22:34:59 +0000 | [diff] [blame] | 756 | if (Len > 11 && !memcmp(Str, "llvm.bswap", 10)) |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 757 | return ConstantInt::get(Op->getValue().byteSwap()); |
Chris Lattner | ecc0274 | 2007-11-23 22:34:59 +0000 | [diff] [blame] | 758 | else if (Len > 11 && !memcmp(Str, "llvm.ctpop", 10)) |
| 759 | return ConstantInt::get(Ty, Op->getValue().countPopulation()); |
| 760 | else if (Len > 10 && !memcmp(Str, "llvm.cttz", 9)) |
| 761 | return ConstantInt::get(Ty, Op->getValue().countTrailingZeros()); |
| 762 | else if (Len > 10 && !memcmp(Str, "llvm.ctlz", 9)) |
| 763 | return ConstantInt::get(Ty, Op->getValue().countLeadingZeros()); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 764 | } |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 765 | } else if (NumOperands == 2) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 766 | if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) { |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 767 | if (Ty!=Type::FloatTy && Ty!=Type::DoubleTy) |
| 768 | return 0; |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 769 | double Op1V = Ty==Type::FloatTy ? |
| 770 | (double)Op1->getValueAPF().convertToFloat(): |
| 771 | Op1->getValueAPF().convertToDouble(); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 772 | if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { |
Dale Johannesen | 43421b3 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 773 | double Op2V = Ty==Type::FloatTy ? |
| 774 | (double)Op2->getValueAPF().convertToFloat(): |
| 775 | Op2->getValueAPF().convertToDouble(); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 776 | |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 777 | if (Len == 3 && !strcmp(Str, "pow")) { |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 778 | return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 779 | } else if (Len == 4 && !strcmp(Str, "fmod")) { |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 780 | return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 781 | } else if (Len == 5 && !strcmp(Str, "atan2")) { |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 782 | return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty); |
Chris Lattner | b5282dc | 2007-01-15 06:27:37 +0000 | [diff] [blame] | 783 | } |
| 784 | } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) { |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 785 | if (!strcmp(Str, "llvm.powi.f32")) { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 786 | return ConstantFP::get(APFloat((float)std::pow((float)Op1V, |
| 787 | (int)Op2C->getZExtValue()))); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 788 | } else if (!strcmp(Str, "llvm.powi.f64")) { |
Chris Lattner | 02a260a | 2008-04-20 00:41:09 +0000 | [diff] [blame] | 789 | return ConstantFP::get(APFloat((double)std::pow((double)Op1V, |
| 790 | (int)Op2C->getZExtValue()))); |
Chris Lattner | b5282dc | 2007-01-15 06:27:37 +0000 | [diff] [blame] | 791 | } |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | } |
| 795 | return 0; |
| 796 | } |
| 797 | |