1 //===- AtomicExpandUtils.h - Utilities for expanding atomic instructions --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CODEGEN_ATOMICEXPANDUTILS_H
11 #define LLVM_CODEGEN_ATOMICEXPANDUTILS_H
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/IR/IRBuilder.h"
15 #include "llvm/Support/AtomicOrdering.h"
22 /// Parameters (see the expansion example below):
23 /// (the builder, %addr, %loaded, %new_val, ordering,
24 /// /* OUT */ %success, /* OUT */ %new_loaded)
25 using CreateCmpXchgInstFun =
26 function_ref<void(IRBuilder<> &, Value *, Value *, Value *, AtomicOrdering,
29 /// \brief Expand an atomic RMW instruction into a loop utilizing
30 /// cmpxchg. You'll want to make sure your target machine likes cmpxchg
31 /// instructions in the first place and that there isn't another, better,
32 /// transformation available (for example AArch32/AArch64 have linked loads).
34 /// This is useful in passes which can't rewrite the more exotic RMW
35 /// instructions directly into a platform specific intrinsics (because, say,
36 /// those intrinsics don't exist). If such a pass is able to expand cmpxchg
37 /// instructions directly however, then, with this function, it could avoid two
38 /// extra module passes (avoiding passes by `-atomic-expand` and itself). A
39 /// specific example would be PNaCl's `RewriteAtomics` pass.
41 /// Given: atomicrmw some_op iN* %addr, iN %incr ordering
43 /// The standard expansion we produce is:
45 /// %init_loaded = load atomic iN* %addr
48 /// %loaded = phi iN [ %init_loaded, %entry ], [ %new_loaded, %loop ]
49 /// %new = some_op iN %loaded, %incr
50 /// ; This is what -atomic-expand will produce using this function on i686
52 /// %pair = cmpxchg iN* %addr, iN %loaded, iN %new_val
53 /// %new_loaded = extractvalue { iN, i1 } %pair, 0
54 /// %success = extractvalue { iN, i1 } %pair, 1
55 /// ; End callback produced IR
56 /// br i1 %success, label %atomicrmw.end, label %loop
60 /// Returns true if the containing function was modified.
61 bool expandAtomicRMWToCmpXchg(AtomicRMWInst *AI, CreateCmpXchgInstFun Factory);
63 } // end namespace llvm
65 #endif // LLVM_CODEGEN_ATOMICEXPANDUTILS_H