ConstantFolding.cpp revision cda9706cb74a65d2feb2175613cb0fe46acc5aa2
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===-- ConstantFolding.cpp - Analyze constant folding possibilities ------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source 60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 10e184baeaa112ceac32420f8ca127b8d4d152d109Argyrios Kyrtzidis// This family of functions determines the possibility of performing constant 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// folding. 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 152a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor#include "llvm/Analysis/ConstantFolding.h" 160de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff#include "llvm/Constants.h" 177da97d0f31e1ec16998d3de2cfd2e88fe3736673Douglas Gregor#include "llvm/DerivedTypes.h" 186c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner#include "llvm/Function.h" 19b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis#include "llvm/GlobalVariable.h" 20e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "llvm/Instructions.h" 2199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes#include "llvm/Intrinsics.h" 22d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor#include "llvm/LLVMContext.h" 231b63e4f732dbc73d90abf886b4d21f8e3a165f6dChris Lattner#include "llvm/ADT/SmallVector.h" 24e91593ef084479340582b2ba177b44be50a717b7Daniel Dunbar#include "llvm/ADT/StringMap.h" 2547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor#include "llvm/Target/TargetData.h" 2627f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek#include "llvm/Support/ErrorHandling.h" 275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/GetElementPtrTypeIterator.h" 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/Support/MathExtras.h" 290b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner#include <cerrno> 300b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner#include <cmath> 310b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattnerusing namespace llvm; 320b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner 330b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner//===----------------------------------------------------------------------===// 340b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner// Constant Folding internal helper functions 350b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner//===----------------------------------------------------------------------===// 360b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner 370b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner/// IsConstantOffsetFromGlobal - If this constant is actually a constant offset 38b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// from a global, return the global and the constant. Because of 39b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis/// constantexprs, this function is recursive. 40b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidisstatic bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV, 41b17166c8077cd900cca83a895c43b30ea6660598Argyrios Kyrtzidis int64_t &Offset, const TargetData &TD) { 420b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner // Trivial case, constant is the global. 43d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner if ((GV = dyn_cast<GlobalValue>(C))) { 446c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner Offset = 0; 456c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner return true; 462d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis } 470b2b6e1cb1573bb295c0a65813dc4df8d57f305bChris Lattner 48ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidis // Otherwise, if this isn't a constant expr, bail out. 493708b3df2e86998dca4c006939014ea1174da834Argyrios Kyrtzidis ConstantExpr *CE = dyn_cast<ConstantExpr>(C); 50ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidis if (!CE) return false; 51ef177820100ab583b08fd3056e2a5a52ee4b1629Argyrios Kyrtzidis 522d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis // Look through ptr->int and ptr->ptr casts. 532d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis if (CE->getOpcode() == Instruction::PtrToInt || 543e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff CE->getOpcode() == Instruction::BitCast) 552d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis return IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD); 562d1c5d313cd0c229cc614e74baa4c5756a4b46f4Argyrios Kyrtzidis 57d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek // i32* getelementptr ([5 x i32]* @a, i32 0, i32 5) 58d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek if (CE->getOpcode() == Instruction::GetElementPtr) { 59d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek // Cannot compute this if the element type of the pointer is missing size 60d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek // info. 61ebf27b1831e6c4d7f4bc30e111a4d6340ff690d1Ted Kremenek if (!cast<PointerType>(CE->getOperand(0)->getType()) 623e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff ->getElementType()->isSized()) 63d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek return false; 64d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek 65d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek // If the base isn't a global+constant, we aren't either. 664111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner if (!IsConstantOffsetFromGlobal(CE->getOperand(0), GV, Offset, TD)) 674afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor return false; 683e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff 694111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner // Otherwise, add any offset that our operands provide. 704111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner gep_type_iterator GTI = gep_type_begin(CE); 71b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar for (User::const_op_iterator i = CE->op_begin() + 1, e = CE->op_end(); 72b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar i != e; ++i, ++GTI) { 73b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar ConstantInt *CI = dyn_cast<ConstantInt>(*i); 74b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar if (!CI) return false; // Index isn't a simple constant? 75b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar if (CI->getZExtValue() == 0) continue; // Not adding anything. 76b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar 77b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar if (const StructType *ST = dyn_cast<StructType>(*GTI)) { 78b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar // N = N + Offset 79b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue()); 80b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar } else { 81b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar const SequentialType *SQT = cast<SequentialType>(*GTI); 82b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar Offset += TD.getTypeAllocSize(SQT->getElementType())*CI->getSExtValue(); 83b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar } 84b286a78c8cce4592306dae6abc3656daf6379c77Daniel Dunbar } 859fdf9c6d3530bb85f3166e6460d841e2ff8e1a2cChris Lattner return true; 860ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner } 87a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis 88a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis return false; 89a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis} 904306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian 914306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian 924306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian/// SymbolicallyEvaluateBinop - One of Op0/Op1 is a constant expression. 9364650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor/// Attempt to symbolically evaluate the result of a binary operator merging 9464650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor/// these together. If target data info is available, it is provided as TD, 954306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanian/// otherwise TD is null. 964306d3cb9116605728252e2738df24b9f6ab53c3Fariborz Jahanianstatic Constant *SymbolicallyEvaluateBinop(unsigned Opc, Constant *Op0, 979e151e154780e9cd443336143af1e996d1f387e5Chris Lattner Constant *Op1, const TargetData *TD, 989e151e154780e9cd443336143af1e996d1f387e5Chris Lattner LLVMContext &Context){ 9978d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor // SROA 10078d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor 10178d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor // Fold (and 0xffffffff00000000, (shl x, 32)) -> shl. 10278d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor // Fold (lshr (or X, Y), 32) -> (lshr [X/Y], 32) if one doesn't contribute 10378d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor // bits. 10478d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor 10578d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor 10678d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor // If the constant expr is something like &A[123] - &A[4].f, fold this into a 10778d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor // constant. This happens frequently when iterating over a global array. 1086393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor if (Opc == Instruction::Sub && TD) { 1096393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor GlobalValue *GV1, *GV2; 1106393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor int64_t Offs1, Offs2; 1116393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor 1126393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor if (IsConstantOffsetFromGlobal(Op0, GV1, Offs1, *TD)) 1136393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor if (IsConstantOffsetFromGlobal(Op1, GV2, Offs2, *TD) && 1146393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor GV1 == GV2) { 1156393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor // (&GV+C1) - (&GV+C2) -> C1-C2, pointer arithmetic cannot overflow. 1166393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor return ConstantInt::get(Op0->getType(), Offs1-Offs2); 1176393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor } 1186393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor } 1196393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor 1206393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor return 0; 1216393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor} 1226393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor 1236393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor/// SymbolicallyEvaluateGEP - If we can symbolically evaluate the specified GEP 1246393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor/// constant expression, do so. 1256393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregorstatic Constant *SymbolicallyEvaluateGEP(Constant* const* Ops, unsigned NumOps, 1266393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor const Type *ResultTy, 1276393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor LLVMContext &Context, 1286393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor const TargetData *TD) { 1296393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor Constant *Ptr = Ops[0]; 13064650af7cc4352c6c67b9bd1bf8ef3ce7471b910Douglas Gregor if (!TD || !cast<PointerType>(Ptr->getType())->getElementType()->isSized()) 13173da9e462576faedc2cdf96b37a1c072b404b73dFariborz Jahanian return 0; 13273da9e462576faedc2cdf96b37a1c072b404b73dFariborz Jahanian 133a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis unsigned BitWidth = TD->getTypeSizeInBits(TD->getIntPtrType(Context)); 134a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis APInt BasePtr(BitWidth, 0); 135a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis bool BaseIsInt = true; 13673da9e462576faedc2cdf96b37a1c072b404b73dFariborz Jahanian if (!Ptr->isNullValue()) { 13773da9e462576faedc2cdf96b37a1c072b404b73dFariborz Jahanian // If this is a inttoptr from a constant int, we can fold this as the base, 1389fdf9c6d3530bb85f3166e6460d841e2ff8e1a2cChris Lattner // otherwise we can't. 1390ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) 140a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis if (CE->getOpcode() == Instruction::IntToPtr) 141a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis if (ConstantInt *Base = dyn_cast<ConstantInt>(CE->getOperand(0))) 142a98e58ddb4696a0020fe97439d5295413f9e90b1Chris Lattner BasePtr = Base->getValue(); 143a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis 1442224f84658fb9b3725a31f2680edb64ae73bf705Douglas Gregor if (BasePtr == 0) 145a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis BaseIsInt = false; 146a75e8534f2b7c2480c48f31f301bd00b241c5499Anders Carlsson } 1472224f84658fb9b3725a31f2680edb64ae73bf705Douglas Gregor 148a98e58ddb4696a0020fe97439d5295413f9e90b1Chris Lattner // If this is a constant expr gep that is effectively computing an 149a98e58ddb4696a0020fe97439d5295413f9e90b1Chris Lattner // "offsetof", fold it into 'cast int Size to T*' instead of 'gep 0, 0, 12' 150090276f5e164d491a1bb3f541bafdb394f5e6f04Steve Naroff for (unsigned i = 1; i != NumOps; ++i) 1513e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff if (!isa<ConstantInt>(Ops[i])) 15256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff return 0; 15356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 15444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor APInt Offset = APInt(BitWidth, 155a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis TD->getIndexedOffset(Ptr->getType(), 156a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis (Value**)Ops+1, NumOps-1)); 157a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis // If the base value for this address is a literal integer value, fold the 1588e25d8681822d8094bfeb97b2239363552548171Chris Lattner // getelementptr to the resulting integer value casted to the pointer type. 1598e25d8681822d8094bfeb97b2239363552548171Chris Lattner if (BaseIsInt) { 1606b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor Constant *C = ConstantInt::get(Context, Offset+BasePtr); 1616b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor return ConstantExpr::getIntToPtr(C, ResultTy); 1626b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor } 1636b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor 1646217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek // Otherwise form a regular getelementptr. Recompute the indices so that 1656b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor // we eliminate over-indexing of the notional static type array bounds. 1666b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor // This makes it easy to determine if the getelementptr is "inbounds". 1676b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor // Also, this helps GlobalOpt do SROA on GlobalVariables. 1686b3945f4bc757bdadd3e443180cf32c2cccb52a0Douglas Gregor const Type *Ty = Ptr->getType(); 169a98e58ddb4696a0020fe97439d5295413f9e90b1Chris Lattner SmallVector<Constant*, 32> NewIdxs; 1700ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner do { 1710ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner if (const SequentialType *ATy = dyn_cast<SequentialType>(Ty)) { 172c63e660882ff93841fa234d70ef6757038302b92Chris Lattner // The only pointer indexing we'll do is on the first index of the GEP. 1734afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor if (isa<PointerType>(ATy) && ATy != Ptr->getType()) 1743e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff break; 1756c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner // Determine which element of the array the offset points into. 1766c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner APInt ElemSize(BitWidth, TD->getTypeAllocSize(ATy->getElementType())); 177d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek if (ElemSize == 0) 178d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek return 0; 179d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek APInt NewIdx = Offset.udiv(ElemSize); 180d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek Offset -= NewIdx * ElemSize; 181d1ac17ae7d61a9244ee5e658d6f63b8fa3da3127Ted Kremenek NewIdxs.push_back(ConstantInt::get(TD->getIntPtrType(Context), NewIdx)); 1829fdf9c6d3530bb85f3166e6460d841e2ff8e1a2cChris Lattner Ty = ATy->getElementType(); 1830ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { 1844afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor // Determine which field of the struct the offset points into. The 1853e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff // getZExtValue is at least as safe as the StructLayout API because we 1866c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner // know the offset is within the struct at this point. 1876c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner const StructLayout &SL = *TD->getStructLayout(STy); 1889fdf9c6d3530bb85f3166e6460d841e2ff8e1a2cChris Lattner unsigned ElIdx = SL.getElementContainingOffset(Offset.getZExtValue()); 189741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor NewIdxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), ElIdx)); 1907df7b6bb800e1987951285ea192e4f347e1b603aDouglas Gregor Offset -= APInt(BitWidth, SL.getElementOffset(ElIdx)); 1918e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor Ty = STy->getTypeAtIndex(ElIdx); 1927df7b6bb800e1987951285ea192e4f347e1b603aDouglas Gregor } else { 1937df7b6bb800e1987951285ea192e4f347e1b603aDouglas Gregor // We've reached some non-indexable type. 1946c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner break; 1956c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner } 196df91eca19bd9738abd9a3b84791f39750e27ad36Ted Kremenek } while (Ty != cast<PointerType>(ResultTy)->getElementType()); 197df91eca19bd9738abd9a3b84791f39750e27ad36Ted Kremenek 198df91eca19bd9738abd9a3b84791f39750e27ad36Ted Kremenek // If we haven't used up the entire offset by descending the static 199df91eca19bd9738abd9a3b84791f39750e27ad36Ted Kremenek // type, then the offset is pointing into the middle of an indivisible 20044b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor // member, so we can't simplify it. 20144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor if (Offset != 0) 20244b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor return 0; 2030b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor 20444b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor // If the base is the start of a GlobalVariable and all the array indices 20544b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor // remain in their static bounds, the GEP is inbounds. We can check that 2064afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor // all indices are in bounds by just checking the first index only 2070ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner // because we've just normalized all the indices. 2088e25d8681822d8094bfeb97b2239363552548171Chris Lattner Constant *C = isa<GlobalVariable>(Ptr) && NewIdxs[0]->isNullValue() ? 2093e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff ConstantExpr::getInBoundsGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size()) : 2108e25d8681822d8094bfeb97b2239363552548171Chris Lattner ConstantExpr::getGetElementPtr(Ptr, &NewIdxs[0], NewIdxs.size()); 2118e25d8681822d8094bfeb97b2239363552548171Chris Lattner assert(cast<PointerType>(C->getType())->getElementType() == Ty && 2126c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner "Computed GetElementPtr has unexpected type!"); 2134afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor 2145239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis // If we ended up indexing a member with a type that doesn't match 2155239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis // the type of what the original indices indexed, add a cast. 21647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor if (Ty != cast<PointerType>(ResultTy)->getElementType()) 21747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor C = ConstantExpr::getBitCast(C, ResultTy); 21847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 21947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor return C; 22047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor} 22147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 22247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor/// FoldBitCast - Constant fold bitcast, symbolically evaluating it with 22347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor/// targetdata. Return 0 if unfoldable. 22447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregorstatic Constant *FoldBitCast(Constant *C, const Type *DestTy, 22547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor const TargetData &TD, LLVMContext &Context) { 22647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // If this is a bitcast from constant vector -> vector, fold it. 22747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor if (ConstantVector *CV = dyn_cast<ConstantVector>(C)) { 22847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor if (const VectorType *DestVTy = dyn_cast<VectorType>(DestTy)) { 22947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // If the element types match, VMCore can fold it. 230f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor unsigned NumDstElt = DestVTy->getNumElements(); 231f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor unsigned NumSrcElt = CV->getNumOperands(); 232f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor if (NumDstElt == NumSrcElt) 233e4f2142d00fa5fdb580c4e2413da91882d955381Chris Lattner return 0; 234f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor 235f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor const Type *SrcEltTy = CV->getType()->getElementType(); 236f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor const Type *DstEltTy = DestVTy->getElementType(); 237d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor 238d249e1d1f1498b81314459ceda19d6ff25c278adDouglas Gregor // Otherwise, we're changing the number of elements in a vector, which 239f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor // requires endianness information to do the right thing. For example, 240f3e7ce4bd9837cdab6a096235922865f95467d3dDouglas Gregor // bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) 24147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // folds to (little endian): 24247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // <4 x i32> <i32 0, i32 0, i32 1, i32 0> 24347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // and to (big endian): 24447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // <4 x i32> <i32 0, i32 0, i32 0, i32 1> 24547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 24647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // First thing is first. We only want to think about integer here, so if 24747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // we have something in FP form, recast it as integer. 24847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor if (DstEltTy->isFloatingPoint()) { 24947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // Fold to an vector of integers with same size as our FP type. 25047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor unsigned FPWidth = DstEltTy->getPrimitiveSizeInBits(); 25147b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor const Type *DestIVTy = VectorType::get( 25247b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor IntegerType::get(Context, FPWidth), NumDstElt); 25347b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // Recursively handle this integer conversion, if possible. 25447b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor C = FoldBitCast(C, DestIVTy, TD, Context); 25547b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor if (!C) return 0; 25647b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 25747b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor // Finally, VMCore can handle this now that #elts line up. 25847b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor return ConstantExpr::getBitCast(C, DestTy); 25947b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor } 26047b9a1ca55e61e37f5a368740e29de190345acc6Douglas Gregor 2614afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor // Okay, we know the destination is integer, if the input is FP, convert 2626ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor // it to integer first. 2636ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor if (SrcEltTy->isFloatingPoint()) { 2642a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor unsigned FPWidth = SrcEltTy->getPrimitiveSizeInBits(); 2652a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor const Type *SrcIVTy = VectorType::get( 2662a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor IntegerType::get(Context, FPWidth), NumSrcElt); 2672a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor // Ask VMCore to do the conversion now that #elts line up. 2682a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor C = ConstantExpr::getBitCast(C, SrcIVTy); 2692a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor CV = dyn_cast<ConstantVector>(C); 2702a3009a432bdcec59e6383d7b2b17494d6f91649Douglas Gregor if (!CV) return 0; // If VMCore wasn't able to fold it, bail out. 2716ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor } 2726ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor 2736ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor // Now we know that the input and output vectors are both integer vectors 2746ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor // of the same size, and that their #elements is not the same. Do the 275e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // conversion here, which depends on whether the input or output has 276e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // more elements. 277e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor bool isLittleEndian = TD.isLittleEndian(); 278e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 279e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor SmallVector<Constant*, 32> Result; 280e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (NumDstElt < NumSrcElt) { 281e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Handle: bitcast (<4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>) 282e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Constant *Zero = Constant::getNullValue(DstEltTy); 2830de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff unsigned Ratio = NumSrcElt/NumDstElt; 2840de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff unsigned SrcBitSize = SrcEltTy->getPrimitiveSizeInBits(); 2850de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff unsigned SrcElt = 0; 2860de21fd85d79bccd32f04256f5b3328ab5ed7c95Steve Naroff for (unsigned i = 0; i != NumDstElt; ++i) { 2876ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor // Build each element of the result. 2886ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor Constant *Elt = Zero; 2896ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize*(Ratio-1); 2906ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor for (unsigned j = 0; j != Ratio; ++j) { 2916ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(SrcElt++)); 2926ed40e351a7c1fb3084434f1db19216b79623cf0Douglas Gregor if (!Src) return 0; // Reject constantexpr elements. 293d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor 294d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor // Zero extend the element to the right size. 295d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor Src = ConstantExpr::getZExt(Src, Elt->getType()); 296d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor 297d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor // Shift it to the right place, depending on endianness. 298d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor Src = ConstantExpr::getShl(Src, 299d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor ConstantInt::get(Src->getType(), ShiftAmt)); 300d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; 301d6f7e9dccd0fa8a5a15d7478324c0ae229fc5e1eDouglas Gregor 3024afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor // Mix it in. 303e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson Elt = ConstantExpr::getOr(Elt, Src); 304e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson } 305e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson Result.push_back(Elt); 306e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson } 307e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson } else { 308e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson // Handle: bitcast (<2 x i64> <i64 0, i64 1> to <4 x i32>) 309e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson unsigned Ratio = NumDstElt/NumSrcElt; 310e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson unsigned DstBitSize = DstEltTy->getPrimitiveSizeInBits(); 311e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson 312e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson // Loop over each source value, expanding into multiple results. 313e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson for (unsigned i = 0; i != NumSrcElt; ++i) { 314e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson Constant *Src = dyn_cast<ConstantInt>(CV->getOperand(i)); 315e136e0e1b74760d7ec3ede38e0e739d5c52a3c0aAnders Carlsson if (!Src) return 0; // Reject constantexpr elements. 3165239304ff761b8b03eefb772bd5d830a9b9f1aeaArgyrios Kyrtzidis 317a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize*(Ratio-1); 318a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis for (unsigned j = 0; j != Ratio; ++j) { 319a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis // Shift the piece of the value into the right place, depending on 320a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis // endianness. 321a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis Constant *Elt = ConstantExpr::getLShr(Src, 322a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis ConstantInt::get(Src->getType(), ShiftAmt)); 323a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; 324a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis 325a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis // Truncate and remember this piece. 326a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis Result.push_back(ConstantExpr::getTrunc(Elt, DstEltTy)); 32799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes } 32899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes } 32999f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes } 3304afa39deaa245592977136d367251ee2c173dd8dDouglas Gregor 331a1d5662d96465f0fddf8819d245da4d19b892effArgyrios Kyrtzidis return ConstantVector::get(Result.data(), Result.size()); 332a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis } 333a5d82000f7b173a0a5ce34dc8c09a03f98d9e439Argyrios Kyrtzidis } 33499f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes 33599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes return 0; 33699f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes} 337df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl 33878d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor 339df2d3cf2be8b91e1e21234ff5a3aa4f820e7001aSebastian Redl//===----------------------------------------------------------------------===// 34078d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor// Constant Folding public APIs 34178d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor//===----------------------------------------------------------------------===// 34278d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor 34378d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor 34478d1583d0b36b7d6d8d10234cdc19ab94adf765aDouglas Gregor/// ConstantFoldInstruction - Attempt to constant fold the specified 34599f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes/// instruction. If successful, the constant result is returned, if not, null 3463e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff/// is returned. Note that this function can only fail when attempting to fold 34799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes/// instructions like loads and stores, which have no constant expression form. 34899f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes/// 34999f06ba988922ea721035a89e6d3c66ba100ba8aNuno LopesConstant *llvm::ConstantFoldInstruction(Instruction *I, LLVMContext &Context, 35099f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes const TargetData *TD) { 35199f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes if (PHINode *PN = dyn_cast<PHINode>(I)) { 35255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis if (PN->getNumIncomingValues() == 0) 35355d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis return UndefValue::get(PN->getType()); 35455d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis 35555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0)); 35655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis if (Result == 0) return 0; 35755d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis 3587caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor // Handle PHI nodes specially here... 3597caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) 3607caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN) 3617caa6825f42a0f7e97d6fc06233133c42b218e46Douglas Gregor return 0; // Not all the same incoming constants... 362275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor 363275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor // If we reach here, all incoming values are the same constant. 364275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor return Result; 365275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor } 366b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor 367b6c8c8bd8d362c8a6cdb767415b0d21e62b77eb2Douglas Gregor // Scan the operand list, checking to see if they are all constants, if so, 368275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor // hand off to ConstantFoldInstOperands. 369275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor SmallVector<Constant*, 8> Ops; 370275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i) 371082d936a5b8323ac2c04558d8bca277a647831a3Ted Kremenek if (Constant *Op = dyn_cast<Constant>(*i)) 372c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis Ops.push_back(Op); 373c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis else 374c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis return 0; // All operands not constant! 375275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor 376c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis if (const CmpInst *CI = dyn_cast<CmpInst>(I)) 377c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis return ConstantFoldCompareInstOperands(CI->getPredicate(), 378c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis Ops.data(), Ops.size(), 379c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis Context, TD); 380c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis else 381275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor return ConstantFoldInstOperands(I->getOpcode(), I->getType(), 382275a369f003f25bd22c00c1c0fc0251c7208caf4Douglas Gregor Ops.data(), Ops.size(), Context, TD); 383b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis} 384f23e839e9ddea324c743d26da43fb767f90ca223Argyrios Kyrtzidis 385fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis/// ConstantFoldConstantExpression - Attempt to fold the constant expression 386fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis/// using the specified TargetData. If successful, the constant result is 38799f06ba988922ea721035a89e6d3c66ba100ba8aNuno Lopes/// result is returned, if not, null is returned. 3888a934233d1582b5bde9d270bc0705aa81e471a79Chris LattnerConstant *llvm::ConstantFoldConstantExpression(ConstantExpr *CE, 3898a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner LLVMContext &Context, 3908a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner const TargetData *TD) { 39127f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek SmallVector<Constant*, 8> Ops; 392250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor for (User::op_iterator i = CE->op_begin(), e = CE->op_end(); i != e; ++i) 393250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor Ops.push_back(cast<Constant>(*i)); 394b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek 395b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek if (CE->isCompare()) 396b65cf41707d190d5ce3d48b9e5bd2dc9d7b4a4c0Ted Kremenek return ConstantFoldCompareInstOperands(CE->getPredicate(), 397460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes Ops.data(), Ops.size(), 3983e9704981d7691fdd44913bf1786e8d760d8a627Steve Naroff Context, TD); 399460b0ac80382fa73337d21dd052c1f18b27435d8Nuno Lopes else 40027f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek return ConstantFoldInstOperands(CE->getOpcode(), CE->getType(), 40127f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek Ops.data(), Ops.size(), Context, TD); 40227f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek} 40327f8a28bee33bb0e857cfe1a61c281bbc234b338Ted Kremenek 4046fb0aee4f9dc261bbec72e1283ad8dc0557a6d96Argyrios Kyrtzidis/// ConstantFoldInstOperands - Attempt to constant fold an instruction with the 405c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis/// specified opcode and operands. If successful, the constant result is 406c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis/// returned, if not, null is returned. Note that this function can fail when 407c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis/// attempting to fold instructions like loads and stores, which have no 408c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis/// constant expression form. 409f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor/// 410f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas GregorConstant *llvm::ConstantFoldInstOperands(unsigned Opcode, const Type *DestTy, 411f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor Constant* const* Ops, unsigned NumOps, 412f009795057dc8ca254f5618c80a0a90f07cd44b4Douglas Gregor LLVMContext &Context, 4135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const TargetData *TD) { 4145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Handle easy binops first. 415d3a413d3b8eb39bcee5944bc545d9997c1abe492Sebastian Redl if (Instruction::isBinaryOp(Opcode)) { 416c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis if (isa<ConstantExpr>(Ops[0]) || isa<ConstantExpr>(Ops[1])) 417c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis if (Constant *C = SymbolicallyEvaluateBinop(Opcode, Ops[0], Ops[1], TD, 418c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis Context)) 419250fc9c859fdeed3f200ae911a7e7ea338f38436Douglas Gregor return C; 4207297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor 4217297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor return ConstantExpr::get(Opcode, Ops[0], Ops[1]); 4227297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor } 4237297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor 4247297134f128423fce2e88f92421ed135bded7d4eDouglas Gregor switch (Opcode) { 42555d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis default: return 0; 42655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis case Instruction::Call: 4271a5364e0fa0482d8d477d6f136d52e503bbe13f4Argyrios Kyrtzidis if (Function *F = dyn_cast<Function>(Ops[0])) 42855d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis if (canConstantFoldCallTo(F)) 42955d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis return ConstantFoldCall(F, Ops+1, NumOps-1); 43055d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis return 0; 43107a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall case Instruction::ICmp: 43207a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall case Instruction::FCmp: 43307a5c22bb6fb0674c95205ae189365bf8e1b695eJohn McCall llvm_unreachable("This function is invalid for compares: no predicate specified"); 43404495c859f81e440748a9b86baa2913461652bb0Douglas Gregor case Instruction::PtrToInt: 43504495c859f81e440748a9b86baa2913461652bb0Douglas Gregor // If the input is a inttoptr, eliminate the pair. This requires knowing 43604495c859f81e440748a9b86baa2913461652bb0Douglas Gregor // the width of a pointer, so it can't be done in ConstantExpr::getCast. 4376393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { 4386393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor if (TD && CE->getOpcode() == Instruction::IntToPtr) { 4396393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor Constant *Input = CE->getOperand(0); 4406393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor unsigned InWidth = Input->getType()->getScalarSizeInBits(); 44140b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis if (TD->getPointerSizeInBits() < InWidth) { 4426393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor Constant *Mask = 4436393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor ConstantInt::get(Context, APInt::getLowBitsSet(InWidth, 4446393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor TD->getPointerSizeInBits())); 4456393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor Input = ConstantExpr::getAnd(Input, Mask); 4466393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor } 44768584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor // Do a zext or trunc to get to the dest size. 44840b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis return ConstantExpr::getIntegerCast(Input, DestTy, false); 4496393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor } 4506393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor } 4516393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor return ConstantExpr::getCast(Opcode, Ops[0], DestTy); 4526393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor case Instruction::IntToPtr: 4536393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor // If the input is a ptrtoint, turn the pair into a ptr to ptr bitcast if 4546393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor // the int size is >= the ptr size. This requires knowing the width of a 4556393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor // pointer, so it can't be done in ConstantExpr::getCast. 4566393519272ce727f4d26e71bbefb5de712274d0eDouglas Gregor if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0])) { 4578499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (TD && 4588499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor TD->getPointerSizeInBits() <= 4598499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor CE->getType()->getScalarSizeInBits()) { 4608499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (CE->getOpcode() == Instruction::PtrToInt) { 4618499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor Constant *Input = CE->getOperand(0); 4628499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor Constant *C = FoldBitCast(Input, DestTy, *TD, Context); 4638499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor return C ? C : ConstantExpr::getBitCast(Input, DestTy); 4648499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor } 4658499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor // If there's a constant offset added to the integer value before 4668499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor // it is casted back to a pointer, see if the expression can be 4678499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor // converted into a GEP. 4688499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (CE->getOpcode() == Instruction::Add) 4698499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (ConstantInt *L = dyn_cast<ConstantInt>(CE->getOperand(0))) 4708499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (ConstantExpr *R = dyn_cast<ConstantExpr>(CE->getOperand(1))) 4718499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (R->getOpcode() == Instruction::PtrToInt) 4728499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (GlobalVariable *GV = 4738499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor dyn_cast<GlobalVariable>(R->getOperand(0))) { 4748499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor const PointerType *GVTy = cast<PointerType>(GV->getType()); 4758499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor if (const ArrayType *AT = 4768499f3f5ff8d5f95ece8047780030a3daad1b6faDouglas Gregor dyn_cast<ArrayType>(GVTy->getElementType())) { 4773e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor const Type *ElTy = AT->getElementType(); 4783e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor uint64_t AllocSize = TD->getTypeAllocSize(ElTy); 4793e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor APInt PSA(L->getValue().getBitWidth(), AllocSize); 4803e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (ElTy == cast<PointerType>(DestTy)->getElementType() && 4813e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor L->getValue().urem(PSA) == 0) { 4823e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor APInt ElemIdx = L->getValue().udiv(PSA); 4833e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor if (ElemIdx.ult(APInt(ElemIdx.getBitWidth(), 4843e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor AT->getNumElements()))) { 4853e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor Constant *Index[] = { 4863c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor Constant::getNullValue(CE->getType()), 4873c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor ConstantInt::get(Context, ElemIdx) 4883c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor }; 4893c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return 4903c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor ConstantExpr::getGetElementPtr(GV, &Index[0], 2); 4913c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor } 4923c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor } 4933c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor } 4943c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor } 4953c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor } 4963c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor } 4973c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return ConstantExpr::getCast(Opcode, Ops[0], DestTy); 4989add31798f621f843233dbff8bba103fca64447bDouglas Gregor case Instruction::Trunc: 4999add31798f621f843233dbff8bba103fca64447bDouglas Gregor case Instruction::ZExt: 5009add31798f621f843233dbff8bba103fca64447bDouglas Gregor case Instruction::SExt: 5019add31798f621f843233dbff8bba103fca64447bDouglas Gregor case Instruction::FPTrunc: 5023c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor case Instruction::FPExt: 5033c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor case Instruction::UIToFP: 5043c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor case Instruction::SIToFP: 5053c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor case Instruction::FPToUI: 5063c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor case Instruction::FPToSI: 5073c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return ConstantExpr::getCast(Opcode, Ops[0], DestTy); 5083c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor case Instruction::BitCast: 5093c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor if (TD) 5103c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor if (Constant *C = FoldBitCast(Ops[0], DestTy, *TD, Context)) 5113c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return C; 5123c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return ConstantExpr::getBitCast(Ops[0], DestTy); 51340b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis case Instruction::Select: 5143c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); 5153c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor case Instruction::ExtractElement: 5163c385e5f8d9008fff18597ca302be19fa86e51f6Douglas Gregor return ConstantExpr::getExtractElement(Ops[0], Ops[1]); 5173e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor case Instruction::InsertElement: 5183e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); 5193e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor case Instruction::ShuffleVector: 5203e41d60eb627dc227c770f1c1c87d06909cf05fdDouglas Gregor return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); 5211ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner case Instruction::GetElementPtr: 5222dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner if (Constant *C = SymbolicallyEvaluateGEP(Ops, NumOps, DestTy, Context, TD)) 5231ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner return C; 5241ad9b28e3217c2349a04f3d3bf14f9c73a99afa7Chris Lattner 52511ddb7dc22bb398a6727318729680630bfcefaaeChris Lattner return ConstantExpr::getGetElementPtr(Ops[0], Ops+1, NumOps-1); 52672564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor } 527d3b9065ec7052ec4741783d2fb4130d13c766933Chris Lattner} 52872564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor 52911ddb7dc22bb398a6727318729680630bfcefaaeChris Lattner/// ConstantFoldCompareInstOperands - Attempt to constant fold a compare 5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// instruction (icmp/fcmp) with the specified operands. If it fails, it 5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// returns a constant expression of the specified operands. 532fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek/// 533fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted KremenekConstant *llvm::ConstantFoldCompareInstOperands(unsigned Predicate, 5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Constant*const * Ops, 5352dbd285f5033ca6dea25babfd1c43d9fec35e7e5Chris Lattner unsigned NumOps, 5365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer LLVMContext &Context, 5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const TargetData *TD) { 5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // fold: icmp (inttoptr x), null -> icmp x, 0 539c0ac4923f08b25ae973a8ee7942cf3eb89da57b7Steve Naroff // fold: icmp (ptrtoint x), 0 -> icmp x, null 540fc767615bc67d3a7587b1fb2e0494c32c9dbd7a5Ted Kremenek // fold: icmp (inttoptr x), (inttoptr y) -> icmp trunc/zext x, trunc/zext y 5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // fold: icmp (ptrtoint x), (ptrtoint y) -> icmp x, y 54255d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis // 54396888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis // ConstantExpr::getCompare cannot do this, because it doesn't have TD 54496888cc2515e55c9b5dd6798063bf4be2c22983aArgyrios Kyrtzidis // around to know if bit truncation is happening. 545cb5f8f59322c352f51714c3de5d8047e70895165Argyrios Kyrtzidis if (ConstantExpr *CE0 = dyn_cast<ConstantExpr>(Ops[0])) { 54655d608cbadf1e9c05064f9287c057d50b7df65b4Argyrios Kyrtzidis if (TD && Ops[1]->isNullValue()) { 5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const Type *IntPtrTy = TD->getIntPtrType(Context); 5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CE0->getOpcode() == Instruction::IntToPtr) { 5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Convert the integer value to the right size to ensure we get the 5508123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner // proper extension or truncation. 5518123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner Constant *C = ConstantExpr::getIntegerCast(CE0->getOperand(0), 5528123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner IntPtrTy, false); 5539e979557eea3875c9e3d100c68188233dd7f46c0Chris Lattner Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; 5548123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, 5558123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner Context, TD); 5568123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner } 557ae0b4e7be78cf0dc2a6a333e865c2be9265774f9Anders Carlsson 5588123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner // Only do this transformation if the int is intptrty in size, otherwise 5598123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner // there is a truncation or extension that we aren't modeling. 5608123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner if (CE0->getOpcode() == Instruction::PtrToInt && 5618123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner CE0->getType() == IntPtrTy) { 5628123a95c33b792d35c2e4992ba6e27882748fb0dChris Lattner Constant *C = CE0->getOperand(0); 56368584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor Constant *NewOps[] = { C, Constant::getNullValue(C->getType()) }; 56440b598eea1310ec9ed554d56ce3e25b34c585458Argyrios Kyrtzidis // FIXME! 5659f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, 5669f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor Context, TD); 567c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis } 568c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis } 5699f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor 5709f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(Ops[1])) { 5719f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor if (TD && CE0->getOpcode() == CE1->getOpcode()) { 5729f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor const Type *IntPtrTy = TD->getIntPtrType(Context); 5739f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor 57468584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor if (CE0->getOpcode() == Instruction::IntToPtr) { 57568584ed35ad819a1668e3f527ba7f5dd4ae6a333Douglas Gregor // Convert the integer value to the right size to ensure we get the 5769f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor // proper extension or truncation. 5779f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor Constant *C0 = ConstantExpr::getIntegerCast(CE0->getOperand(0), 578c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis IntPtrTy, false); 579c37929c9e0dba89770dc5f0fbcfa0c9046da0b06Argyrios Kyrtzidis Constant *C1 = ConstantExpr::getIntegerCast(CE1->getOperand(0), 5809f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor IntPtrTy, false); 5819f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor Constant *NewOps[] = { C0, C1 }; 5829f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, 5839f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor Context, TD); 5849f9bf258f8ebae30bfb70feb9d797d6eb67b0460Douglas Gregor } 585127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 586127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor // Only do this transformation if the int is intptrty in size, otherwise 5871e4bc099882626059f14d687ed7a1a5518b7f3c2Argyrios Kyrtzidis // there is a truncation or extension that we aren't modeling. 588f23e839e9ddea324c743d26da43fb767f90ca223Argyrios Kyrtzidis if ((CE0->getOpcode() == Instruction::PtrToInt && 589127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor CE0->getType() == IntPtrTy && 590127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor CE0->getOperand(0)->getType() == CE1->getOperand(0)->getType())) { 591127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor Constant *NewOps[] = { 592127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor CE0->getOperand(0), CE1->getOperand(0) 593127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor }; 594127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor return ConstantFoldCompareInstOperands(Predicate, NewOps, 2, 595127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor Context, TD); 596127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor } 597b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis } 598f23e839e9ddea324c743d26da43fb767f90ca223Argyrios Kyrtzidis } 599fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis } 600fc7e2a8fbb08f0f496ac6cea0721fe72db8ce240Argyrios Kyrtzidis return ConstantExpr::getCompare(Predicate, Ops[0], Ops[1]); 6011cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor} 6021cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor 6031cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor 604e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor/// ConstantFoldLoadThroughGEPConstantExpr - Given a constant and a 605e94ca9e4371c022329270436b3dd77adc4ddfa8fDouglas Gregor/// getelementptr constantexpr, return the constant value being addressed by the 6061cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor/// constant expression, or null if something is funny and we can't decide. 6071cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas GregorConstant *llvm::ConstantFoldLoadThroughGEPConstantExpr(Constant *C, 6081cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor ConstantExpr *CE, 6091cd1b1e987f5e2f060d7972b13d83239b36d77d6Douglas Gregor LLVMContext &Context) { 61016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (CE->getOperand(1) != Constant::getNullValue(CE->getOperand(1)->getType())) 61116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return 0; // Do not allow stepping over the value! 61216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor 61316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor // Loop over all of the operands, tracking down which value we are 6141fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor // addressing... 61516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); 61616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor for (++I; I != E; ++I) 61716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (const StructType *STy = dyn_cast<StructType>(*I)) { 61816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor ConstantInt *CU = cast<ConstantInt>(I.getOperand()); 61916e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor assert(CU->getZExtValue() < STy->getNumElements() && 62016e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor "Struct index out of range!"); 62116e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor unsigned El = (unsigned)CU->getZExtValue(); 62216e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (ConstantStruct *CS = dyn_cast<ConstantStruct>(C)) { 62316e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor C = CS->getOperand(El); 62416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor } else if (isa<ConstantAggregateZero>(C)) { 62516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor C = Constant::getNullValue(STy->getElementType(El)); 62616e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor } else if (isa<UndefValue>(C)) { 62716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor C = UndefValue::get(STy->getElementType(El)); 62816e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor } else { 6291637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor return 0; 6301637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor } 6311637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor } else if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand())) { 632127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (const ArrayType *ATy = dyn_cast<ArrayType>(*I)) { 633127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (CI->getZExtValue() >= ATy->getNumElements()) 63416e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor return 0; 63516e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor if (ConstantArray *CA = dyn_cast<ConstantArray>(C)) 6361637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor C = CA->getOperand(CI->getZExtValue()); 63716e8be2ac532358d4e413fdfa2643b1876edda78Douglas Gregor else if (isa<ConstantAggregateZero>(C)) 6381637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor C = Constant::getNullValue(ATy->getElementType()); 639127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor else if (isa<UndefValue>(C)) 6401fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor C = UndefValue::get(ATy->getElementType()); 6411fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor else 6421637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor return 0; 6431637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor } else if (const VectorType *PTy = dyn_cast<VectorType>(*I)) { 644127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (CI->getZExtValue() >= PTy->getNumElements()) 645127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor return 0; 646127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) 647127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor C = CP->getOperand(CI->getZExtValue()); 6481637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor else if (isa<ConstantAggregateZero>(C)) 6491637be727f2a0434c1ed7aa385ea1c18328b0ccdDouglas Gregor C = Constant::getNullValue(PTy->getElementType()); 6501fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor else if (isa<UndefValue>(C)) 6511fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor C = UndefValue::get(PTy->getElementType()); 6521fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor else 6531fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return 0; 6541fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor } else { 6551fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return 0; 6561fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor } 6571fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor } else { 6581fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return 0; 6591fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor } 6601fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor return C; 6611fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor} 6621fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor 6631fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor 6641fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor//===----------------------------------------------------------------------===// 6651fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor// Constant Folding for Calls 6661fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor// 6671fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor 6681fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor/// canConstantFoldCallTo - Return true if its even possible to fold a call to 6691fd2dd145d9bcdf0b8d60a88e1795b6ae83656f5Douglas Gregor/// the specified function. 6708a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattnerbool 671bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregorllvm::canConstantFoldCallTo(const Function *F) { 6724b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek switch (F->getIntrinsicID()) { 6734b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek case Intrinsic::sqrt: 674f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis case Intrinsic::powi: 675f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis case Intrinsic::bswap: 676741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor case Intrinsic::ctpop: 677f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis case Intrinsic::ctlz: 678f602c8b6ce1a269c0bf8b3f049e923f4ea5c18e2Argyrios Kyrtzidis case Intrinsic::cttz: 679b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis return true; 6808e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor default: break; 681b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis } 682b57a4fe73b8227c0dba651818b8495dfca61e530Argyrios Kyrtzidis 6830b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor if (!F->hasName()) return false; 6848e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor StringRef Name = F->getName(); 6858e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor 6868e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor // In these cases, the check of the length is required. We don't want to 6878e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor // return true for a name like "cos\0blah" which strcmp would return equal to 6880b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor // "cos", but has length 8. 6890b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor switch (Name[0]) { 6900b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor default: return false; 6910b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor case 'a': 6928e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return Name == "acos" || Name == "asin" || 6938e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor Name == "atan" || Name == "atan2"; 6948e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor case 'c': 6958e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return Name == "cos" || Name == "ceil" || Name == "cosf" || Name == "cosh"; 6968e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor case 'e': 6970b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor return Name == "exp"; 6980b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor case 'f': 6994b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek return Name == "fabs" || Name == "fmod" || Name == "floor"; 7008e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor case 'l': 7018e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return Name == "log" || Name == "log10"; 7028e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor case 'p': 7038e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return Name == "pow"; 7048e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor case 's': 7058e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return Name == "sin" || Name == "sinh" || Name == "sqrt" || 7068e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor Name == "sinf" || Name == "sqrtf"; 7078e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor case 't': 7088e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return Name == "tan" || Name == "tanh"; 7094b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek } 7104b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek} 7114b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek 7128a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattnerstatic Constant *ConstantFoldFP(double (*NativeFP)(double), double V, 7138a934233d1582b5bde9d270bc0705aa81e471a79Chris Lattner const Type *Ty, LLVMContext &Context) { 7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer errno = 0; 71535bc0821c4f80041724cd4c5c4889b2581546a41Argyrios Kyrtzidis V = NativeFP(V); 7168e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor if (errno != 0) { 7178e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor errno = 0; 7188e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregor return 0; 7196359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek } 720bcbffc46f1ad3796c4582fa1e3a9113b5aa26061Douglas Gregor 721082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian if (Ty == Type::getFloatTy(Context)) 7226359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek return ConstantFP::get(Context, APFloat((float)V)); 7236359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek if (Ty == Type::getDoubleTy(Context)) 7246359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek return ConstantFP::get(Context, APFloat(V)); 7256359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek llvm_unreachable("Can only constant fold float/double"); 7264b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek return 0; // dummy return to suppress warning 727741dd9a7e1d63e4e385b657e4ce11c5d96d44f72Douglas Gregor} 728df042e6c2bf06b2d9ed53c52469599ac1bd93a3fTed Kremenek 7298e9e9ef5348bce1a8f0741a5684fac3de9701c28Douglas Gregorstatic Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double), 7304b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek double V, double W, 7314b7c98378ae0c1a3635f0b7756848b4a9923f8bcTed Kremenek const Type *Ty, 7326359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek LLVMContext &Context) { 7336359792ca92e7ca2f416cb804c6604358174e994Ted Kremenek errno = 0; 734997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis V = NativeFP(V, W); 735997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis if (errno != 0) { 736997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis errno = 0; 737997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis return 0; 738997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis } 739997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis 740997b6c6d73541f010afc81e28191c8eae7b24f77Argyrios Kyrtzidis if (Ty == Type::getFloatTy(Context)) 741c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor return ConstantFP::get(Context, APFloat((float)V)); 742c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor if (Ty == Type::getDoubleTy(Context)) 743c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor return ConstantFP::get(Context, APFloat(V)); 744c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor llvm_unreachable("Can only constant fold float/double"); 745c9b5b4074bd73d4af76e69cccf8ecd365fdd1008Douglas Gregor return 0; // dummy return to suppress warning 74644b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor} 74744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor 74844b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor/// ConstantFoldCall - Attempt to constant fold a call to the specified function 7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// with the specified arguments, returning null if unsuccessful. 7500b7a158d120ac8d78c114a823e17eedfec6b6658Douglas Gregor 7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerConstant * 7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerllvm::ConstantFoldCall(Function *F, 75356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff Constant* const* Operands, unsigned NumOperands) { 75456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff if (!F->hasName()) return 0; 75556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff LLVMContext &Context = F->getContext(); 75656ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff StringRef Name = F->getName(); 75756ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff 75856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff const Type *Ty = F->getReturnType(); 75956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff if (NumOperands == 1) { 76056ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { 76156ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff if (Ty!=Type::getFloatTy(F->getContext()) && 76256ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff Ty!=Type::getDoubleTy(Context)) 76356ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff return 0; 76456ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff /// Currently APFloat versions of these functions do not exist, so we use 76556ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff /// the host native double versions. Float versions are not called 766879d27ad4670716c7cea7f86274f6096f6868fe1Ted Kremenek /// directly but for all these it is true (float)(f((double)arg)) == 767879d27ad4670716c7cea7f86274f6096f6868fe1Ted Kremenek /// f(arg). Long double not supported yet. 76856ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff double V = Ty==Type::getFloatTy(F->getContext()) ? 76956ee6896f2efebffb4a2cce5a7610cdf1eddbbbeSteve Naroff (double)Op->getValueAPF().convertToFloat(): 770e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff Op->getValueAPF().convertToDouble(); 771e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff switch (Name[0]) { 772e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff case 'a': 773e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff if (Name == "acos") 774e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff return ConstantFoldFP(acos, V, Ty, Context); 775e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff else if (Name == "asin") 776e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff return ConstantFoldFP(asin, V, Ty, Context); 777e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff else if (Name == "atan") 778e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff return ConstantFoldFP(atan, V, Ty, Context); 779e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff break; 780e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff case 'c': 781e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff if (Name == "ceil") 782e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff return ConstantFoldFP(ceil, V, Ty, Context); 783e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff else if (Name == "cos") 784e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff return ConstantFoldFP(cos, V, Ty, Context); 785e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff else if (Name == "cosh") 786e78b809bbcd92928a63da81f2cd843faad3e4dfdSteve Naroff return ConstantFoldFP(cosh, V, Ty, Context); 787 else if (Name == "cosf") 788 return ConstantFoldFP(cos, V, Ty, Context); 789 break; 790 case 'e': 791 if (Name == "exp") 792 return ConstantFoldFP(exp, V, Ty, Context); 793 break; 794 case 'f': 795 if (Name == "fabs") 796 return ConstantFoldFP(fabs, V, Ty, Context); 797 else if (Name == "floor") 798 return ConstantFoldFP(floor, V, Ty, Context); 799 break; 800 case 'l': 801 if (Name == "log" && V > 0) 802 return ConstantFoldFP(log, V, Ty, Context); 803 else if (Name == "log10" && V > 0) 804 return ConstantFoldFP(log10, V, Ty, Context); 805 else if (Name == "llvm.sqrt.f32" || 806 Name == "llvm.sqrt.f64") { 807 if (V >= -0.0) 808 return ConstantFoldFP(sqrt, V, Ty, Context); 809 else // Undefined 810 return Constant::getNullValue(Ty); 811 } 812 break; 813 case 's': 814 if (Name == "sin") 815 return ConstantFoldFP(sin, V, Ty, Context); 816 else if (Name == "sinh") 817 return ConstantFoldFP(sinh, V, Ty, Context); 818 else if (Name == "sqrt" && V >= 0) 819 return ConstantFoldFP(sqrt, V, Ty, Context); 820 else if (Name == "sqrtf" && V >= 0) 821 return ConstantFoldFP(sqrt, V, Ty, Context); 822 else if (Name == "sinf") 823 return ConstantFoldFP(sin, V, Ty, Context); 824 break; 825 case 't': 826 if (Name == "tan") 827 return ConstantFoldFP(tan, V, Ty, Context); 828 else if (Name == "tanh") 829 return ConstantFoldFP(tanh, V, Ty, Context); 830 break; 831 default: 832 break; 833 } 834 } else if (ConstantInt *Op = dyn_cast<ConstantInt>(Operands[0])) { 835 if (Name.startswith("llvm.bswap")) 836 return ConstantInt::get(Context, Op->getValue().byteSwap()); 837 else if (Name.startswith("llvm.ctpop")) 838 return ConstantInt::get(Ty, Op->getValue().countPopulation()); 839 else if (Name.startswith("llvm.cttz")) 840 return ConstantInt::get(Ty, Op->getValue().countTrailingZeros()); 841 else if (Name.startswith("llvm.ctlz")) 842 return ConstantInt::get(Ty, Op->getValue().countLeadingZeros()); 843 } 844 } else if (NumOperands == 2) { 845 if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) { 846 if (Ty!=Type::getFloatTy(F->getContext()) && 847 Ty!=Type::getDoubleTy(Context)) 848 return 0; 849 double Op1V = Ty==Type::getFloatTy(F->getContext()) ? 850 (double)Op1->getValueAPF().convertToFloat(): 851 Op1->getValueAPF().convertToDouble(); 852 if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { 853 double Op2V = Ty==Type::getFloatTy(F->getContext()) ? 854 (double)Op2->getValueAPF().convertToFloat(): 855 Op2->getValueAPF().convertToDouble(); 856 857 if (Name == "pow") { 858 return ConstantFoldBinaryFP(pow, Op1V, Op2V, Ty, Context); 859 } else if (Name == "fmod") { 860 return ConstantFoldBinaryFP(fmod, Op1V, Op2V, Ty, Context); 861 } else if (Name == "atan2") { 862 return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty, Context); 863 } 864 } else if (ConstantInt *Op2C = dyn_cast<ConstantInt>(Operands[1])) { 865 if (Name == "llvm.powi.f32") { 866 return ConstantFP::get(Context, APFloat((float)std::pow((float)Op1V, 867 (int)Op2C->getZExtValue()))); 868 } else if (Name == "llvm.powi.f64") { 869 return ConstantFP::get(Context, APFloat((double)std::pow((double)Op1V, 870 (int)Op2C->getZExtValue()))); 871 } 872 } 873 } 874 } 875 return 0; 876} 877 878