AtomicExpandUtils.h revision 321369
1321369Sdim//===- AtomicExpandUtils.h - Utilities for expanding atomic instructions --===//
2292915Sdim//
3292915Sdim//                     The LLVM Compiler Infrastructure
4292915Sdim//
5292915Sdim// This file is distributed under the University of Illinois Open Source
6292915Sdim// License. See LICENSE.TXT for details.
7292915Sdim//
8292915Sdim//===----------------------------------------------------------------------===//
9292915Sdim
10321369Sdim#ifndef LLVM_CODEGEN_ATOMICEXPANDUTILS_H
11321369Sdim#define LLVM_CODEGEN_ATOMICEXPANDUTILS_H
12321369Sdim
13292915Sdim#include "llvm/ADT/STLExtras.h"
14292915Sdim#include "llvm/IR/IRBuilder.h"
15321369Sdim#include "llvm/Support/AtomicOrdering.h"
16292915Sdim
17292915Sdimnamespace llvm {
18321369Sdim
19321369Sdimclass AtomicRMWInst;
20292915Sdimclass Value;
21292915Sdim
22292915Sdim/// Parameters (see the expansion example below):
23292915Sdim/// (the builder, %addr, %loaded, %new_val, ordering,
24292915Sdim///  /* OUT */ %success, /* OUT */ %new_loaded)
25321369Sdimusing CreateCmpXchgInstFun =
26321369Sdim    function_ref<void(IRBuilder<> &, Value *, Value *, Value *, AtomicOrdering,
27321369Sdim                      Value *&, Value *&)>;
28292915Sdim
29292915Sdim/// \brief Expand an atomic RMW instruction into a loop utilizing
30292915Sdim/// cmpxchg. You'll want to make sure your target machine likes cmpxchg
31292915Sdim/// instructions in the first place and that there isn't another, better,
32292915Sdim/// transformation available (for example AArch32/AArch64 have linked loads).
33292915Sdim///
34292915Sdim/// This is useful in passes which can't rewrite the more exotic RMW
35292915Sdim/// instructions directly into a platform specific intrinsics (because, say,
36292915Sdim/// those intrinsics don't exist). If such a pass is able to expand cmpxchg
37292915Sdim/// instructions directly however, then, with this function, it could avoid two
38292915Sdim/// extra module passes (avoiding passes by `-atomic-expand` and itself). A
39292915Sdim/// specific example would be PNaCl's `RewriteAtomics` pass.
40292915Sdim///
41292915Sdim/// Given: atomicrmw some_op iN* %addr, iN %incr ordering
42292915Sdim///
43292915Sdim/// The standard expansion we produce is:
44292915Sdim///     [...]
45292915Sdim///     %init_loaded = load atomic iN* %addr
46292915Sdim///     br label %loop
47292915Sdim/// loop:
48292915Sdim///     %loaded = phi iN [ %init_loaded, %entry ], [ %new_loaded, %loop ]
49292915Sdim///     %new = some_op iN %loaded, %incr
50321369Sdim/// ; This is what -atomic-expand will produce using this function on i686
51321369Sdim/// targets:
52292915Sdim///     %pair = cmpxchg iN* %addr, iN %loaded, iN %new_val
53292915Sdim///     %new_loaded = extractvalue { iN, i1 } %pair, 0
54292915Sdim///     %success = extractvalue { iN, i1 } %pair, 1
55292915Sdim/// ; End callback produced IR
56292915Sdim///     br i1 %success, label %atomicrmw.end, label %loop
57292915Sdim/// atomicrmw.end:
58292915Sdim///     [...]
59292915Sdim///
60292915Sdim/// Returns true if the containing function was modified.
61321369Sdimbool expandAtomicRMWToCmpXchg(AtomicRMWInst *AI, CreateCmpXchgInstFun Factory);
62321369Sdim
63321369Sdim} // end namespace llvm
64321369Sdim
65321369Sdim#endif // LLVM_CODEGEN_ATOMICEXPANDUTILS_H
66