522b07e45452d9aeaf5b5be75dd0823a98289adb
[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       NewFn = 0;
65       return true;
66     }
67     break;
68   }
69   }
70
71   //  This may not belong here. This function is effectively being overloaded 
72   //  to both detect an intrinsic which needs upgrading, and to provide the 
73   //  upgraded form of the intrinsic. We should perhaps have two separate 
74   //  functions for this.
75   return false;
76 }
77
78 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
79   NewFn = 0;
80   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
81
82   // Upgrade intrinsic attributes.  This does not change the function.
83   if (NewFn)
84     F = NewFn;
85   if (unsigned id = F->getIntrinsicID())
86     F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
87   return Upgraded;
88 }
89
90 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
91   // Nothing to do yet.
92   return false;
93 }
94
95 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
96 // upgraded intrinsic. All argument and return casting must be provided in 
97 // order to seamlessly integrate with existing context.
98 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
99   Function *F = CI->getCalledFunction();
100   LLVMContext &C = CI->getContext();
101   IRBuilder<> Builder(C);
102   Builder.SetInsertPoint(CI->getParent(), CI);
103
104   assert(F && "Intrinsic call is not direct?");
105
106   if (!NewFn) {
107     // Get the Function's name.
108     StringRef Name = F->getName();
109
110     Value *Rep;
111     // Upgrade packed integer vector compares intrinsics to compare instructions
112     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
113         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
114       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
115                                  "pcmpeq");
116       // need to sign extend since icmp returns vector of i1
117       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
118     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
119                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
120       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
121                                   "pcmpgt");
122       // need to sign extend since icmp returns vector of i1
123       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
124     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
125                Name == "llvm.x86.avx.movnt.ps.256" ||
126                Name == "llvm.x86.avx.movnt.pd.256") {
127       IRBuilder<> Builder(C);
128       Builder.SetInsertPoint(CI->getParent(), CI);
129
130       Module *M = F->getParent();
131       SmallVector<Value *, 1> Elts;
132       Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
133       MDNode *Node = MDNode::get(C, Elts);
134
135       Value *Arg0 = CI->getArgOperand(0);
136       Value *Arg1 = CI->getArgOperand(1);
137
138       // Convert the type of the pointer to a pointer to the stored type.
139       Value *BC = Builder.CreateBitCast(Arg0,
140                                         PointerType::getUnqual(Arg1->getType()),
141                                         "cast");
142       StoreInst *SI = Builder.CreateStore(Arg1, BC);
143       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
144       SI->setAlignment(16);
145
146       // Remove intrinsic.
147       CI->eraseFromParent();
148       return;
149     } else {
150       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
151       if (Name == "llvm.x86.avx.vpermil.pd.256")
152         PD256 = true;
153       else if (Name == "llvm.x86.avx.vpermil.pd")
154         PD128 = true;
155       else if (Name == "llvm.x86.avx.vpermil.ps.256")
156         PS256 = true;
157       else if (Name == "llvm.x86.avx.vpermil.ps")
158         PS128 = true;
159
160       if (PD256 || PD128 || PS256 || PS128) {
161         Value *Op0 = CI->getArgOperand(0);
162         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
163         SmallVector<Constant*, 8> Idxs;
164
165         if (PD128)
166           for (unsigned i = 0; i != 2; ++i)
167             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
168         else if (PD256)
169           for (unsigned l = 0; l != 4; l+=2)
170             for (unsigned i = 0; i != 2; ++i)
171               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
172         else if (PS128)
173           for (unsigned i = 0; i != 4; ++i)
174             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
175         else if (PS256)
176           for (unsigned l = 0; l != 8; l+=4)
177             for (unsigned i = 0; i != 4; ++i)
178               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
179         else
180           llvm_unreachable("Unexpected function");
181
182         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
183       } else {
184         llvm_unreachable("Unknown function for CallInst upgrade.");
185       }
186     }
187
188     CI->replaceAllUsesWith(Rep);
189     CI->eraseFromParent();
190     return;
191   }
192
193   switch (NewFn->getIntrinsicID()) {
194   default:
195     llvm_unreachable("Unknown function for CallInst upgrade.");
196
197   case Intrinsic::ctlz:
198   case Intrinsic::cttz:
199     assert(CI->getNumArgOperands() == 1 &&
200            "Mismatch between function args and call args");
201     StringRef Name = CI->getName();
202     CI->setName(Name + ".old");
203     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
204                                                Builder.getFalse(), Name));
205     CI->eraseFromParent();
206     return;
207   }
208 }
209
210 // This tests each Function to determine if it needs upgrading. When we find 
211 // one we are interested in, we then upgrade all calls to reflect the new 
212 // function.
213 void llvm::UpgradeCallsToIntrinsic(Function* F) {
214   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
215
216   // Upgrade the function and check if it is a totaly new function.
217   Function *NewFn;
218   if (UpgradeIntrinsicFunction(F, NewFn)) {
219     if (NewFn != F) {
220       // Replace all uses to the old function with the new one if necessary.
221       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
222            UI != UE; ) {
223         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
224           UpgradeIntrinsicCall(CI, NewFn);
225       }
226       // Remove old function, no longer used, from the module.
227       F->eraseFromParent();
228     }
229   }
230 }
231