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; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 45 | case 'p': |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 46 | // This upgrades the llvm.prefetch intrinsic to accept one more parameter, |
| 47 | // which is a instruction / data cache identifier. The old version only |
| 48 | // implicitly accepted the data version. |
| 49 | if (Name.compare(5,8,"prefetch",8) == 0) { |
| 50 | // Don't do anything if it has the correct number of arguments already |
| 51 | if (FTy->getNumParams() == 4) |
| 52 | break; |
| 53 | |
| 54 | assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!"); |
| 55 | // We first need to change the name of the old (bad) intrinsic, because |
| 56 | // its type is incorrect, but we cannot overload that name. We |
| 57 | // arbitrarily unique it here allowing us to construct a correctly named |
| 58 | // and typed function below. |
| 59 | F->setName(""); |
| 60 | NewFn = cast<Function>(M->getOrInsertFunction(Name, |
| 61 | FTy->getReturnType(), |
| 62 | FTy->getParamType(0), |
| 63 | FTy->getParamType(1), |
| 64 | FTy->getParamType(2), |
| 65 | FTy->getParamType(2), |
| 66 | (Type*)0)); |
| 67 | return true; |
| 68 | } |
| 69 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 70 | break; |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 71 | case 'x': |
| 72 | // This fixes the poorly named crc32 intrinsics |
| 73 | if (Name.compare(5, 13, "x86.sse42.crc", 13) == 0) { |
| 74 | const char* NewFnName = NULL; |
| 75 | if (Name.compare(18, 2, "32", 2) == 0) { |
Chad Rosier | 3252177 | 2011-05-27 19:38:10 +0000 | [diff] [blame] | 76 | if (Name.compare(20, 2, ".8") == 0 && Name.length() == 22) { |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 77 | NewFnName = "llvm.x86.sse42.crc32.32.8"; |
Chad Rosier | 3252177 | 2011-05-27 19:38:10 +0000 | [diff] [blame] | 78 | } else if (Name.compare(20, 3, ".16") == 0 && Name.length() == 23) { |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 79 | NewFnName = "llvm.x86.sse42.crc32.32.16"; |
Chad Rosier | 3252177 | 2011-05-27 19:38:10 +0000 | [diff] [blame] | 80 | } else if (Name.compare(20, 3, ".32") == 0 && Name.length() == 23) { |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 81 | NewFnName = "llvm.x86.sse42.crc32.32.32"; |
| 82 | } |
| 83 | } |
| 84 | else if (Name.compare(18, 2, "64", 2) == 0) { |
Chad Rosier | 3252177 | 2011-05-27 19:38:10 +0000 | [diff] [blame] | 85 | if (Name.compare(20, 2, ".8") == 0 && Name.length() == 22) { |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 86 | NewFnName = "llvm.x86.sse42.crc32.64.8"; |
Chad Rosier | 3252177 | 2011-05-27 19:38:10 +0000 | [diff] [blame] | 87 | } else if (Name.compare(20, 3, ".64") == 0 && Name.length() == 23) { |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 88 | NewFnName = "llvm.x86.sse42.crc32.64.64"; |
| 89 | } |
| 90 | } |
| 91 | if (NewFnName) { |
| 92 | F->setName(NewFnName); |
| 93 | NewFn = F; |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 98 | if (Name.compare(5, 16, "x86.sse.loadu.ps", 16) == 0 || |
| 99 | Name.compare(5, 17, "x86.sse2.loadu.dq", 17) == 0 || |
| 100 | Name.compare(5, 17, "x86.sse2.loadu.pd", 17) == 0) { |
Bill Wendling | b902f1d | 2011-04-13 00:36:11 +0000 | [diff] [blame] | 101 | // Calls to these instructions are transformed into unaligned loads. |
| 102 | NewFn = 0; |
| 103 | return true; |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 104 | } |
| 105 | |
| 106 | if (Name.compare(5, 16, "x86.sse.movnt.ps", 16) == 0 || |
| 107 | Name.compare(5, 17, "x86.sse2.movnt.dq", 17) == 0 || |
| 108 | Name.compare(5, 17, "x86.sse2.movnt.pd", 17) == 0 || |
| 109 | Name.compare(5, 17, "x86.sse2.movnt.i", 16) == 0) { |
Bill Wendling | db0996c | 2011-05-03 21:11:17 +0000 | [diff] [blame] | 110 | // Calls to these instructions are transformed into nontemporal stores. |
| 111 | NewFn = 0; |
| 112 | return true; |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 113 | } |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 114 | |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 115 | break; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // This may not belong here. This function is effectively being overloaded |
| 119 | // to both detect an intrinsic which needs upgrading, and to provide the |
| 120 | // upgraded form of the intrinsic. We should perhaps have two separate |
| 121 | // functions for this. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 122 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 125 | bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { |
| 126 | NewFn = 0; |
| 127 | bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 128 | |
| 129 | // Upgrade intrinsic attributes. This does not change the function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 130 | if (NewFn) |
| 131 | F = NewFn; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 132 | if (unsigned id = F->getIntrinsicID()) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 133 | F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id)); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 134 | return Upgraded; |
| 135 | } |
| 136 | |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 137 | bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 138 | // Nothing to do yet. |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 142 | // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the |
| 143 | // upgraded intrinsic. All argument and return casting must be provided in |
| 144 | // order to seamlessly integrate with existing context. |
| 145 | void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 146 | Function *F = CI->getCalledFunction(); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 147 | LLVMContext &C = CI->getContext(); |
Gabor Greif | e540653 | 2010-06-23 08:45:32 +0000 | [diff] [blame] | 148 | ImmutableCallSite CS(CI); |
| 149 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 150 | assert(F && "CallInst has no function associated with it."); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 151 | |
| 152 | if (!NewFn) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 153 | if (F->getName() == "llvm.x86.sse.loadu.ps" || |
| 154 | F->getName() == "llvm.x86.sse2.loadu.dq" || |
| 155 | F->getName() == "llvm.x86.sse2.loadu.pd") { |
Bill Wendling | b902f1d | 2011-04-13 00:36:11 +0000 | [diff] [blame] | 156 | // Convert to a native, unaligned load. |
| 157 | const Type *VecTy = CI->getType(); |
| 158 | const Type *IntTy = IntegerType::get(C, 128); |
| 159 | IRBuilder<> Builder(C); |
| 160 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 161 | |
| 162 | Value *BC = Builder.CreateBitCast(CI->getArgOperand(0), |
| 163 | PointerType::getUnqual(IntTy), |
| 164 | "cast"); |
| 165 | LoadInst *LI = Builder.CreateLoad(BC, CI->getName()); |
| 166 | LI->setAlignment(1); // Unaligned load. |
| 167 | BC = Builder.CreateBitCast(LI, VecTy, "new.cast"); |
| 168 | |
| 169 | // Fix up all the uses with our new load. |
| 170 | if (!CI->use_empty()) |
| 171 | CI->replaceAllUsesWith(BC); |
| 172 | |
| 173 | // Remove intrinsic. |
| 174 | CI->eraseFromParent(); |
Bill Wendling | db0996c | 2011-05-03 21:11:17 +0000 | [diff] [blame] | 175 | } else if (F->getName() == "llvm.x86.sse.movnt.ps" || |
| 176 | F->getName() == "llvm.x86.sse2.movnt.dq" || |
| 177 | F->getName() == "llvm.x86.sse2.movnt.pd" || |
| 178 | F->getName() == "llvm.x86.sse2.movnt.i") { |
| 179 | IRBuilder<> Builder(C); |
| 180 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 181 | |
| 182 | Module *M = F->getParent(); |
| 183 | SmallVector<Value *, 1> Elts; |
| 184 | Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); |
| 185 | MDNode *Node = MDNode::get(C, Elts); |
| 186 | |
| 187 | Value *Arg0 = CI->getArgOperand(0); |
| 188 | Value *Arg1 = CI->getArgOperand(1); |
| 189 | |
| 190 | // Convert the type of the pointer to a pointer to the stored type. |
| 191 | Value *BC = Builder.CreateBitCast(Arg0, |
| 192 | PointerType::getUnqual(Arg1->getType()), |
| 193 | "cast"); |
| 194 | StoreInst *SI = Builder.CreateStore(Arg1, BC); |
| 195 | SI->setMetadata(M->getMDKindID("nontemporal"), Node); |
| 196 | SI->setAlignment(16); |
| 197 | |
| 198 | // Remove intrinsic. |
| 199 | CI->eraseFromParent(); |
Evan Cheng | a8288f4 | 2007-12-18 01:04:25 +0000 | [diff] [blame] | 200 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 201 | llvm_unreachable("Unknown function for CallInst upgrade."); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 202 | } |
| 203 | return; |
| 204 | } |
| 205 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 206 | switch (NewFn->getIntrinsicID()) { |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 207 | case Intrinsic::prefetch: { |
| 208 | IRBuilder<> Builder(C); |
| 209 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 210 | const llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext()); |
| 211 | |
| 212 | // Add the extra "data cache" argument |
| 213 | Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1), |
| 214 | CI->getArgOperand(2), |
| 215 | llvm::ConstantInt::get(I32Ty, 1) }; |
| 216 | CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+4, |
| 217 | CI->getName(), CI); |
| 218 | NewCI->setTailCall(CI->isTailCall()); |
| 219 | NewCI->setCallingConv(CI->getCallingConv()); |
| 220 | // Handle any uses of the old CallInst. |
| 221 | if (!CI->use_empty()) |
| 222 | // Replace all uses of the old call with the new cast which has the |
| 223 | // correct type. |
| 224 | CI->replaceAllUsesWith(NewCI); |
| 225 | |
| 226 | // Clean up the old call now that it has been completely upgraded. |
| 227 | CI->eraseFromParent(); |
| 228 | break; |
| 229 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | // This tests each Function to determine if it needs upgrading. When we find |
| 234 | // one we are interested in, we then upgrade all calls to reflect the new |
| 235 | // function. |
| 236 | void llvm::UpgradeCallsToIntrinsic(Function* F) { |
| 237 | assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); |
| 238 | |
| 239 | // Upgrade the function and check if it is a totaly new function. |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 240 | Function *NewFn; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 241 | if (UpgradeIntrinsicFunction(F, NewFn)) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 242 | if (NewFn != F) { |
| 243 | // Replace all uses to the old function with the new one if necessary. |
| 244 | for (Value::use_iterator UI = F->use_begin(), UE = F->use_end(); |
| 245 | UI != UE; ) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 246 | if (CallInst *CI = dyn_cast<CallInst>(*UI++)) |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 247 | UpgradeIntrinsicCall(CI, NewFn); |
| 248 | } |
| 249 | // Remove old function, no longer used, from the module. |
| 250 | F->eraseFromParent(); |
| 251 | } |
| 252 | } |
| 253 | } |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 254 | |
Victor Hernandez | c2044a1 | 2010-01-05 21:13:46 +0000 | [diff] [blame] | 255 | /// This function strips all debug info intrinsics, except for llvm.dbg.declare. |
| 256 | /// If an llvm.dbg.declare intrinsic is invalid, then this function simply |
| 257 | /// strips that use. |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 258 | void llvm::CheckDebugInfoIntrinsics(Module *M) { |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 259 | if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 260 | while (!FuncStart->use_empty()) |
| 261 | cast<CallInst>(FuncStart->use_back())->eraseFromParent(); |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 262 | FuncStart->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 263 | } |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 264 | |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 265 | if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 266 | while (!StopPoint->use_empty()) |
| 267 | cast<CallInst>(StopPoint->use_back())->eraseFromParent(); |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 268 | StopPoint->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 272 | while (!RegionStart->use_empty()) |
| 273 | cast<CallInst>(RegionStart->use_back())->eraseFromParent(); |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 274 | RegionStart->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame^] | 278 | while (!RegionEnd->use_empty()) |
| 279 | cast<CallInst>(RegionEnd->use_back())->eraseFromParent(); |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 280 | RegionEnd->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | if (Function *Declare = M->getFunction("llvm.dbg.declare")) { |
| 284 | if (!Declare->use_empty()) { |
| 285 | DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back()); |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 286 | if (!isa<MDNode>(DDI->getArgOperand(0)) || |
| 287 | !isa<MDNode>(DDI->getArgOperand(1))) { |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 288 | while (!Declare->use_empty()) { |
| 289 | CallInst *CI = cast<CallInst>(Declare->use_back()); |
| 290 | CI->eraseFromParent(); |
| 291 | } |
| 292 | Declare->eraseFromParent(); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |