blob: c99433965338dde780c045c53cdb2a64dd8802ee [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 ||
Bob Wilson4cd8a122010-08-30 20:02:30 +000091 Name.compare(19, 2, "u.", 2) == 0)) ||
92
93 (Name.compare(14, 6, "vmovn.", 6) == 0)) {
Bob Wilsond0c05482010-08-29 05:57:34 +000094
Bob Wilson9a511c02010-08-20 04:54:02 +000095 // Calls to these are transformed into IR without intrinsics.
96 NewFn = 0;
97 return true;
98 }
Bob Wilsonedf722a2010-08-27 17:13:24 +000099 // Old versions of NEON ld/st intrinsics are missing alignment arguments.
100 bool isVLd = (Name.compare(14, 3, "vld", 3) == 0);
101 bool isVSt = (Name.compare(14, 3, "vst", 3) == 0);
102 if (isVLd || isVSt) {
103 unsigned NumVecs = Name.at(17) - '0';
104 if (NumVecs == 0 || NumVecs > 4)
105 return false;
106 bool isLaneOp = (Name.compare(18, 5, "lane.", 5) == 0);
107 if (!isLaneOp && Name.at(18) != '.')
108 return false;
109 unsigned ExpectedArgs = 2; // for the address and alignment
110 if (isVSt || isLaneOp)
111 ExpectedArgs += NumVecs;
112 if (isLaneOp)
113 ExpectedArgs += 1; // for the lane number
114 unsigned NumP = FTy->getNumParams();
115 if (NumP != ExpectedArgs - 1)
116 return false;
117
118 // Change the name of the old (bad) intrinsic, because
119 // its type is incorrect, but we cannot overload that name.
120 F->setName("");
121
122 // One argument is missing: add the alignment argument.
123 std::vector<const Type*> NewParams;
124 for (unsigned p = 0; p < NumP; ++p)
125 NewParams.push_back(FTy->getParamType(p));
126 NewParams.push_back(Type::getInt32Ty(F->getContext()));
127 FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(),
128 NewParams, false);
129 NewFn = cast<Function>(M->getOrInsertFunction(Name, NewFTy));
130 return true;
131 }
Mon P Wang6a490372008-06-25 08:15:39 +0000132 }
133 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000134 case 'b':
135 // This upgrades the name of the llvm.bswap intrinsic function to only use
136 // a single type name for overloading. We only care about the old format
137 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
138 // a '.' after 'bswap.'
139 if (Name.compare(5,6,"bswap.",6) == 0) {
140 std::string::size_type delim = Name.find('.',11);
141
142 if (delim != std::string::npos) {
143 // Construct the new name as 'llvm.bswap' + '.i*'
144 F->setName(Name.substr(0,10)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000145 NewFn = F;
146 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000147 }
148 }
149 break;
150
151 case 'c':
152 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
153 // correct return type, so we check for the name, and then check if the
154 // return type does not match the parameter type.
155 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
156 Name.compare(5,4,"ctlz",4) == 0 ||
157 Name.compare(5,4,"cttz",4) == 0) &&
158 FTy->getReturnType() != FTy->getParamType(0)) {
159 // We first need to change the name of the old (bad) intrinsic, because
160 // its type is incorrect, but we cannot overload that name. We
161 // arbitrarily unique it here allowing us to construct a correctly named
162 // and typed function below.
163 F->setName("");
164
165 // Now construct the new intrinsic with the correct name and type. We
166 // leave the old function around in order to query its type, whatever it
167 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000168 NewFn = cast<Function>(M->getOrInsertFunction(Name,
169 FTy->getParamType(0),
170 FTy->getParamType(0),
171 (Type *)0));
172 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000173 }
174 break;
175
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000176 case 'e':
177 // The old llvm.eh.selector.i32 is equivalent to the new llvm.eh.selector.
178 if (Name.compare("llvm.eh.selector.i32") == 0) {
179 F->setName("llvm.eh.selector");
180 NewFn = F;
181 return true;
182 }
183 // The old llvm.eh.typeid.for.i32 is equivalent to llvm.eh.typeid.for.
184 if (Name.compare("llvm.eh.typeid.for.i32") == 0) {
185 F->setName("llvm.eh.typeid.for");
186 NewFn = F;
187 return true;
188 }
189 // Convert the old llvm.eh.selector.i64 to a call to llvm.eh.selector.
190 if (Name.compare("llvm.eh.selector.i64") == 0) {
191 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_selector);
192 return true;
193 }
194 // Convert the old llvm.eh.typeid.for.i64 to a call to llvm.eh.typeid.for.
195 if (Name.compare("llvm.eh.typeid.for.i64") == 0) {
196 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_typeid_for);
197 return true;
198 }
199 break;
200
Mon P Wangc576ee92010-04-04 03:10:48 +0000201 case 'm': {
202 // This upgrades the llvm.memcpy, llvm.memmove, and llvm.memset to the
203 // new format that allows overloading the pointer for different address
204 // space (e.g., llvm.memcpy.i16 => llvm.memcpy.p0i8.p0i8.i16)
205 const char* NewFnName = NULL;
206 if (Name.compare(5,8,"memcpy.i",8) == 0) {
207 if (Name[13] == '8')
208 NewFnName = "llvm.memcpy.p0i8.p0i8.i8";
209 else if (Name.compare(13,2,"16") == 0)
210 NewFnName = "llvm.memcpy.p0i8.p0i8.i16";
211 else if (Name.compare(13,2,"32") == 0)
212 NewFnName = "llvm.memcpy.p0i8.p0i8.i32";
213 else if (Name.compare(13,2,"64") == 0)
214 NewFnName = "llvm.memcpy.p0i8.p0i8.i64";
215 } else if (Name.compare(5,9,"memmove.i",9) == 0) {
216 if (Name[14] == '8')
217 NewFnName = "llvm.memmove.p0i8.p0i8.i8";
218 else if (Name.compare(14,2,"16") == 0)
219 NewFnName = "llvm.memmove.p0i8.p0i8.i16";
220 else if (Name.compare(14,2,"32") == 0)
221 NewFnName = "llvm.memmove.p0i8.p0i8.i32";
222 else if (Name.compare(14,2,"64") == 0)
223 NewFnName = "llvm.memmove.p0i8.p0i8.i64";
224 }
225 else if (Name.compare(5,8,"memset.i",8) == 0) {
226 if (Name[13] == '8')
227 NewFnName = "llvm.memset.p0i8.i8";
228 else if (Name.compare(13,2,"16") == 0)
229 NewFnName = "llvm.memset.p0i8.i16";
230 else if (Name.compare(13,2,"32") == 0)
231 NewFnName = "llvm.memset.p0i8.i32";
232 else if (Name.compare(13,2,"64") == 0)
233 NewFnName = "llvm.memset.p0i8.i64";
234 }
235 if (NewFnName) {
Mon P Wangc576ee92010-04-04 03:10:48 +0000236 NewFn = cast<Function>(M->getOrInsertFunction(NewFnName,
237 FTy->getReturnType(),
238 FTy->getParamType(0),
239 FTy->getParamType(1),
240 FTy->getParamType(2),
241 FTy->getParamType(3),
242 Type::getInt1Ty(F->getContext()),
243 (Type *)0));
244 return true;
245 }
246 break;
247 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000248 case 'p':
249 // This upgrades the llvm.part.select overloaded intrinsic names to only
250 // use one type specifier in the name. We only care about the old format
251 // 'llvm.part.select.i*.i*', and solve as above with bswap.
252 if (Name.compare(5,12,"part.select.",12) == 0) {
253 std::string::size_type delim = Name.find('.',17);
254
255 if (delim != std::string::npos) {
256 // Construct a new name as 'llvm.part.select' + '.i*'
257 F->setName(Name.substr(0,16)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000258 NewFn = F;
259 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000260 }
261 break;
262 }
263
264 // This upgrades the llvm.part.set intrinsics similarly as above, however
265 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
266 // must match. There is an additional type specifier after these two
267 // matching types that we must retain when upgrading. Thus, we require
268 // finding 2 periods, not just one, after the intrinsic name.
269 if (Name.compare(5,9,"part.set.",9) == 0) {
270 std::string::size_type delim = Name.find('.',14);
271
272 if (delim != std::string::npos &&
273 Name.find('.',delim+1) != std::string::npos) {
274 // Construct a new name as 'llvm.part.select' + '.i*.i*'
275 F->setName(Name.substr(0,13)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000276 NewFn = F;
277 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000278 }
279 break;
280 }
281
282 break;
Anders Carlssonf924f342007-12-14 06:38:54 +0000283 case 'x':
284 // This fixes all MMX shift intrinsic instructions to take a
285 // v1i64 instead of a v2i32 as the second parameter.
286 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
287 (Name.compare(13,4,"psll", 4) == 0 ||
288 Name.compare(13,4,"psra", 4) == 0 ||
Evan Chengcdf22f22008-05-03 00:52:09 +0000289 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
Anders Carlssonf924f342007-12-14 06:38:54 +0000290
Owen Anderson155dccd82009-07-07 23:43:39 +0000291 const llvm::Type *VT =
Owen Anderson55f1c092009-08-13 21:58:54 +0000292 VectorType::get(IntegerType::get(FTy->getContext(), 64), 1);
Anders Carlssonf924f342007-12-14 06:38:54 +0000293
294 // We don't have to do anything if the parameter already has
295 // the correct type.
296 if (FTy->getParamType(1) == VT)
297 break;
298
299 // We first need to change the name of the old (bad) intrinsic, because
300 // its type is incorrect, but we cannot overload that name. We
301 // arbitrarily unique it here allowing us to construct a correctly named
302 // and typed function below.
303 F->setName("");
304
305 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
306
307 // Now construct the new intrinsic with the correct name and type. We
308 // leave the old function around in order to query its type, whatever it
309 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000310 NewFn = cast<Function>(M->getOrInsertFunction(Name,
311 FTy->getReturnType(),
312 FTy->getParamType(0),
313 VT,
314 (Type *)0));
315 return true;
Evan Cheng50659322008-05-24 00:08:39 +0000316 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
317 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
Evan Cheng2146270c2008-05-24 02:14:05 +0000318 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
319 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
320 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
321 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000322 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
323 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
324 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
Evan Cheng50659322008-05-24 00:08:39 +0000325 // Calls to these intrinsics are transformed into ShuffleVector's.
Evan Cheng0e179d02007-12-17 22:33:23 +0000326 NewFn = 0;
327 return true;
Eric Christopher6ad81672010-03-30 18:49:01 +0000328 } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) {
329 // Calls to these intrinsics are transformed into vector multiplies.
330 NewFn = 0;
331 return true;
Eric Christopher64831c62010-04-20 00:59:54 +0000332 } else if (Name.compare(5, 18, "x86.ssse3.palign.r", 18) == 0 ||
333 Name.compare(5, 22, "x86.ssse3.palign.r.128", 22) == 0) {
334 // Calls to these intrinsics are transformed into vector shuffles, shifts,
335 // or 0.
336 NewFn = 0;
337 return true;
Anders Carlssonf924f342007-12-14 06:38:54 +0000338 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000339
Anders Carlssonf924f342007-12-14 06:38:54 +0000340 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000341 }
342
343 // This may not belong here. This function is effectively being overloaded
344 // to both detect an intrinsic which needs upgrading, and to provide the
345 // upgraded form of the intrinsic. We should perhaps have two separate
346 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000347 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000348}
349
Evan Cheng0e179d02007-12-17 22:33:23 +0000350bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
351 NewFn = 0;
352 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000353
354 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000355 if (NewFn)
356 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000357 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000358 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000359 return Upgraded;
360}
361
Chandler Carruth7132e002007-08-04 01:51:18 +0000362// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
363// upgraded intrinsic. All argument and return casting must be provided in
364// order to seamlessly integrate with existing context.
365void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000366 Function *F = CI->getCalledFunction();
Owen Anderson55f1c092009-08-13 21:58:54 +0000367 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +0000368 ImmutableCallSite CS(CI);
369
Chandler Carruth7132e002007-08-04 01:51:18 +0000370 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000371
372 if (!NewFn) {
Bob Wilson9a511c02010-08-20 04:54:02 +0000373 // Get the Function's name.
374 const std::string& Name = F->getName();
375
376 // Upgrade ARM NEON intrinsics.
377 if (Name.compare(5, 9, "arm.neon.", 9) == 0) {
378 Instruction *NewI;
379 if (Name.compare(14, 7, "vmovls.", 7) == 0) {
380 NewI = new SExtInst(CI->getArgOperand(0), CI->getType(),
381 "upgraded." + CI->getName(), CI);
382 } else if (Name.compare(14, 7, "vmovlu.", 7) == 0) {
383 NewI = new ZExtInst(CI->getArgOperand(0), CI->getType(),
384 "upgraded." + CI->getName(), CI);
Bob Wilsond0c05482010-08-29 05:57:34 +0000385
386 } else if (Name.compare(14, 4, "vadd", 4) == 0 ||
387 Name.compare(14, 4, "vsub", 4) == 0) {
388 // Extend one (vaddw/vsubw) or both (vaddl/vsubl) operands.
389 Value *V0 = CI->getArgOperand(0);
390 Value *V1 = CI->getArgOperand(1);
391 if (Name.at(19) == 's') {
392 if (Name.at(18) == 'l')
393 V0 = new SExtInst(CI->getArgOperand(0), CI->getType(), "", CI);
394 V1 = new SExtInst(CI->getArgOperand(1), CI->getType(), "", CI);
395 } else {
396 assert(Name.at(19) == 'u' && "unexpected vadd/vsub intrinsic");
397 if (Name.at(18) == 'l')
398 V0 = new ZExtInst(CI->getArgOperand(0), CI->getType(), "", CI);
399 V1 = new ZExtInst(CI->getArgOperand(1), CI->getType(), "", CI);
400 }
401 if (Name.compare(14, 4, "vadd", 4) == 0)
402 NewI = BinaryOperator::CreateAdd(V0, V1,"upgraded."+CI->getName(),CI);
403 else
404 NewI = BinaryOperator::CreateSub(V0, V1,"upgraded."+CI->getName(),CI);
405
Bob Wilson4cd8a122010-08-30 20:02:30 +0000406 } else if (Name.compare(14, 6, "vmovn.", 6) == 0) {
407 NewI = new TruncInst(CI->getArgOperand(0), CI->getType(),
408 "upgraded." + CI->getName(), CI);
Bob Wilson9a511c02010-08-20 04:54:02 +0000409 } else {
410 llvm_unreachable("Unknown arm.neon function for CallInst upgrade.");
411 }
412 // Replace any uses of the old CallInst.
413 if (!CI->use_empty())
414 CI->replaceAllUsesWith(NewI);
415 CI->eraseFromParent();
416 return;
417 }
418
Evan Cheng50659322008-05-24 00:08:39 +0000419 bool isLoadH = false, isLoadL = false, isMovL = false;
Evan Cheng2146270c2008-05-24 02:14:05 +0000420 bool isMovSD = false, isShufPD = false;
421 bool isUnpckhPD = false, isUnpcklPD = false;
Evan Cheng91a2e562008-05-24 02:56:30 +0000422 bool isPunpckhQPD = false, isPunpcklQPD = false;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000423 if (F->getName() == "llvm.x86.sse2.loadh.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000424 isLoadH = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000425 else if (F->getName() == "llvm.x86.sse2.loadl.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000426 isLoadL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000427 else if (F->getName() == "llvm.x86.sse2.movl.dq")
Evan Cheng50659322008-05-24 00:08:39 +0000428 isMovL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000429 else if (F->getName() == "llvm.x86.sse2.movs.d")
Evan Cheng2146270c2008-05-24 02:14:05 +0000430 isMovSD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000431 else if (F->getName() == "llvm.x86.sse2.shuf.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000432 isShufPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000433 else if (F->getName() == "llvm.x86.sse2.unpckh.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000434 isUnpckhPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000435 else if (F->getName() == "llvm.x86.sse2.unpckl.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000436 isUnpcklPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000437 else if (F->getName() == "llvm.x86.sse2.punpckh.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000438 isPunpckhQPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000439 else if (F->getName() == "llvm.x86.sse2.punpckl.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000440 isPunpcklQPD = true;
Evan Cheng0e179d02007-12-17 22:33:23 +0000441
Evan Cheng2146270c2008-05-24 02:14:05 +0000442 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000443 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Evan Cheng50659322008-05-24 00:08:39 +0000444 std::vector<Constant*> Idxs;
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000445 Value *Op0 = CI->getArgOperand(0);
Evan Cheng2146270c2008-05-24 02:14:05 +0000446 ShuffleVectorInst *SI = NULL;
Evan Cheng50659322008-05-24 00:08:39 +0000447 if (isLoadH || isLoadL) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000448 Value *Op1 = UndefValue::get(Op0->getType());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000449 Value *Addr = new BitCastInst(CI->getArgOperand(1),
Duncan Sands9ed7b162009-10-06 15:40:36 +0000450 Type::getDoublePtrTy(C),
Evan Cheng50659322008-05-24 00:08:39 +0000451 "upgraded.", CI);
452 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
Owen Anderson55f1c092009-08-13 21:58:54 +0000453 Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000454 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
455
456 if (isLoadH) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000457 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
458 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng50659322008-05-24 00:08:39 +0000459 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000460 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
461 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng50659322008-05-24 00:08:39 +0000462 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000463 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000464 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng2146270c2008-05-24 02:14:05 +0000465 } else if (isMovL) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000466 Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000467 Idxs.push_back(Zero);
468 Idxs.push_back(Zero);
469 Idxs.push_back(Zero);
470 Idxs.push_back(Zero);
Owen Anderson4aa32952009-07-28 21:19:26 +0000471 Value *ZeroV = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000472
473 Idxs.clear();
Owen Anderson55f1c092009-08-13 21:58:54 +0000474 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4));
475 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5));
476 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
477 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Owen Anderson4aa32952009-07-28 21:19:26 +0000478 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000479 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
Evan Cheng91a2e562008-05-24 02:56:30 +0000480 } else if (isMovSD ||
481 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000482 Value *Op1 = CI->getArgOperand(1);
Evan Cheng2146270c2008-05-24 02:14:05 +0000483 if (isMovSD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000484 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
485 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng91a2e562008-05-24 02:56:30 +0000486 } else if (isUnpckhPD || isPunpckhQPD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000487 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
488 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Evan Cheng2146270c2008-05-24 02:14:05 +0000489 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000490 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
491 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng2146270c2008-05-24 02:14:05 +0000492 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000493 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000494 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
495 } else if (isShufPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000496 Value *Op1 = CI->getArgOperand(1);
Gabor Greif3e44ea12010-07-22 10:37:47 +0000497 unsigned MaskVal =
498 cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
Owen Anderson55f1c092009-08-13 21:58:54 +0000499 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
500 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
Owen Anderson155dccd82009-07-07 23:43:39 +0000501 ((MaskVal >> 1) & 1)+2));
Owen Anderson4aa32952009-07-28 21:19:26 +0000502 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000503 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng50659322008-05-24 00:08:39 +0000504 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000505
Evan Cheng2146270c2008-05-24 02:14:05 +0000506 assert(SI && "Unexpected!");
507
Evan Cheng0e179d02007-12-17 22:33:23 +0000508 // Handle any uses of the old CallInst.
509 if (!CI->use_empty())
510 // Replace all uses of the old call with the new cast which has the
511 // correct type.
512 CI->replaceAllUsesWith(SI);
513
514 // Clean up the old call now that it has been completely upgraded.
515 CI->eraseFromParent();
Eric Christopher6ad81672010-03-30 18:49:01 +0000516 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
517 // Upgrade this set of intrinsics into vector multiplies.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000518 Instruction *Mul = BinaryOperator::CreateMul(CI->getArgOperand(0),
519 CI->getArgOperand(1),
Eric Christopher6ad81672010-03-30 18:49:01 +0000520 CI->getName(),
521 CI);
522 // Fix up all the uses with our new multiply.
523 if (!CI->use_empty())
524 CI->replaceAllUsesWith(Mul);
525
526 // Remove upgraded multiply.
527 CI->eraseFromParent();
Eric Christopher64831c62010-04-20 00:59:54 +0000528 } else if (F->getName() == "llvm.x86.ssse3.palign.r") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000529 Value *Op1 = CI->getArgOperand(0);
530 Value *Op2 = CI->getArgOperand(1);
531 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000532 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
533 Value *Rep;
534 IRBuilder<> Builder(C);
535 Builder.SetInsertPoint(CI->getParent(), CI);
536
537 // If palignr is shifting the pair of input vectors less than 9 bytes,
538 // emit a shuffle instruction.
539 if (shiftVal <= 8) {
540 const Type *IntTy = Type::getInt32Ty(C);
541 const Type *EltTy = Type::getInt8Ty(C);
542 const Type *VecTy = VectorType::get(EltTy, 8);
543
544 Op2 = Builder.CreateBitCast(Op2, VecTy);
545 Op1 = Builder.CreateBitCast(Op1, VecTy);
546
547 llvm::SmallVector<llvm::Constant*, 8> Indices;
548 for (unsigned i = 0; i != 8; ++i)
549 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
550
551 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
552 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
553 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
554 }
555
556 // If palignr is shifting the pair of input vectors more than 8 but less
557 // than 16 bytes, emit a logical right shift of the destination.
558 else if (shiftVal < 16) {
559 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
560 const Type *EltTy = Type::getInt64Ty(C);
561 const Type *VecTy = VectorType::get(EltTy, 1);
562
563 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
564 Op2 = ConstantInt::get(VecTy, (shiftVal-8) * 8);
565
566 // create i32 constant
567 Function *I =
568 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_mmx_psrl_q);
569 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
570 }
571
572 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
573 else {
574 Rep = Constant::getNullValue(F->getReturnType());
575 }
576
577 // Replace any uses with our new instruction.
578 if (!CI->use_empty())
579 CI->replaceAllUsesWith(Rep);
580
581 // Remove upgraded instruction.
582 CI->eraseFromParent();
583
584 } else if (F->getName() == "llvm.x86.ssse3.palign.r.128") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000585 Value *Op1 = CI->getArgOperand(0);
586 Value *Op2 = CI->getArgOperand(1);
587 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000588 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
589 Value *Rep;
590 IRBuilder<> Builder(C);
591 Builder.SetInsertPoint(CI->getParent(), CI);
592
593 // If palignr is shifting the pair of input vectors less than 17 bytes,
594 // emit a shuffle instruction.
595 if (shiftVal <= 16) {
596 const Type *IntTy = Type::getInt32Ty(C);
597 const Type *EltTy = Type::getInt8Ty(C);
598 const Type *VecTy = VectorType::get(EltTy, 16);
599
600 Op2 = Builder.CreateBitCast(Op2, VecTy);
601 Op1 = Builder.CreateBitCast(Op1, VecTy);
602
603 llvm::SmallVector<llvm::Constant*, 16> Indices;
604 for (unsigned i = 0; i != 16; ++i)
605 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
606
607 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
608 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
609 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
610 }
611
612 // If palignr is shifting the pair of input vectors more than 16 but less
613 // than 32 bytes, emit a logical right shift of the destination.
614 else if (shiftVal < 32) {
615 const Type *EltTy = Type::getInt64Ty(C);
616 const Type *VecTy = VectorType::get(EltTy, 2);
617 const Type *IntTy = Type::getInt32Ty(C);
618
619 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
620 Op2 = ConstantInt::get(IntTy, (shiftVal-16) * 8);
621
622 // create i32 constant
623 Function *I =
624 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_sse2_psrl_dq);
625 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
626 }
627
628 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
629 else {
630 Rep = Constant::getNullValue(F->getReturnType());
631 }
632
633 // Replace any uses with our new instruction.
634 if (!CI->use_empty())
635 CI->replaceAllUsesWith(Rep);
636
637 // Remove upgraded instruction.
638 CI->eraseFromParent();
639
Evan Chenga8288f42007-12-18 01:04:25 +0000640 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000641 llvm_unreachable("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000642 }
643 return;
644 }
645
Gabor Greife9ecc682008-04-06 20:25:17 +0000646 switch (NewFn->getIntrinsicID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000647 default: llvm_unreachable("Unknown function for CallInst upgrade.");
Bob Wilsonedf722a2010-08-27 17:13:24 +0000648 case Intrinsic::arm_neon_vld1:
649 case Intrinsic::arm_neon_vld2:
650 case Intrinsic::arm_neon_vld3:
651 case Intrinsic::arm_neon_vld4:
652 case Intrinsic::arm_neon_vst1:
653 case Intrinsic::arm_neon_vst2:
654 case Intrinsic::arm_neon_vst3:
655 case Intrinsic::arm_neon_vst4:
656 case Intrinsic::arm_neon_vld2lane:
657 case Intrinsic::arm_neon_vld3lane:
658 case Intrinsic::arm_neon_vld4lane:
659 case Intrinsic::arm_neon_vst2lane:
660 case Intrinsic::arm_neon_vst3lane:
661 case Intrinsic::arm_neon_vst4lane: {
662 // Add a default alignment argument of 1.
663 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
664 Operands.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
665 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
666 CI->getName(), CI);
667 NewCI->setTailCall(CI->isTailCall());
668 NewCI->setCallingConv(CI->getCallingConv());
669
670 // Handle any uses of the old CallInst.
671 if (!CI->use_empty())
672 // Replace all uses of the old call with the new cast which has the
673 // correct type.
674 CI->replaceAllUsesWith(NewCI);
675
676 // Clean up the old call now that it has been completely upgraded.
677 CI->eraseFromParent();
678 break;
679 }
680
Anders Carlssonf924f342007-12-14 06:38:54 +0000681 case Intrinsic::x86_mmx_psll_d:
682 case Intrinsic::x86_mmx_psll_q:
683 case Intrinsic::x86_mmx_psll_w:
684 case Intrinsic::x86_mmx_psra_d:
685 case Intrinsic::x86_mmx_psra_w:
686 case Intrinsic::x86_mmx_psrl_d:
687 case Intrinsic::x86_mmx_psrl_q:
688 case Intrinsic::x86_mmx_psrl_w: {
Chris Lattner8a923e72008-03-12 17:45:29 +0000689 Value *Operands[2];
Anders Carlssonf924f342007-12-14 06:38:54 +0000690
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000691 Operands[0] = CI->getArgOperand(0);
Anders Carlssonf924f342007-12-14 06:38:54 +0000692
693 // Cast the second parameter to the correct type.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000694 BitCastInst *BC = new BitCastInst(CI->getArgOperand(1),
Anders Carlssonf924f342007-12-14 06:38:54 +0000695 NewFn->getFunctionType()->getParamType(1),
Evan Cheng50659322008-05-24 00:08:39 +0000696 "upgraded.", CI);
Chris Lattner8a923e72008-03-12 17:45:29 +0000697 Operands[1] = BC;
Anders Carlssonf924f342007-12-14 06:38:54 +0000698
699 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000700 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
701 "upgraded."+CI->getName(), CI);
Anders Carlssonf924f342007-12-14 06:38:54 +0000702 NewCI->setTailCall(CI->isTailCall());
703 NewCI->setCallingConv(CI->getCallingConv());
704
705 // Handle any uses of the old CallInst.
706 if (!CI->use_empty())
707 // Replace all uses of the old call with the new cast which has the
708 // correct type.
709 CI->replaceAllUsesWith(NewCI);
710
711 // Clean up the old call now that it has been completely upgraded.
712 CI->eraseFromParent();
713 break;
714 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000715 case Intrinsic::ctlz:
716 case Intrinsic::ctpop:
Gabor Greife9ecc682008-04-06 20:25:17 +0000717 case Intrinsic::cttz: {
Gabor Greife5406532010-06-23 08:45:32 +0000718 // Build a small vector of the original arguments.
719 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Chandler Carruth7132e002007-08-04 01:51:18 +0000720
721 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000722 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
723 "upgraded."+CI->getName(), CI);
Chandler Carruth7132e002007-08-04 01:51:18 +0000724 NewCI->setTailCall(CI->isTailCall());
725 NewCI->setCallingConv(CI->getCallingConv());
726
727 // Handle any uses of the old CallInst.
728 if (!CI->use_empty()) {
729 // Check for sign extend parameter attributes on the return values.
Devang Patel4c758ea2008-09-25 21:00:45 +0000730 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
731 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
Chandler Carruth7132e002007-08-04 01:51:18 +0000732
733 // Construct an appropriate cast from the new return type to the old.
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000734 CastInst *RetCast = CastInst::Create(
Chandler Carruth7132e002007-08-04 01:51:18 +0000735 CastInst::getCastOpcode(NewCI, SrcSExt,
736 F->getReturnType(),
737 DestSExt),
738 NewCI, F->getReturnType(),
739 NewCI->getName(), CI);
740 NewCI->moveBefore(RetCast);
741
742 // Replace all uses of the old call with the new cast which has the
743 // correct type.
744 CI->replaceAllUsesWith(RetCast);
745 }
746
747 // Clean up the old call now that it has been completely upgraded.
748 CI->eraseFromParent();
Gabor Greife9ecc682008-04-06 20:25:17 +0000749 }
750 break;
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000751 case Intrinsic::eh_selector:
752 case Intrinsic::eh_typeid_for: {
753 // Only the return type changed.
Gabor Greife5406532010-06-23 08:45:32 +0000754 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000755 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
756 "upgraded." + CI->getName(), CI);
757 NewCI->setTailCall(CI->isTailCall());
758 NewCI->setCallingConv(CI->getCallingConv());
759
760 // Handle any uses of the old CallInst.
761 if (!CI->use_empty()) {
762 // Construct an appropriate cast from the new return type to the old.
763 CastInst *RetCast =
764 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
765 F->getReturnType(), true),
766 NewCI, F->getReturnType(), NewCI->getName(), CI);
767 CI->replaceAllUsesWith(RetCast);
768 }
769 CI->eraseFromParent();
770 }
771 break;
Mon P Wangc576ee92010-04-04 03:10:48 +0000772 case Intrinsic::memcpy:
773 case Intrinsic::memmove:
774 case Intrinsic::memset: {
775 // Add isVolatile
776 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(CI->getContext());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000777 Value *Operands[5] = { CI->getArgOperand(0), CI->getArgOperand(1),
778 CI->getArgOperand(2), CI->getArgOperand(3),
Mon P Wangc576ee92010-04-04 03:10:48 +0000779 llvm::ConstantInt::get(I1Ty, 0) };
780 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+5,
781 CI->getName(), CI);
782 NewCI->setTailCall(CI->isTailCall());
783 NewCI->setCallingConv(CI->getCallingConv());
784 // Handle any uses of the old CallInst.
785 if (!CI->use_empty())
786 // Replace all uses of the old call with the new cast which has the
787 // correct type.
788 CI->replaceAllUsesWith(NewCI);
789
790 // Clean up the old call now that it has been completely upgraded.
791 CI->eraseFromParent();
792 break;
793 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000794 }
795}
796
797// This tests each Function to determine if it needs upgrading. When we find
798// one we are interested in, we then upgrade all calls to reflect the new
799// function.
800void llvm::UpgradeCallsToIntrinsic(Function* F) {
801 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
802
803 // Upgrade the function and check if it is a totaly new function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000804 Function* NewFn;
805 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000806 if (NewFn != F) {
807 // Replace all uses to the old function with the new one if necessary.
808 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
809 UI != UE; ) {
810 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
811 UpgradeIntrinsicCall(CI, NewFn);
812 }
813 // Remove old function, no longer used, from the module.
814 F->eraseFromParent();
815 }
816 }
817}
Devang Patel80ae3492009-08-28 23:24:31 +0000818
Victor Hernandezc2044a12010-01-05 21:13:46 +0000819/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
820/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
821/// strips that use.
Devang Patel80ae3492009-08-28 23:24:31 +0000822void llvm::CheckDebugInfoIntrinsics(Module *M) {
823
824
825 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000826 while (!FuncStart->use_empty()) {
827 CallInst *CI = cast<CallInst>(FuncStart->use_back());
828 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000829 }
Devang Patelbe94f232010-01-05 01:10:40 +0000830 FuncStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000831 }
Devang Patelbe94f232010-01-05 01:10:40 +0000832
Devang Patel80ae3492009-08-28 23:24:31 +0000833 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000834 while (!StopPoint->use_empty()) {
835 CallInst *CI = cast<CallInst>(StopPoint->use_back());
836 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000837 }
Devang Patelbe94f232010-01-05 01:10:40 +0000838 StopPoint->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000839 }
840
841 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000842 while (!RegionStart->use_empty()) {
843 CallInst *CI = cast<CallInst>(RegionStart->use_back());
844 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000845 }
Devang Patelbe94f232010-01-05 01:10:40 +0000846 RegionStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000847 }
848
849 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000850 while (!RegionEnd->use_empty()) {
851 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
852 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000853 }
Devang Patelbe94f232010-01-05 01:10:40 +0000854 RegionEnd->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000855 }
856
857 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
858 if (!Declare->use_empty()) {
859 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000860 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
861 !isa<MDNode>(DDI->getArgOperand(1))) {
Devang Patel80ae3492009-08-28 23:24:31 +0000862 while (!Declare->use_empty()) {
863 CallInst *CI = cast<CallInst>(Declare->use_back());
864 CI->eraseFromParent();
865 }
866 Declare->eraseFromParent();
867 }
868 }
869 }
870}