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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This family of functions determines the possibility of performing constant |
| 11 | // folding. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Analysis/ConstantFolding.h" |
| 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 19 | #include "llvm/Instructions.h" |
| 20 | #include "llvm/Intrinsics.h" |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringMap.h" |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 24 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
| 25 | #include "llvm/Support/MathExtras.h" |
| 26 | #include <cerrno> |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 27 | #include <cmath> |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Constant Folding internal helper functions |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | /// IsConstantOffsetFromGlobal - If this constant is actually a constant offset |
| 35 | /// from a global, return the global and the constant. Because of |
| 36 | /// constantexprs, this function is recursive. |
| 37 | static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, |
| 38 | int64_t &Offset, const TargetData &TD) { |
| 39 | // Trivial case, constant is the global. |
| 40 | if ((GV = dyn_cast<GlobalValue>(C))) { |
| 41 | Offset = 0; |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | // Otherwise, if this isn't a constant expr, bail out. |
| 46 | ConstantExpr *CE = dyn_cast<ConstantExpr>(C); |
| 47 | if (!CE) return false; |
| 48 | |
| 49 | // Look through ptr->int and ptr->ptr casts. |
| 50 | if (CE->getOpcode() == Instruction::PtrToInt || |
| 51 | CE->getOpcode() == Instruction::BitCast) |
| 52 | return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD); |
| 53 | |
| 54 | // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) |
| 55 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 56 | // Cannot compute this if the element type of the pointer is missing size |
| 57 | // info. |
| 58 | if (!cast<PointerType>(CE->getOperand(0)->getType())->getElementType()->isSized()) |
| 59 | return false; |
| 60 | |
| 61 | // If the base isn't a global+constant, we aren't either. |
| 62 | if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD)) |
| 63 | return false; |
| 64 | |
| 65 | // Otherwise, add any offset that our operands provide. |
| 66 | gep_type_iterator GTI = gep_type_begin(CE); |
| 67 | for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i, ++GTI) { |
| 68 | ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(i)); |
| 69 | if (!CI) return false; // Index isn't a simple constant? |
| 70 | if (CI->getZExtValue() == 0) continue; // Not adding anything. |
| 71 | |
| 72 | if (const StructType *ST = dyn_cast<StructType>(*GTI)) { |
| 73 | // N = N + Offset |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 74 | Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue()); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 75 | } else { |
Jeff Cohen | ca5183d | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 76 | const SequentialType *SQT = cast<SequentialType>(*GTI); |
| 77 | Offset += TD.getTypeSize(SQT->getElementType())*CI->getSExtValue(); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | /// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression. |
| 88 | /// Attempt to symbolically evaluate the result of a binary operator merging |
| 89 | /// these together. If target data info is available, it is provided as TD, |
| 90 | /// otherwise TD is null. |
| 91 | static Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, |
| 92 | Constant *Op1, const TargetData *TD){ |
| 93 | // SROA |
| 94 | |
| 95 | // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. |
| 96 | // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute |
| 97 | // bits. |
| 98 | |
| 99 | |
| 100 | // If the constant expr is something like &A[123] - &A[4].f, fold this into a |
| 101 | // constant. This happens frequently when iterating over a global array. |
| 102 | if (Opc == Instruction::Sub && TD) { |
| 103 | GlobalValue *GV1, *GV2; |
| 104 | int64_t Offs1, Offs2; |
| 105 | |
| 106 | if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD)) |
| 107 | if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) && |
| 108 | GV1 == GV2) { |
| 109 | // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow. |
| 110 | return ConstantInt::get(Op0->getType(), Offs1-Offs2); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // TODO: Fold icmp setne/seteq as well. |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | /// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP |
| 119 | /// constant expression, do so. |
| 120 | static Constant *SymbolicallyEvaluateGEP(Constant** Ops, unsigned NumOps, |
| 121 | const Type *ResultTy, |
| 122 | const TargetData *TD) { |
| 123 | Constant *Ptr = Ops[0]; |
| 124 | if (!cast<PointerType>(Ptr->getType())->getElementType()->isSized()) |
| 125 | return 0; |
| 126 | |
| 127 | if (TD && Ptr->isNullValue()) { |
| 128 | // If this is a constant expr gep that is effectively computing an |
| 129 | // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' |
| 130 | bool isFoldableGEP = true; |
| 131 | for (unsigned i = 1; i != NumOps; ++i) |
| 132 | if (!isa<ConstantInt>(Ops[i])) { |
| 133 | isFoldableGEP = false; |
| 134 | break; |
| 135 | } |
| 136 | if (isFoldableGEP) { |
Chris Lattner | 309f87e | 2007-02-10 20:33:15 +0000 | [diff] [blame] | 137 | uint64_t Offset = TD->getIndexedOffset(Ptr->getType(), |
| 138 | (Value**)Ops+1, NumOps-1); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 139 | Constant *C = ConstantInt::get(TD->getIntPtrType(), Offset); |
| 140 | return ConstantExpr::getIntToPtr(C, ResultTy); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | //===----------------------------------------------------------------------===// |
| 149 | // Constant Folding public APIs |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | |
| 152 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 153 | /// ConstantFoldInstruction - Attempt to constant fold the specified |
| 154 | /// instruction. If successful, the constant result is returned, if not, null |
| 155 | /// is returned. Note that this function can only fail when attempting to fold |
| 156 | /// instructions like loads and stores, which have no constant expression form. |
| 157 | /// |
| 158 | Constant *llvm::ConstantFoldInstruction(Instruction *I, const TargetData *TD) { |
| 159 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 160 | if (PN->getNumIncomingValues() == 0) |
| 161 | return Constant::getNullValue(PN->getType()); |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 163 | Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0)); |
| 164 | if (Result == 0) return 0; |
| 165 | |
| 166 | // Handle PHI nodes specially here... |
| 167 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
| 168 | if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN) |
| 169 | return 0; // Not all the same incoming constants... |
| 170 | |
| 171 | // If we reach here, all incoming values are the same constant. |
| 172 | return Result; |
| 173 | } |
| 174 | |
| 175 | // Scan the operand list, checking to see if they are all constants, if so, |
| 176 | // hand off to ConstantFoldInstOperands. |
| 177 | SmallVector<Constant*, 8> Ops; |
| 178 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 179 | if (Constant *Op = dyn_cast<Constant>(I->getOperand(i))) |
| 180 | Ops.push_back(Op); |
| 181 | else |
| 182 | return 0; // All operands not constant! |
| 183 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 184 | return ConstantFoldInstOperands(I, &Ops[0], Ops.size(), TD); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /// ConstantFoldInstOperands - Attempt to constant fold an instruction with the |
| 188 | /// specified opcode and operands. If successful, the constant result is |
| 189 | /// returned, if not, null is returned. Note that this function can fail when |
| 190 | /// attempting to fold instructions like loads and stores, which have no |
| 191 | /// constant expression form. |
| 192 | /// |
| 193 | Constant *llvm::ConstantFoldInstOperands(const Instruction* I, |
| 194 | Constant** Ops, unsigned NumOps, |
| 195 | const TargetData *TD) { |
| 196 | unsigned Opc = I->getOpcode(); |
| 197 | const Type *DestTy = I->getType(); |
| 198 | |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 199 | // Handle easy binops first. |
| 200 | if (isa<BinaryOperator>(I)) { |
| 201 | if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) |
| 202 | if (Constant *C = SymbolicallyEvaluateBinop(I->getOpcode(), Ops[0], |
| 203 | Ops[1], TD)) |
| 204 | return C; |
| 205 | |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 206 | return ConstantExpr::get(Opc, Ops[0], Ops[1]); |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 207 | } |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 208 | |
| 209 | switch (Opc) { |
| 210 | default: return 0; |
| 211 | case Instruction::Call: |
| 212 | if (Function *F = dyn_cast<Function>(Ops[0])) |
| 213 | if (canConstantFoldCallTo(F)) |
Chris Lattner | ad58eb3 | 2007-01-31 18:04:55 +0000 | [diff] [blame] | 214 | return ConstantFoldCall(F, Ops+1, NumOps-1); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 215 | return 0; |
| 216 | case Instruction::ICmp: |
| 217 | case Instruction::FCmp: |
| 218 | return ConstantExpr::getCompare(cast<CmpInst>(I)->getPredicate(), Ops[0], |
| 219 | Ops[1]); |
Chris Lattner | 001f753 | 2007-08-11 23:49:01 +0000 | [diff] [blame^] | 220 | case Instruction::PtrToInt: |
| 221 | // If the input is a inttoptr, eliminate the pair. This requires knowing |
| 222 | // the width of a pointer, so it can't be done in ConstantExpr::getCast. |
| 223 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { |
| 224 | if (TD && CE->getOpcode() == Instruction::IntToPtr) { |
| 225 | Constant *Input = CE->getOperand(0); |
| 226 | unsigned InWidth = Input->getType()->getPrimitiveSizeInBits(); |
| 227 | Constant *Mask = |
| 228 | ConstantInt::get(APInt::getLowBitsSet(InWidth, |
| 229 | TD->getPointerSizeInBits())); |
| 230 | Input = ConstantExpr::getAnd(Input, Mask); |
| 231 | // Do a zext or trunc to get to the dest size. |
| 232 | return ConstantExpr::getIntegerCast(Input, I->getType(), false); |
| 233 | } |
| 234 | } |
| 235 | // FALL THROUGH. |
| 236 | case Instruction::IntToPtr: |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 237 | case Instruction::Trunc: |
| 238 | case Instruction::ZExt: |
| 239 | case Instruction::SExt: |
| 240 | case Instruction::FPTrunc: |
| 241 | case Instruction::FPExt: |
| 242 | case Instruction::UIToFP: |
| 243 | case Instruction::SIToFP: |
| 244 | case Instruction::FPToUI: |
| 245 | case Instruction::FPToSI: |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 246 | case Instruction::BitCast: |
| 247 | return ConstantExpr::getCast(Opc, Ops[0], DestTy); |
| 248 | case Instruction::Select: |
| 249 | return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); |
| 250 | case Instruction::ExtractElement: |
| 251 | return ConstantExpr::getExtractElement(Ops[0], Ops[1]); |
| 252 | case Instruction::InsertElement: |
| 253 | return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); |
| 254 | case Instruction::ShuffleVector: |
| 255 | return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); |
| 256 | case Instruction::GetElementPtr: |
Chris Lattner | 03dd25c | 2007-01-31 00:51:48 +0000 | [diff] [blame] | 257 | if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, I->getType(), TD)) |
| 258 | return C; |
| 259 | |
Chris Lattner | d917fe5 | 2007-01-31 04:42:05 +0000 | [diff] [blame] | 260 | return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1); |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | /// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a |
| 265 | /// getelementptr constantexpr, return the constant value being addressed by the |
| 266 | /// constant expression, or null if something is funny and we can't decide. |
| 267 | Constant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, |
| 268 | ConstantExpr *CE) { |
| 269 | if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) |
| 270 | return 0; // Do not allow stepping over the value! |
| 271 | |
| 272 | // Loop over all of the operands, tracking down which value we are |
| 273 | // addressing... |
| 274 | gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); |
| 275 | for (++I; I != E; ++I) |
| 276 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
| 277 | ConstantInt *CU = cast<ConstantInt>(I.getOperand()); |
| 278 | assert(CU->getZExtValue() < STy->getNumElements() && |
| 279 | "Struct index out of range!"); |
| 280 | unsigned El = (unsigned)CU->getZExtValue(); |
| 281 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { |
| 282 | C = CS->getOperand(El); |
| 283 | } else if (isa<ConstantAggregateZero>(C)) { |
| 284 | C = Constant::getNullValue(STy->getElementType(El)); |
| 285 | } else if (isa<UndefValue>(C)) { |
| 286 | C = UndefValue::get(STy->getElementType(El)); |
| 287 | } else { |
| 288 | return 0; |
| 289 | } |
| 290 | } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { |
| 291 | if (const ArrayType *ATy = dyn_cast<ArrayType>(*I)) { |
| 292 | if (CI->getZExtValue() >= ATy->getNumElements()) |
| 293 | return 0; |
| 294 | if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) |
| 295 | C = CA->getOperand(CI->getZExtValue()); |
| 296 | else if (isa<ConstantAggregateZero>(C)) |
| 297 | C = Constant::getNullValue(ATy->getElementType()); |
| 298 | else if (isa<UndefValue>(C)) |
| 299 | C = UndefValue::get(ATy->getElementType()); |
| 300 | else |
| 301 | return 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 302 | } else if (const VectorType *PTy = dyn_cast<VectorType>(*I)) { |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 303 | if (CI->getZExtValue() >= PTy->getNumElements()) |
| 304 | return 0; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 305 | if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) |
Chris Lattner | 5520732 | 2007-01-30 23:45:45 +0000 | [diff] [blame] | 306 | C = CP->getOperand(CI->getZExtValue()); |
| 307 | else if (isa<ConstantAggregateZero>(C)) |
| 308 | C = Constant::getNullValue(PTy->getElementType()); |
| 309 | else if (isa<UndefValue>(C)) |
| 310 | C = UndefValue::get(PTy->getElementType()); |
| 311 | else |
| 312 | return 0; |
| 313 | } else { |
| 314 | return 0; |
| 315 | } |
| 316 | } else { |
| 317 | return 0; |
| 318 | } |
| 319 | return C; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | //===----------------------------------------------------------------------===// |
| 324 | // Constant Folding for Calls |
| 325 | // |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 326 | |
| 327 | /// canConstantFoldCallTo - Return true if its even possible to fold a call to |
| 328 | /// the specified function. |
| 329 | bool |
| 330 | llvm::canConstantFoldCallTo(Function *F) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 331 | switch (F->getIntrinsicID()) { |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 332 | case Intrinsic::sqrt_f32: |
| 333 | case Intrinsic::sqrt_f64: |
Chris Lattner | b5282dc | 2007-01-15 06:27:37 +0000 | [diff] [blame] | 334 | case Intrinsic::powi_f32: |
| 335 | case Intrinsic::powi_f64: |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 336 | case Intrinsic::bswap: |
| 337 | case Intrinsic::ctpop: |
| 338 | case Intrinsic::ctlz: |
| 339 | case Intrinsic::cttz: |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 340 | return true; |
| 341 | default: break; |
| 342 | } |
| 343 | |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 344 | const ValueName *NameVal = F->getValueName(); |
Chris Lattner | a099b6c | 2007-08-08 16:07:23 +0000 | [diff] [blame] | 345 | if (NameVal == 0) return false; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 346 | const char *Str = NameVal->getKeyData(); |
| 347 | unsigned Len = NameVal->getKeyLength(); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 348 | |
| 349 | // In these cases, the check of the length is required. We don't want to |
| 350 | // return true for a name like "cos\0blah" which strcmp would return equal to |
| 351 | // "cos", but has length 8. |
| 352 | switch (Str[0]) { |
| 353 | default: return false; |
| 354 | case 'a': |
| 355 | if (Len == 4) |
| 356 | return !strcmp(Str, "acos") || !strcmp(Str, "asin") || |
| 357 | !strcmp(Str, "atan"); |
| 358 | else if (Len == 5) |
| 359 | return !strcmp(Str, "atan2"); |
| 360 | return false; |
| 361 | case 'c': |
| 362 | if (Len == 3) |
| 363 | return !strcmp(Str, "cos"); |
| 364 | else if (Len == 4) |
| 365 | return !strcmp(Str, "ceil") || !strcmp(Str, "cosf") || |
| 366 | !strcmp(Str, "cosh"); |
| 367 | return false; |
| 368 | case 'e': |
| 369 | if (Len == 3) |
| 370 | return !strcmp(Str, "exp"); |
| 371 | return false; |
| 372 | case 'f': |
| 373 | if (Len == 4) |
| 374 | return !strcmp(Str, "fabs") || !strcmp(Str, "fmod"); |
| 375 | else if (Len == 5) |
| 376 | return !strcmp(Str, "floor"); |
| 377 | return false; |
| 378 | break; |
| 379 | case 'l': |
| 380 | if (Len == 3 && !strcmp(Str, "log")) |
| 381 | return true; |
| 382 | if (Len == 5 && !strcmp(Str, "log10")) |
| 383 | return true; |
| 384 | return false; |
| 385 | case 'p': |
| 386 | if (Len == 3 && !strcmp(Str, "pow")) |
| 387 | return true; |
| 388 | return false; |
| 389 | case 's': |
| 390 | if (Len == 3) |
| 391 | return !strcmp(Str, "sin"); |
| 392 | if (Len == 4) |
| 393 | return !strcmp(Str, "sinh") || !strcmp(Str, "sqrt"); |
| 394 | if (Len == 5) |
| 395 | return !strcmp(Str, "sqrtf"); |
| 396 | return false; |
| 397 | case 't': |
| 398 | if (Len == 3 && !strcmp(Str, "tan")) |
| 399 | return true; |
| 400 | else if (Len == 4 && !strcmp(Str, "tanh")) |
| 401 | return true; |
| 402 | return false; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 406 | static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, |
| 407 | const Type *Ty) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 408 | errno = 0; |
| 409 | V = NativeFP(V); |
| 410 | if (errno == 0) |
| 411 | return ConstantFP::get(Ty, V); |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 412 | errno = 0; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 416 | static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), |
| 417 | double V, double W, |
| 418 | const Type *Ty) { |
| 419 | errno = 0; |
| 420 | V = NativeFP(V, W); |
| 421 | if (errno == 0) |
| 422 | return ConstantFP::get(Ty, V); |
| 423 | errno = 0; |
| 424 | return 0; |
| 425 | } |
| 426 | |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 427 | /// ConstantFoldCall - Attempt to constant fold a call to the specified function |
| 428 | /// with the specified arguments, returning null if unsuccessful. |
| 429 | Constant * |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 430 | llvm::ConstantFoldCall(Function *F, Constant** Operands, unsigned NumOperands) { |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 431 | const ValueName *NameVal = F->getValueName(); |
Chris Lattner | a099b6c | 2007-08-08 16:07:23 +0000 | [diff] [blame] | 432 | if (NameVal == 0) return 0; |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 433 | const char *Str = NameVal->getKeyData(); |
| 434 | unsigned Len = NameVal->getKeyLength(); |
| 435 | |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 436 | const Type *Ty = F->getReturnType(); |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 437 | if (NumOperands == 1) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 438 | if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { |
| 439 | double V = Op->getValue(); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 440 | switch (Str[0]) { |
| 441 | case 'a': |
| 442 | if (Len == 4 && !strcmp(Str, "acos")) |
| 443 | return ConstantFoldFP(acos, V, Ty); |
| 444 | else if (Len == 4 && !strcmp(Str, "asin")) |
| 445 | return ConstantFoldFP(asin, V, Ty); |
| 446 | else if (Len == 4 && !strcmp(Str, "atan")) |
| 447 | return ConstantFoldFP(atan, V, Ty); |
| 448 | break; |
| 449 | case 'c': |
| 450 | if (Len == 4 && !strcmp(Str, "ceil")) |
| 451 | return ConstantFoldFP(ceil, V, Ty); |
| 452 | else if (Len == 3 && !strcmp(Str, "cos")) |
| 453 | return ConstantFoldFP(cos, V, Ty); |
| 454 | else if (Len == 4 && !strcmp(Str, "cosh")) |
| 455 | return ConstantFoldFP(cosh, V, Ty); |
| 456 | break; |
| 457 | case 'e': |
| 458 | if (Len == 3 && !strcmp(Str, "exp")) |
| 459 | return ConstantFoldFP(exp, V, Ty); |
| 460 | break; |
| 461 | case 'f': |
| 462 | if (Len == 4 && !strcmp(Str, "fabs")) |
| 463 | return ConstantFP::get(Ty, fabs(V)); |
| 464 | else if (Len == 5 && !strcmp(Str, "floor")) |
| 465 | return ConstantFoldFP(floor, V, Ty); |
| 466 | break; |
| 467 | case 'l': |
| 468 | if (Len == 3 && !strcmp(Str, "log") && V > 0) |
| 469 | return ConstantFoldFP(log, V, Ty); |
| 470 | else if (Len == 5 && !strcmp(Str, "log10") && V > 0) |
| 471 | return ConstantFoldFP(log10, V, Ty); |
| 472 | else if (!strcmp(Str, "llvm.sqrt.f32") || |
| 473 | !strcmp(Str, "llvm.sqrt.f64")) { |
| 474 | if (V >= -0.0) |
| 475 | return ConstantFP::get(Ty, sqrt(V)); |
| 476 | else // Undefined |
| 477 | return ConstantFP::get(Ty, 0.0); |
| 478 | } |
| 479 | break; |
| 480 | case 's': |
| 481 | if (Len == 3 && !strcmp(Str, "sin")) |
| 482 | return ConstantFoldFP(sin, V, Ty); |
| 483 | else if (Len == 4 && !strcmp(Str, "sinh")) |
| 484 | return ConstantFoldFP(sinh, V, Ty); |
| 485 | else if (Len == 4 && !strcmp(Str, "sqrt") && V >= 0) |
| 486 | return ConstantFoldFP(sqrt, V, Ty); |
| 487 | else if (Len == 5 && !strcmp(Str, "sqrtf") && V >= 0) |
| 488 | return ConstantFoldFP(sqrt, V, Ty); |
| 489 | break; |
| 490 | case 't': |
| 491 | if (Len == 3 && !strcmp(Str, "tan")) |
| 492 | return ConstantFoldFP(tan, V, Ty); |
| 493 | else if (Len == 4 && !strcmp(Str, "tanh")) |
| 494 | return ConstantFoldFP(tanh, V, Ty); |
| 495 | break; |
| 496 | default: |
| 497 | break; |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 498 | } |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 499 | } else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) { |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 500 | if (Len > 11 && !memcmp(Str, "llvm.bswap", 10)) { |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 501 | return ConstantInt::get(Op->getValue().byteSwap()); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 502 | } else if (Len > 11 && !memcmp(Str, "llvm.ctpop", 10)) { |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 503 | uint64_t ctpop = Op->getValue().countPopulation(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 504 | return ConstantInt::get(Ty, ctpop); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 505 | } else if (Len > 10 && !memcmp(Str, "llvm.cttz", 9)) { |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 506 | uint64_t cttz = Op->getValue().countTrailingZeros(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 507 | return ConstantInt::get(Ty, cttz); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 508 | } else if (Len > 10 && !memcmp(Str, "llvm.ctlz", 9)) { |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 509 | uint64_t ctlz = Op->getValue().countLeadingZeros(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 510 | return ConstantInt::get(Ty, ctlz); |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 511 | } |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 512 | } |
Chris Lattner | 72d88ae | 2007-01-30 23:15:43 +0000 | [diff] [blame] | 513 | } else if (NumOperands == 2) { |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 514 | if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) { |
| 515 | double Op1V = Op1->getValue(); |
| 516 | if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { |
| 517 | double Op2V = Op2->getValue(); |
| 518 | |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 519 | if (Len == 3 && !strcmp(Str, "pow")) { |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 520 | return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 521 | } else if (Len == 4 && !strcmp(Str, "fmod")) { |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 522 | return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 523 | } else if (Len == 5 && !strcmp(Str, "atan2")) { |
Dan Gohman | 3841524 | 2007-07-16 15:26:22 +0000 | [diff] [blame] | 524 | return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty); |
Chris Lattner | b5282dc | 2007-01-15 06:27:37 +0000 | [diff] [blame] | 525 | } |
| 526 | } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) { |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 527 | if (!strcmp(Str, "llvm.powi.f32")) { |
Chris Lattner | b5282dc | 2007-01-15 06:27:37 +0000 | [diff] [blame] | 528 | return ConstantFP::get(Ty, std::pow((float)Op1V, |
| 529 | (int)Op2C->getZExtValue())); |
Chris Lattner | c5f6a1f | 2007-08-08 06:55:43 +0000 | [diff] [blame] | 530 | } else if (!strcmp(Str, "llvm.powi.f64")) { |
Chris Lattner | b5282dc | 2007-01-15 06:27:37 +0000 | [diff] [blame] | 531 | return ConstantFP::get(Ty, std::pow((double)Op1V, |
| 532 | (int)Op2C->getZExtValue())); |
| 533 | } |
John Criswell | bd9d370 | 2005-10-27 16:00:10 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | } |
| 537 | return 0; |
| 538 | } |
| 539 | |