blob: ea3d4ba2243653a8d13edccb4c6cd8ade7b9ff18 [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 }
Craig Topper3b1817d2012-02-03 06:10:55 +000058 case 'x': {
59 if (Name.startswith("x86.sse2.pcmpeq.") ||
60 Name.startswith("x86.sse2.pcmpgt.") ||
61 Name.startswith("x86.avx2.pcmpeq.") ||
62 Name.startswith("x86.avx2.pcmpgt.")) {
63 NewFn = 0;
64 return true;
65 }
66 break;
67 }
Chris Lattnerb372f662011-06-18 18:56:39 +000068 }
Chandler Carruth7132e002007-08-04 01:51:18 +000069
70 // This may not belong here. This function is effectively being overloaded
71 // to both detect an intrinsic which needs upgrading, and to provide the
72 // upgraded form of the intrinsic. We should perhaps have two separate
73 // functions for this.
Evan Cheng0e179d02007-12-17 22:33:23 +000074 return false;
Chandler Carruth7132e002007-08-04 01:51:18 +000075}
76
Evan Cheng0e179d02007-12-17 22:33:23 +000077bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
78 NewFn = 0;
79 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
Duncan Sands38ef3a82007-12-03 20:06:50 +000080
81 // Upgrade intrinsic attributes. This does not change the function.
Evan Cheng0e179d02007-12-17 22:33:23 +000082 if (NewFn)
83 F = NewFn;
Dale Johannesenb842d522009-02-05 01:49:45 +000084 if (unsigned id = F->getIntrinsicID())
Devang Patel4c758ea2008-09-25 21:00:45 +000085 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
Duncan Sands38ef3a82007-12-03 20:06:50 +000086 return Upgraded;
87}
88
Bill Wendlinge26fffc2010-09-10 18:51:56 +000089bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +000090 // Nothing to do yet.
Bill Wendlinge26fffc2010-09-10 18:51:56 +000091 return false;
92}
93
Chandler Carruth7132e002007-08-04 01:51:18 +000094// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
95// upgraded intrinsic. All argument and return casting must be provided in
96// order to seamlessly integrate with existing context.
97void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
Craig Topper3b1817d2012-02-03 06:10:55 +000098 Function *F = CI->getCalledFunction();
Nick Lewycky2eb3ade2011-12-12 22:59:34 +000099 LLVMContext &C = CI->getContext();
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000100 IRBuilder<> Builder(C);
101 Builder.SetInsertPoint(CI->getParent(), CI);
102
Craig Topper3b1817d2012-02-03 06:10:55 +0000103 assert(F && "Intrinsic call is not direct?");
104
105 if (!NewFn) {
106 // Get the Function's name.
107 StringRef Name = F->getName();
108
109 Value *Rep;
110 // Upgrade packed integer vector compares intrinsics to compare instructions
111 if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
112 Name.startswith("llvm.x86.avx2.pcmpeq.")) {
113 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
114 "pcmpeq");
115 // need to sign extend since icmp returns vector of i1
116 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
117 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
118 Name.startswith("llvm.x86.avx2.pcmpgt.")) {
119 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
120 "pcmpgt");
121 // need to sign extend since icmp returns vector of i1
122 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
123 } else {
124 llvm_unreachable("Unknown function for CallInst upgrade.");
125 }
126
127 CI->replaceAllUsesWith(Rep);
128 CI->eraseFromParent();
129 return;
130 }
131
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000132 switch (NewFn->getIntrinsicID()) {
133 default:
Chris Lattner0bcbde42011-11-27 08:42:07 +0000134 llvm_unreachable("Unknown function for CallInst upgrade.");
Chandler Carruth58a71ed2011-12-12 04:26:04 +0000135
136 case Intrinsic::ctlz:
137 case Intrinsic::cttz:
138 assert(CI->getNumArgOperands() == 1 &&
139 "Mismatch between function args and call args");
140 StringRef Name = CI->getName();
141 CI->setName(Name + ".old");
142 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
143 Builder.getFalse(), Name));
144 CI->eraseFromParent();
145 return;
Evan Cheng0e179d02007-12-17 22:33:23 +0000146 }
Chandler Carruth7132e002007-08-04 01:51:18 +0000147}
148
149// This tests each Function to determine if it needs upgrading. When we find
150// one we are interested in, we then upgrade all calls to reflect the new
151// function.
152void llvm::UpgradeCallsToIntrinsic(Function* F) {
153 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
154
155 // Upgrade the function and check if it is a totaly new function.
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000156 Function *NewFn;
Evan Cheng0e179d02007-12-17 22:33:23 +0000157 if (UpgradeIntrinsicFunction(F, NewFn)) {
Chandler Carruth7132e002007-08-04 01:51:18 +0000158 if (NewFn != F) {
159 // Replace all uses to the old function with the new one if necessary.
160 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
161 UI != UE; ) {
Chris Lattner80ed9dc2011-06-18 06:05:24 +0000162 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
Chandler Carruth7132e002007-08-04 01:51:18 +0000163 UpgradeIntrinsicCall(CI, NewFn);
164 }
165 // Remove old function, no longer used, from the module.
166 F->eraseFromParent();
167 }
168 }
169}
Devang Patel80ae3492009-08-28 23:24:31 +0000170