1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the auto-upgrade helper functions
12 //===----------------------------------------------------------------------===//
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"
29 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
30 assert(F && "Illegal to upgrade a non-existent Function.");
32 // Quickly eliminate it, if it's not a candidate.
33 StringRef Name = F->getName();
34 if (Name.size() <= 8 || !Name.startswith("llvm."))
36 Name = Name.substr(5); // Strip off "llvm."
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());
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());
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") {
67 // Fix the FMA4 intrinsics to remove the 4
68 if (Name.startswith("x86.fma4.")) {
69 F->setName("llvm.x86.fma" + Name.substr(8));
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.
84 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
86 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
88 // Upgrade intrinsic attributes. This does not change the function.
91 if (unsigned id = F->getIntrinsicID())
92 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
96 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
101 // 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.
104 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
105 Function *F = CI->getCalledFunction();
106 LLVMContext &C = CI->getContext();
107 IRBuilder<> Builder(C);
108 Builder.SetInsertPoint(CI->getParent(), CI);
110 assert(F && "Intrinsic call is not direct?");
113 // Get the Function's name.
114 StringRef Name = F->getName();
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),
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),
128 // need to sign extend since icmp returns vector of i1
129 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
130 } 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);
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);
141 Value *Arg0 = CI->getArgOperand(0);
142 Value *Arg1 = CI->getArgOperand(1);
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()),
148 StoreInst *SI = Builder.CreateStore(Arg1, BC);
149 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
150 SI->setAlignment(16);
153 CI->eraseFromParent();
156 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
157 if (Name == "llvm.x86.avx.vpermil.pd.256")
159 else if (Name == "llvm.x86.avx.vpermil.pd")
161 else if (Name == "llvm.x86.avx.vpermil.ps.256")
163 else if (Name == "llvm.x86.avx.vpermil.ps")
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;
172 for (unsigned i = 0; i != 2; ++i)
173 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
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));
179 for (unsigned i = 0; i != 4; ++i)
180 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
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));
186 llvm_unreachable("Unexpected function");
188 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
190 llvm_unreachable("Unknown function for CallInst upgrade.");
194 CI->replaceAllUsesWith(Rep);
195 CI->eraseFromParent();
199 switch (NewFn->getIntrinsicID()) {
201 llvm_unreachable("Unknown function for CallInst upgrade.");
203 case Intrinsic::ctlz:
204 case Intrinsic::cttz:
205 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();
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
219 void llvm::UpgradeCallsToIntrinsic(Function* F) {
220 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
222 // Upgrade the function and check if it is a totaly new function.
224 if (UpgradeIntrinsicFunction(F, NewFn)) {
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();
229 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
230 UpgradeIntrinsicCall(CI, NewFn);
232 // Remove old function, no longer used, from the module.
233 F->eraseFromParent();