1 //===-- IntrinsicLowering.cpp - Intrinsic Lowering default implementation -===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the default intrinsic lowering implementation.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IntrinsicLowering.h"
15 #include "llvm/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/iOther.h"
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) {
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);
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));
44 const FunctionType *FT = FCache->getFunctionType();
45 std::vector<Value*> Operands;
47 for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
50 if (Arg->getType() != FT->getParamType(ArgNo))
51 Arg = new CastInst(Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
52 Operands.push_back(Arg);
54 // Pass nulls into any additional arguments...
55 for (; ArgNo != FT->getNumParams(); ++ArgNo)
56 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
58 std::string Name = CI->getName(); CI->setName("");
59 if (FT->getReturnType() == Type::VoidTy) Name.clear();
60 return new CallInst(FCache, Operands, Name, CI);
64 void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
65 Function *Callee = CI->getCalledFunction();
66 assert(Callee && "Cannot lower an indirect call!");
68 Module *M = Callee->getParent();
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";
76 std::cerr << "Error: Code generator does not support intrinsic function '"
77 << Callee->getName() << "'!\n";
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.
84 case Intrinsic::setjmp:
85 case Intrinsic::sigsetjmp:
86 if (CI->getType() != Type::VoidTy)
87 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
90 case Intrinsic::longjmp:
91 case Intrinsic::siglongjmp:
92 // Insert the call to abort
93 static Function *AbortFCache = 0;
94 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy,
98 case Intrinsic::returnaddress:
99 case Intrinsic::frameaddress:
100 std::cerr << "WARNING: this target does not support the llvm."
101 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
102 "return" : "frame") << "address intrinsic.\n";
103 CI->replaceAllUsesWith(ConstantPointerNull::get(
104 cast<PointerType>(CI->getType())));
107 case Intrinsic::dbg_stoppoint:
108 case Intrinsic::dbg_region_start:
109 case Intrinsic::dbg_region_end:
110 case Intrinsic::dbg_declare:
111 case Intrinsic::dbg_func_start:
112 if (CI->getType() != Type::VoidTy)
113 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
114 break; // Simply strip out debugging intrinsics
116 case Intrinsic::memcpy:
117 // The memcpy intrinsic take an extra alignment argument that the memcpy
118 // libc function does not.
119 static Function *MemcpyFCache = 0;
120 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
121 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
123 case Intrinsic::memmove:
124 // The memmove intrinsic take an extra alignment argument that the memmove
125 // libc function does not.
126 static Function *MemmoveFCache = 0;
127 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
128 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
130 case Intrinsic::memset:
131 // The memset intrinsic take an extra alignment argument that the memset
132 // libc function does not.
133 static Function *MemsetFCache = 0;
134 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
135 (*(CI->op_begin()+1))->getType(), MemsetFCache);
139 assert(CI->use_empty() &&
140 "Lowering should have eliminated any uses of the intrinsic call!");
141 CI->getParent()->getInstList().erase(CI);