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