blob: 6bdb115a9aacb4465557c7bfe156065dfc81b33c [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"
Bill Wendling45449b12011-08-25 23:22:40 +000017#include "llvm/Instruction.h"
Owen Anderson155dccd82009-07-07 23:43:39 +000018#include "llvm/LLVMContext.h"
Chandler Carruth7132e002007-08-04 01:51:18 +000019#include "llvm/Module.h"
Devang Patel80ae3492009-08-28 23:24:31 +000020#include "llvm/IntrinsicInst.h"
Bill Wendling45449b12011-08-25 23:22:40 +000021#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner8a923e72008-03-12 17:45:29 +000023#include "llvm/ADT/SmallVector.h"
Gabor Greife5406532010-06-23 08:45:32 +000024#include "llvm/Support/CallSite.h"
Bill Wendling45449b12011-08-25 23:22:40 +000025#include "llvm/Support/CFG.h"
Torok Edwin56d06592009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Eric Christopher64831c62010-04-20 00:59:54 +000027#include "llvm/Support/IRBuilder.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000028#include <cstring>
Chandler Carruth7132e002007-08-04 01:51:18 +000029using namespace llvm;
30
31
Evan Cheng0e179d02007-12-17 22:33:23 +000032static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000033 assert(F && "Illegal to upgrade a non-existent Function.");
34
Chandler Carruth7132e002007-08-04 01:51:18 +000035 // Quickly eliminate it, if it's not a candidate.
Chris Lattnerb372f662011-06-18 18:56:39 +000036 StringRef Name = F->getName();
37 if (Name.size() <= 8 || !Name.startswith("llvm."))
Evan Cheng0e179d02007-12-17 22:33:23 +000038 return false;
Chris Lattnerb372f662011-06-18 18:56:39 +000039 Name = Name.substr(5); // Strip off "llvm."
Chris Lattner0bcbde42011-11-27 08:42:07 +000040
Chris Lattnerb372f662011-06-18 18:56:39 +000041 switch (Name[0]) {
Chandler Carruth7132e002007-08-04 01:51:18 +000042 default: break;
Chandler Carruth58a71ed2011-12-12 04:26:04 +000043 case 'c': {
44 Type *Tys[] = { F->arg_begin()->getType() };
45 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
46 F->setName(Name + ".old");
47 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, Tys);
48 return true;
49 }
50 if (Name.startswith("cttz.") && F->arg_size() == 1) {
51 F->setName(Name + ".old");
52 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, Tys);
53 return true;
54 }
55 break;
56 }
Chris Lattnerb372f662011-06-18 18:56:39 +000057 }
Chandler Carruth7132e002007-08-04 01:51:18 +000058
59 // This may not belong here. This function is effectively being overloaded
60 // to both detect an intrinsic which needs upgrading, and to provide the
61 // upgraded form of the intrinsic. We should perhaps have two separate
62 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +000063 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +000064}
65
Evan Cheng0e179d02007-12-17 22:33:23 +000066bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
67 NewFn = 0;
68 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +000069
70 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +000071 if (NewFn)
72 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +000073 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +000074 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +000075 return Upgraded;
76}
77
Bill Wendlinge26fffc2010-09-10 18:51:56 +000078bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +000079 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +000080 return false;
81}
82
Chandler Carruth7132e002007-08-04 01:51:18 +000083// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
84// upgraded intrinsic. All argument and return casting must be provided in
85// order to seamlessly integrate with existing context.
86void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Chandler Carruth7132e002007-08-04 01:51:18 +000087 Function *F = CI->getCalledFunction();
Chandler Carruth58a71ed2011-12-12 04:26:04 +000088 LLVMContext &C = CI->getContext();
Gabor Greife5406532010-06-23 08:45:32 +000089
Chandler Carruth7132e002007-08-04 01:51:18 +000090 assert(F && "CallInst has no function associated with it.");
Evan Cheng0e179d02007-12-17 22:33:23 +000091
Chandler Carruth58a71ed2011-12-12 04:26:04 +000092 if (!NewFn) return;
93
94 IRBuilder<> Builder(C);
95 Builder.SetInsertPoint(CI->getParent(), CI);
96
97 switch (NewFn->getIntrinsicID()) {
98 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +000099 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000100
101 case Intrinsic::ctlz:
102 case Intrinsic::cttz:
103 assert(CI->getNumArgOperands() == 1 &&
104 "Mismatch between function args and call args");
105 StringRef Name = CI->getName();
106 CI->setName(Name + ".old");
107 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
108 Builder.getFalse(), Name));
109 CI->eraseFromParent();
110 return;
Evan Cheng0e179d02007-12-17 22:33:23 +0000111 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000112}
113
114// This tests each Function to determine if it needs upgrading. When we find
115// one we are interested in, we then upgrade all calls to reflect the new
116// function.
117void llvm::UpgradeCallsToIntrinsic(Function* F) {
118 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
119
120 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000121 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000122 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000123 if (NewFn != F) {
124 // Replace all uses to the old function with the new one if necessary.
125 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
126 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000127 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000128 UpgradeIntrinsicCall(CI, NewFn);
129 }
130 // Remove old function, no longer used, from the module.
131 F->eraseFromParent();
132 }
133 }
134}
Devang Patel80ae3492009-08-28 23:24:31 +0000135