blob: 62a46259ac259e57676d5c1c261a7b4de793f2bd [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 ||
84 Name.compare(14, 5, "vsubl", 5) == 0) &&
85 (Name.compare(19, 2, "s.", 2) == 0 ||
86 Name.compare(19, 2, "u.", 2) == 0)) ||
87
88 ((Name.compare(14, 5, "vaddw", 5) == 0 ||
89 Name.compare(14, 5, "vsubw", 5) == 0) &&
90 (Name.compare(19, 2, "s.", 2) == 0 ||
91 Name.compare(19, 2, "u.", 2) == 0))) {
92
Bob Wilson9a511c02010-08-20 04:54:02 +000093 // Calls to these are transformed into IR without intrinsics.
94 NewFn = 0;
95 return true;
96 }
Bob Wilsonedf722a2010-08-27 17:13:24 +000097 // Old versions of NEON ld/st intrinsics are missing alignment arguments.
98 bool isVLd = (Name.compare(14, 3, "vld", 3) == 0);
99 bool isVSt = (Name.compare(14, 3, "vst", 3) == 0);
100 if (isVLd || isVSt) {
101 unsigned NumVecs = Name.at(17) - '0';
102 if (NumVecs == 0 || NumVecs > 4)
103 return false;
104 bool isLaneOp = (Name.compare(18, 5, "lane.", 5) == 0);
105 if (!isLaneOp && Name.at(18) != '.')
106 return false;
107 unsigned ExpectedArgs = 2; // for the address and alignment
108 if (isVSt || isLaneOp)
109 ExpectedArgs += NumVecs;
110 if (isLaneOp)
111 ExpectedArgs += 1; // for the lane number
112 unsigned NumP = FTy->getNumParams();
113 if (NumP != ExpectedArgs - 1)
114 return false;
115
116 // Change the name of the old (bad) intrinsic, because
117 // its type is incorrect, but we cannot overload that name.
118 F->setName("");
119
120 // One argument is missing: add the alignment argument.
121 std::vector<const Type*> NewParams;
122 for (unsigned p = 0; p < NumP; ++p)
123 NewParams.push_back(FTy->getParamType(p));
124 NewParams.push_back(Type::getInt32Ty(F->getContext()));
125 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(),
126 NewParams, false);
127 NewFn = cast<Function>(M->getOrInsertFunction(Name, NewFTy));
128 return true;
129 }
Mon P Wang6a490372008-06-25 08:15:39 +0000130 }
131 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000132 case 'b':
133 // This upgrades the name of the llvm.bswap intrinsic function to only use
134 // a single type name for overloading. We only care about the old format
135 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
136 // a '.' after 'bswap.'
137 if (Name.compare(5,6,"bswap.",6) == 0) {
138 std::string::size_type delim = Name.find('.',11);
139
140 if (delim != std::string::npos) {
141 // Construct the new name as 'llvm.bswap' + '.i*'
142 F->setName(Name.substr(0,10)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000143 NewFn = F;
144 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000145 }
146 }
147 break;
148
149 case 'c':
150 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
151 // correct return type, so we check for the name, and then check if the
152 // return type does not match the parameter type.
153 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
154 Name.compare(5,4,"ctlz",4) == 0 ||
155 Name.compare(5,4,"cttz",4) == 0) &&
156 FTy->getReturnType() != FTy->getParamType(0)) {
157 // We first need to change the name of the old (bad) intrinsic, because
158 // its type is incorrect, but we cannot overload that name. We
159 // arbitrarily unique it here allowing us to construct a correctly named
160 // and typed function below.
161 F->setName("");
162
163 // Now construct the new intrinsic with the correct name and type. We
164 // leave the old function around in order to query its type, whatever it
165 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000166 NewFn = cast<Function>(M->getOrInsertFunction(Name,
167 FTy->getParamType(0),
168 FTy->getParamType(0),
169 (Type *)0));
170 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000171 }
172 break;
173
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000174 case 'e':
175 // The old llvm.eh.selector.i32 is equivalent to the new llvm.eh.selector.
176 if (Name.compare("llvm.eh.selector.i32") == 0) {
177 F->setName("llvm.eh.selector");
178 NewFn = F;
179 return true;
180 }
181 // The old llvm.eh.typeid.for.i32 is equivalent to llvm.eh.typeid.for.
182 if (Name.compare("llvm.eh.typeid.for.i32") == 0) {
183 F->setName("llvm.eh.typeid.for");
184 NewFn = F;
185 return true;
186 }
187 // Convert the old llvm.eh.selector.i64 to a call to llvm.eh.selector.
188 if (Name.compare("llvm.eh.selector.i64") == 0) {
189 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_selector);
190 return true;
191 }
192 // Convert the old llvm.eh.typeid.for.i64 to a call to llvm.eh.typeid.for.
193 if (Name.compare("llvm.eh.typeid.for.i64") == 0) {
194 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_typeid_for);
195 return true;
196 }
197 break;
198
Mon P Wangc576ee92010-04-04 03:10:48 +0000199 case 'm': {
200 // This upgrades the llvm.memcpy, llvm.memmove, and llvm.memset to the
201 // new format that allows overloading the pointer for different address
202 // space (e.g., llvm.memcpy.i16 => llvm.memcpy.p0i8.p0i8.i16)
203 const char* NewFnName = NULL;
204 if (Name.compare(5,8,"memcpy.i",8) == 0) {
205 if (Name[13] == '8')
206 NewFnName = "llvm.memcpy.p0i8.p0i8.i8";
207 else if (Name.compare(13,2,"16") == 0)
208 NewFnName = "llvm.memcpy.p0i8.p0i8.i16";
209 else if (Name.compare(13,2,"32") == 0)
210 NewFnName = "llvm.memcpy.p0i8.p0i8.i32";
211 else if (Name.compare(13,2,"64") == 0)
212 NewFnName = "llvm.memcpy.p0i8.p0i8.i64";
213 } else if (Name.compare(5,9,"memmove.i",9) == 0) {
214 if (Name[14] == '8')
215 NewFnName = "llvm.memmove.p0i8.p0i8.i8";
216 else if (Name.compare(14,2,"16") == 0)
217 NewFnName = "llvm.memmove.p0i8.p0i8.i16";
218 else if (Name.compare(14,2,"32") == 0)
219 NewFnName = "llvm.memmove.p0i8.p0i8.i32";
220 else if (Name.compare(14,2,"64") == 0)
221 NewFnName = "llvm.memmove.p0i8.p0i8.i64";
222 }
223 else if (Name.compare(5,8,"memset.i",8) == 0) {
224 if (Name[13] == '8')
225 NewFnName = "llvm.memset.p0i8.i8";
226 else if (Name.compare(13,2,"16") == 0)
227 NewFnName = "llvm.memset.p0i8.i16";
228 else if (Name.compare(13,2,"32") == 0)
229 NewFnName = "llvm.memset.p0i8.i32";
230 else if (Name.compare(13,2,"64") == 0)
231 NewFnName = "llvm.memset.p0i8.i64";
232 }
233 if (NewFnName) {
Mon P Wangc576ee92010-04-04 03:10:48 +0000234 NewFn = cast<Function>(M->getOrInsertFunction(NewFnName,
235 FTy->getReturnType(),
236 FTy->getParamType(0),
237 FTy->getParamType(1),
238 FTy->getParamType(2),
239 FTy->getParamType(3),
240 Type::getInt1Ty(F->getContext()),
241 (Type *)0));
242 return true;
243 }
244 break;
245 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000246 case 'p':
247 // This upgrades the llvm.part.select overloaded intrinsic names to only
248 // use one type specifier in the name. We only care about the old format
249 // 'llvm.part.select.i*.i*', and solve as above with bswap.
250 if (Name.compare(5,12,"part.select.",12) == 0) {
251 std::string::size_type delim = Name.find('.',17);
252
253 if (delim != std::string::npos) {
254 // Construct a new name as 'llvm.part.select' + '.i*'
255 F->setName(Name.substr(0,16)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000256 NewFn = F;
257 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000258 }
259 break;
260 }
261
262 // This upgrades the llvm.part.set intrinsics similarly as above, however
263 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
264 // must match. There is an additional type specifier after these two
265 // matching types that we must retain when upgrading. Thus, we require
266 // finding 2 periods, not just one, after the intrinsic name.
267 if (Name.compare(5,9,"part.set.",9) == 0) {
268 std::string::size_type delim = Name.find('.',14);
269
270 if (delim != std::string::npos &&
271 Name.find('.',delim+1) != std::string::npos) {
272 // Construct a new name as 'llvm.part.select' + '.i*.i*'
273 F->setName(Name.substr(0,13)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000274 NewFn = F;
275 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000276 }
277 break;
278 }
279
280 break;
Anders Carlssonf924f342007-12-14 06:38:54 +0000281 case 'x':
282 // This fixes all MMX shift intrinsic instructions to take a
283 // v1i64 instead of a v2i32 as the second parameter.
284 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
285 (Name.compare(13,4,"psll", 4) == 0 ||
286 Name.compare(13,4,"psra", 4) == 0 ||
Evan Chengcdf22f22008-05-03 00:52:09 +0000287 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
Anders Carlssonf924f342007-12-14 06:38:54 +0000288
Owen Anderson155dccd82009-07-07 23:43:39 +0000289 const llvm::Type *VT =
Owen Anderson55f1c092009-08-13 21:58:54 +0000290 VectorType::get(IntegerType::get(FTy->getContext(), 64), 1);
Anders Carlssonf924f342007-12-14 06:38:54 +0000291
292 // We don't have to do anything if the parameter already has
293 // the correct type.
294 if (FTy->getParamType(1) == VT)
295 break;
296
297 // We first need to change the name of the old (bad) intrinsic, because
298 // its type is incorrect, but we cannot overload that name. We
299 // arbitrarily unique it here allowing us to construct a correctly named
300 // and typed function below.
301 F->setName("");
302
303 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
304
305 // Now construct the new intrinsic with the correct name and type. We
306 // leave the old function around in order to query its type, whatever it
307 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000308 NewFn = cast<Function>(M->getOrInsertFunction(Name,
309 FTy->getReturnType(),
310 FTy->getParamType(0),
311 VT,
312 (Type *)0));
313 return true;
Evan Cheng50659322008-05-24 00:08:39 +0000314 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
315 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
Evan Cheng2146270c2008-05-24 02:14:05 +0000316 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
317 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
318 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
319 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000320 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
321 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
322 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
Evan Cheng50659322008-05-24 00:08:39 +0000323 // Calls to these intrinsics are transformed into ShuffleVector's.
Evan Cheng0e179d02007-12-17 22:33:23 +0000324 NewFn = 0;
325 return true;
Eric Christopher6ad81672010-03-30 18:49:01 +0000326 } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) {
327 // Calls to these intrinsics are transformed into vector multiplies.
328 NewFn = 0;
329 return true;
Eric Christopher64831c62010-04-20 00:59:54 +0000330 } else if (Name.compare(5, 18, "x86.ssse3.palign.r", 18) == 0 ||
331 Name.compare(5, 22, "x86.ssse3.palign.r.128", 22) == 0) {
332 // Calls to these intrinsics are transformed into vector shuffles, shifts,
333 // or 0.
334 NewFn = 0;
335 return true;
Anders Carlssonf924f342007-12-14 06:38:54 +0000336 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000337
Anders Carlssonf924f342007-12-14 06:38:54 +0000338 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000339 }
340
341 // This may not belong here. This function is effectively being overloaded
342 // to both detect an intrinsic which needs upgrading, and to provide the
343 // upgraded form of the intrinsic. We should perhaps have two separate
344 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000345 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000346}
347
Evan Cheng0e179d02007-12-17 22:33:23 +0000348bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
349 NewFn = 0;
350 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000351
352 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000353 if (NewFn)
354 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000355 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000356 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000357 return Upgraded;
358}
359
Chandler Carruth7132e002007-08-04 01:51:18 +0000360// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
361// upgraded intrinsic. All argument and return casting must be provided in
362// order to seamlessly integrate with existing context.
363void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000364 Function *F = CI->getCalledFunction();
Owen Anderson55f1c092009-08-13 21:58:54 +0000365 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +0000366 ImmutableCallSite CS(CI);
367
Chandler Carruth7132e002007-08-04 01:51:18 +0000368 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000369
370 if (!NewFn) {
Bob Wilson9a511c02010-08-20 04:54:02 +0000371 // Get the Function's name.
372 const std::string& Name = F->getName();
373
374 // Upgrade ARM NEON intrinsics.
375 if (Name.compare(5, 9, "arm.neon.", 9) == 0) {
376 Instruction *NewI;
377 if (Name.compare(14, 7, "vmovls.", 7) == 0) {
378 NewI = new SExtInst(CI->getArgOperand(0), CI->getType(),
379 "upgraded." + CI->getName(), CI);
380 } else if (Name.compare(14, 7, "vmovlu.", 7) == 0) {
381 NewI = new ZExtInst(CI->getArgOperand(0), CI->getType(),
382 "upgraded." + CI->getName(), CI);
Bob Wilsond0c05482010-08-29 05:57:34 +0000383
384 } else if (Name.compare(14, 4, "vadd", 4) == 0 ||
385 Name.compare(14, 4, "vsub", 4) == 0) {
386 // Extend one (vaddw/vsubw) or both (vaddl/vsubl) operands.
387 Value *V0 = CI->getArgOperand(0);
388 Value *V1 = CI->getArgOperand(1);
389 if (Name.at(19) == 's') {
390 if (Name.at(18) == 'l')
391 V0 = new SExtInst(CI->getArgOperand(0), CI->getType(), "", CI);
392 V1 = new SExtInst(CI->getArgOperand(1), CI->getType(), "", CI);
393 } else {
394 assert(Name.at(19) == 'u' && "unexpected vadd/vsub intrinsic");
395 if (Name.at(18) == 'l')
396 V0 = new ZExtInst(CI->getArgOperand(0), CI->getType(), "", CI);
397 V1 = new ZExtInst(CI->getArgOperand(1), CI->getType(), "", CI);
398 }
399 if (Name.compare(14, 4, "vadd", 4) == 0)
400 NewI = BinaryOperator::CreateAdd(V0, V1,"upgraded."+CI->getName(),CI);
401 else
402 NewI = BinaryOperator::CreateSub(V0, V1,"upgraded."+CI->getName(),CI);
403
Bob Wilson9a511c02010-08-20 04:54:02 +0000404 } else {
405 llvm_unreachable("Unknown arm.neon function for CallInst upgrade.");
406 }
407 // Replace any uses of the old CallInst.
408 if (!CI->use_empty())
409 CI->replaceAllUsesWith(NewI);
410 CI->eraseFromParent();
411 return;
412 }
413
Evan Cheng50659322008-05-24 00:08:39 +0000414 bool isLoadH = false, isLoadL = false, isMovL = false;
Evan Cheng2146270c2008-05-24 02:14:05 +0000415 bool isMovSD = false, isShufPD = false;
416 bool isUnpckhPD = false, isUnpcklPD = false;
Evan Cheng91a2e562008-05-24 02:56:30 +0000417 bool isPunpckhQPD = false, isPunpcklQPD = false;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000418 if (F->getName() == "llvm.x86.sse2.loadh.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000419 isLoadH = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000420 else if (F->getName() == "llvm.x86.sse2.loadl.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000421 isLoadL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000422 else if (F->getName() == "llvm.x86.sse2.movl.dq")
Evan Cheng50659322008-05-24 00:08:39 +0000423 isMovL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000424 else if (F->getName() == "llvm.x86.sse2.movs.d")
Evan Cheng2146270c2008-05-24 02:14:05 +0000425 isMovSD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000426 else if (F->getName() == "llvm.x86.sse2.shuf.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000427 isShufPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000428 else if (F->getName() == "llvm.x86.sse2.unpckh.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000429 isUnpckhPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000430 else if (F->getName() == "llvm.x86.sse2.unpckl.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000431 isUnpcklPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000432 else if (F->getName() == "llvm.x86.sse2.punpckh.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000433 isPunpckhQPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000434 else if (F->getName() == "llvm.x86.sse2.punpckl.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000435 isPunpcklQPD = true;
Evan Cheng0e179d02007-12-17 22:33:23 +0000436
Evan Cheng2146270c2008-05-24 02:14:05 +0000437 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000438 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Evan Cheng50659322008-05-24 00:08:39 +0000439 std::vector<Constant*> Idxs;
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000440 Value *Op0 = CI->getArgOperand(0);
Evan Cheng2146270c2008-05-24 02:14:05 +0000441 ShuffleVectorInst *SI = NULL;
Evan Cheng50659322008-05-24 00:08:39 +0000442 if (isLoadH || isLoadL) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000443 Value *Op1 = UndefValue::get(Op0->getType());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000444 Value *Addr = new BitCastInst(CI->getArgOperand(1),
Duncan Sands9ed7b162009-10-06 15:40:36 +0000445 Type::getDoublePtrTy(C),
Evan Cheng50659322008-05-24 00:08:39 +0000446 "upgraded.", CI);
447 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
Owen Anderson55f1c092009-08-13 21:58:54 +0000448 Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000449 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
450
451 if (isLoadH) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000452 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
453 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng50659322008-05-24 00:08:39 +0000454 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000455 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
456 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng50659322008-05-24 00:08:39 +0000457 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000458 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000459 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng2146270c2008-05-24 02:14:05 +0000460 } else if (isMovL) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000461 Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000462 Idxs.push_back(Zero);
463 Idxs.push_back(Zero);
464 Idxs.push_back(Zero);
465 Idxs.push_back(Zero);
Owen Anderson4aa32952009-07-28 21:19:26 +0000466 Value *ZeroV = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000467
468 Idxs.clear();
Owen Anderson55f1c092009-08-13 21:58:54 +0000469 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4));
470 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5));
471 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
472 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Owen Anderson4aa32952009-07-28 21:19:26 +0000473 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000474 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
Evan Cheng91a2e562008-05-24 02:56:30 +0000475 } else if (isMovSD ||
476 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000477 Value *Op1 = CI->getArgOperand(1);
Evan Cheng2146270c2008-05-24 02:14:05 +0000478 if (isMovSD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000479 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
480 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng91a2e562008-05-24 02:56:30 +0000481 } else if (isUnpckhPD || isPunpckhQPD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000482 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
483 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Evan Cheng2146270c2008-05-24 02:14:05 +0000484 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000485 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
486 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng2146270c2008-05-24 02:14:05 +0000487 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000488 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000489 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
490 } else if (isShufPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000491 Value *Op1 = CI->getArgOperand(1);
Gabor Greif3e44ea12010-07-22 10:37:47 +0000492 unsigned MaskVal =
493 cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
Owen Anderson55f1c092009-08-13 21:58:54 +0000494 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
495 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
Owen Anderson155dccd82009-07-07 23:43:39 +0000496 ((MaskVal >> 1) & 1)+2));
Owen Anderson4aa32952009-07-28 21:19:26 +0000497 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000498 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng50659322008-05-24 00:08:39 +0000499 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000500
Evan Cheng2146270c2008-05-24 02:14:05 +0000501 assert(SI && "Unexpected!");
502
Evan Cheng0e179d02007-12-17 22:33:23 +0000503 // Handle any uses of the old CallInst.
504 if (!CI->use_empty())
505 // Replace all uses of the old call with the new cast which has the
506 // correct type.
507 CI->replaceAllUsesWith(SI);
508
509 // Clean up the old call now that it has been completely upgraded.
510 CI->eraseFromParent();
Eric Christopher6ad81672010-03-30 18:49:01 +0000511 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
512 // Upgrade this set of intrinsics into vector multiplies.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000513 Instruction *Mul = BinaryOperator::CreateMul(CI->getArgOperand(0),
514 CI->getArgOperand(1),
Eric Christopher6ad81672010-03-30 18:49:01 +0000515 CI->getName(),
516 CI);
517 // Fix up all the uses with our new multiply.
518 if (!CI->use_empty())
519 CI->replaceAllUsesWith(Mul);
520
521 // Remove upgraded multiply.
522 CI->eraseFromParent();
Eric Christopher64831c62010-04-20 00:59:54 +0000523 } else if (F->getName() == "llvm.x86.ssse3.palign.r") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000524 Value *Op1 = CI->getArgOperand(0);
525 Value *Op2 = CI->getArgOperand(1);
526 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000527 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
528 Value *Rep;
529 IRBuilder<> Builder(C);
530 Builder.SetInsertPoint(CI->getParent(), CI);
531
532 // If palignr is shifting the pair of input vectors less than 9 bytes,
533 // emit a shuffle instruction.
534 if (shiftVal <= 8) {
535 const Type *IntTy = Type::getInt32Ty(C);
536 const Type *EltTy = Type::getInt8Ty(C);
537 const Type *VecTy = VectorType::get(EltTy, 8);
538
539 Op2 = Builder.CreateBitCast(Op2, VecTy);
540 Op1 = Builder.CreateBitCast(Op1, VecTy);
541
542 llvm::SmallVector<llvm::Constant*, 8> Indices;
543 for (unsigned i = 0; i != 8; ++i)
544 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
545
546 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
547 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
548 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
549 }
550
551 // If palignr is shifting the pair of input vectors more than 8 but less
552 // than 16 bytes, emit a logical right shift of the destination.
553 else if (shiftVal < 16) {
554 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
555 const Type *EltTy = Type::getInt64Ty(C);
556 const Type *VecTy = VectorType::get(EltTy, 1);
557
558 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
559 Op2 = ConstantInt::get(VecTy, (shiftVal-8) * 8);
560
561 // create i32 constant
562 Function *I =
563 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_mmx_psrl_q);
564 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
565 }
566
567 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
568 else {
569 Rep = Constant::getNullValue(F->getReturnType());
570 }
571
572 // Replace any uses with our new instruction.
573 if (!CI->use_empty())
574 CI->replaceAllUsesWith(Rep);
575
576 // Remove upgraded instruction.
577 CI->eraseFromParent();
578
579 } else if (F->getName() == "llvm.x86.ssse3.palign.r.128") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000580 Value *Op1 = CI->getArgOperand(0);
581 Value *Op2 = CI->getArgOperand(1);
582 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000583 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
584 Value *Rep;
585 IRBuilder<> Builder(C);
586 Builder.SetInsertPoint(CI->getParent(), CI);
587
588 // If palignr is shifting the pair of input vectors less than 17 bytes,
589 // emit a shuffle instruction.
590 if (shiftVal <= 16) {
591 const Type *IntTy = Type::getInt32Ty(C);
592 const Type *EltTy = Type::getInt8Ty(C);
593 const Type *VecTy = VectorType::get(EltTy, 16);
594
595 Op2 = Builder.CreateBitCast(Op2, VecTy);
596 Op1 = Builder.CreateBitCast(Op1, VecTy);
597
598 llvm::SmallVector<llvm::Constant*, 16> Indices;
599 for (unsigned i = 0; i != 16; ++i)
600 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
601
602 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
603 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
604 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
605 }
606
607 // If palignr is shifting the pair of input vectors more than 16 but less
608 // than 32 bytes, emit a logical right shift of the destination.
609 else if (shiftVal < 32) {
610 const Type *EltTy = Type::getInt64Ty(C);
611 const Type *VecTy = VectorType::get(EltTy, 2);
612 const Type *IntTy = Type::getInt32Ty(C);
613
614 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
615 Op2 = ConstantInt::get(IntTy, (shiftVal-16) * 8);
616
617 // create i32 constant
618 Function *I =
619 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_sse2_psrl_dq);
620 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
621 }
622
623 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
624 else {
625 Rep = Constant::getNullValue(F->getReturnType());
626 }
627
628 // Replace any uses with our new instruction.
629 if (!CI->use_empty())
630 CI->replaceAllUsesWith(Rep);
631
632 // Remove upgraded instruction.
633 CI->eraseFromParent();
634
Evan Chenga8288f42007-12-18 01:04:25 +0000635 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000636 llvm_unreachable("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000637 }
638 return;
639 }
640
Gabor Greife9ecc682008-04-06 20:25:17 +0000641 switch (NewFn->getIntrinsicID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000642 default: llvm_unreachable("Unknown function for CallInst upgrade.");
Bob Wilsonedf722a2010-08-27 17:13:24 +0000643 case Intrinsic::arm_neon_vld1:
644 case Intrinsic::arm_neon_vld2:
645 case Intrinsic::arm_neon_vld3:
646 case Intrinsic::arm_neon_vld4:
647 case Intrinsic::arm_neon_vst1:
648 case Intrinsic::arm_neon_vst2:
649 case Intrinsic::arm_neon_vst3:
650 case Intrinsic::arm_neon_vst4:
651 case Intrinsic::arm_neon_vld2lane:
652 case Intrinsic::arm_neon_vld3lane:
653 case Intrinsic::arm_neon_vld4lane:
654 case Intrinsic::arm_neon_vst2lane:
655 case Intrinsic::arm_neon_vst3lane:
656 case Intrinsic::arm_neon_vst4lane: {
657 // Add a default alignment argument of 1.
658 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
659 Operands.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
660 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
661 CI->getName(), CI);
662 NewCI->setTailCall(CI->isTailCall());
663 NewCI->setCallingConv(CI->getCallingConv());
664
665 // Handle any uses of the old CallInst.
666 if (!CI->use_empty())
667 // Replace all uses of the old call with the new cast which has the
668 // correct type.
669 CI->replaceAllUsesWith(NewCI);
670
671 // Clean up the old call now that it has been completely upgraded.
672 CI->eraseFromParent();
673 break;
674 }
675
Anders Carlssonf924f342007-12-14 06:38:54 +0000676 case Intrinsic::x86_mmx_psll_d:
677 case Intrinsic::x86_mmx_psll_q:
678 case Intrinsic::x86_mmx_psll_w:
679 case Intrinsic::x86_mmx_psra_d:
680 case Intrinsic::x86_mmx_psra_w:
681 case Intrinsic::x86_mmx_psrl_d:
682 case Intrinsic::x86_mmx_psrl_q:
683 case Intrinsic::x86_mmx_psrl_w: {
Chris Lattner8a923e72008-03-12 17:45:29 +0000684 Value *Operands[2];
Anders Carlssonf924f342007-12-14 06:38:54 +0000685
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000686 Operands[0] = CI->getArgOperand(0);
Anders Carlssonf924f342007-12-14 06:38:54 +0000687
688 // Cast the second parameter to the correct type.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000689 BitCastInst *BC = new BitCastInst(CI->getArgOperand(1),
Anders Carlssonf924f342007-12-14 06:38:54 +0000690 NewFn->getFunctionType()->getParamType(1),
Evan Cheng50659322008-05-24 00:08:39 +0000691 "upgraded.", CI);
Chris Lattner8a923e72008-03-12 17:45:29 +0000692 Operands[1] = BC;
Anders Carlssonf924f342007-12-14 06:38:54 +0000693
694 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000695 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
696 "upgraded."+CI->getName(), CI);
Anders Carlssonf924f342007-12-14 06:38:54 +0000697 NewCI->setTailCall(CI->isTailCall());
698 NewCI->setCallingConv(CI->getCallingConv());
699
700 // Handle any uses of the old CallInst.
701 if (!CI->use_empty())
702 // Replace all uses of the old call with the new cast which has the
703 // correct type.
704 CI->replaceAllUsesWith(NewCI);
705
706 // Clean up the old call now that it has been completely upgraded.
707 CI->eraseFromParent();
708 break;
709 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000710 case Intrinsic::ctlz:
711 case Intrinsic::ctpop:
Gabor Greife9ecc682008-04-06 20:25:17 +0000712 case Intrinsic::cttz: {
Gabor Greife5406532010-06-23 08:45:32 +0000713 // Build a small vector of the original arguments.
714 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Chandler Carruth7132e002007-08-04 01:51:18 +0000715
716 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000717 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
718 "upgraded."+CI->getName(), CI);
Chandler Carruth7132e002007-08-04 01:51:18 +0000719 NewCI->setTailCall(CI->isTailCall());
720 NewCI->setCallingConv(CI->getCallingConv());
721
722 // Handle any uses of the old CallInst.
723 if (!CI->use_empty()) {
724 // Check for sign extend parameter attributes on the return values.
Devang Patel4c758ea2008-09-25 21:00:45 +0000725 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
726 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
Chandler Carruth7132e002007-08-04 01:51:18 +0000727
728 // Construct an appropriate cast from the new return type to the old.
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000729 CastInst *RetCast = CastInst::Create(
Chandler Carruth7132e002007-08-04 01:51:18 +0000730 CastInst::getCastOpcode(NewCI, SrcSExt,
731 F->getReturnType(),
732 DestSExt),
733 NewCI, F->getReturnType(),
734 NewCI->getName(), CI);
735 NewCI->moveBefore(RetCast);
736
737 // Replace all uses of the old call with the new cast which has the
738 // correct type.
739 CI->replaceAllUsesWith(RetCast);
740 }
741
742 // Clean up the old call now that it has been completely upgraded.
743 CI->eraseFromParent();
Gabor Greife9ecc682008-04-06 20:25:17 +0000744 }
745 break;
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000746 case Intrinsic::eh_selector:
747 case Intrinsic::eh_typeid_for: {
748 // Only the return type changed.
Gabor Greife5406532010-06-23 08:45:32 +0000749 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000750 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
751 "upgraded." + CI->getName(), CI);
752 NewCI->setTailCall(CI->isTailCall());
753 NewCI->setCallingConv(CI->getCallingConv());
754
755 // Handle any uses of the old CallInst.
756 if (!CI->use_empty()) {
757 // Construct an appropriate cast from the new return type to the old.
758 CastInst *RetCast =
759 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
760 F->getReturnType(), true),
761 NewCI, F->getReturnType(), NewCI->getName(), CI);
762 CI->replaceAllUsesWith(RetCast);
763 }
764 CI->eraseFromParent();
765 }
766 break;
Mon P Wangc576ee92010-04-04 03:10:48 +0000767 case Intrinsic::memcpy:
768 case Intrinsic::memmove:
769 case Intrinsic::memset: {
770 // Add isVolatile
771 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(CI->getContext());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000772 Value *Operands[5] = { CI->getArgOperand(0), CI->getArgOperand(1),
773 CI->getArgOperand(2), CI->getArgOperand(3),
Mon P Wangc576ee92010-04-04 03:10:48 +0000774 llvm::ConstantInt::get(I1Ty, 0) };
775 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+5,
776 CI->getName(), CI);
777 NewCI->setTailCall(CI->isTailCall());
778 NewCI->setCallingConv(CI->getCallingConv());
779 // Handle any uses of the old CallInst.
780 if (!CI->use_empty())
781 // Replace all uses of the old call with the new cast which has the
782 // correct type.
783 CI->replaceAllUsesWith(NewCI);
784
785 // Clean up the old call now that it has been completely upgraded.
786 CI->eraseFromParent();
787 break;
788 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000789 }
790}
791
792// This tests each Function to determine if it needs upgrading. When we find
793// one we are interested in, we then upgrade all calls to reflect the new
794// function.
795void llvm::UpgradeCallsToIntrinsic(Function* F) {
796 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
797
798 // Upgrade the function and check if it is a totaly new function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000799 Function* NewFn;
800 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000801 if (NewFn != F) {
802 // Replace all uses to the old function with the new one if necessary.
803 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
804 UI != UE; ) {
805 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
806 UpgradeIntrinsicCall(CI, NewFn);
807 }
808 // Remove old function, no longer used, from the module.
809 F->eraseFromParent();
810 }
811 }
812}
Devang Patel80ae3492009-08-28 23:24:31 +0000813
Victor Hernandezc2044a12010-01-05 21:13:46 +0000814/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
815/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
816/// strips that use.
Devang Patel80ae3492009-08-28 23:24:31 +0000817void llvm::CheckDebugInfoIntrinsics(Module *M) {
818
819
820 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000821 while (!FuncStart->use_empty()) {
822 CallInst *CI = cast<CallInst>(FuncStart->use_back());
823 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000824 }
Devang Patelbe94f232010-01-05 01:10:40 +0000825 FuncStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000826 }
Devang Patelbe94f232010-01-05 01:10:40 +0000827
Devang Patel80ae3492009-08-28 23:24:31 +0000828 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000829 while (!StopPoint->use_empty()) {
830 CallInst *CI = cast<CallInst>(StopPoint->use_back());
831 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000832 }
Devang Patelbe94f232010-01-05 01:10:40 +0000833 StopPoint->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000834 }
835
836 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000837 while (!RegionStart->use_empty()) {
838 CallInst *CI = cast<CallInst>(RegionStart->use_back());
839 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000840 }
Devang Patelbe94f232010-01-05 01:10:40 +0000841 RegionStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000842 }
843
844 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000845 while (!RegionEnd->use_empty()) {
846 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
847 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000848 }
Devang Patelbe94f232010-01-05 01:10:40 +0000849 RegionEnd->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000850 }
851
852 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
853 if (!Declare->use_empty()) {
854 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000855 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
856 !isa<MDNode>(DDI->getArgOperand(1))) {
Devang Patel80ae3492009-08-28 23:24:31 +0000857 while (!Declare->use_empty()) {
858 CallInst *CI = cast<CallInst>(Declare->use_back());
859 CI->eraseFromParent();
860 }
861 Declare->eraseFromParent();
862 }
863 }
864 }
865}