Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 1 | //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===// |
| 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the default intrinsic lowering implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 42450d8 | 2003-12-28 08:30:20 +0000 | [diff] [blame] | 14 | #include "llvm/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" |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 18 | #include "llvm/iOther.h" |
| 19 | using namespace llvm; |
| 20 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 21 | /// ReplaceCallWith - This function is used when we want to lower an intrinsic |
| 22 | /// call to a call of an external function. This handles hard cases such as |
| 23 | /// when there was already a prototype for the external function, and if that |
| 24 | /// prototype doesn't match the arguments we expect to pass in. |
| 25 | template <class ArgIt> |
| 26 | static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI, |
| 27 | ArgIt ArgBegin, ArgIt ArgEnd, |
| 28 | const Type *RetTy, Function *&FCache) { |
| 29 | if (!FCache) { |
| 30 | // If we haven't already looked up this function, check to see if the |
| 31 | // program already contains a function with this name. |
| 32 | Module *M = CI->getParent()->getParent()->getParent(); |
| 33 | FCache = M->getNamedFunction(NewFn); |
| 34 | if (!FCache) { |
| 35 | // It doesn't already exist in the program, insert a new definition now. |
| 36 | std::vector<const Type *> ParamTys; |
| 37 | for (ArgIt I = ArgBegin; I != ArgEnd; ++I) |
| 38 | ParamTys.push_back((*I)->getType()); |
| 39 | FCache = M->getOrInsertFunction(NewFn, |
| 40 | FunctionType::get(RetTy, ParamTys, false)); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | const FunctionType *FT = FCache->getFunctionType(); |
| 45 | std::vector<Value*> Operands; |
| 46 | unsigned ArgNo = 0; |
| 47 | for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams(); |
| 48 | ++I, ++ArgNo) { |
| 49 | Value *Arg = *I; |
| 50 | if (Arg->getType() != FT->getParamType(ArgNo)) |
| 51 | Arg = new CastInst(Arg, FT->getParamType(ArgNo), Arg->getName(), CI); |
| 52 | Operands.push_back(Arg); |
| 53 | } |
| 54 | // Pass nulls into any additional arguments... |
| 55 | for (; ArgNo != FT->getNumParams(); ++ArgNo) |
| 56 | Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo))); |
| 57 | |
| 58 | std::string Name = CI->getName(); CI->setName(""); |
| 59 | if (FT->getReturnType() == Type::VoidTy) Name.clear(); |
| 60 | return new CallInst(FCache, Operands, Name, CI); |
| 61 | } |
| 62 | |
| 63 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 64 | void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { |
| 65 | Function *Callee = CI->getCalledFunction(); |
| 66 | assert(Callee && "Cannot lower an indirect call!"); |
| 67 | |
| 68 | Module *M = Callee->getParent(); |
| 69 | |
| 70 | switch (Callee->getIntrinsicID()) { |
| 71 | case Intrinsic::not_intrinsic: |
| 72 | std::cerr << "Cannot lower a call to a non-intrinsic function '" |
| 73 | << Callee->getName() << "'!\n"; |
| 74 | abort(); |
| 75 | default: |
| 76 | std::cerr << "Error: Code generator does not support intrinsic function '" |
| 77 | << Callee->getName() << "'!\n"; |
| 78 | abort(); |
| 79 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 80 | // The setjmp/longjmp intrinsics should only exist in the code if it was |
| 81 | // never optimized (ie, right out of the CFE), or if it has been hacked on |
| 82 | // by the lowerinvoke pass. In both cases, the right thing to do is to |
| 83 | // convert the call to an explicit setjmp or longjmp call. |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 84 | case Intrinsic::setjmp: { |
| 85 | static Function *SetjmpFCache = 0; |
| 86 | Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(), |
| 87 | Type::IntTy, SetjmpFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 88 | if (CI->getType() != Type::VoidTy) |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 89 | CI->replaceAllUsesWith(V); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 90 | break; |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 91 | } |
| 92 | case Intrinsic::sigsetjmp: |
| 93 | if (CI->getType() != Type::VoidTy) |
| 94 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 95 | break; |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 96 | |
| 97 | case Intrinsic::longjmp: |
Chris Lattner | 9b700f7 | 2004-02-15 22:24:51 +0000 | [diff] [blame] | 98 | static Function *LongjmpFCache = 0; |
| 99 | ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(), |
| 100 | Type::VoidTy, LongjmpFCache); |
| 101 | break; |
| 102 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 103 | case Intrinsic::siglongjmp: |
| 104 | // Insert the call to abort |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 105 | static Function *AbortFCache = 0; |
| 106 | ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy, |
| 107 | AbortFCache); |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 108 | break; |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 109 | |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 110 | case Intrinsic::returnaddress: |
| 111 | case Intrinsic::frameaddress: |
Chris Lattner | cc42d2c | 2004-02-14 04:52:06 +0000 | [diff] [blame] | 112 | std::cerr << "WARNING: this target does not support the llvm." |
| 113 | << (Callee->getIntrinsicID() == Intrinsic::returnaddress ? |
| 114 | "return" : "frame") << "address intrinsic.\n"; |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 115 | CI->replaceAllUsesWith(ConstantPointerNull::get( |
| 116 | cast<PointerType>(CI->getType()))); |
| 117 | break; |
| 118 | |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 119 | case Intrinsic::dbg_stoppoint: |
| 120 | case Intrinsic::dbg_region_start: |
| 121 | case Intrinsic::dbg_region_end: |
Chris Lattner | f907bac | 2004-01-14 20:41:29 +0000 | [diff] [blame] | 122 | case Intrinsic::dbg_declare: |
Chris Lattner | 77b1330 | 2004-01-05 05:36:30 +0000 | [diff] [blame] | 123 | case Intrinsic::dbg_func_start: |
| 124 | if (CI->getType() != Type::VoidTy) |
| 125 | CI->replaceAllUsesWith(Constant::getNullValue(CI->getType())); |
| 126 | break; // Simply strip out debugging intrinsics |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 128 | case Intrinsic::memcpy: |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 129 | // The memcpy intrinsic take an extra alignment argument that the memcpy |
| 130 | // libc function does not. |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 131 | static Function *MemcpyFCache = 0; |
| 132 | ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1, |
| 133 | (*(CI->op_begin()+1))->getType(), MemcpyFCache); |
Chris Lattner | 5fe51cc | 2004-02-12 17:01:09 +0000 | [diff] [blame] | 134 | break; |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 135 | case Intrinsic::memmove: |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 136 | // The memmove intrinsic take an extra alignment argument that the memmove |
Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 137 | // libc function does not. |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 138 | static Function *MemmoveFCache = 0; |
| 139 | ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1, |
| 140 | (*(CI->op_begin()+1))->getType(), MemmoveFCache); |
Chris Lattner | 2751e76 | 2004-02-12 18:11:20 +0000 | [diff] [blame] | 141 | break; |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 142 | case Intrinsic::memset: |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 143 | // The memset intrinsic take an extra alignment argument that the memset |
| 144 | // libc function does not. |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 145 | static Function *MemsetFCache = 0; |
| 146 | ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1, |
| 147 | (*(CI->op_begin()+1))->getType(), MemsetFCache); |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
Chris Lattner | 588e72d | 2004-02-15 22:16:39 +0000 | [diff] [blame] | 150 | |
Chris Lattner | 3b66ecb | 2003-12-28 08:19:41 +0000 | [diff] [blame] | 151 | assert(CI->use_empty() && |
| 152 | "Lowering should have eliminated any uses of the intrinsic call!"); |
| 153 | CI->getParent()->getInstList().erase(CI); |
| 154 | } |