Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1 | //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the auto-upgrade helper functions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/AutoUpgrade.h" |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
Bill Wendling | 45449b1 | 2011-08-25 23:22:40 +0000 | [diff] [blame] | 17 | #include "llvm/Instruction.h" |
Owen Anderson | 155dccd8 | 2009-07-07 23:43:39 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 20 | #include "llvm/IntrinsicInst.h" |
Bill Wendling | 45449b1 | 2011-08-25 23:22:40 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Gabor Greif | e540653 | 2010-06-23 08:45:32 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CallSite.h" |
Bill Wendling | 45449b1 | 2011-08-25 23:22:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 64831c6 | 2010-04-20 00:59:54 +0000 | [diff] [blame] | 27 | #include "llvm/Support/IRBuilder.h" |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 28 | #include <cstring> |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 32 | static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 33 | assert(F && "Illegal to upgrade a non-existent Function."); |
| 34 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 35 | // Quickly eliminate it, if it's not a candidate. |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 36 | StringRef Name = F->getName(); |
| 37 | if (Name.size() <= 8 || !Name.startswith("llvm.")) |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 38 | return false; |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 39 | Name = Name.substr(5); // Strip off "llvm." |
Chris Lattner | 0bcbde4 | 2011-11-27 08:42:07 +0000 | [diff] [blame] | 40 | |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 41 | switch (Name[0]) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 42 | default: break; |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 43 | case 'c': { |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 44 | if (Name.startswith("ctlz.") && F->arg_size() == 1) { |
| 45 | F->setName(Name + ".old"); |
Chandler Carruth | d4a0240 | 2011-12-12 10:57:20 +0000 | [diff] [blame] | 46 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, |
| 47 | F->arg_begin()->getType()); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | if (Name.startswith("cttz.") && F->arg_size() == 1) { |
| 51 | F->setName(Name + ".old"); |
Chandler Carruth | d4a0240 | 2011-12-12 10:57:20 +0000 | [diff] [blame] | 52 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, |
| 53 | F->arg_begin()->getType()); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 54 | return true; |
| 55 | } |
| 56 | break; |
| 57 | } |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 58 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 59 | |
| 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 Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 64 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 67 | bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { |
| 68 | NewFn = 0; |
| 69 | bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 70 | |
| 71 | // Upgrade intrinsic attributes. This does not change the function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 72 | if (NewFn) |
| 73 | F = NewFn; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 74 | if (unsigned id = F->getIntrinsicID()) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 75 | F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id)); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 76 | return Upgraded; |
| 77 | } |
| 78 | |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 79 | bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 80 | // Nothing to do yet. |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 84 | // 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. |
| 87 | void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { |
Nick Lewycky | 2eb3ade | 2011-12-12 22:59:34 +0000 | [diff] [blame] | 88 | assert(CI->getCalledFunction() && "Intrinsic call is not direct?"); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 89 | if (!NewFn) return; |
| 90 | |
Nick Lewycky | 2eb3ade | 2011-12-12 22:59:34 +0000 | [diff] [blame] | 91 | LLVMContext &C = CI->getContext(); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 92 | IRBuilder<> Builder(C); |
| 93 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 94 | |
| 95 | switch (NewFn->getIntrinsicID()) { |
| 96 | default: |
Chris Lattner | 0bcbde4 | 2011-11-27 08:42:07 +0000 | [diff] [blame] | 97 | llvm_unreachable("Unknown function for CallInst upgrade."); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 98 | |
| 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 Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 109 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 110 | } |
| 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. |
| 115 | void 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 Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 119 | Function *NewFn; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 120 | if (UpgradeIntrinsicFunction(F, NewFn)) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 121 | 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 Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 125 | if (CallInst *CI = dyn_cast<CallInst>(*UI++)) |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 126 | UpgradeIntrinsicCall(CI, NewFn); |
| 127 | } |
| 128 | // Remove old function, no longer used, from the module. |
| 129 | F->eraseFromParent(); |
| 130 | } |
| 131 | } |
| 132 | } |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 133 | |