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