Line data Source code
1 : //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file defines the IRBuilder class, which is used as a convenient way
11 : // to create LLVM instructions with a consistent and simplified interface.
12 : //
13 : //===----------------------------------------------------------------------===//
14 :
15 : #ifndef LLVM_IR_IRBUILDER_H
16 : #define LLVM_IR_IRBUILDER_H
17 :
18 : #include "llvm-c/Types.h"
19 : #include "llvm/ADT/ArrayRef.h"
20 : #include "llvm/ADT/None.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/Function.h"
31 : #include "llvm/IR/GlobalVariable.h"
32 : #include "llvm/IR/InstrTypes.h"
33 : #include "llvm/IR/Instruction.h"
34 : #include "llvm/IR/Instructions.h"
35 : #include "llvm/IR/Intrinsics.h"
36 : #include "llvm/IR/LLVMContext.h"
37 : #include "llvm/IR/Module.h"
38 : #include "llvm/IR/Operator.h"
39 : #include "llvm/IR/Type.h"
40 : #include "llvm/IR/Value.h"
41 : #include "llvm/IR/ValueHandle.h"
42 : #include "llvm/Support/AtomicOrdering.h"
43 : #include "llvm/Support/CBindingWrapping.h"
44 : #include "llvm/Support/Casting.h"
45 : #include <cassert>
46 : #include <cstddef>
47 : #include <cstdint>
48 : #include <functional>
49 : #include <utility>
50 :
51 : namespace llvm {
52 :
53 : class APInt;
54 : class MDNode;
55 : class Use;
56 :
57 : /// This provides the default implementation of the IRBuilder
58 : /// 'InsertHelper' method that is called whenever an instruction is created by
59 : /// IRBuilder and needs to be inserted.
60 : ///
61 : /// By default, this inserts the instruction at the insertion point.
62 : class IRBuilderDefaultInserter {
63 : protected:
64 0 : void InsertHelper(Instruction *I, const Twine &Name,
65 : BasicBlock *BB, BasicBlock::iterator InsertPt) const {
66 0 : if (BB) BB->getInstList().insert(InsertPt, I);
67 0 : I->setName(Name);
68 0 : }
69 : };
70 :
71 : /// Provides an 'InsertHelper' that calls a user-provided callback after
72 : /// performing the default insertion.
73 : class IRBuilderCallbackInserter : IRBuilderDefaultInserter {
74 : std::function<void(Instruction *)> Callback;
75 :
76 : public:
77 : IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
78 : : Callback(std::move(Callback)) {}
79 :
80 : protected:
81 208016 : void InsertHelper(Instruction *I, const Twine &Name,
82 : BasicBlock *BB, BasicBlock::iterator InsertPt) const {
83 208016 : IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
84 208016 : Callback(I);
85 208016 : }
86 : };
87 :
88 : /// Common base class shared among various IRBuilders.
89 : class IRBuilderBase {
90 : DebugLoc CurDbgLocation;
91 :
92 : protected:
93 : BasicBlock *BB;
94 : BasicBlock::iterator InsertPt;
95 : LLVMContext &Context;
96 :
97 : MDNode *DefaultFPMathTag;
98 : FastMathFlags FMF;
99 :
100 : ArrayRef<OperandBundleDef> DefaultOperandBundles;
101 :
102 : public:
103 : IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
104 : ArrayRef<OperandBundleDef> OpBundles = None)
105 16964230 : : Context(context), DefaultFPMathTag(FPMathTag),
106 18030624 : DefaultOperandBundles(OpBundles) {
107 : ClearInsertionPoint();
108 : }
109 :
110 : //===--------------------------------------------------------------------===//
111 : // Builder configuration methods
112 : //===--------------------------------------------------------------------===//
113 :
114 : /// Clear the insertion point: created instructions will not be
115 : /// inserted into a block.
116 : void ClearInsertionPoint() {
117 17582678 : BB = nullptr;
118 15396740 : InsertPt = BasicBlock::iterator();
119 : }
120 :
121 0 : BasicBlock *GetInsertBlock() const { return BB; }
122 0 : BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
123 0 : LLVMContext &getContext() const { return Context; }
124 :
125 : /// This specifies that created instructions should be appended to the
126 : /// end of the specified block.
127 : void SetInsertPoint(BasicBlock *TheBB) {
128 6272421 : BB = TheBB;
129 6275267 : InsertPt = BB->end();
130 : }
131 :
132 : /// This specifies that created instructions should be inserted before
133 : /// the specified instruction.
134 45742876 : void SetInsertPoint(Instruction *I) {
135 45742876 : BB = I->getParent();
136 45742876 : InsertPt = I->getIterator();
137 : assert(InsertPt != BB->end() && "Can't read debug loc from end()");
138 45742876 : SetCurrentDebugLocation(I->getDebugLoc());
139 45742876 : }
140 :
141 : /// This specifies that created instructions should be inserted at the
142 : /// specified point.
143 1362176 : void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
144 1362176 : BB = TheBB;
145 1362176 : InsertPt = IP;
146 1362176 : if (IP != TheBB->end())
147 722096 : SetCurrentDebugLocation(IP->getDebugLoc());
148 1362176 : }
149 :
150 : /// Set location information used by debugging information.
151 : void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
152 :
153 : /// Get location information used by debugging information.
154 3 : const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
155 :
156 : /// If this builder has a current debug location, set it on the
157 : /// specified instruction.
158 19527487 : void SetInstDebugLocation(Instruction *I) const {
159 19527487 : if (CurDbgLocation)
160 2349081 : I->setDebugLoc(CurDbgLocation);
161 19527487 : }
162 :
163 : /// Get the return type of the current function that we're emitting
164 : /// into.
165 : Type *getCurrentFunctionReturnType() const;
166 :
167 : /// InsertPoint - A saved insertion point.
168 : class InsertPoint {
169 : BasicBlock *Block = nullptr;
170 : BasicBlock::iterator Point;
171 :
172 : public:
173 : /// Creates a new insertion point which doesn't point to anything.
174 : InsertPoint() = default;
175 :
176 : /// Creates a new insertion point at the given location.
177 : InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
178 : : Block(InsertBlock), Point(InsertPoint) {}
179 :
180 : /// Returns true if this insert point is set.
181 0 : bool isSet() const { return (Block != nullptr); }
182 :
183 0 : BasicBlock *getBlock() const { return Block; }
184 0 : BasicBlock::iterator getPoint() const { return Point; }
185 : };
186 :
187 : /// Returns the current insert point.
188 : InsertPoint saveIP() const {
189 58202 : return InsertPoint(GetInsertBlock(), GetInsertPoint());
190 : }
191 :
192 : /// Returns the current insert point, clearing it in the process.
193 : InsertPoint saveAndClearIP() {
194 607055 : InsertPoint IP(GetInsertBlock(), GetInsertPoint());
195 : ClearInsertionPoint();
196 : return IP;
197 : }
198 :
199 : /// Sets the current insert point to a previously-saved location.
200 : void restoreIP(InsertPoint IP) {
201 1084814 : if (IP.isSet())
202 1056016 : SetInsertPoint(IP.getBlock(), IP.getPoint());
203 : else
204 : ClearInsertionPoint();
205 : }
206 :
207 : /// Get the floating point math metadata being used.
208 0 : MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
209 :
210 : /// Get the flags to be applied to created floating point ops
211 0 : FastMathFlags getFastMathFlags() const { return FMF; }
212 :
213 : /// Clear the fast-math flags.
214 : void clearFastMathFlags() { FMF.clear(); }
215 :
216 : /// Set the floating point math metadata to be used.
217 1 : void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
218 :
219 : /// Set the fast-math flags to be used with generated fp-math operators
220 835236 : void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
221 :
222 : //===--------------------------------------------------------------------===//
223 : // RAII helpers.
224 : //===--------------------------------------------------------------------===//
225 :
226 : // RAII object that stores the current insertion point and restores it
227 : // when the object is destroyed. This includes the debug location.
228 : class InsertPointGuard {
229 : IRBuilderBase &Builder;
230 : AssertingVH<BasicBlock> Block;
231 : BasicBlock::iterator Point;
232 : DebugLoc DbgLoc;
233 :
234 : public:
235 296088 : InsertPointGuard(IRBuilderBase &B)
236 296088 : : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
237 296088 : DbgLoc(B.getCurrentDebugLocation()) {}
238 :
239 : InsertPointGuard(const InsertPointGuard &) = delete;
240 : InsertPointGuard &operator=(const InsertPointGuard &) = delete;
241 :
242 592176 : ~InsertPointGuard() {
243 296088 : Builder.restoreIP(InsertPoint(Block, Point));
244 592176 : Builder.SetCurrentDebugLocation(DbgLoc);
245 296088 : }
246 : };
247 :
248 : // RAII object that stores the current fast math settings and restores
249 : // them when the object is destroyed.
250 : class FastMathFlagGuard {
251 : IRBuilderBase &Builder;
252 : FastMathFlags FMF;
253 : MDNode *FPMathTag;
254 :
255 : public:
256 : FastMathFlagGuard(IRBuilderBase &B)
257 4343 : : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
258 :
259 : FastMathFlagGuard(const FastMathFlagGuard &) = delete;
260 : FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
261 :
262 : ~FastMathFlagGuard() {
263 4343 : Builder.FMF = FMF;
264 4258 : Builder.DefaultFPMathTag = FPMathTag;
265 : }
266 : };
267 :
268 : //===--------------------------------------------------------------------===//
269 : // Miscellaneous creation methods.
270 : //===--------------------------------------------------------------------===//
271 :
272 : /// Make a new global variable with initializer type i8*
273 : ///
274 : /// Make a new global variable with an initializer that has array of i8 type
275 : /// filled in with the null terminated string value specified. The new global
276 : /// variable will be marked mergable with any others of the same contents. If
277 : /// Name is specified, it is the name of the global variable created.
278 : GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
279 : unsigned AddressSpace = 0);
280 :
281 : /// Get a constant value representing either true or false.
282 200630 : ConstantInt *getInt1(bool V) {
283 200630 : return ConstantInt::get(getInt1Ty(), V);
284 : }
285 :
286 : /// Get the constant value for i1 true.
287 0 : ConstantInt *getTrue() {
288 4936 : return ConstantInt::getTrue(Context);
289 : }
290 :
291 : /// Get the constant value for i1 false.
292 0 : ConstantInt *getFalse() {
293 5393 : return ConstantInt::getFalse(Context);
294 : }
295 :
296 : /// Get a constant 8-bit value.
297 30627 : ConstantInt *getInt8(uint8_t C) {
298 30627 : return ConstantInt::get(getInt8Ty(), C);
299 : }
300 :
301 : /// Get a constant 16-bit value.
302 2145 : ConstantInt *getInt16(uint16_t C) {
303 2145 : return ConstantInt::get(getInt16Ty(), C);
304 : }
305 :
306 : /// Get a constant 32-bit value.
307 530928 : ConstantInt *getInt32(uint32_t C) {
308 530928 : return ConstantInt::get(getInt32Ty(), C);
309 : }
310 :
311 : /// Get a constant 64-bit value.
312 173142 : ConstantInt *getInt64(uint64_t C) {
313 173142 : return ConstantInt::get(getInt64Ty(), C);
314 : }
315 :
316 : /// Get a constant N-bit value, zero extended or truncated from
317 : /// a 64-bit value.
318 13964 : ConstantInt *getIntN(unsigned N, uint64_t C) {
319 13964 : return ConstantInt::get(getIntNTy(N), C);
320 : }
321 :
322 : /// Get a constant integer value.
323 0 : ConstantInt *getInt(const APInt &AI) {
324 77364 : return ConstantInt::get(Context, AI);
325 : }
326 :
327 : //===--------------------------------------------------------------------===//
328 : // Type creation methods
329 : //===--------------------------------------------------------------------===//
330 :
331 : /// Fetch the type representing a single bit
332 0 : IntegerType *getInt1Ty() {
333 258746 : return Type::getInt1Ty(Context);
334 : }
335 :
336 : /// Fetch the type representing an 8-bit integer.
337 0 : IntegerType *getInt8Ty() {
338 49423 : return Type::getInt8Ty(Context);
339 : }
340 :
341 : /// Fetch the type representing a 16-bit integer.
342 0 : IntegerType *getInt16Ty() {
343 2219 : return Type::getInt16Ty(Context);
344 : }
345 :
346 : /// Fetch the type representing a 32-bit integer.
347 0 : IntegerType *getInt32Ty() {
348 558809 : return Type::getInt32Ty(Context);
349 : }
350 :
351 : /// Fetch the type representing a 64-bit integer.
352 0 : IntegerType *getInt64Ty() {
353 174501 : return Type::getInt64Ty(Context);
354 : }
355 :
356 : /// Fetch the type representing a 128-bit integer.
357 3 : IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
358 :
359 : /// Fetch the type representing an N-bit integer.
360 0 : IntegerType *getIntNTy(unsigned N) {
361 17702 : return Type::getIntNTy(Context, N);
362 : }
363 :
364 : /// Fetch the type representing a 16-bit floating point value.
365 0 : Type *getHalfTy() {
366 6 : return Type::getHalfTy(Context);
367 : }
368 :
369 : /// Fetch the type representing a 32-bit floating point value.
370 0 : Type *getFloatTy() {
371 403 : return Type::getFloatTy(Context);
372 : }
373 :
374 : /// Fetch the type representing a 64-bit floating point value.
375 0 : Type *getDoubleTy() {
376 43 : return Type::getDoubleTy(Context);
377 : }
378 :
379 : /// Fetch the type representing void.
380 0 : Type *getVoidTy() {
381 57039 : return Type::getVoidTy(Context);
382 : }
383 :
384 : /// Fetch the type representing a pointer to an 8-bit integer value.
385 0 : PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
386 132834 : return Type::getInt8PtrTy(Context, AddrSpace);
387 : }
388 :
389 : /// Fetch the type representing a pointer to an integer value.
390 0 : IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
391 599 : return DL.getIntPtrType(Context, AddrSpace);
392 : }
393 :
394 : //===--------------------------------------------------------------------===//
395 : // Intrinsic creation methods
396 : //===--------------------------------------------------------------------===//
397 :
398 : /// Create and insert a memset to the specified pointer and the
399 : /// specified value.
400 : ///
401 : /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
402 : /// specified, it will be added to the instruction. Likewise with alias.scope
403 : /// and noalias tags.
404 67 : CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
405 : bool isVolatile = false, MDNode *TBAATag = nullptr,
406 : MDNode *ScopeTag = nullptr,
407 : MDNode *NoAliasTag = nullptr) {
408 24864 : return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
409 67 : TBAATag, ScopeTag, NoAliasTag);
410 : }
411 :
412 : CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
413 : bool isVolatile = false, MDNode *TBAATag = nullptr,
414 : MDNode *ScopeTag = nullptr,
415 : MDNode *NoAliasTag = nullptr);
416 :
417 : /// Create and insert an element unordered-atomic memset of the region of
418 : /// memory starting at the given pointer to the given value.
419 : ///
420 : /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
421 : /// specified, it will be added to the instruction. Likewise with alias.scope
422 : /// and noalias tags.
423 : CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
424 : uint64_t Size, unsigned Align,
425 : uint32_t ElementSize,
426 : MDNode *TBAATag = nullptr,
427 : MDNode *ScopeTag = nullptr,
428 : MDNode *NoAliasTag = nullptr) {
429 : return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), Align,
430 : ElementSize, TBAATag, ScopeTag,
431 : NoAliasTag);
432 : }
433 :
434 : CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
435 : Value *Size, unsigned Align,
436 : uint32_t ElementSize,
437 : MDNode *TBAATag = nullptr,
438 : MDNode *ScopeTag = nullptr,
439 : MDNode *NoAliasTag = nullptr);
440 :
441 : /// Create and insert a memcpy between the specified pointers.
442 : ///
443 : /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
444 : /// specified, it will be added to the instruction. Likewise with alias.scope
445 : /// and noalias tags.
446 35 : CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
447 : unsigned SrcAlign, uint64_t Size,
448 : bool isVolatile = false, MDNode *TBAATag = nullptr,
449 : MDNode *TBAAStructTag = nullptr,
450 : MDNode *ScopeTag = nullptr,
451 : MDNode *NoAliasTag = nullptr) {
452 885 : return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
453 : isVolatile, TBAATag, TBAAStructTag, ScopeTag,
454 35 : NoAliasTag);
455 : }
456 :
457 : CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
458 : unsigned SrcAlign, Value *Size,
459 : bool isVolatile = false, MDNode *TBAATag = nullptr,
460 : MDNode *TBAAStructTag = nullptr,
461 : MDNode *ScopeTag = nullptr,
462 : MDNode *NoAliasTag = nullptr);
463 :
464 : /// Create and insert an element unordered-atomic memcpy between the
465 : /// specified pointers.
466 : ///
467 : /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
468 : ///
469 : /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
470 : /// specified, it will be added to the instruction. Likewise with alias.scope
471 : /// and noalias tags.
472 : CallInst *CreateElementUnorderedAtomicMemCpy(
473 : Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
474 : uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
475 : MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
476 : MDNode *NoAliasTag = nullptr) {
477 : return CreateElementUnorderedAtomicMemCpy(
478 : Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
479 : TBAAStructTag, ScopeTag, NoAliasTag);
480 : }
481 :
482 : CallInst *CreateElementUnorderedAtomicMemCpy(
483 : Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
484 : uint32_t ElementSize, MDNode *TBAATag = nullptr,
485 : MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
486 : MDNode *NoAliasTag = nullptr);
487 :
488 : /// Create and insert a memmove between the specified
489 : /// pointers.
490 : ///
491 : /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
492 : /// specified, it will be added to the instruction. Likewise with alias.scope
493 : /// and noalias tags.
494 : CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
495 : uint64_t Size, bool isVolatile = false,
496 : MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
497 : MDNode *NoAliasTag = nullptr) {
498 4 : return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), isVolatile,
499 : TBAATag, ScopeTag, NoAliasTag);
500 : }
501 :
502 : CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
503 : Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr,
504 : MDNode *ScopeTag = nullptr,
505 : MDNode *NoAliasTag = nullptr);
506 :
507 : /// \brief Create and insert an element unordered-atomic memmove between the
508 : /// specified pointers.
509 : ///
510 : /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
511 : /// respectively.
512 : ///
513 : /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
514 : /// specified, it will be added to the instruction. Likewise with alias.scope
515 : /// and noalias tags.
516 : CallInst *CreateElementUnorderedAtomicMemMove(
517 : Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
518 : uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
519 : MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
520 : MDNode *NoAliasTag = nullptr) {
521 : return CreateElementUnorderedAtomicMemMove(
522 : Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
523 : TBAAStructTag, ScopeTag, NoAliasTag);
524 : }
525 :
526 : CallInst *CreateElementUnorderedAtomicMemMove(
527 : Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
528 : uint32_t ElementSize, MDNode *TBAATag = nullptr,
529 : MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
530 : MDNode *NoAliasTag = nullptr);
531 :
532 : /// Create a vector fadd reduction intrinsic of the source vector.
533 : /// The first parameter is a scalar accumulator value for ordered reductions.
534 : CallInst *CreateFAddReduce(Value *Acc, Value *Src);
535 :
536 : /// Create a vector fmul reduction intrinsic of the source vector.
537 : /// The first parameter is a scalar accumulator value for ordered reductions.
538 : CallInst *CreateFMulReduce(Value *Acc, Value *Src);
539 :
540 : /// Create a vector int add reduction intrinsic of the source vector.
541 : CallInst *CreateAddReduce(Value *Src);
542 :
543 : /// Create a vector int mul reduction intrinsic of the source vector.
544 : CallInst *CreateMulReduce(Value *Src);
545 :
546 : /// Create a vector int AND reduction intrinsic of the source vector.
547 : CallInst *CreateAndReduce(Value *Src);
548 :
549 : /// Create a vector int OR reduction intrinsic of the source vector.
550 : CallInst *CreateOrReduce(Value *Src);
551 :
552 : /// Create a vector int XOR reduction intrinsic of the source vector.
553 : CallInst *CreateXorReduce(Value *Src);
554 :
555 : /// Create a vector integer max reduction intrinsic of the source
556 : /// vector.
557 : CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
558 :
559 : /// Create a vector integer min reduction intrinsic of the source
560 : /// vector.
561 : CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
562 :
563 : /// Create a vector float max reduction intrinsic of the source
564 : /// vector.
565 : CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
566 :
567 : /// Create a vector float min reduction intrinsic of the source
568 : /// vector.
569 : CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
570 :
571 : /// Create a lifetime.start intrinsic.
572 : ///
573 : /// If the pointer isn't i8* it will be converted.
574 : CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
575 :
576 : /// Create a lifetime.end intrinsic.
577 : ///
578 : /// If the pointer isn't i8* it will be converted.
579 : CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
580 :
581 : /// Create a call to invariant.start intrinsic.
582 : ///
583 : /// If the pointer isn't i8* it will be converted.
584 : CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
585 :
586 : /// Create a call to Masked Load intrinsic
587 : CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
588 : Value *PassThru = nullptr, const Twine &Name = "");
589 :
590 : /// Create a call to Masked Store intrinsic
591 : CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
592 : Value *Mask);
593 :
594 : /// Create a call to Masked Gather intrinsic
595 : CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
596 : Value *Mask = nullptr,
597 : Value *PassThru = nullptr,
598 : const Twine& Name = "");
599 :
600 : /// Create a call to Masked Scatter intrinsic
601 : CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
602 : Value *Mask = nullptr);
603 :
604 : /// Create an assume intrinsic call that allows the optimizer to
605 : /// assume that the provided condition will be true.
606 : CallInst *CreateAssumption(Value *Cond);
607 :
608 : /// Create a call to the experimental.gc.statepoint intrinsic to
609 : /// start a new statepoint sequence.
610 : CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
611 : Value *ActualCallee,
612 : ArrayRef<Value *> CallArgs,
613 : ArrayRef<Value *> DeoptArgs,
614 : ArrayRef<Value *> GCArgs,
615 : const Twine &Name = "");
616 :
617 : /// Create a call to the experimental.gc.statepoint intrinsic to
618 : /// start a new statepoint sequence.
619 : CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
620 : Value *ActualCallee, uint32_t Flags,
621 : ArrayRef<Use> CallArgs,
622 : ArrayRef<Use> TransitionArgs,
623 : ArrayRef<Use> DeoptArgs,
624 : ArrayRef<Value *> GCArgs,
625 : const Twine &Name = "");
626 :
627 : /// Conveninence function for the common case when CallArgs are filled
628 : /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
629 : /// .get()'ed to get the Value pointer.
630 : CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
631 : Value *ActualCallee, ArrayRef<Use> CallArgs,
632 : ArrayRef<Value *> DeoptArgs,
633 : ArrayRef<Value *> GCArgs,
634 : const Twine &Name = "");
635 :
636 : /// Create an invoke to the experimental.gc.statepoint intrinsic to
637 : /// start a new statepoint sequence.
638 : InvokeInst *
639 : CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
640 : Value *ActualInvokee, BasicBlock *NormalDest,
641 : BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
642 : ArrayRef<Value *> DeoptArgs,
643 : ArrayRef<Value *> GCArgs, const Twine &Name = "");
644 :
645 : /// Create an invoke to the experimental.gc.statepoint intrinsic to
646 : /// start a new statepoint sequence.
647 : InvokeInst *CreateGCStatepointInvoke(
648 : uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
649 : BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
650 : ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
651 : ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
652 : const Twine &Name = "");
653 :
654 : // Convenience function for the common case when CallArgs are filled in using
655 : // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
656 : // get the Value *.
657 : InvokeInst *
658 : CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
659 : Value *ActualInvokee, BasicBlock *NormalDest,
660 : BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
661 : ArrayRef<Value *> DeoptArgs,
662 : ArrayRef<Value *> GCArgs, const Twine &Name = "");
663 :
664 : /// Create a call to the experimental.gc.result intrinsic to extract
665 : /// the result from a call wrapped in a statepoint.
666 : CallInst *CreateGCResult(Instruction *Statepoint,
667 : Type *ResultType,
668 : const Twine &Name = "");
669 :
670 : /// Create a call to the experimental.gc.relocate intrinsics to
671 : /// project the relocated value of one pointer from the statepoint.
672 : CallInst *CreateGCRelocate(Instruction *Statepoint,
673 : int BaseOffset,
674 : int DerivedOffset,
675 : Type *ResultType,
676 : const Twine &Name = "");
677 :
678 : /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
679 : /// type.
680 : CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
681 : Instruction *FMFSource = nullptr,
682 : const Twine &Name = "");
683 :
684 : /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
685 : /// first type.
686 : CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
687 : Instruction *FMFSource = nullptr,
688 : const Twine &Name = "");
689 :
690 : /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
691 : /// \p FMFSource is provided, copy fast-math-flags from that instruction to
692 : /// the intrinsic.
693 : CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
694 : ArrayRef<Value *> Args,
695 : Instruction *FMFSource = nullptr,
696 : const Twine &Name = "");
697 :
698 : /// Create call to the minnum intrinsic.
699 : CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
700 15 : return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
701 : }
702 :
703 : /// Create call to the maxnum intrinsic.
704 : CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
705 5 : return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
706 : }
707 :
708 : /// Create call to the minimum intrinsic.
709 : CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
710 1 : return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
711 : }
712 :
713 : /// Create call to the maximum intrinsic.
714 : CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
715 1 : return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
716 : }
717 :
718 : private:
719 : /// Create a call to a masked intrinsic with given Id.
720 : CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
721 : ArrayRef<Type *> OverloadedTypes,
722 : const Twine &Name = "");
723 :
724 : Value *getCastedInt8PtrValue(Value *Ptr);
725 : };
726 :
727 : /// This provides a uniform API for creating instructions and inserting
728 : /// them into a basic block: either at the end of a BasicBlock, or at a specific
729 : /// iterator location in a block.
730 : ///
731 : /// Note that the builder does not expose the full generality of LLVM
732 : /// instructions. For access to extra instruction properties, use the mutators
733 : /// (e.g. setVolatile) on the instructions after they have been
734 : /// created. Convenience state exists to specify fast-math flags and fp-math
735 : /// tags.
736 : ///
737 : /// The first template argument specifies a class to use for creating constants.
738 : /// This defaults to creating minimally folded constants. The second template
739 : /// argument allows clients to specify custom insertion hooks that are called on
740 : /// every newly created insertion.
741 : template <typename T = ConstantFolder,
742 : typename Inserter = IRBuilderDefaultInserter>
743 3643545 : class IRBuilder : public IRBuilderBase, public Inserter {
744 : T Folder;
745 :
746 : public:
747 1242682 : IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
748 : MDNode *FPMathTag = nullptr,
749 : ArrayRef<OperandBundleDef> OpBundles = None)
750 : : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
751 1127800 : Folder(F) {}
752 :
753 51311 : explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
754 : ArrayRef<OperandBundleDef> OpBundles = None)
755 : : IRBuilderBase(C, FPMathTag, OpBundles) {}
756 :
757 : explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
758 : ArrayRef<OperandBundleDef> OpBundles = None)
759 : : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
760 : SetInsertPoint(TheBB);
761 : }
762 :
763 2500406 : explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
764 : ArrayRef<OperandBundleDef> OpBundles = None)
765 2500406 : : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
766 : SetInsertPoint(TheBB);
767 : }
768 :
769 14150715 : explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
770 : ArrayRef<OperandBundleDef> OpBundles = None)
771 14150715 : : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles) {
772 14150715 : SetInsertPoint(IP);
773 14150715 : }
774 11197 :
775 567 : IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F,
776 11197 : MDNode *FPMathTag = nullptr,
777 11197 : ArrayRef<OperandBundleDef> OpBundles = None)
778 11764 : : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
779 2634 : SetInsertPoint(TheBB, IP);
780 567 : }
781 2067 :
782 4631 : IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
783 2067 : MDNode *FPMathTag = nullptr,
784 : ArrayRef<OperandBundleDef> OpBundles = None)
785 2564 : : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
786 2564 : SetInsertPoint(TheBB, IP);
787 2564 : }
788 :
789 : /// Get the constant folder being used.
790 : const T &getFolder() { return Folder; }
791 :
792 : /// Insert and return the specified instruction.
793 : template<typename InstTy>
794 17874461 : InstTy *Insert(InstTy *I, const Twine &Name = "") const {
795 19091197 : this->InsertHelper(I, Name, BB, InsertPt);
796 19126668 : this->SetInstDebugLocation(I);
797 17874461 : return I;
798 : }
799 888343 :
800 888343 : /// No-op overload to handle constants.
801 888343 : Constant *Insert(Constant *C, const Twine& = "") const {
802 888343 : return C;
803 : }
804 528262 :
805 631997 : //===--------------------------------------------------------------------===//
806 632060 : // Instruction creation methods: Terminators
807 528262 : //===--------------------------------------------------------------------===//
808 0 :
809 3158531 : private:
810 3158531 : /// Helper to add branch weight and unpredictable metadata onto an
811 3158531 : /// instruction.
812 3158531 : /// \returns The annotated instruction.
813 : template <typename InstTy>
814 3169138 : InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
815 3169138 : if (Weights)
816 3169138 : I->setMetadata(LLVMContext::MD_prof, Weights);
817 3169138 : if (Unpredictable)
818 0 : I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
819 139027 : return I;
820 139027 : }
821 139027 :
822 139027 : public:
823 0 : /// Create a 'ret void' instruction.
824 152306 : ReturnInst *CreateRetVoid() {
825 152306 : return Insert(ReturnInst::Create(Context));
826 152114 : }
827 152114 :
828 0 : /// Create a 'ret <val>' instruction.
829 894158 : ReturnInst *CreateRet(Value *V) {
830 894158 : return Insert(ReturnInst::Create(Context, V));
831 893807 : }
832 893807 :
833 0 : /// Create a sequence of N insertvalue instructions,
834 434077 : /// with one Value from the retVals array each, that build a aggregate
835 434077 : /// return value one value at a time, and a ret instruction to return
836 434077 : /// the resulting aggregate value.
837 434077 : ///
838 8 : /// This is a convenience function for code that uses aggregate return values
839 1208772 : /// as a vehicle for having multiple return values.
840 1208764 : ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
841 1208764 : Value *V = UndefValue::get(getCurrentFunctionReturnType());
842 1208764 : for (unsigned i = 0; i != N; ++i)
843 0 : V = CreateInsertValue(V, retVals[i], i, "mrv");
844 2647112 : return Insert(ReturnInst::Create(Context, V));
845 2647117 : }
846 2647117 :
847 2647112 : /// Create an unconditional 'br label X' instruction.
848 1518 : BranchInst *CreateBr(BasicBlock *Dest) {
849 998453 : return Insert(BranchInst::Create(Dest));
850 996994 : }
851 996994 :
852 996935 : /// Create a conditional 'br Cond, TrueDest, FalseDest'
853 0 : /// instruction.
854 158529 : BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
855 155687 : MDNode *BranchWeights = nullptr,
856 155687 : MDNode *Unpredictable = nullptr) {
857 155687 : return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
858 2842 : BranchWeights, Unpredictable));
859 1285536 : }
860 1285614 :
861 1285458 : /// Create a conditional 'br Cond, TrueDest, FalseDest'
862 1297102 : /// instruction. Copy branch meta data if available.
863 11860 : BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
864 221484 : Instruction *MDSrc) {
865 221512 : BranchInst *Br = BranchInst::Create(True, False, Cond);
866 221668 : if (MDSrc) {
867 221537 : unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
868 5137 : LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
869 1977231 : Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
870 1977146 : }
871 1977359 : return Insert(Br);
872 1982280 : }
873 28 :
874 234 : /// Create a switch instruction with the specified value, default dest,
875 322 : /// and with a hint for the number of cases that will be added (for efficient
876 146 : /// allocation).
877 669 : SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
878 784103 : MDNode *BranchWeights = nullptr,
879 789260 : MDNode *Unpredictable = nullptr) {
880 4038 : return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
881 4441 : BranchWeights, Unpredictable));
882 3918 : }
883 1223 :
884 6511 : /// Create an indirect branch instruction with the specified address
885 5142 : /// operand, with an optional hint for the number of destinations that will be
886 6122 : /// added (for efficient allocation).
887 6122 : IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
888 1767 : return Insert(IndirectBrInst::Create(Addr, NumDests));
889 2390 : }
890 1867 :
891 707 : /// Create an invoke instruction.
892 761 : InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
893 160 : BasicBlock *UnwindDest,
894 1399 : ArrayRef<Value *> Args = None,
895 1079 : const Twine &Name = "") {
896 1066 : return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
897 1104 : Name);
898 446 : }
899 9223 : InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
900 18 : BasicBlock *UnwindDest, ArrayRef<Value *> Args,
901 18 : ArrayRef<OperandBundleDef> OpBundles,
902 66 : const Twine &Name = "") {
903 10310 : return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
904 933 : OpBundles), Name);
905 0 : }
906 0 :
907 180 : ResumeInst *CreateResume(Value *Exn) {
908 2 : return Insert(ResumeInst::Create(Exn));
909 2284 : }
910 0 :
911 0 : CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
912 793579 : BasicBlock *UnwindBB = nullptr) {
913 1187228 : return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
914 782901 : }
915 :
916 1 : CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
917 : unsigned NumHandlers,
918 498849 : const Twine &Name = "") {
919 437627 : return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
920 323493 : Name);
921 322 : }
922 61033 :
923 375 : CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
924 323866 : const Twine &Name = "") {
925 323 : return Insert(CatchPadInst::Create(ParentPad, Args), Name);
926 160 : }
927 0 :
928 6171 : CleanupPadInst *CreateCleanupPad(Value *ParentPad,
929 144 : ArrayRef<Value *> Args = None,
930 160 : const Twine &Name = "") {
931 0 : return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
932 6178 : }
933 149 :
934 11970 : CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
935 1 : return Insert(CatchReturnInst::Create(CatchPad, BB));
936 0 : }
937 2 :
938 15168 : UnreachableInst *CreateUnreachable() {
939 3201 : return Insert(new UnreachableInst(Context));
940 0 : }
941 4777 :
942 118153 : //===--------------------------------------------------------------------===//
943 118335 : // Instruction creation methods: Binary Operators
944 3 : //===--------------------------------------------------------------------===//
945 4605 : private:
946 150334 : BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
947 12 : Value *LHS, Value *RHS,
948 39 : const Twine &Name,
949 2 : bool HasNUW, bool HasNSW) {
950 150331 : BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
951 152964 : if (HasNUW) BO->setHasNoUnsignedWrap();
952 153267 : if (HasNSW) BO->setHasNoSignedWrap();
953 150871 : return BO;
954 83 : }
955 2634 :
956 2834 : Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
957 3228 : FastMathFlags FMF) const {
958 3097 : if (!FPMD)
959 2956 : FPMD = DefaultFPMathTag;
960 370 : if (FPMD)
961 170 : I->setMetadata(LLVMContext::MD_fpmath, FPMD);
962 94 : I->setFastMathFlags(FMF);
963 0 : return I;
964 253 : }
965 250 :
966 688 : Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
967 1022 : Value *R, const Twine &Name = nullptr) const {
968 0 : auto *LC = dyn_cast<Constant>(L);
969 0 : auto *RC = dyn_cast<Constant>(R);
970 438 : return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
971 1210 : }
972 1210 :
973 1210 : public:
974 146101 : Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
975 0 : bool HasNUW = false, bool HasNSW = false) {
976 101 : if (auto *LC = dyn_cast<Constant>(LHS))
977 0 : if (auto *RC = dyn_cast<Constant>(RHS))
978 1981 : return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
979 0 : return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
980 146125 : HasNUW, HasNSW);
981 103 : }
982 127 :
983 127 : Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
984 24 : return CreateAdd(LHS, RHS, Name, false, true);
985 48 : }
986 2761 :
987 45366 : Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
988 593081 : return CreateAdd(LHS, RHS, Name, true, false);
989 8 : }
990 78 :
991 2407 : Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
992 95 : bool HasNUW = false, bool HasNSW = false) {
993 547867 : if (auto *LC = dyn_cast<Constant>(LHS))
994 1040 : if (auto *RC = dyn_cast<Constant>(RHS))
995 141 : return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
996 608 : return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
997 2397 : HasNUW, HasNSW);
998 579 : }
999 0 :
1000 707 : Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1001 1348 : return CreateSub(LHS, RHS, Name, false, true);
1002 503 : }
1003 801 :
1004 53824 : Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1005 55780 : return CreateSub(LHS, RHS, Name, true, false);
1006 1239 : }
1007 844 :
1008 4756 : Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1009 635 : bool HasNUW = false, bool HasNSW = false) {
1010 3002 : if (auto *LC = dyn_cast<Constant>(LHS))
1011 1109 : if (auto *RC = dyn_cast<Constant>(RHS))
1012 38855 : return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
1013 124918 : return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
1014 124443 : HasNUW, HasNSW);
1015 2655 : }
1016 34423 :
1017 34934 : Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1018 32030 : return CreateMul(LHS, RHS, Name, false, true);
1019 31818 : }
1020 77 :
1021 1366 : Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1022 2546 : return CreateMul(LHS, RHS, Name, true, false);
1023 429 : }
1024 658 :
1025 928 : Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1026 2845 : bool isExact = false) {
1027 2327 : if (auto *LC = dyn_cast<Constant>(LHS))
1028 1699 : if (auto *RC = dyn_cast<Constant>(RHS))
1029 1469 : return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
1030 97836 : if (!isExact)
1031 792 : return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1032 3656 : return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1033 3712 : }
1034 98348 :
1035 98389 : Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1036 97600 : return CreateUDiv(LHS, RHS, Name, true);
1037 97607 : }
1038 1200 :
1039 688 : Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1040 65612 : bool isExact = false) {
1041 252 : if (auto *LC = dyn_cast<Constant>(LHS))
1042 11 : if (auto *RC = dyn_cast<Constant>(RHS))
1043 245 : return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
1044 66830 : if (!isExact)
1045 65633 : return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1046 65662 : return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1047 66065 : }
1048 1196 :
1049 40 : Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1050 44 : return CreateSDiv(LHS, RHS, Name, true);
1051 1 : }
1052 96 :
1053 59 : Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1054 83 : if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
1055 323 : return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1056 44 : }
1057 0 :
1058 125845 : Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1059 67 : if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
1060 33 : return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1061 221 : }
1062 28695 :
1063 1517 : Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1064 97603 : bool HasNUW = false, bool HasNSW = false) {
1065 218 : if (auto *LC = dyn_cast<Constant>(LHS))
1066 1 : if (auto *RC = dyn_cast<Constant>(RHS))
1067 1881 : return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
1068 2238 : return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1069 1620 : HasNUW, HasNSW);
1070 0 : }
1071 35 :
1072 1644 : Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1073 515 : bool HasNUW = false, bool HasNSW = false) {
1074 38725 : return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1075 74 : HasNUW, HasNSW);
1076 683 : }
1077 331 :
1078 92040 : Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1079 28256 : bool HasNUW = false, bool HasNSW = false) {
1080 31896 : return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1081 167 : HasNUW, HasNSW);
1082 1196 : }
1083 21454 :
1084 8905 : Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1085 155667 : bool isExact = false) {
1086 53 : if (auto *LC = dyn_cast<Constant>(LHS))
1087 33 : if (auto *RC = dyn_cast<Constant>(RHS))
1088 1357 : return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
1089 90858 : if (!isExact)
1090 1661 : return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1091 60490 : return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1092 13 : }
1093 921 :
1094 49 : Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1095 99201 : bool isExact = false) {
1096 41 : return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1097 19 : }
1098 60 :
1099 958 : Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1100 38 : bool isExact = false) {
1101 989 : return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1102 12 : }
1103 739 :
1104 323 : Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1105 3 : bool isExact = false) {
1106 57 : if (auto *LC = dyn_cast<Constant>(LHS))
1107 8 : if (auto *RC = dyn_cast<Constant>(RHS))
1108 65 : return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
1109 298 : if (!isExact)
1110 304 : return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1111 9 : return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1112 15759 : }
1113 8 :
1114 204 : Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1115 532 : bool isExact = false) {
1116 8 : return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1117 70 : }
1118 277 :
1119 18858 : Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1120 663 : bool isExact = false) {
1121 5 : return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1122 509 : }
1123 12227 :
1124 10945 : Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1125 6902 : if (auto *RC = dyn_cast<Constant>(RHS)) {
1126 3390 : if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
1127 25 : return LHS; // LHS & -1 -> LHS
1128 2 : if (auto *LC = dyn_cast<Constant>(LHS))
1129 363 : return Insert(Folder.CreateAnd(LC, RC), Name);
1130 52 : }
1131 3725 : return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1132 80 : }
1133 14056 :
1134 42 : Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1135 8 : return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1136 19 : }
1137 9 :
1138 15984 : Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1139 339 : return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1140 14360 : }
1141 273 :
1142 2781 : Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1143 0 : if (auto *RC = dyn_cast<Constant>(RHS)) {
1144 15932 : if (RC->isNullValue())
1145 22 : return LHS; // LHS | 0 -> LHS
1146 10 : if (auto *LC = dyn_cast<Constant>(LHS))
1147 4027 : return Insert(Folder.CreateOr(LC, RC), Name);
1148 4232 : }
1149 5645 : return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1150 184 : }
1151 5 :
1152 1391 : Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1153 2681 : return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1154 2788 : }
1155 1513 :
1156 3842 : Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1157 6142 : return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1158 4 : }
1159 277 :
1160 1062 : Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1161 3756 : if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V;
1162 1045 : return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1163 5120 : }
1164 853 :
1165 83 : Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1166 1091 : return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1167 4 : }
1168 43 :
1169 156 : Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1170 156 : return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1171 64 : }
1172 308 :
1173 84 : Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1174 817 : MDNode *FPMD = nullptr) {
1175 610 : if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1176 672 : Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1177 144 : return Insert(I, Name);
1178 4808 : }
1179 605 :
1180 18 : /// Copy fast-math-flags from an instruction rather than using the builder's
1181 21 : /// default FMF.
1182 47 : Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1183 4830 : const Twine &Name = "") {
1184 5632 : if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1185 411 : Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr,
1186 1001 : FMFSource->getFastMathFlags());
1187 : return Insert(I, Name);
1188 976 : }
1189 193 :
1190 288 : Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1191 696 : MDNode *FPMD = nullptr) {
1192 798 : if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1193 118 : Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1194 930 : return Insert(I, Name);
1195 1203 : }
1196 440 :
1197 56 : /// Copy fast-math-flags from an instruction rather than using the builder's
1198 1468 : /// default FMF.
1199 4 : Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1200 0 : const Twine &Name = "") {
1201 0 : if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1202 568 : Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr,
1203 1454 : FMFSource->getFastMathFlags());
1204 1488 : return Insert(I, Name);
1205 300 : }
1206 601 :
1207 1237 : Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1208 4 : MDNode *FPMD = nullptr) {
1209 5020 : if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1210 5001 : Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1211 626 : return Insert(I, Name);
1212 293 : }
1213 845 :
1214 11 : /// Copy fast-math-flags from an instruction rather than using the builder's
1215 642 : /// default FMF.
1216 600 : Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1217 2054 : const Twine &Name = "") {
1218 30528 : if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1219 1414 : Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr,
1220 41639 : FMFSource->getFastMathFlags());
1221 24 : return Insert(I, Name);
1222 11 : }
1223 1922 :
1224 1881 : Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1225 28511 : MDNode *FPMD = nullptr) {
1226 411 : if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1227 431 : Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1228 410 : return Insert(I, Name);
1229 18 : }
1230 255 :
1231 18 : /// Copy fast-math-flags from an instruction rather than using the builder's
1232 120 : /// default FMF.
1233 124 : Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1234 108 : const Twine &Name = "") {
1235 321 : if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1236 8772 : Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr,
1237 204 : FMFSource->getFastMathFlags());
1238 3548 : return Insert(I, Name);
1239 : }
1240 :
1241 2234 : Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1242 : MDNode *FPMD = nullptr) {
1243 6180 : if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1244 : Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1245 2 : return Insert(I, Name);
1246 : }
1247 2 :
1248 4 : /// Copy fast-math-flags from an instruction rather than using the builder's
1249 0 : /// default FMF.
1250 12 : Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1251 4 : const Twine &Name = "") {
1252 12 : if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1253 8 : Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr,
1254 1955 : FMFSource->getFastMathFlags());
1255 1947 : return Insert(I, Name);
1256 1916 : }
1257 0 :
1258 480 : Value *CreateBinOp(Instruction::BinaryOps Opc,
1259 : Value *LHS, Value *RHS, const Twine &Name = "",
1260 : MDNode *FPMathTag = nullptr) {
1261 480 : if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V;
1262 474 : Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1263 474 : if (isa<FPMathOperator>(BinOp))
1264 6791 : BinOp = setFPAttrs(BinOp, FPMathTag, FMF);
1265 474 : return Insert(BinOp, Name);
1266 12 : }
1267 6790 :
1268 7211 : Value *CreateNeg(Value *V, const Twine &Name = "",
1269 6778 : bool HasNUW = false, bool HasNSW = false) {
1270 12 : if (auto *VC = dyn_cast<Constant>(V))
1271 6784 : return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
1272 5876 : BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
1273 427 : if (HasNUW) BO->setHasNoUnsignedWrap();
1274 474 : if (HasNSW) BO->setHasNoSignedWrap();
1275 5448 : return BO;
1276 5402 : }
1277 5448 :
1278 3080 : Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1279 5411 : return CreateNeg(V, Name, false, true);
1280 0 : }
1281 :
1282 4 : Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1283 3 : return CreateNeg(V, Name, true, false);
1284 11 : }
1285 3 :
1286 879 : Value *CreateFNeg(Value *V, const Twine &Name = "",
1287 15 : MDNode *FPMathTag = nullptr) {
1288 52 : if (auto *VC = dyn_cast<Constant>(V))
1289 2 : return Insert(Folder.CreateFNeg(VC), Name);
1290 1727 : return Insert(setFPAttrs(BinaryOperator::CreateFNeg(V), FPMathTag, FMF),
1291 862 : Name);
1292 37 : }
1293 42 :
1294 455 : Value *CreateNot(Value *V, const Twine &Name = "") {
1295 0 : if (auto *VC = dyn_cast<Constant>(V))
1296 23 : return Insert(Folder.CreateNot(VC), Name);
1297 805 : return Insert(BinaryOperator::CreateNot(V), Name);
1298 5 : }
1299 5535 :
1300 5 : //===--------------------------------------------------------------------===//
1301 10721 : // Instruction creation methods: Memory Instructions
1302 5527 : //===--------------------------------------------------------------------===//
1303 10732 :
1304 5185 : AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1305 5185 : Value *ArraySize = nullptr, const Twine &Name = "") {
1306 14 : return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
1307 19 : }
1308 1593 :
1309 1327 : AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1310 14 : const Twine &Name = "") {
1311 3023 : const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
1312 2731 : return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
1313 1421 : }
1314 699 :
1315 1421 : /// Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
1316 4031 : /// converting the string to 'bool' for the isVolatile parameter.
1317 1646 : LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1318 9663 : return Insert(new LoadInst(Ptr), Name);
1319 3222 : }
1320 109473 :
1321 276740 : LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1322 276737 : return Insert(new LoadInst(Ptr), Name);
1323 101851 : }
1324 99517 :
1325 100555 : LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1326 2148 : return Insert(new LoadInst(Ty, Ptr), Name);
1327 99519 : }
1328 3 :
1329 3257 : LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1330 3264 : return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
1331 1133 : }
1332 510795 :
1333 772272 : StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1334 263383 : return Insert(new StoreInst(Val, Ptr, isVolatile));
1335 899 : }
1336 1456 :
1337 964 : /// Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1338 57 : /// correctly, instead of converting the string to 'bool' for the isVolatile
1339 2237 : /// parameter.
1340 1660 : LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1341 192 : LoadInst *LI = CreateLoad(Ptr, Name);
1342 234 : LI->setAlignment(Align);
1343 53 : return LI;
1344 276 : }
1345 0 : LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
1346 304 : const Twine &Name = "") {
1347 43087 : LoadInst *LI = CreateLoad(Ptr, Name);
1348 42783 : LI->setAlignment(Align);
1349 11 : return LI;
1350 47 : }
1351 25 : LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
1352 436 : const Twine &Name = "") {
1353 59 : LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
1354 158 : LI->setAlignment(Align);
1355 1534 : return LI;
1356 510738 : }
1357 509491 :
1358 313 : StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
1359 505 : bool isVolatile = false) {
1360 2936 : StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1361 3165 : SI->setAlignment(Align);
1362 7279 : return SI;
1363 3954 : }
1364 8613 :
1365 8945 : FenceInst *CreateFence(AtomicOrdering Ordering,
1366 8941 : SyncScope::ID SSID = SyncScope::System,
1367 8978 : const Twine &Name = "") {
1368 211873 : return Insert(new FenceInst(Context, Ordering, SSID), Name);
1369 171715 : }
1370 11434 :
1371 32644 : AtomicCmpXchgInst *
1372 19195 : CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1373 20766 : AtomicOrdering SuccessOrdering,
1374 3998 : AtomicOrdering FailureOrdering,
1375 898 : SyncScope::ID SSID = SyncScope::System) {
1376 924 : return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1377 56576 : FailureOrdering, SSID));
1378 57867 : }
1379 4455 :
1380 2156 : AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1381 2691 : AtomicOrdering Ordering,
1382 2936 : SyncScope::ID SSID = SyncScope::System) {
1383 6798 : return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SSID));
1384 8309 : }
1385 693 :
1386 4636 : Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1387 2525 : const Twine &Name = "") {
1388 24975 : return CreateGEP(nullptr, Ptr, IdxList, Name);
1389 131269 : }
1390 111381 :
1391 7119 : Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1392 10133 : const Twine &Name = "") {
1393 176862 : if (auto *PC = dyn_cast<Constant>(Ptr)) {
1394 167743 : // Every index must be constant.
1395 165837 : size_t i, e;
1396 194460 : for (i = 0, e = IdxList.size(); i != e; ++i)
1397 1385890 : if (!isa<Constant>(IdxList[i]))
1398 1343921 : break;
1399 102 : if (i == e)
1400 2828 : return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1401 59356 : }
1402 393752 : return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1403 336738 : }
1404 2048576 :
1405 2055573 : Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1406 6767 : const Twine &Name = "") {
1407 412 : return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1408 1989263 : }
1409 1963016 :
1410 883 : Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1411 10469 : const Twine &Name = "") {
1412 10210 : if (auto *PC = dyn_cast<Constant>(Ptr)) {
1413 106483 : // Every index must be constant.
1414 109008 : size_t i, e;
1415 22996 : for (i = 0, e = IdxList.size(); i != e; ++i)
1416 21162 : if (!isa<Constant>(IdxList[i]))
1417 702 : break;
1418 1083 : if (i == e)
1419 238 : return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1420 163736 : Name);
1421 304525 : }
1422 152899 : return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1423 13201 : }
1424 423363 :
1425 422247 : Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1426 1083 : return CreateGEP(nullptr, Ptr, Idx, Name);
1427 222 : }
1428 2052512 :
1429 2072703 : Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1430 39 : if (auto *PC = dyn_cast<Constant>(Ptr))
1431 784 : if (auto *IC = dyn_cast<Constant>(Idx))
1432 1449 : return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1433 40640 : return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1434 8220 : }
1435 1986214 :
1436 2285897 : Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1437 310852 : const Twine &Name = "") {
1438 26 : if (auto *PC = dyn_cast<Constant>(Ptr))
1439 3858 : if (auto *IC = dyn_cast<Constant>(Idx))
1440 530117 : return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1441 544192 : return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1442 13940 : }
1443 70 :
1444 2199 : Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1445 2146 : return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1446 2375 : }
1447 931 :
1448 202 : Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1449 201913 : const Twine &Name = "") {
1450 204109 : Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1451 2352 :
1452 9620 : if (auto *PC = dyn_cast<Constant>(Ptr))
1453 22 : return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1454 15584 :
1455 12646 : return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1456 64 : }
1457 3341 :
1458 76 : Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1459 696 : const Twine &Name = "") {
1460 172389 : Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1461 447 :
1462 32017 : if (auto *PC = dyn_cast<Constant>(Ptr))
1463 5 : return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1464 469 :
1465 515918 : return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1466 663205 : }
1467 318 :
1468 166482 : Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1469 : const Twine &Name = "") {
1470 173606 : Value *Idxs[] = {
1471 13264 : ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1472 6511 : ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1473 11828 : };
1474 6197 :
1475 200 : if (auto *PC = dyn_cast<Constant>(Ptr))
1476 48361 : return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1477 18465 :
1478 1309 : return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1479 75 : }
1480 69 :
1481 17596 : Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1482 23357 : unsigned Idx1, const Twine &Name = "") {
1483 3210 : Value *Idxs[] = {
1484 9210 : ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1485 3258 : ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1486 9577 : };
1487 3129 :
1488 45353 : if (auto *PC = dyn_cast<Constant>(Ptr))
1489 9921 : return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1490 1648 :
1491 10801 : return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1492 3 : }
1493 11466 :
1494 175 : Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1495 138 : Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1496 322 :
1497 1568 : if (auto *PC = dyn_cast<Constant>(Ptr))
1498 74 : return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
1499 41 :
1500 0 : return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
1501 156810 : }
1502 667 :
1503 33836 : Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1504 152381 : const Twine &Name = "") {
1505 30622 : Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1506 530 :
1507 557 : if (auto *PC = dyn_cast<Constant>(Ptr))
1508 0 : return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
1509 13489 :
1510 84170 : return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
1511 54 : }
1512 5830 :
1513 44435 : Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1514 2136 : const Twine &Name = "") {
1515 44169 : Value *Idxs[] = {
1516 194863 : ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1517 44601 : ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1518 220 : };
1519 :
1520 3007 : if (auto *PC = dyn_cast<Constant>(Ptr))
1521 5988 : return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
1522 485 :
1523 45923 : return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
1524 6683 : }
1525 5563 :
1526 173325 : Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1527 2808 : const Twine &Name = "") {
1528 171494 : Value *Idxs[] = {
1529 172626 : ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1530 171439 : ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1531 6959 : };
1532 209 :
1533 76 : if (auto *PC = dyn_cast<Constant>(Ptr))
1534 285 : return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
1535 94 : Name);
1536 2048 :
1537 44072 : return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
1538 308 : }
1539 44583 :
1540 44380 : Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1541 141682 : const Twine &Name = "") {
1542 644 : return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1543 97559 : }
1544 97927 :
1545 97580 : Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1546 1374 : return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name);
1547 37120 : }
1548 9 :
1549 407 : /// Same as CreateGlobalString, but return a pointer with "i8*" type
1550 5 : /// instead of a pointer to array of i8.
1551 122225 : Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1552 8776 : unsigned AddressSpace = 0) {
1553 24759 : GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace);
1554 24734 : Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1555 24793 : Constant *Indices[] = {Zero, Zero};
1556 1524852 : return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
1557 24776 : Indices);
1558 1501273 : }
1559 1500203 :
1560 1501306 : //===--------------------------------------------------------------------===//
1561 63 : // Instruction creation methods: Cast/Conversion Operators
1562 59 : //===--------------------------------------------------------------------===//
1563 31 :
1564 28 : Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1565 4686 : return CreateCast(Instruction::Trunc, V, DestTy, Name);
1566 860291 : }
1567 672 :
1568 676 : Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1569 3247 : return CreateCast(Instruction::ZExt, V, DestTy, Name);
1570 410214 : }
1571 369 :
1572 410294 : Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1573 411312 : return CreateCast(Instruction::SExt, V, DestTy, Name);
1574 410286 : }
1575 515 :
1576 72 : /// Create a ZExt or Trunc from the integer value V to DestTy. Return
1577 72 : /// the value untouched if the type of V is already DestTy.
1578 48 : Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1579 393 : const Twine &Name = "") {
1580 410144 : assert(V->getType()->isIntOrIntVectorTy() &&
1581 2429 : DestTy->isIntOrIntVectorTy() &&
1582 5383 : "Can only zero extend/truncate integers!");
1583 2489 : Type *VTy = V->getType();
1584 2552 : if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1585 2452 : return CreateZExt(V, DestTy, Name);
1586 25 : if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1587 427 : return CreateTrunc(V, DestTy, Name);
1588 7372 : return V;
1589 7623 : }
1590 77 :
1591 145 : /// Create a SExt or Trunc from the integer value V to DestTy. Return
1592 213 : /// the value untouched if the type of V is already DestTy.
1593 1450 : Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1594 7682 : const Twine &Name = "") {
1595 151 : assert(V->getType()->isIntOrIntVectorTy() &&
1596 184 : DestTy->isIntOrIntVectorTy() &&
1597 11391 : "Can only sign extend/truncate integers!");
1598 9753 : Type *VTy = V->getType();
1599 12218 : if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1600 907 : return CreateSExt(V, DestTy, Name);
1601 912 : if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1602 97623 : return CreateTrunc(V, DestTy, Name);
1603 : return V;
1604 11122 : }
1605 :
1606 9 : Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1607 674 : return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1608 74 : }
1609 27 :
1610 27 : Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1611 87 : return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1612 424 : }
1613 612 :
1614 69 : Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1615 872 : return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1616 130 : }
1617 1500156 :
1618 328 : Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1619 242 : return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1620 75 : }
1621 6 :
1622 34 : Value *CreateFPTrunc(Value *V, Type *DestTy,
1623 : const Twine &Name = "") {
1624 60 : return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1625 335 : }
1626 :
1627 243 : Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1628 78 : return CreateCast(Instruction::FPExt, V, DestTy, Name);
1629 945 : }
1630 243 :
1631 410462 : Value *CreatePtrToInt(Value *V, Type *DestTy,
1632 243 : const Twine &Name = "") {
1633 766 : return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1634 : }
1635 :
1636 0 : Value *CreateIntToPtr(Value *V, Type *DestTy,
1637 759 : const Twine &Name = "") {
1638 3932 : return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1639 7 : }
1640 46417 :
1641 57 : Value *CreateBitCast(Value *V, Type *DestTy,
1642 7 : const Twine &Name = "") {
1643 240666 : return CreateCast(Instruction::BitCast, V, DestTy, Name);
1644 42634 : }
1645 0 :
1646 : Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1647 66 : const Twine &Name = "") {
1648 39 : return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1649 455 : }
1650 104 :
1651 495 : Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1652 15343 : const Twine &Name = "") {
1653 513 : if (V->getType() == DestTy)
1654 12 : return V;
1655 98 : if (auto *VC = dyn_cast<Constant>(V))
1656 223 : return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1657 915 : return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1658 2607 : }
1659 197 :
1660 12 : Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1661 0 : const Twine &Name = "") {
1662 10 : if (V->getType() == DestTy)
1663 27943 : return V;
1664 160 : if (auto *VC = dyn_cast<Constant>(V))
1665 0 : return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1666 0 : return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1667 502 : }
1668 2055 :
1669 836 : Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1670 : const Twine &Name = "") {
1671 836 : if (V->getType() == DestTy)
1672 8 : return V;
1673 522 : if (auto *VC = dyn_cast<Constant>(V))
1674 42 : return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1675 1388 : return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1676 0 : }
1677 163 :
1678 285842 : Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1679 113 : const Twine &Name = "") {
1680 280127 : if (V->getType() == DestTy)
1681 201 : return V;
1682 111 : if (auto *VC = dyn_cast<Constant>(V))
1683 191476 : return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1684 172868 : return Insert(CastInst::Create(Op, V, DestTy), Name);
1685 : }
1686 1277 :
1687 28486 : Value *CreatePointerCast(Value *V, Type *DestTy,
1688 132620 : const Twine &Name = "") {
1689 28534 : if (V->getType() == DestTy)
1690 2377 : return V;
1691 657 : if (auto *VC = dyn_cast<Constant>(V))
1692 3217 : return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1693 60722 : return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1694 520 : }
1695 3238 :
1696 23 : Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1697 850 : const Twine &Name = "") {
1698 403582 : if (V->getType() == DestTy)
1699 4636 : return V;
1700 20 :
1701 2005 : if (auto *VC = dyn_cast<Constant>(V)) {
1702 0 : return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1703 57137 : Name);
1704 86 : }
1705 3261 :
1706 616 : return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1707 288699 : Name);
1708 1126 : }
1709 13697 :
1710 1319 : Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1711 20 : const Twine &Name = "") {
1712 1281 : if (V->getType() == DestTy)
1713 13138 : return V;
1714 851 : if (auto *VC = dyn_cast<Constant>(V))
1715 216 : return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1716 840 : return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1717 25974 : }
1718 880653 :
1719 5779 : Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1720 9 : const Twine &Name = "") {
1721 91 : if (V->getType() == DestTy)
1722 4206 : return V;
1723 28386 : if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
1724 1544237 : return CreatePtrToInt(V, DestTy, Name);
1725 2302 : if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
1726 1 : return CreateIntToPtr(V, DestTy, Name);
1727 24733 :
1728 21704 : return CreateBitCast(V, DestTy, Name);
1729 2338 : }
1730 21619 :
1731 0 : Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1732 71928 : if (V->getType() == DestTy)
1733 16816 : return V;
1734 16700 : if (auto *VC = dyn_cast<Constant>(V))
1735 16015 : return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1736 527 : return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1737 174270 : }
1738 57366 :
1739 31326 : // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1740 72849 : // compile time error, instead of converting the string to bool for the
1741 15 : // isSigned parameter.
1742 2159986 : Value *CreateIntCast(Value *, Type *, const char *) = delete;
1743 3127 :
1744 2209694 : //===--------------------------------------------------------------------===//
1745 2641 : // Instruction creation methods: Compare Instructions
1746 500 : //===--------------------------------------------------------------------===//
1747 70160 :
1748 1107397 : Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1749 5057 : return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1750 259 : }
1751 117 :
1752 18298 : Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1753 51447 : return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1754 21481 : }
1755 49786 :
1756 111767 : Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1757 1639 : return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1758 131610 : }
1759 1592890 :
1760 53641 : Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1761 1544442 : return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1762 38482 : }
1763 2214 :
1764 14562 : Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1765 79392 : return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1766 480 : }
1767 4173 :
1768 213 : Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1769 29672 : return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1770 264 : }
1771 70 :
1772 23145 : Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1773 38 : return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1774 22981 : }
1775 0 :
1776 2311 : Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1777 11219 : return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1778 2393 : }
1779 14337 :
1780 8 : Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1781 2485 : return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1782 940 : }
1783 26415 :
1784 173 : Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1785 1871 : return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1786 334 : }
1787 111 :
1788 2096 : Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1789 20 : MDNode *FPMathTag = nullptr) {
1790 131 : return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
1791 650329 : }
1792 94 :
1793 650665 : Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
1794 6703 : MDNode *FPMathTag = nullptr) {
1795 178 : return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
1796 530713 : }
1797 110009 :
1798 26 : Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
1799 245 : MDNode *FPMathTag = nullptr) {
1800 144 : return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
1801 72 : }
1802 95 :
1803 9 : Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
1804 13935 : MDNode *FPMathTag = nullptr) {
1805 57 : return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
1806 12119 : }
1807 0 :
1808 74 : Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
1809 20 : MDNode *FPMathTag = nullptr) {
1810 8930 : return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
1811 8 : }
1812 12 :
1813 104 : Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
1814 12 : MDNode *FPMathTag = nullptr) {
1815 40 : return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
1816 24 : }
1817 225 :
1818 1 : Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
1819 2 : MDNode *FPMathTag = nullptr) {
1820 370 : return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
1821 26 : }
1822 1 :
1823 6191 : Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
1824 337 : MDNode *FPMathTag = nullptr) {
1825 1159 : return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
1826 : }
1827 :
1828 283 : Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1829 16423 : MDNode *FPMathTag = nullptr) {
1830 1224 : return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
1831 : }
1832 18 :
1833 177 : Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
1834 3423 : MDNode *FPMathTag = nullptr) {
1835 4 : return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
1836 265 : }
1837 949 :
1838 26 : Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
1839 8 : MDNode *FPMathTag = nullptr) {
1840 28 : return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
1841 0 : }
1842 19 :
1843 1034 : Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
1844 44 : MDNode *FPMathTag = nullptr) {
1845 112 : return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
1846 : }
1847 28057 :
1848 : Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
1849 84 : MDNode *FPMathTag = nullptr) {
1850 29 : return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
1851 20 : }
1852 4 :
1853 : Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
1854 20 : MDNode *FPMathTag = nullptr) {
1855 23 : return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
1856 : }
1857 32 :
1858 14616 : Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1859 12 : const Twine &Name = "") {
1860 1 : if (auto *LC = dyn_cast<Constant>(LHS))
1861 25 : if (auto *RC = dyn_cast<Constant>(RHS))
1862 590 : return Insert(Folder.CreateICmp(P, LC, RC), Name);
1863 28076 : return Insert(new ICmpInst(P, LHS, RHS), Name);
1864 857 : }
1865 118 :
1866 404 : Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1867 0 : const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1868 121 : if (auto *LC = dyn_cast<Constant>(LHS))
1869 1460 : if (auto *RC = dyn_cast<Constant>(RHS))
1870 66 : return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1871 754 : return Insert(setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag, FMF), Name);
1872 1665 : }
1873 0 :
1874 : //===--------------------------------------------------------------------===//
1875 132 : // Instruction creation methods: Other Instructions
1876 2 : //===--------------------------------------------------------------------===//
1877 3326 :
1878 4155 : PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1879 149 : const Twine &Name = "") {
1880 3830 : return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1881 : }
1882 0 :
1883 967 : CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
1884 3759 : const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1885 5873 : auto *PTy = cast<PointerType>(Callee->getType());
1886 8293 : auto *FTy = cast<FunctionType>(PTy->getElementType());
1887 4534 : return CreateCall(FTy, Callee, Args, Name, FPMathTag);
1888 28 : }
1889 10 :
1890 102274 : CallInst *CreateCall(FunctionType *FTy, Value *Callee,
1891 3 : ArrayRef<Value *> Args, const Twine &Name = "",
1892 114698 : MDNode *FPMathTag = nullptr) {
1893 102354 : CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
1894 216970 : if (isa<FPMathOperator>(CI))
1895 2435 : CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
1896 102593 : return Insert(CI, Name);
1897 0 : }
1898 424 :
1899 664 : CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1900 148088 : ArrayRef<OperandBundleDef> OpBundles,
1901 248 : const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1902 996 : CallInst *CI = CallInst::Create(Callee, Args, OpBundles);
1903 148627 : if (isa<FPMathOperator>(CI))
1904 148424 : CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
1905 4275 : return Insert(CI, Name);
1906 148137 : }
1907 237 :
1908 260 : CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
1909 73 : const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1910 96716 : return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag);
1911 702 : }
1912 599 :
1913 8990 : Value *CreateSelect(Value *C, Value *True, Value *False,
1914 160 : const Twine &Name = "", Instruction *MDFrom = nullptr) {
1915 679 : if (auto *CC = dyn_cast<Constant>(C))
1916 360 : if (auto *TC = dyn_cast<Constant>(True))
1917 127 : if (auto *FC = dyn_cast<Constant>(False))
1918 2028 : return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1919 1292 :
1920 8254 : SelectInst *Sel = SelectInst::Create(C, True, False);
1921 7885 : if (MDFrom) {
1922 1545 : MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
1923 5383 : MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
1924 148 : Sel = addBranchMetadata(Sel, Prof, Unpred);
1925 5546 : }
1926 8112 : return Insert(Sel, Name);
1927 4210 : }
1928 218 :
1929 154 : VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1930 103833 : return Insert(new VAArgInst(List, Ty), Name);
1931 103610 : }
1932 104096 :
1933 38891 : Value *CreateExtractElement(Value *Vec, Value *Idx,
1934 2513 : const Twine &Name = "") {
1935 4679 : if (auto *VC = dyn_cast<Constant>(Vec))
1936 243 : if (auto *IC = dyn_cast<Constant>(Idx))
1937 275 : return Insert(Folder.CreateExtractElement(VC, IC), Name);
1938 36812 : return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1939 316619 : }
1940 2645 :
1941 179 : Value *CreateExtractElement(Value *Vec, uint64_t Idx,
1942 367 : const Twine &Name = "") {
1943 2593 : return CreateExtractElement(Vec, getInt64(Idx), Name);
1944 315257 : }
1945 489 :
1946 198282 : Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1947 3947 : const Twine &Name = "") {
1948 330 : if (auto *VC = dyn_cast<Constant>(Vec))
1949 383 : if (auto *NC = dyn_cast<Constant>(NewElt))
1950 223 : if (auto *IC = dyn_cast<Constant>(Idx))
1951 192314 : return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1952 36933 : return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1953 2552 : }
1954 3780 :
1955 2405 : Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
1956 3376 : const Twine &Name = "") {
1957 23101 : return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
1958 320 : }
1959 5172 :
1960 11598 : Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1961 4971 : const Twine &Name = "") {
1962 4210 : if (auto *V1C = dyn_cast<Constant>(V1))
1963 5216 : if (auto *V2C = dyn_cast<Constant>(V2))
1964 30 : if (auto *MC = dyn_cast<Constant>(Mask))
1965 16930 : return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1966 27641 : return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1967 13663 : }
1968 90 :
1969 7173 : Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask,
1970 1893 : const Twine &Name = "") {
1971 4925 : Value *Mask = ConstantDataVector::get(Context, IntMask);
1972 8521 : return CreateShuffleVector(V1, V2, Mask, Name);
1973 165 : }
1974 8798 :
1975 4038 : Value *CreateExtractValue(Value *Agg,
1976 828 : ArrayRef<unsigned> Idxs,
1977 142 : const Twine &Name = "") {
1978 219 : if (auto *AggC = dyn_cast<Constant>(Agg))
1979 6172 : return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1980 24954 : return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1981 6369 : }
1982 130 :
1983 510 : Value *CreateInsertValue(Value *Agg, Value *Val,
1984 1204 : ArrayRef<unsigned> Idxs,
1985 2580 : const Twine &Name = "") {
1986 474 : if (auto *AggC = dyn_cast<Constant>(Agg))
1987 1741 : if (auto *ValC = dyn_cast<Constant>(Val))
1988 2295183 : return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1989 1362 : return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1990 252 : }
1991 2295249 :
1992 2305419 : LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
1993 20691 : const Twine &Name = "") {
1994 2296960 : return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
1995 1715 : }
1996 2316 :
1997 9374 : //===--------------------------------------------------------------------===//
1998 2551 : // Utility creation methods
1999 65 : //===--------------------------------------------------------------------===//
2000 1716 :
2001 106 : /// Return an i1 value testing if \p Arg is null.
2002 391 : Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2003 422 : return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
2004 19383 : Name);
2005 1138 : }
2006 18100 :
2007 96 : /// Return an i1 value testing if \p Arg is not null.
2008 799 : Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2009 580 : return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
2010 1704 : Name);
2011 5872 : }
2012 5848 :
2013 5941 : /// Return the i64 difference between two pointer values, dividing out
2014 2 : /// the size of the pointed-to objects.
2015 46 : ///
2016 0 : /// This is intended to implement C-style pointer subtraction. As such, the
2017 24 : /// pointers must be appropriately aligned for their element types and
2018 63 : /// pointing into the same object.
2019 0 : Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
2020 473 : assert(LHS->getType() == RHS->getType() &&
2021 190 : "Pointer subtraction operand types must match!");
2022 0 : auto *ArgType = cast<PointerType>(LHS->getType());
2023 0 : Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
2024 70 : Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
2025 138 : Value *Difference = CreateSub(LHS_int, RHS_int);
2026 305 : return CreateExactSDiv(Difference,
2027 1049 : ConstantExpr::getSizeOf(ArgType->getElementType()),
2028 28 : Name);
2029 53 : }
2030 26 :
2031 18 : /// Create a launder.invariant.group intrinsic call. If Ptr type is
2032 849 : /// different from pointer to i8, it's casted to pointer to i8 in the same
2033 : /// address space before call and casted back to Ptr type after call.
2034 0 : Value *CreateLaunderInvariantGroup(Value *Ptr) {
2035 727 : assert(isa<PointerType>(Ptr->getType()) &&
2036 15686 : "launder.invariant.group only applies to pointers.");
2037 45 : // FIXME: we could potentially avoid casts to/from i8*.
2038 72 : auto *PtrType = Ptr->getType();
2039 2993 : auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2040 2015 : if (PtrType != Int8PtrTy)
2041 0 : Ptr = CreateBitCast(Ptr, Int8PtrTy);
2042 : Module *M = BB->getParent()->getParent();
2043 1140 : Function *FnLaunderInvariantGroup = Intrinsic::getDeclaration(
2044 860 : M, Intrinsic::launder_invariant_group, {Int8PtrTy});
2045 1216 :
2046 2228 : assert(FnLaunderInvariantGroup->getReturnType() == Int8PtrTy &&
2047 2045 : FnLaunderInvariantGroup->getFunctionType()->getParamType(0) ==
2048 362 : Int8PtrTy &&
2049 1992 : "LaunderInvariantGroup should take and return the same type");
2050 346 :
2051 : CallInst *Fn = CreateCall(FnLaunderInvariantGroup, {Ptr});
2052 1979 :
2053 6 : if (PtrType != Int8PtrTy)
2054 496 : return CreateBitCast(Fn, PtrType);
2055 6 : return Fn;
2056 : }
2057 598 :
2058 598 : /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2059 1292 : /// different from pointer to i8, it's casted to pointer to i8 in the same
2060 0 : /// address space before call and casted back to Ptr type after call.
2061 : Value *CreateStripInvariantGroup(Value *Ptr) {
2062 : assert(isa<PointerType>(Ptr->getType()) &&
2063 90692 : "strip.invariant.group only applies to pointers.");
2064 28596 :
2065 14 : // FIXME: we could potentially avoid casts to/from i8*.
2066 165939 : auto *PtrType = Ptr->getType();
2067 24 : auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2068 0 : if (PtrType != Int8PtrTy)
2069 713653 : Ptr = CreateBitCast(Ptr, Int8PtrTy);
2070 165906 : Module *M = BB->getParent()->getParent();
2071 33 : Function *FnStripInvariantGroup = Intrinsic::getDeclaration(
2072 324803 : M, Intrinsic::strip_invariant_group, {Int8PtrTy});
2073 554 :
2074 906582 : assert(FnStripInvariantGroup->getReturnType() == Int8PtrTy &&
2075 176 : FnStripInvariantGroup->getFunctionType()->getParamType(0) ==
2076 6188 : Int8PtrTy &&
2077 13487 : "StripInvariantGroup should take and return the same type");
2078 11987 :
2079 122 : CallInst *Fn = CreateCall(FnStripInvariantGroup, {Ptr});
2080 122 :
2081 122 : if (PtrType != Int8PtrTy)
2082 0 : return CreateBitCast(Fn, PtrType);
2083 354 : return Fn;
2084 4658 : }
2085 4658 :
2086 9054 : /// Return a vector value that contains \arg V broadcasted to \p
2087 : /// NumElts elements.
2088 3420 : Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
2089 3438 : assert(NumElts > 0 && "Cannot splat to an empty vector!");
2090 3400 :
2091 4105 : // First insert it into an undef vector so we can shuffle it.
2092 7737 : Type *I32Ty = getInt32Ty();
2093 3857 : Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
2094 3852 : V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
2095 6008 : Name + ".splatinsert");
2096 1053 :
2097 3086 : // Shuffle the value across the desired number of elements.
2098 7072 : Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
2099 4475 : return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
2100 1051 : }
2101 879 :
2102 27842 : /// Return a value that has been extracted from a larger integer type.
2103 28370 : Value *CreateExtractInteger(const DataLayout &DL, Value *From,
2104 28360 : IntegerType *ExtractedTy, uint64_t Offset,
2105 0 : const Twine &Name) {
2106 394 : auto *IntTy = cast<IntegerType>(From->getType());
2107 0 : assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
2108 43 : DL.getTypeStoreSize(IntTy) &&
2109 42 : "Element extends past full value");
2110 : uint64_t ShAmt = 8 * Offset;
2111 5 : Value *V = From;
2112 5 : if (DL.isBigEndian())
2113 16 : ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
2114 11 : DL.getTypeStoreSize(ExtractedTy) - Offset);
2115 11 : if (ShAmt) {
2116 11 : V = CreateLShr(V, ShAmt, Name + ".shift");
2117 16 : }
2118 12 : assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
2119 : "Cannot extract to a larger integer!");
2120 : if (ExtractedTy != IntTy) {
2121 13 : V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
2122 : }
2123 : return V;
2124 : }
2125 5 :
2126 19 : private:
2127 18 : /// Helper function that creates an assume intrinsic call that
2128 24 : /// represents an alignment assumption on the provided Ptr, Mask, Type
2129 6 : /// and Offset.
2130 30 : CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2131 26 : Value *PtrValue, Value *Mask,
2132 0 : Type *IntPtrTy,
2133 12 : Value *OffsetValue) {
2134 25 : Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
2135 8 :
2136 30 : if (OffsetValue) {
2137 12 : bool IsOffsetZero = false;
2138 12 : if (const auto *CI = dyn_cast<ConstantInt>(OffsetValue))
2139 25 : IsOffsetZero = CI->isZero();
2140 12 :
2141 18 : if (!IsOffsetZero) {
2142 5 : if (OffsetValue->getType() != IntPtrTy)
2143 17 : OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
2144 17 : "offsetcast");
2145 5 : PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
2146 10 : }
2147 0 : }
2148 124 :
2149 17 : Value *Zero = ConstantInt::get(IntPtrTy, 0);
2150 17 : Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
2151 17 : Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
2152 141 : return CreateAssumption(InvCond);
2153 124 : }
2154 129 :
2155 137 : public:
2156 5 : /// Create an assume intrinsic call that represents an alignment
2157 5 : /// assumption on the provided pointer.
2158 124 : ///
2159 124 : /// An optional offset can be provided, and if it is provided, the offset
2160 13 : /// must be subtracted from the provided pointer to get the pointer with the
2161 13 : /// specified alignment.
2162 30 : CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2163 13 : unsigned Alignment,
2164 13 : Value *OffsetValue = nullptr) {
2165 26 : assert(isa<PointerType>(PtrValue->getType()) &&
2166 : "trying to create an alignment assumption on a non-pointer?");
2167 17 : auto *PtrTy = cast<PointerType>(PtrValue->getType());
2168 17 : Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2169 :
2170 17 : Value *Mask = ConstantInt::get(IntPtrTy, Alignment > 0 ? Alignment - 1 : 0);
2171 : return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2172 17 : OffsetValue);
2173 13 : }
2174 :
2175 13 : /// Create an assume intrinsic call that represents an alignment
2176 13 : /// assumption on the provided pointer.
2177 : ///
2178 : /// An optional offset can be provided, and if it is provided, the offset
2179 : /// must be subtracted from the provided pointer to get the pointer with the
2180 : /// specified alignment.
2181 : ///
2182 828 : /// This overload handles the condition where the Alignment is dependent
2183 : /// on an existing value rather than a static value.
2184 : CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2185 : Value *Alignment,
2186 828 : Value *OffsetValue = nullptr) {
2187 828 : assert(isa<PointerType>(PtrValue->getType()) &&
2188 828 : "trying to create an alignment assumption on a non-pointer?");
2189 828 : auto *PtrTy = cast<PointerType>(PtrValue->getType());
2190 : Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2191 :
2192 828 : if (Alignment->getType() != IntPtrTy)
2193 828 : Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ true,
2194 : "alignmentcast");
2195 : Value *IsPositive =
2196 : CreateICmp(CmpInst::ICMP_SGT, Alignment,
2197 : ConstantInt::get(Alignment->getType(), 0), "ispositive");
2198 : Value *PositiveMask =
2199 : CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "positivemask");
2200 : Value *Mask = CreateSelect(IsPositive, PositiveMask,
2201 : ConstantInt::get(IntPtrTy, 0), "mask");
2202 :
2203 : return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2204 : OffsetValue);
2205 : }
2206 : };
2207 :
2208 : // Create wrappers for C Binding types (see CBindingWrapping.h).
2209 : DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
2210 :
2211 294 : } // end namespace llvm
2212 :
2213 : #endif // LLVM_IR_IRBUILDER_H
|