Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 1 | //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 10 | // This file implements the IntrinsicLowering class. |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 16 | #include "llvm/Module.h" |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 17 | #include "llvm/Type.h" |
Bill Wendling | d9fd2ac | 2006-11-28 02:08:17 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/IRBuilder.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 6addf2c | 2007-01-29 17:42:06 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 990b849 | 2007-02-13 06:01:22 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 26 | template <class ArgIt> |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 27 | static void EnsureFunctionExists(Module &M, const char *Name, |
| 28 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 29 | const Type *RetTy) { |
| 30 | // Insert a correctly-typed definition now. |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 31 | std::vector<const Type *> ParamTys; |
| 32 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 33 | ParamTys.push_back(I->getType()); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 34 | M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false)); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Chris Lattner | 556b4a6 | 2009-02-07 22:37:06 +0000 | [diff] [blame] | 37 | static void EnsureFPIntrinsicsExist(Module &M, Function *Fn, |
| 38 | const char *FName, |
| 39 | const char *DName, const char *LDName) { |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 40 | // Insert definitions for all the floating point types. |
Chris Lattner | 556b4a6 | 2009-02-07 22:37:06 +0000 | [diff] [blame] | 41 | switch((int)Fn->arg_begin()->getType()->getTypeID()) { |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 42 | case Type::FloatTyID: |
Chris Lattner | 556b4a6 | 2009-02-07 22:37:06 +0000 | [diff] [blame] | 43 | EnsureFunctionExists(M, FName, Fn->arg_begin(), Fn->arg_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 44 | Type::getFloatTy(M.getContext())); |
Chris Lattner | 556b4a6 | 2009-02-07 22:37:06 +0000 | [diff] [blame] | 45 | break; |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 46 | case Type::DoubleTyID: |
Chris Lattner | 556b4a6 | 2009-02-07 22:37:06 +0000 | [diff] [blame] | 47 | EnsureFunctionExists(M, DName, Fn->arg_begin(), Fn->arg_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 48 | Type::getDoubleTy(M.getContext())); |
Chris Lattner | 556b4a6 | 2009-02-07 22:37:06 +0000 | [diff] [blame] | 49 | break; |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 50 | case Type::X86_FP80TyID: |
| 51 | case Type::FP128TyID: |
| 52 | case Type::PPC_FP128TyID: |
Chris Lattner | 556b4a6 | 2009-02-07 22:37:06 +0000 | [diff] [blame] | 53 | EnsureFunctionExists(M, LDName, Fn->arg_begin(), Fn->arg_end(), |
| 54 | Fn->arg_begin()->getType()); |
| 55 | break; |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 59 | /// ReplaceCallWith - This function is used when we want to lower an intrinsic |
| 60 | /// call to a call of an external function. This handles hard cases such as |
| 61 | /// when there was already a prototype for the external function, and if that |
| 62 | /// prototype doesn't match the arguments we expect to pass in. |
| 63 | template <class ArgIt> |
| 64 | static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 65 | ArgIt ArgBegin, ArgIt ArgEnd, |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 66 | const Type *RetTy) { |
| 67 | // If we haven't already looked up this function, check to see if the |
| 68 | // program already contains a function with this name. |
| 69 | Module *M = CI->getParent()->getParent()->getParent(); |
| 70 | // Get or insert the definition now. |
| 71 | std::vector<const Type *> ParamTys; |
| 72 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 73 | ParamTys.push_back((*I)->getType()); |
| 74 | Constant* FCache = M->getOrInsertFunction(NewFn, |
| 75 | FunctionType::get(RetTy, ParamTys, false)); |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 76 | |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 77 | IRBuilder<> Builder(CI->getParent(), CI); |
David Greene | 52eec54 | 2007-08-01 03:43:44 +0000 | [diff] [blame] | 78 | SmallVector<Value *, 8> Args(ArgBegin, ArgEnd); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 79 | CallInst *NewCI = Builder.CreateCall(FCache, Args.begin(), Args.end()); |
| 80 | NewCI->setName(CI->getName()); |
Chris Lattner | b76efb7 | 2007-01-07 08:12:01 +0000 | [diff] [blame] | 81 | if (!CI->use_empty()) |
| 82 | CI->replaceAllUsesWith(NewCI); |
Chris Lattner | 02348ca | 2004-06-11 02:54:02 +0000 | [diff] [blame] | 83 | return NewCI; |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 86 | void IntrinsicLowering::AddPrototypes(Module &M) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 87 | LLVMContext &Context = M.getContext(); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 88 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 89 | if (I->isDeclaration() && !I->use_empty()) |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 90 | switch (I->getIntrinsicID()) { |
| 91 | default: break; |
| 92 | case Intrinsic::setjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 93 | EnsureFunctionExists(M, "setjmp", I->arg_begin(), I->arg_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 94 | Type::getInt32Ty(M.getContext())); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 95 | break; |
| 96 | case Intrinsic::longjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 97 | EnsureFunctionExists(M, "longjmp", I->arg_begin(), I->arg_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 98 | Type::getVoidTy(M.getContext())); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 99 | break; |
| 100 | case Intrinsic::siglongjmp: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 101 | EnsureFunctionExists(M, "abort", I->arg_end(), I->arg_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 102 | Type::getVoidTy(M.getContext())); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 103 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 104 | case Intrinsic::memcpy: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 105 | M.getOrInsertFunction("memcpy", |
| 106 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 107 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 108 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 109 | TD.getIntPtrType(Context), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 110 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 111 | case Intrinsic::memmove: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 112 | M.getOrInsertFunction("memmove", |
| 113 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 114 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 115 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 116 | TD.getIntPtrType(Context), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 117 | break; |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 118 | case Intrinsic::memset: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 119 | M.getOrInsertFunction("memset", |
| 120 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 121 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 122 | Type::getInt32Ty(M.getContext()), |
| 123 | TD.getIntPtrType(Context), (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 124 | break; |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 125 | case Intrinsic::sqrt: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 126 | EnsureFPIntrinsicsExist(M, I, "sqrtf", "sqrt", "sqrtl"); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 127 | break; |
Dan Gohman | c4c9660 | 2007-10-15 22:07:31 +0000 | [diff] [blame] | 128 | case Intrinsic::sin: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 129 | EnsureFPIntrinsicsExist(M, I, "sinf", "sin", "sinl"); |
Dan Gohman | c4c9660 | 2007-10-15 22:07:31 +0000 | [diff] [blame] | 130 | break; |
| 131 | case Intrinsic::cos: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 132 | EnsureFPIntrinsicsExist(M, I, "cosf", "cos", "cosl"); |
Dan Gohman | c4c9660 | 2007-10-15 22:07:31 +0000 | [diff] [blame] | 133 | break; |
| 134 | case Intrinsic::pow: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 135 | EnsureFPIntrinsicsExist(M, I, "powf", "pow", "powl"); |
Dan Gohman | c4c9660 | 2007-10-15 22:07:31 +0000 | [diff] [blame] | 136 | break; |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 137 | case Intrinsic::log: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 138 | EnsureFPIntrinsicsExist(M, I, "logf", "log", "logl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 139 | break; |
| 140 | case Intrinsic::log2: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 141 | EnsureFPIntrinsicsExist(M, I, "log2f", "log2", "log2l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 142 | break; |
| 143 | case Intrinsic::log10: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 144 | EnsureFPIntrinsicsExist(M, I, "log10f", "log10", "log10l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 145 | break; |
| 146 | case Intrinsic::exp: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 147 | EnsureFPIntrinsicsExist(M, I, "expf", "exp", "expl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 148 | break; |
| 149 | case Intrinsic::exp2: |
Dale Johannesen | c4342ea | 2008-09-22 19:51:58 +0000 | [diff] [blame] | 150 | EnsureFPIntrinsicsExist(M, I, "exp2f", "exp2", "exp2l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 151 | break; |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 153 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 154 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 155 | /// LowerBSWAP - Emit the code to lower bswap of V before the specified |
| 156 | /// instruction IP. |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 157 | static Value *LowerBSWAP(LLVMContext &Context, Value *V, Instruction *IP) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 158 | assert(V->getType()->isInteger() && "Can't bswap a non-integer type!"); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 159 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 160 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 161 | |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 162 | IRBuilder<> Builder(IP->getParent(), IP); |
| 163 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 164 | switch(BitSize) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 165 | default: llvm_unreachable("Unhandled type size of value to byteswap!"); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 166 | case 16: { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 167 | Value *Tmp1 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 168 | "bswap.2"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 169 | Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 170 | "bswap.1"); |
| 171 | V = Builder.CreateOr(Tmp1, Tmp2, "bswap.i16"); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 172 | break; |
| 173 | } |
| 174 | case 32: { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 175 | Value *Tmp4 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 176 | "bswap.4"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 177 | Value *Tmp3 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 178 | "bswap.3"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 179 | Value *Tmp2 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 180 | "bswap.2"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 181 | Value *Tmp1 = Builder.CreateLShr(V,ConstantInt::get(V->getType(), 24), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 182 | "bswap.1"); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 183 | Tmp3 = Builder.CreateAnd(Tmp3, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 184 | ConstantInt::get(Type::getInt32Ty(Context), 0xFF0000), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 185 | "bswap.and3"); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 186 | Tmp2 = Builder.CreateAnd(Tmp2, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 187 | ConstantInt::get(Type::getInt32Ty(Context), 0xFF00), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 188 | "bswap.and2"); |
| 189 | Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or1"); |
| 190 | Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or2"); |
| 191 | V = Builder.CreateOr(Tmp4, Tmp2, "bswap.i32"); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 192 | break; |
| 193 | } |
| 194 | case 64: { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 195 | Value *Tmp8 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 56), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 196 | "bswap.8"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 197 | Value *Tmp7 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 40), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 198 | "bswap.7"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 199 | Value *Tmp6 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 24), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 200 | "bswap.6"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 201 | Value *Tmp5 = Builder.CreateShl(V, ConstantInt::get(V->getType(), 8), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 202 | "bswap.5"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 203 | Value* Tmp4 = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 8), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 204 | "bswap.4"); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 205 | Value* Tmp3 = Builder.CreateLShr(V, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 206 | ConstantInt::get(V->getType(), 24), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 207 | "bswap.3"); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 208 | Value* Tmp2 = Builder.CreateLShr(V, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 209 | ConstantInt::get(V->getType(), 40), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 210 | "bswap.2"); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 211 | Value* Tmp1 = Builder.CreateLShr(V, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 212 | ConstantInt::get(V->getType(), 56), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 213 | "bswap.1"); |
| 214 | Tmp7 = Builder.CreateAnd(Tmp7, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 215 | ConstantInt::get(Type::getInt64Ty(Context), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 216 | 0xFF000000000000ULL), |
| 217 | "bswap.and7"); |
| 218 | Tmp6 = Builder.CreateAnd(Tmp6, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 219 | ConstantInt::get(Type::getInt64Ty(Context), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 220 | 0xFF0000000000ULL), |
| 221 | "bswap.and6"); |
| 222 | Tmp5 = Builder.CreateAnd(Tmp5, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 223 | ConstantInt::get(Type::getInt64Ty(Context), |
| 224 | 0xFF00000000ULL), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 225 | "bswap.and5"); |
| 226 | Tmp4 = Builder.CreateAnd(Tmp4, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 227 | ConstantInt::get(Type::getInt64Ty(Context), |
| 228 | 0xFF000000ULL), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 229 | "bswap.and4"); |
| 230 | Tmp3 = Builder.CreateAnd(Tmp3, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 231 | ConstantInt::get(Type::getInt64Ty(Context), |
| 232 | 0xFF0000ULL), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 233 | "bswap.and3"); |
| 234 | Tmp2 = Builder.CreateAnd(Tmp2, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 235 | ConstantInt::get(Type::getInt64Ty(Context), |
| 236 | 0xFF00ULL), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 237 | "bswap.and2"); |
| 238 | Tmp8 = Builder.CreateOr(Tmp8, Tmp7, "bswap.or1"); |
| 239 | Tmp6 = Builder.CreateOr(Tmp6, Tmp5, "bswap.or2"); |
| 240 | Tmp4 = Builder.CreateOr(Tmp4, Tmp3, "bswap.or3"); |
| 241 | Tmp2 = Builder.CreateOr(Tmp2, Tmp1, "bswap.or4"); |
| 242 | Tmp8 = Builder.CreateOr(Tmp8, Tmp6, "bswap.or5"); |
| 243 | Tmp4 = Builder.CreateOr(Tmp4, Tmp2, "bswap.or6"); |
| 244 | V = Builder.CreateOr(Tmp8, Tmp4, "bswap.i64"); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 245 | break; |
| 246 | } |
| 247 | } |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 248 | return V; |
| 249 | } |
| 250 | |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 251 | /// LowerCTPOP - Emit the code to lower ctpop of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 252 | /// instruction IP. |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 253 | static Value *LowerCTPOP(LLVMContext &Context, Value *V, Instruction *IP) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 254 | assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 255 | |
| 256 | static const uint64_t MaskValues[6] = { |
| 257 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 258 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 259 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 260 | }; |
| 261 | |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 262 | IRBuilder<> Builder(IP->getParent(), IP); |
| 263 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 264 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 265 | unsigned WordSize = (BitSize + 63) / 64; |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 266 | Value *Count = ConstantInt::get(V->getType(), 0); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 267 | |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 268 | for (unsigned n = 0; n < WordSize; ++n) { |
| 269 | Value *PartValue = V; |
| 270 | for (unsigned i = 1, ct = 0; i < (BitSize>64 ? 64 : BitSize); |
| 271 | i <<= 1, ++ct) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 272 | Value *MaskCst = ConstantInt::get(V->getType(), MaskValues[ct]); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 273 | Value *LHS = Builder.CreateAnd(PartValue, MaskCst, "cppop.and1"); |
| 274 | Value *VShift = Builder.CreateLShr(PartValue, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 275 | ConstantInt::get(V->getType(), i), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 276 | "ctpop.sh"); |
| 277 | Value *RHS = Builder.CreateAnd(VShift, MaskCst, "cppop.and2"); |
| 278 | PartValue = Builder.CreateAdd(LHS, RHS, "ctpop.step"); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 279 | } |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 280 | Count = Builder.CreateAdd(PartValue, Count, "ctpop.part"); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 281 | if (BitSize > 64) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 282 | V = Builder.CreateLShr(V, ConstantInt::get(V->getType(), 64), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 283 | "ctpop.part.sh"); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 284 | BitSize -= 64; |
| 285 | } |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Chris Lattner | 914ce45 | 2007-08-06 16:36:18 +0000 | [diff] [blame] | 288 | return Count; |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 291 | /// LowerCTLZ - Emit the code to lower ctlz of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 292 | /// instruction IP. |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 293 | static Value *LowerCTLZ(LLVMContext &Context, Value *V, Instruction *IP) { |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 294 | |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 295 | IRBuilder<> Builder(IP->getParent(), IP); |
| 296 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 297 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
Zhou Sheng | 02031c0 | 2007-06-02 04:10:33 +0000 | [diff] [blame] | 298 | for (unsigned i = 1; i < BitSize; i <<= 1) { |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 299 | Value *ShVal = ConstantInt::get(V->getType(), i); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 300 | ShVal = Builder.CreateLShr(V, ShVal, "ctlz.sh"); |
| 301 | V = Builder.CreateOr(V, ShVal, "ctlz.step"); |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 304 | V = Builder.CreateNot(V); |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 305 | return LowerCTPOP(Context, V, IP); |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 308 | static void ReplaceFPIntrinsicWithCall(CallInst *CI, const char *Fname, |
| 309 | const char *Dname, |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 310 | const char *LDname) { |
| 311 | switch (CI->getOperand(1)->getType()->getTypeID()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 312 | default: llvm_unreachable("Invalid type in intrinsic"); |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 313 | case Type::FloatTyID: |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 314 | ReplaceCallWith(Fname, CI, CI->op_begin() + 1, CI->op_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 315 | Type::getFloatTy(CI->getContext())); |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 316 | break; |
| 317 | case Type::DoubleTyID: |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 318 | ReplaceCallWith(Dname, CI, CI->op_begin() + 1, CI->op_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 319 | Type::getDoubleTy(CI->getContext())); |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 320 | break; |
| 321 | case Type::X86_FP80TyID: |
| 322 | case Type::FP128TyID: |
| 323 | case Type::PPC_FP128TyID: |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 324 | ReplaceCallWith(LDname, CI, CI->op_begin() + 1, CI->op_end(), |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 325 | CI->getOperand(1)->getType()); |
Dale Johannesen | f74185b | 2008-09-22 20:51:30 +0000 | [diff] [blame] | 326 | break; |
| 327 | } |
| 328 | } |
Reid Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 329 | |
Chris Lattner | b71fd78 | 2006-11-15 18:00:10 +0000 | [diff] [blame] | 330 | void IntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 331 | IRBuilder<> Builder(CI->getParent(), CI); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 332 | LLVMContext &Context = CI->getContext(); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 334 | Function *Callee = CI->getCalledFunction(); |
| 335 | assert(Callee && "Cannot lower an indirect call!"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 337 | switch (Callee->getIntrinsicID()) { |
| 338 | case Intrinsic::not_intrinsic: |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 339 | llvm_report_error("Cannot lower a call to a non-intrinsic function '"+ |
| 340 | Callee->getName() + "'!"); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 341 | default: |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 342 | llvm_report_error("Code generator does not support intrinsic function '"+ |
| 343 | Callee->getName()+"'!"); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 345 | // The setjmp/longjmp intrinsics should only exist in the code if it was |
| 346 | // never optimized (ie, right out of the CFE), or if it has been hacked on |
| 347 | // by the lowerinvoke pass. In both cases, the right thing to do is to |
| 348 | // convert the call to an explicit setjmp or longjmp call. |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 349 | case Intrinsic::setjmp: { |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 350 | Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin() + 1, CI->op_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 351 | Type::getInt32Ty(Context)); |
| 352 | if (CI->getType() != Type::getVoidTy(Context)) |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 353 | CI->replaceAllUsesWith(V); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 354 | break; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 355 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 356 | case Intrinsic::sigsetjmp: |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 357 | if (CI->getType() != Type::getVoidTy(Context)) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 358 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 359 | break; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 360 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 361 | case Intrinsic::longjmp: { |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 362 | ReplaceCallWith("longjmp", CI, CI->op_begin() + 1, CI->op_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 363 | Type::getVoidTy(Context)); |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 364 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 365 | } |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 366 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 367 | case Intrinsic::siglongjmp: { |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 368 | // Insert the call to abort |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 369 | ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 370 | Type::getVoidTy(Context)); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 371 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 372 | } |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 373 | case Intrinsic::ctpop: |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 374 | CI->replaceAllUsesWith(LowerCTPOP(Context, CI->getOperand(1), CI)); |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 375 | break; |
| 376 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 377 | case Intrinsic::bswap: |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 378 | CI->replaceAllUsesWith(LowerBSWAP(Context, CI->getOperand(1), CI)); |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 379 | break; |
| 380 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 381 | case Intrinsic::ctlz: |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 382 | CI->replaceAllUsesWith(LowerCTLZ(Context, CI->getOperand(1), CI)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 383 | break; |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 384 | |
Reid Spencer | e9391fd | 2007-04-01 07:35:23 +0000 | [diff] [blame] | 385 | case Intrinsic::cttz: { |
Chris Lattner | a801172 | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 386 | // cttz(x) -> ctpop(~X & (X-1)) |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 387 | Value *Src = CI->getOperand(1); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 388 | Value *NotSrc = Builder.CreateNot(Src); |
| 389 | NotSrc->setName(Src->getName() + ".not"); |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 390 | Value *SrcM1 = ConstantInt::get(Src->getType(), 1); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 391 | SrcM1 = Builder.CreateSub(Src, SrcM1); |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 392 | Src = LowerCTPOP(Context, Builder.CreateAnd(NotSrc, SrcM1), CI); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 393 | CI->replaceAllUsesWith(Src); |
| 394 | break; |
| 395 | } |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 397 | case Intrinsic::stacksave: |
| 398 | case Intrinsic::stackrestore: { |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 399 | if (!Warned) |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame^] | 400 | errs() << "WARNING: this target does not support the llvm.stack" |
| 401 | << (Callee->getIntrinsicID() == Intrinsic::stacksave ? |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 402 | "save" : "restore") << " intrinsic.\n"; |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 403 | Warned = true; |
| 404 | if (Callee->getIntrinsicID() == Intrinsic::stacksave) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 405 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 409 | case Intrinsic::returnaddress: |
| 410 | case Intrinsic::frameaddress: |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame^] | 411 | errs() << "WARNING: this target does not support the llvm." |
| 412 | << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 413 | "return" : "frame") << "address intrinsic.\n"; |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 414 | CI->replaceAllUsesWith(ConstantPointerNull::get( |
| 415 | cast<PointerType>(CI->getType()))); |
| 416 | break; |
| 417 | |
Chris Lattner | 0942b7c | 2005-02-28 19:27:23 +0000 | [diff] [blame] | 418 | case Intrinsic::prefetch: |
| 419 | break; // Simply strip out prefetches on unsupported architectures |
| 420 | |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 421 | case Intrinsic::pcmarker: |
| 422 | break; // Simply strip out pcmarker on unsupported architectures |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 423 | case Intrinsic::readcyclecounter: { |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame^] | 424 | errs() << "WARNING: this target does not support the llvm.readcyclecoun" |
| 425 | << "ter intrinsic. It is being lowered to a constant 0\n"; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 426 | CI->replaceAllUsesWith(ConstantInt::get(Type::getInt64Ty(Context), 0)); |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 427 | break; |
| 428 | } |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 430 | case Intrinsic::dbg_stoppoint: |
| 431 | case Intrinsic::dbg_region_start: |
| 432 | case Intrinsic::dbg_region_end: |
| 433 | case Intrinsic::dbg_func_start: |
Jim Laskey | 43970fe | 2006-03-23 18:06:46 +0000 | [diff] [blame] | 434 | case Intrinsic::dbg_declare: |
Duncan Sands | f664e41 | 2007-07-06 14:46:23 +0000 | [diff] [blame] | 435 | break; // Simply strip out debugging intrinsics |
| 436 | |
Jim Laskey | b180aa1 | 2007-02-21 22:53:45 +0000 | [diff] [blame] | 437 | case Intrinsic::eh_exception: |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 438 | case Intrinsic::eh_selector_i32: |
| 439 | case Intrinsic::eh_selector_i64: |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 440 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
Duncan Sands | f664e41 | 2007-07-06 14:46:23 +0000 | [diff] [blame] | 441 | break; |
| 442 | |
Anton Korobeynikov | 8806c7b | 2007-09-07 11:39:35 +0000 | [diff] [blame] | 443 | case Intrinsic::eh_typeid_for_i32: |
| 444 | case Intrinsic::eh_typeid_for_i64: |
Duncan Sands | f664e41 | 2007-07-06 14:46:23 +0000 | [diff] [blame] | 445 | // Return something different to eh_selector. |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 446 | CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1)); |
Duncan Sands | f664e41 | 2007-07-06 14:46:23 +0000 | [diff] [blame] | 447 | break; |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 448 | |
Tanya Lattner | 24e5aad | 2007-06-15 22:26:58 +0000 | [diff] [blame] | 449 | case Intrinsic::var_annotation: |
| 450 | break; // Strip out annotate intrinsic |
| 451 | |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 452 | case Intrinsic::memcpy: { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 453 | const IntegerType *IntPtr = TD.getIntPtrType(Context); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 454 | Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr, |
| 455 | /* isSigned */ false); |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 456 | Value *Ops[3]; |
| 457 | Ops[0] = CI->getOperand(1); |
| 458 | Ops[1] = CI->getOperand(2); |
| 459 | Ops[2] = Size; |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 460 | ReplaceCallWith("memcpy", CI, Ops, Ops+3, CI->getOperand(1)->getType()); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 461 | break; |
| 462 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 463 | case Intrinsic::memmove: { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 464 | const IntegerType *IntPtr = TD.getIntPtrType(Context); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 465 | Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr, |
| 466 | /* isSigned */ false); |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 467 | Value *Ops[3]; |
| 468 | Ops[0] = CI->getOperand(1); |
| 469 | Ops[1] = CI->getOperand(2); |
| 470 | Ops[2] = Size; |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 471 | ReplaceCallWith("memmove", CI, Ops, Ops+3, CI->getOperand(1)->getType()); |
Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 472 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 473 | } |
Chris Lattner | 824b958 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 474 | case Intrinsic::memset: { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 475 | const IntegerType *IntPtr = TD.getIntPtrType(Context); |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 476 | Value *Size = Builder.CreateIntCast(CI->getOperand(3), IntPtr, |
| 477 | /* isSigned */ false); |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 478 | Value *Ops[3]; |
| 479 | Ops[0] = CI->getOperand(1); |
| 480 | // Extend the amount to i32. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 481 | Ops[1] = Builder.CreateIntCast(CI->getOperand(2), Type::getInt32Ty(Context), |
Jay Foad | e1e2014 | 2009-05-12 20:27:44 +0000 | [diff] [blame] | 482 | /* isSigned */ false); |
Chris Lattner | c67da0c | 2007-02-06 19:06:38 +0000 | [diff] [blame] | 483 | Ops[2] = Size; |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 484 | ReplaceCallWith("memset", CI, Ops, Ops+3, CI->getOperand(1)->getType()); |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 485 | break; |
| 486 | } |
Dale Johannesen | 9ab7fb3 | 2007-10-02 17:43:59 +0000 | [diff] [blame] | 487 | case Intrinsic::sqrt: { |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 488 | ReplaceFPIntrinsicWithCall(CI, "sqrtf", "sqrt", "sqrtl"); |
Dale Johannesen | 4292d1c | 2007-09-28 18:06:58 +0000 | [diff] [blame] | 489 | break; |
| 490 | } |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 491 | case Intrinsic::log: { |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 492 | ReplaceFPIntrinsicWithCall(CI, "logf", "log", "logl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 493 | break; |
| 494 | } |
| 495 | case Intrinsic::log2: { |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 496 | ReplaceFPIntrinsicWithCall(CI, "log2f", "log2", "log2l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 497 | break; |
| 498 | } |
| 499 | case Intrinsic::log10: { |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 500 | ReplaceFPIntrinsicWithCall(CI, "log10f", "log10", "log10l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 501 | break; |
| 502 | } |
| 503 | case Intrinsic::exp: { |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 504 | ReplaceFPIntrinsicWithCall(CI, "expf", "exp", "expl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 505 | break; |
| 506 | } |
| 507 | case Intrinsic::exp2: { |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 508 | ReplaceFPIntrinsicWithCall(CI, "exp2f", "exp2", "exp2l"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 509 | break; |
| 510 | } |
| 511 | case Intrinsic::pow: { |
Owen Anderson | b41b5e0 | 2009-06-26 20:33:47 +0000 | [diff] [blame] | 512 | ReplaceFPIntrinsicWithCall(CI, "powf", "pow", "powl"); |
Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 513 | break; |
| 514 | } |
Anton Korobeynikov | 917c2a6 | 2007-11-15 23:25:33 +0000 | [diff] [blame] | 515 | case Intrinsic::flt_rounds: |
| 516 | // Lower to "round to the nearest" |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 517 | if (CI->getType() != Type::getVoidTy(Context)) |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 518 | CI->replaceAllUsesWith(ConstantInt::get(CI->getType(), 1)); |
Anton Korobeynikov | 917c2a6 | 2007-11-15 23:25:33 +0000 | [diff] [blame] | 519 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 520 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 521 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 522 | assert(CI->use_empty() && |
| 523 | "Lowering should have eliminated any uses of the intrinsic call!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 524 | CI->eraseFromParent(); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 525 | } |