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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
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 | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 24 | template <class ArgIt> |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 25 | static void EnsureFunctionExists(Module &M, const char *Name, |
| 26 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 27 | const Type *RetTy) { |
| 28 | // Insert a correctly-typed definition now. |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 29 | std::vector<const Type *> ParamTys; |
| 30 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 31 | ParamTys.push_back(I->getType()); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 32 | M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 35 | /// ReplaceCallWith - This function is used when we want to lower an intrinsic |
| 36 | /// call to a call of an external function. This handles hard cases such as |
| 37 | /// when there was already a prototype for the external function, and if that |
| 38 | /// prototype doesn't match the arguments we expect to pass in. |
| 39 | template <class ArgIt> |
| 40 | static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 41 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 42 | const Type *RetTy, Constant *&FCache) { |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 43 | if (!FCache) { |
| 44 | // If we haven't already looked up this function, check to see if the |
| 45 | // program already contains a function with this name. |
| 46 | Module *M = CI->getParent()->getParent()->getParent(); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 47 | // Get or insert the definition now. |
| 48 | std::vector<const Type *> ParamTys; |
| 49 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 50 | ParamTys.push_back((*I)->getType()); |
| 51 | FCache = M->getOrInsertFunction(NewFn, |
| 52 | FunctionType::get(RetTy, ParamTys, false)); |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 53 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 54 | |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 55 | std::vector<Value*> Operands(ArgBegin, ArgEnd); |
| 56 | CallInst *NewCI = new CallInst(FCache, Operands, CI->getName(), CI); |
| 57 | if (!CI->use_empty()) |
| 58 | CI->replaceAllUsesWith(NewCI); |
Chris Lattner | 02348ca | 2004-06-11 02:54:02 +0000 | [diff] [blame] | 59 | return NewCI; |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 62 | void IntrinsicLowering::AddPrototypes(Module &M) { |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 63 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 64 | if (I->isDeclaration() && !I->use_empty()) |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 65 | switch (I->getIntrinsicID()) { |
| 66 | default: break; |
| 67 | case Intrinsic::setjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 68 | EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 69 | Type::Int32Ty); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 70 | break; |
| 71 | case Intrinsic::longjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 72 | EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(), |
| 73 | Type::VoidTy); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 74 | break; |
| 75 | case Intrinsic::siglongjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 76 | EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(), |
| 77 | Type::VoidTy); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 78 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 79 | case Intrinsic::memcpy_i32: |
| 80 | case Intrinsic::memcpy_i64: |
Reid Spencer | 1e9126b | 2007-01-28 22:28:00 +0000 | [diff] [blame] | 81 | M.getOrInsertFunction("memcpy", PointerType::get(Type::Int8Ty), |
| 82 | PointerType::get(Type::Int8Ty), |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 83 | PointerType::get(Type::Int8Ty), |
| 84 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 85 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 86 | case Intrinsic::memmove_i32: |
| 87 | case Intrinsic::memmove_i64: |
Reid Spencer | 1e9126b | 2007-01-28 22:28:00 +0000 | [diff] [blame] | 88 | M.getOrInsertFunction("memmove", PointerType::get(Type::Int8Ty), |
| 89 | PointerType::get(Type::Int8Ty), |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 90 | PointerType::get(Type::Int8Ty), |
| 91 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 92 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 93 | case Intrinsic::memset_i32: |
| 94 | case Intrinsic::memset_i64: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 95 | M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty), |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 96 | PointerType::get(Type::Int8Ty), Type::Int32Ty, |
| 97 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 98 | break; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 99 | case Intrinsic::sqrt_f32: |
| 100 | case Intrinsic::sqrt_f64: |
Alkis Evlogimenos | b1beff0 | 2005-04-30 07:13:31 +0000 | [diff] [blame] | 101 | if(I->arg_begin()->getType() == Type::FloatTy) |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 102 | EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(), |
| 103 | Type::FloatTy); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 104 | else |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 105 | EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(), |
| 106 | Type::DoubleTy); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 107 | break; |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 108 | } |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 109 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 110 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 111 | /// LowerBSWAP - Emit the code to lower bswap of V before the specified |
| 112 | /// instruction IP. |
| 113 | static Value *LowerBSWAP(Value *V, Instruction *IP) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 114 | assert(V->getType()->isInteger() && "Can't bswap a non-integer type!"); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 115 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 116 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 117 | |
| 118 | switch(BitSize) { |
| 119 | default: assert(0 && "Unhandled type size of value to byteswap!"); |
| 120 | case 16: { |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 121 | Value *Tmp1 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 122 | ConstantInt::get(V->getType(),8),"bswap.2",IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 123 | Value *Tmp2 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 124 | ConstantInt::get(V->getType(),8),"bswap.1",IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 125 | V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP); |
| 126 | break; |
| 127 | } |
| 128 | case 32: { |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 129 | Value *Tmp4 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 130 | ConstantInt::get(V->getType(),24),"bswap.4", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 131 | Value *Tmp3 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 132 | ConstantInt::get(V->getType(),8),"bswap.3",IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 133 | Value *Tmp2 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 134 | ConstantInt::get(V->getType(),8),"bswap.2",IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 135 | Value *Tmp1 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 136 | ConstantInt::get(V->getType(),24),"bswap.1", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 137 | Tmp3 = BinaryOperator::createAnd(Tmp3, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 138 | ConstantInt::get(Type::Int32Ty, 0xFF0000), |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 139 | "bswap.and3", IP); |
| 140 | Tmp2 = BinaryOperator::createAnd(Tmp2, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 141 | ConstantInt::get(Type::Int32Ty, 0xFF00), |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 142 | "bswap.and2", IP); |
| 143 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP); |
| 144 | Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP); |
| 145 | V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP); |
| 146 | break; |
| 147 | } |
| 148 | case 64: { |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 149 | Value *Tmp8 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 150 | ConstantInt::get(V->getType(),56),"bswap.8", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 151 | Value *Tmp7 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 152 | ConstantInt::get(V->getType(),40),"bswap.7", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 153 | Value *Tmp6 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 154 | ConstantInt::get(V->getType(),24),"bswap.6", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 155 | Value *Tmp5 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 156 | ConstantInt::get(V->getType(),8),"bswap.5", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 157 | Value* Tmp4 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 158 | ConstantInt::get(V->getType(),8),"bswap.4", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 159 | Value* Tmp3 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 160 | ConstantInt::get(V->getType(),24),"bswap.3", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +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(),40),"bswap.2", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 163 | Value* Tmp1 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 164 | ConstantInt::get(V->getType(),56),"bswap.1", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 165 | Tmp7 = BinaryOperator::createAnd(Tmp7, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 166 | ConstantInt::get(Type::Int64Ty, |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 167 | 0xFF000000000000ULL), |
| 168 | "bswap.and7", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 169 | Tmp6 = BinaryOperator::createAnd(Tmp6, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 170 | ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 171 | "bswap.and6", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 172 | Tmp5 = BinaryOperator::createAnd(Tmp5, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 173 | ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 174 | "bswap.and5", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 175 | Tmp4 = BinaryOperator::createAnd(Tmp4, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 176 | ConstantInt::get(Type::Int64Ty, 0xFF000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 177 | "bswap.and4", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 178 | Tmp3 = BinaryOperator::createAnd(Tmp3, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 179 | ConstantInt::get(Type::Int64Ty, 0xFF0000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 180 | "bswap.and3", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 181 | Tmp2 = BinaryOperator::createAnd(Tmp2, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 182 | ConstantInt::get(Type::Int64Ty, 0xFF00ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 183 | "bswap.and2", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 184 | Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP); |
| 185 | Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP); |
| 186 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP); |
| 187 | Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP); |
| 188 | Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP); |
| 189 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP); |
| 190 | V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP); |
| 191 | break; |
| 192 | } |
| 193 | } |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 194 | return V; |
| 195 | } |
| 196 | |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 197 | /// LowerCTPOP - Emit the code to lower ctpop of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 198 | /// instruction IP. |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 199 | static Value *LowerCTPOP(Value *V, Instruction *IP) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 200 | assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 201 | |
| 202 | static const uint64_t MaskValues[6] = { |
| 203 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 204 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 205 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 206 | }; |
| 207 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 208 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 210 | for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) { |
Chris Lattner | 3bb7e3f | 2006-12-12 05:19:46 +0000 | [diff] [blame] | 211 | Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 212 | Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 213 | Value *VShift = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 214 | ConstantInt::get(V->getType(), i), "ctpop.sh", IP); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 215 | Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP); |
| 216 | V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP); |
| 217 | } |
| 218 | |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 219 | return V; |
| 220 | } |
| 221 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 222 | /// LowerCTLZ - Emit the code to lower ctlz of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 223 | /// instruction IP. |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 224 | static Value *LowerCTLZ(Value *V, Instruction *IP) { |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 225 | |
| 226 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 227 | for (unsigned i = 1; i != BitSize; i <<= 1) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 228 | Value *ShVal = ConstantInt::get(V->getType(), i); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 229 | ShVal = BinaryOperator::createLShr(V, ShVal, "ctlz.sh", IP); |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 230 | V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP); |
| 231 | } |
| 232 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 233 | V = BinaryOperator::createNot(V, "", IP); |
| 234 | return LowerCTPOP(V, IP); |
| 235 | } |
| 236 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 237 | |
| 238 | |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 239 | void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 240 | Function *Callee = CI->getCalledFunction(); |
| 241 | assert(Callee && "Cannot lower an indirect call!"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 243 | switch (Callee->getIntrinsicID()) { |
| 244 | case Intrinsic::not_intrinsic: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 245 | cerr << "Cannot lower a call to a non-intrinsic function '" |
| 246 | << Callee->getName() << "'!\n"; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 247 | abort(); |
| 248 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 249 | cerr << "Error: Code generator does not support intrinsic function '" |
| 250 | << Callee->getName() << "'!\n"; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 251 | abort(); |
| 252 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 253 | // The setjmp/longjmp intrinsics should only exist in the code if it was |
| 254 | // never optimized (ie, right out of the CFE), or if it has been hacked on |
| 255 | // by the lowerinvoke pass. In both cases, the right thing to do is to |
| 256 | // convert the call to an explicit setjmp or longjmp call. |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 257 | case Intrinsic::setjmp: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 258 | static Constant *SetjmpFCache = 0; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 259 | Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 260 | Type::Int32Ty, SetjmpFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 261 | if (CI->getType() != Type::VoidTy) |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 262 | CI->replaceAllUsesWith(V); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 263 | break; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 264 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 265 | case Intrinsic::sigsetjmp: |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 266 | if (CI->getType() != Type::VoidTy) |
| 267 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 268 | break; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 269 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 270 | case Intrinsic::longjmp: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 271 | static Constant *LongjmpFCache = 0; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 272 | ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 273 | Type::VoidTy, LongjmpFCache); |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 274 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 275 | } |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 276 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 277 | case Intrinsic::siglongjmp: { |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 278 | // Insert the call to abort |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 279 | static Constant *AbortFCache = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 280 | ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 281 | Type::VoidTy, AbortFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 282 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 283 | } |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 284 | case Intrinsic::ctpop_i8: |
| 285 | case Intrinsic::ctpop_i16: |
| 286 | case Intrinsic::ctpop_i32: |
| 287 | case Intrinsic::ctpop_i64: |
| 288 | CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI)); |
| 289 | break; |
| 290 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 291 | case Intrinsic::bswap_i16: |
| 292 | case Intrinsic::bswap_i32: |
| 293 | case Intrinsic::bswap_i64: |
| 294 | CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI)); |
| 295 | break; |
| 296 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 297 | case Intrinsic::ctlz_i8: |
| 298 | case Intrinsic::ctlz_i16: |
| 299 | case Intrinsic::ctlz_i32: |
| 300 | case Intrinsic::ctlz_i64: |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 301 | CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 302 | break; |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 303 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 304 | case Intrinsic::cttz_i8: |
| 305 | case Intrinsic::cttz_i16: |
| 306 | case Intrinsic::cttz_i32: |
| 307 | case Intrinsic::cttz_i64: { |
Chris Lattner | a801172 | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 308 | // cttz(x) -> ctpop(~X & (X-1)) |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 309 | Value *Src = CI->getOperand(1); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 310 | Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI); |
Chris Lattner | a801172 | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 311 | Value *SrcM1 = ConstantInt::get(Src->getType(), 1); |
| 312 | SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI); |
| 313 | Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 314 | CI->replaceAllUsesWith(Src); |
| 315 | break; |
| 316 | } |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 317 | |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 318 | case Intrinsic::stacksave: |
| 319 | case Intrinsic::stackrestore: { |
| 320 | static bool Warned = false; |
| 321 | if (!Warned) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 322 | cerr << "WARNING: this target does not support the llvm.stack" |
| 323 | << (Callee->getIntrinsicID() == Intrinsic::stacksave ? |
| 324 | "save" : "restore") << " intrinsic.\n"; |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 325 | Warned = true; |
| 326 | if (Callee->getIntrinsicID() == Intrinsic::stacksave) |
| 327 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 328 | break; |
| 329 | } |
| 330 | |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 331 | case Intrinsic::returnaddress: |
| 332 | case Intrinsic::frameaddress: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 333 | cerr << "WARNING: this target does not support the llvm." |
| 334 | << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? |
| 335 | "return" : "frame") << "address intrinsic.\n"; |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 336 | CI->replaceAllUsesWith(ConstantPointerNull::get( |
| 337 | cast<PointerType>(CI->getType()))); |
| 338 | break; |
| 339 | |
Chris Lattner | 0942b7c | 2005-02-28 19:27:23 +0000 | [diff] [blame] | 340 | case Intrinsic::prefetch: |
| 341 | break; // Simply strip out prefetches on unsupported architectures |
| 342 | |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 343 | case Intrinsic::pcmarker: |
| 344 | break; // Simply strip out pcmarker on unsupported architectures |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 345 | case Intrinsic::readcyclecounter: { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 346 | cerr << "WARNING: this target does not support the llvm.readcyclecoun" |
| 347 | << "ter intrinsic. It is being lowered to a constant 0\n"; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 348 | CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 349 | break; |
| 350 | } |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 352 | case Intrinsic::dbg_stoppoint: |
| 353 | case Intrinsic::dbg_region_start: |
| 354 | case Intrinsic::dbg_region_end: |
| 355 | case Intrinsic::dbg_func_start: |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 356 | case Intrinsic::dbg_declare: |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 357 | break; // Simply strip out debugging intrinsics |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 358 | |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 359 | case Intrinsic::memcpy_i32: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 360 | case Intrinsic::memcpy_i64: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 361 | static Constant *MemcpyFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 362 | Value *Size = CI->getOperand(3); |
| 363 | const Type *IntPtr = TD.getIntPtrType(); |
| 364 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 365 | IntPtr->getPrimitiveSizeInBits()) |
| 366 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 367 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 368 | IntPtr->getPrimitiveSizeInBits()) |
| 369 | Size = new TruncInst(Size, IntPtr, "", CI); |
| 370 | Value *Ops[3]; |
| 371 | Ops[0] = CI->getOperand(1); |
| 372 | Ops[1] = CI->getOperand(2); |
| 373 | Ops[2] = Size; |
| 374 | ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 375 | MemcpyFCache); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 376 | break; |
| 377 | } |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 378 | case Intrinsic::memmove_i32: |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 379 | case Intrinsic::memmove_i64: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 380 | static Constant *MemmoveFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 381 | Value *Size = CI->getOperand(3); |
| 382 | const Type *IntPtr = TD.getIntPtrType(); |
| 383 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 384 | IntPtr->getPrimitiveSizeInBits()) |
| 385 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 386 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 387 | IntPtr->getPrimitiveSizeInBits()) |
| 388 | Size = new TruncInst(Size, IntPtr, "", CI); |
| 389 | Value *Ops[3]; |
| 390 | Ops[0] = CI->getOperand(1); |
| 391 | Ops[1] = CI->getOperand(2); |
| 392 | Ops[2] = Size; |
| 393 | ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 394 | MemmoveFCache); |
Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 395 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 396 | } |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 397 | case Intrinsic::memset_i32: |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 398 | case Intrinsic::memset_i64: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 399 | static Constant *MemsetFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 400 | Value *Size = CI->getOperand(3); |
Chris Lattner | 7d6f77d | 2007-02-06 06:07:51 +0000 | [diff] [blame] | 401 | const Type *IntPtr = TD.getIntPtrType(); |
| 402 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 403 | IntPtr->getPrimitiveSizeInBits()) |
| 404 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 405 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 406 | IntPtr->getPrimitiveSizeInBits()) |
| 407 | Size = new TruncInst(Size, IntPtr, "", CI); |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 408 | Value *Ops[3]; |
| 409 | Ops[0] = CI->getOperand(1); |
| 410 | // Extend the amount to i32. |
| 411 | Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI); |
| 412 | Ops[2] = Size; |
| 413 | ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 414 | MemsetFCache); |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 415 | break; |
| 416 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 417 | case Intrinsic::sqrt_f32: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 418 | static Constant *sqrtfFCache = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 419 | ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 420 | Type::FloatTy, sqrtfFCache); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 421 | break; |
| 422 | } |
| 423 | case Intrinsic::sqrt_f64: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 424 | static Constant *sqrtFCache = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 425 | ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 426 | Type::DoubleTy, sqrtFCache); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 427 | break; |
| 428 | } |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 429 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 430 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 431 | assert(CI->use_empty() && |
| 432 | "Lowering should have eliminated any uses of the intrinsic call!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 433 | CI->eraseFromParent(); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 434 | } |