InstCombineCalls.cpp revision 607a7ab3da72a2eb53553a520507cbb8068dd1d8
1551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer//===- InstCombineCalls.cpp -----------------------------------------------===// 27a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell// 3420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola// The LLVM Compiler Infrastructure 4420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola// 5420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola// This file is distributed under the University of Illinois Open Source 6420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola// License. See LICENSE.TXT for details. 7420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola// 8420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola//===----------------------------------------------------------------------===// 9420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola// 10420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola// This file implements the visitCall and visitInvoke functions. 11420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola// 12420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola//===----------------------------------------------------------------------===// 13420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola 14420d23c3d6c0fd0a5ec8f03acbbd222a377339daRafael Espindola#include "InstCombine.h" 157f9ec910a38f769dc91f1b3b3a9b50ee1cecff19Rafael Espindola#include "llvm/IntrinsicInst.h" 167f9ec910a38f769dc91f1b3b3a9b50ee1cecff19Rafael Espindola#include "llvm/Support/CallSite.h" 177f9ec910a38f769dc91f1b3b3a9b50ee1cecff19Rafael Espindola#include "llvm/Target/TargetData.h" 1865c5d75bc8900c61707f54f4057040fac4a8d58aReid Spencer#include "llvm/Analysis/MemoryBuiltins.h" 1965c5d75bc8900c61707f54f4057040fac4a8d58aReid Spencer#include "llvm/Transforms/Utils/BuildLibCalls.h" 2065c5d75bc8900c61707f54f4057040fac4a8d58aReid Spencerusing namespace llvm; 21af58cae8cb9c0ce64ae6a40990a4ad097c71e0e4Reid Spencer 22af58cae8cb9c0ce64ae6a40990a4ad097c71e0e4Reid Spencer/// getPromotedType - Return the specified type promoted as it would be to pass 23af58cae8cb9c0ce64ae6a40990a4ad097c71e0e4Reid Spencer/// though a va_arg area. 240a262ba7c3250ef02833fae864459ccc905a2e9bReid Spencerstatic const Type *getPromotedType(const Type *Ty) { 250a262ba7c3250ef02833fae864459ccc905a2e9bReid Spencer if (const IntegerType* ITy = dyn_cast<IntegerType>(Ty)) { 260a262ba7c3250ef02833fae864459ccc905a2e9bReid Spencer if (ITy->getBitWidth() < 32) 278c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return Type::getInt32Ty(Ty->getContext()); 288c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 298c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return Ty; 308c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer} 318c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 328c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer/// EnforceKnownAlignment - If the specified pointer points to an object that 338c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer/// we control, modify the object's alignment to PrefAlign. This isn't 348c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer/// often possible though. If alignment is important, a more reliable approach 358c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer/// is to simply align all global variables and allocation instructions to 368c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer/// their preferred alignment from the beginning. 378c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer/// 388c8af327b83a210aed30634c908bb4b39f41eedbReid Spencerstatic unsigned EnforceKnownAlignment(Value *V, 398c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer unsigned Align, unsigned PrefAlign) { 408c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 418c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer User *U = dyn_cast<User>(V); 428c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (!U) return Align; 438c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 448c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer switch (Operator::getOpcode(U)) { 458c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer default: break; 468c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer case Instruction::BitCast: 478c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); 480c803894985f80e894b36ad4de58ea4c2e906b07Brian Gaeke case Instruction::GetElementPtr: { 490c803894985f80e894b36ad4de58ea4c2e906b07Brian Gaeke // If all indexes are zero, it is just the alignment of the base pointer. 500c803894985f80e894b36ad4de58ea4c2e906b07Brian Gaeke bool AllZeroOperands = true; 518c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer for (User::op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e; ++i) 528c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (!isa<Constant>(*i) || 538c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer !cast<Constant>(*i)->isNullValue()) { 54f4bb9b1fa76564704a4ab6c27d5d72180b493e9aReid Spencer AllZeroOperands = false; 55f4bb9b1fa76564704a4ab6c27d5d72180b493e9aReid Spencer break; 56f4bb9b1fa76564704a4ab6c27d5d72180b493e9aReid Spencer } 5700ad26ff5760ff2d1b24acb18718e63541088923David Greene 5800ad26ff5760ff2d1b24acb18718e63541088923David Greene if (AllZeroOperands) { 5900ad26ff5760ff2d1b24acb18718e63541088923David Greene // Treat this like a bitcast. 608c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return EnforceKnownAlignment(U->getOperand(0), Align, PrefAlign); 618c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 628c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer break; 638c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 648c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 658c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 668c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 678c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // If there is a large requested alignment and we can, bump up the alignment 688c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // of the global. 698c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (!GV->isDeclaration()) { 708c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (GV->getAlignment() >= PrefAlign) 718c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer Align = GV->getAlignment(); 728c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer else { 738c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer GV->setAlignment(PrefAlign); 748c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer Align = PrefAlign; 758c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 768c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 778c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } else if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) { 78394855a87d52d71b702cfbf0ec1a223bda9163d4Reid Spencer // If there is a requested alignment and if this is an alloca, round up. 79cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke if (AI->getAlignment() >= PrefAlign) 80cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke Align = AI->getAlignment(); 81cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke else { 82cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke AI->setAlignment(PrefAlign); 83cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke Align = PrefAlign; 84cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke } 858c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 868c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 878c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return Align; 88caf0ecec9ed4411280f16b81f43bcca1940a32deReid Spencer} 89caf0ecec9ed4411280f16b81f43bcca1940a32deReid Spencer 90caf0ecec9ed4411280f16b81f43bcca1940a32deReid Spencer/// GetOrEnforceKnownAlignment - If the specified pointer has an alignment that 91ecbd242833cadb8b22006143d5ee4c1152bc81e3Reid Spencer/// we can determine, return it, otherwise return 0. If PrefAlign is specified, 92ecbd242833cadb8b22006143d5ee4c1152bc81e3Reid Spencer/// and it is more than the alignment of the ultimate object, see if we can 93ecbd242833cadb8b22006143d5ee4c1152bc81e3Reid Spencer/// increase the alignment of the ultimate object, making this check succeed. 948c8af327b83a210aed30634c908bb4b39f41eedbReid Spencerunsigned InstCombiner::GetOrEnforceKnownAlignment(Value *V, 958c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer unsigned PrefAlign) { 968c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer unsigned BitWidth = TD ? TD->getTypeSizeInBits(V->getType()) : 978c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer sizeof(PrefAlign) * CHAR_BIT; 988c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer APInt Mask = APInt::getAllOnesValue(BitWidth); 998c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 1008c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer ComputeMaskedBits(V, Mask, KnownZero, KnownOne); 1018c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer unsigned TrailZ = KnownZero.countTrailingOnes(); 1028c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer unsigned Align = 1u << std::min(BitWidth - 1, TrailZ); 1030c803894985f80e894b36ad4de58ea4c2e906b07Brian Gaeke 1040c803894985f80e894b36ad4de58ea4c2e906b07Brian Gaeke if (PrefAlign > Align) 1050c803894985f80e894b36ad4de58ea4c2e906b07Brian Gaeke Align = EnforceKnownAlignment(V, Align, PrefAlign); 1067a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 1077a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // We don't need to make any adjustment. 108d59a64797b12dab9dae1842171b4ca9fba302765Brian Gaeke return Align; 10900ad26ff5760ff2d1b24acb18718e63541088923David Greene} 11000ad26ff5760ff2d1b24acb18718e63541088923David Greene 11100ad26ff5760ff2d1b24acb18718e63541088923David GreeneInstruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) { 112267fddbedd3fe05808a198c147948ffb62ebc866Nick Lewycky unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1)); 113267fddbedd3fe05808a198c147948ffb62ebc866Nick Lewycky unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2)); 114267fddbedd3fe05808a198c147948ffb62ebc866Nick Lewycky unsigned MinAlign = std::min(DstAlign, SrcAlign); 115fb3dcf875bc4262572fee58ac560379879ab8ee2Nick Lewycky unsigned CopyAlign = MI->getAlignment(); 116fb3dcf875bc4262572fee58ac560379879ab8ee2Nick Lewycky 117fb3dcf875bc4262572fee58ac560379879ab8ee2Nick Lewycky if (CopyAlign < MinAlign) { 118fb3dcf875bc4262572fee58ac560379879ab8ee2Nick Lewycky MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), 119fb3dcf875bc4262572fee58ac560379879ab8ee2Nick Lewycky MinAlign, false)); 120fb3dcf875bc4262572fee58ac560379879ab8ee2Nick Lewycky return MI; 121af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer } 1226802b55b6b4b46a2dc965eeeb1f75f1fdc33ba35Brian Gaeke 1237a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with 124f4bb9b1fa76564704a4ab6c27d5d72180b493e9aReid Spencer // load/store. 125f4bb9b1fa76564704a4ab6c27d5d72180b493e9aReid Spencer ConstantInt *MemOpLength = dyn_cast<ConstantInt>(MI->getOperand(3)); 126f4bb9b1fa76564704a4ab6c27d5d72180b493e9aReid Spencer if (MemOpLength == 0) return 0; 12796cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer 12896cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer // Source and destination pointer types are always "i8*" for intrinsic. See 12996cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer // if the size is something we can handle with a single primitive load/store. 1307a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // A single load+store correctly handles overlapping memory in the memmove 1317a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // case. 1327a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell unsigned Size = MemOpLength->getZExtValue(); 1337a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (Size == 0) return MI; // Delete this mem transfer. 1347a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 1357a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (Size > 8 || (Size&(Size-1))) 1369ba8a76f8baaa1092d60ccfbc04e7efdc207c98fAnton Korobeynikov return 0; // If not 1/2/4/8 bytes, exit. 1379ba8a76f8baaa1092d60ccfbc04e7efdc207c98fAnton Korobeynikov 1389ba8a76f8baaa1092d60ccfbc04e7efdc207c98fAnton Korobeynikov // Use an integer load+store unless we can find something better. 1395c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke unsigned SrcAddrSp = 1405c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke cast<PointerType>(MI->getOperand(2)->getType())->getAddressSpace(); 1415c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke unsigned DstAddrSp = 1427a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell cast<PointerType>(MI->getOperand(1)->getType())->getAddressSpace(); 1437a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 1447a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell const IntegerType* IntType = IntegerType::get(MI->getContext(), Size<<3); 1458a2246f32ec6480b6430078ff2c5740a0a11a3fbReid Spencer Type *NewSrcPtrTy = PointerType::get(IntType, SrcAddrSp); 1468a2246f32ec6480b6430078ff2c5740a0a11a3fbReid Spencer Type *NewDstPtrTy = PointerType::get(IntType, DstAddrSp); 1478a2246f32ec6480b6430078ff2c5740a0a11a3fbReid Spencer 148be13028264888b01ac6fcd46667cf31a7e84e9cfReid Spencer // Memcpy forces the use of i8* for the source and destination. That means 149be13028264888b01ac6fcd46667cf31a7e84e9cfReid Spencer // that if you're using memcpy to move one double around, you'll get a cast 150be13028264888b01ac6fcd46667cf31a7e84e9cfReid Spencer // from double* to i8*. We'd much rather use a double load+store rather than 1518c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // an i64 load+store, here because this improves the odds that the source or 1528c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // dest address will be promotable. See if we can find a better type than the 1538c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // integer datatype. 154cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke Value *StrippedDest = MI->getOperand(1)->stripPointerCasts(); 155cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke if (StrippedDest != MI->getOperand(1)) { 156cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke const Type *SrcETy = cast<PointerType>(StrippedDest->getType()) 1577a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell ->getElementType(); 1587a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (TD && SrcETy->isSized() && TD->getTypeStoreSize(SrcETy) == Size) { 1597a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // The SrcETy might be something like {{{double}}} or [1 x double]. Rip 16002cef96a2855b1b01b81d4b3429ed5cd707dd899Brian Gaeke // down through these levels if so. 16102cef96a2855b1b01b81d4b3429ed5cd707dd899Brian Gaeke while (!SrcETy->isSingleValueType()) { 16202cef96a2855b1b01b81d4b3429ed5cd707dd899Brian Gaeke if (const StructType *STy = dyn_cast<StructType>(SrcETy)) { 163af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer if (STy->getNumElements() == 1) 164c64d41e7063df8598e2eb6d741994292e3baa7cbBrian Gaeke SrcETy = STy->getElementType(0); 165c64d41e7063df8598e2eb6d741994292e3baa7cbBrian Gaeke else 166af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer break; 167c64d41e7063df8598e2eb6d741994292e3baa7cbBrian Gaeke } else if (const ArrayType *ATy = dyn_cast<ArrayType>(SrcETy)) { 168c64d41e7063df8598e2eb6d741994292e3baa7cbBrian Gaeke if (ATy->getNumElements() == 1) 169af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer SrcETy = ATy->getElementType(); 1705c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke else 1715c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke break; 172af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer } else 1735c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke break; 1745c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke } 1758c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 1768c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (SrcETy->isSingleValueType()) { 1778c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer NewSrcPtrTy = PointerType::get(SrcETy, SrcAddrSp); 178484fc8e38421f5b26593a9edacd734baf844c9e9Reid Spencer NewDstPtrTy = PointerType::get(SrcETy, DstAddrSp); 179484fc8e38421f5b26593a9edacd734baf844c9e9Reid Spencer } 180484fc8e38421f5b26593a9edacd734baf844c9e9Reid Spencer } 1813484a99ba1ecddd221c9c77b321d66edb456a7fcReid Spencer } 1823484a99ba1ecddd221c9c77b321d66edb456a7fcReid Spencer 1833484a99ba1ecddd221c9c77b321d66edb456a7fcReid Spencer 184484fc8e38421f5b26593a9edacd734baf844c9e9Reid Spencer // If the memcpy/memmove provides better alignment info than we can 185484fc8e38421f5b26593a9edacd734baf844c9e9Reid Spencer // infer, use it. 186484fc8e38421f5b26593a9edacd734baf844c9e9Reid Spencer SrcAlign = std::max(SrcAlign, CopyAlign); 1875da60469f9a9513c28c9d715d818f7d789972e90Edward O'Callaghan DstAlign = std::max(DstAlign, CopyAlign); 1885da60469f9a9513c28c9d715d818f7d789972e90Edward O'Callaghan 1895da60469f9a9513c28c9d715d818f7d789972e90Edward O'Callaghan Value *Src = Builder->CreateBitCast(MI->getOperand(2), NewSrcPtrTy); 1908cd4c3e6534a14566bf163301fd45bca34e655c1Anton Korobeynikov Value *Dest = Builder->CreateBitCast(MI->getOperand(1), NewDstPtrTy); 1918cd4c3e6534a14566bf163301fd45bca34e655c1Anton Korobeynikov Instruction *L = new LoadInst(Src, "tmp", MI->isVolatile(), SrcAlign); 1928cd4c3e6534a14566bf163301fd45bca34e655c1Anton Korobeynikov InsertNewInstBefore(L, *MI); 1937a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell InsertNewInstBefore(new StoreInst(L, Dest, MI->isVolatile(), DstAlign), 1947a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell *MI); 1957a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 196731c6abc469863ca6b8259319991bdc0455fc8b0Nick Lewycky // Set the size of the copy to 0, it will be deleted on the next iteration. 197731c6abc469863ca6b8259319991bdc0455fc8b0Nick Lewycky MI->setOperand(3, Constant::getNullValue(MemOpLength->getType())); 198731c6abc469863ca6b8259319991bdc0455fc8b0Nick Lewycky return MI; 199cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke} 200cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke 201cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian GaekeInstruction *InstCombiner::SimplifyMemSet(MemSetInst *MI) { 202ff336a4e7f9a71834bb60a540a14dd023e7cbcfdBrian Gaeke unsigned Alignment = GetOrEnforceKnownAlignment(MI->getDest()); 203ff336a4e7f9a71834bb60a540a14dd023e7cbcfdBrian Gaeke if (MI->getAlignment() < Alignment) { 204ff336a4e7f9a71834bb60a540a14dd023e7cbcfdBrian Gaeke MI->setAlignment(ConstantInt::get(MI->getAlignmentType(), 205ff336a4e7f9a71834bb60a540a14dd023e7cbcfdBrian Gaeke Alignment, false)); 206cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer return MI; 207cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer } 208cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer 209a6d990a73f5ce41c6ce53d940f807621ecb4d260Reid Spencer // Extract the length and alignment and fill if they are constant. 210a6d990a73f5ce41c6ce53d940f807621ecb4d260Reid Spencer ConstantInt *LenC = dyn_cast<ConstantInt>(MI->getLength()); 211a6d990a73f5ce41c6ce53d940f807621ecb4d260Reid Spencer ConstantInt *FillC = dyn_cast<ConstantInt>(MI->getValue()); 2128c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (!LenC || !FillC || !FillC->getType()->isIntegerTy(8)) 2138c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return 0; 2148c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer uint64_t Len = LenC->getZExtValue(); 215cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke Alignment = MI->getAlignment(); 216cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke 217cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke // If the length is zero, this is a no-op 2187a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (Len == 0) return MI; // memset(d,c,0,a) -> noop 2197a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 2207a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // memset(s,c,n) -> store s, c (for n=1,2,4,8) 2210b14259eb2b2663ca9ee02ad8919b27e2723d330Chris Lattner if (Len <= 8 && isPowerOf2_32((uint32_t)Len)) { 2220b14259eb2b2663ca9ee02ad8919b27e2723d330Chris Lattner const Type *ITy = IntegerType::get(MI->getContext(), Len*8); // n=1 -> i8. 2230b14259eb2b2663ca9ee02ad8919b27e2723d330Chris Lattner 224368c36ff32b93bf8e29fb7a4ff2c3a10f3aec164Chris Lattner Value *Dest = MI->getDest(); 225368c36ff32b93bf8e29fb7a4ff2c3a10f3aec164Chris Lattner Dest = Builder->CreateBitCast(Dest, PointerType::getUnqual(ITy)); 226368c36ff32b93bf8e29fb7a4ff2c3a10f3aec164Chris Lattner 2278c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // Alignment 0 is identity for alignment 1 for memset, but not store. 2288c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (Alignment == 0) Alignment = 1; 2298c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 2308c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // Extract the fill value and store. 2318c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer uint64_t Fill = FillC->getZExtValue()*0x0101010101010101ULL; 2328c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer InsertNewInstBefore(new StoreInst(ConstantInt::get(ITy, Fill), 2337a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell Dest, false, Alignment), *MI); 2347a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 2357a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // Set the size of the copy to 0, it will be deleted on the next iteration. 236f9960f769ae574705b4dff4d6253285705a7012aReid Spencer MI->setLength(Constant::getNullValue(LenC->getType())); 237f9960f769ae574705b4dff4d6253285705a7012aReid Spencer return MI; 238f9960f769ae574705b4dff4d6253285705a7012aReid Spencer } 2395c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke 2405c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke return 0; 2415c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke} 242f9960f769ae574705b4dff4d6253285705a7012aReid Spencer 243f9960f769ae574705b4dff4d6253285705a7012aReid Spencer/// visitCallInst - CallInst simplification. This mostly only handles folding 244f9960f769ae574705b4dff4d6253285705a7012aReid Spencer/// of intrinsic instructions. For normal calls, it allows visitCallSite to do 2457a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell/// the heavy lifting. 2467a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell/// 2477a73b80b9052136c8cd2234eb3433a07df7cf38eJohn CriswellInstruction *InstCombiner::visitCallInst(CallInst &CI) { 248cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke if (isFreeCall(&CI)) 249cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke return visitFree(CI); 250cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke 251cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke // If the caller function is nounwind, mark the call as nounwind, even if the 252cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke // callee isn't. 253cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke if (CI.getParent()->getParent()->doesNotThrow() && 254cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke !CI.doesNotThrow()) { 2558c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer CI.setDoesNotThrow(); 2568c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return &CI; 2578c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 258df3be82dcbf61bd4ad9e13e93dfbf37a7a46ee51Reid Spencer 259df3be82dcbf61bd4ad9e13e93dfbf37a7a46ee51Reid Spencer IntrinsicInst *II = dyn_cast<IntrinsicInst>(&CI); 260df3be82dcbf61bd4ad9e13e93dfbf37a7a46ee51Reid Spencer if (!II) return visitCallSite(&CI); 26100ad26ff5760ff2d1b24acb18718e63541088923David Greene 26200ad26ff5760ff2d1b24acb18718e63541088923David Greene // Intrinsics cannot occur in an invoke, so handle them here instead of in 26300ad26ff5760ff2d1b24acb18718e63541088923David Greene // visitCallSite. 2648c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(II)) { 2658c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer bool Changed = false; 2668c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 267e429182c013128ecc1cc2ccc6fd457d311003947Eric Christopher // memmove/cpy/set of zero bytes is a noop. 268e429182c013128ecc1cc2ccc6fd457d311003947Eric Christopher if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) { 269e429182c013128ecc1cc2ccc6fd457d311003947Eric Christopher if (NumBytes->isNullValue()) return EraseInstFromFunction(CI); 27096cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer 27196cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes)) 27296cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer if (CI->getZExtValue() == 1) { 2738c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // Replace the instruction with just byte operations. We would 2748c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // transform other cases to loads/stores, but we don't know if 2758c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // alignment is sufficient. 2769058349aa431ef4cbdea51d30302f88105f61b76Brian Gaeke } 2779058349aa431ef4cbdea51d30302f88105f61b76Brian Gaeke } 2789058349aa431ef4cbdea51d30302f88105f61b76Brian Gaeke 27927fcfe1364943dadd99fd0ef5af6793f58acc446Owen Anderson // If we have a memmove and the source operation is a constant global, 28027fcfe1364943dadd99fd0ef5af6793f58acc446Owen Anderson // then the source and dest pointers can't alias, so we can change this 28127fcfe1364943dadd99fd0ef5af6793f58acc446Owen Anderson // into a call to memcpy. 282f8e9f7c70af05e40a65f75b49073b48941257839Reid Spencer if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI)) { 283f8e9f7c70af05e40a65f75b49073b48941257839Reid Spencer if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource())) 284f8e9f7c70af05e40a65f75b49073b48941257839Reid Spencer if (GVSrc->isConstant()) { 2854046846d2cb36a6281b853c73776584063cca19dJohn Criswell Module *M = CI.getParent()->getParent()->getParent(); 2864046846d2cb36a6281b853c73776584063cca19dJohn Criswell Intrinsic::ID MemCpyID = Intrinsic::memcpy; 2874046846d2cb36a6281b853c73776584063cca19dJohn Criswell const Type *Tys[3] = { CI.getOperand(1)->getType(), 288de8aed2808224f0651400b8efee35830b83020a5Owen Anderson CI.getOperand(2)->getType(), 289de8aed2808224f0651400b8efee35830b83020a5Owen Anderson CI.getOperand(3)->getType() }; 290de8aed2808224f0651400b8efee35830b83020a5Owen Anderson CI.setOperand(0, 291af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer Intrinsic::getDeclaration(M, MemCpyID, Tys, 3)); 292af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer Changed = true; 293af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer } 2948c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 2958c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 2968c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { 297f9960f769ae574705b4dff4d6253285705a7012aReid Spencer // memmove(x,x,size) -> noop. 298f9960f769ae574705b4dff4d6253285705a7012aReid Spencer if (MTI->getSource() == MTI->getDest()) 299f9960f769ae574705b4dff4d6253285705a7012aReid Spencer return EraseInstFromFunction(CI); 3008c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 3018c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 3028c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // If we can determine a pointer alignment that is bigger than currently 303df3be82dcbf61bd4ad9e13e93dfbf37a7a46ee51Reid Spencer // set, update the alignment. 304df3be82dcbf61bd4ad9e13e93dfbf37a7a46ee51Reid Spencer if (isa<MemTransferInst>(MI)) { 3058085cff7ebbee3efd932b8f27f67187a4ba707e1Reid Spencer if (Instruction *I = SimplifyMemTransfer(MI)) 30696cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer return I; 30796cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(MI)) { 30896cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer if (Instruction *I = SimplifyMemSet(MSI)) 309df3be82dcbf61bd4ad9e13e93dfbf37a7a46ee51Reid Spencer return I; 310df3be82dcbf61bd4ad9e13e93dfbf37a7a46ee51Reid Spencer } 3118085cff7ebbee3efd932b8f27f67187a4ba707e1Reid Spencer 3129d9c19cf9672d4a19237b9bf7c22cbb0c5809b07Reid Spencer if (Changed) return II; 3139d9c19cf9672d4a19237b9bf7c22cbb0c5809b07Reid Spencer } 3149d9c19cf9672d4a19237b9bf7c22cbb0c5809b07Reid Spencer 315b7a8d400be7ce9e275c6e09a2a90fbacd0566476Jeffrey Yasskin switch (II->getIntrinsicID()) { 316b7a8d400be7ce9e275c6e09a2a90fbacd0566476Jeffrey Yasskin default: break; 317b7a8d400be7ce9e275c6e09a2a90fbacd0566476Jeffrey Yasskin case Intrinsic::objectsize: { 318cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer // We need target data for just about everything so depend on it. 319cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer if (!TD) break; 320cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer 321cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer const Type *ReturnTy = CI.getType(); 322cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer bool Min = (cast<ConstantInt>(II->getOperand(2))->getZExtValue() == 1); 323cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer 32459473af39565dcff7416426b8aaed3ad63476c81Reid Spencer // Get to the real allocated thing and offset as fast as possible. 32559473af39565dcff7416426b8aaed3ad63476c81Reid Spencer Value *Op1 = II->getOperand(1)->stripPointerCasts(); 32659473af39565dcff7416426b8aaed3ad63476c81Reid Spencer 3278c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // If we've stripped down to a single global variable that we 3288c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // can know the size of then just return that. 3298c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op1)) { 330cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer if (GV->hasDefinitiveInitializer()) { 331cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer Constant *C = GV->getInitializer(); 332cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer uint64_t GlobalSize = TD->getTypeAllocSize(C->getType()); 33359473af39565dcff7416426b8aaed3ad63476c81Reid Spencer return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, GlobalSize)); 33459473af39565dcff7416426b8aaed3ad63476c81Reid Spencer } else { 33559473af39565dcff7416426b8aaed3ad63476c81Reid Spencer // Can't determine size of the GV. 336cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); 337cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer return ReplaceInstUsesWith(CI, RetVal); 338cdb08a3691b0949ea9fea690571631e1a842bf3aReid Spencer } 3397a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell } else if (AllocaInst *AI = dyn_cast<AllocaInst>(Op1)) { 3407a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // Get alloca size. 3417a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (AI->getAllocatedType()->isSized()) { 3428c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer uint64_t AllocaSize = TD->getTypeAllocSize(AI->getAllocatedType()); 3438c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (AI->isArrayAllocation()) { 3448c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer const ConstantInt *C = dyn_cast<ConstantInt>(AI->getArraySize()); 3457a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (!C) break; 3467a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell AllocaSize *= C->getZExtValue(); 3477a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell } 348af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, AllocaSize)); 349c64d41e7063df8598e2eb6d741994292e3baa7cbBrian Gaeke } 350c64d41e7063df8598e2eb6d741994292e3baa7cbBrian Gaeke } else if (CallInst *MI = extractMallocCall(Op1)) { 351af362fc492bf0bfc6e43e3025621834ea031a29eReid Spencer const Type* MallocType = getMallocAllocatedType(MI); 3525c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke // Get alloca size. 3535c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke if (MallocType && MallocType->isSized()) { 3548c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (Value *NElems = getMallocArraySize(MI, TD, true)) { 3558c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (ConstantInt *NElements = dyn_cast<ConstantInt>(NElems)) 3568c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return ReplaceInstUsesWith(CI, ConstantInt::get(ReturnTy, 3578c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer (NElements->getZExtValue() * TD->getTypeAllocSize(MallocType)))); 3588c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 3598c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 3607a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op1)) { 3617a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // Only handle constant GEPs here. 3627a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (CE->getOpcode() != Instruction::GetElementPtr) break; 3637931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer GEPOperator *GEP = cast<GEPOperator>(CE); 3647931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer 3657931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer // Make sure we're not a constant offset from an external 3667931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer // global. 3677931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer Value *Operand = GEP->getPointerOperand(); 3687931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer Operand = Operand->stripPointerCasts(); 369342a343a570a8e6c80ce11ecb9df7bc65c315975Jeffrey Yasskin if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Operand)) 370342a343a570a8e6c80ce11ecb9df7bc65c315975Jeffrey Yasskin if (!GV->hasDefinitiveInitializer()) break; 371342a343a570a8e6c80ce11ecb9df7bc65c315975Jeffrey Yasskin 3727a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // Get what we're pointing to and its size. 3737a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell const PointerType *BaseType = 3747a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell cast<PointerType>(Operand->getType()); 3757a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell uint64_t Size = TD->getTypeAllocSize(BaseType->getElementType()); 3767a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 3777a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // Get the current byte offset into the thing. Use the original 3788c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // operand in case we're looking through a bitcast. 3798c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer SmallVector<Value*, 8> Ops(CE->op_begin()+1, CE->op_end()); 3808c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer const PointerType *OffsetType = 38196cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer cast<PointerType>(GEP->getPointerOperand()->getType()); 38296cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer uint64_t Offset = TD->getIndexedOffset(OffsetType, &Ops[0], Ops.size()); 38396cf58777bf7d28968054b0dd8cb0624a7433e89Reid Spencer 384cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke if (Size < Offset) { 385cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke // Out of bound reference? Negative index normalized to large 386cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke // index? Just return "I don't know". 387cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL); 388cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke return ReplaceInstUsesWith(CI, RetVal); 389cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke } 39051a33548d46a338a251eb48a937bc2eec758a01cReid Spencer 39151a33548d46a338a251eb48a937bc2eec758a01cReid Spencer Constant *RetVal = ConstantInt::get(ReturnTy, Size-Offset); 39251a33548d46a338a251eb48a937bc2eec758a01cReid Spencer return ReplaceInstUsesWith(CI, RetVal); 3938c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 3948c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 3958c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 3968c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // Do not return "I don't know" here. Later optimization passes could 3978c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // make it possible to evaluate objectsize to a constant. 3988c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer break; 3998c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 40001746745f1287effa1772ef51b973988afcea699Douglas Gregor case Intrinsic::bswap: 40101746745f1287effa1772ef51b973988afcea699Douglas Gregor // bswap(bswap(x)) -> x 40201746745f1287effa1772ef51b973988afcea699Douglas Gregor if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(II->getOperand(1))) 403cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke if (Operand->getIntrinsicID() == Intrinsic::bswap) 404cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke return ReplaceInstUsesWith(CI, Operand->getOperand(1)); 405cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke 4068c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // bswap(trunc(bswap(x))) -> trunc(lshr(x, c)) 4078c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (TruncInst *TI = dyn_cast<TruncInst>(II->getOperand(1))) { 4088c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (IntrinsicInst *Operand = dyn_cast<IntrinsicInst>(TI->getOperand(0))) 4098c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (Operand->getIntrinsicID() == Intrinsic::bswap) { 41059473af39565dcff7416426b8aaed3ad63476c81Reid Spencer unsigned C = Operand->getType()->getPrimitiveSizeInBits() - 41159473af39565dcff7416426b8aaed3ad63476c81Reid Spencer TI->getType()->getPrimitiveSizeInBits(); 41259473af39565dcff7416426b8aaed3ad63476c81Reid Spencer Value *CV = ConstantInt::get(Operand->getType(), C); 413cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke Value *V = Builder->CreateLShr(Operand->getOperand(1), CV); 414cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke return new TruncInst(V, TI->getType()); 415cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke } 4167a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell } 4177a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 4187a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell break; 4197a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell case Intrinsic::powi: 4207a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getOperand(2))) { 4217a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // powi(x, 0) -> 1.0 4227a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (Power->isZero()) 4237a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell return ReplaceInstUsesWith(CI, ConstantFP::get(CI.getType(), 1.0)); 4247a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // powi(x, 1) -> x 4257a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (Power->isOne()) 4267a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell return ReplaceInstUsesWith(CI, II->getOperand(1)); 4277a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // powi(x, -1) -> 1/x 428071d73d67e7cea60e7334f6ae96c1e8f8050a662Douglas Gregor if (Power->isAllOnesValue()) 429071d73d67e7cea60e7334f6ae96c1e8f8050a662Douglas Gregor return BinaryOperator::CreateFDiv(ConstantFP::get(CI.getType(), 1.0), 430071d73d67e7cea60e7334f6ae96c1e8f8050a662Douglas Gregor II->getOperand(1)); 43100ad26ff5760ff2d1b24acb18718e63541088923David Greene } 43200ad26ff5760ff2d1b24acb18718e63541088923David Greene break; 43300ad26ff5760ff2d1b24acb18718e63541088923David Greene case Intrinsic::cttz: { 434cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke // If all bits below the first known one are known zero, 435cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke // this value is constant. 436cbeedf73d403ca0b65b3d4151b5630c5e335055bBrian Gaeke const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType()); 4377a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell uint32_t BitWidth = IT->getBitWidth(); 4387a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell APInt KnownZero(BitWidth, 0); 4397a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell APInt KnownOne(BitWidth, 0); 44059473af39565dcff7416426b8aaed3ad63476c81Reid Spencer ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth), 44159473af39565dcff7416426b8aaed3ad63476c81Reid Spencer KnownZero, KnownOne); 44259473af39565dcff7416426b8aaed3ad63476c81Reid Spencer unsigned TrailingZeros = KnownOne.countTrailingZeros(); 4431f4a27947f5d7840df45ea9e757b88e6269bda5fReid Spencer APInt Mask(APInt::getLowBitsSet(BitWidth, TrailingZeros)); 4441f4a27947f5d7840df45ea9e757b88e6269bda5fReid Spencer if ((Mask & KnownZero) == Mask) 4451f4a27947f5d7840df45ea9e757b88e6269bda5fReid Spencer return ReplaceInstUsesWith(CI, ConstantInt::get(IT, 446f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin APInt(BitWidth, TrailingZeros))); 447f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin 448f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin } 4495c039879649f12c17f6c52c08be9a82d838525f9Brian Gaeke break; 450b2815e0139727b78a701af5fb66676944b859e2dJohn Criswell case Intrinsic::ctlz: { 451b2815e0139727b78a701af5fb66676944b859e2dJohn Criswell // If all bits above the first known one are known zero, 4523b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov // this value is constant. 4533b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType()); 4543b30a6e92d1da79674451879d4112a8f83cc12a4Anton Korobeynikov uint32_t BitWidth = IT->getBitWidth(); 4551cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer APInt KnownZero(BitWidth, 0); 4561cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer APInt KnownOne(BitWidth, 0); 4571cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer ComputeMaskedBits(II->getOperand(1), APInt::getAllOnesValue(BitWidth), 4581cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer KnownZero, KnownOne); 4591cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer unsigned LeadingZeros = KnownOne.countLeadingZeros(); 4601cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer APInt Mask(APInt::getHighBitsSet(BitWidth, LeadingZeros)); 4610abe116022a45879d5f17e6c4964769f24bb05aaGordon Henriksen if ((Mask & KnownZero) == Mask) 4621cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer return ReplaceInstUsesWith(CI, ConstantInt::get(IT, 4631cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer APInt(BitWidth, LeadingZeros))); 4640abe116022a45879d5f17e6c4964769f24bb05aaGordon Henriksen 4650abe116022a45879d5f17e6c4964769f24bb05aaGordon Henriksen } 4660abe116022a45879d5f17e6c4964769f24bb05aaGordon Henriksen break; 467ff22c42e0ee8e526352f3ac84ae9dd0d0bec2519Reid Spencer case Intrinsic::uadd_with_overflow: { 468ff22c42e0ee8e526352f3ac84ae9dd0d0bec2519Reid Spencer Value *LHS = II->getOperand(1), *RHS = II->getOperand(2); 469ff22c42e0ee8e526352f3ac84ae9dd0d0bec2519Reid Spencer const IntegerType *IT = cast<IntegerType>(II->getOperand(1)->getType()); 470790e11cdff59ed6312c2f1d3d8f63537c2ae2ec5Eric Christopher uint32_t BitWidth = IT->getBitWidth(); 471790e11cdff59ed6312c2f1d3d8f63537c2ae2ec5Eric Christopher APInt Mask = APInt::getSignBit(BitWidth); 472790e11cdff59ed6312c2f1d3d8f63537c2ae2ec5Eric Christopher APInt LHSKnownZero(BitWidth, 0); 4731cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer APInt LHSKnownOne(BitWidth, 0); 4741cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer ComputeMaskedBits(LHS, Mask, LHSKnownZero, LHSKnownOne); 4751cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer bool LHSKnownNegative = LHSKnownOne[BitWidth - 1]; 4761cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer bool LHSKnownPositive = LHSKnownZero[BitWidth - 1]; 4771cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer 4781cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer if (LHSKnownNegative || LHSKnownPositive) { 4791cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer APInt RHSKnownZero(BitWidth, 0); 4801cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer APInt RHSKnownOne(BitWidth, 0); 4811cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer ComputeMaskedBits(RHS, Mask, RHSKnownZero, RHSKnownOne); 4821cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer bool RHSKnownNegative = RHSKnownOne[BitWidth - 1]; 4831cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer bool RHSKnownPositive = RHSKnownZero[BitWidth - 1]; 4841cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer if (LHSKnownNegative && RHSKnownNegative) { 485197ca8e939bb4fa3b436685a58957a11e2adb8ffOwen Anderson // The sign bit is set in both cases: this MUST overflow. 486197ca8e939bb4fa3b436685a58957a11e2adb8ffOwen Anderson // Create a simple add instruction, and insert it into the struct. 487197ca8e939bb4fa3b436685a58957a11e2adb8ffOwen Anderson Instruction *Add = BinaryOperator::CreateAdd(LHS, RHS, "", &CI); 4880711c303765d858a0aa75f76b6ad3c90542416d4Douglas Gregor Worklist.Add(Add); 4890711c303765d858a0aa75f76b6ad3c90542416d4Douglas Gregor Constant *V[] = { 4900711c303765d858a0aa75f76b6ad3c90542416d4Douglas Gregor UndefValue::get(LHS->getType()),ConstantInt::getTrue(II->getContext()) 4917b3e851e2bc9014782e3cae379c4f1561380acbfReid Spencer }; 4927b3e851e2bc9014782e3cae379c4f1561380acbfReid Spencer Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 4937b3e851e2bc9014782e3cae379c4f1561380acbfReid Spencer return InsertValueInst::Create(Struct, Add, 0); 4947b3e851e2bc9014782e3cae379c4f1561380acbfReid Spencer } 4957b3e851e2bc9014782e3cae379c4f1561380acbfReid Spencer 4967b3e851e2bc9014782e3cae379c4f1561380acbfReid Spencer if (LHSKnownPositive && RHSKnownPositive) { 49700ad26ff5760ff2d1b24acb18718e63541088923David Greene // The sign bit is clear in both cases: this CANNOT overflow. 49800ad26ff5760ff2d1b24acb18718e63541088923David Greene // Create a simple add instruction, and insert it into the struct. 49900ad26ff5760ff2d1b24acb18718e63541088923David Greene Instruction *Add = BinaryOperator::CreateNUWAdd(LHS, RHS, "", &CI); 500caf0ecec9ed4411280f16b81f43bcca1940a32deReid Spencer Worklist.Add(Add); 501caf0ecec9ed4411280f16b81f43bcca1940a32deReid Spencer Constant *V[] = { 502caf0ecec9ed4411280f16b81f43bcca1940a32deReid Spencer UndefValue::get(LHS->getType()), 503ecbd242833cadb8b22006143d5ee4c1152bc81e3Reid Spencer ConstantInt::getFalse(II->getContext()) 504ecbd242833cadb8b22006143d5ee4c1152bc81e3Reid Spencer }; 505ecbd242833cadb8b22006143d5ee4c1152bc81e3Reid Spencer Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 50600ad26ff5760ff2d1b24acb18718e63541088923David Greene return InsertValueInst::Create(Struct, Add, 0); 50700ad26ff5760ff2d1b24acb18718e63541088923David Greene } 50800ad26ff5760ff2d1b24acb18718e63541088923David Greene } 509c232a658274117b8db1f6dfd1aa2dbb3ab390381Reid Spencer } 510c232a658274117b8db1f6dfd1aa2dbb3ab390381Reid Spencer // FALL THROUGH uadd into sadd 511c232a658274117b8db1f6dfd1aa2dbb3ab390381Reid Spencer case Intrinsic::sadd_with_overflow: 512be13028264888b01ac6fcd46667cf31a7e84e9cfReid Spencer // Canonicalize constants into the RHS. 513be13028264888b01ac6fcd46667cf31a7e84e9cfReid Spencer if (isa<Constant>(II->getOperand(1)) && 514be13028264888b01ac6fcd46667cf31a7e84e9cfReid Spencer !isa<Constant>(II->getOperand(2))) { 51500ad26ff5760ff2d1b24acb18718e63541088923David Greene Value *LHS = II->getOperand(1); 51600ad26ff5760ff2d1b24acb18718e63541088923David Greene II->setOperand(1, II->getOperand(2)); 51700ad26ff5760ff2d1b24acb18718e63541088923David Greene II->setOperand(2, LHS); 51800ad26ff5760ff2d1b24acb18718e63541088923David Greene return II; 51900ad26ff5760ff2d1b24acb18718e63541088923David Greene } 52000ad26ff5760ff2d1b24acb18718e63541088923David Greene 5211cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer // X + undef -> undef 5221cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer if (isa<UndefValue>(II->getOperand(2))) 5231cd3bee548ef0395065d5e789119c7654f239b2cReid Spencer return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); 5248c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer 5258c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) { 5268c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // X + 0 -> {X, false} 5278c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (RHS->isZero()) { 5288c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer Constant *V[] = { 5298c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer UndefValue::get(II->getOperand(0)->getType()), 5308c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer ConstantInt::getFalse(II->getContext()) 5318c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer }; 5328c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 5338c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return InsertValueInst::Create(Struct, II->getOperand(1), 0); 5348c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 5358c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 5368c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer break; 5378c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer case Intrinsic::usub_with_overflow: 5388c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer case Intrinsic::ssub_with_overflow: 5398c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // undef - X -> undef 5408c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // X - undef -> undef 5417931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer if (isa<UndefValue>(II->getOperand(1)) || 5427931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer isa<UndefValue>(II->getOperand(2))) 5437931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); 5447931a7867b602fa159f1939daf37756d9c3b1532Reid Spencer 5458c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (ConstantInt *RHS = dyn_cast<ConstantInt>(II->getOperand(2))) { 5468c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer // X - 0 -> {X, false} 5478c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer if (RHS->isZero()) { 5487a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell Constant *V[] = { 5497a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell UndefValue::get(II->getOperand(1)->getType()), 5507a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell ConstantInt::getFalse(II->getContext()) 5517a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell }; 5527a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 5537a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell return InsertValueInst::Create(Struct, II->getOperand(1), 0); 5547a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell } 5557a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell } 5567a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell break; 5577a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell case Intrinsic::umul_with_overflow: 5587a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell case Intrinsic::smul_with_overflow: 5597a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // Canonicalize constants into the RHS. 5607a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (isa<Constant>(II->getOperand(1)) && 5617a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell !isa<Constant>(II->getOperand(2))) { 5627a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell Value *LHS = II->getOperand(1); 5637a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell II->setOperand(1, II->getOperand(2)); 5647a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell II->setOperand(2, LHS); 5657a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell return II; 56659473af39565dcff7416426b8aaed3ad63476c81Reid Spencer } 56759473af39565dcff7416426b8aaed3ad63476c81Reid Spencer 56859473af39565dcff7416426b8aaed3ad63476c81Reid Spencer // X * undef -> undef 5697a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (isa<UndefValue>(II->getOperand(2))) 5707a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell return ReplaceInstUsesWith(CI, UndefValue::get(II->getType())); 5717a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 5727a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (ConstantInt *RHSI = dyn_cast<ConstantInt>(II->getOperand(2))) { 5737a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // X*0 -> {0, false} 5747a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell if (RHSI->isZero()) 5757a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell return ReplaceInstUsesWith(CI, Constant::getNullValue(II->getType())); 5767a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell 5777a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // X * 1 -> {X, false} 578a93e77073faf8547f8405919339f44cebdc74d11Jeffrey Yasskin if (RHSI->equalsInt(1)) { 579a93e77073faf8547f8405919339f44cebdc74d11Jeffrey Yasskin Constant *V[] = { 580a93e77073faf8547f8405919339f44cebdc74d11Jeffrey Yasskin UndefValue::get(II->getOperand(1)->getType()), 5818cd4c3e6534a14566bf163301fd45bca34e655c1Anton Korobeynikov ConstantInt::getFalse(II->getContext()) 5828cd4c3e6534a14566bf163301fd45bca34e655c1Anton Korobeynikov }; 5838cd4c3e6534a14566bf163301fd45bca34e655c1Anton Korobeynikov Constant *Struct = ConstantStruct::get(II->getContext(), V, 2, false); 5848c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer return InsertValueInst::Create(Struct, II->getOperand(1), 0); 5858c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 5868c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer } 5878c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer break; 5888c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer case Intrinsic::ppc_altivec_lvx: 5898c8af327b83a210aed30634c908bb4b39f41eedbReid Spencer case Intrinsic::ppc_altivec_lvxl: 5907a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell case Intrinsic::x86_sse_loadu_ps: 5917a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell case Intrinsic::x86_sse2_loadu_pd: 5927a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell case Intrinsic::x86_sse2_loadu_dq: 593a773bd54f32ceb55af08286fe00c6ec1b73e5b9aReid Spencer // Turn PPC lvx -> load if the pointer is known aligned. 5947a73b80b9052136c8cd2234eb3433a07df7cf38eJohn Criswell // Turn X86 loadups -> load if the pointer is known aligned. 595 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { 596 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), 597 PointerType::getUnqual(II->getType())); 598 return new LoadInst(Ptr); 599 } 600 break; 601 case Intrinsic::ppc_altivec_stvx: 602 case Intrinsic::ppc_altivec_stvxl: 603 // Turn stvx -> store if the pointer is known aligned. 604 if (GetOrEnforceKnownAlignment(II->getOperand(2), 16) >= 16) { 605 const Type *OpPtrTy = 606 PointerType::getUnqual(II->getOperand(1)->getType()); 607 Value *Ptr = Builder->CreateBitCast(II->getOperand(2), OpPtrTy); 608 return new StoreInst(II->getOperand(1), Ptr); 609 } 610 break; 611 case Intrinsic::x86_sse_storeu_ps: 612 case Intrinsic::x86_sse2_storeu_pd: 613 case Intrinsic::x86_sse2_storeu_dq: 614 // Turn X86 storeu -> store if the pointer is known aligned. 615 if (GetOrEnforceKnownAlignment(II->getOperand(1), 16) >= 16) { 616 const Type *OpPtrTy = 617 PointerType::getUnqual(II->getOperand(2)->getType()); 618 Value *Ptr = Builder->CreateBitCast(II->getOperand(1), OpPtrTy); 619 return new StoreInst(II->getOperand(2), Ptr); 620 } 621 break; 622 623 case Intrinsic::x86_sse_cvttss2si: { 624 // These intrinsics only demands the 0th element of its input vector. If 625 // we can simplify the input based on that, do so now. 626 unsigned VWidth = 627 cast<VectorType>(II->getOperand(1)->getType())->getNumElements(); 628 APInt DemandedElts(VWidth, 1); 629 APInt UndefElts(VWidth, 0); 630 if (Value *V = SimplifyDemandedVectorElts(II->getOperand(1), DemandedElts, 631 UndefElts)) { 632 II->setOperand(1, V); 633 return II; 634 } 635 break; 636 } 637 638 case Intrinsic::ppc_altivec_vperm: 639 // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant. 640 if (ConstantVector *Mask = dyn_cast<ConstantVector>(II->getOperand(3))) { 641 assert(Mask->getNumOperands() == 16 && "Bad type for intrinsic!"); 642 643 // Check that all of the elements are integer constants or undefs. 644 bool AllEltsOk = true; 645 for (unsigned i = 0; i != 16; ++i) { 646 if (!isa<ConstantInt>(Mask->getOperand(i)) && 647 !isa<UndefValue>(Mask->getOperand(i))) { 648 AllEltsOk = false; 649 break; 650 } 651 } 652 653 if (AllEltsOk) { 654 // Cast the input vectors to byte vectors. 655 Value *Op0 = Builder->CreateBitCast(II->getOperand(1), Mask->getType()); 656 Value *Op1 = Builder->CreateBitCast(II->getOperand(2), Mask->getType()); 657 Value *Result = UndefValue::get(Op0->getType()); 658 659 // Only extract each element once. 660 Value *ExtractedElts[32]; 661 memset(ExtractedElts, 0, sizeof(ExtractedElts)); 662 663 for (unsigned i = 0; i != 16; ++i) { 664 if (isa<UndefValue>(Mask->getOperand(i))) 665 continue; 666 unsigned Idx=cast<ConstantInt>(Mask->getOperand(i))->getZExtValue(); 667 Idx &= 31; // Match the hardware behavior. 668 669 if (ExtractedElts[Idx] == 0) { 670 ExtractedElts[Idx] = 671 Builder->CreateExtractElement(Idx < 16 ? Op0 : Op1, 672 ConstantInt::get(Type::getInt32Ty(II->getContext()), 673 Idx&15, false), "tmp"); 674 } 675 676 // Insert this value into the result vector. 677 Result = Builder->CreateInsertElement(Result, ExtractedElts[Idx], 678 ConstantInt::get(Type::getInt32Ty(II->getContext()), 679 i, false), "tmp"); 680 } 681 return CastInst::Create(Instruction::BitCast, Result, CI.getType()); 682 } 683 } 684 break; 685 686 case Intrinsic::stackrestore: { 687 // If the save is right next to the restore, remove the restore. This can 688 // happen when variable allocas are DCE'd. 689 if (IntrinsicInst *SS = dyn_cast<IntrinsicInst>(II->getOperand(1))) { 690 if (SS->getIntrinsicID() == Intrinsic::stacksave) { 691 BasicBlock::iterator BI = SS; 692 if (&*++BI == II) 693 return EraseInstFromFunction(CI); 694 } 695 } 696 697 // Scan down this block to see if there is another stack restore in the 698 // same block without an intervening call/alloca. 699 BasicBlock::iterator BI = II; 700 TerminatorInst *TI = II->getParent()->getTerminator(); 701 bool CannotRemove = false; 702 for (++BI; &*BI != TI; ++BI) { 703 if (isa<AllocaInst>(BI) || isMalloc(BI)) { 704 CannotRemove = true; 705 break; 706 } 707 if (CallInst *BCI = dyn_cast<CallInst>(BI)) { 708 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(BCI)) { 709 // If there is a stackrestore below this one, remove this one. 710 if (II->getIntrinsicID() == Intrinsic::stackrestore) 711 return EraseInstFromFunction(CI); 712 // Otherwise, ignore the intrinsic. 713 } else { 714 // If we found a non-intrinsic call, we can't remove the stack 715 // restore. 716 CannotRemove = true; 717 break; 718 } 719 } 720 } 721 722 // If the stack restore is in a return/unwind block and if there are no 723 // allocas or calls between the restore and the return, nuke the restore. 724 if (!CannotRemove && (isa<ReturnInst>(TI) || isa<UnwindInst>(TI))) 725 return EraseInstFromFunction(CI); 726 break; 727 } 728 } 729 730 return visitCallSite(II); 731} 732 733// InvokeInst simplification 734// 735Instruction *InstCombiner::visitInvokeInst(InvokeInst &II) { 736 return visitCallSite(&II); 737} 738 739/// isSafeToEliminateVarargsCast - If this cast does not affect the value 740/// passed through the varargs area, we can eliminate the use of the cast. 741static bool isSafeToEliminateVarargsCast(const CallSite CS, 742 const CastInst * const CI, 743 const TargetData * const TD, 744 const int ix) { 745 if (!CI->isLosslessCast()) 746 return false; 747 748 // The size of ByVal arguments is derived from the type, so we 749 // can't change to a type with a different size. If the size were 750 // passed explicitly we could avoid this check. 751 if (!CS.paramHasAttr(ix, Attribute::ByVal)) 752 return true; 753 754 const Type* SrcTy = 755 cast<PointerType>(CI->getOperand(0)->getType())->getElementType(); 756 const Type* DstTy = cast<PointerType>(CI->getType())->getElementType(); 757 if (!SrcTy->isSized() || !DstTy->isSized()) 758 return false; 759 if (!TD || TD->getTypeAllocSize(SrcTy) != TD->getTypeAllocSize(DstTy)) 760 return false; 761 return true; 762} 763 764namespace { 765class InstCombineFortifiedLibCalls : public SimplifyFortifiedLibCalls { 766 InstCombiner *IC; 767protected: 768 void replaceCall(Value *With) { 769 NewInstruction = IC->ReplaceInstUsesWith(*CI, With); 770 } 771 bool isFoldable(unsigned SizeCIOp, unsigned SizeArgOp, bool isString) const { 772 if (ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(SizeCIOp))) { 773 if (SizeCI->isAllOnesValue()) 774 return true; 775 if (isString) 776 return SizeCI->getZExtValue() >= 777 GetStringLength(CI->getOperand(SizeArgOp)); 778 if (ConstantInt *Arg = dyn_cast<ConstantInt>(CI->getOperand(SizeArgOp))) 779 return SizeCI->getZExtValue() >= Arg->getZExtValue(); 780 } 781 return false; 782 } 783public: 784 InstCombineFortifiedLibCalls(InstCombiner *IC) : IC(IC), NewInstruction(0) { } 785 Instruction *NewInstruction; 786}; 787} // end anonymous namespace 788 789// Try to fold some different type of calls here. 790// Currently we're only working with the checking functions, memcpy_chk, 791// mempcpy_chk, memmove_chk, memset_chk, strcpy_chk, stpcpy_chk, strncpy_chk, 792// strcat_chk and strncat_chk. 793Instruction *InstCombiner::tryOptimizeCall(CallInst *CI, const TargetData *TD) { 794 if (CI->getCalledFunction() == 0) return 0; 795 796 InstCombineFortifiedLibCalls Simplifier(this); 797 Simplifier.fold(CI, TD); 798 return Simplifier.NewInstruction; 799} 800 801// visitCallSite - Improvements for call and invoke instructions. 802// 803Instruction *InstCombiner::visitCallSite(CallSite CS) { 804 bool Changed = false; 805 806 // If the callee is a constexpr cast of a function, attempt to move the cast 807 // to the arguments of the call/invoke. 808 if (transformConstExprCastCall(CS)) return 0; 809 810 Value *Callee = CS.getCalledValue(); 811 812 if (Function *CalleeF = dyn_cast<Function>(Callee)) 813 // If the call and callee calling conventions don't match, this call must 814 // be unreachable, as the call is undefined. 815 if (CalleeF->getCallingConv() != CS.getCallingConv() && 816 // Only do this for calls to a function with a body. A prototype may 817 // not actually end up matching the implementation's calling conv for a 818 // variety of reasons (e.g. it may be written in assembly). 819 !CalleeF->isDeclaration()) { 820 Instruction *OldCall = CS.getInstruction(); 821 new StoreInst(ConstantInt::getTrue(Callee->getContext()), 822 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), 823 OldCall); 824 // If OldCall dues not return void then replaceAllUsesWith undef. 825 // This allows ValueHandlers and custom metadata to adjust itself. 826 if (!OldCall->getType()->isVoidTy()) 827 OldCall->replaceAllUsesWith(UndefValue::get(OldCall->getType())); 828 if (isa<CallInst>(OldCall)) 829 return EraseInstFromFunction(*OldCall); 830 831 // We cannot remove an invoke, because it would change the CFG, just 832 // change the callee to a null pointer. 833 cast<InvokeInst>(OldCall)->setCalledFunction( 834 Constant::getNullValue(CalleeF->getType())); 835 return 0; 836 } 837 838 if (isa<ConstantPointerNull>(Callee) || isa<UndefValue>(Callee)) { 839 // This instruction is not reachable, just remove it. We insert a store to 840 // undef so that we know that this code is not reachable, despite the fact 841 // that we can't modify the CFG here. 842 new StoreInst(ConstantInt::getTrue(Callee->getContext()), 843 UndefValue::get(Type::getInt1PtrTy(Callee->getContext())), 844 CS.getInstruction()); 845 846 // If CS dues not return void then replaceAllUsesWith undef. 847 // This allows ValueHandlers and custom metadata to adjust itself. 848 if (!CS.getInstruction()->getType()->isVoidTy()) 849 CS.getInstruction()-> 850 replaceAllUsesWith(UndefValue::get(CS.getInstruction()->getType())); 851 852 if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) { 853 // Don't break the CFG, insert a dummy cond branch. 854 BranchInst::Create(II->getNormalDest(), II->getUnwindDest(), 855 ConstantInt::getTrue(Callee->getContext()), II); 856 } 857 return EraseInstFromFunction(*CS.getInstruction()); 858 } 859 860 if (BitCastInst *BC = dyn_cast<BitCastInst>(Callee)) 861 if (IntrinsicInst *In = dyn_cast<IntrinsicInst>(BC->getOperand(0))) 862 if (In->getIntrinsicID() == Intrinsic::init_trampoline) 863 return transformCallThroughTrampoline(CS); 864 865 const PointerType *PTy = cast<PointerType>(Callee->getType()); 866 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 867 if (FTy->isVarArg()) { 868 int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1); 869 // See if we can optimize any arguments passed through the varargs area of 870 // the call. 871 for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(), 872 E = CS.arg_end(); I != E; ++I, ++ix) { 873 CastInst *CI = dyn_cast<CastInst>(*I); 874 if (CI && isSafeToEliminateVarargsCast(CS, CI, TD, ix)) { 875 *I = CI->getOperand(0); 876 Changed = true; 877 } 878 } 879 } 880 881 if (isa<InlineAsm>(Callee) && !CS.doesNotThrow()) { 882 // Inline asm calls cannot throw - mark them 'nounwind'. 883 CS.setDoesNotThrow(); 884 Changed = true; 885 } 886 887 // Try to optimize the call if possible, we require TargetData for most of 888 // this. None of these calls are seen as possibly dead so go ahead and 889 // delete the instruction now. 890 if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) { 891 Instruction *I = tryOptimizeCall(CI, TD); 892 // If we changed something return the result, etc. Otherwise let 893 // the fallthrough check. 894 if (I) return EraseInstFromFunction(*I); 895 } 896 897 return Changed ? CS.getInstruction() : 0; 898} 899 900// transformConstExprCastCall - If the callee is a constexpr cast of a function, 901// attempt to move the cast to the arguments of the call/invoke. 902// 903bool InstCombiner::transformConstExprCastCall(CallSite CS) { 904 if (!isa<ConstantExpr>(CS.getCalledValue())) return false; 905 ConstantExpr *CE = cast<ConstantExpr>(CS.getCalledValue()); 906 if (CE->getOpcode() != Instruction::BitCast || 907 !isa<Function>(CE->getOperand(0))) 908 return false; 909 Function *Callee = cast<Function>(CE->getOperand(0)); 910 Instruction *Caller = CS.getInstruction(); 911 const AttrListPtr &CallerPAL = CS.getAttributes(); 912 913 // Okay, this is a cast from a function to a different type. Unless doing so 914 // would cause a type conversion of one of our arguments, change this call to 915 // be a direct call with arguments casted to the appropriate types. 916 // 917 const FunctionType *FT = Callee->getFunctionType(); 918 const Type *OldRetTy = Caller->getType(); 919 const Type *NewRetTy = FT->getReturnType(); 920 921 if (NewRetTy->isStructTy()) 922 return false; // TODO: Handle multiple return values. 923 924 // Check to see if we are changing the return type... 925 if (OldRetTy != NewRetTy) { 926 if (Callee->isDeclaration() && 927 // Conversion is ok if changing from one pointer type to another or from 928 // a pointer to an integer of the same size. 929 !((OldRetTy->isPointerTy() || !TD || 930 OldRetTy == TD->getIntPtrType(Caller->getContext())) && 931 (NewRetTy->isPointerTy() || !TD || 932 NewRetTy == TD->getIntPtrType(Caller->getContext())))) 933 return false; // Cannot transform this return value. 934 935 if (!Caller->use_empty() && 936 // void -> non-void is handled specially 937 !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy)) 938 return false; // Cannot transform this return value. 939 940 if (!CallerPAL.isEmpty() && !Caller->use_empty()) { 941 Attributes RAttrs = CallerPAL.getRetAttributes(); 942 if (RAttrs & Attribute::typeIncompatible(NewRetTy)) 943 return false; // Attribute not compatible with transformed value. 944 } 945 946 // If the callsite is an invoke instruction, and the return value is used by 947 // a PHI node in a successor, we cannot change the return type of the call 948 // because there is no place to put the cast instruction (without breaking 949 // the critical edge). Bail out in this case. 950 if (!Caller->use_empty()) 951 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) 952 for (Value::use_iterator UI = II->use_begin(), E = II->use_end(); 953 UI != E; ++UI) 954 if (PHINode *PN = dyn_cast<PHINode>(*UI)) 955 if (PN->getParent() == II->getNormalDest() || 956 PN->getParent() == II->getUnwindDest()) 957 return false; 958 } 959 960 unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin()); 961 unsigned NumCommonArgs = std::min(FT->getNumParams(), NumActualArgs); 962 963 CallSite::arg_iterator AI = CS.arg_begin(); 964 for (unsigned i = 0, e = NumCommonArgs; i != e; ++i, ++AI) { 965 const Type *ParamTy = FT->getParamType(i); 966 const Type *ActTy = (*AI)->getType(); 967 968 if (!CastInst::isCastable(ActTy, ParamTy)) 969 return false; // Cannot transform this parameter value. 970 971 if (CallerPAL.getParamAttributes(i + 1) 972 & Attribute::typeIncompatible(ParamTy)) 973 return false; // Attribute not compatible with transformed value. 974 975 // Converting from one pointer type to another or between a pointer and an 976 // integer of the same size is safe even if we do not have a body. 977 bool isConvertible = ActTy == ParamTy || 978 (TD && ((ParamTy->isPointerTy() || 979 ParamTy == TD->getIntPtrType(Caller->getContext())) && 980 (ActTy->isPointerTy() || 981 ActTy == TD->getIntPtrType(Caller->getContext())))); 982 if (Callee->isDeclaration() && !isConvertible) return false; 983 } 984 985 if (FT->getNumParams() < NumActualArgs && !FT->isVarArg() && 986 Callee->isDeclaration()) 987 return false; // Do not delete arguments unless we have a function body. 988 989 if (FT->getNumParams() < NumActualArgs && FT->isVarArg() && 990 !CallerPAL.isEmpty()) 991 // In this case we have more arguments than the new function type, but we 992 // won't be dropping them. Check that these extra arguments have attributes 993 // that are compatible with being a vararg call argument. 994 for (unsigned i = CallerPAL.getNumSlots(); i; --i) { 995 if (CallerPAL.getSlot(i - 1).Index <= FT->getNumParams()) 996 break; 997 Attributes PAttrs = CallerPAL.getSlot(i - 1).Attrs; 998 if (PAttrs & Attribute::VarArgsIncompatible) 999 return false; 1000 } 1001 1002 // Okay, we decided that this is a safe thing to do: go ahead and start 1003 // inserting cast instructions as necessary... 1004 std::vector<Value*> Args; 1005 Args.reserve(NumActualArgs); 1006 SmallVector<AttributeWithIndex, 8> attrVec; 1007 attrVec.reserve(NumCommonArgs); 1008 1009 // Get any return attributes. 1010 Attributes RAttrs = CallerPAL.getRetAttributes(); 1011 1012 // If the return value is not being used, the type may not be compatible 1013 // with the existing attributes. Wipe out any problematic attributes. 1014 RAttrs &= ~Attribute::typeIncompatible(NewRetTy); 1015 1016 // Add the new return attributes. 1017 if (RAttrs) 1018 attrVec.push_back(AttributeWithIndex::get(0, RAttrs)); 1019 1020 AI = CS.arg_begin(); 1021 for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { 1022 const Type *ParamTy = FT->getParamType(i); 1023 if ((*AI)->getType() == ParamTy) { 1024 Args.push_back(*AI); 1025 } else { 1026 Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, 1027 false, ParamTy, false); 1028 Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy, "tmp")); 1029 } 1030 1031 // Add any parameter attributes. 1032 if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) 1033 attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); 1034 } 1035 1036 // If the function takes more arguments than the call was taking, add them 1037 // now. 1038 for (unsigned i = NumCommonArgs; i != FT->getNumParams(); ++i) 1039 Args.push_back(Constant::getNullValue(FT->getParamType(i))); 1040 1041 // If we are removing arguments to the function, emit an obnoxious warning. 1042 if (FT->getNumParams() < NumActualArgs) { 1043 if (!FT->isVarArg()) { 1044 errs() << "WARNING: While resolving call to function '" 1045 << Callee->getName() << "' arguments were dropped!\n"; 1046 } else { 1047 // Add all of the arguments in their promoted form to the arg list. 1048 for (unsigned i = FT->getNumParams(); i != NumActualArgs; ++i, ++AI) { 1049 const Type *PTy = getPromotedType((*AI)->getType()); 1050 if (PTy != (*AI)->getType()) { 1051 // Must promote to pass through va_arg area! 1052 Instruction::CastOps opcode = 1053 CastInst::getCastOpcode(*AI, false, PTy, false); 1054 Args.push_back(Builder->CreateCast(opcode, *AI, PTy, "tmp")); 1055 } else { 1056 Args.push_back(*AI); 1057 } 1058 1059 // Add any parameter attributes. 1060 if (Attributes PAttrs = CallerPAL.getParamAttributes(i + 1)) 1061 attrVec.push_back(AttributeWithIndex::get(i + 1, PAttrs)); 1062 } 1063 } 1064 } 1065 1066 if (Attributes FnAttrs = CallerPAL.getFnAttributes()) 1067 attrVec.push_back(AttributeWithIndex::get(~0, FnAttrs)); 1068 1069 if (NewRetTy->isVoidTy()) 1070 Caller->setName(""); // Void type should not have a name. 1071 1072 const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec.begin(), 1073 attrVec.end()); 1074 1075 Instruction *NC; 1076 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { 1077 NC = InvokeInst::Create(Callee, II->getNormalDest(), II->getUnwindDest(), 1078 Args.begin(), Args.end(), 1079 Caller->getName(), Caller); 1080 cast<InvokeInst>(NC)->setCallingConv(II->getCallingConv()); 1081 cast<InvokeInst>(NC)->setAttributes(NewCallerPAL); 1082 } else { 1083 NC = CallInst::Create(Callee, Args.begin(), Args.end(), 1084 Caller->getName(), Caller); 1085 CallInst *CI = cast<CallInst>(Caller); 1086 if (CI->isTailCall()) 1087 cast<CallInst>(NC)->setTailCall(); 1088 cast<CallInst>(NC)->setCallingConv(CI->getCallingConv()); 1089 cast<CallInst>(NC)->setAttributes(NewCallerPAL); 1090 } 1091 1092 // Insert a cast of the return type as necessary. 1093 Value *NV = NC; 1094 if (OldRetTy != NV->getType() && !Caller->use_empty()) { 1095 if (!NV->getType()->isVoidTy()) { 1096 Instruction::CastOps opcode = CastInst::getCastOpcode(NC, false, 1097 OldRetTy, false); 1098 NV = NC = CastInst::Create(opcode, NC, OldRetTy, "tmp"); 1099 1100 // If this is an invoke instruction, we should insert it after the first 1101 // non-phi, instruction in the normal successor block. 1102 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { 1103 BasicBlock::iterator I = II->getNormalDest()->getFirstNonPHI(); 1104 InsertNewInstBefore(NC, *I); 1105 } else { 1106 // Otherwise, it's a call, just insert cast right after the call instr 1107 InsertNewInstBefore(NC, *Caller); 1108 } 1109 Worklist.AddUsersToWorkList(*Caller); 1110 } else { 1111 NV = UndefValue::get(Caller->getType()); 1112 } 1113 } 1114 1115 1116 if (!Caller->use_empty()) 1117 Caller->replaceAllUsesWith(NV); 1118 1119 EraseInstFromFunction(*Caller); 1120 return true; 1121} 1122 1123// transformCallThroughTrampoline - Turn a call to a function created by the 1124// init_trampoline intrinsic into a direct call to the underlying function. 1125// 1126Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) { 1127 Value *Callee = CS.getCalledValue(); 1128 const PointerType *PTy = cast<PointerType>(Callee->getType()); 1129 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType()); 1130 const AttrListPtr &Attrs = CS.getAttributes(); 1131 1132 // If the call already has the 'nest' attribute somewhere then give up - 1133 // otherwise 'nest' would occur twice after splicing in the chain. 1134 if (Attrs.hasAttrSomewhere(Attribute::Nest)) 1135 return 0; 1136 1137 IntrinsicInst *Tramp = 1138 cast<IntrinsicInst>(cast<BitCastInst>(Callee)->getOperand(0)); 1139 1140 Function *NestF = cast<Function>(Tramp->getOperand(2)->stripPointerCasts()); 1141 const PointerType *NestFPTy = cast<PointerType>(NestF->getType()); 1142 const FunctionType *NestFTy = cast<FunctionType>(NestFPTy->getElementType()); 1143 1144 const AttrListPtr &NestAttrs = NestF->getAttributes(); 1145 if (!NestAttrs.isEmpty()) { 1146 unsigned NestIdx = 1; 1147 const Type *NestTy = 0; 1148 Attributes NestAttr = Attribute::None; 1149 1150 // Look for a parameter marked with the 'nest' attribute. 1151 for (FunctionType::param_iterator I = NestFTy->param_begin(), 1152 E = NestFTy->param_end(); I != E; ++NestIdx, ++I) 1153 if (NestAttrs.paramHasAttr(NestIdx, Attribute::Nest)) { 1154 // Record the parameter type and any other attributes. 1155 NestTy = *I; 1156 NestAttr = NestAttrs.getParamAttributes(NestIdx); 1157 break; 1158 } 1159 1160 if (NestTy) { 1161 Instruction *Caller = CS.getInstruction(); 1162 std::vector<Value*> NewArgs; 1163 NewArgs.reserve(unsigned(CS.arg_end()-CS.arg_begin())+1); 1164 1165 SmallVector<AttributeWithIndex, 8> NewAttrs; 1166 NewAttrs.reserve(Attrs.getNumSlots() + 1); 1167 1168 // Insert the nest argument into the call argument list, which may 1169 // mean appending it. Likewise for attributes. 1170 1171 // Add any result attributes. 1172 if (Attributes Attr = Attrs.getRetAttributes()) 1173 NewAttrs.push_back(AttributeWithIndex::get(0, Attr)); 1174 1175 { 1176 unsigned Idx = 1; 1177 CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); 1178 do { 1179 if (Idx == NestIdx) { 1180 // Add the chain argument and attributes. 1181 Value *NestVal = Tramp->getOperand(3); 1182 if (NestVal->getType() != NestTy) 1183 NestVal = new BitCastInst(NestVal, NestTy, "nest", Caller); 1184 NewArgs.push_back(NestVal); 1185 NewAttrs.push_back(AttributeWithIndex::get(NestIdx, NestAttr)); 1186 } 1187 1188 if (I == E) 1189 break; 1190 1191 // Add the original argument and attributes. 1192 NewArgs.push_back(*I); 1193 if (Attributes Attr = Attrs.getParamAttributes(Idx)) 1194 NewAttrs.push_back 1195 (AttributeWithIndex::get(Idx + (Idx >= NestIdx), Attr)); 1196 1197 ++Idx, ++I; 1198 } while (1); 1199 } 1200 1201 // Add any function attributes. 1202 if (Attributes Attr = Attrs.getFnAttributes()) 1203 NewAttrs.push_back(AttributeWithIndex::get(~0, Attr)); 1204 1205 // The trampoline may have been bitcast to a bogus type (FTy). 1206 // Handle this by synthesizing a new function type, equal to FTy 1207 // with the chain parameter inserted. 1208 1209 std::vector<const Type*> NewTypes; 1210 NewTypes.reserve(FTy->getNumParams()+1); 1211 1212 // Insert the chain's type into the list of parameter types, which may 1213 // mean appending it. 1214 { 1215 unsigned Idx = 1; 1216 FunctionType::param_iterator I = FTy->param_begin(), 1217 E = FTy->param_end(); 1218 1219 do { 1220 if (Idx == NestIdx) 1221 // Add the chain's type. 1222 NewTypes.push_back(NestTy); 1223 1224 if (I == E) 1225 break; 1226 1227 // Add the original type. 1228 NewTypes.push_back(*I); 1229 1230 ++Idx, ++I; 1231 } while (1); 1232 } 1233 1234 // Replace the trampoline call with a direct call. Let the generic 1235 // code sort out any function type mismatches. 1236 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), NewTypes, 1237 FTy->isVarArg()); 1238 Constant *NewCallee = 1239 NestF->getType() == PointerType::getUnqual(NewFTy) ? 1240 NestF : ConstantExpr::getBitCast(NestF, 1241 PointerType::getUnqual(NewFTy)); 1242 const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs.begin(), 1243 NewAttrs.end()); 1244 1245 Instruction *NewCaller; 1246 if (InvokeInst *II = dyn_cast<InvokeInst>(Caller)) { 1247 NewCaller = InvokeInst::Create(NewCallee, 1248 II->getNormalDest(), II->getUnwindDest(), 1249 NewArgs.begin(), NewArgs.end(), 1250 Caller->getName(), Caller); 1251 cast<InvokeInst>(NewCaller)->setCallingConv(II->getCallingConv()); 1252 cast<InvokeInst>(NewCaller)->setAttributes(NewPAL); 1253 } else { 1254 NewCaller = CallInst::Create(NewCallee, NewArgs.begin(), NewArgs.end(), 1255 Caller->getName(), Caller); 1256 if (cast<CallInst>(Caller)->isTailCall()) 1257 cast<CallInst>(NewCaller)->setTailCall(); 1258 cast<CallInst>(NewCaller)-> 1259 setCallingConv(cast<CallInst>(Caller)->getCallingConv()); 1260 cast<CallInst>(NewCaller)->setAttributes(NewPAL); 1261 } 1262 if (!Caller->getType()->isVoidTy()) 1263 Caller->replaceAllUsesWith(NewCaller); 1264 Caller->eraseFromParent(); 1265 Worklist.Remove(Caller); 1266 return 0; 1267 } 1268 } 1269 1270 // Replace the trampoline call with a direct call. Since there is no 'nest' 1271 // parameter, there is no need to adjust the argument list. Let the generic 1272 // code sort out any function type mismatches. 1273 Constant *NewCallee = 1274 NestF->getType() == PTy ? NestF : 1275 ConstantExpr::getBitCast(NestF, PTy); 1276 CS.setCalledFunction(NewCallee); 1277 return CS.getInstruction(); 1278} 1279 1280