LLVM API Documentation
00001 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the auto-upgrade helper functions 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/AutoUpgrade.h" 00015 #include "llvm/Constants.h" 00016 #include "llvm/Function.h" 00017 #include "llvm/Module.h" 00018 #include "llvm/Instructions.h" 00019 #include "llvm/Intrinsics.h" 00020 #include "llvm/ADT/SmallVector.h" 00021 #include <cstring> 00022 using namespace llvm; 00023 00024 00025 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { 00026 assert(F && "Illegal to upgrade a non-existent Function."); 00027 00028 // Get the Function's name. 00029 const std::string& Name = F->getName(); 00030 00031 // Convenience 00032 const FunctionType *FTy = F->getFunctionType(); 00033 00034 // Quickly eliminate it, if it's not a candidate. 00035 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || 00036 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.') 00037 return false; 00038 00039 Module *M = F->getParent(); 00040 switch (Name[5]) { 00041 default: break; 00042 case 'a': 00043 // This upgrades the llvm.atomic.lcs, llvm.atomic.las, llvm.atomic.lss, 00044 // and atomics with default address spaces to their new names to their new 00045 // function name (e.g. llvm.atomic.add.i32 => llvm.atomic.add.i32.p0i32) 00046 if (Name.compare(5,7,"atomic.",7) == 0) { 00047 if (Name.compare(12,3,"lcs",3) == 0) { 00048 std::string::size_type delim = Name.find('.',12); 00049 F->setName("llvm.atomic.cmp.swap" + Name.substr(delim) + 00050 ".p0" + Name.substr(delim+1)); 00051 NewFn = F; 00052 return true; 00053 } 00054 else if (Name.compare(12,3,"las",3) == 0) { 00055 std::string::size_type delim = Name.find('.',12); 00056 F->setName("llvm.atomic.load.add"+Name.substr(delim) 00057 + ".p0" + Name.substr(delim+1)); 00058 NewFn = F; 00059 return true; 00060 } 00061 else if (Name.compare(12,3,"lss",3) == 0) { 00062 std::string::size_type delim = Name.find('.',12); 00063 F->setName("llvm.atomic.load.sub"+Name.substr(delim) 00064 + ".p0" + Name.substr(delim+1)); 00065 NewFn = F; 00066 return true; 00067 } 00068 else if (Name.rfind(".p") == std::string::npos) { 00069 // We don't have an address space qualifier so this has be upgraded 00070 // to the new name. Copy the type name at the end of the intrinsic 00071 // and add to it 00072 std::string::size_type delim = Name.find_last_of('.'); 00073 assert(delim != std::string::npos && "can not find type"); 00074 F->setName(Name + ".p0" + Name.substr(delim+1)); 00075 NewFn = F; 00076 return true; 00077 } 00078 } 00079 break; 00080 case 'b': 00081 // This upgrades the name of the llvm.bswap intrinsic function to only use 00082 // a single type name for overloading. We only care about the old format 00083 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being 00084 // a '.' after 'bswap.' 00085 if (Name.compare(5,6,"bswap.",6) == 0) { 00086 std::string::size_type delim = Name.find('.',11); 00087 00088 if (delim != std::string::npos) { 00089 // Construct the new name as 'llvm.bswap' + '.i*' 00090 F->setName(Name.substr(0,10)+Name.substr(delim)); 00091 NewFn = F; 00092 return true; 00093 } 00094 } 00095 break; 00096 00097 case 'c': 00098 // We only want to fix the 'llvm.ct*' intrinsics which do not have the 00099 // correct return type, so we check for the name, and then check if the 00100 // return type does not match the parameter type. 00101 if ( (Name.compare(5,5,"ctpop",5) == 0 || 00102 Name.compare(5,4,"ctlz",4) == 0 || 00103 Name.compare(5,4,"cttz",4) == 0) && 00104 FTy->getReturnType() != FTy->getParamType(0)) { 00105 // We first need to change the name of the old (bad) intrinsic, because 00106 // its type is incorrect, but we cannot overload that name. We 00107 // arbitrarily unique it here allowing us to construct a correctly named 00108 // and typed function below. 00109 F->setName(""); 00110 00111 // Now construct the new intrinsic with the correct name and type. We 00112 // leave the old function around in order to query its type, whatever it 00113 // may be, and correctly convert up to the new type. 00114 NewFn = cast<Function>(M->getOrInsertFunction(Name, 00115 FTy->getParamType(0), 00116 FTy->getParamType(0), 00117 (Type *)0)); 00118 return true; 00119 } 00120 break; 00121 00122 case 'p': 00123 // This upgrades the llvm.part.select overloaded intrinsic names to only 00124 // use one type specifier in the name. We only care about the old format 00125 // 'llvm.part.select.i*.i*', and solve as above with bswap. 00126 if (Name.compare(5,12,"part.select.",12) == 0) { 00127 std::string::size_type delim = Name.find('.',17); 00128 00129 if (delim != std::string::npos) { 00130 // Construct a new name as 'llvm.part.select' + '.i*' 00131 F->setName(Name.substr(0,16)+Name.substr(delim)); 00132 NewFn = F; 00133 return true; 00134 } 00135 break; 00136 } 00137 00138 // This upgrades the llvm.part.set intrinsics similarly as above, however 00139 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types 00140 // must match. There is an additional type specifier after these two 00141 // matching types that we must retain when upgrading. Thus, we require 00142 // finding 2 periods, not just one, after the intrinsic name. 00143 if (Name.compare(5,9,"part.set.",9) == 0) { 00144 std::string::size_type delim = Name.find('.',14); 00145 00146 if (delim != std::string::npos && 00147 Name.find('.',delim+1) != std::string::npos) { 00148 // Construct a new name as 'llvm.part.select' + '.i*.i*' 00149 F->setName(Name.substr(0,13)+Name.substr(delim)); 00150 NewFn = F; 00151 return true; 00152 } 00153 break; 00154 } 00155 00156 break; 00157 case 'x': 00158 // This fixes all MMX shift intrinsic instructions to take a 00159 // v1i64 instead of a v2i32 as the second parameter. 00160 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 && 00161 (Name.compare(13,4,"psll", 4) == 0 || 00162 Name.compare(13,4,"psra", 4) == 0 || 00163 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') { 00164 00165 const llvm::Type *VT = VectorType::get(IntegerType::get(64), 1); 00166 00167 // We don't have to do anything if the parameter already has 00168 // the correct type. 00169 if (FTy->getParamType(1) == VT) 00170 break; 00171 00172 // We first need to change the name of the old (bad) intrinsic, because 00173 // its type is incorrect, but we cannot overload that name. We 00174 // arbitrarily unique it here allowing us to construct a correctly named 00175 // and typed function below. 00176 F->setName(""); 00177 00178 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!"); 00179 00180 // Now construct the new intrinsic with the correct name and type. We 00181 // leave the old function around in order to query its type, whatever it 00182 // may be, and correctly convert up to the new type. 00183 NewFn = cast<Function>(M->getOrInsertFunction(Name, 00184 FTy->getReturnType(), 00185 FTy->getParamType(0), 00186 VT, 00187 (Type *)0)); 00188 return true; 00189 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 || 00190 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 || 00191 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 || 00192 Name.compare(5,15,"x86.sse2.movs.d",15) == 0 || 00193 Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 || 00194 Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 || 00195 Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 || 00196 Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 || 00197 Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) { 00198 // Calls to these intrinsics are transformed into ShuffleVector's. 00199 NewFn = 0; 00200 return true; 00201 } 00202 00203 break; 00204 } 00205 00206 // This may not belong here. This function is effectively being overloaded 00207 // to both detect an intrinsic which needs upgrading, and to provide the 00208 // upgraded form of the intrinsic. We should perhaps have two separate 00209 // functions for this. 00210 return false; 00211 } 00212 00213 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { 00214 NewFn = 0; 00215 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); 00216 00217 // Upgrade intrinsic attributes. This does not change the function. 00218 if (NewFn) 00219 F = NewFn; 00220 if (unsigned id = F->getIntrinsicID(true)) 00221 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id)); 00222 return Upgraded; 00223 } 00224 00225 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 00226 // upgraded intrinsic. All argument and return casting must be provided in 00227 // order to seamlessly integrate with existing context. 00228 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { 00229 Function *F = CI->getCalledFunction(); 00230 assert(F && "CallInst has no function associated with it."); 00231 00232 if (!NewFn) { 00233 bool isLoadH = false, isLoadL = false, isMovL = false; 00234 bool isMovSD = false, isShufPD = false; 00235 bool isUnpckhPD = false, isUnpcklPD = false; 00236 bool isPunpckhQPD = false, isPunpcklQPD = false; 00237 if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadh.pd") == 0) 00238 isLoadH = true; 00239 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadl.pd") == 0) 00240 isLoadL = true; 00241 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0) 00242 isMovL = true; 00243 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movs.d") == 0) 00244 isMovSD = true; 00245 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.shuf.pd") == 0) 00246 isShufPD = true; 00247 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckh.pd") == 0) 00248 isUnpckhPD = true; 00249 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckl.pd") == 0) 00250 isUnpcklPD = true; 00251 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.punpckh.qdq") == 0) 00252 isPunpckhQPD = true; 00253 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.punpckl.qdq") == 0) 00254 isPunpcklQPD = true; 00255 00256 if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD || 00257 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) { 00258 std::vector<Constant*> Idxs; 00259 Value *Op0 = CI->getOperand(1); 00260 ShuffleVectorInst *SI = NULL; 00261 if (isLoadH || isLoadL) { 00262 Value *Op1 = UndefValue::get(Op0->getType()); 00263 Value *Addr = new BitCastInst(CI->getOperand(2), 00264 PointerType::getUnqual(Type::DoubleTy), 00265 "upgraded.", CI); 00266 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI); 00267 Value *Idx = ConstantInt::get(Type::Int32Ty, 0); 00268 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI); 00269 00270 if (isLoadH) { 00271 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 0)); 00272 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); 00273 } else { 00274 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); 00275 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1)); 00276 } 00277 Value *Mask = ConstantVector::get(Idxs); 00278 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); 00279 } else if (isMovL) { 00280 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0); 00281 Idxs.push_back(Zero); 00282 Idxs.push_back(Zero); 00283 Idxs.push_back(Zero); 00284 Idxs.push_back(Zero); 00285 Value *ZeroV = ConstantVector::get(Idxs); 00286 00287 Idxs.clear(); 00288 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 4)); 00289 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 5)); 00290 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); 00291 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3)); 00292 Value *Mask = ConstantVector::get(Idxs); 00293 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI); 00294 } else if (isMovSD || 00295 isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) { 00296 Value *Op1 = CI->getOperand(2); 00297 if (isMovSD) { 00298 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); 00299 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1)); 00300 } else if (isUnpckhPD || isPunpckhQPD) { 00301 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1)); 00302 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3)); 00303 } else { 00304 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 0)); 00305 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); 00306 } 00307 Value *Mask = ConstantVector::get(Idxs); 00308 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); 00309 } else if (isShufPD) { 00310 Value *Op1 = CI->getOperand(2); 00311 unsigned MaskVal = cast<ConstantInt>(CI->getOperand(3))->getZExtValue(); 00312 Idxs.push_back(ConstantInt::get(Type::Int32Ty, MaskVal & 1)); 00313 Idxs.push_back(ConstantInt::get(Type::Int32Ty, ((MaskVal >> 1) & 1)+2)); 00314 Value *Mask = ConstantVector::get(Idxs); 00315 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); 00316 } 00317 00318 assert(SI && "Unexpected!"); 00319 00320 // Handle any uses of the old CallInst. 00321 if (!CI->use_empty()) 00322 // Replace all uses of the old call with the new cast which has the 00323 // correct type. 00324 CI->replaceAllUsesWith(SI); 00325 00326 // Clean up the old call now that it has been completely upgraded. 00327 CI->eraseFromParent(); 00328 } else { 00329 assert(0 && "Unknown function for CallInst upgrade."); 00330 } 00331 return; 00332 } 00333 00334 switch (NewFn->getIntrinsicID()) { 00335 default: assert(0 && "Unknown function for CallInst upgrade."); 00336 case Intrinsic::x86_mmx_psll_d: 00337 case Intrinsic::x86_mmx_psll_q: 00338 case Intrinsic::x86_mmx_psll_w: 00339 case Intrinsic::x86_mmx_psra_d: 00340 case Intrinsic::x86_mmx_psra_w: 00341 case Intrinsic::x86_mmx_psrl_d: 00342 case Intrinsic::x86_mmx_psrl_q: 00343 case Intrinsic::x86_mmx_psrl_w: { 00344 Value *Operands[2]; 00345 00346 Operands[0] = CI->getOperand(1); 00347 00348 // Cast the second parameter to the correct type. 00349 BitCastInst *BC = new BitCastInst(CI->getOperand(2), 00350 NewFn->getFunctionType()->getParamType(1), 00351 "upgraded.", CI); 00352 Operands[1] = BC; 00353 00354 // Construct a new CallInst 00355 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2, 00356 "upgraded."+CI->getName(), CI); 00357 NewCI->setTailCall(CI->isTailCall()); 00358 NewCI->setCallingConv(CI->getCallingConv()); 00359 00360 // Handle any uses of the old CallInst. 00361 if (!CI->use_empty()) 00362 // Replace all uses of the old call with the new cast which has the 00363 // correct type. 00364 CI->replaceAllUsesWith(NewCI); 00365 00366 // Clean up the old call now that it has been completely upgraded. 00367 CI->eraseFromParent(); 00368 break; 00369 } 00370 case Intrinsic::ctlz: 00371 case Intrinsic::ctpop: 00372 case Intrinsic::cttz: { 00373 // Build a small vector of the 1..(N-1) operands, which are the 00374 // parameters. 00375 SmallVector<Value*, 8> Operands(CI->op_begin()+1, CI->op_end()); 00376 00377 // Construct a new CallInst 00378 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(), 00379 "upgraded."+CI->getName(), CI); 00380 NewCI->setTailCall(CI->isTailCall()); 00381 NewCI->setCallingConv(CI->getCallingConv()); 00382 00383 // Handle any uses of the old CallInst. 00384 if (!CI->use_empty()) { 00385 // Check for sign extend parameter attributes on the return values. 00386 bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt); 00387 bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt); 00388 00389 // Construct an appropriate cast from the new return type to the old. 00390 CastInst *RetCast = CastInst::Create( 00391 CastInst::getCastOpcode(NewCI, SrcSExt, 00392 F->getReturnType(), 00393 DestSExt), 00394 NewCI, F->getReturnType(), 00395 NewCI->getName(), CI); 00396 NewCI->moveBefore(RetCast); 00397 00398 // Replace all uses of the old call with the new cast which has the 00399 // correct type. 00400 CI->replaceAllUsesWith(RetCast); 00401 } 00402 00403 // Clean up the old call now that it has been completely upgraded. 00404 CI->eraseFromParent(); 00405 } 00406 break; 00407 } 00408 } 00409 00410 // This tests each Function to determine if it needs upgrading. When we find 00411 // one we are interested in, we then upgrade all calls to reflect the new 00412 // function. 00413 void llvm::UpgradeCallsToIntrinsic(Function* F) { 00414 assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); 00415 00416 // Upgrade the function and check if it is a totaly new function. 00417 Function* NewFn; 00418 if (UpgradeIntrinsicFunction(F, NewFn)) { 00419 if (NewFn != F) { 00420 // Replace all uses to the old function with the new one if necessary. 00421 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end(); 00422 UI != UE; ) { 00423 if (CallInst* CI = dyn_cast<CallInst>(*UI++)) 00424 UpgradeIntrinsicCall(CI, NewFn); 00425 } 00426 // Remove old function, no longer used, from the module. 00427 F->eraseFromParent(); 00428 } 00429 } 00430 }