Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1 | //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the auto-upgrade helper functions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/AutoUpgrade.h" |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/Instructions.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 19 | #include "llvm/Intrinsics.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 21 | #include <cstring> |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 25 | static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 26 | assert(F && "Illegal to upgrade a non-existent Function."); |
| 27 | |
| 28 | // Get the Function's name. |
| 29 | const std::string& Name = F->getName(); |
| 30 | |
| 31 | // Convenience |
| 32 | const FunctionType *FTy = F->getFunctionType(); |
| 33 | |
| 34 | // Quickly eliminate it, if it's not a candidate. |
| 35 | if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || |
| 36 | Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.') |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 37 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 38 | |
| 39 | Module *M = F->getParent(); |
| 40 | switch (Name[5]) { |
| 41 | default: break; |
| 42 | case 'b': |
| 43 | // This upgrades the name of the llvm.bswap intrinsic function to only use |
| 44 | // a single type name for overloading. We only care about the old format |
| 45 | // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being |
| 46 | // a '.' after 'bswap.' |
| 47 | if (Name.compare(5,6,"bswap.",6) == 0) { |
| 48 | std::string::size_type delim = Name.find('.',11); |
| 49 | |
| 50 | if (delim != std::string::npos) { |
| 51 | // Construct the new name as 'llvm.bswap' + '.i*' |
| 52 | F->setName(Name.substr(0,10)+Name.substr(delim)); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 53 | NewFn = F; |
| 54 | return true; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | break; |
| 58 | |
| 59 | case 'c': |
| 60 | // We only want to fix the 'llvm.ct*' intrinsics which do not have the |
| 61 | // correct return type, so we check for the name, and then check if the |
| 62 | // return type does not match the parameter type. |
| 63 | if ( (Name.compare(5,5,"ctpop",5) == 0 || |
| 64 | Name.compare(5,4,"ctlz",4) == 0 || |
| 65 | Name.compare(5,4,"cttz",4) == 0) && |
| 66 | FTy->getReturnType() != FTy->getParamType(0)) { |
| 67 | // We first need to change the name of the old (bad) intrinsic, because |
| 68 | // its type is incorrect, but we cannot overload that name. We |
| 69 | // arbitrarily unique it here allowing us to construct a correctly named |
| 70 | // and typed function below. |
| 71 | F->setName(""); |
| 72 | |
| 73 | // Now construct the new intrinsic with the correct name and type. We |
| 74 | // leave the old function around in order to query its type, whatever it |
| 75 | // may be, and correctly convert up to the new type. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 76 | NewFn = cast<Function>(M->getOrInsertFunction(Name, |
| 77 | FTy->getParamType(0), |
| 78 | FTy->getParamType(0), |
| 79 | (Type *)0)); |
| 80 | return true; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 81 | } |
| 82 | break; |
| 83 | |
| 84 | case 'p': |
| 85 | // This upgrades the llvm.part.select overloaded intrinsic names to only |
| 86 | // use one type specifier in the name. We only care about the old format |
| 87 | // 'llvm.part.select.i*.i*', and solve as above with bswap. |
| 88 | if (Name.compare(5,12,"part.select.",12) == 0) { |
| 89 | std::string::size_type delim = Name.find('.',17); |
| 90 | |
| 91 | if (delim != std::string::npos) { |
| 92 | // Construct a new name as 'llvm.part.select' + '.i*' |
| 93 | F->setName(Name.substr(0,16)+Name.substr(delim)); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 94 | NewFn = F; |
| 95 | return true; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 96 | } |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | // This upgrades the llvm.part.set intrinsics similarly as above, however |
| 101 | // we care about 'llvm.part.set.i*.i*.i*', but only the first two types |
| 102 | // must match. There is an additional type specifier after these two |
| 103 | // matching types that we must retain when upgrading. Thus, we require |
| 104 | // finding 2 periods, not just one, after the intrinsic name. |
| 105 | if (Name.compare(5,9,"part.set.",9) == 0) { |
| 106 | std::string::size_type delim = Name.find('.',14); |
| 107 | |
| 108 | if (delim != std::string::npos && |
| 109 | Name.find('.',delim+1) != std::string::npos) { |
| 110 | // Construct a new name as 'llvm.part.select' + '.i*.i*' |
| 111 | F->setName(Name.substr(0,13)+Name.substr(delim)); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 112 | NewFn = F; |
| 113 | return true; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 114 | } |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | break; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 119 | case 'x': |
| 120 | // This fixes all MMX shift intrinsic instructions to take a |
| 121 | // v1i64 instead of a v2i32 as the second parameter. |
| 122 | if (Name.compare(5,10,"x86.mmx.ps",10) == 0 && |
| 123 | (Name.compare(13,4,"psll", 4) == 0 || |
| 124 | Name.compare(13,4,"psra", 4) == 0 || |
Evan Cheng | cdf22f2 | 2008-05-03 00:52:09 +0000 | [diff] [blame] | 125 | Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') { |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 126 | |
| 127 | const llvm::Type *VT = VectorType::get(IntegerType::get(64), 1); |
| 128 | |
| 129 | // We don't have to do anything if the parameter already has |
| 130 | // the correct type. |
| 131 | if (FTy->getParamType(1) == VT) |
| 132 | break; |
| 133 | |
| 134 | // We first need to change the name of the old (bad) intrinsic, because |
| 135 | // its type is incorrect, but we cannot overload that name. We |
| 136 | // arbitrarily unique it here allowing us to construct a correctly named |
| 137 | // and typed function below. |
| 138 | F->setName(""); |
| 139 | |
| 140 | assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!"); |
| 141 | |
| 142 | // Now construct the new intrinsic with the correct name and type. We |
| 143 | // leave the old function around in order to query its type, whatever it |
| 144 | // may be, and correctly convert up to the new type. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 145 | NewFn = cast<Function>(M->getOrInsertFunction(Name, |
| 146 | FTy->getReturnType(), |
| 147 | FTy->getParamType(0), |
| 148 | VT, |
| 149 | (Type *)0)); |
| 150 | return true; |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 151 | } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 || |
| 152 | Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 || |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame^] | 153 | Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 || |
| 154 | Name.compare(5,15,"x86.sse2.movs.d",15) == 0 || |
| 155 | Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 || |
| 156 | Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 || |
| 157 | Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 ) { |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 158 | // Calls to these intrinsics are transformed into ShuffleVector's. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 159 | NewFn = 0; |
| 160 | return true; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 161 | } |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 162 | |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 163 | break; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // This may not belong here. This function is effectively being overloaded |
| 167 | // to both detect an intrinsic which needs upgrading, and to provide the |
| 168 | // upgraded form of the intrinsic. We should perhaps have two separate |
| 169 | // functions for this. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 170 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 173 | bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { |
| 174 | NewFn = 0; |
| 175 | bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 176 | |
| 177 | // Upgrade intrinsic attributes. This does not change the function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 178 | if (NewFn) |
| 179 | F = NewFn; |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 180 | if (unsigned id = F->getIntrinsicID(true)) |
| 181 | F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id)); |
| 182 | return Upgraded; |
| 183 | } |
| 184 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 185 | // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the |
| 186 | // upgraded intrinsic. All argument and return casting must be provided in |
| 187 | // order to seamlessly integrate with existing context. |
| 188 | void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 189 | Function *F = CI->getCalledFunction(); |
| 190 | assert(F && "CallInst has no function associated with it."); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 191 | |
| 192 | if (!NewFn) { |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 193 | bool isLoadH = false, isLoadL = false, isMovL = false; |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame^] | 194 | bool isMovSD = false, isShufPD = false; |
| 195 | bool isUnpckhPD = false, isUnpcklPD = false; |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 196 | if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadh.pd") == 0) |
| 197 | isLoadH = true; |
| 198 | else if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadl.pd") == 0) |
| 199 | isLoadL = true; |
| 200 | else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0) |
| 201 | isMovL = true; |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame^] | 202 | else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movs.d") == 0) |
| 203 | isMovSD = true; |
| 204 | else if (strcmp(F->getNameStart(), "llvm.x86.sse2.shuf.pd") == 0) |
| 205 | isShufPD = true; |
| 206 | else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckh.pd") == 0) |
| 207 | isUnpckhPD = true; |
| 208 | else if (strcmp(F->getNameStart(), "llvm.x86.sse2.unpckl.pd") == 0) |
| 209 | isUnpcklPD = true; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 210 | |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame^] | 211 | if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD || |
| 212 | isUnpckhPD || isUnpcklPD) { |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 213 | std::vector<Constant*> Idxs; |
| 214 | Value *Op0 = CI->getOperand(1); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame^] | 215 | ShuffleVectorInst *SI = NULL; |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 216 | if (isLoadH || isLoadL) { |
| 217 | Value *Op1 = UndefValue::get(Op0->getType()); |
| 218 | Value *Addr = new BitCastInst(CI->getOperand(2), |
| 219 | PointerType::getUnqual(Type::DoubleTy), |
| 220 | "upgraded.", CI); |
| 221 | Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI); |
| 222 | Value *Idx = ConstantInt::get(Type::Int32Ty, 0); |
| 223 | Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI); |
| 224 | |
| 225 | if (isLoadH) { |
| 226 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 0)); |
| 227 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); |
| 228 | } else { |
| 229 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); |
| 230 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1)); |
| 231 | } |
| 232 | Value *Mask = ConstantVector::get(Idxs); |
| 233 | SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame^] | 234 | } else if (isMovL) { |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 235 | Constant *Zero = ConstantInt::get(Type::Int32Ty, 0); |
| 236 | Idxs.push_back(Zero); |
| 237 | Idxs.push_back(Zero); |
| 238 | Idxs.push_back(Zero); |
| 239 | Idxs.push_back(Zero); |
| 240 | Value *ZeroV = ConstantVector::get(Idxs); |
| 241 | |
| 242 | Idxs.clear(); |
| 243 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 4)); |
| 244 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 5)); |
| 245 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); |
| 246 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3)); |
| 247 | Value *Mask = ConstantVector::get(Idxs); |
| 248 | SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame^] | 249 | } else if (isMovSD || isUnpckhPD || isUnpcklPD) { |
| 250 | Value *Op1 = CI->getOperand(2); |
| 251 | if (isMovSD) { |
| 252 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); |
| 253 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1)); |
| 254 | } else if (isUnpckhPD) { |
| 255 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1)); |
| 256 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3)); |
| 257 | } else { |
| 258 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 0)); |
| 259 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2)); |
| 260 | } |
| 261 | Value *Mask = ConstantVector::get(Idxs); |
| 262 | SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); |
| 263 | } else if (isShufPD) { |
| 264 | Value *Op1 = CI->getOperand(2); |
| 265 | unsigned MaskVal = cast<ConstantInt>(CI->getOperand(3))->getZExtValue(); |
| 266 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, MaskVal & 1)); |
| 267 | Idxs.push_back(ConstantInt::get(Type::Int32Ty, ((MaskVal >> 1) & 1)+2)); |
| 268 | Value *Mask = ConstantVector::get(Idxs); |
| 269 | SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 270 | } |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 271 | |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame^] | 272 | assert(SI && "Unexpected!"); |
| 273 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 274 | // Handle any uses of the old CallInst. |
| 275 | if (!CI->use_empty()) |
| 276 | // Replace all uses of the old call with the new cast which has the |
| 277 | // correct type. |
| 278 | CI->replaceAllUsesWith(SI); |
| 279 | |
| 280 | // Clean up the old call now that it has been completely upgraded. |
| 281 | CI->eraseFromParent(); |
Evan Cheng | a8288f4 | 2007-12-18 01:04:25 +0000 | [diff] [blame] | 282 | } else { |
| 283 | assert(0 && "Unknown function for CallInst upgrade."); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 284 | } |
| 285 | return; |
| 286 | } |
| 287 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 288 | switch (NewFn->getIntrinsicID()) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 289 | default: assert(0 && "Unknown function for CallInst upgrade."); |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 290 | case Intrinsic::x86_mmx_psll_d: |
| 291 | case Intrinsic::x86_mmx_psll_q: |
| 292 | case Intrinsic::x86_mmx_psll_w: |
| 293 | case Intrinsic::x86_mmx_psra_d: |
| 294 | case Intrinsic::x86_mmx_psra_w: |
| 295 | case Intrinsic::x86_mmx_psrl_d: |
| 296 | case Intrinsic::x86_mmx_psrl_q: |
| 297 | case Intrinsic::x86_mmx_psrl_w: { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 298 | Value *Operands[2]; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 300 | Operands[0] = CI->getOperand(1); |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 301 | |
| 302 | // Cast the second parameter to the correct type. |
| 303 | BitCastInst *BC = new BitCastInst(CI->getOperand(2), |
| 304 | NewFn->getFunctionType()->getParamType(1), |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 305 | "upgraded.", CI); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 306 | Operands[1] = BC; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 307 | |
| 308 | // Construct a new CallInst |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 309 | CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2, |
| 310 | "upgraded."+CI->getName(), CI); |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 311 | NewCI->setTailCall(CI->isTailCall()); |
| 312 | NewCI->setCallingConv(CI->getCallingConv()); |
| 313 | |
| 314 | // Handle any uses of the old CallInst. |
| 315 | if (!CI->use_empty()) |
| 316 | // Replace all uses of the old call with the new cast which has the |
| 317 | // correct type. |
| 318 | CI->replaceAllUsesWith(NewCI); |
| 319 | |
| 320 | // Clean up the old call now that it has been completely upgraded. |
| 321 | CI->eraseFromParent(); |
| 322 | break; |
| 323 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 324 | case Intrinsic::ctlz: |
| 325 | case Intrinsic::ctpop: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 326 | case Intrinsic::cttz: { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 327 | // Build a small vector of the 1..(N-1) operands, which are the |
| 328 | // parameters. |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 329 | SmallVector<Value*, 8> Operands(CI->op_begin()+1, CI->op_end()); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 330 | |
| 331 | // Construct a new CallInst |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 332 | CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(), |
| 333 | "upgraded."+CI->getName(), CI); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 334 | NewCI->setTailCall(CI->isTailCall()); |
| 335 | NewCI->setCallingConv(CI->getCallingConv()); |
| 336 | |
| 337 | // Handle any uses of the old CallInst. |
| 338 | if (!CI->use_empty()) { |
| 339 | // Check for sign extend parameter attributes on the return values. |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 340 | bool SrcSExt = NewFn->getParamAttrs().paramHasAttr(0, ParamAttr::SExt); |
| 341 | bool DestSExt = F->getParamAttrs().paramHasAttr(0, ParamAttr::SExt); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 342 | |
| 343 | // Construct an appropriate cast from the new return type to the old. |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 344 | CastInst *RetCast = CastInst::Create( |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 345 | CastInst::getCastOpcode(NewCI, SrcSExt, |
| 346 | F->getReturnType(), |
| 347 | DestSExt), |
| 348 | NewCI, F->getReturnType(), |
| 349 | NewCI->getName(), CI); |
| 350 | NewCI->moveBefore(RetCast); |
| 351 | |
| 352 | // Replace all uses of the old call with the new cast which has the |
| 353 | // correct type. |
| 354 | CI->replaceAllUsesWith(RetCast); |
| 355 | } |
| 356 | |
| 357 | // Clean up the old call now that it has been completely upgraded. |
| 358 | CI->eraseFromParent(); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 359 | } |
| 360 | break; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | // This tests each Function to determine if it needs upgrading. When we find |
| 365 | // one we are interested in, we then upgrade all calls to reflect the new |
| 366 | // function. |
| 367 | void llvm::UpgradeCallsToIntrinsic(Function* F) { |
| 368 | assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); |
| 369 | |
| 370 | // Upgrade the function and check if it is a totaly new function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 371 | Function* NewFn; |
| 372 | if (UpgradeIntrinsicFunction(F, NewFn)) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 373 | if (NewFn != F) { |
| 374 | // Replace all uses to the old function with the new one if necessary. |
| 375 | for (Value::use_iterator UI = F->use_begin(), UE = F->use_end(); |
| 376 | UI != UE; ) { |
| 377 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 378 | UpgradeIntrinsicCall(CI, NewFn); |
| 379 | } |
| 380 | // Remove old function, no longer used, from the module. |
| 381 | F->eraseFromParent(); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |