Chandler Carruth | 6994040 | 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 | 4ee451d | 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 | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Matt Arsenault | 3f49322 | 2013-07-20 17:46:00 +0000 | [diff] [blame] | 10 | // This file implements the auto-upgrade helper functions |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/AutoUpgrade.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Constants.h" |
| 16 | #include "llvm/IR/Function.h" |
| 17 | #include "llvm/IR/IRBuilder.h" |
| 18 | #include "llvm/IR/Instruction.h" |
| 19 | #include "llvm/IR/IntrinsicInst.h" |
| 20 | #include "llvm/IR/LLVMContext.h" |
| 21 | #include "llvm/IR/Module.h" |
Bill Wendling | c82a61c | 2011-08-25 23:22:40 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CFG.h" |
Chandler Carruth | 06cb8ed | 2012-06-29 12:38:19 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CallSite.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 25 | #include <cstring> |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Nadav Rotem | 3c98ce2 | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 28 | // Upgrade the declarations of the SSE4.1 functions whose arguments have |
| 29 | // changed their type from v4f32 to v2i64. |
| 30 | static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID, |
| 31 | Function *&NewFn) { |
| 32 | // Check whether this is an old version of the function, which received |
| 33 | // v4f32 arguments. |
| 34 | Type *Arg0Type = F->getFunctionType()->getParamType(0); |
| 35 | if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4)) |
| 36 | return false; |
| 37 | |
| 38 | // Yes, it's old, replace it with new version. |
| 39 | F->setName(F->getName() + ".old"); |
| 40 | NewFn = Intrinsic::getDeclaration(F->getParent(), IID); |
| 41 | return true; |
| 42 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 43 | |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 44 | static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 45 | assert(F && "Illegal to upgrade a non-existent Function."); |
| 46 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 47 | // Quickly eliminate it, if it's not a candidate. |
Chris Lattner | 747fddd | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 48 | StringRef Name = F->getName(); |
| 49 | if (Name.size() <= 8 || !Name.startswith("llvm.")) |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 50 | return false; |
Chris Lattner | 747fddd | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 51 | Name = Name.substr(5); // Strip off "llvm." |
Chris Lattner | b5dd9de | 2011-11-27 08:42:07 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 747fddd | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 53 | switch (Name[0]) { |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 54 | default: break; |
Joel Jones | 06a6a30 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 55 | case 'a': { |
| 56 | if (Name.startswith("arm.neon.vclz")) { |
| 57 | Type* args[2] = { |
Matt Arsenault | 3f49322 | 2013-07-20 17:46:00 +0000 | [diff] [blame] | 58 | F->arg_begin()->getType(), |
Joel Jones | 06a6a30 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 59 | Type::getInt1Ty(F->getContext()) |
| 60 | }; |
| 61 | // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to |
| 62 | // the end of the name. Change name from llvm.arm.neon.vclz.* to |
| 63 | // llvm.ctlz.* |
| 64 | FunctionType* fType = FunctionType::get(F->getReturnType(), args, false); |
Matt Arsenault | 3f49322 | 2013-07-20 17:46:00 +0000 | [diff] [blame] | 65 | NewFn = Function::Create(fType, F->getLinkage(), |
Joel Jones | 06a6a30 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 66 | "llvm.ctlz." + Name.substr(14), F->getParent()); |
| 67 | return true; |
| 68 | } |
Joel Jones | 7c82e6a | 2012-07-18 00:02:16 +0000 | [diff] [blame] | 69 | if (Name.startswith("arm.neon.vcnt")) { |
| 70 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop, |
| 71 | F->arg_begin()->getType()); |
| 72 | return true; |
| 73 | } |
Joel Jones | 06a6a30 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 74 | break; |
| 75 | } |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 76 | case 'c': { |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 77 | if (Name.startswith("ctlz.") && F->arg_size() == 1) { |
| 78 | F->setName(Name + ".old"); |
Chandler Carruth | a56f558 | 2011-12-12 10:57:20 +0000 | [diff] [blame] | 79 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, |
| 80 | F->arg_begin()->getType()); |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | if (Name.startswith("cttz.") && F->arg_size() == 1) { |
| 84 | F->setName(Name + ".old"); |
Chandler Carruth | a56f558 | 2011-12-12 10:57:20 +0000 | [diff] [blame] | 85 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, |
| 86 | F->arg_begin()->getType()); |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | break; |
| 90 | } |
Matt Arsenault | c4a8c07 | 2013-10-07 18:06:48 +0000 | [diff] [blame] | 91 | case 'o': |
| 92 | // We only need to change the name to match the mangling including the |
| 93 | // address space. |
| 94 | if (F->arg_size() == 2 && Name.startswith("objectsize.")) { |
| 95 | Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() }; |
| 96 | if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) { |
| 97 | F->setName(Name + ".old"); |
| 98 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 99 | Intrinsic::objectsize, Tys); |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | break; |
| 104 | |
Craig Topper | e058d27 | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 105 | case 'x': { |
| 106 | if (Name.startswith("x86.sse2.pcmpeq.") || |
| 107 | Name.startswith("x86.sse2.pcmpgt.") || |
| 108 | Name.startswith("x86.avx2.pcmpeq.") || |
Craig Topper | a963c81 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 109 | Name.startswith("x86.avx2.pcmpgt.") || |
Craig Topper | 189bce4 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 110 | Name.startswith("x86.avx.vpermil.") || |
| 111 | Name == "x86.avx.movnt.dq.256" || |
| 112 | Name == "x86.avx.movnt.pd.256" || |
Craig Topper | c29106b | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 113 | Name == "x86.avx.movnt.ps.256" || |
Craig Topper | 390ff49 | 2013-10-15 05:20:47 +0000 | [diff] [blame] | 114 | Name == "x86.sse42.crc32.64.8" || |
Craig Topper | c29106b | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 115 | (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) { |
Craig Topper | e058d27 | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 116 | NewFn = 0; |
| 117 | return true; |
| 118 | } |
Nadav Rotem | 3c98ce2 | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 119 | // SSE4.1 ptest functions may have an old signature. |
| 120 | if (Name.startswith("x86.sse41.ptest")) { |
| 121 | if (Name == "x86.sse41.ptestc") |
| 122 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn); |
| 123 | if (Name == "x86.sse41.ptestz") |
| 124 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn); |
| 125 | if (Name == "x86.sse41.ptestnzc") |
| 126 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn); |
| 127 | } |
Craig Topper | cc95b57 | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 128 | // frcz.ss/sd may need to have an argument dropped |
| 129 | if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) { |
| 130 | F->setName(Name + ".old"); |
| 131 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 132 | Intrinsic::x86_xop_vfrcz_ss); |
| 133 | return true; |
| 134 | } |
| 135 | if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) { |
| 136 | F->setName(Name + ".old"); |
| 137 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 138 | Intrinsic::x86_xop_vfrcz_sd); |
| 139 | return true; |
| 140 | } |
Craig Topper | 7678de4 | 2012-06-03 08:07:25 +0000 | [diff] [blame] | 141 | // Fix the FMA4 intrinsics to remove the 4 |
| 142 | if (Name.startswith("x86.fma4.")) { |
Craig Topper | bb6e61c | 2012-06-03 16:48:52 +0000 | [diff] [blame] | 143 | F->setName("llvm.x86.fma" + Name.substr(8)); |
| 144 | NewFn = F; |
| 145 | return true; |
Craig Topper | 7678de4 | 2012-06-03 08:07:25 +0000 | [diff] [blame] | 146 | } |
Craig Topper | e058d27 | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 147 | break; |
| 148 | } |
Chris Lattner | 747fddd | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 149 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 150 | |
Nadav Rotem | 3c98ce2 | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 151 | // This may not belong here. This function is effectively being overloaded |
| 152 | // to both detect an intrinsic which needs upgrading, and to provide the |
| 153 | // upgraded form of the intrinsic. We should perhaps have two separate |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 154 | // functions for this. |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 155 | return false; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 158 | bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { |
| 159 | NewFn = 0; |
| 160 | bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 161 | |
| 162 | // Upgrade intrinsic attributes. This does not change the function. |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 163 | if (NewFn) |
| 164 | F = NewFn; |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 165 | if (unsigned id = F->getIntrinsicID()) |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame] | 166 | F->setAttributes(Intrinsic::getAttributes(F->getContext(), |
| 167 | (Intrinsic::ID)id)); |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 168 | return Upgraded; |
| 169 | } |
| 170 | |
Bill Wendling | de49f36 | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 171 | bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { |
Chris Lattner | b85e4eb | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 172 | // Nothing to do yet. |
Bill Wendling | de49f36 | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Nadav Rotem | 3c98ce2 | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 176 | // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the |
| 177 | // upgraded intrinsic. All argument and return casting must be provided in |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 178 | // order to seamlessly integrate with existing context. |
| 179 | void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { |
Craig Topper | e058d27 | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 180 | Function *F = CI->getCalledFunction(); |
Nick Lewycky | bf47c76 | 2011-12-12 22:59:34 +0000 | [diff] [blame] | 181 | LLVMContext &C = CI->getContext(); |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 182 | IRBuilder<> Builder(C); |
| 183 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 184 | |
Craig Topper | e058d27 | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 185 | assert(F && "Intrinsic call is not direct?"); |
| 186 | |
| 187 | if (!NewFn) { |
| 188 | // Get the Function's name. |
| 189 | StringRef Name = F->getName(); |
| 190 | |
| 191 | Value *Rep; |
| 192 | // Upgrade packed integer vector compares intrinsics to compare instructions |
| 193 | if (Name.startswith("llvm.x86.sse2.pcmpeq.") || |
| 194 | Name.startswith("llvm.x86.avx2.pcmpeq.")) { |
| 195 | Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1), |
| 196 | "pcmpeq"); |
| 197 | // need to sign extend since icmp returns vector of i1 |
| 198 | Rep = Builder.CreateSExt(Rep, CI->getType(), ""); |
| 199 | } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") || |
| 200 | Name.startswith("llvm.x86.avx2.pcmpgt.")) { |
| 201 | Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1), |
| 202 | "pcmpgt"); |
| 203 | // need to sign extend since icmp returns vector of i1 |
| 204 | Rep = Builder.CreateSExt(Rep, CI->getType(), ""); |
Craig Topper | 189bce4 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 205 | } else if (Name == "llvm.x86.avx.movnt.dq.256" || |
| 206 | Name == "llvm.x86.avx.movnt.ps.256" || |
| 207 | Name == "llvm.x86.avx.movnt.pd.256") { |
| 208 | IRBuilder<> Builder(C); |
| 209 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 210 | |
| 211 | Module *M = F->getParent(); |
| 212 | SmallVector<Value *, 1> Elts; |
| 213 | Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1)); |
| 214 | MDNode *Node = MDNode::get(C, Elts); |
| 215 | |
| 216 | Value *Arg0 = CI->getArgOperand(0); |
| 217 | Value *Arg1 = CI->getArgOperand(1); |
| 218 | |
| 219 | // Convert the type of the pointer to a pointer to the stored type. |
| 220 | Value *BC = Builder.CreateBitCast(Arg0, |
| 221 | PointerType::getUnqual(Arg1->getType()), |
| 222 | "cast"); |
| 223 | StoreInst *SI = Builder.CreateStore(Arg1, BC); |
| 224 | SI->setMetadata(M->getMDKindID("nontemporal"), Node); |
| 225 | SI->setAlignment(16); |
| 226 | |
| 227 | // Remove intrinsic. |
| 228 | CI->eraseFromParent(); |
| 229 | return; |
Craig Topper | c29106b | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 230 | } else if (Name.startswith("llvm.x86.xop.vpcom")) { |
| 231 | Intrinsic::ID intID; |
| 232 | if (Name.endswith("ub")) |
| 233 | intID = Intrinsic::x86_xop_vpcomub; |
| 234 | else if (Name.endswith("uw")) |
| 235 | intID = Intrinsic::x86_xop_vpcomuw; |
| 236 | else if (Name.endswith("ud")) |
| 237 | intID = Intrinsic::x86_xop_vpcomud; |
| 238 | else if (Name.endswith("uq")) |
| 239 | intID = Intrinsic::x86_xop_vpcomuq; |
| 240 | else if (Name.endswith("b")) |
| 241 | intID = Intrinsic::x86_xop_vpcomb; |
| 242 | else if (Name.endswith("w")) |
| 243 | intID = Intrinsic::x86_xop_vpcomw; |
| 244 | else if (Name.endswith("d")) |
| 245 | intID = Intrinsic::x86_xop_vpcomd; |
| 246 | else if (Name.endswith("q")) |
| 247 | intID = Intrinsic::x86_xop_vpcomq; |
| 248 | else |
| 249 | llvm_unreachable("Unknown suffix"); |
| 250 | |
| 251 | Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom" |
| 252 | unsigned Imm; |
| 253 | if (Name.startswith("lt")) |
| 254 | Imm = 0; |
| 255 | else if (Name.startswith("le")) |
| 256 | Imm = 1; |
| 257 | else if (Name.startswith("gt")) |
| 258 | Imm = 2; |
| 259 | else if (Name.startswith("ge")) |
| 260 | Imm = 3; |
| 261 | else if (Name.startswith("eq")) |
| 262 | Imm = 4; |
| 263 | else if (Name.startswith("ne")) |
| 264 | Imm = 5; |
| 265 | else if (Name.startswith("true")) |
| 266 | Imm = 6; |
| 267 | else if (Name.startswith("false")) |
| 268 | Imm = 7; |
| 269 | else |
| 270 | llvm_unreachable("Unknown condition"); |
| 271 | |
| 272 | Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID); |
| 273 | Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0), |
| 274 | CI->getArgOperand(1), Builder.getInt8(Imm)); |
Craig Topper | 390ff49 | 2013-10-15 05:20:47 +0000 | [diff] [blame] | 275 | } else if (Name == "llvm.x86.sse42.crc32.64.8") { |
| 276 | Function *CRC32 = Intrinsic::getDeclaration(F->getParent(), |
| 277 | Intrinsic::x86_sse42_crc32_32_8); |
| 278 | Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C)); |
| 279 | Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1)); |
| 280 | Rep = Builder.CreateZExt(Rep, CI->getType(), ""); |
Craig Topper | e058d27 | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 281 | } else { |
Craig Topper | a963c81 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 282 | bool PD128 = false, PD256 = false, PS128 = false, PS256 = false; |
Craig Topper | 189bce4 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 283 | if (Name == "llvm.x86.avx.vpermil.pd.256") |
Craig Topper | a963c81 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 284 | PD256 = true; |
Craig Topper | 189bce4 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 285 | else if (Name == "llvm.x86.avx.vpermil.pd") |
Craig Topper | a963c81 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 286 | PD128 = true; |
Craig Topper | 189bce4 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 287 | else if (Name == "llvm.x86.avx.vpermil.ps.256") |
Craig Topper | a963c81 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 288 | PS256 = true; |
Craig Topper | 189bce4 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 289 | else if (Name == "llvm.x86.avx.vpermil.ps") |
Craig Topper | a963c81 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 290 | PS128 = true; |
| 291 | |
| 292 | if (PD256 || PD128 || PS256 || PS128) { |
| 293 | Value *Op0 = CI->getArgOperand(0); |
| 294 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
| 295 | SmallVector<Constant*, 8> Idxs; |
| 296 | |
| 297 | if (PD128) |
| 298 | for (unsigned i = 0; i != 2; ++i) |
| 299 | Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1)); |
| 300 | else if (PD256) |
| 301 | for (unsigned l = 0; l != 4; l+=2) |
| 302 | for (unsigned i = 0; i != 2; ++i) |
| 303 | Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l)); |
| 304 | else if (PS128) |
| 305 | for (unsigned i = 0; i != 4; ++i) |
| 306 | Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3)); |
| 307 | else if (PS256) |
| 308 | for (unsigned l = 0; l != 8; l+=4) |
| 309 | for (unsigned i = 0; i != 4; ++i) |
| 310 | Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l)); |
| 311 | else |
| 312 | llvm_unreachable("Unexpected function"); |
| 313 | |
| 314 | Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs)); |
| 315 | } else { |
| 316 | llvm_unreachable("Unknown function for CallInst upgrade."); |
| 317 | } |
Craig Topper | e058d27 | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | CI->replaceAllUsesWith(Rep); |
| 321 | CI->eraseFromParent(); |
| 322 | return; |
| 323 | } |
| 324 | |
Chandler Carruth | 7325f06 | 2012-07-20 21:09:18 +0000 | [diff] [blame] | 325 | std::string Name = CI->getName().str(); |
| 326 | CI->setName(Name + ".old"); |
Nadav Rotem | 3c98ce2 | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 327 | |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 328 | switch (NewFn->getIntrinsicID()) { |
| 329 | default: |
Chris Lattner | b5dd9de | 2011-11-27 08:42:07 +0000 | [diff] [blame] | 330 | llvm_unreachable("Unknown function for CallInst upgrade."); |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 331 | |
| 332 | case Intrinsic::ctlz: |
Nuno Lopes | 23e75da | 2012-05-22 15:25:31 +0000 | [diff] [blame] | 333 | case Intrinsic::cttz: |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 334 | assert(CI->getNumArgOperands() == 1 && |
| 335 | "Mismatch between function args and call args"); |
Chandler Carruth | ccbf1e3 | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 336 | CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0), |
| 337 | Builder.getFalse(), Name)); |
| 338 | CI->eraseFromParent(); |
| 339 | return; |
Nadav Rotem | 3c98ce2 | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 340 | |
Matt Arsenault | c4a8c07 | 2013-10-07 18:06:48 +0000 | [diff] [blame] | 341 | case Intrinsic::objectsize: |
| 342 | CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, |
| 343 | CI->getArgOperand(0), |
| 344 | CI->getArgOperand(1), |
| 345 | Name)); |
| 346 | CI->eraseFromParent(); |
| 347 | return; |
| 348 | |
Joel Jones | 06a6a30 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 349 | case Intrinsic::arm_neon_vclz: { |
| 350 | // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.* |
| 351 | CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0), |
Joel Jones | 7c82e6a | 2012-07-18 00:02:16 +0000 | [diff] [blame] | 352 | Builder.getFalse(), |
Joel Jones | 06a6a30 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 353 | "llvm.ctlz." + Name.substr(14))); |
| 354 | CI->eraseFromParent(); |
| 355 | return; |
| 356 | } |
Joel Jones | 7c82e6a | 2012-07-18 00:02:16 +0000 | [diff] [blame] | 357 | case Intrinsic::ctpop: { |
| 358 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0))); |
| 359 | CI->eraseFromParent(); |
| 360 | return; |
| 361 | } |
Joel Jones | 06a6a30 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 362 | |
Craig Topper | cc95b57 | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 363 | case Intrinsic::x86_xop_vfrcz_ss: |
| 364 | case Intrinsic::x86_xop_vfrcz_sd: |
| 365 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1), |
| 366 | Name)); |
| 367 | CI->eraseFromParent(); |
| 368 | return; |
| 369 | |
Nadav Rotem | 3c98ce2 | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 370 | case Intrinsic::x86_sse41_ptestc: |
| 371 | case Intrinsic::x86_sse41_ptestz: |
Craig Topper | cc95b57 | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 372 | case Intrinsic::x86_sse41_ptestnzc: { |
Nadav Rotem | 3c98ce2 | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 373 | // The arguments for these intrinsics used to be v4f32, and changed |
| 374 | // to v2i64. This is purely a nop, since those are bitwise intrinsics. |
| 375 | // So, the only thing required is a bitcast for both arguments. |
| 376 | // First, check the arguments have the old type. |
| 377 | Value *Arg0 = CI->getArgOperand(0); |
| 378 | if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4)) |
| 379 | return; |
| 380 | |
| 381 | // Old intrinsic, add bitcasts |
| 382 | Value *Arg1 = CI->getArgOperand(1); |
| 383 | |
| 384 | Value *BC0 = |
| 385 | Builder.CreateBitCast(Arg0, |
| 386 | VectorType::get(Type::getInt64Ty(C), 2), |
| 387 | "cast"); |
| 388 | Value *BC1 = |
| 389 | Builder.CreateBitCast(Arg1, |
| 390 | VectorType::get(Type::getInt64Ty(C), 2), |
| 391 | "cast"); |
| 392 | |
| 393 | CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name); |
| 394 | CI->replaceAllUsesWith(NewCall); |
| 395 | CI->eraseFromParent(); |
| 396 | return; |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 397 | } |
Craig Topper | cc95b57 | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 398 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Matt Arsenault | 3f49322 | 2013-07-20 17:46:00 +0000 | [diff] [blame] | 401 | // This tests each Function to determine if it needs upgrading. When we find |
| 402 | // one we are interested in, we then upgrade all calls to reflect the new |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 403 | // function. |
| 404 | void llvm::UpgradeCallsToIntrinsic(Function* F) { |
| 405 | assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); |
| 406 | |
| 407 | // Upgrade the function and check if it is a totaly new function. |
Chris Lattner | b85e4eb | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 408 | Function *NewFn; |
Evan Cheng | f9b83fc | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 409 | if (UpgradeIntrinsicFunction(F, NewFn)) { |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 410 | if (NewFn != F) { |
| 411 | // Replace all uses to the old function with the new one if necessary. |
| 412 | for (Value::use_iterator UI = F->use_begin(), UE = F->use_end(); |
| 413 | UI != UE; ) { |
Chris Lattner | b85e4eb | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 414 | if (CallInst *CI = dyn_cast<CallInst>(*UI++)) |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 415 | UpgradeIntrinsicCall(CI, NewFn); |
| 416 | } |
| 417 | // Remove old function, no longer used, from the module. |
| 418 | F->eraseFromParent(); |
| 419 | } |
| 420 | } |
| 421 | } |
Devang Patel | e4b2756 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 422 | |
Manman Ren | 804f034 | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 423 | void llvm::UpgradeInstWithTBAATag(Instruction *I) { |
| 424 | MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa); |
| 425 | assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag"); |
| 426 | // Check if the tag uses struct-path aware TBAA format. |
| 427 | if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3) |
| 428 | return; |
| 429 | |
| 430 | if (MD->getNumOperands() == 3) { |
| 431 | Value *Elts[] = { |
| 432 | MD->getOperand(0), |
| 433 | MD->getOperand(1) |
| 434 | }; |
| 435 | MDNode *ScalarType = MDNode::get(I->getContext(), Elts); |
| 436 | // Create a MDNode <ScalarType, ScalarType, offset 0, const> |
| 437 | Value *Elts2[] = { |
| 438 | ScalarType, ScalarType, |
| 439 | Constant::getNullValue(Type::getInt64Ty(I->getContext())), |
| 440 | MD->getOperand(2) |
| 441 | }; |
| 442 | I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); |
| 443 | } else { |
| 444 | // Create a MDNode <MD, MD, offset 0> |
| 445 | Value *Elts[] = {MD, MD, |
| 446 | Constant::getNullValue(Type::getInt64Ty(I->getContext()))}; |
| 447 | I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); |
| 448 | } |
| 449 | } |
Matt Arsenault | 59d3ae6 | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 450 | |
| 451 | Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy, |
| 452 | Instruction *&Temp) { |
| 453 | if (Opc != Instruction::BitCast) |
| 454 | return 0; |
| 455 | |
| 456 | Temp = 0; |
| 457 | Type *SrcTy = V->getType(); |
| 458 | if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && |
| 459 | SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { |
| 460 | LLVMContext &Context = V->getContext(); |
| 461 | |
| 462 | // We have no information about target data layout, so we assume that |
| 463 | // the maximum pointer size is 64bit. |
| 464 | Type *MidTy = Type::getInt64Ty(Context); |
| 465 | Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy); |
| 466 | |
| 467 | return CastInst::Create(Instruction::IntToPtr, Temp, DestTy); |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) { |
| 474 | if (Opc != Instruction::BitCast) |
| 475 | return 0; |
| 476 | |
| 477 | Type *SrcTy = C->getType(); |
| 478 | if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && |
| 479 | SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { |
| 480 | LLVMContext &Context = C->getContext(); |
| 481 | |
| 482 | // We have no information about target data layout, so we assume that |
| 483 | // the maximum pointer size is 64bit. |
| 484 | Type *MidTy = Type::getInt64Ty(Context); |
| 485 | |
| 486 | return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy), |
| 487 | DestTy); |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |