Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 1 | //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===// |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +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 | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 2775aba | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 10 | // This file implements the IntrinsicLowering class. |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
| 15 | #include "llvm/DerivedTypes.h" |
| 16 | #include "llvm/Module.h" |
Misha Brukman | 63b38bd | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 18 | #include "llvm/Type.h" |
Bill Wendling | 3f6f0fd | 2006-11-28 02:08:17 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/IntrinsicLowering.h" |
| 20 | #include "llvm/Support/Streams.h" |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | template <class ArgIt> |
| 24 | static Function *EnsureFunctionExists(Module &M, const char *Name, |
| 25 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 26 | const Type *RetTy) { |
| 27 | if (Function *F = M.getNamedFunction(Name)) return F; |
| 28 | // It doesn't already exist in the program, insert a new definition now. |
| 29 | std::vector<const Type *> ParamTys; |
| 30 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 31 | ParamTys.push_back(I->getType()); |
| 32 | return M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); |
| 33 | } |
| 34 | |
| 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, |
| 41 | ArgIt ArgBegin, ArgIt ArgEnd, |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 42 | const unsigned *castOpcodes, |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 43 | const Type *RetTy, Function *&FCache) { |
| 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(); |
| 48 | FCache = M->getNamedFunction(NewFn); |
| 49 | if (!FCache) { |
| 50 | // It doesn't already exist in the program, insert a new definition now. |
| 51 | std::vector<const Type *> ParamTys; |
| 52 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 53 | ParamTys.push_back((*I)->getType()); |
| 54 | FCache = M->getOrInsertFunction(NewFn, |
| 55 | FunctionType::get(RetTy, ParamTys, false)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | const FunctionType *FT = FCache->getFunctionType(); |
| 60 | std::vector<Value*> Operands; |
| 61 | unsigned ArgNo = 0; |
| 62 | for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams(); |
| 63 | ++I, ++ArgNo) { |
| 64 | Value *Arg = *I; |
| 65 | if (Arg->getType() != FT->getParamType(ArgNo)) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 66 | if (castOpcodes[ArgNo]) |
| 67 | Arg = CastInst::create(Instruction::CastOps(castOpcodes[ArgNo]), |
| 68 | Arg, FT->getParamType(ArgNo), Arg->getName(), CI); |
Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame^] | 69 | else { |
| 70 | Instruction::CastOps opcode = CastInst::getCastOpcode(Arg, |
| 71 | Arg->getType()->isSigned(), FT->getParamType(ArgNo), |
| 72 | FT->getParamType(ArgNo)->isSigned()); |
| 73 | Arg = CastInst::create(opcode, Arg, FT->getParamType(ArgNo), |
| 74 | Arg->getName(), CI); |
| 75 | } |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 76 | Operands.push_back(Arg); |
| 77 | } |
| 78 | // Pass nulls into any additional arguments... |
| 79 | for (; ArgNo != FT->getNumParams(); ++ArgNo) |
| 80 | Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo))); |
| 81 | |
| 82 | std::string Name = CI->getName(); CI->setName(""); |
| 83 | if (FT->getReturnType() == Type::VoidTy) Name.clear(); |
| 84 | CallInst *NewCI = new CallInst(FCache, Operands, Name, CI); |
| 85 | if (!CI->use_empty()) { |
| 86 | Value *V = NewCI; |
Reid Spencer | 668d90f | 2006-12-18 08:47:13 +0000 | [diff] [blame^] | 87 | if (CI->getType() != NewCI->getType()) { |
| 88 | Instruction::CastOps opcode = CastInst::getCastOpcode(NewCI, |
| 89 | NewCI->getType()->isSigned(), CI->getType(), |
| 90 | CI->getType()->isSigned()); |
| 91 | V = CastInst::create(opcode, NewCI, CI->getType(), Name, CI); |
| 92 | } |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 93 | CI->replaceAllUsesWith(V); |
| 94 | } |
| 95 | return NewCI; |
| 96 | } |
| 97 | |
Chris Lattner | 2775aba | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 98 | void IntrinsicLowering::AddPrototypes(Module &M) { |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 99 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 100 | if (I->isExternal() && !I->use_empty()) |
| 101 | switch (I->getIntrinsicID()) { |
| 102 | default: break; |
| 103 | case Intrinsic::setjmp: |
Chris Lattner | 9acd314 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 104 | EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(), |
| 105 | Type::IntTy); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 106 | break; |
| 107 | case Intrinsic::longjmp: |
Chris Lattner | 9acd314 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 108 | EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(), |
| 109 | Type::VoidTy); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 110 | break; |
| 111 | case Intrinsic::siglongjmp: |
Chris Lattner | 9acd314 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 112 | EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(), |
| 113 | Type::VoidTy); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 114 | break; |
Chris Lattner | 093c159 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 115 | case Intrinsic::memcpy_i32: |
| 116 | case Intrinsic::memcpy_i64: |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 117 | EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(), |
| 118 | I->arg_begin()->getType()); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 119 | break; |
Chris Lattner | 093c159 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 120 | case Intrinsic::memmove_i32: |
| 121 | case Intrinsic::memmove_i64: |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 122 | EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(), |
| 123 | I->arg_begin()->getType()); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 124 | break; |
Chris Lattner | 093c159 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 125 | case Intrinsic::memset_i32: |
| 126 | case Intrinsic::memset_i64: |
Chris Lattner | 9acd314 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 127 | M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy), |
| 128 | PointerType::get(Type::SByteTy), |
Jeff Cohen | 11e26b5 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 129 | Type::IntTy, (--(--I->arg_end()))->getType(), |
| 130 | (Type *)0); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 131 | break; |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 132 | case Intrinsic::isunordered_f32: |
| 133 | case Intrinsic::isunordered_f64: |
Chris Lattner | 9acd314 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 134 | EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(), |
| 135 | Type::BoolTy); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 136 | break; |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 137 | case Intrinsic::sqrt_f32: |
| 138 | case Intrinsic::sqrt_f64: |
Alkis Evlogimenos | d7e534b | 2005-04-30 07:13:31 +0000 | [diff] [blame] | 139 | if(I->arg_begin()->getType() == Type::FloatTy) |
Chris Lattner | 9acd314 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 140 | EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(), |
| 141 | Type::FloatTy); |
Chris Lattner | 30fe4ac2 | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 142 | else |
Chris Lattner | 9acd314 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 143 | EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(), |
| 144 | Type::DoubleTy); |
Chris Lattner | 30fe4ac2 | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 145 | break; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Nate Begeman | 7d831fa | 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) { |
| 152 | assert(V->getType()->isInteger() && "Can't bswap a non-integer type!"); |
| 153 | |
Nate Begeman | 7d831fa | 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: { |
| 159 | Value *Tmp1 = new ShiftInst(Instruction::Shl, V, |
| 160 | ConstantInt::get(Type::UByteTy,8),"bswap.2",IP); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 161 | Value *Tmp2 = new ShiftInst(Instruction::LShr, V, |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 162 | ConstantInt::get(Type::UByteTy,8),"bswap.1",IP); |
| 163 | V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP); |
| 164 | break; |
| 165 | } |
| 166 | case 32: { |
| 167 | Value *Tmp4 = new ShiftInst(Instruction::Shl, V, |
| 168 | ConstantInt::get(Type::UByteTy,24),"bswap.4", IP); |
| 169 | Value *Tmp3 = new ShiftInst(Instruction::Shl, V, |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 170 | ConstantInt::get(Type::UByteTy,8),"bswap.3",IP); |
| 171 | Value *Tmp2 = new ShiftInst(Instruction::LShr, V, |
| 172 | ConstantInt::get(Type::UByteTy,8),"bswap.2",IP); |
| 173 | Value *Tmp1 = new ShiftInst(Instruction::LShr, V, |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 174 | ConstantInt::get(Type::UByteTy,24),"bswap.1", IP); |
| 175 | Tmp3 = BinaryOperator::createAnd(Tmp3, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 176 | ConstantInt::get(Type::UIntTy, 0xFF0000), |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 177 | "bswap.and3", IP); |
| 178 | Tmp2 = BinaryOperator::createAnd(Tmp2, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 179 | ConstantInt::get(Type::UIntTy, 0xFF00), |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 180 | "bswap.and2", IP); |
| 181 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP); |
| 182 | Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP); |
| 183 | V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP); |
| 184 | break; |
| 185 | } |
| 186 | case 64: { |
| 187 | Value *Tmp8 = new ShiftInst(Instruction::Shl, V, |
| 188 | ConstantInt::get(Type::UByteTy,56),"bswap.8", IP); |
| 189 | Value *Tmp7 = new ShiftInst(Instruction::Shl, V, |
| 190 | ConstantInt::get(Type::UByteTy,40),"bswap.7", IP); |
| 191 | Value *Tmp6 = new ShiftInst(Instruction::Shl, V, |
| 192 | ConstantInt::get(Type::UByteTy,24),"bswap.6", IP); |
| 193 | Value *Tmp5 = new ShiftInst(Instruction::Shl, V, |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 194 | ConstantInt::get(Type::UByteTy,8),"bswap.5", IP); |
| 195 | Value* Tmp4 = new ShiftInst(Instruction::LShr, V, |
| 196 | ConstantInt::get(Type::UByteTy,8),"bswap.4", IP); |
| 197 | Value* Tmp3 = new ShiftInst(Instruction::LShr, V, |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 198 | ConstantInt::get(Type::UByteTy,24),"bswap.3", IP); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 199 | Value* Tmp2 = new ShiftInst(Instruction::LShr, V, |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 200 | ConstantInt::get(Type::UByteTy,40),"bswap.2", IP); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 201 | Value* Tmp1 = new ShiftInst(Instruction::LShr, V, |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 202 | ConstantInt::get(Type::UByteTy,56),"bswap.1", IP); |
| 203 | Tmp7 = BinaryOperator::createAnd(Tmp7, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 204 | ConstantInt::get(Type::ULongTy, |
| 205 | 0xFF000000000000ULL), |
| 206 | "bswap.and7", IP); |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 207 | Tmp6 = BinaryOperator::createAnd(Tmp6, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 208 | ConstantInt::get(Type::ULongTy, 0xFF0000000000ULL), |
| 209 | "bswap.and6", IP); |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 210 | Tmp5 = BinaryOperator::createAnd(Tmp5, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 211 | ConstantInt::get(Type::ULongTy, 0xFF00000000ULL), |
| 212 | "bswap.and5", IP); |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 213 | Tmp4 = BinaryOperator::createAnd(Tmp4, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 214 | ConstantInt::get(Type::ULongTy, 0xFF000000ULL), |
| 215 | "bswap.and4", IP); |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 216 | Tmp3 = BinaryOperator::createAnd(Tmp3, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 217 | ConstantInt::get(Type::ULongTy, 0xFF0000ULL), |
| 218 | "bswap.and3", IP); |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 219 | Tmp2 = BinaryOperator::createAnd(Tmp2, |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 220 | ConstantInt::get(Type::ULongTy, 0xFF00ULL), |
| 221 | "bswap.and2", IP); |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +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); |
| 229 | break; |
| 230 | } |
| 231 | } |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 232 | return V; |
| 233 | } |
| 234 | |
Chris Lattner | 9ec975a | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 235 | /// LowerCTPOP - Emit the code to lower ctpop of V before the specified |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 236 | /// instruction IP. |
Chris Lattner | 9ec975a | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 237 | static Value *LowerCTPOP(Value *V, Instruction *IP) { |
| 238 | assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!"); |
Chris Lattner | 9ec975a | 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 | 991ce36 | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 246 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 9ec975a | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 248 | for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) { |
Chris Lattner | 6801f02 | 2006-12-12 05:19:46 +0000 | [diff] [blame] | 249 | Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); |
Chris Lattner | 9ec975a | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 250 | Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 251 | Value *VShift = new ShiftInst(Instruction::LShr, V, |
Chris Lattner | 9ec975a | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 252 | ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP); |
| 253 | Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP); |
| 254 | V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP); |
| 255 | } |
| 256 | |
Chris Lattner | 9ec975a | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 257 | return V; |
| 258 | } |
| 259 | |
Chris Lattner | 991ce36 | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 260 | /// LowerCTLZ - Emit the code to lower ctlz of V before the specified |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 261 | /// instruction IP. |
Chris Lattner | 991ce36 | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 262 | static Value *LowerCTLZ(Value *V, Instruction *IP) { |
Chris Lattner | 991ce36 | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 263 | |
| 264 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 265 | for (unsigned i = 1; i != BitSize; i <<= 1) { |
| 266 | Value *ShVal = ConstantInt::get(Type::UByteTy, i); |
Reid Spencer | fdff938 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 267 | ShVal = new ShiftInst(Instruction::LShr, V, ShVal, "ctlz.sh", IP); |
Chris Lattner | 991ce36 | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 268 | V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP); |
| 269 | } |
| 270 | |
Chris Lattner | 991ce36 | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 271 | V = BinaryOperator::createNot(V, "", IP); |
| 272 | return LowerCTPOP(V, IP); |
| 273 | } |
| 274 | |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 275 | |
| 276 | |
Chris Lattner | 2775aba | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 277 | void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 278 | Function *Callee = CI->getCalledFunction(); |
| 279 | assert(Callee && "Cannot lower an indirect call!"); |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 280 | |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 281 | switch (Callee->getIntrinsicID()) { |
| 282 | case Intrinsic::not_intrinsic: |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 283 | cerr << "Cannot lower a call to a non-intrinsic function '" |
| 284 | << Callee->getName() << "'!\n"; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 285 | abort(); |
| 286 | default: |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 287 | cerr << "Error: Code generator does not support intrinsic function '" |
| 288 | << Callee->getName() << "'!\n"; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 289 | abort(); |
| 290 | |
| 291 | // The setjmp/longjmp intrinsics should only exist in the code if it was |
| 292 | // never optimized (ie, right out of the CFE), or if it has been hacked on |
| 293 | // by the lowerinvoke pass. In both cases, the right thing to do is to |
| 294 | // convert the call to an explicit setjmp or longjmp call. |
| 295 | case Intrinsic::setjmp: { |
| 296 | static Function *SetjmpFCache = 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 297 | static const unsigned castOpcodes[] = { Instruction::BitCast }; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 298 | Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(), |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 299 | castOpcodes, Type::IntTy, SetjmpFCache); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 300 | if (CI->getType() != Type::VoidTy) |
| 301 | CI->replaceAllUsesWith(V); |
| 302 | break; |
| 303 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 304 | case Intrinsic::sigsetjmp: |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 305 | if (CI->getType() != Type::VoidTy) |
| 306 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 307 | break; |
| 308 | |
| 309 | case Intrinsic::longjmp: { |
| 310 | static Function *LongjmpFCache = 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 311 | static const unsigned castOpcodes[] = |
| 312 | { Instruction::BitCast, 0 }; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 313 | ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(), |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 314 | castOpcodes, Type::VoidTy, LongjmpFCache); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 315 | break; |
| 316 | } |
| 317 | |
| 318 | case Intrinsic::siglongjmp: { |
| 319 | // Insert the call to abort |
| 320 | static Function *AbortFCache = 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 321 | static const unsigned castOpcodes[] = |
| 322 | { Instruction::BitCast, 0 }; |
| 323 | ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), |
| 324 | castOpcodes, Type::VoidTy, AbortFCache); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 325 | break; |
| 326 | } |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 327 | case Intrinsic::ctpop_i8: |
| 328 | case Intrinsic::ctpop_i16: |
| 329 | case Intrinsic::ctpop_i32: |
| 330 | case Intrinsic::ctpop_i64: |
| 331 | CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI)); |
| 332 | break; |
| 333 | |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 334 | case Intrinsic::bswap_i16: |
| 335 | case Intrinsic::bswap_i32: |
| 336 | case Intrinsic::bswap_i64: |
| 337 | CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI)); |
| 338 | break; |
| 339 | |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 340 | case Intrinsic::ctlz_i8: |
| 341 | case Intrinsic::ctlz_i16: |
| 342 | case Intrinsic::ctlz_i32: |
| 343 | case Intrinsic::ctlz_i64: |
Chris Lattner | 991ce36 | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 344 | CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI)); |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 345 | break; |
Nate Begeman | 7d831fa | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 346 | |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 347 | case Intrinsic::cttz_i8: |
| 348 | case Intrinsic::cttz_i16: |
| 349 | case Intrinsic::cttz_i32: |
| 350 | case Intrinsic::cttz_i64: { |
Chris Lattner | fe5759b | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 351 | // cttz(x) -> ctpop(~X & (X-1)) |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 352 | Value *Src = CI->getOperand(1); |
Chris Lattner | 9ec975a | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 353 | Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI); |
Chris Lattner | fe5759b | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 354 | Value *SrcM1 = ConstantInt::get(Src->getType(), 1); |
| 355 | SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI); |
| 356 | Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI); |
Andrew Lenharth | 5e17782 | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 357 | CI->replaceAllUsesWith(Src); |
| 358 | break; |
| 359 | } |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 3b2b0af | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 361 | case Intrinsic::stacksave: |
| 362 | case Intrinsic::stackrestore: { |
| 363 | static bool Warned = false; |
| 364 | if (!Warned) |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 365 | cerr << "WARNING: this target does not support the llvm.stack" |
| 366 | << (Callee->getIntrinsicID() == Intrinsic::stacksave ? |
| 367 | "save" : "restore") << " intrinsic.\n"; |
Chris Lattner | 3b2b0af | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 368 | Warned = true; |
| 369 | if (Callee->getIntrinsicID() == Intrinsic::stacksave) |
| 370 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 371 | break; |
| 372 | } |
| 373 | |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 374 | case Intrinsic::returnaddress: |
| 375 | case Intrinsic::frameaddress: |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 376 | cerr << "WARNING: this target does not support the llvm." |
| 377 | << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? |
| 378 | "return" : "frame") << "address intrinsic.\n"; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 379 | CI->replaceAllUsesWith(ConstantPointerNull::get( |
| 380 | cast<PointerType>(CI->getType()))); |
| 381 | break; |
| 382 | |
Chris Lattner | c87e03a | 2005-02-28 19:27:23 +0000 | [diff] [blame] | 383 | case Intrinsic::prefetch: |
| 384 | break; // Simply strip out prefetches on unsupported architectures |
| 385 | |
Andrew Lenharth | b442791 | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 386 | case Intrinsic::pcmarker: |
| 387 | break; // Simply strip out pcmarker on unsupported architectures |
Andrew Lenharth | 01aa563 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 388 | case Intrinsic::readcyclecounter: { |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 389 | cerr << "WARNING: this target does not support the llvm.readcyclecoun" |
| 390 | << "ter intrinsic. It is being lowered to a constant 0\n"; |
Reid Spencer | e0fc4df | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 391 | CI->replaceAllUsesWith(ConstantInt::get(Type::ULongTy, 0)); |
Andrew Lenharth | 01aa563 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 392 | break; |
| 393 | } |
Andrew Lenharth | b442791 | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 394 | |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 395 | case Intrinsic::dbg_stoppoint: |
| 396 | case Intrinsic::dbg_region_start: |
| 397 | case Intrinsic::dbg_region_end: |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 398 | case Intrinsic::dbg_func_start: |
Jim Laskey | a8bdac8 | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 399 | case Intrinsic::dbg_declare: |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 400 | break; // Simply strip out debugging intrinsics |
| 401 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 402 | case Intrinsic::memcpy_i32: { |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 403 | // The memcpy intrinsic take an extra alignment argument that the memcpy |
| 404 | // libc function does not. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 405 | static unsigned opcodes[] = |
| 406 | { Instruction::BitCast, Instruction::BitCast, Instruction::BitCast }; |
| 407 | // FIXME: |
| 408 | // if (target_is_64_bit) opcodes[2] = Instruction::ZExt; |
| 409 | // else opcodes[2] = Instruction::BitCast; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 410 | static Function *MemcpyFCache = 0; |
| 411 | ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1, |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 412 | opcodes, (*(CI->op_begin()+1))->getType(), MemcpyFCache); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 413 | break; |
| 414 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 415 | case Intrinsic::memcpy_i64: { |
| 416 | static unsigned opcodes[] = |
| 417 | { Instruction::BitCast, Instruction::BitCast, Instruction::Trunc }; |
| 418 | // FIXME: |
| 419 | // if (target_is_64_bit) opcodes[2] = Instruction::BitCast; |
| 420 | // else opcodes[2] = Instruction::Trunc; |
| 421 | static Function *MemcpyFCache = 0; |
| 422 | ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1, |
| 423 | opcodes, (*(CI->op_begin()+1))->getType(), MemcpyFCache); |
| 424 | break; |
| 425 | } |
| 426 | case Intrinsic::memmove_i32: { |
| 427 | // The memmove intrinsic take an extra alignment argument that the memmove |
| 428 | // libc function does not. |
| 429 | static unsigned opcodes[] = |
| 430 | { Instruction::BitCast, Instruction::BitCast, Instruction::BitCast }; |
| 431 | // FIXME: |
| 432 | // if (target_is_64_bit) opcodes[2] = Instruction::ZExt; |
| 433 | // else opcodes[2] = Instruction::BitCast; |
| 434 | static Function *MemmoveFCache = 0; |
| 435 | ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1, |
| 436 | opcodes, (*(CI->op_begin()+1))->getType(), MemmoveFCache); |
| 437 | break; |
| 438 | } |
Chris Lattner | 093c159 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 439 | case Intrinsic::memmove_i64: { |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 440 | // The memmove intrinsic take an extra alignment argument that the memmove |
| 441 | // libc function does not. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 442 | static const unsigned opcodes[] = |
| 443 | { Instruction::BitCast, Instruction::BitCast, Instruction::Trunc }; |
| 444 | // if (target_is_64_bit) opcodes[2] = Instruction::BitCast; |
| 445 | // else opcodes[2] = Instruction::Trunc; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 446 | static Function *MemmoveFCache = 0; |
| 447 | ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1, |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 448 | opcodes, (*(CI->op_begin()+1))->getType(), MemmoveFCache); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 449 | break; |
| 450 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 451 | case Intrinsic::memset_i32: { |
| 452 | // The memset intrinsic take an extra alignment argument that the memset |
| 453 | // libc function does not. |
| 454 | static const unsigned opcodes[] = |
| 455 | { Instruction::BitCast, Instruction::ZExt, Instruction::ZExt, 0 }; |
| 456 | // if (target_is_64_bit) opcodes[2] = Instruction::BitCast; |
| 457 | // else opcodes[2] = Instruction::ZExt; |
| 458 | static Function *MemsetFCache = 0; |
| 459 | ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1, |
| 460 | opcodes, (*(CI->op_begin()+1))->getType(), MemsetFCache); |
| 461 | } |
Chris Lattner | 093c159 | 2006-03-03 00:00:25 +0000 | [diff] [blame] | 462 | case Intrinsic::memset_i64: { |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 463 | // The memset intrinsic take an extra alignment argument that the memset |
| 464 | // libc function does not. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 465 | static const unsigned opcodes[] = |
| 466 | { Instruction::BitCast, Instruction::ZExt, Instruction::Trunc, 0 }; |
| 467 | // if (target_is_64_bit) opcodes[2] = Instruction::BitCast; |
| 468 | // else opcodes[2] = Instruction::Trunc; |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 469 | static Function *MemsetFCache = 0; |
| 470 | ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1, |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 471 | opcodes, (*(CI->op_begin()+1))->getType(), MemsetFCache); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 472 | break; |
| 473 | } |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 474 | case Intrinsic::isunordered_f32: |
| 475 | case Intrinsic::isunordered_f64: { |
Alkis Evlogimenos | b3846f4 | 2005-03-01 02:07:58 +0000 | [diff] [blame] | 476 | Value *L = CI->getOperand(1); |
| 477 | Value *R = CI->getOperand(2); |
| 478 | |
| 479 | Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI); |
| 480 | Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI); |
| 481 | CI->replaceAllUsesWith( |
| 482 | BinaryOperator::create(Instruction::Or, LIsNan, RIsNan, |
| 483 | "isunordered", CI)); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 484 | break; |
| 485 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 486 | case Intrinsic::sqrt_f32: { |
| 487 | static const unsigned opcodes[] = { 0 }; |
Chris Lattner | 30fe4ac2 | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 488 | static Function *sqrtfFCache = 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 489 | ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(), |
| 490 | opcodes, Type::FloatTy, sqrtfFCache); |
| 491 | break; |
| 492 | } |
| 493 | case Intrinsic::sqrt_f64: { |
| 494 | static const unsigned opcodes[] = { 0 }; |
| 495 | static Function *sqrtFCache = 0; |
| 496 | ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(), |
| 497 | opcodes, Type::DoubleTy, sqrtFCache); |
Chris Lattner | 30fe4ac2 | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 498 | break; |
| 499 | } |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 500 | } |
Misha Brukman | 835702a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 501 | |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 502 | assert(CI->use_empty() && |
| 503 | "Lowering should have eliminated any uses of the intrinsic call!"); |
Chris Lattner | 9ec975a | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 504 | CI->eraseFromParent(); |
Chris Lattner | bcdadf3 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 505 | } |