blob: f3aa742c6bd978cdd27536cc440bb4c181b969e8 [file] [log] [blame]
Chandler Carruth7132e002007-08-04 01:51:18 +00001//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chandler Carruth7132e002007-08-04 01:51:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the auto-upgrade helper functions
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/AutoUpgrade.h"
Anders Carlssonf924f342007-12-14 06:38:54 +000015#include "llvm/Constants.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000016#include "llvm/Function.h"
Owen Anderson155dccd82009-07-07 23:43:39 +000017#include "llvm/LLVMContext.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000018#include "llvm/Module.h"
19#include "llvm/Instructions.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000020#include "llvm/Intrinsics.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000021#include "llvm/ADT/SmallVector.h"
Torok Edwin56d06592009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000023#include <cstring>
Chandler Carruth7132e002007-08-04 01:51:18 +000024using namespace llvm;
25
26
Evan Cheng0e179d02007-12-17 22:33:23 +000027static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000028 assert(F && "Illegal to upgrade a non-existent Function.");
29
Owen Anderson155dccd82009-07-07 23:43:39 +000030 LLVMContext* Context = F->getContext();
31
Chandler Carruth7132e002007-08-04 01:51:18 +000032 // Get the Function's name.
33 const std::string& Name = F->getName();
34
35 // Convenience
36 const FunctionType *FTy = F->getFunctionType();
37
38 // Quickly eliminate it, if it's not a candidate.
39 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
40 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
Evan Cheng0e179d02007-12-17 22:33:23 +000041 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +000042
43 Module *M = F->getParent();
44 switch (Name[5]) {
45 default: break;
Mon P Wang6a490372008-06-25 08:15:39 +000046 case 'a':
Mon P Wang2c839d42008-07-30 04:36:53 +000047 // This upgrades the llvm.atomic.lcs, llvm.atomic.las, llvm.atomic.lss,
48 // and atomics with default address spaces to their new names to their new
49 // function name (e.g. llvm.atomic.add.i32 => llvm.atomic.add.i32.p0i32)
50 if (Name.compare(5,7,"atomic.",7) == 0) {
Mon P Wang6a490372008-06-25 08:15:39 +000051 if (Name.compare(12,3,"lcs",3) == 0) {
52 std::string::size_type delim = Name.find('.',12);
Mon P Wang2c839d42008-07-30 04:36:53 +000053 F->setName("llvm.atomic.cmp.swap" + Name.substr(delim) +
54 ".p0" + Name.substr(delim+1));
Mon P Wang6a490372008-06-25 08:15:39 +000055 NewFn = F;
56 return true;
57 }
58 else if (Name.compare(12,3,"las",3) == 0) {
59 std::string::size_type delim = Name.find('.',12);
Mon P Wang2c839d42008-07-30 04:36:53 +000060 F->setName("llvm.atomic.load.add"+Name.substr(delim)
61 + ".p0" + Name.substr(delim+1));
Mon P Wang6a490372008-06-25 08:15:39 +000062 NewFn = F;
63 return true;
64 }
65 else if (Name.compare(12,3,"lss",3) == 0) {
66 std::string::size_type delim = Name.find('.',12);
Mon P Wang2c839d42008-07-30 04:36:53 +000067 F->setName("llvm.atomic.load.sub"+Name.substr(delim)
68 + ".p0" + Name.substr(delim+1));
69 NewFn = F;
70 return true;
71 }
72 else if (Name.rfind(".p") == std::string::npos) {
73 // We don't have an address space qualifier so this has be upgraded
74 // to the new name. Copy the type name at the end of the intrinsic
75 // and add to it
76 std::string::size_type delim = Name.find_last_of('.');
77 assert(delim != std::string::npos && "can not find type");
78 F->setName(Name + ".p0" + Name.substr(delim+1));
Mon P Wang6a490372008-06-25 08:15:39 +000079 NewFn = F;
80 return true;
81 }
82 }
83 break;
Chandler Carruth7132e002007-08-04 01:51:18 +000084 case 'b':
85 // This upgrades the name of the llvm.bswap intrinsic function to only use
86 // a single type name for overloading. We only care about the old format
87 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
88 // a '.' after 'bswap.'
89 if (Name.compare(5,6,"bswap.",6) == 0) {
90 std::string::size_type delim = Name.find('.',11);
91
92 if (delim != std::string::npos) {
93 // Construct the new name as 'llvm.bswap' + '.i*'
94 F->setName(Name.substr(0,10)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +000095 NewFn = F;
96 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +000097 }
98 }
99 break;
100
101 case 'c':
102 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
103 // correct return type, so we check for the name, and then check if the
104 // return type does not match the parameter type.
105 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
106 Name.compare(5,4,"ctlz",4) == 0 ||
107 Name.compare(5,4,"cttz",4) == 0) &&
108 FTy->getReturnType() != FTy->getParamType(0)) {
109 // We first need to change the name of the old (bad) intrinsic, because
110 // its type is incorrect, but we cannot overload that name. We
111 // arbitrarily unique it here allowing us to construct a correctly named
112 // and typed function below.
113 F->setName("");
114
115 // Now construct the new intrinsic with the correct name and type. We
116 // leave the old function around in order to query its type, whatever it
117 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000118 NewFn = cast<Function>(M->getOrInsertFunction(Name,
119 FTy->getParamType(0),
120 FTy->getParamType(0),
121 (Type *)0));
122 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000123 }
124 break;
125
126 case 'p':
127 // This upgrades the llvm.part.select overloaded intrinsic names to only
128 // use one type specifier in the name. We only care about the old format
129 // 'llvm.part.select.i*.i*', and solve as above with bswap.
130 if (Name.compare(5,12,"part.select.",12) == 0) {
131 std::string::size_type delim = Name.find('.',17);
132
133 if (delim != std::string::npos) {
134 // Construct a new name as 'llvm.part.select' + '.i*'
135 F->setName(Name.substr(0,16)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000136 NewFn = F;
137 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000138 }
139 break;
140 }
141
142 // This upgrades the llvm.part.set intrinsics similarly as above, however
143 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
144 // must match. There is an additional type specifier after these two
145 // matching types that we must retain when upgrading. Thus, we require
146 // finding 2 periods, not just one, after the intrinsic name.
147 if (Name.compare(5,9,"part.set.",9) == 0) {
148 std::string::size_type delim = Name.find('.',14);
149
150 if (delim != std::string::npos &&
151 Name.find('.',delim+1) != std::string::npos) {
152 // Construct a new name as 'llvm.part.select' + '.i*.i*'
153 F->setName(Name.substr(0,13)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000154 NewFn = F;
155 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000156 }
157 break;
158 }
159
160 break;
Anders Carlssonf924f342007-12-14 06:38:54 +0000161 case 'x':
162 // This fixes all MMX shift intrinsic instructions to take a
163 // v1i64 instead of a v2i32 as the second parameter.
164 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
165 (Name.compare(13,4,"psll", 4) == 0 ||
166 Name.compare(13,4,"psra", 4) == 0 ||
Evan Chengcdf22f22008-05-03 00:52:09 +0000167 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
Anders Carlssonf924f342007-12-14 06:38:54 +0000168
Owen Anderson155dccd82009-07-07 23:43:39 +0000169 const llvm::Type *VT =
170 Context->getVectorType(Context->getIntegerType(64), 1);
Anders Carlssonf924f342007-12-14 06:38:54 +0000171
172 // We don't have to do anything if the parameter already has
173 // the correct type.
174 if (FTy->getParamType(1) == VT)
175 break;
176
177 // We first need to change the name of the old (bad) intrinsic, because
178 // its type is incorrect, but we cannot overload that name. We
179 // arbitrarily unique it here allowing us to construct a correctly named
180 // and typed function below.
181 F->setName("");
182
183 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
184
185 // Now construct the new intrinsic with the correct name and type. We
186 // leave the old function around in order to query its type, whatever it
187 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000188 NewFn = cast<Function>(M->getOrInsertFunction(Name,
189 FTy->getReturnType(),
190 FTy->getParamType(0),
191 VT,
192 (Type *)0));
193 return true;
Evan Cheng50659322008-05-24 00:08:39 +0000194 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
195 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
Evan Cheng2146270c2008-05-24 02:14:05 +0000196 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
197 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
198 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
199 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000200 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
201 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
202 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
Evan Cheng50659322008-05-24 00:08:39 +0000203 // Calls to these intrinsics are transformed into ShuffleVector's.
Evan Cheng0e179d02007-12-17 22:33:23 +0000204 NewFn = 0;
205 return true;
Anders Carlssonf924f342007-12-14 06:38:54 +0000206 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000207
Anders Carlssonf924f342007-12-14 06:38:54 +0000208 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000209 }
210
211 // This may not belong here. This function is effectively being overloaded
212 // to both detect an intrinsic which needs upgrading, and to provide the
213 // upgraded form of the intrinsic. We should perhaps have two separate
214 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000215 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000216}
217
Evan Cheng0e179d02007-12-17 22:33:23 +0000218bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
219 NewFn = 0;
220 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000221
222 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000223 if (NewFn)
224 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000225 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000226 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000227 return Upgraded;
228}
229
Chandler Carruth7132e002007-08-04 01:51:18 +0000230// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
231// upgraded intrinsic. All argument and return casting must be provided in
232// order to seamlessly integrate with existing context.
233void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000234 Function *F = CI->getCalledFunction();
Owen Anderson155dccd82009-07-07 23:43:39 +0000235 LLVMContext* Context = F->getContext();
236
Chandler Carruth7132e002007-08-04 01:51:18 +0000237 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000238
239 if (!NewFn) {
Evan Cheng50659322008-05-24 00:08:39 +0000240 bool isLoadH = false, isLoadL = false, isMovL = false;
Evan Cheng2146270c2008-05-24 02:14:05 +0000241 bool isMovSD = false, isShufPD = false;
242 bool isUnpckhPD = false, isUnpcklPD = false;
Evan Cheng91a2e562008-05-24 02:56:30 +0000243 bool isPunpckhQPD = false, isPunpcklQPD = false;
Evan Cheng50659322008-05-24 00:08:39 +0000244 if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadh.pd") == 0)
245 isLoadH = true;
246 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadl.pd") == 0)
247 isLoadL = true;
248 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0)
249 isMovL = true;
Evan Cheng2146270c2008-05-24 02:14:05 +0000250 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movs.d") == 0)
251 isMovSD = true;
252 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.shuf.pd") == 0)
253 isShufPD = true;
254 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckh.pd") == 0)
255 isUnpckhPD = true;
256 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckl.pd") == 0)
257 isUnpcklPD = true;
Evan Cheng91a2e562008-05-24 02:56:30 +0000258 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.punpckh.qdq") == 0)
259 isPunpckhQPD = true;
260 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.punpckl.qdq") == 0)
261 isPunpcklQPD = true;
Evan Cheng0e179d02007-12-17 22:33:23 +0000262
Evan Cheng2146270c2008-05-24 02:14:05 +0000263 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000264 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Evan Cheng50659322008-05-24 00:08:39 +0000265 std::vector<Constant*> Idxs;
266 Value *Op0 = CI->getOperand(1);
Evan Cheng2146270c2008-05-24 02:14:05 +0000267 ShuffleVectorInst *SI = NULL;
Evan Cheng50659322008-05-24 00:08:39 +0000268 if (isLoadH || isLoadL) {
Owen Anderson155dccd82009-07-07 23:43:39 +0000269 Value *Op1 = Context->getUndef(Op0->getType());
Evan Cheng50659322008-05-24 00:08:39 +0000270 Value *Addr = new BitCastInst(CI->getOperand(2),
Owen Anderson155dccd82009-07-07 23:43:39 +0000271 Context->getPointerTypeUnqual(Type::DoubleTy),
Evan Cheng50659322008-05-24 00:08:39 +0000272 "upgraded.", CI);
273 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
Owen Anderson155dccd82009-07-07 23:43:39 +0000274 Value *Idx = Context->getConstantInt(Type::Int32Ty, 0);
Evan Cheng50659322008-05-24 00:08:39 +0000275 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
276
277 if (isLoadH) {
Owen Anderson155dccd82009-07-07 23:43:39 +0000278 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 0));
279 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
Evan Cheng50659322008-05-24 00:08:39 +0000280 } else {
Owen Anderson155dccd82009-07-07 23:43:39 +0000281 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
282 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
Evan Cheng50659322008-05-24 00:08:39 +0000283 }
Owen Anderson155dccd82009-07-07 23:43:39 +0000284 Value *Mask = Context->getConstantVector(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000285 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng2146270c2008-05-24 02:14:05 +0000286 } else if (isMovL) {
Owen Anderson155dccd82009-07-07 23:43:39 +0000287 Constant *Zero = Context->getConstantInt(Type::Int32Ty, 0);
Evan Cheng50659322008-05-24 00:08:39 +0000288 Idxs.push_back(Zero);
289 Idxs.push_back(Zero);
290 Idxs.push_back(Zero);
291 Idxs.push_back(Zero);
Owen Anderson155dccd82009-07-07 23:43:39 +0000292 Value *ZeroV = Context->getConstantVector(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000293
294 Idxs.clear();
Owen Anderson155dccd82009-07-07 23:43:39 +0000295 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 4));
296 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 5));
297 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
298 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 3));
299 Value *Mask = Context->getConstantVector(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000300 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
Evan Cheng91a2e562008-05-24 02:56:30 +0000301 } else if (isMovSD ||
302 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Evan Cheng2146270c2008-05-24 02:14:05 +0000303 Value *Op1 = CI->getOperand(2);
304 if (isMovSD) {
Owen Anderson155dccd82009-07-07 23:43:39 +0000305 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
306 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
Evan Cheng91a2e562008-05-24 02:56:30 +0000307 } else if (isUnpckhPD || isPunpckhQPD) {
Owen Anderson155dccd82009-07-07 23:43:39 +0000308 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 1));
309 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 3));
Evan Cheng2146270c2008-05-24 02:14:05 +0000310 } else {
Owen Anderson155dccd82009-07-07 23:43:39 +0000311 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 0));
312 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, 2));
Evan Cheng2146270c2008-05-24 02:14:05 +0000313 }
Owen Anderson155dccd82009-07-07 23:43:39 +0000314 Value *Mask = Context->getConstantVector(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000315 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
316 } else if (isShufPD) {
317 Value *Op1 = CI->getOperand(2);
318 unsigned MaskVal = cast<ConstantInt>(CI->getOperand(3))->getZExtValue();
Owen Anderson155dccd82009-07-07 23:43:39 +0000319 Idxs.push_back(Context->getConstantInt(Type::Int32Ty, MaskVal & 1));
320 Idxs.push_back(Context->getConstantInt(Type::Int32Ty,
321 ((MaskVal >> 1) & 1)+2));
322 Value *Mask = Context->getConstantVector(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000323 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng50659322008-05-24 00:08:39 +0000324 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000325
Evan Cheng2146270c2008-05-24 02:14:05 +0000326 assert(SI && "Unexpected!");
327
Evan Cheng0e179d02007-12-17 22:33:23 +0000328 // Handle any uses of the old CallInst.
329 if (!CI->use_empty())
330 // Replace all uses of the old call with the new cast which has the
331 // correct type.
332 CI->replaceAllUsesWith(SI);
333
334 // Clean up the old call now that it has been completely upgraded.
335 CI->eraseFromParent();
Evan Chenga8288f42007-12-18 01:04:25 +0000336 } else {
Torok Edwin56d06592009-07-11 20:10:48 +0000337 LLVM_UNREACHABLE("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000338 }
339 return;
340 }
341
Gabor Greife9ecc682008-04-06 20:25:17 +0000342 switch (NewFn->getIntrinsicID()) {
Torok Edwin56d06592009-07-11 20:10:48 +0000343 default: LLVM_UNREACHABLE("Unknown function for CallInst upgrade.");
Anders Carlssonf924f342007-12-14 06:38:54 +0000344 case Intrinsic::x86_mmx_psll_d:
345 case Intrinsic::x86_mmx_psll_q:
346 case Intrinsic::x86_mmx_psll_w:
347 case Intrinsic::x86_mmx_psra_d:
348 case Intrinsic::x86_mmx_psra_w:
349 case Intrinsic::x86_mmx_psrl_d:
350 case Intrinsic::x86_mmx_psrl_q:
351 case Intrinsic::x86_mmx_psrl_w: {
Chris Lattner8a923e72008-03-12 17:45:29 +0000352 Value *Operands[2];
Anders Carlssonf924f342007-12-14 06:38:54 +0000353
Chris Lattner8a923e72008-03-12 17:45:29 +0000354 Operands[0] = CI->getOperand(1);
Anders Carlssonf924f342007-12-14 06:38:54 +0000355
356 // Cast the second parameter to the correct type.
357 BitCastInst *BC = new BitCastInst(CI->getOperand(2),
358 NewFn->getFunctionType()->getParamType(1),
Evan Cheng50659322008-05-24 00:08:39 +0000359 "upgraded.", CI);
Chris Lattner8a923e72008-03-12 17:45:29 +0000360 Operands[1] = BC;
Anders Carlssonf924f342007-12-14 06:38:54 +0000361
362 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000363 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
364 "upgraded."+CI->getName(), CI);
Anders Carlssonf924f342007-12-14 06:38:54 +0000365 NewCI->setTailCall(CI->isTailCall());
366 NewCI->setCallingConv(CI->getCallingConv());
367
368 // Handle any uses of the old CallInst.
369 if (!CI->use_empty())
370 // Replace all uses of the old call with the new cast which has the
371 // correct type.
372 CI->replaceAllUsesWith(NewCI);
373
374 // Clean up the old call now that it has been completely upgraded.
375 CI->eraseFromParent();
376 break;
377 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000378 case Intrinsic::ctlz:
379 case Intrinsic::ctpop:
Gabor Greife9ecc682008-04-06 20:25:17 +0000380 case Intrinsic::cttz: {
Chandler Carruth7132e002007-08-04 01:51:18 +0000381 // Build a small vector of the 1..(N-1) operands, which are the
382 // parameters.
Chris Lattner8a923e72008-03-12 17:45:29 +0000383 SmallVector<Value*, 8> Operands(CI->op_begin()+1, CI->op_end());
Chandler Carruth7132e002007-08-04 01:51:18 +0000384
385 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000386 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
387 "upgraded."+CI->getName(), CI);
Chandler Carruth7132e002007-08-04 01:51:18 +0000388 NewCI->setTailCall(CI->isTailCall());
389 NewCI->setCallingConv(CI->getCallingConv());
390
391 // Handle any uses of the old CallInst.
392 if (!CI->use_empty()) {
393 // Check for sign extend parameter attributes on the return values.
Devang Patel4c758ea2008-09-25 21:00:45 +0000394 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
395 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
Chandler Carruth7132e002007-08-04 01:51:18 +0000396
397 // Construct an appropriate cast from the new return type to the old.
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000398 CastInst *RetCast = CastInst::Create(
Chandler Carruth7132e002007-08-04 01:51:18 +0000399 CastInst::getCastOpcode(NewCI, SrcSExt,
400 F->getReturnType(),
401 DestSExt),
402 NewCI, F->getReturnType(),
403 NewCI->getName(), CI);
404 NewCI->moveBefore(RetCast);
405
406 // Replace all uses of the old call with the new cast which has the
407 // correct type.
408 CI->replaceAllUsesWith(RetCast);
409 }
410
411 // Clean up the old call now that it has been completely upgraded.
412 CI->eraseFromParent();
Gabor Greife9ecc682008-04-06 20:25:17 +0000413 }
414 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000415 }
416}
417
418// This tests each Function to determine if it needs upgrading. When we find
419// one we are interested in, we then upgrade all calls to reflect the new
420// function.
421void llvm::UpgradeCallsToIntrinsic(Function* F) {
422 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
423
424 // Upgrade the function and check if it is a totaly new function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000425 Function* NewFn;
426 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000427 if (NewFn != F) {
428 // Replace all uses to the old function with the new one if necessary.
429 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
430 UI != UE; ) {
431 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
432 UpgradeIntrinsicCall(CI, NewFn);
433 }
434 // Remove old function, no longer used, from the module.
435 F->eraseFromParent();
436 }
437 }
438}