2007-08-04 03:51:18 +02:00
|
|
|
//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-04 03:51:18 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the auto-upgrade helper functions
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/AutoUpgrade.h"
|
2007-12-14 07:38:54 +01:00
|
|
|
#include "llvm/Constants.h"
|
2007-08-04 03:51:18 +02:00
|
|
|
#include "llvm/Function.h"
|
2009-07-08 01:43:39 +02:00
|
|
|
#include "llvm/LLVMContext.h"
|
2007-08-04 03:51:18 +02:00
|
|
|
#include "llvm/Module.h"
|
2009-08-29 01:24:31 +02:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2008-03-12 18:45:29 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-06-23 10:45:32 +02:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2009-07-11 22:10:48 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-04-20 02:59:54 +02:00
|
|
|
#include "llvm/Support/IRBuilder.h"
|
2008-02-20 12:08:44 +01:00
|
|
|
#include <cstring>
|
2007-08-04 03:51:18 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
2007-12-17 23:33:23 +01:00
|
|
|
static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
|
2007-08-04 03:51:18 +02:00
|
|
|
assert(F && "Illegal to upgrade a non-existent Function.");
|
|
|
|
|
|
|
|
// Quickly eliminate it, if it's not a candidate.
|
2011-06-18 20:56:39 +02:00
|
|
|
StringRef Name = F->getName();
|
|
|
|
if (Name.size() <= 8 || !Name.startswith("llvm."))
|
2007-12-17 23:33:23 +01:00
|
|
|
return false;
|
2011-06-18 20:56:39 +02:00
|
|
|
Name = Name.substr(5); // Strip off "llvm."
|
2007-08-04 03:51:18 +02:00
|
|
|
|
2011-07-18 06:54:35 +02:00
|
|
|
FunctionType *FTy = F->getFunctionType();
|
2007-08-04 03:51:18 +02:00
|
|
|
Module *M = F->getParent();
|
2011-06-18 20:56:39 +02:00
|
|
|
|
|
|
|
switch (Name[0]) {
|
2007-08-04 03:51:18 +02:00
|
|
|
default: break;
|
|
|
|
case 'p':
|
2011-06-14 06:58:37 +02:00
|
|
|
// This upgrades the llvm.prefetch intrinsic to accept one more parameter,
|
|
|
|
// which is a instruction / data cache identifier. The old version only
|
|
|
|
// implicitly accepted the data version.
|
2011-06-18 20:56:39 +02:00
|
|
|
if (Name == "prefetch") {
|
2011-06-14 06:58:37 +02:00
|
|
|
// Don't do anything if it has the correct number of arguments already
|
|
|
|
if (FTy->getNumParams() == 4)
|
|
|
|
break;
|
|
|
|
|
|
|
|
assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
|
|
|
|
// We first need to change the name of the old (bad) intrinsic, because
|
|
|
|
// its type is incorrect, but we cannot overload that name. We
|
|
|
|
// arbitrarily unique it here allowing us to construct a correctly named
|
|
|
|
// and typed function below.
|
2011-06-18 20:56:39 +02:00
|
|
|
std::string NameTmp = F->getName();
|
2011-06-14 06:58:37 +02:00
|
|
|
F->setName("");
|
2011-06-18 20:56:39 +02:00
|
|
|
NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
|
2011-06-14 06:58:37 +02:00
|
|
|
FTy->getReturnType(),
|
|
|
|
FTy->getParamType(0),
|
|
|
|
FTy->getParamType(1),
|
|
|
|
FTy->getParamType(2),
|
|
|
|
FTy->getParamType(2),
|
|
|
|
(Type*)0));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-12-14 07:38:54 +01:00
|
|
|
break;
|
2011-06-18 20:56:39 +02:00
|
|
|
case 'x': {
|
|
|
|
const char *NewFnName = NULL;
|
|
|
|
// This fixes the poorly named crc32 intrinsics.
|
|
|
|
if (Name == "x86.sse42.crc32.8")
|
|
|
|
NewFnName = "llvm.x86.sse42.crc32.32.8";
|
|
|
|
else if (Name == "x86.sse42.crc32.16")
|
|
|
|
NewFnName = "llvm.x86.sse42.crc32.32.16";
|
|
|
|
else if (Name == "x86.sse42.crc32.32")
|
|
|
|
NewFnName = "llvm.x86.sse42.crc32.32.32";
|
|
|
|
else if (Name == "x86.sse42.crc64.8")
|
|
|
|
NewFnName = "llvm.x86.sse42.crc32.64.8";
|
|
|
|
else if (Name == "x86.sse42.crc64.64")
|
|
|
|
NewFnName = "llvm.x86.sse42.crc32.64.64";
|
|
|
|
|
|
|
|
if (NewFnName) {
|
|
|
|
F->setName(NewFnName);
|
|
|
|
NewFn = F;
|
|
|
|
return true;
|
2011-05-27 01:13:19 +02:00
|
|
|
}
|
|
|
|
|
2011-06-18 20:56:39 +02:00
|
|
|
// Calls to these instructions are transformed into unaligned loads.
|
|
|
|
if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" ||
|
|
|
|
Name == "x86.sse2.loadu.pd")
|
2011-04-13 02:36:11 +02:00
|
|
|
return true;
|
2011-06-18 08:05:24 +02:00
|
|
|
|
2011-06-18 20:56:39 +02:00
|
|
|
// Calls to these instructions are transformed into nontemporal stores.
|
|
|
|
if (Name == "x86.sse.movnt.ps" || Name == "x86.sse2.movnt.dq" ||
|
|
|
|
Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i")
|
2011-05-03 23:11:17 +02:00
|
|
|
return true;
|
2007-12-17 23:33:23 +01:00
|
|
|
|
2007-08-04 03:51:18 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-06-18 20:56:39 +02:00
|
|
|
}
|
2007-08-04 03:51:18 +02:00
|
|
|
|
|
|
|
// This may not belong here. This function is effectively being overloaded
|
|
|
|
// to both detect an intrinsic which needs upgrading, and to provide the
|
|
|
|
// upgraded form of the intrinsic. We should perhaps have two separate
|
|
|
|
// functions for this.
|
2007-12-17 23:33:23 +01:00
|
|
|
return false;
|
2007-08-04 03:51:18 +02:00
|
|
|
}
|
|
|
|
|
2007-12-17 23:33:23 +01:00
|
|
|
bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
|
|
|
|
NewFn = 0;
|
|
|
|
bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
|
2007-12-03 21:06:50 +01:00
|
|
|
|
|
|
|
// Upgrade intrinsic attributes. This does not change the function.
|
2007-12-17 23:33:23 +01:00
|
|
|
if (NewFn)
|
|
|
|
F = NewFn;
|
2009-02-05 02:49:45 +01:00
|
|
|
if (unsigned id = F->getIntrinsicID())
|
2008-09-25 23:00:45 +02:00
|
|
|
F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
|
2007-12-03 21:06:50 +01:00
|
|
|
return Upgraded;
|
|
|
|
}
|
|
|
|
|
2010-09-10 20:51:56 +02:00
|
|
|
bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
|
2011-06-18 08:05:24 +02:00
|
|
|
// Nothing to do yet.
|
2010-09-10 20:51:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-04 03:51:18 +02:00
|
|
|
// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
|
|
|
|
// upgraded intrinsic. All argument and return casting must be provided in
|
|
|
|
// order to seamlessly integrate with existing context.
|
|
|
|
void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
|
|
|
|
Function *F = CI->getCalledFunction();
|
2009-08-13 23:58:54 +02:00
|
|
|
LLVMContext &C = CI->getContext();
|
2010-06-23 10:45:32 +02:00
|
|
|
ImmutableCallSite CS(CI);
|
|
|
|
|
2007-08-04 03:51:18 +02:00
|
|
|
assert(F && "CallInst has no function associated with it.");
|
2007-12-17 23:33:23 +01:00
|
|
|
|
|
|
|
if (!NewFn) {
|
2011-06-18 08:05:24 +02:00
|
|
|
if (F->getName() == "llvm.x86.sse.loadu.ps" ||
|
|
|
|
F->getName() == "llvm.x86.sse2.loadu.dq" ||
|
|
|
|
F->getName() == "llvm.x86.sse2.loadu.pd") {
|
2011-04-13 02:36:11 +02:00
|
|
|
// Convert to a native, unaligned load.
|
2011-07-18 06:54:35 +02:00
|
|
|
Type *VecTy = CI->getType();
|
|
|
|
Type *IntTy = IntegerType::get(C, 128);
|
2011-04-13 02:36:11 +02:00
|
|
|
IRBuilder<> Builder(C);
|
|
|
|
Builder.SetInsertPoint(CI->getParent(), CI);
|
|
|
|
|
|
|
|
Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
|
|
|
|
PointerType::getUnqual(IntTy),
|
|
|
|
"cast");
|
|
|
|
LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
|
|
|
|
LI->setAlignment(1); // Unaligned load.
|
|
|
|
BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
|
|
|
|
|
|
|
|
// Fix up all the uses with our new load.
|
|
|
|
if (!CI->use_empty())
|
|
|
|
CI->replaceAllUsesWith(BC);
|
|
|
|
|
2011-05-03 23:11:17 +02:00
|
|
|
// Remove intrinsic.
|
|
|
|
CI->eraseFromParent();
|
|
|
|
} else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
|
|
|
|
F->getName() == "llvm.x86.sse2.movnt.dq" ||
|
|
|
|
F->getName() == "llvm.x86.sse2.movnt.pd" ||
|
|
|
|
F->getName() == "llvm.x86.sse2.movnt.i") {
|
|
|
|
IRBuilder<> Builder(C);
|
|
|
|
Builder.SetInsertPoint(CI->getParent(), CI);
|
|
|
|
|
|
|
|
Module *M = F->getParent();
|
|
|
|
SmallVector<Value *, 1> Elts;
|
|
|
|
Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
|
|
|
|
MDNode *Node = MDNode::get(C, Elts);
|
|
|
|
|
|
|
|
Value *Arg0 = CI->getArgOperand(0);
|
|
|
|
Value *Arg1 = CI->getArgOperand(1);
|
|
|
|
|
|
|
|
// Convert the type of the pointer to a pointer to the stored type.
|
|
|
|
Value *BC = Builder.CreateBitCast(Arg0,
|
|
|
|
PointerType::getUnqual(Arg1->getType()),
|
|
|
|
"cast");
|
|
|
|
StoreInst *SI = Builder.CreateStore(Arg1, BC);
|
|
|
|
SI->setMetadata(M->getMDKindID("nontemporal"), Node);
|
|
|
|
SI->setAlignment(16);
|
|
|
|
|
2011-04-13 02:36:11 +02:00
|
|
|
// Remove intrinsic.
|
|
|
|
CI->eraseFromParent();
|
2007-12-18 02:04:25 +01:00
|
|
|
} else {
|
2009-07-14 18:55:14 +02:00
|
|
|
llvm_unreachable("Unknown function for CallInst upgrade.");
|
2007-12-17 23:33:23 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-06 22:25:17 +02:00
|
|
|
switch (NewFn->getIntrinsicID()) {
|
2011-06-14 06:58:37 +02:00
|
|
|
case Intrinsic::prefetch: {
|
|
|
|
IRBuilder<> Builder(C);
|
|
|
|
Builder.SetInsertPoint(CI->getParent(), CI);
|
2011-07-18 06:54:35 +02:00
|
|
|
llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
|
2011-06-14 06:58:37 +02:00
|
|
|
|
|
|
|
// Add the extra "data cache" argument
|
|
|
|
Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
|
|
|
|
CI->getArgOperand(2),
|
|
|
|
llvm::ConstantInt::get(I32Ty, 1) };
|
2011-07-15 10:37:34 +02:00
|
|
|
CallInst *NewCI = CallInst::Create(NewFn, Operands,
|
2011-06-14 06:58:37 +02:00
|
|
|
CI->getName(), CI);
|
|
|
|
NewCI->setTailCall(CI->isTailCall());
|
|
|
|
NewCI->setCallingConv(CI->getCallingConv());
|
|
|
|
// Handle any uses of the old CallInst.
|
|
|
|
if (!CI->use_empty())
|
|
|
|
// Replace all uses of the old call with the new cast which has the
|
|
|
|
// correct type.
|
|
|
|
CI->replaceAllUsesWith(NewCI);
|
|
|
|
|
|
|
|
// Clean up the old call now that it has been completely upgraded.
|
|
|
|
CI->eraseFromParent();
|
|
|
|
break;
|
|
|
|
}
|
2007-08-04 03:51:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This tests each Function to determine if it needs upgrading. When we find
|
|
|
|
// one we are interested in, we then upgrade all calls to reflect the new
|
|
|
|
// function.
|
|
|
|
void llvm::UpgradeCallsToIntrinsic(Function* F) {
|
|
|
|
assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
|
|
|
|
|
|
|
|
// Upgrade the function and check if it is a totaly new function.
|
2011-06-18 08:05:24 +02:00
|
|
|
Function *NewFn;
|
2007-12-17 23:33:23 +01:00
|
|
|
if (UpgradeIntrinsicFunction(F, NewFn)) {
|
2007-08-04 03:51:18 +02:00
|
|
|
if (NewFn != F) {
|
|
|
|
// Replace all uses to the old function with the new one if necessary.
|
|
|
|
for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
|
|
|
|
UI != UE; ) {
|
2011-06-18 08:05:24 +02:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*UI++))
|
2007-08-04 03:51:18 +02:00
|
|
|
UpgradeIntrinsicCall(CI, NewFn);
|
|
|
|
}
|
|
|
|
// Remove old function, no longer used, from the module.
|
|
|
|
F->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-29 01:24:31 +02:00
|
|
|
|
2010-01-05 22:13:46 +01:00
|
|
|
/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
|
|
|
|
/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
|
|
|
|
/// strips that use.
|
2009-08-29 01:24:31 +02:00
|
|
|
void llvm::CheckDebugInfoIntrinsics(Module *M) {
|
|
|
|
if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
|
2011-06-18 08:05:24 +02:00
|
|
|
while (!FuncStart->use_empty())
|
|
|
|
cast<CallInst>(FuncStart->use_back())->eraseFromParent();
|
2010-01-05 02:10:40 +01:00
|
|
|
FuncStart->eraseFromParent();
|
2009-08-29 01:24:31 +02:00
|
|
|
}
|
2010-01-05 02:10:40 +01:00
|
|
|
|
2009-08-29 01:24:31 +02:00
|
|
|
if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
|
2011-06-18 08:05:24 +02:00
|
|
|
while (!StopPoint->use_empty())
|
|
|
|
cast<CallInst>(StopPoint->use_back())->eraseFromParent();
|
2010-01-05 02:10:40 +01:00
|
|
|
StopPoint->eraseFromParent();
|
2009-08-29 01:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
|
2011-06-18 08:05:24 +02:00
|
|
|
while (!RegionStart->use_empty())
|
|
|
|
cast<CallInst>(RegionStart->use_back())->eraseFromParent();
|
2010-01-05 02:10:40 +01:00
|
|
|
RegionStart->eraseFromParent();
|
2009-08-29 01:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
|
2011-06-18 08:05:24 +02:00
|
|
|
while (!RegionEnd->use_empty())
|
|
|
|
cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
|
2010-01-05 02:10:40 +01:00
|
|
|
RegionEnd->eraseFromParent();
|
2009-08-29 01:24:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
|
|
|
|
if (!Declare->use_empty()) {
|
|
|
|
DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
|
2010-06-22 22:40:38 +02:00
|
|
|
if (!isa<MDNode>(DDI->getArgOperand(0)) ||
|
|
|
|
!isa<MDNode>(DDI->getArgOperand(1))) {
|
2009-08-29 01:24:31 +02:00
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
Declare->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|