| 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 | // |
| 10 | // This file implements the default intrinsic lowering implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| Chris Lattner | 3048373 | 2004-06-20 07:49:54 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/IntrinsicLowering.h" |
| Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
| Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 19 | #include "llvm/Type.h" |
| Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 20 | #include <iostream> |
| 21 | |
| 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> |
| 25 | static Function *EnsureFunctionExists(Module &M, const char *Name, |
| 26 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 27 | const Type *RetTy) { |
| 28 | if (Function *F = M.getNamedFunction(Name)) return F; |
| 29 | // It doesn't already exist in the program, insert a new definition now. |
| 30 | std::vector<const Type *> ParamTys; |
| 31 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 32 | ParamTys.push_back(I->getType()); |
| 33 | return M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); |
| 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, |
| 42 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 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 | } |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 57 | } |
| Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 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)) |
| 66 | Arg = new CastInst(Arg, FT->getParamType(ArgNo), Arg->getName(), CI); |
| 67 | Operands.push_back(Arg); |
| 68 | } |
| 69 | // Pass nulls into any additional arguments... |
| 70 | for (; ArgNo != FT->getNumParams(); ++ArgNo) |
| 71 | Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo))); |
| 72 | |
| 73 | std::string Name = CI->getName(); CI->setName(""); |
| 74 | if (FT->getReturnType() == Type::VoidTy) Name.clear(); |
| Chris Lattner | 02348ca | 2004-06-11 02:54:02 +0000 | [diff] [blame] | 75 | CallInst *NewCI = new CallInst(FCache, Operands, Name, CI); |
| 76 | if (!CI->use_empty()) { |
| 77 | Value *V = NewCI; |
| 78 | if (CI->getType() != NewCI->getType()) |
| 79 | V = new CastInst(NewCI, CI->getType(), Name, CI); |
| 80 | CI->replaceAllUsesWith(V); |
| 81 | } |
| 82 | return NewCI; |
| Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 85 | void DefaultIntrinsicLowering::AddPrototypes(Module &M) { |
| 86 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 87 | if (I->isExternal() && !I->use_empty()) |
| 88 | switch (I->getIntrinsicID()) { |
| 89 | default: break; |
| 90 | case Intrinsic::setjmp: |
| Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 91 | EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(), |
| 92 | Type::IntTy); |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 93 | break; |
| 94 | case Intrinsic::longjmp: |
| Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 95 | EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(), |
| 96 | Type::VoidTy); |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 97 | break; |
| 98 | case Intrinsic::siglongjmp: |
| Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 99 | EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(), |
| 100 | Type::VoidTy); |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 101 | break; |
| 102 | case Intrinsic::memcpy: |
| Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 103 | EnsureFunctionExists(M, "memcpy", I->arg_begin(), --I->arg_end(), |
| 104 | I->arg_begin()->getType()); |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 105 | break; |
| 106 | case Intrinsic::memmove: |
| Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 107 | EnsureFunctionExists(M, "memmove", I->arg_begin(), --I->arg_end(), |
| 108 | I->arg_begin()->getType()); |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 109 | break; |
| 110 | case Intrinsic::memset: |
| Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 111 | M.getOrInsertFunction("memset", PointerType::get(Type::SByteTy), |
| 112 | PointerType::get(Type::SByteTy), |
| 113 | Type::IntTy, (--(--I->arg_end()))->getType(), 0); |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 114 | break; |
| Chris Lattner | be78ac4 | 2004-06-15 21:42:23 +0000 | [diff] [blame] | 115 | case Intrinsic::isunordered: |
| Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 116 | EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(), |
| 117 | Type::BoolTy); |
| Chris Lattner | 02348ca | 2004-06-11 02:54:02 +0000 | [diff] [blame] | 118 | break; |
| Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 119 | case Intrinsic::sqrt: |
| Alkis Evlogimenos | b1beff0 | 2005-04-30 07:13:31 +0000 | [diff] [blame] | 120 | if(I->arg_begin()->getType() == Type::FloatTy) |
| Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 121 | EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(), |
| 122 | Type::FloatTy); |
| Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 123 | else |
| Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 124 | EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(), |
| 125 | Type::DoubleTy); |
| Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 126 | break; |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 127 | } |
| Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 128 | } |
| Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 129 | |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 130 | void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { |
| 131 | Function *Callee = CI->getCalledFunction(); |
| 132 | assert(Callee && "Cannot lower an indirect call!"); |
| Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 133 | |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 134 | switch (Callee->getIntrinsicID()) { |
| 135 | case Intrinsic::not_intrinsic: |
| 136 | std::cerr << "Cannot lower a call to a non-intrinsic function '" |
| 137 | << Callee->getName() << "'!\n"; |
| 138 | abort(); |
| 139 | default: |
| 140 | std::cerr << "Error: Code generator does not support intrinsic function '" |
| 141 | << Callee->getName() << "'!\n"; |
| 142 | abort(); |
| 143 | |
| Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 144 | // The setjmp/longjmp intrinsics should only exist in the code if it was |
| 145 | // never optimized (ie, right out of the CFE), or if it has been hacked on |
| 146 | // by the lowerinvoke pass. In both cases, the right thing to do is to |
| 147 | // convert the call to an explicit setjmp or longjmp call. |
| Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 148 | case Intrinsic::setjmp: { |
| 149 | static Function *SetjmpFCache = 0; |
| 150 | Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(), |
| 151 | Type::IntTy, SetjmpFCache); |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 152 | if (CI->getType() != Type::VoidTy) |
| Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 153 | CI->replaceAllUsesWith(V); |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 154 | break; |
| Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 155 | } |
| Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 156 | case Intrinsic::sigsetjmp: |
| Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 157 | if (CI->getType() != Type::VoidTy) |
| 158 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 159 | break; |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 160 | |
| Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 161 | case Intrinsic::longjmp: { |
| Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 162 | static Function *LongjmpFCache = 0; |
| 163 | ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(), |
| 164 | Type::VoidTy, LongjmpFCache); |
| 165 | break; |
| Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 166 | } |
| Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 167 | |
| Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 168 | case Intrinsic::siglongjmp: { |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 169 | // Insert the call to abort |
| Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 170 | static Function *AbortFCache = 0; |
| 171 | ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy, |
| 172 | AbortFCache); |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 173 | break; |
| Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 174 | } |
| Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 175 | case Intrinsic::ctpop: { |
| 176 | Value *Src = CI->getOperand(1); |
| 177 | switch (CI->getOperand(0)->getType()->getTypeID()) |
| 178 | { |
| 179 | case Type::SByteTyID: |
| 180 | case Type::UByteTyID: |
| 181 | { |
| 182 | Value* SA = ConstantUInt::get(Type::UByteTy, 1); |
| 183 | Value* MA = ConstantUInt::get(Type::UIntTy, 0x55); |
| 184 | Src = BinaryOperator::create(Instruction::Add, |
| 185 | BinaryOperator::create(Instruction::And, Src, MA), |
| 186 | BinaryOperator::create(Instruction::And, |
| 187 | new ShiftInst(Instruction::Shr, Src, SA), |
| 188 | MA)); |
| 189 | SA = ConstantUInt::get(Type::UByteTy, 2); |
| 190 | MA = ConstantUInt::get(Type::UIntTy, 0x33); |
| 191 | Src = BinaryOperator::create(Instruction::Add, |
| 192 | BinaryOperator::create(Instruction::And, Src, MA), |
| 193 | BinaryOperator::create(Instruction::And, |
| 194 | new ShiftInst(Instruction::Shr, Src, SA), |
| 195 | MA)); |
| 196 | SA = ConstantUInt::get(Type::UByteTy, 4); |
| 197 | MA = ConstantUInt::get(Type::UIntTy, 0x0F); |
| 198 | Src = BinaryOperator::create(Instruction::Add, |
| 199 | BinaryOperator::create(Instruction::And, Src, MA), |
| 200 | BinaryOperator::create(Instruction::And, |
| 201 | new ShiftInst(Instruction::Shr, Src, SA), |
| 202 | MA), "ctpop"); |
| 203 | } |
| 204 | break; |
| 205 | case Type::ShortTyID: |
| 206 | case Type::UShortTyID: |
| 207 | { |
| 208 | Value* SA = ConstantUInt::get(Type::UByteTy, 1); |
| 209 | Value* MA = ConstantUInt::get(Type::UIntTy, 0x5555); |
| 210 | Src = BinaryOperator::create(Instruction::Add, |
| 211 | BinaryOperator::create(Instruction::And, Src, MA), |
| 212 | BinaryOperator::create(Instruction::And, |
| 213 | new ShiftInst(Instruction::Shr, Src, SA), |
| 214 | MA)); |
| 215 | SA = ConstantUInt::get(Type::UByteTy, 2); |
| 216 | MA = ConstantUInt::get(Type::UIntTy, 0x3333); |
| 217 | Src = BinaryOperator::create(Instruction::Add, |
| 218 | BinaryOperator::create(Instruction::And, Src, MA), |
| 219 | BinaryOperator::create(Instruction::And, |
| 220 | new ShiftInst(Instruction::Shr, Src, SA), |
| 221 | MA)); |
| 222 | SA = ConstantUInt::get(Type::UByteTy, 4); |
| 223 | MA = ConstantUInt::get(Type::UIntTy, 0x0F0F); |
| 224 | Src = BinaryOperator::create(Instruction::Add, |
| 225 | BinaryOperator::create(Instruction::And, Src, MA), |
| 226 | BinaryOperator::create(Instruction::And, |
| 227 | new ShiftInst(Instruction::Shr, Src, SA), |
| 228 | MA)); |
| 229 | SA = ConstantUInt::get(Type::UByteTy, 8); |
| 230 | MA = ConstantUInt::get(Type::UIntTy, 0x00FF); |
| 231 | Src = BinaryOperator::create(Instruction::Add, |
| 232 | BinaryOperator::create(Instruction::And, Src, MA), |
| 233 | BinaryOperator::create(Instruction::And, |
| 234 | new ShiftInst(Instruction::Shr, Src, SA), |
| 235 | MA), "ctpop"); |
| 236 | |
| 237 | } |
| 238 | break; |
| 239 | case Type::IntTyID: |
| 240 | case Type::UIntTyID: |
| 241 | { |
| 242 | Value* SA = ConstantUInt::get(Type::UByteTy, 1); |
| 243 | Value* MA = ConstantUInt::get(Type::UIntTy, 0x55555555); |
| 244 | Src = BinaryOperator::create(Instruction::Add, |
| 245 | BinaryOperator::create(Instruction::And, Src, MA), |
| 246 | BinaryOperator::create(Instruction::And, |
| 247 | new ShiftInst(Instruction::Shr, Src, SA), |
| 248 | MA)); |
| 249 | SA = ConstantUInt::get(Type::UByteTy, 2); |
| 250 | MA = ConstantUInt::get(Type::UIntTy, 0x33333333); |
| 251 | Src = BinaryOperator::create(Instruction::Add, |
| 252 | BinaryOperator::create(Instruction::And, Src, MA), |
| 253 | BinaryOperator::create(Instruction::And, |
| 254 | new ShiftInst(Instruction::Shr, Src, SA), |
| 255 | MA)); |
| 256 | SA = ConstantUInt::get(Type::UByteTy, 4); |
| 257 | MA = ConstantUInt::get(Type::UIntTy, 0x0F0F0F0F); |
| 258 | Src = BinaryOperator::create(Instruction::Add, |
| 259 | BinaryOperator::create(Instruction::And, Src, MA), |
| 260 | BinaryOperator::create(Instruction::And, |
| 261 | new ShiftInst(Instruction::Shr, Src, SA), |
| 262 | MA)); |
| 263 | SA = ConstantUInt::get(Type::UByteTy, 8); |
| 264 | MA = ConstantUInt::get(Type::UIntTy, 0x00FF00FF); |
| 265 | Src = BinaryOperator::create(Instruction::Add, |
| 266 | BinaryOperator::create(Instruction::And, Src, MA), |
| 267 | BinaryOperator::create(Instruction::And, |
| 268 | new ShiftInst(Instruction::Shr, Src, SA), |
| 269 | MA)); |
| 270 | SA = ConstantUInt::get(Type::UByteTy, 8); |
| 271 | MA = ConstantUInt::get(Type::UIntTy, 0x0000FFFF); |
| 272 | Src = BinaryOperator::create(Instruction::Add, |
| 273 | BinaryOperator::create(Instruction::And, Src, MA), |
| 274 | BinaryOperator::create(Instruction::And, |
| 275 | new ShiftInst(Instruction::Shr, Src, SA), |
| 276 | MA), "ctpop"); |
| 277 | } |
| 278 | break; |
| 279 | case Type::LongTyID: |
| 280 | case Type::ULongTyID: |
| 281 | { |
| 282 | Value* SA = ConstantUInt::get(Type::UByteTy, 1); |
| 283 | Value* MA = ConstantUInt::get(Type::ULongTy, 0x5555555555555555ULL); |
| 284 | Src = BinaryOperator::create(Instruction::Add, |
| 285 | BinaryOperator::create(Instruction::And, Src, MA), |
| 286 | BinaryOperator::create(Instruction::And, |
| 287 | new ShiftInst(Instruction::Shr, Src, SA), |
| 288 | MA)); |
| 289 | SA = ConstantUInt::get(Type::UByteTy, 2); |
| 290 | MA = ConstantUInt::get(Type::ULongTy, 0x3333333333333333ULL); |
| 291 | Src = BinaryOperator::create(Instruction::Add, |
| 292 | BinaryOperator::create(Instruction::And, Src, MA), |
| 293 | BinaryOperator::create(Instruction::And, |
| 294 | new ShiftInst(Instruction::Shr, Src, SA), |
| 295 | MA)); |
| 296 | SA = ConstantUInt::get(Type::UByteTy, 4); |
| 297 | MA = ConstantUInt::get(Type::ULongTy, 0x0F0F0F0F0F0F0F0FULL); |
| 298 | Src = BinaryOperator::create(Instruction::Add, |
| 299 | BinaryOperator::create(Instruction::And, Src, MA), |
| 300 | BinaryOperator::create(Instruction::And, |
| 301 | new ShiftInst(Instruction::Shr, Src, SA), |
| 302 | MA)); |
| 303 | SA = ConstantUInt::get(Type::UByteTy, 8); |
| 304 | MA = ConstantUInt::get(Type::ULongTy, 0x00FF00FF00FF00FFULL); |
| 305 | Src = BinaryOperator::create(Instruction::Add, |
| 306 | BinaryOperator::create(Instruction::And, Src, MA), |
| 307 | BinaryOperator::create(Instruction::And, |
| 308 | new ShiftInst(Instruction::Shr, Src, SA), |
| 309 | MA)); |
| 310 | SA = ConstantUInt::get(Type::UByteTy, 16); |
| 311 | MA = ConstantUInt::get(Type::ULongTy, 0x00000000FFFFFFFFULL); |
| 312 | Src = BinaryOperator::create(Instruction::Add, |
| 313 | BinaryOperator::create(Instruction::And, Src, MA), |
| 314 | BinaryOperator::create(Instruction::And, |
| 315 | new ShiftInst(Instruction::Shr, Src, SA), |
| 316 | MA), "ctpop"); |
| 317 | } |
| 318 | break; |
| 319 | default: |
| 320 | abort(); |
| 321 | } |
| 322 | |
| 323 | CI->replaceAllUsesWith(Src); |
| 324 | break; |
| 325 | } |
| 326 | case Intrinsic::ctlz: { |
| 327 | Value *Src = CI->getOperand(1); |
| 328 | Value* SA; |
| 329 | switch (CI->getOperand(0)->getType()->getTypeID()) |
| 330 | { |
| 331 | case Type::LongTyID: |
| 332 | case Type::ULongTyID: |
| 333 | SA = ConstantUInt::get(Type::UByteTy, 32); |
| 334 | Src = BinaryOperator::create(Instruction::Or, Src, new ShiftInst(Instruction::Shr, Src, SA)); |
| 335 | case Type::IntTyID: |
| 336 | case Type::UIntTyID: |
| 337 | SA = ConstantUInt::get(Type::UByteTy, 16); |
| 338 | Src = BinaryOperator::create(Instruction::Or, Src, new ShiftInst(Instruction::Shr, Src, SA)); |
| 339 | case Type::ShortTyID: |
| 340 | case Type::UShortTyID: |
| 341 | SA = ConstantUInt::get(Type::UByteTy, 8); |
| 342 | Src = BinaryOperator::create(Instruction::Or, Src, new ShiftInst(Instruction::Shr, Src, SA)); |
| 343 | default: |
| 344 | SA = ConstantUInt::get(Type::UByteTy, 1); |
| 345 | Src = BinaryOperator::create(Instruction::Or, Src, new ShiftInst(Instruction::Shr, Src, SA)); |
| 346 | SA = ConstantUInt::get(Type::UByteTy, 2); |
| 347 | Src = BinaryOperator::create(Instruction::Or, Src, new ShiftInst(Instruction::Shr, Src, SA)); |
| 348 | SA = ConstantUInt::get(Type::UByteTy, 4); |
| 349 | Src = BinaryOperator::create(Instruction::Or, Src, new ShiftInst(Instruction::Shr, Src, SA)); |
| 350 | }; |
| 351 | Src = BinaryOperator::createNot(Src); |
| 352 | |
| 353 | Src = new CallInst(new Function(CI->getCalledFunction()->getFunctionType(), |
| 354 | CI->getCalledFunction()->getLinkage(), |
| 355 | "llvm.ctpop"), Src); |
| 356 | CI->replaceAllUsesWith(Src); |
| 357 | break; |
| 358 | } |
| 359 | case Intrinsic::cttz: { |
| 360 | Value *Src = CI->getOperand(1); |
| 361 | Src = BinaryOperator::create(Instruction::And, BinaryOperator::createNot(Src), |
| 362 | BinaryOperator::create(Instruction::Sub, Src, |
| 363 | ConstantUInt::get(CI->getOperand(0)->getType(), 1))); |
| 364 | Src = new CallInst(new Function(CI->getCalledFunction()->getFunctionType(), |
| 365 | CI->getCalledFunction()->getLinkage(), |
| 366 | "llvm.ctpop"), Src); |
| 367 | CI->replaceAllUsesWith(Src); |
| 368 | break; |
| 369 | } |
| Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 370 | |
| Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 371 | case Intrinsic::returnaddress: |
| 372 | case Intrinsic::frameaddress: |
| Chris Lattner | cc42d2c | 2004-02-14 04:52:06 +0000 | [diff] [blame] | 373 | std::cerr << "WARNING: this target does not support the llvm." |
| Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 374 | << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? |
| Chris Lattner | cc42d2c | 2004-02-14 04:52:06 +0000 | [diff] [blame] | 375 | "return" : "frame") << "address intrinsic.\n"; |
| Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 376 | CI->replaceAllUsesWith(ConstantPointerNull::get( |
| 377 | cast<PointerType>(CI->getType()))); |
| 378 | break; |
| 379 | |
| Chris Lattner | 0942b7c | 2005-02-28 19:27:23 +0000 | [diff] [blame] | 380 | case Intrinsic::prefetch: |
| 381 | break; // Simply strip out prefetches on unsupported architectures |
| 382 | |
| Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 383 | case Intrinsic::pcmarker: |
| 384 | break; // Simply strip out pcmarker on unsupported architectures |
| 385 | |
| Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 386 | case Intrinsic::dbg_stoppoint: |
| 387 | case Intrinsic::dbg_region_start: |
| 388 | case Intrinsic::dbg_region_end: |
| Chris Lattner | f907bac | 2004-01-14 20:41:29 +0000 | [diff] [blame] | 389 | case Intrinsic::dbg_declare: |
| Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 390 | case Intrinsic::dbg_func_start: |
| 391 | if (CI->getType() != Type::VoidTy) |
| 392 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 393 | break; // Simply strip out debugging intrinsics |
| Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 394 | |
| Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 395 | case Intrinsic::memcpy: { |
| Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 396 | // The memcpy intrinsic take an extra alignment argument that the memcpy |
| 397 | // libc function does not. |
| Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 398 | static Function *MemcpyFCache = 0; |
| 399 | ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1, |
| 400 | (*(CI->op_begin()+1))->getType(), MemcpyFCache); |
| Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 401 | break; |
| Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 402 | } |
| 403 | case Intrinsic::memmove: { |
| Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 404 | // The memmove intrinsic take an extra alignment argument that the memmove |
| Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 405 | // libc function does not. |
| Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 406 | static Function *MemmoveFCache = 0; |
| 407 | ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1, |
| 408 | (*(CI->op_begin()+1))->getType(), MemmoveFCache); |
| Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 409 | break; |
| Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 410 | } |
| 411 | case Intrinsic::memset: { |
| Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 412 | // The memset intrinsic take an extra alignment argument that the memset |
| 413 | // libc function does not. |
| Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 414 | static Function *MemsetFCache = 0; |
| 415 | ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1, |
| 416 | (*(CI->op_begin()+1))->getType(), MemsetFCache); |
| Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 417 | break; |
| 418 | } |
| Alkis Evlogimenos | 9685372 | 2004-06-12 19:19:14 +0000 | [diff] [blame] | 419 | case Intrinsic::isunordered: { |
| Alkis Evlogimenos | d0656fc | 2005-03-01 02:07:58 +0000 | [diff] [blame] | 420 | Value *L = CI->getOperand(1); |
| 421 | Value *R = CI->getOperand(2); |
| 422 | |
| 423 | Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI); |
| 424 | Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI); |
| 425 | CI->replaceAllUsesWith( |
| 426 | BinaryOperator::create(Instruction::Or, LIsNan, RIsNan, |
| 427 | "isunordered", CI)); |
| Alkis Evlogimenos | 9685372 | 2004-06-12 19:19:14 +0000 | [diff] [blame] | 428 | break; |
| 429 | } |
| Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 430 | case Intrinsic::sqrt: { |
| 431 | static Function *sqrtFCache = 0; |
| 432 | static Function *sqrtfFCache = 0; |
| 433 | if(CI->getType() == Type::FloatTy) |
| 434 | ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(), |
| 435 | Type::FloatTy, sqrtfFCache); |
| 436 | else |
| 437 | ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(), |
| 438 | Type::DoubleTy, sqrtFCache); |
| 439 | break; |
| 440 | } |
| Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 441 | } |
| Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 442 | |
| Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 443 | assert(CI->use_empty() && |
| 444 | "Lowering should have eliminated any uses of the intrinsic call!"); |
| 445 | CI->getParent()->getInstList().erase(CI); |
| 446 | } |