blob: b323540356443b61680c52fec5d7b0fc81835ae4 [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
Dale Johannesendd224d22010-09-30 23:57:10 +0000291 // x86_mmx instead of a v1i64, v2i32, v4i16, or v8i8.
292 if (Name.compare(5, 8, "x86.mmx.", 8) == 0) {
293 const Type *X86_MMXTy = VectorType::getX86_MMXTy(FTy->getContext());
Anders Carlssonf924f342007-12-14 06:38:54 +0000294
Dale Johannesendd224d22010-09-30 23:57:10 +0000295 if (Name.compare(13, 4, "padd", 4) == 0 ||
296 Name.compare(13, 4, "psub", 4) == 0 ||
297 Name.compare(13, 4, "pmul", 4) == 0 ||
298 Name.compare(13, 5, "pmadd", 5) == 0 ||
299 Name.compare(13, 4, "pand", 4) == 0 ||
300 Name.compare(13, 3, "por", 3) == 0 ||
301 Name.compare(13, 4, "pxor", 4) == 0 ||
302 Name.compare(13, 4, "pavg", 4) == 0 ||
303 Name.compare(13, 4, "pmax", 4) == 0 ||
304 Name.compare(13, 4, "pmin", 4) == 0 ||
305 Name.compare(13, 4, "psad", 4) == 0 ||
306 Name.compare(13, 4, "psll", 4) == 0 ||
307 Name.compare(13, 4, "psrl", 4) == 0 ||
308 Name.compare(13, 4, "psra", 4) == 0 ||
309 Name.compare(13, 4, "pack", 4) == 0 ||
310 Name.compare(13, 6, "punpck", 6) == 0 ||
311 Name.compare(13, 4, "pcmp", 4) == 0) {
312 assert(FTy->getNumParams() == 2 && "MMX intrinsic takes 2 args!");
313 const Type *SecondParamTy = X86_MMXTy;
314
315 if (Name.compare(13, 5, "pslli", 5) == 0 ||
316 Name.compare(13, 5, "psrli", 5) == 0 ||
317 Name.compare(13, 5, "psrai", 5) == 0)
318 SecondParamTy = FTy->getParamType(1);
319
320 // Don't do anything if it has the correct types.
321 if (FTy->getReturnType() == X86_MMXTy &&
322 FTy->getParamType(0) == X86_MMXTy &&
323 FTy->getParamType(1) == SecondParamTy)
324 break;
325
326 // We first need to change the name of the old (bad) intrinsic, because
327 // its type is incorrect, but we cannot overload that name. We
328 // arbitrarily unique it here allowing us to construct a correctly named
329 // and typed function below.
330 F->setName("");
331
332 // Now construct the new intrinsic with the correct name and type. We
333 // leave the old function around in order to query its type, whatever it
334 // may be, and correctly convert up to the new type.
335 NewFn = cast<Function>(M->getOrInsertFunction(Name,
336 X86_MMXTy, X86_MMXTy,
337 SecondParamTy, (Type*)0));
338 return true;
339 }
340
341 if (Name.compare(13, 8, "maskmovq", 8) == 0) {
342 // Don't do anything if it has the correct types.
343 if (FTy->getParamType(0) == X86_MMXTy &&
344 FTy->getParamType(1) == X86_MMXTy)
345 break;
346
347 F->setName("");
348 NewFn = cast<Function>(M->getOrInsertFunction(Name,
349 FTy->getReturnType(),
350 X86_MMXTy,
351 X86_MMXTy,
352 FTy->getParamType(2),
353 (Type*)0));
354 return true;
355 }
356
357 if (Name.compare(13, 8, "pmovmskb", 8) == 0) {
358 if (FTy->getParamType(0) == X86_MMXTy)
359 break;
360
361 F->setName("");
362 NewFn = cast<Function>(M->getOrInsertFunction(Name,
363 FTy->getReturnType(),
364 X86_MMXTy,
365 (Type*)0));
366 return true;
367 }
368
369 if (Name.compare(13, 5, "movnt", 5) == 0) {
370 if (FTy->getParamType(1) == X86_MMXTy)
371 break;
372
373 F->setName("");
374 NewFn = cast<Function>(M->getOrInsertFunction(Name,
375 FTy->getReturnType(),
376 FTy->getParamType(0),
377 X86_MMXTy,
378 (Type*)0));
379 return true;
380 }
381
382 if (Name.compare(13, 7, "palignr", 7) == 0) {
383 if (FTy->getReturnType() == X86_MMXTy &&
384 FTy->getParamType(0) == X86_MMXTy &&
385 FTy->getParamType(1) == X86_MMXTy)
386 break;
387
388 F->setName("");
389 NewFn = cast<Function>(M->getOrInsertFunction(Name,
390 X86_MMXTy,
391 X86_MMXTy,
392 X86_MMXTy,
393 FTy->getParamType(2),
394 (Type*)0));
395 return true;
396 }
397
398 if (Name.compare(13, 5, "pextr", 5) == 0) {
399 if (FTy->getParamType(0) == X86_MMXTy)
400 break;
401
402 F->setName("");
403 NewFn = cast<Function>(M->getOrInsertFunction(Name,
404 FTy->getReturnType(),
405 X86_MMXTy,
406 FTy->getParamType(1),
407 (Type*)0));
408 return true;
409 }
410
411 if (Name.compare(13, 5, "pinsr", 5) == 0) {
412 if (FTy->getReturnType() == X86_MMXTy &&
413 FTy->getParamType(0) == X86_MMXTy)
414 break;
415
416 F->setName("");
417 NewFn = cast<Function>(M->getOrInsertFunction(Name,
418 X86_MMXTy,
419 X86_MMXTy,
420 FTy->getParamType(1),
421 FTy->getParamType(2),
422 (Type*)0));
423 return true;
424 }
425
426 if (Name.compare(13, 12, "cvtsi32.si64", 12) == 0) {
427 if (FTy->getReturnType() == X86_MMXTy)
428 break;
429
430 F->setName("");
431 NewFn = cast<Function>(M->getOrInsertFunction(Name,
432 X86_MMXTy,
433 FTy->getParamType(0),
434 (Type*)0));
435 return true;
436 }
437
438 if (Name.compare(13, 12, "cvtsi64.si32", 12) == 0) {
439 if (FTy->getParamType(0) == X86_MMXTy)
440 break;
441
442 F->setName("");
443 NewFn = cast<Function>(M->getOrInsertFunction(Name,
444 FTy->getReturnType(),
445 X86_MMXTy,
446 (Type*)0));
447 return true;
448 }
449
450 if (Name.compare(13, 8, "vec.init", 8) == 0) {
451 if (FTy->getReturnType() == X86_MMXTy)
452 break;
453
454 F->setName("");
455
456 if (Name.compare(21, 2, ".b", 2) == 0)
457 NewFn = cast<Function>(M->getOrInsertFunction(Name,
458 X86_MMXTy,
459 FTy->getParamType(0),
460 FTy->getParamType(1),
461 FTy->getParamType(2),
462 FTy->getParamType(3),
463 FTy->getParamType(4),
464 FTy->getParamType(5),
465 FTy->getParamType(6),
466 FTy->getParamType(7),
467 (Type*)0));
468 else if (Name.compare(21, 2, ".w", 2) == 0)
469 NewFn = cast<Function>(M->getOrInsertFunction(Name,
470 X86_MMXTy,
471 FTy->getParamType(0),
472 FTy->getParamType(1),
473 FTy->getParamType(2),
474 FTy->getParamType(3),
475 (Type*)0));
476 else if (Name.compare(21, 2, ".d", 2) == 0)
477 NewFn = cast<Function>(M->getOrInsertFunction(Name,
478 X86_MMXTy,
479 FTy->getParamType(0),
480 FTy->getParamType(1),
481 (Type*)0));
482 return true;
483 }
484
485
486 if (Name.compare(13, 9, "vec.ext.d", 9) == 0) {
487 if (FTy->getReturnType() == X86_MMXTy &&
488 FTy->getParamType(0) == X86_MMXTy)
489 break;
490
491 F->setName("");
492 NewFn = cast<Function>(M->getOrInsertFunction(Name,
493 X86_MMXTy,
494 X86_MMXTy,
495 FTy->getParamType(1),
496 (Type*)0));
497 return true;
498 }
499
500 if (Name.compare(13, 9, "emms", 4) == 0 ||
501 Name.compare(13, 9, "femms", 5) == 0) {
502 NewFn = 0;
503 break;
504 }
505
506 // We really shouldn't get here ever.
507 assert(0 && "Invalid MMX intrinsic!");
508 break;
Evan Cheng50659322008-05-24 00:08:39 +0000509 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
510 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
Evan Cheng2146270c2008-05-24 02:14:05 +0000511 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
512 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
513 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
514 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000515 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
516 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
517 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
Evan Cheng50659322008-05-24 00:08:39 +0000518 // Calls to these intrinsics are transformed into ShuffleVector's.
Evan Cheng0e179d02007-12-17 22:33:23 +0000519 NewFn = 0;
520 return true;
Eric Christopher6ad81672010-03-30 18:49:01 +0000521 } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) {
522 // Calls to these intrinsics are transformed into vector multiplies.
523 NewFn = 0;
524 return true;
Eric Christopher64831c62010-04-20 00:59:54 +0000525 } else if (Name.compare(5, 18, "x86.ssse3.palign.r", 18) == 0 ||
526 Name.compare(5, 22, "x86.ssse3.palign.r.128", 22) == 0) {
527 // Calls to these intrinsics are transformed into vector shuffles, shifts,
528 // or 0.
529 NewFn = 0;
530 return true;
Bill Wendling402e5482010-10-04 20:24:01 +0000531 } else if (Name.compare(5, 17, "x86.ssse3.pshuf.w", 17) == 0) {
532 // This is an SSE/MMX instruction.
533 const Type *X86_MMXTy = VectorType::getX86_MMXTy(FTy->getContext());
534 NewFn =
535 cast<Function>(M->getOrInsertFunction("llvm.x86.sse.pshuf.w",
536 X86_MMXTy,
537 X86_MMXTy,
538 Type::getInt8Ty(F->getContext()),
539 (Type*)0));
540 return true;
Anders Carlssonf924f342007-12-14 06:38:54 +0000541 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000542
Anders Carlssonf924f342007-12-14 06:38:54 +0000543 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000544 }
545
546 // This may not belong here. This function is effectively being overloaded
547 // to both detect an intrinsic which needs upgrading, and to provide the
548 // upgraded form of the intrinsic. We should perhaps have two separate
549 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000550 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000551}
552
Evan Cheng0e179d02007-12-17 22:33:23 +0000553bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
554 NewFn = 0;
555 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000556
557 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000558 if (NewFn)
559 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000560 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000561 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000562 return Upgraded;
563}
564
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000565bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Bill Wendling55165fe2010-09-10 20:42:26 +0000566 StringRef Name(GV->getName());
Bill Wendling6a57e242010-09-10 19:06:58 +0000567
568 // We are only upgrading one symbol here.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000569 if (Name == ".llvm.eh.catch.all.value") {
570 GV->setName("llvm.eh.catch.all.value");
571 return true;
572 }
573
574 return false;
575}
576
Bob Wilson38ab35a2010-09-01 23:50:19 +0000577/// ExtendNEONArgs - For NEON "long" and "wide" operations, where the results
578/// have vector elements twice as big as one or both source operands, do the
579/// sign- or zero-extension that used to be handled by intrinsics. The
580/// extended values are returned via V0 and V1.
581static void ExtendNEONArgs(CallInst *CI, Value *Arg0, Value *Arg1,
582 Value *&V0, Value *&V1) {
583 Function *F = CI->getCalledFunction();
584 const std::string& Name = F->getName();
585 bool isLong = (Name.at(18) == 'l');
586 bool isSigned = (Name.at(19) == 's');
587
588 if (isSigned) {
589 if (isLong)
590 V0 = new SExtInst(Arg0, CI->getType(), "", CI);
591 else
592 V0 = Arg0;
593 V1 = new SExtInst(Arg1, CI->getType(), "", CI);
594 } else {
595 if (isLong)
596 V0 = new ZExtInst(Arg0, CI->getType(), "", CI);
597 else
598 V0 = Arg0;
599 V1 = new ZExtInst(Arg1, CI->getType(), "", CI);
600 }
601}
602
Bob Wilsonf65c9ef2010-09-03 01:35:08 +0000603/// CallVABD - As part of expanding a call to one of the old NEON vabdl, vaba,
604/// or vabal intrinsics, construct a call to a vabd intrinsic. Examine the
605/// name of the old intrinsic to determine whether to use a signed or unsigned
606/// vabd intrinsic. Get the type from the old call instruction, adjusted for
607/// half-size vector elements if the old intrinsic was vabdl or vabal.
608static Instruction *CallVABD(CallInst *CI, Value *Arg0, Value *Arg1) {
609 Function *F = CI->getCalledFunction();
610 const std::string& Name = F->getName();
611 bool isLong = (Name.at(18) == 'l');
612 bool isSigned = (Name.at(isLong ? 19 : 18) == 's');
613
614 Intrinsic::ID intID;
615 if (isSigned)
616 intID = Intrinsic::arm_neon_vabds;
617 else
618 intID = Intrinsic::arm_neon_vabdu;
619
620 const Type *Ty = CI->getType();
621 if (isLong)
622 Ty = VectorType::getTruncatedElementVectorType(cast<const VectorType>(Ty));
623
624 Function *VABD = Intrinsic::getDeclaration(F->getParent(), intID, &Ty, 1);
625 Value *Operands[2];
626 Operands[0] = Arg0;
627 Operands[1] = Arg1;
628 return CallInst::Create(VABD, Operands, Operands+2,
629 "upgraded."+CI->getName(), CI);
630}
631
Dale Johannesendd224d22010-09-30 23:57:10 +0000632/// ConstructNewCallInst - Construct a new CallInst with the signature of NewFn.
633static void ConstructNewCallInst(Function *NewFn, CallInst *OldCI,
634 Value **Operands, unsigned NumOps,
635 bool AssignName = true) {
636 // Construct a new CallInst.
637 CallInst *NewCI =
638 CallInst::Create(NewFn, Operands, Operands + NumOps,
639 AssignName ? "upgraded." + OldCI->getName() : "", OldCI);
640
641 NewCI->setTailCall(OldCI->isTailCall());
642 NewCI->setCallingConv(OldCI->getCallingConv());
643
Bill Wendling402e5482010-10-04 20:24:01 +0000644 // Handle any uses of the old CallInst. If the type has changed, add a cast.
Dale Johannesendd224d22010-09-30 23:57:10 +0000645 if (!OldCI->use_empty()) {
Dale Johannesendd224d22010-09-30 23:57:10 +0000646 if (OldCI->getType() != NewCI->getType()) {
647 Function *OldFn = OldCI->getCalledFunction();
648 CastInst *RetCast =
649 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
650 OldFn->getReturnType(), true),
651 NewCI, OldFn->getReturnType(), NewCI->getName(),OldCI);
Bill Wendling402e5482010-10-04 20:24:01 +0000652
653 // Replace all uses of the old call with the new cast which has the
654 // correct type.
655 OldCI->replaceAllUsesWith(RetCast);
656 } else {
657 OldCI->replaceAllUsesWith(NewCI);
Dale Johannesendd224d22010-09-30 23:57:10 +0000658 }
Dale Johannesendd224d22010-09-30 23:57:10 +0000659 }
Bill Wendling402e5482010-10-04 20:24:01 +0000660
Dale Johannesendd224d22010-09-30 23:57:10 +0000661 // Clean up the old call now that it has been completely upgraded.
662 OldCI->eraseFromParent();
663}
664
Chandler Carruth7132e002007-08-04 01:51:18 +0000665// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
666// upgraded intrinsic. All argument and return casting must be provided in
667// order to seamlessly integrate with existing context.
668void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000669 Function *F = CI->getCalledFunction();
Owen Anderson55f1c092009-08-13 21:58:54 +0000670 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +0000671 ImmutableCallSite CS(CI);
672
Chandler Carruth7132e002007-08-04 01:51:18 +0000673 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000674
675 if (!NewFn) {
Bob Wilson9a511c02010-08-20 04:54:02 +0000676 // Get the Function's name.
677 const std::string& Name = F->getName();
678
679 // Upgrade ARM NEON intrinsics.
680 if (Name.compare(5, 9, "arm.neon.", 9) == 0) {
681 Instruction *NewI;
Bob Wilson38ab35a2010-09-01 23:50:19 +0000682 Value *V0, *V1;
Bob Wilson9a511c02010-08-20 04:54:02 +0000683 if (Name.compare(14, 7, "vmovls.", 7) == 0) {
684 NewI = new SExtInst(CI->getArgOperand(0), CI->getType(),
685 "upgraded." + CI->getName(), CI);
686 } else if (Name.compare(14, 7, "vmovlu.", 7) == 0) {
687 NewI = new ZExtInst(CI->getArgOperand(0), CI->getType(),
688 "upgraded." + CI->getName(), CI);
Bob Wilson38ab35a2010-09-01 23:50:19 +0000689 } else if (Name.compare(14, 4, "vadd", 4) == 0) {
690 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
691 NewI = BinaryOperator::CreateAdd(V0, V1, "upgraded."+CI->getName(), CI);
692 } else if (Name.compare(14, 4, "vsub", 4) == 0) {
693 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
694 NewI = BinaryOperator::CreateSub(V0, V1,"upgraded."+CI->getName(),CI);
695 } else if (Name.compare(14, 4, "vmul", 4) == 0) {
696 ExtendNEONArgs(CI, CI->getArgOperand(0), CI->getArgOperand(1), V0, V1);
697 NewI = BinaryOperator::CreateMul(V0, V1,"upgraded."+CI->getName(),CI);
698 } else if (Name.compare(14, 4, "vmla", 4) == 0) {
699 ExtendNEONArgs(CI, CI->getArgOperand(1), CI->getArgOperand(2), V0, V1);
700 Instruction *MulI = BinaryOperator::CreateMul(V0, V1, "", CI);
701 NewI = BinaryOperator::CreateAdd(CI->getArgOperand(0), MulI,
702 "upgraded."+CI->getName(), CI);
703 } else if (Name.compare(14, 4, "vmls", 4) == 0) {
704 ExtendNEONArgs(CI, CI->getArgOperand(1), CI->getArgOperand(2), V0, V1);
705 Instruction *MulI = BinaryOperator::CreateMul(V0, V1, "", CI);
706 NewI = BinaryOperator::CreateSub(CI->getArgOperand(0), MulI,
707 "upgraded."+CI->getName(), CI);
Bob Wilsonf65c9ef2010-09-03 01:35:08 +0000708 } else if (Name.compare(14, 4, "vabd", 4) == 0) {
709 NewI = CallVABD(CI, CI->getArgOperand(0), CI->getArgOperand(1));
710 NewI = new ZExtInst(NewI, CI->getType(), "upgraded."+CI->getName(), CI);
711 } else if (Name.compare(14, 4, "vaba", 4) == 0) {
712 NewI = CallVABD(CI, CI->getArgOperand(1), CI->getArgOperand(2));
713 if (Name.at(18) == 'l')
714 NewI = new ZExtInst(NewI, CI->getType(), "", CI);
715 NewI = BinaryOperator::CreateAdd(CI->getArgOperand(0), NewI,
716 "upgraded."+CI->getName(), CI);
Bob Wilson4cd8a122010-08-30 20:02:30 +0000717 } else if (Name.compare(14, 6, "vmovn.", 6) == 0) {
718 NewI = new TruncInst(CI->getArgOperand(0), CI->getType(),
719 "upgraded." + CI->getName(), CI);
Bob Wilson9a511c02010-08-20 04:54:02 +0000720 } else {
721 llvm_unreachable("Unknown arm.neon function for CallInst upgrade.");
722 }
723 // Replace any uses of the old CallInst.
724 if (!CI->use_empty())
725 CI->replaceAllUsesWith(NewI);
726 CI->eraseFromParent();
727 return;
728 }
729
Evan Cheng50659322008-05-24 00:08:39 +0000730 bool isLoadH = false, isLoadL = false, isMovL = false;
Evan Cheng2146270c2008-05-24 02:14:05 +0000731 bool isMovSD = false, isShufPD = false;
732 bool isUnpckhPD = false, isUnpcklPD = false;
Evan Cheng91a2e562008-05-24 02:56:30 +0000733 bool isPunpckhQPD = false, isPunpcklQPD = false;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000734 if (F->getName() == "llvm.x86.sse2.loadh.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000735 isLoadH = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000736 else if (F->getName() == "llvm.x86.sse2.loadl.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000737 isLoadL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000738 else if (F->getName() == "llvm.x86.sse2.movl.dq")
Evan Cheng50659322008-05-24 00:08:39 +0000739 isMovL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000740 else if (F->getName() == "llvm.x86.sse2.movs.d")
Evan Cheng2146270c2008-05-24 02:14:05 +0000741 isMovSD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000742 else if (F->getName() == "llvm.x86.sse2.shuf.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000743 isShufPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000744 else if (F->getName() == "llvm.x86.sse2.unpckh.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000745 isUnpckhPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000746 else if (F->getName() == "llvm.x86.sse2.unpckl.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000747 isUnpcklPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000748 else if (F->getName() == "llvm.x86.sse2.punpckh.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000749 isPunpckhQPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000750 else if (F->getName() == "llvm.x86.sse2.punpckl.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000751 isPunpcklQPD = true;
Evan Cheng0e179d02007-12-17 22:33:23 +0000752
Evan Cheng2146270c2008-05-24 02:14:05 +0000753 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000754 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Evan Cheng50659322008-05-24 00:08:39 +0000755 std::vector<Constant*> Idxs;
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000756 Value *Op0 = CI->getArgOperand(0);
Evan Cheng2146270c2008-05-24 02:14:05 +0000757 ShuffleVectorInst *SI = NULL;
Evan Cheng50659322008-05-24 00:08:39 +0000758 if (isLoadH || isLoadL) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000759 Value *Op1 = UndefValue::get(Op0->getType());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000760 Value *Addr = new BitCastInst(CI->getArgOperand(1),
Duncan Sands9ed7b162009-10-06 15:40:36 +0000761 Type::getDoublePtrTy(C),
Evan Cheng50659322008-05-24 00:08:39 +0000762 "upgraded.", CI);
763 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
Owen Anderson55f1c092009-08-13 21:58:54 +0000764 Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000765 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
766
767 if (isLoadH) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000768 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
769 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng50659322008-05-24 00:08:39 +0000770 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000771 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
772 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng50659322008-05-24 00:08:39 +0000773 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000774 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000775 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng2146270c2008-05-24 02:14:05 +0000776 } else if (isMovL) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000777 Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000778 Idxs.push_back(Zero);
779 Idxs.push_back(Zero);
780 Idxs.push_back(Zero);
781 Idxs.push_back(Zero);
Owen Anderson4aa32952009-07-28 21:19:26 +0000782 Value *ZeroV = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000783
784 Idxs.clear();
Owen Anderson55f1c092009-08-13 21:58:54 +0000785 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4));
786 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5));
787 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
788 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Owen Anderson4aa32952009-07-28 21:19:26 +0000789 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000790 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
Evan Cheng91a2e562008-05-24 02:56:30 +0000791 } else if (isMovSD ||
792 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000793 Value *Op1 = CI->getArgOperand(1);
Evan Cheng2146270c2008-05-24 02:14:05 +0000794 if (isMovSD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000795 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
796 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng91a2e562008-05-24 02:56:30 +0000797 } else if (isUnpckhPD || isPunpckhQPD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000798 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
799 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Evan Cheng2146270c2008-05-24 02:14:05 +0000800 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000801 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
802 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng2146270c2008-05-24 02:14:05 +0000803 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000804 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000805 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
806 } else if (isShufPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000807 Value *Op1 = CI->getArgOperand(1);
Gabor Greif3e44ea12010-07-22 10:37:47 +0000808 unsigned MaskVal =
809 cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
Owen Anderson55f1c092009-08-13 21:58:54 +0000810 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
811 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
Owen Anderson155dccd82009-07-07 23:43:39 +0000812 ((MaskVal >> 1) & 1)+2));
Owen Anderson4aa32952009-07-28 21:19:26 +0000813 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000814 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng50659322008-05-24 00:08:39 +0000815 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000816
Evan Cheng2146270c2008-05-24 02:14:05 +0000817 assert(SI && "Unexpected!");
818
Evan Cheng0e179d02007-12-17 22:33:23 +0000819 // Handle any uses of the old CallInst.
820 if (!CI->use_empty())
821 // Replace all uses of the old call with the new cast which has the
822 // correct type.
823 CI->replaceAllUsesWith(SI);
824
825 // Clean up the old call now that it has been completely upgraded.
826 CI->eraseFromParent();
Eric Christopher6ad81672010-03-30 18:49:01 +0000827 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
828 // Upgrade this set of intrinsics into vector multiplies.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000829 Instruction *Mul = BinaryOperator::CreateMul(CI->getArgOperand(0),
830 CI->getArgOperand(1),
Eric Christopher6ad81672010-03-30 18:49:01 +0000831 CI->getName(),
832 CI);
833 // Fix up all the uses with our new multiply.
834 if (!CI->use_empty())
835 CI->replaceAllUsesWith(Mul);
836
837 // Remove upgraded multiply.
838 CI->eraseFromParent();
Eric Christopher64831c62010-04-20 00:59:54 +0000839 } else if (F->getName() == "llvm.x86.ssse3.palign.r") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000840 Value *Op1 = CI->getArgOperand(0);
841 Value *Op2 = CI->getArgOperand(1);
842 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000843 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
844 Value *Rep;
845 IRBuilder<> Builder(C);
846 Builder.SetInsertPoint(CI->getParent(), CI);
847
848 // If palignr is shifting the pair of input vectors less than 9 bytes,
849 // emit a shuffle instruction.
850 if (shiftVal <= 8) {
851 const Type *IntTy = Type::getInt32Ty(C);
852 const Type *EltTy = Type::getInt8Ty(C);
853 const Type *VecTy = VectorType::get(EltTy, 8);
854
855 Op2 = Builder.CreateBitCast(Op2, VecTy);
856 Op1 = Builder.CreateBitCast(Op1, VecTy);
857
858 llvm::SmallVector<llvm::Constant*, 8> Indices;
859 for (unsigned i = 0; i != 8; ++i)
860 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
861
Chris Lattner69229312011-02-15 00:14:00 +0000862 Value *SV = ConstantVector::get(Indices);
Eric Christopher64831c62010-04-20 00:59:54 +0000863 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
864 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
865 }
866
867 // If palignr is shifting the pair of input vectors more than 8 but less
868 // than 16 bytes, emit a logical right shift of the destination.
869 else if (shiftVal < 16) {
870 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
871 const Type *EltTy = Type::getInt64Ty(C);
872 const Type *VecTy = VectorType::get(EltTy, 1);
873
874 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
875 Op2 = ConstantInt::get(VecTy, (shiftVal-8) * 8);
876
877 // create i32 constant
878 Function *I =
879 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_mmx_psrl_q);
880 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
881 }
882
883 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
884 else {
885 Rep = Constant::getNullValue(F->getReturnType());
886 }
887
888 // Replace any uses with our new instruction.
889 if (!CI->use_empty())
890 CI->replaceAllUsesWith(Rep);
891
892 // Remove upgraded instruction.
893 CI->eraseFromParent();
894
895 } else if (F->getName() == "llvm.x86.ssse3.palign.r.128") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000896 Value *Op1 = CI->getArgOperand(0);
897 Value *Op2 = CI->getArgOperand(1);
898 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000899 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
900 Value *Rep;
901 IRBuilder<> Builder(C);
902 Builder.SetInsertPoint(CI->getParent(), CI);
903
904 // If palignr is shifting the pair of input vectors less than 17 bytes,
905 // emit a shuffle instruction.
906 if (shiftVal <= 16) {
907 const Type *IntTy = Type::getInt32Ty(C);
908 const Type *EltTy = Type::getInt8Ty(C);
909 const Type *VecTy = VectorType::get(EltTy, 16);
910
911 Op2 = Builder.CreateBitCast(Op2, VecTy);
912 Op1 = Builder.CreateBitCast(Op1, VecTy);
913
914 llvm::SmallVector<llvm::Constant*, 16> Indices;
915 for (unsigned i = 0; i != 16; ++i)
916 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
917
Chris Lattner69229312011-02-15 00:14:00 +0000918 Value *SV = ConstantVector::get(Indices);
Eric Christopher64831c62010-04-20 00:59:54 +0000919 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
920 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
921 }
922
923 // If palignr is shifting the pair of input vectors more than 16 but less
924 // than 32 bytes, emit a logical right shift of the destination.
925 else if (shiftVal < 32) {
926 const Type *EltTy = Type::getInt64Ty(C);
927 const Type *VecTy = VectorType::get(EltTy, 2);
928 const Type *IntTy = Type::getInt32Ty(C);
929
930 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
931 Op2 = ConstantInt::get(IntTy, (shiftVal-16) * 8);
932
933 // create i32 constant
934 Function *I =
935 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_sse2_psrl_dq);
936 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
937 }
938
939 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
940 else {
941 Rep = Constant::getNullValue(F->getReturnType());
942 }
943
944 // Replace any uses with our new instruction.
945 if (!CI->use_empty())
946 CI->replaceAllUsesWith(Rep);
947
948 // Remove upgraded instruction.
949 CI->eraseFromParent();
950
Evan Chenga8288f42007-12-18 01:04:25 +0000951 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000952 llvm_unreachable("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000953 }
954 return;
955 }
956
Gabor Greife9ecc682008-04-06 20:25:17 +0000957 switch (NewFn->getIntrinsicID()) {
Bob Wilsonf65c9ef2010-09-03 01:35:08 +0000958 default: llvm_unreachable("Unknown function for CallInst upgrade.");
Bob Wilsonedf722a2010-08-27 17:13:24 +0000959 case Intrinsic::arm_neon_vld1:
960 case Intrinsic::arm_neon_vld2:
961 case Intrinsic::arm_neon_vld3:
962 case Intrinsic::arm_neon_vld4:
963 case Intrinsic::arm_neon_vst1:
964 case Intrinsic::arm_neon_vst2:
965 case Intrinsic::arm_neon_vst3:
966 case Intrinsic::arm_neon_vst4:
967 case Intrinsic::arm_neon_vld2lane:
968 case Intrinsic::arm_neon_vld3lane:
969 case Intrinsic::arm_neon_vld4lane:
970 case Intrinsic::arm_neon_vst2lane:
971 case Intrinsic::arm_neon_vst3lane:
972 case Intrinsic::arm_neon_vst4lane: {
973 // Add a default alignment argument of 1.
974 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
975 Operands.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
976 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
977 CI->getName(), CI);
978 NewCI->setTailCall(CI->isTailCall());
979 NewCI->setCallingConv(CI->getCallingConv());
980
981 // Handle any uses of the old CallInst.
982 if (!CI->use_empty())
983 // Replace all uses of the old call with the new cast which has the
984 // correct type.
985 CI->replaceAllUsesWith(NewCI);
986
987 // Clean up the old call now that it has been completely upgraded.
988 CI->eraseFromParent();
989 break;
990 }
991
Dale Johannesendd224d22010-09-30 23:57:10 +0000992 case Intrinsic::x86_mmx_padd_b:
993 case Intrinsic::x86_mmx_padd_w:
994 case Intrinsic::x86_mmx_padd_d:
995 case Intrinsic::x86_mmx_padd_q:
996 case Intrinsic::x86_mmx_padds_b:
997 case Intrinsic::x86_mmx_padds_w:
998 case Intrinsic::x86_mmx_paddus_b:
999 case Intrinsic::x86_mmx_paddus_w:
1000 case Intrinsic::x86_mmx_psub_b:
1001 case Intrinsic::x86_mmx_psub_w:
1002 case Intrinsic::x86_mmx_psub_d:
1003 case Intrinsic::x86_mmx_psub_q:
1004 case Intrinsic::x86_mmx_psubs_b:
1005 case Intrinsic::x86_mmx_psubs_w:
1006 case Intrinsic::x86_mmx_psubus_b:
1007 case Intrinsic::x86_mmx_psubus_w:
1008 case Intrinsic::x86_mmx_pmulh_w:
1009 case Intrinsic::x86_mmx_pmull_w:
1010 case Intrinsic::x86_mmx_pmulhu_w:
1011 case Intrinsic::x86_mmx_pmulu_dq:
1012 case Intrinsic::x86_mmx_pmadd_wd:
1013 case Intrinsic::x86_mmx_pand:
1014 case Intrinsic::x86_mmx_pandn:
1015 case Intrinsic::x86_mmx_por:
1016 case Intrinsic::x86_mmx_pxor:
1017 case Intrinsic::x86_mmx_pavg_b:
1018 case Intrinsic::x86_mmx_pavg_w:
1019 case Intrinsic::x86_mmx_pmaxu_b:
1020 case Intrinsic::x86_mmx_pmaxs_w:
1021 case Intrinsic::x86_mmx_pminu_b:
1022 case Intrinsic::x86_mmx_pmins_w:
1023 case Intrinsic::x86_mmx_psad_bw:
1024 case Intrinsic::x86_mmx_psll_w:
Anders Carlssonf924f342007-12-14 06:38:54 +00001025 case Intrinsic::x86_mmx_psll_d:
1026 case Intrinsic::x86_mmx_psll_q:
Dale Johannesendd224d22010-09-30 23:57:10 +00001027 case Intrinsic::x86_mmx_pslli_w:
1028 case Intrinsic::x86_mmx_pslli_d:
1029 case Intrinsic::x86_mmx_pslli_q:
1030 case Intrinsic::x86_mmx_psrl_w:
Anders Carlssonf924f342007-12-14 06:38:54 +00001031 case Intrinsic::x86_mmx_psrl_d:
1032 case Intrinsic::x86_mmx_psrl_q:
Dale Johannesendd224d22010-09-30 23:57:10 +00001033 case Intrinsic::x86_mmx_psrli_w:
1034 case Intrinsic::x86_mmx_psrli_d:
1035 case Intrinsic::x86_mmx_psrli_q:
1036 case Intrinsic::x86_mmx_psra_w:
1037 case Intrinsic::x86_mmx_psra_d:
1038 case Intrinsic::x86_mmx_psrai_w:
1039 case Intrinsic::x86_mmx_psrai_d:
1040 case Intrinsic::x86_mmx_packsswb:
1041 case Intrinsic::x86_mmx_packssdw:
1042 case Intrinsic::x86_mmx_packuswb:
1043 case Intrinsic::x86_mmx_punpckhbw:
1044 case Intrinsic::x86_mmx_punpckhwd:
1045 case Intrinsic::x86_mmx_punpckhdq:
1046 case Intrinsic::x86_mmx_punpcklbw:
1047 case Intrinsic::x86_mmx_punpcklwd:
1048 case Intrinsic::x86_mmx_punpckldq:
1049 case Intrinsic::x86_mmx_pcmpeq_b:
1050 case Intrinsic::x86_mmx_pcmpeq_w:
1051 case Intrinsic::x86_mmx_pcmpeq_d:
1052 case Intrinsic::x86_mmx_pcmpgt_b:
1053 case Intrinsic::x86_mmx_pcmpgt_w:
1054 case Intrinsic::x86_mmx_pcmpgt_d: {
Chris Lattner8a923e72008-03-12 17:45:29 +00001055 Value *Operands[2];
Anders Carlssonf924f342007-12-14 06:38:54 +00001056
Dale Johannesendd224d22010-09-30 23:57:10 +00001057 // Cast the operand to the X86 MMX type.
1058 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1059 NewFn->getFunctionType()->getParamType(0),
1060 "upgraded.", CI);
1061
1062 switch (NewFn->getIntrinsicID()) {
1063 default:
1064 // Cast to the X86 MMX type.
1065 Operands[1] = new BitCastInst(CI->getArgOperand(1),
1066 NewFn->getFunctionType()->getParamType(1),
1067 "upgraded.", CI);
1068 break;
1069 case Intrinsic::x86_mmx_pslli_w:
1070 case Intrinsic::x86_mmx_pslli_d:
1071 case Intrinsic::x86_mmx_pslli_q:
1072 case Intrinsic::x86_mmx_psrli_w:
1073 case Intrinsic::x86_mmx_psrli_d:
1074 case Intrinsic::x86_mmx_psrli_q:
1075 case Intrinsic::x86_mmx_psrai_w:
1076 case Intrinsic::x86_mmx_psrai_d:
1077 // These take an i32 as their second parameter.
1078 Operands[1] = CI->getArgOperand(1);
1079 break;
1080 }
1081
1082 ConstructNewCallInst(NewFn, CI, Operands, 2);
Anders Carlssonf924f342007-12-14 06:38:54 +00001083 break;
Dale Johannesendd224d22010-09-30 23:57:10 +00001084 }
1085 case Intrinsic::x86_mmx_maskmovq: {
1086 Value *Operands[3];
1087
1088 // Cast the operands to the X86 MMX type.
1089 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1090 NewFn->getFunctionType()->getParamType(0),
1091 "upgraded.", CI);
1092 Operands[1] = new BitCastInst(CI->getArgOperand(1),
1093 NewFn->getFunctionType()->getParamType(1),
1094 "upgraded.", CI);
1095 Operands[2] = CI->getArgOperand(2);
1096
1097 ConstructNewCallInst(NewFn, CI, Operands, 3, false);
1098 break;
1099 }
1100 case Intrinsic::x86_mmx_pmovmskb: {
1101 Value *Operands[1];
1102
1103 // Cast the operand to the X86 MMX type.
1104 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1105 NewFn->getFunctionType()->getParamType(0),
1106 "upgraded.", CI);
1107
1108 ConstructNewCallInst(NewFn, CI, Operands, 1);
1109 break;
1110 }
1111 case Intrinsic::x86_mmx_movnt_dq: {
1112 Value *Operands[2];
1113
1114 Operands[0] = CI->getArgOperand(0);
1115
1116 // Cast the operand to the X86 MMX type.
1117 Operands[1] = new BitCastInst(CI->getArgOperand(1),
1118 NewFn->getFunctionType()->getParamType(1),
1119 "upgraded.", CI);
1120
1121 ConstructNewCallInst(NewFn, CI, Operands, 2, false);
1122 break;
1123 }
1124 case Intrinsic::x86_mmx_palignr_b: {
1125 Value *Operands[3];
1126
1127 // Cast the operands to the X86 MMX type.
1128 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1129 NewFn->getFunctionType()->getParamType(0),
1130 "upgraded.", CI);
1131 Operands[1] = new BitCastInst(CI->getArgOperand(1),
1132 NewFn->getFunctionType()->getParamType(1),
1133 "upgraded.", CI);
1134 Operands[2] = CI->getArgOperand(2);
1135
1136 ConstructNewCallInst(NewFn, CI, Operands, 3);
1137 break;
1138 }
1139 case Intrinsic::x86_mmx_pextr_w: {
1140 Value *Operands[2];
1141
1142 // Cast the operands to the X86 MMX type.
1143 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1144 NewFn->getFunctionType()->getParamType(0),
1145 "upgraded.", CI);
1146 Operands[1] = CI->getArgOperand(1);
1147
1148 ConstructNewCallInst(NewFn, CI, Operands, 2);
1149 break;
1150 }
1151 case Intrinsic::x86_mmx_pinsr_w: {
1152 Value *Operands[3];
1153
1154 // Cast the operands to the X86 MMX type.
1155 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1156 NewFn->getFunctionType()->getParamType(0),
1157 "upgraded.", CI);
1158 Operands[1] = CI->getArgOperand(1);
1159 Operands[2] = CI->getArgOperand(2);
1160
1161 ConstructNewCallInst(NewFn, CI, Operands, 3);
1162 break;
1163 }
Bill Wendling402e5482010-10-04 20:24:01 +00001164 case Intrinsic::x86_sse_pshuf_w: {
1165 IRBuilder<> Builder(C);
1166 Builder.SetInsertPoint(CI->getParent(), CI);
1167
1168 // Cast the operand to the X86 MMX type.
1169 Value *Operands[2];
1170 Operands[0] =
1171 Builder.CreateBitCast(CI->getArgOperand(0),
1172 NewFn->getFunctionType()->getParamType(0),
1173 "upgraded.");
1174 Operands[1] =
1175 Builder.CreateTrunc(CI->getArgOperand(1),
1176 Type::getInt8Ty(C),
1177 "upgraded.");
1178
1179 ConstructNewCallInst(NewFn, CI, Operands, 2);
1180 break;
1181 }
1182
Dale Johannesendd224d22010-09-30 23:57:10 +00001183#if 0
1184 case Intrinsic::x86_mmx_cvtsi32_si64: {
1185 // The return type needs to be changed.
1186 Value *Operands[1];
1187 Operands[0] = CI->getArgOperand(0);
1188 ConstructNewCallInst(NewFn, CI, Operands, 1);
1189 break;
1190 }
1191 case Intrinsic::x86_mmx_cvtsi64_si32: {
1192 Value *Operands[1];
1193
1194 // Cast the operand to the X86 MMX type.
1195 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1196 NewFn->getFunctionType()->getParamType(0),
1197 "upgraded.", CI);
1198
1199 ConstructNewCallInst(NewFn, CI, Operands, 1);
1200 break;
1201 }
1202 case Intrinsic::x86_mmx_vec_init_b:
1203 case Intrinsic::x86_mmx_vec_init_w:
1204 case Intrinsic::x86_mmx_vec_init_d: {
1205 // The return type needs to be changed.
1206 Value *Operands[8];
1207 unsigned NumOps = 0;
1208
1209 switch (NewFn->getIntrinsicID()) {
1210 default: break;
1211 case Intrinsic::x86_mmx_vec_init_b: NumOps = 8; break;
1212 case Intrinsic::x86_mmx_vec_init_w: NumOps = 4; break;
1213 case Intrinsic::x86_mmx_vec_init_d: NumOps = 2; break;
1214 }
1215
1216 switch (NewFn->getIntrinsicID()) {
1217 default: break;
1218 case Intrinsic::x86_mmx_vec_init_b:
1219 Operands[7] = CI->getArgOperand(7);
1220 Operands[6] = CI->getArgOperand(6);
1221 Operands[5] = CI->getArgOperand(5);
1222 Operands[4] = CI->getArgOperand(4);
1223 // FALLTHRU
1224 case Intrinsic::x86_mmx_vec_init_w:
1225 Operands[3] = CI->getArgOperand(3);
1226 Operands[2] = CI->getArgOperand(2);
1227 // FALLTHRU
1228 case Intrinsic::x86_mmx_vec_init_d:
1229 Operands[1] = CI->getArgOperand(1);
1230 Operands[0] = CI->getArgOperand(0);
1231 break;
1232 }
1233
1234 ConstructNewCallInst(NewFn, CI, Operands, NumOps);
1235 break;
1236 }
1237 case Intrinsic::x86_mmx_vec_ext_d: {
1238 Value *Operands[2];
1239
1240 // Cast the operand to the X86 MMX type.
1241 Operands[0] = new BitCastInst(CI->getArgOperand(0),
1242 NewFn->getFunctionType()->getParamType(0),
1243 "upgraded.", CI);
1244 Operands[1] = CI->getArgOperand(1);
1245
1246 ConstructNewCallInst(NewFn, CI, Operands, 2);
1247 break;
1248 }
1249#endif
1250
Chandler Carruth7132e002007-08-04 01:51:18 +00001251 case Intrinsic::ctlz:
1252 case Intrinsic::ctpop:
Gabor Greife9ecc682008-04-06 20:25:17 +00001253 case Intrinsic::cttz: {
Gabor Greife5406532010-06-23 08:45:32 +00001254 // Build a small vector of the original arguments.
1255 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Chandler Carruth7132e002007-08-04 01:51:18 +00001256
1257 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +00001258 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
1259 "upgraded."+CI->getName(), CI);
Chandler Carruth7132e002007-08-04 01:51:18 +00001260 NewCI->setTailCall(CI->isTailCall());
1261 NewCI->setCallingConv(CI->getCallingConv());
1262
1263 // Handle any uses of the old CallInst.
1264 if (!CI->use_empty()) {
1265 // Check for sign extend parameter attributes on the return values.
Devang Patel4c758ea2008-09-25 21:00:45 +00001266 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
1267 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
Chandler Carruth7132e002007-08-04 01:51:18 +00001268
1269 // Construct an appropriate cast from the new return type to the old.
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001270 CastInst *RetCast = CastInst::Create(
Chandler Carruth7132e002007-08-04 01:51:18 +00001271 CastInst::getCastOpcode(NewCI, SrcSExt,
1272 F->getReturnType(),
1273 DestSExt),
1274 NewCI, F->getReturnType(),
1275 NewCI->getName(), CI);
1276 NewCI->moveBefore(RetCast);
1277
1278 // Replace all uses of the old call with the new cast which has the
1279 // correct type.
1280 CI->replaceAllUsesWith(RetCast);
1281 }
1282
1283 // Clean up the old call now that it has been completely upgraded.
1284 CI->eraseFromParent();
Gabor Greife9ecc682008-04-06 20:25:17 +00001285 }
1286 break;
Duncan Sands8e6ccb62009-10-14 16:11:37 +00001287 case Intrinsic::eh_selector:
1288 case Intrinsic::eh_typeid_for: {
1289 // Only the return type changed.
Gabor Greife5406532010-06-23 08:45:32 +00001290 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Duncan Sands8e6ccb62009-10-14 16:11:37 +00001291 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
1292 "upgraded." + CI->getName(), CI);
1293 NewCI->setTailCall(CI->isTailCall());
1294 NewCI->setCallingConv(CI->getCallingConv());
1295
1296 // Handle any uses of the old CallInst.
1297 if (!CI->use_empty()) {
1298 // Construct an appropriate cast from the new return type to the old.
1299 CastInst *RetCast =
1300 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
1301 F->getReturnType(), true),
1302 NewCI, F->getReturnType(), NewCI->getName(), CI);
1303 CI->replaceAllUsesWith(RetCast);
1304 }
1305 CI->eraseFromParent();
1306 }
1307 break;
Mon P Wangc576ee92010-04-04 03:10:48 +00001308 case Intrinsic::memcpy:
1309 case Intrinsic::memmove:
1310 case Intrinsic::memset: {
1311 // Add isVolatile
1312 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(CI->getContext());
Gabor Greifc89d2aa2010-06-22 20:40:38 +00001313 Value *Operands[5] = { CI->getArgOperand(0), CI->getArgOperand(1),
1314 CI->getArgOperand(2), CI->getArgOperand(3),
Mon P Wangc576ee92010-04-04 03:10:48 +00001315 llvm::ConstantInt::get(I1Ty, 0) };
1316 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+5,
1317 CI->getName(), CI);
1318 NewCI->setTailCall(CI->isTailCall());
1319 NewCI->setCallingConv(CI->getCallingConv());
1320 // Handle any uses of the old CallInst.
1321 if (!CI->use_empty())
1322 // Replace all uses of the old call with the new cast which has the
1323 // correct type.
1324 CI->replaceAllUsesWith(NewCI);
1325
1326 // Clean up the old call now that it has been completely upgraded.
1327 CI->eraseFromParent();
1328 break;
1329 }
Chandler Carruth7132e002007-08-04 01:51:18 +00001330 }
1331}
1332
1333// This tests each Function to determine if it needs upgrading. When we find
1334// one we are interested in, we then upgrade all calls to reflect the new
1335// function.
1336void llvm::UpgradeCallsToIntrinsic(Function* F) {
1337 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
1338
1339 // Upgrade the function and check if it is a totaly new function.
Evan Cheng0e179d02007-12-17 22:33:23 +00001340 Function* NewFn;
1341 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +00001342 if (NewFn != F) {
1343 // Replace all uses to the old function with the new one if necessary.
1344 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
1345 UI != UE; ) {
1346 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
1347 UpgradeIntrinsicCall(CI, NewFn);
1348 }
1349 // Remove old function, no longer used, from the module.
1350 F->eraseFromParent();
1351 }
1352 }
1353}
Devang Patel80ae3492009-08-28 23:24:31 +00001354
Victor Hernandezc2044a12010-01-05 21:13:46 +00001355/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
1356/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
1357/// strips that use.
Devang Patel80ae3492009-08-28 23:24:31 +00001358void llvm::CheckDebugInfoIntrinsics(Module *M) {
1359
1360
1361 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +00001362 while (!FuncStart->use_empty()) {
1363 CallInst *CI = cast<CallInst>(FuncStart->use_back());
1364 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +00001365 }
Devang Patelbe94f232010-01-05 01:10:40 +00001366 FuncStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +00001367 }
Devang Patelbe94f232010-01-05 01:10:40 +00001368
Devang Patel80ae3492009-08-28 23:24:31 +00001369 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
Devang Patelbe94f232010-01-05 01:10:40 +00001370 while (!StopPoint->use_empty()) {
1371 CallInst *CI = cast<CallInst>(StopPoint->use_back());
1372 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +00001373 }
Devang Patelbe94f232010-01-05 01:10:40 +00001374 StopPoint->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +00001375 }
1376
1377 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +00001378 while (!RegionStart->use_empty()) {
1379 CallInst *CI = cast<CallInst>(RegionStart->use_back());
1380 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +00001381 }
Devang Patelbe94f232010-01-05 01:10:40 +00001382 RegionStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +00001383 }
1384
1385 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
Devang Patelbe94f232010-01-05 01:10:40 +00001386 while (!RegionEnd->use_empty()) {
1387 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
1388 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +00001389 }
Devang Patelbe94f232010-01-05 01:10:40 +00001390 RegionEnd->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +00001391 }
1392
1393 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
1394 if (!Declare->use_empty()) {
1395 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
Gabor Greifc89d2aa2010-06-22 20:40:38 +00001396 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
1397 !isa<MDNode>(DDI->getArgOperand(1))) {
Devang Patel80ae3492009-08-28 23:24:31 +00001398 while (!Declare->use_empty()) {
1399 CallInst *CI = cast<CallInst>(Declare->use_back());
1400 CI->eraseFromParent();
1401 }
1402 Declare->eraseFromParent();
1403 }
1404 }
1405 }
1406}