]> git.proxmox.com Git - rustc.git/blame - src/llvm/include/llvm/IR/IRBuilder.h
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / include / llvm / IR / IRBuilder.h
CommitLineData
223e47cc
LB
1//===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the IRBuilder class, which is used as a convenient way
11// to create LLVM instructions with a consistent and simplified interface.
12//
13//===----------------------------------------------------------------------===//
14
970d7e83
LB
15#ifndef LLVM_IR_IRBUILDER_H
16#define LLVM_IR_IRBUILDER_H
223e47cc 17
223e47cc
LB
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Twine.h"
970d7e83 21#include "llvm/IR/BasicBlock.h"
1a4d82fc 22#include "llvm/IR/ConstantFolder.h"
970d7e83
LB
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Operator.h"
1a4d82fc
JJ
27#include "llvm/IR/ValueHandle.h"
28#include "llvm/Support/CBindingWrapping.h"
223e47cc
LB
29
30namespace llvm {
85aaf69f 31class MDNode;
223e47cc 32
970d7e83
LB
33/// \brief This provides the default implementation of the IRBuilder
34/// 'InsertHelper' method that is called whenever an instruction is created by
35/// IRBuilder and needs to be inserted.
36///
37/// By default, this inserts the instruction at the insertion point.
223e47cc
LB
38template <bool preserveNames = true>
39class IRBuilderDefaultInserter {
40protected:
41 void InsertHelper(Instruction *I, const Twine &Name,
42 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
43 if (BB) BB->getInstList().insert(InsertPt, I);
44 if (preserveNames)
45 I->setName(Name);
46 }
47};
48
970d7e83 49/// \brief Common base class shared among various IRBuilders.
223e47cc
LB
50class IRBuilderBase {
51 DebugLoc CurDbgLocation;
52protected:
53 BasicBlock *BB;
54 BasicBlock::iterator InsertPt;
55 LLVMContext &Context;
1a4d82fc
JJ
56
57 MDNode *DefaultFPMathTag;
58 FastMathFlags FMF;
223e47cc
LB
59public:
60
1a4d82fc
JJ
61 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr)
62 : Context(context), DefaultFPMathTag(FPMathTag), FMF() {
223e47cc
LB
63 ClearInsertionPoint();
64 }
65
66 //===--------------------------------------------------------------------===//
67 // Builder configuration methods
68 //===--------------------------------------------------------------------===//
69
970d7e83
LB
70 /// \brief Clear the insertion point: created instructions will not be
71 /// inserted into a block.
223e47cc 72 void ClearInsertionPoint() {
1a4d82fc
JJ
73 BB = nullptr;
74 InsertPt = nullptr;
223e47cc
LB
75 }
76
77 BasicBlock *GetInsertBlock() const { return BB; }
78 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
79 LLVMContext &getContext() const { return Context; }
80
970d7e83
LB
81 /// \brief This specifies that created instructions should be appended to the
82 /// end of the specified block.
223e47cc
LB
83 void SetInsertPoint(BasicBlock *TheBB) {
84 BB = TheBB;
85 InsertPt = BB->end();
86 }
87
970d7e83
LB
88 /// \brief This specifies that created instructions should be inserted before
89 /// the specified instruction.
223e47cc
LB
90 void SetInsertPoint(Instruction *I) {
91 BB = I->getParent();
92 InsertPt = I;
1a4d82fc 93 assert(I != BB->end() && "Can't read debug loc from end()");
223e47cc
LB
94 SetCurrentDebugLocation(I->getDebugLoc());
95 }
96
970d7e83
LB
97 /// \brief This specifies that created instructions should be inserted at the
98 /// specified point.
223e47cc
LB
99 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
100 BB = TheBB;
101 InsertPt = IP;
102 }
103
970d7e83
LB
104 /// \brief Find the nearest point that dominates this use, and specify that
105 /// created instructions should be inserted at this point.
223e47cc
LB
106 void SetInsertPoint(Use &U) {
107 Instruction *UseInst = cast<Instruction>(U.getUser());
108 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
109 BasicBlock *PredBB = Phi->getIncomingBlock(U);
110 assert(U != PredBB->getTerminator() && "critical edge not split");
111 SetInsertPoint(PredBB, PredBB->getTerminator());
112 return;
113 }
114 SetInsertPoint(UseInst);
115 }
116
970d7e83 117 /// \brief Set location information used by debugging information.
223e47cc
LB
118 void SetCurrentDebugLocation(const DebugLoc &L) {
119 CurDbgLocation = L;
120 }
121
970d7e83 122 /// \brief Get location information used by debugging information.
223e47cc
LB
123 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
124
970d7e83
LB
125 /// \brief If this builder has a current debug location, set it on the
126 /// specified instruction.
223e47cc
LB
127 void SetInstDebugLocation(Instruction *I) const {
128 if (!CurDbgLocation.isUnknown())
129 I->setDebugLoc(CurDbgLocation);
130 }
131
970d7e83
LB
132 /// \brief Get the return type of the current function that we're emitting
133 /// into.
223e47cc
LB
134 Type *getCurrentFunctionReturnType() const;
135
136 /// InsertPoint - A saved insertion point.
137 class InsertPoint {
138 BasicBlock *Block;
139 BasicBlock::iterator Point;
140
141 public:
970d7e83 142 /// \brief Creates a new insertion point which doesn't point to anything.
1a4d82fc 143 InsertPoint() : Block(nullptr) {}
223e47cc 144
970d7e83 145 /// \brief Creates a new insertion point at the given location.
223e47cc
LB
146 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
147 : Block(InsertBlock), Point(InsertPoint) {}
148
970d7e83 149 /// \brief Returns true if this insert point is set.
1a4d82fc 150 bool isSet() const { return (Block != nullptr); }
223e47cc
LB
151
152 llvm::BasicBlock *getBlock() const { return Block; }
153 llvm::BasicBlock::iterator getPoint() const { return Point; }
154 };
155
970d7e83 156 /// \brief Returns the current insert point.
223e47cc
LB
157 InsertPoint saveIP() const {
158 return InsertPoint(GetInsertBlock(), GetInsertPoint());
159 }
160
970d7e83 161 /// \brief Returns the current insert point, clearing it in the process.
223e47cc
LB
162 InsertPoint saveAndClearIP() {
163 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
164 ClearInsertionPoint();
165 return IP;
166 }
167
970d7e83 168 /// \brief Sets the current insert point to a previously-saved location.
223e47cc
LB
169 void restoreIP(InsertPoint IP) {
170 if (IP.isSet())
171 SetInsertPoint(IP.getBlock(), IP.getPoint());
172 else
173 ClearInsertionPoint();
174 }
175
1a4d82fc
JJ
176 /// \brief Get the floating point math metadata being used.
177 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
178
179 /// \brief Get the flags to be applied to created floating point ops
180 FastMathFlags getFastMathFlags() const { return FMF; }
181
182 /// \brief Clear the fast-math flags.
183 void clearFastMathFlags() { FMF.clear(); }
184
185 /// \brief Set the floating point math metadata to be used.
186 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
187
188 /// \brief Set the fast-math flags to be used with generated fp-math operators
189 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
190
191 //===--------------------------------------------------------------------===//
192 // RAII helpers.
193 //===--------------------------------------------------------------------===//
194
195 // \brief RAII object that stores the current insertion point and restores it
196 // when the object is destroyed. This includes the debug location.
197 class InsertPointGuard {
198 IRBuilderBase &Builder;
199 AssertingVH<BasicBlock> Block;
200 BasicBlock::iterator Point;
201 DebugLoc DbgLoc;
202
203 InsertPointGuard(const InsertPointGuard &) LLVM_DELETED_FUNCTION;
204 InsertPointGuard &operator=(const InsertPointGuard &) LLVM_DELETED_FUNCTION;
205
206 public:
207 InsertPointGuard(IRBuilderBase &B)
208 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
209 DbgLoc(B.getCurrentDebugLocation()) {}
210
211 ~InsertPointGuard() {
212 Builder.restoreIP(InsertPoint(Block, Point));
213 Builder.SetCurrentDebugLocation(DbgLoc);
214 }
215 };
216
217 // \brief RAII object that stores the current fast math settings and restores
218 // them when the object is destroyed.
219 class FastMathFlagGuard {
220 IRBuilderBase &Builder;
221 FastMathFlags FMF;
222 MDNode *FPMathTag;
223
224 FastMathFlagGuard(const FastMathFlagGuard &) LLVM_DELETED_FUNCTION;
225 FastMathFlagGuard &operator=(
226 const FastMathFlagGuard &) LLVM_DELETED_FUNCTION;
227
228 public:
229 FastMathFlagGuard(IRBuilderBase &B)
230 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
231
232 ~FastMathFlagGuard() {
233 Builder.FMF = FMF;
234 Builder.DefaultFPMathTag = FPMathTag;
235 }
236 };
237
223e47cc
LB
238 //===--------------------------------------------------------------------===//
239 // Miscellaneous creation methods.
240 //===--------------------------------------------------------------------===//
241
970d7e83
LB
242 /// \brief Make a new global variable with initializer type i8*
243 ///
244 /// Make a new global variable with an initializer that has array of i8 type
245 /// filled in with the null terminated string value specified. The new global
246 /// variable will be marked mergable with any others of the same contents. If
247 /// Name is specified, it is the name of the global variable created.
223e47cc
LB
248 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
249
970d7e83 250 /// \brief Get a constant value representing either true or false.
223e47cc
LB
251 ConstantInt *getInt1(bool V) {
252 return ConstantInt::get(getInt1Ty(), V);
253 }
254
970d7e83 255 /// \brief Get the constant value for i1 true.
223e47cc
LB
256 ConstantInt *getTrue() {
257 return ConstantInt::getTrue(Context);
258 }
259
970d7e83 260 /// \brief Get the constant value for i1 false.
223e47cc
LB
261 ConstantInt *getFalse() {
262 return ConstantInt::getFalse(Context);
263 }
264
970d7e83 265 /// \brief Get a constant 8-bit value.
223e47cc
LB
266 ConstantInt *getInt8(uint8_t C) {
267 return ConstantInt::get(getInt8Ty(), C);
268 }
269
970d7e83 270 /// \brief Get a constant 16-bit value.
223e47cc
LB
271 ConstantInt *getInt16(uint16_t C) {
272 return ConstantInt::get(getInt16Ty(), C);
273 }
274
970d7e83 275 /// \brief Get a constant 32-bit value.
223e47cc
LB
276 ConstantInt *getInt32(uint32_t C) {
277 return ConstantInt::get(getInt32Ty(), C);
278 }
279
970d7e83 280 /// \brief Get a constant 64-bit value.
223e47cc
LB
281 ConstantInt *getInt64(uint64_t C) {
282 return ConstantInt::get(getInt64Ty(), C);
283 }
284
1a4d82fc
JJ
285 /// \brief Get a constant N-bit value, zero extended or truncated from
286 /// a 64-bit value.
287 ConstantInt *getIntN(unsigned N, uint64_t C) {
288 return ConstantInt::get(getIntNTy(N), C);
289 }
290
970d7e83 291 /// \brief Get a constant integer value.
223e47cc
LB
292 ConstantInt *getInt(const APInt &AI) {
293 return ConstantInt::get(Context, AI);
294 }
295
296 //===--------------------------------------------------------------------===//
297 // Type creation methods
298 //===--------------------------------------------------------------------===//
299
970d7e83 300 /// \brief Fetch the type representing a single bit
223e47cc
LB
301 IntegerType *getInt1Ty() {
302 return Type::getInt1Ty(Context);
303 }
304
970d7e83 305 /// \brief Fetch the type representing an 8-bit integer.
223e47cc
LB
306 IntegerType *getInt8Ty() {
307 return Type::getInt8Ty(Context);
308 }
309
970d7e83 310 /// \brief Fetch the type representing a 16-bit integer.
223e47cc
LB
311 IntegerType *getInt16Ty() {
312 return Type::getInt16Ty(Context);
313 }
314
970d7e83 315 /// \brief Fetch the type representing a 32-bit integer.
223e47cc
LB
316 IntegerType *getInt32Ty() {
317 return Type::getInt32Ty(Context);
318 }
319
970d7e83 320 /// \brief Fetch the type representing a 64-bit integer.
223e47cc
LB
321 IntegerType *getInt64Ty() {
322 return Type::getInt64Ty(Context);
323 }
324
1a4d82fc
JJ
325 /// \brief Fetch the type representing an N-bit integer.
326 IntegerType *getIntNTy(unsigned N) {
327 return Type::getIntNTy(Context, N);
328 }
329
330 /// \brief Fetch the type representing a 16-bit floating point value.
331 Type *getHalfTy() {
332 return Type::getHalfTy(Context);
333 }
334
970d7e83 335 /// \brief Fetch the type representing a 32-bit floating point value.
223e47cc
LB
336 Type *getFloatTy() {
337 return Type::getFloatTy(Context);
338 }
339
970d7e83 340 /// \brief Fetch the type representing a 64-bit floating point value.
223e47cc
LB
341 Type *getDoubleTy() {
342 return Type::getDoubleTy(Context);
343 }
344
970d7e83 345 /// \brief Fetch the type representing void.
223e47cc
LB
346 Type *getVoidTy() {
347 return Type::getVoidTy(Context);
348 }
349
970d7e83 350 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
223e47cc
LB
351 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
352 return Type::getInt8PtrTy(Context, AddrSpace);
353 }
354
970d7e83 355 /// \brief Fetch the type representing a pointer to an integer value.
1a4d82fc 356 IntegerType* getIntPtrTy(const DataLayout *DL, unsigned AddrSpace = 0) {
970d7e83
LB
357 return DL->getIntPtrType(Context, AddrSpace);
358 }
359
223e47cc
LB
360 //===--------------------------------------------------------------------===//
361 // Intrinsic creation methods
362 //===--------------------------------------------------------------------===//
363
970d7e83
LB
364 /// \brief Create and insert a memset to the specified pointer and the
365 /// specified value.
366 ///
1a4d82fc
JJ
367 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
368 /// specified, it will be added to the instruction. Likewise with alias.scope
369 /// and noalias tags.
223e47cc 370 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
1a4d82fc
JJ
371 bool isVolatile = false, MDNode *TBAATag = nullptr,
372 MDNode *ScopeTag = nullptr,
373 MDNode *NoAliasTag = nullptr) {
374 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
375 TBAATag, ScopeTag, NoAliasTag);
223e47cc
LB
376 }
377
378 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
1a4d82fc
JJ
379 bool isVolatile = false, MDNode *TBAATag = nullptr,
380 MDNode *ScopeTag = nullptr,
381 MDNode *NoAliasTag = nullptr);
223e47cc 382
970d7e83
LB
383 /// \brief Create and insert a memcpy between the specified pointers.
384 ///
223e47cc 385 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
1a4d82fc
JJ
386 /// specified, it will be added to the instruction. Likewise with alias.scope
387 /// and noalias tags.
223e47cc 388 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
1a4d82fc
JJ
389 bool isVolatile = false, MDNode *TBAATag = nullptr,
390 MDNode *TBAAStructTag = nullptr,
391 MDNode *ScopeTag = nullptr,
392 MDNode *NoAliasTag = nullptr) {
223e47cc 393 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
1a4d82fc 394 TBAAStructTag, ScopeTag, NoAliasTag);
223e47cc
LB
395 }
396
397 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
1a4d82fc
JJ
398 bool isVolatile = false, MDNode *TBAATag = nullptr,
399 MDNode *TBAAStructTag = nullptr,
400 MDNode *ScopeTag = nullptr,
401 MDNode *NoAliasTag = nullptr);
223e47cc 402
970d7e83
LB
403 /// \brief Create and insert a memmove between the specified
404 /// pointers.
405 ///
406 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
1a4d82fc
JJ
407 /// specified, it will be added to the instruction. Likewise with alias.scope
408 /// and noalias tags.
223e47cc 409 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
1a4d82fc
JJ
410 bool isVolatile = false, MDNode *TBAATag = nullptr,
411 MDNode *ScopeTag = nullptr,
412 MDNode *NoAliasTag = nullptr) {
413 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile,
414 TBAATag, ScopeTag, NoAliasTag);
223e47cc
LB
415 }
416
417 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
1a4d82fc
JJ
418 bool isVolatile = false, MDNode *TBAATag = nullptr,
419 MDNode *ScopeTag = nullptr,
420 MDNode *NoAliasTag = nullptr);
223e47cc 421
970d7e83
LB
422 /// \brief Create a lifetime.start intrinsic.
423 ///
424 /// If the pointer isn't i8* it will be converted.
1a4d82fc 425 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
223e47cc 426
970d7e83
LB
427 /// \brief Create a lifetime.end intrinsic.
428 ///
429 /// If the pointer isn't i8* it will be converted.
1a4d82fc 430 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
223e47cc 431
85aaf69f
SL
432 /// \brief Create a call to Masked Load intrinsic
433 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
434 Value *PassThru = 0, const Twine &Name = "");
435
436 /// \brief Create a call to Masked Store intrinsic
437 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
438 Value *Mask);
439
440 /// \brief Create an assume intrinsic call that allows the optimizer to
441 /// assume that the provided condition will be true.
442 CallInst *CreateAssumption(Value *Cond);
443
444 /// \brief Create a call to the experimental.gc.statepoint intrinsic to
445 /// start a new statepoint sequence.
446 CallInst *CreateGCStatepoint(Value *ActualCallee,
447 ArrayRef<Value*> CallArgs,
448 ArrayRef<Value*> DeoptArgs,
449 ArrayRef<Value*> GCArgs,
450 const Twine &Name = "");
451
452 /// \brief Create a call to the experimental.gc.result intrinsic to extract
453 /// the result from a call wrapped in a statepoint.
454 CallInst *CreateGCResult(Instruction *Statepoint,
455 Type *ResultType,
456 const Twine &Name = "");
457
458 /// \brief Create a call to the experimental.gc.relocate intrinsics to
459 /// project the relocated value of one pointer from the statepoint.
460 CallInst *CreateGCRelocate(Instruction *Statepoint,
461 int BaseOffset,
462 int DerivedOffset,
463 Type *ResultType,
464 const Twine &Name = "");
465
223e47cc 466private:
85aaf69f
SL
467 /// \brief Create a call to a masked intrinsic with given Id.
468 /// Masked intrinsic has only one overloaded type - data type.
469 CallInst *CreateMaskedIntrinsic(unsigned Id, ArrayRef<Value *> Ops,
470 Type *DataTy, const Twine &Name = "");
471
223e47cc
LB
472 Value *getCastedInt8PtrValue(Value *Ptr);
473};
474
970d7e83
LB
475/// \brief This provides a uniform API for creating instructions and inserting
476/// them into a basic block: either at the end of a BasicBlock, or at a specific
477/// iterator location in a block.
223e47cc
LB
478///
479/// Note that the builder does not expose the full generality of LLVM
480/// instructions. For access to extra instruction properties, use the mutators
970d7e83
LB
481/// (e.g. setVolatile) on the instructions after they have been
482/// created. Convenience state exists to specify fast-math flags and fp-math
483/// tags.
484///
223e47cc
LB
485/// The first template argument handles whether or not to preserve names in the
486/// final instruction output. This defaults to on. The second template argument
487/// specifies a class to use for creating constants. This defaults to creating
1a4d82fc 488/// minimally folded constants. The third template argument allows clients to
223e47cc
LB
489/// specify custom insertion hooks that are called on every newly created
490/// insertion.
491template<bool preserveNames = true, typename T = ConstantFolder,
492 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
493class IRBuilder : public IRBuilderBase, public Inserter {
494 T Folder;
223e47cc
LB
495public:
496 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
1a4d82fc
JJ
497 MDNode *FPMathTag = nullptr)
498 : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
223e47cc
LB
499 }
500
1a4d82fc
JJ
501 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
502 : IRBuilderBase(C, FPMathTag), Folder() {
223e47cc
LB
503 }
504
1a4d82fc
JJ
505 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
506 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
223e47cc
LB
507 SetInsertPoint(TheBB);
508 }
509
1a4d82fc
JJ
510 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
511 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
223e47cc
LB
512 SetInsertPoint(TheBB);
513 }
514
1a4d82fc
JJ
515 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
516 : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
223e47cc
LB
517 SetInsertPoint(IP);
518 SetCurrentDebugLocation(IP->getDebugLoc());
519 }
520
1a4d82fc
JJ
521 explicit IRBuilder(Use &U, MDNode *FPMathTag = nullptr)
522 : IRBuilderBase(U->getContext(), FPMathTag), Folder() {
223e47cc
LB
523 SetInsertPoint(U);
524 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
525 }
526
527 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
1a4d82fc
JJ
528 MDNode *FPMathTag = nullptr)
529 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
223e47cc
LB
530 SetInsertPoint(TheBB, IP);
531 }
532
1a4d82fc
JJ
533 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
534 MDNode *FPMathTag = nullptr)
535 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
223e47cc
LB
536 SetInsertPoint(TheBB, IP);
537 }
538
970d7e83 539 /// \brief Get the constant folder being used.
223e47cc
LB
540 const T &getFolder() { return Folder; }
541
970d7e83
LB
542 /// \brief Return true if this builder is configured to actually add the
543 /// requested names to IR created through it.
223e47cc
LB
544 bool isNamePreserving() const { return preserveNames; }
545
970d7e83 546 /// \brief Insert and return the specified instruction.
223e47cc
LB
547 template<typename InstTy>
548 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
549 this->InsertHelper(I, Name, BB, InsertPt);
970d7e83 550 this->SetInstDebugLocation(I);
223e47cc
LB
551 return I;
552 }
553
970d7e83 554 /// \brief No-op overload to handle constants.
223e47cc
LB
555 Constant *Insert(Constant *C, const Twine& = "") const {
556 return C;
557 }
558
559 //===--------------------------------------------------------------------===//
560 // Instruction creation methods: Terminators
561 //===--------------------------------------------------------------------===//
562
563private:
564 /// \brief Helper to add branch weight metadata onto an instruction.
565 /// \returns The annotated instruction.
566 template <typename InstTy>
567 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
568 if (Weights)
569 I->setMetadata(LLVMContext::MD_prof, Weights);
570 return I;
571 }
572
573public:
970d7e83 574 /// \brief Create a 'ret void' instruction.
223e47cc
LB
575 ReturnInst *CreateRetVoid() {
576 return Insert(ReturnInst::Create(Context));
577 }
578
970d7e83 579 /// \brief Create a 'ret <val>' instruction.
223e47cc
LB
580 ReturnInst *CreateRet(Value *V) {
581 return Insert(ReturnInst::Create(Context, V));
582 }
583
970d7e83 584 /// \brief Create a sequence of N insertvalue instructions,
223e47cc
LB
585 /// with one Value from the retVals array each, that build a aggregate
586 /// return value one value at a time, and a ret instruction to return
970d7e83 587 /// the resulting aggregate value.
223e47cc 588 ///
970d7e83
LB
589 /// This is a convenience function for code that uses aggregate return values
590 /// as a vehicle for having multiple return values.
223e47cc
LB
591 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
592 Value *V = UndefValue::get(getCurrentFunctionReturnType());
593 for (unsigned i = 0; i != N; ++i)
594 V = CreateInsertValue(V, retVals[i], i, "mrv");
595 return Insert(ReturnInst::Create(Context, V));
596 }
597
970d7e83 598 /// \brief Create an unconditional 'br label X' instruction.
223e47cc
LB
599 BranchInst *CreateBr(BasicBlock *Dest) {
600 return Insert(BranchInst::Create(Dest));
601 }
602
970d7e83 603 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
223e47cc
LB
604 /// instruction.
605 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1a4d82fc 606 MDNode *BranchWeights = nullptr) {
223e47cc
LB
607 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
608 BranchWeights));
609 }
610
970d7e83
LB
611 /// \brief Create a switch instruction with the specified value, default dest,
612 /// and with a hint for the number of cases that will be added (for efficient
613 /// allocation).
223e47cc 614 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1a4d82fc 615 MDNode *BranchWeights = nullptr) {
223e47cc
LB
616 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
617 BranchWeights));
618 }
619
970d7e83
LB
620 /// \brief Create an indirect branch instruction with the specified address
621 /// operand, with an optional hint for the number of destinations that will be
622 /// added (for efficient allocation).
223e47cc
LB
623 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
624 return Insert(IndirectBrInst::Create(Addr, NumDests));
625 }
626
627 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
628 BasicBlock *UnwindDest, const Twine &Name = "") {
1a4d82fc 629 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, None),
223e47cc
LB
630 Name);
631 }
632 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
633 BasicBlock *UnwindDest, Value *Arg1,
634 const Twine &Name = "") {
635 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
636 Name);
637 }
638 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
639 BasicBlock *UnwindDest, Value *Arg1,
640 Value *Arg2, Value *Arg3,
641 const Twine &Name = "") {
642 Value *Args[] = { Arg1, Arg2, Arg3 };
643 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
644 Name);
645 }
970d7e83 646 /// \brief Create an invoke instruction.
223e47cc
LB
647 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
648 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
649 const Twine &Name = "") {
650 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
651 Name);
652 }
653
654 ResumeInst *CreateResume(Value *Exn) {
655 return Insert(ResumeInst::Create(Exn));
656 }
657
658 UnreachableInst *CreateUnreachable() {
659 return Insert(new UnreachableInst(Context));
660 }
661
662 //===--------------------------------------------------------------------===//
663 // Instruction creation methods: Binary Operators
664 //===--------------------------------------------------------------------===//
665private:
666 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
667 Value *LHS, Value *RHS,
668 const Twine &Name,
669 bool HasNUW, bool HasNSW) {
670 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
671 if (HasNUW) BO->setHasNoUnsignedWrap();
672 if (HasNSW) BO->setHasNoSignedWrap();
673 return BO;
674 }
675
970d7e83
LB
676 Instruction *AddFPMathAttributes(Instruction *I,
677 MDNode *FPMathTag,
678 FastMathFlags FMF) const {
223e47cc
LB
679 if (!FPMathTag)
680 FPMathTag = DefaultFPMathTag;
681 if (FPMathTag)
682 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
970d7e83 683 I->setFastMathFlags(FMF);
223e47cc
LB
684 return I;
685 }
686public:
687 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
688 bool HasNUW = false, bool HasNSW = false) {
689 if (Constant *LC = dyn_cast<Constant>(LHS))
690 if (Constant *RC = dyn_cast<Constant>(RHS))
691 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
692 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
693 HasNUW, HasNSW);
694 }
695 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
696 return CreateAdd(LHS, RHS, Name, false, true);
697 }
698 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
699 return CreateAdd(LHS, RHS, Name, true, false);
700 }
701 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1a4d82fc 702 MDNode *FPMathTag = nullptr) {
223e47cc
LB
703 if (Constant *LC = dyn_cast<Constant>(LHS))
704 if (Constant *RC = dyn_cast<Constant>(RHS))
705 return Insert(Folder.CreateFAdd(LC, RC), Name);
970d7e83
LB
706 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
707 FPMathTag, FMF), Name);
223e47cc
LB
708 }
709 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
710 bool HasNUW = false, bool HasNSW = false) {
711 if (Constant *LC = dyn_cast<Constant>(LHS))
712 if (Constant *RC = dyn_cast<Constant>(RHS))
1a4d82fc 713 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
223e47cc
LB
714 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
715 HasNUW, HasNSW);
716 }
717 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
718 return CreateSub(LHS, RHS, Name, false, true);
719 }
720 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
721 return CreateSub(LHS, RHS, Name, true, false);
722 }
723 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
1a4d82fc 724 MDNode *FPMathTag = nullptr) {
223e47cc
LB
725 if (Constant *LC = dyn_cast<Constant>(LHS))
726 if (Constant *RC = dyn_cast<Constant>(RHS))
727 return Insert(Folder.CreateFSub(LC, RC), Name);
970d7e83
LB
728 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
729 FPMathTag, FMF), Name);
223e47cc
LB
730 }
731 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
732 bool HasNUW = false, bool HasNSW = false) {
733 if (Constant *LC = dyn_cast<Constant>(LHS))
734 if (Constant *RC = dyn_cast<Constant>(RHS))
1a4d82fc 735 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
223e47cc
LB
736 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
737 HasNUW, HasNSW);
738 }
739 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
740 return CreateMul(LHS, RHS, Name, false, true);
741 }
742 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
743 return CreateMul(LHS, RHS, Name, true, false);
744 }
745 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
1a4d82fc 746 MDNode *FPMathTag = nullptr) {
223e47cc
LB
747 if (Constant *LC = dyn_cast<Constant>(LHS))
748 if (Constant *RC = dyn_cast<Constant>(RHS))
749 return Insert(Folder.CreateFMul(LC, RC), Name);
970d7e83
LB
750 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
751 FPMathTag, FMF), Name);
223e47cc
LB
752 }
753 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
754 bool isExact = false) {
755 if (Constant *LC = dyn_cast<Constant>(LHS))
756 if (Constant *RC = dyn_cast<Constant>(RHS))
757 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
758 if (!isExact)
759 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
760 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
761 }
762 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
763 return CreateUDiv(LHS, RHS, Name, true);
764 }
765 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
766 bool isExact = false) {
767 if (Constant *LC = dyn_cast<Constant>(LHS))
768 if (Constant *RC = dyn_cast<Constant>(RHS))
769 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
770 if (!isExact)
771 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
772 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
773 }
774 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
775 return CreateSDiv(LHS, RHS, Name, true);
776 }
777 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1a4d82fc 778 MDNode *FPMathTag = nullptr) {
223e47cc
LB
779 if (Constant *LC = dyn_cast<Constant>(LHS))
780 if (Constant *RC = dyn_cast<Constant>(RHS))
781 return Insert(Folder.CreateFDiv(LC, RC), Name);
970d7e83
LB
782 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
783 FPMathTag, FMF), Name);
223e47cc
LB
784 }
785 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
786 if (Constant *LC = dyn_cast<Constant>(LHS))
787 if (Constant *RC = dyn_cast<Constant>(RHS))
788 return Insert(Folder.CreateURem(LC, RC), Name);
789 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
790 }
791 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
792 if (Constant *LC = dyn_cast<Constant>(LHS))
793 if (Constant *RC = dyn_cast<Constant>(RHS))
794 return Insert(Folder.CreateSRem(LC, RC), Name);
795 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
796 }
797 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
1a4d82fc 798 MDNode *FPMathTag = nullptr) {
223e47cc
LB
799 if (Constant *LC = dyn_cast<Constant>(LHS))
800 if (Constant *RC = dyn_cast<Constant>(RHS))
801 return Insert(Folder.CreateFRem(LC, RC), Name);
970d7e83
LB
802 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
803 FPMathTag, FMF), Name);
223e47cc
LB
804 }
805
806 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
807 bool HasNUW = false, bool HasNSW = false) {
808 if (Constant *LC = dyn_cast<Constant>(LHS))
809 if (Constant *RC = dyn_cast<Constant>(RHS))
810 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
811 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
812 HasNUW, HasNSW);
813 }
814 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
815 bool HasNUW = false, bool HasNSW = false) {
816 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
817 HasNUW, HasNSW);
818 }
819 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
820 bool HasNUW = false, bool HasNSW = false) {
821 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
822 HasNUW, HasNSW);
823 }
824
825 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
826 bool isExact = false) {
827 if (Constant *LC = dyn_cast<Constant>(LHS))
828 if (Constant *RC = dyn_cast<Constant>(RHS))
829 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
830 if (!isExact)
831 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
832 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
833 }
834 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
835 bool isExact = false) {
836 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
837 }
838 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
839 bool isExact = false) {
840 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
841 }
842
843 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
844 bool isExact = false) {
845 if (Constant *LC = dyn_cast<Constant>(LHS))
846 if (Constant *RC = dyn_cast<Constant>(RHS))
847 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
848 if (!isExact)
849 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
850 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
851 }
852 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
853 bool isExact = false) {
854 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
855 }
856 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
857 bool isExact = false) {
858 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
859 }
860
861 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
862 if (Constant *RC = dyn_cast<Constant>(RHS)) {
863 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
864 return LHS; // LHS & -1 -> LHS
865 if (Constant *LC = dyn_cast<Constant>(LHS))
866 return Insert(Folder.CreateAnd(LC, RC), Name);
867 }
868 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
869 }
870 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
871 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
872 }
873 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
874 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
875 }
876
877 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
878 if (Constant *RC = dyn_cast<Constant>(RHS)) {
879 if (RC->isNullValue())
880 return LHS; // LHS | 0 -> LHS
881 if (Constant *LC = dyn_cast<Constant>(LHS))
882 return Insert(Folder.CreateOr(LC, RC), Name);
883 }
884 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
885 }
886 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
887 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
888 }
889 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
890 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
891 }
892
893 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
894 if (Constant *LC = dyn_cast<Constant>(LHS))
895 if (Constant *RC = dyn_cast<Constant>(RHS))
896 return Insert(Folder.CreateXor(LC, RC), Name);
897 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
898 }
899 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
900 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
901 }
902 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
903 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
904 }
905
906 Value *CreateBinOp(Instruction::BinaryOps Opc,
1a4d82fc
JJ
907 Value *LHS, Value *RHS, const Twine &Name = "",
908 MDNode *FPMathTag = nullptr) {
223e47cc
LB
909 if (Constant *LC = dyn_cast<Constant>(LHS))
910 if (Constant *RC = dyn_cast<Constant>(RHS))
911 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
1a4d82fc
JJ
912 llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
913 if (isa<FPMathOperator>(BinOp))
914 BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
915 return Insert(BinOp, Name);
223e47cc
LB
916 }
917
918 Value *CreateNeg(Value *V, const Twine &Name = "",
919 bool HasNUW = false, bool HasNSW = false) {
920 if (Constant *VC = dyn_cast<Constant>(V))
921 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
922 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
923 if (HasNUW) BO->setHasNoUnsignedWrap();
924 if (HasNSW) BO->setHasNoSignedWrap();
925 return BO;
926 }
927 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
928 return CreateNeg(V, Name, false, true);
929 }
930 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
931 return CreateNeg(V, Name, true, false);
932 }
1a4d82fc
JJ
933 Value *CreateFNeg(Value *V, const Twine &Name = "",
934 MDNode *FPMathTag = nullptr) {
223e47cc
LB
935 if (Constant *VC = dyn_cast<Constant>(V))
936 return Insert(Folder.CreateFNeg(VC), Name);
970d7e83
LB
937 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
938 FPMathTag, FMF), Name);
223e47cc
LB
939 }
940 Value *CreateNot(Value *V, const Twine &Name = "") {
941 if (Constant *VC = dyn_cast<Constant>(V))
942 return Insert(Folder.CreateNot(VC), Name);
943 return Insert(BinaryOperator::CreateNot(V), Name);
944 }
945
946 //===--------------------------------------------------------------------===//
947 // Instruction creation methods: Memory Instructions
948 //===--------------------------------------------------------------------===//
949
1a4d82fc 950 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
223e47cc
LB
951 const Twine &Name = "") {
952 return Insert(new AllocaInst(Ty, ArraySize), Name);
953 }
970d7e83 954 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
223e47cc
LB
955 // converting the string to 'bool' for the isVolatile parameter.
956 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
957 return Insert(new LoadInst(Ptr), Name);
958 }
959 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
960 return Insert(new LoadInst(Ptr), Name);
961 }
962 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1a4d82fc 963 return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
223e47cc
LB
964 }
965 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
966 return Insert(new StoreInst(Val, Ptr, isVolatile));
967 }
970d7e83
LB
968 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
969 // correctly, instead of converting the string to 'bool' for the isVolatile
970 // parameter.
223e47cc
LB
971 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
972 LoadInst *LI = CreateLoad(Ptr, Name);
973 LI->setAlignment(Align);
974 return LI;
975 }
976 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
977 const Twine &Name = "") {
978 LoadInst *LI = CreateLoad(Ptr, Name);
979 LI->setAlignment(Align);
980 return LI;
981 }
982 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
983 const Twine &Name = "") {
984 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
985 LI->setAlignment(Align);
986 return LI;
987 }
988 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
989 bool isVolatile = false) {
990 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
991 SI->setAlignment(Align);
992 return SI;
993 }
994 FenceInst *CreateFence(AtomicOrdering Ordering,
1a4d82fc
JJ
995 SynchronizationScope SynchScope = CrossThread,
996 const Twine &Name = "") {
997 return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
998 }
999 AtomicCmpXchgInst *
1000 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1001 AtomicOrdering SuccessOrdering,
1002 AtomicOrdering FailureOrdering,
1003 SynchronizationScope SynchScope = CrossThread) {
1004 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1005 FailureOrdering, SynchScope));
223e47cc
LB
1006 }
1007 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1008 AtomicOrdering Ordering,
1009 SynchronizationScope SynchScope = CrossThread) {
1010 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
1011 }
1012 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1013 const Twine &Name = "") {
1014 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1015 // Every index must be constant.
1016 size_t i, e;
1017 for (i = 0, e = IdxList.size(); i != e; ++i)
1018 if (!isa<Constant>(IdxList[i]))
1019 break;
1020 if (i == e)
1021 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
1022 }
1023 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
1024 }
1025 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1026 const Twine &Name = "") {
1027 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1028 // Every index must be constant.
1029 size_t i, e;
1030 for (i = 0, e = IdxList.size(); i != e; ++i)
1031 if (!isa<Constant>(IdxList[i]))
1032 break;
1033 if (i == e)
1034 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
1035 }
1036 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
1037 }
1038 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1039 if (Constant *PC = dyn_cast<Constant>(Ptr))
1040 if (Constant *IC = dyn_cast<Constant>(Idx))
1041 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
1042 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
1043 }
1044 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1045 if (Constant *PC = dyn_cast<Constant>(Ptr))
1046 if (Constant *IC = dyn_cast<Constant>(Idx))
1047 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
1048 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1049 }
1050 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1051 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1052
1053 if (Constant *PC = dyn_cast<Constant>(Ptr))
1054 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
1055
1056 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
1057 }
1058 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
1059 const Twine &Name = "") {
1060 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1061
1062 if (Constant *PC = dyn_cast<Constant>(Ptr))
1063 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
1064
1065 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1066 }
1067 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1068 const Twine &Name = "") {
1069 Value *Idxs[] = {
1070 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1071 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1072 };
1073
1074 if (Constant *PC = dyn_cast<Constant>(Ptr))
1075 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1076
1077 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1078 }
1079 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1080 const Twine &Name = "") {
1081 Value *Idxs[] = {
1082 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1083 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1084 };
1085
1086 if (Constant *PC = dyn_cast<Constant>(Ptr))
1087 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1088
1089 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1090 }
1091 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1092 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1093
1094 if (Constant *PC = dyn_cast<Constant>(Ptr))
1095 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
1096
1097 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
1098 }
1099 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1100 const Twine &Name = "") {
1101 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1102
1103 if (Constant *PC = dyn_cast<Constant>(Ptr))
1104 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
1105
1106 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1107 }
1108 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1109 const Twine &Name = "") {
1110 Value *Idxs[] = {
1111 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1112 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1113 };
1114
1115 if (Constant *PC = dyn_cast<Constant>(Ptr))
1116 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1117
1118 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1119 }
1120 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1121 const Twine &Name = "") {
1122 Value *Idxs[] = {
1123 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1124 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1125 };
1126
1127 if (Constant *PC = dyn_cast<Constant>(Ptr))
1128 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1129
1130 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1131 }
1132 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1133 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
1134 }
1135
970d7e83
LB
1136 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1137 /// instead of a pointer to array of i8.
223e47cc
LB
1138 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1139 Value *gv = CreateGlobalString(Str, Name);
1140 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1141 Value *Args[] = { zero, zero };
1142 return CreateInBoundsGEP(gv, Args, Name);
1143 }
1144
1145 //===--------------------------------------------------------------------===//
1146 // Instruction creation methods: Cast/Conversion Operators
1147 //===--------------------------------------------------------------------===//
1148
1149 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1150 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1151 }
1152 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1153 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1154 }
1155 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1156 return CreateCast(Instruction::SExt, V, DestTy, Name);
1157 }
970d7e83
LB
1158 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1159 /// the value untouched if the type of V is already DestTy.
1160 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
223e47cc 1161 const Twine &Name = "") {
970d7e83
LB
1162 assert(V->getType()->isIntOrIntVectorTy() &&
1163 DestTy->isIntOrIntVectorTy() &&
1164 "Can only zero extend/truncate integers!");
1165 Type *VTy = V->getType();
1166 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
223e47cc 1167 return CreateZExt(V, DestTy, Name);
970d7e83 1168 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
223e47cc
LB
1169 return CreateTrunc(V, DestTy, Name);
1170 return V;
1171 }
970d7e83
LB
1172 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1173 /// the value untouched if the type of V is already DestTy.
1174 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
223e47cc 1175 const Twine &Name = "") {
970d7e83
LB
1176 assert(V->getType()->isIntOrIntVectorTy() &&
1177 DestTy->isIntOrIntVectorTy() &&
1178 "Can only sign extend/truncate integers!");
1179 Type *VTy = V->getType();
1180 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
223e47cc 1181 return CreateSExt(V, DestTy, Name);
970d7e83 1182 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
223e47cc
LB
1183 return CreateTrunc(V, DestTy, Name);
1184 return V;
1185 }
1186 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1187 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1188 }
1189 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1190 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1191 }
1192 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1193 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1194 }
1195 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1196 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1197 }
1198 Value *CreateFPTrunc(Value *V, Type *DestTy,
1199 const Twine &Name = "") {
1200 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1201 }
1202 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1203 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1204 }
1205 Value *CreatePtrToInt(Value *V, Type *DestTy,
1206 const Twine &Name = "") {
1207 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1208 }
1209 Value *CreateIntToPtr(Value *V, Type *DestTy,
1210 const Twine &Name = "") {
1211 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1212 }
1213 Value *CreateBitCast(Value *V, Type *DestTy,
1214 const Twine &Name = "") {
1215 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1216 }
1a4d82fc
JJ
1217 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1218 const Twine &Name = "") {
1219 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1220 }
223e47cc
LB
1221 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1222 const Twine &Name = "") {
1223 if (V->getType() == DestTy)
1224 return V;
1225 if (Constant *VC = dyn_cast<Constant>(V))
1226 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1227 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1228 }
1229 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1230 const Twine &Name = "") {
1231 if (V->getType() == DestTy)
1232 return V;
1233 if (Constant *VC = dyn_cast<Constant>(V))
1234 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1235 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1236 }
1237 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1238 const Twine &Name = "") {
1239 if (V->getType() == DestTy)
1240 return V;
1241 if (Constant *VC = dyn_cast<Constant>(V))
1242 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1243 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1244 }
1245 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1246 const Twine &Name = "") {
1247 if (V->getType() == DestTy)
1248 return V;
1249 if (Constant *VC = dyn_cast<Constant>(V))
1250 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1251 return Insert(CastInst::Create(Op, V, DestTy), Name);
1252 }
1253 Value *CreatePointerCast(Value *V, Type *DestTy,
1254 const Twine &Name = "") {
1255 if (V->getType() == DestTy)
1256 return V;
1257 if (Constant *VC = dyn_cast<Constant>(V))
1258 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1259 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1260 }
1a4d82fc
JJ
1261
1262 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1263 const Twine &Name = "") {
1264 if (V->getType() == DestTy)
1265 return V;
1266
1267 if (Constant *VC = dyn_cast<Constant>(V)) {
1268 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1269 Name);
1270 }
1271
1272 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1273 Name);
1274 }
1275
223e47cc
LB
1276 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1277 const Twine &Name = "") {
1278 if (V->getType() == DestTy)
1279 return V;
1280 if (Constant *VC = dyn_cast<Constant>(V))
1281 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1282 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1283 }
85aaf69f
SL
1284
1285 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1286 const Twine &Name = "") {
1287 if (V->getType() == DestTy)
1288 return V;
1289 if (V->getType()->isPointerTy() && DestTy->isIntegerTy())
1290 return CreatePtrToInt(V, DestTy, Name);
1291 if (V->getType()->isIntegerTy() && DestTy->isPointerTy())
1292 return CreateIntToPtr(V, DestTy, Name);
1293
1294 return CreateBitCast(V, DestTy, Name);
1295 }
223e47cc 1296private:
970d7e83
LB
1297 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1298 // compile time error, instead of converting the string to bool for the
1299 // isSigned parameter.
223e47cc
LB
1300 Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
1301public:
1302 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1303 if (V->getType() == DestTy)
1304 return V;
1305 if (Constant *VC = dyn_cast<Constant>(V))
1306 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1307 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1308 }
1309
1310 //===--------------------------------------------------------------------===//
1311 // Instruction creation methods: Compare Instructions
1312 //===--------------------------------------------------------------------===//
1313
1314 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1315 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1316 }
1317 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1318 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1319 }
1320 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1321 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1322 }
1323 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1324 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1325 }
1326 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1327 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1328 }
1329 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1330 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1331 }
1332 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1333 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1334 }
1335 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1336 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1337 }
1338 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1339 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1340 }
1341 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1342 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1343 }
1344
1345 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1346 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1347 }
1348 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1349 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1350 }
1351 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1352 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1353 }
1354 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1355 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1356 }
1357 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1358 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1359 }
1360 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1361 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1362 }
1363 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1364 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1365 }
1366 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1367 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1368 }
1369 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1370 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1371 }
1372 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1373 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1374 }
1375 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1376 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1377 }
1378 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1379 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1380 }
1381 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1382 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1383 }
1384 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1385 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1386 }
1387
1388 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1389 const Twine &Name = "") {
1390 if (Constant *LC = dyn_cast<Constant>(LHS))
1391 if (Constant *RC = dyn_cast<Constant>(RHS))
1392 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1393 return Insert(new ICmpInst(P, LHS, RHS), Name);
1394 }
1395 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1396 const Twine &Name = "") {
1397 if (Constant *LC = dyn_cast<Constant>(LHS))
1398 if (Constant *RC = dyn_cast<Constant>(RHS))
1399 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1400 return Insert(new FCmpInst(P, LHS, RHS), Name);
1401 }
1402
1403 //===--------------------------------------------------------------------===//
1404 // Instruction creation methods: Other Instructions
1405 //===--------------------------------------------------------------------===//
1406
1407 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1408 const Twine &Name = "") {
1409 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1410 }
1411
1412 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1413 return Insert(CallInst::Create(Callee), Name);
1414 }
1415 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1416 return Insert(CallInst::Create(Callee, Arg), Name);
1417 }
1418 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1419 const Twine &Name = "") {
1420 Value *Args[] = { Arg1, Arg2 };
1421 return Insert(CallInst::Create(Callee, Args), Name);
1422 }
1423 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1424 const Twine &Name = "") {
1425 Value *Args[] = { Arg1, Arg2, Arg3 };
1426 return Insert(CallInst::Create(Callee, Args), Name);
1427 }
1428 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1429 Value *Arg4, const Twine &Name = "") {
1430 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1431 return Insert(CallInst::Create(Callee, Args), Name);
1432 }
1433 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1434 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1435 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1436 return Insert(CallInst::Create(Callee, Args), Name);
1437 }
1438
1439 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1440 const Twine &Name = "") {
1441 return Insert(CallInst::Create(Callee, Args), Name);
1442 }
1443
1444 Value *CreateSelect(Value *C, Value *True, Value *False,
1445 const Twine &Name = "") {
1446 if (Constant *CC = dyn_cast<Constant>(C))
1447 if (Constant *TC = dyn_cast<Constant>(True))
1448 if (Constant *FC = dyn_cast<Constant>(False))
1449 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1450 return Insert(SelectInst::Create(C, True, False), Name);
1451 }
1452
1453 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1454 return Insert(new VAArgInst(List, Ty), Name);
1455 }
1456
1457 Value *CreateExtractElement(Value *Vec, Value *Idx,
1458 const Twine &Name = "") {
1459 if (Constant *VC = dyn_cast<Constant>(Vec))
1460 if (Constant *IC = dyn_cast<Constant>(Idx))
1461 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1462 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1463 }
1464
1465 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1466 const Twine &Name = "") {
1467 if (Constant *VC = dyn_cast<Constant>(Vec))
1468 if (Constant *NC = dyn_cast<Constant>(NewElt))
1469 if (Constant *IC = dyn_cast<Constant>(Idx))
1470 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1471 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1472 }
1473
1474 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1475 const Twine &Name = "") {
1476 if (Constant *V1C = dyn_cast<Constant>(V1))
1477 if (Constant *V2C = dyn_cast<Constant>(V2))
1478 if (Constant *MC = dyn_cast<Constant>(Mask))
1479 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1480 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1481 }
1482
1483 Value *CreateExtractValue(Value *Agg,
1484 ArrayRef<unsigned> Idxs,
1485 const Twine &Name = "") {
1486 if (Constant *AggC = dyn_cast<Constant>(Agg))
1487 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1488 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1489 }
1490
1491 Value *CreateInsertValue(Value *Agg, Value *Val,
1492 ArrayRef<unsigned> Idxs,
1493 const Twine &Name = "") {
1494 if (Constant *AggC = dyn_cast<Constant>(Agg))
1495 if (Constant *ValC = dyn_cast<Constant>(Val))
1496 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1497 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1498 }
1499
1500 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1501 const Twine &Name = "") {
970d7e83 1502 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
223e47cc
LB
1503 }
1504
1505 //===--------------------------------------------------------------------===//
1506 // Utility creation methods
1507 //===--------------------------------------------------------------------===//
1508
970d7e83 1509 /// \brief Return an i1 value testing if \p Arg is null.
223e47cc
LB
1510 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1511 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1512 Name);
1513 }
1514
970d7e83 1515 /// \brief Return an i1 value testing if \p Arg is not null.
223e47cc
LB
1516 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1517 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1518 Name);
1519 }
1520
970d7e83
LB
1521 /// \brief Return the i64 difference between two pointer values, dividing out
1522 /// the size of the pointed-to objects.
1523 ///
1524 /// This is intended to implement C-style pointer subtraction. As such, the
1525 /// pointers must be appropriately aligned for their element types and
1526 /// pointing into the same object.
223e47cc
LB
1527 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1528 assert(LHS->getType() == RHS->getType() &&
1529 "Pointer subtraction operand types must match!");
1530 PointerType *ArgType = cast<PointerType>(LHS->getType());
1531 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1532 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1533 Value *Difference = CreateSub(LHS_int, RHS_int);
1534 return CreateExactSDiv(Difference,
1535 ConstantExpr::getSizeOf(ArgType->getElementType()),
1536 Name);
1537 }
970d7e83
LB
1538
1539 /// \brief Return a vector value that contains \arg V broadcasted to \p
1540 /// NumElts elements.
1541 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1542 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1543
1544 // First insert it into an undef vector so we can shuffle it.
1545 Type *I32Ty = getInt32Ty();
1546 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1547 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1548 Name + ".splatinsert");
1549
1550 // Shuffle the value across the desired number of elements.
1551 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1552 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1553 }
1a4d82fc
JJ
1554
1555 /// \brief Return a value that has been extracted from a larger integer type.
1556 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1557 IntegerType *ExtractedTy, uint64_t Offset,
1558 const Twine &Name) {
1559 IntegerType *IntTy = cast<IntegerType>(From->getType());
1560 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1561 DL.getTypeStoreSize(IntTy) &&
1562 "Element extends past full value");
1563 uint64_t ShAmt = 8 * Offset;
1564 Value *V = From;
1565 if (DL.isBigEndian())
1566 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1567 DL.getTypeStoreSize(ExtractedTy) - Offset);
1568 if (ShAmt) {
1569 V = CreateLShr(V, ShAmt, Name + ".shift");
1570 }
1571 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1572 "Cannot extract to a larger integer!");
1573 if (ExtractedTy != IntTy) {
1574 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1575 }
1576 return V;
1577 }
85aaf69f
SL
1578
1579 /// \brief Create an assume intrinsic call that represents an alignment
1580 /// assumption on the provided pointer.
1581 ///
1582 /// An optional offset can be provided, and if it is provided, the offset
1583 /// must be subtracted from the provided pointer to get the pointer with the
1584 /// specified alignment.
1585 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
1586 unsigned Alignment,
1587 Value *OffsetValue = nullptr) {
1588 assert(isa<PointerType>(PtrValue->getType()) &&
1589 "trying to create an alignment assumption on a non-pointer?");
1590
1591 PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
1592 Type *IntPtrTy = getIntPtrTy(&DL, PtrTy->getAddressSpace());
1593 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
1594
1595 Value *Mask = ConstantInt::get(IntPtrTy,
1596 Alignment > 0 ? Alignment - 1 : 0);
1597 if (OffsetValue) {
1598 bool IsOffsetZero = false;
1599 if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
1600 IsOffsetZero = CI->isZero();
1601
1602 if (!IsOffsetZero) {
1603 if (OffsetValue->getType() != IntPtrTy)
1604 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
1605 "offsetcast");
1606 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
1607 }
1608 }
1609
1610 Value *Zero = ConstantInt::get(IntPtrTy, 0);
1611 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
1612 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
1613
1614 return CreateAssumption(InvCond);
1615 }
223e47cc
LB
1616};
1617
1a4d82fc
JJ
1618// Create wrappers for C Binding types (see CBindingWrapping.h).
1619DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
1620
223e47cc
LB
1621}
1622
1623#endif