2010-08-03 18:19:16 +02:00
|
|
|
//===- LowerAtomic.cpp - Lower atomic intrinsics --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass lowers atomic intrinsics to non-atomic form for use in a known
|
|
|
|
// non-preemptible environment.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "loweratomic"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Function.h"
|
2010-09-05 22:10:47 +02:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2010-08-03 18:19:16 +02:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/IRBuilder.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-09-05 22:10:47 +02:00
|
|
|
static bool LowerAtomicIntrinsic(IntrinsicInst *II) {
|
|
|
|
IRBuilder<> Builder(II->getParent(), II);
|
|
|
|
unsigned IID = II->getIntrinsicID();
|
2010-08-03 18:19:16 +02:00
|
|
|
switch (IID) {
|
|
|
|
case Intrinsic::memory_barrier:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Intrinsic::atomic_load_add:
|
|
|
|
case Intrinsic::atomic_load_sub:
|
|
|
|
case Intrinsic::atomic_load_and:
|
|
|
|
case Intrinsic::atomic_load_nand:
|
|
|
|
case Intrinsic::atomic_load_or:
|
|
|
|
case Intrinsic::atomic_load_xor:
|
|
|
|
case Intrinsic::atomic_load_max:
|
|
|
|
case Intrinsic::atomic_load_min:
|
|
|
|
case Intrinsic::atomic_load_umax:
|
|
|
|
case Intrinsic::atomic_load_umin: {
|
2010-09-05 22:13:07 +02:00
|
|
|
Value *Ptr = II->getArgOperand(0), *Delta = II->getArgOperand(1);
|
2010-08-03 18:19:16 +02:00
|
|
|
|
|
|
|
LoadInst *Orig = Builder.CreateLoad(Ptr);
|
2010-08-06 09:43:46 +02:00
|
|
|
Value *Res = NULL;
|
2010-08-03 18:19:16 +02:00
|
|
|
switch (IID) {
|
2010-09-05 22:13:07 +02:00
|
|
|
default: assert(0 && "Unrecognized atomic modify operation");
|
|
|
|
case Intrinsic::atomic_load_add:
|
|
|
|
Res = Builder.CreateAdd(Orig, Delta);
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_sub:
|
|
|
|
Res = Builder.CreateSub(Orig, Delta);
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_and:
|
|
|
|
Res = Builder.CreateAnd(Orig, Delta);
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_nand:
|
|
|
|
Res = Builder.CreateNot(Builder.CreateAnd(Orig, Delta));
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_or:
|
|
|
|
Res = Builder.CreateOr(Orig, Delta);
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_xor:
|
|
|
|
Res = Builder.CreateXor(Orig, Delta);
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_max:
|
|
|
|
Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Delta),
|
|
|
|
Delta, Orig);
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_min:
|
|
|
|
Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Delta),
|
|
|
|
Orig, Delta);
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_umax:
|
|
|
|
Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Delta),
|
|
|
|
Delta, Orig);
|
|
|
|
break;
|
|
|
|
case Intrinsic::atomic_load_umin:
|
|
|
|
Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Delta),
|
|
|
|
Orig, Delta);
|
|
|
|
break;
|
2010-08-03 18:19:16 +02:00
|
|
|
}
|
|
|
|
Builder.CreateStore(Res, Ptr);
|
|
|
|
|
2010-09-05 22:10:47 +02:00
|
|
|
II->replaceAllUsesWith(Orig);
|
2010-08-03 18:19:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Intrinsic::atomic_swap: {
|
2010-09-05 22:13:07 +02:00
|
|
|
Value *Ptr = II->getArgOperand(0), *Val = II->getArgOperand(1);
|
2010-08-03 18:19:16 +02:00
|
|
|
LoadInst *Orig = Builder.CreateLoad(Ptr);
|
|
|
|
Builder.CreateStore(Val, Ptr);
|
2010-09-05 22:10:47 +02:00
|
|
|
II->replaceAllUsesWith(Orig);
|
2010-08-03 18:19:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Intrinsic::atomic_cmp_swap: {
|
2010-09-05 22:13:07 +02:00
|
|
|
Value *Ptr = II->getArgOperand(0), *Cmp = II->getArgOperand(1);
|
2010-09-05 22:10:47 +02:00
|
|
|
Value *Val = II->getArgOperand(2);
|
2010-08-03 18:19:16 +02:00
|
|
|
|
|
|
|
LoadInst *Orig = Builder.CreateLoad(Ptr);
|
|
|
|
Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
|
|
|
|
Value *Res = Builder.CreateSelect(Equal, Val, Orig);
|
|
|
|
Builder.CreateStore(Res, Ptr);
|
2010-09-05 22:10:47 +02:00
|
|
|
II->replaceAllUsesWith(Orig);
|
2010-08-03 18:19:16 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-05 22:10:47 +02:00
|
|
|
assert(II->use_empty() &&
|
2010-08-03 18:19:16 +02:00
|
|
|
"Lowering should have eliminated any uses of the intrinsic call!");
|
2010-09-05 22:10:47 +02:00
|
|
|
II->eraseFromParent();
|
2010-08-03 18:19:16 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-29 05:05:32 +02:00
|
|
|
bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI) {
|
|
|
|
IRBuilder<> Builder(CXI->getParent(), CXI);
|
|
|
|
Value *Ptr = CXI->getPointerOperand();
|
|
|
|
Value *Cmp = CXI->getCompareOperand();
|
|
|
|
Value *Val = CXI->getNewValOperand();
|
|
|
|
|
|
|
|
LoadInst *Orig = Builder.CreateLoad(Ptr);
|
|
|
|
Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
|
|
|
|
Value *Res = Builder.CreateSelect(Equal, Val, Orig);
|
|
|
|
Builder.CreateStore(Res, Ptr);
|
|
|
|
|
|
|
|
CXI->replaceAllUsesWith(Orig);
|
|
|
|
CXI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LowerAtomicRMWInst(AtomicRMWInst *RMWI) {
|
|
|
|
IRBuilder<> Builder(RMWI->getParent(), RMWI);
|
|
|
|
Value *Ptr = RMWI->getPointerOperand();
|
|
|
|
Value *Val = RMWI->getValOperand();
|
|
|
|
|
|
|
|
LoadInst *Orig = Builder.CreateLoad(Ptr);
|
|
|
|
Value *Res = NULL;
|
|
|
|
|
|
|
|
switch (RMWI->getOperation()) {
|
|
|
|
default: llvm_unreachable("Unexpected RMW operation");
|
|
|
|
case AtomicRMWInst::Xchg:
|
|
|
|
Res = Val;
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::Add:
|
|
|
|
Res = Builder.CreateAdd(Orig, Val);
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::Sub:
|
|
|
|
Res = Builder.CreateSub(Orig, Val);
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::And:
|
|
|
|
Res = Builder.CreateAnd(Orig, Val);
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::Nand:
|
|
|
|
Res = Builder.CreateNot(Builder.CreateAnd(Orig, Val));
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::Or:
|
|
|
|
Res = Builder.CreateOr(Orig, Val);
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::Xor:
|
|
|
|
Res = Builder.CreateXor(Orig, Val);
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::Max:
|
|
|
|
Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
|
|
|
|
Val, Orig);
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::Min:
|
|
|
|
Res = Builder.CreateSelect(Builder.CreateICmpSLT(Orig, Val),
|
|
|
|
Orig, Val);
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::UMax:
|
|
|
|
Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
|
|
|
|
Val, Orig);
|
|
|
|
break;
|
|
|
|
case AtomicRMWInst::UMin:
|
|
|
|
Res = Builder.CreateSelect(Builder.CreateICmpULT(Orig, Val),
|
|
|
|
Orig, Val);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Builder.CreateStore(Res, Ptr);
|
|
|
|
RMWI->replaceAllUsesWith(Orig);
|
|
|
|
RMWI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-07-27 03:08:30 +02:00
|
|
|
static bool LowerFenceInst(FenceInst *FI) {
|
|
|
|
FI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-10 01:02:53 +02:00
|
|
|
static bool LowerLoadInst(LoadInst *LI) {
|
|
|
|
LI->setAtomic(NotAtomic);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool LowerStoreInst(StoreInst *SI) {
|
|
|
|
SI->setAtomic(NotAtomic);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-05 22:10:47 +02:00
|
|
|
namespace {
|
|
|
|
struct LowerAtomic : public BasicBlockPass {
|
|
|
|
static char ID;
|
2010-10-19 19:21:58 +02:00
|
|
|
LowerAtomic() : BasicBlockPass(ID) {
|
|
|
|
initializeLowerAtomicPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-09-05 22:10:47 +02:00
|
|
|
bool runOnBasicBlock(BasicBlock &BB) {
|
|
|
|
bool Changed = false;
|
2011-07-27 03:08:30 +02:00
|
|
|
for (BasicBlock::iterator DI = BB.begin(), DE = BB.end(); DI != DE; ) {
|
|
|
|
Instruction *Inst = DI++;
|
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
|
2010-09-05 22:10:47 +02:00
|
|
|
Changed |= LowerAtomicIntrinsic(II);
|
2011-07-28 03:20:19 +02:00
|
|
|
else if (FenceInst *FI = dyn_cast<FenceInst>(Inst))
|
2011-07-27 03:08:30 +02:00
|
|
|
Changed |= LowerFenceInst(FI);
|
2011-07-29 05:05:32 +02:00
|
|
|
else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(Inst))
|
|
|
|
Changed |= LowerAtomicCmpXchgInst(CXI);
|
|
|
|
else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(Inst))
|
|
|
|
Changed |= LowerAtomicRMWInst(RMWI);
|
2011-08-10 01:02:53 +02:00
|
|
|
else if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) {
|
|
|
|
if (LI->isAtomic())
|
|
|
|
LowerLoadInst(LI);
|
|
|
|
} else if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
|
|
|
|
if (SI->isAtomic())
|
|
|
|
LowerStoreInst(SI);
|
|
|
|
}
|
2011-07-27 03:08:30 +02:00
|
|
|
}
|
2010-09-05 22:10:47 +02:00
|
|
|
return Changed;
|
2010-08-03 18:19:16 +02:00
|
|
|
}
|
2010-09-05 22:10:47 +02:00
|
|
|
};
|
2010-08-03 18:19:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char LowerAtomic::ID = 0;
|
2010-08-23 19:52:01 +02:00
|
|
|
INITIALIZE_PASS(LowerAtomic, "loweratomic",
|
|
|
|
"Lower atomic intrinsics to non-atomic form",
|
2010-10-08 00:25:06 +02:00
|
|
|
false, false)
|
2010-08-03 18:19:16 +02:00
|
|
|
|
|
|
|
Pass *llvm::createLowerAtomicPass() { return new LowerAtomic(); }
|