blob: 3ffcfdcbaf58f57b6a61f5a031b8a4224c12c4ce [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
31 // Get the Function's name.
32 const std::string& Name = F->getName();
33
34 // Convenience
35 const FunctionType *FTy = F->getFunctionType();
36
37 // Quickly eliminate it, if it's not a candidate.
38 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
39 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
Evan Cheng0e179d02007-12-17 22:33:23 +000040 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +000041
42 Module *M = F->getParent();
43 switch (Name[5]) {
44 default: break;
Chandler Carruth7132e002007-08-04 01:51:18 +000045 case 'p':
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +000046 // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
47 // which is a instruction / data cache identifier. The old version only
48 // implicitly accepted the data version.
49 if (Name.compare(5,8,"prefetch",8) == 0) {
50 // Don't do anything if it has the correct number of arguments already
51 if (FTy->getNumParams() == 4)
52 break;
53
54 assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
55 // We first need to change the name of the old (bad) intrinsic, because
56 // its type is incorrect, but we cannot overload that name. We
57 // arbitrarily unique it here allowing us to construct a correctly named
58 // and typed function below.
59 F->setName("");
60 NewFn = cast<Function>(M->getOrInsertFunction(Name,
61 FTy->getReturnType(),
62 FTy->getParamType(0),
63 FTy->getParamType(1),
64 FTy->getParamType(2),
65 FTy->getParamType(2),
66 (Type*)0));
67 return true;
68 }
69
Chandler Carruth7132e002007-08-04 01:51:18 +000070 break;
Chad Rosierb3628842011-05-26 23:13:19 +000071 case 'x':
72 // This fixes the poorly named crc32 intrinsics
73 if (Name.compare(5, 13, "x86.sse42.crc", 13) == 0) {
74 const char* NewFnName = NULL;
75 if (Name.compare(18, 2, "32", 2) == 0) {
Chad Rosier32521772011-05-27 19:38:10 +000076 if (Name.compare(20, 2, ".8") == 0 && Name.length() == 22) {
Chad Rosierb3628842011-05-26 23:13:19 +000077 NewFnName = "llvm.x86.sse42.crc32.32.8";
Chad Rosier32521772011-05-27 19:38:10 +000078 } else if (Name.compare(20, 3, ".16") == 0 && Name.length() == 23) {
Chad Rosierb3628842011-05-26 23:13:19 +000079 NewFnName = "llvm.x86.sse42.crc32.32.16";
Chad Rosier32521772011-05-27 19:38:10 +000080 } else if (Name.compare(20, 3, ".32") == 0 && Name.length() == 23) {
Chad Rosierb3628842011-05-26 23:13:19 +000081 NewFnName = "llvm.x86.sse42.crc32.32.32";
82 }
83 }
84 else if (Name.compare(18, 2, "64", 2) == 0) {
Chad Rosier32521772011-05-27 19:38:10 +000085 if (Name.compare(20, 2, ".8") == 0 && Name.length() == 22) {
Chad Rosierb3628842011-05-26 23:13:19 +000086 NewFnName = "llvm.x86.sse42.crc32.64.8";
Chad Rosier32521772011-05-27 19:38:10 +000087 } else if (Name.compare(20, 3, ".64") == 0 && Name.length() == 23) {
Chad Rosierb3628842011-05-26 23:13:19 +000088 NewFnName = "llvm.x86.sse42.crc32.64.64";
89 }
90 }
91 if (NewFnName) {
92 F->setName(NewFnName);
93 NewFn = F;
94 return true;
95 }
96 }
97
Chris Lattner80ed9dc2011-06-18 06:05:24 +000098 if (Name.compare(5, 16, "x86.sse.loadu.ps", 16) == 0 ||
99 Name.compare(5, 17, "x86.sse2.loadu.dq", 17) == 0 ||
100 Name.compare(5, 17, "x86.sse2.loadu.pd", 17) == 0) {
Bill Wendlingb902f1d2011-04-13 00:36:11 +0000101 // Calls to these instructions are transformed into unaligned loads.
102 NewFn = 0;
103 return true;
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000104 }
105
106 if (Name.compare(5, 16, "x86.sse.movnt.ps", 16) == 0 ||
107 Name.compare(5, 17, "x86.sse2.movnt.dq", 17) == 0 ||
108 Name.compare(5, 17, "x86.sse2.movnt.pd", 17) == 0 ||
109 Name.compare(5, 17, "x86.sse2.movnt.i", 16) == 0) {
Bill Wendlingdb0996c2011-05-03 21:11:17 +0000110 // Calls to these instructions are transformed into nontemporal stores.
111 NewFn = 0;
112 return true;
Anders Carlssonf924f342007-12-14 06:38:54 +0000113 }
Evan Cheng0e179d02007-12-17 22:33:23 +0000114
Anders Carlssonf924f342007-12-14 06:38:54 +0000115 break;
Chandler Carruth7132e002007-08-04 01:51:18 +0000116 }
117
118 // This may not belong here. This function is effectively being overloaded
119 // to both detect an intrinsic which needs upgrading, and to provide the
120 // upgraded form of the intrinsic. We should perhaps have two separate
121 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +0000122 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +0000123}
124
Evan Cheng0e179d02007-12-17 22:33:23 +0000125bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
126 NewFn = 0;
127 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +0000128
129 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +0000130 if (NewFn)
131 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +0000132 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +0000133 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +0000134 return Upgraded;
135}
136
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000137bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000138 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +0000139 return false;
140}
141
Chandler Carruth7132e002007-08-04 01:51:18 +0000142// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
143// upgraded intrinsic. All argument and return casting must be provided in
144// order to seamlessly integrate with existing context.
145void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000146 Function *F = CI->getCalledFunction();
Owen Anderson55f1c092009-08-13 21:58:54 +0000147 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +0000148 ImmutableCallSite CS(CI);
149
Chandler Carruth7132e002007-08-04 01:51:18 +0000150 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000151
152 if (!NewFn) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000153 if (F->getName() == "llvm.x86.sse.loadu.ps" ||
154 F->getName() == "llvm.x86.sse2.loadu.dq" ||
155 F->getName() == "llvm.x86.sse2.loadu.pd") {
Bill Wendlingb902f1d2011-04-13 00:36:11 +0000156 // Convert to a native, unaligned load.
157 const Type *VecTy = CI->getType();
158 const Type *IntTy = IntegerType::get(C, 128);
159 IRBuilder<> Builder(C);
160 Builder.SetInsertPoint(CI->getParent(), CI);
161
162 Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
163 PointerType::getUnqual(IntTy),
164 "cast");
165 LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
166 LI->setAlignment(1); // Unaligned load.
167 BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
168
169 // Fix up all the uses with our new load.
170 if (!CI->use_empty())
171 CI->replaceAllUsesWith(BC);
172
173 // Remove intrinsic.
174 CI->eraseFromParent();
Bill Wendlingdb0996c2011-05-03 21:11:17 +0000175 } else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
176 F->getName() == "llvm.x86.sse2.movnt.dq" ||
177 F->getName() == "llvm.x86.sse2.movnt.pd" ||
178 F->getName() == "llvm.x86.sse2.movnt.i") {
179 IRBuilder<> Builder(C);
180 Builder.SetInsertPoint(CI->getParent(), CI);
181
182 Module *M = F->getParent();
183 SmallVector<Value *, 1> Elts;
184 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
185 MDNode *Node = MDNode::get(C, Elts);
186
187 Value *Arg0 = CI->getArgOperand(0);
188 Value *Arg1 = CI->getArgOperand(1);
189
190 // Convert the type of the pointer to a pointer to the stored type.
191 Value *BC = Builder.CreateBitCast(Arg0,
192 PointerType::getUnqual(Arg1->getType()),
193 "cast");
194 StoreInst *SI = Builder.CreateStore(Arg1, BC);
195 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
196 SI->setAlignment(16);
197
198 // Remove intrinsic.
199 CI->eraseFromParent();
Evan Chenga8288f42007-12-18 01:04:25 +0000200 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000201 llvm_unreachable("Unknown function for CallInst upgrade.");
Evan Cheng0e179d02007-12-17 22:33:23 +0000202 }
203 return;
204 }
205
Gabor Greife9ecc682008-04-06 20:25:17 +0000206 switch (NewFn->getIntrinsicID()) {
Bruno Cardoso Lopesdc9ff3a2011-06-14 04:58:37 +0000207 case Intrinsic::prefetch: {
208 IRBuilder<> Builder(C);
209 Builder.SetInsertPoint(CI->getParent(), CI);
210 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
211
212 // Add the extra "data cache" argument
213 Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
214 CI->getArgOperand(2),
215 llvm::ConstantInt::get(I32Ty, 1) };
216 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+4,
217 CI->getName(), CI);
218 NewCI->setTailCall(CI->isTailCall());
219 NewCI->setCallingConv(CI->getCallingConv());
220 // Handle any uses of the old CallInst.
221 if (!CI->use_empty())
222 // Replace all uses of the old call with the new cast which has the
223 // correct type.
224 CI->replaceAllUsesWith(NewCI);
225
226 // Clean up the old call now that it has been completely upgraded.
227 CI->eraseFromParent();
228 break;
229 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000230 }
231}
232
233// This tests each Function to determine if it needs upgrading. When we find
234// one we are interested in, we then upgrade all calls to reflect the new
235// function.
236void llvm::UpgradeCallsToIntrinsic(Function* F) {
237 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
238
239 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000240 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000241 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000242 if (NewFn != F) {
243 // Replace all uses to the old function with the new one if necessary.
244 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
245 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000246 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000247 UpgradeIntrinsicCall(CI, NewFn);
248 }
249 // Remove old function, no longer used, from the module.
250 F->eraseFromParent();
251 }
252 }
253}
Devang Patel80ae3492009-08-28 23:24:31 +0000254
Victor Hernandezc2044a12010-01-05 21:13:46 +0000255/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
256/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
257/// strips that use.
Devang Patel80ae3492009-08-28 23:24:31 +0000258void llvm::CheckDebugInfoIntrinsics(Module *M) {
Devang Patel80ae3492009-08-28 23:24:31 +0000259 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000260 while (!FuncStart->use_empty())
261 cast<CallInst>(FuncStart->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000262 FuncStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000263 }
Devang Patelbe94f232010-01-05 01:10:40 +0000264
Devang Patel80ae3492009-08-28 23:24:31 +0000265 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000266 while (!StopPoint->use_empty())
267 cast<CallInst>(StopPoint->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000268 StopPoint->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000269 }
270
271 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000272 while (!RegionStart->use_empty())
273 cast<CallInst>(RegionStart->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000274 RegionStart->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000275 }
276
277 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000278 while (!RegionEnd->use_empty())
279 cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
Devang Patelbe94f232010-01-05 01:10:40 +0000280 RegionEnd->eraseFromParent();
Devang Patel80ae3492009-08-28 23:24:31 +0000281 }
282
283 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
284 if (!Declare->use_empty()) {
285 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
Gabor Greifc89d2aa2010-06-22 20:40:38 +0000286 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
287 !isa<MDNode>(DDI->getArgOperand(1))) {
Devang Patel80ae3492009-08-28 23:24:31 +0000288 while (!Declare->use_empty()) {
289 CallInst *CI = cast<CallInst>(Declare->use_back());
290 CI->eraseFromParent();
291 }
292 Declare->eraseFromParent();
293 }
294 }
295 }
296}