blob: 9e93ff370e256e1e5b749bd386e84a04f3f7ba50 [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"
Owen Anderson155dccd82009-07-07 23:43:39 +000017#include "llvm/LLVMContext.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000018#include "llvm/Module.h"
Devang Patel80ae3492009-08-28 23:24:31 +000019#include "llvm/IntrinsicInst.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000020#include "llvm/ADT/SmallVector.h"
Gabor Greife5406532010-06-23 08:45:32 +000021#include "llvm/Support/CallSite.h"
Torok Edwin56d06592009-07-11 20:10:48 +000022#include "llvm/Support/ErrorHandling.h"
Eric Christopher64831c62010-04-20 00:59:54 +000023#include "llvm/Support/IRBuilder.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000024#include <cstring>
Chandler Carruth7132e002007-08-04 01:51:18 +000025using namespace llvm;
26
27
Evan Cheng0e179d02007-12-17 22:33:23 +000028static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000029 assert(F && "Illegal to upgrade a non-existent Function.");
30
Chandler Carruth7132e002007-08-04 01:51:18 +000031 // Quickly eliminate it, if it's not a candidate.
Chris Lattnerb372f662011-06-18 18:56:39 +000032 StringRef Name = F->getName();
33 if (Name.size() <= 8 || !Name.startswith("llvm."))
Evan Cheng0e179d02007-12-17 22:33:23 +000034 return false;
Chris Lattnerb372f662011-06-18 18:56:39 +000035 Name = Name.substr(5); // Strip off "llvm."
Chandler Carruth7132e002007-08-04 01:51:18 +000036
Chris Lattnerb372f662011-06-18 18:56:39 +000037 const FunctionType *FTy = F->getFunctionType();
Chandler Carruth7132e002007-08-04 01:51:18 +000038 Module *M = F->getParent();
Chris Lattnerb372f662011-06-18 18:56:39 +000039
40 switch (Name[0]) {
Chandler Carruth7132e002007-08-04 01:51:18 +000041 default: break;
Chandler Carruth7132e002007-08-04 01:51:18 +000042 case 'p':
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000043 // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
44 // which is a instruction / data cache identifier. The old version only
45 // implicitly accepted the data version.
Chris Lattnerb372f662011-06-18 18:56:39 +000046 if (Name == "prefetch") {
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000047 // Don't do anything if it has the correct number of arguments already
48 if (FTy->getNumParams() == 4)
49 break;
50
51 assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
52 // We first need to change the name of the old (bad) intrinsic, because
53 // its type is incorrect, but we cannot overload that name. We
54 // arbitrarily unique it here allowing us to construct a correctly named
55 // and typed function below.
Chris Lattnerb372f662011-06-18 18:56:39 +000056 std::string NameTmp = F->getName();
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000057 F->setName("");
Chris Lattnerb372f662011-06-18 18:56:39 +000058 NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000059 FTy->getReturnType(),
60 FTy->getParamType(0),
61 FTy->getParamType(1),
62 FTy->getParamType(2),
63 FTy->getParamType(2),
64 (Type*)0));
65 return true;
66 }
67
Chandler Carruth7132e002007-08-04 01:51:18 +000068 break;
Chris Lattnerb372f662011-06-18 18:56:39 +000069 case 'x': {
70 const char *NewFnName = NULL;
71 // This fixes the poorly named crc32 intrinsics.
72 if (Name == "x86.sse42.crc32.8")
73 NewFnName = "llvm.x86.sse42.crc32.32.8";
74 else if (Name == "x86.sse42.crc32.16")
75 NewFnName = "llvm.x86.sse42.crc32.32.16";
76 else if (Name == "x86.sse42.crc32.32")
77 NewFnName = "llvm.x86.sse42.crc32.32.32";
78 else if (Name == "x86.sse42.crc64.8")
79 NewFnName = "llvm.x86.sse42.crc32.64.8";
80 else if (Name == "x86.sse42.crc64.64")
81 NewFnName = "llvm.x86.sse42.crc32.64.64";
82
83 if (NewFnName) {
84 F->setName(NewFnName);
85 NewFn = F;
86 return true;
Chad Rosierb3628842011-05-26 23:13:19 +000087 }
88
Chris Lattnerb372f662011-06-18 18:56:39 +000089 // Calls to these instructions are transformed into unaligned loads.
90 if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" ||
91 Name == "x86.sse2.loadu.pd")
Bill Wendlingb902f1d2011-04-13 00:36:11 +000092 return true;
Chris Lattner80ed9dc2011-06-18 06:05:24 +000093
Chris Lattnerb372f662011-06-18 18:56:39 +000094 // Calls to these instructions are transformed into nontemporal stores.
95 if (Name == "x86.sse.movnt.ps" || Name == "x86.sse2.movnt.dq" ||
96 Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i")
Bill Wendlingdb0996c2011-05-03 21:11:17 +000097 return true;
Evan Cheng0e179d02007-12-17 22:33:23 +000098
Anders Carlssonf924f342007-12-14 06:38:54 +000099 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000100 }
Chris Lattnerb372f662011-06-18 18:56:39 +0000101 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000102
103 // This may not belong here. This function is effectively being overloaded
104 // to both detect an intrinsic which needs upgrading, and to provide the
105 // upgraded form of the intrinsic. We should perhaps have two separate
106 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000107 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000108}
109
Evan Cheng0e179d02007-12-17 22:33:23 +0000110bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
111 NewFn = 0;
112 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000113
114 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000115 if (NewFn)
116 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000117 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000118 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000119 return Upgraded;
120}
121
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000122bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000123 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000124 return false;
125}
126
Chandler Carruth7132e002007-08-04 01:51:18 +0000127// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
128// upgraded intrinsic. All argument and return casting must be provided in
129// order to seamlessly integrate with existing context.
130void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000131 Function *F = CI->getCalledFunction();
Owen Anderson55f1c092009-08-13 21:58:54 +0000132 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +0000133 ImmutableCallSite CS(CI);
134
Chandler Carruth7132e002007-08-04 01:51:18 +0000135 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000136
137 if (!NewFn) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000138 if (F->getName() == "llvm.x86.sse.loadu.ps" ||
139 F->getName() == "llvm.x86.sse2.loadu.dq" ||
140 F->getName() == "llvm.x86.sse2.loadu.pd") {
Bill Wendlingb902f1d2011-04-13 00:36:11 +0000141 // Convert to a native, unaligned load.
142 const Type *VecTy = CI->getType();
143 const Type *IntTy = IntegerType::get(C, 128);
144 IRBuilder<> Builder(C);
145 Builder.SetInsertPoint(CI->getParent(), CI);
146
147 Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
148 PointerType::getUnqual(IntTy),
149 "cast");
150 LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
151 LI->setAlignment(1); // Unaligned load.
152 BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
153
154 // Fix up all the uses with our new load.
155 if (!CI->use_empty())
156 CI->replaceAllUsesWith(BC);
157
158 // Remove intrinsic.
159 CI->eraseFromParent();
Bill Wendlingdb0996c2011-05-03 21:11:17 +0000160 } else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
161 F->getName() == "llvm.x86.sse2.movnt.dq" ||
162 F->getName() == "llvm.x86.sse2.movnt.pd" ||
163 F->getName() == "llvm.x86.sse2.movnt.i") {
164 IRBuilder<> Builder(C);
165 Builder.SetInsertPoint(CI->getParent(), CI);
166
167 Module *M = F->getParent();
168 SmallVector<Value *, 1> Elts;
169 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
170 MDNode *Node = MDNode::get(C, Elts);
171
172 Value *Arg0 = CI->getArgOperand(0);
173 Value *Arg1 = CI->getArgOperand(1);
174
175 // Convert the type of the pointer to a pointer to the stored type.
176 Value *BC = Builder.CreateBitCast(Arg0,
177 PointerType::getUnqual(Arg1->getType()),
178 "cast");
179 StoreInst *SI = Builder.CreateStore(Arg1, BC);
180 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
181 SI->setAlignment(16);
182
183 // Remove intrinsic.
184 CI->eraseFromParent();
Evan Chenga8288f42007-12-18 01:04:25 +0000185 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000186 llvm_unreachable("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000187 }
188 return;
189 }
190
Gabor Greife9ecc682008-04-06 20:25:17 +0000191 switch (NewFn->getIntrinsicID()) {
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +0000192 case Intrinsic::prefetch: {
193 IRBuilder<> Builder(C);
194 Builder.SetInsertPoint(CI->getParent(), CI);
195 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
196
197 // Add the extra "data cache" argument
198 Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
199 CI->getArgOperand(2),
200 llvm::ConstantInt::get(I32Ty, 1) };
Jay Foad5bd375a2011-07-15 08:37:34 +0000201 CallInst *NewCI = CallInst::Create(NewFn, Operands,
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +0000202 CI->getName(), CI);
203 NewCI->setTailCall(CI->isTailCall());
204 NewCI->setCallingConv(CI->getCallingConv());
205 // Handle any uses of the old CallInst.
206 if (!CI->use_empty())
207 // Replace all uses of the old call with the new cast which has the
208 // correct type.
209 CI->replaceAllUsesWith(NewCI);
210
211 // Clean up the old call now that it has been completely upgraded.
212 CI->eraseFromParent();
213 break;
214 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000215 }
216}
217
218// This tests each Function to determine if it needs upgrading. When we find
219// one we are interested in, we then upgrade all calls to reflect the new
220// function.
221void llvm::UpgradeCallsToIntrinsic(Function* F) {
222 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
223
224 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000225 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000226 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000227 if (NewFn != F) {
228 // Replace all uses to the old function with the new one if necessary.
229 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
230 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000231 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000232 UpgradeIntrinsicCall(CI, NewFn);
233 }
234 // Remove old function, no longer used, from the module.
235 F->eraseFromParent();
236 }
237 }
238}
Devang Patel80ae3492009-08-28 23:24:31 +0000239
Victor Hernandezc2044a12010-01-05 21:13:46 +0000240/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
241/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
242/// strips that use.
Devang Patel80ae3492009-08-28 23:24:31 +0000243void llvm::CheckDebugInfoIntrinsics(Module *M) {
Devang Patel80ae3492009-08-28 23:24:31 +0000244 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000245 while (!FuncStart->use_empty())
246 cast<CallInst>(FuncStart->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000247 FuncStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000248 }
Devang Patelbe94f232010-01-05 01:10:40 +0000249
Devang Patel80ae3492009-08-28 23:24:31 +0000250 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000251 while (!StopPoint->use_empty())
252 cast<CallInst>(StopPoint->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000253 StopPoint->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000254 }
255
256 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000257 while (!RegionStart->use_empty())
258 cast<CallInst>(RegionStart->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000259 RegionStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000260 }
261
262 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000263 while (!RegionEnd->use_empty())
264 cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000265 RegionEnd->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000266 }
267
268 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
269 if (!Declare->use_empty()) {
270 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000271 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
272 !isa<MDNode>(DDI->getArgOperand(1))) {
Devang Patel80ae3492009-08-28 23:24:31 +0000273 while (!Declare->use_empty()) {
274 CallInst *CI = cast<CallInst>(Declare->use_back());
275 CI->eraseFromParent();
276 }
277 Declare->eraseFromParent();
278 }
279 }
280 }
281}