LLVM 20.0.0git
Atomic.h
Go to the documentation of this file.
1//===--- Atomic.h - Codegen of atomic operations ------------------------===//
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#ifndef LLVM_FRONTEND_ATOMIC_ATOMIC_H
10#define LLVM_FRONTEND_ATOMIC_ATOMIC_H
11
12#include "llvm/IR/IRBuilder.h"
13#include "llvm/IR/Module.h"
14
15namespace llvm {
17protected:
25
26public:
29 bool UseLibcall)
33
34 virtual ~AtomicInfo() = default;
35
39 bool shouldUseLibcall() const { return UseLibcall; }
40 Type *getAtomicTy() const { return Ty; }
41
42 virtual Value *getAtomicPointer() const = 0;
43 virtual void decorateWithTBAA(Instruction *I) = 0;
44 virtual AllocaInst *CreateAlloca(Type *Ty, const Twine &Name) const = 0;
45
46 /*
47 * Is the atomic size larger than the underlying value type?
48 * Note that the absence of padding does not mean that atomic
49 * objects are completely interchangeable with non-atomic
50 * objects: we might have promoted the alignment of a type
51 * without making it bigger.
52 */
53 bool hasPadding() const { return (ValueSizeInBits != AtomicSizeInBits); }
54
56
57 bool shouldCastToInt(Type *ValTy, bool CmpXchg);
58
59 Value *EmitAtomicLoadOp(AtomicOrdering AO, bool IsVolatile,
60 bool CmpXchg = false);
61
62 CallInst *EmitAtomicLibcall(StringRef fnName, Type *ResultType,
64
67 // TODO: Get from llvm::TargetMachine / clang::TargetInfo
68 // if clang shares this codegen in future
69 constexpr uint16_t SizeTBits = 64;
70 constexpr uint16_t BitsPerByte = 8;
71 return ConstantInt::get(IntegerType::get(ctx, SizeTBits),
72 AtomicSizeInBits / BitsPerByte);
73 }
74
75 std::pair<Value *, Value *>
76 EmitAtomicCompareExchangeLibcall(Value *ExpectedVal, Value *DesiredVal,
78 AtomicOrdering Failure);
79
81 return addr; // opaque pointer
82 }
83
86 }
87
88 std::pair<Value *, Value *>
89 EmitAtomicCompareExchangeOp(Value *ExpectedVal, Value *DesiredVal,
91 bool IsVolatile = false, bool IsWeak = false);
92
93 std::pair<Value *, Value *>
94 EmitAtomicCompareExchange(Value *ExpectedVal, Value *DesiredVal,
96 bool IsVolatile, bool IsWeak);
97
98 std::pair<LoadInst *, AllocaInst *> EmitAtomicLoadLibcall(AtomicOrdering AO);
99};
100} // end namespace llvm
101
102#endif /* LLVM_FRONTEND_ATOMIC_ATOMIC_H */
#define Success
std::string Name
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
an instruction to allocate memory on the stack
Definition: Instructions.h:63
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Align AtomicAlign
Definition: Atomic.h:22
uint64_t getValueSizeInBits() const
Definition: Atomic.h:38
bool UseLibcall
Definition: Atomic.h:24
virtual ~AtomicInfo()=default
IRBuilderBase * Builder
Definition: Atomic.h:18
std::pair< Value *, Value * > EmitAtomicCompareExchange(Value *ExpectedVal, Value *DesiredVal, AtomicOrdering Success, AtomicOrdering Failure, bool IsVolatile, bool IsWeak)
Definition: Atomic.cpp:144
virtual AllocaInst * CreateAlloca(Type *Ty, const Twine &Name) const =0
std::pair< LoadInst *, AllocaInst * > EmitAtomicLoadLibcall(AtomicOrdering AO)
Definition: Atomic.cpp:107
std::pair< Value *, Value * > EmitAtomicCompareExchangeOp(Value *ExpectedVal, Value *DesiredVal, AtomicOrdering Success, AtomicOrdering Failure, bool IsVolatile=false, bool IsWeak=false)
Definition: Atomic.cpp:89
Type * getAtomicTy() const
Definition: Atomic.h:40
uint64_t getAtomicSizeInBits() const
Definition: Atomic.h:37
Value * EmitAtomicLoadOp(AtomicOrdering AO, bool IsVolatile, bool CmpXchg=false)
Definition: Atomic.cpp:22
virtual Value * getAtomicPointer() const =0
Value * castToAtomicIntPointer(Value *addr) const
Definition: Atomic.h:80
std::pair< Value *, Value * > EmitAtomicCompareExchangeLibcall(Value *ExpectedVal, Value *DesiredVal, AtomicOrdering Success, AtomicOrdering Failure)
Definition: Atomic.cpp:57
bool shouldCastToInt(Type *ValTy, bool CmpXchg)
Definition: Atomic.cpp:16
AtomicInfo(IRBuilderBase *Builder, Type *Ty, uint64_t AtomicSizeInBits, uint64_t ValueSizeInBits, Align AtomicAlign, Align ValueAlign, bool UseLibcall)
Definition: Atomic.h:27
bool hasPadding() const
Definition: Atomic.h:53
uint64_t AtomicSizeInBits
Definition: Atomic.h:20
uint64_t ValueSizeInBits
Definition: Atomic.h:21
LLVMContext & getLLVMContext() const
Definition: Atomic.h:55
Value * getAtomicAddressAsAtomicIntPointer() const
Definition: Atomic.h:84
Align ValueAlign
Definition: Atomic.h:23
virtual void decorateWithTBAA(Instruction *I)=0
CallInst * EmitAtomicLibcall(StringRef fnName, Type *ResultType, ArrayRef< Value * > Args)
Definition: Atomic.cpp:37
Value * getAtomicSizeValue() const
Definition: Atomic.h:65
bool shouldUseLibcall() const
Definition: Atomic.h:39
Type * Ty
Definition: Atomic.h:19
Align getAtomicAlignment() const
Definition: Atomic.h:36
This class represents a function call, abstracting a target machine's calling convention.
Common base class shared among various IRBuilders.
Definition: IRBuilder.h:91
LLVMContext & getContext() const
Definition: IRBuilder.h:173
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:311
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
AtomicOrdering
Atomic ordering for LLVM's memory model.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39