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