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