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