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), |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 113 | Type::IntTy, (--(--I->arg_end()))->getType(), |
| 114 | (Type *)0); |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 115 | break; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 116 | case Intrinsic::isunordered_f32: |
| 117 | case Intrinsic::isunordered_f64: |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 118 | EnsureFunctionExists(M, "isunordered", I->arg_begin(), I->arg_end(), |
| 119 | Type::BoolTy); |
Chris Lattner | 02348ca | 2004-06-11 02:54:02 +0000 | [diff] [blame] | 120 | break; |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 121 | case Intrinsic::sqrt_f32: |
| 122 | case Intrinsic::sqrt_f64: |
Alkis Evlogimenos | b1beff0 | 2005-04-30 07:13:31 +0000 | [diff] [blame] | 123 | if(I->arg_begin()->getType() == Type::FloatTy) |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 124 | EnsureFunctionExists(M, "sqrtf", I->arg_begin(), I->arg_end(), |
| 125 | Type::FloatTy); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 126 | else |
Chris Lattner | 1f243e9 | 2005-05-08 19:46:29 +0000 | [diff] [blame] | 127 | EnsureFunctionExists(M, "sqrt", I->arg_begin(), I->arg_end(), |
| 128 | Type::DoubleTy); |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 129 | break; |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 0979ca7 | 2004-05-09 04:29:57 +0000 | [diff] [blame] | 131 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 132 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 133 | /// LowerBSWAP - Emit the code to lower bswap of V before the specified |
| 134 | /// instruction IP. |
| 135 | static Value *LowerBSWAP(Value *V, Instruction *IP) { |
| 136 | assert(V->getType()->isInteger() && "Can't bswap a non-integer type!"); |
| 137 | |
| 138 | const Type *DestTy = V->getType(); |
| 139 | |
| 140 | // Force to unsigned so that the shift rights are logical. |
| 141 | if (DestTy->isSigned()) |
| 142 | V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP); |
| 143 | |
| 144 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 145 | |
| 146 | switch(BitSize) { |
| 147 | default: assert(0 && "Unhandled type size of value to byteswap!"); |
| 148 | case 16: { |
| 149 | Value *Tmp1 = new ShiftInst(Instruction::Shl, V, |
| 150 | ConstantInt::get(Type::UByteTy,8),"bswap.2",IP); |
| 151 | Value *Tmp2 = new ShiftInst(Instruction::Shr, V, |
| 152 | ConstantInt::get(Type::UByteTy,8),"bswap.1",IP); |
| 153 | V = BinaryOperator::createOr(Tmp1, Tmp2, "bswap.i16", IP); |
| 154 | break; |
| 155 | } |
| 156 | case 32: { |
| 157 | Value *Tmp4 = new ShiftInst(Instruction::Shl, V, |
| 158 | ConstantInt::get(Type::UByteTy,24),"bswap.4", IP); |
| 159 | Value *Tmp3 = new ShiftInst(Instruction::Shl, V, |
| 160 | ConstantInt::get(Type::UByteTy,8),"bswap.3",IP); |
| 161 | Value *Tmp2 = new ShiftInst(Instruction::Shr, V, |
| 162 | ConstantInt::get(Type::UByteTy,8),"bswap.2",IP); |
| 163 | Value *Tmp1 = new ShiftInst(Instruction::Shr, V, |
| 164 | ConstantInt::get(Type::UByteTy,24),"bswap.1", IP); |
| 165 | Tmp3 = BinaryOperator::createAnd(Tmp3, |
| 166 | ConstantUInt::get(Type::UIntTy, 0xFF0000), |
| 167 | "bswap.and3", IP); |
| 168 | Tmp2 = BinaryOperator::createAnd(Tmp2, |
| 169 | ConstantUInt::get(Type::UIntTy, 0xFF00), |
| 170 | "bswap.and2", IP); |
| 171 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or1", IP); |
| 172 | Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or2", IP); |
| 173 | V = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.i32", IP); |
| 174 | break; |
| 175 | } |
| 176 | case 64: { |
| 177 | Value *Tmp8 = new ShiftInst(Instruction::Shl, V, |
| 178 | ConstantInt::get(Type::UByteTy,56),"bswap.8", IP); |
| 179 | Value *Tmp7 = new ShiftInst(Instruction::Shl, V, |
| 180 | ConstantInt::get(Type::UByteTy,40),"bswap.7", IP); |
| 181 | Value *Tmp6 = new ShiftInst(Instruction::Shl, V, |
| 182 | ConstantInt::get(Type::UByteTy,24),"bswap.6", IP); |
| 183 | Value *Tmp5 = new ShiftInst(Instruction::Shl, V, |
| 184 | ConstantInt::get(Type::UByteTy,8),"bswap.5",IP); |
| 185 | Value *Tmp4 = new ShiftInst(Instruction::Shr, V, |
| 186 | ConstantInt::get(Type::UByteTy,8),"bswap.4",IP); |
| 187 | Value *Tmp3 = new ShiftInst(Instruction::Shr, V, |
| 188 | ConstantInt::get(Type::UByteTy,24),"bswap.3", IP); |
| 189 | Value *Tmp2 = new ShiftInst(Instruction::Shr, V, |
| 190 | ConstantInt::get(Type::UByteTy,40),"bswap.2", IP); |
| 191 | Value *Tmp1 = new ShiftInst(Instruction::Shr, V, |
| 192 | ConstantInt::get(Type::UByteTy,56),"bswap.1", IP); |
| 193 | Tmp7 = BinaryOperator::createAnd(Tmp7, |
| 194 | ConstantUInt::get(Type::ULongTy, 0xFF000000000000ULL), |
| 195 | "bswap.and7", IP); |
| 196 | Tmp6 = BinaryOperator::createAnd(Tmp6, |
| 197 | ConstantUInt::get(Type::ULongTy, 0xFF0000000000ULL), |
| 198 | "bswap.and6", IP); |
| 199 | Tmp5 = BinaryOperator::createAnd(Tmp5, |
| 200 | ConstantUInt::get(Type::ULongTy, 0xFF00000000ULL), |
| 201 | "bswap.and5", IP); |
| 202 | Tmp4 = BinaryOperator::createAnd(Tmp4, |
| 203 | ConstantUInt::get(Type::ULongTy, 0xFF000000ULL), |
| 204 | "bswap.and4", IP); |
| 205 | Tmp3 = BinaryOperator::createAnd(Tmp3, |
| 206 | ConstantUInt::get(Type::ULongTy, 0xFF0000ULL), |
| 207 | "bswap.and3", IP); |
| 208 | Tmp2 = BinaryOperator::createAnd(Tmp2, |
| 209 | ConstantUInt::get(Type::ULongTy, 0xFF00ULL), |
| 210 | "bswap.and2", IP); |
| 211 | Tmp8 = BinaryOperator::createOr(Tmp8, Tmp7, "bswap.or1", IP); |
| 212 | Tmp6 = BinaryOperator::createOr(Tmp6, Tmp5, "bswap.or2", IP); |
| 213 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp3, "bswap.or3", IP); |
| 214 | Tmp2 = BinaryOperator::createOr(Tmp2, Tmp1, "bswap.or4", IP); |
| 215 | Tmp8 = BinaryOperator::createOr(Tmp8, Tmp6, "bswap.or5", IP); |
| 216 | Tmp4 = BinaryOperator::createOr(Tmp4, Tmp2, "bswap.or6", IP); |
| 217 | V = BinaryOperator::createOr(Tmp8, Tmp4, "bswap.i64", IP); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | if (V->getType() != DestTy) |
| 223 | V = new CastInst(V, DestTy, V->getName(), IP); |
| 224 | return V; |
| 225 | } |
| 226 | |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 227 | /// LowerCTPOP - Emit the code to lower ctpop of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 228 | /// instruction IP. |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 229 | static Value *LowerCTPOP(Value *V, Instruction *IP) { |
| 230 | assert(V->getType()->isInteger() && "Can't ctpop a non-integer type!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 231 | |
| 232 | static const uint64_t MaskValues[6] = { |
| 233 | 0x5555555555555555ULL, 0x3333333333333333ULL, |
| 234 | 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL, |
| 235 | 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL |
| 236 | }; |
| 237 | |
| 238 | const Type *DestTy = V->getType(); |
| 239 | |
| 240 | // Force to unsigned so that the shift rights are logical. |
| 241 | if (DestTy->isSigned()) |
| 242 | V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP); |
| 243 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 244 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 245 | for (unsigned i = 1, ct = 0; i != BitSize; i <<= 1, ++ct) { |
| 246 | Value *MaskCst = |
| 247 | ConstantExpr::getCast(ConstantUInt::get(Type::ULongTy, |
| 248 | MaskValues[ct]), V->getType()); |
| 249 | Value *LHS = BinaryOperator::createAnd(V, MaskCst, "cppop.and1", IP); |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 250 | Value *VShift = new ShiftInst(Instruction::Shr, V, |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 251 | ConstantInt::get(Type::UByteTy, i), "ctpop.sh", IP); |
| 252 | Value *RHS = BinaryOperator::createAnd(VShift, MaskCst, "cppop.and2", IP); |
| 253 | V = BinaryOperator::createAdd(LHS, RHS, "ctpop.step", IP); |
| 254 | } |
| 255 | |
| 256 | if (V->getType() != DestTy) |
| 257 | V = new CastInst(V, DestTy, V->getName(), IP); |
| 258 | return V; |
| 259 | } |
| 260 | |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 261 | /// LowerCTLZ - Emit the code to lower ctlz of V before the specified |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 262 | /// instruction IP. |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 263 | static Value *LowerCTLZ(Value *V, Instruction *IP) { |
| 264 | const Type *DestTy = V->getType(); |
| 265 | |
| 266 | // Force to unsigned so that the shift rights are logical. |
| 267 | if (DestTy->isSigned()) |
| 268 | V = new CastInst(V, DestTy->getUnsignedVersion(), V->getName(), IP); |
| 269 | |
| 270 | unsigned BitSize = V->getType()->getPrimitiveSizeInBits(); |
| 271 | for (unsigned i = 1; i != BitSize; i <<= 1) { |
| 272 | Value *ShVal = ConstantInt::get(Type::UByteTy, i); |
| 273 | ShVal = new ShiftInst(Instruction::Shr, V, ShVal, "ctlz.sh", IP); |
| 274 | V = BinaryOperator::createOr(V, ShVal, "ctlz.step", IP); |
| 275 | } |
| 276 | |
| 277 | if (V->getType() != DestTy) |
| 278 | V = new CastInst(V, DestTy, V->getName(), IP); |
| 279 | |
| 280 | V = BinaryOperator::createNot(V, "", IP); |
| 281 | return LowerCTPOP(V, IP); |
| 282 | } |
| 283 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 284 | |
| 285 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 286 | void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { |
| 287 | Function *Callee = CI->getCalledFunction(); |
| 288 | assert(Callee && "Cannot lower an indirect call!"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 290 | switch (Callee->getIntrinsicID()) { |
| 291 | case Intrinsic::not_intrinsic: |
| 292 | std::cerr << "Cannot lower a call to a non-intrinsic function '" |
| 293 | << Callee->getName() << "'!\n"; |
| 294 | abort(); |
| 295 | default: |
| 296 | std::cerr << "Error: Code generator does not support intrinsic function '" |
| 297 | << Callee->getName() << "'!\n"; |
| 298 | abort(); |
| 299 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 300 | // The setjmp/longjmp intrinsics should only exist in the code if it was |
| 301 | // never optimized (ie, right out of the CFE), or if it has been hacked on |
| 302 | // by the lowerinvoke pass. In both cases, the right thing to do is to |
| 303 | // convert the call to an explicit setjmp or longjmp call. |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 304 | case Intrinsic::setjmp: { |
| 305 | static Function *SetjmpFCache = 0; |
| 306 | Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(), |
| 307 | Type::IntTy, SetjmpFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 308 | if (CI->getType() != Type::VoidTy) |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 309 | CI->replaceAllUsesWith(V); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 310 | break; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 311 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 312 | case Intrinsic::sigsetjmp: |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 313 | if (CI->getType() != Type::VoidTy) |
| 314 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 315 | break; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 316 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 317 | case Intrinsic::longjmp: { |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 318 | static Function *LongjmpFCache = 0; |
| 319 | ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(), |
| 320 | Type::VoidTy, LongjmpFCache); |
| 321 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 323 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 324 | case Intrinsic::siglongjmp: { |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 325 | // Insert the call to abort |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 326 | static Function *AbortFCache = 0; |
| 327 | ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy, |
| 328 | AbortFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 329 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 330 | } |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 331 | case Intrinsic::ctpop_i8: |
| 332 | case Intrinsic::ctpop_i16: |
| 333 | case Intrinsic::ctpop_i32: |
| 334 | case Intrinsic::ctpop_i64: |
| 335 | CI->replaceAllUsesWith(LowerCTPOP(CI->getOperand(1), CI)); |
| 336 | break; |
| 337 | |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 338 | case Intrinsic::bswap_i16: |
| 339 | case Intrinsic::bswap_i32: |
| 340 | case Intrinsic::bswap_i64: |
| 341 | CI->replaceAllUsesWith(LowerBSWAP(CI->getOperand(1), CI)); |
| 342 | break; |
| 343 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 344 | case Intrinsic::ctlz_i8: |
| 345 | case Intrinsic::ctlz_i16: |
| 346 | case Intrinsic::ctlz_i32: |
| 347 | case Intrinsic::ctlz_i64: |
Chris Lattner | 98cf45b | 2005-05-11 20:24:12 +0000 | [diff] [blame] | 348 | CI->replaceAllUsesWith(LowerCTLZ(CI->getOperand(1), CI)); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 349 | break; |
Nate Begeman | e598181 | 2006-01-16 07:57:00 +0000 | [diff] [blame] | 350 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 351 | case Intrinsic::cttz_i8: |
| 352 | case Intrinsic::cttz_i16: |
| 353 | case Intrinsic::cttz_i32: |
| 354 | case Intrinsic::cttz_i64: { |
Chris Lattner | a801172 | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 355 | // cttz(x) -> ctpop(~X & (X-1)) |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 356 | Value *Src = CI->getOperand(1); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 357 | Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI); |
Chris Lattner | a801172 | 2005-05-11 20:02:14 +0000 | [diff] [blame] | 358 | Value *SrcM1 = ConstantInt::get(Src->getType(), 1); |
| 359 | SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI); |
| 360 | Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI); |
Andrew Lenharth | 691ef2b | 2005-05-03 17:19:30 +0000 | [diff] [blame] | 361 | CI->replaceAllUsesWith(Src); |
| 362 | break; |
| 363 | } |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 365 | case Intrinsic::stacksave: |
| 366 | case Intrinsic::stackrestore: { |
| 367 | static bool Warned = false; |
| 368 | if (!Warned) |
| 369 | std::cerr << "WARNING: this target does not support the llvm.stack" |
| 370 | << (Callee->getIntrinsicID() == Intrinsic::stacksave ? |
| 371 | "save" : "restore") << " intrinsic.\n"; |
| 372 | Warned = true; |
| 373 | if (Callee->getIntrinsicID() == Intrinsic::stacksave) |
| 374 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 375 | break; |
| 376 | } |
| 377 | |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 378 | case Intrinsic::returnaddress: |
| 379 | case Intrinsic::frameaddress: |
Chris Lattner | cc42d2c | 2004-02-14 04:52:06 +0000 | [diff] [blame] | 380 | std::cerr << "WARNING: this target does not support the llvm." |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 381 | << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? |
Chris Lattner | cc42d2c | 2004-02-14 04:52:06 +0000 | [diff] [blame] | 382 | "return" : "frame") << "address intrinsic.\n"; |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 383 | CI->replaceAllUsesWith(ConstantPointerNull::get( |
| 384 | cast<PointerType>(CI->getType()))); |
| 385 | break; |
| 386 | |
Chris Lattner | 0942b7c | 2005-02-28 19:27:23 +0000 | [diff] [blame] | 387 | case Intrinsic::prefetch: |
| 388 | break; // Simply strip out prefetches on unsupported architectures |
| 389 | |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 390 | case Intrinsic::pcmarker: |
| 391 | break; // Simply strip out pcmarker on unsupported architectures |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 392 | case Intrinsic::readcyclecounter: { |
Chris Lattner | 0c067bc | 2006-01-13 02:22:08 +0000 | [diff] [blame] | 393 | std::cerr << "WARNING: this target does not support the llvm.readcyclecoun" |
| 394 | << "ter intrinsic. It is being lowered to a constant 0\n"; |
Andrew Lenharth | 51b8d54 | 2005-11-11 16:47:30 +0000 | [diff] [blame] | 395 | CI->replaceAllUsesWith(ConstantUInt::get(Type::ULongTy, 0)); |
| 396 | break; |
| 397 | } |
Andrew Lenharth | 7f4ec3b | 2005-03-28 20:05:49 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 399 | case Intrinsic::dbg_stoppoint: |
| 400 | case Intrinsic::dbg_region_start: |
| 401 | case Intrinsic::dbg_region_end: |
Chris Lattner | f907bac | 2004-01-14 20:41:29 +0000 | [diff] [blame] | 402 | case Intrinsic::dbg_declare: |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 403 | case Intrinsic::dbg_func_start: |
| 404 | if (CI->getType() != Type::VoidTy) |
| 405 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 406 | break; // Simply strip out debugging intrinsics |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 407 | |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 408 | case Intrinsic::memcpy: { |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 409 | // The memcpy intrinsic take an extra alignment argument that the memcpy |
| 410 | // libc function does not. |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 411 | static Function *MemcpyFCache = 0; |
| 412 | ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1, |
| 413 | (*(CI->op_begin()+1))->getType(), MemcpyFCache); |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 414 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 415 | } |
| 416 | case Intrinsic::memmove: { |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 417 | // The memmove intrinsic take an extra alignment argument that the memmove |
Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 418 | // libc function does not. |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 419 | static Function *MemmoveFCache = 0; |
| 420 | ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1, |
| 421 | (*(CI->op_begin()+1))->getType(), MemmoveFCache); |
Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 422 | break; |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 423 | } |
| 424 | case Intrinsic::memset: { |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 425 | // The memset intrinsic take an extra alignment argument that the memset |
| 426 | // libc function does not. |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 427 | static Function *MemsetFCache = 0; |
| 428 | ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1, |
| 429 | (*(CI->op_begin()+1))->getType(), MemsetFCache); |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 430 | break; |
| 431 | } |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 432 | case Intrinsic::isunordered_f32: |
| 433 | case Intrinsic::isunordered_f64: { |
Alkis Evlogimenos | d0656fc | 2005-03-01 02:07:58 +0000 | [diff] [blame] | 434 | Value *L = CI->getOperand(1); |
| 435 | Value *R = CI->getOperand(2); |
| 436 | |
| 437 | Value *LIsNan = new SetCondInst(Instruction::SetNE, L, L, "LIsNan", CI); |
| 438 | Value *RIsNan = new SetCondInst(Instruction::SetNE, R, R, "RIsNan", CI); |
| 439 | CI->replaceAllUsesWith( |
| 440 | BinaryOperator::create(Instruction::Or, LIsNan, RIsNan, |
| 441 | "isunordered", CI)); |
Alkis Evlogimenos | 9685372 | 2004-06-12 19:19:14 +0000 | [diff] [blame] | 442 | break; |
| 443 | } |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 444 | case Intrinsic::sqrt_f32: |
| 445 | case Intrinsic::sqrt_f64: { |
Chris Lattner | b42a9ff | 2005-04-30 04:07:50 +0000 | [diff] [blame] | 446 | static Function *sqrtFCache = 0; |
| 447 | static Function *sqrtfFCache = 0; |
| 448 | if(CI->getType() == Type::FloatTy) |
| 449 | ReplaceCallWith("sqrtf", CI, CI->op_begin()+1, CI->op_end(), |
| 450 | Type::FloatTy, sqrtfFCache); |
| 451 | else |
| 452 | ReplaceCallWith("sqrt", CI, CI->op_begin()+1, CI->op_end(), |
| 453 | Type::DoubleTy, sqrtFCache); |
| 454 | break; |
| 455 | } |
Chris Lattner | f0a3e6c | 2004-06-05 01:05:19 +0000 | [diff] [blame] | 456 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 458 | assert(CI->use_empty() && |
| 459 | "Lowering should have eliminated any uses of the intrinsic call!"); |
Chris Lattner | 86f3e0c | 2005-05-11 19:42:05 +0000 | [diff] [blame] | 460 | CI->eraseFromParent(); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 461 | } |