blob: 4f4daeba4d37042847dc717a162c88dcf3532dc2 [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 }
81 }
82 break;
Chandler Carruth7132e002007-08-04 01:51:18 +000083 case 'b':
84 // This upgrades the name of the llvm.bswap intrinsic function to only use
85 // a single type name for overloading. We only care about the old format
86 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
87 // a '.' after 'bswap.'
88 if (Name.compare(5,6,"bswap.",6) == 0) {
89 std::string::size_type delim = Name.find('.',11);
90
91 if (delim != std::string::npos) {
92 // Construct the new name as 'llvm.bswap' + '.i*'
93 F->setName(Name.substr(0,10)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +000094 NewFn = F;
95 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +000096 }
97 }
98 break;
99
100 case 'c':
101 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
102 // correct return type, so we check for the name, and then check if the
103 // return type does not match the parameter type.
104 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
105 Name.compare(5,4,"ctlz",4) == 0 ||
106 Name.compare(5,4,"cttz",4) == 0) &&
107 FTy->getReturnType() != FTy->getParamType(0)) {
108 // We first need to change the name of the old (bad) intrinsic, because
109 // its type is incorrect, but we cannot overload that name. We
110 // arbitrarily unique it here allowing us to construct a correctly named
111 // and typed function below.
112 F->setName("");
113
114 // Now construct the new intrinsic with the correct name and type. We
115 // leave the old function around in order to query its type, whatever it
116 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000117 NewFn = cast<Function>(M->getOrInsertFunction(Name,
118 FTy->getParamType(0),
119 FTy->getParamType(0),
120 (Type *)0));
121 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000122 }
123 break;
124
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000125 case 'e':
126 // The old llvm.eh.selector.i32 is equivalent to the new llvm.eh.selector.
127 if (Name.compare("llvm.eh.selector.i32") == 0) {
128 F->setName("llvm.eh.selector");
129 NewFn = F;
130 return true;
131 }
132 // The old llvm.eh.typeid.for.i32 is equivalent to llvm.eh.typeid.for.
133 if (Name.compare("llvm.eh.typeid.for.i32") == 0) {
134 F->setName("llvm.eh.typeid.for");
135 NewFn = F;
136 return true;
137 }
138 // Convert the old llvm.eh.selector.i64 to a call to llvm.eh.selector.
139 if (Name.compare("llvm.eh.selector.i64") == 0) {
140 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_selector);
141 return true;
142 }
143 // Convert the old llvm.eh.typeid.for.i64 to a call to llvm.eh.typeid.for.
144 if (Name.compare("llvm.eh.typeid.for.i64") == 0) {
145 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_typeid_for);
146 return true;
147 }
148 break;
149
Mon P Wangc576ee92010-04-04 03:10:48 +0000150 case 'm': {
151 // This upgrades the llvm.memcpy, llvm.memmove, and llvm.memset to the
152 // new format that allows overloading the pointer for different address
153 // space (e.g., llvm.memcpy.i16 => llvm.memcpy.p0i8.p0i8.i16)
154 const char* NewFnName = NULL;
155 if (Name.compare(5,8,"memcpy.i",8) == 0) {
156 if (Name[13] == '8')
157 NewFnName = "llvm.memcpy.p0i8.p0i8.i8";
158 else if (Name.compare(13,2,"16") == 0)
159 NewFnName = "llvm.memcpy.p0i8.p0i8.i16";
160 else if (Name.compare(13,2,"32") == 0)
161 NewFnName = "llvm.memcpy.p0i8.p0i8.i32";
162 else if (Name.compare(13,2,"64") == 0)
163 NewFnName = "llvm.memcpy.p0i8.p0i8.i64";
164 } else if (Name.compare(5,9,"memmove.i",9) == 0) {
165 if (Name[14] == '8')
166 NewFnName = "llvm.memmove.p0i8.p0i8.i8";
167 else if (Name.compare(14,2,"16") == 0)
168 NewFnName = "llvm.memmove.p0i8.p0i8.i16";
169 else if (Name.compare(14,2,"32") == 0)
170 NewFnName = "llvm.memmove.p0i8.p0i8.i32";
171 else if (Name.compare(14,2,"64") == 0)
172 NewFnName = "llvm.memmove.p0i8.p0i8.i64";
173 }
174 else if (Name.compare(5,8,"memset.i",8) == 0) {
175 if (Name[13] == '8')
176 NewFnName = "llvm.memset.p0i8.i8";
177 else if (Name.compare(13,2,"16") == 0)
178 NewFnName = "llvm.memset.p0i8.i16";
179 else if (Name.compare(13,2,"32") == 0)
180 NewFnName = "llvm.memset.p0i8.i32";
181 else if (Name.compare(13,2,"64") == 0)
182 NewFnName = "llvm.memset.p0i8.i64";
183 }
184 if (NewFnName) {
185 const FunctionType *FTy = F->getFunctionType();
186 NewFn = cast<Function>(M->getOrInsertFunction(NewFnName,
187 FTy->getReturnType(),
188 FTy->getParamType(0),
189 FTy->getParamType(1),
190 FTy->getParamType(2),
191 FTy->getParamType(3),
192 Type::getInt1Ty(F->getContext()),
193 (Type *)0));
194 return true;
195 }
196 break;
197 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000198 case 'p':
199 // This upgrades the llvm.part.select overloaded intrinsic names to only
200 // use one type specifier in the name. We only care about the old format
201 // 'llvm.part.select.i*.i*', and solve as above with bswap.
202 if (Name.compare(5,12,"part.select.",12) == 0) {
203 std::string::size_type delim = Name.find('.',17);
204
205 if (delim != std::string::npos) {
206 // Construct a new name as 'llvm.part.select' + '.i*'
207 F->setName(Name.substr(0,16)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000208 NewFn = F;
209 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000210 }
211 break;
212 }
213
214 // This upgrades the llvm.part.set intrinsics similarly as above, however
215 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
216 // must match. There is an additional type specifier after these two
217 // matching types that we must retain when upgrading. Thus, we require
218 // finding 2 periods, not just one, after the intrinsic name.
219 if (Name.compare(5,9,"part.set.",9) == 0) {
220 std::string::size_type delim = Name.find('.',14);
221
222 if (delim != std::string::npos &&
223 Name.find('.',delim+1) != std::string::npos) {
224 // Construct a new name as 'llvm.part.select' + '.i*.i*'
225 F->setName(Name.substr(0,13)+Name.substr(delim));
Evan Cheng0e179d02007-12-17 22:33:23 +0000226 NewFn = F;
227 return true;
Chandler Carruth7132e002007-08-04 01:51:18 +0000228 }
229 break;
230 }
231
232 break;
Anders Carlssonf924f342007-12-14 06:38:54 +0000233 case 'x':
234 // This fixes all MMX shift intrinsic instructions to take a
235 // v1i64 instead of a v2i32 as the second parameter.
236 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
237 (Name.compare(13,4,"psll", 4) == 0 ||
238 Name.compare(13,4,"psra", 4) == 0 ||
Evan Chengcdf22f22008-05-03 00:52:09 +0000239 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
Anders Carlssonf924f342007-12-14 06:38:54 +0000240
Owen Anderson155dccd82009-07-07 23:43:39 +0000241 const llvm::Type *VT =
Owen Anderson55f1c092009-08-13 21:58:54 +0000242 VectorType::get(IntegerType::get(FTy->getContext(), 64), 1);
Anders Carlssonf924f342007-12-14 06:38:54 +0000243
244 // We don't have to do anything if the parameter already has
245 // the correct type.
246 if (FTy->getParamType(1) == VT)
247 break;
248
249 // We first need to change the name of the old (bad) intrinsic, because
250 // its type is incorrect, but we cannot overload that name. We
251 // arbitrarily unique it here allowing us to construct a correctly named
252 // and typed function below.
253 F->setName("");
254
255 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
256
257 // Now construct the new intrinsic with the correct name and type. We
258 // leave the old function around in order to query its type, whatever it
259 // may be, and correctly convert up to the new type.
Evan Cheng0e179d02007-12-17 22:33:23 +0000260 NewFn = cast<Function>(M->getOrInsertFunction(Name,
261 FTy->getReturnType(),
262 FTy->getParamType(0),
263 VT,
264 (Type *)0));
265 return true;
Evan Cheng50659322008-05-24 00:08:39 +0000266 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
267 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
Evan Cheng2146270c2008-05-24 02:14:05 +0000268 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
269 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
270 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
271 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000272 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
273 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
274 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
Evan Cheng50659322008-05-24 00:08:39 +0000275 // Calls to these intrinsics are transformed into ShuffleVector's.
Evan Cheng0e179d02007-12-17 22:33:23 +0000276 NewFn = 0;
277 return true;
Eric Christopher6ad81672010-03-30 18:49:01 +0000278 } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) {
279 // Calls to these intrinsics are transformed into vector multiplies.
280 NewFn = 0;
281 return true;
Eric Christopher64831c62010-04-20 00:59:54 +0000282 } else if (Name.compare(5, 18, "x86.ssse3.palign.r", 18) == 0 ||
283 Name.compare(5, 22, "x86.ssse3.palign.r.128", 22) == 0) {
284 // Calls to these intrinsics are transformed into vector shuffles, shifts,
285 // or 0.
286 NewFn = 0;
287 return true;
Anders Carlssonf924f342007-12-14 06:38:54 +0000288 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000289
Anders Carlssonf924f342007-12-14 06:38:54 +0000290 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000291 }
292
293 // This may not belong here. This function is effectively being overloaded
294 // to both detect an intrinsic which needs upgrading, and to provide the
295 // upgraded form of the intrinsic. We should perhaps have two separate
296 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000297 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000298}
299
Evan Cheng0e179d02007-12-17 22:33:23 +0000300bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
301 NewFn = 0;
302 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000303
304 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000305 if (NewFn)
306 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000307 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000308 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000309 return Upgraded;
310}
311
Chandler Carruth7132e002007-08-04 01:51:18 +0000312// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
313// upgraded intrinsic. All argument and return casting must be provided in
314// order to seamlessly integrate with existing context.
315void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000316 Function *F = CI->getCalledFunction();
Owen Anderson55f1c092009-08-13 21:58:54 +0000317 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +0000318 ImmutableCallSite CS(CI);
319
Chandler Carruth7132e002007-08-04 01:51:18 +0000320 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000321
322 if (!NewFn) {
Evan Cheng50659322008-05-24 00:08:39 +0000323 bool isLoadH = false, isLoadL = false, isMovL = false;
Evan Cheng2146270c2008-05-24 02:14:05 +0000324 bool isMovSD = false, isShufPD = false;
325 bool isUnpckhPD = false, isUnpcklPD = false;
Evan Cheng91a2e562008-05-24 02:56:30 +0000326 bool isPunpckhQPD = false, isPunpcklQPD = false;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000327 if (F->getName() == "llvm.x86.sse2.loadh.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000328 isLoadH = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000329 else if (F->getName() == "llvm.x86.sse2.loadl.pd")
Evan Cheng50659322008-05-24 00:08:39 +0000330 isLoadL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000331 else if (F->getName() == "llvm.x86.sse2.movl.dq")
Evan Cheng50659322008-05-24 00:08:39 +0000332 isMovL = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000333 else if (F->getName() == "llvm.x86.sse2.movs.d")
Evan Cheng2146270c2008-05-24 02:14:05 +0000334 isMovSD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000335 else if (F->getName() == "llvm.x86.sse2.shuf.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000336 isShufPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000337 else if (F->getName() == "llvm.x86.sse2.unpckh.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000338 isUnpckhPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000339 else if (F->getName() == "llvm.x86.sse2.unpckl.pd")
Evan Cheng2146270c2008-05-24 02:14:05 +0000340 isUnpcklPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000341 else if (F->getName() == "llvm.x86.sse2.punpckh.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000342 isPunpckhQPD = true;
Daniel Dunbare03eecb2009-07-25 23:55:21 +0000343 else if (F->getName() == "llvm.x86.sse2.punpckl.qdq")
Evan Cheng91a2e562008-05-24 02:56:30 +0000344 isPunpcklQPD = true;
Evan Cheng0e179d02007-12-17 22:33:23 +0000345
Evan Cheng2146270c2008-05-24 02:14:05 +0000346 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
Evan Cheng91a2e562008-05-24 02:56:30 +0000347 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Evan Cheng50659322008-05-24 00:08:39 +0000348 std::vector<Constant*> Idxs;
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000349 Value *Op0 = CI->getArgOperand(0);
Evan Cheng2146270c2008-05-24 02:14:05 +0000350 ShuffleVectorInst *SI = NULL;
Evan Cheng50659322008-05-24 00:08:39 +0000351 if (isLoadH || isLoadL) {
Owen Andersonb292b8c2009-07-30 23:03:37 +0000352 Value *Op1 = UndefValue::get(Op0->getType());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000353 Value *Addr = new BitCastInst(CI->getArgOperand(1),
Duncan Sands9ed7b162009-10-06 15:40:36 +0000354 Type::getDoublePtrTy(C),
Evan Cheng50659322008-05-24 00:08:39 +0000355 "upgraded.", CI);
356 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
Owen Anderson55f1c092009-08-13 21:58:54 +0000357 Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000358 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
359
360 if (isLoadH) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000361 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
362 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng50659322008-05-24 00:08:39 +0000363 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000364 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
365 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng50659322008-05-24 00:08:39 +0000366 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000367 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000368 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng2146270c2008-05-24 02:14:05 +0000369 } else if (isMovL) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000370 Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0);
Evan Cheng50659322008-05-24 00:08:39 +0000371 Idxs.push_back(Zero);
372 Idxs.push_back(Zero);
373 Idxs.push_back(Zero);
374 Idxs.push_back(Zero);
Owen Anderson4aa32952009-07-28 21:19:26 +0000375 Value *ZeroV = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000376
377 Idxs.clear();
Owen Anderson55f1c092009-08-13 21:58:54 +0000378 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4));
379 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5));
380 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
381 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Owen Anderson4aa32952009-07-28 21:19:26 +0000382 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng50659322008-05-24 00:08:39 +0000383 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
Evan Cheng91a2e562008-05-24 02:56:30 +0000384 } else if (isMovSD ||
385 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000386 Value *Op1 = CI->getArgOperand(1);
Evan Cheng2146270c2008-05-24 02:14:05 +0000387 if (isMovSD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000388 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
389 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
Evan Cheng91a2e562008-05-24 02:56:30 +0000390 } else if (isUnpckhPD || isPunpckhQPD) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000391 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
392 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
Evan Cheng2146270c2008-05-24 02:14:05 +0000393 } else {
Owen Anderson55f1c092009-08-13 21:58:54 +0000394 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
395 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
Evan Cheng2146270c2008-05-24 02:14:05 +0000396 }
Owen Anderson4aa32952009-07-28 21:19:26 +0000397 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000398 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
399 } else if (isShufPD) {
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000400 Value *Op1 = CI->getArgOperand(1);
Gabor Greif3e44ea12010-07-22 10:37:47 +0000401 unsigned MaskVal =
402 cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
Owen Anderson55f1c092009-08-13 21:58:54 +0000403 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
404 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
Owen Anderson155dccd82009-07-07 23:43:39 +0000405 ((MaskVal >> 1) & 1)+2));
Owen Anderson4aa32952009-07-28 21:19:26 +0000406 Value *Mask = ConstantVector::get(Idxs);
Evan Cheng2146270c2008-05-24 02:14:05 +0000407 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
Evan Cheng50659322008-05-24 00:08:39 +0000408 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000409
Evan Cheng2146270c2008-05-24 02:14:05 +0000410 assert(SI && "Unexpected!");
411
Evan Cheng0e179d02007-12-17 22:33:23 +0000412 // Handle any uses of the old CallInst.
413 if (!CI->use_empty())
414 // Replace all uses of the old call with the new cast which has the
415 // correct type.
416 CI->replaceAllUsesWith(SI);
417
418 // Clean up the old call now that it has been completely upgraded.
419 CI->eraseFromParent();
Eric Christopher6ad81672010-03-30 18:49:01 +0000420 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
421 // Upgrade this set of intrinsics into vector multiplies.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000422 Instruction *Mul = BinaryOperator::CreateMul(CI->getArgOperand(0),
423 CI->getArgOperand(1),
Eric Christopher6ad81672010-03-30 18:49:01 +0000424 CI->getName(),
425 CI);
426 // Fix up all the uses with our new multiply.
427 if (!CI->use_empty())
428 CI->replaceAllUsesWith(Mul);
429
430 // Remove upgraded multiply.
431 CI->eraseFromParent();
Eric Christopher64831c62010-04-20 00:59:54 +0000432 } else if (F->getName() == "llvm.x86.ssse3.palign.r") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000433 Value *Op1 = CI->getArgOperand(0);
434 Value *Op2 = CI->getArgOperand(1);
435 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000436 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
437 Value *Rep;
438 IRBuilder<> Builder(C);
439 Builder.SetInsertPoint(CI->getParent(), CI);
440
441 // If palignr is shifting the pair of input vectors less than 9 bytes,
442 // emit a shuffle instruction.
443 if (shiftVal <= 8) {
444 const Type *IntTy = Type::getInt32Ty(C);
445 const Type *EltTy = Type::getInt8Ty(C);
446 const Type *VecTy = VectorType::get(EltTy, 8);
447
448 Op2 = Builder.CreateBitCast(Op2, VecTy);
449 Op1 = Builder.CreateBitCast(Op1, VecTy);
450
451 llvm::SmallVector<llvm::Constant*, 8> Indices;
452 for (unsigned i = 0; i != 8; ++i)
453 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
454
455 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
456 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
457 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
458 }
459
460 // If palignr is shifting the pair of input vectors more than 8 but less
461 // than 16 bytes, emit a logical right shift of the destination.
462 else if (shiftVal < 16) {
463 // MMX has these as 1 x i64 vectors for some odd optimization reasons.
464 const Type *EltTy = Type::getInt64Ty(C);
465 const Type *VecTy = VectorType::get(EltTy, 1);
466
467 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
468 Op2 = ConstantInt::get(VecTy, (shiftVal-8) * 8);
469
470 // create i32 constant
471 Function *I =
472 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_mmx_psrl_q);
473 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
474 }
475
476 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
477 else {
478 Rep = Constant::getNullValue(F->getReturnType());
479 }
480
481 // Replace any uses with our new instruction.
482 if (!CI->use_empty())
483 CI->replaceAllUsesWith(Rep);
484
485 // Remove upgraded instruction.
486 CI->eraseFromParent();
487
488 } else if (F->getName() == "llvm.x86.ssse3.palign.r.128") {
Gabor Greifeab748d2010-06-29 16:17:26 +0000489 Value *Op1 = CI->getArgOperand(0);
490 Value *Op2 = CI->getArgOperand(1);
491 Value *Op3 = CI->getArgOperand(2);
Eric Christopher64831c62010-04-20 00:59:54 +0000492 unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue();
493 Value *Rep;
494 IRBuilder<> Builder(C);
495 Builder.SetInsertPoint(CI->getParent(), CI);
496
497 // If palignr is shifting the pair of input vectors less than 17 bytes,
498 // emit a shuffle instruction.
499 if (shiftVal <= 16) {
500 const Type *IntTy = Type::getInt32Ty(C);
501 const Type *EltTy = Type::getInt8Ty(C);
502 const Type *VecTy = VectorType::get(EltTy, 16);
503
504 Op2 = Builder.CreateBitCast(Op2, VecTy);
505 Op1 = Builder.CreateBitCast(Op1, VecTy);
506
507 llvm::SmallVector<llvm::Constant*, 16> Indices;
508 for (unsigned i = 0; i != 16; ++i)
509 Indices.push_back(ConstantInt::get(IntTy, shiftVal + i));
510
511 Value *SV = ConstantVector::get(Indices.begin(), Indices.size());
512 Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr");
513 Rep = Builder.CreateBitCast(Rep, F->getReturnType());
514 }
515
516 // If palignr is shifting the pair of input vectors more than 16 but less
517 // than 32 bytes, emit a logical right shift of the destination.
518 else if (shiftVal < 32) {
519 const Type *EltTy = Type::getInt64Ty(C);
520 const Type *VecTy = VectorType::get(EltTy, 2);
521 const Type *IntTy = Type::getInt32Ty(C);
522
523 Op1 = Builder.CreateBitCast(Op1, VecTy, "cast");
524 Op2 = ConstantInt::get(IntTy, (shiftVal-16) * 8);
525
526 // create i32 constant
527 Function *I =
528 Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_sse2_psrl_dq);
529 Rep = Builder.CreateCall2(I, Op1, Op2, "palignr");
530 }
531
532 // If palignr is shifting the pair of vectors more than 32 bytes, emit zero.
533 else {
534 Rep = Constant::getNullValue(F->getReturnType());
535 }
536
537 // Replace any uses with our new instruction.
538 if (!CI->use_empty())
539 CI->replaceAllUsesWith(Rep);
540
541 // Remove upgraded instruction.
542 CI->eraseFromParent();
543
Evan Chenga8288f42007-12-18 01:04:25 +0000544 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000545 llvm_unreachable("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000546 }
547 return;
548 }
549
Gabor Greife9ecc682008-04-06 20:25:17 +0000550 switch (NewFn->getIntrinsicID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000551 default: llvm_unreachable("Unknown function for CallInst upgrade.");
Anders Carlssonf924f342007-12-14 06:38:54 +0000552 case Intrinsic::x86_mmx_psll_d:
553 case Intrinsic::x86_mmx_psll_q:
554 case Intrinsic::x86_mmx_psll_w:
555 case Intrinsic::x86_mmx_psra_d:
556 case Intrinsic::x86_mmx_psra_w:
557 case Intrinsic::x86_mmx_psrl_d:
558 case Intrinsic::x86_mmx_psrl_q:
559 case Intrinsic::x86_mmx_psrl_w: {
Chris Lattner8a923e72008-03-12 17:45:29 +0000560 Value *Operands[2];
Anders Carlssonf924f342007-12-14 06:38:54 +0000561
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000562 Operands[0] = CI->getArgOperand(0);
Anders Carlssonf924f342007-12-14 06:38:54 +0000563
564 // Cast the second parameter to the correct type.
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000565 BitCastInst *BC = new BitCastInst(CI->getArgOperand(1),
Anders Carlssonf924f342007-12-14 06:38:54 +0000566 NewFn->getFunctionType()->getParamType(1),
Evan Cheng50659322008-05-24 00:08:39 +0000567 "upgraded.", CI);
Chris Lattner8a923e72008-03-12 17:45:29 +0000568 Operands[1] = BC;
Anders Carlssonf924f342007-12-14 06:38:54 +0000569
570 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000571 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
572 "upgraded."+CI->getName(), CI);
Anders Carlssonf924f342007-12-14 06:38:54 +0000573 NewCI->setTailCall(CI->isTailCall());
574 NewCI->setCallingConv(CI->getCallingConv());
575
576 // Handle any uses of the old CallInst.
577 if (!CI->use_empty())
578 // Replace all uses of the old call with the new cast which has the
579 // correct type.
580 CI->replaceAllUsesWith(NewCI);
581
582 // Clean up the old call now that it has been completely upgraded.
583 CI->eraseFromParent();
584 break;
585 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000586 case Intrinsic::ctlz:
587 case Intrinsic::ctpop:
Gabor Greife9ecc682008-04-06 20:25:17 +0000588 case Intrinsic::cttz: {
Gabor Greife5406532010-06-23 08:45:32 +0000589 // Build a small vector of the original arguments.
590 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Chandler Carruth7132e002007-08-04 01:51:18 +0000591
592 // Construct a new CallInst
Gabor Greife9ecc682008-04-06 20:25:17 +0000593 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
594 "upgraded."+CI->getName(), CI);
Chandler Carruth7132e002007-08-04 01:51:18 +0000595 NewCI->setTailCall(CI->isTailCall());
596 NewCI->setCallingConv(CI->getCallingConv());
597
598 // Handle any uses of the old CallInst.
599 if (!CI->use_empty()) {
600 // Check for sign extend parameter attributes on the return values.
Devang Patel4c758ea2008-09-25 21:00:45 +0000601 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
602 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
Chandler Carruth7132e002007-08-04 01:51:18 +0000603
604 // Construct an appropriate cast from the new return type to the old.
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000605 CastInst *RetCast = CastInst::Create(
Chandler Carruth7132e002007-08-04 01:51:18 +0000606 CastInst::getCastOpcode(NewCI, SrcSExt,
607 F->getReturnType(),
608 DestSExt),
609 NewCI, F->getReturnType(),
610 NewCI->getName(), CI);
611 NewCI->moveBefore(RetCast);
612
613 // Replace all uses of the old call with the new cast which has the
614 // correct type.
615 CI->replaceAllUsesWith(RetCast);
616 }
617
618 // Clean up the old call now that it has been completely upgraded.
619 CI->eraseFromParent();
Gabor Greife9ecc682008-04-06 20:25:17 +0000620 }
621 break;
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000622 case Intrinsic::eh_selector:
623 case Intrinsic::eh_typeid_for: {
624 // Only the return type changed.
Gabor Greife5406532010-06-23 08:45:32 +0000625 SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end());
Duncan Sands8e6ccb62009-10-14 16:11:37 +0000626 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
627 "upgraded." + CI->getName(), CI);
628 NewCI->setTailCall(CI->isTailCall());
629 NewCI->setCallingConv(CI->getCallingConv());
630
631 // Handle any uses of the old CallInst.
632 if (!CI->use_empty()) {
633 // Construct an appropriate cast from the new return type to the old.
634 CastInst *RetCast =
635 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
636 F->getReturnType(), true),
637 NewCI, F->getReturnType(), NewCI->getName(), CI);
638 CI->replaceAllUsesWith(RetCast);
639 }
640 CI->eraseFromParent();
641 }
642 break;
Mon P Wangc576ee92010-04-04 03:10:48 +0000643 case Intrinsic::memcpy:
644 case Intrinsic::memmove:
645 case Intrinsic::memset: {
646 // Add isVolatile
647 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(CI->getContext());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000648 Value *Operands[5] = { CI->getArgOperand(0), CI->getArgOperand(1),
649 CI->getArgOperand(2), CI->getArgOperand(3),
Mon P Wangc576ee92010-04-04 03:10:48 +0000650 llvm::ConstantInt::get(I1Ty, 0) };
651 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+5,
652 CI->getName(), CI);
653 NewCI->setTailCall(CI->isTailCall());
654 NewCI->setCallingConv(CI->getCallingConv());
655 // Handle any uses of the old CallInst.
656 if (!CI->use_empty())
657 // Replace all uses of the old call with the new cast which has the
658 // correct type.
659 CI->replaceAllUsesWith(NewCI);
660
661 // Clean up the old call now that it has been completely upgraded.
662 CI->eraseFromParent();
663 break;
664 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000665 }
666}
667
668// This tests each Function to determine if it needs upgrading. When we find
669// one we are interested in, we then upgrade all calls to reflect the new
670// function.
671void llvm::UpgradeCallsToIntrinsic(Function* F) {
672 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
673
674 // Upgrade the function and check if it is a totaly new function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000675 Function* NewFn;
676 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000677 if (NewFn != F) {
678 // Replace all uses to the old function with the new one if necessary.
679 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
680 UI != UE; ) {
681 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
682 UpgradeIntrinsicCall(CI, NewFn);
683 }
684 // Remove old function, no longer used, from the module.
685 F->eraseFromParent();
686 }
687 }
688}
Devang Patel80ae3492009-08-28 23:24:31 +0000689
Victor Hernandezc2044a12010-01-05 21:13:46 +0000690/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
691/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
692/// strips that use.
Devang Patel80ae3492009-08-28 23:24:31 +0000693void llvm::CheckDebugInfoIntrinsics(Module *M) {
694
695
696 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000697 while (!FuncStart->use_empty()) {
698 CallInst *CI = cast<CallInst>(FuncStart->use_back());
699 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000700 }
Devang Patelbe94f232010-01-05 01:10:40 +0000701 FuncStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000702 }
Devang Patelbe94f232010-01-05 01:10:40 +0000703
Devang Patel80ae3492009-08-28 23:24:31 +0000704 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000705 while (!StopPoint->use_empty()) {
706 CallInst *CI = cast<CallInst>(StopPoint->use_back());
707 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000708 }
Devang Patelbe94f232010-01-05 01:10:40 +0000709 StopPoint->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000710 }
711
712 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000713 while (!RegionStart->use_empty()) {
714 CallInst *CI = cast<CallInst>(RegionStart->use_back());
715 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000716 }
Devang Patelbe94f232010-01-05 01:10:40 +0000717 RegionStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000718 }
719
720 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
Devang Patelbe94f232010-01-05 01:10:40 +0000721 while (!RegionEnd->use_empty()) {
722 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
723 CI->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000724 }
Devang Patelbe94f232010-01-05 01:10:40 +0000725 RegionEnd->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000726 }
727
728 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
729 if (!Declare->use_empty()) {
730 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000731 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
732 !isa<MDNode>(DDI->getArgOperand(1))) {
Devang Patel80ae3492009-08-28 23:24:31 +0000733 while (!Declare->use_empty()) {
734 CallInst *CI = cast<CallInst>(Declare->use_back());
735 CI->eraseFromParent();
736 }
737 Declare->eraseFromParent();
738 }
739 }
740 }
741}