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