2010-08-04 00:19:16 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2010-08-04 00:19:16 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "loweratomic"
|
|
|
|
|
2011-08-19 09:42:18 +08:00
|
|
|
static bool LowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI) {
|
2011-07-29 11:05:32 +08:00
|
|
|
IRBuilder<> Builder(CXI->getParent(), CXI);
|
|
|
|
Value *Ptr = CXI->getPointerOperand();
|
|
|
|
Value *Cmp = CXI->getCompareOperand();
|
|
|
|
Value *Val = CXI->getNewValOperand();
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2011-07-29 11:05:32 +08:00
|
|
|
LoadInst *Orig = Builder.CreateLoad(Ptr);
|
|
|
|
Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
|
|
|
|
Value *Res = Builder.CreateSelect(Equal, Val, Orig);
|
|
|
|
Builder.CreateStore(Res, Ptr);
|
2012-07-24 18:51:42 +08:00
|
|
|
|
IR: add "cmpxchg weak" variant to support permitted failure.
This commit adds a weak variant of the cmpxchg operation, as described
in C++11. A cmpxchg instruction with this modifier is permitted to
fail to store, even if the comparison indicated it should.
As a result, cmpxchg instructions must return a flag indicating
success in addition to their original iN value loaded. Thus, for
uniformity *all* cmpxchg instructions now return "{ iN, i1 }". The
second flag is 1 when the store succeeded.
At the DAG level, a new ATOMIC_CMP_SWAP_WITH_SUCCESS node has been
added as the natural representation for the new cmpxchg instructions.
It is a strong cmpxchg.
By default this gets Expanded to the existing ATOMIC_CMP_SWAP during
Legalization, so existing backends should see no change in behaviour.
If they wish to deal with the enhanced node instead, they can call
setOperationAction on it. Beware: as a node with 2 results, it cannot
be selected from TableGen.
Currently, no use is made of the extra information provided in this
patch. Test updates are almost entirely adapting the input IR to the
new scheme.
Summary for out of tree users:
------------------------------
+ Legacy Bitcode files are upgraded during read.
+ Legacy assembly IR files will be invalid.
+ Front-ends must adapt to different type for "cmpxchg".
+ Backends should be unaffected by default.
llvm-svn: 210903
2014-06-13 22:24:07 +08:00
|
|
|
Res = Builder.CreateInsertValue(UndefValue::get(CXI->getType()), Orig, 0);
|
|
|
|
Res = Builder.CreateInsertValue(Res, Equal, 1);
|
|
|
|
|
|
|
|
CXI->replaceAllUsesWith(Res);
|
2011-07-29 11:05:32 +08:00
|
|
|
CXI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-19 09:42:18 +08:00
|
|
|
static bool LowerAtomicRMWInst(AtomicRMWInst *RMWI) {
|
2011-07-29 11:05:32 +08:00
|
|
|
IRBuilder<> Builder(RMWI->getParent(), RMWI);
|
|
|
|
Value *Ptr = RMWI->getPointerOperand();
|
|
|
|
Value *Val = RMWI->getValOperand();
|
|
|
|
|
|
|
|
LoadInst *Orig = Builder.CreateLoad(Ptr);
|
2014-04-25 13:29:35 +08:00
|
|
|
Value *Res = nullptr;
|
2011-07-29 11:05:32 +08:00
|
|
|
|
|
|
|
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 09:08:30 +08:00
|
|
|
static bool LowerFenceInst(FenceInst *FI) {
|
|
|
|
FI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-08-10 07:02:53 +08:00
|
|
|
static bool LowerLoadInst(LoadInst *LI) {
|
|
|
|
LI->setAtomic(NotAtomic);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool LowerStoreInst(StoreInst *SI) {
|
|
|
|
SI->setAtomic(NotAtomic);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-09-06 04:10:47 +08:00
|
|
|
namespace {
|
|
|
|
struct LowerAtomic : public BasicBlockPass {
|
|
|
|
static char ID;
|
2010-10-20 01:21:58 +08:00
|
|
|
LowerAtomic() : BasicBlockPass(ID) {
|
|
|
|
initializeLowerAtomicPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnBasicBlock(BasicBlock &BB) override {
|
2014-02-06 08:07:05 +08:00
|
|
|
if (skipOptnoneFunction(BB))
|
|
|
|
return false;
|
2010-09-06 04:10:47 +08:00
|
|
|
bool Changed = false;
|
2011-07-27 09:08:30 +08:00
|
|
|
for (BasicBlock::iterator DI = BB.begin(), DE = BB.end(); DI != DE; ) {
|
|
|
|
Instruction *Inst = DI++;
|
2011-10-07 07:20:49 +08:00
|
|
|
if (FenceInst *FI = dyn_cast<FenceInst>(Inst))
|
2011-07-27 09:08:30 +08:00
|
|
|
Changed |= LowerFenceInst(FI);
|
2011-07-29 11:05:32 +08: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 07:02:53 +08: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 09:08:30 +08:00
|
|
|
}
|
2010-09-06 04:10:47 +08:00
|
|
|
return Changed;
|
2010-08-04 00:19:16 +08:00
|
|
|
}
|
2010-09-06 04:10:47 +08:00
|
|
|
};
|
2010-08-04 00:19:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char LowerAtomic::ID = 0;
|
2010-08-24 01:52:01 +08:00
|
|
|
INITIALIZE_PASS(LowerAtomic, "loweratomic",
|
|
|
|
"Lower atomic intrinsics to non-atomic form",
|
2010-10-08 06:25:06 +08:00
|
|
|
false, false)
|
2010-08-04 00:19:16 +08:00
|
|
|
|
|
|
|
Pass *llvm::createLowerAtomicPass() { return new LowerAtomic(); }
|