File: | build/llvm-toolchain-snapshot-15~++20220420111733+e13d2efed663/llvm/include/llvm/IR/IRBuilder.h |
Warning: | line 2523, column 23 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- Target/X86/X86LowerAMXType.cpp - -------------------------*- C++ -*-===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | /// \file Pass to transform <256 x i32> load/store | ||||
10 | /// <256 x i32> is bitcasted to x86_amx on X86, and AMX instruction set only | ||||
11 | /// provides simple operation on x86_amx. The basic elementwise operation | ||||
12 | /// is not supported by AMX. Since x86_amx is bitcasted from vector <256 x i32> | ||||
13 | /// and only AMX intrinsics can operate on the type, we need transform | ||||
14 | /// load/store <256 x i32> instruction to AMX load/store. If the bitcast can | ||||
15 | /// not be combined with load/store, we transform the bitcast to amx load/store | ||||
16 | /// and <256 x i32> store/load. | ||||
17 | /// | ||||
18 | /// If Front End not use O0 but the Mid/Back end use O0, (e.g. "Clang -O2 -S | ||||
19 | /// -emit-llvm t.c" + "llc t.ll") we should make sure the amx data is volatile, | ||||
20 | /// because that is necessary for AMX fast register allocation. (In Fast | ||||
21 | /// registera allocation, register will be allocated before spill/reload, so | ||||
22 | /// there is no additional register for amx to identify the step in spill.) | ||||
23 | /// The volatileTileData() will handle this case. | ||||
24 | /// e.g. | ||||
25 | /// ---------------------------------------------------------- | ||||
26 | /// | def %td = ... | | ||||
27 | /// | ... | | ||||
28 | /// | "use %td" | | ||||
29 | /// ---------------------------------------------------------- | ||||
30 | /// will transfer to --> | ||||
31 | /// ---------------------------------------------------------- | ||||
32 | /// | def %td = ... | | ||||
33 | /// | call void @llvm.x86.tilestored64.internal(mem, %td) | | ||||
34 | /// | ... | | ||||
35 | /// | %td2 = call x86_amx @llvm.x86.tileloadd64.internal(mem)| | ||||
36 | /// | "use %td2" | | ||||
37 | /// ---------------------------------------------------------- | ||||
38 | // | ||||
39 | //===----------------------------------------------------------------------===// | ||||
40 | // | ||||
41 | #include "X86.h" | ||||
42 | #include "llvm/ADT/PostOrderIterator.h" | ||||
43 | #include "llvm/ADT/SetVector.h" | ||||
44 | #include "llvm/ADT/SmallSet.h" | ||||
45 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | ||||
46 | #include "llvm/Analysis/TargetLibraryInfo.h" | ||||
47 | #include "llvm/Analysis/TargetTransformInfo.h" | ||||
48 | #include "llvm/CodeGen/Passes.h" | ||||
49 | #include "llvm/CodeGen/TargetPassConfig.h" | ||||
50 | #include "llvm/CodeGen/ValueTypes.h" | ||||
51 | #include "llvm/IR/DataLayout.h" | ||||
52 | #include "llvm/IR/Function.h" | ||||
53 | #include "llvm/IR/IRBuilder.h" | ||||
54 | #include "llvm/IR/Instructions.h" | ||||
55 | #include "llvm/IR/IntrinsicInst.h" | ||||
56 | #include "llvm/IR/IntrinsicsX86.h" | ||||
57 | #include "llvm/IR/PatternMatch.h" | ||||
58 | #include "llvm/InitializePasses.h" | ||||
59 | #include "llvm/Pass.h" | ||||
60 | #include "llvm/Target/TargetMachine.h" | ||||
61 | #include "llvm/Transforms/Utils/AssumeBundleBuilder.h" | ||||
62 | #include "llvm/Transforms/Utils/Local.h" | ||||
63 | |||||
64 | #include <map> | ||||
65 | |||||
66 | using namespace llvm; | ||||
67 | using namespace PatternMatch; | ||||
68 | |||||
69 | #define DEBUG_TYPE"lower-amx-type" "lower-amx-type" | ||||
70 | |||||
71 | static bool isAMXCast(Instruction *II) { | ||||
72 | return match(II, | ||||
73 | m_Intrinsic<Intrinsic::x86_cast_vector_to_tile>(m_Value())) || | ||||
74 | match(II, m_Intrinsic<Intrinsic::x86_cast_tile_to_vector>(m_Value())); | ||||
75 | } | ||||
76 | |||||
77 | static bool isAMXInstrinsic(User *I) { | ||||
78 | auto *II = dyn_cast<IntrinsicInst>(I); | ||||
79 | if (!II) | ||||
80 | return false; | ||||
81 | if (isAMXCast(II)) | ||||
82 | return false; | ||||
83 | // Check if return type or parameter is x86_amx. If it is x86_amx | ||||
84 | // the intrinsic must be x86 amx intrinsics. | ||||
85 | if (II->getType()->isX86_AMXTy()) | ||||
86 | return true; | ||||
87 | for (Value *V : II->args()) { | ||||
88 | if (V->getType()->isX86_AMXTy()) | ||||
89 | return true; | ||||
90 | } | ||||
91 | |||||
92 | return false; | ||||
93 | } | ||||
94 | |||||
95 | static AllocaInst *createAllocaInstAtEntry(IRBuilder<> &Builder, BasicBlock *BB, | ||||
96 | Type *Ty) { | ||||
97 | Function &F = *BB->getParent(); | ||||
98 | Module *M = BB->getModule(); | ||||
99 | const DataLayout &DL = M->getDataLayout(); | ||||
100 | |||||
101 | LLVMContext &Ctx = Builder.getContext(); | ||||
102 | auto AllocaAlignment = DL.getPrefTypeAlign(Type::getX86_AMXTy(Ctx)); | ||||
103 | unsigned AllocaAS = DL.getAllocaAddrSpace(); | ||||
104 | AllocaInst *AllocaRes = | ||||
105 | new AllocaInst(Ty, AllocaAS, "", &F.getEntryBlock().front()); | ||||
106 | AllocaRes->setAlignment(AllocaAlignment); | ||||
107 | return AllocaRes; | ||||
108 | } | ||||
109 | |||||
110 | static Instruction *getFirstNonAllocaInTheEntryBlock(Function &F) { | ||||
111 | for (Instruction &I : F.getEntryBlock()) | ||||
112 | if (!isa<AllocaInst>(&I)) | ||||
113 | return &I; | ||||
114 | llvm_unreachable("No terminator in the entry block!")::llvm::llvm_unreachable_internal("No terminator in the entry block!" , "llvm/lib/Target/X86/X86LowerAMXType.cpp", 114); | ||||
115 | } | ||||
116 | |||||
117 | static std::pair<Value *, Value *> getShape(IntrinsicInst *II, unsigned OpNo) { | ||||
118 | IRBuilder<> Builder(II); | ||||
119 | Value *Row = nullptr, *Col = nullptr; | ||||
120 | switch (II->getIntrinsicID()) { | ||||
121 | default: | ||||
122 | llvm_unreachable("Expect amx intrinsics")::llvm::llvm_unreachable_internal("Expect amx intrinsics", "llvm/lib/Target/X86/X86LowerAMXType.cpp" , 122); | ||||
123 | case Intrinsic::x86_tileloadd64_internal: | ||||
124 | case Intrinsic::x86_tileloaddt164_internal: | ||||
125 | case Intrinsic::x86_tilestored64_internal: { | ||||
126 | Row = II->getArgOperand(0); | ||||
127 | Col = II->getArgOperand(1); | ||||
128 | break; | ||||
129 | } | ||||
130 | // a * b + c | ||||
131 | // The shape depends on which operand. | ||||
132 | case Intrinsic::x86_tdpbssd_internal: | ||||
133 | case Intrinsic::x86_tdpbsud_internal: | ||||
134 | case Intrinsic::x86_tdpbusd_internal: | ||||
135 | case Intrinsic::x86_tdpbuud_internal: | ||||
136 | case Intrinsic::x86_tdpbf16ps_internal: { | ||||
137 | switch (OpNo) { | ||||
138 | case 3: | ||||
139 | Row = II->getArgOperand(0); | ||||
140 | Col = II->getArgOperand(1); | ||||
141 | break; | ||||
142 | case 4: | ||||
143 | Row = II->getArgOperand(0); | ||||
144 | Col = II->getArgOperand(2); | ||||
145 | break; | ||||
146 | case 5: | ||||
147 | if (isa<ConstantInt>(II->getArgOperand(2))) | ||||
148 | Row = Builder.getInt16( | ||||
149 | (cast<ConstantInt>(II->getOperand(2))->getSExtValue()) / 4); | ||||
150 | else if (isa<Instruction>(II->getArgOperand(2))) { | ||||
151 | // When it is not a const value and it is not a function argument, we | ||||
152 | // create Row after the definition of II->getOperand(2) instead of | ||||
153 | // before II. For example, II is %118, we try to getshape for %117: | ||||
154 | // %117 = call x86_amx @llvm.x86.cast.vector.to.tile.v256i32(<256 x | ||||
155 | // i32> %115). | ||||
156 | // %118 = call x86_amx @llvm.x86.tdpbf16ps.internal(i16 | ||||
157 | // %104, i16 %105, i16 %106, x86_amx %110, x86_amx %114, x86_amx | ||||
158 | // %117). | ||||
159 | // If we create %row = udiv i16 %106, 4 before %118(aka. II), then its | ||||
160 | // definition is after its user(new tileload for %117). | ||||
161 | // So, the best choice is to create %row right after the definition of | ||||
162 | // %106. | ||||
163 | Builder.SetInsertPoint(cast<Instruction>(II->getOperand(2))); | ||||
164 | Row = Builder.CreateUDiv(II->getOperand(2), Builder.getInt16(4)); | ||||
165 | cast<Instruction>(Row)->moveAfter(cast<Instruction>(II->getOperand(2))); | ||||
166 | } else { | ||||
167 | // When it is not a const value and it is a function argument, we create | ||||
168 | // Row at the entry bb. | ||||
169 | IRBuilder<> NewBuilder( | ||||
170 | getFirstNonAllocaInTheEntryBlock(*II->getFunction())); | ||||
171 | Row = NewBuilder.CreateUDiv(II->getOperand(2), NewBuilder.getInt16(4)); | ||||
172 | } | ||||
173 | Col = II->getArgOperand(1); | ||||
174 | break; | ||||
175 | } | ||||
176 | break; | ||||
177 | } | ||||
178 | } | ||||
179 | |||||
180 | return std::make_pair(Row, Col); | ||||
181 | } | ||||
182 | |||||
183 | static std::pair<Value *, Value *> getShape(PHINode *Phi) { | ||||
184 | Use &U = *(Phi->use_begin()); | ||||
185 | unsigned OpNo = U.getOperandNo(); | ||||
186 | User *V = U.getUser(); | ||||
187 | // TODO We don't traverse all users. To make the algorithm simple, here we | ||||
188 | // just traverse the first user. If we can find shape, then return the shape, | ||||
189 | // otherwise just return nullptr and the optimization for undef/zero will be | ||||
190 | // abandoned. | ||||
191 | while (V) { | ||||
192 | if (isAMXCast(dyn_cast<Instruction>(V))) { | ||||
193 | if (V->use_empty()) | ||||
194 | break; | ||||
195 | Use &U = *(V->use_begin()); | ||||
196 | OpNo = U.getOperandNo(); | ||||
197 | V = U.getUser(); | ||||
198 | } else if (isAMXInstrinsic(V)) { | ||||
199 | return getShape(cast<IntrinsicInst>(V), OpNo); | ||||
200 | } else if (isa<PHINode>(V)) { | ||||
201 | if (V->use_empty()) | ||||
202 | break; | ||||
203 | Use &U = *(V->use_begin()); | ||||
204 | V = U.getUser(); | ||||
205 | } else { | ||||
206 | break; | ||||
207 | } | ||||
208 | } | ||||
209 | |||||
210 | return std::make_pair(nullptr, nullptr); | ||||
211 | } | ||||
212 | |||||
213 | namespace { | ||||
214 | class X86LowerAMXType { | ||||
215 | Function &Func; | ||||
216 | |||||
217 | // In AMX intrinsics we let Shape = {Row, Col}, but the | ||||
218 | // RealCol = Col / ElementSize. We may use the RealCol | ||||
219 | // as a new Row for other new created AMX intrinsics. | ||||
220 | std::map<Value *, Value *> Col2Row; | ||||
221 | |||||
222 | public: | ||||
223 | X86LowerAMXType(Function &F) : Func(F) {} | ||||
224 | bool visit(); | ||||
225 | void combineLoadBitcast(LoadInst *LD, BitCastInst *Bitcast); | ||||
226 | void combineBitcastStore(BitCastInst *Bitcast, StoreInst *ST); | ||||
227 | bool transformBitcast(BitCastInst *Bitcast); | ||||
228 | }; | ||||
229 | |||||
230 | // %src = load <256 x i32>, <256 x i32>* %addr, align 64 | ||||
231 | // %2 = bitcast <256 x i32> %src to x86_amx | ||||
232 | // --> | ||||
233 | // %2 = call x86_amx @llvm.x86.tileloadd64.internal(i16 %row, i16 %col, | ||||
234 | // i8* %addr, i64 %stride64) | ||||
235 | void X86LowerAMXType::combineLoadBitcast(LoadInst *LD, BitCastInst *Bitcast) { | ||||
236 | Value *Row = nullptr, *Col = nullptr; | ||||
237 | Use &U = *(Bitcast->use_begin()); | ||||
238 | unsigned OpNo = U.getOperandNo(); | ||||
239 | auto *II = cast<IntrinsicInst>(U.getUser()); | ||||
240 | std::tie(Row, Col) = getShape(II, OpNo); | ||||
241 | IRBuilder<> Builder(Bitcast); | ||||
242 | // Use the maximun column as stride. | ||||
243 | Value *Stride = Builder.getInt64(64); | ||||
244 | Value *I8Ptr = | ||||
245 | Builder.CreateBitCast(LD->getOperand(0), Builder.getInt8PtrTy()); | ||||
246 | std::array<Value *, 4> Args = {Row, Col, I8Ptr, Stride}; | ||||
247 | |||||
248 | Value *NewInst = | ||||
249 | Builder.CreateIntrinsic(Intrinsic::x86_tileloadd64_internal, None, Args); | ||||
250 | Bitcast->replaceAllUsesWith(NewInst); | ||||
251 | } | ||||
252 | |||||
253 | // %src = call x86_amx @llvm.x86.tileloadd64.internal(%row, %col, %addr, | ||||
254 | // %stride); | ||||
255 | // %13 = bitcast x86_amx %src to <256 x i32> | ||||
256 | // store <256 x i32> %13, <256 x i32>* %addr, align 64 | ||||
257 | // --> | ||||
258 | // call void @llvm.x86.tilestored64.internal(%row, %col, %addr, | ||||
259 | // %stride64, %13) | ||||
260 | void X86LowerAMXType::combineBitcastStore(BitCastInst *Bitcast, StoreInst *ST) { | ||||
261 | |||||
262 | Value *Tile = Bitcast->getOperand(0); | ||||
263 | auto *II = cast<IntrinsicInst>(Tile); | ||||
264 | // Tile is output from AMX intrinsic. The first operand of the | ||||
265 | // intrinsic is row, the second operand of the intrinsic is column. | ||||
266 | Value *Row = II->getOperand(0); | ||||
267 | Value *Col = II->getOperand(1); | ||||
268 | IRBuilder<> Builder(ST); | ||||
269 | // Use the maximum column as stride. It must be the same with load | ||||
270 | // stride. | ||||
271 | Value *Stride = Builder.getInt64(64); | ||||
272 | Value *I8Ptr = | ||||
273 | Builder.CreateBitCast(ST->getOperand(1), Builder.getInt8PtrTy()); | ||||
274 | std::array<Value *, 5> Args = {Row, Col, I8Ptr, Stride, Tile}; | ||||
275 | Builder.CreateIntrinsic(Intrinsic::x86_tilestored64_internal, None, Args); | ||||
276 | if (Bitcast->hasOneUse()) | ||||
277 | return; | ||||
278 | // %13 = bitcast x86_amx %src to <256 x i32> | ||||
279 | // store <256 x i32> %13, <256 x i32>* %addr, align 64 | ||||
280 | // %add = <256 x i32> %13, <256 x i32> %src2 | ||||
281 | // --> | ||||
282 | // %13 = bitcast x86_amx %src to <256 x i32> | ||||
283 | // call void @llvm.x86.tilestored64.internal(%row, %col, %addr, | ||||
284 | // %stride64, %13) | ||||
285 | // %14 = load <256 x i32>, %addr | ||||
286 | // %add = <256 x i32> %14, <256 x i32> %src2 | ||||
287 | Value *Vec = Builder.CreateLoad(Bitcast->getType(), ST->getOperand(1)); | ||||
288 | Bitcast->replaceAllUsesWith(Vec); | ||||
289 | } | ||||
290 | |||||
291 | // transform bitcast to <store, load> instructions. | ||||
292 | bool X86LowerAMXType::transformBitcast(BitCastInst *Bitcast) { | ||||
293 | IRBuilder<> Builder(Bitcast); | ||||
294 | AllocaInst *AllocaAddr; | ||||
295 | Value *I8Ptr, *Stride; | ||||
296 | auto *Src = Bitcast->getOperand(0); | ||||
297 | |||||
298 | auto Prepare = [&](Type *MemTy) { | ||||
299 | AllocaAddr = createAllocaInstAtEntry(Builder, Bitcast->getParent(), MemTy); | ||||
300 | I8Ptr = Builder.CreateBitCast(AllocaAddr, Builder.getInt8PtrTy()); | ||||
301 | Stride = Builder.getInt64(64); | ||||
302 | }; | ||||
303 | |||||
304 | if (Bitcast->getType()->isX86_AMXTy()) { | ||||
305 | // %2 = bitcast <256 x i32> %src to x86_amx | ||||
306 | // --> | ||||
307 | // %addr = alloca <256 x i32>, align 64 | ||||
308 | // store <256 x i32> %src, <256 x i32>* %addr, align 64 | ||||
309 | // %addr2 = bitcast <256 x i32>* to i8* | ||||
310 | // %2 = call x86_amx @llvm.x86.tileloadd64.internal(i16 %row, i16 %col, | ||||
311 | // i8* %addr2, | ||||
312 | // i64 64) | ||||
313 | Use &U = *(Bitcast->use_begin()); | ||||
314 | unsigned OpNo = U.getOperandNo(); | ||||
315 | auto *II = dyn_cast<IntrinsicInst>(U.getUser()); | ||||
316 | if (!II) | ||||
317 | return false; // May be bitcast from x86amx to <256 x i32>. | ||||
318 | Prepare(Bitcast->getOperand(0)->getType()); | ||||
319 | Builder.CreateStore(Src, AllocaAddr); | ||||
320 | // TODO we can pick an constant operand for the shape. | ||||
321 | Value *Row = nullptr, *Col = nullptr; | ||||
322 | std::tie(Row, Col) = getShape(II, OpNo); | ||||
323 | std::array<Value *, 4> Args = {Row, Col, I8Ptr, Stride}; | ||||
324 | Value *NewInst = Builder.CreateIntrinsic( | ||||
325 | Intrinsic::x86_tileloadd64_internal, None, Args); | ||||
326 | Bitcast->replaceAllUsesWith(NewInst); | ||||
327 | } else { | ||||
328 | // %2 = bitcast x86_amx %src to <256 x i32> | ||||
329 | // --> | ||||
330 | // %addr = alloca <256 x i32>, align 64 | ||||
331 | // %addr2 = bitcast <256 x i32>* to i8* | ||||
332 | // call void @llvm.x86.tilestored64.internal(i16 %row, i16 %col, | ||||
333 | // i8* %addr2, i64 %stride) | ||||
334 | // %2 = load <256 x i32>, <256 x i32>* %addr, align 64 | ||||
335 | auto *II = dyn_cast<IntrinsicInst>(Src); | ||||
336 | if (!II) | ||||
337 | return false; // May be bitcast from <256 x i32> to x86amx. | ||||
338 | Prepare(Bitcast->getType()); | ||||
339 | Value *Row = II->getOperand(0); | ||||
340 | Value *Col = II->getOperand(1); | ||||
341 | std::array<Value *, 5> Args = {Row, Col, I8Ptr, Stride, Src}; | ||||
342 | Builder.CreateIntrinsic(Intrinsic::x86_tilestored64_internal, None, Args); | ||||
343 | Value *NewInst = Builder.CreateLoad(Bitcast->getType(), AllocaAddr); | ||||
344 | Bitcast->replaceAllUsesWith(NewInst); | ||||
345 | } | ||||
346 | |||||
347 | return true; | ||||
348 | } | ||||
349 | |||||
350 | bool X86LowerAMXType::visit() { | ||||
351 | SmallVector<Instruction *, 8> DeadInsts; | ||||
352 | Col2Row.clear(); | ||||
353 | |||||
354 | for (BasicBlock *BB : post_order(&Func)) { | ||||
355 | for (Instruction &Inst : llvm::make_early_inc_range(llvm::reverse(*BB))) { | ||||
356 | auto *Bitcast = dyn_cast<BitCastInst>(&Inst); | ||||
357 | if (!Bitcast) | ||||
358 | continue; | ||||
359 | |||||
360 | Value *Src = Bitcast->getOperand(0); | ||||
361 | if (Bitcast->getType()->isX86_AMXTy()) { | ||||
362 | if (Bitcast->user_empty()) { | ||||
363 | DeadInsts.push_back(Bitcast); | ||||
364 | continue; | ||||
365 | } | ||||
366 | LoadInst *LD = dyn_cast<LoadInst>(Src); | ||||
367 | if (!LD) { | ||||
368 | if (transformBitcast(Bitcast)) | ||||
369 | DeadInsts.push_back(Bitcast); | ||||
370 | continue; | ||||
371 | } | ||||
372 | // If load has mutli-user, duplicate a vector load. | ||||
373 | // %src = load <256 x i32>, <256 x i32>* %addr, align 64 | ||||
374 | // %2 = bitcast <256 x i32> %src to x86_amx | ||||
375 | // %add = add <256 x i32> %src, <256 x i32> %src2 | ||||
376 | // --> | ||||
377 | // %src = load <256 x i32>, <256 x i32>* %addr, align 64 | ||||
378 | // %2 = call x86_amx @llvm.x86.tileloadd64.internal(i16 %row, i16 %col, | ||||
379 | // i8* %addr, i64 %stride64) | ||||
380 | // %add = add <256 x i32> %src, <256 x i32> %src2 | ||||
381 | |||||
382 | // If load has one user, the load will be eliminated in DAG ISel. | ||||
383 | // %src = load <256 x i32>, <256 x i32>* %addr, align 64 | ||||
384 | // %2 = bitcast <256 x i32> %src to x86_amx | ||||
385 | // --> | ||||
386 | // %2 = call x86_amx @llvm.x86.tileloadd64.internal(i16 %row, i16 %col, | ||||
387 | // i8* %addr, i64 %stride64) | ||||
388 | combineLoadBitcast(LD, Bitcast); | ||||
389 | DeadInsts.push_back(Bitcast); | ||||
390 | if (LD->hasOneUse()) | ||||
391 | DeadInsts.push_back(LD); | ||||
392 | } else if (Src->getType()->isX86_AMXTy()) { | ||||
393 | if (Bitcast->user_empty()) { | ||||
394 | DeadInsts.push_back(Bitcast); | ||||
395 | continue; | ||||
396 | } | ||||
397 | StoreInst *ST = nullptr; | ||||
398 | for (Use &U : Bitcast->uses()) { | ||||
399 | ST = dyn_cast<StoreInst>(U.getUser()); | ||||
400 | if (ST) | ||||
401 | break; | ||||
402 | } | ||||
403 | if (!ST) { | ||||
404 | if (transformBitcast(Bitcast)) | ||||
405 | DeadInsts.push_back(Bitcast); | ||||
406 | continue; | ||||
407 | } | ||||
408 | // If bitcast (%13) has one use, combine bitcast and store to amx store. | ||||
409 | // %src = call x86_amx @llvm.x86.tileloadd64.internal(%row, %col, %addr, | ||||
410 | // %stride); | ||||
411 | // %13 = bitcast x86_amx %src to <256 x i32> | ||||
412 | // store <256 x i32> %13, <256 x i32>* %addr, align 64 | ||||
413 | // --> | ||||
414 | // call void @llvm.x86.tilestored64.internal(%row, %col, %addr, | ||||
415 | // %stride64, %13) | ||||
416 | // | ||||
417 | // If bitcast (%13) has multi-use, transform as below. | ||||
418 | // %13 = bitcast x86_amx %src to <256 x i32> | ||||
419 | // store <256 x i32> %13, <256 x i32>* %addr, align 64 | ||||
420 | // %add = <256 x i32> %13, <256 x i32> %src2 | ||||
421 | // --> | ||||
422 | // %13 = bitcast x86_amx %src to <256 x i32> | ||||
423 | // call void @llvm.x86.tilestored64.internal(%row, %col, %addr, | ||||
424 | // %stride64, %13) | ||||
425 | // %14 = load <256 x i32>, %addr | ||||
426 | // %add = <256 x i32> %14, <256 x i32> %src2 | ||||
427 | // | ||||
428 | combineBitcastStore(Bitcast, ST); | ||||
429 | // Delete user first. | ||||
430 | DeadInsts.push_back(ST); | ||||
431 | DeadInsts.push_back(Bitcast); | ||||
432 | } | ||||
433 | } | ||||
434 | } | ||||
435 | |||||
436 | bool C = !DeadInsts.empty(); | ||||
437 | |||||
438 | for (auto *Inst : DeadInsts) | ||||
439 | Inst->eraseFromParent(); | ||||
440 | |||||
441 | return C; | ||||
442 | } | ||||
443 | } // anonymous namespace | ||||
444 | |||||
445 | static Value *getAllocaPos(BasicBlock *BB) { | ||||
446 | Module *M = BB->getModule(); | ||||
447 | Function *F = BB->getParent(); | ||||
448 | IRBuilder<> Builder(&F->getEntryBlock().front()); | ||||
449 | const DataLayout &DL = M->getDataLayout(); | ||||
450 | unsigned AllocaAS = DL.getAllocaAddrSpace(); | ||||
451 | Type *V256I32Ty = VectorType::get(Builder.getInt32Ty(), 256, false); | ||||
452 | AllocaInst *AllocaRes = | ||||
453 | new AllocaInst(V256I32Ty, AllocaAS, "", &F->getEntryBlock().front()); | ||||
454 | BasicBlock::iterator Iter = AllocaRes->getIterator(); | ||||
455 | ++Iter; | ||||
456 | Builder.SetInsertPoint(&*Iter); | ||||
457 | Value *I8Ptr = Builder.CreateBitCast(AllocaRes, Builder.getInt8PtrTy()); | ||||
458 | return I8Ptr; | ||||
459 | } | ||||
460 | |||||
461 | static Instruction *createTileStore(Instruction *TileDef, Value *Ptr) { | ||||
462 | assert(TileDef->getType()->isX86_AMXTy() && "Not define tile!")(static_cast <bool> (TileDef->getType()->isX86_AMXTy () && "Not define tile!") ? void (0) : __assert_fail ( "TileDef->getType()->isX86_AMXTy() && \"Not define tile!\"" , "llvm/lib/Target/X86/X86LowerAMXType.cpp", 462, __extension__ __PRETTY_FUNCTION__)); | ||||
463 | auto *II = cast<IntrinsicInst>(TileDef); | ||||
464 | assert(II && "Not tile intrinsic!")(static_cast <bool> (II && "Not tile intrinsic!" ) ? void (0) : __assert_fail ("II && \"Not tile intrinsic!\"" , "llvm/lib/Target/X86/X86LowerAMXType.cpp", 464, __extension__ __PRETTY_FUNCTION__)); | ||||
465 | Value *Row = II->getOperand(0); | ||||
466 | Value *Col = II->getOperand(1); | ||||
467 | |||||
468 | BasicBlock *BB = TileDef->getParent(); | ||||
469 | BasicBlock::iterator Iter = TileDef->getIterator(); | ||||
470 | IRBuilder<> Builder(BB, ++Iter); | ||||
471 | Value *Stride = Builder.getInt64(64); | ||||
472 | std::array<Value *, 5> Args = {Row, Col, Ptr, Stride, TileDef}; | ||||
473 | |||||
474 | Instruction *TileStore = | ||||
475 | Builder.CreateIntrinsic(Intrinsic::x86_tilestored64_internal, None, Args); | ||||
476 | return TileStore; | ||||
477 | } | ||||
478 | |||||
479 | static void replaceWithTileLoad(Use &U, Value *Ptr, bool IsPHI = false) { | ||||
480 | Value *V = U.get(); | ||||
481 | assert(V->getType()->isX86_AMXTy() && "Not define tile!")(static_cast <bool> (V->getType()->isX86_AMXTy() && "Not define tile!") ? void (0) : __assert_fail ("V->getType()->isX86_AMXTy() && \"Not define tile!\"" , "llvm/lib/Target/X86/X86LowerAMXType.cpp", 481, __extension__ __PRETTY_FUNCTION__)); | ||||
482 | |||||
483 | // Get tile shape. | ||||
484 | IntrinsicInst *II = nullptr; | ||||
485 | if (IsPHI
| ||||
486 | Value *PhiOp = dyn_cast<PHINode>(V)->getIncomingValue(0); | ||||
487 | II = cast<IntrinsicInst>(PhiOp); | ||||
488 | } else { | ||||
489 | II = cast<IntrinsicInst>(V); | ||||
490 | } | ||||
491 | Value *Row = II->getOperand(0); | ||||
492 | Value *Col = II->getOperand(1); | ||||
493 | |||||
494 | Instruction *UserI = dyn_cast<Instruction>(U.getUser()); | ||||
495 | IRBuilder<> Builder(UserI); | ||||
496 | Value *Stride = Builder.getInt64(64); | ||||
497 | std::array<Value *, 4> Args = {Row, Col, Ptr, Stride}; | ||||
498 | |||||
499 | Value *TileLoad = | ||||
500 | Builder.CreateIntrinsic(Intrinsic::x86_tileloadd64_internal, None, Args); | ||||
501 | UserI->replaceUsesOfWith(V, TileLoad); | ||||
502 | } | ||||
503 | |||||
504 | static bool isIncomingOfPHI(Instruction *I) { | ||||
505 | for (Use &U : I->uses()) { | ||||
506 | User *V = U.getUser(); | ||||
507 | if (isa<PHINode>(V)) | ||||
508 | return true; | ||||
509 | } | ||||
510 | return false; | ||||
511 | } | ||||
512 | |||||
513 | // Let all AMX tile data become volatile data, shorten the life range | ||||
514 | // of each tile register before fast register allocation. | ||||
515 | namespace { | ||||
516 | class X86VolatileTileData { | ||||
517 | Function &F; | ||||
518 | |||||
519 | public: | ||||
520 | X86VolatileTileData(Function &Func) : F(Func) {} | ||||
521 | Value *updatePhiIncomings(BasicBlock *BB, | ||||
522 | SmallVector<Instruction *, 2> &Incomings); | ||||
523 | void replacePhiDefWithLoad(Instruction *PHI, Value *StorePtr); | ||||
524 | bool volatileTileData(); | ||||
525 | void volatileTilePHI(PHINode *Inst); | ||||
526 | void volatileTileNonPHI(Instruction *I); | ||||
527 | }; | ||||
528 | |||||
529 | Value *X86VolatileTileData::updatePhiIncomings( | ||||
530 | BasicBlock *BB, SmallVector<Instruction *, 2> &Incomings) { | ||||
531 | Value *I8Ptr = getAllocaPos(BB); | ||||
532 | |||||
533 | for (auto *I : Incomings) { | ||||
534 | User *Store = createTileStore(I, I8Ptr); | ||||
535 | |||||
536 | // All its uses (except phi) should load from stored mem. | ||||
537 | for (Use &U : I->uses()) { | ||||
538 | User *V = U.getUser(); | ||||
539 | if (isa<PHINode>(V) || V == Store) | ||||
540 | continue; | ||||
541 | replaceWithTileLoad(U, I8Ptr); | ||||
542 | } | ||||
543 | } | ||||
544 | return I8Ptr; | ||||
545 | } | ||||
546 | |||||
547 | void X86VolatileTileData::replacePhiDefWithLoad(Instruction *PHI, | ||||
548 | Value *StorePtr) { | ||||
549 | for (Use &U : PHI->uses()) | ||||
550 | replaceWithTileLoad(U, StorePtr, true); | ||||
551 | PHI->eraseFromParent(); | ||||
552 | } | ||||
553 | |||||
554 | // Smilar with volatileTileNonPHI, this function only handle PHI Nodes | ||||
555 | // and their related AMX intrinsics. | ||||
556 | // 1) PHI Def should change to tileload. | ||||
557 | // 2) PHI Incoming Values should tilestored in just after their def. | ||||
558 | // 3) The mem of these tileload and tilestores should be same. | ||||
559 | // e.g. | ||||
560 | // ------------------------------------------------------ | ||||
561 | // bb_dom: | ||||
562 | // ... | ||||
563 | // br i1 %bool.cond, label %if.else, label %if.then | ||||
564 | // | ||||
565 | // if.then: | ||||
566 | // def %t0 = ... | ||||
567 | // ... | ||||
568 | // use %t0 | ||||
569 | // ... | ||||
570 | // br label %if.end | ||||
571 | // | ||||
572 | // if.else: | ||||
573 | // def %t1 = ... | ||||
574 | // br label %if.end | ||||
575 | // | ||||
576 | // if.end: | ||||
577 | // %td = phi x86_amx [ %t1, %if.else ], [ %t0, %if.then ] | ||||
578 | // ... | ||||
579 | // use %td | ||||
580 | // ------------------------------------------------------ | ||||
581 | // --> | ||||
582 | // ------------------------------------------------------ | ||||
583 | // bb_entry: | ||||
584 | // %mem = alloca <256 x i32>, align 1024 * | ||||
585 | // ... | ||||
586 | // bb_dom: | ||||
587 | // ... | ||||
588 | // br i1 %bool.cond, label %if.else, label %if.then | ||||
589 | // | ||||
590 | // if.then: | ||||
591 | // def %t0 = ... | ||||
592 | // call void @llvm.x86.tilestored64.internal(mem, %t0) * | ||||
593 | // ... | ||||
594 | // %t0` = call x86_amx @llvm.x86.tileloadd64.internal(mem)* | ||||
595 | // use %t0` * | ||||
596 | // ... | ||||
597 | // br label %if.end | ||||
598 | // | ||||
599 | // if.else: | ||||
600 | // def %t1 = ... | ||||
601 | // call void @llvm.x86.tilestored64.internal(mem, %t1) * | ||||
602 | // br label %if.end | ||||
603 | // | ||||
604 | // if.end: | ||||
605 | // ... | ||||
606 | // %td = call x86_amx @llvm.x86.tileloadd64.internal(mem) * | ||||
607 | // use %td | ||||
608 | // ------------------------------------------------------ | ||||
609 | void X86VolatileTileData::volatileTilePHI(PHINode *PHI) { | ||||
610 | BasicBlock *BB = PHI->getParent(); | ||||
611 | SmallVector<Instruction *, 2> Incomings; | ||||
612 | |||||
613 | for (unsigned I = 0, E = PHI->getNumIncomingValues(); I != E; ++I) { | ||||
614 | Value *Op = PHI->getIncomingValue(I); | ||||
615 | Instruction *Inst = dyn_cast<Instruction>(Op); | ||||
616 | assert(Inst && "We shouldn't fold AMX instrution!")(static_cast <bool> (Inst && "We shouldn't fold AMX instrution!" ) ? void (0) : __assert_fail ("Inst && \"We shouldn't fold AMX instrution!\"" , "llvm/lib/Target/X86/X86LowerAMXType.cpp", 616, __extension__ __PRETTY_FUNCTION__)); | ||||
617 | Incomings.push_back(Inst); | ||||
618 | } | ||||
619 | |||||
620 | Value *StorePtr = updatePhiIncomings(BB, Incomings); | ||||
621 | replacePhiDefWithLoad(PHI, StorePtr); | ||||
622 | } | ||||
623 | |||||
624 | // Store the defined tile and load it before use. | ||||
625 | // All its users are not PHI. | ||||
626 | // e.g. | ||||
627 | // ------------------------------------------------------ | ||||
628 | // def %td = ... | ||||
629 | // ... | ||||
630 | // "use %td" | ||||
631 | // ------------------------------------------------------ | ||||
632 | // --> | ||||
633 | // ------------------------------------------------------ | ||||
634 | // def %td = ... | ||||
635 | // call void @llvm.x86.tilestored64.internal(mem, %td) | ||||
636 | // ... | ||||
637 | // %td2 = call x86_amx @llvm.x86.tileloadd64.internal(mem) | ||||
638 | // "use %td2" | ||||
639 | // ------------------------------------------------------ | ||||
640 | void X86VolatileTileData::volatileTileNonPHI(Instruction *I) { | ||||
641 | BasicBlock *BB = I->getParent(); | ||||
642 | Value *I8Ptr = getAllocaPos(BB); | ||||
643 | User *Store = createTileStore(I, I8Ptr); | ||||
644 | |||||
645 | // All its uses should load from stored mem. | ||||
646 | for (Use &U : I->uses()) { | ||||
647 | User *V = U.getUser(); | ||||
648 | assert(!isa<PHINode>(V) && "PHI Nodes should be excluded!")(static_cast <bool> (!isa<PHINode>(V) && "PHI Nodes should be excluded!" ) ? void (0) : __assert_fail ("!isa<PHINode>(V) && \"PHI Nodes should be excluded!\"" , "llvm/lib/Target/X86/X86LowerAMXType.cpp", 648, __extension__ __PRETTY_FUNCTION__)); | ||||
649 | if (V != Store) | ||||
650 | replaceWithTileLoad(U, I8Ptr); | ||||
651 | } | ||||
652 | } | ||||
653 | |||||
654 | // Volatile Tile Model: | ||||
655 | // 1) All the uses of tile data comes from tileload in time. | ||||
656 | // 2) All the defs of tile data tilestore into mem immediately. | ||||
657 | // For example: | ||||
658 | // -------------------------------------------------------------------------- | ||||
659 | // %t1 = call x86_amx @llvm.x86.tileloadd64.internal(m, k, ...) key | ||||
660 | // %t2 = call x86_amx @llvm.x86.tileloadd64.internal(k, n, ...) | ||||
661 | // %t3 = call x86_amx @llvm.x86.tileloadd64.internal(m, n, ...) amx | ||||
662 | // %td = tail call x86_amx @llvm.x86.tdpbssd.internal(m, n, k, t1, t2, t3) | ||||
663 | // call void @llvm.x86.tilestored64.internal(... td) area | ||||
664 | // -------------------------------------------------------------------------- | ||||
665 | // 3) No terminator, call or other amx instructions in the key amx area. | ||||
666 | bool X86VolatileTileData::volatileTileData() { | ||||
667 | bool Changed = false; | ||||
668 | for (BasicBlock &BB : F) { | ||||
669 | SmallVector<Instruction *, 2> PHIInsts; | ||||
670 | SmallVector<Instruction *, 8> AMXDefInsts; | ||||
671 | |||||
672 | for (Instruction &I : BB) { | ||||
673 | if (!I.getType()->isX86_AMXTy()) | ||||
674 | continue; | ||||
675 | if (isa<PHINode>(&I)) | ||||
676 | PHIInsts.push_back(&I); | ||||
677 | else | ||||
678 | AMXDefInsts.push_back(&I); | ||||
679 | } | ||||
680 | |||||
681 | // First we "volatile" the non-phi related amx intrinsics. | ||||
682 | for (Instruction *I : AMXDefInsts) { | ||||
683 | if (isIncomingOfPHI(I)) | ||||
684 | continue; | ||||
685 | volatileTileNonPHI(I); | ||||
686 | Changed = true; | ||||
687 | } | ||||
688 | |||||
689 | for (Instruction *I : PHIInsts) { | ||||
690 | volatileTilePHI(dyn_cast<PHINode>(I)); | ||||
691 | Changed = true; | ||||
692 | } | ||||
693 | } | ||||
694 | return Changed; | ||||
695 | } | ||||
696 | |||||
697 | } // anonymous namespace | ||||
698 | |||||
699 | namespace { | ||||
700 | |||||
701 | class X86LowerAMXCast { | ||||
702 | Function &Func; | ||||
703 | |||||
704 | public: | ||||
705 | X86LowerAMXCast(Function &F) : Func(F) {} | ||||
706 | bool combineAMXcast(TargetLibraryInfo *TLI); | ||||
707 | bool transformAMXCast(IntrinsicInst *AMXCast); | ||||
708 | bool transformAllAMXCast(); | ||||
709 | bool optimizeAMXCastFromPhi(IntrinsicInst *CI, PHINode *PN, | ||||
710 | SmallSetVector<Instruction *, 16> &DeadInst); | ||||
711 | }; | ||||
712 | |||||
713 | static bool DCEInstruction(Instruction *I, | ||||
714 | SmallSetVector<Instruction *, 16> &WorkList, | ||||
715 | const TargetLibraryInfo *TLI) { | ||||
716 | if (isInstructionTriviallyDead(I, TLI)) { | ||||
717 | salvageDebugInfo(*I); | ||||
718 | salvageKnowledge(I); | ||||
719 | |||||
720 | // Null out all of the instruction's operands to see if any operand becomes | ||||
721 | // dead as we go. | ||||
722 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { | ||||
723 | Value *OpV = I->getOperand(i); | ||||
724 | I->setOperand(i, nullptr); | ||||
725 | |||||
726 | if (!OpV->use_empty() || I == OpV) | ||||
727 | continue; | ||||
728 | |||||
729 | // If the operand is an instruction that became dead as we nulled out the | ||||
730 | // operand, and if it is 'trivially' dead, delete it in a future loop | ||||
731 | // iteration. | ||||
732 | if (Instruction *OpI = dyn_cast<Instruction>(OpV)) { | ||||
733 | if (isInstructionTriviallyDead(OpI, TLI)) { | ||||
734 | WorkList.insert(OpI); | ||||
735 | } | ||||
736 | } | ||||
737 | } | ||||
738 | I->eraseFromParent(); | ||||
739 | return true; | ||||
740 | } | ||||
741 | return false; | ||||
742 | } | ||||
743 | |||||
744 | /// This function handles following case | ||||
745 | /// | ||||
746 | /// A -> B amxcast | ||||
747 | /// PHI | ||||
748 | /// B -> A amxcast | ||||
749 | /// | ||||
750 | /// All the related PHI nodes can be replaced by new PHI nodes with type A. | ||||
751 | /// The uses of \p CI can be changed to the new PHI node corresponding to \p PN. | ||||
752 | bool X86LowerAMXCast::optimizeAMXCastFromPhi( | ||||
753 | IntrinsicInst *CI, PHINode *PN, | ||||
754 | SmallSetVector<Instruction *, 16> &DeadInst) { | ||||
755 | IRBuilder<> Builder(CI); | ||||
756 | Value *Src = CI->getOperand(0); | ||||
757 | Type *SrcTy = Src->getType(); // Type B | ||||
758 | Type *DestTy = CI->getType(); // Type A | ||||
759 | |||||
760 | SmallVector<PHINode *, 4> PhiWorklist; | ||||
761 | SmallSetVector<PHINode *, 4> OldPhiNodes; | ||||
762 | |||||
763 | // Find all of the A->B casts and PHI nodes. | ||||
764 | // We need to inspect all related PHI nodes, but PHIs can be cyclic, so | ||||
765 | // OldPhiNodes is used to track all known PHI nodes, before adding a new | ||||
766 | // PHI to PhiWorklist, it is checked against and added to OldPhiNodes first. | ||||
767 | PhiWorklist.push_back(PN); | ||||
768 | OldPhiNodes.insert(PN); | ||||
769 | while (!PhiWorklist.empty()) { | ||||
770 | auto *OldPN = PhiWorklist.pop_back_val(); | ||||
771 | for (unsigned I = 0; I < OldPN->getNumOperands(); ++I) { | ||||
772 | Value *IncValue = OldPN->getIncomingValue(I); | ||||
773 | // TODO: currently, We ignore cases where it is a const. In the future, we | ||||
774 | // might support const. | ||||
775 | if (isa<Constant>(IncValue)) { | ||||
776 | auto *IncConst = dyn_cast<Constant>(IncValue); | ||||
777 | if (!isa<UndefValue>(IncValue) && !IncConst->isZeroValue()) | ||||
778 | return false; | ||||
779 | Value *Row = nullptr, *Col = nullptr; | ||||
780 | std::tie(Row, Col) = getShape(OldPN); | ||||
781 | // TODO: If it is not constant the Row and Col must domoniate tilezero | ||||
782 | // that we are going to create. | ||||
783 | if (!Row || !Col || !isa<Constant>(Row) || !isa<Constant>(Col)) | ||||
784 | return false; | ||||
785 | // Create tilezero at the end of incoming block. | ||||
786 | auto *Block = OldPN->getIncomingBlock(I); | ||||
787 | BasicBlock::iterator Iter = Block->getTerminator()->getIterator(); | ||||
788 | Instruction *NewInst = Builder.CreateIntrinsic( | ||||
789 | Intrinsic::x86_tilezero_internal, None, {Row, Col}); | ||||
790 | NewInst->moveBefore(&*Iter); | ||||
791 | NewInst = Builder.CreateIntrinsic(Intrinsic::x86_cast_tile_to_vector, | ||||
792 | {IncValue->getType()}, {NewInst}); | ||||
793 | NewInst->moveBefore(&*Iter); | ||||
794 | // Replace InValue with new Value. | ||||
795 | OldPN->setIncomingValue(I, NewInst); | ||||
796 | IncValue = NewInst; | ||||
797 | } | ||||
798 | |||||
799 | if (auto *PNode = dyn_cast<PHINode>(IncValue)) { | ||||
800 | if (OldPhiNodes.insert(PNode)) | ||||
801 | PhiWorklist.push_back(PNode); | ||||
802 | continue; | ||||
803 | } | ||||
804 | Instruction *ACI = dyn_cast<Instruction>(IncValue); | ||||
805 | if (ACI && isAMXCast(ACI)) { | ||||
806 | // Verify it's a A->B cast. | ||||
807 | Type *TyA = ACI->getOperand(0)->getType(); | ||||
808 | Type *TyB = ACI->getType(); | ||||
809 | if (TyA != DestTy || TyB != SrcTy) | ||||
810 | return false; | ||||
811 | continue; | ||||
812 | } | ||||
813 | return false; | ||||
814 | } | ||||
815 | } | ||||
816 | |||||
817 | // Check that each user of each old PHI node is something that we can | ||||
818 | // rewrite, so that all of the old PHI nodes can be cleaned up afterwards. | ||||
819 | for (auto *OldPN : OldPhiNodes) { | ||||
820 | for (User *V : OldPN->users()) { | ||||
821 | Instruction *ACI = dyn_cast<Instruction>(V); | ||||
822 | if (ACI && isAMXCast(ACI)) { | ||||
823 | // Verify it's a B->A cast. | ||||
824 | Type *TyB = ACI->getOperand(0)->getType(); | ||||
825 | Type *TyA = ACI->getType(); | ||||
826 | if (TyA != DestTy || TyB != SrcTy) | ||||
827 | return false; | ||||
828 | } else if (auto *PHI = dyn_cast<PHINode>(V)) { | ||||
829 | // As long as the user is another old PHI node, then even if we don't | ||||
830 | // rewrite it, the PHI web we're considering won't have any users | ||||
831 | // outside itself, so it'll be dead. | ||||
832 | // example: | ||||
833 | // bb.0: | ||||
834 | // %0 = amxcast ... | ||||
835 | // bb.1: | ||||
836 | // %1 = amxcast ... | ||||
837 | // bb.2: | ||||
838 | // %goodphi = phi %0, %1 | ||||
839 | // %3 = amxcast %goodphi | ||||
840 | // bb.3: | ||||
841 | // %goodphi2 = phi %0, %goodphi | ||||
842 | // %4 = amxcast %goodphi2 | ||||
843 | // When optimizeAMXCastFromPhi process %3 and %goodphi, %goodphi2 is | ||||
844 | // outside the phi-web, so the combination stop When | ||||
845 | // optimizeAMXCastFromPhi process %4 and %goodphi2, the optimization | ||||
846 | // will be done. | ||||
847 | if (OldPhiNodes.count(PHI) == 0) | ||||
848 | return false; | ||||
849 | } else | ||||
850 | return false; | ||||
851 | } | ||||
852 | } | ||||
853 | |||||
854 | // For each old PHI node, create a corresponding new PHI node with a type A. | ||||
855 | SmallDenseMap<PHINode *, PHINode *> NewPNodes; | ||||
856 | for (auto *OldPN : OldPhiNodes) { | ||||
857 | Builder.SetInsertPoint(OldPN); | ||||
858 | PHINode *NewPN = Builder.CreatePHI(DestTy, OldPN->getNumOperands()); | ||||
859 | NewPNodes[OldPN] = NewPN; | ||||
860 | } | ||||
861 | |||||
862 | // Fill in the operands of new PHI nodes. | ||||
863 | for (auto *OldPN : OldPhiNodes) { | ||||
864 | PHINode *NewPN = NewPNodes[OldPN]; | ||||
865 | for (unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) { | ||||
866 | Value *V = OldPN->getOperand(j); | ||||
867 | Value *NewV = nullptr; | ||||
868 | Instruction *ACI = dyn_cast<Instruction>(V); | ||||
869 | // There should not be a AMXcast from a const. | ||||
870 | if (ACI && isAMXCast(ACI)) | ||||
871 | NewV = ACI->getOperand(0); | ||||
872 | else if (auto *PrevPN = dyn_cast<PHINode>(V)) | ||||
873 | NewV = NewPNodes[PrevPN]; | ||||
874 | assert(NewV)(static_cast <bool> (NewV) ? void (0) : __assert_fail ( "NewV", "llvm/lib/Target/X86/X86LowerAMXType.cpp", 874, __extension__ __PRETTY_FUNCTION__)); | ||||
875 | NewPN->addIncoming(NewV, OldPN->getIncomingBlock(j)); | ||||
876 | } | ||||
877 | } | ||||
878 | |||||
879 | // Traverse all accumulated PHI nodes and process its users, | ||||
880 | // which are Stores and BitcCasts. Without this processing | ||||
881 | // NewPHI nodes could be replicated and could lead to extra | ||||
882 | // moves generated after DeSSA. | ||||
883 | // If there is a store with type B, change it to type A. | ||||
884 | |||||
885 | // Replace users of BitCast B->A with NewPHI. These will help | ||||
886 | // later to get rid of a closure formed by OldPHI nodes. | ||||
887 | for (auto *OldPN : OldPhiNodes) { | ||||
888 | PHINode *NewPN = NewPNodes[OldPN]; | ||||
889 | for (User *V : make_early_inc_range(OldPN->users())) { | ||||
890 | Instruction *ACI = dyn_cast<Instruction>(V); | ||||
891 | if (ACI && isAMXCast(ACI)) { | ||||
892 | Type *TyB = ACI->getOperand(0)->getType(); | ||||
893 | Type *TyA = ACI->getType(); | ||||
894 | assert(TyA == DestTy && TyB == SrcTy)(static_cast <bool> (TyA == DestTy && TyB == SrcTy ) ? void (0) : __assert_fail ("TyA == DestTy && TyB == SrcTy" , "llvm/lib/Target/X86/X86LowerAMXType.cpp", 894, __extension__ __PRETTY_FUNCTION__)); | ||||
895 | (void)TyA; | ||||
896 | (void)TyB; | ||||
897 | ACI->replaceAllUsesWith(NewPN); | ||||
898 | DeadInst.insert(ACI); | ||||
899 | } else if (auto *PHI = dyn_cast<PHINode>(V)) { | ||||
900 | // We don't need to push PHINode into DeadInst since they are operands | ||||
901 | // of rootPN DCE can safely delete rootPN's operands if rootPN is dead. | ||||
902 | assert(OldPhiNodes.contains(PHI))(static_cast <bool> (OldPhiNodes.contains(PHI)) ? void ( 0) : __assert_fail ("OldPhiNodes.contains(PHI)", "llvm/lib/Target/X86/X86LowerAMXType.cpp" , 902, __extension__ __PRETTY_FUNCTION__)); | ||||
903 | (void)PHI; | ||||
904 | } else | ||||
905 | llvm_unreachable("all uses should be handled")::llvm::llvm_unreachable_internal("all uses should be handled" , "llvm/lib/Target/X86/X86LowerAMXType.cpp", 905); | ||||
906 | } | ||||
907 | } | ||||
908 | return true; | ||||
909 | } | ||||
910 | |||||
911 | bool X86LowerAMXCast::combineAMXcast(TargetLibraryInfo *TLI) { | ||||
912 | bool Change = false; | ||||
913 | // Collect tile cast instruction. | ||||
914 | SmallVector<Instruction *, 8> Vec2TileInsts; | ||||
915 | SmallVector<Instruction *, 8> Tile2VecInsts; | ||||
916 | SmallVector<Instruction *, 8> PhiCastWorkList; | ||||
917 | SmallSetVector<Instruction *, 16> DeadInst; | ||||
918 | for (BasicBlock &BB : Func) { | ||||
919 | for (Instruction &I : BB) { | ||||
920 | Value *Vec; | ||||
921 | if (match(&I, | ||||
922 | m_Intrinsic<Intrinsic::x86_cast_vector_to_tile>(m_Value(Vec)))) | ||||
923 | Vec2TileInsts.push_back(&I); | ||||
924 | else if (match(&I, m_Intrinsic<Intrinsic::x86_cast_tile_to_vector>( | ||||
925 | m_Value(Vec)))) | ||||
926 | Tile2VecInsts.push_back(&I); | ||||
927 | } | ||||
928 | } | ||||
929 | |||||
930 | auto Convert = [&](SmallVectorImpl<Instruction *> &Insts, Intrinsic::ID IID) { | ||||
931 | for (auto *Inst : Insts) { | ||||
932 | for (User *U : Inst->users()) { | ||||
933 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(U); | ||||
934 | if (!II || II->getIntrinsicID() != IID) | ||||
935 | continue; | ||||
936 | // T1 = vec2tile V0 | ||||
937 | // V2 = tile2vec T1 | ||||
938 | // V3 = OP V2 | ||||
939 | // --> | ||||
940 | // T1 = vec2tile V0 | ||||
941 | // V2 = tile2vec T1 | ||||
942 | // V3 = OP V0 | ||||
943 | II->replaceAllUsesWith(Inst->getOperand(0)); | ||||
944 | Change = true; | ||||
945 | } | ||||
946 | } | ||||
947 | }; | ||||
948 | |||||
949 | Convert(Vec2TileInsts, Intrinsic::x86_cast_tile_to_vector); | ||||
950 | Convert(Tile2VecInsts, Intrinsic::x86_cast_vector_to_tile); | ||||
951 | |||||
952 | auto EraseInst = [&](SmallVectorImpl<Instruction *> &Insts) { | ||||
953 | for (auto *Inst : Insts) { | ||||
954 | if (Inst->use_empty()) { | ||||
955 | Inst->eraseFromParent(); | ||||
956 | Change = true; | ||||
957 | } | ||||
958 | } | ||||
959 | }; | ||||
960 | |||||
961 | EraseInst(Vec2TileInsts); | ||||
962 | EraseInst(Tile2VecInsts); | ||||
963 | |||||
964 | // Handle the A->B->A cast, and there is an intervening PHI node. | ||||
965 | for (BasicBlock &BB : Func) { | ||||
966 | for (Instruction &I : BB) { | ||||
967 | if (isAMXCast(&I)) { | ||||
968 | if (isa<PHINode>(I.getOperand(0))) | ||||
969 | PhiCastWorkList.push_back(&I); | ||||
970 | } | ||||
971 | } | ||||
972 | } | ||||
973 | for (auto *I : PhiCastWorkList) { | ||||
974 | // We skip the dead Amxcast. | ||||
975 | if (DeadInst.contains(I)) | ||||
976 | continue; | ||||
977 | PHINode *PN = cast<PHINode>(I->getOperand(0)); | ||||
978 | if (optimizeAMXCastFromPhi(cast<IntrinsicInst>(I), PN, DeadInst)) { | ||||
979 | DeadInst.insert(PN); | ||||
980 | Change = true; | ||||
981 | } | ||||
982 | } | ||||
983 | |||||
984 | // Since we create new phi and merge AMXCast, some old phis and AMXCast might | ||||
985 | // have no uses. We do some DeadCodeElimination for them. | ||||
986 | while (!DeadInst.empty()) { | ||||
987 | Instruction *I = DeadInst.pop_back_val(); | ||||
988 | Change |= DCEInstruction(I, DeadInst, TLI); | ||||
989 | } | ||||
990 | return Change; | ||||
991 | } | ||||
992 | |||||
993 | // There might be remaining AMXcast after combineAMXcast and they should be | ||||
994 | // handled elegantly. | ||||
995 | bool X86LowerAMXCast::transformAMXCast(IntrinsicInst *AMXCast) { | ||||
996 | IRBuilder<> Builder(AMXCast); | ||||
997 | AllocaInst *AllocaAddr; | ||||
998 | Value *I8Ptr, *Stride; | ||||
999 | auto *Src = AMXCast->getOperand(0); | ||||
1000 | |||||
1001 | auto Prepare = [&](Type *MemTy) { | ||||
1002 | AllocaAddr = createAllocaInstAtEntry(Builder, AMXCast->getParent(), MemTy); | ||||
1003 | I8Ptr = Builder.CreateBitCast(AllocaAddr, Builder.getInt8PtrTy()); | ||||
1004 | Stride = Builder.getInt64(64); | ||||
1005 | }; | ||||
1006 | |||||
1007 | if (AMXCast->getType()->isX86_AMXTy()) { | ||||
1008 | // %2 = amxcast <225 x i32> %src to x86_amx | ||||
1009 | // call void @llvm.x86.tilestored64.internal(i16 15, i16 60, | ||||
1010 | // i8* %addr3, i64 60, x86_amx %2) | ||||
1011 | // --> | ||||
1012 | // %addr = alloca <225 x i32>, align 64 | ||||
1013 | // store <225 x i32> %src, <225 x i32>* %addr, align 64 | ||||
1014 | // %addr2 = bitcast <225 x i32>* %addr to i8* | ||||
1015 | // %2 = call x86_amx @llvm.x86.tileloadd64.internal(i16 15, i16 60, | ||||
1016 | // i8* %addr2, | ||||
1017 | // i64 60) | ||||
1018 | // call void @llvm.x86.tilestored64.internal(i16 15, i16 60, | ||||
1019 | // i8* %addr3, i64 60, x86_amx %2) | ||||
1020 | if (AMXCast->use_empty()) { | ||||
1021 | AMXCast->eraseFromParent(); | ||||
1022 | return true; | ||||
1023 | } | ||||
1024 | Use &U = *(AMXCast->use_begin()); | ||||
1025 | unsigned OpNo = U.getOperandNo(); | ||||
1026 | auto *II = dyn_cast<IntrinsicInst>(U.getUser()); | ||||
1027 | if (!II) | ||||
1028 | return false; // May be bitcast from x86amx to <256 x i32>. | ||||
1029 | Prepare(AMXCast->getOperand(0)->getType()); | ||||
1030 | Builder.CreateStore(Src, AllocaAddr); | ||||
1031 | // TODO we can pick an constant operand for the shape. | ||||
1032 | Value *Row = nullptr, *Col = nullptr; | ||||
1033 | std::tie(Row, Col) = getShape(II, OpNo); | ||||
1034 | std::array<Value *, 4> Args = { | ||||
1035 | Row, Col, I8Ptr, Builder.CreateSExt(Col, Builder.getInt64Ty())}; | ||||
1036 | Value *NewInst = Builder.CreateIntrinsic( | ||||
1037 | Intrinsic::x86_tileloadd64_internal, None, Args); | ||||
1038 | AMXCast->replaceAllUsesWith(NewInst); | ||||
1039 | AMXCast->eraseFromParent(); | ||||
1040 | } else { | ||||
1041 | // %2 = amxcast x86_amx %src to <225 x i32> | ||||
1042 | // --> | ||||
1043 | // %addr = alloca <225 x i32>, align 64 | ||||
1044 | // %addr2 = bitcast <225 x i32>* to i8* | ||||
1045 | // call void @llvm.x86.tilestored64.internal(i16 %row, i16 %col, | ||||
1046 | // i8* %addr2, i64 %stride) | ||||
1047 | // %2 = load <225 x i32>, <225 x i32>* %addr, align 64 | ||||
1048 | auto *II = dyn_cast<IntrinsicInst>(Src); | ||||
1049 | if (!II) | ||||
1050 | return false; // May be bitcast from <256 x i32> to x86amx. | ||||
1051 | Prepare(AMXCast->getType()); | ||||
1052 | Value *Row = II->getOperand(0); | ||||
1053 | Value *Col = II->getOperand(1); | ||||
1054 | std::array<Value *, 5> Args = { | ||||
1055 | Row, Col, I8Ptr, Builder.CreateSExt(Col, Builder.getInt64Ty()), Src}; | ||||
1056 | Builder.CreateIntrinsic(Intrinsic::x86_tilestored64_internal, None, Args); | ||||
1057 | Value *NewInst = Builder.CreateLoad(AMXCast->getType(), AllocaAddr); | ||||
1058 | AMXCast->replaceAllUsesWith(NewInst); | ||||
1059 | AMXCast->eraseFromParent(); | ||||
1060 | } | ||||
1061 | |||||
1062 | return true; | ||||
1063 | } | ||||
1064 | |||||
1065 | bool X86LowerAMXCast::transformAllAMXCast() { | ||||
1066 | bool Change = false; | ||||
1067 | // Collect tile cast instruction. | ||||
1068 | SmallVector<Instruction *, 8> WorkLists; | ||||
1069 | for (BasicBlock &BB : Func) { | ||||
1070 | for (Instruction &I : BB) { | ||||
1071 | if (isAMXCast(&I)) | ||||
1072 | WorkLists.push_back(&I); | ||||
1073 | } | ||||
1074 | } | ||||
1075 | |||||
1076 | for (auto *Inst : WorkLists) { | ||||
1077 | Change |= transformAMXCast(cast<IntrinsicInst>(Inst)); | ||||
1078 | } | ||||
1079 | |||||
1080 | return Change; | ||||
1081 | } | ||||
1082 | |||||
1083 | } // anonymous namespace | ||||
1084 | |||||
1085 | namespace { | ||||
1086 | |||||
1087 | class X86LowerAMXTypeLegacyPass : public FunctionPass { | ||||
1088 | public: | ||||
1089 | static char ID; | ||||
1090 | |||||
1091 | X86LowerAMXTypeLegacyPass() : FunctionPass(ID) { | ||||
1092 | initializeX86LowerAMXTypeLegacyPassPass(*PassRegistry::getPassRegistry()); | ||||
1093 | } | ||||
1094 | |||||
1095 | bool runOnFunction(Function &F) override { | ||||
1096 | bool C = false; | ||||
1097 | TargetMachine *TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); | ||||
1098 | TargetLibraryInfo *TLI = | ||||
1099 | &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); | ||||
1100 | X86LowerAMXCast LAC(F); | ||||
1101 | C |= LAC.combineAMXcast(TLI); | ||||
1102 | // There might be remaining AMXcast after combineAMXcast and they should be | ||||
1103 | // handled elegantly. | ||||
1104 | C |= LAC.transformAllAMXCast(); | ||||
1105 | |||||
1106 | X86LowerAMXType LAT(F); | ||||
1107 | C |= LAT.visit(); | ||||
1108 | |||||
1109 | // Prepare for fast register allocation at O0. | ||||
1110 | // Todo: May better check the volatile model of AMX code, not just | ||||
1111 | // by checking Attribute::OptimizeNone and CodeGenOpt::None. | ||||
1112 | if (TM->getOptLevel() == CodeGenOpt::None) { | ||||
| |||||
1113 | // If Front End not use O0 but the Mid/Back end use O0, (e.g. | ||||
1114 | // "Clang -O2 -S -emit-llvm t.c" + "llc t.ll") we should make | ||||
1115 | // sure the amx data is volatile, that is nessary for AMX fast | ||||
1116 | // register allocation. | ||||
1117 | if (!F.hasFnAttribute(Attribute::OptimizeNone)) { | ||||
1118 | X86VolatileTileData VTD(F); | ||||
1119 | C = VTD.volatileTileData() || C; | ||||
1120 | } | ||||
1121 | } | ||||
1122 | |||||
1123 | return C; | ||||
1124 | } | ||||
1125 | |||||
1126 | void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||
1127 | AU.setPreservesCFG(); | ||||
1128 | AU.addRequired<TargetPassConfig>(); | ||||
1129 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | ||||
1130 | } | ||||
1131 | }; | ||||
1132 | |||||
1133 | } // anonymous namespace | ||||
1134 | |||||
1135 | static const char PassName[] = "Lower AMX type for load/store"; | ||||
1136 | char X86LowerAMXTypeLegacyPass::ID = 0; | ||||
1137 | INITIALIZE_PASS_BEGIN(X86LowerAMXTypeLegacyPass, DEBUG_TYPE, PassName, false,static void *initializeX86LowerAMXTypeLegacyPassPassOnce(PassRegistry &Registry) { | ||||
1138 | false)static void *initializeX86LowerAMXTypeLegacyPassPassOnce(PassRegistry &Registry) { | ||||
1139 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)initializeTargetPassConfigPass(Registry); | ||||
1140 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry); | ||||
1141 | INITIALIZE_PASS_END(X86LowerAMXTypeLegacyPass, DEBUG_TYPE, PassName, false,PassInfo *PI = new PassInfo( PassName, "lower-amx-type", & X86LowerAMXTypeLegacyPass::ID, PassInfo::NormalCtor_t(callDefaultCtor <X86LowerAMXTypeLegacyPass>), false, false); Registry.registerPass (*PI, true); return PI; } static llvm::once_flag InitializeX86LowerAMXTypeLegacyPassPassFlag ; void llvm::initializeX86LowerAMXTypeLegacyPassPass(PassRegistry &Registry) { llvm::call_once(InitializeX86LowerAMXTypeLegacyPassPassFlag , initializeX86LowerAMXTypeLegacyPassPassOnce, std::ref(Registry )); } | ||||
1142 | false)PassInfo *PI = new PassInfo( PassName, "lower-amx-type", & X86LowerAMXTypeLegacyPass::ID, PassInfo::NormalCtor_t(callDefaultCtor <X86LowerAMXTypeLegacyPass>), false, false); Registry.registerPass (*PI, true); return PI; } static llvm::once_flag InitializeX86LowerAMXTypeLegacyPassPassFlag ; void llvm::initializeX86LowerAMXTypeLegacyPassPass(PassRegistry &Registry) { llvm::call_once(InitializeX86LowerAMXTypeLegacyPassPassFlag , initializeX86LowerAMXTypeLegacyPassPassOnce, std::ref(Registry )); } | ||||
1143 | |||||
1144 | FunctionPass *llvm::createX86LowerAMXTypePass() { | ||||
1145 | return new X86LowerAMXTypeLegacyPass(); | ||||
1146 | } |
1 | //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file defines the IRBuilder class, which is used as a convenient way | |||
10 | // to create LLVM instructions with a consistent and simplified interface. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #ifndef LLVM_IR_IRBUILDER_H | |||
15 | #define LLVM_IR_IRBUILDER_H | |||
16 | ||||
17 | #include "llvm-c/Types.h" | |||
18 | #include "llvm/ADT/ArrayRef.h" | |||
19 | #include "llvm/ADT/None.h" | |||
20 | #include "llvm/ADT/STLExtras.h" | |||
21 | #include "llvm/ADT/StringRef.h" | |||
22 | #include "llvm/ADT/Twine.h" | |||
23 | #include "llvm/IR/BasicBlock.h" | |||
24 | #include "llvm/IR/Constant.h" | |||
25 | #include "llvm/IR/ConstantFolder.h" | |||
26 | #include "llvm/IR/Constants.h" | |||
27 | #include "llvm/IR/DataLayout.h" | |||
28 | #include "llvm/IR/DebugLoc.h" | |||
29 | #include "llvm/IR/DerivedTypes.h" | |||
30 | #include "llvm/IR/FPEnv.h" | |||
31 | #include "llvm/IR/Function.h" | |||
32 | #include "llvm/IR/GlobalVariable.h" | |||
33 | #include "llvm/IR/InstrTypes.h" | |||
34 | #include "llvm/IR/Instruction.h" | |||
35 | #include "llvm/IR/Instructions.h" | |||
36 | #include "llvm/IR/Intrinsics.h" | |||
37 | #include "llvm/IR/LLVMContext.h" | |||
38 | #include "llvm/IR/Module.h" | |||
39 | #include "llvm/IR/Operator.h" | |||
40 | #include "llvm/IR/Type.h" | |||
41 | #include "llvm/IR/Value.h" | |||
42 | #include "llvm/IR/ValueHandle.h" | |||
43 | #include "llvm/Support/AtomicOrdering.h" | |||
44 | #include "llvm/Support/CBindingWrapping.h" | |||
45 | #include "llvm/Support/Casting.h" | |||
46 | #include <cassert> | |||
47 | #include <cstdint> | |||
48 | #include <functional> | |||
49 | #include <utility> | |||
50 | ||||
51 | namespace llvm { | |||
52 | ||||
53 | class APInt; | |||
54 | class Use; | |||
55 | ||||
56 | /// This provides the default implementation of the IRBuilder | |||
57 | /// 'InsertHelper' method that is called whenever an instruction is created by | |||
58 | /// IRBuilder and needs to be inserted. | |||
59 | /// | |||
60 | /// By default, this inserts the instruction at the insertion point. | |||
61 | class IRBuilderDefaultInserter { | |||
62 | public: | |||
63 | virtual ~IRBuilderDefaultInserter(); | |||
64 | ||||
65 | virtual void InsertHelper(Instruction *I, const Twine &Name, | |||
66 | BasicBlock *BB, | |||
67 | BasicBlock::iterator InsertPt) const { | |||
68 | if (BB) BB->getInstList().insert(InsertPt, I); | |||
69 | I->setName(Name); | |||
70 | } | |||
71 | }; | |||
72 | ||||
73 | /// Provides an 'InsertHelper' that calls a user-provided callback after | |||
74 | /// performing the default insertion. | |||
75 | class IRBuilderCallbackInserter : public IRBuilderDefaultInserter { | |||
76 | std::function<void(Instruction *)> Callback; | |||
77 | ||||
78 | public: | |||
79 | ~IRBuilderCallbackInserter() override; | |||
80 | ||||
81 | IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback) | |||
82 | : Callback(std::move(Callback)) {} | |||
83 | ||||
84 | void InsertHelper(Instruction *I, const Twine &Name, | |||
85 | BasicBlock *BB, | |||
86 | BasicBlock::iterator InsertPt) const override { | |||
87 | IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt); | |||
88 | Callback(I); | |||
89 | } | |||
90 | }; | |||
91 | ||||
92 | /// Common base class shared among various IRBuilders. | |||
93 | class IRBuilderBase { | |||
94 | /// Pairs of (metadata kind, MDNode *) that should be added to all newly | |||
95 | /// created instructions, like !dbg metadata. | |||
96 | SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy; | |||
97 | ||||
98 | /// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not | |||
99 | /// null. If \p MD is null, remove the entry with \p Kind. | |||
100 | void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) { | |||
101 | if (!MD) { | |||
102 | erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) { | |||
103 | return KV.first == Kind; | |||
104 | }); | |||
105 | return; | |||
106 | } | |||
107 | ||||
108 | for (auto &KV : MetadataToCopy) | |||
109 | if (KV.first == Kind) { | |||
110 | KV.second = MD; | |||
111 | return; | |||
112 | } | |||
113 | ||||
114 | MetadataToCopy.emplace_back(Kind, MD); | |||
115 | } | |||
116 | ||||
117 | protected: | |||
118 | BasicBlock *BB; | |||
119 | BasicBlock::iterator InsertPt; | |||
120 | LLVMContext &Context; | |||
121 | const IRBuilderFolder &Folder; | |||
122 | const IRBuilderDefaultInserter &Inserter; | |||
123 | ||||
124 | MDNode *DefaultFPMathTag; | |||
125 | FastMathFlags FMF; | |||
126 | ||||
127 | bool IsFPConstrained = false; | |||
128 | fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict; | |||
129 | RoundingMode DefaultConstrainedRounding = RoundingMode::Dynamic; | |||
130 | ||||
131 | ArrayRef<OperandBundleDef> DefaultOperandBundles; | |||
132 | ||||
133 | public: | |||
134 | IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder, | |||
135 | const IRBuilderDefaultInserter &Inserter, MDNode *FPMathTag, | |||
136 | ArrayRef<OperandBundleDef> OpBundles) | |||
137 | : Context(context), Folder(Folder), Inserter(Inserter), | |||
138 | DefaultFPMathTag(FPMathTag), DefaultOperandBundles(OpBundles) { | |||
139 | ClearInsertionPoint(); | |||
140 | } | |||
141 | ||||
142 | /// Insert and return the specified instruction. | |||
143 | template<typename InstTy> | |||
144 | InstTy *Insert(InstTy *I, const Twine &Name = "") const { | |||
145 | Inserter.InsertHelper(I, Name, BB, InsertPt); | |||
146 | AddMetadataToInst(I); | |||
147 | return I; | |||
148 | } | |||
149 | ||||
150 | /// No-op overload to handle constants. | |||
151 | Constant *Insert(Constant *C, const Twine& = "") const { | |||
152 | return C; | |||
153 | } | |||
154 | ||||
155 | Value *Insert(Value *V, const Twine &Name = "") const { | |||
156 | if (Instruction *I = dyn_cast<Instruction>(V)) | |||
157 | return Insert(I, Name); | |||
158 | assert(isa<Constant>(V))(static_cast <bool> (isa<Constant>(V)) ? void (0) : __assert_fail ("isa<Constant>(V)", "llvm/include/llvm/IR/IRBuilder.h" , 158, __extension__ __PRETTY_FUNCTION__)); | |||
159 | return V; | |||
160 | } | |||
161 | ||||
162 | //===--------------------------------------------------------------------===// | |||
163 | // Builder configuration methods | |||
164 | //===--------------------------------------------------------------------===// | |||
165 | ||||
166 | /// Clear the insertion point: created instructions will not be | |||
167 | /// inserted into a block. | |||
168 | void ClearInsertionPoint() { | |||
169 | BB = nullptr; | |||
170 | InsertPt = BasicBlock::iterator(); | |||
171 | } | |||
172 | ||||
173 | BasicBlock *GetInsertBlock() const { return BB; } | |||
174 | BasicBlock::iterator GetInsertPoint() const { return InsertPt; } | |||
175 | LLVMContext &getContext() const { return Context; } | |||
176 | ||||
177 | /// This specifies that created instructions should be appended to the | |||
178 | /// end of the specified block. | |||
179 | void SetInsertPoint(BasicBlock *TheBB) { | |||
180 | BB = TheBB; | |||
181 | InsertPt = BB->end(); | |||
182 | } | |||
183 | ||||
184 | /// This specifies that created instructions should be inserted before | |||
185 | /// the specified instruction. | |||
186 | void SetInsertPoint(Instruction *I) { | |||
187 | BB = I->getParent(); | |||
188 | InsertPt = I->getIterator(); | |||
189 | assert(InsertPt != BB->end() && "Can't read debug loc from end()")(static_cast <bool> (InsertPt != BB->end() && "Can't read debug loc from end()") ? void (0) : __assert_fail ("InsertPt != BB->end() && \"Can't read debug loc from end()\"" , "llvm/include/llvm/IR/IRBuilder.h", 189, __extension__ __PRETTY_FUNCTION__ )); | |||
190 | SetCurrentDebugLocation(I->getDebugLoc()); | |||
191 | } | |||
192 | ||||
193 | /// This specifies that created instructions should be inserted at the | |||
194 | /// specified point. | |||
195 | void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) { | |||
196 | BB = TheBB; | |||
197 | InsertPt = IP; | |||
198 | if (IP != TheBB->end()) | |||
199 | SetCurrentDebugLocation(IP->getDebugLoc()); | |||
200 | } | |||
201 | ||||
202 | /// Set location information used by debugging information. | |||
203 | void SetCurrentDebugLocation(DebugLoc L) { | |||
204 | AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode()); | |||
205 | } | |||
206 | ||||
207 | /// Collect metadata with IDs \p MetadataKinds from \p Src which should be | |||
208 | /// added to all created instructions. Entries present in MedataDataToCopy but | |||
209 | /// not on \p Src will be dropped from MetadataToCopy. | |||
210 | void CollectMetadataToCopy(Instruction *Src, | |||
211 | ArrayRef<unsigned> MetadataKinds) { | |||
212 | for (unsigned K : MetadataKinds) | |||
213 | AddOrRemoveMetadataToCopy(K, Src->getMetadata(K)); | |||
214 | } | |||
215 | ||||
216 | /// Get location information used by debugging information. | |||
217 | DebugLoc getCurrentDebugLocation() const; | |||
218 | ||||
219 | /// If this builder has a current debug location, set it on the | |||
220 | /// specified instruction. | |||
221 | void SetInstDebugLocation(Instruction *I) const; | |||
222 | ||||
223 | /// Add all entries in MetadataToCopy to \p I. | |||
224 | void AddMetadataToInst(Instruction *I) const { | |||
225 | for (auto &KV : MetadataToCopy) | |||
226 | I->setMetadata(KV.first, KV.second); | |||
227 | } | |||
228 | ||||
229 | /// Get the return type of the current function that we're emitting | |||
230 | /// into. | |||
231 | Type *getCurrentFunctionReturnType() const; | |||
232 | ||||
233 | /// InsertPoint - A saved insertion point. | |||
234 | class InsertPoint { | |||
235 | BasicBlock *Block = nullptr; | |||
236 | BasicBlock::iterator Point; | |||
237 | ||||
238 | public: | |||
239 | /// Creates a new insertion point which doesn't point to anything. | |||
240 | InsertPoint() = default; | |||
241 | ||||
242 | /// Creates a new insertion point at the given location. | |||
243 | InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint) | |||
244 | : Block(InsertBlock), Point(InsertPoint) {} | |||
245 | ||||
246 | /// Returns true if this insert point is set. | |||
247 | bool isSet() const { return (Block != nullptr); } | |||
248 | ||||
249 | BasicBlock *getBlock() const { return Block; } | |||
250 | BasicBlock::iterator getPoint() const { return Point; } | |||
251 | }; | |||
252 | ||||
253 | /// Returns the current insert point. | |||
254 | InsertPoint saveIP() const { | |||
255 | return InsertPoint(GetInsertBlock(), GetInsertPoint()); | |||
256 | } | |||
257 | ||||
258 | /// Returns the current insert point, clearing it in the process. | |||
259 | InsertPoint saveAndClearIP() { | |||
260 | InsertPoint IP(GetInsertBlock(), GetInsertPoint()); | |||
261 | ClearInsertionPoint(); | |||
262 | return IP; | |||
263 | } | |||
264 | ||||
265 | /// Sets the current insert point to a previously-saved location. | |||
266 | void restoreIP(InsertPoint IP) { | |||
267 | if (IP.isSet()) | |||
268 | SetInsertPoint(IP.getBlock(), IP.getPoint()); | |||
269 | else | |||
270 | ClearInsertionPoint(); | |||
271 | } | |||
272 | ||||
273 | /// Get the floating point math metadata being used. | |||
274 | MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; } | |||
275 | ||||
276 | /// Get the flags to be applied to created floating point ops | |||
277 | FastMathFlags getFastMathFlags() const { return FMF; } | |||
278 | ||||
279 | FastMathFlags &getFastMathFlags() { return FMF; } | |||
280 | ||||
281 | /// Clear the fast-math flags. | |||
282 | void clearFastMathFlags() { FMF.clear(); } | |||
283 | ||||
284 | /// Set the floating point math metadata to be used. | |||
285 | void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; } | |||
286 | ||||
287 | /// Set the fast-math flags to be used with generated fp-math operators | |||
288 | void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; } | |||
289 | ||||
290 | /// Enable/Disable use of constrained floating point math. When | |||
291 | /// enabled the CreateF<op>() calls instead create constrained | |||
292 | /// floating point intrinsic calls. Fast math flags are unaffected | |||
293 | /// by this setting. | |||
294 | void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; } | |||
295 | ||||
296 | /// Query for the use of constrained floating point math | |||
297 | bool getIsFPConstrained() { return IsFPConstrained; } | |||
298 | ||||
299 | /// Set the exception handling to be used with constrained floating point | |||
300 | void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) { | |||
301 | #ifndef NDEBUG | |||
302 | Optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(NewExcept); | |||
303 | assert(ExceptStr.hasValue() && "Garbage strict exception behavior!")(static_cast <bool> (ExceptStr.hasValue() && "Garbage strict exception behavior!" ) ? void (0) : __assert_fail ("ExceptStr.hasValue() && \"Garbage strict exception behavior!\"" , "llvm/include/llvm/IR/IRBuilder.h", 303, __extension__ __PRETTY_FUNCTION__ )); | |||
304 | #endif | |||
305 | DefaultConstrainedExcept = NewExcept; | |||
306 | } | |||
307 | ||||
308 | /// Set the rounding mode handling to be used with constrained floating point | |||
309 | void setDefaultConstrainedRounding(RoundingMode NewRounding) { | |||
310 | #ifndef NDEBUG | |||
311 | Optional<StringRef> RoundingStr = convertRoundingModeToStr(NewRounding); | |||
312 | assert(RoundingStr.hasValue() && "Garbage strict rounding mode!")(static_cast <bool> (RoundingStr.hasValue() && "Garbage strict rounding mode!" ) ? void (0) : __assert_fail ("RoundingStr.hasValue() && \"Garbage strict rounding mode!\"" , "llvm/include/llvm/IR/IRBuilder.h", 312, __extension__ __PRETTY_FUNCTION__ )); | |||
313 | #endif | |||
314 | DefaultConstrainedRounding = NewRounding; | |||
315 | } | |||
316 | ||||
317 | /// Get the exception handling used with constrained floating point | |||
318 | fp::ExceptionBehavior getDefaultConstrainedExcept() { | |||
319 | return DefaultConstrainedExcept; | |||
320 | } | |||
321 | ||||
322 | /// Get the rounding mode handling used with constrained floating point | |||
323 | RoundingMode getDefaultConstrainedRounding() { | |||
324 | return DefaultConstrainedRounding; | |||
325 | } | |||
326 | ||||
327 | void setConstrainedFPFunctionAttr() { | |||
328 | assert(BB && "Must have a basic block to set any function attributes!")(static_cast <bool> (BB && "Must have a basic block to set any function attributes!" ) ? void (0) : __assert_fail ("BB && \"Must have a basic block to set any function attributes!\"" , "llvm/include/llvm/IR/IRBuilder.h", 328, __extension__ __PRETTY_FUNCTION__ )); | |||
329 | ||||
330 | Function *F = BB->getParent(); | |||
331 | if (!F->hasFnAttribute(Attribute::StrictFP)) { | |||
332 | F->addFnAttr(Attribute::StrictFP); | |||
333 | } | |||
334 | } | |||
335 | ||||
336 | void setConstrainedFPCallAttr(CallBase *I) { | |||
337 | I->addFnAttr(Attribute::StrictFP); | |||
338 | } | |||
339 | ||||
340 | void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) { | |||
341 | DefaultOperandBundles = OpBundles; | |||
342 | } | |||
343 | ||||
344 | //===--------------------------------------------------------------------===// | |||
345 | // RAII helpers. | |||
346 | //===--------------------------------------------------------------------===// | |||
347 | ||||
348 | // RAII object that stores the current insertion point and restores it | |||
349 | // when the object is destroyed. This includes the debug location. | |||
350 | class InsertPointGuard { | |||
351 | IRBuilderBase &Builder; | |||
352 | AssertingVH<BasicBlock> Block; | |||
353 | BasicBlock::iterator Point; | |||
354 | DebugLoc DbgLoc; | |||
355 | ||||
356 | public: | |||
357 | InsertPointGuard(IRBuilderBase &B) | |||
358 | : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()), | |||
359 | DbgLoc(B.getCurrentDebugLocation()) {} | |||
360 | ||||
361 | InsertPointGuard(const InsertPointGuard &) = delete; | |||
362 | InsertPointGuard &operator=(const InsertPointGuard &) = delete; | |||
363 | ||||
364 | ~InsertPointGuard() { | |||
365 | Builder.restoreIP(InsertPoint(Block, Point)); | |||
366 | Builder.SetCurrentDebugLocation(DbgLoc); | |||
367 | } | |||
368 | }; | |||
369 | ||||
370 | // RAII object that stores the current fast math settings and restores | |||
371 | // them when the object is destroyed. | |||
372 | class FastMathFlagGuard { | |||
373 | IRBuilderBase &Builder; | |||
374 | FastMathFlags FMF; | |||
375 | MDNode *FPMathTag; | |||
376 | bool IsFPConstrained; | |||
377 | fp::ExceptionBehavior DefaultConstrainedExcept; | |||
378 | RoundingMode DefaultConstrainedRounding; | |||
379 | ||||
380 | public: | |||
381 | FastMathFlagGuard(IRBuilderBase &B) | |||
382 | : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag), | |||
383 | IsFPConstrained(B.IsFPConstrained), | |||
384 | DefaultConstrainedExcept(B.DefaultConstrainedExcept), | |||
385 | DefaultConstrainedRounding(B.DefaultConstrainedRounding) {} | |||
386 | ||||
387 | FastMathFlagGuard(const FastMathFlagGuard &) = delete; | |||
388 | FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete; | |||
389 | ||||
390 | ~FastMathFlagGuard() { | |||
391 | Builder.FMF = FMF; | |||
392 | Builder.DefaultFPMathTag = FPMathTag; | |||
393 | Builder.IsFPConstrained = IsFPConstrained; | |||
394 | Builder.DefaultConstrainedExcept = DefaultConstrainedExcept; | |||
395 | Builder.DefaultConstrainedRounding = DefaultConstrainedRounding; | |||
396 | } | |||
397 | }; | |||
398 | ||||
399 | // RAII object that stores the current default operand bundles and restores | |||
400 | // them when the object is destroyed. | |||
401 | class OperandBundlesGuard { | |||
402 | IRBuilderBase &Builder; | |||
403 | ArrayRef<OperandBundleDef> DefaultOperandBundles; | |||
404 | ||||
405 | public: | |||
406 | OperandBundlesGuard(IRBuilderBase &B) | |||
407 | : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {} | |||
408 | ||||
409 | OperandBundlesGuard(const OperandBundlesGuard &) = delete; | |||
410 | OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete; | |||
411 | ||||
412 | ~OperandBundlesGuard() { | |||
413 | Builder.DefaultOperandBundles = DefaultOperandBundles; | |||
414 | } | |||
415 | }; | |||
416 | ||||
417 | ||||
418 | //===--------------------------------------------------------------------===// | |||
419 | // Miscellaneous creation methods. | |||
420 | //===--------------------------------------------------------------------===// | |||
421 | ||||
422 | /// Make a new global variable with initializer type i8* | |||
423 | /// | |||
424 | /// Make a new global variable with an initializer that has array of i8 type | |||
425 | /// filled in with the null terminated string value specified. The new global | |||
426 | /// variable will be marked mergable with any others of the same contents. If | |||
427 | /// Name is specified, it is the name of the global variable created. | |||
428 | /// | |||
429 | /// If no module is given via \p M, it is take from the insertion point basic | |||
430 | /// block. | |||
431 | GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "", | |||
432 | unsigned AddressSpace = 0, | |||
433 | Module *M = nullptr); | |||
434 | ||||
435 | /// Get a constant value representing either true or false. | |||
436 | ConstantInt *getInt1(bool V) { | |||
437 | return ConstantInt::get(getInt1Ty(), V); | |||
438 | } | |||
439 | ||||
440 | /// Get the constant value for i1 true. | |||
441 | ConstantInt *getTrue() { | |||
442 | return ConstantInt::getTrue(Context); | |||
443 | } | |||
444 | ||||
445 | /// Get the constant value for i1 false. | |||
446 | ConstantInt *getFalse() { | |||
447 | return ConstantInt::getFalse(Context); | |||
448 | } | |||
449 | ||||
450 | /// Get a constant 8-bit value. | |||
451 | ConstantInt *getInt8(uint8_t C) { | |||
452 | return ConstantInt::get(getInt8Ty(), C); | |||
453 | } | |||
454 | ||||
455 | /// Get a constant 16-bit value. | |||
456 | ConstantInt *getInt16(uint16_t C) { | |||
457 | return ConstantInt::get(getInt16Ty(), C); | |||
458 | } | |||
459 | ||||
460 | /// Get a constant 32-bit value. | |||
461 | ConstantInt *getInt32(uint32_t C) { | |||
462 | return ConstantInt::get(getInt32Ty(), C); | |||
463 | } | |||
464 | ||||
465 | /// Get a constant 64-bit value. | |||
466 | ConstantInt *getInt64(uint64_t C) { | |||
467 | return ConstantInt::get(getInt64Ty(), C); | |||
468 | } | |||
469 | ||||
470 | /// Get a constant N-bit value, zero extended or truncated from | |||
471 | /// a 64-bit value. | |||
472 | ConstantInt *getIntN(unsigned N, uint64_t C) { | |||
473 | return ConstantInt::get(getIntNTy(N), C); | |||
474 | } | |||
475 | ||||
476 | /// Get a constant integer value. | |||
477 | ConstantInt *getInt(const APInt &AI) { | |||
478 | return ConstantInt::get(Context, AI); | |||
479 | } | |||
480 | ||||
481 | //===--------------------------------------------------------------------===// | |||
482 | // Type creation methods | |||
483 | //===--------------------------------------------------------------------===// | |||
484 | ||||
485 | /// Fetch the type representing a single bit | |||
486 | IntegerType *getInt1Ty() { | |||
487 | return Type::getInt1Ty(Context); | |||
488 | } | |||
489 | ||||
490 | /// Fetch the type representing an 8-bit integer. | |||
491 | IntegerType *getInt8Ty() { | |||
492 | return Type::getInt8Ty(Context); | |||
493 | } | |||
494 | ||||
495 | /// Fetch the type representing a 16-bit integer. | |||
496 | IntegerType *getInt16Ty() { | |||
497 | return Type::getInt16Ty(Context); | |||
498 | } | |||
499 | ||||
500 | /// Fetch the type representing a 32-bit integer. | |||
501 | IntegerType *getInt32Ty() { | |||
502 | return Type::getInt32Ty(Context); | |||
503 | } | |||
504 | ||||
505 | /// Fetch the type representing a 64-bit integer. | |||
506 | IntegerType *getInt64Ty() { | |||
507 | return Type::getInt64Ty(Context); | |||
508 | } | |||
509 | ||||
510 | /// Fetch the type representing a 128-bit integer. | |||
511 | IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); } | |||
512 | ||||
513 | /// Fetch the type representing an N-bit integer. | |||
514 | IntegerType *getIntNTy(unsigned N) { | |||
515 | return Type::getIntNTy(Context, N); | |||
516 | } | |||
517 | ||||
518 | /// Fetch the type representing a 16-bit floating point value. | |||
519 | Type *getHalfTy() { | |||
520 | return Type::getHalfTy(Context); | |||
521 | } | |||
522 | ||||
523 | /// Fetch the type representing a 16-bit brain floating point value. | |||
524 | Type *getBFloatTy() { | |||
525 | return Type::getBFloatTy(Context); | |||
526 | } | |||
527 | ||||
528 | /// Fetch the type representing a 32-bit floating point value. | |||
529 | Type *getFloatTy() { | |||
530 | return Type::getFloatTy(Context); | |||
531 | } | |||
532 | ||||
533 | /// Fetch the type representing a 64-bit floating point value. | |||
534 | Type *getDoubleTy() { | |||
535 | return Type::getDoubleTy(Context); | |||
536 | } | |||
537 | ||||
538 | /// Fetch the type representing void. | |||
539 | Type *getVoidTy() { | |||
540 | return Type::getVoidTy(Context); | |||
541 | } | |||
542 | ||||
543 | /// Fetch the type representing a pointer to an 8-bit integer value. | |||
544 | PointerType *getInt8PtrTy(unsigned AddrSpace = 0) { | |||
545 | return Type::getInt8PtrTy(Context, AddrSpace); | |||
546 | } | |||
547 | ||||
548 | /// Fetch the type representing a pointer to an integer value. | |||
549 | IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) { | |||
550 | return DL.getIntPtrType(Context, AddrSpace); | |||
551 | } | |||
552 | ||||
553 | //===--------------------------------------------------------------------===// | |||
554 | // Intrinsic creation methods | |||
555 | //===--------------------------------------------------------------------===// | |||
556 | ||||
557 | /// Create and insert a memset to the specified pointer and the | |||
558 | /// specified value. | |||
559 | /// | |||
560 | /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is | |||
561 | /// specified, it will be added to the instruction. Likewise with alias.scope | |||
562 | /// and noalias tags. | |||
563 | CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, | |||
564 | MaybeAlign Align, bool isVolatile = false, | |||
565 | MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, | |||
566 | MDNode *NoAliasTag = nullptr) { | |||
567 | return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, | |||
568 | TBAATag, ScopeTag, NoAliasTag); | |||
569 | } | |||
570 | ||||
571 | CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align, | |||
572 | bool isVolatile = false, MDNode *TBAATag = nullptr, | |||
573 | MDNode *ScopeTag = nullptr, | |||
574 | MDNode *NoAliasTag = nullptr); | |||
575 | ||||
576 | /// Create and insert an element unordered-atomic memset of the region of | |||
577 | /// memory starting at the given pointer to the given value. | |||
578 | /// | |||
579 | /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is | |||
580 | /// specified, it will be added to the instruction. Likewise with alias.scope | |||
581 | /// and noalias tags. | |||
582 | CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, | |||
583 | uint64_t Size, Align Alignment, | |||
584 | uint32_t ElementSize, | |||
585 | MDNode *TBAATag = nullptr, | |||
586 | MDNode *ScopeTag = nullptr, | |||
587 | MDNode *NoAliasTag = nullptr) { | |||
588 | return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), | |||
589 | Align(Alignment), ElementSize, | |||
590 | TBAATag, ScopeTag, NoAliasTag); | |||
591 | } | |||
592 | ||||
593 | CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, | |||
594 | Value *Size, Align Alignment, | |||
595 | uint32_t ElementSize, | |||
596 | MDNode *TBAATag = nullptr, | |||
597 | MDNode *ScopeTag = nullptr, | |||
598 | MDNode *NoAliasTag = nullptr); | |||
599 | ||||
600 | /// Create and insert a memcpy between the specified pointers. | |||
601 | /// | |||
602 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is | |||
603 | /// specified, it will be added to the instruction. Likewise with alias.scope | |||
604 | /// and noalias tags. | |||
605 | CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, | |||
606 | MaybeAlign SrcAlign, uint64_t Size, | |||
607 | bool isVolatile = false, MDNode *TBAATag = nullptr, | |||
608 | MDNode *TBAAStructTag = nullptr, | |||
609 | MDNode *ScopeTag = nullptr, | |||
610 | MDNode *NoAliasTag = nullptr) { | |||
611 | return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size), | |||
612 | isVolatile, TBAATag, TBAAStructTag, ScopeTag, | |||
613 | NoAliasTag); | |||
614 | } | |||
615 | ||||
616 | CallInst *CreateMemTransferInst( | |||
617 | Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src, | |||
618 | MaybeAlign SrcAlign, Value *Size, bool isVolatile = false, | |||
619 | MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, | |||
620 | MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr); | |||
621 | ||||
622 | CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, | |||
623 | MaybeAlign SrcAlign, Value *Size, | |||
624 | bool isVolatile = false, MDNode *TBAATag = nullptr, | |||
625 | MDNode *TBAAStructTag = nullptr, | |||
626 | MDNode *ScopeTag = nullptr, | |||
627 | MDNode *NoAliasTag = nullptr) { | |||
628 | return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src, | |||
629 | SrcAlign, Size, isVolatile, TBAATag, | |||
630 | TBAAStructTag, ScopeTag, NoAliasTag); | |||
631 | } | |||
632 | ||||
633 | CallInst * | |||
634 | CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src, | |||
635 | MaybeAlign SrcAlign, Value *Size, bool IsVolatile = false, | |||
636 | MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, | |||
637 | MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr); | |||
638 | ||||
639 | /// Create and insert an element unordered-atomic memcpy between the | |||
640 | /// specified pointers. | |||
641 | /// | |||
642 | /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively. | |||
643 | /// | |||
644 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is | |||
645 | /// specified, it will be added to the instruction. Likewise with alias.scope | |||
646 | /// and noalias tags. | |||
647 | CallInst *CreateElementUnorderedAtomicMemCpy( | |||
648 | Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, | |||
649 | uint32_t ElementSize, MDNode *TBAATag = nullptr, | |||
650 | MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, | |||
651 | MDNode *NoAliasTag = nullptr); | |||
652 | ||||
653 | CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src, | |||
654 | MaybeAlign SrcAlign, uint64_t Size, | |||
655 | bool isVolatile = false, MDNode *TBAATag = nullptr, | |||
656 | MDNode *ScopeTag = nullptr, | |||
657 | MDNode *NoAliasTag = nullptr) { | |||
658 | return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), | |||
659 | isVolatile, TBAATag, ScopeTag, NoAliasTag); | |||
660 | } | |||
661 | ||||
662 | CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src, | |||
663 | MaybeAlign SrcAlign, Value *Size, | |||
664 | bool isVolatile = false, MDNode *TBAATag = nullptr, | |||
665 | MDNode *ScopeTag = nullptr, | |||
666 | MDNode *NoAliasTag = nullptr); | |||
667 | ||||
668 | /// \brief Create and insert an element unordered-atomic memmove between the | |||
669 | /// specified pointers. | |||
670 | /// | |||
671 | /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, | |||
672 | /// respectively. | |||
673 | /// | |||
674 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is | |||
675 | /// specified, it will be added to the instruction. Likewise with alias.scope | |||
676 | /// and noalias tags. | |||
677 | CallInst *CreateElementUnorderedAtomicMemMove( | |||
678 | Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, | |||
679 | uint32_t ElementSize, MDNode *TBAATag = nullptr, | |||
680 | MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, | |||
681 | MDNode *NoAliasTag = nullptr); | |||
682 | ||||
683 | /// Create a sequential vector fadd reduction intrinsic of the source vector. | |||
684 | /// The first parameter is a scalar accumulator value. An unordered reduction | |||
685 | /// can be created by adding the reassoc fast-math flag to the resulting | |||
686 | /// sequential reduction. | |||
687 | CallInst *CreateFAddReduce(Value *Acc, Value *Src); | |||
688 | ||||
689 | /// Create a sequential vector fmul reduction intrinsic of the source vector. | |||
690 | /// The first parameter is a scalar accumulator value. An unordered reduction | |||
691 | /// can be created by adding the reassoc fast-math flag to the resulting | |||
692 | /// sequential reduction. | |||
693 | CallInst *CreateFMulReduce(Value *Acc, Value *Src); | |||
694 | ||||
695 | /// Create a vector int add reduction intrinsic of the source vector. | |||
696 | CallInst *CreateAddReduce(Value *Src); | |||
697 | ||||
698 | /// Create a vector int mul reduction intrinsic of the source vector. | |||
699 | CallInst *CreateMulReduce(Value *Src); | |||
700 | ||||
701 | /// Create a vector int AND reduction intrinsic of the source vector. | |||
702 | CallInst *CreateAndReduce(Value *Src); | |||
703 | ||||
704 | /// Create a vector int OR reduction intrinsic of the source vector. | |||
705 | CallInst *CreateOrReduce(Value *Src); | |||
706 | ||||
707 | /// Create a vector int XOR reduction intrinsic of the source vector. | |||
708 | CallInst *CreateXorReduce(Value *Src); | |||
709 | ||||
710 | /// Create a vector integer max reduction intrinsic of the source | |||
711 | /// vector. | |||
712 | CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false); | |||
713 | ||||
714 | /// Create a vector integer min reduction intrinsic of the source | |||
715 | /// vector. | |||
716 | CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false); | |||
717 | ||||
718 | /// Create a vector float max reduction intrinsic of the source | |||
719 | /// vector. | |||
720 | CallInst *CreateFPMaxReduce(Value *Src); | |||
721 | ||||
722 | /// Create a vector float min reduction intrinsic of the source | |||
723 | /// vector. | |||
724 | CallInst *CreateFPMinReduce(Value *Src); | |||
725 | ||||
726 | /// Create a lifetime.start intrinsic. | |||
727 | /// | |||
728 | /// If the pointer isn't i8* it will be converted. | |||
729 | CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr); | |||
730 | ||||
731 | /// Create a lifetime.end intrinsic. | |||
732 | /// | |||
733 | /// If the pointer isn't i8* it will be converted. | |||
734 | CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr); | |||
735 | ||||
736 | /// Create a call to invariant.start intrinsic. | |||
737 | /// | |||
738 | /// If the pointer isn't i8* it will be converted. | |||
739 | CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr); | |||
740 | ||||
741 | /// Create a call to Masked Load intrinsic | |||
742 | CallInst *CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask, | |||
743 | Value *PassThru = nullptr, const Twine &Name = ""); | |||
744 | ||||
745 | /// Create a call to Masked Store intrinsic | |||
746 | CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment, | |||
747 | Value *Mask); | |||
748 | ||||
749 | /// Create a call to Masked Gather intrinsic | |||
750 | CallInst *CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment, | |||
751 | Value *Mask = nullptr, Value *PassThru = nullptr, | |||
752 | const Twine &Name = ""); | |||
753 | ||||
754 | /// Create a call to Masked Scatter intrinsic | |||
755 | CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment, | |||
756 | Value *Mask = nullptr); | |||
757 | ||||
758 | /// Create an assume intrinsic call that allows the optimizer to | |||
759 | /// assume that the provided condition will be true. | |||
760 | /// | |||
761 | /// The optional argument \p OpBundles specifies operand bundles that are | |||
762 | /// added to the call instruction. | |||
763 | CallInst *CreateAssumption(Value *Cond, | |||
764 | ArrayRef<OperandBundleDef> OpBundles = llvm::None); | |||
765 | ||||
766 | /// Create a llvm.experimental.noalias.scope.decl intrinsic call. | |||
767 | Instruction *CreateNoAliasScopeDeclaration(Value *Scope); | |||
768 | Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) { | |||
769 | return CreateNoAliasScopeDeclaration( | |||
770 | MetadataAsValue::get(Context, ScopeTag)); | |||
771 | } | |||
772 | ||||
773 | /// Create a call to the experimental.gc.statepoint intrinsic to | |||
774 | /// start a new statepoint sequence. | |||
775 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, | |||
776 | FunctionCallee ActualCallee, | |||
777 | ArrayRef<Value *> CallArgs, | |||
778 | Optional<ArrayRef<Value *>> DeoptArgs, | |||
779 | ArrayRef<Value *> GCArgs, | |||
780 | const Twine &Name = ""); | |||
781 | ||||
782 | /// Create a call to the experimental.gc.statepoint intrinsic to | |||
783 | /// start a new statepoint sequence. | |||
784 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, | |||
785 | FunctionCallee ActualCallee, uint32_t Flags, | |||
786 | ArrayRef<Value *> CallArgs, | |||
787 | Optional<ArrayRef<Use>> TransitionArgs, | |||
788 | Optional<ArrayRef<Use>> DeoptArgs, | |||
789 | ArrayRef<Value *> GCArgs, | |||
790 | const Twine &Name = ""); | |||
791 | ||||
792 | /// Conveninence function for the common case when CallArgs are filled | |||
793 | /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be | |||
794 | /// .get()'ed to get the Value pointer. | |||
795 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, | |||
796 | FunctionCallee ActualCallee, | |||
797 | ArrayRef<Use> CallArgs, | |||
798 | Optional<ArrayRef<Value *>> DeoptArgs, | |||
799 | ArrayRef<Value *> GCArgs, | |||
800 | const Twine &Name = ""); | |||
801 | ||||
802 | /// Create an invoke to the experimental.gc.statepoint intrinsic to | |||
803 | /// start a new statepoint sequence. | |||
804 | InvokeInst * | |||
805 | CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, | |||
806 | FunctionCallee ActualInvokee, BasicBlock *NormalDest, | |||
807 | BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs, | |||
808 | Optional<ArrayRef<Value *>> DeoptArgs, | |||
809 | ArrayRef<Value *> GCArgs, const Twine &Name = ""); | |||
810 | ||||
811 | /// Create an invoke to the experimental.gc.statepoint intrinsic to | |||
812 | /// start a new statepoint sequence. | |||
813 | InvokeInst *CreateGCStatepointInvoke( | |||
814 | uint64_t ID, uint32_t NumPatchBytes, FunctionCallee ActualInvokee, | |||
815 | BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, | |||
816 | ArrayRef<Value *> InvokeArgs, Optional<ArrayRef<Use>> TransitionArgs, | |||
817 | Optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs, | |||
818 | const Twine &Name = ""); | |||
819 | ||||
820 | // Convenience function for the common case when CallArgs are filled in using | |||
821 | // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to | |||
822 | // get the Value *. | |||
823 | InvokeInst * | |||
824 | CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, | |||
825 | FunctionCallee ActualInvokee, BasicBlock *NormalDest, | |||
826 | BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs, | |||
827 | Optional<ArrayRef<Value *>> DeoptArgs, | |||
828 | ArrayRef<Value *> GCArgs, const Twine &Name = ""); | |||
829 | ||||
830 | /// Create a call to the experimental.gc.result intrinsic to extract | |||
831 | /// the result from a call wrapped in a statepoint. | |||
832 | CallInst *CreateGCResult(Instruction *Statepoint, | |||
833 | Type *ResultType, | |||
834 | const Twine &Name = ""); | |||
835 | ||||
836 | /// Create a call to the experimental.gc.relocate intrinsics to | |||
837 | /// project the relocated value of one pointer from the statepoint. | |||
838 | CallInst *CreateGCRelocate(Instruction *Statepoint, | |||
839 | int BaseOffset, | |||
840 | int DerivedOffset, | |||
841 | Type *ResultType, | |||
842 | const Twine &Name = ""); | |||
843 | ||||
844 | /// Create a call to the experimental.gc.pointer.base intrinsic to get the | |||
845 | /// base pointer for the specified derived pointer. | |||
846 | CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = ""); | |||
847 | ||||
848 | /// Create a call to the experimental.gc.get.pointer.offset intrinsic to get | |||
849 | /// the offset of the specified derived pointer from its base. | |||
850 | CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = ""); | |||
851 | ||||
852 | /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale | |||
853 | /// will be the same type as that of \p Scaling. | |||
854 | Value *CreateVScale(Constant *Scaling, const Twine &Name = ""); | |||
855 | ||||
856 | /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...> | |||
857 | Value *CreateStepVector(Type *DstType, const Twine &Name = ""); | |||
858 | ||||
859 | /// Create a call to intrinsic \p ID with 1 operand which is mangled on its | |||
860 | /// type. | |||
861 | CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, | |||
862 | Instruction *FMFSource = nullptr, | |||
863 | const Twine &Name = ""); | |||
864 | ||||
865 | /// Create a call to intrinsic \p ID with 2 operands which is mangled on the | |||
866 | /// first type. | |||
867 | CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, | |||
868 | Instruction *FMFSource = nullptr, | |||
869 | const Twine &Name = ""); | |||
870 | ||||
871 | /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If | |||
872 | /// \p FMFSource is provided, copy fast-math-flags from that instruction to | |||
873 | /// the intrinsic. | |||
874 | CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types, | |||
875 | ArrayRef<Value *> Args, | |||
876 | Instruction *FMFSource = nullptr, | |||
877 | const Twine &Name = ""); | |||
878 | ||||
879 | /// Create call to the minnum intrinsic. | |||
880 | CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
881 | return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name); | |||
882 | } | |||
883 | ||||
884 | /// Create call to the maxnum intrinsic. | |||
885 | CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
886 | return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name); | |||
887 | } | |||
888 | ||||
889 | /// Create call to the minimum intrinsic. | |||
890 | CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
891 | return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name); | |||
892 | } | |||
893 | ||||
894 | /// Create call to the maximum intrinsic. | |||
895 | CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
896 | return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name); | |||
897 | } | |||
898 | ||||
899 | /// Create a call to the arithmetic_fence intrinsic. | |||
900 | CallInst *CreateArithmeticFence(Value *Val, Type *DstType, | |||
901 | const Twine &Name = "") { | |||
902 | return CreateIntrinsic(Intrinsic::arithmetic_fence, DstType, Val, nullptr, | |||
903 | Name); | |||
904 | } | |||
905 | ||||
906 | /// Create a call to the experimental.vector.extract intrinsic. | |||
907 | CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx, | |||
908 | const Twine &Name = "") { | |||
909 | return CreateIntrinsic(Intrinsic::experimental_vector_extract, | |||
910 | {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr, | |||
911 | Name); | |||
912 | } | |||
913 | ||||
914 | /// Create a call to the experimental.vector.insert intrinsic. | |||
915 | CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec, | |||
916 | Value *Idx, const Twine &Name = "") { | |||
917 | return CreateIntrinsic(Intrinsic::experimental_vector_insert, | |||
918 | {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx}, | |||
919 | nullptr, Name); | |||
920 | } | |||
921 | ||||
922 | private: | |||
923 | /// Create a call to a masked intrinsic with given Id. | |||
924 | CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops, | |||
925 | ArrayRef<Type *> OverloadedTypes, | |||
926 | const Twine &Name = ""); | |||
927 | ||||
928 | Value *getCastedInt8PtrValue(Value *Ptr); | |||
929 | ||||
930 | //===--------------------------------------------------------------------===// | |||
931 | // Instruction creation methods: Terminators | |||
932 | //===--------------------------------------------------------------------===// | |||
933 | ||||
934 | private: | |||
935 | /// Helper to add branch weight and unpredictable metadata onto an | |||
936 | /// instruction. | |||
937 | /// \returns The annotated instruction. | |||
938 | template <typename InstTy> | |||
939 | InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) { | |||
940 | if (Weights) | |||
941 | I->setMetadata(LLVMContext::MD_prof, Weights); | |||
942 | if (Unpredictable) | |||
943 | I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable); | |||
944 | return I; | |||
945 | } | |||
946 | ||||
947 | public: | |||
948 | /// Create a 'ret void' instruction. | |||
949 | ReturnInst *CreateRetVoid() { | |||
950 | return Insert(ReturnInst::Create(Context)); | |||
951 | } | |||
952 | ||||
953 | /// Create a 'ret <val>' instruction. | |||
954 | ReturnInst *CreateRet(Value *V) { | |||
955 | return Insert(ReturnInst::Create(Context, V)); | |||
956 | } | |||
957 | ||||
958 | /// Create a sequence of N insertvalue instructions, | |||
959 | /// with one Value from the retVals array each, that build a aggregate | |||
960 | /// return value one value at a time, and a ret instruction to return | |||
961 | /// the resulting aggregate value. | |||
962 | /// | |||
963 | /// This is a convenience function for code that uses aggregate return values | |||
964 | /// as a vehicle for having multiple return values. | |||
965 | ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) { | |||
966 | Value *V = UndefValue::get(getCurrentFunctionReturnType()); | |||
967 | for (unsigned i = 0; i != N; ++i) | |||
968 | V = CreateInsertValue(V, retVals[i], i, "mrv"); | |||
969 | return Insert(ReturnInst::Create(Context, V)); | |||
970 | } | |||
971 | ||||
972 | /// Create an unconditional 'br label X' instruction. | |||
973 | BranchInst *CreateBr(BasicBlock *Dest) { | |||
974 | return Insert(BranchInst::Create(Dest)); | |||
975 | } | |||
976 | ||||
977 | /// Create a conditional 'br Cond, TrueDest, FalseDest' | |||
978 | /// instruction. | |||
979 | BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, | |||
980 | MDNode *BranchWeights = nullptr, | |||
981 | MDNode *Unpredictable = nullptr) { | |||
982 | return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond), | |||
983 | BranchWeights, Unpredictable)); | |||
984 | } | |||
985 | ||||
986 | /// Create a conditional 'br Cond, TrueDest, FalseDest' | |||
987 | /// instruction. Copy branch meta data if available. | |||
988 | BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, | |||
989 | Instruction *MDSrc) { | |||
990 | BranchInst *Br = BranchInst::Create(True, False, Cond); | |||
991 | if (MDSrc) { | |||
992 | unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable, | |||
993 | LLVMContext::MD_make_implicit, LLVMContext::MD_dbg}; | |||
994 | Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4)); | |||
995 | } | |||
996 | return Insert(Br); | |||
997 | } | |||
998 | ||||
999 | /// Create a switch instruction with the specified value, default dest, | |||
1000 | /// and with a hint for the number of cases that will be added (for efficient | |||
1001 | /// allocation). | |||
1002 | SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10, | |||
1003 | MDNode *BranchWeights = nullptr, | |||
1004 | MDNode *Unpredictable = nullptr) { | |||
1005 | return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases), | |||
1006 | BranchWeights, Unpredictable)); | |||
1007 | } | |||
1008 | ||||
1009 | /// Create an indirect branch instruction with the specified address | |||
1010 | /// operand, with an optional hint for the number of destinations that will be | |||
1011 | /// added (for efficient allocation). | |||
1012 | IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) { | |||
1013 | return Insert(IndirectBrInst::Create(Addr, NumDests)); | |||
1014 | } | |||
1015 | ||||
1016 | /// Create an invoke instruction. | |||
1017 | InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee, | |||
1018 | BasicBlock *NormalDest, BasicBlock *UnwindDest, | |||
1019 | ArrayRef<Value *> Args, | |||
1020 | ArrayRef<OperandBundleDef> OpBundles, | |||
1021 | const Twine &Name = "") { | |||
1022 | InvokeInst *II = | |||
1023 | InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles); | |||
1024 | if (IsFPConstrained) | |||
1025 | setConstrainedFPCallAttr(II); | |||
1026 | return Insert(II, Name); | |||
1027 | } | |||
1028 | InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee, | |||
1029 | BasicBlock *NormalDest, BasicBlock *UnwindDest, | |||
1030 | ArrayRef<Value *> Args = None, | |||
1031 | const Twine &Name = "") { | |||
1032 | InvokeInst *II = | |||
1033 | InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args); | |||
1034 | if (IsFPConstrained) | |||
1035 | setConstrainedFPCallAttr(II); | |||
1036 | return Insert(II, Name); | |||
1037 | } | |||
1038 | ||||
1039 | InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, | |||
1040 | BasicBlock *UnwindDest, ArrayRef<Value *> Args, | |||
1041 | ArrayRef<OperandBundleDef> OpBundles, | |||
1042 | const Twine &Name = "") { | |||
1043 | return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(), | |||
1044 | NormalDest, UnwindDest, Args, OpBundles, Name); | |||
1045 | } | |||
1046 | ||||
1047 | InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, | |||
1048 | BasicBlock *UnwindDest, | |||
1049 | ArrayRef<Value *> Args = None, | |||
1050 | const Twine &Name = "") { | |||
1051 | return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(), | |||
1052 | NormalDest, UnwindDest, Args, Name); | |||
1053 | } | |||
1054 | ||||
1055 | /// \brief Create a callbr instruction. | |||
1056 | CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee, | |||
1057 | BasicBlock *DefaultDest, | |||
1058 | ArrayRef<BasicBlock *> IndirectDests, | |||
1059 | ArrayRef<Value *> Args = None, | |||
1060 | const Twine &Name = "") { | |||
1061 | return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, | |||
1062 | Args), Name); | |||
1063 | } | |||
1064 | CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee, | |||
1065 | BasicBlock *DefaultDest, | |||
1066 | ArrayRef<BasicBlock *> IndirectDests, | |||
1067 | ArrayRef<Value *> Args, | |||
1068 | ArrayRef<OperandBundleDef> OpBundles, | |||
1069 | const Twine &Name = "") { | |||
1070 | return Insert( | |||
1071 | CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args, | |||
1072 | OpBundles), Name); | |||
1073 | } | |||
1074 | ||||
1075 | CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, | |||
1076 | ArrayRef<BasicBlock *> IndirectDests, | |||
1077 | ArrayRef<Value *> Args = None, | |||
1078 | const Twine &Name = "") { | |||
1079 | return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(), | |||
1080 | DefaultDest, IndirectDests, Args, Name); | |||
1081 | } | |||
1082 | CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, | |||
1083 | ArrayRef<BasicBlock *> IndirectDests, | |||
1084 | ArrayRef<Value *> Args, | |||
1085 | ArrayRef<OperandBundleDef> OpBundles, | |||
1086 | const Twine &Name = "") { | |||
1087 | return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(), | |||
1088 | DefaultDest, IndirectDests, Args, Name); | |||
1089 | } | |||
1090 | ||||
1091 | ResumeInst *CreateResume(Value *Exn) { | |||
1092 | return Insert(ResumeInst::Create(Exn)); | |||
1093 | } | |||
1094 | ||||
1095 | CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad, | |||
1096 | BasicBlock *UnwindBB = nullptr) { | |||
1097 | return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB)); | |||
1098 | } | |||
1099 | ||||
1100 | CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB, | |||
1101 | unsigned NumHandlers, | |||
1102 | const Twine &Name = "") { | |||
1103 | return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers), | |||
1104 | Name); | |||
1105 | } | |||
1106 | ||||
1107 | CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args, | |||
1108 | const Twine &Name = "") { | |||
1109 | return Insert(CatchPadInst::Create(ParentPad, Args), Name); | |||
1110 | } | |||
1111 | ||||
1112 | CleanupPadInst *CreateCleanupPad(Value *ParentPad, | |||
1113 | ArrayRef<Value *> Args = None, | |||
1114 | const Twine &Name = "") { | |||
1115 | return Insert(CleanupPadInst::Create(ParentPad, Args), Name); | |||
1116 | } | |||
1117 | ||||
1118 | CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) { | |||
1119 | return Insert(CatchReturnInst::Create(CatchPad, BB)); | |||
1120 | } | |||
1121 | ||||
1122 | UnreachableInst *CreateUnreachable() { | |||
1123 | return Insert(new UnreachableInst(Context)); | |||
1124 | } | |||
1125 | ||||
1126 | //===--------------------------------------------------------------------===// | |||
1127 | // Instruction creation methods: Binary Operators | |||
1128 | //===--------------------------------------------------------------------===// | |||
1129 | private: | |||
1130 | BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc, | |||
1131 | Value *LHS, Value *RHS, | |||
1132 | const Twine &Name, | |||
1133 | bool HasNUW, bool HasNSW) { | |||
1134 | BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name); | |||
1135 | if (HasNUW) BO->setHasNoUnsignedWrap(); | |||
1136 | if (HasNSW) BO->setHasNoSignedWrap(); | |||
1137 | return BO; | |||
1138 | } | |||
1139 | ||||
1140 | Instruction *setFPAttrs(Instruction *I, MDNode *FPMD, | |||
1141 | FastMathFlags FMF) const { | |||
1142 | if (!FPMD) | |||
1143 | FPMD = DefaultFPMathTag; | |||
1144 | if (FPMD) | |||
1145 | I->setMetadata(LLVMContext::MD_fpmath, FPMD); | |||
1146 | I->setFastMathFlags(FMF); | |||
1147 | return I; | |||
1148 | } | |||
1149 | ||||
1150 | Value *foldConstant(Instruction::BinaryOps Opc, Value *L, | |||
1151 | Value *R, const Twine &Name) const { | |||
1152 | auto *LC = dyn_cast<Constant>(L); | |||
1153 | auto *RC = dyn_cast<Constant>(R); | |||
1154 | return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr; | |||
1155 | } | |||
1156 | ||||
1157 | Value *getConstrainedFPRounding(Optional<RoundingMode> Rounding) { | |||
1158 | RoundingMode UseRounding = DefaultConstrainedRounding; | |||
1159 | ||||
1160 | if (Rounding.hasValue()) | |||
1161 | UseRounding = Rounding.getValue(); | |||
1162 | ||||
1163 | Optional<StringRef> RoundingStr = convertRoundingModeToStr(UseRounding); | |||
1164 | assert(RoundingStr.hasValue() && "Garbage strict rounding mode!")(static_cast <bool> (RoundingStr.hasValue() && "Garbage strict rounding mode!" ) ? void (0) : __assert_fail ("RoundingStr.hasValue() && \"Garbage strict rounding mode!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1164, __extension__ __PRETTY_FUNCTION__ )); | |||
1165 | auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue()); | |||
1166 | ||||
1167 | return MetadataAsValue::get(Context, RoundingMDS); | |||
1168 | } | |||
1169 | ||||
1170 | Value *getConstrainedFPExcept(Optional<fp::ExceptionBehavior> Except) { | |||
1171 | fp::ExceptionBehavior UseExcept = DefaultConstrainedExcept; | |||
1172 | ||||
1173 | if (Except.hasValue()) | |||
1174 | UseExcept = Except.getValue(); | |||
1175 | ||||
1176 | Optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(UseExcept); | |||
1177 | assert(ExceptStr.hasValue() && "Garbage strict exception behavior!")(static_cast <bool> (ExceptStr.hasValue() && "Garbage strict exception behavior!" ) ? void (0) : __assert_fail ("ExceptStr.hasValue() && \"Garbage strict exception behavior!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1177, __extension__ __PRETTY_FUNCTION__ )); | |||
1178 | auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue()); | |||
1179 | ||||
1180 | return MetadataAsValue::get(Context, ExceptMDS); | |||
1181 | } | |||
1182 | ||||
1183 | Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) { | |||
1184 | assert(CmpInst::isFPPredicate(Predicate) &&(static_cast <bool> (CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst ::FCMP_TRUE && "Invalid constrained FP comparison predicate!" ) ? void (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1187, __extension__ __PRETTY_FUNCTION__ )) | |||
1185 | Predicate != CmpInst::FCMP_FALSE &&(static_cast <bool> (CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst ::FCMP_TRUE && "Invalid constrained FP comparison predicate!" ) ? void (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1187, __extension__ __PRETTY_FUNCTION__ )) | |||
1186 | Predicate != CmpInst::FCMP_TRUE &&(static_cast <bool> (CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst ::FCMP_TRUE && "Invalid constrained FP comparison predicate!" ) ? void (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1187, __extension__ __PRETTY_FUNCTION__ )) | |||
1187 | "Invalid constrained FP comparison predicate!")(static_cast <bool> (CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst ::FCMP_TRUE && "Invalid constrained FP comparison predicate!" ) ? void (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1187, __extension__ __PRETTY_FUNCTION__ )); | |||
1188 | ||||
1189 | StringRef PredicateStr = CmpInst::getPredicateName(Predicate); | |||
1190 | auto *PredicateMDS = MDString::get(Context, PredicateStr); | |||
1191 | ||||
1192 | return MetadataAsValue::get(Context, PredicateMDS); | |||
1193 | } | |||
1194 | ||||
1195 | public: | |||
1196 | Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "", | |||
1197 | bool HasNUW = false, bool HasNSW = false) { | |||
1198 | if (auto *V = Folder.FoldAdd(LHS, RHS, HasNUW, HasNSW)) | |||
1199 | return V; | |||
1200 | return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, | |||
1201 | HasNUW, HasNSW); | |||
1202 | } | |||
1203 | ||||
1204 | Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1205 | return CreateAdd(LHS, RHS, Name, false, true); | |||
1206 | } | |||
1207 | ||||
1208 | Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1209 | return CreateAdd(LHS, RHS, Name, true, false); | |||
1210 | } | |||
1211 | ||||
1212 | Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "", | |||
1213 | bool HasNUW = false, bool HasNSW = false) { | |||
1214 | if (auto *LC = dyn_cast<Constant>(LHS)) | |||
1215 | if (auto *RC = dyn_cast<Constant>(RHS)) | |||
1216 | return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name); | |||
1217 | return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, | |||
1218 | HasNUW, HasNSW); | |||
1219 | } | |||
1220 | ||||
1221 | Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1222 | return CreateSub(LHS, RHS, Name, false, true); | |||
1223 | } | |||
1224 | ||||
1225 | Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1226 | return CreateSub(LHS, RHS, Name, true, false); | |||
1227 | } | |||
1228 | ||||
1229 | Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "", | |||
1230 | bool HasNUW = false, bool HasNSW = false) { | |||
1231 | if (auto *LC = dyn_cast<Constant>(LHS)) | |||
1232 | if (auto *RC = dyn_cast<Constant>(RHS)) | |||
1233 | return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name); | |||
1234 | return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, | |||
1235 | HasNUW, HasNSW); | |||
1236 | } | |||
1237 | ||||
1238 | Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1239 | return CreateMul(LHS, RHS, Name, false, true); | |||
1240 | } | |||
1241 | ||||
1242 | Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1243 | return CreateMul(LHS, RHS, Name, true, false); | |||
1244 | } | |||
1245 | ||||
1246 | Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "", | |||
1247 | bool isExact = false) { | |||
1248 | if (auto *LC = dyn_cast<Constant>(LHS)) | |||
1249 | if (auto *RC = dyn_cast<Constant>(RHS)) | |||
1250 | return Insert(Folder.CreateUDiv(LC, RC, isExact), Name); | |||
1251 | if (!isExact) | |||
1252 | return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name); | |||
1253 | return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name); | |||
1254 | } | |||
1255 | ||||
1256 | Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1257 | return CreateUDiv(LHS, RHS, Name, true); | |||
1258 | } | |||
1259 | ||||
1260 | Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "", | |||
1261 | bool isExact = false) { | |||
1262 | if (auto *LC = dyn_cast<Constant>(LHS)) | |||
1263 | if (auto *RC = dyn_cast<Constant>(RHS)) | |||
1264 | return Insert(Folder.CreateSDiv(LC, RC, isExact), Name); | |||
1265 | if (!isExact) | |||
1266 | return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name); | |||
1267 | return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name); | |||
1268 | } | |||
1269 | ||||
1270 | Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1271 | return CreateSDiv(LHS, RHS, Name, true); | |||
1272 | } | |||
1273 | ||||
1274 | Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1275 | if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V; | |||
1276 | return Insert(BinaryOperator::CreateURem(LHS, RHS), Name); | |||
1277 | } | |||
1278 | ||||
1279 | Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1280 | if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V; | |||
1281 | return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name); | |||
1282 | } | |||
1283 | ||||
1284 | Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "", | |||
1285 | bool HasNUW = false, bool HasNSW = false) { | |||
1286 | if (auto *LC = dyn_cast<Constant>(LHS)) | |||
1287 | if (auto *RC = dyn_cast<Constant>(RHS)) | |||
1288 | return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name); | |||
1289 | return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name, | |||
1290 | HasNUW, HasNSW); | |||
1291 | } | |||
1292 | ||||
1293 | Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "", | |||
1294 | bool HasNUW = false, bool HasNSW = false) { | |||
1295 | return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, | |||
1296 | HasNUW, HasNSW); | |||
1297 | } | |||
1298 | ||||
1299 | Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "", | |||
1300 | bool HasNUW = false, bool HasNSW = false) { | |||
1301 | return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, | |||
1302 | HasNUW, HasNSW); | |||
1303 | } | |||
1304 | ||||
1305 | Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "", | |||
1306 | bool isExact = false) { | |||
1307 | if (auto *LC = dyn_cast<Constant>(LHS)) | |||
1308 | if (auto *RC = dyn_cast<Constant>(RHS)) | |||
1309 | return Insert(Folder.CreateLShr(LC, RC, isExact), Name); | |||
1310 | if (!isExact) | |||
1311 | return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name); | |||
1312 | return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name); | |||
1313 | } | |||
1314 | ||||
1315 | Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "", | |||
1316 | bool isExact = false) { | |||
1317 | return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); | |||
1318 | } | |||
1319 | ||||
1320 | Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "", | |||
1321 | bool isExact = false) { | |||
1322 | return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); | |||
1323 | } | |||
1324 | ||||
1325 | Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "", | |||
1326 | bool isExact = false) { | |||
1327 | if (auto *LC = dyn_cast<Constant>(LHS)) | |||
1328 | if (auto *RC = dyn_cast<Constant>(RHS)) | |||
1329 | return Insert(Folder.CreateAShr(LC, RC, isExact), Name); | |||
1330 | if (!isExact) | |||
1331 | return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name); | |||
1332 | return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name); | |||
1333 | } | |||
1334 | ||||
1335 | Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "", | |||
1336 | bool isExact = false) { | |||
1337 | return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); | |||
1338 | } | |||
1339 | ||||
1340 | Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "", | |||
1341 | bool isExact = false) { | |||
1342 | return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); | |||
1343 | } | |||
1344 | ||||
1345 | Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1346 | if (auto *V = Folder.FoldAnd(LHS, RHS)) | |||
1347 | return V; | |||
1348 | return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name); | |||
1349 | } | |||
1350 | ||||
1351 | Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") { | |||
1352 | return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | |||
1353 | } | |||
1354 | ||||
1355 | Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") { | |||
1356 | return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | |||
1357 | } | |||
1358 | ||||
1359 | Value *CreateAnd(ArrayRef<Value*> Ops) { | |||
1360 | assert(!Ops.empty())(static_cast <bool> (!Ops.empty()) ? void (0) : __assert_fail ("!Ops.empty()", "llvm/include/llvm/IR/IRBuilder.h", 1360, __extension__ __PRETTY_FUNCTION__)); | |||
1361 | Value *Accum = Ops[0]; | |||
1362 | for (unsigned i = 1; i < Ops.size(); i++) | |||
1363 | Accum = CreateAnd(Accum, Ops[i]); | |||
1364 | return Accum; | |||
1365 | } | |||
1366 | ||||
1367 | Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1368 | if (auto *V = Folder.FoldOr(LHS, RHS)) | |||
1369 | return V; | |||
1370 | return Insert(BinaryOperator::CreateOr(LHS, RHS), Name); | |||
1371 | } | |||
1372 | ||||
1373 | Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") { | |||
1374 | return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | |||
1375 | } | |||
1376 | ||||
1377 | Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") { | |||
1378 | return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | |||
1379 | } | |||
1380 | ||||
1381 | Value *CreateOr(ArrayRef<Value*> Ops) { | |||
1382 | assert(!Ops.empty())(static_cast <bool> (!Ops.empty()) ? void (0) : __assert_fail ("!Ops.empty()", "llvm/include/llvm/IR/IRBuilder.h", 1382, __extension__ __PRETTY_FUNCTION__)); | |||
1383 | Value *Accum = Ops[0]; | |||
1384 | for (unsigned i = 1; i < Ops.size(); i++) | |||
1385 | Accum = CreateOr(Accum, Ops[i]); | |||
1386 | return Accum; | |||
1387 | } | |||
1388 | ||||
1389 | Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
1390 | if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V; | |||
1391 | return Insert(BinaryOperator::CreateXor(LHS, RHS), Name); | |||
1392 | } | |||
1393 | ||||
1394 | Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") { | |||
1395 | return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | |||
1396 | } | |||
1397 | ||||
1398 | Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") { | |||
1399 | return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); | |||
1400 | } | |||
1401 | ||||
1402 | Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "", | |||
1403 | MDNode *FPMD = nullptr) { | |||
1404 | if (IsFPConstrained) | |||
1405 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd, | |||
1406 | L, R, nullptr, Name, FPMD); | |||
1407 | ||||
1408 | if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V; | |||
1409 | Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF); | |||
1410 | return Insert(I, Name); | |||
1411 | } | |||
1412 | ||||
1413 | /// Copy fast-math-flags from an instruction rather than using the builder's | |||
1414 | /// default FMF. | |||
1415 | Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource, | |||
1416 | const Twine &Name = "") { | |||
1417 | if (IsFPConstrained) | |||
1418 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd, | |||
1419 | L, R, FMFSource, Name); | |||
1420 | ||||
1421 | if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V; | |||
1422 | Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr, | |||
1423 | FMFSource->getFastMathFlags()); | |||
1424 | return Insert(I, Name); | |||
1425 | } | |||
1426 | ||||
1427 | Value *CreateFSub(Value *L, Value *R, const Twine &Name = "", | |||
1428 | MDNode *FPMD = nullptr) { | |||
1429 | if (IsFPConstrained) | |||
1430 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub, | |||
1431 | L, R, nullptr, Name, FPMD); | |||
1432 | ||||
1433 | if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V; | |||
1434 | Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF); | |||
1435 | return Insert(I, Name); | |||
1436 | } | |||
1437 | ||||
1438 | /// Copy fast-math-flags from an instruction rather than using the builder's | |||
1439 | /// default FMF. | |||
1440 | Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource, | |||
1441 | const Twine &Name = "") { | |||
1442 | if (IsFPConstrained) | |||
1443 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub, | |||
1444 | L, R, FMFSource, Name); | |||
1445 | ||||
1446 | if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V; | |||
1447 | Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr, | |||
1448 | FMFSource->getFastMathFlags()); | |||
1449 | return Insert(I, Name); | |||
1450 | } | |||
1451 | ||||
1452 | Value *CreateFMul(Value *L, Value *R, const Twine &Name = "", | |||
1453 | MDNode *FPMD = nullptr) { | |||
1454 | if (IsFPConstrained) | |||
1455 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul, | |||
1456 | L, R, nullptr, Name, FPMD); | |||
1457 | ||||
1458 | if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V; | |||
1459 | Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF); | |||
1460 | return Insert(I, Name); | |||
1461 | } | |||
1462 | ||||
1463 | /// Copy fast-math-flags from an instruction rather than using the builder's | |||
1464 | /// default FMF. | |||
1465 | Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource, | |||
1466 | const Twine &Name = "") { | |||
1467 | if (IsFPConstrained) | |||
1468 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul, | |||
1469 | L, R, FMFSource, Name); | |||
1470 | ||||
1471 | if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V; | |||
1472 | Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr, | |||
1473 | FMFSource->getFastMathFlags()); | |||
1474 | return Insert(I, Name); | |||
1475 | } | |||
1476 | ||||
1477 | Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "", | |||
1478 | MDNode *FPMD = nullptr) { | |||
1479 | if (IsFPConstrained) | |||
1480 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv, | |||
1481 | L, R, nullptr, Name, FPMD); | |||
1482 | ||||
1483 | if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V; | |||
1484 | Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF); | |||
1485 | return Insert(I, Name); | |||
1486 | } | |||
1487 | ||||
1488 | /// Copy fast-math-flags from an instruction rather than using the builder's | |||
1489 | /// default FMF. | |||
1490 | Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource, | |||
1491 | const Twine &Name = "") { | |||
1492 | if (IsFPConstrained) | |||
1493 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv, | |||
1494 | L, R, FMFSource, Name); | |||
1495 | ||||
1496 | if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V; | |||
1497 | Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr, | |||
1498 | FMFSource->getFastMathFlags()); | |||
1499 | return Insert(I, Name); | |||
1500 | } | |||
1501 | ||||
1502 | Value *CreateFRem(Value *L, Value *R, const Twine &Name = "", | |||
1503 | MDNode *FPMD = nullptr) { | |||
1504 | if (IsFPConstrained) | |||
1505 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem, | |||
1506 | L, R, nullptr, Name, FPMD); | |||
1507 | ||||
1508 | if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V; | |||
1509 | Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF); | |||
1510 | return Insert(I, Name); | |||
1511 | } | |||
1512 | ||||
1513 | /// Copy fast-math-flags from an instruction rather than using the builder's | |||
1514 | /// default FMF. | |||
1515 | Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource, | |||
1516 | const Twine &Name = "") { | |||
1517 | if (IsFPConstrained) | |||
1518 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem, | |||
1519 | L, R, FMFSource, Name); | |||
1520 | ||||
1521 | if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V; | |||
1522 | Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr, | |||
1523 | FMFSource->getFastMathFlags()); | |||
1524 | return Insert(I, Name); | |||
1525 | } | |||
1526 | ||||
1527 | Value *CreateBinOp(Instruction::BinaryOps Opc, | |||
1528 | Value *LHS, Value *RHS, const Twine &Name = "", | |||
1529 | MDNode *FPMathTag = nullptr) { | |||
1530 | if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V; | |||
1531 | Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS); | |||
1532 | if (isa<FPMathOperator>(BinOp)) | |||
1533 | setFPAttrs(BinOp, FPMathTag, FMF); | |||
1534 | return Insert(BinOp, Name); | |||
1535 | } | |||
1536 | ||||
1537 | Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") { | |||
1538 | assert(Cond2->getType()->isIntOrIntVectorTy(1))(static_cast <bool> (Cond2->getType()->isIntOrIntVectorTy (1)) ? void (0) : __assert_fail ("Cond2->getType()->isIntOrIntVectorTy(1)" , "llvm/include/llvm/IR/IRBuilder.h", 1538, __extension__ __PRETTY_FUNCTION__ )); | |||
1539 | return CreateSelect(Cond1, Cond2, | |||
1540 | ConstantInt::getNullValue(Cond2->getType()), Name); | |||
1541 | } | |||
1542 | ||||
1543 | Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") { | |||
1544 | assert(Cond2->getType()->isIntOrIntVectorTy(1))(static_cast <bool> (Cond2->getType()->isIntOrIntVectorTy (1)) ? void (0) : __assert_fail ("Cond2->getType()->isIntOrIntVectorTy(1)" , "llvm/include/llvm/IR/IRBuilder.h", 1544, __extension__ __PRETTY_FUNCTION__ )); | |||
1545 | return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()), | |||
1546 | Cond2, Name); | |||
1547 | } | |||
1548 | ||||
1549 | // NOTE: this is sequential, non-commutative, ordered reduction! | |||
1550 | Value *CreateLogicalOr(ArrayRef<Value *> Ops) { | |||
1551 | assert(!Ops.empty())(static_cast <bool> (!Ops.empty()) ? void (0) : __assert_fail ("!Ops.empty()", "llvm/include/llvm/IR/IRBuilder.h", 1551, __extension__ __PRETTY_FUNCTION__)); | |||
1552 | Value *Accum = Ops[0]; | |||
1553 | for (unsigned i = 1; i < Ops.size(); i++) | |||
1554 | Accum = CreateLogicalOr(Accum, Ops[i]); | |||
1555 | return Accum; | |||
1556 | } | |||
1557 | ||||
1558 | CallInst *CreateConstrainedFPBinOp( | |||
1559 | Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr, | |||
1560 | const Twine &Name = "", MDNode *FPMathTag = nullptr, | |||
1561 | Optional<RoundingMode> Rounding = None, | |||
1562 | Optional<fp::ExceptionBehavior> Except = None); | |||
1563 | ||||
1564 | Value *CreateNeg(Value *V, const Twine &Name = "", | |||
1565 | bool HasNUW = false, bool HasNSW = false) { | |||
1566 | if (auto *VC = dyn_cast<Constant>(V)) | |||
1567 | return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name); | |||
1568 | BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name); | |||
1569 | if (HasNUW) BO->setHasNoUnsignedWrap(); | |||
1570 | if (HasNSW) BO->setHasNoSignedWrap(); | |||
1571 | return BO; | |||
1572 | } | |||
1573 | ||||
1574 | Value *CreateNSWNeg(Value *V, const Twine &Name = "") { | |||
1575 | return CreateNeg(V, Name, false, true); | |||
1576 | } | |||
1577 | ||||
1578 | Value *CreateNUWNeg(Value *V, const Twine &Name = "") { | |||
1579 | return CreateNeg(V, Name, true, false); | |||
1580 | } | |||
1581 | ||||
1582 | Value *CreateFNeg(Value *V, const Twine &Name = "", | |||
1583 | MDNode *FPMathTag = nullptr) { | |||
1584 | if (auto *VC = dyn_cast<Constant>(V)) | |||
1585 | return Insert(Folder.CreateFNeg(VC), Name); | |||
1586 | return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF), | |||
1587 | Name); | |||
1588 | } | |||
1589 | ||||
1590 | /// Copy fast-math-flags from an instruction rather than using the builder's | |||
1591 | /// default FMF. | |||
1592 | Value *CreateFNegFMF(Value *V, Instruction *FMFSource, | |||
1593 | const Twine &Name = "") { | |||
1594 | if (auto *VC = dyn_cast<Constant>(V)) | |||
1595 | return Insert(Folder.CreateFNeg(VC), Name); | |||
1596 | return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr, | |||
1597 | FMFSource->getFastMathFlags()), | |||
1598 | Name); | |||
1599 | } | |||
1600 | ||||
1601 | Value *CreateNot(Value *V, const Twine &Name = "") { | |||
1602 | if (auto *VC = dyn_cast<Constant>(V)) | |||
1603 | return Insert(Folder.CreateNot(VC), Name); | |||
1604 | return Insert(BinaryOperator::CreateNot(V), Name); | |||
1605 | } | |||
1606 | ||||
1607 | Value *CreateUnOp(Instruction::UnaryOps Opc, | |||
1608 | Value *V, const Twine &Name = "", | |||
1609 | MDNode *FPMathTag = nullptr) { | |||
1610 | if (auto *VC = dyn_cast<Constant>(V)) | |||
1611 | return Insert(Folder.CreateUnOp(Opc, VC), Name); | |||
1612 | Instruction *UnOp = UnaryOperator::Create(Opc, V); | |||
1613 | if (isa<FPMathOperator>(UnOp)) | |||
1614 | setFPAttrs(UnOp, FPMathTag, FMF); | |||
1615 | return Insert(UnOp, Name); | |||
1616 | } | |||
1617 | ||||
1618 | /// Create either a UnaryOperator or BinaryOperator depending on \p Opc. | |||
1619 | /// Correct number of operands must be passed accordingly. | |||
1620 | Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops, | |||
1621 | const Twine &Name = "", MDNode *FPMathTag = nullptr); | |||
1622 | ||||
1623 | //===--------------------------------------------------------------------===// | |||
1624 | // Instruction creation methods: Memory Instructions | |||
1625 | //===--------------------------------------------------------------------===// | |||
1626 | ||||
1627 | AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace, | |||
1628 | Value *ArraySize = nullptr, const Twine &Name = "") { | |||
1629 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1630 | Align AllocaAlign = DL.getPrefTypeAlign(Ty); | |||
1631 | return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name); | |||
1632 | } | |||
1633 | ||||
1634 | AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr, | |||
1635 | const Twine &Name = "") { | |||
1636 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1637 | Align AllocaAlign = DL.getPrefTypeAlign(Ty); | |||
1638 | unsigned AddrSpace = DL.getAllocaAddrSpace(); | |||
1639 | return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name); | |||
1640 | } | |||
1641 | ||||
1642 | /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of | |||
1643 | /// converting the string to 'bool' for the isVolatile parameter. | |||
1644 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) { | |||
1645 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name); | |||
1646 | } | |||
1647 | ||||
1648 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") { | |||
1649 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name); | |||
1650 | } | |||
1651 | ||||
1652 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile, | |||
1653 | const Twine &Name = "") { | |||
1654 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name); | |||
1655 | } | |||
1656 | ||||
1657 | StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) { | |||
1658 | return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile); | |||
1659 | } | |||
1660 | ||||
1661 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, | |||
1662 | const char *Name) { | |||
1663 | return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name); | |||
1664 | } | |||
1665 | ||||
1666 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, | |||
1667 | const Twine &Name = "") { | |||
1668 | return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name); | |||
1669 | } | |||
1670 | ||||
1671 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, | |||
1672 | bool isVolatile, const Twine &Name = "") { | |||
1673 | if (!Align) { | |||
1674 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1675 | Align = DL.getABITypeAlign(Ty); | |||
1676 | } | |||
1677 | return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name); | |||
1678 | } | |||
1679 | ||||
1680 | StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, | |||
1681 | bool isVolatile = false) { | |||
1682 | if (!Align) { | |||
1683 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1684 | Align = DL.getABITypeAlign(Val->getType()); | |||
1685 | } | |||
1686 | return Insert(new StoreInst(Val, Ptr, isVolatile, *Align)); | |||
1687 | } | |||
1688 | FenceInst *CreateFence(AtomicOrdering Ordering, | |||
1689 | SyncScope::ID SSID = SyncScope::System, | |||
1690 | const Twine &Name = "") { | |||
1691 | return Insert(new FenceInst(Context, Ordering, SSID), Name); | |||
1692 | } | |||
1693 | ||||
1694 | AtomicCmpXchgInst * | |||
1695 | CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align, | |||
1696 | AtomicOrdering SuccessOrdering, | |||
1697 | AtomicOrdering FailureOrdering, | |||
1698 | SyncScope::ID SSID = SyncScope::System) { | |||
1699 | if (!Align) { | |||
1700 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1701 | Align = llvm::Align(DL.getTypeStoreSize(New->getType())); | |||
1702 | } | |||
1703 | ||||
1704 | return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering, | |||
1705 | FailureOrdering, SSID)); | |||
1706 | } | |||
1707 | ||||
1708 | AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, | |||
1709 | Value *Val, MaybeAlign Align, | |||
1710 | AtomicOrdering Ordering, | |||
1711 | SyncScope::ID SSID = SyncScope::System) { | |||
1712 | if (!Align) { | |||
1713 | const DataLayout &DL = BB->getModule()->getDataLayout(); | |||
1714 | Align = llvm::Align(DL.getTypeStoreSize(Val->getType())); | |||
1715 | } | |||
1716 | ||||
1717 | return Insert(new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID)); | |||
1718 | } | |||
1719 | ||||
1720 | Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, | |||
1721 | const Twine &Name = "") { | |||
1722 | if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, /*IsInBounds=*/false)) | |||
1723 | return V; | |||
1724 | return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name); | |||
1725 | } | |||
1726 | ||||
1727 | Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, | |||
1728 | const Twine &Name = "") { | |||
1729 | if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, /*IsInBounds=*/true)) | |||
1730 | return V; | |||
1731 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name); | |||
1732 | } | |||
1733 | ||||
1734 | Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") { | |||
1735 | if (auto *V = Folder.FoldGEP(Ty, Ptr, {Idx}, /*IsInBounds=*/false)) | |||
1736 | return V; | |||
1737 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); | |||
1738 | } | |||
1739 | ||||
1740 | Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx, | |||
1741 | const Twine &Name = "") { | |||
1742 | if (auto *V = Folder.FoldGEP(Ty, Ptr, {Idx}, /*IsInBounds=*/true)) | |||
1743 | return V; | |||
1744 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); | |||
1745 | } | |||
1746 | ||||
1747 | Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, | |||
1748 | const Twine &Name = "") { | |||
1749 | Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); | |||
1750 | ||||
1751 | if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/false)) | |||
1752 | return V; | |||
1753 | ||||
1754 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); | |||
1755 | } | |||
1756 | ||||
1757 | Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, | |||
1758 | const Twine &Name = "") { | |||
1759 | Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); | |||
1760 | ||||
1761 | if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/true)) | |||
1762 | return V; | |||
1763 | ||||
1764 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); | |||
1765 | } | |||
1766 | ||||
1767 | Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, | |||
1768 | const Twine &Name = "") { | |||
1769 | Value *Idxs[] = { | |||
1770 | ConstantInt::get(Type::getInt32Ty(Context), Idx0), | |||
1771 | ConstantInt::get(Type::getInt32Ty(Context), Idx1) | |||
1772 | }; | |||
1773 | ||||
1774 | if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/false)) | |||
1775 | return V; | |||
1776 | ||||
1777 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); | |||
1778 | } | |||
1779 | ||||
1780 | Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, | |||
1781 | unsigned Idx1, const Twine &Name = "") { | |||
1782 | Value *Idxs[] = { | |||
1783 | ConstantInt::get(Type::getInt32Ty(Context), Idx0), | |||
1784 | ConstantInt::get(Type::getInt32Ty(Context), Idx1) | |||
1785 | }; | |||
1786 | ||||
1787 | if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/true)) | |||
1788 | return V; | |||
1789 | ||||
1790 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); | |||
1791 | } | |||
1792 | ||||
1793 | Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, | |||
1794 | const Twine &Name = "") { | |||
1795 | Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); | |||
1796 | ||||
1797 | if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/false)) | |||
1798 | return V; | |||
1799 | ||||
1800 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); | |||
1801 | } | |||
1802 | ||||
1803 | Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, | |||
1804 | const Twine &Name = "") { | |||
1805 | Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); | |||
1806 | ||||
1807 | if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/true)) | |||
1808 | return V; | |||
1809 | ||||
1810 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); | |||
1811 | } | |||
1812 | ||||
1813 | Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, | |||
1814 | const Twine &Name = "") { | |||
1815 | Value *Idxs[] = { | |||
1816 | ConstantInt::get(Type::getInt64Ty(Context), Idx0), | |||
1817 | ConstantInt::get(Type::getInt64Ty(Context), Idx1) | |||
1818 | }; | |||
1819 | ||||
1820 | if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/false)) | |||
1821 | return V; | |||
1822 | ||||
1823 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); | |||
1824 | } | |||
1825 | ||||
1826 | Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, | |||
1827 | uint64_t Idx1, const Twine &Name = "") { | |||
1828 | Value *Idxs[] = { | |||
1829 | ConstantInt::get(Type::getInt64Ty(Context), Idx0), | |||
1830 | ConstantInt::get(Type::getInt64Ty(Context), Idx1) | |||
1831 | }; | |||
1832 | ||||
1833 | if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/true)) | |||
1834 | return V; | |||
1835 | ||||
1836 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); | |||
1837 | } | |||
1838 | ||||
1839 | Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx, | |||
1840 | const Twine &Name = "") { | |||
1841 | return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name); | |||
1842 | } | |||
1843 | ||||
1844 | /// Same as CreateGlobalString, but return a pointer with "i8*" type | |||
1845 | /// instead of a pointer to array of i8. | |||
1846 | /// | |||
1847 | /// If no module is given via \p M, it is take from the insertion point basic | |||
1848 | /// block. | |||
1849 | Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "", | |||
1850 | unsigned AddressSpace = 0, | |||
1851 | Module *M = nullptr) { | |||
1852 | GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M); | |||
1853 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0); | |||
1854 | Constant *Indices[] = {Zero, Zero}; | |||
1855 | return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV, | |||
1856 | Indices); | |||
1857 | } | |||
1858 | ||||
1859 | //===--------------------------------------------------------------------===// | |||
1860 | // Instruction creation methods: Cast/Conversion Operators | |||
1861 | //===--------------------------------------------------------------------===// | |||
1862 | ||||
1863 | Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") { | |||
1864 | return CreateCast(Instruction::Trunc, V, DestTy, Name); | |||
1865 | } | |||
1866 | ||||
1867 | Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") { | |||
1868 | return CreateCast(Instruction::ZExt, V, DestTy, Name); | |||
1869 | } | |||
1870 | ||||
1871 | Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") { | |||
1872 | return CreateCast(Instruction::SExt, V, DestTy, Name); | |||
1873 | } | |||
1874 | ||||
1875 | /// Create a ZExt or Trunc from the integer value V to DestTy. Return | |||
1876 | /// the value untouched if the type of V is already DestTy. | |||
1877 | Value *CreateZExtOrTrunc(Value *V, Type *DestTy, | |||
1878 | const Twine &Name = "") { | |||
1879 | assert(V->getType()->isIntOrIntVectorTy() &&(static_cast <bool> (V->getType()->isIntOrIntVectorTy () && DestTy->isIntOrIntVectorTy() && "Can only zero extend/truncate integers!" ) ? void (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1881, __extension__ __PRETTY_FUNCTION__ )) | |||
1880 | DestTy->isIntOrIntVectorTy() &&(static_cast <bool> (V->getType()->isIntOrIntVectorTy () && DestTy->isIntOrIntVectorTy() && "Can only zero extend/truncate integers!" ) ? void (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1881, __extension__ __PRETTY_FUNCTION__ )) | |||
1881 | "Can only zero extend/truncate integers!")(static_cast <bool> (V->getType()->isIntOrIntVectorTy () && DestTy->isIntOrIntVectorTy() && "Can only zero extend/truncate integers!" ) ? void (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1881, __extension__ __PRETTY_FUNCTION__ )); | |||
1882 | Type *VTy = V->getType(); | |||
1883 | if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) | |||
1884 | return CreateZExt(V, DestTy, Name); | |||
1885 | if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) | |||
1886 | return CreateTrunc(V, DestTy, Name); | |||
1887 | return V; | |||
1888 | } | |||
1889 | ||||
1890 | /// Create a SExt or Trunc from the integer value V to DestTy. Return | |||
1891 | /// the value untouched if the type of V is already DestTy. | |||
1892 | Value *CreateSExtOrTrunc(Value *V, Type *DestTy, | |||
1893 | const Twine &Name = "") { | |||
1894 | assert(V->getType()->isIntOrIntVectorTy() &&(static_cast <bool> (V->getType()->isIntOrIntVectorTy () && DestTy->isIntOrIntVectorTy() && "Can only sign extend/truncate integers!" ) ? void (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1896, __extension__ __PRETTY_FUNCTION__ )) | |||
1895 | DestTy->isIntOrIntVectorTy() &&(static_cast <bool> (V->getType()->isIntOrIntVectorTy () && DestTy->isIntOrIntVectorTy() && "Can only sign extend/truncate integers!" ) ? void (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1896, __extension__ __PRETTY_FUNCTION__ )) | |||
1896 | "Can only sign extend/truncate integers!")(static_cast <bool> (V->getType()->isIntOrIntVectorTy () && DestTy->isIntOrIntVectorTy() && "Can only sign extend/truncate integers!" ) ? void (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\"" , "llvm/include/llvm/IR/IRBuilder.h", 1896, __extension__ __PRETTY_FUNCTION__ )); | |||
1897 | Type *VTy = V->getType(); | |||
1898 | if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) | |||
1899 | return CreateSExt(V, DestTy, Name); | |||
1900 | if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) | |||
1901 | return CreateTrunc(V, DestTy, Name); | |||
1902 | return V; | |||
1903 | } | |||
1904 | ||||
1905 | Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") { | |||
1906 | if (IsFPConstrained) | |||
1907 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui, | |||
1908 | V, DestTy, nullptr, Name); | |||
1909 | return CreateCast(Instruction::FPToUI, V, DestTy, Name); | |||
1910 | } | |||
1911 | ||||
1912 | Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") { | |||
1913 | if (IsFPConstrained) | |||
1914 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi, | |||
1915 | V, DestTy, nullptr, Name); | |||
1916 | return CreateCast(Instruction::FPToSI, V, DestTy, Name); | |||
1917 | } | |||
1918 | ||||
1919 | Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ | |||
1920 | if (IsFPConstrained) | |||
1921 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp, | |||
1922 | V, DestTy, nullptr, Name); | |||
1923 | return CreateCast(Instruction::UIToFP, V, DestTy, Name); | |||
1924 | } | |||
1925 | ||||
1926 | Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ | |||
1927 | if (IsFPConstrained) | |||
1928 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp, | |||
1929 | V, DestTy, nullptr, Name); | |||
1930 | return CreateCast(Instruction::SIToFP, V, DestTy, Name); | |||
1931 | } | |||
1932 | ||||
1933 | Value *CreateFPTrunc(Value *V, Type *DestTy, | |||
1934 | const Twine &Name = "") { | |||
1935 | if (IsFPConstrained) | |||
1936 | return CreateConstrainedFPCast( | |||
1937 | Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr, | |||
1938 | Name); | |||
1939 | return CreateCast(Instruction::FPTrunc, V, DestTy, Name); | |||
1940 | } | |||
1941 | ||||
1942 | Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") { | |||
1943 | if (IsFPConstrained) | |||
1944 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext, | |||
1945 | V, DestTy, nullptr, Name); | |||
1946 | return CreateCast(Instruction::FPExt, V, DestTy, Name); | |||
1947 | } | |||
1948 | ||||
1949 | Value *CreatePtrToInt(Value *V, Type *DestTy, | |||
1950 | const Twine &Name = "") { | |||
1951 | return CreateCast(Instruction::PtrToInt, V, DestTy, Name); | |||
1952 | } | |||
1953 | ||||
1954 | Value *CreateIntToPtr(Value *V, Type *DestTy, | |||
1955 | const Twine &Name = "") { | |||
1956 | return CreateCast(Instruction::IntToPtr, V, DestTy, Name); | |||
1957 | } | |||
1958 | ||||
1959 | Value *CreateBitCast(Value *V, Type *DestTy, | |||
1960 | const Twine &Name = "") { | |||
1961 | return CreateCast(Instruction::BitCast, V, DestTy, Name); | |||
1962 | } | |||
1963 | ||||
1964 | Value *CreateAddrSpaceCast(Value *V, Type *DestTy, | |||
1965 | const Twine &Name = "") { | |||
1966 | return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name); | |||
1967 | } | |||
1968 | ||||
1969 | Value *CreateZExtOrBitCast(Value *V, Type *DestTy, | |||
1970 | const Twine &Name = "") { | |||
1971 | if (V->getType() == DestTy) | |||
1972 | return V; | |||
1973 | if (auto *VC = dyn_cast<Constant>(V)) | |||
1974 | return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name); | |||
1975 | return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name); | |||
1976 | } | |||
1977 | ||||
1978 | Value *CreateSExtOrBitCast(Value *V, Type *DestTy, | |||
1979 | const Twine &Name = "") { | |||
1980 | if (V->getType() == DestTy) | |||
1981 | return V; | |||
1982 | if (auto *VC = dyn_cast<Constant>(V)) | |||
1983 | return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name); | |||
1984 | return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name); | |||
1985 | } | |||
1986 | ||||
1987 | Value *CreateTruncOrBitCast(Value *V, Type *DestTy, | |||
1988 | const Twine &Name = "") { | |||
1989 | if (V->getType() == DestTy) | |||
1990 | return V; | |||
1991 | if (auto *VC = dyn_cast<Constant>(V)) | |||
1992 | return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name); | |||
1993 | return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name); | |||
1994 | } | |||
1995 | ||||
1996 | Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, | |||
1997 | const Twine &Name = "") { | |||
1998 | if (V->getType() == DestTy) | |||
1999 | return V; | |||
2000 | if (auto *VC = dyn_cast<Constant>(V)) | |||
2001 | return Insert(Folder.CreateCast(Op, VC, DestTy), Name); | |||
2002 | return Insert(CastInst::Create(Op, V, DestTy), Name); | |||
2003 | } | |||
2004 | ||||
2005 | Value *CreatePointerCast(Value *V, Type *DestTy, | |||
2006 | const Twine &Name = "") { | |||
2007 | if (V->getType() == DestTy) | |||
2008 | return V; | |||
2009 | if (auto *VC = dyn_cast<Constant>(V)) | |||
2010 | return Insert(Folder.CreatePointerCast(VC, DestTy), Name); | |||
2011 | return Insert(CastInst::CreatePointerCast(V, DestTy), Name); | |||
2012 | } | |||
2013 | ||||
2014 | Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy, | |||
2015 | const Twine &Name = "") { | |||
2016 | if (V->getType() == DestTy) | |||
2017 | return V; | |||
2018 | ||||
2019 | if (auto *VC = dyn_cast<Constant>(V)) { | |||
2020 | return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy), | |||
2021 | Name); | |||
2022 | } | |||
2023 | ||||
2024 | return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy), | |||
2025 | Name); | |||
2026 | } | |||
2027 | ||||
2028 | Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned, | |||
2029 | const Twine &Name = "") { | |||
2030 | if (V->getType() == DestTy) | |||
2031 | return V; | |||
2032 | if (auto *VC = dyn_cast<Constant>(V)) | |||
2033 | return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name); | |||
2034 | return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name); | |||
2035 | } | |||
2036 | ||||
2037 | Value *CreateBitOrPointerCast(Value *V, Type *DestTy, | |||
2038 | const Twine &Name = "") { | |||
2039 | if (V->getType() == DestTy) | |||
2040 | return V; | |||
2041 | if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy()) | |||
2042 | return CreatePtrToInt(V, DestTy, Name); | |||
2043 | if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy()) | |||
2044 | return CreateIntToPtr(V, DestTy, Name); | |||
2045 | ||||
2046 | return CreateBitCast(V, DestTy, Name); | |||
2047 | } | |||
2048 | ||||
2049 | Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") { | |||
2050 | if (V->getType() == DestTy) | |||
2051 | return V; | |||
2052 | if (auto *VC = dyn_cast<Constant>(V)) | |||
2053 | return Insert(Folder.CreateFPCast(VC, DestTy), Name); | |||
2054 | return Insert(CastInst::CreateFPCast(V, DestTy), Name); | |||
2055 | } | |||
2056 | ||||
2057 | CallInst *CreateConstrainedFPCast( | |||
2058 | Intrinsic::ID ID, Value *V, Type *DestTy, | |||
2059 | Instruction *FMFSource = nullptr, const Twine &Name = "", | |||
2060 | MDNode *FPMathTag = nullptr, | |||
2061 | Optional<RoundingMode> Rounding = None, | |||
2062 | Optional<fp::ExceptionBehavior> Except = None); | |||
2063 | ||||
2064 | // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a | |||
2065 | // compile time error, instead of converting the string to bool for the | |||
2066 | // isSigned parameter. | |||
2067 | Value *CreateIntCast(Value *, Type *, const char *) = delete; | |||
2068 | ||||
2069 | //===--------------------------------------------------------------------===// | |||
2070 | // Instruction creation methods: Compare Instructions | |||
2071 | //===--------------------------------------------------------------------===// | |||
2072 | ||||
2073 | Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2074 | return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name); | |||
2075 | } | |||
2076 | ||||
2077 | Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2078 | return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name); | |||
2079 | } | |||
2080 | ||||
2081 | Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2082 | return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name); | |||
2083 | } | |||
2084 | ||||
2085 | Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2086 | return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name); | |||
2087 | } | |||
2088 | ||||
2089 | Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2090 | return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name); | |||
2091 | } | |||
2092 | ||||
2093 | Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2094 | return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name); | |||
2095 | } | |||
2096 | ||||
2097 | Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2098 | return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name); | |||
2099 | } | |||
2100 | ||||
2101 | Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2102 | return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name); | |||
2103 | } | |||
2104 | ||||
2105 | Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2106 | return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name); | |||
2107 | } | |||
2108 | ||||
2109 | Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") { | |||
2110 | return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name); | |||
2111 | } | |||
2112 | ||||
2113 | Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2114 | MDNode *FPMathTag = nullptr) { | |||
2115 | return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag); | |||
2116 | } | |||
2117 | ||||
2118 | Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2119 | MDNode *FPMathTag = nullptr) { | |||
2120 | return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag); | |||
2121 | } | |||
2122 | ||||
2123 | Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2124 | MDNode *FPMathTag = nullptr) { | |||
2125 | return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag); | |||
2126 | } | |||
2127 | ||||
2128 | Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2129 | MDNode *FPMathTag = nullptr) { | |||
2130 | return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag); | |||
2131 | } | |||
2132 | ||||
2133 | Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2134 | MDNode *FPMathTag = nullptr) { | |||
2135 | return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag); | |||
2136 | } | |||
2137 | ||||
2138 | Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2139 | MDNode *FPMathTag = nullptr) { | |||
2140 | return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag); | |||
2141 | } | |||
2142 | ||||
2143 | Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2144 | MDNode *FPMathTag = nullptr) { | |||
2145 | return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag); | |||
2146 | } | |||
2147 | ||||
2148 | Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2149 | MDNode *FPMathTag = nullptr) { | |||
2150 | return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag); | |||
2151 | } | |||
2152 | ||||
2153 | Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2154 | MDNode *FPMathTag = nullptr) { | |||
2155 | return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag); | |||
2156 | } | |||
2157 | ||||
2158 | Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2159 | MDNode *FPMathTag = nullptr) { | |||
2160 | return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag); | |||
2161 | } | |||
2162 | ||||
2163 | Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2164 | MDNode *FPMathTag = nullptr) { | |||
2165 | return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag); | |||
2166 | } | |||
2167 | ||||
2168 | Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2169 | MDNode *FPMathTag = nullptr) { | |||
2170 | return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag); | |||
2171 | } | |||
2172 | ||||
2173 | Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2174 | MDNode *FPMathTag = nullptr) { | |||
2175 | return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag); | |||
2176 | } | |||
2177 | ||||
2178 | Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "", | |||
2179 | MDNode *FPMathTag = nullptr) { | |||
2180 | return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag); | |||
2181 | } | |||
2182 | ||||
2183 | Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, | |||
2184 | const Twine &Name = "") { | |||
2185 | if (auto *V = Folder.FoldICmp(P, LHS, RHS)) | |||
2186 | return V; | |||
2187 | return Insert(new ICmpInst(P, LHS, RHS), Name); | |||
2188 | } | |||
2189 | ||||
2190 | // Create a quiet floating-point comparison (i.e. one that raises an FP | |||
2191 | // exception only in the case where an input is a signaling NaN). | |||
2192 | // Note that this differs from CreateFCmpS only if IsFPConstrained is true. | |||
2193 | Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, | |||
2194 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | |||
2195 | return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, false); | |||
2196 | } | |||
2197 | ||||
2198 | Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, | |||
2199 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | |||
2200 | return CmpInst::isFPPredicate(Pred) | |||
2201 | ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag) | |||
2202 | : CreateICmp(Pred, LHS, RHS, Name); | |||
2203 | } | |||
2204 | ||||
2205 | // Create a signaling floating-point comparison (i.e. one that raises an FP | |||
2206 | // exception whenever an input is any NaN, signaling or quiet). | |||
2207 | // Note that this differs from CreateFCmp only if IsFPConstrained is true. | |||
2208 | Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS, | |||
2209 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | |||
2210 | return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, true); | |||
2211 | } | |||
2212 | ||||
2213 | private: | |||
2214 | // Helper routine to create either a signaling or a quiet FP comparison. | |||
2215 | Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS, | |||
2216 | const Twine &Name, MDNode *FPMathTag, | |||
2217 | bool IsSignaling); | |||
2218 | ||||
2219 | public: | |||
2220 | CallInst *CreateConstrainedFPCmp( | |||
2221 | Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R, | |||
2222 | const Twine &Name = "", Optional<fp::ExceptionBehavior> Except = None); | |||
2223 | ||||
2224 | //===--------------------------------------------------------------------===// | |||
2225 | // Instruction creation methods: Other Instructions | |||
2226 | //===--------------------------------------------------------------------===// | |||
2227 | ||||
2228 | PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues, | |||
2229 | const Twine &Name = "") { | |||
2230 | PHINode *Phi = PHINode::Create(Ty, NumReservedValues); | |||
2231 | if (isa<FPMathOperator>(Phi)) | |||
2232 | setFPAttrs(Phi, nullptr /* MDNode* */, FMF); | |||
2233 | return Insert(Phi, Name); | |||
2234 | } | |||
2235 | ||||
2236 | CallInst *CreateCall(FunctionType *FTy, Value *Callee, | |||
2237 | ArrayRef<Value *> Args = None, const Twine &Name = "", | |||
2238 | MDNode *FPMathTag = nullptr) { | |||
2239 | CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles); | |||
2240 | if (IsFPConstrained) | |||
2241 | setConstrainedFPCallAttr(CI); | |||
2242 | if (isa<FPMathOperator>(CI)) | |||
2243 | setFPAttrs(CI, FPMathTag, FMF); | |||
2244 | return Insert(CI, Name); | |||
2245 | } | |||
2246 | ||||
2247 | CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args, | |||
2248 | ArrayRef<OperandBundleDef> OpBundles, | |||
2249 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | |||
2250 | CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles); | |||
2251 | if (IsFPConstrained) | |||
2252 | setConstrainedFPCallAttr(CI); | |||
2253 | if (isa<FPMathOperator>(CI)) | |||
2254 | setFPAttrs(CI, FPMathTag, FMF); | |||
2255 | return Insert(CI, Name); | |||
2256 | } | |||
2257 | ||||
2258 | CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None, | |||
2259 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | |||
2260 | return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name, | |||
2261 | FPMathTag); | |||
2262 | } | |||
2263 | ||||
2264 | CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args, | |||
2265 | ArrayRef<OperandBundleDef> OpBundles, | |||
2266 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { | |||
2267 | return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, | |||
2268 | OpBundles, Name, FPMathTag); | |||
2269 | } | |||
2270 | ||||
2271 | CallInst *CreateConstrainedFPCall( | |||
2272 | Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "", | |||
2273 | Optional<RoundingMode> Rounding = None, | |||
2274 | Optional<fp::ExceptionBehavior> Except = None); | |||
2275 | ||||
2276 | Value *CreateSelect(Value *C, Value *True, Value *False, | |||
2277 | const Twine &Name = "", Instruction *MDFrom = nullptr); | |||
2278 | ||||
2279 | VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") { | |||
2280 | return Insert(new VAArgInst(List, Ty), Name); | |||
2281 | } | |||
2282 | ||||
2283 | Value *CreateExtractElement(Value *Vec, Value *Idx, | |||
2284 | const Twine &Name = "") { | |||
2285 | if (auto *VC = dyn_cast<Constant>(Vec)) | |||
2286 | if (auto *IC = dyn_cast<Constant>(Idx)) | |||
2287 | return Insert(Folder.CreateExtractElement(VC, IC), Name); | |||
2288 | return Insert(ExtractElementInst::Create(Vec, Idx), Name); | |||
2289 | } | |||
2290 | ||||
2291 | Value *CreateExtractElement(Value *Vec, uint64_t Idx, | |||
2292 | const Twine &Name = "") { | |||
2293 | return CreateExtractElement(Vec, getInt64(Idx), Name); | |||
2294 | } | |||
2295 | ||||
2296 | Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, | |||
2297 | const Twine &Name = "") { | |||
2298 | return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name); | |||
2299 | } | |||
2300 | ||||
2301 | Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx, | |||
2302 | const Twine &Name = "") { | |||
2303 | return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name); | |||
2304 | } | |||
2305 | ||||
2306 | Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, | |||
2307 | const Twine &Name = "") { | |||
2308 | if (auto *VC = dyn_cast<Constant>(Vec)) | |||
2309 | if (auto *NC = dyn_cast<Constant>(NewElt)) | |||
2310 | if (auto *IC = dyn_cast<Constant>(Idx)) | |||
2311 | return Insert(Folder.CreateInsertElement(VC, NC, IC), Name); | |||
2312 | return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name); | |||
2313 | } | |||
2314 | ||||
2315 | Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx, | |||
2316 | const Twine &Name = "") { | |||
2317 | return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name); | |||
2318 | } | |||
2319 | ||||
2320 | Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask, | |||
2321 | const Twine &Name = "") { | |||
2322 | SmallVector<int, 16> IntMask; | |||
2323 | ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask); | |||
2324 | return CreateShuffleVector(V1, V2, IntMask, Name); | |||
2325 | } | |||
2326 | ||||
2327 | /// See class ShuffleVectorInst for a description of the mask representation. | |||
2328 | Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask, | |||
2329 | const Twine &Name = "") { | |||
2330 | if (auto *V1C = dyn_cast<Constant>(V1)) | |||
2331 | if (auto *V2C = dyn_cast<Constant>(V2)) | |||
2332 | return Insert(Folder.CreateShuffleVector(V1C, V2C, Mask), Name); | |||
2333 | return Insert(new ShuffleVectorInst(V1, V2, Mask), Name); | |||
2334 | } | |||
2335 | ||||
2336 | /// Create a unary shuffle. The second vector operand of the IR instruction | |||
2337 | /// is poison. | |||
2338 | Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask, | |||
2339 | const Twine &Name = "") { | |||
2340 | return CreateShuffleVector(V, PoisonValue::get(V->getType()), Mask, Name); | |||
2341 | } | |||
2342 | ||||
2343 | Value *CreateExtractValue(Value *Agg, | |||
2344 | ArrayRef<unsigned> Idxs, | |||
2345 | const Twine &Name = "") { | |||
2346 | if (auto *AggC = dyn_cast<Constant>(Agg)) | |||
2347 | return Insert(Folder.CreateExtractValue(AggC, Idxs), Name); | |||
2348 | return Insert(ExtractValueInst::Create(Agg, Idxs), Name); | |||
2349 | } | |||
2350 | ||||
2351 | Value *CreateInsertValue(Value *Agg, Value *Val, | |||
2352 | ArrayRef<unsigned> Idxs, | |||
2353 | const Twine &Name = "") { | |||
2354 | if (auto *AggC = dyn_cast<Constant>(Agg)) | |||
2355 | if (auto *ValC = dyn_cast<Constant>(Val)) | |||
2356 | return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name); | |||
2357 | return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name); | |||
2358 | } | |||
2359 | ||||
2360 | LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses, | |||
2361 | const Twine &Name = "") { | |||
2362 | return Insert(LandingPadInst::Create(Ty, NumClauses), Name); | |||
2363 | } | |||
2364 | ||||
2365 | Value *CreateFreeze(Value *V, const Twine &Name = "") { | |||
2366 | return Insert(new FreezeInst(V), Name); | |||
2367 | } | |||
2368 | ||||
2369 | //===--------------------------------------------------------------------===// | |||
2370 | // Utility creation methods | |||
2371 | //===--------------------------------------------------------------------===// | |||
2372 | ||||
2373 | /// Return an i1 value testing if \p Arg is null. | |||
2374 | Value *CreateIsNull(Value *Arg, const Twine &Name = "") { | |||
2375 | return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), | |||
2376 | Name); | |||
2377 | } | |||
2378 | ||||
2379 | /// Return an i1 value testing if \p Arg is not null. | |||
2380 | Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") { | |||
2381 | return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), | |||
2382 | Name); | |||
2383 | } | |||
2384 | ||||
2385 | /// Return the i64 difference between two pointer values, dividing out | |||
2386 | /// the size of the pointed-to objects. | |||
2387 | /// | |||
2388 | /// This is intended to implement C-style pointer subtraction. As such, the | |||
2389 | /// pointers must be appropriately aligned for their element types and | |||
2390 | /// pointing into the same object. | |||
2391 | Value *CreatePtrDiff(Type *ElemTy, Value *LHS, Value *RHS, | |||
2392 | const Twine &Name = ""); | |||
2393 | ||||
2394 | /// Create a launder.invariant.group intrinsic call. If Ptr type is | |||
2395 | /// different from pointer to i8, it's casted to pointer to i8 in the same | |||
2396 | /// address space before call and casted back to Ptr type after call. | |||
2397 | Value *CreateLaunderInvariantGroup(Value *Ptr); | |||
2398 | ||||
2399 | /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is | |||
2400 | /// different from pointer to i8, it's casted to pointer to i8 in the same | |||
2401 | /// address space before call and casted back to Ptr type after call. | |||
2402 | Value *CreateStripInvariantGroup(Value *Ptr); | |||
2403 | ||||
2404 | /// Return a vector value that contains the vector V reversed | |||
2405 | Value *CreateVectorReverse(Value *V, const Twine &Name = ""); | |||
2406 | ||||
2407 | /// Return a vector splice intrinsic if using scalable vectors, otherwise | |||
2408 | /// return a shufflevector. If the immediate is positive, a vector is | |||
2409 | /// extracted from concat(V1, V2), starting at Imm. If the immediate | |||
2410 | /// is negative, we extract -Imm elements from V1 and the remaining | |||
2411 | /// elements from V2. Imm is a signed integer in the range | |||
2412 | /// -VL <= Imm < VL (where VL is the runtime vector length of the | |||
2413 | /// source/result vector) | |||
2414 | Value *CreateVectorSplice(Value *V1, Value *V2, int64_t Imm, | |||
2415 | const Twine &Name = ""); | |||
2416 | ||||
2417 | /// Return a vector value that contains \arg V broadcasted to \p | |||
2418 | /// NumElts elements. | |||
2419 | Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = ""); | |||
2420 | ||||
2421 | /// Return a vector value that contains \arg V broadcasted to \p | |||
2422 | /// EC elements. | |||
2423 | Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = ""); | |||
2424 | ||||
2425 | /// Return a value that has been extracted from a larger integer type. | |||
2426 | Value *CreateExtractInteger(const DataLayout &DL, Value *From, | |||
2427 | IntegerType *ExtractedTy, uint64_t Offset, | |||
2428 | const Twine &Name); | |||
2429 | ||||
2430 | Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base, | |||
2431 | unsigned Dimension, unsigned LastIndex, | |||
2432 | MDNode *DbgInfo); | |||
2433 | ||||
2434 | Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex, | |||
2435 | MDNode *DbgInfo); | |||
2436 | ||||
2437 | Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base, | |||
2438 | unsigned Index, unsigned FieldIndex, | |||
2439 | MDNode *DbgInfo); | |||
2440 | ||||
2441 | private: | |||
2442 | /// Helper function that creates an assume intrinsic call that | |||
2443 | /// represents an alignment assumption on the provided pointer \p PtrValue | |||
2444 | /// with offset \p OffsetValue and alignment value \p AlignValue. | |||
2445 | CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL, | |||
2446 | Value *PtrValue, Value *AlignValue, | |||
2447 | Value *OffsetValue); | |||
2448 | ||||
2449 | public: | |||
2450 | /// Create an assume intrinsic call that represents an alignment | |||
2451 | /// assumption on the provided pointer. | |||
2452 | /// | |||
2453 | /// An optional offset can be provided, and if it is provided, the offset | |||
2454 | /// must be subtracted from the provided pointer to get the pointer with the | |||
2455 | /// specified alignment. | |||
2456 | CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, | |||
2457 | unsigned Alignment, | |||
2458 | Value *OffsetValue = nullptr); | |||
2459 | ||||
2460 | /// Create an assume intrinsic call that represents an alignment | |||
2461 | /// assumption on the provided pointer. | |||
2462 | /// | |||
2463 | /// An optional offset can be provided, and if it is provided, the offset | |||
2464 | /// must be subtracted from the provided pointer to get the pointer with the | |||
2465 | /// specified alignment. | |||
2466 | /// | |||
2467 | /// This overload handles the condition where the Alignment is dependent | |||
2468 | /// on an existing value rather than a static value. | |||
2469 | CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, | |||
2470 | Value *Alignment, | |||
2471 | Value *OffsetValue = nullptr); | |||
2472 | }; | |||
2473 | ||||
2474 | /// This provides a uniform API for creating instructions and inserting | |||
2475 | /// them into a basic block: either at the end of a BasicBlock, or at a specific | |||
2476 | /// iterator location in a block. | |||
2477 | /// | |||
2478 | /// Note that the builder does not expose the full generality of LLVM | |||
2479 | /// instructions. For access to extra instruction properties, use the mutators | |||
2480 | /// (e.g. setVolatile) on the instructions after they have been | |||
2481 | /// created. Convenience state exists to specify fast-math flags and fp-math | |||
2482 | /// tags. | |||
2483 | /// | |||
2484 | /// The first template argument specifies a class to use for creating constants. | |||
2485 | /// This defaults to creating minimally folded constants. The second template | |||
2486 | /// argument allows clients to specify custom insertion hooks that are called on | |||
2487 | /// every newly created insertion. | |||
2488 | template <typename FolderTy = ConstantFolder, | |||
2489 | typename InserterTy = IRBuilderDefaultInserter> | |||
2490 | class IRBuilder : public IRBuilderBase { | |||
2491 | private: | |||
2492 | FolderTy Folder; | |||
2493 | InserterTy Inserter; | |||
2494 | ||||
2495 | public: | |||
2496 | IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(), | |||
2497 | MDNode *FPMathTag = nullptr, | |||
2498 | ArrayRef<OperandBundleDef> OpBundles = None) | |||
2499 | : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles), | |||
2500 | Folder(Folder), Inserter(Inserter) {} | |||
2501 | ||||
2502 | explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr, | |||
2503 | ArrayRef<OperandBundleDef> OpBundles = None) | |||
2504 | : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {} | |||
2505 | ||||
2506 | explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder, | |||
2507 | MDNode *FPMathTag = nullptr, | |||
2508 | ArrayRef<OperandBundleDef> OpBundles = None) | |||
2509 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, | |||
2510 | FPMathTag, OpBundles), Folder(Folder) { | |||
2511 | SetInsertPoint(TheBB); | |||
2512 | } | |||
2513 | ||||
2514 | explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr, | |||
2515 | ArrayRef<OperandBundleDef> OpBundles = None) | |||
2516 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, | |||
2517 | FPMathTag, OpBundles) { | |||
2518 | SetInsertPoint(TheBB); | |||
2519 | } | |||
2520 | ||||
2521 | explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr, | |||
2522 | ArrayRef<OperandBundleDef> OpBundles = None) | |||
2523 | : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, | |||
| ||||
2524 | FPMathTag, OpBundles) { | |||
2525 | SetInsertPoint(IP); | |||
2526 | } | |||
2527 | ||||
2528 | IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder, | |||
2529 | MDNode *FPMathTag = nullptr, | |||
2530 | ArrayRef<OperandBundleDef> OpBundles = None) | |||
2531 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, | |||
2532 | FPMathTag, OpBundles), Folder(Folder) { | |||
2533 | SetInsertPoint(TheBB, IP); | |||
2534 | } | |||
2535 | ||||
2536 | IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, | |||
2537 | MDNode *FPMathTag = nullptr, | |||
2538 | ArrayRef<OperandBundleDef> OpBundles = None) | |||
2539 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, | |||
2540 | FPMathTag, OpBundles) { | |||
2541 | SetInsertPoint(TheBB, IP); | |||
2542 | } | |||
2543 | ||||
2544 | /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard | |||
2545 | /// or FastMathFlagGuard instead. | |||
2546 | IRBuilder(const IRBuilder &) = delete; | |||
2547 | ||||
2548 | InserterTy &getInserter() { return Inserter; } | |||
2549 | }; | |||
2550 | ||||
2551 | // Create wrappers for C Binding types (see CBindingWrapping.h). | |||
2552 | DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)inline IRBuilder<> *unwrap(LLVMBuilderRef P) { return reinterpret_cast <IRBuilder<>*>(P); } inline LLVMBuilderRef wrap(const IRBuilder<> *P) { return reinterpret_cast<LLVMBuilderRef >(const_cast<IRBuilder<>*>(P)); } | |||
2553 | ||||
2554 | } // end namespace llvm | |||
2555 | ||||
2556 | #endif // LLVM_IR_IRBUILDER_H |