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