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 | 990b849 | 2007-02-13 06:01:22 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 25 | template <class ArgIt> |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 26 | static void EnsureFunctionExists(Module &M, const char *Name, |
| 27 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 28 | const Type *RetTy) { |
| 29 | // Insert a correctly-typed definition now. |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 30 | std::vector<const Type *> ParamTys; |
| 31 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 32 | ParamTys.push_back(I->getType()); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 33 | M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 36 | /// ReplaceCallWith - This function is used when we want to lower an intrinsic |
| 37 | /// call to a call of an external function. This handles hard cases such as |
| 38 | /// when there was already a prototype for the external function, and if that |
| 39 | /// prototype doesn't match the arguments we expect to pass in. |
| 40 | template <class ArgIt> |
| 41 | static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 42 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 43 | const Type *RetTy, Constant *&FCache) { |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 44 | if (!FCache) { |
| 45 | // If we haven't already looked up this function, check to see if the |
| 46 | // program already contains a function with this name. |
| 47 | Module *M = CI->getParent()->getParent()->getParent(); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 48 | // Get or insert the definition now. |
| 49 | std::vector<const Type *> ParamTys; |
| 50 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 51 | ParamTys.push_back((*I)->getType()); |
| 52 | FCache = M->getOrInsertFunction(NewFn, |
| 53 | FunctionType::get(RetTy, ParamTys, false)); |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 54 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 990b849 | 2007-02-13 06:01:22 +0000 | [diff] [blame] | 56 | SmallVector<Value*, 8> Operands(ArgBegin, ArgEnd); |
| 57 | CallInst *NewCI = new CallInst(FCache, &Operands[0], Operands.size(), |
| 58 | CI->getName(), CI); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 59 | if (!CI->use_empty()) |
| 60 | CI->replaceAllUsesWith(NewCI); |
Chris Lattner | 02348ca | 2004-06-11 02:54:02 +0000 | [diff] [blame] | 61 | return NewCI; |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 64 | void IntrinsicLowering::AddPrototypes(Module &M) { |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 65 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 66 | if (I->isDeclaration() && !I->use_empty()) |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 67 | switch (I->getIntrinsicID()) { |
| 68 | default: break; |
| 69 | case Intrinsic::setjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 70 | EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(), |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 71 | Type::Int32Ty); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 72 | break; |
| 73 | case Intrinsic::longjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 74 | EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(), |
| 75 | Type::VoidTy); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 76 | break; |
| 77 | case Intrinsic::siglongjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 78 | EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(), |
| 79 | Type::VoidTy); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 80 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 81 | case Intrinsic::memcpy_i32: |
| 82 | case Intrinsic::memcpy_i64: |
Reid Spencer | 1e9126b | 2007-01-28 22:28:00 +0000 | [diff] [blame] | 83 | M.getOrInsertFunction("memcpy", PointerType::get(Type::Int8Ty), |
| 84 | PointerType::get(Type::Int8Ty), |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 85 | PointerType::get(Type::Int8Ty), |
| 86 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 87 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 88 | case Intrinsic::memmove_i32: |
| 89 | case Intrinsic::memmove_i64: |
Reid Spencer | 1e9126b | 2007-01-28 22:28:00 +0000 | [diff] [blame] | 90 | M.getOrInsertFunction("memmove", PointerType::get(Type::Int8Ty), |
| 91 | PointerType::get(Type::Int8Ty), |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 92 | PointerType::get(Type::Int8Ty), |
| 93 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 94 | break; |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 95 | case Intrinsic::memset_i32: |
| 96 | case Intrinsic::memset_i64: |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 97 | M.getOrInsertFunction("memset", PointerType::get(Type::Int8Ty), |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 98 | PointerType::get(Type::Int8Ty), Type::Int32Ty, |
| 99 | TD.getIntPtrType(), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 100 | break; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 101 | case Intrinsic::sqrt_f32: |
| 102 | case Intrinsic::sqrt_f64: |
Alkis Evlogimenos | b1beff0 | 2005-04-30 07:13:31 +0000 | [diff] [blame] | 103 | if(I->arg_begin()->getType() == Type::FloatTy) |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 104 | EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(), |
| 105 | Type::FloatTy); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 106 | else |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 107 | EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(), |
| 108 | Type::DoubleTy); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 109 | break; |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 112 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 113 | /// LowerBSWAP - Emit the code to lower bswap of V before the specified |
| 114 | /// instruction IP. |
| 115 | static Value *LowerBSWAP(Value *V, Instruction *IP) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 116 | assert(V->getType()->isInteger() && "Can't bswap a non-integer type!"); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 117 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 118 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 119 | |
| 120 | switch(BitSize) { |
| 121 | default: assert(0 && "Unhandled type size of value to byteswap!"); |
| 122 | case 16: { |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 123 | Value *Tmp1 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 124 | ConstantInt::get(V->getType(),8),"bswap.2",IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 125 | Value *Tmp2 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 126 | ConstantInt::get(V->getType(),8),"bswap.1",IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 127 | V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP); |
| 128 | break; |
| 129 | } |
| 130 | case 32: { |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 131 | Value *Tmp4 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 132 | ConstantInt::get(V->getType(),24),"bswap.4", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 133 | Value *Tmp3 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 134 | ConstantInt::get(V->getType(),8),"bswap.3",IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 135 | Value *Tmp2 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 136 | ConstantInt::get(V->getType(),8),"bswap.2",IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 137 | Value *Tmp1 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 138 | ConstantInt::get(V->getType(),24),"bswap.1", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 139 | Tmp3 = BinaryOperator::createAnd(Tmp3, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 140 | ConstantInt::get(Type::Int32Ty, 0xFF0000), |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 141 | "bswap.and3", IP); |
| 142 | Tmp2 = BinaryOperator::createAnd(Tmp2, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 143 | ConstantInt::get(Type::Int32Ty, 0xFF00), |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 144 | "bswap.and2", IP); |
| 145 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP); |
| 146 | Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP); |
| 147 | V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP); |
| 148 | break; |
| 149 | } |
| 150 | case 64: { |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 151 | Value *Tmp8 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 152 | ConstantInt::get(V->getType(),56),"bswap.8", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 153 | Value *Tmp7 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 154 | ConstantInt::get(V->getType(),40),"bswap.7", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 155 | Value *Tmp6 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 156 | ConstantInt::get(V->getType(),24),"bswap.6", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 157 | Value *Tmp5 = BinaryOperator::createShl(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 158 | ConstantInt::get(V->getType(),8),"bswap.5", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 159 | Value* Tmp4 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 160 | ConstantInt::get(V->getType(),8),"bswap.4", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 161 | Value* Tmp3 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 162 | ConstantInt::get(V->getType(),24),"bswap.3", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 163 | Value* Tmp2 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 164 | ConstantInt::get(V->getType(),40),"bswap.2", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 165 | Value* Tmp1 = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 166 | ConstantInt::get(V->getType(),56),"bswap.1", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 167 | Tmp7 = BinaryOperator::createAnd(Tmp7, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 168 | ConstantInt::get(Type::Int64Ty, |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 169 | 0xFF000000000000ULL), |
| 170 | "bswap.and7", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 171 | Tmp6 = BinaryOperator::createAnd(Tmp6, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 172 | ConstantInt::get(Type::Int64Ty, 0xFF0000000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 173 | "bswap.and6", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 174 | Tmp5 = BinaryOperator::createAnd(Tmp5, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 175 | ConstantInt::get(Type::Int64Ty, 0xFF00000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 176 | "bswap.and5", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 177 | Tmp4 = BinaryOperator::createAnd(Tmp4, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 178 | ConstantInt::get(Type::Int64Ty, 0xFF000000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 179 | "bswap.and4", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 180 | Tmp3 = BinaryOperator::createAnd(Tmp3, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 181 | ConstantInt::get(Type::Int64Ty, 0xFF0000ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 182 | "bswap.and3", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 183 | Tmp2 = BinaryOperator::createAnd(Tmp2, |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 184 | ConstantInt::get(Type::Int64Ty, 0xFF00ULL), |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 185 | "bswap.and2", IP); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 186 | Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP); |
| 187 | Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP); |
| 188 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP); |
| 189 | Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP); |
| 190 | Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP); |
| 191 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP); |
| 192 | V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP); |
| 193 | break; |
| 194 | } |
| 195 | } |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 196 | return V; |
| 197 | } |
| 198 | |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 199 | /// LowerCTPOP - Emit the code to lower ctpop of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 200 | /// instruction IP. |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 201 | static Value *LowerCTPOP(Value *V, Instruction *IP) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 202 | assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 203 | |
| 204 | static const uint64_t MaskValues[6] = { |
| 205 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 206 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 207 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 208 | }; |
| 209 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 210 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 211 | |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 212 | for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) { |
Chris Lattner | 3bb7e3f | 2006-12-12 05:19:46 +0000 | [diff] [blame] | 213 | Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 214 | Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 215 | Value *VShift = BinaryOperator::createLShr(V, |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 216 | ConstantInt::get(V->getType(), i), "ctpop.sh", IP); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 217 | Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP); |
| 218 | V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP); |
| 219 | } |
| 220 | |
Reid Spencer | dc1966e | 2007-04-02 01:01:49 +0000 | [diff] [blame] | 221 | return CastInst::createIntegerCast(V, Type::Int32Ty, false, "ctpop", IP); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 224 | /// LowerCTLZ - Emit the code to lower ctlz of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 225 | /// instruction IP. |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 226 | static Value *LowerCTLZ(Value *V, Instruction *IP) { |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 227 | |
| 228 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 229 | for (unsigned i = 1; i != BitSize; i <<= 1) { |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 230 | Value *ShVal = ConstantInt::get(V->getType(), i); |
Reid Spencer | 1b19cd3 | 2007-02-02 14:09:34 +0000 | [diff] [blame] | 231 | ShVal = BinaryOperator::createLShr(V, ShVal, "ctlz.sh", IP); |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 232 | V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP); |
| 233 | } |
| 234 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 235 | V = BinaryOperator::createNot(V, "", IP); |
| 236 | return LowerCTPOP(V, IP); |
| 237 | } |
| 238 | |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 239 | /// Convert the llvm.part.select.iX.iY intrinsic. This intrinsic takes |
| 240 | /// three integer arguments. The first argument is the Value from which the |
| 241 | /// bits will be selected. It may be of any bit width. The second and third |
| 242 | /// arguments specify a range of bits to select with the second argument |
| 243 | /// specifying the low bit and the third argument specifying the high bit. Both |
| 244 | /// must be type i32. The result is the corresponding selected bits from the |
| 245 | /// Value in the same width as the Value (first argument). If the low bit index |
| 246 | /// is higher than the high bit index then the inverse selection is done and |
| 247 | /// the bits are returned in inverse order. |
| 248 | /// @brief Lowering of llvm.part.select intrinsic. |
| 249 | static Instruction *LowerPartSelect(CallInst *CI) { |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 250 | // Make sure we're dealing with a part select intrinsic here |
| 251 | Function *F = CI->getCalledFunction(); |
| 252 | const FunctionType *FT = F->getFunctionType(); |
| 253 | if (!F->isDeclaration() || !FT->getReturnType()->isInteger() || |
| 254 | FT->getNumParams() != 3 || !FT->getParamType(0)->isInteger() || |
| 255 | !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger()) |
| 256 | return CI; |
| 257 | |
| 258 | // Get the intrinsic implementation function by converting all the . to _ |
| 259 | // in the intrinsic's function name and then reconstructing the function |
| 260 | // declaration. |
| 261 | std::string Name(F->getName()); |
| 262 | for (unsigned i = 4; i < Name.length(); ++i) |
| 263 | if (Name[i] == '.') |
| 264 | Name[i] = '_'; |
| 265 | Module* M = F->getParent(); |
| 266 | F = cast<Function>(M->getOrInsertFunction(Name, FT)); |
| 267 | F->setLinkage(GlobalValue::InternalLinkage); |
| 268 | |
| 269 | // If we haven't defined the impl function yet, do so now |
| 270 | if (F->isDeclaration()) { |
| 271 | |
| 272 | // Get the arguments to the function |
| 273 | Value* Val = F->getOperand(0); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 274 | Value* Right = F->getOperand(1); |
| 275 | Value* Left = F->getOperand(2); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 276 | |
| 277 | // We want to select a range of bits here such that [Left, Right] is shifted |
| 278 | // down to the low bits. However, it is quite possible that Left is smaller |
| 279 | // than Right in which case the bits have to be reversed. |
| 280 | |
| 281 | // Create the blocks we will need for the two cases (forward, reverse) |
| 282 | BasicBlock* CurBB = new BasicBlock("entry", F); |
| 283 | BasicBlock *RevSize = new BasicBlock("revsize", CurBB->getParent()); |
| 284 | BasicBlock *FwdSize = new BasicBlock("fwdsize", CurBB->getParent()); |
| 285 | BasicBlock *Compute = new BasicBlock("compute", CurBB->getParent()); |
| 286 | BasicBlock *Reverse = new BasicBlock("reverse", CurBB->getParent()); |
| 287 | BasicBlock *RsltBlk = new BasicBlock("result", CurBB->getParent()); |
| 288 | |
| 289 | // Cast Left and Right to the size of Val so the widths are all the same |
| 290 | if (Left->getType() != Val->getType()) |
| 291 | Left = CastInst::createIntegerCast(Left, Val->getType(), false, |
| 292 | "tmp", CurBB); |
| 293 | if (Right->getType() != Val->getType()) |
| 294 | Right = CastInst::createIntegerCast(Right, Val->getType(), false, |
| 295 | "tmp", CurBB); |
| 296 | |
| 297 | // Compute a few things that both cases will need, up front. |
| 298 | Constant* Zero = ConstantInt::get(Val->getType(), 0); |
| 299 | Constant* One = ConstantInt::get(Val->getType(), 1); |
| 300 | Constant* AllOnes = ConstantInt::getAllOnesValue(Val->getType()); |
| 301 | |
| 302 | // Compare the Left and Right bit positions. This is used to determine |
| 303 | // which case we have (forward or reverse) |
| 304 | ICmpInst *Cmp = new ICmpInst(ICmpInst::ICMP_ULT, Left, Right, "less",CurBB); |
| 305 | new BranchInst(RevSize, FwdSize, Cmp, CurBB); |
| 306 | |
| 307 | // First, copmute the number of bits in the forward case. |
| 308 | Instruction* FBitSize = |
| 309 | BinaryOperator::createSub(Left, Right,"fbits", FwdSize); |
| 310 | new BranchInst(Compute, FwdSize); |
| 311 | |
| 312 | // Second, compute the number of bits in the reverse case. |
| 313 | Instruction* RBitSize = |
| 314 | BinaryOperator::createSub(Right, Left, "rbits", RevSize); |
| 315 | new BranchInst(Compute, RevSize); |
| 316 | |
| 317 | // Now, compute the bit range. Start by getting the bitsize and the shift |
| 318 | // amount (either Left or Right) from PHI nodes. Then we compute a mask for |
| 319 | // the number of bits we want in the range. We shift the bits down to the |
| 320 | // least significant bits, apply the mask to zero out unwanted high bits, |
| 321 | // and we have computed the "forward" result. It may still need to be |
| 322 | // reversed. |
| 323 | |
| 324 | // Get the BitSize from one of the two subtractions |
| 325 | PHINode *BitSize = new PHINode(Val->getType(), "bits", Compute); |
| 326 | BitSize->reserveOperandSpace(2); |
| 327 | BitSize->addIncoming(FBitSize, FwdSize); |
| 328 | BitSize->addIncoming(RBitSize, RevSize); |
| 329 | |
| 330 | // Get the ShiftAmount as the smaller of Left/Right |
| 331 | PHINode *ShiftAmt = new PHINode(Val->getType(), "shiftamt", Compute); |
| 332 | ShiftAmt->reserveOperandSpace(2); |
| 333 | ShiftAmt->addIncoming(Right, FwdSize); |
| 334 | ShiftAmt->addIncoming(Left, RevSize); |
| 335 | |
| 336 | // Increment the bit size |
| 337 | Instruction *BitSizePlusOne = |
| 338 | BinaryOperator::createAdd(BitSize, One, "bits", Compute); |
| 339 | |
| 340 | // Create a Mask to zero out the high order bits. |
| 341 | Instruction* Mask = |
| 342 | BinaryOperator::createShl(AllOnes, BitSizePlusOne, "mask", Compute); |
| 343 | Mask = BinaryOperator::createNot(Mask, "mask", Compute); |
| 344 | |
| 345 | // Shift the bits down and apply the mask |
| 346 | Instruction* FRes = |
| 347 | BinaryOperator::createLShr(Val, ShiftAmt, "fres", Compute); |
| 348 | FRes = BinaryOperator::createAnd(FRes, Mask, "fres", Compute); |
| 349 | new BranchInst(Reverse, RsltBlk, Cmp, Compute); |
| 350 | |
| 351 | // In the Reverse block we have the mask already in FRes but we must reverse |
| 352 | // it by shifting FRes bits right and putting them in RRes by shifting them |
| 353 | // in from left. |
| 354 | |
| 355 | // First set up our loop counters |
| 356 | PHINode *Count = new PHINode(Val->getType(), "count", Reverse); |
| 357 | Count->reserveOperandSpace(2); |
| 358 | Count->addIncoming(BitSizePlusOne, Compute); |
| 359 | |
| 360 | // Next, get the value that we are shifting. |
| 361 | PHINode *BitsToShift = new PHINode(Val->getType(), "val", Reverse); |
| 362 | BitsToShift->reserveOperandSpace(2); |
| 363 | BitsToShift->addIncoming(FRes, Compute); |
| 364 | |
| 365 | // Finally, get the result of the last computation |
| 366 | PHINode *RRes = new PHINode(Val->getType(), "rres", Reverse); |
| 367 | RRes->reserveOperandSpace(2); |
| 368 | RRes->addIncoming(Zero, Compute); |
| 369 | |
| 370 | // Decrement the counter |
| 371 | Instruction *Decr = BinaryOperator::createSub(Count, One, "decr", Reverse); |
| 372 | Count->addIncoming(Decr, Reverse); |
| 373 | |
| 374 | // Compute the Bit that we want to move |
| 375 | Instruction *Bit = |
| 376 | BinaryOperator::createAnd(BitsToShift, One, "bit", Reverse); |
| 377 | |
| 378 | // Compute the new value for next iteration. |
| 379 | Instruction *NewVal = |
| 380 | BinaryOperator::createLShr(BitsToShift, One, "rshift", Reverse); |
| 381 | BitsToShift->addIncoming(NewVal, Reverse); |
| 382 | |
| 383 | // Shift the bit into the low bits of the result. |
| 384 | Instruction *NewRes = |
| 385 | BinaryOperator::createShl(RRes, One, "lshift", Reverse); |
| 386 | NewRes = BinaryOperator::createOr(NewRes, Bit, "addbit", Reverse); |
| 387 | RRes->addIncoming(NewRes, Reverse); |
| 388 | |
| 389 | // Terminate loop if we've moved all the bits. |
| 390 | ICmpInst *Cond = |
| 391 | new ICmpInst(ICmpInst::ICMP_EQ, Decr, Zero, "cond", Reverse); |
| 392 | new BranchInst(RsltBlk, Reverse, Cond, Reverse); |
| 393 | |
| 394 | // Finally, in the result block, select one of the two results with a PHI |
| 395 | // node and return the result; |
| 396 | CurBB = RsltBlk; |
| 397 | PHINode *BitSelect = new PHINode(Val->getType(), "part_select", CurBB); |
| 398 | BitSelect->reserveOperandSpace(2); |
| 399 | BitSelect->addIncoming(FRes, Compute); |
| 400 | BitSelect->addIncoming(NewRes, Reverse); |
| 401 | new ReturnInst(BitSelect, CurBB); |
| 402 | } |
| 403 | |
| 404 | // Return a call to the implementation function |
| 405 | Value *Args[3]; |
| 406 | Args[0] = CI->getOperand(0); |
| 407 | Args[1] = CI->getOperand(1); |
| 408 | Args[2] = CI->getOperand(2); |
| 409 | return new CallInst(F, Args, 3, CI->getName(), CI); |
| 410 | } |
| 411 | |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 412 | /// Convert the llvm.part.set.iX.iY.iZ intrinsic. This intrinsic takes |
| 413 | /// four integer arguments (iAny %Value, iAny %Replacement, i32 %Low, i32 %High) |
| 414 | /// The first two arguments can be any bit width. The result is the same width |
| 415 | /// as %Value. The operation replaces bits between %Low and %High with the value |
| 416 | /// in %Replacement. If %Replacement is not the same width, it is truncated or |
| 417 | /// zero extended as appropriate to fit the bits being replaced. If %Low is |
| 418 | /// greater than %High then the inverse set of bits are replaced. |
| 419 | /// @brief Lowering of llvm.bit.part.set intrinsic. |
| 420 | static Instruction *LowerPartSet(CallInst *CI) { |
| 421 | // Make sure we're dealing with a part select intrinsic here |
| 422 | Function *F = CI->getCalledFunction(); |
| 423 | const FunctionType *FT = F->getFunctionType(); |
| 424 | if (!F->isDeclaration() || !FT->getReturnType()->isInteger() || |
| 425 | FT->getNumParams() != 4 || !FT->getParamType(0)->isInteger() || |
| 426 | !FT->getParamType(1)->isInteger() || !FT->getParamType(2)->isInteger() || |
| 427 | !FT->getParamType(3)->isInteger()) |
| 428 | return CI; |
| 429 | |
| 430 | // Get the intrinsic implementation function by converting all the . to _ |
| 431 | // in the intrinsic's function name and then reconstructing the function |
| 432 | // declaration. |
| 433 | std::string Name(F->getName()); |
| 434 | for (unsigned i = 4; i < Name.length(); ++i) |
| 435 | if (Name[i] == '.') |
| 436 | Name[i] = '_'; |
| 437 | Module* M = F->getParent(); |
| 438 | F = cast<Function>(M->getOrInsertFunction(Name, FT)); |
| 439 | F->setLinkage(GlobalValue::InternalLinkage); |
| 440 | |
| 441 | // If we haven't defined the impl function yet, do so now |
| 442 | if (F->isDeclaration()) { |
| 443 | // Note: the following code is based on code generated by llvm2cpp with |
| 444 | // the following input. This is just *one* example of a generated function. |
| 445 | // The functions vary by bit width of result and first two arguments. |
| 446 | // The generated code has been changed to deal with any bit width not just |
| 447 | // the 32/64 bitwidths used in the above sample. |
| 448 | // |
| 449 | // define i64 @part_set(i64 %Val, i32 %Rep, i32 %Lo, i32 %Hi) { |
| 450 | // entry: |
| 451 | // %is_forward = icmp ult i32 %Lo, %Hi |
| 452 | // %Lo.pn = select i1 %is_forward, i32 %Hi, i32 %Lo |
| 453 | // %Hi.pn = select i1 %is_forward, i32 %Lo, i32 %Hi |
| 454 | // %iftmp.16.0 = sub i32 %Lo.pn, %Hi.pn |
| 455 | // icmp ult i32 %iftmp.16.0, 32 |
| 456 | // br i1 %1, label %cond_true11, label %cond_next19 |
| 457 | // cond_true11: |
| 458 | // %tmp13 = sub i32 32, %iftmp.16.0 |
| 459 | // %tmp14 = lshr i32 -1, %tmp13 |
| 460 | // %tmp16 = and i32 %tmp14, %Rep |
| 461 | // br label %cond_next19 |
| 462 | // cond_next19: |
| 463 | // %iftmp.17.0 = phi i32 [ %tmp16, %cond_true11 ], [ %Rep, %entry ] |
| 464 | // %tmp2021 = zext i32 %iftmp.17.0 to i64 |
| 465 | // icmp ugt i32 %Lo, %Hi |
| 466 | // br i1 %2, label %cond_next60, label %cond_true24 |
| 467 | // cond_true24: |
| 468 | // %tmp25.cast = zext i32 %Hi to i64 |
| 469 | // %tmp26 = lshr i64 -1, %tmp25.cast |
| 470 | // %tmp27.cast = zext i32 %Lo to i64 |
| 471 | // %tmp28 = shl i64 %tmp26, %tmp27.cast |
| 472 | // %tmp28not = xor i64 %tmp28, -1 |
| 473 | // %tmp31 = shl i64 %tmp2021, %tmp27.cast |
| 474 | // %tmp34 = and i64 %tmp28not, %Val |
| 475 | // %Val_addr.064 = or i64 %tmp31, %tmp34 |
| 476 | // ret i64 %Val_addr.064 |
| 477 | // cond_next60: |
| 478 | // %tmp39.cast = zext i32 %Lo to i64 |
| 479 | // %tmp40 = shl i64 -1, %tmp39.cast |
| 480 | // %tmp41.cast = zext i32 %Hi to i64 |
| 481 | // %tmp42 = shl i64 -1, %tmp41.cast |
| 482 | // %tmp45.demorgan = or i64 %tmp42, %tmp40 |
| 483 | // %tmp45 = xor i64 %tmp45.demorgan, -1 |
| 484 | // %tmp47 = and i64 %tmp45, %Val |
| 485 | // %tmp50 = shl i64 %tmp2021, %tmp39.cast |
| 486 | // %tmp52 = sub i32 32, %Hi |
| 487 | // %tmp52.cast = zext i32 %tmp52 to i64 |
| 488 | // %tmp54 = lshr i64 %tmp2021, %tmp52.cast |
| 489 | // %tmp57 = or i64 %tmp50, %tmp47 |
| 490 | // %Val_addr.0 = or i64 %tmp57, %tmp54 |
| 491 | // ret i64 %Val_addr.0 |
| 492 | // } |
| 493 | |
| 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); |
| 511 | |
| 512 | BasicBlock* entry = new BasicBlock("entry",F,0); |
| 513 | BasicBlock* large = new BasicBlock("large",F,0); |
| 514 | BasicBlock* small = new BasicBlock("small",F,0); |
Reid Spencer | 3795809 | 2007-04-12 12:46:33 +0000 | [diff] [blame^] | 515 | BasicBlock* forward = new BasicBlock("forward",F,0); |
| 516 | BasicBlock* reverse = new BasicBlock("reverse",F,0); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 517 | |
| 518 | // Block entry (entry) |
| 519 | // First, convert Lo and Hi to ValTy bit width |
| 520 | if (ValBits > 32) { |
| 521 | Hi = new ZExtInst(Hi, ValTy, "", entry); |
| 522 | Lo = new ZExtInst(Lo, ValTy, "", entry); |
| 523 | } else if (ValBits < 32) { |
| 524 | Hi = new TruncInst(Hi, ValTy, "", entry); |
| 525 | Lo = new TruncInst(Lo, ValTy, "", entry); |
| 526 | } |
| 527 | ICmpInst* is_forward = |
| 528 | new ICmpInst(ICmpInst::ICMP_ULT, Lo, Hi, "", entry); |
| 529 | SelectInst* Lo_pn = new SelectInst(is_forward, Hi, Lo, "", entry); |
| 530 | SelectInst* Hi_pn = new SelectInst(is_forward, Lo, Hi, "", entry); |
| 531 | BinaryOperator* NumBits = BinaryOperator::createSub(Lo_pn, Hi_pn, "",entry); |
| 532 | ICmpInst* is_large = |
| 533 | new ICmpInst(ICmpInst::ICMP_ULT, NumBits, RepBitWidth, "", entry); |
| 534 | new BranchInst(large, small, is_large, entry); |
| 535 | |
| 536 | // Block "large" |
| 537 | BinaryOperator* MaskBits = |
| 538 | BinaryOperator::createSub(RepBitWidth, NumBits, "", large); |
| 539 | BinaryOperator* Mask1 = |
| 540 | BinaryOperator::createLShr(RepMask, MaskBits, "", large); |
| 541 | BinaryOperator* Rep2 = BinaryOperator::createAnd(Mask1, Rep, "", large); |
| 542 | new BranchInst(small, large); |
| 543 | |
| 544 | // Block "small" |
| 545 | PHINode* Rep3 = new PHINode(RepTy, "", small); |
| 546 | Rep3->reserveOperandSpace(2); |
| 547 | Rep3->addIncoming(Rep2, small); |
| 548 | Rep3->addIncoming(Rep, entry); |
Reid Spencer | 3795809 | 2007-04-12 12:46:33 +0000 | [diff] [blame^] | 549 | Value* Rep4 = Rep3; |
| 550 | if (ValBits > RepBits) |
| 551 | Rep4 = new ZExtInst(Rep3, ValTy, "", small); |
| 552 | else if (ValBits < RepBits) |
| 553 | Rep4 = new TruncInst(Rep3, ValTy, "", small); |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 554 | ICmpInst* is_reverse = |
| 555 | new ICmpInst(ICmpInst::ICMP_UGT, Lo, Hi, "", small); |
| 556 | new BranchInst(reverse, forward, is_reverse, small); |
| 557 | |
| 558 | // Block "forward" |
| 559 | Value* t1 = BinaryOperator::createLShr(ValMask, Hi, "", forward); |
| 560 | Value* t2 = BinaryOperator::createShl(t1, Lo, "", forward); |
| 561 | Value* nott2 = BinaryOperator::createXor(t2, ValMask, "", forward); |
| 562 | Value* t3 = BinaryOperator::createShl(Rep4, Lo, "", forward); |
| 563 | Value* t4 = BinaryOperator::createAnd(nott2, Val, "", forward); |
| 564 | Value* FRslt = BinaryOperator::createOr(t3, t4, "", forward); |
| 565 | new ReturnInst(FRslt, forward); |
| 566 | |
| 567 | // Block "reverse" |
| 568 | Value* t5 = BinaryOperator::createShl(ValMask, Lo, "", reverse); |
| 569 | Value* t6 = BinaryOperator::createShl(ValMask, Hi, "", reverse); |
| 570 | Value* t7 = BinaryOperator::createOr(t6, t5, "", reverse); |
| 571 | Value* t8 = BinaryOperator::createXor(t7, ValMask, "", reverse); |
| 572 | Value* t9 = BinaryOperator::createAnd(t8, Val, "", reverse); |
| 573 | Value* t10 = BinaryOperator::createShl(Rep4, Lo, "", reverse); |
| 574 | Value* t11 = BinaryOperator::createSub(RepBitWidth, Hi, "", reverse); |
| 575 | Value* t12 = new ZExtInst(t11, ValTy, "", reverse); |
| 576 | Value* t13 = BinaryOperator::createLShr(Rep4, t12, "",reverse); |
| 577 | Value* t14 = BinaryOperator::createOr(t10, t9, "", reverse); |
| 578 | Value* RRslt = BinaryOperator::createOr(t14, t13, "", reverse); |
| 579 | new ReturnInst(RRslt, reverse); |
| 580 | } |
| 581 | |
| 582 | // Return a call to the implementation function |
| 583 | Value *Args[3]; |
| 584 | Args[0] = CI->getOperand(0); |
| 585 | Args[1] = CI->getOperand(1); |
| 586 | Args[2] = CI->getOperand(2); |
| 587 | Args[3] = CI->getOperand(3); |
| 588 | return new CallInst(F, Args, 4, CI->getName(), CI); |
| 589 | } |
| 590 | |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 591 | |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 592 | void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 593 | Function *Callee = CI->getCalledFunction(); |
| 594 | assert(Callee && "Cannot lower an indirect call!"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 595 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 596 | switch (Callee->getIntrinsicID()) { |
| 597 | case Intrinsic::not_intrinsic: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 598 | cerr << "Cannot lower a call to a non-intrinsic function '" |
| 599 | << Callee->getName() << "'!\n"; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 600 | abort(); |
| 601 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 602 | cerr << "Error: Code generator does not support intrinsic function '" |
| 603 | << Callee->getName() << "'!\n"; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 604 | abort(); |
| 605 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 606 | // The setjmp/longjmp intrinsics should only exist in the code if it was |
| 607 | // never optimized (ie, right out of the CFE), or if it has been hacked on |
| 608 | // by the lowerinvoke pass. In both cases, the right thing to do is to |
| 609 | // convert the call to an explicit setjmp or longjmp call. |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 610 | case Intrinsic::setjmp: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 611 | static Constant *SetjmpFCache = 0; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 612 | Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 613 | Type::Int32Ty, SetjmpFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 614 | if (CI->getType() != Type::VoidTy) |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 615 | CI->replaceAllUsesWith(V); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 616 | break; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 617 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 618 | case Intrinsic::sigsetjmp: |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 619 | if (CI->getType() != Type::VoidTy) |
| 620 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 621 | break; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 622 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 623 | case Intrinsic::longjmp: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 624 | static Constant *LongjmpFCache = 0; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 625 | ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 626 | Type::VoidTy, LongjmpFCache); |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 627 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 628 | } |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 629 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 630 | case Intrinsic::siglongjmp: { |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 631 | // Insert the call to abort |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 632 | static Constant *AbortFCache = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 633 | ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 634 | Type::VoidTy, AbortFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 635 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 636 | } |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 637 | case Intrinsic::ctpop: |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 638 | CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI)); |
| 639 | break; |
| 640 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 641 | case Intrinsic::bswap: |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 642 | CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI)); |
| 643 | break; |
| 644 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 645 | case Intrinsic::ctlz: |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 646 | CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 647 | break; |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 648 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 649 | case Intrinsic::cttz: { |
Chris Lattner | a801172 | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 650 | // cttz(x) -> ctpop(~X & (X-1)) |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 651 | Value *Src = CI->getOperand(1); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 652 | Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI); |
Chris Lattner | a801172 | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 653 | Value *SrcM1 = ConstantInt::get(Src->getType(), 1); |
| 654 | SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI); |
| 655 | Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 656 | CI->replaceAllUsesWith(Src); |
| 657 | break; |
| 658 | } |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 659 | |
Chris Lattner | c6eb6d7 | 2007-04-10 03:20:39 +0000 | [diff] [blame] | 660 | case Intrinsic::part_select: |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 661 | CI->replaceAllUsesWith(LowerPartSelect(CI)); |
| 662 | break; |
| 663 | |
| 664 | case Intrinsic::part_set: |
| 665 | CI->replaceAllUsesWith(LowerPartSet(CI)); |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 666 | break; |
| 667 | |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 668 | case Intrinsic::stacksave: |
| 669 | case Intrinsic::stackrestore: { |
| 670 | static bool Warned = false; |
| 671 | if (!Warned) |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 672 | cerr << "WARNING: this target does not support the llvm.stack" |
| 673 | << (Callee->getIntrinsicID() == Intrinsic::stacksave ? |
| 674 | "save" : "restore") << " intrinsic.\n"; |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 675 | Warned = true; |
| 676 | if (Callee->getIntrinsicID() == Intrinsic::stacksave) |
| 677 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 678 | break; |
| 679 | } |
| 680 | |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 681 | case Intrinsic::returnaddress: |
| 682 | case Intrinsic::frameaddress: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 683 | cerr << "WARNING: this target does not support the llvm." |
| 684 | << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? |
| 685 | "return" : "frame") << "address intrinsic.\n"; |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 686 | CI->replaceAllUsesWith(ConstantPointerNull::get( |
| 687 | cast<PointerType>(CI->getType()))); |
| 688 | break; |
| 689 | |
Chris Lattner | 0942b7c | 2005-02-28 19:27:23 +0000 | [diff] [blame] | 690 | case Intrinsic::prefetch: |
| 691 | break; // Simply strip out prefetches on unsupported architectures |
| 692 | |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 693 | case Intrinsic::pcmarker: |
| 694 | break; // Simply strip out pcmarker on unsupported architectures |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 695 | case Intrinsic::readcyclecounter: { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 696 | cerr << "WARNING: this target does not support the llvm.readcyclecoun" |
| 697 | << "ter intrinsic. It is being lowered to a constant 0\n"; |
Reid Spencer | 4785781 | 2006-12-31 05:55:36 +0000 | [diff] [blame] | 698 | CI->replaceAllUsesWith(ConstantInt::get(Type::Int64Ty, 0)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 699 | break; |
| 700 | } |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 702 | case Intrinsic::dbg_stoppoint: |
| 703 | case Intrinsic::dbg_region_start: |
| 704 | case Intrinsic::dbg_region_end: |
| 705 | case Intrinsic::dbg_func_start: |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 706 | case Intrinsic::dbg_declare: |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 707 | case Intrinsic::eh_exception: |
Jim Laskey | 63f3e3f | 2007-02-28 18:37:50 +0000 | [diff] [blame] | 708 | case Intrinsic::eh_selector: |
Jim Laskey | 0b4711b | 2007-03-01 20:24:30 +0000 | [diff] [blame] | 709 | case Intrinsic::eh_filter: |
Jim Laskey | 774b864 | 2007-02-22 18:51:19 +0000 | [diff] [blame] | 710 | break; // Simply strip out debugging and eh intrinsics |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 711 | |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 712 | case Intrinsic::memcpy_i32: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 713 | case Intrinsic::memcpy_i64: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 714 | static Constant *MemcpyFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 715 | Value *Size = CI->getOperand(3); |
| 716 | const Type *IntPtr = TD.getIntPtrType(); |
| 717 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 718 | IntPtr->getPrimitiveSizeInBits()) |
| 719 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 720 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 721 | IntPtr->getPrimitiveSizeInBits()) |
| 722 | Size = new TruncInst(Size, IntPtr, "", CI); |
| 723 | Value *Ops[3]; |
| 724 | Ops[0] = CI->getOperand(1); |
| 725 | Ops[1] = CI->getOperand(2); |
| 726 | Ops[2] = Size; |
| 727 | ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 728 | MemcpyFCache); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 729 | break; |
| 730 | } |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 731 | case Intrinsic::memmove_i32: |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 732 | case Intrinsic::memmove_i64: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 733 | static Constant *MemmoveFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 734 | Value *Size = CI->getOperand(3); |
| 735 | const Type *IntPtr = TD.getIntPtrType(); |
| 736 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 737 | IntPtr->getPrimitiveSizeInBits()) |
| 738 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 739 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 740 | IntPtr->getPrimitiveSizeInBits()) |
| 741 | Size = new TruncInst(Size, IntPtr, "", CI); |
| 742 | Value *Ops[3]; |
| 743 | Ops[0] = CI->getOperand(1); |
| 744 | Ops[1] = CI->getOperand(2); |
| 745 | Ops[2] = Size; |
| 746 | ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 747 | MemmoveFCache); |
Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 748 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 749 | } |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 750 | case Intrinsic::memset_i32: |
Chris Lattner | 03dd465 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 751 | case Intrinsic::memset_i64: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 752 | static Constant *MemsetFCache = 0; |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 753 | Value *Size = CI->getOperand(3); |
Chris Lattner | 7d6f77d | 2007-02-06 06:07:51 +0000 | [diff] [blame] | 754 | const Type *IntPtr = TD.getIntPtrType(); |
| 755 | if (Size->getType()->getPrimitiveSizeInBits() < |
| 756 | IntPtr->getPrimitiveSizeInBits()) |
| 757 | Size = new ZExtInst(Size, IntPtr, "", CI); |
| 758 | else if (Size->getType()->getPrimitiveSizeInBits() > |
| 759 | IntPtr->getPrimitiveSizeInBits()) |
| 760 | Size = new TruncInst(Size, IntPtr, "", CI); |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 761 | Value *Ops[3]; |
| 762 | Ops[0] = CI->getOperand(1); |
| 763 | // Extend the amount to i32. |
| 764 | Ops[1] = new ZExtInst(CI->getOperand(2), Type::Int32Ty, "", CI); |
| 765 | Ops[2] = Size; |
| 766 | ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType(), |
| 767 | MemsetFCache); |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 768 | break; |
| 769 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 770 | case Intrinsic::sqrt_f32: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 771 | static Constant *sqrtfFCache = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 772 | ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 773 | Type::FloatTy, sqrtfFCache); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 774 | break; |
| 775 | } |
| 776 | case Intrinsic::sqrt_f64: { |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 777 | static Constant *sqrtFCache = 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 778 | ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(), |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 779 | Type::DoubleTy, sqrtFCache); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 780 | break; |
| 781 | } |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 782 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 784 | assert(CI->use_empty() && |
| 785 | "Lowering should have eliminated any uses of the intrinsic call!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 786 | CI->eraseFromParent(); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 787 | } |