blob: 59424f9644b3dff8fc401f9388d5e641d46d50ce [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': {
Chandler Carruth58a71ed2011-12-12 04:26:04 +000044 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
45 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +000046 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
47 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +000048 return true;
49 }
50 if (Name.startswith("cttz.") && F->arg_size() == 1) {
51 F->setName(Name + ".old");
Chandler Carruthd4a02402011-12-12 10:57:20 +000052 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
53 F->arg_begin()->getType());
Chandler Carruth58a71ed2011-12-12 04:26:04 +000054 return true;
55 }
56 break;
57 }
Chris Lattnerb372f662011-06-18 18:56:39 +000058 }
Chandler Carruth7132e002007-08-04 01:51:18 +000059
60 // This may not belong here. This function is effectively being overloaded
61 // to both detect an intrinsic which needs upgrading, and to provide the
62 // upgraded form of the intrinsic. We should perhaps have two separate
63 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +000064 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +000065}
66
Evan Cheng0e179d02007-12-17 22:33:23 +000067bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
68 NewFn = 0;
69 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +000070
71 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +000072 if (NewFn)
73 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +000074 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +000075 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +000076 return Upgraded;
77}
78
Bill Wendlinge26fffc2010-09-10 18:51:56 +000079bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +000080 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +000081 return false;
82}
83
Chandler Carruth7132e002007-08-04 01:51:18 +000084// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
85// upgraded intrinsic. All argument and return casting must be provided in
86// order to seamlessly integrate with existing context.
87void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Nick Lewycky2eb3ade2011-12-12 22:59:34 +000088 assert(CI->getCalledFunction() && "Intrinsic call is not direct?");
Chandler Carruth58a71ed2011-12-12 04:26:04 +000089 if (!NewFn) return;
90
Nick Lewycky2eb3ade2011-12-12 22:59:34 +000091 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +000092 IRBuilder<> Builder(C);
93 Builder.SetInsertPoint(CI->getParent(), CI);
94
95 switch (NewFn->getIntrinsicID()) {
96 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +000097 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +000098
99 case Intrinsic::ctlz:
100 case Intrinsic::cttz:
101 assert(CI->getNumArgOperands() == 1 &&
102 "Mismatch between function args and call args");
103 StringRef Name = CI->getName();
104 CI->setName(Name + ".old");
105 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
106 Builder.getFalse(), Name));
107 CI->eraseFromParent();
108 return;
Evan Cheng0e179d02007-12-17 22:33:23 +0000109 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000110}
111
112// This tests each Function to determine if it needs upgrading. When we find
113// one we are interested in, we then upgrade all calls to reflect the new
114// function.
115void llvm::UpgradeCallsToIntrinsic(Function* F) {
116 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
117
118 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000119 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000120 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000121 if (NewFn != F) {
122 // Replace all uses to the old function with the new one if necessary.
123 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
124 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000125 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000126 UpgradeIntrinsicCall(CI, NewFn);
127 }
128 // Remove old function, no longer used, from the module.
129 F->eraseFromParent();
130 }
131 }
132}
Devang Patel80ae3492009-08-28 23:24:31 +0000133