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