blob: eafe1b5a5dc9cc933137b0b342f94605091c7fd1 [file] [log] [blame]
Chandler Carruth7132e002007-08-04 01:51:18 +00001//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chandler Carruth7132e002007-08-04 01:51:18 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the auto-upgrade helper functions
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/AutoUpgrade.h"
Anders Carlssonf924f342007-12-14 06:38:54 +000015#include "llvm/Constants.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000016#include "llvm/Function.h"
Bill Wendling45449b12011-08-25 23:22:40 +000017#include "llvm/Instruction.h"
Owen Anderson155dccd82009-07-07 23:43:39 +000018#include "llvm/LLVMContext.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000019#include "llvm/Module.h"
Devang Patel80ae3492009-08-28 23:24:31 +000020#include "llvm/IntrinsicInst.h"
Gabor Greife5406532010-06-23 08:45:32 +000021#include "llvm/Support/CallSite.h"
Bill Wendling45449b12011-08-25 23:22:40 +000022#include "llvm/Support/CFG.h"
Torok Edwin56d06592009-07-11 20:10:48 +000023#include "llvm/Support/ErrorHandling.h"
Eric Christopher64831c62010-04-20 00:59:54 +000024#include "llvm/Support/IRBuilder.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000025#include <cstring>
Chandler Carruth7132e002007-08-04 01:51:18 +000026using namespace llvm;
27
28
Evan Cheng0e179d02007-12-17 22:33:23 +000029static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000030 assert(F && "Illegal to upgrade a non-existent Function.");
31
Chandler Carruth7132e002007-08-04 01:51:18 +000032 // Quickly eliminate it, if it's not a candidate.
Chris Lattnerb372f662011-06-18 18:56:39 +000033 StringRef Name = F->getName();
34 if (Name.size() <= 8 || !Name.startswith("llvm."))
Evan Cheng0e179d02007-12-17 22:33:23 +000035 return false;
Chris Lattnerb372f662011-06-18 18:56:39 +000036 Name = Name.substr(5); // Strip off "llvm."
Chris Lattner0bcbde42011-11-27 08:42:07 +000037
Chris Lattnerb372f662011-06-18 18:56:39 +000038 switch (Name[0]) {
Chandler Carruth7132e002007-08-04 01:51:18 +000039 default: break;
Chandler Carruth58a71ed2011-12-12 04:26:04 +000040 case 'c': {
Chandler Carruth58a71ed2011-12-12 04:26:04 +000041 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
42 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +000043 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
44 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +000045 return true;
46 }
47 if (Name.startswith("cttz.") && F->arg_size() == 1) {
48 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +000049 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
50 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +000051 return true;
52 }
53 break;
54 }
Craig Topper3b1817d2012-02-03 06:10:55 +000055 case 'x': {
56 if (Name.startswith("x86.sse2.pcmpeq.") ||
57 Name.startswith("x86.sse2.pcmpgt.") ||
58 Name.startswith("x86.avx2.pcmpeq.") ||
Craig Topperd3c9e402012-04-18 05:24:00 +000059 Name.startswith("x86.avx2.pcmpgt.") ||
Craig Topper7daf8972012-05-08 06:58:15 +000060 Name.startswith("x86.avx.vpermil.") ||
61 Name == "x86.avx.movnt.dq.256" ||
62 Name == "x86.avx.movnt.pd.256" ||
63 Name == "x86.avx.movnt.ps.256") {
Craig Topper3b1817d2012-02-03 06:10:55 +000064 NewFn = 0;
65 return true;
66 }
Craig Topper720c7bd2012-06-03 08:07:25 +000067 // Fix the FMA4 intrinsics to remove the 4
68 if (Name.startswith("x86.fma4.")) {
Craig Topper2c5ccd82012-06-03 16:48:52 +000069 F->setName("llvm.x86.fma" + Name.substr(8));
70 NewFn = F;
71 return true;
Craig Topper720c7bd2012-06-03 08:07:25 +000072 }
Craig Topper3b1817d2012-02-03 06:10:55 +000073 break;
74 }
Chris Lattnerb372f662011-06-18 18:56:39 +000075 }
Chandler Carruth7132e002007-08-04 01:51:18 +000076
77 // This may not belong here. This function is effectively being overloaded
78 // to both detect an intrinsic which needs upgrading, and to provide the
79 // upgraded form of the intrinsic. We should perhaps have two separate
80 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +000081 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +000082}
83
Evan Cheng0e179d02007-12-17 22:33:23 +000084bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
85 NewFn = 0;
86 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +000087
88 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +000089 if (NewFn)
90 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +000091 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +000092 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +000093 return Upgraded;
94}
95
Bill Wendlinge26fffc2010-09-10 18:51:56 +000096bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +000097 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +000098 return false;
99}
100
Chandler Carruth7132e002007-08-04 01:51:18 +0000101// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
102// upgraded intrinsic. All argument and return casting must be provided in
103// order to seamlessly integrate with existing context.
104void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +0000105 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +0000106 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000107 IRBuilder<> Builder(C);
108 Builder.SetInsertPoint(CI->getParent(), CI);
109
Craig Topper3b1817d2012-02-03 06:10:55 +0000110 assert(F && "Intrinsic call is not direct?");
111
112 if (!NewFn) {
113 // Get the Function's name.
114 StringRef Name = F->getName();
115
116 Value *Rep;
117 // Upgrade packed integer vector compares intrinsics to compare instructions
118 if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
119 Name.startswith("llvm.x86.avx2.pcmpeq.")) {
120 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
121 "pcmpeq");
122 // need to sign extend since icmp returns vector of i1
123 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
124 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
125 Name.startswith("llvm.x86.avx2.pcmpgt.")) {
126 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
127 "pcmpgt");
128 // need to sign extend since icmp returns vector of i1
129 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
Craig Topper7daf8972012-05-08 06:58:15 +0000130 } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
131 Name == "llvm.x86.avx.movnt.ps.256" ||
132 Name == "llvm.x86.avx.movnt.pd.256") {
133 IRBuilder<> Builder(C);
134 Builder.SetInsertPoint(CI->getParent(), CI);
135
136 Module *M = F->getParent();
137 SmallVector<Value *, 1> Elts;
138 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
139 MDNode *Node = MDNode::get(C, Elts);
140
141 Value *Arg0 = CI->getArgOperand(0);
142 Value *Arg1 = CI->getArgOperand(1);
143
144 // Convert the type of the pointer to a pointer to the stored type.
145 Value *BC = Builder.CreateBitCast(Arg0,
146 PointerType::getUnqual(Arg1->getType()),
147 "cast");
148 StoreInst *SI = Builder.CreateStore(Arg1, BC);
149 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
150 SI->setAlignment(16);
151
152 // Remove intrinsic.
153 CI->eraseFromParent();
154 return;
Craig Topper3b1817d2012-02-03 06:10:55 +0000155 } else {
Craig Topperd3c9e402012-04-18 05:24:00 +0000156 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
Craig Topper7daf8972012-05-08 06:58:15 +0000157 if (Name == "llvm.x86.avx.vpermil.pd.256")
Craig Topperd3c9e402012-04-18 05:24:00 +0000158 PD256 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000159 else if (Name == "llvm.x86.avx.vpermil.pd")
Craig Topperd3c9e402012-04-18 05:24:00 +0000160 PD128 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000161 else if (Name == "llvm.x86.avx.vpermil.ps.256")
Craig Topperd3c9e402012-04-18 05:24:00 +0000162 PS256 = true;
Craig Topper7daf8972012-05-08 06:58:15 +0000163 else if (Name == "llvm.x86.avx.vpermil.ps")
Craig Topperd3c9e402012-04-18 05:24:00 +0000164 PS128 = true;
165
166 if (PD256 || PD128 || PS256 || PS128) {
167 Value *Op0 = CI->getArgOperand(0);
168 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
169 SmallVector<Constant*, 8> Idxs;
170
171 if (PD128)
172 for (unsigned i = 0; i != 2; ++i)
173 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
174 else if (PD256)
175 for (unsigned l = 0; l != 4; l+=2)
176 for (unsigned i = 0; i != 2; ++i)
177 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
178 else if (PS128)
179 for (unsigned i = 0; i != 4; ++i)
180 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
181 else if (PS256)
182 for (unsigned l = 0; l != 8; l+=4)
183 for (unsigned i = 0; i != 4; ++i)
184 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
185 else
186 llvm_unreachable("Unexpected function");
187
188 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
189 } else {
190 llvm_unreachable("Unknown function for CallInst upgrade.");
191 }
Craig Topper3b1817d2012-02-03 06:10:55 +0000192 }
193
194 CI->replaceAllUsesWith(Rep);
195 CI->eraseFromParent();
196 return;
197 }
198
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000199 switch (NewFn->getIntrinsicID()) {
200 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +0000201 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000202
203 case Intrinsic::ctlz:
Nuno Lopesad40c0a2012-05-22 15:25:31 +0000204 case Intrinsic::cttz:
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000205 assert(CI->getNumArgOperands() == 1 &&
206 "Mismatch between function args and call args");
207 StringRef Name = CI->getName();
208 CI->setName(Name + ".old");
209 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
210 Builder.getFalse(), Name));
211 CI->eraseFromParent();
212 return;
Evan Cheng0e179d02007-12-17 22:33:23 +0000213 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000214}
215
216// This tests each Function to determine if it needs upgrading. When we find
217// one we are interested in, we then upgrade all calls to reflect the new
218// function.
219void llvm::UpgradeCallsToIntrinsic(Function* F) {
220 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
221
222 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000223 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000224 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000225 if (NewFn != F) {
226 // Replace all uses to the old function with the new one if necessary.
227 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
228 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000229 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000230 UpgradeIntrinsicCall(CI, NewFn);
231 }
232 // Remove old function, no longer used, from the module.
233 F->eraseFromParent();
234 }
235 }
236}
Devang Patel80ae3492009-08-28 23:24:31 +0000237