Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 1 | //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 10 | // This file implements the IntrinsicLowering class. |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 18 | #include "llvm/Type.h" |
Bill Wendling | d9fd2ac | 2006-11-28 02:08:17 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/IntrinsicLowering.h" |
| 20 | #include "llvm/Support/Streams.h" |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 990b849 | 2007-02-13 06:01:22 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Owen Anderson | 718cb66 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 26 | template <class ArgIt> |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 27 | static void EnsureFunctionExists(Module &M, const char *Name, |
| 28 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 29 | const Type *RetTy) { |
| 30 | // Insert a correctly-typed definition now. |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 31 | std::vector<const Type *> ParamTys; |
| 32 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 33 | ParamTys.push_back(I->getType()); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 34 | M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 37 | static void EnsureFPIntrinsicsExist(Module &M, Module::iterator I, |
| 38 | const char *FName, |
| 39 | const char *DName, const char *LDName) { |
| 40 | // Insert definitions for all the floating point types. |
| 41 | switch((int)I->arg_begin()->getType()->getTypeID()) { |
| 42 | case Type::FloatTyID: |
| 43 | EnsureFunctionExists(M, FName, I->arg_begin(), I->arg_end(), |
| 44 | Type::FloatTy); |
| 45 | case Type::DoubleTyID: |
| 46 | EnsureFunctionExists(M, DName, I->arg_begin(), I->arg_end(), |
| 47 | Type::DoubleTy); |
| 48 | case Type::X86_FP80TyID: |
| 49 | case Type::FP128TyID: |
| 50 | case Type::PPC_FP128TyID: |
| 51 | EnsureFunctionExists(M, LDName, I->arg_begin(), I->arg_end(), |
| 52 | I->arg_begin()->getType()); |
| 53 | } |
| 54 | } |
| 55 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 56 | /// ReplaceCallWith - This function is used when we want to lower an intrinsic |
| 57 | /// call to a call of an external function. This handles hard cases such as |
| 58 | /// when there was already a prototype for the external function, and if that |
| 59 | /// prototype doesn't match the arguments we expect to pass in. |
| 60 | template <class ArgIt> |
| 61 | static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 62 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 63 | const Type *RetTy, Constant *&FCache) { |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 64 | if (!FCache) { |
| 65 | // If we haven't already looked up this function, check to see if the |
| 66 | // program already contains a function with this name. |
| 67 | Module *M = CI->getParent()->getParent()->getParent(); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 68 | // Get or insert the definition now. |
| 69 | std::vector<const Type *> ParamTys; |
| 70 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 71 | ParamTys.push_back((*I)->getType()); |
| 72 | FCache = M->getOrInsertFunction(NewFn, |
| 73 | FunctionType::get(RetTy, ParamTys, false)); |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 75 | |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 76 | SmallVector<Value *, 8> Args(ArgBegin, ArgEnd); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 77 | CallInst *NewCI = CallInst::Create(FCache, Args.begin(), Args.end(), |
| 78 | CI->getName(), CI); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 79 | if (!CI->use_empty()) |
| 80 | CI->replaceAllUsesWith(NewCI); |
Chris Lattner | 02348ca | 2004-06-11 02:54:02 +0000 | [diff] [blame] | 81 | return NewCI; |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 84 | void IntrinsicLowering::AddPrototypes(Module &M) { |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 85 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 86 | if (I->isDeclaration() && !I->use_empty()) |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 87 | switch (I->getIntrinsicID()) { |
| 88 | default: break; |
| 89 | case Intrinsic::setjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 90 | EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 91 | Type::Int32Ty); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 92 | break; |
| 93 | case Intrinsic::longjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 94 | EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(), |
| 95 | Type::VoidTy); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 96 | break; |
| 97 | case Intrinsic::siglongjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 98 | EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(), |
| 99 | Type::VoidTy); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 100 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 101 | case Intrinsic::memcpy: |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 102 | M.getOrInsertFunction("memcpy", PointerType::getUnqual(Type::Int8Ty), |
| 103 | PointerType::getUnqual(Type::Int8Ty), |
| 104 | PointerType::getUnqual(Type::Int8Ty), |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 105 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 106 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 107 | case Intrinsic::memmove: |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 108 | M.getOrInsertFunction("memmove", PointerType::getUnqual(Type::Int8Ty), |
| 109 | PointerType::getUnqual(Type::Int8Ty), |
| 110 | PointerType::getUnqual(Type::Int8Ty), |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 111 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 112 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 113 | case Intrinsic::memset: |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 114 | M.getOrInsertFunction("memset", PointerType::getUnqual(Type::Int8Ty), |
| 115 | PointerType::getUnqual(Type::Int8Ty), |
| 116 | Type::Int32Ty, |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 117 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 118 | break; |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 119 | case Intrinsic::sqrt: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 120 | EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl"); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 121 | break; |
Dan Gohman | c4c9660 | 2007-10-15 22:07:31 +0000 | [diff] [blame] | 122 | case Intrinsic::sin: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 123 | EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl"); |
Dan Gohman | c4c9660 | 2007-10-15 22:07:31 +0000 | [diff] [blame] | 124 | break; |
| 125 | case Intrinsic::cos: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 126 | EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl"); |
Dan Gohman | c4c9660 | 2007-10-15 22:07:31 +0000 | [diff] [blame] | 127 | break; |
| 128 | case Intrinsic::pow: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 129 | EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl"); |
Dan Gohman | c4c9660 | 2007-10-15 22:07:31 +0000 | [diff] [blame] | 130 | break; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 131 | case Intrinsic::log: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 132 | EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 133 | break; |
| 134 | case Intrinsic::log2: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 135 | EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 136 | break; |
| 137 | case Intrinsic::log10: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 138 | EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 139 | break; |
| 140 | case Intrinsic::exp: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 141 | EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 142 | break; |
| 143 | case Intrinsic::exp2: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 144 | EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 145 | break; |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 148 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 149 | /// LowerBSWAP - Emit the code to lower bswap of V before the specified |
| 150 | /// instruction IP. |
| 151 | static Value *LowerBSWAP(Value *V, Instruction *IP) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 152 | assert(V->getType()->isInteger() && "Can't bswap a non-integer type!"); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 153 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 154 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 155 | |
| 156 | switch(BitSize) { |
| 157 | default: assert(0 && "Unhandled type size of value to byteswap!"); |
| 158 | case 16: { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 159 | Value *Tmp1 = BinaryOperator::CreateShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 160 | ConstantInt::get(V->getType(),8),"bswap.2",IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 161 | Value *Tmp2 = BinaryOperator::CreateLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 162 | ConstantInt::get(V->getType(),8),"bswap.1",IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 163 | V = BinaryOperator::CreateOr(Tmp1, Tmp2, "bswap.i16", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 164 | break; |
| 165 | } |
| 166 | case 32: { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 167 | Value *Tmp4 = BinaryOperator::CreateShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 168 | ConstantInt::get(V->getType(),24),"bswap.4", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 169 | Value *Tmp3 = BinaryOperator::CreateShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 170 | ConstantInt::get(V->getType(),8),"bswap.3",IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 171 | Value *Tmp2 = BinaryOperator::CreateLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 172 | ConstantInt::get(V->getType(),8),"bswap.2",IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 173 | Value *Tmp1 = BinaryOperator::CreateLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 174 | ConstantInt::get(V->getType(),24),"bswap.1", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 175 | Tmp3 = BinaryOperator::CreateAnd(Tmp3, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 176 | ConstantInt::get(Type::Int32Ty, 0xFF0000), |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 177 | "bswap.and3", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 178 | Tmp2 = BinaryOperator::CreateAnd(Tmp2, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 179 | ConstantInt::get(Type::Int32Ty, 0xFF00), |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 180 | "bswap.and2", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 181 | Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or1", IP); |
| 182 | Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or2", IP); |
| 183 | V = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.i32", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | case 64: { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 187 | Value *Tmp8 = BinaryOperator::CreateShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 188 | ConstantInt::get(V->getType(),56),"bswap.8", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 189 | Value *Tmp7 = BinaryOperator::CreateShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 190 | ConstantInt::get(V->getType(),40),"bswap.7", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 191 | Value *Tmp6 = BinaryOperator::CreateShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 192 | ConstantInt::get(V->getType(),24),"bswap.6", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 193 | Value *Tmp5 = BinaryOperator::CreateShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 194 | ConstantInt::get(V->getType(),8),"bswap.5", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 195 | Value* Tmp4 = BinaryOperator::CreateLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 196 | ConstantInt::get(V->getType(),8),"bswap.4", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 197 | Value* Tmp3 = BinaryOperator::CreateLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 198 | ConstantInt::get(V->getType(),24),"bswap.3", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 199 | Value* Tmp2 = BinaryOperator::CreateLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 200 | ConstantInt::get(V->getType(),40),"bswap.2", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 201 | Value* Tmp1 = BinaryOperator::CreateLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 202 | ConstantInt::get(V->getType(),56),"bswap.1", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 203 | Tmp7 = BinaryOperator::CreateAnd(Tmp7, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 204 | ConstantInt::get(Type::Int64Ty, |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 205 | 0xFF000000000000ULL), |
| 206 | "bswap.and7", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 207 | Tmp6 = BinaryOperator::CreateAnd(Tmp6, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 208 | ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 209 | "bswap.and6", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 210 | Tmp5 = BinaryOperator::CreateAnd(Tmp5, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 211 | ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 212 | "bswap.and5", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 213 | Tmp4 = BinaryOperator::CreateAnd(Tmp4, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 214 | ConstantInt::get(Type::Int64Ty, 0xFF000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 215 | "bswap.and4", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 216 | Tmp3 = BinaryOperator::CreateAnd(Tmp3, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 217 | ConstantInt::get(Type::Int64Ty, 0xFF0000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 218 | "bswap.and3", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 219 | Tmp2 = BinaryOperator::CreateAnd(Tmp2, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 220 | ConstantInt::get(Type::Int64Ty, 0xFF00ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 221 | "bswap.and2", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 222 | Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp7, "bswap.or1", IP); |
| 223 | Tmp6 = BinaryOperator::CreateOr(Tmp6, Tmp5, "bswap.or2", IP); |
| 224 | Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp3, "bswap.or3", IP); |
| 225 | Tmp2 = BinaryOperator::CreateOr(Tmp2, Tmp1, "bswap.or4", IP); |
| 226 | Tmp8 = BinaryOperator::CreateOr(Tmp8, Tmp6, "bswap.or5", IP); |
| 227 | Tmp4 = BinaryOperator::CreateOr(Tmp4, Tmp2, "bswap.or6", IP); |
| 228 | V = BinaryOperator::CreateOr(Tmp8, Tmp4, "bswap.i64", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | } |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 232 | return V; |
| 233 | } |
| 234 | |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 235 | /// LowerCTPOP - Emit the code to lower ctpop of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 236 | /// instruction IP. |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 237 | static Value *LowerCTPOP(Value *V, Instruction *IP) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 238 | assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 239 | |
| 240 | static const uint64_t MaskValues[6] = { |
| 241 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 242 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 243 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 244 | }; |
| 245 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 246 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 247 | unsigned WordSize = (BitSize + 63) / 64; |
| 248 | Value *Count = ConstantInt::get(V->getType(), 0); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 249 | |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 250 | for (unsigned n = 0; n < WordSize; ++n) { |
| 251 | Value *PartValue = V; |
| 252 | for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize); |
| 253 | i <<= 1, ++ct) { |
| 254 | Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 255 | Value *LHS = BinaryOperator::CreateAnd( |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 256 | PartValue, MaskCst, "cppop.and1", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 257 | Value *VShift = BinaryOperator::CreateLShr(PartValue, |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 258 | ConstantInt::get(V->getType(), i), "ctpop.sh", IP); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 259 | Value *RHS = BinaryOperator::CreateAnd(VShift, MaskCst, "cppop.and2", IP); |
| 260 | PartValue = BinaryOperator::CreateAdd(LHS, RHS, "ctpop.step", IP); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 261 | } |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 262 | Count = BinaryOperator::CreateAdd(PartValue, Count, "ctpop.part", IP); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 263 | if (BitSize > 64) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 264 | V = BinaryOperator::CreateLShr(V, ConstantInt::get(V->getType(), 64), |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 265 | "ctpop.part.sh", IP); |
| 266 | BitSize -= 64; |
| 267 | } |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Chris Lattner | 914ce45 | 2007-08-06 16:36:18 +0000 | [diff] [blame] | 270 | return Count; |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 273 | /// LowerCTLZ - Emit the code to lower ctlz of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 274 | /// instruction IP. |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 275 | static Value *LowerCTLZ(Value *V, Instruction *IP) { |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 276 | |
| 277 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 278 | for (unsigned i = 1; i < BitSize; i <<= 1) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 279 | Value *ShVal = ConstantInt::get(V->getType(), i); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 280 | ShVal = BinaryOperator::CreateLShr(V, ShVal, "ctlz.sh", IP); |
| 281 | V = BinaryOperator::CreateOr(V, ShVal, "ctlz.step", IP); |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 284 | V = BinaryOperator::CreateNot(V, "", IP); |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 285 | return LowerCTPOP(V, IP); |
| 286 | } |
| 287 | |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 288 | /// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes |
| 289 | /// three integer arguments. The first argument is the Value from which the |
| 290 | /// bits will be selected. It may be of any bit width. The second and third |
| 291 | /// arguments specify a range of bits to select with the second argument |
| 292 | /// specifying the low bit and the third argument specifying the high bit. Both |
| 293 | /// must be type i32. The result is the corresponding selected bits from the |
| 294 | /// Value in the same width as the Value (first argument). If the low bit index |
| 295 | /// is higher than the high bit index then the inverse selection is done and |
| 296 | /// the bits are returned in inverse order. |
| 297 | /// @brief Lowering of llvm.part.select intrinsic. |
| 298 | static Instruction *LowerPartSelect(CallInst *CI) { |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 299 | // Make sure we're dealing with a part select intrinsic here |
| 300 | Function *F = CI->getCalledFunction(); |
| 301 | const FunctionType *FT = F->getFunctionType(); |
| 302 | if (!F->isDeclaration() || !FT->getReturnType()->isInteger() || |
| 303 | FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() || |
| 304 | !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger()) |
| 305 | return CI; |
| 306 | |
| 307 | // Get the intrinsic implementation function by converting all the . to _ |
| 308 | // in the intrinsic's function name and then reconstructing the function |
| 309 | // declaration. |
| 310 | std::string Name(F->getName()); |
| 311 | for (unsigned i = 4; i < Name.length(); ++i) |
| 312 | if (Name[i] == '.') |
| 313 | Name[i] = '_'; |
| 314 | Module* M = F->getParent(); |
| 315 | F = cast<Function>(M->getOrInsertFunction(Name, FT)); |
Reid Spencer | df41353 | 2007-04-12 21:53:38 +0000 | [diff] [blame] | 316 | F->setLinkage(GlobalValue::WeakLinkage); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 317 | |
| 318 | // If we haven't defined the impl function yet, do so now |
| 319 | if (F->isDeclaration()) { |
| 320 | |
| 321 | // Get the arguments to the function |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 322 | Function::arg_iterator args = F->arg_begin(); |
| 323 | Value* Val = args++; Val->setName("Val"); |
| 324 | Value* Lo = args++; Lo->setName("Lo"); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 325 | Value* Hi = args++; Hi->setName("High"); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 326 | |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 327 | // We want to select a range of bits here such that [Hi, Lo] is shifted |
| 328 | // down to the low bits. However, it is quite possible that Hi is smaller |
| 329 | // than Lo in which case the bits have to be reversed. |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 330 | |
| 331 | // Create the blocks we will need for the two cases (forward, reverse) |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 332 | BasicBlock* CurBB = BasicBlock::Create("entry", F); |
| 333 | BasicBlock *RevSize = BasicBlock::Create("revsize", CurBB->getParent()); |
| 334 | BasicBlock *FwdSize = BasicBlock::Create("fwdsize", CurBB->getParent()); |
| 335 | BasicBlock *Compute = BasicBlock::Create("compute", CurBB->getParent()); |
| 336 | BasicBlock *Reverse = BasicBlock::Create("reverse", CurBB->getParent()); |
| 337 | BasicBlock *RsltBlk = BasicBlock::Create("result", CurBB->getParent()); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 338 | |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 339 | // Cast Hi and Lo to the size of Val so the widths are all the same |
| 340 | if (Hi->getType() != Val->getType()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 341 | Hi = CastInst::CreateIntegerCast(Hi, Val->getType(), false, |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 342 | "tmp", CurBB); |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 343 | if (Lo->getType() != Val->getType()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 344 | Lo = CastInst::CreateIntegerCast(Lo, Val->getType(), false, |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 345 | "tmp", CurBB); |
| 346 | |
| 347 | // Compute a few things that both cases will need, up front. |
| 348 | Constant* Zero = ConstantInt::get(Val->getType(), 0); |
| 349 | Constant* One = ConstantInt::get(Val->getType(), 1); |
| 350 | Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType()); |
| 351 | |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 352 | // Compare the Hi and Lo bit positions. This is used to determine |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 353 | // which case we have (forward or reverse) |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 354 | ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Hi, Lo, "less",CurBB); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 355 | BranchInst::Create(RevSize, FwdSize, Cmp, CurBB); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 356 | |
| 357 | // First, copmute the number of bits in the forward case. |
| 358 | Instruction* FBitSize = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 359 | BinaryOperator::CreateSub(Hi, Lo,"fbits", FwdSize); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 360 | BranchInst::Create(Compute, FwdSize); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 361 | |
| 362 | // Second, compute the number of bits in the reverse case. |
| 363 | Instruction* RBitSize = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 364 | BinaryOperator::CreateSub(Lo, Hi, "rbits", RevSize); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 365 | BranchInst::Create(Compute, RevSize); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 366 | |
| 367 | // Now, compute the bit range. Start by getting the bitsize and the shift |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 368 | // amount (either Hi or Lo) from PHI nodes. Then we compute a mask for |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 369 | // the number of bits we want in the range. We shift the bits down to the |
| 370 | // least significant bits, apply the mask to zero out unwanted high bits, |
| 371 | // and we have computed the "forward" result. It may still need to be |
| 372 | // reversed. |
| 373 | |
| 374 | // Get the BitSize from one of the two subtractions |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 375 | PHINode *BitSize = PHINode::Create(Val->getType(), "bits", Compute); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 376 | BitSize->reserveOperandSpace(2); |
| 377 | BitSize->addIncoming(FBitSize, FwdSize); |
| 378 | BitSize->addIncoming(RBitSize, RevSize); |
| 379 | |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 380 | // Get the ShiftAmount as the smaller of Hi/Lo |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 381 | PHINode *ShiftAmt = PHINode::Create(Val->getType(), "shiftamt", Compute); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 382 | ShiftAmt->reserveOperandSpace(2); |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 383 | ShiftAmt->addIncoming(Lo, FwdSize); |
| 384 | ShiftAmt->addIncoming(Hi, RevSize); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 385 | |
| 386 | // Increment the bit size |
| 387 | Instruction *BitSizePlusOne = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 388 | BinaryOperator::CreateAdd(BitSize, One, "bits", Compute); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 389 | |
| 390 | // Create a Mask to zero out the high order bits. |
| 391 | Instruction* Mask = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 392 | BinaryOperator::CreateShl(AllOnes, BitSizePlusOne, "mask", Compute); |
| 393 | Mask = BinaryOperator::CreateNot(Mask, "mask", Compute); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 394 | |
| 395 | // Shift the bits down and apply the mask |
| 396 | Instruction* FRes = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 397 | BinaryOperator::CreateLShr(Val, ShiftAmt, "fres", Compute); |
| 398 | FRes = BinaryOperator::CreateAnd(FRes, Mask, "fres", Compute); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 399 | BranchInst::Create(Reverse, RsltBlk, Cmp, Compute); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 400 | |
| 401 | // In the Reverse block we have the mask already in FRes but we must reverse |
| 402 | // it by shifting FRes bits right and putting them in RRes by shifting them |
| 403 | // in from left. |
| 404 | |
| 405 | // First set up our loop counters |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 406 | PHINode *Count = PHINode::Create(Val->getType(), "count", Reverse); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 407 | Count->reserveOperandSpace(2); |
| 408 | Count->addIncoming(BitSizePlusOne, Compute); |
| 409 | |
| 410 | // Next, get the value that we are shifting. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 411 | PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", Reverse); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 412 | BitsToShift->reserveOperandSpace(2); |
| 413 | BitsToShift->addIncoming(FRes, Compute); |
| 414 | |
| 415 | // Finally, get the result of the last computation |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 416 | PHINode *RRes = PHINode::Create(Val->getType(), "rres", Reverse); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 417 | RRes->reserveOperandSpace(2); |
| 418 | RRes->addIncoming(Zero, Compute); |
| 419 | |
| 420 | // Decrement the counter |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 421 | Instruction *Decr = BinaryOperator::CreateSub(Count, One, "decr", Reverse); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 422 | Count->addIncoming(Decr, Reverse); |
| 423 | |
| 424 | // Compute the Bit that we want to move |
| 425 | Instruction *Bit = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 426 | BinaryOperator::CreateAnd(BitsToShift, One, "bit", Reverse); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 427 | |
| 428 | // Compute the new value for next iteration. |
| 429 | Instruction *NewVal = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 430 | BinaryOperator::CreateLShr(BitsToShift, One, "rshift", Reverse); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 431 | BitsToShift->addIncoming(NewVal, Reverse); |
| 432 | |
| 433 | // Shift the bit into the low bits of the result. |
| 434 | Instruction *NewRes = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 435 | BinaryOperator::CreateShl(RRes, One, "lshift", Reverse); |
| 436 | NewRes = BinaryOperator::CreateOr(NewRes, Bit, "addbit", Reverse); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 437 | RRes->addIncoming(NewRes, Reverse); |
| 438 | |
| 439 | // Terminate loop if we've moved all the bits. |
| 440 | ICmpInst *Cond = |
| 441 | new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 442 | BranchInst::Create(RsltBlk, Reverse, Cond, Reverse); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 443 | |
| 444 | // Finally, in the result block, select one of the two results with a PHI |
| 445 | // node and return the result; |
| 446 | CurBB = RsltBlk; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 447 | PHINode *BitSelect = PHINode::Create(Val->getType(), "part_select", CurBB); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 448 | BitSelect->reserveOperandSpace(2); |
| 449 | BitSelect->addIncoming(FRes, Compute); |
| 450 | BitSelect->addIncoming(NewRes, Reverse); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 451 | ReturnInst::Create(BitSelect, CurBB); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | // Return a call to the implementation function |
Reid Spencer | 5156f5b | 2007-05-12 11:07:40 +0000 | [diff] [blame] | 455 | Value *Args[] = { |
| 456 | CI->getOperand(1), |
| 457 | CI->getOperand(2), |
| 458 | CI->getOperand(3) |
| 459 | }; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 460 | return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 463 | /// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes |
| 464 | /// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High) |
| 465 | /// The first two arguments can be any bit width. The result is the same width |
| 466 | /// as %Value. The operation replaces bits between %Low and %High with the value |
| 467 | /// in %Replacement. If %Replacement is not the same width, it is truncated or |
| 468 | /// zero extended as appropriate to fit the bits being replaced. If %Low is |
| 469 | /// greater than %High then the inverse set of bits are replaced. |
| 470 | /// @brief Lowering of llvm.bit.part.set intrinsic. |
| 471 | static Instruction *LowerPartSet(CallInst *CI) { |
| 472 | // Make sure we're dealing with a part select intrinsic here |
| 473 | Function *F = CI->getCalledFunction(); |
| 474 | const FunctionType *FT = F->getFunctionType(); |
| 475 | if (!F->isDeclaration() || !FT->getReturnType()->isInteger() || |
| 476 | FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() || |
| 477 | !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() || |
| 478 | !FT->getParamType(3)->isInteger()) |
| 479 | return CI; |
| 480 | |
| 481 | // Get the intrinsic implementation function by converting all the . to _ |
| 482 | // in the intrinsic's function name and then reconstructing the function |
| 483 | // declaration. |
| 484 | std::string Name(F->getName()); |
| 485 | for (unsigned i = 4; i < Name.length(); ++i) |
| 486 | if (Name[i] == '.') |
| 487 | Name[i] = '_'; |
| 488 | Module* M = F->getParent(); |
| 489 | F = cast<Function>(M->getOrInsertFunction(Name, FT)); |
Reid Spencer | df41353 | 2007-04-12 21:53:38 +0000 | [diff] [blame] | 490 | F->setLinkage(GlobalValue::WeakLinkage); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 491 | |
| 492 | // If we haven't defined the impl function yet, do so now |
| 493 | if (F->isDeclaration()) { |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 494 | // Get the arguments for the function. |
| 495 | Function::arg_iterator args = F->arg_begin(); |
| 496 | Value* Val = args++; Val->setName("Val"); |
| 497 | Value* Rep = args++; Rep->setName("Rep"); |
| 498 | Value* Lo = args++; Lo->setName("Lo"); |
| 499 | Value* Hi = args++; Hi->setName("Hi"); |
| 500 | |
| 501 | // Get some types we need |
| 502 | const IntegerType* ValTy = cast<IntegerType>(Val->getType()); |
| 503 | const IntegerType* RepTy = cast<IntegerType>(Rep->getType()); |
| 504 | uint32_t ValBits = ValTy->getBitWidth(); |
| 505 | uint32_t RepBits = RepTy->getBitWidth(); |
| 506 | |
| 507 | // Constant Definitions |
| 508 | ConstantInt* RepBitWidth = ConstantInt::get(Type::Int32Ty, RepBits); |
| 509 | ConstantInt* RepMask = ConstantInt::getAllOnesValue(RepTy); |
| 510 | ConstantInt* ValMask = ConstantInt::getAllOnesValue(ValTy); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 511 | ConstantInt* One = ConstantInt::get(Type::Int32Ty, 1); |
| 512 | ConstantInt* ValOne = ConstantInt::get(ValTy, 1); |
| 513 | ConstantInt* Zero = ConstantInt::get(Type::Int32Ty, 0); |
| 514 | ConstantInt* ValZero = ConstantInt::get(ValTy, 0); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 515 | |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 516 | // Basic blocks we fill in below. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 517 | BasicBlock* entry = BasicBlock::Create("entry", F, 0); |
| 518 | BasicBlock* large = BasicBlock::Create("large", F, 0); |
| 519 | BasicBlock* small = BasicBlock::Create("small", F, 0); |
| 520 | BasicBlock* reverse = BasicBlock::Create("reverse", F, 0); |
| 521 | BasicBlock* result = BasicBlock::Create("result", F, 0); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 522 | |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 523 | // BASIC BLOCK: entry |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 524 | // First, get the number of bits that we're placing as an i32 |
| 525 | ICmpInst* is_forward = |
| 526 | new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 527 | SelectInst* Hi_pn = SelectInst::Create(is_forward, Hi, Lo, "", entry); |
| 528 | SelectInst* Lo_pn = SelectInst::Create(is_forward, Lo, Hi, "", entry); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 529 | BinaryOperator* NumBits = BinaryOperator::CreateSub(Hi_pn, Lo_pn, "",entry); |
| 530 | NumBits = BinaryOperator::CreateAdd(NumBits, One, "", entry); |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 531 | // Now, convert Lo and Hi to ValTy bit width |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 532 | if (ValBits > 32) { |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 533 | Lo = new ZExtInst(Lo_pn, ValTy, "", entry); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 534 | } else if (ValBits < 32) { |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 535 | Lo = new TruncInst(Lo_pn, ValTy, "", entry); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 536 | } |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 537 | // Determine if the replacement bits are larger than the number of bits we |
| 538 | // are replacing and deal with it. |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 539 | ICmpInst* is_large = |
| 540 | new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 541 | BranchInst::Create(large, small, is_large, entry); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 542 | |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 543 | // BASIC BLOCK: large |
Reid Spencer | 9a9203b | 2007-04-16 22:21:14 +0000 | [diff] [blame] | 544 | Instruction* MaskBits = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 545 | BinaryOperator::CreateSub(RepBitWidth, NumBits, "", large); |
| 546 | MaskBits = CastInst::CreateIntegerCast(MaskBits, RepMask->getType(), |
Reid Spencer | 9a9203b | 2007-04-16 22:21:14 +0000 | [diff] [blame] | 547 | false, "", large); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 548 | BinaryOperator* Mask1 = |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 549 | BinaryOperator::CreateLShr(RepMask, MaskBits, "", large); |
| 550 | BinaryOperator* Rep2 = BinaryOperator::CreateAnd(Mask1, Rep, "", large); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 551 | BranchInst::Create(small, large); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 552 | |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 553 | // BASIC BLOCK: small |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 554 | PHINode* Rep3 = PHINode::Create(RepTy, "", small); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 555 | Rep3->reserveOperandSpace(2); |
Reid Spencer | eeedcb6 | 2007-04-12 13:30:14 +0000 | [diff] [blame] | 556 | Rep3->addIncoming(Rep2, large); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 557 | Rep3->addIncoming(Rep, entry); |
Reid Spencer | 3795809 | 2007-04-12 12:46:33 +0000 | [diff] [blame] | 558 | Value* Rep4 = Rep3; |
| 559 | if (ValBits > RepBits) |
| 560 | Rep4 = new ZExtInst(Rep3, ValTy, "", small); |
| 561 | else if (ValBits < RepBits) |
| 562 | Rep4 = new TruncInst(Rep3, ValTy, "", small); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 563 | BranchInst::Create(result, reverse, is_forward, small); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 564 | |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 565 | // BASIC BLOCK: reverse (reverses the bits of the replacement) |
| 566 | // Set up our loop counter as a PHI so we can decrement on each iteration. |
| 567 | // We will loop for the number of bits in the replacement value. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 568 | PHINode *Count = PHINode::Create(Type::Int32Ty, "count", reverse); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 569 | Count->reserveOperandSpace(2); |
| 570 | Count->addIncoming(NumBits, small); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 571 | |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 572 | // Get the value that we are shifting bits out of as a PHI because |
| 573 | // we'll change this with each iteration. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 574 | PHINode *BitsToShift = PHINode::Create(Val->getType(), "val", reverse); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 575 | BitsToShift->reserveOperandSpace(2); |
| 576 | BitsToShift->addIncoming(Rep4, small); |
| 577 | |
| 578 | // Get the result of the last computation or zero on first iteration |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 579 | PHINode *RRes = PHINode::Create(Val->getType(), "rres", reverse); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 580 | RRes->reserveOperandSpace(2); |
| 581 | RRes->addIncoming(ValZero, small); |
| 582 | |
| 583 | // Decrement the loop counter by one |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 584 | Instruction *Decr = BinaryOperator::CreateSub(Count, One, "", reverse); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 585 | Count->addIncoming(Decr, reverse); |
| 586 | |
| 587 | // Get the bit that we want to move into the result |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 588 | Value *Bit = BinaryOperator::CreateAnd(BitsToShift, ValOne, "", reverse); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 589 | |
| 590 | // Compute the new value of the bits to shift for the next iteration. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 591 | Value *NewVal = BinaryOperator::CreateLShr(BitsToShift, ValOne,"", reverse); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 592 | BitsToShift->addIncoming(NewVal, reverse); |
| 593 | |
| 594 | // Shift the bit we extracted into the low bit of the result. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 595 | Instruction *NewRes = BinaryOperator::CreateShl(RRes, ValOne, "", reverse); |
| 596 | NewRes = BinaryOperator::CreateOr(NewRes, Bit, "", reverse); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 597 | RRes->addIncoming(NewRes, reverse); |
| 598 | |
| 599 | // Terminate loop if we've moved all the bits. |
| 600 | ICmpInst *Cond = new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "", reverse); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 601 | BranchInst::Create(result, reverse, Cond, reverse); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 602 | |
| 603 | // BASIC BLOCK: result |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 604 | PHINode *Rplcmnt = PHINode::Create(Val->getType(), "", result); |
Reid Spencer | 76c94b6 | 2007-05-15 02:26:52 +0000 | [diff] [blame] | 605 | Rplcmnt->reserveOperandSpace(2); |
| 606 | Rplcmnt->addIncoming(NewRes, reverse); |
| 607 | Rplcmnt->addIncoming(Rep4, small); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 608 | Value* t0 = CastInst::CreateIntegerCast(NumBits,ValTy,false,"",result); |
| 609 | Value* t1 = BinaryOperator::CreateShl(ValMask, Lo, "", result); |
| 610 | Value* t2 = BinaryOperator::CreateNot(t1, "", result); |
| 611 | Value* t3 = BinaryOperator::CreateShl(t1, t0, "", result); |
| 612 | Value* t4 = BinaryOperator::CreateOr(t2, t3, "", result); |
| 613 | Value* t5 = BinaryOperator::CreateAnd(t4, Val, "", result); |
| 614 | Value* t6 = BinaryOperator::CreateShl(Rplcmnt, Lo, "", result); |
| 615 | Value* Rslt = BinaryOperator::CreateOr(t5, t6, "part_set", result); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 616 | ReturnInst::Create(Rslt, result); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // Return a call to the implementation function |
Reid Spencer | 5156f5b | 2007-05-12 11:07:40 +0000 | [diff] [blame] | 620 | Value *Args[] = { |
| 621 | CI->getOperand(1), |
| 622 | CI->getOperand(2), |
| 623 | CI->getOperand(3), |
| 624 | CI->getOperand(4) |
| 625 | }; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 626 | return CallInst::Create(F, Args, array_endof(Args), CI->getName(), CI); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 629 | static void ReplaceFPIntrinsicWithCall(CallInst *CI, Constant *FCache, |
| 630 | Constant *DCache, Constant *LDCache, |
| 631 | const char *Fname, const char *Dname, |
| 632 | const char *LDname) { |
| 633 | switch (CI->getOperand(1)->getType()->getTypeID()) { |
| 634 | default: assert(0 && "Invalid type in intrinsic"); abort(); |
| 635 | case Type::FloatTyID: |
| 636 | ReplaceCallWith(Fname, CI, CI->op_begin()+1, CI->op_end(), |
| 637 | Type::FloatTy, FCache); |
| 638 | break; |
| 639 | case Type::DoubleTyID: |
| 640 | ReplaceCallWith(Dname, CI, CI->op_begin()+1, CI->op_end(), |
| 641 | Type::DoubleTy, DCache); |
| 642 | break; |
| 643 | case Type::X86_FP80TyID: |
| 644 | case Type::FP128TyID: |
| 645 | case Type::PPC_FP128TyID: |
| 646 | ReplaceCallWith(LDname, CI, CI->op_begin()+1, CI->op_end(), |
| 647 | CI->getOperand(1)->getType(), LDCache); |
| 648 | break; |
| 649 | } |
| 650 | } |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 651 | |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 652 | void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 653 | Function *Callee = CI->getCalledFunction(); |
| 654 | assert(Callee && "Cannot lower an indirect call!"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 656 | switch (Callee->getIntrinsicID()) { |
| 657 | case Intrinsic::not_intrinsic: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 658 | cerr << "Cannot lower a call to a non-intrinsic function '" |
| 659 | << Callee->getName() << "'!\n"; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 660 | abort(); |
| 661 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 662 | cerr << "Error: Code generator does not support intrinsic function '" |
| 663 | << Callee->getName() << "'!\n"; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 664 | abort(); |
| 665 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 666 | // The setjmp/longjmp intrinsics should only exist in the code if it was |
| 667 | // never optimized (ie, right out of the CFE), or if it has been hacked on |
| 668 | // by the lowerinvoke pass. In both cases, the right thing to do is to |
| 669 | // convert the call to an explicit setjmp or longjmp call. |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 670 | case Intrinsic::setjmp: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 671 | static Constant *SetjmpFCache = 0; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 672 | Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 673 | Type::Int32Ty, SetjmpFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 674 | if (CI->getType() != Type::VoidTy) |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 675 | CI->replaceAllUsesWith(V); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 676 | break; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 677 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 678 | case Intrinsic::sigsetjmp: |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 679 | if (CI->getType() != Type::VoidTy) |
| 680 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 681 | break; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 682 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 683 | case Intrinsic::longjmp: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 684 | static Constant *LongjmpFCache = 0; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 685 | ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 686 | Type::VoidTy, LongjmpFCache); |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 687 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 689 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 690 | case Intrinsic::siglongjmp: { |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 691 | // Insert the call to abort |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 692 | static Constant *AbortFCache = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 693 | ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 694 | Type::VoidTy, AbortFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 695 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 696 | } |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 697 | case Intrinsic::ctpop: |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 698 | CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI)); |
| 699 | break; |
| 700 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 701 | case Intrinsic::bswap: |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 702 | CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI)); |
| 703 | break; |
| 704 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 705 | case Intrinsic::ctlz: |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 706 | CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 707 | break; |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 708 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 709 | case Intrinsic::cttz: { |
Chris Lattner | a801172 | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 710 | // cttz(x) -> ctpop(~X & (X-1)) |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 711 | Value *Src = CI->getOperand(1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 712 | Value *NotSrc = BinaryOperator::CreateNot(Src, Src->getName()+".not", CI); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 713 | Value *SrcM1 = ConstantInt::get(Src->getType(), 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 714 | SrcM1 = BinaryOperator::CreateSub(Src, SrcM1, "", CI); |
| 715 | Src = LowerCTPOP(BinaryOperator::CreateAnd(NotSrc, SrcM1, "", CI), CI); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 716 | CI->replaceAllUsesWith(Src); |
| 717 | break; |
| 718 | } |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 719 | |
Chris Lattner | c6eb6d7 | 2007-04-10 03:20:39 +0000 | [diff] [blame] | 720 | case Intrinsic::part_select: |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 721 | CI->replaceAllUsesWith(LowerPartSelect(CI)); |
| 722 | break; |
| 723 | |
| 724 | case Intrinsic::part_set: |
| 725 | CI->replaceAllUsesWith(LowerPartSet(CI)); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 726 | break; |
| 727 | |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 728 | case Intrinsic::stacksave: |
| 729 | case Intrinsic::stackrestore: { |
| 730 | static bool Warned = false; |
| 731 | if (!Warned) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 732 | cerr << "WARNING: this target does not support the llvm.stack" |
| 733 | << (Callee->getIntrinsicID() == Intrinsic::stacksave ? |
| 734 | "save" : "restore") << " intrinsic.\n"; |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 735 | Warned = true; |
| 736 | if (Callee->getIntrinsicID() == Intrinsic::stacksave) |
| 737 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 738 | break; |
| 739 | } |
| 740 | |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 741 | case Intrinsic::returnaddress: |
| 742 | case Intrinsic::frameaddress: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 743 | cerr << "WARNING: this target does not support the llvm." |
| 744 | << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? |
| 745 | "return" : "frame") << "address intrinsic.\n"; |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 746 | CI->replaceAllUsesWith(ConstantPointerNull::get( |
| 747 | cast<PointerType>(CI->getType()))); |
| 748 | break; |
| 749 | |
Chris Lattner | 0942b7c | 2005-02-28 19:27:23 +0000 | [diff] [blame] | 750 | case Intrinsic::prefetch: |
| 751 | break; // Simply strip out prefetches on unsupported architectures |
| 752 | |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 753 | case Intrinsic::pcmarker: |
| 754 | break; // Simply strip out pcmarker on unsupported architectures |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 755 | case Intrinsic::readcyclecounter: { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 756 | cerr << "WARNING: this target does not support the llvm.readcyclecoun" |
| 757 | << "ter intrinsic. It is being lowered to a constant 0\n"; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 758 | CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 759 | break; |
| 760 | } |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 762 | case Intrinsic::dbg_stoppoint: |
| 763 | case Intrinsic::dbg_region_start: |
| 764 | case Intrinsic::dbg_region_end: |
| 765 | case Intrinsic::dbg_func_start: |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 766 | case Intrinsic::dbg_declare: |
Duncan Sands | f664e41 | 2007-07-06 14:46:23 +0000 | [diff] [blame] | 767 | break; // Simply strip out debugging intrinsics |
| 768 | |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 769 | case Intrinsic::eh_exception: |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 770 | case Intrinsic::eh_selector_i32: |
| 771 | case Intrinsic::eh_selector_i64: |
Duncan Sands | f664e41 | 2007-07-06 14:46:23 +0000 | [diff] [blame] | 772 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 773 | break; |
| 774 | |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 775 | case Intrinsic::eh_typeid_for_i32: |
| 776 | case Intrinsic::eh_typeid_for_i64: |
Duncan Sands | f664e41 | 2007-07-06 14:46:23 +0000 | [diff] [blame] | 777 | // Return something different to eh_selector. |
| 778 | CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1)); |
| 779 | break; |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 780 | |
Tanya Lattner | 24e5aad | 2007-06-15 22:26:58 +0000 | [diff] [blame] | 781 | case Intrinsic::var_annotation: |
| 782 | break; // Strip out annotate intrinsic |
| 783 | |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 784 | case Intrinsic::memcpy: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 785 | static Constant *MemcpyFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 786 | Value *Size = CI->getOperand(3); |
| 787 | const Type *IntPtr = TD.getIntPtrType(); |
| 788 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 789 | IntPtr->getPrimitiveSizeInBits()) |
| 790 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 791 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 792 | IntPtr->getPrimitiveSizeInBits()) |
| 793 | Size = new TruncInst(Size, IntPtr, "", CI); |
| 794 | Value *Ops[3]; |
| 795 | Ops[0] = CI->getOperand(1); |
| 796 | Ops[1] = CI->getOperand(2); |
| 797 | Ops[2] = Size; |
| 798 | ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 799 | MemcpyFCache); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 800 | break; |
| 801 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 802 | case Intrinsic::memmove: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 803 | static Constant *MemmoveFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 804 | Value *Size = CI->getOperand(3); |
| 805 | const Type *IntPtr = TD.getIntPtrType(); |
| 806 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 807 | IntPtr->getPrimitiveSizeInBits()) |
| 808 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 809 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 810 | IntPtr->getPrimitiveSizeInBits()) |
| 811 | Size = new TruncInst(Size, IntPtr, "", CI); |
| 812 | Value *Ops[3]; |
| 813 | Ops[0] = CI->getOperand(1); |
| 814 | Ops[1] = CI->getOperand(2); |
| 815 | Ops[2] = Size; |
| 816 | ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 817 | MemmoveFCache); |
Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 818 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 819 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 820 | case Intrinsic::memset: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 821 | static Constant *MemsetFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 822 | Value *Size = CI->getOperand(3); |
Chris Lattner | 7d6f77d | 2007-02-06 06:07:51 +0000 | [diff] [blame] | 823 | const Type *IntPtr = TD.getIntPtrType(); |
| 824 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 825 | IntPtr->getPrimitiveSizeInBits()) |
| 826 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 827 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 828 | IntPtr->getPrimitiveSizeInBits()) |
| 829 | Size = new TruncInst(Size, IntPtr, "", CI); |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 830 | Value *Ops[3]; |
| 831 | Ops[0] = CI->getOperand(1); |
| 832 | // Extend the amount to i32. |
| 833 | Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI); |
| 834 | Ops[2] = Size; |
| 835 | ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 836 | MemsetFCache); |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 837 | break; |
| 838 | } |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 839 | case Intrinsic::sqrt: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 840 | static Constant *sqrtFCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 841 | static Constant *sqrtDCache = 0; |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 842 | static Constant *sqrtLDCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 843 | ReplaceFPIntrinsicWithCall(CI, sqrtFCache, sqrtDCache, sqrtLDCache, |
| 844 | "sqrtf", "sqrt", "sqrtl"); |
Dale Johannesen | 4292d1c | 2007-09-28 18:06:58 +0000 | [diff] [blame] | 845 | break; |
| 846 | } |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 847 | case Intrinsic::log: { |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 848 | static Constant *logFCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 849 | static Constant *logDCache = 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 850 | static Constant *logLDCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 851 | ReplaceFPIntrinsicWithCall(CI, logFCache, logDCache, logLDCache, |
| 852 | "logf", "log", "logl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | case Intrinsic::log2: { |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 856 | static Constant *log2FCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 857 | static Constant *log2DCache = 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 858 | static Constant *log2LDCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 859 | ReplaceFPIntrinsicWithCall(CI, log2FCache, log2DCache, log2LDCache, |
| 860 | "log2f", "log2", "log2l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 861 | break; |
| 862 | } |
| 863 | case Intrinsic::log10: { |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 864 | static Constant *log10FCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 865 | static Constant *log10DCache = 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 866 | static Constant *log10LDCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 867 | ReplaceFPIntrinsicWithCall(CI, log10FCache, log10DCache, log10LDCache, |
| 868 | "log10f", "log10", "log10l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 869 | break; |
| 870 | } |
| 871 | case Intrinsic::exp: { |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 872 | static Constant *expFCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 873 | static Constant *expDCache = 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 874 | static Constant *expLDCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 875 | ReplaceFPIntrinsicWithCall(CI, expFCache, expDCache, expLDCache, |
| 876 | "expf", "exp", "expl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | case Intrinsic::exp2: { |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 880 | static Constant *exp2FCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 881 | static Constant *exp2DCache = 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 882 | static Constant *exp2LDCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 883 | ReplaceFPIntrinsicWithCall(CI, exp2FCache, exp2DCache, exp2LDCache, |
| 884 | "exp2f", "exp2", "exp2l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 885 | break; |
| 886 | } |
| 887 | case Intrinsic::pow: { |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 888 | static Constant *powFCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 889 | static Constant *powDCache = 0; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 890 | static Constant *powLDCache = 0; |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 891 | ReplaceFPIntrinsicWithCall(CI, powFCache, powDCache, powLDCache, |
| 892 | "powf", "pow", "powl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 893 | break; |
| 894 | } |
Anton Korobeynikov | 917c2a6 | 2007-11-15 23:25:33 +0000 | [diff] [blame] | 895 | case Intrinsic::flt_rounds: |
| 896 | // Lower to "round to the nearest" |
| 897 | if (CI->getType() != Type::VoidTy) |
| 898 | CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1)); |
| 899 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 900 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 901 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 902 | assert(CI->use_empty() && |
| 903 | "Lowering should have eliminated any uses of the intrinsic call!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 904 | CI->eraseFromParent(); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 905 | } |