AutoUpgrade.cpp revision 4ee451de366474b9c228b4e5fa573795a715216d
16994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===// 26994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// 36994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// The LLVM Compiler Infrastructure 46994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 76994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// 86994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//===----------------------------------------------------------------------===// 96994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// 106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// This file implements the auto-upgrade helper functions 116994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// 126994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth//===----------------------------------------------------------------------===// 136994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 146994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/AutoUpgrade.h" 15d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson#include "llvm/Constants.h" 166994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Function.h" 176994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Module.h" 186994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Instructions.h" 196994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/ParameterAttributes.h" 206994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth#include "llvm/Intrinsics.h" 216994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthusing namespace llvm; 226994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 236994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 24f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Chengstatic bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { 256994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth assert(F && "Illegal to upgrade a non-existent Function."); 266994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 276994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Get the Function's name. 286994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth const std::string& Name = F->getName(); 296994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 306994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Convenience 316994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth const FunctionType *FTy = F->getFunctionType(); 326994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 336994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Quickly eliminate it, if it's not a candidate. 346994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || 356994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.') 36f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return false; 376994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 386994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth Module *M = F->getParent(); 396994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth switch (Name[5]) { 406994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth default: break; 416994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth case 'b': 426994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // This upgrades the name of the llvm.bswap intrinsic function to only use 436994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // a single type name for overloading. We only care about the old format 446994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being 456994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // a '.' after 'bswap.' 466994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (Name.compare(5,6,"bswap.",6) == 0) { 476994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth std::string::size_type delim = Name.find('.',11); 486994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 496994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (delim != std::string::npos) { 506994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Construct the new name as 'llvm.bswap' + '.i*' 516994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth F->setName(Name.substr(0,10)+Name.substr(delim)); 52f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng NewFn = F; 53f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return true; 546994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 556994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 566994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth break; 576994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 586994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth case 'c': 596994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // We only want to fix the 'llvm.ct*' intrinsics which do not have the 606994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // correct return type, so we check for the name, and then check if the 616994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // return type does not match the parameter type. 626994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if ( (Name.compare(5,5,"ctpop",5) == 0 || 636994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth Name.compare(5,4,"ctlz",4) == 0 || 646994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth Name.compare(5,4,"cttz",4) == 0) && 656994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth FTy->getReturnType() != FTy->getParamType(0)) { 666994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // We first need to change the name of the old (bad) intrinsic, because 676994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // its type is incorrect, but we cannot overload that name. We 686994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // arbitrarily unique it here allowing us to construct a correctly named 696994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // and typed function below. 706994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth F->setName(""); 716994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 726994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Now construct the new intrinsic with the correct name and type. We 736994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // leave the old function around in order to query its type, whatever it 746994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // may be, and correctly convert up to the new type. 75f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng NewFn = cast<Function>(M->getOrInsertFunction(Name, 76f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng FTy->getParamType(0), 77f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng FTy->getParamType(0), 78f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng (Type *)0)); 79f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return true; 806994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 816994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth break; 826994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 836994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth case 'p': 846994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // This upgrades the llvm.part.select overloaded intrinsic names to only 856994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // use one type specifier in the name. We only care about the old format 866994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // 'llvm.part.select.i*.i*', and solve as above with bswap. 876994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (Name.compare(5,12,"part.select.",12) == 0) { 886994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth std::string::size_type delim = Name.find('.',17); 896994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 906994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (delim != std::string::npos) { 916994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Construct a new name as 'llvm.part.select' + '.i*' 926994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth F->setName(Name.substr(0,16)+Name.substr(delim)); 93f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng NewFn = F; 94f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return true; 956994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 966994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth break; 976994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 986994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 996994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // This upgrades the llvm.part.set intrinsics similarly as above, however 1006994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // we care about 'llvm.part.set.i*.i*.i*', but only the first two types 1016994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // must match. There is an additional type specifier after these two 1026994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // matching types that we must retain when upgrading. Thus, we require 1036994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // finding 2 periods, not just one, after the intrinsic name. 1046994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (Name.compare(5,9,"part.set.",9) == 0) { 1056994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth std::string::size_type delim = Name.find('.',14); 1066994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 1076994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (delim != std::string::npos && 1086994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth Name.find('.',delim+1) != std::string::npos) { 1096994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Construct a new name as 'llvm.part.select' + '.i*.i*' 1106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth F->setName(Name.substr(0,13)+Name.substr(delim)); 111f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng NewFn = F; 112f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return true; 1136994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 1146994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth break; 1156994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 1166994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 1176994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth break; 118d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case 'x': 119d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // This fixes all MMX shift intrinsic instructions to take a 120d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // v1i64 instead of a v2i32 as the second parameter. 121d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson if (Name.compare(5,10,"x86.mmx.ps",10) == 0 && 122d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson (Name.compare(13,4,"psll", 4) == 0 || 123d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson Name.compare(13,4,"psra", 4) == 0 || 124d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson Name.compare(13,4,"psrl", 4) == 0)) { 125d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 126d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson const llvm::Type *VT = VectorType::get(IntegerType::get(64), 1); 127d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 128d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // We don't have to do anything if the parameter already has 129d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // the correct type. 130d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson if (FTy->getParamType(1) == VT) 131d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson break; 132d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 133d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // We first need to change the name of the old (bad) intrinsic, because 134d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // its type is incorrect, but we cannot overload that name. We 135d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // arbitrarily unique it here allowing us to construct a correctly named 136d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // and typed function below. 137d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson F->setName(""); 138d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 139d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!"); 140d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 141d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // Now construct the new intrinsic with the correct name and type. We 142d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // leave the old function around in order to query its type, whatever it 143d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // may be, and correctly convert up to the new type. 144f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng NewFn = cast<Function>(M->getOrInsertFunction(Name, 145f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng FTy->getReturnType(), 146f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng FTy->getParamType(0), 147f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng VT, 148f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng (Type *)0)); 149f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return true; 150f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng } else if (Name.compare(5,16,"x86.sse2.movl.dq",16) == 0) { 151f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng // Calls to this intrinsic are transformed into ShuffleVector's. 152f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng NewFn = 0; 153f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return true; 154d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson } 155f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng 156d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson break; 1576994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 1586994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 1596994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // This may not belong here. This function is effectively being overloaded 1606994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // to both detect an intrinsic which needs upgrading, and to provide the 1616994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // upgraded form of the intrinsic. We should perhaps have two separate 1626994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // functions for this. 163f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return false; 1646994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth} 1656994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 166f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Chengbool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { 167f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng NewFn = 0; 168f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); 169a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands 170a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands // Upgrade intrinsic attributes. This does not change the function. 171f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng if (NewFn) 172f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng F = NewFn; 173a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands if (unsigned id = F->getIntrinsicID(true)) 174a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id)); 175a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands return Upgraded; 176a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands} 177a3355ffb3d30d19d226bbb75707991c60f236e37Duncan Sands 1786994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 1796994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// upgraded intrinsic. All argument and return casting must be provided in 1806994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// order to seamlessly integrate with existing context. 1816994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthvoid llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { 1826994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth Function *F = CI->getCalledFunction(); 1836994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth assert(F && "CallInst has no function associated with it."); 184f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng 185f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng if (!NewFn) { 186a5cecd0a130f7ebc4787f8f5b3e8959e41786673Evan Cheng if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0) { 187f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng std::vector<Constant*> Idxs; 188f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Constant *Zero = ConstantInt::get(Type::Int32Ty, 0); 189f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.push_back(Zero); 190f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.push_back(Zero); 191f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.push_back(Zero); 192f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.push_back(Zero); 193f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Value *ZeroV = ConstantVector::get(Idxs); 194f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng 195f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.clear(); 196f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.push_back(ConstantInt::get(Type::Int32Ty, 4)); 197f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.push_back(ConstantInt::get(Type::Int32Ty, 5)); 198f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); 199f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3)); 200f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Value *Mask = ConstantVector::get(Idxs); 201f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng ShuffleVectorInst *SI = new ShuffleVectorInst(ZeroV, CI->getOperand(1), 202f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Mask, "upgraded", CI); 203f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng 204f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng // Handle any uses of the old CallInst. 205f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng if (!CI->use_empty()) 206f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng // Replace all uses of the old call with the new cast which has the 207f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng // correct type. 208f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng CI->replaceAllUsesWith(SI); 209f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng 210f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng // Clean up the old call now that it has been completely upgraded. 211f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng CI->eraseFromParent(); 212a5cecd0a130f7ebc4787f8f5b3e8959e41786673Evan Cheng } else { 213a5cecd0a130f7ebc4787f8f5b3e8959e41786673Evan Cheng assert(0 && "Unknown function for CallInst upgrade."); 214f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng } 215f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng return; 216f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng } 217f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng 2186994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth switch(NewFn->getIntrinsicID()) { 2196994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth default: assert(0 && "Unknown function for CallInst upgrade."); 220d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case Intrinsic::x86_mmx_psll_d: 221d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case Intrinsic::x86_mmx_psll_q: 222d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case Intrinsic::x86_mmx_psll_w: 223d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case Intrinsic::x86_mmx_psra_d: 224d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case Intrinsic::x86_mmx_psra_w: 225d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case Intrinsic::x86_mmx_psrl_d: 226d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case Intrinsic::x86_mmx_psrl_q: 227d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson case Intrinsic::x86_mmx_psrl_w: { 228d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson SmallVector<Value*, 2> Operands; 229d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 230d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson Operands.push_back(CI->getOperand(1)); 231d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 232d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // Cast the second parameter to the correct type. 233d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson BitCastInst *BC = new BitCastInst(CI->getOperand(2), 234d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson NewFn->getFunctionType()->getParamType(1), 235d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson "upgraded", CI); 236d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson Operands.push_back(BC); 237d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 238d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // Construct a new CallInst 239d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(), 240d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson "upgraded."+CI->getName(), CI); 241d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson NewCI->setTailCall(CI->isTailCall()); 242d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson NewCI->setCallingConv(CI->getCallingConv()); 243d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 244d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // Handle any uses of the old CallInst. 245d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson if (!CI->use_empty()) 246d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // Replace all uses of the old call with the new cast which has the 247d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // correct type. 248d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson CI->replaceAllUsesWith(NewCI); 249d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson 250d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson // Clean up the old call now that it has been completely upgraded. 251d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson CI->eraseFromParent(); 252d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson break; 253d04764a8acabe0b3f0452b3099bc5964ba783884Anders Carlsson } 2546994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth case Intrinsic::ctlz: 2556994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth case Intrinsic::ctpop: 2566994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth case Intrinsic::cttz: 2576994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Build a small vector of the 1..(N-1) operands, which are the 2586994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // parameters. 2596994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth SmallVector<Value*, 8> Operands(CI->op_begin()+1, CI->op_end()); 2606994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 2616994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Construct a new CallInst 2626994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth CallInst *NewCI = new CallInst(NewFn, Operands.begin(), Operands.end(), 2636994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth "upgraded."+CI->getName(), CI); 2646994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth NewCI->setTailCall(CI->isTailCall()); 2656994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth NewCI->setCallingConv(CI->getCallingConv()); 2666994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 2676994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Handle any uses of the old CallInst. 2686994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (!CI->use_empty()) { 2696994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Check for sign extend parameter attributes on the return values. 270dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands bool SrcSExt = NewFn->getParamAttrs() && 271dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands NewFn->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt); 272dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands bool DestSExt = F->getParamAttrs() && 273dc024674ff96820d6020757b48d47f46d4c07db2Duncan Sands F->getParamAttrs()->paramHasAttr(0,ParamAttr::SExt); 2746994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 2756994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Construct an appropriate cast from the new return type to the old. 2766994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth CastInst *RetCast = CastInst::create( 2776994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth CastInst::getCastOpcode(NewCI, SrcSExt, 2786994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth F->getReturnType(), 2796994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth DestSExt), 2806994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth NewCI, F->getReturnType(), 2816994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth NewCI->getName(), CI); 2826994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth NewCI->moveBefore(RetCast); 2836994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 2846994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Replace all uses of the old call with the new cast which has the 2856994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // correct type. 2866994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth CI->replaceAllUsesWith(RetCast); 2876994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 2886994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 2896994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Clean up the old call now that it has been completely upgraded. 2906994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth CI->eraseFromParent(); 2916994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth break; 2926994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 2936994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth} 2946994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 2956994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// This tests each Function to determine if it needs upgrading. When we find 2966994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// one we are interested in, we then upgrade all calls to reflect the new 2976994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth// function. 2986994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruthvoid llvm::UpgradeCallsToIntrinsic(Function* F) { 2996994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); 3006994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 3016994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Upgrade the function and check if it is a totaly new function. 302f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng Function* NewFn; 303f9b83fcf95e8a280d7b117af8858596fe5b10d94Evan Cheng if (UpgradeIntrinsicFunction(F, NewFn)) { 3046994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (NewFn != F) { 3056994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Replace all uses to the old function with the new one if necessary. 3066994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth for (Value::use_iterator UI = F->use_begin(), UE = F->use_end(); 3076994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth UI != UE; ) { 3086994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth if (CallInst* CI = dyn_cast<CallInst>(*UI++)) 3096994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth UpgradeIntrinsicCall(CI, NewFn); 3106994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 3116994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth // Remove old function, no longer used, from the module. 3126994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth F->eraseFromParent(); 3136994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 3146994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth } 3156994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth} 3166994040a952e5fb27605eb3cf29ed86c4e59cf62Chandler Carruth 317