LLVM 19.0.0git
Go to the documentation of this file.
1//===- LowerAtomic.cpp - Lower atomic intrinsics --------------------------===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9// This pass lowers atomic intrinsics to non-atomic form for use in a known
10// non-preemptible environment.
15#include "llvm/IR/Function.h"
16#include "llvm/IR/IRBuilder.h"
18using namespace llvm;
20#define DEBUG_TYPE "loweratomic"
23 IRBuilder<> Builder(CXI);
24 Value *Ptr = CXI->getPointerOperand();
25 Value *Cmp = CXI->getCompareOperand();
26 Value *Val = CXI->getNewValOperand();
28 LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
29 Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
30 Value *Res = Builder.CreateSelect(Equal, Val, Orig);
31 Builder.CreateStore(Res, Ptr);
33 Res = Builder.CreateInsertValue(PoisonValue::get(CXI->getType()), Orig, 0);
34 Res = Builder.CreateInsertValue(Res, Equal, 1);
36 CXI->replaceAllUsesWith(Res);
37 CXI->eraseFromParent();
38 return true;
42 IRBuilderBase &Builder, Value *Loaded,
43 Value *Val) {
44 Value *NewVal;
45 switch (Op) {
47 return Val;
49 return Builder.CreateAdd(Loaded, Val, "new");
51 return Builder.CreateSub(Loaded, Val, "new");
53 return Builder.CreateAnd(Loaded, Val, "new");
55 return Builder.CreateNot(Builder.CreateAnd(Loaded, Val), "new");
57 return Builder.CreateOr(Loaded, Val, "new");
59 return Builder.CreateXor(Loaded, Val, "new");
61 NewVal = Builder.CreateICmpSGT(Loaded, Val);
62 return Builder.CreateSelect(NewVal, Loaded, Val, "new");
64 NewVal = Builder.CreateICmpSLE(Loaded, Val);
65 return Builder.CreateSelect(NewVal, Loaded, Val, "new");
67 NewVal = Builder.CreateICmpUGT(Loaded, Val);
68 return Builder.CreateSelect(NewVal, Loaded, Val, "new");
70 NewVal = Builder.CreateICmpULE(Loaded, Val);
71 return Builder.CreateSelect(NewVal, Loaded, Val, "new");
73 return Builder.CreateFAdd(Loaded, Val, "new");
75 return Builder.CreateFSub(Loaded, Val, "new");
77 return Builder.CreateMaxNum(Loaded, Val);
79 return Builder.CreateMinNum(Loaded, Val);
81 Constant *One = ConstantInt::get(Loaded->getType(), 1);
82 Value *Inc = Builder.CreateAdd(Loaded, One);
83 Value *Cmp = Builder.CreateICmpUGE(Loaded, Val);
84 Constant *Zero = ConstantInt::get(Loaded->getType(), 0);
85 return Builder.CreateSelect(Cmp, Zero, Inc, "new");
86 }
88 Constant *Zero = ConstantInt::get(Loaded->getType(), 0);
89 Constant *One = ConstantInt::get(Loaded->getType(), 1);
91 Value *Dec = Builder.CreateSub(Loaded, One);
92 Value *CmpEq0 = Builder.CreateICmpEQ(Loaded, Zero);
93 Value *CmpOldGtVal = Builder.CreateICmpUGT(Loaded, Val);
94 Value *Or = Builder.CreateOr(CmpEq0, CmpOldGtVal);
95 return Builder.CreateSelect(Or, Val, Dec, "new");
96 }
97 default:
98 llvm_unreachable("Unknown atomic op");
99 }
103 IRBuilder<> Builder(RMWI);
104 Builder.setIsFPConstrained(
105 RMWI->getFunction()->hasFnAttribute(Attribute::StrictFP));
107 Value *Ptr = RMWI->getPointerOperand();
108 Value *Val = RMWI->getValOperand();
110 LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
111 Value *Res = buildAtomicRMWValue(RMWI->getOperation(), Builder, Orig, Val);
112 Builder.CreateStore(Res, Ptr);
113 RMWI->replaceAllUsesWith(Orig);
114 RMWI->eraseFromParent();
115 return true;
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:539
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:748
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:760
@ Add
*p = old + v
Definition: Instructions.h:764
@ FAdd
*p = old + v
Definition: Instructions.h:785
@ Min
*p = old <signed v ? old : v
Definition: Instructions.h:778
@ Or
*p = old | v
Definition: Instructions.h:772
@ Sub
*p = old - v
Definition: Instructions.h:766
@ And
*p = old & v
Definition: Instructions.h:768
@ Xor
*p = old ^ v
Definition: Instructions.h:774
@ FSub
*p = old - v
Definition: Instructions.h:788
@ UIncWrap
Increment one up to a maximum value.
Definition: Instructions.h:800
@ Max
*p = old >signed v ? old : v
Definition: Instructions.h:776
@ UMin
*p = old <unsigned v ? old : v
Definition: Instructions.h:782
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
Definition: Instructions.h:796
@ UMax
*p = old >unsigned v ? old : v
Definition: Instructions.h:780
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
Definition: Instructions.h:792
@ UDecWrap
Decrement one until a minimum value or zero.
Definition: Instructions.h:804
@ Nand
*p = ~(old & v)
Definition: Instructions.h:770
Value * getPointerOperand()
Definition: Instructions.h:910
BinOp getOperation() const
Definition: Instructions.h:845
Value * getValOperand()
Definition: Instructions.h:914
This is an important base class in LLVM.
Definition: Constant.h:41
This class represents an Operation in the Expression.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:675
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:94
Value * CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name="")
Create call to the maxnum intrinsic.
Definition: IRBuilder.h:1002
Value * CreateFSub(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1560
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2523
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2265
Value * CreateFAdd(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1533
Value * CreateMinNum(Value *LHS, Value *RHS, const Twine &Name="")
Create call to the minnum intrinsic.
Definition: IRBuilder.h:992
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1110
Value * CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2277
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1749
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2241
void setIsFPConstrained(bool IsCon)
Enable/Disable use of constrained floating point math.
Definition: IRBuilder.h:317
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1344
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2249
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1790
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1475
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1803
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1327
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1497
Value * CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2253
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1519
Value * CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2261
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2666
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:86
An instruction for reading from memory.
Definition: Instructions.h:184
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1827
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Value * buildAtomicRMWValue(AtomicRMWInst::BinOp Op, IRBuilderBase &Builder, Value *Loaded, Value *Val)
Emit IR to implement the given atomicrmw operation on values in registers, returning the new value.
Definition: LowerAtomic.cpp:41
@ Or
Bitwise or logical OR of integers.
bool lowerAtomicCmpXchgInst(AtomicCmpXchgInst *CXI)
Convert the given Cmpxchg into primitive load and compare.
Definition: LowerAtomic.cpp:22
bool lowerAtomicRMWInst(AtomicRMWInst *RMWI)
Convert the given RMWI into primitive load and stores, assuming that doing so is legal.