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