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