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" |
Gabor Greif | e540653 | 2010-06-23 08:45:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CallSite.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 64831c6 | 2010-04-20 00:59:54 +0000 | [diff] [blame] | 23 | #include "llvm/Support/IRBuilder.h" |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 24 | #include <cstring> |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 28 | static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 29 | assert(F && "Illegal to upgrade a non-existent Function."); |
| 30 | |
| 31 | // Get the Function's name. |
| 32 | const std::string& Name = F->getName(); |
| 33 | |
| 34 | // Convenience |
| 35 | const FunctionType *FTy = F->getFunctionType(); |
| 36 | |
| 37 | // Quickly eliminate it, if it's not a candidate. |
| 38 | if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || |
| 39 | Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.') |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 40 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 41 | |
| 42 | Module *M = F->getParent(); |
| 43 | switch (Name[5]) { |
| 44 | default: break; |
Mon P Wang | 6a49037 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 45 | case 'a': |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 46 | // This upgrades the llvm.atomic.lcs, llvm.atomic.las, llvm.atomic.lss, |
| 47 | // and atomics with default address spaces to their new names to their new |
| 48 | // function name (e.g. llvm.atomic.add.i32 => llvm.atomic.add.i32.p0i32) |
| 49 | if (Name.compare(5,7,"atomic.",7) == 0) { |
Mon P Wang | 6a49037 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 50 | if (Name.compare(12,3,"lcs",3) == 0) { |
| 51 | std::string::size_type delim = Name.find('.',12); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 52 | F->setName("llvm.atomic.cmp.swap" + Name.substr(delim) + |
| 53 | ".p0" + Name.substr(delim+1)); |
Mon P Wang | 6a49037 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 54 | NewFn = F; |
| 55 | return true; |
| 56 | } |
| 57 | else if (Name.compare(12,3,"las",3) == 0) { |
| 58 | std::string::size_type delim = Name.find('.',12); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 59 | F->setName("llvm.atomic.load.add"+Name.substr(delim) |
| 60 | + ".p0" + Name.substr(delim+1)); |
Mon P Wang | 6a49037 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 61 | NewFn = F; |
| 62 | return true; |
| 63 | } |
| 64 | else if (Name.compare(12,3,"lss",3) == 0) { |
| 65 | std::string::size_type delim = Name.find('.',12); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 66 | F->setName("llvm.atomic.load.sub"+Name.substr(delim) |
| 67 | + ".p0" + Name.substr(delim+1)); |
| 68 | NewFn = F; |
| 69 | return true; |
| 70 | } |
| 71 | else if (Name.rfind(".p") == std::string::npos) { |
| 72 | // We don't have an address space qualifier so this has be upgraded |
| 73 | // to the new name. Copy the type name at the end of the intrinsic |
| 74 | // and add to it |
| 75 | std::string::size_type delim = Name.find_last_of('.'); |
| 76 | assert(delim != std::string::npos && "can not find type"); |
| 77 | F->setName(Name + ".p0" + Name.substr(delim+1)); |
Mon P Wang | 6a49037 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 78 | NewFn = F; |
| 79 | return true; |
| 80 | } |
Bob Wilson | 9a511c0 | 2010-08-20 04:54:02 +0000 | [diff] [blame] | 81 | } else if (Name.compare(5, 9, "arm.neon.", 9) == 0) { |
| 82 | if (Name.compare(14, 7, "vmovls.", 7) == 0 || |
| 83 | Name.compare(14, 7, "vmovlu.", 7) == 0) { |
| 84 | // Calls to these are transformed into IR without intrinsics. |
| 85 | NewFn = 0; |
| 86 | return true; |
| 87 | } |
Bob Wilson | edf722a | 2010-08-27 17:13:24 +0000 | [diff] [blame] | 88 | // Old versions of NEON ld/st intrinsics are missing alignment arguments. |
| 89 | bool isVLd = (Name.compare(14, 3, "vld", 3) == 0); |
| 90 | bool isVSt = (Name.compare(14, 3, "vst", 3) == 0); |
| 91 | if (isVLd || isVSt) { |
| 92 | unsigned NumVecs = Name.at(17) - '0'; |
| 93 | if (NumVecs == 0 || NumVecs > 4) |
| 94 | return false; |
| 95 | bool isLaneOp = (Name.compare(18, 5, "lane.", 5) == 0); |
| 96 | if (!isLaneOp && Name.at(18) != '.') |
| 97 | return false; |
| 98 | unsigned ExpectedArgs = 2; // for the address and alignment |
| 99 | if (isVSt || isLaneOp) |
| 100 | ExpectedArgs += NumVecs; |
| 101 | if (isLaneOp) |
| 102 | ExpectedArgs += 1; // for the lane number |
| 103 | unsigned NumP = FTy->getNumParams(); |
| 104 | if (NumP != ExpectedArgs - 1) |
| 105 | return false; |
| 106 | |
| 107 | // Change the name of the old (bad) intrinsic, because |
| 108 | // its type is incorrect, but we cannot overload that name. |
| 109 | F->setName(""); |
| 110 | |
| 111 | // One argument is missing: add the alignment argument. |
| 112 | std::vector<const Type*> NewParams; |
| 113 | for (unsigned p = 0; p < NumP; ++p) |
| 114 | NewParams.push_back(FTy->getParamType(p)); |
| 115 | NewParams.push_back(Type::getInt32Ty(F->getContext())); |
| 116 | FunctionType *NewFTy = FunctionType::get(FTy->getReturnType(), |
| 117 | NewParams, false); |
| 118 | NewFn = cast<Function>(M->getOrInsertFunction(Name, NewFTy)); |
| 119 | return true; |
| 120 | } |
Mon P Wang | 6a49037 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 121 | } |
| 122 | break; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 123 | case 'b': |
| 124 | // This upgrades the name of the llvm.bswap intrinsic function to only use |
| 125 | // a single type name for overloading. We only care about the old format |
| 126 | // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being |
| 127 | // a '.' after 'bswap.' |
| 128 | if (Name.compare(5,6,"bswap.",6) == 0) { |
| 129 | std::string::size_type delim = Name.find('.',11); |
| 130 | |
| 131 | if (delim != std::string::npos) { |
| 132 | // Construct the new name as 'llvm.bswap' + '.i*' |
| 133 | F->setName(Name.substr(0,10)+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 | } |
| 138 | break; |
| 139 | |
| 140 | case 'c': |
| 141 | // We only want to fix the 'llvm.ct*' intrinsics which do not have the |
| 142 | // correct return type, so we check for the name, and then check if the |
| 143 | // return type does not match the parameter type. |
| 144 | if ( (Name.compare(5,5,"ctpop",5) == 0 || |
| 145 | Name.compare(5,4,"ctlz",4) == 0 || |
| 146 | Name.compare(5,4,"cttz",4) == 0) && |
| 147 | FTy->getReturnType() != FTy->getParamType(0)) { |
| 148 | // We first need to change the name of the old (bad) intrinsic, because |
| 149 | // its type is incorrect, but we cannot overload that name. We |
| 150 | // arbitrarily unique it here allowing us to construct a correctly named |
| 151 | // and typed function below. |
| 152 | F->setName(""); |
| 153 | |
| 154 | // Now construct the new intrinsic with the correct name and type. We |
| 155 | // leave the old function around in order to query its type, whatever it |
| 156 | // may be, and correctly convert up to the new type. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 157 | NewFn = cast<Function>(M->getOrInsertFunction(Name, |
| 158 | FTy->getParamType(0), |
| 159 | FTy->getParamType(0), |
| 160 | (Type *)0)); |
| 161 | return true; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 162 | } |
| 163 | break; |
| 164 | |
Duncan Sands | 8e6ccb6 | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 165 | case 'e': |
| 166 | // The old llvm.eh.selector.i32 is equivalent to the new llvm.eh.selector. |
| 167 | if (Name.compare("llvm.eh.selector.i32") == 0) { |
| 168 | F->setName("llvm.eh.selector"); |
| 169 | NewFn = F; |
| 170 | return true; |
| 171 | } |
| 172 | // The old llvm.eh.typeid.for.i32 is equivalent to llvm.eh.typeid.for. |
| 173 | if (Name.compare("llvm.eh.typeid.for.i32") == 0) { |
| 174 | F->setName("llvm.eh.typeid.for"); |
| 175 | NewFn = F; |
| 176 | return true; |
| 177 | } |
| 178 | // Convert the old llvm.eh.selector.i64 to a call to llvm.eh.selector. |
| 179 | if (Name.compare("llvm.eh.selector.i64") == 0) { |
| 180 | NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_selector); |
| 181 | return true; |
| 182 | } |
| 183 | // Convert the old llvm.eh.typeid.for.i64 to a call to llvm.eh.typeid.for. |
| 184 | if (Name.compare("llvm.eh.typeid.for.i64") == 0) { |
| 185 | NewFn = Intrinsic::getDeclaration(M, Intrinsic::eh_typeid_for); |
| 186 | return true; |
| 187 | } |
| 188 | break; |
| 189 | |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 190 | case 'm': { |
| 191 | // This upgrades the llvm.memcpy, llvm.memmove, and llvm.memset to the |
| 192 | // new format that allows overloading the pointer for different address |
| 193 | // space (e.g., llvm.memcpy.i16 => llvm.memcpy.p0i8.p0i8.i16) |
| 194 | const char* NewFnName = NULL; |
| 195 | if (Name.compare(5,8,"memcpy.i",8) == 0) { |
| 196 | if (Name[13] == '8') |
| 197 | NewFnName = "llvm.memcpy.p0i8.p0i8.i8"; |
| 198 | else if (Name.compare(13,2,"16") == 0) |
| 199 | NewFnName = "llvm.memcpy.p0i8.p0i8.i16"; |
| 200 | else if (Name.compare(13,2,"32") == 0) |
| 201 | NewFnName = "llvm.memcpy.p0i8.p0i8.i32"; |
| 202 | else if (Name.compare(13,2,"64") == 0) |
| 203 | NewFnName = "llvm.memcpy.p0i8.p0i8.i64"; |
| 204 | } else if (Name.compare(5,9,"memmove.i",9) == 0) { |
| 205 | if (Name[14] == '8') |
| 206 | NewFnName = "llvm.memmove.p0i8.p0i8.i8"; |
| 207 | else if (Name.compare(14,2,"16") == 0) |
| 208 | NewFnName = "llvm.memmove.p0i8.p0i8.i16"; |
| 209 | else if (Name.compare(14,2,"32") == 0) |
| 210 | NewFnName = "llvm.memmove.p0i8.p0i8.i32"; |
| 211 | else if (Name.compare(14,2,"64") == 0) |
| 212 | NewFnName = "llvm.memmove.p0i8.p0i8.i64"; |
| 213 | } |
| 214 | else if (Name.compare(5,8,"memset.i",8) == 0) { |
| 215 | if (Name[13] == '8') |
| 216 | NewFnName = "llvm.memset.p0i8.i8"; |
| 217 | else if (Name.compare(13,2,"16") == 0) |
| 218 | NewFnName = "llvm.memset.p0i8.i16"; |
| 219 | else if (Name.compare(13,2,"32") == 0) |
| 220 | NewFnName = "llvm.memset.p0i8.i32"; |
| 221 | else if (Name.compare(13,2,"64") == 0) |
| 222 | NewFnName = "llvm.memset.p0i8.i64"; |
| 223 | } |
| 224 | if (NewFnName) { |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 225 | NewFn = cast<Function>(M->getOrInsertFunction(NewFnName, |
| 226 | FTy->getReturnType(), |
| 227 | FTy->getParamType(0), |
| 228 | FTy->getParamType(1), |
| 229 | FTy->getParamType(2), |
| 230 | FTy->getParamType(3), |
| 231 | Type::getInt1Ty(F->getContext()), |
| 232 | (Type *)0)); |
| 233 | return true; |
| 234 | } |
| 235 | break; |
| 236 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 237 | case 'p': |
| 238 | // This upgrades the llvm.part.select overloaded intrinsic names to only |
| 239 | // use one type specifier in the name. We only care about the old format |
| 240 | // 'llvm.part.select.i*.i*', and solve as above with bswap. |
| 241 | if (Name.compare(5,12,"part.select.",12) == 0) { |
| 242 | std::string::size_type delim = Name.find('.',17); |
| 243 | |
| 244 | if (delim != std::string::npos) { |
| 245 | // Construct a new name as 'llvm.part.select' + '.i*' |
| 246 | F->setName(Name.substr(0,16)+Name.substr(delim)); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 247 | NewFn = F; |
| 248 | return true; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 249 | } |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | // This upgrades the llvm.part.set intrinsics similarly as above, however |
| 254 | // we care about 'llvm.part.set.i*.i*.i*', but only the first two types |
| 255 | // must match. There is an additional type specifier after these two |
| 256 | // matching types that we must retain when upgrading. Thus, we require |
| 257 | // finding 2 periods, not just one, after the intrinsic name. |
| 258 | if (Name.compare(5,9,"part.set.",9) == 0) { |
| 259 | std::string::size_type delim = Name.find('.',14); |
| 260 | |
| 261 | if (delim != std::string::npos && |
| 262 | Name.find('.',delim+1) != std::string::npos) { |
| 263 | // Construct a new name as 'llvm.part.select' + '.i*.i*' |
| 264 | F->setName(Name.substr(0,13)+Name.substr(delim)); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 265 | NewFn = F; |
| 266 | return true; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 267 | } |
| 268 | break; |
| 269 | } |
| 270 | |
| 271 | break; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 272 | case 'x': |
| 273 | // This fixes all MMX shift intrinsic instructions to take a |
| 274 | // v1i64 instead of a v2i32 as the second parameter. |
| 275 | if (Name.compare(5,10,"x86.mmx.ps",10) == 0 && |
| 276 | (Name.compare(13,4,"psll", 4) == 0 || |
| 277 | Name.compare(13,4,"psra", 4) == 0 || |
Evan Cheng | cdf22f2 | 2008-05-03 00:52:09 +0000 | [diff] [blame] | 278 | Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') { |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 279 | |
Owen Anderson | 155dccd8 | 2009-07-07 23:43:39 +0000 | [diff] [blame] | 280 | const llvm::Type *VT = |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 281 | VectorType::get(IntegerType::get(FTy->getContext(), 64), 1); |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 282 | |
| 283 | // We don't have to do anything if the parameter already has |
| 284 | // the correct type. |
| 285 | if (FTy->getParamType(1) == VT) |
| 286 | break; |
| 287 | |
| 288 | // We first need to change the name of the old (bad) intrinsic, because |
| 289 | // its type is incorrect, but we cannot overload that name. We |
| 290 | // arbitrarily unique it here allowing us to construct a correctly named |
| 291 | // and typed function below. |
| 292 | F->setName(""); |
| 293 | |
| 294 | assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!"); |
| 295 | |
| 296 | // Now construct the new intrinsic with the correct name and type. We |
| 297 | // leave the old function around in order to query its type, whatever it |
| 298 | // may be, and correctly convert up to the new type. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 299 | NewFn = cast<Function>(M->getOrInsertFunction(Name, |
| 300 | FTy->getReturnType(), |
| 301 | FTy->getParamType(0), |
| 302 | VT, |
| 303 | (Type *)0)); |
| 304 | return true; |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 305 | } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 || |
| 306 | Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 || |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 307 | Name.compare(5,16,"x86.sse2.movl.dq",16) == 0 || |
| 308 | Name.compare(5,15,"x86.sse2.movs.d",15) == 0 || |
| 309 | Name.compare(5,16,"x86.sse2.shuf.pd",16) == 0 || |
| 310 | Name.compare(5,18,"x86.sse2.unpckh.pd",18) == 0 || |
Evan Cheng | 91a2e56 | 2008-05-24 02:56:30 +0000 | [diff] [blame] | 311 | Name.compare(5,18,"x86.sse2.unpckl.pd",18) == 0 || |
| 312 | Name.compare(5,20,"x86.sse2.punpckh.qdq",20) == 0 || |
| 313 | Name.compare(5,20,"x86.sse2.punpckl.qdq",20) == 0) { |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 314 | // Calls to these intrinsics are transformed into ShuffleVector's. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 315 | NewFn = 0; |
| 316 | return true; |
Eric Christopher | 6ad8167 | 2010-03-30 18:49:01 +0000 | [diff] [blame] | 317 | } else if (Name.compare(5, 16, "x86.sse41.pmulld", 16) == 0) { |
| 318 | // Calls to these intrinsics are transformed into vector multiplies. |
| 319 | NewFn = 0; |
| 320 | return true; |
Eric Christopher | 64831c6 | 2010-04-20 00:59:54 +0000 | [diff] [blame] | 321 | } else if (Name.compare(5, 18, "x86.ssse3.palign.r", 18) == 0 || |
| 322 | Name.compare(5, 22, "x86.ssse3.palign.r.128", 22) == 0) { |
| 323 | // Calls to these intrinsics are transformed into vector shuffles, shifts, |
| 324 | // or 0. |
| 325 | NewFn = 0; |
| 326 | return true; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 327 | } |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 328 | |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 329 | break; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // This may not belong here. This function is effectively being overloaded |
| 333 | // to both detect an intrinsic which needs upgrading, and to provide the |
| 334 | // upgraded form of the intrinsic. We should perhaps have two separate |
| 335 | // functions for this. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 336 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 339 | bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { |
| 340 | NewFn = 0; |
| 341 | bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 342 | |
| 343 | // Upgrade intrinsic attributes. This does not change the function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 344 | if (NewFn) |
| 345 | F = NewFn; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 346 | if (unsigned id = F->getIntrinsicID()) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 347 | F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id)); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 348 | return Upgraded; |
| 349 | } |
| 350 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 351 | // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the |
| 352 | // upgraded intrinsic. All argument and return casting must be provided in |
| 353 | // order to seamlessly integrate with existing context. |
| 354 | void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 355 | Function *F = CI->getCalledFunction(); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 356 | LLVMContext &C = CI->getContext(); |
Gabor Greif | e540653 | 2010-06-23 08:45:32 +0000 | [diff] [blame] | 357 | ImmutableCallSite CS(CI); |
| 358 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 359 | assert(F && "CallInst has no function associated with it."); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 360 | |
| 361 | if (!NewFn) { |
Bob Wilson | 9a511c0 | 2010-08-20 04:54:02 +0000 | [diff] [blame] | 362 | // Get the Function's name. |
| 363 | const std::string& Name = F->getName(); |
| 364 | |
| 365 | // Upgrade ARM NEON intrinsics. |
| 366 | if (Name.compare(5, 9, "arm.neon.", 9) == 0) { |
| 367 | Instruction *NewI; |
| 368 | if (Name.compare(14, 7, "vmovls.", 7) == 0) { |
| 369 | NewI = new SExtInst(CI->getArgOperand(0), CI->getType(), |
| 370 | "upgraded." + CI->getName(), CI); |
| 371 | } else if (Name.compare(14, 7, "vmovlu.", 7) == 0) { |
| 372 | NewI = new ZExtInst(CI->getArgOperand(0), CI->getType(), |
| 373 | "upgraded." + CI->getName(), CI); |
| 374 | } else { |
| 375 | llvm_unreachable("Unknown arm.neon function for CallInst upgrade."); |
| 376 | } |
| 377 | // Replace any uses of the old CallInst. |
| 378 | if (!CI->use_empty()) |
| 379 | CI->replaceAllUsesWith(NewI); |
| 380 | CI->eraseFromParent(); |
| 381 | return; |
| 382 | } |
| 383 | |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 384 | bool isLoadH = false, isLoadL = false, isMovL = false; |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 385 | bool isMovSD = false, isShufPD = false; |
| 386 | bool isUnpckhPD = false, isUnpcklPD = false; |
Evan Cheng | 91a2e56 | 2008-05-24 02:56:30 +0000 | [diff] [blame] | 387 | bool isPunpckhQPD = false, isPunpcklQPD = false; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 388 | if (F->getName() == "llvm.x86.sse2.loadh.pd") |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 389 | isLoadH = true; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 390 | else if (F->getName() == "llvm.x86.sse2.loadl.pd") |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 391 | isLoadL = true; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 392 | else if (F->getName() == "llvm.x86.sse2.movl.dq") |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 393 | isMovL = true; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 394 | else if (F->getName() == "llvm.x86.sse2.movs.d") |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 395 | isMovSD = true; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 396 | else if (F->getName() == "llvm.x86.sse2.shuf.pd") |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 397 | isShufPD = true; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 398 | else if (F->getName() == "llvm.x86.sse2.unpckh.pd") |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 399 | isUnpckhPD = true; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 400 | else if (F->getName() == "llvm.x86.sse2.unpckl.pd") |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 401 | isUnpcklPD = true; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 402 | else if (F->getName() == "llvm.x86.sse2.punpckh.qdq") |
Evan Cheng | 91a2e56 | 2008-05-24 02:56:30 +0000 | [diff] [blame] | 403 | isPunpckhQPD = true; |
Daniel Dunbar | e03eecb | 2009-07-25 23:55:21 +0000 | [diff] [blame] | 404 | else if (F->getName() == "llvm.x86.sse2.punpckl.qdq") |
Evan Cheng | 91a2e56 | 2008-05-24 02:56:30 +0000 | [diff] [blame] | 405 | isPunpcklQPD = true; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 406 | |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 407 | if (isLoadH || isLoadL || isMovL || isMovSD || isShufPD || |
Evan Cheng | 91a2e56 | 2008-05-24 02:56:30 +0000 | [diff] [blame] | 408 | isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) { |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 409 | std::vector<Constant*> Idxs; |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 410 | Value *Op0 = CI->getArgOperand(0); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 411 | ShuffleVectorInst *SI = NULL; |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 412 | if (isLoadH || isLoadL) { |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 413 | Value *Op1 = UndefValue::get(Op0->getType()); |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 414 | Value *Addr = new BitCastInst(CI->getArgOperand(1), |
Duncan Sands | 9ed7b16 | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 415 | Type::getDoublePtrTy(C), |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 416 | "upgraded.", CI); |
| 417 | Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 418 | Value *Idx = ConstantInt::get(Type::getInt32Ty(C), 0); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 419 | Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI); |
| 420 | |
| 421 | if (isLoadH) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 422 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0)); |
| 423 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2)); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 424 | } else { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 425 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2)); |
| 426 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 427 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 428 | Value *Mask = ConstantVector::get(Idxs); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 429 | SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 430 | } else if (isMovL) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 431 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(C), 0); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 432 | Idxs.push_back(Zero); |
| 433 | Idxs.push_back(Zero); |
| 434 | Idxs.push_back(Zero); |
| 435 | Idxs.push_back(Zero); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 436 | Value *ZeroV = ConstantVector::get(Idxs); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 437 | |
| 438 | Idxs.clear(); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 439 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 4)); |
| 440 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 5)); |
| 441 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2)); |
| 442 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 443 | Value *Mask = ConstantVector::get(Idxs); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 444 | SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI); |
Evan Cheng | 91a2e56 | 2008-05-24 02:56:30 +0000 | [diff] [blame] | 445 | } else if (isMovSD || |
| 446 | isUnpckhPD || isUnpcklPD || isPunpckhQPD || isPunpcklQPD) { |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 447 | Value *Op1 = CI->getArgOperand(1); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 448 | if (isMovSD) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 449 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2)); |
| 450 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); |
Evan Cheng | 91a2e56 | 2008-05-24 02:56:30 +0000 | [diff] [blame] | 451 | } else if (isUnpckhPD || isPunpckhQPD) { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 452 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); |
| 453 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 3)); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 454 | } else { |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 455 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 0)); |
| 456 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), 2)); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 457 | } |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 458 | Value *Mask = ConstantVector::get(Idxs); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 459 | SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); |
| 460 | } else if (isShufPD) { |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 461 | Value *Op1 = CI->getArgOperand(1); |
Gabor Greif | 3e44ea1 | 2010-07-22 10:37:47 +0000 | [diff] [blame] | 462 | unsigned MaskVal = |
| 463 | cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue(); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 464 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), MaskVal & 1)); |
| 465 | Idxs.push_back(ConstantInt::get(Type::getInt32Ty(C), |
Owen Anderson | 155dccd8 | 2009-07-07 23:43:39 +0000 | [diff] [blame] | 466 | ((MaskVal >> 1) & 1)+2)); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 467 | Value *Mask = ConstantVector::get(Idxs); |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 468 | SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI); |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 469 | } |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 470 | |
Evan Cheng | 2146270c | 2008-05-24 02:14:05 +0000 | [diff] [blame] | 471 | assert(SI && "Unexpected!"); |
| 472 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 473 | // Handle any uses of the old CallInst. |
| 474 | if (!CI->use_empty()) |
| 475 | // Replace all uses of the old call with the new cast which has the |
| 476 | // correct type. |
| 477 | CI->replaceAllUsesWith(SI); |
| 478 | |
| 479 | // Clean up the old call now that it has been completely upgraded. |
| 480 | CI->eraseFromParent(); |
Eric Christopher | 6ad8167 | 2010-03-30 18:49:01 +0000 | [diff] [blame] | 481 | } else if (F->getName() == "llvm.x86.sse41.pmulld") { |
| 482 | // Upgrade this set of intrinsics into vector multiplies. |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 483 | Instruction *Mul = BinaryOperator::CreateMul(CI->getArgOperand(0), |
| 484 | CI->getArgOperand(1), |
Eric Christopher | 6ad8167 | 2010-03-30 18:49:01 +0000 | [diff] [blame] | 485 | CI->getName(), |
| 486 | CI); |
| 487 | // Fix up all the uses with our new multiply. |
| 488 | if (!CI->use_empty()) |
| 489 | CI->replaceAllUsesWith(Mul); |
| 490 | |
| 491 | // Remove upgraded multiply. |
| 492 | CI->eraseFromParent(); |
Eric Christopher | 64831c6 | 2010-04-20 00:59:54 +0000 | [diff] [blame] | 493 | } else if (F->getName() == "llvm.x86.ssse3.palign.r") { |
Gabor Greif | eab748d | 2010-06-29 16:17:26 +0000 | [diff] [blame] | 494 | Value *Op1 = CI->getArgOperand(0); |
| 495 | Value *Op2 = CI->getArgOperand(1); |
| 496 | Value *Op3 = CI->getArgOperand(2); |
Eric Christopher | 64831c6 | 2010-04-20 00:59:54 +0000 | [diff] [blame] | 497 | unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue(); |
| 498 | Value *Rep; |
| 499 | IRBuilder<> Builder(C); |
| 500 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 501 | |
| 502 | // If palignr is shifting the pair of input vectors less than 9 bytes, |
| 503 | // emit a shuffle instruction. |
| 504 | if (shiftVal <= 8) { |
| 505 | const Type *IntTy = Type::getInt32Ty(C); |
| 506 | const Type *EltTy = Type::getInt8Ty(C); |
| 507 | const Type *VecTy = VectorType::get(EltTy, 8); |
| 508 | |
| 509 | Op2 = Builder.CreateBitCast(Op2, VecTy); |
| 510 | Op1 = Builder.CreateBitCast(Op1, VecTy); |
| 511 | |
| 512 | llvm::SmallVector<llvm::Constant*, 8> Indices; |
| 513 | for (unsigned i = 0; i != 8; ++i) |
| 514 | Indices.push_back(ConstantInt::get(IntTy, shiftVal + i)); |
| 515 | |
| 516 | Value *SV = ConstantVector::get(Indices.begin(), Indices.size()); |
| 517 | Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr"); |
| 518 | Rep = Builder.CreateBitCast(Rep, F->getReturnType()); |
| 519 | } |
| 520 | |
| 521 | // If palignr is shifting the pair of input vectors more than 8 but less |
| 522 | // than 16 bytes, emit a logical right shift of the destination. |
| 523 | else if (shiftVal < 16) { |
| 524 | // MMX has these as 1 x i64 vectors for some odd optimization reasons. |
| 525 | const Type *EltTy = Type::getInt64Ty(C); |
| 526 | const Type *VecTy = VectorType::get(EltTy, 1); |
| 527 | |
| 528 | Op1 = Builder.CreateBitCast(Op1, VecTy, "cast"); |
| 529 | Op2 = ConstantInt::get(VecTy, (shiftVal-8) * 8); |
| 530 | |
| 531 | // create i32 constant |
| 532 | Function *I = |
| 533 | Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_mmx_psrl_q); |
| 534 | Rep = Builder.CreateCall2(I, Op1, Op2, "palignr"); |
| 535 | } |
| 536 | |
| 537 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 538 | else { |
| 539 | Rep = Constant::getNullValue(F->getReturnType()); |
| 540 | } |
| 541 | |
| 542 | // Replace any uses with our new instruction. |
| 543 | if (!CI->use_empty()) |
| 544 | CI->replaceAllUsesWith(Rep); |
| 545 | |
| 546 | // Remove upgraded instruction. |
| 547 | CI->eraseFromParent(); |
| 548 | |
| 549 | } else if (F->getName() == "llvm.x86.ssse3.palign.r.128") { |
Gabor Greif | eab748d | 2010-06-29 16:17:26 +0000 | [diff] [blame] | 550 | Value *Op1 = CI->getArgOperand(0); |
| 551 | Value *Op2 = CI->getArgOperand(1); |
| 552 | Value *Op3 = CI->getArgOperand(2); |
Eric Christopher | 64831c6 | 2010-04-20 00:59:54 +0000 | [diff] [blame] | 553 | unsigned shiftVal = cast<ConstantInt>(Op3)->getZExtValue(); |
| 554 | Value *Rep; |
| 555 | IRBuilder<> Builder(C); |
| 556 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 557 | |
| 558 | // If palignr is shifting the pair of input vectors less than 17 bytes, |
| 559 | // emit a shuffle instruction. |
| 560 | if (shiftVal <= 16) { |
| 561 | const Type *IntTy = Type::getInt32Ty(C); |
| 562 | const Type *EltTy = Type::getInt8Ty(C); |
| 563 | const Type *VecTy = VectorType::get(EltTy, 16); |
| 564 | |
| 565 | Op2 = Builder.CreateBitCast(Op2, VecTy); |
| 566 | Op1 = Builder.CreateBitCast(Op1, VecTy); |
| 567 | |
| 568 | llvm::SmallVector<llvm::Constant*, 16> Indices; |
| 569 | for (unsigned i = 0; i != 16; ++i) |
| 570 | Indices.push_back(ConstantInt::get(IntTy, shiftVal + i)); |
| 571 | |
| 572 | Value *SV = ConstantVector::get(Indices.begin(), Indices.size()); |
| 573 | Rep = Builder.CreateShuffleVector(Op2, Op1, SV, "palignr"); |
| 574 | Rep = Builder.CreateBitCast(Rep, F->getReturnType()); |
| 575 | } |
| 576 | |
| 577 | // If palignr is shifting the pair of input vectors more than 16 but less |
| 578 | // than 32 bytes, emit a logical right shift of the destination. |
| 579 | else if (shiftVal < 32) { |
| 580 | const Type *EltTy = Type::getInt64Ty(C); |
| 581 | const Type *VecTy = VectorType::get(EltTy, 2); |
| 582 | const Type *IntTy = Type::getInt32Ty(C); |
| 583 | |
| 584 | Op1 = Builder.CreateBitCast(Op1, VecTy, "cast"); |
| 585 | Op2 = ConstantInt::get(IntTy, (shiftVal-16) * 8); |
| 586 | |
| 587 | // create i32 constant |
| 588 | Function *I = |
| 589 | Intrinsic::getDeclaration(F->getParent(), Intrinsic::x86_sse2_psrl_dq); |
| 590 | Rep = Builder.CreateCall2(I, Op1, Op2, "palignr"); |
| 591 | } |
| 592 | |
| 593 | // If palignr is shifting the pair of vectors more than 32 bytes, emit zero. |
| 594 | else { |
| 595 | Rep = Constant::getNullValue(F->getReturnType()); |
| 596 | } |
| 597 | |
| 598 | // Replace any uses with our new instruction. |
| 599 | if (!CI->use_empty()) |
| 600 | CI->replaceAllUsesWith(Rep); |
| 601 | |
| 602 | // Remove upgraded instruction. |
| 603 | CI->eraseFromParent(); |
| 604 | |
Evan Cheng | a8288f4 | 2007-12-18 01:04:25 +0000 | [diff] [blame] | 605 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 606 | llvm_unreachable("Unknown function for CallInst upgrade."); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 607 | } |
| 608 | return; |
| 609 | } |
| 610 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 611 | switch (NewFn->getIntrinsicID()) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 612 | default: llvm_unreachable("Unknown function for CallInst upgrade."); |
Bob Wilson | edf722a | 2010-08-27 17:13:24 +0000 | [diff] [blame] | 613 | case Intrinsic::arm_neon_vld1: |
| 614 | case Intrinsic::arm_neon_vld2: |
| 615 | case Intrinsic::arm_neon_vld3: |
| 616 | case Intrinsic::arm_neon_vld4: |
| 617 | case Intrinsic::arm_neon_vst1: |
| 618 | case Intrinsic::arm_neon_vst2: |
| 619 | case Intrinsic::arm_neon_vst3: |
| 620 | case Intrinsic::arm_neon_vst4: |
| 621 | case Intrinsic::arm_neon_vld2lane: |
| 622 | case Intrinsic::arm_neon_vld3lane: |
| 623 | case Intrinsic::arm_neon_vld4lane: |
| 624 | case Intrinsic::arm_neon_vst2lane: |
| 625 | case Intrinsic::arm_neon_vst3lane: |
| 626 | case Intrinsic::arm_neon_vst4lane: { |
| 627 | // Add a default alignment argument of 1. |
| 628 | SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end()); |
| 629 | Operands.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); |
| 630 | CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(), |
| 631 | CI->getName(), CI); |
| 632 | NewCI->setTailCall(CI->isTailCall()); |
| 633 | NewCI->setCallingConv(CI->getCallingConv()); |
| 634 | |
| 635 | // Handle any uses of the old CallInst. |
| 636 | if (!CI->use_empty()) |
| 637 | // Replace all uses of the old call with the new cast which has the |
| 638 | // correct type. |
| 639 | CI->replaceAllUsesWith(NewCI); |
| 640 | |
| 641 | // Clean up the old call now that it has been completely upgraded. |
| 642 | CI->eraseFromParent(); |
| 643 | break; |
| 644 | } |
| 645 | |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 646 | case Intrinsic::x86_mmx_psll_d: |
| 647 | case Intrinsic::x86_mmx_psll_q: |
| 648 | case Intrinsic::x86_mmx_psll_w: |
| 649 | case Intrinsic::x86_mmx_psra_d: |
| 650 | case Intrinsic::x86_mmx_psra_w: |
| 651 | case Intrinsic::x86_mmx_psrl_d: |
| 652 | case Intrinsic::x86_mmx_psrl_q: |
| 653 | case Intrinsic::x86_mmx_psrl_w: { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 654 | Value *Operands[2]; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 655 | |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 656 | Operands[0] = CI->getArgOperand(0); |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 657 | |
| 658 | // Cast the second parameter to the correct type. |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 659 | BitCastInst *BC = new BitCastInst(CI->getArgOperand(1), |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 660 | NewFn->getFunctionType()->getParamType(1), |
Evan Cheng | 5065932 | 2008-05-24 00:08:39 +0000 | [diff] [blame] | 661 | "upgraded.", CI); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 662 | Operands[1] = BC; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 663 | |
| 664 | // Construct a new CallInst |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 665 | CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2, |
| 666 | "upgraded."+CI->getName(), CI); |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 667 | NewCI->setTailCall(CI->isTailCall()); |
| 668 | NewCI->setCallingConv(CI->getCallingConv()); |
| 669 | |
| 670 | // Handle any uses of the old CallInst. |
| 671 | if (!CI->use_empty()) |
| 672 | // Replace all uses of the old call with the new cast which has the |
| 673 | // correct type. |
| 674 | CI->replaceAllUsesWith(NewCI); |
| 675 | |
| 676 | // Clean up the old call now that it has been completely upgraded. |
| 677 | CI->eraseFromParent(); |
| 678 | break; |
| 679 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 680 | case Intrinsic::ctlz: |
| 681 | case Intrinsic::ctpop: |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 682 | case Intrinsic::cttz: { |
Gabor Greif | e540653 | 2010-06-23 08:45:32 +0000 | [diff] [blame] | 683 | // Build a small vector of the original arguments. |
| 684 | SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end()); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 685 | |
| 686 | // Construct a new CallInst |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 687 | CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(), |
| 688 | "upgraded."+CI->getName(), CI); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 689 | NewCI->setTailCall(CI->isTailCall()); |
| 690 | NewCI->setCallingConv(CI->getCallingConv()); |
| 691 | |
| 692 | // Handle any uses of the old CallInst. |
| 693 | if (!CI->use_empty()) { |
| 694 | // Check for sign extend parameter attributes on the return values. |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 695 | bool SrcSExt = NewFn->getAttributes().paramHasAttr(0, Attribute::SExt); |
| 696 | bool DestSExt = F->getAttributes().paramHasAttr(0, Attribute::SExt); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 697 | |
| 698 | // Construct an appropriate cast from the new return type to the old. |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 699 | CastInst *RetCast = CastInst::Create( |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 700 | CastInst::getCastOpcode(NewCI, SrcSExt, |
| 701 | F->getReturnType(), |
| 702 | DestSExt), |
| 703 | NewCI, F->getReturnType(), |
| 704 | NewCI->getName(), CI); |
| 705 | NewCI->moveBefore(RetCast); |
| 706 | |
| 707 | // Replace all uses of the old call with the new cast which has the |
| 708 | // correct type. |
| 709 | CI->replaceAllUsesWith(RetCast); |
| 710 | } |
| 711 | |
| 712 | // Clean up the old call now that it has been completely upgraded. |
| 713 | CI->eraseFromParent(); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 714 | } |
| 715 | break; |
Duncan Sands | 8e6ccb6 | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 716 | case Intrinsic::eh_selector: |
| 717 | case Intrinsic::eh_typeid_for: { |
| 718 | // Only the return type changed. |
Gabor Greif | e540653 | 2010-06-23 08:45:32 +0000 | [diff] [blame] | 719 | SmallVector<Value*, 8> Operands(CS.arg_begin(), CS.arg_end()); |
Duncan Sands | 8e6ccb6 | 2009-10-14 16:11:37 +0000 | [diff] [blame] | 720 | CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(), |
| 721 | "upgraded." + CI->getName(), CI); |
| 722 | NewCI->setTailCall(CI->isTailCall()); |
| 723 | NewCI->setCallingConv(CI->getCallingConv()); |
| 724 | |
| 725 | // Handle any uses of the old CallInst. |
| 726 | if (!CI->use_empty()) { |
| 727 | // Construct an appropriate cast from the new return type to the old. |
| 728 | CastInst *RetCast = |
| 729 | CastInst::Create(CastInst::getCastOpcode(NewCI, true, |
| 730 | F->getReturnType(), true), |
| 731 | NewCI, F->getReturnType(), NewCI->getName(), CI); |
| 732 | CI->replaceAllUsesWith(RetCast); |
| 733 | } |
| 734 | CI->eraseFromParent(); |
| 735 | } |
| 736 | break; |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 737 | case Intrinsic::memcpy: |
| 738 | case Intrinsic::memmove: |
| 739 | case Intrinsic::memset: { |
| 740 | // Add isVolatile |
| 741 | const llvm::Type *I1Ty = llvm::Type::getInt1Ty(CI->getContext()); |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 742 | Value *Operands[5] = { CI->getArgOperand(0), CI->getArgOperand(1), |
| 743 | CI->getArgOperand(2), CI->getArgOperand(3), |
Mon P Wang | c576ee9 | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 744 | llvm::ConstantInt::get(I1Ty, 0) }; |
| 745 | CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+5, |
| 746 | CI->getName(), CI); |
| 747 | NewCI->setTailCall(CI->isTailCall()); |
| 748 | NewCI->setCallingConv(CI->getCallingConv()); |
| 749 | // Handle any uses of the old CallInst. |
| 750 | if (!CI->use_empty()) |
| 751 | // Replace all uses of the old call with the new cast which has the |
| 752 | // correct type. |
| 753 | CI->replaceAllUsesWith(NewCI); |
| 754 | |
| 755 | // Clean up the old call now that it has been completely upgraded. |
| 756 | CI->eraseFromParent(); |
| 757 | break; |
| 758 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
| 762 | // This tests each Function to determine if it needs upgrading. When we find |
| 763 | // one we are interested in, we then upgrade all calls to reflect the new |
| 764 | // function. |
| 765 | void llvm::UpgradeCallsToIntrinsic(Function* F) { |
| 766 | assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); |
| 767 | |
| 768 | // Upgrade the function and check if it is a totaly new function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 769 | Function* NewFn; |
| 770 | if (UpgradeIntrinsicFunction(F, NewFn)) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 771 | if (NewFn != F) { |
| 772 | // Replace all uses to the old function with the new one if necessary. |
| 773 | for (Value::use_iterator UI = F->use_begin(), UE = F->use_end(); |
| 774 | UI != UE; ) { |
| 775 | if (CallInst* CI = dyn_cast<CallInst>(*UI++)) |
| 776 | UpgradeIntrinsicCall(CI, NewFn); |
| 777 | } |
| 778 | // Remove old function, no longer used, from the module. |
| 779 | F->eraseFromParent(); |
| 780 | } |
| 781 | } |
| 782 | } |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 783 | |
Victor Hernandez | c2044a1 | 2010-01-05 21:13:46 +0000 | [diff] [blame] | 784 | /// This function strips all debug info intrinsics, except for llvm.dbg.declare. |
| 785 | /// If an llvm.dbg.declare intrinsic is invalid, then this function simply |
| 786 | /// strips that use. |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 787 | void llvm::CheckDebugInfoIntrinsics(Module *M) { |
| 788 | |
| 789 | |
| 790 | if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) { |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 791 | while (!FuncStart->use_empty()) { |
| 792 | CallInst *CI = cast<CallInst>(FuncStart->use_back()); |
| 793 | CI->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 794 | } |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 795 | FuncStart->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 796 | } |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 797 | |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 798 | if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) { |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 799 | while (!StopPoint->use_empty()) { |
| 800 | CallInst *CI = cast<CallInst>(StopPoint->use_back()); |
| 801 | CI->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 802 | } |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 803 | StopPoint->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) { |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 807 | while (!RegionStart->use_empty()) { |
| 808 | CallInst *CI = cast<CallInst>(RegionStart->use_back()); |
| 809 | CI->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 810 | } |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 811 | RegionStart->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) { |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 815 | while (!RegionEnd->use_empty()) { |
| 816 | CallInst *CI = cast<CallInst>(RegionEnd->use_back()); |
| 817 | CI->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 818 | } |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 819 | RegionEnd->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | if (Function *Declare = M->getFunction("llvm.dbg.declare")) { |
| 823 | if (!Declare->use_empty()) { |
| 824 | DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back()); |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 825 | if (!isa<MDNode>(DDI->getArgOperand(0)) || |
| 826 | !isa<MDNode>(DDI->getArgOperand(1))) { |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 827 | while (!Declare->use_empty()) { |
| 828 | CallInst *CI = cast<CallInst>(Declare->use_back()); |
| 829 | CI->eraseFromParent(); |
| 830 | } |
| 831 | Declare->eraseFromParent(); |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | } |