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/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/IRBuilder.h"
32 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
33 assert(F && "Illegal to upgrade a non-existent Function.");
35 // Quickly eliminate it, if it's not a candidate.
36 StringRef Name = F->getName();
37 if (Name.size() <= 8 || !Name.startswith("llvm."))
39 Name = Name.substr(5); // Strip off "llvm."
41 FunctionType *FTy = F->getFunctionType();
42 Module *M = F->getParent();
47 if (Name.startswith("atomic.cmp.swap") ||
48 Name.startswith("atomic.swap") ||
49 Name.startswith("atomic.load.add") ||
50 Name.startswith("atomic.load.sub") ||
51 Name.startswith("atomic.load.and") ||
52 Name.startswith("atomic.load.nand") ||
53 Name.startswith("atomic.load.or") ||
54 Name.startswith("atomic.load.xor") ||
55 Name.startswith("atomic.load.max") ||
56 Name.startswith("atomic.load.min") ||
57 Name.startswith("atomic.load.umax") ||
58 Name.startswith("atomic.load.umin"))
61 // This upgrades the old llvm.init.trampoline to the new
62 // llvm.init.trampoline and llvm.adjust.trampoline pair.
63 if (Name == "init.trampoline") {
64 // The new llvm.init.trampoline returns nothing.
65 if (FTy->getReturnType()->isVoidTy())
68 assert(FTy->getNumParams() == 3 && "old init.trampoline takes 3 args!");
70 // Change the name of the old intrinsic so that we can play with its type.
71 std::string NameTmp = F->getName();
73 NewFn = cast<Function>(M->getOrInsertFunction(
75 Type::getVoidTy(M->getContext()),
76 FTy->getParamType(0), FTy->getParamType(1),
77 FTy->getParamType(2), (Type *)0));
81 if (Name == "memory.barrier")
84 // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
85 // which is a instruction / data cache identifier. The old version only
86 // implicitly accepted the data version.
87 if (Name == "prefetch") {
88 // Don't do anything if it has the correct number of arguments already
89 if (FTy->getNumParams() == 4)
92 assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
93 // We first need to change the name of the old (bad) intrinsic, because
94 // its type is incorrect, but we cannot overload that name. We
95 // arbitrarily unique it here allowing us to construct a correctly named
96 // and typed function below.
97 std::string NameTmp = F->getName();
99 NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
100 FTy->getReturnType(),
101 FTy->getParamType(0),
102 FTy->getParamType(1),
103 FTy->getParamType(2),
104 FTy->getParamType(2),
111 const char *NewFnName = NULL;
112 // This fixes the poorly named crc32 intrinsics.
113 if (Name == "x86.sse42.crc32.8")
114 NewFnName = "llvm.x86.sse42.crc32.32.8";
115 else if (Name == "x86.sse42.crc32.16")
116 NewFnName = "llvm.x86.sse42.crc32.32.16";
117 else if (Name == "x86.sse42.crc32.32")
118 NewFnName = "llvm.x86.sse42.crc32.32.32";
119 else if (Name == "x86.sse42.crc64.8")
120 NewFnName = "llvm.x86.sse42.crc32.64.8";
121 else if (Name == "x86.sse42.crc64.64")
122 NewFnName = "llvm.x86.sse42.crc32.64.64";
125 F->setName(NewFnName);
130 // Calls to these instructions are transformed into unaligned loads.
131 if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" ||
132 Name == "x86.sse2.loadu.pd")
135 // Calls to these instructions are transformed into nontemporal stores.
136 if (Name == "x86.sse.movnt.ps" || Name == "x86.sse2.movnt.dq" ||
137 Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i")
144 // This may not belong here. This function is effectively being overloaded
145 // to both detect an intrinsic which needs upgrading, and to provide the
146 // upgraded form of the intrinsic. We should perhaps have two separate
147 // functions for this.
151 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
153 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
155 // Upgrade intrinsic attributes. This does not change the function.
158 if (unsigned id = F->getIntrinsicID())
159 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
163 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
164 // Nothing to do yet.
168 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
169 // upgraded intrinsic. All argument and return casting must be provided in
170 // order to seamlessly integrate with existing context.
171 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
172 Function *F = CI->getCalledFunction();
173 LLVMContext &C = CI->getContext();
174 ImmutableCallSite CS(CI);
176 assert(F && "CallInst has no function associated with it.");
179 if (F->getName() == "llvm.x86.sse.loadu.ps" ||
180 F->getName() == "llvm.x86.sse2.loadu.dq" ||
181 F->getName() == "llvm.x86.sse2.loadu.pd") {
182 // Convert to a native, unaligned load.
183 Type *VecTy = CI->getType();
184 Type *IntTy = IntegerType::get(C, 128);
185 IRBuilder<> Builder(C);
186 Builder.SetInsertPoint(CI->getParent(), CI);
188 Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
189 PointerType::getUnqual(IntTy),
191 LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
192 LI->setAlignment(1); // Unaligned load.
193 BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
195 // Fix up all the uses with our new load.
196 if (!CI->use_empty())
197 CI->replaceAllUsesWith(BC);
200 CI->eraseFromParent();
201 } else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
202 F->getName() == "llvm.x86.sse2.movnt.dq" ||
203 F->getName() == "llvm.x86.sse2.movnt.pd" ||
204 F->getName() == "llvm.x86.sse2.movnt.i") {
205 IRBuilder<> Builder(C);
206 Builder.SetInsertPoint(CI->getParent(), CI);
208 Module *M = F->getParent();
209 SmallVector<Value *, 1> Elts;
210 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
211 MDNode *Node = MDNode::get(C, Elts);
213 Value *Arg0 = CI->getArgOperand(0);
214 Value *Arg1 = CI->getArgOperand(1);
216 // Convert the type of the pointer to a pointer to the stored type.
217 Value *BC = Builder.CreateBitCast(Arg0,
218 PointerType::getUnqual(Arg1->getType()),
220 StoreInst *SI = Builder.CreateStore(Arg1, BC);
221 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
222 SI->setAlignment(16);
225 CI->eraseFromParent();
226 } else if (F->getName().startswith("llvm.atomic.cmp.swap")) {
227 IRBuilder<> Builder(C);
228 Builder.SetInsertPoint(CI->getParent(), CI);
229 Value *Val = Builder.CreateAtomicCmpXchg(CI->getArgOperand(0),
230 CI->getArgOperand(1),
231 CI->getArgOperand(2),
234 // Replace intrinsic.
236 if (!CI->use_empty())
237 CI->replaceAllUsesWith(Val);
238 CI->eraseFromParent();
239 } else if (F->getName().startswith("llvm.atomic")) {
240 IRBuilder<> Builder(C);
241 Builder.SetInsertPoint(CI->getParent(), CI);
243 AtomicRMWInst::BinOp Op;
244 if (F->getName().startswith("llvm.atomic.swap"))
245 Op = AtomicRMWInst::Xchg;
246 else if (F->getName().startswith("llvm.atomic.load.add"))
247 Op = AtomicRMWInst::Add;
248 else if (F->getName().startswith("llvm.atomic.load.sub"))
249 Op = AtomicRMWInst::Sub;
250 else if (F->getName().startswith("llvm.atomic.load.and"))
251 Op = AtomicRMWInst::And;
252 else if (F->getName().startswith("llvm.atomic.load.nand"))
253 Op = AtomicRMWInst::Nand;
254 else if (F->getName().startswith("llvm.atomic.load.or"))
255 Op = AtomicRMWInst::Or;
256 else if (F->getName().startswith("llvm.atomic.load.xor"))
257 Op = AtomicRMWInst::Xor;
258 else if (F->getName().startswith("llvm.atomic.load.max"))
259 Op = AtomicRMWInst::Max;
260 else if (F->getName().startswith("llvm.atomic.load.min"))
261 Op = AtomicRMWInst::Min;
262 else if (F->getName().startswith("llvm.atomic.load.umax"))
263 Op = AtomicRMWInst::UMax;
264 else if (F->getName().startswith("llvm.atomic.load.umin"))
265 Op = AtomicRMWInst::UMin;
267 llvm_unreachable("Unknown atomic");
269 Value *Val = Builder.CreateAtomicRMW(Op, CI->getArgOperand(0),
270 CI->getArgOperand(1),
273 // Replace intrinsic.
275 if (!CI->use_empty())
276 CI->replaceAllUsesWith(Val);
277 CI->eraseFromParent();
278 } else if (F->getName() == "llvm.memory.barrier") {
279 IRBuilder<> Builder(C);
280 Builder.SetInsertPoint(CI->getParent(), CI);
282 // Note that this conversion ignores the "device" bit; it was not really
283 // well-defined, and got abused because nobody paid enough attention to
284 // get it right. In practice, this probably doesn't matter; application
285 // code generally doesn't need anything stronger than
286 // SequentiallyConsistent (and realistically, SequentiallyConsistent
287 // is lowered to a strong enough barrier for almost anything).
289 if (cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue())
290 Builder.CreateFence(SequentiallyConsistent);
291 else if (!cast<ConstantInt>(CI->getArgOperand(0))->getZExtValue())
292 Builder.CreateFence(Release);
293 else if (!cast<ConstantInt>(CI->getArgOperand(3))->getZExtValue())
294 Builder.CreateFence(Acquire);
296 Builder.CreateFence(AcquireRelease);
299 CI->eraseFromParent();
301 llvm_unreachable("Unknown function for CallInst upgrade.");
306 switch (NewFn->getIntrinsicID()) {
307 case Intrinsic::prefetch: {
308 IRBuilder<> Builder(C);
309 Builder.SetInsertPoint(CI->getParent(), CI);
310 llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
312 // Add the extra "data cache" argument
313 Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
314 CI->getArgOperand(2),
315 llvm::ConstantInt::get(I32Ty, 1) };
316 CallInst *NewCI = CallInst::Create(NewFn, Operands,
318 NewCI->setTailCall(CI->isTailCall());
319 NewCI->setCallingConv(CI->getCallingConv());
320 // Handle any uses of the old CallInst.
321 if (!CI->use_empty())
322 // Replace all uses of the old call with the new cast which has the
324 CI->replaceAllUsesWith(NewCI);
326 // Clean up the old call now that it has been completely upgraded.
327 CI->eraseFromParent();
330 case Intrinsic::init_trampoline: {
333 // %tramp = call i8* llvm.init.trampoline (i8* x, i8* y, i8* z)
335 // call void llvm.init.trampoline (i8* %x, i8* %y, i8* %z)
336 // %tramp = call i8* llvm.adjust.trampoline (i8* %x)
338 Function *AdjustTrampolineFn =
339 cast<Function>(Intrinsic::getDeclaration(F->getParent(),
340 Intrinsic::adjust_trampoline));
342 IRBuilder<> Builder(C);
343 Builder.SetInsertPoint(CI);
345 Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1),
346 CI->getArgOperand(2));
348 CallInst *AdjustCall = Builder.CreateCall(AdjustTrampolineFn,
349 CI->getArgOperand(0),
351 if (!CI->use_empty())
352 CI->replaceAllUsesWith(AdjustCall);
353 CI->eraseFromParent();
359 // This tests each Function to determine if it needs upgrading. When we find
360 // one we are interested in, we then upgrade all calls to reflect the new
362 void llvm::UpgradeCallsToIntrinsic(Function* F) {
363 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
365 // Upgrade the function and check if it is a totaly new function.
367 if (UpgradeIntrinsicFunction(F, NewFn)) {
369 // Replace all uses to the old function with the new one if necessary.
370 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
372 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
373 UpgradeIntrinsicCall(CI, NewFn);
375 // Remove old function, no longer used, from the module.
376 F->eraseFromParent();
381 /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
382 /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
384 void llvm::CheckDebugInfoIntrinsics(Module *M) {
385 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
386 while (!FuncStart->use_empty())
387 cast<CallInst>(FuncStart->use_back())->eraseFromParent();
388 FuncStart->eraseFromParent();
391 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
392 while (!StopPoint->use_empty())
393 cast<CallInst>(StopPoint->use_back())->eraseFromParent();
394 StopPoint->eraseFromParent();
397 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
398 while (!RegionStart->use_empty())
399 cast<CallInst>(RegionStart->use_back())->eraseFromParent();
400 RegionStart->eraseFromParent();
403 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
404 while (!RegionEnd->use_empty())
405 cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
406 RegionEnd->eraseFromParent();
409 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
410 if (!Declare->use_empty()) {
411 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
412 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
413 !isa<MDNode>(DDI->getArgOperand(1))) {
414 while (!Declare->use_empty()) {
415 CallInst *CI = cast<CallInst>(Declare->use_back());
416 CI->eraseFromParent();
418 Declare->eraseFromParent();