2007-08-04 01:51:18 +00:00
|
|
|
//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-04 01:51:18 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the auto-upgrade helper functions
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/AutoUpgrade.h"
|
2007-12-14 06:38:54 +00:00
|
|
|
#include "llvm/Constants.h"
|
2007-08-04 01:51:18 +00:00
|
|
|
#include "llvm/Function.h"
|
2009-07-07 23:43:39 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2007-08-04 01:51:18 +00:00
|
|
|
#include "llvm/Module.h"
|
2009-08-28 23:24:31 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2008-03-12 17:45:29 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-06-23 08:45:32 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-04-20 00:59:54 +00:00
|
|
|
#include "llvm/Support/IRBuilder.h"
|
2008-02-20 11:08:44 +00:00
|
|
|
#include <cstring>
|
2007-08-04 01:51:18 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
2007-12-17 22:33:23 +00:00
|
|
|
static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
|
2007-08-04 01:51:18 +00:00
|
|
|
assert(F && "Illegal to upgrade a non-existent Function.");
|
|
|
|
|
|
|
|
// Get the Function's name.
|
|
|
|
const std::string& Name = F->getName();
|
|
|
|
|
|
|
|
// Convenience
|
|
|
|
const FunctionType *FTy = F->getFunctionType();
|
|
|
|
|
|
|
|
// Quickly eliminate it, if it's not a candidate.
|
|
|
|
if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
|
|
|
|
Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
|
2007-12-17 22:33:23 +00:00
|
|
|
return false;
|
2007-08-04 01:51:18 +00:00
|
|
|
|
|
|
|
Module *M = F->getParent();
|
|
|
|
switch (Name[5]) {
|
|
|
|
default: break;
|
|
|
|
case 'p':
|
2011-06-14 04:58:37 +00: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.
|
|
|
|
if (Name.compare(5,8,"prefetch",8) == 0) {
|
|
|
|
// 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.
|
|
|
|
F->setName("");
|
|
|
|
NewFn = cast<Function>(M->getOrInsertFunction(Name,
|
|
|
|
FTy->getReturnType(),
|
|
|
|
FTy->getParamType(0),
|
|
|
|
FTy->getParamType(1),
|
|
|
|
FTy->getParamType(2),
|
|
|
|
FTy->getParamType(2),
|
|
|
|
(Type*)0));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-12-14 06:38:54 +00:00
|
|
|
break;
|
2011-05-26 23:13:19 +00:00
|
|
|
case 'x':
|
|
|
|
// This fixes the poorly named crc32 intrinsics
|
|
|
|
if (Name.compare(5, 13, "x86.sse42.crc", 13) == 0) {
|
|
|
|
const char* NewFnName = NULL;
|
|
|
|
if (Name.compare(18, 2, "32", 2) == 0) {
|
2011-05-27 19:38:10 +00:00
|
|
|
if (Name.compare(20, 2, ".8") == 0 && Name.length() == 22) {
|
2011-05-26 23:13:19 +00:00
|
|
|
NewFnName = "llvm.x86.sse42.crc32.32.8";
|
2011-05-27 19:38:10 +00:00
|
|
|
} else if (Name.compare(20, 3, ".16") == 0 && Name.length() == 23) {
|
2011-05-26 23:13:19 +00:00
|
|
|
NewFnName = "llvm.x86.sse42.crc32.32.16";
|
2011-05-27 19:38:10 +00:00
|
|
|
} else if (Name.compare(20, 3, ".32") == 0 && Name.length() == 23) {
|
2011-05-26 23:13:19 +00:00
|
|
|
NewFnName = "llvm.x86.sse42.crc32.32.32";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Name.compare(18, 2, "64", 2) == 0) {
|
2011-05-27 19:38:10 +00:00
|
|
|
if (Name.compare(20, 2, ".8") == 0 && Name.length() == 22) {
|
2011-05-26 23:13:19 +00:00
|
|
|
NewFnName = "llvm.x86.sse42.crc32.64.8";
|
2011-05-27 19:38:10 +00:00
|
|
|
} else if (Name.compare(20, 3, ".64") == 0 && Name.length() == 23) {
|
2011-05-26 23:13:19 +00:00
|
|
|
NewFnName = "llvm.x86.sse42.crc32.64.64";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NewFnName) {
|
|
|
|
F->setName(NewFnName);
|
|
|
|
NewFn = F;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-18 06:05:24 +00:00
|
|
|
if (Name.compare(5, 16, "x86.sse.loadu.ps", 16) == 0 ||
|
|
|
|
Name.compare(5, 17, "x86.sse2.loadu.dq", 17) == 0 ||
|
|
|
|
Name.compare(5, 17, "x86.sse2.loadu.pd", 17) == 0) {
|
2011-04-13 00:36:11 +00:00
|
|
|
// Calls to these instructions are transformed into unaligned loads.
|
|
|
|
NewFn = 0;
|
|
|
|
return true;
|
2011-06-18 06:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Name.compare(5, 16, "x86.sse.movnt.ps", 16) == 0 ||
|
|
|
|
Name.compare(5, 17, "x86.sse2.movnt.dq", 17) == 0 ||
|
|
|
|
Name.compare(5, 17, "x86.sse2.movnt.pd", 17) == 0 ||
|
|
|
|
Name.compare(5, 17, "x86.sse2.movnt.i", 16) == 0) {
|
2011-05-03 21:11:17 +00:00
|
|
|
// Calls to these instructions are transformed into nontemporal stores.
|
|
|
|
NewFn = 0;
|
|
|
|
return true;
|
2007-12-14 06:38:54 +00:00
|
|
|
}
|
2007-12-17 22:33:23 +00:00
|
|
|
|
2007-08-04 01:51:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 22:33:23 +00:00
|
|
|
return false;
|
2007-08-04 01:51:18 +00:00
|
|
|
}
|
|
|
|
|
2007-12-17 22:33:23 +00:00
|
|
|
bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
|
|
|
|
NewFn = 0;
|
|
|
|
bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
|
2007-12-03 20:06:50 +00:00
|
|
|
|
|
|
|
// Upgrade intrinsic attributes. This does not change the function.
|
2007-12-17 22:33:23 +00:00
|
|
|
if (NewFn)
|
|
|
|
F = NewFn;
|
2009-02-05 01:49:45 +00:00
|
|
|
if (unsigned id = F->getIntrinsicID())
|
2008-09-25 21:00:45 +00:00
|
|
|
F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
|
2007-12-03 20:06:50 +00:00
|
|
|
return Upgraded;
|
|
|
|
}
|
|
|
|
|
2010-09-10 18:51:56 +00:00
|
|
|
bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
|
2011-06-18 06:05:24 +00:00
|
|
|
// Nothing to do yet.
|
2010-09-10 18:51:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-04 01:51:18 +00: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 21:58:54 +00:00
|
|
|
LLVMContext &C = CI->getContext();
|
2010-06-23 08:45:32 +00:00
|
|
|
ImmutableCallSite CS(CI);
|
|
|
|
|
2007-08-04 01:51:18 +00:00
|
|
|
assert(F && "CallInst has no function associated with it.");
|
2007-12-17 22:33:23 +00:00
|
|
|
|
|
|
|
if (!NewFn) {
|
2011-06-18 06:05:24 +00: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 00:36:11 +00:00
|
|
|
// Convert to a native, unaligned load.
|
|
|
|
const Type *VecTy = CI->getType();
|
|
|
|
const Type *IntTy = IntegerType::get(C, 128);
|
|
|
|
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 21:11:17 +00: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 00:36:11 +00:00
|
|
|
// Remove intrinsic.
|
|
|
|
CI->eraseFromParent();
|
2007-12-18 01:04:25 +00:00
|
|
|
} else {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unknown function for CallInst upgrade.");
|
2007-12-17 22:33:23 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-06 20:25:17 +00:00
|
|
|
switch (NewFn->getIntrinsicID()) {
|
2011-06-14 04:58:37 +00:00
|
|
|
case Intrinsic::prefetch: {
|
|
|
|
IRBuilder<> Builder(C);
|
|
|
|
Builder.SetInsertPoint(CI->getParent(), CI);
|
|
|
|
const llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
|
|
|
|
|
|
|
|
// Add the extra "data cache" argument
|
|
|
|
Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
|
|
|
|
CI->getArgOperand(2),
|
|
|
|
llvm::ConstantInt::get(I32Ty, 1) };
|
|
|
|
CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+4,
|
|
|
|
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 01:51:18 +00: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 06:05:24 +00:00
|
|
|
Function *NewFn;
|
2007-12-17 22:33:23 +00:00
|
|
|
if (UpgradeIntrinsicFunction(F, NewFn)) {
|
2007-08-04 01:51:18 +00: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 06:05:24 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*UI++))
|
2007-08-04 01:51:18 +00:00
|
|
|
UpgradeIntrinsicCall(CI, NewFn);
|
|
|
|
}
|
|
|
|
// Remove old function, no longer used, from the module.
|
|
|
|
F->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-28 23:24:31 +00:00
|
|
|
|
2010-01-05 21:13:46 +00: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-28 23:24:31 +00:00
|
|
|
void llvm::CheckDebugInfoIntrinsics(Module *M) {
|
|
|
|
if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
|
2011-06-18 06:05:24 +00:00
|
|
|
while (!FuncStart->use_empty())
|
|
|
|
cast<CallInst>(FuncStart->use_back())->eraseFromParent();
|
2010-01-05 01:10:40 +00:00
|
|
|
FuncStart->eraseFromParent();
|
2009-08-28 23:24:31 +00:00
|
|
|
}
|
2010-01-05 01:10:40 +00:00
|
|
|
|
2009-08-28 23:24:31 +00:00
|
|
|
if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
|
2011-06-18 06:05:24 +00:00
|
|
|
while (!StopPoint->use_empty())
|
|
|
|
cast<CallInst>(StopPoint->use_back())->eraseFromParent();
|
2010-01-05 01:10:40 +00:00
|
|
|
StopPoint->eraseFromParent();
|
2009-08-28 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
|
2011-06-18 06:05:24 +00:00
|
|
|
while (!RegionStart->use_empty())
|
|
|
|
cast<CallInst>(RegionStart->use_back())->eraseFromParent();
|
2010-01-05 01:10:40 +00:00
|
|
|
RegionStart->eraseFromParent();
|
2009-08-28 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
|
2011-06-18 06:05:24 +00:00
|
|
|
while (!RegionEnd->use_empty())
|
|
|
|
cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
|
2010-01-05 01:10:40 +00:00
|
|
|
RegionEnd->eraseFromParent();
|
2009-08-28 23:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
|
|
|
|
if (!Declare->use_empty()) {
|
|
|
|
DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
|
2010-06-22 20:40:38 +00:00
|
|
|
if (!isa<MDNode>(DDI->getArgOperand(0)) ||
|
|
|
|
!isa<MDNode>(DDI->getArgOperand(1))) {
|
2009-08-28 23:24:31 +00:00
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
|
|
|
CI->eraseFromParent();
|
|
|
|
}
|
|
|
|
Declare->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|