blob: 2e0619d54f8601cce9d9f38db4bb626d8d147f67 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the IRBuilder class, which is used as a convenient way
10// to create LLVM instructions with a consistent and simplified interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_IRBUILDER_H
15#define LLVM_IR_IRBUILDER_H
16
17#include "llvm-c/Types.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/IR/BasicBlock.h"
23#include "llvm/IR/Constant.h"
24#include "llvm/IR/ConstantFolder.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DebugLoc.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/GlobalVariable.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/Instruction.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/Intrinsics.h"
35#include "llvm/IR/LLVMContext.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/Operator.h"
38#include "llvm/IR/Type.h"
39#include "llvm/IR/Value.h"
40#include "llvm/IR/ValueHandle.h"
41#include "llvm/Support/AtomicOrdering.h"
42#include "llvm/Support/CBindingWrapping.h"
43#include "llvm/Support/Casting.h"
44#include <cassert>
45#include <cstddef>
46#include <cstdint>
47#include <functional>
48#include <utility>
49
50namespace llvm {
51
52class APInt;
53class MDNode;
54class Use;
55
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056/// This provides the default implementation of the IRBuilder
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057/// 'InsertHelper' method that is called whenever an instruction is created by
58/// IRBuilder and needs to be inserted.
59///
60/// By default, this inserts the instruction at the insertion point.
61class IRBuilderDefaultInserter {
62protected:
63 void InsertHelper(Instruction *I, const Twine &Name,
64 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
65 if (BB) BB->getInstList().insert(InsertPt, I);
66 I->setName(Name);
67 }
68};
69
70/// Provides an 'InsertHelper' that calls a user-provided callback after
71/// performing the default insertion.
72class IRBuilderCallbackInserter : IRBuilderDefaultInserter {
73 std::function<void(Instruction *)> Callback;
74
75public:
76 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
77 : Callback(std::move(Callback)) {}
78
79protected:
80 void InsertHelper(Instruction *I, const Twine &Name,
81 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
82 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
83 Callback(I);
84 }
85};
86
Andrew Scullcdfcccc2018-10-05 20:58:37 +010087/// Common base class shared among various IRBuilders.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010088class IRBuilderBase {
89 DebugLoc CurDbgLocation;
90
91protected:
92 BasicBlock *BB;
93 BasicBlock::iterator InsertPt;
94 LLVMContext &Context;
95
96 MDNode *DefaultFPMathTag;
97 FastMathFlags FMF;
98
99 ArrayRef<OperandBundleDef> DefaultOperandBundles;
100
101public:
102 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
103 ArrayRef<OperandBundleDef> OpBundles = None)
104 : Context(context), DefaultFPMathTag(FPMathTag),
105 DefaultOperandBundles(OpBundles) {
106 ClearInsertionPoint();
107 }
108
109 //===--------------------------------------------------------------------===//
110 // Builder configuration methods
111 //===--------------------------------------------------------------------===//
112
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100113 /// Clear the insertion point: created instructions will not be
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100114 /// inserted into a block.
115 void ClearInsertionPoint() {
116 BB = nullptr;
117 InsertPt = BasicBlock::iterator();
118 }
119
120 BasicBlock *GetInsertBlock() const { return BB; }
121 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
122 LLVMContext &getContext() const { return Context; }
123
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100124 /// This specifies that created instructions should be appended to the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100125 /// end of the specified block.
126 void SetInsertPoint(BasicBlock *TheBB) {
127 BB = TheBB;
128 InsertPt = BB->end();
129 }
130
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100131 /// This specifies that created instructions should be inserted before
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100132 /// the specified instruction.
133 void SetInsertPoint(Instruction *I) {
134 BB = I->getParent();
135 InsertPt = I->getIterator();
136 assert(InsertPt != BB->end() && "Can't read debug loc from end()");
137 SetCurrentDebugLocation(I->getDebugLoc());
138 }
139
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100140 /// This specifies that created instructions should be inserted at the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100141 /// specified point.
142 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
143 BB = TheBB;
144 InsertPt = IP;
145 if (IP != TheBB->end())
146 SetCurrentDebugLocation(IP->getDebugLoc());
147 }
148
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100149 /// Set location information used by debugging information.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100150 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
151
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100152 /// Get location information used by debugging information.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100153 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
154
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100155 /// If this builder has a current debug location, set it on the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100156 /// specified instruction.
157 void SetInstDebugLocation(Instruction *I) const {
158 if (CurDbgLocation)
159 I->setDebugLoc(CurDbgLocation);
160 }
161
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100162 /// Get the return type of the current function that we're emitting
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100163 /// into.
164 Type *getCurrentFunctionReturnType() const;
165
166 /// InsertPoint - A saved insertion point.
167 class InsertPoint {
168 BasicBlock *Block = nullptr;
169 BasicBlock::iterator Point;
170
171 public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100172 /// Creates a new insertion point which doesn't point to anything.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100173 InsertPoint() = default;
174
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100175 /// Creates a new insertion point at the given location.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100176 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
177 : Block(InsertBlock), Point(InsertPoint) {}
178
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100179 /// Returns true if this insert point is set.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100180 bool isSet() const { return (Block != nullptr); }
181
182 BasicBlock *getBlock() const { return Block; }
183 BasicBlock::iterator getPoint() const { return Point; }
184 };
185
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100186 /// Returns the current insert point.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187 InsertPoint saveIP() const {
188 return InsertPoint(GetInsertBlock(), GetInsertPoint());
189 }
190
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100191 /// Returns the current insert point, clearing it in the process.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100192 InsertPoint saveAndClearIP() {
193 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
194 ClearInsertionPoint();
195 return IP;
196 }
197
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100198 /// Sets the current insert point to a previously-saved location.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100199 void restoreIP(InsertPoint IP) {
200 if (IP.isSet())
201 SetInsertPoint(IP.getBlock(), IP.getPoint());
202 else
203 ClearInsertionPoint();
204 }
205
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100206 /// Get the floating point math metadata being used.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100207 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
208
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100209 /// Get the flags to be applied to created floating point ops
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100210 FastMathFlags getFastMathFlags() const { return FMF; }
211
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100212 /// Clear the fast-math flags.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100213 void clearFastMathFlags() { FMF.clear(); }
214
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100215 /// Set the floating point math metadata to be used.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100216 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
217
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100218 /// Set the fast-math flags to be used with generated fp-math operators
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100219 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
220
221 //===--------------------------------------------------------------------===//
222 // RAII helpers.
223 //===--------------------------------------------------------------------===//
224
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100225 // RAII object that stores the current insertion point and restores it
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100226 // when the object is destroyed. This includes the debug location.
227 class InsertPointGuard {
228 IRBuilderBase &Builder;
229 AssertingVH<BasicBlock> Block;
230 BasicBlock::iterator Point;
231 DebugLoc DbgLoc;
232
233 public:
234 InsertPointGuard(IRBuilderBase &B)
235 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
236 DbgLoc(B.getCurrentDebugLocation()) {}
237
238 InsertPointGuard(const InsertPointGuard &) = delete;
239 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
240
241 ~InsertPointGuard() {
242 Builder.restoreIP(InsertPoint(Block, Point));
243 Builder.SetCurrentDebugLocation(DbgLoc);
244 }
245 };
246
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100247 // RAII object that stores the current fast math settings and restores
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100248 // them when the object is destroyed.
249 class FastMathFlagGuard {
250 IRBuilderBase &Builder;
251 FastMathFlags FMF;
252 MDNode *FPMathTag;
253
254 public:
255 FastMathFlagGuard(IRBuilderBase &B)
256 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
257
258 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
259 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
260
261 ~FastMathFlagGuard() {
262 Builder.FMF = FMF;
263 Builder.DefaultFPMathTag = FPMathTag;
264 }
265 };
266
267 //===--------------------------------------------------------------------===//
268 // Miscellaneous creation methods.
269 //===--------------------------------------------------------------------===//
270
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100271 /// Make a new global variable with initializer type i8*
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100272 ///
273 /// Make a new global variable with an initializer that has array of i8 type
274 /// filled in with the null terminated string value specified. The new global
275 /// variable will be marked mergable with any others of the same contents. If
276 /// Name is specified, it is the name of the global variable created.
277 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
278 unsigned AddressSpace = 0);
279
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100280 /// Get a constant value representing either true or false.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100281 ConstantInt *getInt1(bool V) {
282 return ConstantInt::get(getInt1Ty(), V);
283 }
284
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100285 /// Get the constant value for i1 true.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100286 ConstantInt *getTrue() {
287 return ConstantInt::getTrue(Context);
288 }
289
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100290 /// Get the constant value for i1 false.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100291 ConstantInt *getFalse() {
292 return ConstantInt::getFalse(Context);
293 }
294
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100295 /// Get a constant 8-bit value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100296 ConstantInt *getInt8(uint8_t C) {
297 return ConstantInt::get(getInt8Ty(), C);
298 }
299
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100300 /// Get a constant 16-bit value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100301 ConstantInt *getInt16(uint16_t C) {
302 return ConstantInt::get(getInt16Ty(), C);
303 }
304
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100305 /// Get a constant 32-bit value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100306 ConstantInt *getInt32(uint32_t C) {
307 return ConstantInt::get(getInt32Ty(), C);
308 }
309
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100310 /// Get a constant 64-bit value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100311 ConstantInt *getInt64(uint64_t C) {
312 return ConstantInt::get(getInt64Ty(), C);
313 }
314
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100315 /// Get a constant N-bit value, zero extended or truncated from
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100316 /// a 64-bit value.
317 ConstantInt *getIntN(unsigned N, uint64_t C) {
318 return ConstantInt::get(getIntNTy(N), C);
319 }
320
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100321 /// Get a constant integer value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100322 ConstantInt *getInt(const APInt &AI) {
323 return ConstantInt::get(Context, AI);
324 }
325
326 //===--------------------------------------------------------------------===//
327 // Type creation methods
328 //===--------------------------------------------------------------------===//
329
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100330 /// Fetch the type representing a single bit
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100331 IntegerType *getInt1Ty() {
332 return Type::getInt1Ty(Context);
333 }
334
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100335 /// Fetch the type representing an 8-bit integer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100336 IntegerType *getInt8Ty() {
337 return Type::getInt8Ty(Context);
338 }
339
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100340 /// Fetch the type representing a 16-bit integer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100341 IntegerType *getInt16Ty() {
342 return Type::getInt16Ty(Context);
343 }
344
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100345 /// Fetch the type representing a 32-bit integer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100346 IntegerType *getInt32Ty() {
347 return Type::getInt32Ty(Context);
348 }
349
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100350 /// Fetch the type representing a 64-bit integer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100351 IntegerType *getInt64Ty() {
352 return Type::getInt64Ty(Context);
353 }
354
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100355 /// Fetch the type representing a 128-bit integer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100356 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
357
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100358 /// Fetch the type representing an N-bit integer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100359 IntegerType *getIntNTy(unsigned N) {
360 return Type::getIntNTy(Context, N);
361 }
362
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100363 /// Fetch the type representing a 16-bit floating point value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100364 Type *getHalfTy() {
365 return Type::getHalfTy(Context);
366 }
367
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100368 /// Fetch the type representing a 32-bit floating point value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100369 Type *getFloatTy() {
370 return Type::getFloatTy(Context);
371 }
372
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100373 /// Fetch the type representing a 64-bit floating point value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100374 Type *getDoubleTy() {
375 return Type::getDoubleTy(Context);
376 }
377
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100378 /// Fetch the type representing void.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100379 Type *getVoidTy() {
380 return Type::getVoidTy(Context);
381 }
382
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100383 /// Fetch the type representing a pointer to an 8-bit integer value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100384 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
385 return Type::getInt8PtrTy(Context, AddrSpace);
386 }
387
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100388 /// Fetch the type representing a pointer to an integer value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100389 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
390 return DL.getIntPtrType(Context, AddrSpace);
391 }
392
393 //===--------------------------------------------------------------------===//
394 // Intrinsic creation methods
395 //===--------------------------------------------------------------------===//
396
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100397 /// Create and insert a memset to the specified pointer and the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100398 /// specified value.
399 ///
400 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
401 /// specified, it will be added to the instruction. Likewise with alias.scope
402 /// and noalias tags.
403 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
404 bool isVolatile = false, MDNode *TBAATag = nullptr,
405 MDNode *ScopeTag = nullptr,
406 MDNode *NoAliasTag = nullptr) {
407 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
408 TBAATag, ScopeTag, NoAliasTag);
409 }
410
411 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
412 bool isVolatile = false, MDNode *TBAATag = nullptr,
413 MDNode *ScopeTag = nullptr,
414 MDNode *NoAliasTag = nullptr);
415
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100416 /// Create and insert an element unordered-atomic memset of the region of
417 /// memory starting at the given pointer to the given value.
418 ///
419 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
420 /// specified, it will be added to the instruction. Likewise with alias.scope
421 /// and noalias tags.
422 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
423 uint64_t Size, unsigned Align,
424 uint32_t ElementSize,
425 MDNode *TBAATag = nullptr,
426 MDNode *ScopeTag = nullptr,
427 MDNode *NoAliasTag = nullptr) {
428 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), Align,
429 ElementSize, TBAATag, ScopeTag,
430 NoAliasTag);
431 }
432
433 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
434 Value *Size, unsigned Align,
435 uint32_t ElementSize,
436 MDNode *TBAATag = nullptr,
437 MDNode *ScopeTag = nullptr,
438 MDNode *NoAliasTag = nullptr);
439
440 /// Create and insert a memcpy between the specified pointers.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100441 ///
442 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
443 /// specified, it will be added to the instruction. Likewise with alias.scope
444 /// and noalias tags.
445 CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
446 unsigned SrcAlign, uint64_t Size,
447 bool isVolatile = false, MDNode *TBAATag = nullptr,
448 MDNode *TBAAStructTag = nullptr,
449 MDNode *ScopeTag = nullptr,
450 MDNode *NoAliasTag = nullptr) {
451 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
452 isVolatile, TBAATag, TBAAStructTag, ScopeTag,
453 NoAliasTag);
454 }
455
456 CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
457 unsigned SrcAlign, Value *Size,
458 bool isVolatile = false, MDNode *TBAATag = nullptr,
459 MDNode *TBAAStructTag = nullptr,
460 MDNode *ScopeTag = nullptr,
461 MDNode *NoAliasTag = nullptr);
462
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100463 /// Create and insert an element unordered-atomic memcpy between the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100464 /// specified pointers.
465 ///
466 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
467 ///
468 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
469 /// specified, it will be added to the instruction. Likewise with alias.scope
470 /// and noalias tags.
471 CallInst *CreateElementUnorderedAtomicMemCpy(
472 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
473 uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
474 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
475 MDNode *NoAliasTag = nullptr) {
476 return CreateElementUnorderedAtomicMemCpy(
477 Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
478 TBAAStructTag, ScopeTag, NoAliasTag);
479 }
480
481 CallInst *CreateElementUnorderedAtomicMemCpy(
482 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
483 uint32_t ElementSize, MDNode *TBAATag = nullptr,
484 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
485 MDNode *NoAliasTag = nullptr);
486
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100487 /// Create and insert a memmove between the specified
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100488 /// pointers.
489 ///
490 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
491 /// specified, it will be added to the instruction. Likewise with alias.scope
492 /// and noalias tags.
493 CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
494 uint64_t Size, bool isVolatile = false,
495 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
496 MDNode *NoAliasTag = nullptr) {
497 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), isVolatile,
498 TBAATag, ScopeTag, NoAliasTag);
499 }
500
501 CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
502 Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr,
503 MDNode *ScopeTag = nullptr,
504 MDNode *NoAliasTag = nullptr);
505
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100506 /// \brief Create and insert an element unordered-atomic memmove between the
507 /// specified pointers.
508 ///
509 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
510 /// respectively.
511 ///
512 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
513 /// specified, it will be added to the instruction. Likewise with alias.scope
514 /// and noalias tags.
515 CallInst *CreateElementUnorderedAtomicMemMove(
516 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
517 uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
518 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
519 MDNode *NoAliasTag = nullptr) {
520 return CreateElementUnorderedAtomicMemMove(
521 Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
522 TBAAStructTag, ScopeTag, NoAliasTag);
523 }
524
525 CallInst *CreateElementUnorderedAtomicMemMove(
526 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
527 uint32_t ElementSize, MDNode *TBAATag = nullptr,
528 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
529 MDNode *NoAliasTag = nullptr);
530
531 /// Create a vector fadd reduction intrinsic of the source vector.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100532 /// The first parameter is a scalar accumulator value for ordered reductions.
533 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
534
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100535 /// Create a vector fmul reduction intrinsic of the source vector.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100536 /// The first parameter is a scalar accumulator value for ordered reductions.
537 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
538
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100539 /// Create a vector int add reduction intrinsic of the source vector.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100540 CallInst *CreateAddReduce(Value *Src);
541
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100542 /// Create a vector int mul reduction intrinsic of the source vector.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100543 CallInst *CreateMulReduce(Value *Src);
544
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100545 /// Create a vector int AND reduction intrinsic of the source vector.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100546 CallInst *CreateAndReduce(Value *Src);
547
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100548 /// Create a vector int OR reduction intrinsic of the source vector.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100549 CallInst *CreateOrReduce(Value *Src);
550
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100551 /// Create a vector int XOR reduction intrinsic of the source vector.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100552 CallInst *CreateXorReduce(Value *Src);
553
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100554 /// Create a vector integer max reduction intrinsic of the source
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100555 /// vector.
556 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
557
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100558 /// Create a vector integer min reduction intrinsic of the source
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100559 /// vector.
560 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
561
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100562 /// Create a vector float max reduction intrinsic of the source
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100563 /// vector.
564 CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
565
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100566 /// Create a vector float min reduction intrinsic of the source
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100567 /// vector.
568 CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
569
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100570 /// Create a lifetime.start intrinsic.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100571 ///
572 /// If the pointer isn't i8* it will be converted.
573 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
574
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100575 /// Create a lifetime.end intrinsic.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100576 ///
577 /// If the pointer isn't i8* it will be converted.
578 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
579
580 /// Create a call to invariant.start intrinsic.
581 ///
582 /// If the pointer isn't i8* it will be converted.
583 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
584
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100585 /// Create a call to Masked Load intrinsic
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100586 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
587 Value *PassThru = nullptr, const Twine &Name = "");
588
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100589 /// Create a call to Masked Store intrinsic
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100590 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
591 Value *Mask);
592
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100593 /// Create a call to Masked Gather intrinsic
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100594 CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
595 Value *Mask = nullptr,
596 Value *PassThru = nullptr,
597 const Twine& Name = "");
598
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100599 /// Create a call to Masked Scatter intrinsic
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100600 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
601 Value *Mask = nullptr);
602
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100603 /// Create an assume intrinsic call that allows the optimizer to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100604 /// assume that the provided condition will be true.
605 CallInst *CreateAssumption(Value *Cond);
606
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100607 /// Create a call to the experimental.gc.statepoint intrinsic to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100608 /// start a new statepoint sequence.
609 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
610 Value *ActualCallee,
611 ArrayRef<Value *> CallArgs,
612 ArrayRef<Value *> DeoptArgs,
613 ArrayRef<Value *> GCArgs,
614 const Twine &Name = "");
615
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100616 /// Create a call to the experimental.gc.statepoint intrinsic to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100617 /// start a new statepoint sequence.
618 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
619 Value *ActualCallee, uint32_t Flags,
620 ArrayRef<Use> CallArgs,
621 ArrayRef<Use> TransitionArgs,
622 ArrayRef<Use> DeoptArgs,
623 ArrayRef<Value *> GCArgs,
624 const Twine &Name = "");
625
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100626 /// Conveninence function for the common case when CallArgs are filled
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100627 /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
628 /// .get()'ed to get the Value pointer.
629 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
630 Value *ActualCallee, ArrayRef<Use> CallArgs,
631 ArrayRef<Value *> DeoptArgs,
632 ArrayRef<Value *> GCArgs,
633 const Twine &Name = "");
634
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100635 /// Create an invoke to the experimental.gc.statepoint intrinsic to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100636 /// start a new statepoint sequence.
637 InvokeInst *
638 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
639 Value *ActualInvokee, BasicBlock *NormalDest,
640 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
641 ArrayRef<Value *> DeoptArgs,
642 ArrayRef<Value *> GCArgs, const Twine &Name = "");
643
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100644 /// Create an invoke to the experimental.gc.statepoint intrinsic to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100645 /// start a new statepoint sequence.
646 InvokeInst *CreateGCStatepointInvoke(
647 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
648 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
649 ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
650 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
651 const Twine &Name = "");
652
Andrew Scull0372a572018-11-16 15:47:06 +0000653 // Convenience function for the common case when CallArgs are filled in using
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100654 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
655 // get the Value *.
656 InvokeInst *
657 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
658 Value *ActualInvokee, BasicBlock *NormalDest,
659 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
660 ArrayRef<Value *> DeoptArgs,
661 ArrayRef<Value *> GCArgs, const Twine &Name = "");
662
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100663 /// Create a call to the experimental.gc.result intrinsic to extract
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100664 /// the result from a call wrapped in a statepoint.
665 CallInst *CreateGCResult(Instruction *Statepoint,
666 Type *ResultType,
667 const Twine &Name = "");
668
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100669 /// Create a call to the experimental.gc.relocate intrinsics to
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100670 /// project the relocated value of one pointer from the statepoint.
671 CallInst *CreateGCRelocate(Instruction *Statepoint,
672 int BaseOffset,
673 int DerivedOffset,
674 Type *ResultType,
675 const Twine &Name = "");
676
Andrew Scull0372a572018-11-16 15:47:06 +0000677 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
678 /// type.
679 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
680 Instruction *FMFSource = nullptr,
681 const Twine &Name = "");
682
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100683 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
684 /// first type.
Andrew Scull0372a572018-11-16 15:47:06 +0000685 CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
686 Instruction *FMFSource = nullptr,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100687 const Twine &Name = "");
688
Andrew Scull0372a572018-11-16 15:47:06 +0000689 /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
690 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
691 /// the intrinsic.
692 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
693 ArrayRef<Value *> Args,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100694 Instruction *FMFSource = nullptr,
695 const Twine &Name = "");
696
697 /// Create call to the minnum intrinsic.
698 CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
Andrew Scull0372a572018-11-16 15:47:06 +0000699 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100700 }
701
702 /// Create call to the maxnum intrinsic.
703 CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
Andrew Scull0372a572018-11-16 15:47:06 +0000704 return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100705 }
706
Andrew Walbran16937d02019-10-22 13:54:20 +0100707 /// Create call to the minimum intrinsic.
708 CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
709 return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
710 }
711
712 /// Create call to the maximum intrinsic.
713 CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
714 return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
715 }
716
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100717private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100718 /// Create a call to a masked intrinsic with given Id.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100719 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
720 ArrayRef<Type *> OverloadedTypes,
721 const Twine &Name = "");
722
723 Value *getCastedInt8PtrValue(Value *Ptr);
724};
725
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100726/// This provides a uniform API for creating instructions and inserting
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100727/// them into a basic block: either at the end of a BasicBlock, or at a specific
728/// iterator location in a block.
729///
730/// Note that the builder does not expose the full generality of LLVM
731/// instructions. For access to extra instruction properties, use the mutators
732/// (e.g. setVolatile) on the instructions after they have been
733/// created. Convenience state exists to specify fast-math flags and fp-math
734/// tags.
735///
736/// The first template argument specifies a class to use for creating constants.
737/// This defaults to creating minimally folded constants. The second template
738/// argument allows clients to specify custom insertion hooks that are called on
739/// every newly created insertion.
740template <typename T = ConstantFolder,
741 typename Inserter = IRBuilderDefaultInserter>
742class IRBuilder : public IRBuilderBase, public Inserter {
743 T Folder;
744
745public:
746 IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
747 MDNode *FPMathTag = nullptr,
748 ArrayRef<OperandBundleDef> OpBundles = None)
749 : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
750 Folder(F) {}
751
752 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
753 ArrayRef<OperandBundleDef> OpBundles = None)
754 : IRBuilderBase(C, FPMathTag, OpBundles) {}
755
756 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
757 ArrayRef<OperandBundleDef> OpBundles = None)
758 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
759 SetInsertPoint(TheBB);
760 }
761
762 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
763 ArrayRef<OperandBundleDef> OpBundles = None)
764 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
765 SetInsertPoint(TheBB);
766 }
767
768 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
769 ArrayRef<OperandBundleDef> OpBundles = None)
770 : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles) {
771 SetInsertPoint(IP);
772 }
773
774 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F,
775 MDNode *FPMathTag = nullptr,
776 ArrayRef<OperandBundleDef> OpBundles = None)
777 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
778 SetInsertPoint(TheBB, IP);
779 }
780
781 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
782 MDNode *FPMathTag = nullptr,
783 ArrayRef<OperandBundleDef> OpBundles = None)
784 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
785 SetInsertPoint(TheBB, IP);
786 }
787
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100788 /// Get the constant folder being used.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100789 const T &getFolder() { return Folder; }
790
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100791 /// Insert and return the specified instruction.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100792 template<typename InstTy>
793 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
794 this->InsertHelper(I, Name, BB, InsertPt);
795 this->SetInstDebugLocation(I);
796 return I;
797 }
798
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100799 /// No-op overload to handle constants.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100800 Constant *Insert(Constant *C, const Twine& = "") const {
801 return C;
802 }
803
804 //===--------------------------------------------------------------------===//
805 // Instruction creation methods: Terminators
806 //===--------------------------------------------------------------------===//
807
808private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100809 /// Helper to add branch weight and unpredictable metadata onto an
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100810 /// instruction.
811 /// \returns The annotated instruction.
812 template <typename InstTy>
813 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
814 if (Weights)
815 I->setMetadata(LLVMContext::MD_prof, Weights);
816 if (Unpredictable)
817 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
818 return I;
819 }
820
821public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100822 /// Create a 'ret void' instruction.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100823 ReturnInst *CreateRetVoid() {
824 return Insert(ReturnInst::Create(Context));
825 }
826
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100827 /// Create a 'ret <val>' instruction.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100828 ReturnInst *CreateRet(Value *V) {
829 return Insert(ReturnInst::Create(Context, V));
830 }
831
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100832 /// Create a sequence of N insertvalue instructions,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100833 /// with one Value from the retVals array each, that build a aggregate
834 /// return value one value at a time, and a ret instruction to return
835 /// the resulting aggregate value.
836 ///
837 /// This is a convenience function for code that uses aggregate return values
838 /// as a vehicle for having multiple return values.
839 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
840 Value *V = UndefValue::get(getCurrentFunctionReturnType());
841 for (unsigned i = 0; i != N; ++i)
842 V = CreateInsertValue(V, retVals[i], i, "mrv");
843 return Insert(ReturnInst::Create(Context, V));
844 }
845
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100846 /// Create an unconditional 'br label X' instruction.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100847 BranchInst *CreateBr(BasicBlock *Dest) {
848 return Insert(BranchInst::Create(Dest));
849 }
850
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100851 /// Create a conditional 'br Cond, TrueDest, FalseDest'
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100852 /// instruction.
853 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
854 MDNode *BranchWeights = nullptr,
855 MDNode *Unpredictable = nullptr) {
856 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
857 BranchWeights, Unpredictable));
858 }
859
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100860 /// Create a conditional 'br Cond, TrueDest, FalseDest'
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100861 /// instruction. Copy branch meta data if available.
862 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
863 Instruction *MDSrc) {
864 BranchInst *Br = BranchInst::Create(True, False, Cond);
865 if (MDSrc) {
866 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
867 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
868 Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
869 }
870 return Insert(Br);
871 }
872
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100873 /// Create a switch instruction with the specified value, default dest,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100874 /// and with a hint for the number of cases that will be added (for efficient
875 /// allocation).
876 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
877 MDNode *BranchWeights = nullptr,
878 MDNode *Unpredictable = nullptr) {
879 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
880 BranchWeights, Unpredictable));
881 }
882
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100883 /// Create an indirect branch instruction with the specified address
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100884 /// operand, with an optional hint for the number of destinations that will be
885 /// added (for efficient allocation).
886 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
887 return Insert(IndirectBrInst::Create(Addr, NumDests));
888 }
889
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100890 /// Create an invoke instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100891 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
892 BasicBlock *NormalDest, BasicBlock *UnwindDest,
893 ArrayRef<Value *> Args,
894 ArrayRef<OperandBundleDef> OpBundles,
895 const Twine &Name = "") {
896 return Insert(
897 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles),
898 Name);
899 }
900 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
901 BasicBlock *NormalDest, BasicBlock *UnwindDest,
902 ArrayRef<Value *> Args = None,
903 const Twine &Name = "") {
904 return Insert(InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args),
905 Name);
906 }
907
908 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
909 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
910 ArrayRef<OperandBundleDef> OpBundles,
911 const Twine &Name = "") {
912 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
913 NormalDest, UnwindDest, Args, OpBundles, Name);
914 }
915
916 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100917 BasicBlock *UnwindDest,
918 ArrayRef<Value *> Args = None,
919 const Twine &Name = "") {
Andrew Walbran16937d02019-10-22 13:54:20 +0100920 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
921 NormalDest, UnwindDest, Args, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100922 }
Andrew Walbran16937d02019-10-22 13:54:20 +0100923
924 // Deprecated [opaque pointer types]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100925 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
926 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
927 ArrayRef<OperandBundleDef> OpBundles,
928 const Twine &Name = "") {
Andrew Walbran16937d02019-10-22 13:54:20 +0100929 return CreateInvoke(
930 cast<FunctionType>(
931 cast<PointerType>(Callee->getType())->getElementType()),
932 Callee, NormalDest, UnwindDest, Args, OpBundles, Name);
933 }
934
935 // Deprecated [opaque pointer types]
936 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
937 BasicBlock *UnwindDest,
938 ArrayRef<Value *> Args = None,
939 const Twine &Name = "") {
940 return CreateInvoke(
941 cast<FunctionType>(
942 cast<PointerType>(Callee->getType())->getElementType()),
943 Callee, NormalDest, UnwindDest, Args, Name);
944 }
945
946 /// \brief Create a callbr instruction.
947 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
948 BasicBlock *DefaultDest,
949 ArrayRef<BasicBlock *> IndirectDests,
950 ArrayRef<Value *> Args = None,
951 const Twine &Name = "") {
952 return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
953 Args), Name);
954 }
955 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
956 BasicBlock *DefaultDest,
957 ArrayRef<BasicBlock *> IndirectDests,
958 ArrayRef<Value *> Args,
959 ArrayRef<OperandBundleDef> OpBundles,
960 const Twine &Name = "") {
961 return Insert(
962 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
963 OpBundles), Name);
964 }
965
966 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
967 ArrayRef<BasicBlock *> IndirectDests,
968 ArrayRef<Value *> Args = None,
969 const Twine &Name = "") {
970 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
971 DefaultDest, IndirectDests, Args, Name);
972 }
973 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
974 ArrayRef<BasicBlock *> IndirectDests,
975 ArrayRef<Value *> Args,
976 ArrayRef<OperandBundleDef> OpBundles,
977 const Twine &Name = "") {
978 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
979 DefaultDest, IndirectDests, Args, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100980 }
981
982 ResumeInst *CreateResume(Value *Exn) {
983 return Insert(ResumeInst::Create(Exn));
984 }
985
986 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
987 BasicBlock *UnwindBB = nullptr) {
988 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
989 }
990
991 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
992 unsigned NumHandlers,
993 const Twine &Name = "") {
994 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
995 Name);
996 }
997
998 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
999 const Twine &Name = "") {
1000 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1001 }
1002
1003 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1004 ArrayRef<Value *> Args = None,
1005 const Twine &Name = "") {
1006 return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1007 }
1008
1009 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1010 return Insert(CatchReturnInst::Create(CatchPad, BB));
1011 }
1012
1013 UnreachableInst *CreateUnreachable() {
1014 return Insert(new UnreachableInst(Context));
1015 }
1016
1017 //===--------------------------------------------------------------------===//
1018 // Instruction creation methods: Binary Operators
1019 //===--------------------------------------------------------------------===//
1020private:
1021 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1022 Value *LHS, Value *RHS,
1023 const Twine &Name,
1024 bool HasNUW, bool HasNSW) {
1025 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1026 if (HasNUW) BO->setHasNoUnsignedWrap();
1027 if (HasNSW) BO->setHasNoSignedWrap();
1028 return BO;
1029 }
1030
1031 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1032 FastMathFlags FMF) const {
1033 if (!FPMD)
1034 FPMD = DefaultFPMathTag;
1035 if (FPMD)
1036 I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1037 I->setFastMathFlags(FMF);
1038 return I;
1039 }
1040
1041 Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
Andrew Walbran16937d02019-10-22 13:54:20 +01001042 Value *R, const Twine &Name) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001043 auto *LC = dyn_cast<Constant>(L);
1044 auto *RC = dyn_cast<Constant>(R);
1045 return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
1046 }
1047
1048public:
1049 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1050 bool HasNUW = false, bool HasNSW = false) {
1051 if (auto *LC = dyn_cast<Constant>(LHS))
1052 if (auto *RC = dyn_cast<Constant>(RHS))
1053 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
1054 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
1055 HasNUW, HasNSW);
1056 }
1057
1058 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1059 return CreateAdd(LHS, RHS, Name, false, true);
1060 }
1061
1062 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1063 return CreateAdd(LHS, RHS, Name, true, false);
1064 }
1065
1066 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1067 bool HasNUW = false, bool HasNSW = false) {
1068 if (auto *LC = dyn_cast<Constant>(LHS))
1069 if (auto *RC = dyn_cast<Constant>(RHS))
1070 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
1071 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
1072 HasNUW, HasNSW);
1073 }
1074
1075 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1076 return CreateSub(LHS, RHS, Name, false, true);
1077 }
1078
1079 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1080 return CreateSub(LHS, RHS, Name, true, false);
1081 }
1082
1083 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1084 bool HasNUW = false, bool HasNSW = false) {
1085 if (auto *LC = dyn_cast<Constant>(LHS))
1086 if (auto *RC = dyn_cast<Constant>(RHS))
1087 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
1088 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
1089 HasNUW, HasNSW);
1090 }
1091
1092 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1093 return CreateMul(LHS, RHS, Name, false, true);
1094 }
1095
1096 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1097 return CreateMul(LHS, RHS, Name, true, false);
1098 }
1099
1100 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1101 bool isExact = false) {
1102 if (auto *LC = dyn_cast<Constant>(LHS))
1103 if (auto *RC = dyn_cast<Constant>(RHS))
1104 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
1105 if (!isExact)
1106 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1107 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1108 }
1109
1110 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1111 return CreateUDiv(LHS, RHS, Name, true);
1112 }
1113
1114 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1115 bool isExact = false) {
1116 if (auto *LC = dyn_cast<Constant>(LHS))
1117 if (auto *RC = dyn_cast<Constant>(RHS))
1118 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
1119 if (!isExact)
1120 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1121 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1122 }
1123
1124 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1125 return CreateSDiv(LHS, RHS, Name, true);
1126 }
1127
1128 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1129 if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
1130 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1131 }
1132
1133 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1134 if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
1135 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1136 }
1137
1138 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1139 bool HasNUW = false, bool HasNSW = false) {
1140 if (auto *LC = dyn_cast<Constant>(LHS))
1141 if (auto *RC = dyn_cast<Constant>(RHS))
1142 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
1143 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1144 HasNUW, HasNSW);
1145 }
1146
1147 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1148 bool HasNUW = false, bool HasNSW = false) {
1149 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1150 HasNUW, HasNSW);
1151 }
1152
1153 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1154 bool HasNUW = false, bool HasNSW = false) {
1155 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1156 HasNUW, HasNSW);
1157 }
1158
1159 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1160 bool isExact = false) {
1161 if (auto *LC = dyn_cast<Constant>(LHS))
1162 if (auto *RC = dyn_cast<Constant>(RHS))
1163 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
1164 if (!isExact)
1165 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1166 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1167 }
1168
1169 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1170 bool isExact = false) {
1171 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1172 }
1173
1174 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1175 bool isExact = false) {
1176 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1177 }
1178
1179 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1180 bool isExact = false) {
1181 if (auto *LC = dyn_cast<Constant>(LHS))
1182 if (auto *RC = dyn_cast<Constant>(RHS))
1183 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
1184 if (!isExact)
1185 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1186 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1187 }
1188
1189 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1190 bool isExact = false) {
1191 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1192 }
1193
1194 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1195 bool isExact = false) {
1196 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1197 }
1198
1199 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1200 if (auto *RC = dyn_cast<Constant>(RHS)) {
1201 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
1202 return LHS; // LHS & -1 -> LHS
1203 if (auto *LC = dyn_cast<Constant>(LHS))
1204 return Insert(Folder.CreateAnd(LC, RC), Name);
1205 }
1206 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1207 }
1208
1209 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1210 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1211 }
1212
1213 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1214 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1215 }
1216
1217 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1218 if (auto *RC = dyn_cast<Constant>(RHS)) {
1219 if (RC->isNullValue())
1220 return LHS; // LHS | 0 -> LHS
1221 if (auto *LC = dyn_cast<Constant>(LHS))
1222 return Insert(Folder.CreateOr(LC, RC), Name);
1223 }
1224 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1225 }
1226
1227 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1228 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1229 }
1230
1231 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1232 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1233 }
1234
1235 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1236 if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V;
1237 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1238 }
1239
1240 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1241 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1242 }
1243
1244 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1245 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1246 }
1247
1248 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1249 MDNode *FPMD = nullptr) {
1250 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1251 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1252 return Insert(I, Name);
1253 }
1254
1255 /// Copy fast-math-flags from an instruction rather than using the builder's
1256 /// default FMF.
1257 Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1258 const Twine &Name = "") {
1259 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1260 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr,
1261 FMFSource->getFastMathFlags());
1262 return Insert(I, Name);
1263 }
1264
1265 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1266 MDNode *FPMD = nullptr) {
1267 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1268 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1269 return Insert(I, Name);
1270 }
1271
1272 /// Copy fast-math-flags from an instruction rather than using the builder's
1273 /// default FMF.
1274 Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1275 const Twine &Name = "") {
1276 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1277 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr,
1278 FMFSource->getFastMathFlags());
1279 return Insert(I, Name);
1280 }
1281
1282 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1283 MDNode *FPMD = nullptr) {
1284 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1285 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1286 return Insert(I, Name);
1287 }
1288
1289 /// Copy fast-math-flags from an instruction rather than using the builder's
1290 /// default FMF.
1291 Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1292 const Twine &Name = "") {
1293 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1294 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr,
1295 FMFSource->getFastMathFlags());
1296 return Insert(I, Name);
1297 }
1298
1299 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1300 MDNode *FPMD = nullptr) {
1301 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1302 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1303 return Insert(I, Name);
1304 }
1305
1306 /// Copy fast-math-flags from an instruction rather than using the builder's
1307 /// default FMF.
1308 Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1309 const Twine &Name = "") {
1310 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1311 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr,
1312 FMFSource->getFastMathFlags());
1313 return Insert(I, Name);
1314 }
1315
1316 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1317 MDNode *FPMD = nullptr) {
1318 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1319 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1320 return Insert(I, Name);
1321 }
1322
1323 /// Copy fast-math-flags from an instruction rather than using the builder's
1324 /// default FMF.
1325 Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1326 const Twine &Name = "") {
1327 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1328 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr,
1329 FMFSource->getFastMathFlags());
1330 return Insert(I, Name);
1331 }
1332
1333 Value *CreateBinOp(Instruction::BinaryOps Opc,
1334 Value *LHS, Value *RHS, const Twine &Name = "",
1335 MDNode *FPMathTag = nullptr) {
1336 if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V;
1337 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1338 if (isa<FPMathOperator>(BinOp))
1339 BinOp = setFPAttrs(BinOp, FPMathTag, FMF);
1340 return Insert(BinOp, Name);
1341 }
1342
1343 Value *CreateNeg(Value *V, const Twine &Name = "",
1344 bool HasNUW = false, bool HasNSW = false) {
1345 if (auto *VC = dyn_cast<Constant>(V))
1346 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
1347 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
1348 if (HasNUW) BO->setHasNoUnsignedWrap();
1349 if (HasNSW) BO->setHasNoSignedWrap();
1350 return BO;
1351 }
1352
1353 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1354 return CreateNeg(V, Name, false, true);
1355 }
1356
1357 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1358 return CreateNeg(V, Name, true, false);
1359 }
1360
1361 Value *CreateFNeg(Value *V, const Twine &Name = "",
1362 MDNode *FPMathTag = nullptr) {
1363 if (auto *VC = dyn_cast<Constant>(V))
1364 return Insert(Folder.CreateFNeg(VC), Name);
1365 return Insert(setFPAttrs(BinaryOperator::CreateFNeg(V), FPMathTag, FMF),
1366 Name);
1367 }
1368
1369 Value *CreateNot(Value *V, const Twine &Name = "") {
1370 if (auto *VC = dyn_cast<Constant>(V))
1371 return Insert(Folder.CreateNot(VC), Name);
1372 return Insert(BinaryOperator::CreateNot(V), Name);
1373 }
1374
1375 //===--------------------------------------------------------------------===//
1376 // Instruction creation methods: Memory Instructions
1377 //===--------------------------------------------------------------------===//
1378
1379 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1380 Value *ArraySize = nullptr, const Twine &Name = "") {
1381 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
1382 }
1383
1384 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1385 const Twine &Name = "") {
1386 const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
1387 return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
1388 }
1389
Andrew Walbran16937d02019-10-22 13:54:20 +01001390 /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001391 /// converting the string to 'bool' for the isVolatile parameter.
Andrew Walbran16937d02019-10-22 13:54:20 +01001392 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1393 return Insert(new LoadInst(Ty, Ptr), Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001394 }
1395
1396 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1397 return Insert(new LoadInst(Ty, Ptr), Name);
1398 }
1399
Andrew Walbran16937d02019-10-22 13:54:20 +01001400 LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1401 const Twine &Name = "") {
1402 return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile), Name);
1403 }
1404
1405 // Deprecated [opaque pointer types]
1406 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1407 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1408 }
1409
1410 // Deprecated [opaque pointer types]
1411 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1412 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1413 }
1414
1415 // Deprecated [opaque pointer types]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001416 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
Andrew Walbran16937d02019-10-22 13:54:20 +01001417 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, isVolatile,
1418 Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001419 }
1420
1421 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1422 return Insert(new StoreInst(Val, Ptr, isVolatile));
1423 }
1424
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001425 /// Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001426 /// correctly, instead of converting the string to 'bool' for the isVolatile
1427 /// parameter.
Andrew Walbran16937d02019-10-22 13:54:20 +01001428 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1429 const char *Name) {
1430 LoadInst *LI = CreateLoad(Ty, Ptr, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001431 LI->setAlignment(Align);
1432 return LI;
1433 }
Andrew Walbran16937d02019-10-22 13:54:20 +01001434 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1435 const Twine &Name = "") {
1436 LoadInst *LI = CreateLoad(Ty, Ptr, Name);
1437 LI->setAlignment(Align);
1438 return LI;
1439 }
1440 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1441 bool isVolatile, const Twine &Name = "") {
1442 LoadInst *LI = CreateLoad(Ty, Ptr, isVolatile, Name);
1443 LI->setAlignment(Align);
1444 return LI;
1445 }
1446
1447 // Deprecated [opaque pointer types]
1448 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1449 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1450 Align, Name);
1451 }
1452 // Deprecated [opaque pointer types]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001453 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
1454 const Twine &Name = "") {
Andrew Walbran16937d02019-10-22 13:54:20 +01001455 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1456 Align, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001457 }
Andrew Walbran16937d02019-10-22 13:54:20 +01001458 // Deprecated [opaque pointer types]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001459 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
1460 const Twine &Name = "") {
Andrew Walbran16937d02019-10-22 13:54:20 +01001461 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1462 Align, isVolatile, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001463 }
1464
1465 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
1466 bool isVolatile = false) {
1467 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1468 SI->setAlignment(Align);
1469 return SI;
1470 }
1471
1472 FenceInst *CreateFence(AtomicOrdering Ordering,
1473 SyncScope::ID SSID = SyncScope::System,
1474 const Twine &Name = "") {
1475 return Insert(new FenceInst(Context, Ordering, SSID), Name);
1476 }
1477
1478 AtomicCmpXchgInst *
1479 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1480 AtomicOrdering SuccessOrdering,
1481 AtomicOrdering FailureOrdering,
1482 SyncScope::ID SSID = SyncScope::System) {
1483 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1484 FailureOrdering, SSID));
1485 }
1486
1487 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1488 AtomicOrdering Ordering,
1489 SyncScope::ID SSID = SyncScope::System) {
1490 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SSID));
1491 }
1492
1493 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1494 const Twine &Name = "") {
1495 return CreateGEP(nullptr, Ptr, IdxList, Name);
1496 }
1497
1498 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1499 const Twine &Name = "") {
1500 if (auto *PC = dyn_cast<Constant>(Ptr)) {
1501 // Every index must be constant.
1502 size_t i, e;
1503 for (i = 0, e = IdxList.size(); i != e; ++i)
1504 if (!isa<Constant>(IdxList[i]))
1505 break;
1506 if (i == e)
1507 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1508 }
1509 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1510 }
1511
1512 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1513 const Twine &Name = "") {
1514 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1515 }
1516
1517 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1518 const Twine &Name = "") {
1519 if (auto *PC = dyn_cast<Constant>(Ptr)) {
1520 // Every index must be constant.
1521 size_t i, e;
1522 for (i = 0, e = IdxList.size(); i != e; ++i)
1523 if (!isa<Constant>(IdxList[i]))
1524 break;
1525 if (i == e)
1526 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1527 Name);
1528 }
1529 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1530 }
1531
1532 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1533 return CreateGEP(nullptr, Ptr, Idx, Name);
1534 }
1535
1536 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1537 if (auto *PC = dyn_cast<Constant>(Ptr))
1538 if (auto *IC = dyn_cast<Constant>(Idx))
1539 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1540 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1541 }
1542
1543 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1544 const Twine &Name = "") {
1545 if (auto *PC = dyn_cast<Constant>(Ptr))
1546 if (auto *IC = dyn_cast<Constant>(Idx))
1547 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1548 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1549 }
1550
1551 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1552 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1553 }
1554
1555 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1556 const Twine &Name = "") {
1557 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1558
1559 if (auto *PC = dyn_cast<Constant>(Ptr))
1560 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1561
1562 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1563 }
1564
1565 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1566 const Twine &Name = "") {
1567 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1568
1569 if (auto *PC = dyn_cast<Constant>(Ptr))
1570 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1571
1572 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1573 }
1574
1575 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1576 const Twine &Name = "") {
1577 Value *Idxs[] = {
1578 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1579 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1580 };
1581
1582 if (auto *PC = dyn_cast<Constant>(Ptr))
1583 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1584
1585 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1586 }
1587
1588 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1589 unsigned Idx1, const Twine &Name = "") {
1590 Value *Idxs[] = {
1591 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1592 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1593 };
1594
1595 if (auto *PC = dyn_cast<Constant>(Ptr))
1596 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1597
1598 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1599 }
1600
Andrew Walbran16937d02019-10-22 13:54:20 +01001601 Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1602 const Twine &Name = "") {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001603 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1604
1605 if (auto *PC = dyn_cast<Constant>(Ptr))
Andrew Walbran16937d02019-10-22 13:54:20 +01001606 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001607
Andrew Walbran16937d02019-10-22 13:54:20 +01001608 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1609 }
1610
1611 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1612 return CreateConstGEP1_64(nullptr, Ptr, Idx0, Name);
1613 }
1614
1615 Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1616 const Twine &Name = "") {
1617 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1618
1619 if (auto *PC = dyn_cast<Constant>(Ptr))
1620 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1621
1622 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001623 }
1624
1625 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1626 const Twine &Name = "") {
Andrew Walbran16937d02019-10-22 13:54:20 +01001627 return CreateConstInBoundsGEP1_64(nullptr, Ptr, Idx0, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001628 }
1629
Andrew Walbran16937d02019-10-22 13:54:20 +01001630 Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1631 const Twine &Name = "") {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001632 Value *Idxs[] = {
1633 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1634 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1635 };
1636
1637 if (auto *PC = dyn_cast<Constant>(Ptr))
Andrew Walbran16937d02019-10-22 13:54:20 +01001638 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001639
Andrew Walbran16937d02019-10-22 13:54:20 +01001640 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1641 }
1642
1643 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1644 const Twine &Name = "") {
1645 return CreateConstGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1646 }
1647
1648 Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1649 uint64_t Idx1, const Twine &Name = "") {
1650 Value *Idxs[] = {
1651 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1652 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1653 };
1654
1655 if (auto *PC = dyn_cast<Constant>(Ptr))
1656 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1657
1658 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001659 }
1660
1661 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1662 const Twine &Name = "") {
Andrew Walbran16937d02019-10-22 13:54:20 +01001663 return CreateConstInBoundsGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001664 }
1665
1666 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1667 const Twine &Name = "") {
1668 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1669 }
1670
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001671 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1672 return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name);
1673 }
1674
1675 /// Same as CreateGlobalString, but return a pointer with "i8*" type
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001676 /// instead of a pointer to array of i8.
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001677 Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1678 unsigned AddressSpace = 0) {
1679 GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace);
1680 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1681 Constant *Indices[] = {Zero, Zero};
1682 return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
1683 Indices);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001684 }
1685
1686 //===--------------------------------------------------------------------===//
1687 // Instruction creation methods: Cast/Conversion Operators
1688 //===--------------------------------------------------------------------===//
1689
1690 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1691 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1692 }
1693
1694 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1695 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1696 }
1697
1698 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1699 return CreateCast(Instruction::SExt, V, DestTy, Name);
1700 }
1701
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001702 /// Create a ZExt or Trunc from the integer value V to DestTy. Return
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001703 /// the value untouched if the type of V is already DestTy.
1704 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1705 const Twine &Name = "") {
1706 assert(V->getType()->isIntOrIntVectorTy() &&
1707 DestTy->isIntOrIntVectorTy() &&
1708 "Can only zero extend/truncate integers!");
1709 Type *VTy = V->getType();
1710 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1711 return CreateZExt(V, DestTy, Name);
1712 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1713 return CreateTrunc(V, DestTy, Name);
1714 return V;
1715 }
1716
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001717 /// Create a SExt or Trunc from the integer value V to DestTy. Return
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001718 /// the value untouched if the type of V is already DestTy.
1719 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1720 const Twine &Name = "") {
1721 assert(V->getType()->isIntOrIntVectorTy() &&
1722 DestTy->isIntOrIntVectorTy() &&
1723 "Can only sign extend/truncate integers!");
1724 Type *VTy = V->getType();
1725 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1726 return CreateSExt(V, DestTy, Name);
1727 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1728 return CreateTrunc(V, DestTy, Name);
1729 return V;
1730 }
1731
1732 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1733 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1734 }
1735
1736 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1737 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1738 }
1739
1740 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1741 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1742 }
1743
1744 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1745 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1746 }
1747
1748 Value *CreateFPTrunc(Value *V, Type *DestTy,
1749 const Twine &Name = "") {
1750 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1751 }
1752
1753 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1754 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1755 }
1756
1757 Value *CreatePtrToInt(Value *V, Type *DestTy,
1758 const Twine &Name = "") {
1759 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1760 }
1761
1762 Value *CreateIntToPtr(Value *V, Type *DestTy,
1763 const Twine &Name = "") {
1764 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1765 }
1766
1767 Value *CreateBitCast(Value *V, Type *DestTy,
1768 const Twine &Name = "") {
1769 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1770 }
1771
1772 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1773 const Twine &Name = "") {
1774 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1775 }
1776
1777 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1778 const Twine &Name = "") {
1779 if (V->getType() == DestTy)
1780 return V;
1781 if (auto *VC = dyn_cast<Constant>(V))
1782 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1783 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1784 }
1785
1786 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1787 const Twine &Name = "") {
1788 if (V->getType() == DestTy)
1789 return V;
1790 if (auto *VC = dyn_cast<Constant>(V))
1791 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1792 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1793 }
1794
1795 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1796 const Twine &Name = "") {
1797 if (V->getType() == DestTy)
1798 return V;
1799 if (auto *VC = dyn_cast<Constant>(V))
1800 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1801 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1802 }
1803
1804 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1805 const Twine &Name = "") {
1806 if (V->getType() == DestTy)
1807 return V;
1808 if (auto *VC = dyn_cast<Constant>(V))
1809 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1810 return Insert(CastInst::Create(Op, V, DestTy), Name);
1811 }
1812
1813 Value *CreatePointerCast(Value *V, Type *DestTy,
1814 const Twine &Name = "") {
1815 if (V->getType() == DestTy)
1816 return V;
1817 if (auto *VC = dyn_cast<Constant>(V))
1818 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1819 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1820 }
1821
1822 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1823 const Twine &Name = "") {
1824 if (V->getType() == DestTy)
1825 return V;
1826
1827 if (auto *VC = dyn_cast<Constant>(V)) {
1828 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1829 Name);
1830 }
1831
1832 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1833 Name);
1834 }
1835
1836 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1837 const Twine &Name = "") {
1838 if (V->getType() == DestTy)
1839 return V;
1840 if (auto *VC = dyn_cast<Constant>(V))
1841 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1842 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1843 }
1844
1845 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1846 const Twine &Name = "") {
1847 if (V->getType() == DestTy)
1848 return V;
1849 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
1850 return CreatePtrToInt(V, DestTy, Name);
1851 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
1852 return CreateIntToPtr(V, DestTy, Name);
1853
1854 return CreateBitCast(V, DestTy, Name);
1855 }
1856
1857 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1858 if (V->getType() == DestTy)
1859 return V;
1860 if (auto *VC = dyn_cast<Constant>(V))
1861 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1862 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1863 }
1864
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001865 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001866 // compile time error, instead of converting the string to bool for the
1867 // isSigned parameter.
1868 Value *CreateIntCast(Value *, Type *, const char *) = delete;
1869
1870 //===--------------------------------------------------------------------===//
1871 // Instruction creation methods: Compare Instructions
1872 //===--------------------------------------------------------------------===//
1873
1874 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1875 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1876 }
1877
1878 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1879 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1880 }
1881
1882 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1883 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1884 }
1885
1886 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1887 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1888 }
1889
1890 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1891 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1892 }
1893
1894 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1895 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1896 }
1897
1898 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1899 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1900 }
1901
1902 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1903 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1904 }
1905
1906 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1907 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1908 }
1909
1910 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1911 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1912 }
1913
1914 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1915 MDNode *FPMathTag = nullptr) {
1916 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
1917 }
1918
1919 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
1920 MDNode *FPMathTag = nullptr) {
1921 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
1922 }
1923
1924 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
1925 MDNode *FPMathTag = nullptr) {
1926 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
1927 }
1928
1929 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
1930 MDNode *FPMathTag = nullptr) {
1931 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
1932 }
1933
1934 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
1935 MDNode *FPMathTag = nullptr) {
1936 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
1937 }
1938
1939 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
1940 MDNode *FPMathTag = nullptr) {
1941 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
1942 }
1943
1944 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
1945 MDNode *FPMathTag = nullptr) {
1946 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
1947 }
1948
1949 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
1950 MDNode *FPMathTag = nullptr) {
1951 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
1952 }
1953
1954 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1955 MDNode *FPMathTag = nullptr) {
1956 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
1957 }
1958
1959 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
1960 MDNode *FPMathTag = nullptr) {
1961 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
1962 }
1963
1964 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
1965 MDNode *FPMathTag = nullptr) {
1966 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
1967 }
1968
1969 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
1970 MDNode *FPMathTag = nullptr) {
1971 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
1972 }
1973
1974 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
1975 MDNode *FPMathTag = nullptr) {
1976 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
1977 }
1978
1979 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
1980 MDNode *FPMathTag = nullptr) {
1981 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
1982 }
1983
1984 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1985 const Twine &Name = "") {
1986 if (auto *LC = dyn_cast<Constant>(LHS))
1987 if (auto *RC = dyn_cast<Constant>(RHS))
1988 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1989 return Insert(new ICmpInst(P, LHS, RHS), Name);
1990 }
1991
1992 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1993 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1994 if (auto *LC = dyn_cast<Constant>(LHS))
1995 if (auto *RC = dyn_cast<Constant>(RHS))
1996 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1997 return Insert(setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag, FMF), Name);
1998 }
1999
2000 //===--------------------------------------------------------------------===//
2001 // Instruction creation methods: Other Instructions
2002 //===--------------------------------------------------------------------===//
2003
2004 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2005 const Twine &Name = "") {
2006 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
2007 }
2008
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002009 CallInst *CreateCall(FunctionType *FTy, Value *Callee,
Andrew Walbran16937d02019-10-22 13:54:20 +01002010 ArrayRef<Value *> Args = None, const Twine &Name = "",
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002011 MDNode *FPMathTag = nullptr) {
2012 CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2013 if (isa<FPMathOperator>(CI))
2014 CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
2015 return Insert(CI, Name);
2016 }
2017
Andrew Walbran16937d02019-10-22 13:54:20 +01002018 CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002019 ArrayRef<OperandBundleDef> OpBundles,
2020 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
Andrew Walbran16937d02019-10-22 13:54:20 +01002021 CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002022 if (isa<FPMathOperator>(CI))
2023 CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
2024 return Insert(CI, Name);
2025 }
2026
Andrew Walbran16937d02019-10-22 13:54:20 +01002027 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002028 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
Andrew Walbran16937d02019-10-22 13:54:20 +01002029 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2030 FPMathTag);
2031 }
2032
2033 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2034 ArrayRef<OperandBundleDef> OpBundles,
2035 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2036 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2037 OpBundles, Name, FPMathTag);
2038 }
2039
2040 // Deprecated [opaque pointer types]
2041 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
2042 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2043 return CreateCall(
2044 cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee,
2045 Args, Name, FPMathTag);
2046 }
2047
2048 // Deprecated [opaque pointer types]
2049 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
2050 ArrayRef<OperandBundleDef> OpBundles,
2051 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2052 return CreateCall(
2053 cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee,
2054 Args, OpBundles, Name, FPMathTag);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002055 }
2056
2057 Value *CreateSelect(Value *C, Value *True, Value *False,
2058 const Twine &Name = "", Instruction *MDFrom = nullptr) {
2059 if (auto *CC = dyn_cast<Constant>(C))
2060 if (auto *TC = dyn_cast<Constant>(True))
2061 if (auto *FC = dyn_cast<Constant>(False))
2062 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
2063
2064 SelectInst *Sel = SelectInst::Create(C, True, False);
2065 if (MDFrom) {
2066 MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
2067 MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
2068 Sel = addBranchMetadata(Sel, Prof, Unpred);
2069 }
2070 return Insert(Sel, Name);
2071 }
2072
2073 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2074 return Insert(new VAArgInst(List, Ty), Name);
2075 }
2076
2077 Value *CreateExtractElement(Value *Vec, Value *Idx,
2078 const Twine &Name = "") {
2079 if (auto *VC = dyn_cast<Constant>(Vec))
2080 if (auto *IC = dyn_cast<Constant>(Idx))
2081 return Insert(Folder.CreateExtractElement(VC, IC), Name);
2082 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2083 }
2084
2085 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2086 const Twine &Name = "") {
2087 return CreateExtractElement(Vec, getInt64(Idx), Name);
2088 }
2089
2090 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2091 const Twine &Name = "") {
2092 if (auto *VC = dyn_cast<Constant>(Vec))
2093 if (auto *NC = dyn_cast<Constant>(NewElt))
2094 if (auto *IC = dyn_cast<Constant>(Idx))
2095 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
2096 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2097 }
2098
2099 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2100 const Twine &Name = "") {
2101 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2102 }
2103
2104 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2105 const Twine &Name = "") {
2106 if (auto *V1C = dyn_cast<Constant>(V1))
2107 if (auto *V2C = dyn_cast<Constant>(V2))
2108 if (auto *MC = dyn_cast<Constant>(Mask))
2109 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
2110 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2111 }
2112
2113 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask,
2114 const Twine &Name = "") {
2115 Value *Mask = ConstantDataVector::get(Context, IntMask);
2116 return CreateShuffleVector(V1, V2, Mask, Name);
2117 }
2118
2119 Value *CreateExtractValue(Value *Agg,
2120 ArrayRef<unsigned> Idxs,
2121 const Twine &Name = "") {
2122 if (auto *AggC = dyn_cast<Constant>(Agg))
2123 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
2124 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2125 }
2126
2127 Value *CreateInsertValue(Value *Agg, Value *Val,
2128 ArrayRef<unsigned> Idxs,
2129 const Twine &Name = "") {
2130 if (auto *AggC = dyn_cast<Constant>(Agg))
2131 if (auto *ValC = dyn_cast<Constant>(Val))
2132 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
2133 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2134 }
2135
2136 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2137 const Twine &Name = "") {
2138 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2139 }
2140
2141 //===--------------------------------------------------------------------===//
2142 // Utility creation methods
2143 //===--------------------------------------------------------------------===//
2144
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002145 /// Return an i1 value testing if \p Arg is null.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002146 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2147 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
2148 Name);
2149 }
2150
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002151 /// Return an i1 value testing if \p Arg is not null.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002152 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2153 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
2154 Name);
2155 }
2156
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002157 /// Return the i64 difference between two pointer values, dividing out
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002158 /// the size of the pointed-to objects.
2159 ///
2160 /// This is intended to implement C-style pointer subtraction. As such, the
2161 /// pointers must be appropriately aligned for their element types and
2162 /// pointing into the same object.
2163 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
2164 assert(LHS->getType() == RHS->getType() &&
2165 "Pointer subtraction operand types must match!");
2166 auto *ArgType = cast<PointerType>(LHS->getType());
2167 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
2168 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
2169 Value *Difference = CreateSub(LHS_int, RHS_int);
2170 return CreateExactSDiv(Difference,
2171 ConstantExpr::getSizeOf(ArgType->getElementType()),
2172 Name);
2173 }
2174
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002175 /// Create a launder.invariant.group intrinsic call. If Ptr type is
2176 /// different from pointer to i8, it's casted to pointer to i8 in the same
2177 /// address space before call and casted back to Ptr type after call.
2178 Value *CreateLaunderInvariantGroup(Value *Ptr) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002179 assert(isa<PointerType>(Ptr->getType()) &&
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002180 "launder.invariant.group only applies to pointers.");
2181 // FIXME: we could potentially avoid casts to/from i8*.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002182 auto *PtrType = Ptr->getType();
2183 auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2184 if (PtrType != Int8PtrTy)
2185 Ptr = CreateBitCast(Ptr, Int8PtrTy);
2186 Module *M = BB->getParent()->getParent();
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002187 Function *FnLaunderInvariantGroup = Intrinsic::getDeclaration(
2188 M, Intrinsic::launder_invariant_group, {Int8PtrTy});
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002189
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002190 assert(FnLaunderInvariantGroup->getReturnType() == Int8PtrTy &&
2191 FnLaunderInvariantGroup->getFunctionType()->getParamType(0) ==
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002192 Int8PtrTy &&
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002193 "LaunderInvariantGroup should take and return the same type");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002194
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002195 CallInst *Fn = CreateCall(FnLaunderInvariantGroup, {Ptr});
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002196
2197 if (PtrType != Int8PtrTy)
2198 return CreateBitCast(Fn, PtrType);
2199 return Fn;
2200 }
2201
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002202 /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2203 /// different from pointer to i8, it's casted to pointer to i8 in the same
2204 /// address space before call and casted back to Ptr type after call.
2205 Value *CreateStripInvariantGroup(Value *Ptr) {
2206 assert(isa<PointerType>(Ptr->getType()) &&
2207 "strip.invariant.group only applies to pointers.");
2208
2209 // FIXME: we could potentially avoid casts to/from i8*.
2210 auto *PtrType = Ptr->getType();
2211 auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2212 if (PtrType != Int8PtrTy)
2213 Ptr = CreateBitCast(Ptr, Int8PtrTy);
2214 Module *M = BB->getParent()->getParent();
2215 Function *FnStripInvariantGroup = Intrinsic::getDeclaration(
2216 M, Intrinsic::strip_invariant_group, {Int8PtrTy});
2217
2218 assert(FnStripInvariantGroup->getReturnType() == Int8PtrTy &&
2219 FnStripInvariantGroup->getFunctionType()->getParamType(0) ==
2220 Int8PtrTy &&
2221 "StripInvariantGroup should take and return the same type");
2222
2223 CallInst *Fn = CreateCall(FnStripInvariantGroup, {Ptr});
2224
2225 if (PtrType != Int8PtrTy)
2226 return CreateBitCast(Fn, PtrType);
2227 return Fn;
2228 }
2229
2230 /// Return a vector value that contains \arg V broadcasted to \p
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002231 /// NumElts elements.
2232 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
2233 assert(NumElts > 0 && "Cannot splat to an empty vector!");
2234
2235 // First insert it into an undef vector so we can shuffle it.
2236 Type *I32Ty = getInt32Ty();
2237 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
2238 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
2239 Name + ".splatinsert");
2240
2241 // Shuffle the value across the desired number of elements.
2242 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
2243 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
2244 }
2245
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002246 /// Return a value that has been extracted from a larger integer type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002247 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
2248 IntegerType *ExtractedTy, uint64_t Offset,
2249 const Twine &Name) {
2250 auto *IntTy = cast<IntegerType>(From->getType());
2251 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
2252 DL.getTypeStoreSize(IntTy) &&
2253 "Element extends past full value");
2254 uint64_t ShAmt = 8 * Offset;
2255 Value *V = From;
2256 if (DL.isBigEndian())
2257 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
2258 DL.getTypeStoreSize(ExtractedTy) - Offset);
2259 if (ShAmt) {
2260 V = CreateLShr(V, ShAmt, Name + ".shift");
2261 }
2262 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
2263 "Cannot extract to a larger integer!");
2264 if (ExtractedTy != IntTy) {
2265 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
2266 }
2267 return V;
2268 }
2269
2270private:
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002271 /// Helper function that creates an assume intrinsic call that
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002272 /// represents an alignment assumption on the provided Ptr, Mask, Type
Andrew Walbran16937d02019-10-22 13:54:20 +01002273 /// and Offset. It may be sometimes useful to do some other logic
2274 /// based on this alignment check, thus it can be stored into 'TheCheck'.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002275 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2276 Value *PtrValue, Value *Mask,
Andrew Walbran16937d02019-10-22 13:54:20 +01002277 Type *IntPtrTy, Value *OffsetValue,
2278 Value **TheCheck) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002279 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
2280
2281 if (OffsetValue) {
2282 bool IsOffsetZero = false;
2283 if (const auto *CI = dyn_cast<ConstantInt>(OffsetValue))
2284 IsOffsetZero = CI->isZero();
2285
2286 if (!IsOffsetZero) {
2287 if (OffsetValue->getType() != IntPtrTy)
2288 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
2289 "offsetcast");
2290 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
2291 }
2292 }
2293
2294 Value *Zero = ConstantInt::get(IntPtrTy, 0);
2295 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
2296 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
Andrew Walbran16937d02019-10-22 13:54:20 +01002297 if (TheCheck)
2298 *TheCheck = InvCond;
2299
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002300 return CreateAssumption(InvCond);
2301 }
2302
2303public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002304 /// Create an assume intrinsic call that represents an alignment
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002305 /// assumption on the provided pointer.
2306 ///
2307 /// An optional offset can be provided, and if it is provided, the offset
2308 /// must be subtracted from the provided pointer to get the pointer with the
2309 /// specified alignment.
Andrew Walbran16937d02019-10-22 13:54:20 +01002310 ///
2311 /// It may be sometimes useful to do some other logic
2312 /// based on this alignment check, thus it can be stored into 'TheCheck'.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002313 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2314 unsigned Alignment,
Andrew Walbran16937d02019-10-22 13:54:20 +01002315 Value *OffsetValue = nullptr,
2316 Value **TheCheck = nullptr) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002317 assert(isa<PointerType>(PtrValue->getType()) &&
2318 "trying to create an alignment assumption on a non-pointer?");
Andrew Walbran16937d02019-10-22 13:54:20 +01002319 assert(Alignment != 0 && "Invalid Alignment");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002320 auto *PtrTy = cast<PointerType>(PtrValue->getType());
2321 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2322
Andrew Walbran16937d02019-10-22 13:54:20 +01002323 Value *Mask = ConstantInt::get(IntPtrTy, Alignment - 1);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002324 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
Andrew Walbran16937d02019-10-22 13:54:20 +01002325 OffsetValue, TheCheck);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002326 }
2327
Andrew Scullcdfcccc2018-10-05 20:58:37 +01002328 /// Create an assume intrinsic call that represents an alignment
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002329 /// assumption on the provided pointer.
2330 ///
2331 /// An optional offset can be provided, and if it is provided, the offset
2332 /// must be subtracted from the provided pointer to get the pointer with the
2333 /// specified alignment.
2334 ///
Andrew Walbran16937d02019-10-22 13:54:20 +01002335 /// It may be sometimes useful to do some other logic
2336 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2337 ///
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002338 /// This overload handles the condition where the Alignment is dependent
2339 /// on an existing value rather than a static value.
2340 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2341 Value *Alignment,
Andrew Walbran16937d02019-10-22 13:54:20 +01002342 Value *OffsetValue = nullptr,
2343 Value **TheCheck = nullptr) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002344 assert(isa<PointerType>(PtrValue->getType()) &&
2345 "trying to create an alignment assumption on a non-pointer?");
2346 auto *PtrTy = cast<PointerType>(PtrValue->getType());
2347 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2348
2349 if (Alignment->getType() != IntPtrTy)
Andrew Walbran16937d02019-10-22 13:54:20 +01002350 Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ false,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002351 "alignmentcast");
Andrew Walbran16937d02019-10-22 13:54:20 +01002352
2353 Value *Mask = CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "mask");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002354
2355 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
Andrew Walbran16937d02019-10-22 13:54:20 +01002356 OffsetValue, TheCheck);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002357 }
2358};
2359
2360// Create wrappers for C Binding types (see CBindingWrapping.h).
2361DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
2362
2363} // end namespace llvm
2364
2365#endif // LLVM_IR_IRBUILDER_H