blob: c200aebf06da3fe1919c6f7c1965af1191763eca [file] [log] [blame]
Chris Lattner3b66ecb2003-12-28 08:19:41 +00001//===-- 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 Lattner30483732004-06-20 07:49:54 +000014#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnercf899082004-02-14 02:47:17 +000015#include "llvm/Constants.h"
Chris Lattner5fe51cc2004-02-12 17:01:09 +000016#include "llvm/DerivedTypes.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000017#include "llvm/Module.h"
Chris Lattner3b66ecb2003-12-28 08:19:41 +000018#include "llvm/iOther.h"
Reid Spencer954da372004-07-04 12:19:56 +000019#include <iostream>
20
Chris Lattner3b66ecb2003-12-28 08:19:41 +000021using namespace llvm;
22
Chris Lattner0979ca72004-05-09 04:29:57 +000023template <class ArgIt>
24static Function *EnsureFunctionExists(Module &M, const char *Name,
25 ArgIt ArgBegin, ArgIt ArgEnd,
26 const Type *RetTy) {
27 if (Function *F = M.getNamedFunction(Name)) return F;
28 // It doesn't already exist in the program, insert a new definition now.
29 std::vector<const Type *> ParamTys;
30 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
31 ParamTys.push_back(I->getType());
32 return M.getOrInsertFunction(Name, FunctionType::get(RetTy, ParamTys, false));
33}
34
Chris Lattner588e72d2004-02-15 22:16:39 +000035/// ReplaceCallWith - This function is used when we want to lower an intrinsic
36/// call to a call of an external function. This handles hard cases such as
37/// when there was already a prototype for the external function, and if that
38/// prototype doesn't match the arguments we expect to pass in.
39template <class ArgIt>
40static CallInst *ReplaceCallWith(const char *NewFn, CallInst *CI,
41 ArgIt ArgBegin, ArgIt ArgEnd,
42 const Type *RetTy, Function *&FCache) {
43 if (!FCache) {
44 // If we haven't already looked up this function, check to see if the
45 // program already contains a function with this name.
46 Module *M = CI->getParent()->getParent()->getParent();
47 FCache = M->getNamedFunction(NewFn);
48 if (!FCache) {
49 // It doesn't already exist in the program, insert a new definition now.
50 std::vector<const Type *> ParamTys;
51 for (ArgIt I = ArgBegin; I != ArgEnd; ++I)
52 ParamTys.push_back((*I)->getType());
53 FCache = M->getOrInsertFunction(NewFn,
54 FunctionType::get(RetTy, ParamTys, false));
55 }
Chris Lattner0979ca72004-05-09 04:29:57 +000056 }
Chris Lattner588e72d2004-02-15 22:16:39 +000057
58 const FunctionType *FT = FCache->getFunctionType();
59 std::vector<Value*> Operands;
60 unsigned ArgNo = 0;
61 for (ArgIt I = ArgBegin; I != ArgEnd && ArgNo != FT->getNumParams();
62 ++I, ++ArgNo) {
63 Value *Arg = *I;
64 if (Arg->getType() != FT->getParamType(ArgNo))
65 Arg = new CastInst(Arg, FT->getParamType(ArgNo), Arg->getName(), CI);
66 Operands.push_back(Arg);
67 }
68 // Pass nulls into any additional arguments...
69 for (; ArgNo != FT->getNumParams(); ++ArgNo)
70 Operands.push_back(Constant::getNullValue(FT->getParamType(ArgNo)));
71
72 std::string Name = CI->getName(); CI->setName("");
73 if (FT->getReturnType() == Type::VoidTy) Name.clear();
Chris Lattner02348ca2004-06-11 02:54:02 +000074 CallInst *NewCI = new CallInst(FCache, Operands, Name, CI);
75 if (!CI->use_empty()) {
76 Value *V = NewCI;
77 if (CI->getType() != NewCI->getType())
78 V = new CastInst(NewCI, CI->getType(), Name, CI);
79 CI->replaceAllUsesWith(V);
80 }
81 return NewCI;
Chris Lattner588e72d2004-02-15 22:16:39 +000082}
83
Chris Lattner0979ca72004-05-09 04:29:57 +000084void DefaultIntrinsicLowering::AddPrototypes(Module &M) {
85 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
86 if (I->isExternal() && !I->use_empty())
87 switch (I->getIntrinsicID()) {
88 default: break;
89 case Intrinsic::setjmp:
90 EnsureFunctionExists(M, "setjmp", I->abegin(), I->aend(), Type::IntTy);
91 break;
92 case Intrinsic::longjmp:
93 EnsureFunctionExists(M, "longjmp", I->abegin(), I->aend(),Type::VoidTy);
94 break;
95 case Intrinsic::siglongjmp:
96 EnsureFunctionExists(M, "abort", I->aend(), I->aend(), Type::VoidTy);
97 break;
98 case Intrinsic::memcpy:
99 EnsureFunctionExists(M, "memcpy", I->abegin(), --I->aend(),
100 I->abegin()->getType());
101 break;
102 case Intrinsic::memmove:
103 EnsureFunctionExists(M, "memmove", I->abegin(), --I->aend(),
104 I->abegin()->getType());
105 break;
106 case Intrinsic::memset:
107 EnsureFunctionExists(M, "memset", I->abegin(), --I->aend(),
108 I->abegin()->getType());
109 break;
Chris Lattnerbe78ac42004-06-15 21:42:23 +0000110 case Intrinsic::isunordered:
111 EnsureFunctionExists(M, "isunordered", I->abegin(), I->aend(), Type::BoolTy);
Chris Lattner02348ca2004-06-11 02:54:02 +0000112 break;
Chris Lattner0979ca72004-05-09 04:29:57 +0000113 }
114
115}
Chris Lattner588e72d2004-02-15 22:16:39 +0000116
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000117void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) {
118 Function *Callee = CI->getCalledFunction();
119 assert(Callee && "Cannot lower an indirect call!");
120
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000121 switch (Callee->getIntrinsicID()) {
122 case Intrinsic::not_intrinsic:
123 std::cerr << "Cannot lower a call to a non-intrinsic function '"
124 << Callee->getName() << "'!\n";
125 abort();
126 default:
127 std::cerr << "Error: Code generator does not support intrinsic function '"
128 << Callee->getName() << "'!\n";
129 abort();
130
Chris Lattner588e72d2004-02-15 22:16:39 +0000131 // The setjmp/longjmp intrinsics should only exist in the code if it was
132 // never optimized (ie, right out of the CFE), or if it has been hacked on
133 // by the lowerinvoke pass. In both cases, the right thing to do is to
134 // convert the call to an explicit setjmp or longjmp call.
Chris Lattner9b700f72004-02-15 22:24:51 +0000135 case Intrinsic::setjmp: {
136 static Function *SetjmpFCache = 0;
137 Value *V = ReplaceCallWith("setjmp", CI, CI->op_begin()+1, CI->op_end(),
138 Type::IntTy, SetjmpFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000139 if (CI->getType() != Type::VoidTy)
Chris Lattner9b700f72004-02-15 22:24:51 +0000140 CI->replaceAllUsesWith(V);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000141 break;
Chris Lattner9b700f72004-02-15 22:24:51 +0000142 }
143 case Intrinsic::sigsetjmp:
144 if (CI->getType() != Type::VoidTy)
145 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
146 break;
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000147
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000148 case Intrinsic::longjmp: {
Chris Lattner9b700f72004-02-15 22:24:51 +0000149 static Function *LongjmpFCache = 0;
150 ReplaceCallWith("longjmp", CI, CI->op_begin()+1, CI->op_end(),
151 Type::VoidTy, LongjmpFCache);
152 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000153 }
Chris Lattner9b700f72004-02-15 22:24:51 +0000154
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000155 case Intrinsic::siglongjmp: {
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000156 // Insert the call to abort
Chris Lattner588e72d2004-02-15 22:16:39 +0000157 static Function *AbortFCache = 0;
158 ReplaceCallWith("abort", CI, CI->op_end(), CI->op_end(), Type::VoidTy,
159 AbortFCache);
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000160 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000161 }
Chris Lattner77b13302004-01-05 05:36:30 +0000162
Chris Lattnercf899082004-02-14 02:47:17 +0000163 case Intrinsic::returnaddress:
164 case Intrinsic::frameaddress:
Chris Lattnercc42d2c2004-02-14 04:52:06 +0000165 std::cerr << "WARNING: this target does not support the llvm."
166 << (Callee->getIntrinsicID() == Intrinsic::returnaddress ?
167 "return" : "frame") << "address intrinsic.\n";
Chris Lattnercf899082004-02-14 02:47:17 +0000168 CI->replaceAllUsesWith(ConstantPointerNull::get(
169 cast<PointerType>(CI->getType())));
170 break;
171
Chris Lattner77b13302004-01-05 05:36:30 +0000172 case Intrinsic::dbg_stoppoint:
173 case Intrinsic::dbg_region_start:
174 case Intrinsic::dbg_region_end:
Chris Lattnerf907bac2004-01-14 20:41:29 +0000175 case Intrinsic::dbg_declare:
Chris Lattner77b13302004-01-05 05:36:30 +0000176 case Intrinsic::dbg_func_start:
177 if (CI->getType() != Type::VoidTy)
178 CI->replaceAllUsesWith(Constant::getNullValue(CI->getType()));
179 break; // Simply strip out debugging intrinsics
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000180
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000181 case Intrinsic::memcpy: {
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000182 // The memcpy intrinsic take an extra alignment argument that the memcpy
183 // libc function does not.
Chris Lattner588e72d2004-02-15 22:16:39 +0000184 static Function *MemcpyFCache = 0;
185 ReplaceCallWith("memcpy", CI, CI->op_begin()+1, CI->op_end()-1,
186 (*(CI->op_begin()+1))->getType(), MemcpyFCache);
Chris Lattner5fe51cc2004-02-12 17:01:09 +0000187 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000188 }
189 case Intrinsic::memmove: {
Chris Lattnercf899082004-02-14 02:47:17 +0000190 // The memmove intrinsic take an extra alignment argument that the memmove
Chris Lattner2751e762004-02-12 18:11:20 +0000191 // libc function does not.
Chris Lattner588e72d2004-02-15 22:16:39 +0000192 static Function *MemmoveFCache = 0;
193 ReplaceCallWith("memmove", CI, CI->op_begin()+1, CI->op_end()-1,
194 (*(CI->op_begin()+1))->getType(), MemmoveFCache);
Chris Lattner2751e762004-02-12 18:11:20 +0000195 break;
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000196 }
197 case Intrinsic::memset: {
Chris Lattnercf899082004-02-14 02:47:17 +0000198 // The memset intrinsic take an extra alignment argument that the memset
199 // libc function does not.
Chris Lattner588e72d2004-02-15 22:16:39 +0000200 static Function *MemsetFCache = 0;
201 ReplaceCallWith("memset", CI, CI->op_begin()+1, CI->op_end()-1,
202 (*(CI->op_begin()+1))->getType(), MemsetFCache);
Chris Lattnercf899082004-02-14 02:47:17 +0000203 break;
204 }
Alkis Evlogimenos96853722004-06-12 19:19:14 +0000205 case Intrinsic::isunordered: {
206 static Function *isunorderedFCache = 0;
207 ReplaceCallWith("isunordered", CI, CI->op_begin()+1, CI->op_end(),
208 Type::BoolTy, isunorderedFCache);
209 break;
210 }
Chris Lattnerf0a3e6c2004-06-05 01:05:19 +0000211 }
Chris Lattner588e72d2004-02-15 22:16:39 +0000212
Chris Lattner3b66ecb2003-12-28 08:19:41 +0000213 assert(CI->use_empty() &&
214 "Lowering should have eliminated any uses of the intrinsic call!");
215 CI->getParent()->getInstList().erase(CI);
216}