blob: 9330e141c34120f4cd45e4c4a68f6ca6dc734d3b [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"
Devang Patel80ae3492009-08-28 23:24:31 +000019#include "llvm/IntrinsicInst.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000020#include "llvm/ADT/SmallVector.h"
Gabor Greife5406532010-06-23 08:45:32 +000021#include "llvm/Support/CallSite.h"
Torok Edwin56d06592009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Eric Christopher64831c62010-04-20 00:59:54 +000023#include "llvm/Support/IRBuilder.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000024#include <cstring>
Chandler Carruth7132e002007-08-04 01:51:18 +000025using namespace llvm;
26
27
Evan Cheng0e179d02007-12-17 22:33:23 +000028static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000029 assert(F && "Illegal to upgrade a non-existent Function.");
30
31 // Get the Function's name.
32 const std::string& Name = F->getName();
33
34 // Convenience
35 const FunctionType *FTy = F->getFunctionType();
36
37 // Quickly eliminate it, if it's not a candidate.
38 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
39 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
Evan Cheng0e179d02007-12-17 22:33:23 +000040 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +000041
42 Module *M = F->getParent();
43 switch (Name[5]) {
44 default: break;
Mon P Wang6a490372008-06-25 08:15:39 +000045 case 'a':
Mon P Wang2c839d42008-07-30 04:36:53 +000046 // This upgrades the llvm.atomic.lcs, llvm.atomic.las, llvm.atomic.lss,
47 // and atomics with default address spaces to their new names to their new
48 // function name (e.g. llvm.atomic.add.i32 => llvm.atomic.add.i32.p0i32)
49 if (Name.compare(5,7,"atomic.",7) == 0) {
Mon P Wang6a490372008-06-25 08:15:39 +000050 if (Name.compare(12,3,"lcs",3) == 0) {
51 std::string::size_type delim = Name.find('.',12);
Mon P Wang2c839d42008-07-30 04:36:53 +000052 F->setName("llvm.atomic.cmp.swap" + Name.substr(delim) +
53 ".p0" + Name.substr(delim+1));
Mon P Wang6a490372008-06-25 08:15:39 +000054 NewFn = F;
55 return true;
56 }
57 else if (Name.compare(12,3,"las",3) == 0) {
58 std::string::size_type delim = Name.find('.',12);
Mon P Wang2c839d42008-07-30 04:36:53 +000059 F->setName("llvm.atomic.load.add"+Name.substr(delim)
60 + ".p0" + Name.substr(delim+1));
Mon P Wang6a490372008-06-25 08:15:39 +000061 NewFn = F;
62 return true;
63 }
64 else if (Name.compare(12,3,"lss",3) == 0) {
65 std::string::size_type delim = Name.find('.',12);
Mon P Wang2c839d42008-07-30 04:36:53 +000066 F->setName("llvm.atomic.load.sub"+Name.substr(delim)
67 + ".p0" + Name.substr(delim+1));
68 NewFn = F;
69 return true;
70 }
71 else if (Name.rfind(".p") == std::string::npos) {
72 // We don't have an address space qualifier so this has be upgraded
73 // to the new name. Copy the type name at the end of the intrinsic
74 // and add to it
75 std::string::size_type delim = Name.find_last_of('.');
76 assert(delim != std::string::npos && "can not find type");
77 F->setName(Name + ".p0" + Name.substr(delim+1));
Mon P Wang6a490372008-06-25 08:15:39 +000078 NewFn = F;
79 return true;
80 }
Bob Wilson9a511c02010-08-20 04:54:02 +000081 } else if (Name.compare(5, 9, "arm.neon.", 9) == 0) {
Bob Wilsond0c05482010-08-29 05:57:34 +000082 if (((Name.compare(14, 5, "vmovl", 5) == 0 ||
83 Name.compare(14, 5, "vaddl", 5) == 0 ||
Bob Wilsonf65c9ef2010-09-03 01:35:08 +000084 Name.compare(14, 5, "vsubl", 5) == 0 ||
85 Name.compare(14, 5, "vaddw", 5) == 0 ||
86 Name.compare(14, 5, "vsubw", 5) == 0 ||
87 Name.compare(14, 5, "vmull", 5) == 0 ||
Bob Wilson38ab35a2010-09-01 23:50:19 +000088 Name.compare(14, 5, "vmlal", 5) == 0 ||
Bob Wilsonf65c9ef2010-09-03 01:35:08 +000089 Name.compare(14, 5, "vmlsl", 5) == 0 ||
90 Name.compare(14, 5, "vabdl", 5) == 0 ||
91 Name.compare(14, 5, "vabal", 5) == 0) &&
Bob Wilson38ab35a2010-09-01 23:50:19 +000092 (Name.compare(19, 2, "s.", 2) == 0 ||
93 Name.compare(19, 2, "u.", 2) == 0)) ||
94
Bob Wilsonf65c9ef2010-09-03 01:35:08 +000095 (Name.compare(14, 4, "vaba", 4) == 0 &&
96 (Name.compare(18, 2, "s.", 2) == 0 ||
97 Name.compare(18, 2, "u.", 2) == 0)) ||
98
Bob Wilson4cd8a122010-08-30 20:02:30 +000099 (Name.compare(14, 6, "vmovn.", 6) == 0)) {
Bob Wilsond0c05482010-08-29 05:57:34 +0000100
Bob Wilson9a511c02010-08-20 04:54:02 +0000101 // Calls to these are transformed into IR without intrinsics.
102 NewFn = 0;
103 return true;
104 }
Bob Wilsonedf722a2010-08-27 17:13:24 +0000105 // Old versions of NEON ld/st intrinsics are missing alignment arguments.
106 bool isVLd = (Name.compare(14, 3, "vld", 3) == 0);
107 bool isVSt = (Name.compare(14, 3, "vst", 3) == 0);
108 if (isVLd || isVSt) {
109 unsigned NumVecs = Name.at(17) - '0';
110 if (NumVecs == 0 || NumVecs > 4)
111 return false;
112 bool isLaneOp = (Name.compare(18, 5, "lane.", 5) == 0);
113 if (!isLaneOp && Name.at(18) != '.')
114 return false;
115 unsigned ExpectedArgs = 2; // for the address and alignment
116 if (isVSt || isLaneOp)
117 ExpectedArgs += NumVecs;
118 if (isLaneOp)
119 ExpectedArgs += 1; // for the lane number
120 unsigned NumP = FTy->getNumParams();
121 if (NumP != ExpectedArgs - 1)
122 return false;
123
124 // Change the name of the old (bad) intrinsic, because
125 // its type is incorrect, but we cannot overload that name.
126 F->setName("");
127
128 // One argument is missing: add the alignment argument.
129 std::vector<const Type*> NewParams;
130 for (unsigned p = 0; p < NumP; ++p)
131 NewParams.push_back(FTy->getParamType(p));
132 NewParams.push_back(Type::getInt32Ty(F->getContext()));
133 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(),
134 NewParams, false);
135 NewFn = cast<Function>(M->getOrInsertFunction(Name, NewFTy));
136 return true;
137 }
Mon P Wang6a490372008-06-25 08:15:39 +0000138 }
139 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000140 case 'b':
141 // This upgrades the name of the llvm.bswap intrinsic function to only use
142 // a single type name for overloading. We only care about the old format
143 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
144 // a '.' after 'bswap.'
145 if (Name.compare(5,6,"bswap.",6) == 0) {
146 std::string::size_type delim = Name.find('.',11);
147
148 if (delim != std::string::npos) {
149 // Construct the new name as 'llvm.bswap' + '.i*'
150 F->setName(Name.substr(0,10)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000151 NewFn = F;
152 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000153 }
154 }
155 break;
156
157 case 'c':
158 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
159 // correct return type, so we check for the name, and then check if the
160 // return type does not match the parameter type.
161 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
162 Name.compare(5,4,"ctlz",4) == 0 ||
163 Name.compare(5,4,"cttz",4) == 0) &&
164 FTy->getReturnType() != FTy->getParamType(0)) {
165 // We first need to change the name of the old (bad) intrinsic, because
166 // its type is incorrect, but we cannot overload that name. We
167 // arbitrarily unique it here allowing us to construct a correctly named
168 // and typed function below.
169 F->setName("");
170
171 // Now construct the new intrinsic with the correct name and type. We
172 // leave the old function around in order to query its type, whatever it
173 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000174 NewFn = cast<Function>(M->getOrInsertFunction(Name,
175 FTy->getParamType(0),
176 FTy->getParamType(0),
177 (Type *)0));
178 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000179 }
180 break;
181
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000182 case 'e':
183 // The old llvm.eh.selector.i32 is equivalent to the new llvm.eh.selector.
184 if (Name.compare("llvm.eh.selector.i32") == 0) {
185 F->setName("llvm.eh.selector");
186 NewFn = F;
187 return true;
188 }
189 // The old llvm.eh.typeid.for.i32 is equivalent to llvm.eh.typeid.for.
190 if (Name.compare("llvm.eh.typeid.for.i32") == 0) {
191 F->setName("llvm.eh.typeid.for");
192 NewFn = F;
193 return true;
194 }
195 // Convert the old llvm.eh.selector.i64 to a call to llvm.eh.selector.
196 if (Name.compare("llvm.eh.selector.i64") == 0) {
197 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_selector);
198 return true;
199 }
200 // Convert the old llvm.eh.typeid.for.i64 to a call to llvm.eh.typeid.for.
201 if (Name.compare("llvm.eh.typeid.for.i64") == 0) {
202 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_typeid_for);
203 return true;
204 }
205 break;
206
Mon P Wangc576ee92010-04-04 03:10:48 +0000207 case 'm': {
208 // This upgrades the llvm.memcpy, llvm.memmove, and llvm.memset to the
209 // new format that allows overloading the pointer for different address
210 // space (e.g., llvm.memcpy.i16 => llvm.memcpy.p0i8.p0i8.i16)
211 const char* NewFnName = NULL;
212 if (Name.compare(5,8,"memcpy.i",8) == 0) {
213 if (Name[13] == '8')
214 NewFnName = "llvm.memcpy.p0i8.p0i8.i8";
215 else if (Name.compare(13,2,"16") == 0)
216 NewFnName = "llvm.memcpy.p0i8.p0i8.i16";
217 else if (Name.compare(13,2,"32") == 0)
218 NewFnName = "llvm.memcpy.p0i8.p0i8.i32";
219 else if (Name.compare(13,2,"64") == 0)
220 NewFnName = "llvm.memcpy.p0i8.p0i8.i64";
221 } else if (Name.compare(5,9,"memmove.i",9) == 0) {
222 if (Name[14] == '8')
223 NewFnName = "llvm.memmove.p0i8.p0i8.i8";
224 else if (Name.compare(14,2,"16") == 0)
225 NewFnName = "llvm.memmove.p0i8.p0i8.i16";
226 else if (Name.compare(14,2,"32") == 0)
227 NewFnName = "llvm.memmove.p0i8.p0i8.i32";
228 else if (Name.compare(14,2,"64") == 0)
229 NewFnName = "llvm.memmove.p0i8.p0i8.i64";
230 }
231 else if (Name.compare(5,8,"memset.i",8) == 0) {
232 if (Name[13] == '8')
233 NewFnName = "llvm.memset.p0i8.i8";
234 else if (Name.compare(13,2,"16") == 0)
235 NewFnName = "llvm.memset.p0i8.i16";
236 else if (Name.compare(13,2,"32") == 0)
237 NewFnName = "llvm.memset.p0i8.i32";
238 else if (Name.compare(13,2,"64") == 0)
239 NewFnName = "llvm.memset.p0i8.i64";
240 }
241 if (NewFnName) {
Mon P Wangc576ee92010-04-04 03:10:48 +0000242 NewFn = cast<Function>(M->getOrInsertFunction(NewFnName,
243 FTy->getReturnType(),
244 FTy->getParamType(0),
245 FTy->getParamType(1),
246 FTy->getParamType(2),
247 FTy->getParamType(3),
248 Type::getInt1Ty(F->getContext()),
249 (Type *)0));
250 return true;
251 }
252 break;
253 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000254 case 'p':
255 // This upgrades the llvm.part.select overloaded intrinsic names to only
256 // use one type specifier in the name. We only care about the old format
257 // 'llvm.part.select.i*.i*', and solve as above with bswap.
258 if (Name.compare(5,12,"part.select.",12) == 0) {
259 std::string::size_type delim = Name.find('.',17);
260
261 if (delim != std::string::npos) {
262 // Construct a new name as 'llvm.part.select' + '.i*'
263 F->setName(Name.substr(0,16)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000264 NewFn = F;
265 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000266 }
267 break;
268 }
269
270 // This upgrades the llvm.part.set intrinsics similarly as above, however
271 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
272 // must match. There is an additional type specifier after these two
273 // matching types that we must retain when upgrading. Thus, we require
274 // finding 2 periods, not just one, after the intrinsic name.
275 if (Name.compare(5,9,"part.set.",9) == 0) {
276 std::string::size_type delim = Name.find('.',14);
277
278 if (delim != std::string::npos &&
279 Name.find('.',delim+1) != std::string::npos) {
280 // Construct a new name as 'llvm.part.select' + '.i*.i*'
281 F->setName(Name.substr(0,13)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000282 NewFn = F;
283 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000284 }
285 break;
286 }
287
288 break;
Anders Carlssonf924f342007-12-14 06:38:54 +0000289 case 'x':
290 // This fixes all MMX shift intrinsic instructions to take a
291 // v1i64 instead of a v2i32 as the second parameter.
292 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
293 (Name.compare(13,4,"psll", 4) == 0 ||
294 Name.compare(13,4,"psra", 4) == 0 ||
Evan Chengcdf22f22008-05-03 00:52:09 +0000295 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
Anders Carlssonf924f342007-12-14 06:38:54 +0000296
Owen Anderson155dccd82009-07-07 23:43:39 +0000297 const llvm::Type *VT =
Owen Anderson55f1c092009-08-13 21:58:54 +0000298 VectorType::get(IntegerType::get(FTy->getContext(), 64), 1);
Anders Carlssonf924f342007-12-14 06:38:54 +0000299
300 // We don't have to do anything if the parameter already has
301 // the correct type.
302 if (FTy->getParamType(1) == VT)
303 break;
304
305 // We first need to change the name of the old (bad) intrinsic, because
306 // its type is incorrect, but we cannot overload that name. We
307 // arbitrarily unique it here allowing us to construct a correctly named
308 // and typed function below.
309 F->setName("");
310
311 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
312
313 // Now construct the new intrinsic with the correct name and type. We
314 // leave the old function around in order to query its type, whatever it
315 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000316 NewFn = cast<Function>(M->getOrInsertFunction(Name,
317 FTy->getReturnType(),
318 FTy->getParamType(0),
319 VT,
320 (Type *)0));
321 return true;
Evan Cheng50659322008-05-24 00:08:39 +0000322 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
323 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
Evan Cheng2146270c2008-05-24 02:14:05 +0000324 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
325 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
326 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
327 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000328 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
329 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
330 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
Evan Cheng50659322008-05-24 00:08:39 +0000331 // Calls to these intrinsics are transformed into ShuffleVector's.
Evan Cheng0e179d02007-12-17 22:33:23 +0000332 NewFn = 0;
333 return true;
Eric Christopher6ad81672010-03-30 18:49:01 +0000334 } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) {
335 // Calls to these intrinsics are transformed into vector multiplies.
336 NewFn = 0;
337 return true;
Eric Christopher64831c62010-04-20 00:59:54 +0000338 } else if (Name.compare(5, 18, "x86.ssse3.palign.r", 18) == 0 ||
339 Name.compare(5, 22, "x86.ssse3.palign.r.128", 22) == 0) {
340 // Calls to these intrinsics are transformed into vector shuffles, shifts,
341 // or 0.
342 NewFn = 0;
343 return true;
Anders Carlssonf924f342007-12-14 06:38:54 +0000344 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000345
Anders Carlssonf924f342007-12-14 06:38:54 +0000346 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000347 }
348
349 // This may not belong here. This function is effectively being overloaded
350 // to both detect an intrinsic which needs upgrading, and to provide the
351 // upgraded form of the intrinsic. We should perhaps have two separate
352 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000353 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000354}
355
Evan Cheng0e179d02007-12-17 22:33:23 +0000356bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
357 NewFn = 0;
358 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000359
360 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000361 if (NewFn)
362 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000363 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000364 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000365 return Upgraded;
366}
367
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000368bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Bill Wendling55165fe2010-09-10 20:42:26 +0000369 StringRef Name(GV->getName());
Bill Wendling6a57e242010-09-10 19:06:58 +0000370
371 // We are only upgrading one symbol here.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000372 if (Name == ".llvm.eh.catch.all.value") {
373 GV->setName("llvm.eh.catch.all.value");
374 return true;
375 }
376
377 return false;
378}
379
Bob Wilson38ab35a2010-09-01 23:50:19 +0000380/// ExtendNEONArgs - For NEON "long" and "wide" operations, where the results
381/// have vector elements twice as big as one or both source operands, do the
382/// sign- or zero-extension that used to be handled by intrinsics. The
383/// extended values are returned via V0 and V1.
384static void ExtendNEONArgs(CallInst *CI, Value *Arg0, Value *Arg1,
385 Value *&V0, Value *&V1) {
386 Function *F = CI->getCalledFunction();
387 const std::string& Name = F->getName();
388 bool isLong = (Name.at(18) == 'l');
389 bool isSigned = (Name.at(19) == 's');
390
391 if (isSigned) {
392 if (isLong)
393 V0 = new SExtInst(Arg0, CI->getType(), "", CI);
394 else
395 V0 = Arg0;
396 V1 = new SExtInst(Arg1, CI->getType(), "", CI);
397 } else {
398 if (isLong)
399 V0 = new ZExtInst(Arg0, CI->getType(), "", CI);
400 else
401 V0 = Arg0;
402 V1 = new ZExtInst(Arg1, CI->getType(), "", CI);
403 }
404}
405
Bob Wilsonf65c9ef2010-09-03 01:35:08 +0000406/// CallVABD - As part of expanding a call to one of the old NEON vabdl, vaba,
407/// or vabal intrinsics, construct a call to a vabd intrinsic. Examine the
408/// name of the old intrinsic to determine whether to use a signed or unsigned
409/// vabd intrinsic. Get the type from the old call instruction, adjusted for
410/// half-size vector elements if the old intrinsic was vabdl or vabal.
411static Instruction *CallVABD(CallInst *CI, Value *Arg0, Value *Arg1) {
412 Function *F = CI->getCalledFunction();
413 const std::string& Name = F->getName();
414 bool isLong = (Name.at(18) == 'l');
415 bool isSigned = (Name.at(isLong ? 19 : 18) == 's');
416
417 Intrinsic::ID intID;
418 if (isSigned)
419 intID = Intrinsic::arm_neon_vabds;
420 else
421 intID = Intrinsic::arm_neon_vabdu;
422
423 const Type *Ty = CI->getType();
424 if (isLong)
425 Ty = VectorType::getTruncatedElementVectorType(cast<const VectorType>(Ty));
426
427 Function *VABD = Intrinsic::getDeclaration(F->getParent(), intID, &Ty, 1);
428 Value *Operands[2];
429 Operands[0] = Arg0;
430 Operands[1] = Arg1;
431 return CallInst::Create(VABD, Operands, Operands+2,
432 "upgraded."+CI->getName(), CI);
433}
434
Chandler Carruth7132e002007-08-04 01:51:18 +0000435// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
436// upgraded intrinsic. All argument and return casting must be provided in
437// order to seamlessly integrate with existing context.
438void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000439 Function *F = CI->getCalledFunction();
Owen Anderson55f1c092009-08-13 21:58:54 +0000440 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +0000441 ImmutableCallSite CS(CI);
442
Chandler Carruth7132e002007-08-04 01:51:18 +0000443 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000444
445 if (!NewFn) {
Bob Wilson9a511c02010-08-20 04:54:02 +0000446 // Get the Function's name.
447 const std::string& Name = F->getName();
448
449 // Upgrade ARM NEON intrinsics.
450 if (Name.compare(5, 9, "arm.neon.", 9) == 0) {
451 Instruction *NewI;
Bob Wilson38ab35a2010-09-01 23:50:19 +0000452 Value *V0, *V1;
Bob Wilson9a511c02010-08-20 04:54:02 +0000453 if (Name.compare(14, 7, "vmovls.", 7) == 0) {
454 NewI = new SExtInst(CI->getArgOperand(0), CI->getType(),
455 "upgraded." + CI->getName(), CI);
456 } else if (Name.compare(14, 7, "vmovlu.", 7) == 0) {
457 NewI = new ZExtInst(CI->getArgOperand(0), CI->getType(),
458 "upgraded." + CI->getName(), CI);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000459 } else if (Name.compare(14, 4, "vadd", 4) == 0) {
460 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
461 NewI = BinaryOperator::CreateAdd(V0, V1, "upgraded."+CI->getName(), CI);
462 } else if (Name.compare(14, 4, "vsub", 4) == 0) {
463 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
464 NewI = BinaryOperator::CreateSub(V0, V1,"upgraded."+CI->getName(),CI);
465 } else if (Name.compare(14, 4, "vmul", 4) == 0) {
466 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
467 NewI = BinaryOperator::CreateMul(V0, V1,"upgraded."+CI->getName(),CI);
468 } else if (Name.compare(14, 4, "vmla", 4) == 0) {
469 ExtendNEONArgs(CI, CI->getArgOperand(1), CI->getArgOperand(2), V0, V1);
470 Instruction *MulI = BinaryOperator::CreateMul(V0, V1, "", CI);
471 NewI = BinaryOperator::CreateAdd(CI->getArgOperand(0), MulI,
472 "upgraded."+CI->getName(), CI);
473 } else if (Name.compare(14, 4, "vmls", 4) == 0) {
474 ExtendNEONArgs(CI, CI->getArgOperand(1), CI->getArgOperand(2), V0, V1);
475 Instruction *MulI = BinaryOperator::CreateMul(V0, V1, "", CI);
476 NewI = BinaryOperator::CreateSub(CI->getArgOperand(0), MulI,
477 "upgraded."+CI->getName(), CI);
Bob Wilsonf65c9ef2010-09-03 01:35:08 +0000478 } else if (Name.compare(14, 4, "vabd", 4) == 0) {
479 NewI = CallVABD(CI, CI->getArgOperand(0), CI->getArgOperand(1));
480 NewI = new ZExtInst(NewI, CI->getType(), "upgraded."+CI->getName(), CI);
481 } else if (Name.compare(14, 4, "vaba", 4) == 0) {
482 NewI = CallVABD(CI, CI->getArgOperand(1), CI->getArgOperand(2));
483 if (Name.at(18) == 'l')
484 NewI = new ZExtInst(NewI, CI->getType(), "", CI);
485 NewI = BinaryOperator::CreateAdd(CI->getArgOperand(0), NewI,
486 "upgraded."+CI->getName(), CI);
Bob Wilson4cd8a122010-08-30 20:02:30 +0000487 } else if (Name.compare(14, 6, "vmovn.", 6) == 0) {
488 NewI = new TruncInst(CI->getArgOperand(0), CI->getType(),
489 "upgraded." + CI->getName(), CI);
Bob Wilson9a511c02010-08-20 04:54:02 +0000490 } else {
491 llvm_unreachable("Unknown arm.neon function for CallInst upgrade.");
492 }
493 // Replace any uses of the old CallInst.
494 if (!CI->use_empty())
495 CI->replaceAllUsesWith(NewI);
496 CI->eraseFromParent();
497 return;
498 }
499
Evan Cheng50659322008-05-24 00:08:39 +0000500 bool isLoadH = false, isLoadL = false, isMovL = false;
Evan Cheng2146270c2008-05-24 02:14:05 +0000501 bool isMovSD = false, isShufPD = false;
502 bool isUnpckhPD = false, isUnpcklPD = false;
Evan Cheng91a2e562008-05-24 02:56:30 +0000503 bool isPunpckhQPD = false, isPunpcklQPD = false;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000504 if (F->getName() == "llvm.x86.sse2.loadh.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000505 isLoadH = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000506 else if (F->getName() == "llvm.x86.sse2.loadl.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000507 isLoadL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000508 else if (F->getName() == "llvm.x86.sse2.movl.dq")
Evan Cheng50659322008-05-24 00:08:39 +0000509 isMovL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000510 else if (F->getName() == "llvm.x86.sse2.movs.d")
Evan Cheng2146270c2008-05-24 02:14:05 +0000511 isMovSD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000512 else if (F->getName() == "llvm.x86.sse2.shuf.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000513 isShufPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000514 else if (F->getName() == "llvm.x86.sse2.unpckh.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000515 isUnpckhPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000516 else if (F->getName() == "llvm.x86.sse2.unpckl.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000517 isUnpcklPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000518 else if (F->getName() == "llvm.x86.sse2.punpckh.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000519 isPunpckhQPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000520 else if (F->getName() == "llvm.x86.sse2.punpckl.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000521 isPunpcklQPD = true;
Evan Cheng0e179d02007-12-17 22:33:23 +0000522
Evan Cheng2146270c2008-05-24 02:14:05 +0000523 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000524 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Evan Cheng50659322008-05-24 00:08:39 +0000525 std::vector<Constant*> Idxs;
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000526 Value *Op0 = CI->getArgOperand(0);
Evan Cheng2146270c2008-05-24 02:14:05 +0000527 ShuffleVectorInst *SI = NULL;
Evan Cheng50659322008-05-24 00:08:39 +0000528 if (isLoadH || isLoadL) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000529 Value *Op1 = UndefValue::get(Op0->getType());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000530 Value *Addr = new BitCastInst(CI->getArgOperand(1),
Duncan Sands9ed7b162009-10-06 15:40:36 +0000531 Type::getDoublePtrTy(C),
Evan Cheng50659322008-05-24 00:08:39 +0000532 "upgraded.", CI);
533 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
Owen Anderson55f1c092009-08-13 21:58:54 +0000534 Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000535 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
536
537 if (isLoadH) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000538 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
539 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng50659322008-05-24 00:08:39 +0000540 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000541 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
542 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng50659322008-05-24 00:08:39 +0000543 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000544 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000545 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng2146270c2008-05-24 02:14:05 +0000546 } else if (isMovL) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000547 Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000548 Idxs.push_back(Zero);
549 Idxs.push_back(Zero);
550 Idxs.push_back(Zero);
551 Idxs.push_back(Zero);
Owen Anderson4aa32952009-07-28 21:19:26 +0000552 Value *ZeroV = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000553
554 Idxs.clear();
Owen Anderson55f1c092009-08-13 21:58:54 +0000555 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4));
556 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5));
557 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
558 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Owen Anderson4aa32952009-07-28 21:19:26 +0000559 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000560 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
Evan Cheng91a2e562008-05-24 02:56:30 +0000561 } else if (isMovSD ||
562 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000563 Value *Op1 = CI->getArgOperand(1);
Evan Cheng2146270c2008-05-24 02:14:05 +0000564 if (isMovSD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000565 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
566 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng91a2e562008-05-24 02:56:30 +0000567 } else if (isUnpckhPD || isPunpckhQPD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000568 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
569 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Evan Cheng2146270c2008-05-24 02:14:05 +0000570 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000571 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
572 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng2146270c2008-05-24 02:14:05 +0000573 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000574 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000575 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
576 } else if (isShufPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000577 Value *Op1 = CI->getArgOperand(1);
Gabor Greif3e44ea12010-07-22 10:37:47 +0000578 unsigned MaskVal =
579 cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
Owen Anderson55f1c092009-08-13 21:58:54 +0000580 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
581 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
Owen Anderson155dccd82009-07-07 23:43:39 +0000582 ((MaskVal >> 1) & 1)+2));
Owen Anderson4aa32952009-07-28 21:19:26 +0000583 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000584 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng50659322008-05-24 00:08:39 +0000585 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000586
Evan Cheng2146270c2008-05-24 02:14:05 +0000587 assert(SI && "Unexpected!");
588
Evan Cheng0e179d02007-12-17 22:33:23 +0000589 // Handle any uses of the old CallInst.
590 if (!CI->use_empty())
591 // Replace all uses of the old call with the new cast which has the
592 // correct type.
593 CI->replaceAllUsesWith(SI);
594
595 // Clean up the old call now that it has been completely upgraded.
596 CI->eraseFromParent();
Eric Christopher6ad81672010-03-30 18:49:01 +0000597 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
598 // Upgrade this set of intrinsics into vector multiplies.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000599 Instruction *Mul = BinaryOperator::CreateMul(CI->getArgOperand(0),
600 CI->getArgOperand(1),
Eric Christopher6ad81672010-03-30 18:49:01 +0000601 CI->getName(),
602 CI);
603 // Fix up all the uses with our new multiply.
604 if (!CI->use_empty())
605 CI->replaceAllUsesWith(Mul);
606
607 // Remove upgraded multiply.
608 CI->eraseFromParent();
Eric Christopher64831c62010-04-20 00:59:54 +0000609 } else if (F->getName() == "llvm.x86.ssse3.palign.r") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000610 Value *Op1 = CI->getArgOperand(0);
611 Value *Op2 = CI->getArgOperand(1);
612 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000613 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
614 Value *Rep;
615 IRBuilder<> Builder(C);
616 Builder.SetInsertPoint(CI->getParent(), CI);
617
618 // If palignr is shifting the pair of input vectors less than 9 bytes,
619 // emit a shuffle instruction.
620 if (shiftVal <= 8) {
621 const Type *IntTy = Type::getInt32Ty(C);
622 const Type *EltTy = Type::getInt8Ty(C);
623 const Type *VecTy = VectorType::get(EltTy, 8);
624
625 Op2 = Builder.CreateBitCast(Op2, VecTy);
626 Op1 = Builder.CreateBitCast(Op1, VecTy);
627
628 llvm::SmallVector<llvm::Constant*, 8> Indices;
629 for (unsigned i = 0; i != 8; ++i)
630 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
631
632 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
633 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
634 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
635 }
636
637 // If palignr is shifting the pair of input vectors more than 8 but less
638 // than 16 bytes, emit a logical right shift of the destination.
639 else if (shiftVal < 16) {
640 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
641 const Type *EltTy = Type::getInt64Ty(C);
642 const Type *VecTy = VectorType::get(EltTy, 1);
643
644 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
645 Op2 = ConstantInt::get(VecTy, (shiftVal-8) * 8);
646
647 // create i32 constant
648 Function *I =
649 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_mmx_psrl_q);
650 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
651 }
652
653 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
654 else {
655 Rep = Constant::getNullValue(F->getReturnType());
656 }
657
658 // Replace any uses with our new instruction.
659 if (!CI->use_empty())
660 CI->replaceAllUsesWith(Rep);
661
662 // Remove upgraded instruction.
663 CI->eraseFromParent();
664
665 } else if (F->getName() == "llvm.x86.ssse3.palign.r.128") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000666 Value *Op1 = CI->getArgOperand(0);
667 Value *Op2 = CI->getArgOperand(1);
668 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000669 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
670 Value *Rep;
671 IRBuilder<> Builder(C);
672 Builder.SetInsertPoint(CI->getParent(), CI);
673
674 // If palignr is shifting the pair of input vectors less than 17 bytes,
675 // emit a shuffle instruction.
676 if (shiftVal <= 16) {
677 const Type *IntTy = Type::getInt32Ty(C);
678 const Type *EltTy = Type::getInt8Ty(C);
679 const Type *VecTy = VectorType::get(EltTy, 16);
680
681 Op2 = Builder.CreateBitCast(Op2, VecTy);
682 Op1 = Builder.CreateBitCast(Op1, VecTy);
683
684 llvm::SmallVector<llvm::Constant*, 16> Indices;
685 for (unsigned i = 0; i != 16; ++i)
686 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
687
688 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
689 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
690 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
691 }
692
693 // If palignr is shifting the pair of input vectors more than 16 but less
694 // than 32 bytes, emit a logical right shift of the destination.
695 else if (shiftVal < 32) {
696 const Type *EltTy = Type::getInt64Ty(C);
697 const Type *VecTy = VectorType::get(EltTy, 2);
698 const Type *IntTy = Type::getInt32Ty(C);
699
700 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
701 Op2 = ConstantInt::get(IntTy, (shiftVal-16) * 8);
702
703 // create i32 constant
704 Function *I =
705 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_sse2_psrl_dq);
706 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
707 }
708
709 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
710 else {
711 Rep = Constant::getNullValue(F->getReturnType());
712 }
713
714 // Replace any uses with our new instruction.
715 if (!CI->use_empty())
716 CI->replaceAllUsesWith(Rep);
717
718 // Remove upgraded instruction.
719 CI->eraseFromParent();
720
Evan Chenga8288f42007-12-18 01:04:25 +0000721 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000722 llvm_unreachable("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000723 }
724 return;
725 }
726
Gabor Greife9ecc682008-04-06 20:25:17 +0000727 switch (NewFn->getIntrinsicID()) {
Bob Wilsonf65c9ef2010-09-03 01:35:08 +0000728 default: llvm_unreachable("Unknown function for CallInst upgrade.");
Bob Wilsonedf722a2010-08-27 17:13:24 +0000729 case Intrinsic::arm_neon_vld1:
730 case Intrinsic::arm_neon_vld2:
731 case Intrinsic::arm_neon_vld3:
732 case Intrinsic::arm_neon_vld4:
733 case Intrinsic::arm_neon_vst1:
734 case Intrinsic::arm_neon_vst2:
735 case Intrinsic::arm_neon_vst3:
736 case Intrinsic::arm_neon_vst4:
737 case Intrinsic::arm_neon_vld2lane:
738 case Intrinsic::arm_neon_vld3lane:
739 case Intrinsic::arm_neon_vld4lane:
740 case Intrinsic::arm_neon_vst2lane:
741 case Intrinsic::arm_neon_vst3lane:
742 case Intrinsic::arm_neon_vst4lane: {
743 // Add a default alignment argument of 1.
744 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
745 Operands.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
746 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
747 CI->getName(), CI);
748 NewCI->setTailCall(CI->isTailCall());
749 NewCI->setCallingConv(CI->getCallingConv());
750
751 // Handle any uses of the old CallInst.
752 if (!CI->use_empty())
753 // Replace all uses of the old call with the new cast which has the
754 // correct type.
755 CI->replaceAllUsesWith(NewCI);
756
757 // Clean up the old call now that it has been completely upgraded.
758 CI->eraseFromParent();
759 break;
760 }
761
Anders Carlssonf924f342007-12-14 06:38:54 +0000762 case Intrinsic::x86_mmx_psll_d:
763 case Intrinsic::x86_mmx_psll_q:
764 case Intrinsic::x86_mmx_psll_w:
765 case Intrinsic::x86_mmx_psra_d:
766 case Intrinsic::x86_mmx_psra_w:
767 case Intrinsic::x86_mmx_psrl_d:
768 case Intrinsic::x86_mmx_psrl_q:
769 case Intrinsic::x86_mmx_psrl_w: {
Chris Lattner8a923e72008-03-12 17:45:29 +0000770 Value *Operands[2];
Anders Carlssonf924f342007-12-14 06:38:54 +0000771
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000772 Operands[0] = CI->getArgOperand(0);
Anders Carlssonf924f342007-12-14 06:38:54 +0000773
774 // Cast the second parameter to the correct type.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000775 BitCastInst *BC = new BitCastInst(CI->getArgOperand(1),
Anders Carlssonf924f342007-12-14 06:38:54 +0000776 NewFn->getFunctionType()->getParamType(1),
Evan Cheng50659322008-05-24 00:08:39 +0000777 "upgraded.", CI);
Chris Lattner8a923e72008-03-12 17:45:29 +0000778 Operands[1] = BC;
Anders Carlssonf924f342007-12-14 06:38:54 +0000779
780 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000781 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
782 "upgraded."+CI->getName(), CI);
Anders Carlssonf924f342007-12-14 06:38:54 +0000783 NewCI->setTailCall(CI->isTailCall());
784 NewCI->setCallingConv(CI->getCallingConv());
785
786 // Handle any uses of the old CallInst.
787 if (!CI->use_empty())
788 // Replace all uses of the old call with the new cast which has the
789 // correct type.
790 CI->replaceAllUsesWith(NewCI);
791
792 // Clean up the old call now that it has been completely upgraded.
793 CI->eraseFromParent();
794 break;
795 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000796 case Intrinsic::ctlz:
797 case Intrinsic::ctpop:
Gabor Greife9ecc682008-04-06 20:25:17 +0000798 case Intrinsic::cttz: {
Gabor Greife5406532010-06-23 08:45:32 +0000799 // Build a small vector of the original arguments.
800 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Chandler Carruth7132e002007-08-04 01:51:18 +0000801
802 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000803 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
804 "upgraded."+CI->getName(), CI);
Chandler Carruth7132e002007-08-04 01:51:18 +0000805 NewCI->setTailCall(CI->isTailCall());
806 NewCI->setCallingConv(CI->getCallingConv());
807
808 // Handle any uses of the old CallInst.
809 if (!CI->use_empty()) {
810 // Check for sign extend parameter attributes on the return values.
Devang Patel4c758ea2008-09-25 21:00:45 +0000811 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
812 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
Chandler Carruth7132e002007-08-04 01:51:18 +0000813
814 // Construct an appropriate cast from the new return type to the old.
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000815 CastInst *RetCast = CastInst::Create(
Chandler Carruth7132e002007-08-04 01:51:18 +0000816 CastInst::getCastOpcode(NewCI, SrcSExt,
817 F->getReturnType(),
818 DestSExt),
819 NewCI, F->getReturnType(),
820 NewCI->getName(), CI);
821 NewCI->moveBefore(RetCast);
822
823 // Replace all uses of the old call with the new cast which has the
824 // correct type.
825 CI->replaceAllUsesWith(RetCast);
826 }
827
828 // Clean up the old call now that it has been completely upgraded.
829 CI->eraseFromParent();
Gabor Greife9ecc682008-04-06 20:25:17 +0000830 }
831 break;
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000832 case Intrinsic::eh_selector:
833 case Intrinsic::eh_typeid_for: {
834 // Only the return type changed.
Gabor Greife5406532010-06-23 08:45:32 +0000835 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000836 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
837 "upgraded." + CI->getName(), CI);
838 NewCI->setTailCall(CI->isTailCall());
839 NewCI->setCallingConv(CI->getCallingConv());
840
841 // Handle any uses of the old CallInst.
842 if (!CI->use_empty()) {
843 // Construct an appropriate cast from the new return type to the old.
844 CastInst *RetCast =
845 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
846 F->getReturnType(), true),
847 NewCI, F->getReturnType(), NewCI->getName(), CI);
848 CI->replaceAllUsesWith(RetCast);
849 }
850 CI->eraseFromParent();
851 }
852 break;
Mon P Wangc576ee92010-04-04 03:10:48 +0000853 case Intrinsic::memcpy:
854 case Intrinsic::memmove:
855 case Intrinsic::memset: {
856 // Add isVolatile
857 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(CI->getContext());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000858 Value *Operands[5] = { CI->getArgOperand(0), CI->getArgOperand(1),
859 CI->getArgOperand(2), CI->getArgOperand(3),
Mon P Wangc576ee92010-04-04 03:10:48 +0000860 llvm::ConstantInt::get(I1Ty, 0) };
861 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+5,
862 CI->getName(), CI);
863 NewCI->setTailCall(CI->isTailCall());
864 NewCI->setCallingConv(CI->getCallingConv());
865 // Handle any uses of the old CallInst.
866 if (!CI->use_empty())
867 // Replace all uses of the old call with the new cast which has the
868 // correct type.
869 CI->replaceAllUsesWith(NewCI);
870
871 // Clean up the old call now that it has been completely upgraded.
872 CI->eraseFromParent();
873 break;
874 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000875 }
876}
877
878// This tests each Function to determine if it needs upgrading. When we find
879// one we are interested in, we then upgrade all calls to reflect the new
880// function.
881void llvm::UpgradeCallsToIntrinsic(Function* F) {
882 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
883
884 // Upgrade the function and check if it is a totaly new function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000885 Function* NewFn;
886 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000887 if (NewFn != F) {
888 // Replace all uses to the old function with the new one if necessary.
889 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
890 UI != UE; ) {
891 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
892 UpgradeIntrinsicCall(CI, NewFn);
893 }
894 // Remove old function, no longer used, from the module.
895 F->eraseFromParent();
896 }
897 }
898}
Devang Patel80ae3492009-08-28 23:24:31 +0000899
Victor Hernandezc2044a12010-01-05 21:13:46 +0000900/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
901/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
902/// strips that use.
Devang Patel80ae3492009-08-28 23:24:31 +0000903void llvm::CheckDebugInfoIntrinsics(Module *M) {
904
905
906 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000907 while (!FuncStart->use_empty()) {
908 CallInst *CI = cast<CallInst>(FuncStart->use_back());
909 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000910 }
Devang Patelbe94f232010-01-05 01:10:40 +0000911 FuncStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000912 }
Devang Patelbe94f232010-01-05 01:10:40 +0000913
Devang Patel80ae3492009-08-28 23:24:31 +0000914 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000915 while (!StopPoint->use_empty()) {
916 CallInst *CI = cast<CallInst>(StopPoint->use_back());
917 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000918 }
Devang Patelbe94f232010-01-05 01:10:40 +0000919 StopPoint->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000920 }
921
922 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000923 while (!RegionStart->use_empty()) {
924 CallInst *CI = cast<CallInst>(RegionStart->use_back());
925 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000926 }
Devang Patelbe94f232010-01-05 01:10:40 +0000927 RegionStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000928 }
929
930 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000931 while (!RegionEnd->use_empty()) {
932 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
933 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000934 }
Devang Patelbe94f232010-01-05 01:10:40 +0000935 RegionEnd->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000936 }
937
938 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
939 if (!Declare->use_empty()) {
940 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000941 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
942 !isa<MDNode>(DDI->getArgOperand(1))) {
Devang Patel80ae3492009-08-28 23:24:31 +0000943 while (!Declare->use_empty()) {
944 CallInst *CI = cast<CallInst>(Declare->use_back());
945 CI->eraseFromParent();
946 }
947 Declare->eraseFromParent();
948 }
949 }
950 }
951}