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