1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the auto-upgrade helper functions
12 //===----------------------------------------------------------------------===//
14 #include "llvm/AutoUpgrade.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Function.h"
17 #include "llvm/LLVMContext.h"
18 #include "llvm/Module.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/ErrorHandling.h"
26 static bool UpgradeIntrinsicFunction1(Function
*F
, Function
*&NewFn
) {
27 assert(F
&& "Illegal to upgrade a non-existent Function.");
29 // Get the Function's name.
30 const std::string
& Name
= F
->getName();
33 const FunctionType
*FTy
= F
->getFunctionType();
35 // Quickly eliminate it, if it's not a candidate.
36 if (Name
.length() <= 8 || Name
[0] != 'l' || Name
[1] != 'l' ||
37 Name
[2] != 'v' || Name
[3] != 'm' || Name
[4] != '.')
40 Module
*M
= F
->getParent();
44 // This upgrades the llvm.atomic.lcs, llvm.atomic.las, llvm.atomic.lss,
45 // and atomics with default address spaces to their new names to their new
46 // function name (e.g. llvm.atomic.add.i32 => llvm.atomic.add.i32.p0i32)
47 if (Name
.compare(5,7,"atomic.",7) == 0) {
48 if (Name
.compare(12,3,"lcs",3) == 0) {
49 std::string::size_type delim
= Name
.find('.',12);
50 F
->setName("llvm.atomic.cmp.swap" + Name
.substr(delim
) +
51 ".p0" + Name
.substr(delim
+1));
55 else if (Name
.compare(12,3,"las",3) == 0) {
56 std::string::size_type delim
= Name
.find('.',12);
57 F
->setName("llvm.atomic.load.add"+Name
.substr(delim
)
58 + ".p0" + Name
.substr(delim
+1));
62 else if (Name
.compare(12,3,"lss",3) == 0) {
63 std::string::size_type delim
= Name
.find('.',12);
64 F
->setName("llvm.atomic.load.sub"+Name
.substr(delim
)
65 + ".p0" + Name
.substr(delim
+1));
69 else if (Name
.rfind(".p") == std::string::npos
) {
70 // We don't have an address space qualifier so this has be upgraded
71 // to the new name. Copy the type name at the end of the intrinsic
73 std::string::size_type delim
= Name
.find_last_of('.');
74 assert(delim
!= std::string::npos
&& "can not find type");
75 F
->setName(Name
+ ".p0" + Name
.substr(delim
+1));
82 // This upgrades the name of the llvm.bswap intrinsic function to only use
83 // a single type name for overloading. We only care about the old format
84 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
85 // a '.' after 'bswap.'
86 if (Name
.compare(5,6,"bswap.",6) == 0) {
87 std::string::size_type delim
= Name
.find('.',11);
89 if (delim
!= std::string::npos
) {
90 // Construct the new name as 'llvm.bswap' + '.i*'
91 F
->setName(Name
.substr(0,10)+Name
.substr(delim
));
99 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
100 // correct return type, so we check for the name, and then check if the
101 // return type does not match the parameter type.
102 if ( (Name
.compare(5,5,"ctpop",5) == 0 ||
103 Name
.compare(5,4,"ctlz",4) == 0 ||
104 Name
.compare(5,4,"cttz",4) == 0) &&
105 FTy
->getReturnType() != FTy
->getParamType(0)) {
106 // We first need to change the name of the old (bad) intrinsic, because
107 // its type is incorrect, but we cannot overload that name. We
108 // arbitrarily unique it here allowing us to construct a correctly named
109 // and typed function below.
112 // Now construct the new intrinsic with the correct name and type. We
113 // leave the old function around in order to query its type, whatever it
114 // may be, and correctly convert up to the new type.
115 NewFn
= cast
<Function
>(M
->getOrInsertFunction(Name
,
116 FTy
->getParamType(0),
117 FTy
->getParamType(0),
124 // This upgrades the llvm.part.select overloaded intrinsic names to only
125 // use one type specifier in the name. We only care about the old format
126 // 'llvm.part.select.i*.i*', and solve as above with bswap.
127 if (Name
.compare(5,12,"part.select.",12) == 0) {
128 std::string::size_type delim
= Name
.find('.',17);
130 if (delim
!= std::string::npos
) {
131 // Construct a new name as 'llvm.part.select' + '.i*'
132 F
->setName(Name
.substr(0,16)+Name
.substr(delim
));
139 // This upgrades the llvm.part.set intrinsics similarly as above, however
140 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
141 // must match. There is an additional type specifier after these two
142 // matching types that we must retain when upgrading. Thus, we require
143 // finding 2 periods, not just one, after the intrinsic name.
144 if (Name
.compare(5,9,"part.set.",9) == 0) {
145 std::string::size_type delim
= Name
.find('.',14);
147 if (delim
!= std::string::npos
&&
148 Name
.find('.',delim
+1) != std::string::npos
) {
149 // Construct a new name as 'llvm.part.select' + '.i*.i*'
150 F
->setName(Name
.substr(0,13)+Name
.substr(delim
));
159 // This fixes all MMX shift intrinsic instructions to take a
160 // v1i64 instead of a v2i32 as the second parameter.
161 if (Name
.compare(5,10,"x86.mmx.ps",10) == 0 &&
162 (Name
.compare(13,4,"psll", 4) == 0 ||
163 Name
.compare(13,4,"psra", 4) == 0 ||
164 Name
.compare(13,4,"psrl", 4) == 0) && Name
[17] != 'i') {
166 const llvm::Type
*VT
=
167 VectorType::get(IntegerType::get(FTy
->getContext(), 64), 1);
169 // We don't have to do anything if the parameter already has
171 if (FTy
->getParamType(1) == VT
)
174 // We first need to change the name of the old (bad) intrinsic, because
175 // its type is incorrect, but we cannot overload that name. We
176 // arbitrarily unique it here allowing us to construct a correctly named
177 // and typed function below.
180 assert(FTy
->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
182 // Now construct the new intrinsic with the correct name and type. We
183 // leave the old function around in order to query its type, whatever it
184 // may be, and correctly convert up to the new type.
185 NewFn
= cast
<Function
>(M
->getOrInsertFunction(Name
,
186 FTy
->getReturnType(),
187 FTy
->getParamType(0),
191 } else if (Name
.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
192 Name
.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
193 Name
.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
194 Name
.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
195 Name
.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
196 Name
.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
197 Name
.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
198 Name
.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
199 Name
.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
200 // Calls to these intrinsics are transformed into ShuffleVector's.
208 // This may not belong here. This function is effectively being overloaded
209 // to both detect an intrinsic which needs upgrading, and to provide the
210 // upgraded form of the intrinsic. We should perhaps have two separate
211 // functions for this.
215 bool llvm::UpgradeIntrinsicFunction(Function
*F
, Function
*&NewFn
) {
217 bool Upgraded
= UpgradeIntrinsicFunction1(F
, NewFn
);
219 // Upgrade intrinsic attributes. This does not change the function.
222 if (unsigned id
= F
->getIntrinsicID())
223 F
->setAttributes(Intrinsic::getAttributes((Intrinsic::ID
)id
));
227 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
228 // upgraded intrinsic. All argument and return casting must be provided in
229 // order to seamlessly integrate with existing context.
230 void llvm::UpgradeIntrinsicCall(CallInst
*CI
, Function
*NewFn
) {
231 Function
*F
= CI
->getCalledFunction();
232 LLVMContext
&C
= CI
->getContext();
234 assert(F
&& "CallInst has no function associated with it.");
237 bool isLoadH
= false, isLoadL
= false, isMovL
= false;
238 bool isMovSD
= false, isShufPD
= false;
239 bool isUnpckhPD
= false, isUnpcklPD
= false;
240 bool isPunpckhQPD
= false, isPunpcklQPD
= false;
241 if (F
->getName() == "llvm.x86.sse2.loadh.pd")
243 else if (F
->getName() == "llvm.x86.sse2.loadl.pd")
245 else if (F
->getName() == "llvm.x86.sse2.movl.dq")
247 else if (F
->getName() == "llvm.x86.sse2.movs.d")
249 else if (F
->getName() == "llvm.x86.sse2.shuf.pd")
251 else if (F
->getName() == "llvm.x86.sse2.unpckh.pd")
253 else if (F
->getName() == "llvm.x86.sse2.unpckl.pd")
255 else if (F
->getName() == "llvm.x86.sse2.punpckh.qdq")
257 else if (F
->getName() == "llvm.x86.sse2.punpckl.qdq")
260 if (isLoadH
|| isLoadL
|| isMovL
|| isMovSD
|| isShufPD
||
261 isUnpckhPD
|| isUnpcklPD
|| isPunpckhQPD
|| isPunpcklQPD
) {
262 std::vector
<Constant
*> Idxs
;
263 Value
*Op0
= CI
->getOperand(1);
264 ShuffleVectorInst
*SI
= NULL
;
265 if (isLoadH
|| isLoadL
) {
266 Value
*Op1
= UndefValue::get(Op0
->getType());
267 Value
*Addr
= new BitCastInst(CI
->getOperand(2),
268 PointerType::getUnqual(Type::getDoubleTy(C
)),
270 Value
*Load
= new LoadInst(Addr
, "upgraded.", false, 8, CI
);
271 Value
*Idx
= ConstantInt::get(Type::getInt32Ty(C
), 0);
272 Op1
= InsertElementInst::Create(Op1
, Load
, Idx
, "upgraded.", CI
);
275 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 0));
276 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 2));
278 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 2));
279 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 1));
281 Value
*Mask
= ConstantVector::get(Idxs
);
282 SI
= new ShuffleVectorInst(Op0
, Op1
, Mask
, "upgraded.", CI
);
284 Constant
*Zero
= ConstantInt::get(Type::getInt32Ty(C
), 0);
285 Idxs
.push_back(Zero
);
286 Idxs
.push_back(Zero
);
287 Idxs
.push_back(Zero
);
288 Idxs
.push_back(Zero
);
289 Value
*ZeroV
= ConstantVector::get(Idxs
);
292 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 4));
293 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 5));
294 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 2));
295 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 3));
296 Value
*Mask
= ConstantVector::get(Idxs
);
297 SI
= new ShuffleVectorInst(ZeroV
, Op0
, Mask
, "upgraded.", CI
);
298 } else if (isMovSD
||
299 isUnpckhPD
|| isUnpcklPD
|| isPunpckhQPD
|| isPunpcklQPD
) {
300 Value
*Op1
= CI
->getOperand(2);
302 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 2));
303 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 1));
304 } else if (isUnpckhPD
|| isPunpckhQPD
) {
305 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 1));
306 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 3));
308 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 0));
309 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 2));
311 Value
*Mask
= ConstantVector::get(Idxs
);
312 SI
= new ShuffleVectorInst(Op0
, Op1
, Mask
, "upgraded.", CI
);
313 } else if (isShufPD
) {
314 Value
*Op1
= CI
->getOperand(2);
315 unsigned MaskVal
= cast
<ConstantInt
>(CI
->getOperand(3))->getZExtValue();
316 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
), MaskVal
& 1));
317 Idxs
.push_back(ConstantInt::get(Type::getInt32Ty(C
),
318 ((MaskVal
>> 1) & 1)+2));
319 Value
*Mask
= ConstantVector::get(Idxs
);
320 SI
= new ShuffleVectorInst(Op0
, Op1
, Mask
, "upgraded.", CI
);
323 assert(SI
&& "Unexpected!");
325 // Handle any uses of the old CallInst.
326 if (!CI
->use_empty())
327 // Replace all uses of the old call with the new cast which has the
329 CI
->replaceAllUsesWith(SI
);
331 // Clean up the old call now that it has been completely upgraded.
332 CI
->eraseFromParent();
334 llvm_unreachable("Unknown function for CallInst upgrade.");
339 switch (NewFn
->getIntrinsicID()) {
340 default: llvm_unreachable("Unknown function for CallInst upgrade.");
341 case Intrinsic::x86_mmx_psll_d
:
342 case Intrinsic::x86_mmx_psll_q
:
343 case Intrinsic::x86_mmx_psll_w
:
344 case Intrinsic::x86_mmx_psra_d
:
345 case Intrinsic::x86_mmx_psra_w
:
346 case Intrinsic::x86_mmx_psrl_d
:
347 case Intrinsic::x86_mmx_psrl_q
:
348 case Intrinsic::x86_mmx_psrl_w
: {
351 Operands
[0] = CI
->getOperand(1);
353 // Cast the second parameter to the correct type.
354 BitCastInst
*BC
= new BitCastInst(CI
->getOperand(2),
355 NewFn
->getFunctionType()->getParamType(1),
359 // Construct a new CallInst
360 CallInst
*NewCI
= CallInst::Create(NewFn
, Operands
, Operands
+2,
361 "upgraded."+CI
->getName(), CI
);
362 NewCI
->setTailCall(CI
->isTailCall());
363 NewCI
->setCallingConv(CI
->getCallingConv());
365 // Handle any uses of the old CallInst.
366 if (!CI
->use_empty())
367 // Replace all uses of the old call with the new cast which has the
369 CI
->replaceAllUsesWith(NewCI
);
371 // Clean up the old call now that it has been completely upgraded.
372 CI
->eraseFromParent();
375 case Intrinsic::ctlz
:
376 case Intrinsic::ctpop
:
377 case Intrinsic::cttz
: {
378 // Build a small vector of the 1..(N-1) operands, which are the
380 SmallVector
<Value
*, 8> Operands(CI
->op_begin()+1, CI
->op_end());
382 // Construct a new CallInst
383 CallInst
*NewCI
= CallInst::Create(NewFn
, Operands
.begin(), Operands
.end(),
384 "upgraded."+CI
->getName(), CI
);
385 NewCI
->setTailCall(CI
->isTailCall());
386 NewCI
->setCallingConv(CI
->getCallingConv());
388 // Handle any uses of the old CallInst.
389 if (!CI
->use_empty()) {
390 // Check for sign extend parameter attributes on the return values.
391 bool SrcSExt
= NewFn
->getAttributes().paramHasAttr(0, Attribute::SExt
);
392 bool DestSExt
= F
->getAttributes().paramHasAttr(0, Attribute::SExt
);
394 // Construct an appropriate cast from the new return type to the old.
395 CastInst
*RetCast
= CastInst::Create(
396 CastInst::getCastOpcode(NewCI
, SrcSExt
,
399 NewCI
, F
->getReturnType(),
400 NewCI
->getName(), CI
);
401 NewCI
->moveBefore(RetCast
);
403 // Replace all uses of the old call with the new cast which has the
405 CI
->replaceAllUsesWith(RetCast
);
408 // Clean up the old call now that it has been completely upgraded.
409 CI
->eraseFromParent();
415 // This tests each Function to determine if it needs upgrading. When we find
416 // one we are interested in, we then upgrade all calls to reflect the new
418 void llvm::UpgradeCallsToIntrinsic(Function
* F
) {
419 assert(F
&& "Illegal attempt to upgrade a non-existent intrinsic.");
421 // Upgrade the function and check if it is a totaly new function.
423 if (UpgradeIntrinsicFunction(F
, NewFn
)) {
425 // Replace all uses to the old function with the new one if necessary.
426 for (Value::use_iterator UI
= F
->use_begin(), UE
= F
->use_end();
428 if (CallInst
* CI
= dyn_cast
<CallInst
>(*UI
++))
429 UpgradeIntrinsicCall(CI
, NewFn
);
431 // Remove old function, no longer used, from the module.
432 F
->eraseFromParent();
437 /// This function checks debug info intrinsics. If an intrinsic is invalid
438 /// then this function simply removes the intrinsic.
439 void llvm::CheckDebugInfoIntrinsics(Module
*M
) {
442 if (Function
*FuncStart
= M
->getFunction("llvm.dbg.func.start")) {
443 if (!FuncStart
->use_empty()) {
444 DbgFuncStartInst
*DFSI
= cast
<DbgFuncStartInst
>(FuncStart
->use_back());
445 if (!isa
<MDNode
>(DFSI
->getOperand(1))) {
446 while (!FuncStart
->use_empty()) {
447 CallInst
*CI
= cast
<CallInst
>(FuncStart
->use_back());
448 CI
->eraseFromParent();
450 FuncStart
->eraseFromParent();
455 if (Function
*StopPoint
= M
->getFunction("llvm.dbg.stoppoint")) {
456 if (!StopPoint
->use_empty()) {
457 DbgStopPointInst
*DSPI
= cast
<DbgStopPointInst
>(StopPoint
->use_back());
458 if (!isa
<MDNode
>(DSPI
->getOperand(3))) {
459 while (!StopPoint
->use_empty()) {
460 CallInst
*CI
= cast
<CallInst
>(StopPoint
->use_back());
461 CI
->eraseFromParent();
463 StopPoint
->eraseFromParent();
468 if (Function
*RegionStart
= M
->getFunction("llvm.dbg.region.start")) {
469 if (!RegionStart
->use_empty()) {
470 DbgRegionStartInst
*DRSI
= cast
<DbgRegionStartInst
>(RegionStart
->use_back());
471 if (!isa
<MDNode
>(DRSI
->getOperand(1))) {
472 while (!RegionStart
->use_empty()) {
473 CallInst
*CI
= cast
<CallInst
>(RegionStart
->use_back());
474 CI
->eraseFromParent();
476 RegionStart
->eraseFromParent();
481 if (Function
*RegionEnd
= M
->getFunction("llvm.dbg.region.end")) {
482 if (!RegionEnd
->use_empty()) {
483 DbgRegionEndInst
*DREI
= cast
<DbgRegionEndInst
>(RegionEnd
->use_back());
484 if (!isa
<MDNode
>(DREI
->getOperand(1))) {
485 while (!RegionEnd
->use_empty()) {
486 CallInst
*CI
= cast
<CallInst
>(RegionEnd
->use_back());
487 CI
->eraseFromParent();
489 RegionEnd
->eraseFromParent();
494 if (Function
*Declare
= M
->getFunction("llvm.dbg.declare")) {
495 if (!Declare
->use_empty()) {
496 DbgDeclareInst
*DDI
= cast
<DbgDeclareInst
>(Declare
->use_back());
497 if (!isa
<MDNode
>(DDI
->getOperand(2))) {
498 while (!Declare
->use_empty()) {
499 CallInst
*CI
= cast
<CallInst
>(Declare
->use_back());
500 CI
->eraseFromParent();
502 Declare
->eraseFromParent();