1321369Sdim//===- AtomicExpandUtils.h - Utilities for expanding atomic instructions --===// 2292915Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6292915Sdim// 7292915Sdim//===----------------------------------------------------------------------===// 8292915Sdim 9321369Sdim#ifndef LLVM_CODEGEN_ATOMICEXPANDUTILS_H 10321369Sdim#define LLVM_CODEGEN_ATOMICEXPANDUTILS_H 11321369Sdim 12292915Sdim#include "llvm/ADT/STLExtras.h" 13292915Sdim#include "llvm/IR/IRBuilder.h" 14321369Sdim#include "llvm/Support/AtomicOrdering.h" 15292915Sdim 16292915Sdimnamespace llvm { 17321369Sdim 18321369Sdimclass AtomicRMWInst; 19292915Sdimclass Value; 20292915Sdim 21292915Sdim/// Parameters (see the expansion example below): 22292915Sdim/// (the builder, %addr, %loaded, %new_val, ordering, 23292915Sdim/// /* OUT */ %success, /* OUT */ %new_loaded) 24321369Sdimusing CreateCmpXchgInstFun = 25321369Sdim function_ref<void(IRBuilder<> &, Value *, Value *, Value *, AtomicOrdering, 26321369Sdim Value *&, Value *&)>; 27292915Sdim 28341825Sdim/// Expand an atomic RMW instruction into a loop utilizing 29292915Sdim/// cmpxchg. You'll want to make sure your target machine likes cmpxchg 30292915Sdim/// instructions in the first place and that there isn't another, better, 31292915Sdim/// transformation available (for example AArch32/AArch64 have linked loads). 32292915Sdim/// 33292915Sdim/// This is useful in passes which can't rewrite the more exotic RMW 34292915Sdim/// instructions directly into a platform specific intrinsics (because, say, 35292915Sdim/// those intrinsics don't exist). If such a pass is able to expand cmpxchg 36292915Sdim/// instructions directly however, then, with this function, it could avoid two 37292915Sdim/// extra module passes (avoiding passes by `-atomic-expand` and itself). A 38292915Sdim/// specific example would be PNaCl's `RewriteAtomics` pass. 39292915Sdim/// 40292915Sdim/// Given: atomicrmw some_op iN* %addr, iN %incr ordering 41292915Sdim/// 42292915Sdim/// The standard expansion we produce is: 43292915Sdim/// [...] 44292915Sdim/// %init_loaded = load atomic iN* %addr 45292915Sdim/// br label %loop 46292915Sdim/// loop: 47292915Sdim/// %loaded = phi iN [ %init_loaded, %entry ], [ %new_loaded, %loop ] 48292915Sdim/// %new = some_op iN %loaded, %incr 49321369Sdim/// ; This is what -atomic-expand will produce using this function on i686 50321369Sdim/// targets: 51292915Sdim/// %pair = cmpxchg iN* %addr, iN %loaded, iN %new_val 52292915Sdim/// %new_loaded = extractvalue { iN, i1 } %pair, 0 53292915Sdim/// %success = extractvalue { iN, i1 } %pair, 1 54292915Sdim/// ; End callback produced IR 55292915Sdim/// br i1 %success, label %atomicrmw.end, label %loop 56292915Sdim/// atomicrmw.end: 57292915Sdim/// [...] 58292915Sdim/// 59292915Sdim/// Returns true if the containing function was modified. 60341825Sdimbool expandAtomicRMWToCmpXchg(AtomicRMWInst *AI, CreateCmpXchgInstFun CreateCmpXchg); 61321369Sdim 62321369Sdim} // end namespace llvm 63321369Sdim 64321369Sdim#endif // LLVM_CODEGEN_ATOMICEXPANDUTILS_H 65