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 // The old llvm.eh.selector.i32 is equivalent to the new llvm.eh.selector.
125 if (Name.compare("llvm.eh.selector.i32") == 0) {
126 F->setName("llvm.eh.selector");
130 // The old llvm.eh.typeid.for.i32 is equivalent to llvm.eh.typeid.for.
131 if (Name.compare("llvm.eh.typeid.for.i32") == 0) {
132 F->setName("llvm.eh.typeid.for");
136 // Convert the old llvm.eh.selector.i64 to a call to llvm.eh.selector.
137 if (Name.compare("llvm.eh.selector.i64") == 0) {
138 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_selector);
141 // Convert the old llvm.eh.typeid.for.i64 to a call to llvm.eh.typeid.for.
142 if (Name.compare("llvm.eh.typeid.for.i64") == 0) {
143 NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_typeid_for);
149 // This upgrades the llvm.part.select overloaded intrinsic names to only
150 // use one type specifier in the name. We only care about the old format
151 // 'llvm.part.select.i*.i*', and solve as above with bswap.
152 if (Name.compare(5,12,"part.select.",12) == 0) {
153 std::string::size_type delim = Name.find('.',17);
155 if (delim != std::string::npos) {
156 // Construct a new name as 'llvm.part.select' + '.i*'
157 F->setName(Name.substr(0,16)+Name.substr(delim));
164 // This upgrades the llvm.part.set intrinsics similarly as above, however
165 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
166 // must match. There is an additional type specifier after these two
167 // matching types that we must retain when upgrading. Thus, we require
168 // finding 2 periods, not just one, after the intrinsic name.
169 if (Name.compare(5,9,"part.set.",9) == 0) {
170 std::string::size_type delim = Name.find('.',14);
172 if (delim != std::string::npos &&
173 Name.find('.',delim+1) != std::string::npos) {
174 // Construct a new name as 'llvm.part.select' + '.i*.i*'
175 F->setName(Name.substr(0,13)+Name.substr(delim));
184 // This fixes all MMX shift intrinsic instructions to take a
185 // v1i64 instead of a v2i32 as the second parameter.
186 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
187 (Name.compare(13,4,"psll", 4) == 0 ||
188 Name.compare(13,4,"psra", 4) == 0 ||
189 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
191 const llvm::Type *VT =
192 VectorType::get(IntegerType::get(FTy->getContext(), 64), 1);
194 // We don't have to do anything if the parameter already has
196 if (FTy->getParamType(1) == VT)
199 // We first need to change the name of the old (bad) intrinsic, because
200 // its type is incorrect, but we cannot overload that name. We
201 // arbitrarily unique it here allowing us to construct a correctly named
202 // and typed function below.
205 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
207 // Now construct the new intrinsic with the correct name and type. We
208 // leave the old function around in order to query its type, whatever it
209 // may be, and correctly convert up to the new type.
210 NewFn = cast<Function>(M->getOrInsertFunction(Name,
211 FTy->getReturnType(),
212 FTy->getParamType(0),
216 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
217 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
218 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 ||
219 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 ||
220 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 ||
221 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 ||
222 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ||
223 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 ||
224 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) {
225 // Calls to these intrinsics are transformed into ShuffleVector's.
228 } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) {
229 // Calls to these intrinsics are transformed into vector multiplies.
238 // This may not belong here. This function is effectively being overloaded
239 // to both detect an intrinsic which needs upgrading, and to provide the
240 // upgraded form of the intrinsic. We should perhaps have two separate
241 // functions for this.
245 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
247 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
249 // Upgrade intrinsic attributes. This does not change the function.
252 if (unsigned id = F->getIntrinsicID())
253 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
257 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
258 // upgraded intrinsic. All argument and return casting must be provided in
259 // order to seamlessly integrate with existing context.
260 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
261 Function *F = CI->getCalledFunction();
262 LLVMContext &C = CI->getContext();
264 assert(F && "CallInst has no function associated with it.");
267 bool isLoadH = false, isLoadL = false, isMovL = false;
268 bool isMovSD = false, isShufPD = false;
269 bool isUnpckhPD = false, isUnpcklPD = false;
270 bool isPunpckhQPD = false, isPunpcklQPD = false;
271 if (F->getName() == "llvm.x86.sse2.loadh.pd")
273 else if (F->getName() == "llvm.x86.sse2.loadl.pd")
275 else if (F->getName() == "llvm.x86.sse2.movl.dq")
277 else if (F->getName() == "llvm.x86.sse2.movs.d")
279 else if (F->getName() == "llvm.x86.sse2.shuf.pd")
281 else if (F->getName() == "llvm.x86.sse2.unpckh.pd")
283 else if (F->getName() == "llvm.x86.sse2.unpckl.pd")
285 else if (F->getName() == "llvm.x86.sse2.punpckh.qdq")
287 else if (F->getName() == "llvm.x86.sse2.punpckl.qdq")
290 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD ||
291 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
292 std::vector<Constant*> Idxs;
293 Value *Op0 = CI->getOperand(1);
294 ShuffleVectorInst *SI = NULL;
295 if (isLoadH || isLoadL) {
296 Value *Op1 = UndefValue::get(Op0->getType());
297 Value *Addr = new BitCastInst(CI->getOperand(2),
298 Type::getDoublePtrTy(C),
300 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
301 Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0);
302 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
305 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
306 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
308 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
309 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
311 Value *Mask = ConstantVector::get(Idxs);
312 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
314 Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0);
315 Idxs.push_back(Zero);
316 Idxs.push_back(Zero);
317 Idxs.push_back(Zero);
318 Idxs.push_back(Zero);
319 Value *ZeroV = ConstantVector::get(Idxs);
322 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4));
323 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5));
324 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
325 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
326 Value *Mask = ConstantVector::get(Idxs);
327 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
328 } else if (isMovSD ||
329 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) {
330 Value *Op1 = CI->getOperand(2);
332 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
333 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
334 } else if (isUnpckhPD || isPunpckhQPD) {
335 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
336 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3));
338 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0));
339 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2));
341 Value *Mask = ConstantVector::get(Idxs);
342 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
343 } else if (isShufPD) {
344 Value *Op1 = CI->getOperand(2);
345 unsigned MaskVal = cast<ConstantInt>(CI->getOperand(3))->getZExtValue();
346 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1));
347 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C),
348 ((MaskVal >> 1) & 1)+2));
349 Value *Mask = ConstantVector::get(Idxs);
350 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
353 assert(SI && "Unexpected!");
355 // Handle any uses of the old CallInst.
356 if (!CI->use_empty())
357 // Replace all uses of the old call with the new cast which has the
359 CI->replaceAllUsesWith(SI);
361 // Clean up the old call now that it has been completely upgraded.
362 CI->eraseFromParent();
363 } else if (F->getName() == "llvm.x86.sse41.pmulld") {
364 // Upgrade this set of intrinsics into vector multiplies.
365 Instruction *Mul = BinaryOperator::CreateMul(CI->getOperand(1),
369 // Fix up all the uses with our new multiply.
370 if (!CI->use_empty())
371 CI->replaceAllUsesWith(Mul);
373 // Remove upgraded multiply.
374 CI->eraseFromParent();
376 llvm_unreachable("Unknown function for CallInst upgrade.");
381 switch (NewFn->getIntrinsicID()) {
382 default: llvm_unreachable("Unknown function for CallInst upgrade.");
383 case Intrinsic::x86_mmx_psll_d:
384 case Intrinsic::x86_mmx_psll_q:
385 case Intrinsic::x86_mmx_psll_w:
386 case Intrinsic::x86_mmx_psra_d:
387 case Intrinsic::x86_mmx_psra_w:
388 case Intrinsic::x86_mmx_psrl_d:
389 case Intrinsic::x86_mmx_psrl_q:
390 case Intrinsic::x86_mmx_psrl_w: {
393 Operands[0] = CI->getOperand(1);
395 // Cast the second parameter to the correct type.
396 BitCastInst *BC = new BitCastInst(CI->getOperand(2),
397 NewFn->getFunctionType()->getParamType(1),
401 // Construct a new CallInst
402 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
403 "upgraded."+CI->getName(), CI);
404 NewCI->setTailCall(CI->isTailCall());
405 NewCI->setCallingConv(CI->getCallingConv());
407 // Handle any uses of the old CallInst.
408 if (!CI->use_empty())
409 // Replace all uses of the old call with the new cast which has the
411 CI->replaceAllUsesWith(NewCI);
413 // Clean up the old call now that it has been completely upgraded.
414 CI->eraseFromParent();
417 case Intrinsic::ctlz:
418 case Intrinsic::ctpop:
419 case Intrinsic::cttz: {
420 // Build a small vector of the 1..(N-1) operands, which are the
422 SmallVector<Value*, 8> Operands(CI->op_begin()+1, CI->op_end());
424 // Construct a new CallInst
425 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
426 "upgraded."+CI->getName(), CI);
427 NewCI->setTailCall(CI->isTailCall());
428 NewCI->setCallingConv(CI->getCallingConv());
430 // Handle any uses of the old CallInst.
431 if (!CI->use_empty()) {
432 // Check for sign extend parameter attributes on the return values.
433 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt);
434 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt);
436 // Construct an appropriate cast from the new return type to the old.
437 CastInst *RetCast = CastInst::Create(
438 CastInst::getCastOpcode(NewCI, SrcSExt,
441 NewCI, F->getReturnType(),
442 NewCI->getName(), CI);
443 NewCI->moveBefore(RetCast);
445 // Replace all uses of the old call with the new cast which has the
447 CI->replaceAllUsesWith(RetCast);
450 // Clean up the old call now that it has been completely upgraded.
451 CI->eraseFromParent();
454 case Intrinsic::eh_selector:
455 case Intrinsic::eh_typeid_for: {
456 // Only the return type changed.
457 SmallVector<Value*, 8> Operands(CI->op_begin() + 1, CI->op_end());
458 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
459 "upgraded." + CI->getName(), CI);
460 NewCI->setTailCall(CI->isTailCall());
461 NewCI->setCallingConv(CI->getCallingConv());
463 // Handle any uses of the old CallInst.
464 if (!CI->use_empty()) {
465 // Construct an appropriate cast from the new return type to the old.
467 CastInst::Create(CastInst::getCastOpcode(NewCI, true,
468 F->getReturnType(), true),
469 NewCI, F->getReturnType(), NewCI->getName(), CI);
470 CI->replaceAllUsesWith(RetCast);
472 CI->eraseFromParent();
478 // This tests each Function to determine if it needs upgrading. When we find
479 // one we are interested in, we then upgrade all calls to reflect the new
481 void llvm::UpgradeCallsToIntrinsic(Function* F) {
482 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
484 // Upgrade the function and check if it is a totaly new function.
486 if (UpgradeIntrinsicFunction(F, NewFn)) {
488 // Replace all uses to the old function with the new one if necessary.
489 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
491 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
492 UpgradeIntrinsicCall(CI, NewFn);
494 // Remove old function, no longer used, from the module.
495 F->eraseFromParent();
500 /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
501 /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
503 void llvm::CheckDebugInfoIntrinsics(Module *M) {
506 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
507 while (!FuncStart->use_empty()) {
508 CallInst *CI = cast<CallInst>(FuncStart->use_back());
509 CI->eraseFromParent();
511 FuncStart->eraseFromParent();
514 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
515 while (!StopPoint->use_empty()) {
516 CallInst *CI = cast<CallInst>(StopPoint->use_back());
517 CI->eraseFromParent();
519 StopPoint->eraseFromParent();
522 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
523 while (!RegionStart->use_empty()) {
524 CallInst *CI = cast<CallInst>(RegionStart->use_back());
525 CI->eraseFromParent();
527 RegionStart->eraseFromParent();
530 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
531 while (!RegionEnd->use_empty()) {
532 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
533 CI->eraseFromParent();
535 RegionEnd->eraseFromParent();
538 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
539 if (!Declare->use_empty()) {
540 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
541 if (!isa<MDNode>(DDI->getOperand(1)) ||!isa<MDNode>(DDI->getOperand(2))) {
542 while (!Declare->use_empty()) {
543 CallInst *CI = cast<CallInst>(Declare->use_back());
544 CI->eraseFromParent();
546 Declare->eraseFromParent();