181ad6265SDimitry Andric //===- LowerAtomic.cpp - Lower atomic intrinsics --------------------------===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric //
981ad6265SDimitry Andric // This pass lowers atomic intrinsics to non-atomic form for use in a known
1081ad6265SDimitry Andric // non-preemptible environment.
1181ad6265SDimitry Andric //
1281ad6265SDimitry Andric //===----------------------------------------------------------------------===//
1381ad6265SDimitry Andric
1481ad6265SDimitry Andric #include "llvm/Transforms/Utils/LowerAtomic.h"
1581ad6265SDimitry Andric #include "llvm/IR/Function.h"
1681ad6265SDimitry Andric #include "llvm/IR/IRBuilder.h"
17*06c3fb27SDimitry Andric
1881ad6265SDimitry Andric using namespace llvm;
1981ad6265SDimitry Andric
2081ad6265SDimitry Andric #define DEBUG_TYPE "loweratomic"
2181ad6265SDimitry Andric
lowerAtomicCmpXchgInst(AtomicCmpXchgInst * CXI)2281ad6265SDimitry Andric bool llvm::lowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI) {
2381ad6265SDimitry Andric IRBuilder<> Builder(CXI);
2481ad6265SDimitry Andric Value *Ptr = CXI->getPointerOperand();
2581ad6265SDimitry Andric Value *Cmp = CXI->getCompareOperand();
2681ad6265SDimitry Andric Value *Val = CXI->getNewValOperand();
2781ad6265SDimitry Andric
2881ad6265SDimitry Andric LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
2981ad6265SDimitry Andric Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
3081ad6265SDimitry Andric Value *Res = Builder.CreateSelect(Equal, Val, Orig);
3181ad6265SDimitry Andric Builder.CreateStore(Res, Ptr);
3281ad6265SDimitry Andric
33fcaf7f86SDimitry Andric Res = Builder.CreateInsertValue(PoisonValue::get(CXI->getType()), Orig, 0);
3481ad6265SDimitry Andric Res = Builder.CreateInsertValue(Res, Equal, 1);
3581ad6265SDimitry Andric
3681ad6265SDimitry Andric CXI->replaceAllUsesWith(Res);
3781ad6265SDimitry Andric CXI->eraseFromParent();
3881ad6265SDimitry Andric return true;
3981ad6265SDimitry Andric }
4081ad6265SDimitry Andric
buildAtomicRMWValue(AtomicRMWInst::BinOp Op,IRBuilderBase & Builder,Value * Loaded,Value * Val)4181ad6265SDimitry Andric Value *llvm::buildAtomicRMWValue(AtomicRMWInst::BinOp Op,
4281ad6265SDimitry Andric IRBuilderBase &Builder, Value *Loaded,
43bdd1243dSDimitry Andric Value *Val) {
4481ad6265SDimitry Andric Value *NewVal;
4581ad6265SDimitry Andric switch (Op) {
4681ad6265SDimitry Andric case AtomicRMWInst::Xchg:
47bdd1243dSDimitry Andric return Val;
4881ad6265SDimitry Andric case AtomicRMWInst::Add:
49bdd1243dSDimitry Andric return Builder.CreateAdd(Loaded, Val, "new");
5081ad6265SDimitry Andric case AtomicRMWInst::Sub:
51bdd1243dSDimitry Andric return Builder.CreateSub(Loaded, Val, "new");
5281ad6265SDimitry Andric case AtomicRMWInst::And:
53bdd1243dSDimitry Andric return Builder.CreateAnd(Loaded, Val, "new");
5481ad6265SDimitry Andric case AtomicRMWInst::Nand:
55bdd1243dSDimitry Andric return Builder.CreateNot(Builder.CreateAnd(Loaded, Val), "new");
5681ad6265SDimitry Andric case AtomicRMWInst::Or:
57bdd1243dSDimitry Andric return Builder.CreateOr(Loaded, Val, "new");
5881ad6265SDimitry Andric case AtomicRMWInst::Xor:
59bdd1243dSDimitry Andric return Builder.CreateXor(Loaded, Val, "new");
6081ad6265SDimitry Andric case AtomicRMWInst::Max:
61bdd1243dSDimitry Andric NewVal = Builder.CreateICmpSGT(Loaded, Val);
62bdd1243dSDimitry Andric return Builder.CreateSelect(NewVal, Loaded, Val, "new");
6381ad6265SDimitry Andric case AtomicRMWInst::Min:
64bdd1243dSDimitry Andric NewVal = Builder.CreateICmpSLE(Loaded, Val);
65bdd1243dSDimitry Andric return Builder.CreateSelect(NewVal, Loaded, Val, "new");
6681ad6265SDimitry Andric case AtomicRMWInst::UMax:
67bdd1243dSDimitry Andric NewVal = Builder.CreateICmpUGT(Loaded, Val);
68bdd1243dSDimitry Andric return Builder.CreateSelect(NewVal, Loaded, Val, "new");
6981ad6265SDimitry Andric case AtomicRMWInst::UMin:
70bdd1243dSDimitry Andric NewVal = Builder.CreateICmpULE(Loaded, Val);
71bdd1243dSDimitry Andric return Builder.CreateSelect(NewVal, Loaded, Val, "new");
7281ad6265SDimitry Andric case AtomicRMWInst::FAdd:
73bdd1243dSDimitry Andric return Builder.CreateFAdd(Loaded, Val, "new");
7481ad6265SDimitry Andric case AtomicRMWInst::FSub:
75bdd1243dSDimitry Andric return Builder.CreateFSub(Loaded, Val, "new");
76753f127fSDimitry Andric case AtomicRMWInst::FMax:
77bdd1243dSDimitry Andric return Builder.CreateMaxNum(Loaded, Val);
78753f127fSDimitry Andric case AtomicRMWInst::FMin:
79bdd1243dSDimitry Andric return Builder.CreateMinNum(Loaded, Val);
80bdd1243dSDimitry Andric case AtomicRMWInst::UIncWrap: {
81bdd1243dSDimitry Andric Constant *One = ConstantInt::get(Loaded->getType(), 1);
82bdd1243dSDimitry Andric Value *Inc = Builder.CreateAdd(Loaded, One);
83bdd1243dSDimitry Andric Value *Cmp = Builder.CreateICmpUGE(Loaded, Val);
84bdd1243dSDimitry Andric Constant *Zero = ConstantInt::get(Loaded->getType(), 0);
85bdd1243dSDimitry Andric return Builder.CreateSelect(Cmp, Zero, Inc, "new");
86bdd1243dSDimitry Andric }
87bdd1243dSDimitry Andric case AtomicRMWInst::UDecWrap: {
88bdd1243dSDimitry Andric Constant *Zero = ConstantInt::get(Loaded->getType(), 0);
89bdd1243dSDimitry Andric Constant *One = ConstantInt::get(Loaded->getType(), 1);
90bdd1243dSDimitry Andric
91bdd1243dSDimitry Andric Value *Dec = Builder.CreateSub(Loaded, One);
92bdd1243dSDimitry Andric Value *CmpEq0 = Builder.CreateICmpEQ(Loaded, Zero);
93bdd1243dSDimitry Andric Value *CmpOldGtVal = Builder.CreateICmpUGT(Loaded, Val);
94bdd1243dSDimitry Andric Value *Or = Builder.CreateOr(CmpEq0, CmpOldGtVal);
95bdd1243dSDimitry Andric return Builder.CreateSelect(Or, Val, Dec, "new");
96bdd1243dSDimitry Andric }
9781ad6265SDimitry Andric default:
9881ad6265SDimitry Andric llvm_unreachable("Unknown atomic op");
9981ad6265SDimitry Andric }
10081ad6265SDimitry Andric }
10181ad6265SDimitry Andric
lowerAtomicRMWInst(AtomicRMWInst * RMWI)10281ad6265SDimitry Andric bool llvm::lowerAtomicRMWInst(AtomicRMWInst *RMWI) {
10381ad6265SDimitry Andric IRBuilder<> Builder(RMWI);
104*06c3fb27SDimitry Andric Builder.setIsFPConstrained(
105*06c3fb27SDimitry Andric RMWI->getFunction()->hasFnAttribute(Attribute::StrictFP));
106*06c3fb27SDimitry Andric
10781ad6265SDimitry Andric Value *Ptr = RMWI->getPointerOperand();
10881ad6265SDimitry Andric Value *Val = RMWI->getValOperand();
10981ad6265SDimitry Andric
11081ad6265SDimitry Andric LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
11181ad6265SDimitry Andric Value *Res = buildAtomicRMWValue(RMWI->getOperation(), Builder, Orig, Val);
11281ad6265SDimitry Andric Builder.CreateStore(Res, Ptr);
11381ad6265SDimitry Andric RMWI->replaceAllUsesWith(Orig);
11481ad6265SDimitry Andric RMWI->eraseFromParent();
11581ad6265SDimitry Andric return true;
11681ad6265SDimitry Andric }
117