LLVM 20.0.0git
LowerAtomic.cpp
Go to the documentation of this file.
1//===- LowerAtomic.cpp - Lower atomic intrinsics --------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass lowers atomic intrinsics to non-atomic form for use in a known
10// non-preemptible environment.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/IR/Function.h"
16#include "llvm/IR/IRBuilder.h"
17
18using namespace llvm;
19
20#define DEBUG_TYPE "loweratomic"
21
23 IRBuilder<> Builder(CXI);
24 Value *Ptr = CXI->getPointerOperand();
25 Value *Cmp = CXI->getCompareOperand();
26 Value *Val = CXI->getNewValOperand();
27
28 LoadInst *Orig =
29 Builder.CreateAlignedLoad(Val->getType(), Ptr, CXI->getAlign());
30 Value *Equal = Builder.CreateICmpEQ(Orig, Cmp);
31 Value *Res = Builder.CreateSelect(Equal, Val, Orig);
32 Builder.CreateAlignedStore(Res, Ptr, CXI->getAlign());
33
34 Res = Builder.CreateInsertValue(PoisonValue::get(CXI->getType()), Orig, 0);
35 Res = Builder.CreateInsertValue(Res, Equal, 1);
36
37 CXI->replaceAllUsesWith(Res);
38 CXI->eraseFromParent();
39 return true;
40}
41
43 IRBuilderBase &Builder, Value *Loaded,
44 Value *Val) {
45 Value *NewVal;
46 switch (Op) {
48 return Val;
50 return Builder.CreateAdd(Loaded, Val, "new");
52 return Builder.CreateSub(Loaded, Val, "new");
54 return Builder.CreateAnd(Loaded, Val, "new");
56 return Builder.CreateNot(Builder.CreateAnd(Loaded, Val), "new");
58 return Builder.CreateOr(Loaded, Val, "new");
60 return Builder.CreateXor(Loaded, Val, "new");
62 NewVal = Builder.CreateICmpSGT(Loaded, Val);
63 return Builder.CreateSelect(NewVal, Loaded, Val, "new");
65 NewVal = Builder.CreateICmpSLE(Loaded, Val);
66 return Builder.CreateSelect(NewVal, Loaded, Val, "new");
68 NewVal = Builder.CreateICmpUGT(Loaded, Val);
69 return Builder.CreateSelect(NewVal, Loaded, Val, "new");
71 NewVal = Builder.CreateICmpULE(Loaded, Val);
72 return Builder.CreateSelect(NewVal, Loaded, Val, "new");
74 return Builder.CreateFAdd(Loaded, Val, "new");
76 return Builder.CreateFSub(Loaded, Val, "new");
78 return Builder.CreateMaxNum(Loaded, Val);
80 return Builder.CreateMinNum(Loaded, Val);
82 Constant *One = ConstantInt::get(Loaded->getType(), 1);
83 Value *Inc = Builder.CreateAdd(Loaded, One);
84 Value *Cmp = Builder.CreateICmpUGE(Loaded, Val);
85 Constant *Zero = ConstantInt::get(Loaded->getType(), 0);
86 return Builder.CreateSelect(Cmp, Zero, Inc, "new");
87 }
89 Constant *Zero = ConstantInt::get(Loaded->getType(), 0);
90 Constant *One = ConstantInt::get(Loaded->getType(), 1);
91
92 Value *Dec = Builder.CreateSub(Loaded, One);
93 Value *CmpEq0 = Builder.CreateICmpEQ(Loaded, Zero);
94 Value *CmpOldGtVal = Builder.CreateICmpUGT(Loaded, Val);
95 Value *Or = Builder.CreateOr(CmpEq0, CmpOldGtVal);
96 return Builder.CreateSelect(Or, Val, Dec, "new");
97 }
98 default:
99 llvm_unreachable("Unknown atomic op");
100 }
101}
102
104 IRBuilder<> Builder(RMWI);
105 Builder.setIsFPConstrained(
106 RMWI->getFunction()->hasFnAttribute(Attribute::StrictFP));
107
108 Value *Ptr = RMWI->getPointerOperand();
109 Value *Val = RMWI->getValOperand();
110
111 LoadInst *Orig = Builder.CreateLoad(Val->getType(), Ptr);
112 Value *Res = buildAtomicRMWValue(RMWI->getOperation(), Builder, Orig, Val);
113 Builder.CreateStore(Res, Ptr);
114 RMWI->replaceAllUsesWith(Orig);
115 RMWI->eraseFromParent();
116 return true;
117}
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:495
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instructions.h:536
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:696
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:708
@ Add
*p = old + v
Definition: Instructions.h:712
@ FAdd
*p = old + v
Definition: Instructions.h:733
@ Min
*p = old <signed v ? old : v
Definition: Instructions.h:726
@ Or
*p = old | v
Definition: Instructions.h:720
@ Sub
*p = old - v
Definition: Instructions.h:714
@ And
*p = old & v
Definition: Instructions.h:716
@ Xor
*p = old ^ v
Definition: Instructions.h:722
@ FSub
*p = old - v
Definition: Instructions.h:736
@ UIncWrap
Increment one up to a maximum value.
Definition: Instructions.h:748
@ Max
*p = old >signed v ? old : v
Definition: Instructions.h:724
@ UMin
*p = old <unsigned v ? old : v
Definition: Instructions.h:730
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
Definition: Instructions.h:744
@ UMax
*p = old >unsigned v ? old : v
Definition: Instructions.h:728
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
Definition: Instructions.h:740
@ UDecWrap
Decrement one until a minimum value or zero.
Definition: Instructions.h:752
@ Nand
*p = ~(old & v)
Definition: Instructions.h:718
Value * getPointerOperand()
Definition: Instructions.h:852
BinOp getOperation() const
Definition: Instructions.h:787
Value * getValOperand()
Definition: Instructions.h:856
This is an important base class in LLVM.
Definition: Constant.h:42
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:743
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:91
Value * CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name="")
Create call to the maxnum intrinsic.
Definition: IRBuilder.h:999
Value * CreateFSub(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1577
Value * CreateInsertValue(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &Name="")
Definition: IRBuilder.h:2543
Value * CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2285
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition: IRBuilder.h:1824
Value * CreateFAdd(Value *L, Value *R, const Twine &Name="", MDNode *FPMD=nullptr)
Definition: IRBuilder.h:1550
Value * CreateMinNum(Value *LHS, Value *RHS, const Twine &Name="")
Create call to the minnum intrinsic.
Definition: IRBuilder.h:989
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1091
Value * CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2297
Value * CreateNot(Value *V, const Twine &Name="")
Definition: IRBuilder.h:1766
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2261
void setIsFPConstrained(bool IsCon)
Enable/Disable use of constrained floating point math.
Definition: IRBuilder.h:314
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1361
Value * CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2269
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:1807
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1492
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1820
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1344
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1514
Value * CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2273
StoreInst * CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, bool isVolatile=false)
Definition: IRBuilder.h:1843
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1536
Value * CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2281
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2686
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:92
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:70
An instruction for reading from memory.
Definition: Instructions.h:174
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1852
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:42
@ 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.