summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/InstCombine/InstCombineAtomicRMW.cpp
blob: 86bbfb15986cfb646fd7095b839c138347a5ed89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//===- InstCombineAtomicRMW.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the visit functions for atomic rmw instructions.
//
//===----------------------------------------------------------------------===//
#include "InstCombineInternal.h"
#include "llvm/IR/Instructions.h"

using namespace llvm;

Instruction *InstCombiner::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
  switch (RMWI.getOperation()) {
  default:
    break;
  case AtomicRMWInst::Add:
  case AtomicRMWInst::Sub:
  case AtomicRMWInst::Or:
    // Replace atomicrmw <op> addr, 0 => load atomic addr.

    // Volatile RMWs perform a load and a store, we cannot replace
    // this by just a load.
    if (RMWI.isVolatile())
      break;

    auto *CI = dyn_cast<ConstantInt>(RMWI.getValOperand());
    if (!CI || !CI->isZero())
      break;
    // Check if the required ordering is compatible with an
    // atomic load.
    AtomicOrdering Ordering = RMWI.getOrdering();
    assert(Ordering != AtomicOrdering::NotAtomic &&
           Ordering != AtomicOrdering::Unordered &&
           "AtomicRMWs don't make sense with Unordered or NotAtomic");
    if (Ordering != AtomicOrdering::Acquire &&
        Ordering != AtomicOrdering::Monotonic)
      break;
    LoadInst *Load = new LoadInst(RMWI.getType(), RMWI.getPointerOperand());
    Load->setAtomic(Ordering, RMWI.getSyncScopeID());
    return Load;
  }
  return nullptr;
}
OpenPOWER on IntegriCloud