annotate lib/CodeGen/AtomicExpandPass.cpp @ 120:1172e4bd9c6f

update 4.0.0
author mir3636
date Fri, 25 Nov 2016 19:14:25 +0900
parents 7d135dc70f03
children 803732b1fca8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===-- AtomicExpandPass.cpp - Expand atomic instructions -------===//
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 //
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 //
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // This file contains a pass (at IR level) to replace atomic instructions with
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
11 // __atomic_* library calls, or target specific instruction which implement the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
12 // same semantics in a way which better fits the target backend. This can
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
13 // include the use of (intrinsic-based) load-linked/store-conditional loops,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
14 // AtomicCmpXchg, or type coercions.
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 //
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 //===----------------------------------------------------------------------===//
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
18 #include "llvm/CodeGen/AtomicExpandUtils.h"
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 #include "llvm/CodeGen/Passes.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "llvm/IR/Function.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "llvm/IR/IRBuilder.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "llvm/IR/InstIterator.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "llvm/IR/Instructions.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "llvm/IR/Intrinsics.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include "llvm/IR/Module.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 #include "llvm/Support/Debug.h"
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
27 #include "llvm/Support/raw_ostream.h"
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #include "llvm/Target/TargetLowering.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 #include "llvm/Target/TargetMachine.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 #include "llvm/Target/TargetSubtargetInfo.h"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 using namespace llvm;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 #define DEBUG_TYPE "atomic-expand"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 namespace {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 class AtomicExpand: public FunctionPass {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 const TargetMachine *TM;
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
39 const TargetLowering *TLI;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 public:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 static char ID; // Pass identification, replacement for typeid
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 explicit AtomicExpand(const TargetMachine *TM = nullptr)
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
43 : FunctionPass(ID), TM(TM), TLI(nullptr) {
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 initializeAtomicExpandPass(*PassRegistry::getPassRegistry());
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 bool runOnFunction(Function &F) override;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 private:
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
50 bool bracketInstWithFences(Instruction *I, AtomicOrdering Order,
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
51 bool IsStore, bool IsLoad);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
52 IntegerType *getCorrespondingIntegerType(Type *T, const DataLayout &DL);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
53 LoadInst *convertAtomicLoadToIntegerType(LoadInst *LI);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
54 bool tryExpandAtomicLoad(LoadInst *LI);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
55 bool expandAtomicLoadToLL(LoadInst *LI);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
56 bool expandAtomicLoadToCmpXchg(LoadInst *LI);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
57 StoreInst *convertAtomicStoreToIntegerType(StoreInst *SI);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 bool expandAtomicStore(StoreInst *SI);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
59 bool tryExpandAtomicRMW(AtomicRMWInst *AI);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
60 Value *
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
61 insertRMWLLSCLoop(IRBuilder<> &Builder, Type *ResultTy, Value *Addr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
62 AtomicOrdering MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
63 function_ref<Value *(IRBuilder<> &, Value *)> PerformOp);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
64 void expandAtomicOpToLLSC(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
65 Instruction *I, Type *ResultTy, Value *Addr, AtomicOrdering MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
66 function_ref<Value *(IRBuilder<> &, Value *)> PerformOp);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
67 void expandPartwordAtomicRMW(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
68 AtomicRMWInst *I,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
69 TargetLoweringBase::AtomicExpansionKind ExpansionKind);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
70 void expandPartwordCmpXchg(AtomicCmpXchgInst *I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
71
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
72 AtomicCmpXchgInst *convertCmpXchgToIntegerType(AtomicCmpXchgInst *CI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
73 static Value *insertRMWCmpXchgLoop(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
74 IRBuilder<> &Builder, Type *ResultType, Value *Addr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
75 AtomicOrdering MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
76 function_ref<Value *(IRBuilder<> &, Value *)> PerformOp,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
77 CreateCmpXchgInstFun CreateCmpXchg);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
78
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 bool expandAtomicCmpXchg(AtomicCmpXchgInst *CI);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
80 bool isIdempotentRMW(AtomicRMWInst *AI);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
81 bool simplifyIdempotentRMW(AtomicRMWInst *AI);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
82
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
83 bool expandAtomicOpToLibcall(Instruction *I, unsigned Size, unsigned Align,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
84 Value *PointerOperand, Value *ValueOperand,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
85 Value *CASExpected, AtomicOrdering Ordering,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
86 AtomicOrdering Ordering2,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
87 ArrayRef<RTLIB::Libcall> Libcalls);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
88 void expandAtomicLoadToLibcall(LoadInst *LI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
89 void expandAtomicStoreToLibcall(StoreInst *LI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
90 void expandAtomicRMWToLibcall(AtomicRMWInst *I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
91 void expandAtomicCASToLibcall(AtomicCmpXchgInst *I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
92
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
93 friend bool
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
94 llvm::expandAtomicRMWToCmpXchg(AtomicRMWInst *AI,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
95 CreateCmpXchgInstFun CreateCmpXchg);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 };
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 char AtomicExpand::ID = 0;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 char &llvm::AtomicExpandID = AtomicExpand::ID;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
101 INITIALIZE_TM_PASS(AtomicExpand, "atomic-expand", "Expand Atomic instructions",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
102 false, false)
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 FunctionPass *llvm::createAtomicExpandPass(const TargetMachine *TM) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 return new AtomicExpand(TM);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
108 namespace {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
109 // Helper functions to retrieve the size of atomic instructions.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
110 unsigned getAtomicOpSize(LoadInst *LI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
111 const DataLayout &DL = LI->getModule()->getDataLayout();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
112 return DL.getTypeStoreSize(LI->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
113 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
114
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
115 unsigned getAtomicOpSize(StoreInst *SI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
116 const DataLayout &DL = SI->getModule()->getDataLayout();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
117 return DL.getTypeStoreSize(SI->getValueOperand()->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
118 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
119
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
120 unsigned getAtomicOpSize(AtomicRMWInst *RMWI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
121 const DataLayout &DL = RMWI->getModule()->getDataLayout();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
122 return DL.getTypeStoreSize(RMWI->getValOperand()->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
123 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
124
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
125 unsigned getAtomicOpSize(AtomicCmpXchgInst *CASI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
126 const DataLayout &DL = CASI->getModule()->getDataLayout();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
127 return DL.getTypeStoreSize(CASI->getCompareOperand()->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
128 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
129
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
130 // Helper functions to retrieve the alignment of atomic instructions.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
131 unsigned getAtomicOpAlign(LoadInst *LI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
132 unsigned Align = LI->getAlignment();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
133 // In the future, if this IR restriction is relaxed, we should
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
134 // return DataLayout::getABITypeAlignment when there's no align
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
135 // value.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
136 assert(Align != 0 && "An atomic LoadInst always has an explicit alignment");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
137 return Align;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
138 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
139
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
140 unsigned getAtomicOpAlign(StoreInst *SI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
141 unsigned Align = SI->getAlignment();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
142 // In the future, if this IR restriction is relaxed, we should
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
143 // return DataLayout::getABITypeAlignment when there's no align
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
144 // value.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
145 assert(Align != 0 && "An atomic StoreInst always has an explicit alignment");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
146 return Align;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
147 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
148
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
149 unsigned getAtomicOpAlign(AtomicRMWInst *RMWI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
150 // TODO(PR27168): This instruction has no alignment attribute, but unlike the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
151 // default alignment for load/store, the default here is to assume
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
152 // it has NATURAL alignment, not DataLayout-specified alignment.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
153 const DataLayout &DL = RMWI->getModule()->getDataLayout();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
154 return DL.getTypeStoreSize(RMWI->getValOperand()->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
155 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
156
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
157 unsigned getAtomicOpAlign(AtomicCmpXchgInst *CASI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
158 // TODO(PR27168): same comment as above.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
159 const DataLayout &DL = CASI->getModule()->getDataLayout();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
160 return DL.getTypeStoreSize(CASI->getCompareOperand()->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
161 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
162
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
163 // Determine if a particular atomic operation has a supported size,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
164 // and is of appropriate alignment, to be passed through for target
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
165 // lowering. (Versus turning into a __atomic libcall)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
166 template <typename Inst>
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
167 bool atomicSizeSupported(const TargetLowering *TLI, Inst *I) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
168 unsigned Size = getAtomicOpSize(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
169 unsigned Align = getAtomicOpAlign(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
170 return Align >= Size && Size <= TLI->getMaxAtomicSizeInBitsSupported() / 8;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
171 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
172
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
173 } // end anonymous namespace
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
174
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 bool AtomicExpand::runOnFunction(Function &F) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
176 if (!TM || !TM->getSubtargetImpl(F)->enableAtomicExpand())
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 return false;
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
178 TLI = TM->getSubtargetImpl(F)->getTargetLowering();
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 SmallVector<Instruction *, 1> AtomicInsts;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 // Changing control-flow while iterating through it is a bad idea, so gather a
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 // list of all atomic instructions before we start.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
184 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
185 Instruction *I = &*II;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
186 if (I->isAtomic() && !isa<FenceInst>(I))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
187 AtomicInsts.push_back(I);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 bool MadeChange = false;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 for (auto I : AtomicInsts) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 auto LI = dyn_cast<LoadInst>(I);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 auto SI = dyn_cast<StoreInst>(I);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 auto RMWI = dyn_cast<AtomicRMWInst>(I);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 auto CASI = dyn_cast<AtomicCmpXchgInst>(I);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
196 assert((LI || SI || RMWI || CASI) && "Unknown atomic instruction");
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
198 // If the Size/Alignment is not supported, replace with a libcall.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
199 if (LI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
200 if (!atomicSizeSupported(TLI, LI)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
201 expandAtomicLoadToLibcall(LI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
202 MadeChange = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
203 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
204 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
205 } else if (SI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
206 if (!atomicSizeSupported(TLI, SI)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
207 expandAtomicStoreToLibcall(SI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
208 MadeChange = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
209 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
210 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
211 } else if (RMWI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
212 if (!atomicSizeSupported(TLI, RMWI)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
213 expandAtomicRMWToLibcall(RMWI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
214 MadeChange = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
215 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
216 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
217 } else if (CASI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
218 if (!atomicSizeSupported(TLI, CASI)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
219 expandAtomicCASToLibcall(CASI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
220 MadeChange = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
221 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
222 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
223 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
224
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
225 if (TLI->shouldInsertFencesForAtomic(I)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
226 auto FenceOrdering = AtomicOrdering::Monotonic;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
227 bool IsStore, IsLoad;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
228 if (LI && isAcquireOrStronger(LI->getOrdering())) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
229 FenceOrdering = LI->getOrdering();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
230 LI->setOrdering(AtomicOrdering::Monotonic);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
231 IsStore = false;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
232 IsLoad = true;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
233 } else if (SI && isReleaseOrStronger(SI->getOrdering())) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
234 FenceOrdering = SI->getOrdering();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
235 SI->setOrdering(AtomicOrdering::Monotonic);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
236 IsStore = true;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
237 IsLoad = false;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
238 } else if (RMWI && (isReleaseOrStronger(RMWI->getOrdering()) ||
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
239 isAcquireOrStronger(RMWI->getOrdering()))) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
240 FenceOrdering = RMWI->getOrdering();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
241 RMWI->setOrdering(AtomicOrdering::Monotonic);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
242 IsStore = IsLoad = true;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
243 } else if (CASI && !TLI->shouldExpandAtomicCmpXchgInIR(CASI) &&
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
244 (isReleaseOrStronger(CASI->getSuccessOrdering()) ||
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
245 isAcquireOrStronger(CASI->getSuccessOrdering()))) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
246 // If a compare and swap is lowered to LL/SC, we can do smarter fence
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
247 // insertion, with a stronger one on the success path than on the
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
248 // failure path. As a result, fence insertion is directly done by
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
249 // expandAtomicCmpXchg in that case.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
250 FenceOrdering = CASI->getSuccessOrdering();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
251 CASI->setSuccessOrdering(AtomicOrdering::Monotonic);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
252 CASI->setFailureOrdering(AtomicOrdering::Monotonic);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
253 IsStore = IsLoad = true;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
254 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
255
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
256 if (FenceOrdering != AtomicOrdering::Monotonic) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
257 MadeChange |= bracketInstWithFences(I, FenceOrdering, IsStore, IsLoad);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
258 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
259 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
260
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
261 if (LI) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
262 if (LI->getType()->isFloatingPointTy()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
263 // TODO: add a TLI hook to control this so that each target can
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
264 // convert to lowering the original type one at a time.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
265 LI = convertAtomicLoadToIntegerType(LI);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
266 assert(LI->getType()->isIntegerTy() && "invariant broken");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
267 MadeChange = true;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
268 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
269
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
270 MadeChange |= tryExpandAtomicLoad(LI);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
271 } else if (SI) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
272 if (SI->getValueOperand()->getType()->isFloatingPointTy()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
273 // TODO: add a TLI hook to control this so that each target can
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
274 // convert to lowering the original type one at a time.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
275 SI = convertAtomicStoreToIntegerType(SI);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
276 assert(SI->getValueOperand()->getType()->isIntegerTy() &&
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
277 "invariant broken");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
278 MadeChange = true;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
279 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
280
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
281 if (TLI->shouldExpandAtomicStoreInIR(SI))
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
282 MadeChange |= expandAtomicStore(SI);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
283 } else if (RMWI) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
284 // There are two different ways of expanding RMW instructions:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
285 // - into a load if it is idempotent
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
286 // - into a Cmpxchg/LL-SC loop otherwise
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
287 // we try them in that order.
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
288
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
289 if (isIdempotentRMW(RMWI) && simplifyIdempotentRMW(RMWI)) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
290 MadeChange = true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
291 } else {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
292 MadeChange |= tryExpandAtomicRMW(RMWI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
293 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
294 } else if (CASI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
295 // TODO: when we're ready to make the change at the IR level, we can
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
296 // extend convertCmpXchgToInteger for floating point too.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
297 assert(!CASI->getCompareOperand()->getType()->isFloatingPointTy() &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
298 "unimplemented - floating point not legal at IR level");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
299 if (CASI->getCompareOperand()->getType()->isPointerTy() ) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
300 // TODO: add a TLI hook to control this so that each target can
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
301 // convert to lowering the original type one at a time.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
302 CASI = convertCmpXchgToIntegerType(CASI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
303 assert(CASI->getCompareOperand()->getType()->isIntegerTy() &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
304 "invariant broken");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
305 MadeChange = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
306 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
307
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
308 unsigned MinCASSize = TLI->getMinCmpXchgSizeInBits() / 8;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
309 unsigned ValueSize = getAtomicOpSize(CASI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
310 if (ValueSize < MinCASSize) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
311 assert(!TLI->shouldExpandAtomicCmpXchgInIR(CASI) &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
312 "MinCmpXchgSizeInBits not yet supported for LL/SC expansions.");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
313 expandPartwordCmpXchg(CASI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
314 } else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
315 if (TLI->shouldExpandAtomicCmpXchgInIR(CASI))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
316 MadeChange |= expandAtomicCmpXchg(CASI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
317 }
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 return MadeChange;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
323 bool AtomicExpand::bracketInstWithFences(Instruction *I, AtomicOrdering Order,
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
324 bool IsStore, bool IsLoad) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
325 IRBuilder<> Builder(I);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
326
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
327 auto LeadingFence = TLI->emitLeadingFence(Builder, Order, IsStore, IsLoad);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
328
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
329 auto TrailingFence = TLI->emitTrailingFence(Builder, Order, IsStore, IsLoad);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
330 // The trailing fence is emitted before the instruction instead of after
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
331 // because there is no easy way of setting Builder insertion point after
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
332 // an instruction. So we must erase it from the BB, and insert it back
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
333 // in the right place.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
334 // We have a guard here because not every atomic operation generates a
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
335 // trailing fence.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
336 if (TrailingFence) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
337 TrailingFence->removeFromParent();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
338 TrailingFence->insertAfter(I);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
339 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
340
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
341 return (LeadingFence || TrailingFence);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
342 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
343
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
344 /// Get the iX type with the same bitwidth as T.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
345 IntegerType *AtomicExpand::getCorrespondingIntegerType(Type *T,
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
346 const DataLayout &DL) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
347 EVT VT = TLI->getValueType(DL, T);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
348 unsigned BitWidth = VT.getStoreSizeInBits();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
349 assert(BitWidth == VT.getSizeInBits() && "must be a power of two");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
350 return IntegerType::get(T->getContext(), BitWidth);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
351 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
352
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
353 /// Convert an atomic load of a non-integral type to an integer load of the
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
354 /// equivalent bitwidth. See the function comment on
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
355 /// convertAtomicStoreToIntegerType for background.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
356 LoadInst *AtomicExpand::convertAtomicLoadToIntegerType(LoadInst *LI) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
357 auto *M = LI->getModule();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
358 Type *NewTy = getCorrespondingIntegerType(LI->getType(),
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
359 M->getDataLayout());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
360
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
361 IRBuilder<> Builder(LI);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
362
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
363 Value *Addr = LI->getPointerOperand();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
364 Type *PT = PointerType::get(NewTy,
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
365 Addr->getType()->getPointerAddressSpace());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
366 Value *NewAddr = Builder.CreateBitCast(Addr, PT);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
367
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
368 auto *NewLI = Builder.CreateLoad(NewAddr);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
369 NewLI->setAlignment(LI->getAlignment());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
370 NewLI->setVolatile(LI->isVolatile());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
371 NewLI->setAtomic(LI->getOrdering(), LI->getSynchScope());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
372 DEBUG(dbgs() << "Replaced " << *LI << " with " << *NewLI << "\n");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
373
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
374 Value *NewVal = Builder.CreateBitCast(NewLI, LI->getType());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
375 LI->replaceAllUsesWith(NewVal);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
376 LI->eraseFromParent();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
377 return NewLI;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
378 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
379
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
380 bool AtomicExpand::tryExpandAtomicLoad(LoadInst *LI) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
381 switch (TLI->shouldExpandAtomicLoadInIR(LI)) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
382 case TargetLoweringBase::AtomicExpansionKind::None:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
383 return false;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
384 case TargetLoweringBase::AtomicExpansionKind::LLSC:
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
385 expandAtomicOpToLLSC(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
386 LI, LI->getType(), LI->getPointerOperand(), LI->getOrdering(),
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
387 [](IRBuilder<> &Builder, Value *Loaded) { return Loaded; });
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
388 return true;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
389 case TargetLoweringBase::AtomicExpansionKind::LLOnly:
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
390 return expandAtomicLoadToLL(LI);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
391 case TargetLoweringBase::AtomicExpansionKind::CmpXChg:
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
392 return expandAtomicLoadToCmpXchg(LI);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
393 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
394 llvm_unreachable("Unhandled case in tryExpandAtomicLoad");
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
395 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
396
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
397 bool AtomicExpand::expandAtomicLoadToLL(LoadInst *LI) {
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
398 IRBuilder<> Builder(LI);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
399
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
400 // On some architectures, load-linked instructions are atomic for larger
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
401 // sizes than normal loads. For example, the only 64-bit load guaranteed
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
402 // to be single-copy atomic by ARM is an ldrexd (A3.5.3).
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
403 Value *Val =
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
404 TLI->emitLoadLinked(Builder, LI->getPointerOperand(), LI->getOrdering());
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
405 TLI->emitAtomicCmpXchgNoStoreLLBalance(Builder);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
406
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
407 LI->replaceAllUsesWith(Val);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
408 LI->eraseFromParent();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 return true;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
412
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
413 bool AtomicExpand::expandAtomicLoadToCmpXchg(LoadInst *LI) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
414 IRBuilder<> Builder(LI);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
415 AtomicOrdering Order = LI->getOrdering();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
416 Value *Addr = LI->getPointerOperand();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
417 Type *Ty = cast<PointerType>(Addr->getType())->getElementType();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
418 Constant *DummyVal = Constant::getNullValue(Ty);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
419
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
420 Value *Pair = Builder.CreateAtomicCmpXchg(
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
421 Addr, DummyVal, DummyVal, Order,
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
422 AtomicCmpXchgInst::getStrongestFailureOrdering(Order));
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
423 Value *Loaded = Builder.CreateExtractValue(Pair, 0, "loaded");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
424
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
425 LI->replaceAllUsesWith(Loaded);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
426 LI->eraseFromParent();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
427
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
428 return true;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
429 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
430
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
431 /// Convert an atomic store of a non-integral type to an integer store of the
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
432 /// equivalent bitwidth. We used to not support floating point or vector
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
433 /// atomics in the IR at all. The backends learned to deal with the bitcast
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
434 /// idiom because that was the only way of expressing the notion of a atomic
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
435 /// float or vector store. The long term plan is to teach each backend to
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
436 /// instruction select from the original atomic store, but as a migration
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
437 /// mechanism, we convert back to the old format which the backends understand.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
438 /// Each backend will need individual work to recognize the new format.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
439 StoreInst *AtomicExpand::convertAtomicStoreToIntegerType(StoreInst *SI) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
440 IRBuilder<> Builder(SI);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
441 auto *M = SI->getModule();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
442 Type *NewTy = getCorrespondingIntegerType(SI->getValueOperand()->getType(),
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
443 M->getDataLayout());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
444 Value *NewVal = Builder.CreateBitCast(SI->getValueOperand(), NewTy);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
445
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
446 Value *Addr = SI->getPointerOperand();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
447 Type *PT = PointerType::get(NewTy,
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
448 Addr->getType()->getPointerAddressSpace());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
449 Value *NewAddr = Builder.CreateBitCast(Addr, PT);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
450
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
451 StoreInst *NewSI = Builder.CreateStore(NewVal, NewAddr);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
452 NewSI->setAlignment(SI->getAlignment());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
453 NewSI->setVolatile(SI->isVolatile());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
454 NewSI->setAtomic(SI->getOrdering(), SI->getSynchScope());
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
455 DEBUG(dbgs() << "Replaced " << *SI << " with " << *NewSI << "\n");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
456 SI->eraseFromParent();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
457 return NewSI;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
458 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
459
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 bool AtomicExpand::expandAtomicStore(StoreInst *SI) {
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
461 // This function is only called on atomic stores that are too large to be
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
462 // atomic if implemented as a native store. So we replace them by an
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
463 // atomic swap, that can be implemented for example as a ldrex/strex on ARM
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
464 // or lock cmpxchg8/16b on X86, as these are atomic for larger sizes.
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
465 // It is the responsibility of the target to only signal expansion via
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
466 // shouldExpandAtomicRMW in cases where this is required and possible.
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
467 IRBuilder<> Builder(SI);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
468 AtomicRMWInst *AI =
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
469 Builder.CreateAtomicRMW(AtomicRMWInst::Xchg, SI->getPointerOperand(),
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
470 SI->getValueOperand(), SI->getOrdering());
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
471 SI->eraseFromParent();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
472
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
473 // Now we have an appropriate swap instruction, lower it as usual.
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
474 return tryExpandAtomicRMW(AI);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
475 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
476
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
477 static void createCmpXchgInstFun(IRBuilder<> &Builder, Value *Addr,
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
478 Value *Loaded, Value *NewVal,
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
479 AtomicOrdering MemOpOrder,
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
480 Value *&Success, Value *&NewLoaded) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
481 Value* Pair = Builder.CreateAtomicCmpXchg(
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
482 Addr, Loaded, NewVal, MemOpOrder,
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
483 AtomicCmpXchgInst::getStrongestFailureOrdering(MemOpOrder));
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
484 Success = Builder.CreateExtractValue(Pair, 1, "success");
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
485 NewLoaded = Builder.CreateExtractValue(Pair, 0, "newloaded");
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
486 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
487
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
488 /// Emit IR to implement the given atomicrmw operation on values in registers,
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
489 /// returning the new value.
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
490 static Value *performAtomicOp(AtomicRMWInst::BinOp Op, IRBuilder<> &Builder,
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
491 Value *Loaded, Value *Inc) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
492 Value *NewVal;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
493 switch (Op) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
494 case AtomicRMWInst::Xchg:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
495 return Inc;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
496 case AtomicRMWInst::Add:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
497 return Builder.CreateAdd(Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
498 case AtomicRMWInst::Sub:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
499 return Builder.CreateSub(Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
500 case AtomicRMWInst::And:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
501 return Builder.CreateAnd(Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
502 case AtomicRMWInst::Nand:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
503 return Builder.CreateNot(Builder.CreateAnd(Loaded, Inc), "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
504 case AtomicRMWInst::Or:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
505 return Builder.CreateOr(Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
506 case AtomicRMWInst::Xor:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
507 return Builder.CreateXor(Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
508 case AtomicRMWInst::Max:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
509 NewVal = Builder.CreateICmpSGT(Loaded, Inc);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
510 return Builder.CreateSelect(NewVal, Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
511 case AtomicRMWInst::Min:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
512 NewVal = Builder.CreateICmpSLE(Loaded, Inc);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
513 return Builder.CreateSelect(NewVal, Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
514 case AtomicRMWInst::UMax:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
515 NewVal = Builder.CreateICmpUGT(Loaded, Inc);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
516 return Builder.CreateSelect(NewVal, Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
517 case AtomicRMWInst::UMin:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
518 NewVal = Builder.CreateICmpULE(Loaded, Inc);
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
519 return Builder.CreateSelect(NewVal, Loaded, Inc, "new");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
520 default:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
521 llvm_unreachable("Unknown atomic op");
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
522 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
523 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
524
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
525 bool AtomicExpand::tryExpandAtomicRMW(AtomicRMWInst *AI) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
526 switch (TLI->shouldExpandAtomicRMWInIR(AI)) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
527 case TargetLoweringBase::AtomicExpansionKind::None:
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
528 return false;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
529 case TargetLoweringBase::AtomicExpansionKind::LLSC: {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
530 unsigned MinCASSize = TLI->getMinCmpXchgSizeInBits() / 8;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
531 unsigned ValueSize = getAtomicOpSize(AI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
532 if (ValueSize < MinCASSize) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
533 llvm_unreachable(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
534 "MinCmpXchgSizeInBits not yet supported for LL/SC architectures.");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
535 } else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
536 auto PerformOp = [&](IRBuilder<> &Builder, Value *Loaded) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
537 return performAtomicOp(AI->getOperation(), Builder, Loaded,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
538 AI->getValOperand());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
539 };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
540 expandAtomicOpToLLSC(AI, AI->getType(), AI->getPointerOperand(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
541 AI->getOrdering(), PerformOp);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
542 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
543 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
544 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
545 case TargetLoweringBase::AtomicExpansionKind::CmpXChg: {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
546 unsigned MinCASSize = TLI->getMinCmpXchgSizeInBits() / 8;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
547 unsigned ValueSize = getAtomicOpSize(AI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
548 if (ValueSize < MinCASSize) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
549 expandPartwordAtomicRMW(AI,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
550 TargetLoweringBase::AtomicExpansionKind::CmpXChg);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
551 } else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
552 expandAtomicRMWToCmpXchg(AI, createCmpXchgInstFun);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
553 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
554 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
555 }
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
556 default:
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
557 llvm_unreachable("Unhandled case in tryExpandAtomicRMW");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
558 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
559 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
560
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
561 namespace {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
562
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
563 /// Result values from createMaskInstrs helper.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
564 struct PartwordMaskValues {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
565 Type *WordType;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
566 Type *ValueType;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
567 Value *AlignedAddr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
568 Value *ShiftAmt;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
569 Value *Mask;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
570 Value *Inv_Mask;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
571 };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
572 } // end anonymous namespace
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
573
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
574 /// This is a helper function which builds instructions to provide
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
575 /// values necessary for partword atomic operations. It takes an
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
576 /// incoming address, Addr, and ValueType, and constructs the address,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
577 /// shift-amounts and masks needed to work with a larger value of size
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
578 /// WordSize.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
579 ///
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
580 /// AlignedAddr: Addr rounded down to a multiple of WordSize
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
581 ///
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
582 /// ShiftAmt: Number of bits to right-shift a WordSize value loaded
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
583 /// from AlignAddr for it to have the same value as if
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
584 /// ValueType was loaded from Addr.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
585 ///
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
586 /// Mask: Value to mask with the value loaded from AlignAddr to
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
587 /// include only the part that would've been loaded from Addr.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
588 ///
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
589 /// Inv_Mask: The inverse of Mask.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
590
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
591 static PartwordMaskValues createMaskInstrs(IRBuilder<> &Builder, Instruction *I,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
592 Type *ValueType, Value *Addr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
593 unsigned WordSize) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
594 PartwordMaskValues Ret;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
595
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
596 BasicBlock *BB = I->getParent();
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
597 Function *F = BB->getParent();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
598 Module *M = I->getModule();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
599
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
600 LLVMContext &Ctx = F->getContext();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
601 const DataLayout &DL = M->getDataLayout();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
602
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
603 unsigned ValueSize = DL.getTypeStoreSize(ValueType);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
604
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
605 assert(ValueSize < WordSize);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
606
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
607 Ret.ValueType = ValueType;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
608 Ret.WordType = Type::getIntNTy(Ctx, WordSize * 8);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
609
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
610 Type *WordPtrType =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
611 Ret.WordType->getPointerTo(Addr->getType()->getPointerAddressSpace());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
612
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
613 Value *AddrInt = Builder.CreatePtrToInt(Addr, DL.getIntPtrType(Ctx));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
614 Ret.AlignedAddr = Builder.CreateIntToPtr(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
615 Builder.CreateAnd(AddrInt, ~(uint64_t)(WordSize - 1)), WordPtrType,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
616 "AlignedAddr");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
617
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
618 Value *PtrLSB = Builder.CreateAnd(AddrInt, WordSize - 1, "PtrLSB");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
619 if (DL.isLittleEndian()) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
620 // turn bytes into bits
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
621 Ret.ShiftAmt = Builder.CreateShl(PtrLSB, 3);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
622 } else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
623 // turn bytes into bits, and count from the other side.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
624 Ret.ShiftAmt =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
625 Builder.CreateShl(Builder.CreateXor(PtrLSB, WordSize - ValueSize), 3);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
626 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
627
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
628 Ret.ShiftAmt = Builder.CreateTrunc(Ret.ShiftAmt, Ret.WordType, "ShiftAmt");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
629 Ret.Mask = Builder.CreateShl(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
630 ConstantInt::get(Ret.WordType, (1 << ValueSize * 8) - 1), Ret.ShiftAmt,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
631 "Mask");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
632 Ret.Inv_Mask = Builder.CreateNot(Ret.Mask, "Inv_Mask");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
633
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
634 return Ret;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
635 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
636
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
637 /// Emit IR to implement a masked version of a given atomicrmw
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
638 /// operation. (That is, only the bits under the Mask should be
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
639 /// affected by the operation)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
640 static Value *performMaskedAtomicOp(AtomicRMWInst::BinOp Op,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
641 IRBuilder<> &Builder, Value *Loaded,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
642 Value *Shifted_Inc, Value *Inc,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
643 const PartwordMaskValues &PMV) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
644 switch (Op) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
645 case AtomicRMWInst::Xchg: {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
646 Value *Loaded_MaskOut = Builder.CreateAnd(Loaded, PMV.Inv_Mask);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
647 Value *FinalVal = Builder.CreateOr(Loaded_MaskOut, Shifted_Inc);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
648 return FinalVal;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
649 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
650 case AtomicRMWInst::Or:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
651 case AtomicRMWInst::Xor:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
652 // Or/Xor won't affect any other bits, so can just be done
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
653 // directly.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
654 return performAtomicOp(Op, Builder, Loaded, Shifted_Inc);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
655 case AtomicRMWInst::Add:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
656 case AtomicRMWInst::Sub:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
657 case AtomicRMWInst::And:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
658 case AtomicRMWInst::Nand: {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
659 // The other arithmetic ops need to be masked into place.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
660 Value *NewVal = performAtomicOp(Op, Builder, Loaded, Shifted_Inc);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
661 Value *NewVal_Masked = Builder.CreateAnd(NewVal, PMV.Mask);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
662 Value *Loaded_MaskOut = Builder.CreateAnd(Loaded, PMV.Inv_Mask);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
663 Value *FinalVal = Builder.CreateOr(Loaded_MaskOut, NewVal_Masked);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
664 return FinalVal;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
665 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
666 case AtomicRMWInst::Max:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
667 case AtomicRMWInst::Min:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
668 case AtomicRMWInst::UMax:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
669 case AtomicRMWInst::UMin: {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
670 // Finally, comparison ops will operate on the full value, so
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
671 // truncate down to the original size, and expand out again after
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
672 // doing the operation.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
673 Value *Loaded_Shiftdown = Builder.CreateTrunc(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
674 Builder.CreateLShr(Loaded, PMV.ShiftAmt), PMV.ValueType);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
675 Value *NewVal = performAtomicOp(Op, Builder, Loaded_Shiftdown, Inc);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
676 Value *NewVal_Shiftup = Builder.CreateShl(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
677 Builder.CreateZExt(NewVal, PMV.WordType), PMV.ShiftAmt);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
678 Value *Loaded_MaskOut = Builder.CreateAnd(Loaded, PMV.Inv_Mask);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
679 Value *FinalVal = Builder.CreateOr(Loaded_MaskOut, NewVal_Shiftup);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
680 return FinalVal;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
681 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
682 default:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
683 llvm_unreachable("Unknown atomic op");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
684 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
685 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
686
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
687 /// Expand a sub-word atomicrmw operation into an appropriate
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
688 /// word-sized operation.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
689 ///
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
690 /// It will create an LL/SC or cmpxchg loop, as appropriate, the same
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
691 /// way as a typical atomicrmw expansion. The only difference here is
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
692 /// that the operation inside of the loop must operate only upon a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
693 /// part of the value.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
694 void AtomicExpand::expandPartwordAtomicRMW(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
695 AtomicRMWInst *AI, TargetLoweringBase::AtomicExpansionKind ExpansionKind) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
696
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
697 assert(ExpansionKind == TargetLoweringBase::AtomicExpansionKind::CmpXChg);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
698
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
699 AtomicOrdering MemOpOrder = AI->getOrdering();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
700
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
701 IRBuilder<> Builder(AI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
702
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
703 PartwordMaskValues PMV =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
704 createMaskInstrs(Builder, AI, AI->getType(), AI->getPointerOperand(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
705 TLI->getMinCmpXchgSizeInBits() / 8);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
706
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
707 Value *ValOperand_Shifted =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
708 Builder.CreateShl(Builder.CreateZExt(AI->getValOperand(), PMV.WordType),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
709 PMV.ShiftAmt, "ValOperand_Shifted");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
710
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
711 auto PerformPartwordOp = [&](IRBuilder<> &Builder, Value *Loaded) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
712 return performMaskedAtomicOp(AI->getOperation(), Builder, Loaded,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
713 ValOperand_Shifted, AI->getValOperand(), PMV);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
714 };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
715
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
716 // TODO: When we're ready to support LLSC conversions too, use
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
717 // insertRMWLLSCLoop here for ExpansionKind==LLSC.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
718 Value *OldResult =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
719 insertRMWCmpXchgLoop(Builder, PMV.WordType, PMV.AlignedAddr, MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
720 PerformPartwordOp, createCmpXchgInstFun);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
721 Value *FinalOldResult = Builder.CreateTrunc(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
722 Builder.CreateLShr(OldResult, PMV.ShiftAmt), PMV.ValueType);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
723 AI->replaceAllUsesWith(FinalOldResult);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
724 AI->eraseFromParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
725 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
726
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
727 void AtomicExpand::expandPartwordCmpXchg(AtomicCmpXchgInst *CI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
728 // The basic idea here is that we're expanding a cmpxchg of a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
729 // smaller memory size up to a word-sized cmpxchg. To do this, we
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
730 // need to add a retry-loop for strong cmpxchg, so that
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
731 // modifications to other parts of the word don't cause a spurious
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
732 // failure.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
733
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
734 // This generates code like the following:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
735 // [[Setup mask values PMV.*]]
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
736 // %NewVal_Shifted = shl i32 %NewVal, %PMV.ShiftAmt
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
737 // %Cmp_Shifted = shl i32 %Cmp, %PMV.ShiftAmt
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
738 // %InitLoaded = load i32* %addr
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
739 // %InitLoaded_MaskOut = and i32 %InitLoaded, %PMV.Inv_Mask
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
740 // br partword.cmpxchg.loop
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
741 // partword.cmpxchg.loop:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
742 // %Loaded_MaskOut = phi i32 [ %InitLoaded_MaskOut, %entry ],
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
743 // [ %OldVal_MaskOut, %partword.cmpxchg.failure ]
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
744 // %FullWord_NewVal = or i32 %Loaded_MaskOut, %NewVal_Shifted
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
745 // %FullWord_Cmp = or i32 %Loaded_MaskOut, %Cmp_Shifted
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
746 // %NewCI = cmpxchg i32* %PMV.AlignedAddr, i32 %FullWord_Cmp,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
747 // i32 %FullWord_NewVal success_ordering failure_ordering
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
748 // %OldVal = extractvalue { i32, i1 } %NewCI, 0
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
749 // %Success = extractvalue { i32, i1 } %NewCI, 1
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
750 // br i1 %Success, label %partword.cmpxchg.end,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
751 // label %partword.cmpxchg.failure
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
752 // partword.cmpxchg.failure:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
753 // %OldVal_MaskOut = and i32 %OldVal, %PMV.Inv_Mask
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
754 // %ShouldContinue = icmp ne i32 %Loaded_MaskOut, %OldVal_MaskOut
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
755 // br i1 %ShouldContinue, label %partword.cmpxchg.loop,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
756 // label %partword.cmpxchg.end
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
757 // partword.cmpxchg.end:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
758 // %tmp1 = lshr i32 %OldVal, %PMV.ShiftAmt
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
759 // %FinalOldVal = trunc i32 %tmp1 to i8
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
760 // %tmp2 = insertvalue { i8, i1 } undef, i8 %FinalOldVal, 0
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
761 // %Res = insertvalue { i8, i1 } %25, i1 %Success, 1
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
762
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
763 Value *Addr = CI->getPointerOperand();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
764 Value *Cmp = CI->getCompareOperand();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
765 Value *NewVal = CI->getNewValOperand();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
766
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
767 BasicBlock *BB = CI->getParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
768 Function *F = BB->getParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
769 IRBuilder<> Builder(CI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
770 LLVMContext &Ctx = Builder.getContext();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
771
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
772 const int WordSize = TLI->getMinCmpXchgSizeInBits() / 8;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
773
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
774 BasicBlock *EndBB =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
775 BB->splitBasicBlock(CI->getIterator(), "partword.cmpxchg.end");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
776 auto FailureBB =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
777 BasicBlock::Create(Ctx, "partword.cmpxchg.failure", F, EndBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
778 auto LoopBB = BasicBlock::Create(Ctx, "partword.cmpxchg.loop", F, FailureBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
779
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
780 // The split call above "helpfully" added a branch at the end of BB
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
781 // (to the wrong place).
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
782 std::prev(BB->end())->eraseFromParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
783 Builder.SetInsertPoint(BB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
784
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
785 PartwordMaskValues PMV = createMaskInstrs(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
786 Builder, CI, CI->getCompareOperand()->getType(), Addr, WordSize);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
787
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
788 // Shift the incoming values over, into the right location in the word.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
789 Value *NewVal_Shifted =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
790 Builder.CreateShl(Builder.CreateZExt(NewVal, PMV.WordType), PMV.ShiftAmt);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
791 Value *Cmp_Shifted =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
792 Builder.CreateShl(Builder.CreateZExt(Cmp, PMV.WordType), PMV.ShiftAmt);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
793
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
794 // Load the entire current word, and mask into place the expected and new
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
795 // values
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
796 LoadInst *InitLoaded = Builder.CreateLoad(PMV.WordType, PMV.AlignedAddr);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
797 InitLoaded->setVolatile(CI->isVolatile());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
798 Value *InitLoaded_MaskOut = Builder.CreateAnd(InitLoaded, PMV.Inv_Mask);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
799 Builder.CreateBr(LoopBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
800
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
801 // partword.cmpxchg.loop:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
802 Builder.SetInsertPoint(LoopBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
803 PHINode *Loaded_MaskOut = Builder.CreatePHI(PMV.WordType, 2);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
804 Loaded_MaskOut->addIncoming(InitLoaded_MaskOut, BB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
805
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
806 // Mask/Or the expected and new values into place in the loaded word.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
807 Value *FullWord_NewVal = Builder.CreateOr(Loaded_MaskOut, NewVal_Shifted);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
808 Value *FullWord_Cmp = Builder.CreateOr(Loaded_MaskOut, Cmp_Shifted);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
809 AtomicCmpXchgInst *NewCI = Builder.CreateAtomicCmpXchg(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
810 PMV.AlignedAddr, FullWord_Cmp, FullWord_NewVal, CI->getSuccessOrdering(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
811 CI->getFailureOrdering(), CI->getSynchScope());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
812 NewCI->setVolatile(CI->isVolatile());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
813 // When we're building a strong cmpxchg, we need a loop, so you
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
814 // might think we could use a weak cmpxchg inside. But, using strong
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
815 // allows the below comparison for ShouldContinue, and we're
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
816 // expecting the underlying cmpxchg to be a machine instruction,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
817 // which is strong anyways.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
818 NewCI->setWeak(CI->isWeak());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
819
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
820 Value *OldVal = Builder.CreateExtractValue(NewCI, 0);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
821 Value *Success = Builder.CreateExtractValue(NewCI, 1);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
822
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
823 if (CI->isWeak())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
824 Builder.CreateBr(EndBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
825 else
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
826 Builder.CreateCondBr(Success, EndBB, FailureBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
827
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
828 // partword.cmpxchg.failure:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
829 Builder.SetInsertPoint(FailureBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
830 // Upon failure, verify that the masked-out part of the loaded value
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
831 // has been modified. If it didn't, abort the cmpxchg, since the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
832 // masked-in part must've.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
833 Value *OldVal_MaskOut = Builder.CreateAnd(OldVal, PMV.Inv_Mask);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
834 Value *ShouldContinue = Builder.CreateICmpNE(Loaded_MaskOut, OldVal_MaskOut);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
835 Builder.CreateCondBr(ShouldContinue, LoopBB, EndBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
836
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
837 // Add the second value to the phi from above
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
838 Loaded_MaskOut->addIncoming(OldVal_MaskOut, FailureBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
839
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
840 // partword.cmpxchg.end:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
841 Builder.SetInsertPoint(CI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
842
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
843 Value *FinalOldVal = Builder.CreateTrunc(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
844 Builder.CreateLShr(OldVal, PMV.ShiftAmt), PMV.ValueType);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
845 Value *Res = UndefValue::get(CI->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
846 Res = Builder.CreateInsertValue(Res, FinalOldVal, 0);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
847 Res = Builder.CreateInsertValue(Res, Success, 1);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
848
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
849 CI->replaceAllUsesWith(Res);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
850 CI->eraseFromParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
851 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
852
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
853 void AtomicExpand::expandAtomicOpToLLSC(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
854 Instruction *I, Type *ResultType, Value *Addr, AtomicOrdering MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
855 function_ref<Value *(IRBuilder<> &, Value *)> PerformOp) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
856 IRBuilder<> Builder(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
857 Value *Loaded =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
858 insertRMWLLSCLoop(Builder, ResultType, Addr, MemOpOrder, PerformOp);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
859
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
860 I->replaceAllUsesWith(Loaded);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
861 I->eraseFromParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
862 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
863
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
864 Value *AtomicExpand::insertRMWLLSCLoop(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
865 IRBuilder<> &Builder, Type *ResultTy, Value *Addr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
866 AtomicOrdering MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
867 function_ref<Value *(IRBuilder<> &, Value *)> PerformOp) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
868 LLVMContext &Ctx = Builder.getContext();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
869 BasicBlock *BB = Builder.GetInsertBlock();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
870 Function *F = BB->getParent();
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
871
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
872 // Given: atomicrmw some_op iN* %addr, iN %incr ordering
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
873 //
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
874 // The standard expansion we produce is:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
875 // [...]
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
876 // atomicrmw.start:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
877 // %loaded = @load.linked(%addr)
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
878 // %new = some_op iN %loaded, %incr
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
879 // %stored = @store_conditional(%new, %addr)
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
880 // %try_again = icmp i32 ne %stored, 0
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
881 // br i1 %try_again, label %loop, label %atomicrmw.end
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
882 // atomicrmw.end:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
883 // [...]
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
884 BasicBlock *ExitBB =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
885 BB->splitBasicBlock(Builder.GetInsertPoint(), "atomicrmw.end");
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
886 BasicBlock *LoopBB = BasicBlock::Create(Ctx, "atomicrmw.start", F, ExitBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
887
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
888 // The split call above "helpfully" added a branch at the end of BB (to the
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
889 // wrong place).
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
890 std::prev(BB->end())->eraseFromParent();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
891 Builder.SetInsertPoint(BB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
892 Builder.CreateBr(LoopBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
893
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
894 // Start the main loop block now that we've taken care of the preliminaries.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
895 Builder.SetInsertPoint(LoopBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
896 Value *Loaded = TLI->emitLoadLinked(Builder, Addr, MemOpOrder);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
897
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
898 Value *NewVal = PerformOp(Builder, Loaded);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
899
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
900 Value *StoreSuccess =
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
901 TLI->emitStoreConditional(Builder, NewVal, Addr, MemOpOrder);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
902 Value *TryAgain = Builder.CreateICmpNE(
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
903 StoreSuccess, ConstantInt::get(IntegerType::get(Ctx, 32), 0), "tryagain");
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
904 Builder.CreateCondBr(TryAgain, LoopBB, ExitBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
905
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
906 Builder.SetInsertPoint(ExitBB, ExitBB->begin());
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
907 return Loaded;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
908 }
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
909
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
910 /// Convert an atomic cmpxchg of a non-integral type to an integer cmpxchg of
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
911 /// the equivalent bitwidth. We used to not support pointer cmpxchg in the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
912 /// IR. As a migration step, we convert back to what use to be the standard
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
913 /// way to represent a pointer cmpxchg so that we can update backends one by
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
914 /// one.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
915 AtomicCmpXchgInst *AtomicExpand::convertCmpXchgToIntegerType(AtomicCmpXchgInst *CI) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
916 auto *M = CI->getModule();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
917 Type *NewTy = getCorrespondingIntegerType(CI->getCompareOperand()->getType(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
918 M->getDataLayout());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
919
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
920 IRBuilder<> Builder(CI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
921
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
922 Value *Addr = CI->getPointerOperand();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
923 Type *PT = PointerType::get(NewTy,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
924 Addr->getType()->getPointerAddressSpace());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
925 Value *NewAddr = Builder.CreateBitCast(Addr, PT);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
926
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
927 Value *NewCmp = Builder.CreatePtrToInt(CI->getCompareOperand(), NewTy);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
928 Value *NewNewVal = Builder.CreatePtrToInt(CI->getNewValOperand(), NewTy);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
929
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
930
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
931 auto *NewCI = Builder.CreateAtomicCmpXchg(NewAddr, NewCmp, NewNewVal,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
932 CI->getSuccessOrdering(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
933 CI->getFailureOrdering(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
934 CI->getSynchScope());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
935 NewCI->setVolatile(CI->isVolatile());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
936 NewCI->setWeak(CI->isWeak());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
937 DEBUG(dbgs() << "Replaced " << *CI << " with " << *NewCI << "\n");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
938
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
939 Value *OldVal = Builder.CreateExtractValue(NewCI, 0);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
940 Value *Succ = Builder.CreateExtractValue(NewCI, 1);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
941
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
942 OldVal = Builder.CreateIntToPtr(OldVal, CI->getCompareOperand()->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
943
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
944 Value *Res = UndefValue::get(CI->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
945 Res = Builder.CreateInsertValue(Res, OldVal, 0);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
946 Res = Builder.CreateInsertValue(Res, Succ, 1);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
947
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
948 CI->replaceAllUsesWith(Res);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
949 CI->eraseFromParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
950 return NewCI;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
951 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
952
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
953
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
954 bool AtomicExpand::expandAtomicCmpXchg(AtomicCmpXchgInst *CI) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
955 AtomicOrdering SuccessOrder = CI->getSuccessOrdering();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
956 AtomicOrdering FailureOrder = CI->getFailureOrdering();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
957 Value *Addr = CI->getPointerOperand();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
958 BasicBlock *BB = CI->getParent();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
959 Function *F = BB->getParent();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
960 LLVMContext &Ctx = F->getContext();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
961 // If shouldInsertFencesForAtomic() returns true, then the target does not
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
962 // want to deal with memory orders, and emitLeading/TrailingFence should take
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
963 // care of everything. Otherwise, emitLeading/TrailingFence are no-op and we
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
964 // should preserve the ordering.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
965 bool ShouldInsertFencesForAtomic = TLI->shouldInsertFencesForAtomic(CI);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
966 AtomicOrdering MemOpOrder =
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
967 ShouldInsertFencesForAtomic ? AtomicOrdering::Monotonic : SuccessOrder;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
968
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
969 // In implementations which use a barrier to achieve release semantics, we can
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
970 // delay emitting this barrier until we know a store is actually going to be
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
971 // attempted. The cost of this delay is that we need 2 copies of the block
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
972 // emitting the load-linked, affecting code size.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
973 //
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
974 // Ideally, this logic would be unconditional except for the minsize check
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
975 // since in other cases the extra blocks naturally collapse down to the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
976 // minimal loop. Unfortunately, this puts too much stress on later
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
977 // optimisations so we avoid emitting the extra logic in those cases too.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
978 bool HasReleasedLoadBB = !CI->isWeak() && ShouldInsertFencesForAtomic &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
979 SuccessOrder != AtomicOrdering::Monotonic &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
980 SuccessOrder != AtomicOrdering::Acquire &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
981 !F->optForMinSize();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
982
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
983 // There's no overhead for sinking the release barrier in a weak cmpxchg, so
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
984 // do it even on minsize.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
985 bool UseUnconditionalReleaseBarrier = F->optForMinSize() && !CI->isWeak();
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
986
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
987 // Given: cmpxchg some_op iN* %addr, iN %desired, iN %new success_ord fail_ord
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
988 //
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
989 // The full expansion we produce is:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
990 // [...]
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
991 // cmpxchg.start:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
992 // %unreleasedload = @load.linked(%addr)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
993 // %should_store = icmp eq %unreleasedload, %desired
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
994 // br i1 %should_store, label %cmpxchg.fencedstore,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
995 // label %cmpxchg.nostore
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
996 // cmpxchg.releasingstore:
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
997 // fence?
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
998 // br label cmpxchg.trystore
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
999 // cmpxchg.trystore:
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1000 // %loaded.trystore = phi [%unreleasedload, %releasingstore],
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1001 // [%releasedload, %cmpxchg.releasedload]
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1002 // %stored = @store_conditional(%new, %addr)
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1003 // %success = icmp eq i32 %stored, 0
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1004 // br i1 %success, label %cmpxchg.success,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1005 // label %cmpxchg.releasedload/%cmpxchg.failure
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1006 // cmpxchg.releasedload:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1007 // %releasedload = @load.linked(%addr)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1008 // %should_store = icmp eq %releasedload, %desired
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1009 // br i1 %should_store, label %cmpxchg.trystore,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1010 // label %cmpxchg.failure
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1011 // cmpxchg.success:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1012 // fence?
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1013 // br label %cmpxchg.end
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1014 // cmpxchg.nostore:
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1015 // %loaded.nostore = phi [%unreleasedload, %cmpxchg.start],
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1016 // [%releasedload,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1017 // %cmpxchg.releasedload/%cmpxchg.trystore]
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1018 // @load_linked_fail_balance()?
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1019 // br label %cmpxchg.failure
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1020 // cmpxchg.failure:
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1021 // fence?
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1022 // br label %cmpxchg.end
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1023 // cmpxchg.end:
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1024 // %loaded = phi [%loaded.nostore, %cmpxchg.failure],
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1025 // [%loaded.trystore, %cmpxchg.trystore]
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1026 // %success = phi i1 [true, %cmpxchg.success], [false, %cmpxchg.failure]
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1027 // %restmp = insertvalue { iN, i1 } undef, iN %loaded, 0
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1028 // %res = insertvalue { iN, i1 } %restmp, i1 %success, 1
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1029 // [...]
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1030 BasicBlock *ExitBB = BB->splitBasicBlock(CI->getIterator(), "cmpxchg.end");
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1031 auto FailureBB = BasicBlock::Create(Ctx, "cmpxchg.failure", F, ExitBB);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1032 auto NoStoreBB = BasicBlock::Create(Ctx, "cmpxchg.nostore", F, FailureBB);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1033 auto SuccessBB = BasicBlock::Create(Ctx, "cmpxchg.success", F, NoStoreBB);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1034 auto ReleasedLoadBB =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1035 BasicBlock::Create(Ctx, "cmpxchg.releasedload", F, SuccessBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1036 auto TryStoreBB =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1037 BasicBlock::Create(Ctx, "cmpxchg.trystore", F, ReleasedLoadBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1038 auto ReleasingStoreBB =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1039 BasicBlock::Create(Ctx, "cmpxchg.fencedstore", F, TryStoreBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1040 auto StartBB = BasicBlock::Create(Ctx, "cmpxchg.start", F, ReleasingStoreBB);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1041
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1042 // This grabs the DebugLoc from CI
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1043 IRBuilder<> Builder(CI);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1044
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1045 // The split call above "helpfully" added a branch at the end of BB (to the
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1046 // wrong place), but we might want a fence too. It's easiest to just remove
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1047 // the branch entirely.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1048 std::prev(BB->end())->eraseFromParent();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1049 Builder.SetInsertPoint(BB);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1050 if (ShouldInsertFencesForAtomic && UseUnconditionalReleaseBarrier)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1051 TLI->emitLeadingFence(Builder, SuccessOrder, /*IsStore=*/true,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1052 /*IsLoad=*/true);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1053 Builder.CreateBr(StartBB);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1054
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1055 // Start the main loop block now that we've taken care of the preliminaries.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1056 Builder.SetInsertPoint(StartBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1057 Value *UnreleasedLoad = TLI->emitLoadLinked(Builder, Addr, MemOpOrder);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1058 Value *ShouldStore = Builder.CreateICmpEQ(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1059 UnreleasedLoad, CI->getCompareOperand(), "should_store");
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1060
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1061 // If the cmpxchg doesn't actually need any ordering when it fails, we can
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1062 // jump straight past that fence instruction (if it exists).
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1063 Builder.CreateCondBr(ShouldStore, ReleasingStoreBB, NoStoreBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1064
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1065 Builder.SetInsertPoint(ReleasingStoreBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1066 if (ShouldInsertFencesForAtomic && !UseUnconditionalReleaseBarrier)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1067 TLI->emitLeadingFence(Builder, SuccessOrder, /*IsStore=*/true,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1068 /*IsLoad=*/true);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1069 Builder.CreateBr(TryStoreBB);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1070
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1071 Builder.SetInsertPoint(TryStoreBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1072 Value *StoreSuccess = TLI->emitStoreConditional(
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1073 Builder, CI->getNewValOperand(), Addr, MemOpOrder);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1074 StoreSuccess = Builder.CreateICmpEQ(
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1075 StoreSuccess, ConstantInt::get(Type::getInt32Ty(Ctx), 0), "success");
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1076 BasicBlock *RetryBB = HasReleasedLoadBB ? ReleasedLoadBB : StartBB;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1077 Builder.CreateCondBr(StoreSuccess, SuccessBB,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1078 CI->isWeak() ? FailureBB : RetryBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1079
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1080 Builder.SetInsertPoint(ReleasedLoadBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1081 Value *SecondLoad;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1082 if (HasReleasedLoadBB) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1083 SecondLoad = TLI->emitLoadLinked(Builder, Addr, MemOpOrder);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1084 ShouldStore = Builder.CreateICmpEQ(SecondLoad, CI->getCompareOperand(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1085 "should_store");
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1086
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1087 // If the cmpxchg doesn't actually need any ordering when it fails, we can
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1088 // jump straight past that fence instruction (if it exists).
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1089 Builder.CreateCondBr(ShouldStore, TryStoreBB, NoStoreBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1090 } else
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1091 Builder.CreateUnreachable();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1092
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1093 // Make sure later instructions don't get reordered with a fence if
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1094 // necessary.
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1095 Builder.SetInsertPoint(SuccessBB);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1096 if (ShouldInsertFencesForAtomic)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1097 TLI->emitTrailingFence(Builder, SuccessOrder, /*IsStore=*/true,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1098 /*IsLoad=*/true);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1099 Builder.CreateBr(ExitBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1100
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1101 Builder.SetInsertPoint(NoStoreBB);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1102 // In the failing case, where we don't execute the store-conditional, the
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1103 // target might want to balance out the load-linked with a dedicated
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1104 // instruction (e.g., on ARM, clearing the exclusive monitor).
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1105 TLI->emitAtomicCmpXchgNoStoreLLBalance(Builder);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1106 Builder.CreateBr(FailureBB);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1107
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1108 Builder.SetInsertPoint(FailureBB);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1109 if (ShouldInsertFencesForAtomic)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1110 TLI->emitTrailingFence(Builder, FailureOrder, /*IsStore=*/true,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1111 /*IsLoad=*/true);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1112 Builder.CreateBr(ExitBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1113
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1114 // Finally, we have control-flow based knowledge of whether the cmpxchg
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1115 // succeeded or not. We expose this to later passes by converting any
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1116 // subsequent "icmp eq/ne %loaded, %oldval" into a use of an appropriate
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1117 // PHI.
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1118 Builder.SetInsertPoint(ExitBB, ExitBB->begin());
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1119 PHINode *Success = Builder.CreatePHI(Type::getInt1Ty(Ctx), 2);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1120 Success->addIncoming(ConstantInt::getTrue(Ctx), SuccessBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1121 Success->addIncoming(ConstantInt::getFalse(Ctx), FailureBB);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1122
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1123 // Setup the builder so we can create any PHIs we need.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1124 Value *Loaded;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1125 if (!HasReleasedLoadBB)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1126 Loaded = UnreleasedLoad;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1127 else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1128 Builder.SetInsertPoint(TryStoreBB, TryStoreBB->begin());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1129 PHINode *TryStoreLoaded = Builder.CreatePHI(UnreleasedLoad->getType(), 2);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1130 TryStoreLoaded->addIncoming(UnreleasedLoad, ReleasingStoreBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1131 TryStoreLoaded->addIncoming(SecondLoad, ReleasedLoadBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1132
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1133 Builder.SetInsertPoint(NoStoreBB, NoStoreBB->begin());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1134 PHINode *NoStoreLoaded = Builder.CreatePHI(UnreleasedLoad->getType(), 2);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1135 NoStoreLoaded->addIncoming(UnreleasedLoad, StartBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1136 NoStoreLoaded->addIncoming(SecondLoad, ReleasedLoadBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1137
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1138 Builder.SetInsertPoint(ExitBB, ++ExitBB->begin());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1139 PHINode *ExitLoaded = Builder.CreatePHI(UnreleasedLoad->getType(), 2);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1140 ExitLoaded->addIncoming(TryStoreLoaded, SuccessBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1141 ExitLoaded->addIncoming(NoStoreLoaded, FailureBB);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1142
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1143 Loaded = ExitLoaded;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1144 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1145
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1146 // Look for any users of the cmpxchg that are just comparing the loaded value
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1147 // against the desired one, and replace them with the CFG-derived version.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1148 SmallVector<ExtractValueInst *, 2> PrunedInsts;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1149 for (auto User : CI->users()) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1150 ExtractValueInst *EV = dyn_cast<ExtractValueInst>(User);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1151 if (!EV)
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1152 continue;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1153
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1154 assert(EV->getNumIndices() == 1 && EV->getIndices()[0] <= 1 &&
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1155 "weird extraction from { iN, i1 }");
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1156
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1157 if (EV->getIndices()[0] == 0)
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1158 EV->replaceAllUsesWith(Loaded);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1159 else
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1160 EV->replaceAllUsesWith(Success);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1161
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1162 PrunedInsts.push_back(EV);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1163 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1164
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1165 // We can remove the instructions now we're no longer iterating through them.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1166 for (auto EV : PrunedInsts)
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1167 EV->eraseFromParent();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1168
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1169 if (!CI->use_empty()) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1170 // Some use of the full struct return that we don't understand has happened,
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1171 // so we've got to reconstruct it properly.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1172 Value *Res;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1173 Res = Builder.CreateInsertValue(UndefValue::get(CI->getType()), Loaded, 0);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1174 Res = Builder.CreateInsertValue(Res, Success, 1);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1175
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1176 CI->replaceAllUsesWith(Res);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1177 }
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1178
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1179 CI->eraseFromParent();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1180 return true;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1181 }
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1182
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1183 bool AtomicExpand::isIdempotentRMW(AtomicRMWInst* RMWI) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1184 auto C = dyn_cast<ConstantInt>(RMWI->getValOperand());
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1185 if(!C)
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1186 return false;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1187
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1188 AtomicRMWInst::BinOp Op = RMWI->getOperation();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1189 switch(Op) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1190 case AtomicRMWInst::Add:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1191 case AtomicRMWInst::Sub:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1192 case AtomicRMWInst::Or:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1193 case AtomicRMWInst::Xor:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1194 return C->isZero();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1195 case AtomicRMWInst::And:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1196 return C->isMinusOne();
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1197 // FIXME: we could also treat Min/Max/UMin/UMax by the INT_MIN/INT_MAX/...
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1198 default:
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1199 return false;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1200 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1201 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1202
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1203 bool AtomicExpand::simplifyIdempotentRMW(AtomicRMWInst* RMWI) {
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1204 if (auto ResultingLoad = TLI->lowerIdempotentRMWIntoFencedLoad(RMWI)) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1205 tryExpandAtomicLoad(ResultingLoad);
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1206 return true;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1207 }
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1208 return false;
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
1209 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1210
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1211 Value *AtomicExpand::insertRMWCmpXchgLoop(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1212 IRBuilder<> &Builder, Type *ResultTy, Value *Addr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1213 AtomicOrdering MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1214 function_ref<Value *(IRBuilder<> &, Value *)> PerformOp,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1215 CreateCmpXchgInstFun CreateCmpXchg) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1216 LLVMContext &Ctx = Builder.getContext();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1217 BasicBlock *BB = Builder.GetInsertBlock();
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1218 Function *F = BB->getParent();
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1219
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1220 // Given: atomicrmw some_op iN* %addr, iN %incr ordering
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1221 //
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1222 // The standard expansion we produce is:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1223 // [...]
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1224 // %init_loaded = load atomic iN* %addr
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1225 // br label %loop
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1226 // loop:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1227 // %loaded = phi iN [ %init_loaded, %entry ], [ %new_loaded, %loop ]
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1228 // %new = some_op iN %loaded, %incr
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1229 // %pair = cmpxchg iN* %addr, iN %loaded, iN %new
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1230 // %new_loaded = extractvalue { iN, i1 } %pair, 0
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1231 // %success = extractvalue { iN, i1 } %pair, 1
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1232 // br i1 %success, label %atomicrmw.end, label %loop
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1233 // atomicrmw.end:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1234 // [...]
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1235 BasicBlock *ExitBB =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1236 BB->splitBasicBlock(Builder.GetInsertPoint(), "atomicrmw.end");
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1237 BasicBlock *LoopBB = BasicBlock::Create(Ctx, "atomicrmw.start", F, ExitBB);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1238
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1239 // The split call above "helpfully" added a branch at the end of BB (to the
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1240 // wrong place), but we want a load. It's easiest to just remove
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1241 // the branch entirely.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1242 std::prev(BB->end())->eraseFromParent();
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1243 Builder.SetInsertPoint(BB);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1244 LoadInst *InitLoaded = Builder.CreateLoad(ResultTy, Addr);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1245 // Atomics require at least natural alignment.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1246 InitLoaded->setAlignment(ResultTy->getPrimitiveSizeInBits() / 8);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1247 Builder.CreateBr(LoopBB);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1248
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1249 // Start the main loop block now that we've taken care of the preliminaries.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1250 Builder.SetInsertPoint(LoopBB);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1251 PHINode *Loaded = Builder.CreatePHI(ResultTy, 2, "loaded");
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1252 Loaded->addIncoming(InitLoaded, BB);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1253
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1254 Value *NewVal = PerformOp(Builder, Loaded);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1255
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1256 Value *NewLoaded = nullptr;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1257 Value *Success = nullptr;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1258
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1259 CreateCmpXchg(Builder, Addr, Loaded, NewVal,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1260 MemOpOrder == AtomicOrdering::Unordered
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1261 ? AtomicOrdering::Monotonic
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1262 : MemOpOrder,
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1263 Success, NewLoaded);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1264 assert(Success && NewLoaded);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1265
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1266 Loaded->addIncoming(NewLoaded, LoopBB);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1267
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1268 Builder.CreateCondBr(Success, ExitBB, LoopBB);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1269
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1270 Builder.SetInsertPoint(ExitBB, ExitBB->begin());
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1271 return NewLoaded;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1272 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1273
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1274 // Note: This function is exposed externally by AtomicExpandUtils.h
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1275 bool llvm::expandAtomicRMWToCmpXchg(AtomicRMWInst *AI,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1276 CreateCmpXchgInstFun CreateCmpXchg) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1277 IRBuilder<> Builder(AI);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1278 Value *Loaded = AtomicExpand::insertRMWCmpXchgLoop(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1279 Builder, AI->getType(), AI->getPointerOperand(), AI->getOrdering(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1280 [&](IRBuilder<> &Builder, Value *Loaded) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1281 return performAtomicOp(AI->getOperation(), Builder, Loaded,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1282 AI->getValOperand());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1283 },
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1284 CreateCmpXchg);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1285
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1286 AI->replaceAllUsesWith(Loaded);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1287 AI->eraseFromParent();
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1288 return true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1289 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1290
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1291 // In order to use one of the sized library calls such as
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1292 // __atomic_fetch_add_4, the alignment must be sufficient, the size
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1293 // must be one of the potentially-specialized sizes, and the value
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1294 // type must actually exist in C on the target (otherwise, the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1295 // function wouldn't actually be defined.)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1296 static bool canUseSizedAtomicCall(unsigned Size, unsigned Align,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1297 const DataLayout &DL) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1298 // TODO: "LargestSize" is an approximation for "largest type that
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1299 // you can express in C". It seems to be the case that int128 is
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1300 // supported on all 64-bit platforms, otherwise only up to 64-bit
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1301 // integers are supported. If we get this wrong, then we'll try to
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1302 // call a sized libcall that doesn't actually exist. There should
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1303 // really be some more reliable way in LLVM of determining integer
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1304 // sizes which are valid in the target's C ABI...
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1305 unsigned LargestSize = DL.getLargestLegalIntTypeSizeInBits() >= 64 ? 16 : 8;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1306 return Align >= Size &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1307 (Size == 1 || Size == 2 || Size == 4 || Size == 8 || Size == 16) &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1308 Size <= LargestSize;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1309 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1310
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1311 void AtomicExpand::expandAtomicLoadToLibcall(LoadInst *I) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1312 static const RTLIB::Libcall Libcalls[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1313 RTLIB::ATOMIC_LOAD, RTLIB::ATOMIC_LOAD_1, RTLIB::ATOMIC_LOAD_2,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1314 RTLIB::ATOMIC_LOAD_4, RTLIB::ATOMIC_LOAD_8, RTLIB::ATOMIC_LOAD_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1315 unsigned Size = getAtomicOpSize(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1316 unsigned Align = getAtomicOpAlign(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1317
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1318 bool expanded = expandAtomicOpToLibcall(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1319 I, Size, Align, I->getPointerOperand(), nullptr, nullptr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1320 I->getOrdering(), AtomicOrdering::NotAtomic, Libcalls);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1321 (void)expanded;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1322 assert(expanded && "expandAtomicOpToLibcall shouldn't fail tor Load");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1323 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1324
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1325 void AtomicExpand::expandAtomicStoreToLibcall(StoreInst *I) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1326 static const RTLIB::Libcall Libcalls[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1327 RTLIB::ATOMIC_STORE, RTLIB::ATOMIC_STORE_1, RTLIB::ATOMIC_STORE_2,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1328 RTLIB::ATOMIC_STORE_4, RTLIB::ATOMIC_STORE_8, RTLIB::ATOMIC_STORE_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1329 unsigned Size = getAtomicOpSize(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1330 unsigned Align = getAtomicOpAlign(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1331
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1332 bool expanded = expandAtomicOpToLibcall(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1333 I, Size, Align, I->getPointerOperand(), I->getValueOperand(), nullptr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1334 I->getOrdering(), AtomicOrdering::NotAtomic, Libcalls);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1335 (void)expanded;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1336 assert(expanded && "expandAtomicOpToLibcall shouldn't fail tor Store");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1337 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1338
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1339 void AtomicExpand::expandAtomicCASToLibcall(AtomicCmpXchgInst *I) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1340 static const RTLIB::Libcall Libcalls[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1341 RTLIB::ATOMIC_COMPARE_EXCHANGE, RTLIB::ATOMIC_COMPARE_EXCHANGE_1,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1342 RTLIB::ATOMIC_COMPARE_EXCHANGE_2, RTLIB::ATOMIC_COMPARE_EXCHANGE_4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1343 RTLIB::ATOMIC_COMPARE_EXCHANGE_8, RTLIB::ATOMIC_COMPARE_EXCHANGE_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1344 unsigned Size = getAtomicOpSize(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1345 unsigned Align = getAtomicOpAlign(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1346
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1347 bool expanded = expandAtomicOpToLibcall(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1348 I, Size, Align, I->getPointerOperand(), I->getNewValOperand(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1349 I->getCompareOperand(), I->getSuccessOrdering(), I->getFailureOrdering(),
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1350 Libcalls);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1351 (void)expanded;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1352 assert(expanded && "expandAtomicOpToLibcall shouldn't fail tor CAS");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1353 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1354
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1355 static ArrayRef<RTLIB::Libcall> GetRMWLibcall(AtomicRMWInst::BinOp Op) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1356 static const RTLIB::Libcall LibcallsXchg[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1357 RTLIB::ATOMIC_EXCHANGE, RTLIB::ATOMIC_EXCHANGE_1,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1358 RTLIB::ATOMIC_EXCHANGE_2, RTLIB::ATOMIC_EXCHANGE_4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1359 RTLIB::ATOMIC_EXCHANGE_8, RTLIB::ATOMIC_EXCHANGE_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1360 static const RTLIB::Libcall LibcallsAdd[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1361 RTLIB::UNKNOWN_LIBCALL, RTLIB::ATOMIC_FETCH_ADD_1,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1362 RTLIB::ATOMIC_FETCH_ADD_2, RTLIB::ATOMIC_FETCH_ADD_4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1363 RTLIB::ATOMIC_FETCH_ADD_8, RTLIB::ATOMIC_FETCH_ADD_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1364 static const RTLIB::Libcall LibcallsSub[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1365 RTLIB::UNKNOWN_LIBCALL, RTLIB::ATOMIC_FETCH_SUB_1,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1366 RTLIB::ATOMIC_FETCH_SUB_2, RTLIB::ATOMIC_FETCH_SUB_4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1367 RTLIB::ATOMIC_FETCH_SUB_8, RTLIB::ATOMIC_FETCH_SUB_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1368 static const RTLIB::Libcall LibcallsAnd[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1369 RTLIB::UNKNOWN_LIBCALL, RTLIB::ATOMIC_FETCH_AND_1,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1370 RTLIB::ATOMIC_FETCH_AND_2, RTLIB::ATOMIC_FETCH_AND_4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1371 RTLIB::ATOMIC_FETCH_AND_8, RTLIB::ATOMIC_FETCH_AND_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1372 static const RTLIB::Libcall LibcallsOr[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1373 RTLIB::UNKNOWN_LIBCALL, RTLIB::ATOMIC_FETCH_OR_1,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1374 RTLIB::ATOMIC_FETCH_OR_2, RTLIB::ATOMIC_FETCH_OR_4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1375 RTLIB::ATOMIC_FETCH_OR_8, RTLIB::ATOMIC_FETCH_OR_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1376 static const RTLIB::Libcall LibcallsXor[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1377 RTLIB::UNKNOWN_LIBCALL, RTLIB::ATOMIC_FETCH_XOR_1,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1378 RTLIB::ATOMIC_FETCH_XOR_2, RTLIB::ATOMIC_FETCH_XOR_4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1379 RTLIB::ATOMIC_FETCH_XOR_8, RTLIB::ATOMIC_FETCH_XOR_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1380 static const RTLIB::Libcall LibcallsNand[6] = {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1381 RTLIB::UNKNOWN_LIBCALL, RTLIB::ATOMIC_FETCH_NAND_1,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1382 RTLIB::ATOMIC_FETCH_NAND_2, RTLIB::ATOMIC_FETCH_NAND_4,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1383 RTLIB::ATOMIC_FETCH_NAND_8, RTLIB::ATOMIC_FETCH_NAND_16};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1384
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1385 switch (Op) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1386 case AtomicRMWInst::BAD_BINOP:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1387 llvm_unreachable("Should not have BAD_BINOP.");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1388 case AtomicRMWInst::Xchg:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1389 return makeArrayRef(LibcallsXchg);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1390 case AtomicRMWInst::Add:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1391 return makeArrayRef(LibcallsAdd);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1392 case AtomicRMWInst::Sub:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1393 return makeArrayRef(LibcallsSub);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1394 case AtomicRMWInst::And:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1395 return makeArrayRef(LibcallsAnd);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1396 case AtomicRMWInst::Or:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1397 return makeArrayRef(LibcallsOr);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1398 case AtomicRMWInst::Xor:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1399 return makeArrayRef(LibcallsXor);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1400 case AtomicRMWInst::Nand:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1401 return makeArrayRef(LibcallsNand);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1402 case AtomicRMWInst::Max:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1403 case AtomicRMWInst::Min:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1404 case AtomicRMWInst::UMax:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1405 case AtomicRMWInst::UMin:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1406 // No atomic libcalls are available for max/min/umax/umin.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1407 return {};
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1408 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1409 llvm_unreachable("Unexpected AtomicRMW operation.");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1410 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1411
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1412 void AtomicExpand::expandAtomicRMWToLibcall(AtomicRMWInst *I) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1413 ArrayRef<RTLIB::Libcall> Libcalls = GetRMWLibcall(I->getOperation());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1414
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1415 unsigned Size = getAtomicOpSize(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1416 unsigned Align = getAtomicOpAlign(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1417
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1418 bool Success = false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1419 if (!Libcalls.empty())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1420 Success = expandAtomicOpToLibcall(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1421 I, Size, Align, I->getPointerOperand(), I->getValOperand(), nullptr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1422 I->getOrdering(), AtomicOrdering::NotAtomic, Libcalls);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1423
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1424 // The expansion failed: either there were no libcalls at all for
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1425 // the operation (min/max), or there were only size-specialized
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1426 // libcalls (add/sub/etc) and we needed a generic. So, expand to a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1427 // CAS libcall, via a CAS loop, instead.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1428 if (!Success) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1429 expandAtomicRMWToCmpXchg(I, [this](IRBuilder<> &Builder, Value *Addr,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1430 Value *Loaded, Value *NewVal,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1431 AtomicOrdering MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1432 Value *&Success, Value *&NewLoaded) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1433 // Create the CAS instruction normally...
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1434 AtomicCmpXchgInst *Pair = Builder.CreateAtomicCmpXchg(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1435 Addr, Loaded, NewVal, MemOpOrder,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1436 AtomicCmpXchgInst::getStrongestFailureOrdering(MemOpOrder));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1437 Success = Builder.CreateExtractValue(Pair, 1, "success");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1438 NewLoaded = Builder.CreateExtractValue(Pair, 0, "newloaded");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1439
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1440 // ...and then expand the CAS into a libcall.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1441 expandAtomicCASToLibcall(Pair);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1442 });
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1443 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1444 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1445
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1446 // A helper routine for the above expandAtomic*ToLibcall functions.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1447 //
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1448 // 'Libcalls' contains an array of enum values for the particular
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1449 // ATOMIC libcalls to be emitted. All of the other arguments besides
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1450 // 'I' are extracted from the Instruction subclass by the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1451 // caller. Depending on the particular call, some will be null.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1452 bool AtomicExpand::expandAtomicOpToLibcall(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1453 Instruction *I, unsigned Size, unsigned Align, Value *PointerOperand,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1454 Value *ValueOperand, Value *CASExpected, AtomicOrdering Ordering,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1455 AtomicOrdering Ordering2, ArrayRef<RTLIB::Libcall> Libcalls) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1456 assert(Libcalls.size() == 6);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1457
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1458 LLVMContext &Ctx = I->getContext();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1459 Module *M = I->getModule();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1460 const DataLayout &DL = M->getDataLayout();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1461 IRBuilder<> Builder(I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1462 IRBuilder<> AllocaBuilder(&I->getFunction()->getEntryBlock().front());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1463
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1464 bool UseSizedLibcall = canUseSizedAtomicCall(Size, Align, DL);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1465 Type *SizedIntTy = Type::getIntNTy(Ctx, Size * 8);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1466
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1467 unsigned AllocaAlignment = DL.getPrefTypeAlignment(SizedIntTy);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1468
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1469 // TODO: the "order" argument type is "int", not int32. So
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1470 // getInt32Ty may be wrong if the arch uses e.g. 16-bit ints.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1471 ConstantInt *SizeVal64 = ConstantInt::get(Type::getInt64Ty(Ctx), Size);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1472 assert(Ordering != AtomicOrdering::NotAtomic && "expect atomic MO");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1473 Constant *OrderingVal =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1474 ConstantInt::get(Type::getInt32Ty(Ctx), (int)toCABI(Ordering));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1475 Constant *Ordering2Val = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1476 if (CASExpected) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1477 assert(Ordering2 != AtomicOrdering::NotAtomic && "expect atomic MO");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1478 Ordering2Val =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1479 ConstantInt::get(Type::getInt32Ty(Ctx), (int)toCABI(Ordering2));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1480 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1481 bool HasResult = I->getType() != Type::getVoidTy(Ctx);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1482
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1483 RTLIB::Libcall RTLibType;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1484 if (UseSizedLibcall) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1485 switch (Size) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1486 case 1: RTLibType = Libcalls[1]; break;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1487 case 2: RTLibType = Libcalls[2]; break;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1488 case 4: RTLibType = Libcalls[3]; break;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1489 case 8: RTLibType = Libcalls[4]; break;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1490 case 16: RTLibType = Libcalls[5]; break;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1491 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1492 } else if (Libcalls[0] != RTLIB::UNKNOWN_LIBCALL) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1493 RTLibType = Libcalls[0];
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1494 } else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1495 // Can't use sized function, and there's no generic for this
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1496 // operation, so give up.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1497 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1498 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1499
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1500 // Build up the function call. There's two kinds. First, the sized
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1501 // variants. These calls are going to be one of the following (with
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1502 // N=1,2,4,8,16):
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1503 // iN __atomic_load_N(iN *ptr, int ordering)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1504 // void __atomic_store_N(iN *ptr, iN val, int ordering)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1505 // iN __atomic_{exchange|fetch_*}_N(iN *ptr, iN val, int ordering)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1506 // bool __atomic_compare_exchange_N(iN *ptr, iN *expected, iN desired,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1507 // int success_order, int failure_order)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1508 //
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1509 // Note that these functions can be used for non-integer atomic
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1510 // operations, the values just need to be bitcast to integers on the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1511 // way in and out.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1512 //
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1513 // And, then, the generic variants. They look like the following:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1514 // void __atomic_load(size_t size, void *ptr, void *ret, int ordering)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1515 // void __atomic_store(size_t size, void *ptr, void *val, int ordering)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1516 // void __atomic_exchange(size_t size, void *ptr, void *val, void *ret,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1517 // int ordering)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1518 // bool __atomic_compare_exchange(size_t size, void *ptr, void *expected,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1519 // void *desired, int success_order,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1520 // int failure_order)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1521 //
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1522 // The different signatures are built up depending on the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1523 // 'UseSizedLibcall', 'CASExpected', 'ValueOperand', and 'HasResult'
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1524 // variables.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1525
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1526 AllocaInst *AllocaCASExpected = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1527 Value *AllocaCASExpected_i8 = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1528 AllocaInst *AllocaValue = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1529 Value *AllocaValue_i8 = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1530 AllocaInst *AllocaResult = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1531 Value *AllocaResult_i8 = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1532
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1533 Type *ResultTy;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1534 SmallVector<Value *, 6> Args;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1535 AttributeSet Attr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1536
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1537 // 'size' argument.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1538 if (!UseSizedLibcall) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1539 // Note, getIntPtrType is assumed equivalent to size_t.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1540 Args.push_back(ConstantInt::get(DL.getIntPtrType(Ctx), Size));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1541 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1542
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1543 // 'ptr' argument.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1544 Value *PtrVal =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1545 Builder.CreateBitCast(PointerOperand, Type::getInt8PtrTy(Ctx));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1546 Args.push_back(PtrVal);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1547
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1548 // 'expected' argument, if present.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1549 if (CASExpected) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1550 AllocaCASExpected = AllocaBuilder.CreateAlloca(CASExpected->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1551 AllocaCASExpected->setAlignment(AllocaAlignment);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1552 AllocaCASExpected_i8 =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1553 Builder.CreateBitCast(AllocaCASExpected, Type::getInt8PtrTy(Ctx));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1554 Builder.CreateLifetimeStart(AllocaCASExpected_i8, SizeVal64);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1555 Builder.CreateAlignedStore(CASExpected, AllocaCASExpected, AllocaAlignment);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1556 Args.push_back(AllocaCASExpected_i8);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1557 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1558
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1559 // 'val' argument ('desired' for cas), if present.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1560 if (ValueOperand) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1561 if (UseSizedLibcall) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1562 Value *IntValue =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1563 Builder.CreateBitOrPointerCast(ValueOperand, SizedIntTy);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1564 Args.push_back(IntValue);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1565 } else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1566 AllocaValue = AllocaBuilder.CreateAlloca(ValueOperand->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1567 AllocaValue->setAlignment(AllocaAlignment);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1568 AllocaValue_i8 =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1569 Builder.CreateBitCast(AllocaValue, Type::getInt8PtrTy(Ctx));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1570 Builder.CreateLifetimeStart(AllocaValue_i8, SizeVal64);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1571 Builder.CreateAlignedStore(ValueOperand, AllocaValue, AllocaAlignment);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1572 Args.push_back(AllocaValue_i8);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1573 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1574 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1575
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1576 // 'ret' argument.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1577 if (!CASExpected && HasResult && !UseSizedLibcall) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1578 AllocaResult = AllocaBuilder.CreateAlloca(I->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1579 AllocaResult->setAlignment(AllocaAlignment);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1580 AllocaResult_i8 =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1581 Builder.CreateBitCast(AllocaResult, Type::getInt8PtrTy(Ctx));
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1582 Builder.CreateLifetimeStart(AllocaResult_i8, SizeVal64);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1583 Args.push_back(AllocaResult_i8);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1584 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1585
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1586 // 'ordering' ('success_order' for cas) argument.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1587 Args.push_back(OrderingVal);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1588
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1589 // 'failure_order' argument, if present.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1590 if (Ordering2Val)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1591 Args.push_back(Ordering2Val);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1592
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1593 // Now, the return type.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1594 if (CASExpected) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1595 ResultTy = Type::getInt1Ty(Ctx);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1596 Attr = Attr.addAttribute(Ctx, AttributeSet::ReturnIndex, Attribute::ZExt);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1597 } else if (HasResult && UseSizedLibcall)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1598 ResultTy = SizedIntTy;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1599 else
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1600 ResultTy = Type::getVoidTy(Ctx);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1601
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1602 // Done with setting up arguments and return types, create the call:
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1603 SmallVector<Type *, 6> ArgTys;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1604 for (Value *Arg : Args)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1605 ArgTys.push_back(Arg->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1606 FunctionType *FnType = FunctionType::get(ResultTy, ArgTys, false);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1607 Constant *LibcallFn =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1608 M->getOrInsertFunction(TLI->getLibcallName(RTLibType), FnType, Attr);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1609 CallInst *Call = Builder.CreateCall(LibcallFn, Args);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1610 Call->setAttributes(Attr);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1611 Value *Result = Call;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1612
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1613 // And then, extract the results...
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1614 if (ValueOperand && !UseSizedLibcall)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1615 Builder.CreateLifetimeEnd(AllocaValue_i8, SizeVal64);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1616
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1617 if (CASExpected) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1618 // The final result from the CAS is {load of 'expected' alloca, bool result
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1619 // from call}
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1620 Type *FinalResultTy = I->getType();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1621 Value *V = UndefValue::get(FinalResultTy);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1622 Value *ExpectedOut =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1623 Builder.CreateAlignedLoad(AllocaCASExpected, AllocaAlignment);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1624 Builder.CreateLifetimeEnd(AllocaCASExpected_i8, SizeVal64);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1625 V = Builder.CreateInsertValue(V, ExpectedOut, 0);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1626 V = Builder.CreateInsertValue(V, Result, 1);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1627 I->replaceAllUsesWith(V);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1628 } else if (HasResult) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1629 Value *V;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1630 if (UseSizedLibcall)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1631 V = Builder.CreateBitOrPointerCast(Result, I->getType());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1632 else {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1633 V = Builder.CreateAlignedLoad(AllocaResult, AllocaAlignment);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1634 Builder.CreateLifetimeEnd(AllocaResult_i8, SizeVal64);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1635 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1636 I->replaceAllUsesWith(V);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1637 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1638 I->eraseFromParent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1639 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1640 }