b1d44cc71df524f8f951d6df0b5c80ec942ebd30
[oota-llvm.git] / lib / VMCore / AutoUpgrade.cpp
1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the auto-upgrade helper functions 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/AutoUpgrade.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Function.h"
17 #include "llvm/Instruction.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/Support/CFG.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/IRBuilder.h"
25 #include <cstring>
26 using namespace llvm;
27
28
29 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
30   assert(F && "Illegal to upgrade a non-existent Function.");
31
32   // Quickly eliminate it, if it's not a candidate.
33   StringRef Name = F->getName();
34   if (Name.size() <= 8 || !Name.startswith("llvm."))
35     return false;
36   Name = Name.substr(5); // Strip off "llvm."
37
38   switch (Name[0]) {
39   default: break;
40   case 'c': {
41     if (Name.startswith("ctlz.") && F->arg_size() == 1) {
42       F->setName(Name + ".old");
43       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
44                                         F->arg_begin()->getType());
45       return true;
46     }
47     if (Name.startswith("cttz.") && F->arg_size() == 1) {
48       F->setName(Name + ".old");
49       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
50                                         F->arg_begin()->getType());
51       return true;
52     }
53     break;
54   }
55   case 'x': {
56     if (Name.startswith("x86.sse2.pcmpeq.") ||
57         Name.startswith("x86.sse2.pcmpgt.") ||
58         Name.startswith("x86.avx2.pcmpeq.") ||
59         Name.startswith("x86.avx2.pcmpgt.") ||
60         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" ||
64         (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
65       NewFn = 0;
66       return true;
67     }
68     // Fix the FMA4 intrinsics to remove the 4
69     if (Name.startswith("x86.fma4.")) {
70       F->setName("llvm.x86.fma" + Name.substr(8));
71       NewFn = F;
72       return true;
73     }
74     break;
75   }
76   }
77
78   //  This may not belong here. This function is effectively being overloaded 
79   //  to both detect an intrinsic which needs upgrading, and to provide the 
80   //  upgraded form of the intrinsic. We should perhaps have two separate 
81   //  functions for this.
82   return false;
83 }
84
85 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
86   NewFn = 0;
87   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
88
89   // Upgrade intrinsic attributes.  This does not change the function.
90   if (NewFn)
91     F = NewFn;
92   if (unsigned id = F->getIntrinsicID())
93     F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
94   return Upgraded;
95 }
96
97 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
98   // Nothing to do yet.
99   return false;
100 }
101
102 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
103 // upgraded intrinsic. All argument and return casting must be provided in 
104 // order to seamlessly integrate with existing context.
105 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
106   Function *F = CI->getCalledFunction();
107   LLVMContext &C = CI->getContext();
108   IRBuilder<> Builder(C);
109   Builder.SetInsertPoint(CI->getParent(), CI);
110
111   assert(F && "Intrinsic call is not direct?");
112
113   if (!NewFn) {
114     // Get the Function's name.
115     StringRef Name = F->getName();
116
117     Value *Rep;
118     // Upgrade packed integer vector compares intrinsics to compare instructions
119     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
120         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
121       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
122                                  "pcmpeq");
123       // need to sign extend since icmp returns vector of i1
124       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
125     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
126                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
127       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
128                                   "pcmpgt");
129       // need to sign extend since icmp returns vector of i1
130       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
131     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
132                Name == "llvm.x86.avx.movnt.ps.256" ||
133                Name == "llvm.x86.avx.movnt.pd.256") {
134       IRBuilder<> Builder(C);
135       Builder.SetInsertPoint(CI->getParent(), CI);
136
137       Module *M = F->getParent();
138       SmallVector<Value *, 1> Elts;
139       Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
140       MDNode *Node = MDNode::get(C, Elts);
141
142       Value *Arg0 = CI->getArgOperand(0);
143       Value *Arg1 = CI->getArgOperand(1);
144
145       // Convert the type of the pointer to a pointer to the stored type.
146       Value *BC = Builder.CreateBitCast(Arg0,
147                                         PointerType::getUnqual(Arg1->getType()),
148                                         "cast");
149       StoreInst *SI = Builder.CreateStore(Arg1, BC);
150       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
151       SI->setAlignment(16);
152
153       // Remove intrinsic.
154       CI->eraseFromParent();
155       return;
156     } else if (Name.startswith("llvm.x86.xop.vpcom")) {
157       Intrinsic::ID intID;
158       if (Name.endswith("ub"))
159         intID = Intrinsic::x86_xop_vpcomub;
160       else if (Name.endswith("uw"))
161         intID = Intrinsic::x86_xop_vpcomuw;
162       else if (Name.endswith("ud"))
163         intID = Intrinsic::x86_xop_vpcomud;
164       else if (Name.endswith("uq"))
165         intID = Intrinsic::x86_xop_vpcomuq;
166       else if (Name.endswith("b"))
167         intID = Intrinsic::x86_xop_vpcomb;
168       else if (Name.endswith("w"))
169         intID = Intrinsic::x86_xop_vpcomw;
170       else if (Name.endswith("d"))
171         intID = Intrinsic::x86_xop_vpcomd;
172       else if (Name.endswith("q"))
173         intID = Intrinsic::x86_xop_vpcomq;
174       else
175         llvm_unreachable("Unknown suffix");
176
177       Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
178       unsigned Imm;
179       if (Name.startswith("lt"))
180         Imm = 0;
181       else if (Name.startswith("le"))
182         Imm = 1;
183       else if (Name.startswith("gt"))
184         Imm = 2;
185       else if (Name.startswith("ge"))
186         Imm = 3;
187       else if (Name.startswith("eq"))
188         Imm = 4;
189       else if (Name.startswith("ne"))
190         Imm = 5;
191       else if (Name.startswith("true"))
192         Imm = 6;
193       else if (Name.startswith("false"))
194         Imm = 7;
195       else
196         llvm_unreachable("Unknown condition");
197
198       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
199       Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
200                                 CI->getArgOperand(1), Builder.getInt8(Imm));
201     } else {
202       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
203       if (Name == "llvm.x86.avx.vpermil.pd.256")
204         PD256 = true;
205       else if (Name == "llvm.x86.avx.vpermil.pd")
206         PD128 = true;
207       else if (Name == "llvm.x86.avx.vpermil.ps.256")
208         PS256 = true;
209       else if (Name == "llvm.x86.avx.vpermil.ps")
210         PS128 = true;
211
212       if (PD256 || PD128 || PS256 || PS128) {
213         Value *Op0 = CI->getArgOperand(0);
214         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
215         SmallVector<Constant*, 8> Idxs;
216
217         if (PD128)
218           for (unsigned i = 0; i != 2; ++i)
219             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
220         else if (PD256)
221           for (unsigned l = 0; l != 4; l+=2)
222             for (unsigned i = 0; i != 2; ++i)
223               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
224         else if (PS128)
225           for (unsigned i = 0; i != 4; ++i)
226             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
227         else if (PS256)
228           for (unsigned l = 0; l != 8; l+=4)
229             for (unsigned i = 0; i != 4; ++i)
230               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
231         else
232           llvm_unreachable("Unexpected function");
233
234         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
235       } else {
236         llvm_unreachable("Unknown function for CallInst upgrade.");
237       }
238     }
239
240     CI->replaceAllUsesWith(Rep);
241     CI->eraseFromParent();
242     return;
243   }
244
245   switch (NewFn->getIntrinsicID()) {
246   default:
247     llvm_unreachable("Unknown function for CallInst upgrade.");
248
249   case Intrinsic::ctlz:
250   case Intrinsic::cttz:
251     assert(CI->getNumArgOperands() == 1 &&
252            "Mismatch between function args and call args");
253     StringRef Name = CI->getName();
254     CI->setName(Name + ".old");
255     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
256                                                Builder.getFalse(), Name));
257     CI->eraseFromParent();
258     return;
259   }
260 }
261
262 // This tests each Function to determine if it needs upgrading. When we find 
263 // one we are interested in, we then upgrade all calls to reflect the new 
264 // function.
265 void llvm::UpgradeCallsToIntrinsic(Function* F) {
266   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
267
268   // Upgrade the function and check if it is a totaly new function.
269   Function *NewFn;
270   if (UpgradeIntrinsicFunction(F, NewFn)) {
271     if (NewFn != F) {
272       // Replace all uses to the old function with the new one if necessary.
273       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
274            UI != UE; ) {
275         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
276           UpgradeIntrinsicCall(CI, NewFn);
277       }
278       // Remove old function, no longer used, from the module.
279       F->eraseFromParent();
280     }
281   }
282 }
283