Bug Summary

File:llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
Warning:line 910, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name SeparateConstOffsetFromGEP.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/build-llvm/lib/Transforms/Scalar -I /build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar -I /build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/build-llvm/lib/Transforms/Scalar -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-12-11-210320-5824-1 -x c++ /build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

1//===- SeparateConstOffsetFromGEP.cpp -------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Loop unrolling may create many similar GEPs for array accesses.
10// e.g., a 2-level loop
11//
12// float a[32][32]; // global variable
13//
14// for (int i = 0; i < 2; ++i) {
15// for (int j = 0; j < 2; ++j) {
16// ...
17// ... = a[x + i][y + j];
18// ...
19// }
20// }
21//
22// will probably be unrolled to:
23//
24// gep %a, 0, %x, %y; load
25// gep %a, 0, %x, %y + 1; load
26// gep %a, 0, %x + 1, %y; load
27// gep %a, 0, %x + 1, %y + 1; load
28//
29// LLVM's GVN does not use partial redundancy elimination yet, and is thus
30// unable to reuse (gep %a, 0, %x, %y). As a result, this misoptimization incurs
31// significant slowdown in targets with limited addressing modes. For instance,
32// because the PTX target does not support the reg+reg addressing mode, the
33// NVPTX backend emits PTX code that literally computes the pointer address of
34// each GEP, wasting tons of registers. It emits the following PTX for the
35// first load and similar PTX for other loads.
36//
37// mov.u32 %r1, %x;
38// mov.u32 %r2, %y;
39// mul.wide.u32 %rl2, %r1, 128;
40// mov.u64 %rl3, a;
41// add.s64 %rl4, %rl3, %rl2;
42// mul.wide.u32 %rl5, %r2, 4;
43// add.s64 %rl6, %rl4, %rl5;
44// ld.global.f32 %f1, [%rl6];
45//
46// To reduce the register pressure, the optimization implemented in this file
47// merges the common part of a group of GEPs, so we can compute each pointer
48// address by adding a simple offset to the common part, saving many registers.
49//
50// It works by splitting each GEP into a variadic base and a constant offset.
51// The variadic base can be computed once and reused by multiple GEPs, and the
52// constant offsets can be nicely folded into the reg+immediate addressing mode
53// (supported by most targets) without using any extra register.
54//
55// For instance, we transform the four GEPs and four loads in the above example
56// into:
57//
58// base = gep a, 0, x, y
59// load base
60// laod base + 1 * sizeof(float)
61// load base + 32 * sizeof(float)
62// load base + 33 * sizeof(float)
63//
64// Given the transformed IR, a backend that supports the reg+immediate
65// addressing mode can easily fold the pointer arithmetics into the loads. For
66// example, the NVPTX backend can easily fold the pointer arithmetics into the
67// ld.global.f32 instructions, and the resultant PTX uses much fewer registers.
68//
69// mov.u32 %r1, %tid.x;
70// mov.u32 %r2, %tid.y;
71// mul.wide.u32 %rl2, %r1, 128;
72// mov.u64 %rl3, a;
73// add.s64 %rl4, %rl3, %rl2;
74// mul.wide.u32 %rl5, %r2, 4;
75// add.s64 %rl6, %rl4, %rl5;
76// ld.global.f32 %f1, [%rl6]; // so far the same as unoptimized PTX
77// ld.global.f32 %f2, [%rl6+4]; // much better
78// ld.global.f32 %f3, [%rl6+128]; // much better
79// ld.global.f32 %f4, [%rl6+132]; // much better
80//
81// Another improvement enabled by the LowerGEP flag is to lower a GEP with
82// multiple indices to either multiple GEPs with a single index or arithmetic
83// operations (depending on whether the target uses alias analysis in codegen).
84// Such transformation can have following benefits:
85// (1) It can always extract constants in the indices of structure type.
86// (2) After such Lowering, there are more optimization opportunities such as
87// CSE, LICM and CGP.
88//
89// E.g. The following GEPs have multiple indices:
90// BB1:
91// %p = getelementptr [10 x %struct]* %ptr, i64 %i, i64 %j1, i32 3
92// load %p
93// ...
94// BB2:
95// %p2 = getelementptr [10 x %struct]* %ptr, i64 %i, i64 %j1, i32 2
96// load %p2
97// ...
98//
99// We can not do CSE to the common part related to index "i64 %i". Lowering
100// GEPs can achieve such goals.
101// If the target does not use alias analysis in codegen, this pass will
102// lower a GEP with multiple indices into arithmetic operations:
103// BB1:
104// %1 = ptrtoint [10 x %struct]* %ptr to i64 ; CSE opportunity
105// %2 = mul i64 %i, length_of_10xstruct ; CSE opportunity
106// %3 = add i64 %1, %2 ; CSE opportunity
107// %4 = mul i64 %j1, length_of_struct
108// %5 = add i64 %3, %4
109// %6 = add i64 %3, struct_field_3 ; Constant offset
110// %p = inttoptr i64 %6 to i32*
111// load %p
112// ...
113// BB2:
114// %7 = ptrtoint [10 x %struct]* %ptr to i64 ; CSE opportunity
115// %8 = mul i64 %i, length_of_10xstruct ; CSE opportunity
116// %9 = add i64 %7, %8 ; CSE opportunity
117// %10 = mul i64 %j2, length_of_struct
118// %11 = add i64 %9, %10
119// %12 = add i64 %11, struct_field_2 ; Constant offset
120// %p = inttoptr i64 %12 to i32*
121// load %p2
122// ...
123//
124// If the target uses alias analysis in codegen, this pass will lower a GEP
125// with multiple indices into multiple GEPs with a single index:
126// BB1:
127// %1 = bitcast [10 x %struct]* %ptr to i8* ; CSE opportunity
128// %2 = mul i64 %i, length_of_10xstruct ; CSE opportunity
129// %3 = getelementptr i8* %1, i64 %2 ; CSE opportunity
130// %4 = mul i64 %j1, length_of_struct
131// %5 = getelementptr i8* %3, i64 %4
132// %6 = getelementptr i8* %5, struct_field_3 ; Constant offset
133// %p = bitcast i8* %6 to i32*
134// load %p
135// ...
136// BB2:
137// %7 = bitcast [10 x %struct]* %ptr to i8* ; CSE opportunity
138// %8 = mul i64 %i, length_of_10xstruct ; CSE opportunity
139// %9 = getelementptr i8* %7, i64 %8 ; CSE opportunity
140// %10 = mul i64 %j2, length_of_struct
141// %11 = getelementptr i8* %9, i64 %10
142// %12 = getelementptr i8* %11, struct_field_2 ; Constant offset
143// %p2 = bitcast i8* %12 to i32*
144// load %p2
145// ...
146//
147// Lowering GEPs can also benefit other passes such as LICM and CGP.
148// LICM (Loop Invariant Code Motion) can not hoist/sink a GEP of multiple
149// indices if one of the index is variant. If we lower such GEP into invariant
150// parts and variant parts, LICM can hoist/sink those invariant parts.
151// CGP (CodeGen Prepare) tries to sink address calculations that match the
152// target's addressing modes. A GEP with multiple indices may not match and will
153// not be sunk. If we lower such GEP into smaller parts, CGP may sink some of
154// them. So we end up with a better addressing mode.
155//
156//===----------------------------------------------------------------------===//
157
158#include "llvm/Transforms/Scalar/SeparateConstOffsetFromGEP.h"
159#include "llvm/ADT/APInt.h"
160#include "llvm/ADT/DenseMap.h"
161#include "llvm/ADT/DepthFirstIterator.h"
162#include "llvm/ADT/SmallVector.h"
163#include "llvm/Analysis/LoopInfo.h"
164#include "llvm/Analysis/MemoryBuiltins.h"
165#include "llvm/Analysis/ScalarEvolution.h"
166#include "llvm/Analysis/TargetLibraryInfo.h"
167#include "llvm/Analysis/TargetTransformInfo.h"
168#include "llvm/Analysis/ValueTracking.h"
169#include "llvm/IR/BasicBlock.h"
170#include "llvm/IR/Constant.h"
171#include "llvm/IR/Constants.h"
172#include "llvm/IR/DataLayout.h"
173#include "llvm/IR/DerivedTypes.h"
174#include "llvm/IR/Dominators.h"
175#include "llvm/IR/Function.h"
176#include "llvm/IR/GetElementPtrTypeIterator.h"
177#include "llvm/IR/IRBuilder.h"
178#include "llvm/IR/Instruction.h"
179#include "llvm/IR/Instructions.h"
180#include "llvm/IR/Module.h"
181#include "llvm/IR/PassManager.h"
182#include "llvm/IR/PatternMatch.h"
183#include "llvm/IR/Type.h"
184#include "llvm/IR/User.h"
185#include "llvm/IR/Value.h"
186#include "llvm/InitializePasses.h"
187#include "llvm/Pass.h"
188#include "llvm/Support/Casting.h"
189#include "llvm/Support/CommandLine.h"
190#include "llvm/Support/ErrorHandling.h"
191#include "llvm/Support/raw_ostream.h"
192#include "llvm/Target/TargetMachine.h"
193#include "llvm/Transforms/Scalar.h"
194#include "llvm/Transforms/Utils/Local.h"
195#include <cassert>
196#include <cstdint>
197#include <string>
198
199using namespace llvm;
200using namespace llvm::PatternMatch;
201
202static cl::opt<bool> DisableSeparateConstOffsetFromGEP(
203 "disable-separate-const-offset-from-gep", cl::init(false),
204 cl::desc("Do not separate the constant offset from a GEP instruction"),
205 cl::Hidden);
206
207// Setting this flag may emit false positives when the input module already
208// contains dead instructions. Therefore, we set it only in unit tests that are
209// free of dead code.
210static cl::opt<bool>
211 VerifyNoDeadCode("reassociate-geps-verify-no-dead-code", cl::init(false),
212 cl::desc("Verify this pass produces no dead code"),
213 cl::Hidden);
214
215namespace {
216
217/// A helper class for separating a constant offset from a GEP index.
218///
219/// In real programs, a GEP index may be more complicated than a simple addition
220/// of something and a constant integer which can be trivially splitted. For
221/// example, to split ((a << 3) | 5) + b, we need to search deeper for the
222/// constant offset, so that we can separate the index to (a << 3) + b and 5.
223///
224/// Therefore, this class looks into the expression that computes a given GEP
225/// index, and tries to find a constant integer that can be hoisted to the
226/// outermost level of the expression as an addition. Not every constant in an
227/// expression can jump out. e.g., we cannot transform (b * (a + 5)) to (b * a +
228/// 5); nor can we transform (3 * (a + 5)) to (3 * a + 5), however in this case,
229/// -instcombine probably already optimized (3 * (a + 5)) to (3 * a + 15).
230class ConstantOffsetExtractor {
231public:
232 /// Extracts a constant offset from the given GEP index. It returns the
233 /// new index representing the remainder (equal to the original index minus
234 /// the constant offset), or nullptr if we cannot extract a constant offset.
235 /// \p Idx The given GEP index
236 /// \p GEP The given GEP
237 /// \p UserChainTail Outputs the tail of UserChain so that we can
238 /// garbage-collect unused instructions in UserChain.
239 static Value *Extract(Value *Idx, GetElementPtrInst *GEP,
240 User *&UserChainTail, const DominatorTree *DT);
241
242 /// Looks for a constant offset from the given GEP index without extracting
243 /// it. It returns the numeric value of the extracted constant offset (0 if
244 /// failed). The meaning of the arguments are the same as Extract.
245 static int64_t Find(Value *Idx, GetElementPtrInst *GEP,
246 const DominatorTree *DT);
247
248private:
249 ConstantOffsetExtractor(Instruction *InsertionPt, const DominatorTree *DT)
250 : IP(InsertionPt), DL(InsertionPt->getModule()->getDataLayout()), DT(DT) {
251 }
252
253 /// Searches the expression that computes V for a non-zero constant C s.t.
254 /// V can be reassociated into the form V' + C. If the searching is
255 /// successful, returns C and update UserChain as a def-use chain from C to V;
256 /// otherwise, UserChain is empty.
257 ///
258 /// \p V The given expression
259 /// \p SignExtended Whether V will be sign-extended in the computation of the
260 /// GEP index
261 /// \p ZeroExtended Whether V will be zero-extended in the computation of the
262 /// GEP index
263 /// \p NonNegative Whether V is guaranteed to be non-negative. For example,
264 /// an index of an inbounds GEP is guaranteed to be
265 /// non-negative. Levaraging this, we can better split
266 /// inbounds GEPs.
267 APInt find(Value *V, bool SignExtended, bool ZeroExtended, bool NonNegative);
268
269 /// A helper function to look into both operands of a binary operator.
270 APInt findInEitherOperand(BinaryOperator *BO, bool SignExtended,
271 bool ZeroExtended);
272
273 /// After finding the constant offset C from the GEP index I, we build a new
274 /// index I' s.t. I' + C = I. This function builds and returns the new
275 /// index I' according to UserChain produced by function "find".
276 ///
277 /// The building conceptually takes two steps:
278 /// 1) iteratively distribute s/zext towards the leaves of the expression tree
279 /// that computes I
280 /// 2) reassociate the expression tree to the form I' + C.
281 ///
282 /// For example, to extract the 5 from sext(a + (b + 5)), we first distribute
283 /// sext to a, b and 5 so that we have
284 /// sext(a) + (sext(b) + 5).
285 /// Then, we reassociate it to
286 /// (sext(a) + sext(b)) + 5.
287 /// Given this form, we know I' is sext(a) + sext(b).
288 Value *rebuildWithoutConstOffset();
289
290 /// After the first step of rebuilding the GEP index without the constant
291 /// offset, distribute s/zext to the operands of all operators in UserChain.
292 /// e.g., zext(sext(a + (b + 5)) (assuming no overflow) =>
293 /// zext(sext(a)) + (zext(sext(b)) + zext(sext(5))).
294 ///
295 /// The function also updates UserChain to point to new subexpressions after
296 /// distributing s/zext. e.g., the old UserChain of the above example is
297 /// 5 -> b + 5 -> a + (b + 5) -> sext(...) -> zext(sext(...)),
298 /// and the new UserChain is
299 /// zext(sext(5)) -> zext(sext(b)) + zext(sext(5)) ->
300 /// zext(sext(a)) + (zext(sext(b)) + zext(sext(5))
301 ///
302 /// \p ChainIndex The index to UserChain. ChainIndex is initially
303 /// UserChain.size() - 1, and is decremented during
304 /// the recursion.
305 Value *distributeExtsAndCloneChain(unsigned ChainIndex);
306
307 /// Reassociates the GEP index to the form I' + C and returns I'.
308 Value *removeConstOffset(unsigned ChainIndex);
309
310 /// A helper function to apply ExtInsts, a list of s/zext, to value V.
311 /// e.g., if ExtInsts = [sext i32 to i64, zext i16 to i32], this function
312 /// returns "sext i32 (zext i16 V to i32) to i64".
313 Value *applyExts(Value *V);
314
315 /// A helper function that returns whether we can trace into the operands
316 /// of binary operator BO for a constant offset.
317 ///
318 /// \p SignExtended Whether BO is surrounded by sext
319 /// \p ZeroExtended Whether BO is surrounded by zext
320 /// \p NonNegative Whether BO is known to be non-negative, e.g., an in-bound
321 /// array index.
322 bool CanTraceInto(bool SignExtended, bool ZeroExtended, BinaryOperator *BO,
323 bool NonNegative);
324
325 /// The path from the constant offset to the old GEP index. e.g., if the GEP
326 /// index is "a * b + (c + 5)". After running function find, UserChain[0] will
327 /// be the constant 5, UserChain[1] will be the subexpression "c + 5", and
328 /// UserChain[2] will be the entire expression "a * b + (c + 5)".
329 ///
330 /// This path helps to rebuild the new GEP index.
331 SmallVector<User *, 8> UserChain;
332
333 /// A data structure used in rebuildWithoutConstOffset. Contains all
334 /// sext/zext instructions along UserChain.
335 SmallVector<CastInst *, 16> ExtInsts;
336
337 /// Insertion position of cloned instructions.
338 Instruction *IP;
339
340 const DataLayout &DL;
341 const DominatorTree *DT;
342};
343
344/// A pass that tries to split every GEP in the function into a variadic
345/// base and a constant offset. It is a FunctionPass because searching for the
346/// constant offset may inspect other basic blocks.
347class SeparateConstOffsetFromGEPLegacyPass : public FunctionPass {
348public:
349 static char ID;
350
351 SeparateConstOffsetFromGEPLegacyPass(bool LowerGEP = false)
352 : FunctionPass(ID), LowerGEP(LowerGEP) {
353 initializeSeparateConstOffsetFromGEPLegacyPassPass(
354 *PassRegistry::getPassRegistry());
355 }
356
357 void getAnalysisUsage(AnalysisUsage &AU) const override {
358 AU.addRequired<DominatorTreeWrapperPass>();
359 AU.addRequired<ScalarEvolutionWrapperPass>();
360 AU.addRequired<TargetTransformInfoWrapperPass>();
361 AU.addRequired<LoopInfoWrapperPass>();
362 AU.setPreservesCFG();
363 AU.addRequired<TargetLibraryInfoWrapperPass>();
364 }
365
366 bool runOnFunction(Function &F) override;
367
368private:
369 bool LowerGEP;
370};
371
372/// A pass that tries to split every GEP in the function into a variadic
373/// base and a constant offset. It is a FunctionPass because searching for the
374/// constant offset may inspect other basic blocks.
375class SeparateConstOffsetFromGEP {
376public:
377 SeparateConstOffsetFromGEP(
378 DominatorTree *DT, ScalarEvolution *SE, LoopInfo *LI,
379 TargetLibraryInfo *TLI,
380 function_ref<TargetTransformInfo &(Function &)> GetTTI, bool LowerGEP)
381 : DT(DT), SE(SE), LI(LI), TLI(TLI), GetTTI(GetTTI), LowerGEP(LowerGEP) {}
382
383 bool run(Function &F);
384
385private:
386 /// Tries to split the given GEP into a variadic base and a constant offset,
387 /// and returns true if the splitting succeeds.
388 bool splitGEP(GetElementPtrInst *GEP);
389
390 /// Lower a GEP with multiple indices into multiple GEPs with a single index.
391 /// Function splitGEP already split the original GEP into a variadic part and
392 /// a constant offset (i.e., AccumulativeByteOffset). This function lowers the
393 /// variadic part into a set of GEPs with a single index and applies
394 /// AccumulativeByteOffset to it.
395 /// \p Variadic The variadic part of the original GEP.
396 /// \p AccumulativeByteOffset The constant offset.
397 void lowerToSingleIndexGEPs(GetElementPtrInst *Variadic,
398 int64_t AccumulativeByteOffset);
399
400 /// Lower a GEP with multiple indices into ptrtoint+arithmetics+inttoptr form.
401 /// Function splitGEP already split the original GEP into a variadic part and
402 /// a constant offset (i.e., AccumulativeByteOffset). This function lowers the
403 /// variadic part into a set of arithmetic operations and applies
404 /// AccumulativeByteOffset to it.
405 /// \p Variadic The variadic part of the original GEP.
406 /// \p AccumulativeByteOffset The constant offset.
407 void lowerToArithmetics(GetElementPtrInst *Variadic,
408 int64_t AccumulativeByteOffset);
409
410 /// Finds the constant offset within each index and accumulates them. If
411 /// LowerGEP is true, it finds in indices of both sequential and structure
412 /// types, otherwise it only finds in sequential indices. The output
413 /// NeedsExtraction indicates whether we successfully find a non-zero constant
414 /// offset.
415 int64_t accumulateByteOffset(GetElementPtrInst *GEP, bool &NeedsExtraction);
416
417 /// Canonicalize array indices to pointer-size integers. This helps to
418 /// simplify the logic of splitting a GEP. For example, if a + b is a
419 /// pointer-size integer, we have
420 /// gep base, a + b = gep (gep base, a), b
421 /// However, this equality may not hold if the size of a + b is smaller than
422 /// the pointer size, because LLVM conceptually sign-extends GEP indices to
423 /// pointer size before computing the address
424 /// (http://llvm.org/docs/LangRef.html#id181).
425 ///
426 /// This canonicalization is very likely already done in clang and
427 /// instcombine. Therefore, the program will probably remain the same.
428 ///
429 /// Returns true if the module changes.
430 ///
431 /// Verified in @i32_add in split-gep.ll
432 bool canonicalizeArrayIndicesToPointerSize(GetElementPtrInst *GEP);
433
434 /// Optimize sext(a)+sext(b) to sext(a+b) when a+b can't sign overflow.
435 /// SeparateConstOffsetFromGEP distributes a sext to leaves before extracting
436 /// the constant offset. After extraction, it becomes desirable to reunion the
437 /// distributed sexts. For example,
438 ///
439 /// &a[sext(i +nsw (j +nsw 5)]
440 /// => distribute &a[sext(i) +nsw (sext(j) +nsw 5)]
441 /// => constant extraction &a[sext(i) + sext(j)] + 5
442 /// => reunion &a[sext(i +nsw j)] + 5
443 bool reuniteExts(Function &F);
444
445 /// A helper that reunites sexts in an instruction.
446 bool reuniteExts(Instruction *I);
447
448 /// Find the closest dominator of <Dominatee> that is equivalent to <Key>.
449 Instruction *findClosestMatchingDominator(
450 const SCEV *Key, Instruction *Dominatee,
451 DenseMap<const SCEV *, SmallVector<Instruction *, 2>> &DominatingExprs);
452
453 /// Verify F is free of dead code.
454 void verifyNoDeadCode(Function &F);
455
456 bool hasMoreThanOneUseInLoop(Value *v, Loop *L);
457
458 // Swap the index operand of two GEP.
459 void swapGEPOperand(GetElementPtrInst *First, GetElementPtrInst *Second);
460
461 // Check if it is safe to swap operand of two GEP.
462 bool isLegalToSwapOperand(GetElementPtrInst *First, GetElementPtrInst *Second,
463 Loop *CurLoop);
464
465 const DataLayout *DL = nullptr;
466 DominatorTree *DT = nullptr;
467 ScalarEvolution *SE;
468 LoopInfo *LI;
469 TargetLibraryInfo *TLI;
470 // Retrieved lazily since not always used.
471 function_ref<TargetTransformInfo &(Function &)> GetTTI;
472
473 /// Whether to lower a GEP with multiple indices into arithmetic operations or
474 /// multiple GEPs with a single index.
475 bool LowerGEP;
476
477 DenseMap<const SCEV *, SmallVector<Instruction *, 2>> DominatingAdds;
478 DenseMap<const SCEV *, SmallVector<Instruction *, 2>> DominatingSubs;
479};
480
481} // end anonymous namespace
482
483char SeparateConstOffsetFromGEPLegacyPass::ID = 0;
484
485INITIALIZE_PASS_BEGIN(static void *initializeSeparateConstOffsetFromGEPLegacyPassPassOnce
(PassRegistry &Registry) {
486 SeparateConstOffsetFromGEPLegacyPass, "separate-const-offset-from-gep",static void *initializeSeparateConstOffsetFromGEPLegacyPassPassOnce
(PassRegistry &Registry) {
487 "Split GEPs to a variadic base and a constant offset for better CSE", false,static void *initializeSeparateConstOffsetFromGEPLegacyPassPassOnce
(PassRegistry &Registry) {
488 false)static void *initializeSeparateConstOffsetFromGEPLegacyPassPassOnce
(PassRegistry &Registry) {
489INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)initializeDominatorTreeWrapperPassPass(Registry);
490INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)initializeScalarEvolutionWrapperPassPass(Registry);
491INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry);
492INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)initializeLoopInfoWrapperPassPass(Registry);
493INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry);
494INITIALIZE_PASS_END(PassInfo *PI = new PassInfo( "Split GEPs to a variadic base and a constant offset for better CSE"
, "separate-const-offset-from-gep", &SeparateConstOffsetFromGEPLegacyPass
::ID, PassInfo::NormalCtor_t(callDefaultCtor<SeparateConstOffsetFromGEPLegacyPass
>), false, false); Registry.registerPass(*PI, true); return
PI; } static llvm::once_flag InitializeSeparateConstOffsetFromGEPLegacyPassPassFlag
; void llvm::initializeSeparateConstOffsetFromGEPLegacyPassPass
(PassRegistry &Registry) { llvm::call_once(InitializeSeparateConstOffsetFromGEPLegacyPassPassFlag
, initializeSeparateConstOffsetFromGEPLegacyPassPassOnce, std
::ref(Registry)); }
495 SeparateConstOffsetFromGEPLegacyPass, "separate-const-offset-from-gep",PassInfo *PI = new PassInfo( "Split GEPs to a variadic base and a constant offset for better CSE"
, "separate-const-offset-from-gep", &SeparateConstOffsetFromGEPLegacyPass
::ID, PassInfo::NormalCtor_t(callDefaultCtor<SeparateConstOffsetFromGEPLegacyPass
>), false, false); Registry.registerPass(*PI, true); return
PI; } static llvm::once_flag InitializeSeparateConstOffsetFromGEPLegacyPassPassFlag
; void llvm::initializeSeparateConstOffsetFromGEPLegacyPassPass
(PassRegistry &Registry) { llvm::call_once(InitializeSeparateConstOffsetFromGEPLegacyPassPassFlag
, initializeSeparateConstOffsetFromGEPLegacyPassPassOnce, std
::ref(Registry)); }
496 "Split GEPs to a variadic base and a constant offset for better CSE", false,PassInfo *PI = new PassInfo( "Split GEPs to a variadic base and a constant offset for better CSE"
, "separate-const-offset-from-gep", &SeparateConstOffsetFromGEPLegacyPass
::ID, PassInfo::NormalCtor_t(callDefaultCtor<SeparateConstOffsetFromGEPLegacyPass
>), false, false); Registry.registerPass(*PI, true); return
PI; } static llvm::once_flag InitializeSeparateConstOffsetFromGEPLegacyPassPassFlag
; void llvm::initializeSeparateConstOffsetFromGEPLegacyPassPass
(PassRegistry &Registry) { llvm::call_once(InitializeSeparateConstOffsetFromGEPLegacyPassPassFlag
, initializeSeparateConstOffsetFromGEPLegacyPassPassOnce, std
::ref(Registry)); }
497 false)PassInfo *PI = new PassInfo( "Split GEPs to a variadic base and a constant offset for better CSE"
, "separate-const-offset-from-gep", &SeparateConstOffsetFromGEPLegacyPass
::ID, PassInfo::NormalCtor_t(callDefaultCtor<SeparateConstOffsetFromGEPLegacyPass
>), false, false); Registry.registerPass(*PI, true); return
PI; } static llvm::once_flag InitializeSeparateConstOffsetFromGEPLegacyPassPassFlag
; void llvm::initializeSeparateConstOffsetFromGEPLegacyPassPass
(PassRegistry &Registry) { llvm::call_once(InitializeSeparateConstOffsetFromGEPLegacyPassPassFlag
, initializeSeparateConstOffsetFromGEPLegacyPassPassOnce, std
::ref(Registry)); }
498
499FunctionPass *llvm::createSeparateConstOffsetFromGEPPass(bool LowerGEP) {
500 return new SeparateConstOffsetFromGEPLegacyPass(LowerGEP);
501}
502
503bool ConstantOffsetExtractor::CanTraceInto(bool SignExtended,
504 bool ZeroExtended,
505 BinaryOperator *BO,
506 bool NonNegative) {
507 // We only consider ADD, SUB and OR, because a non-zero constant found in
508 // expressions composed of these operations can be easily hoisted as a
509 // constant offset by reassociation.
510 if (BO->getOpcode() != Instruction::Add &&
511 BO->getOpcode() != Instruction::Sub &&
512 BO->getOpcode() != Instruction::Or) {
513 return false;
514 }
515
516 Value *LHS = BO->getOperand(0), *RHS = BO->getOperand(1);
517 // Do not trace into "or" unless it is equivalent to "add". If LHS and RHS
518 // don't have common bits, (LHS | RHS) is equivalent to (LHS + RHS).
519 // FIXME: this does not appear to be covered by any tests
520 // (with x86/aarch64 backends at least)
521 if (BO->getOpcode() == Instruction::Or &&
522 !haveNoCommonBitsSet(LHS, RHS, DL, nullptr, BO, DT))
523 return false;
524
525 // In addition, tracing into BO requires that its surrounding s/zext (if
526 // any) is distributable to both operands.
527 //
528 // Suppose BO = A op B.
529 // SignExtended | ZeroExtended | Distributable?
530 // --------------+--------------+----------------------------------
531 // 0 | 0 | true because no s/zext exists
532 // 0 | 1 | zext(BO) == zext(A) op zext(B)
533 // 1 | 0 | sext(BO) == sext(A) op sext(B)
534 // 1 | 1 | zext(sext(BO)) ==
535 // | | zext(sext(A)) op zext(sext(B))
536 if (BO->getOpcode() == Instruction::Add && !ZeroExtended && NonNegative) {
537 // If a + b >= 0 and (a >= 0 or b >= 0), then
538 // sext(a + b) = sext(a) + sext(b)
539 // even if the addition is not marked nsw.
540 //
541 // Leveraging this invariant, we can trace into an sext'ed inbound GEP
542 // index if the constant offset is non-negative.
543 //
544 // Verified in @sext_add in split-gep.ll.
545 if (ConstantInt *ConstLHS = dyn_cast<ConstantInt>(LHS)) {
546 if (!ConstLHS->isNegative())
547 return true;
548 }
549 if (ConstantInt *ConstRHS = dyn_cast<ConstantInt>(RHS)) {
550 if (!ConstRHS->isNegative())
551 return true;
552 }
553 }
554
555 // sext (add/sub nsw A, B) == add/sub nsw (sext A), (sext B)
556 // zext (add/sub nuw A, B) == add/sub nuw (zext A), (zext B)
557 if (BO->getOpcode() == Instruction::Add ||
558 BO->getOpcode() == Instruction::Sub) {
559 if (SignExtended && !BO->hasNoSignedWrap())
560 return false;
561 if (ZeroExtended && !BO->hasNoUnsignedWrap())
562 return false;
563 }
564
565 return true;
566}
567
568APInt ConstantOffsetExtractor::findInEitherOperand(BinaryOperator *BO,
569 bool SignExtended,
570 bool ZeroExtended) {
571 // Save off the current height of the chain, in case we need to restore it.
572 size_t ChainLength = UserChain.size();
573
574 // BO being non-negative does not shed light on whether its operands are
575 // non-negative. Clear the NonNegative flag here.
576 APInt ConstantOffset = find(BO->getOperand(0), SignExtended, ZeroExtended,
577 /* NonNegative */ false);
578 // If we found a constant offset in the left operand, stop and return that.
579 // This shortcut might cause us to miss opportunities of combining the
580 // constant offsets in both operands, e.g., (a + 4) + (b + 5) => (a + b) + 9.
581 // However, such cases are probably already handled by -instcombine,
582 // given this pass runs after the standard optimizations.
583 if (ConstantOffset != 0) return ConstantOffset;
584
585 // Reset the chain back to where it was when we started exploring this node,
586 // since visiting the LHS didn't pan out.
587 UserChain.resize(ChainLength);
588
589 ConstantOffset = find(BO->getOperand(1), SignExtended, ZeroExtended,
590 /* NonNegative */ false);
591 // If U is a sub operator, negate the constant offset found in the right
592 // operand.
593 if (BO->getOpcode() == Instruction::Sub)
594 ConstantOffset = -ConstantOffset;
595
596 // If RHS wasn't a suitable candidate either, reset the chain again.
597 if (ConstantOffset == 0)
598 UserChain.resize(ChainLength);
599
600 return ConstantOffset;
601}
602
603APInt ConstantOffsetExtractor::find(Value *V, bool SignExtended,
604 bool ZeroExtended, bool NonNegative) {
605 // TODO(jingyue): We could trace into integer/pointer casts, such as
606 // inttoptr, ptrtoint, bitcast, and addrspacecast. We choose to handle only
607 // integers because it gives good enough results for our benchmarks.
608 unsigned BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
609
610 // We cannot do much with Values that are not a User, such as an Argument.
611 User *U = dyn_cast<User>(V);
612 if (U == nullptr) return APInt(BitWidth, 0);
613
614 APInt ConstantOffset(BitWidth, 0);
615 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
616 // Hooray, we found it!
617 ConstantOffset = CI->getValue();
618 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V)) {
619 // Trace into subexpressions for more hoisting opportunities.
620 if (CanTraceInto(SignExtended, ZeroExtended, BO, NonNegative))
621 ConstantOffset = findInEitherOperand(BO, SignExtended, ZeroExtended);
622 } else if (isa<TruncInst>(V)) {
623 ConstantOffset =
624 find(U->getOperand(0), SignExtended, ZeroExtended, NonNegative)
625 .trunc(BitWidth);
626 } else if (isa<SExtInst>(V)) {
627 ConstantOffset = find(U->getOperand(0), /* SignExtended */ true,
628 ZeroExtended, NonNegative).sext(BitWidth);
629 } else if (isa<ZExtInst>(V)) {
630 // As an optimization, we can clear the SignExtended flag because
631 // sext(zext(a)) = zext(a). Verified in @sext_zext in split-gep.ll.
632 //
633 // Clear the NonNegative flag, because zext(a) >= 0 does not imply a >= 0.
634 ConstantOffset =
635 find(U->getOperand(0), /* SignExtended */ false,
636 /* ZeroExtended */ true, /* NonNegative */ false).zext(BitWidth);
637 }
638
639 // If we found a non-zero constant offset, add it to the path for
640 // rebuildWithoutConstOffset. Zero is a valid constant offset, but doesn't
641 // help this optimization.
642 if (ConstantOffset != 0)
643 UserChain.push_back(U);
644 return ConstantOffset;
645}
646
647Value *ConstantOffsetExtractor::applyExts(Value *V) {
648 Value *Current = V;
649 // ExtInsts is built in the use-def order. Therefore, we apply them to V
650 // in the reversed order.
651 for (auto I = ExtInsts.rbegin(), E = ExtInsts.rend(); I != E; ++I) {
652 if (Constant *C = dyn_cast<Constant>(Current)) {
653 // If Current is a constant, apply s/zext using ConstantExpr::getCast.
654 // ConstantExpr::getCast emits a ConstantInt if C is a ConstantInt.
655 Current = ConstantExpr::getCast((*I)->getOpcode(), C, (*I)->getType());
656 } else {
657 Instruction *Ext = (*I)->clone();
658 Ext->setOperand(0, Current);
659 Ext->insertBefore(IP);
660 Current = Ext;
661 }
662 }
663 return Current;
664}
665
666Value *ConstantOffsetExtractor::rebuildWithoutConstOffset() {
667 distributeExtsAndCloneChain(UserChain.size() - 1);
668 // Remove all nullptrs (used to be s/zext) from UserChain.
669 unsigned NewSize = 0;
670 for (User *I : UserChain) {
671 if (I != nullptr) {
672 UserChain[NewSize] = I;
673 NewSize++;
674 }
675 }
676 UserChain.resize(NewSize);
677 return removeConstOffset(UserChain.size() - 1);
678}
679
680Value *
681ConstantOffsetExtractor::distributeExtsAndCloneChain(unsigned ChainIndex) {
682 User *U = UserChain[ChainIndex];
683 if (ChainIndex == 0) {
684 assert(isa<ConstantInt>(U))((isa<ConstantInt>(U)) ? static_cast<void> (0) : __assert_fail
("isa<ConstantInt>(U)", "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 684, __PRETTY_FUNCTION__))
;
685 // If U is a ConstantInt, applyExts will return a ConstantInt as well.
686 return UserChain[ChainIndex] = cast<ConstantInt>(applyExts(U));
687 }
688
689 if (CastInst *Cast = dyn_cast<CastInst>(U)) {
690 assert((((isa<SExtInst>(Cast) || isa<ZExtInst>(Cast) || isa
<TruncInst>(Cast)) && "Only following instructions can be traced: sext, zext & trunc"
) ? static_cast<void> (0) : __assert_fail ("(isa<SExtInst>(Cast) || isa<ZExtInst>(Cast) || isa<TruncInst>(Cast)) && \"Only following instructions can be traced: sext, zext & trunc\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 692, __PRETTY_FUNCTION__))
691 (isa<SExtInst>(Cast) || isa<ZExtInst>(Cast) || isa<TruncInst>(Cast)) &&(((isa<SExtInst>(Cast) || isa<ZExtInst>(Cast) || isa
<TruncInst>(Cast)) && "Only following instructions can be traced: sext, zext & trunc"
) ? static_cast<void> (0) : __assert_fail ("(isa<SExtInst>(Cast) || isa<ZExtInst>(Cast) || isa<TruncInst>(Cast)) && \"Only following instructions can be traced: sext, zext & trunc\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 692, __PRETTY_FUNCTION__))
692 "Only following instructions can be traced: sext, zext & trunc")(((isa<SExtInst>(Cast) || isa<ZExtInst>(Cast) || isa
<TruncInst>(Cast)) && "Only following instructions can be traced: sext, zext & trunc"
) ? static_cast<void> (0) : __assert_fail ("(isa<SExtInst>(Cast) || isa<ZExtInst>(Cast) || isa<TruncInst>(Cast)) && \"Only following instructions can be traced: sext, zext & trunc\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 692, __PRETTY_FUNCTION__))
;
693 ExtInsts.push_back(Cast);
694 UserChain[ChainIndex] = nullptr;
695 return distributeExtsAndCloneChain(ChainIndex - 1);
696 }
697
698 // Function find only trace into BinaryOperator and CastInst.
699 BinaryOperator *BO = cast<BinaryOperator>(U);
700 // OpNo = which operand of BO is UserChain[ChainIndex - 1]
701 unsigned OpNo = (BO->getOperand(0) == UserChain[ChainIndex - 1] ? 0 : 1);
702 Value *TheOther = applyExts(BO->getOperand(1 - OpNo));
703 Value *NextInChain = distributeExtsAndCloneChain(ChainIndex - 1);
704
705 BinaryOperator *NewBO = nullptr;
706 if (OpNo == 0) {
707 NewBO = BinaryOperator::Create(BO->getOpcode(), NextInChain, TheOther,
708 BO->getName(), IP);
709 } else {
710 NewBO = BinaryOperator::Create(BO->getOpcode(), TheOther, NextInChain,
711 BO->getName(), IP);
712 }
713 return UserChain[ChainIndex] = NewBO;
714}
715
716Value *ConstantOffsetExtractor::removeConstOffset(unsigned ChainIndex) {
717 if (ChainIndex == 0) {
718 assert(isa<ConstantInt>(UserChain[ChainIndex]))((isa<ConstantInt>(UserChain[ChainIndex])) ? static_cast
<void> (0) : __assert_fail ("isa<ConstantInt>(UserChain[ChainIndex])"
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 718, __PRETTY_FUNCTION__))
;
719 return ConstantInt::getNullValue(UserChain[ChainIndex]->getType());
720 }
721
722 BinaryOperator *BO = cast<BinaryOperator>(UserChain[ChainIndex]);
723 assert((BO->use_empty() || BO->hasOneUse()) &&(((BO->use_empty() || BO->hasOneUse()) && "distributeExtsAndCloneChain clones each BinaryOperator in "
"UserChain, so no one should be used more than " "once") ? static_cast
<void> (0) : __assert_fail ("(BO->use_empty() || BO->hasOneUse()) && \"distributeExtsAndCloneChain clones each BinaryOperator in \" \"UserChain, so no one should be used more than \" \"once\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 726, __PRETTY_FUNCTION__))
724 "distributeExtsAndCloneChain clones each BinaryOperator in "(((BO->use_empty() || BO->hasOneUse()) && "distributeExtsAndCloneChain clones each BinaryOperator in "
"UserChain, so no one should be used more than " "once") ? static_cast
<void> (0) : __assert_fail ("(BO->use_empty() || BO->hasOneUse()) && \"distributeExtsAndCloneChain clones each BinaryOperator in \" \"UserChain, so no one should be used more than \" \"once\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 726, __PRETTY_FUNCTION__))
725 "UserChain, so no one should be used more than "(((BO->use_empty() || BO->hasOneUse()) && "distributeExtsAndCloneChain clones each BinaryOperator in "
"UserChain, so no one should be used more than " "once") ? static_cast
<void> (0) : __assert_fail ("(BO->use_empty() || BO->hasOneUse()) && \"distributeExtsAndCloneChain clones each BinaryOperator in \" \"UserChain, so no one should be used more than \" \"once\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 726, __PRETTY_FUNCTION__))
726 "once")(((BO->use_empty() || BO->hasOneUse()) && "distributeExtsAndCloneChain clones each BinaryOperator in "
"UserChain, so no one should be used more than " "once") ? static_cast
<void> (0) : __assert_fail ("(BO->use_empty() || BO->hasOneUse()) && \"distributeExtsAndCloneChain clones each BinaryOperator in \" \"UserChain, so no one should be used more than \" \"once\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 726, __PRETTY_FUNCTION__))
;
727
728 unsigned OpNo = (BO->getOperand(0) == UserChain[ChainIndex - 1] ? 0 : 1);
729 assert(BO->getOperand(OpNo) == UserChain[ChainIndex - 1])((BO->getOperand(OpNo) == UserChain[ChainIndex - 1]) ? static_cast
<void> (0) : __assert_fail ("BO->getOperand(OpNo) == UserChain[ChainIndex - 1]"
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 729, __PRETTY_FUNCTION__))
;
730 Value *NextInChain = removeConstOffset(ChainIndex - 1);
731 Value *TheOther = BO->getOperand(1 - OpNo);
732
733 // If NextInChain is 0 and not the LHS of a sub, we can simplify the
734 // sub-expression to be just TheOther.
735 if (ConstantInt *CI = dyn_cast<ConstantInt>(NextInChain)) {
736 if (CI->isZero() && !(BO->getOpcode() == Instruction::Sub && OpNo == 0))
737 return TheOther;
738 }
739
740 BinaryOperator::BinaryOps NewOp = BO->getOpcode();
741 if (BO->getOpcode() == Instruction::Or) {
742 // Rebuild "or" as "add", because "or" may be invalid for the new
743 // expression.
744 //
745 // For instance, given
746 // a | (b + 5) where a and b + 5 have no common bits,
747 // we can extract 5 as the constant offset.
748 //
749 // However, reusing the "or" in the new index would give us
750 // (a | b) + 5
751 // which does not equal a | (b + 5).
752 //
753 // Replacing the "or" with "add" is fine, because
754 // a | (b + 5) = a + (b + 5) = (a + b) + 5
755 NewOp = Instruction::Add;
756 }
757
758 BinaryOperator *NewBO;
759 if (OpNo == 0) {
760 NewBO = BinaryOperator::Create(NewOp, NextInChain, TheOther, "", IP);
761 } else {
762 NewBO = BinaryOperator::Create(NewOp, TheOther, NextInChain, "", IP);
763 }
764 NewBO->takeName(BO);
765 return NewBO;
766}
767
768Value *ConstantOffsetExtractor::Extract(Value *Idx, GetElementPtrInst *GEP,
769 User *&UserChainTail,
770 const DominatorTree *DT) {
771 ConstantOffsetExtractor Extractor(GEP, DT);
772 // Find a non-zero constant offset first.
773 APInt ConstantOffset =
774 Extractor.find(Idx, /* SignExtended */ false, /* ZeroExtended */ false,
775 GEP->isInBounds());
776 if (ConstantOffset == 0) {
777 UserChainTail = nullptr;
778 return nullptr;
779 }
780 // Separates the constant offset from the GEP index.
781 Value *IdxWithoutConstOffset = Extractor.rebuildWithoutConstOffset();
782 UserChainTail = Extractor.UserChain.back();
783 return IdxWithoutConstOffset;
784}
785
786int64_t ConstantOffsetExtractor::Find(Value *Idx, GetElementPtrInst *GEP,
787 const DominatorTree *DT) {
788 // If Idx is an index of an inbound GEP, Idx is guaranteed to be non-negative.
789 return ConstantOffsetExtractor(GEP, DT)
790 .find(Idx, /* SignExtended */ false, /* ZeroExtended */ false,
791 GEP->isInBounds())
792 .getSExtValue();
793}
794
795bool SeparateConstOffsetFromGEP::canonicalizeArrayIndicesToPointerSize(
796 GetElementPtrInst *GEP) {
797 bool Changed = false;
798 Type *IntPtrTy = DL->getIntPtrType(GEP->getType());
799 gep_type_iterator GTI = gep_type_begin(*GEP);
800 for (User::op_iterator I = GEP->op_begin() + 1, E = GEP->op_end();
801 I != E; ++I, ++GTI) {
802 // Skip struct member indices which must be i32.
803 if (GTI.isSequential()) {
804 if ((*I)->getType() != IntPtrTy) {
805 *I = CastInst::CreateIntegerCast(*I, IntPtrTy, true, "idxprom", GEP);
806 Changed = true;
807 }
808 }
809 }
810 return Changed;
811}
812
813int64_t
814SeparateConstOffsetFromGEP::accumulateByteOffset(GetElementPtrInst *GEP,
815 bool &NeedsExtraction) {
816 NeedsExtraction = false;
817 int64_t AccumulativeByteOffset = 0;
818 gep_type_iterator GTI = gep_type_begin(*GEP);
819 for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
820 if (GTI.isSequential()) {
821 // Tries to extract a constant offset from this GEP index.
822 int64_t ConstantOffset =
823 ConstantOffsetExtractor::Find(GEP->getOperand(I), GEP, DT);
824 if (ConstantOffset != 0) {
825 NeedsExtraction = true;
826 // A GEP may have multiple indices. We accumulate the extracted
827 // constant offset to a byte offset, and later offset the remainder of
828 // the original GEP with this byte offset.
829 AccumulativeByteOffset +=
830 ConstantOffset * DL->getTypeAllocSize(GTI.getIndexedType());
831 }
832 } else if (LowerGEP) {
833 StructType *StTy = GTI.getStructType();
834 uint64_t Field = cast<ConstantInt>(GEP->getOperand(I))->getZExtValue();
835 // Skip field 0 as the offset is always 0.
836 if (Field != 0) {
837 NeedsExtraction = true;
838 AccumulativeByteOffset +=
839 DL->getStructLayout(StTy)->getElementOffset(Field);
840 }
841 }
842 }
843 return AccumulativeByteOffset;
844}
845
846void SeparateConstOffsetFromGEP::lowerToSingleIndexGEPs(
847 GetElementPtrInst *Variadic, int64_t AccumulativeByteOffset) {
848 IRBuilder<> Builder(Variadic);
849 Type *IntPtrTy = DL->getIntPtrType(Variadic->getType());
850
851 Type *I8PtrTy =
852 Builder.getInt8PtrTy(Variadic->getType()->getPointerAddressSpace());
853 Value *ResultPtr = Variadic->getOperand(0);
854 Loop *L = LI->getLoopFor(Variadic->getParent());
855 // Check if the base is not loop invariant or used more than once.
856 bool isSwapCandidate =
857 L && L->isLoopInvariant(ResultPtr) &&
19
Assuming 'L' is null
858 !hasMoreThanOneUseInLoop(ResultPtr, L);
859 Value *FirstResult = nullptr;
860
861 if (ResultPtr->getType() != I8PtrTy)
20
Assuming the condition is false
21
Taking false branch
862 ResultPtr = Builder.CreateBitCast(ResultPtr, I8PtrTy);
863
864 gep_type_iterator GTI = gep_type_begin(*Variadic);
865 // Create an ugly GEP for each sequential index. We don't create GEPs for
866 // structure indices, as they are accumulated in the constant offset index.
867 for (unsigned I = 1, E = Variadic->getNumOperands(); I != E; ++I, ++GTI) {
22
Assuming 'I' is equal to 'E'
23
Loop condition is false. Execution continues on line 895
868 if (GTI.isSequential()) {
869 Value *Idx = Variadic->getOperand(I);
870 // Skip zero indices.
871 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx))
872 if (CI->isZero())
873 continue;
874
875 APInt ElementSize = APInt(IntPtrTy->getIntegerBitWidth(),
876 DL->getTypeAllocSize(GTI.getIndexedType()));
877 // Scale the index by element size.
878 if (ElementSize != 1) {
879 if (ElementSize.isPowerOf2()) {
880 Idx = Builder.CreateShl(
881 Idx, ConstantInt::get(IntPtrTy, ElementSize.logBase2()));
882 } else {
883 Idx = Builder.CreateMul(Idx, ConstantInt::get(IntPtrTy, ElementSize));
884 }
885 }
886 // Create an ugly GEP with a single index for each index.
887 ResultPtr =
888 Builder.CreateGEP(Builder.getInt8Ty(), ResultPtr, Idx, "uglygep");
889 if (FirstResult == nullptr)
890 FirstResult = ResultPtr;
891 }
892 }
893
894 // Create a GEP with the constant offset index.
895 if (AccumulativeByteOffset != 0) {
24
Assuming 'AccumulativeByteOffset' is not equal to 0
25
Taking true branch
896 Value *Offset = ConstantInt::get(IntPtrTy, AccumulativeByteOffset);
897 ResultPtr =
33
Value assigned to 'ResultPtr'
898 Builder.CreateGEP(Builder.getInt8Ty(), ResultPtr, Offset, "uglygep");
26
Calling 'IRBuilderBase::CreateGEP'
32
Returning from 'IRBuilderBase::CreateGEP'
899 } else
900 isSwapCandidate = false;
901
902 // If we created a GEP with constant index, and the base is loop invariant,
903 // then we swap the first one with it, so LICM can move constant GEP out
904 // later.
905 GetElementPtrInst *FirstGEP = dyn_cast_or_null<GetElementPtrInst>(FirstResult);
34
Assuming null pointer is passed into cast
906 GetElementPtrInst *SecondGEP = dyn_cast_or_null<GetElementPtrInst>(ResultPtr);
35
Assuming null pointer is passed into cast
36
Assuming pointer value is null
907 if (isSwapCandidate
36.1
'isSwapCandidate' is false
36.1
'isSwapCandidate' is false
&& isLegalToSwapOperand(FirstGEP, SecondGEP, L))
908 swapGEPOperand(FirstGEP, SecondGEP);
909
910 if (ResultPtr->getType() != Variadic->getType())
37
Called C++ object pointer is null
911 ResultPtr = Builder.CreateBitCast(ResultPtr, Variadic->getType());
912
913 Variadic->replaceAllUsesWith(ResultPtr);
914 Variadic->eraseFromParent();
915}
916
917void
918SeparateConstOffsetFromGEP::lowerToArithmetics(GetElementPtrInst *Variadic,
919 int64_t AccumulativeByteOffset) {
920 IRBuilder<> Builder(Variadic);
921 Type *IntPtrTy = DL->getIntPtrType(Variadic->getType());
922
923 Value *ResultPtr = Builder.CreatePtrToInt(Variadic->getOperand(0), IntPtrTy);
924 gep_type_iterator GTI = gep_type_begin(*Variadic);
925 // Create ADD/SHL/MUL arithmetic operations for each sequential indices. We
926 // don't create arithmetics for structure indices, as they are accumulated
927 // in the constant offset index.
928 for (unsigned I = 1, E = Variadic->getNumOperands(); I != E; ++I, ++GTI) {
929 if (GTI.isSequential()) {
930 Value *Idx = Variadic->getOperand(I);
931 // Skip zero indices.
932 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx))
933 if (CI->isZero())
934 continue;
935
936 APInt ElementSize = APInt(IntPtrTy->getIntegerBitWidth(),
937 DL->getTypeAllocSize(GTI.getIndexedType()));
938 // Scale the index by element size.
939 if (ElementSize != 1) {
940 if (ElementSize.isPowerOf2()) {
941 Idx = Builder.CreateShl(
942 Idx, ConstantInt::get(IntPtrTy, ElementSize.logBase2()));
943 } else {
944 Idx = Builder.CreateMul(Idx, ConstantInt::get(IntPtrTy, ElementSize));
945 }
946 }
947 // Create an ADD for each index.
948 ResultPtr = Builder.CreateAdd(ResultPtr, Idx);
949 }
950 }
951
952 // Create an ADD for the constant offset index.
953 if (AccumulativeByteOffset != 0) {
954 ResultPtr = Builder.CreateAdd(
955 ResultPtr, ConstantInt::get(IntPtrTy, AccumulativeByteOffset));
956 }
957
958 ResultPtr = Builder.CreateIntToPtr(ResultPtr, Variadic->getType());
959 Variadic->replaceAllUsesWith(ResultPtr);
960 Variadic->eraseFromParent();
961}
962
963bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
964 // Skip vector GEPs.
965 if (GEP->getType()->isVectorTy())
8
Taking false branch
966 return false;
967
968 // The backend can already nicely handle the case where all indices are
969 // constant.
970 if (GEP->hasAllConstantIndices())
9
Assuming the condition is false
10
Taking false branch
971 return false;
972
973 bool Changed = canonicalizeArrayIndicesToPointerSize(GEP);
974
975 bool NeedsExtraction;
976 int64_t AccumulativeByteOffset = accumulateByteOffset(GEP, NeedsExtraction);
977
978 if (!NeedsExtraction
10.1
'NeedsExtraction' is true
10.1
'NeedsExtraction' is true
)
11
Taking false branch
979 return Changed;
980
981 TargetTransformInfo &TTI = GetTTI(*GEP->getFunction());
982
983 // If LowerGEP is disabled, before really splitting the GEP, check whether the
984 // backend supports the addressing mode we are about to produce. If no, this
985 // splitting probably won't be beneficial.
986 // If LowerGEP is enabled, even the extracted constant offset can not match
987 // the addressing mode, we can still do optimizations to other lowered parts
988 // of variable indices. Therefore, we don't check for addressing modes in that
989 // case.
990 if (!LowerGEP
11.1
Field 'LowerGEP' is true
11.1
Field 'LowerGEP' is true
) {
12
Taking false branch
991 unsigned AddrSpace = GEP->getPointerAddressSpace();
992 if (!TTI.isLegalAddressingMode(GEP->getResultElementType(),
993 /*BaseGV=*/nullptr, AccumulativeByteOffset,
994 /*HasBaseReg=*/true, /*Scale=*/0,
995 AddrSpace)) {
996 return Changed;
997 }
998 }
999
1000 // Remove the constant offset in each sequential index. The resultant GEP
1001 // computes the variadic base.
1002 // Notice that we don't remove struct field indices here. If LowerGEP is
1003 // disabled, a structure index is not accumulated and we still use the old
1004 // one. If LowerGEP is enabled, a structure index is accumulated in the
1005 // constant offset. LowerToSingleIndexGEPs or lowerToArithmetics will later
1006 // handle the constant offset and won't need a new structure index.
1007 gep_type_iterator GTI = gep_type_begin(*GEP);
1008 for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
13
Assuming 'I' is equal to 'E'
14
Loop condition is false. Execution continues on line 1046
1009 if (GTI.isSequential()) {
1010 // Splits this GEP index into a variadic part and a constant offset, and
1011 // uses the variadic part as the new index.
1012 Value *OldIdx = GEP->getOperand(I);
1013 User *UserChainTail;
1014 Value *NewIdx =
1015 ConstantOffsetExtractor::Extract(OldIdx, GEP, UserChainTail, DT);
1016 if (NewIdx != nullptr) {
1017 // Switches to the index with the constant offset removed.
1018 GEP->setOperand(I, NewIdx);
1019 // After switching to the new index, we can garbage-collect UserChain
1020 // and the old index if they are not used.
1021 RecursivelyDeleteTriviallyDeadInstructions(UserChainTail);
1022 RecursivelyDeleteTriviallyDeadInstructions(OldIdx);
1023 }
1024 }
1025 }
1026
1027 // Clear the inbounds attribute because the new index may be off-bound.
1028 // e.g.,
1029 //
1030 // b = add i64 a, 5
1031 // addr = gep inbounds float, float* p, i64 b
1032 //
1033 // is transformed to:
1034 //
1035 // addr2 = gep float, float* p, i64 a ; inbounds removed
1036 // addr = gep inbounds float, float* addr2, i64 5
1037 //
1038 // If a is -4, although the old index b is in bounds, the new index a is
1039 // off-bound. http://llvm.org/docs/LangRef.html#id181 says "if the
1040 // inbounds keyword is not present, the offsets are added to the base
1041 // address with silently-wrapping two's complement arithmetic".
1042 // Therefore, the final code will be a semantically equivalent.
1043 //
1044 // TODO(jingyue): do some range analysis to keep as many inbounds as
1045 // possible. GEPs with inbounds are more friendly to alias analysis.
1046 bool GEPWasInBounds = GEP->isInBounds();
1047 GEP->setIsInBounds(false);
1048
1049 // Lowers a GEP to either GEPs with a single index or arithmetic operations.
1050 if (LowerGEP
14.1
Field 'LowerGEP' is true
14.1
Field 'LowerGEP' is true
) {
15
Taking true branch
1051 // As currently BasicAA does not analyze ptrtoint/inttoptr, do not lower to
1052 // arithmetic operations if the target uses alias analysis in codegen.
1053 if (TTI.useAA())
16
Assuming the condition is true
17
Taking true branch
1054 lowerToSingleIndexGEPs(GEP, AccumulativeByteOffset);
18
Calling 'SeparateConstOffsetFromGEP::lowerToSingleIndexGEPs'
1055 else
1056 lowerToArithmetics(GEP, AccumulativeByteOffset);
1057 return true;
1058 }
1059
1060 // No need to create another GEP if the accumulative byte offset is 0.
1061 if (AccumulativeByteOffset == 0)
1062 return true;
1063
1064 // Offsets the base with the accumulative byte offset.
1065 //
1066 // %gep ; the base
1067 // ... %gep ...
1068 //
1069 // => add the offset
1070 //
1071 // %gep2 ; clone of %gep
1072 // %new.gep = gep %gep2, <offset / sizeof(*%gep)>
1073 // %gep ; will be removed
1074 // ... %gep ...
1075 //
1076 // => replace all uses of %gep with %new.gep and remove %gep
1077 //
1078 // %gep2 ; clone of %gep
1079 // %new.gep = gep %gep2, <offset / sizeof(*%gep)>
1080 // ... %new.gep ...
1081 //
1082 // If AccumulativeByteOffset is not a multiple of sizeof(*%gep), we emit an
1083 // uglygep (http://llvm.org/docs/GetElementPtr.html#what-s-an-uglygep):
1084 // bitcast %gep2 to i8*, add the offset, and bitcast the result back to the
1085 // type of %gep.
1086 //
1087 // %gep2 ; clone of %gep
1088 // %0 = bitcast %gep2 to i8*
1089 // %uglygep = gep %0, <offset>
1090 // %new.gep = bitcast %uglygep to <type of %gep>
1091 // ... %new.gep ...
1092 Instruction *NewGEP = GEP->clone();
1093 NewGEP->insertBefore(GEP);
1094
1095 // Per ANSI C standard, signed / unsigned = unsigned and signed % unsigned =
1096 // unsigned.. Therefore, we cast ElementTypeSizeOfGEP to signed because it is
1097 // used with unsigned integers later.
1098 int64_t ElementTypeSizeOfGEP = static_cast<int64_t>(
1099 DL->getTypeAllocSize(GEP->getResultElementType()));
1100 Type *IntPtrTy = DL->getIntPtrType(GEP->getType());
1101 if (AccumulativeByteOffset % ElementTypeSizeOfGEP == 0) {
1102 // Very likely. As long as %gep is naturally aligned, the byte offset we
1103 // extracted should be a multiple of sizeof(*%gep).
1104 int64_t Index = AccumulativeByteOffset / ElementTypeSizeOfGEP;
1105 NewGEP = GetElementPtrInst::Create(GEP->getResultElementType(), NewGEP,
1106 ConstantInt::get(IntPtrTy, Index, true),
1107 GEP->getName(), GEP);
1108 NewGEP->copyMetadata(*GEP);
1109 // Inherit the inbounds attribute of the original GEP.
1110 cast<GetElementPtrInst>(NewGEP)->setIsInBounds(GEPWasInBounds);
1111 } else {
1112 // Unlikely but possible. For example,
1113 // #pragma pack(1)
1114 // struct S {
1115 // int a[3];
1116 // int64 b[8];
1117 // };
1118 // #pragma pack()
1119 //
1120 // Suppose the gep before extraction is &s[i + 1].b[j + 3]. After
1121 // extraction, it becomes &s[i].b[j] and AccumulativeByteOffset is
1122 // sizeof(S) + 3 * sizeof(int64) = 100, which is not a multiple of
1123 // sizeof(int64).
1124 //
1125 // Emit an uglygep in this case.
1126 Type *I8PtrTy = Type::getInt8PtrTy(GEP->getContext(),
1127 GEP->getPointerAddressSpace());
1128 NewGEP = new BitCastInst(NewGEP, I8PtrTy, "", GEP);
1129 NewGEP = GetElementPtrInst::Create(
1130 Type::getInt8Ty(GEP->getContext()), NewGEP,
1131 ConstantInt::get(IntPtrTy, AccumulativeByteOffset, true), "uglygep",
1132 GEP);
1133 NewGEP->copyMetadata(*GEP);
1134 // Inherit the inbounds attribute of the original GEP.
1135 cast<GetElementPtrInst>(NewGEP)->setIsInBounds(GEPWasInBounds);
1136 if (GEP->getType() != I8PtrTy)
1137 NewGEP = new BitCastInst(NewGEP, GEP->getType(), GEP->getName(), GEP);
1138 }
1139
1140 GEP->replaceAllUsesWith(NewGEP);
1141 GEP->eraseFromParent();
1142
1143 return true;
1144}
1145
1146bool SeparateConstOffsetFromGEPLegacyPass::runOnFunction(Function &F) {
1147 if (skipFunction(F))
1148 return false;
1149 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1150 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1151 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1152 auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1153 auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
1154 return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
1155 };
1156 SeparateConstOffsetFromGEP Impl(DT, SE, LI, TLI, GetTTI, LowerGEP);
1157 return Impl.run(F);
1158}
1159
1160bool SeparateConstOffsetFromGEP::run(Function &F) {
1161 if (DisableSeparateConstOffsetFromGEP)
2
Assuming the condition is false
3
Taking false branch
1162 return false;
1163
1164 DL = &F.getParent()->getDataLayout();
1165 bool Changed = false;
1166 for (BasicBlock &B : F) {
1167 for (BasicBlock::iterator I = B.begin(), IE = B.end(); I != IE;)
4
Loop condition is true. Entering loop body
1168 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I++))
5
Assuming 'GEP' is non-null
6
Taking true branch
1169 Changed |= splitGEP(GEP);
7
Calling 'SeparateConstOffsetFromGEP::splitGEP'
1170 // No need to split GEP ConstantExprs because all its indices are constant
1171 // already.
1172 }
1173
1174 Changed |= reuniteExts(F);
1175
1176 if (VerifyNoDeadCode)
1177 verifyNoDeadCode(F);
1178
1179 return Changed;
1180}
1181
1182Instruction *SeparateConstOffsetFromGEP::findClosestMatchingDominator(
1183 const SCEV *Key, Instruction *Dominatee,
1184 DenseMap<const SCEV *, SmallVector<Instruction *, 2>> &DominatingExprs) {
1185 auto Pos = DominatingExprs.find(Key);
1186 if (Pos == DominatingExprs.end())
1187 return nullptr;
1188
1189 auto &Candidates = Pos->second;
1190 // Because we process the basic blocks in pre-order of the dominator tree, a
1191 // candidate that doesn't dominate the current instruction won't dominate any
1192 // future instruction either. Therefore, we pop it out of the stack. This
1193 // optimization makes the algorithm O(n).
1194 while (!Candidates.empty()) {
1195 Instruction *Candidate = Candidates.back();
1196 if (DT->dominates(Candidate, Dominatee))
1197 return Candidate;
1198 Candidates.pop_back();
1199 }
1200 return nullptr;
1201}
1202
1203bool SeparateConstOffsetFromGEP::reuniteExts(Instruction *I) {
1204 if (!SE->isSCEVable(I->getType()))
1205 return false;
1206
1207 // Dom: LHS+RHS
1208 // I: sext(LHS)+sext(RHS)
1209 // If Dom can't sign overflow and Dom dominates I, optimize I to sext(Dom).
1210 // TODO: handle zext
1211 Value *LHS = nullptr, *RHS = nullptr;
1212 if (match(I, m_Add(m_SExt(m_Value(LHS)), m_SExt(m_Value(RHS))))) {
1213 if (LHS->getType() == RHS->getType()) {
1214 const SCEV *Key =
1215 SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS));
1216 if (auto *Dom = findClosestMatchingDominator(Key, I, DominatingAdds)) {
1217 Instruction *NewSExt = new SExtInst(Dom, I->getType(), "", I);
1218 NewSExt->takeName(I);
1219 I->replaceAllUsesWith(NewSExt);
1220 RecursivelyDeleteTriviallyDeadInstructions(I);
1221 return true;
1222 }
1223 }
1224 } else if (match(I, m_Sub(m_SExt(m_Value(LHS)), m_SExt(m_Value(RHS))))) {
1225 if (LHS->getType() == RHS->getType()) {
1226 const SCEV *Key =
1227 SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS));
1228 if (auto *Dom = findClosestMatchingDominator(Key, I, DominatingSubs)) {
1229 Instruction *NewSExt = new SExtInst(Dom, I->getType(), "", I);
1230 NewSExt->takeName(I);
1231 I->replaceAllUsesWith(NewSExt);
1232 RecursivelyDeleteTriviallyDeadInstructions(I);
1233 return true;
1234 }
1235 }
1236 }
1237
1238 // Add I to DominatingExprs if it's an add/sub that can't sign overflow.
1239 if (match(I, m_NSWAdd(m_Value(LHS), m_Value(RHS)))) {
1240 if (programUndefinedIfPoison(I)) {
1241 const SCEV *Key =
1242 SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS));
1243 DominatingAdds[Key].push_back(I);
1244 }
1245 } else if (match(I, m_NSWSub(m_Value(LHS), m_Value(RHS)))) {
1246 if (programUndefinedIfPoison(I)) {
1247 const SCEV *Key =
1248 SE->getAddExpr(SE->getUnknown(LHS), SE->getUnknown(RHS));
1249 DominatingSubs[Key].push_back(I);
1250 }
1251 }
1252 return false;
1253}
1254
1255bool SeparateConstOffsetFromGEP::reuniteExts(Function &F) {
1256 bool Changed = false;
1257 DominatingAdds.clear();
1258 DominatingSubs.clear();
1259 for (const auto Node : depth_first(DT)) {
1260 BasicBlock *BB = Node->getBlock();
1261 for (auto I = BB->begin(); I != BB->end(); ) {
1262 Instruction *Cur = &*I++;
1263 Changed |= reuniteExts(Cur);
1264 }
1265 }
1266 return Changed;
1267}
1268
1269void SeparateConstOffsetFromGEP::verifyNoDeadCode(Function &F) {
1270 for (BasicBlock &B : F) {
1271 for (Instruction &I : B) {
1272 if (isInstructionTriviallyDead(&I)) {
1273 std::string ErrMessage;
1274 raw_string_ostream RSO(ErrMessage);
1275 RSO << "Dead instruction detected!\n" << I << "\n";
1276 llvm_unreachable(RSO.str().c_str())::llvm::llvm_unreachable_internal(RSO.str().c_str(), "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp"
, 1276)
;
1277 }
1278 }
1279 }
1280}
1281
1282bool SeparateConstOffsetFromGEP::isLegalToSwapOperand(
1283 GetElementPtrInst *FirstGEP, GetElementPtrInst *SecondGEP, Loop *CurLoop) {
1284 if (!FirstGEP || !FirstGEP->hasOneUse())
1285 return false;
1286
1287 if (!SecondGEP || FirstGEP->getParent() != SecondGEP->getParent())
1288 return false;
1289
1290 if (FirstGEP == SecondGEP)
1291 return false;
1292
1293 unsigned FirstNum = FirstGEP->getNumOperands();
1294 unsigned SecondNum = SecondGEP->getNumOperands();
1295 // Give up if the number of operands are not 2.
1296 if (FirstNum != SecondNum || FirstNum != 2)
1297 return false;
1298
1299 Value *FirstBase = FirstGEP->getOperand(0);
1300 Value *SecondBase = SecondGEP->getOperand(0);
1301 Value *FirstOffset = FirstGEP->getOperand(1);
1302 // Give up if the index of the first GEP is loop invariant.
1303 if (CurLoop->isLoopInvariant(FirstOffset))
1304 return false;
1305
1306 // Give up if base doesn't have same type.
1307 if (FirstBase->getType() != SecondBase->getType())
1308 return false;
1309
1310 Instruction *FirstOffsetDef = dyn_cast<Instruction>(FirstOffset);
1311
1312 // Check if the second operand of first GEP has constant coefficient.
1313 // For an example, for the following code, we won't gain anything by
1314 // hoisting the second GEP out because the second GEP can be folded away.
1315 // %scevgep.sum.ur159 = add i64 %idxprom48.ur, 256
1316 // %67 = shl i64 %scevgep.sum.ur159, 2
1317 // %uglygep160 = getelementptr i8* %65, i64 %67
1318 // %uglygep161 = getelementptr i8* %uglygep160, i64 -1024
1319
1320 // Skip constant shift instruction which may be generated by Splitting GEPs.
1321 if (FirstOffsetDef && FirstOffsetDef->isShift() &&
1322 isa<ConstantInt>(FirstOffsetDef->getOperand(1)))
1323 FirstOffsetDef = dyn_cast<Instruction>(FirstOffsetDef->getOperand(0));
1324
1325 // Give up if FirstOffsetDef is an Add or Sub with constant.
1326 // Because it may not profitable at all due to constant folding.
1327 if (FirstOffsetDef)
1328 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FirstOffsetDef)) {
1329 unsigned opc = BO->getOpcode();
1330 if ((opc == Instruction::Add || opc == Instruction::Sub) &&
1331 (isa<ConstantInt>(BO->getOperand(0)) ||
1332 isa<ConstantInt>(BO->getOperand(1))))
1333 return false;
1334 }
1335 return true;
1336}
1337
1338bool SeparateConstOffsetFromGEP::hasMoreThanOneUseInLoop(Value *V, Loop *L) {
1339 int UsesInLoop = 0;
1340 for (User *U : V->users()) {
1341 if (Instruction *User = dyn_cast<Instruction>(U))
1342 if (L->contains(User))
1343 if (++UsesInLoop > 1)
1344 return true;
1345 }
1346 return false;
1347}
1348
1349void SeparateConstOffsetFromGEP::swapGEPOperand(GetElementPtrInst *First,
1350 GetElementPtrInst *Second) {
1351 Value *Offset1 = First->getOperand(1);
1352 Value *Offset2 = Second->getOperand(1);
1353 First->setOperand(1, Offset2);
1354 Second->setOperand(1, Offset1);
1355
1356 // We changed p+o+c to p+c+o, p+c may not be inbound anymore.
1357 const DataLayout &DAL = First->getModule()->getDataLayout();
1358 APInt Offset(DAL.getIndexSizeInBits(
1359 cast<PointerType>(First->getType())->getAddressSpace()),
1360 0);
1361 Value *NewBase =
1362 First->stripAndAccumulateInBoundsConstantOffsets(DAL, Offset);
1363 uint64_t ObjectSize;
1364 if (!getObjectSize(NewBase, ObjectSize, DAL, TLI) ||
1365 Offset.ugt(ObjectSize)) {
1366 First->setIsInBounds(false);
1367 Second->setIsInBounds(false);
1368 } else
1369 First->setIsInBounds(true);
1370}
1371
1372PreservedAnalyses
1373SeparateConstOffsetFromGEPPass::run(Function &F, FunctionAnalysisManager &AM) {
1374 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
1375 auto *SE = &AM.getResult<ScalarEvolutionAnalysis>(F);
1376 auto *LI = &AM.getResult<LoopAnalysis>(F);
1377 auto *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
1378 auto GetTTI = [&AM](Function &F) -> TargetTransformInfo & {
1379 return AM.getResult<TargetIRAnalysis>(F);
1380 };
1381 SeparateConstOffsetFromGEP Impl(DT, SE, LI, TLI, GetTTI, LowerGEP);
1382 if (!Impl.run(F))
1
Calling 'SeparateConstOffsetFromGEP::run'
1383 return PreservedAnalyses::all();
1384 PreservedAnalyses PA;
1385 PA.preserveSet<CFGAnalyses>();
1386 return PA;
1387}

/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h

1//===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the IRBuilder class, which is used as a convenient way
10// to create LLVM instructions with a consistent and simplified interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_IR_IRBUILDER_H
15#define LLVM_IR_IRBUILDER_H
16
17#include "llvm-c/Types.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/None.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/IR/BasicBlock.h"
23#include "llvm/IR/Constant.h"
24#include "llvm/IR/ConstantFolder.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DebugLoc.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/GlobalVariable.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/Instruction.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/LLVMContext.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/Operator.h"
38#include "llvm/IR/Type.h"
39#include "llvm/IR/Value.h"
40#include "llvm/IR/ValueHandle.h"
41#include "llvm/Support/AtomicOrdering.h"
42#include "llvm/Support/CBindingWrapping.h"
43#include "llvm/Support/Casting.h"
44#include <cassert>
45#include <cstddef>
46#include <cstdint>
47#include <functional>
48#include <utility>
49
50namespace llvm {
51
52class APInt;
53class MDNode;
54class Use;
55
56/// This provides the default implementation of the IRBuilder
57/// 'InsertHelper' method that is called whenever an instruction is created by
58/// IRBuilder and needs to be inserted.
59///
60/// By default, this inserts the instruction at the insertion point.
61class IRBuilderDefaultInserter {
62public:
63 virtual ~IRBuilderDefaultInserter();
64
65 virtual void InsertHelper(Instruction *I, const Twine &Name,
66 BasicBlock *BB,
67 BasicBlock::iterator InsertPt) const {
68 if (BB) BB->getInstList().insert(InsertPt, I);
69 I->setName(Name);
70 }
71};
72
73/// Provides an 'InsertHelper' that calls a user-provided callback after
74/// performing the default insertion.
75class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
76 std::function<void(Instruction *)> Callback;
77
78public:
79 virtual ~IRBuilderCallbackInserter();
80
81 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
82 : Callback(std::move(Callback)) {}
83
84 void InsertHelper(Instruction *I, const Twine &Name,
85 BasicBlock *BB,
86 BasicBlock::iterator InsertPt) const override {
87 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
88 Callback(I);
89 }
90};
91
92/// Common base class shared among various IRBuilders.
93class IRBuilderBase {
94 DebugLoc CurDbgLocation;
95
96protected:
97 BasicBlock *BB;
98 BasicBlock::iterator InsertPt;
99 LLVMContext &Context;
100 const IRBuilderFolder &Folder;
101 const IRBuilderDefaultInserter &Inserter;
102
103 MDNode *DefaultFPMathTag;
104 FastMathFlags FMF;
105
106 bool IsFPConstrained;
107 fp::ExceptionBehavior DefaultConstrainedExcept;
108 RoundingMode DefaultConstrainedRounding;
109
110 ArrayRef<OperandBundleDef> DefaultOperandBundles;
111
112public:
113 IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
114 const IRBuilderDefaultInserter &Inserter,
115 MDNode *FPMathTag, ArrayRef<OperandBundleDef> OpBundles)
116 : Context(context), Folder(Folder), Inserter(Inserter),
117 DefaultFPMathTag(FPMathTag), IsFPConstrained(false),
118 DefaultConstrainedExcept(fp::ebStrict),
119 DefaultConstrainedRounding(RoundingMode::Dynamic),
120 DefaultOperandBundles(OpBundles) {
121 ClearInsertionPoint();
122 }
123
124 /// Insert and return the specified instruction.
125 template<typename InstTy>
126 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
127 Inserter.InsertHelper(I, Name, BB, InsertPt);
128 SetInstDebugLocation(I);
129 return I;
130 }
131
132 /// No-op overload to handle constants.
133 Constant *Insert(Constant *C, const Twine& = "") const {
134 return C;
135 }
136
137 Value *Insert(Value *V, const Twine &Name = "") const {
138 if (Instruction *I = dyn_cast<Instruction>(V))
139 return Insert(I, Name);
140 assert(isa<Constant>(V))((isa<Constant>(V)) ? static_cast<void> (0) : __assert_fail
("isa<Constant>(V)", "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 140, __PRETTY_FUNCTION__))
;
141 return V;
142 }
143
144 //===--------------------------------------------------------------------===//
145 // Builder configuration methods
146 //===--------------------------------------------------------------------===//
147
148 /// Clear the insertion point: created instructions will not be
149 /// inserted into a block.
150 void ClearInsertionPoint() {
151 BB = nullptr;
152 InsertPt = BasicBlock::iterator();
153 }
154
155 BasicBlock *GetInsertBlock() const { return BB; }
156 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
157 LLVMContext &getContext() const { return Context; }
158
159 /// This specifies that created instructions should be appended to the
160 /// end of the specified block.
161 void SetInsertPoint(BasicBlock *TheBB) {
162 BB = TheBB;
163 InsertPt = BB->end();
164 }
165
166 /// This specifies that created instructions should be inserted before
167 /// the specified instruction.
168 void SetInsertPoint(Instruction *I) {
169 BB = I->getParent();
170 InsertPt = I->getIterator();
171 assert(InsertPt != BB->end() && "Can't read debug loc from end()")((InsertPt != BB->end() && "Can't read debug loc from end()"
) ? static_cast<void> (0) : __assert_fail ("InsertPt != BB->end() && \"Can't read debug loc from end()\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 171, __PRETTY_FUNCTION__))
;
172 SetCurrentDebugLocation(I->getDebugLoc());
173 }
174
175 /// This specifies that created instructions should be inserted at the
176 /// specified point.
177 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
178 BB = TheBB;
179 InsertPt = IP;
180 if (IP != TheBB->end())
181 SetCurrentDebugLocation(IP->getDebugLoc());
182 }
183
184 /// Set location information used by debugging information.
185 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
186
187 /// Get location information used by debugging information.
188 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
189
190 /// If this builder has a current debug location, set it on the
191 /// specified instruction.
192 void SetInstDebugLocation(Instruction *I) const {
193 if (CurDbgLocation)
194 I->setDebugLoc(CurDbgLocation);
195 }
196
197 /// Get the return type of the current function that we're emitting
198 /// into.
199 Type *getCurrentFunctionReturnType() const;
200
201 /// InsertPoint - A saved insertion point.
202 class InsertPoint {
203 BasicBlock *Block = nullptr;
204 BasicBlock::iterator Point;
205
206 public:
207 /// Creates a new insertion point which doesn't point to anything.
208 InsertPoint() = default;
209
210 /// Creates a new insertion point at the given location.
211 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
212 : Block(InsertBlock), Point(InsertPoint) {}
213
214 /// Returns true if this insert point is set.
215 bool isSet() const { return (Block != nullptr); }
216
217 BasicBlock *getBlock() const { return Block; }
218 BasicBlock::iterator getPoint() const { return Point; }
219 };
220
221 /// Returns the current insert point.
222 InsertPoint saveIP() const {
223 return InsertPoint(GetInsertBlock(), GetInsertPoint());
224 }
225
226 /// Returns the current insert point, clearing it in the process.
227 InsertPoint saveAndClearIP() {
228 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
229 ClearInsertionPoint();
230 return IP;
231 }
232
233 /// Sets the current insert point to a previously-saved location.
234 void restoreIP(InsertPoint IP) {
235 if (IP.isSet())
236 SetInsertPoint(IP.getBlock(), IP.getPoint());
237 else
238 ClearInsertionPoint();
239 }
240
241 /// Get the floating point math metadata being used.
242 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
243
244 /// Get the flags to be applied to created floating point ops
245 FastMathFlags getFastMathFlags() const { return FMF; }
246
247 FastMathFlags &getFastMathFlags() { return FMF; }
248
249 /// Clear the fast-math flags.
250 void clearFastMathFlags() { FMF.clear(); }
251
252 /// Set the floating point math metadata to be used.
253 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
254
255 /// Set the fast-math flags to be used with generated fp-math operators
256 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
257
258 /// Enable/Disable use of constrained floating point math. When
259 /// enabled the CreateF<op>() calls instead create constrained
260 /// floating point intrinsic calls. Fast math flags are unaffected
261 /// by this setting.
262 void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
263
264 /// Query for the use of constrained floating point math
265 bool getIsFPConstrained() { return IsFPConstrained; }
266
267 /// Set the exception handling to be used with constrained floating point
268 void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
269#ifndef NDEBUG
270 Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(NewExcept);
271 assert(ExceptStr.hasValue() && "Garbage strict exception behavior!")((ExceptStr.hasValue() && "Garbage strict exception behavior!"
) ? static_cast<void> (0) : __assert_fail ("ExceptStr.hasValue() && \"Garbage strict exception behavior!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 271, __PRETTY_FUNCTION__))
;
272#endif
273 DefaultConstrainedExcept = NewExcept;
274 }
275
276 /// Set the rounding mode handling to be used with constrained floating point
277 void setDefaultConstrainedRounding(RoundingMode NewRounding) {
278#ifndef NDEBUG
279 Optional<StringRef> RoundingStr = RoundingModeToStr(NewRounding);
280 assert(RoundingStr.hasValue() && "Garbage strict rounding mode!")((RoundingStr.hasValue() && "Garbage strict rounding mode!"
) ? static_cast<void> (0) : __assert_fail ("RoundingStr.hasValue() && \"Garbage strict rounding mode!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 280, __PRETTY_FUNCTION__))
;
281#endif
282 DefaultConstrainedRounding = NewRounding;
283 }
284
285 /// Get the exception handling used with constrained floating point
286 fp::ExceptionBehavior getDefaultConstrainedExcept() {
287 return DefaultConstrainedExcept;
288 }
289
290 /// Get the rounding mode handling used with constrained floating point
291 RoundingMode getDefaultConstrainedRounding() {
292 return DefaultConstrainedRounding;
293 }
294
295 void setConstrainedFPFunctionAttr() {
296 assert(BB && "Must have a basic block to set any function attributes!")((BB && "Must have a basic block to set any function attributes!"
) ? static_cast<void> (0) : __assert_fail ("BB && \"Must have a basic block to set any function attributes!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 296, __PRETTY_FUNCTION__))
;
297
298 Function *F = BB->getParent();
299 if (!F->hasFnAttribute(Attribute::StrictFP)) {
300 F->addFnAttr(Attribute::StrictFP);
301 }
302 }
303
304 void setConstrainedFPCallAttr(CallInst *I) {
305 I->addAttribute(AttributeList::FunctionIndex, Attribute::StrictFP);
306 }
307
308 void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) {
309 DefaultOperandBundles = OpBundles;
310 }
311
312 //===--------------------------------------------------------------------===//
313 // RAII helpers.
314 //===--------------------------------------------------------------------===//
315
316 // RAII object that stores the current insertion point and restores it
317 // when the object is destroyed. This includes the debug location.
318 class InsertPointGuard {
319 IRBuilderBase &Builder;
320 AssertingVH<BasicBlock> Block;
321 BasicBlock::iterator Point;
322 DebugLoc DbgLoc;
323
324 public:
325 InsertPointGuard(IRBuilderBase &B)
326 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
327 DbgLoc(B.getCurrentDebugLocation()) {}
328
329 InsertPointGuard(const InsertPointGuard &) = delete;
330 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
331
332 ~InsertPointGuard() {
333 Builder.restoreIP(InsertPoint(Block, Point));
334 Builder.SetCurrentDebugLocation(DbgLoc);
335 }
336 };
337
338 // RAII object that stores the current fast math settings and restores
339 // them when the object is destroyed.
340 class FastMathFlagGuard {
341 IRBuilderBase &Builder;
342 FastMathFlags FMF;
343 MDNode *FPMathTag;
344 bool IsFPConstrained;
345 fp::ExceptionBehavior DefaultConstrainedExcept;
346 RoundingMode DefaultConstrainedRounding;
347
348 public:
349 FastMathFlagGuard(IRBuilderBase &B)
350 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
351 IsFPConstrained(B.IsFPConstrained),
352 DefaultConstrainedExcept(B.DefaultConstrainedExcept),
353 DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}
354
355 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
356 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
357
358 ~FastMathFlagGuard() {
359 Builder.FMF = FMF;
360 Builder.DefaultFPMathTag = FPMathTag;
361 Builder.IsFPConstrained = IsFPConstrained;
362 Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
363 Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
364 }
365 };
366
367 // RAII object that stores the current default operand bundles and restores
368 // them when the object is destroyed.
369 class OperandBundlesGuard {
370 IRBuilderBase &Builder;
371 ArrayRef<OperandBundleDef> DefaultOperandBundles;
372
373 public:
374 OperandBundlesGuard(IRBuilderBase &B)
375 : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {}
376
377 OperandBundlesGuard(const OperandBundlesGuard &) = delete;
378 OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete;
379
380 ~OperandBundlesGuard() {
381 Builder.DefaultOperandBundles = DefaultOperandBundles;
382 }
383 };
384
385
386 //===--------------------------------------------------------------------===//
387 // Miscellaneous creation methods.
388 //===--------------------------------------------------------------------===//
389
390 /// Make a new global variable with initializer type i8*
391 ///
392 /// Make a new global variable with an initializer that has array of i8 type
393 /// filled in with the null terminated string value specified. The new global
394 /// variable will be marked mergable with any others of the same contents. If
395 /// Name is specified, it is the name of the global variable created.
396 ///
397 /// If no module is given via \p M, it is take from the insertion point basic
398 /// block.
399 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
400 unsigned AddressSpace = 0,
401 Module *M = nullptr);
402
403 /// Get a constant value representing either true or false.
404 ConstantInt *getInt1(bool V) {
405 return ConstantInt::get(getInt1Ty(), V);
406 }
407
408 /// Get the constant value for i1 true.
409 ConstantInt *getTrue() {
410 return ConstantInt::getTrue(Context);
411 }
412
413 /// Get the constant value for i1 false.
414 ConstantInt *getFalse() {
415 return ConstantInt::getFalse(Context);
416 }
417
418 /// Get a constant 8-bit value.
419 ConstantInt *getInt8(uint8_t C) {
420 return ConstantInt::get(getInt8Ty(), C);
421 }
422
423 /// Get a constant 16-bit value.
424 ConstantInt *getInt16(uint16_t C) {
425 return ConstantInt::get(getInt16Ty(), C);
426 }
427
428 /// Get a constant 32-bit value.
429 ConstantInt *getInt32(uint32_t C) {
430 return ConstantInt::get(getInt32Ty(), C);
431 }
432
433 /// Get a constant 64-bit value.
434 ConstantInt *getInt64(uint64_t C) {
435 return ConstantInt::get(getInt64Ty(), C);
436 }
437
438 /// Get a constant N-bit value, zero extended or truncated from
439 /// a 64-bit value.
440 ConstantInt *getIntN(unsigned N, uint64_t C) {
441 return ConstantInt::get(getIntNTy(N), C);
442 }
443
444 /// Get a constant integer value.
445 ConstantInt *getInt(const APInt &AI) {
446 return ConstantInt::get(Context, AI);
447 }
448
449 //===--------------------------------------------------------------------===//
450 // Type creation methods
451 //===--------------------------------------------------------------------===//
452
453 /// Fetch the type representing a single bit
454 IntegerType *getInt1Ty() {
455 return Type::getInt1Ty(Context);
456 }
457
458 /// Fetch the type representing an 8-bit integer.
459 IntegerType *getInt8Ty() {
460 return Type::getInt8Ty(Context);
461 }
462
463 /// Fetch the type representing a 16-bit integer.
464 IntegerType *getInt16Ty() {
465 return Type::getInt16Ty(Context);
466 }
467
468 /// Fetch the type representing a 32-bit integer.
469 IntegerType *getInt32Ty() {
470 return Type::getInt32Ty(Context);
471 }
472
473 /// Fetch the type representing a 64-bit integer.
474 IntegerType *getInt64Ty() {
475 return Type::getInt64Ty(Context);
476 }
477
478 /// Fetch the type representing a 128-bit integer.
479 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
480
481 /// Fetch the type representing an N-bit integer.
482 IntegerType *getIntNTy(unsigned N) {
483 return Type::getIntNTy(Context, N);
484 }
485
486 /// Fetch the type representing a 16-bit floating point value.
487 Type *getHalfTy() {
488 return Type::getHalfTy(Context);
489 }
490
491 /// Fetch the type representing a 16-bit brain floating point value.
492 Type *getBFloatTy() {
493 return Type::getBFloatTy(Context);
494 }
495
496 /// Fetch the type representing a 32-bit floating point value.
497 Type *getFloatTy() {
498 return Type::getFloatTy(Context);
499 }
500
501 /// Fetch the type representing a 64-bit floating point value.
502 Type *getDoubleTy() {
503 return Type::getDoubleTy(Context);
504 }
505
506 /// Fetch the type representing void.
507 Type *getVoidTy() {
508 return Type::getVoidTy(Context);
509 }
510
511 /// Fetch the type representing a pointer to an 8-bit integer value.
512 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
513 return Type::getInt8PtrTy(Context, AddrSpace);
514 }
515
516 /// Fetch the type representing a pointer to an integer value.
517 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
518 return DL.getIntPtrType(Context, AddrSpace);
519 }
520
521 //===--------------------------------------------------------------------===//
522 // Intrinsic creation methods
523 //===--------------------------------------------------------------------===//
524
525 /// Create and insert a memset to the specified pointer and the
526 /// specified value.
527 ///
528 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
529 /// specified, it will be added to the instruction. Likewise with alias.scope
530 /// and noalias tags.
531 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
532 MaybeAlign Align, bool isVolatile = false,
533 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
534 MDNode *NoAliasTag = nullptr) {
535 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
536 TBAATag, ScopeTag, NoAliasTag);
537 }
538
539 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
540 bool isVolatile = false, MDNode *TBAATag = nullptr,
541 MDNode *ScopeTag = nullptr,
542 MDNode *NoAliasTag = nullptr);
543
544 /// Create and insert an element unordered-atomic memset of the region of
545 /// memory starting at the given pointer to the given value.
546 ///
547 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
548 /// specified, it will be added to the instruction. Likewise with alias.scope
549 /// and noalias tags.
550 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
551 uint64_t Size, Align Alignment,
552 uint32_t ElementSize,
553 MDNode *TBAATag = nullptr,
554 MDNode *ScopeTag = nullptr,
555 MDNode *NoAliasTag = nullptr) {
556 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
557 Align(Alignment), ElementSize,
558 TBAATag, ScopeTag, NoAliasTag);
559 }
560
561 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
562 Value *Size, Align Alignment,
563 uint32_t ElementSize,
564 MDNode *TBAATag = nullptr,
565 MDNode *ScopeTag = nullptr,
566 MDNode *NoAliasTag = nullptr);
567
568 /// Create and insert a memcpy between the specified pointers.
569 ///
570 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
571 /// specified, it will be added to the instruction. Likewise with alias.scope
572 /// and noalias tags.
573 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
574 MaybeAlign SrcAlign, uint64_t Size,
575 bool isVolatile = false, MDNode *TBAATag = nullptr,
576 MDNode *TBAAStructTag = nullptr,
577 MDNode *ScopeTag = nullptr,
578 MDNode *NoAliasTag = nullptr) {
579 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
580 isVolatile, TBAATag, TBAAStructTag, ScopeTag,
581 NoAliasTag);
582 }
583
584 CallInst *CreateMemTransferInst(
585 Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
586 MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
587 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
588 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
589
590 CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
591 MaybeAlign SrcAlign, Value *Size,
592 bool isVolatile = false, MDNode *TBAATag = nullptr,
593 MDNode *TBAAStructTag = nullptr,
594 MDNode *ScopeTag = nullptr,
595 MDNode *NoAliasTag = nullptr) {
596 return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src,
597 SrcAlign, Size, isVolatile, TBAATag,
598 TBAAStructTag, ScopeTag, NoAliasTag);
599 }
600
601 CallInst *CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
602 MaybeAlign SrcAlign, Value *Size);
603
604 /// Create and insert an element unordered-atomic memcpy between the
605 /// specified pointers.
606 ///
607 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
608 ///
609 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
610 /// specified, it will be added to the instruction. Likewise with alias.scope
611 /// and noalias tags.
612 CallInst *CreateElementUnorderedAtomicMemCpy(
613 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
614 uint32_t ElementSize, MDNode *TBAATag = nullptr,
615 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
616 MDNode *NoAliasTag = nullptr);
617
618 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemCpy(CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
619 Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
620 unsigned SrcAlign, uint64_t Size,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
621 uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
622 MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
623 MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
624 MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
625 "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
{
626 return CreateElementUnorderedAtomicMemCpy(
627 Dst, Align(DstAlign), Src, Align(SrcAlign), getInt64(Size), ElementSize,
628 TBAATag, TBAAStructTag, ScopeTag, NoAliasTag);
629 }
630
631 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemCpy(CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
632 Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
633 unsigned SrcAlign, Value *Size,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
634 uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
635 MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
636 MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
637 MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
638 "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
{
639 return CreateElementUnorderedAtomicMemCpy(
640 Dst, Align(DstAlign), Src, Align(SrcAlign), Size, ElementSize, TBAATag,
641 TBAAStructTag, ScopeTag, NoAliasTag);
642 }
643
644 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
645 MaybeAlign SrcAlign, uint64_t Size,
646 bool isVolatile = false, MDNode *TBAATag = nullptr,
647 MDNode *ScopeTag = nullptr,
648 MDNode *NoAliasTag = nullptr) {
649 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
650 isVolatile, TBAATag, ScopeTag, NoAliasTag);
651 }
652
653 CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
654 MaybeAlign SrcAlign, Value *Size,
655 bool isVolatile = false, MDNode *TBAATag = nullptr,
656 MDNode *ScopeTag = nullptr,
657 MDNode *NoAliasTag = nullptr);
658
659 /// \brief Create and insert an element unordered-atomic memmove between the
660 /// specified pointers.
661 ///
662 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
663 /// respectively.
664 ///
665 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
666 /// specified, it will be added to the instruction. Likewise with alias.scope
667 /// and noalias tags.
668 CallInst *CreateElementUnorderedAtomicMemMove(
669 Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
670 uint32_t ElementSize, MDNode *TBAATag = nullptr,
671 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
672 MDNode *NoAliasTag = nullptr);
673
674 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemMove(CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
675 Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
676 unsigned SrcAlign, uint64_t Size,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
677 uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
678 MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
679 MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
680 MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
681 "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
{
682 return CreateElementUnorderedAtomicMemMove(
683 Dst, Align(DstAlign), Src, Align(SrcAlign), getInt64(Size), ElementSize,
684 TBAATag, TBAAStructTag, ScopeTag, NoAliasTag);
685 }
686
687 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemMove(CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
688 Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
689 unsigned SrcAlign, Value *Size,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
690 uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
691 MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
692 MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
693 MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
694 "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned
DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t
ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag
= nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr
) __attribute__((deprecated("Use the version that takes Align instead"
)))
{
695 return CreateElementUnorderedAtomicMemMove(
696 Dst, Align(DstAlign), Src, Align(SrcAlign), Size, ElementSize, TBAATag,
697 TBAAStructTag, ScopeTag, NoAliasTag);
698 }
699
700 /// Create a vector fadd reduction intrinsic of the source vector.
701 /// The first parameter is a scalar accumulator value for ordered reductions.
702 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
703
704 /// Create a vector fmul reduction intrinsic of the source vector.
705 /// The first parameter is a scalar accumulator value for ordered reductions.
706 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
707
708 /// Create a vector int add reduction intrinsic of the source vector.
709 CallInst *CreateAddReduce(Value *Src);
710
711 /// Create a vector int mul reduction intrinsic of the source vector.
712 CallInst *CreateMulReduce(Value *Src);
713
714 /// Create a vector int AND reduction intrinsic of the source vector.
715 CallInst *CreateAndReduce(Value *Src);
716
717 /// Create a vector int OR reduction intrinsic of the source vector.
718 CallInst *CreateOrReduce(Value *Src);
719
720 /// Create a vector int XOR reduction intrinsic of the source vector.
721 CallInst *CreateXorReduce(Value *Src);
722
723 /// Create a vector integer max reduction intrinsic of the source
724 /// vector.
725 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
726
727 /// Create a vector integer min reduction intrinsic of the source
728 /// vector.
729 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
730
731 /// Create a vector float max reduction intrinsic of the source
732 /// vector.
733 CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
734
735 /// Create a vector float min reduction intrinsic of the source
736 /// vector.
737 CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
738
739 /// Create a lifetime.start intrinsic.
740 ///
741 /// If the pointer isn't i8* it will be converted.
742 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
743
744 /// Create a lifetime.end intrinsic.
745 ///
746 /// If the pointer isn't i8* it will be converted.
747 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
748
749 /// Create a call to invariant.start intrinsic.
750 ///
751 /// If the pointer isn't i8* it will be converted.
752 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
753
754 /// Create a call to Masked Load intrinsic
755 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value
*Mask, Value *PassThru = nullptr, const Twine &Name = ""
) __attribute__((deprecated("Use the version that takes Align instead"
)))
756 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask,CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value
*Mask, Value *PassThru = nullptr, const Twine &Name = ""
) __attribute__((deprecated("Use the version that takes Align instead"
)))
757 Value *PassThru = nullptr,CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value
*Mask, Value *PassThru = nullptr, const Twine &Name = ""
) __attribute__((deprecated("Use the version that takes Align instead"
)))
758 const Twine &Name = ""),CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value
*Mask, Value *PassThru = nullptr, const Twine &Name = ""
) __attribute__((deprecated("Use the version that takes Align instead"
)))
759 "Use the version that takes Align instead")CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value
*Mask, Value *PassThru = nullptr, const Twine &Name = ""
) __attribute__((deprecated("Use the version that takes Align instead"
)))
{
760 return CreateMaskedLoad(Ptr, assumeAligned(Alignment), Mask, PassThru,
761 Name);
762 }
763 CallInst *CreateMaskedLoad(Value *Ptr, Align Alignment, Value *Mask,
764 Value *PassThru = nullptr, const Twine &Name = "");
765
766 /// Create a call to Masked Store intrinsic
767 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedStore(Value *Val, Value *Ptr,CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment
, Value *Mask) __attribute__((deprecated("Use the version that takes Align instead"
)))
768 unsigned Alignment,CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment
, Value *Mask) __attribute__((deprecated("Use the version that takes Align instead"
)))
769 Value *Mask),CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment
, Value *Mask) __attribute__((deprecated("Use the version that takes Align instead"
)))
770 "Use the version that takes Align instead")CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment
, Value *Mask) __attribute__((deprecated("Use the version that takes Align instead"
)))
{
771 return CreateMaskedStore(Val, Ptr, assumeAligned(Alignment), Mask);
772 }
773
774 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment,
775 Value *Mask);
776
777 /// Create a call to Masked Gather intrinsic
778 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,
Value *Mask = nullptr, Value *PassThru = nullptr, const Twine
&Name = "") __attribute__((deprecated("Use the version that takes Align instead"
)))
779 CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,
Value *Mask = nullptr, Value *PassThru = nullptr, const Twine
&Name = "") __attribute__((deprecated("Use the version that takes Align instead"
)))
780 Value *Mask = nullptr,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,
Value *Mask = nullptr, Value *PassThru = nullptr, const Twine
&Name = "") __attribute__((deprecated("Use the version that takes Align instead"
)))
781 Value *PassThru = nullptr,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,
Value *Mask = nullptr, Value *PassThru = nullptr, const Twine
&Name = "") __attribute__((deprecated("Use the version that takes Align instead"
)))
782 const Twine &Name = ""),CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,
Value *Mask = nullptr, Value *PassThru = nullptr, const Twine
&Name = "") __attribute__((deprecated("Use the version that takes Align instead"
)))
783 "Use the version that takes Align instead")CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,
Value *Mask = nullptr, Value *PassThru = nullptr, const Twine
&Name = "") __attribute__((deprecated("Use the version that takes Align instead"
)))
{
784 return CreateMaskedGather(Ptrs, Align(Alignment), Mask, PassThru, Name);
785 }
786
787 /// Create a call to Masked Gather intrinsic
788 CallInst *CreateMaskedGather(Value *Ptrs, Align Alignment,
789 Value *Mask = nullptr, Value *PassThru = nullptr,
790 const Twine &Name = "");
791
792 /// Create a call to Masked Scatter intrinsic
793 LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned
Alignment, Value *Mask = nullptr) __attribute__((deprecated(
"Use the version that takes Align instead")))
794 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment,CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned
Alignment, Value *Mask = nullptr) __attribute__((deprecated(
"Use the version that takes Align instead")))
795 Value *Mask = nullptr),CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned
Alignment, Value *Mask = nullptr) __attribute__((deprecated(
"Use the version that takes Align instead")))
796 "Use the version that takes Align instead")CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned
Alignment, Value *Mask = nullptr) __attribute__((deprecated(
"Use the version that takes Align instead")))
{
797 return CreateMaskedScatter(Val, Ptrs, Align(Alignment), Mask);
798 }
799
800 /// Create a call to Masked Scatter intrinsic
801 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment,
802 Value *Mask = nullptr);
803
804 /// Create an assume intrinsic call that allows the optimizer to
805 /// assume that the provided condition will be true.
806 ///
807 /// The optional argument \p OpBundles specifies operand bundles that are
808 /// added to the call instruction.
809 CallInst *CreateAssumption(Value *Cond,
810 ArrayRef<OperandBundleDef> OpBundles = llvm::None);
811
812 /// Create a call to the experimental.gc.statepoint intrinsic to
813 /// start a new statepoint sequence.
814 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
815 Value *ActualCallee,
816 ArrayRef<Value *> CallArgs,
817 Optional<ArrayRef<Value *>> DeoptArgs,
818 ArrayRef<Value *> GCArgs,
819 const Twine &Name = "");
820
821 /// Create a call to the experimental.gc.statepoint intrinsic to
822 /// start a new statepoint sequence.
823 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
824 Value *ActualCallee, uint32_t Flags,
825 ArrayRef<Value *> CallArgs,
826 Optional<ArrayRef<Use>> TransitionArgs,
827 Optional<ArrayRef<Use>> DeoptArgs,
828 ArrayRef<Value *> GCArgs,
829 const Twine &Name = "");
830
831 /// Conveninence function for the common case when CallArgs are filled
832 /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
833 /// .get()'ed to get the Value pointer.
834 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
835 Value *ActualCallee, ArrayRef<Use> CallArgs,
836 Optional<ArrayRef<Value *>> DeoptArgs,
837 ArrayRef<Value *> GCArgs,
838 const Twine &Name = "");
839
840 /// Create an invoke to the experimental.gc.statepoint intrinsic to
841 /// start a new statepoint sequence.
842 InvokeInst *
843 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
844 Value *ActualInvokee, BasicBlock *NormalDest,
845 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
846 Optional<ArrayRef<Value *>> DeoptArgs,
847 ArrayRef<Value *> GCArgs, const Twine &Name = "");
848
849 /// Create an invoke to the experimental.gc.statepoint intrinsic to
850 /// start a new statepoint sequence.
851 InvokeInst *CreateGCStatepointInvoke(
852 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
853 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
854 ArrayRef<Value *> InvokeArgs, Optional<ArrayRef<Use>> TransitionArgs,
855 Optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs,
856 const Twine &Name = "");
857
858 // Convenience function for the common case when CallArgs are filled in using
859 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
860 // get the Value *.
861 InvokeInst *
862 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
863 Value *ActualInvokee, BasicBlock *NormalDest,
864 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
865 Optional<ArrayRef<Value *>> DeoptArgs,
866 ArrayRef<Value *> GCArgs, const Twine &Name = "");
867
868 /// Create a call to the experimental.gc.result intrinsic to extract
869 /// the result from a call wrapped in a statepoint.
870 CallInst *CreateGCResult(Instruction *Statepoint,
871 Type *ResultType,
872 const Twine &Name = "");
873
874 /// Create a call to the experimental.gc.relocate intrinsics to
875 /// project the relocated value of one pointer from the statepoint.
876 CallInst *CreateGCRelocate(Instruction *Statepoint,
877 int BaseOffset,
878 int DerivedOffset,
879 Type *ResultType,
880 const Twine &Name = "");
881
882 /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale
883 /// will be the same type as that of \p Scaling.
884 Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
885
886 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
887 /// type.
888 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
889 Instruction *FMFSource = nullptr,
890 const Twine &Name = "");
891
892 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
893 /// first type.
894 CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
895 Instruction *FMFSource = nullptr,
896 const Twine &Name = "");
897
898 /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
899 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
900 /// the intrinsic.
901 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
902 ArrayRef<Value *> Args,
903 Instruction *FMFSource = nullptr,
904 const Twine &Name = "");
905
906 /// Create call to the minnum intrinsic.
907 CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
908 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
909 }
910
911 /// Create call to the maxnum intrinsic.
912 CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
913 return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
914 }
915
916 /// Create call to the minimum intrinsic.
917 CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
918 return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
919 }
920
921 /// Create call to the maximum intrinsic.
922 CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
923 return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
924 }
925
926private:
927 /// Create a call to a masked intrinsic with given Id.
928 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
929 ArrayRef<Type *> OverloadedTypes,
930 const Twine &Name = "");
931
932 Value *getCastedInt8PtrValue(Value *Ptr);
933
934 //===--------------------------------------------------------------------===//
935 // Instruction creation methods: Terminators
936 //===--------------------------------------------------------------------===//
937
938private:
939 /// Helper to add branch weight and unpredictable metadata onto an
940 /// instruction.
941 /// \returns The annotated instruction.
942 template <typename InstTy>
943 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
944 if (Weights)
945 I->setMetadata(LLVMContext::MD_prof, Weights);
946 if (Unpredictable)
947 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
948 return I;
949 }
950
951public:
952 /// Create a 'ret void' instruction.
953 ReturnInst *CreateRetVoid() {
954 return Insert(ReturnInst::Create(Context));
955 }
956
957 /// Create a 'ret <val>' instruction.
958 ReturnInst *CreateRet(Value *V) {
959 return Insert(ReturnInst::Create(Context, V));
960 }
961
962 /// Create a sequence of N insertvalue instructions,
963 /// with one Value from the retVals array each, that build a aggregate
964 /// return value one value at a time, and a ret instruction to return
965 /// the resulting aggregate value.
966 ///
967 /// This is a convenience function for code that uses aggregate return values
968 /// as a vehicle for having multiple return values.
969 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
970 Value *V = UndefValue::get(getCurrentFunctionReturnType());
971 for (unsigned i = 0; i != N; ++i)
972 V = CreateInsertValue(V, retVals[i], i, "mrv");
973 return Insert(ReturnInst::Create(Context, V));
974 }
975
976 /// Create an unconditional 'br label X' instruction.
977 BranchInst *CreateBr(BasicBlock *Dest) {
978 return Insert(BranchInst::Create(Dest));
979 }
980
981 /// Create a conditional 'br Cond, TrueDest, FalseDest'
982 /// instruction.
983 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
984 MDNode *BranchWeights = nullptr,
985 MDNode *Unpredictable = nullptr) {
986 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
987 BranchWeights, Unpredictable));
988 }
989
990 /// Create a conditional 'br Cond, TrueDest, FalseDest'
991 /// instruction. Copy branch meta data if available.
992 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
993 Instruction *MDSrc) {
994 BranchInst *Br = BranchInst::Create(True, False, Cond);
995 if (MDSrc) {
996 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
997 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
998 Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
999 }
1000 return Insert(Br);
1001 }
1002
1003 /// Create a switch instruction with the specified value, default dest,
1004 /// and with a hint for the number of cases that will be added (for efficient
1005 /// allocation).
1006 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1007 MDNode *BranchWeights = nullptr,
1008 MDNode *Unpredictable = nullptr) {
1009 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
1010 BranchWeights, Unpredictable));
1011 }
1012
1013 /// Create an indirect branch instruction with the specified address
1014 /// operand, with an optional hint for the number of destinations that will be
1015 /// added (for efficient allocation).
1016 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
1017 return Insert(IndirectBrInst::Create(Addr, NumDests));
1018 }
1019
1020 /// Create an invoke instruction.
1021 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1022 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1023 ArrayRef<Value *> Args,
1024 ArrayRef<OperandBundleDef> OpBundles,
1025 const Twine &Name = "") {
1026 return Insert(
1027 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles),
1028 Name);
1029 }
1030 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1031 BasicBlock *NormalDest, BasicBlock *UnwindDest,
1032 ArrayRef<Value *> Args = None,
1033 const Twine &Name = "") {
1034 return Insert(InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args),
1035 Name);
1036 }
1037
1038 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1039 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1040 ArrayRef<OperandBundleDef> OpBundles,
1041 const Twine &Name = "") {
1042 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1043 NormalDest, UnwindDest, Args, OpBundles, Name);
1044 }
1045
1046 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1047 BasicBlock *UnwindDest,
1048 ArrayRef<Value *> Args = None,
1049 const Twine &Name = "") {
1050 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1051 NormalDest, UnwindDest, Args, Name);
1052 }
1053
1054 /// \brief Create a callbr instruction.
1055 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1056 BasicBlock *DefaultDest,
1057 ArrayRef<BasicBlock *> IndirectDests,
1058 ArrayRef<Value *> Args = None,
1059 const Twine &Name = "") {
1060 return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
1061 Args), Name);
1062 }
1063 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1064 BasicBlock *DefaultDest,
1065 ArrayRef<BasicBlock *> IndirectDests,
1066 ArrayRef<Value *> Args,
1067 ArrayRef<OperandBundleDef> OpBundles,
1068 const Twine &Name = "") {
1069 return Insert(
1070 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1071 OpBundles), Name);
1072 }
1073
1074 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1075 ArrayRef<BasicBlock *> IndirectDests,
1076 ArrayRef<Value *> Args = None,
1077 const Twine &Name = "") {
1078 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1079 DefaultDest, IndirectDests, Args, Name);
1080 }
1081 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1082 ArrayRef<BasicBlock *> IndirectDests,
1083 ArrayRef<Value *> Args,
1084 ArrayRef<OperandBundleDef> OpBundles,
1085 const Twine &Name = "") {
1086 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1087 DefaultDest, IndirectDests, Args, Name);
1088 }
1089
1090 ResumeInst *CreateResume(Value *Exn) {
1091 return Insert(ResumeInst::Create(Exn));
1092 }
1093
1094 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1095 BasicBlock *UnwindBB = nullptr) {
1096 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1097 }
1098
1099 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1100 unsigned NumHandlers,
1101 const Twine &Name = "") {
1102 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1103 Name);
1104 }
1105
1106 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1107 const Twine &Name = "") {
1108 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1109 }
1110
1111 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1112 ArrayRef<Value *> Args = None,
1113 const Twine &Name = "") {
1114 return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1115 }
1116
1117 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1118 return Insert(CatchReturnInst::Create(CatchPad, BB));
1119 }
1120
1121 UnreachableInst *CreateUnreachable() {
1122 return Insert(new UnreachableInst(Context));
1123 }
1124
1125 //===--------------------------------------------------------------------===//
1126 // Instruction creation methods: Binary Operators
1127 //===--------------------------------------------------------------------===//
1128private:
1129 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1130 Value *LHS, Value *RHS,
1131 const Twine &Name,
1132 bool HasNUW, bool HasNSW) {
1133 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1134 if (HasNUW) BO->setHasNoUnsignedWrap();
1135 if (HasNSW) BO->setHasNoSignedWrap();
1136 return BO;
1137 }
1138
1139 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1140 FastMathFlags FMF) const {
1141 if (!FPMD)
1142 FPMD = DefaultFPMathTag;
1143 if (FPMD)
1144 I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1145 I->setFastMathFlags(FMF);
1146 return I;
1147 }
1148
1149 Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
1150 Value *R, const Twine &Name) const {
1151 auto *LC = dyn_cast<Constant>(L);
1152 auto *RC = dyn_cast<Constant>(R);
1153 return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
1154 }
1155
1156 Value *getConstrainedFPRounding(Optional<RoundingMode> Rounding) {
1157 RoundingMode UseRounding = DefaultConstrainedRounding;
1158
1159 if (Rounding.hasValue())
1160 UseRounding = Rounding.getValue();
1161
1162 Optional<StringRef> RoundingStr = RoundingModeToStr(UseRounding);
1163 assert(RoundingStr.hasValue() && "Garbage strict rounding mode!")((RoundingStr.hasValue() && "Garbage strict rounding mode!"
) ? static_cast<void> (0) : __assert_fail ("RoundingStr.hasValue() && \"Garbage strict rounding mode!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 1163, __PRETTY_FUNCTION__))
;
1164 auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue());
1165
1166 return MetadataAsValue::get(Context, RoundingMDS);
1167 }
1168
1169 Value *getConstrainedFPExcept(Optional<fp::ExceptionBehavior> Except) {
1170 fp::ExceptionBehavior UseExcept = DefaultConstrainedExcept;
1171
1172 if (Except.hasValue())
1173 UseExcept = Except.getValue();
1174
1175 Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(UseExcept);
1176 assert(ExceptStr.hasValue() && "Garbage strict exception behavior!")((ExceptStr.hasValue() && "Garbage strict exception behavior!"
) ? static_cast<void> (0) : __assert_fail ("ExceptStr.hasValue() && \"Garbage strict exception behavior!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 1176, __PRETTY_FUNCTION__))
;
1177 auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue());
1178
1179 return MetadataAsValue::get(Context, ExceptMDS);
1180 }
1181
1182 Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
1183 assert(CmpInst::isFPPredicate(Predicate) &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst
::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE &&
"Invalid constrained FP comparison predicate!") ? static_cast
<void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 1186, __PRETTY_FUNCTION__))
1184 Predicate != CmpInst::FCMP_FALSE &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst
::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE &&
"Invalid constrained FP comparison predicate!") ? static_cast
<void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 1186, __PRETTY_FUNCTION__))
1185 Predicate != CmpInst::FCMP_TRUE &&((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst
::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE &&
"Invalid constrained FP comparison predicate!") ? static_cast
<void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 1186, __PRETTY_FUNCTION__))
1186 "Invalid constrained FP comparison predicate!")((CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst
::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE &&
"Invalid constrained FP comparison predicate!") ? static_cast
<void> (0) : __assert_fail ("CmpInst::isFPPredicate(Predicate) && Predicate != CmpInst::FCMP_FALSE && Predicate != CmpInst::FCMP_TRUE && \"Invalid constrained FP comparison predicate!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 1186, __PRETTY_FUNCTION__))
;
1187
1188 StringRef PredicateStr = CmpInst::getPredicateName(Predicate);
1189 auto *PredicateMDS = MDString::get(Context, PredicateStr);
1190
1191 return MetadataAsValue::get(Context, PredicateMDS);
1192 }
1193
1194public:
1195 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1196 bool HasNUW = false, bool HasNSW = false) {
1197 if (auto *LC = dyn_cast<Constant>(LHS))
1198 if (auto *RC = dyn_cast<Constant>(RHS))
1199 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
1200 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
1201 HasNUW, HasNSW);
1202 }
1203
1204 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1205 return CreateAdd(LHS, RHS, Name, false, true);
1206 }
1207
1208 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1209 return CreateAdd(LHS, RHS, Name, true, false);
1210 }
1211
1212 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1213 bool HasNUW = false, bool HasNSW = false) {
1214 if (auto *LC = dyn_cast<Constant>(LHS))
1215 if (auto *RC = dyn_cast<Constant>(RHS))
1216 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
1217 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
1218 HasNUW, HasNSW);
1219 }
1220
1221 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1222 return CreateSub(LHS, RHS, Name, false, true);
1223 }
1224
1225 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1226 return CreateSub(LHS, RHS, Name, true, false);
1227 }
1228
1229 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1230 bool HasNUW = false, bool HasNSW = false) {
1231 if (auto *LC = dyn_cast<Constant>(LHS))
1232 if (auto *RC = dyn_cast<Constant>(RHS))
1233 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
1234 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
1235 HasNUW, HasNSW);
1236 }
1237
1238 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1239 return CreateMul(LHS, RHS, Name, false, true);
1240 }
1241
1242 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1243 return CreateMul(LHS, RHS, Name, true, false);
1244 }
1245
1246 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1247 bool isExact = false) {
1248 if (auto *LC = dyn_cast<Constant>(LHS))
1249 if (auto *RC = dyn_cast<Constant>(RHS))
1250 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
1251 if (!isExact)
1252 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1253 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1254 }
1255
1256 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1257 return CreateUDiv(LHS, RHS, Name, true);
1258 }
1259
1260 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1261 bool isExact = false) {
1262 if (auto *LC = dyn_cast<Constant>(LHS))
1263 if (auto *RC = dyn_cast<Constant>(RHS))
1264 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
1265 if (!isExact)
1266 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1267 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1268 }
1269
1270 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1271 return CreateSDiv(LHS, RHS, Name, true);
1272 }
1273
1274 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1275 if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
1276 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1277 }
1278
1279 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1280 if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
1281 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1282 }
1283
1284 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1285 bool HasNUW = false, bool HasNSW = false) {
1286 if (auto *LC = dyn_cast<Constant>(LHS))
1287 if (auto *RC = dyn_cast<Constant>(RHS))
1288 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
1289 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1290 HasNUW, HasNSW);
1291 }
1292
1293 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1294 bool HasNUW = false, bool HasNSW = false) {
1295 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1296 HasNUW, HasNSW);
1297 }
1298
1299 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1300 bool HasNUW = false, bool HasNSW = false) {
1301 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1302 HasNUW, HasNSW);
1303 }
1304
1305 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1306 bool isExact = false) {
1307 if (auto *LC = dyn_cast<Constant>(LHS))
1308 if (auto *RC = dyn_cast<Constant>(RHS))
1309 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
1310 if (!isExact)
1311 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1312 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1313 }
1314
1315 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1316 bool isExact = false) {
1317 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1318 }
1319
1320 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1321 bool isExact = false) {
1322 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1323 }
1324
1325 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1326 bool isExact = false) {
1327 if (auto *LC = dyn_cast<Constant>(LHS))
1328 if (auto *RC = dyn_cast<Constant>(RHS))
1329 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
1330 if (!isExact)
1331 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1332 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1333 }
1334
1335 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1336 bool isExact = false) {
1337 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1338 }
1339
1340 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1341 bool isExact = false) {
1342 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1343 }
1344
1345 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1346 if (auto *RC = dyn_cast<Constant>(RHS)) {
1347 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
1348 return LHS; // LHS & -1 -> LHS
1349 if (auto *LC = dyn_cast<Constant>(LHS))
1350 return Insert(Folder.CreateAnd(LC, RC), Name);
1351 }
1352 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1353 }
1354
1355 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1356 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1357 }
1358
1359 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1360 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1361 }
1362
1363 Value *CreateAnd(ArrayRef<Value*> Ops) {
1364 assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail
("!Ops.empty()", "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 1364, __PRETTY_FUNCTION__))
;
1365 Value *Accum = Ops[0];
1366 for (unsigned i = 1; i < Ops.size(); i++)
1367 Accum = CreateAnd(Accum, Ops[i]);
1368 return Accum;
1369 }
1370
1371 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1372 if (auto *RC = dyn_cast<Constant>(RHS)) {
1373 if (RC->isNullValue())
1374 return LHS; // LHS | 0 -> LHS
1375 if (auto *LC = dyn_cast<Constant>(LHS))
1376 return Insert(Folder.CreateOr(LC, RC), Name);
1377 }
1378 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1379 }
1380
1381 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1382 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1383 }
1384
1385 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1386 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1387 }
1388
1389 Value *CreateOr(ArrayRef<Value*> Ops) {
1390 assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail
("!Ops.empty()", "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 1390, __PRETTY_FUNCTION__))
;
1391 Value *Accum = Ops[0];
1392 for (unsigned i = 1; i < Ops.size(); i++)
1393 Accum = CreateOr(Accum, Ops[i]);
1394 return Accum;
1395 }
1396
1397 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1398 if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V;
1399 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1400 }
1401
1402 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1403 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1404 }
1405
1406 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1407 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1408 }
1409
1410 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1411 MDNode *FPMD = nullptr) {
1412 if (IsFPConstrained)
1413 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1414 L, R, nullptr, Name, FPMD);
1415
1416 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1417 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1418 return Insert(I, Name);
1419 }
1420
1421 /// Copy fast-math-flags from an instruction rather than using the builder's
1422 /// default FMF.
1423 Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1424 const Twine &Name = "") {
1425 if (IsFPConstrained)
1426 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1427 L, R, FMFSource, Name);
1428
1429 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1430 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr,
1431 FMFSource->getFastMathFlags());
1432 return Insert(I, Name);
1433 }
1434
1435 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1436 MDNode *FPMD = nullptr) {
1437 if (IsFPConstrained)
1438 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1439 L, R, nullptr, Name, FPMD);
1440
1441 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1442 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1443 return Insert(I, Name);
1444 }
1445
1446 /// Copy fast-math-flags from an instruction rather than using the builder's
1447 /// default FMF.
1448 Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1449 const Twine &Name = "") {
1450 if (IsFPConstrained)
1451 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1452 L, R, FMFSource, Name);
1453
1454 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1455 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr,
1456 FMFSource->getFastMathFlags());
1457 return Insert(I, Name);
1458 }
1459
1460 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1461 MDNode *FPMD = nullptr) {
1462 if (IsFPConstrained)
1463 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1464 L, R, nullptr, Name, FPMD);
1465
1466 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1467 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1468 return Insert(I, Name);
1469 }
1470
1471 /// Copy fast-math-flags from an instruction rather than using the builder's
1472 /// default FMF.
1473 Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1474 const Twine &Name = "") {
1475 if (IsFPConstrained)
1476 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1477 L, R, FMFSource, Name);
1478
1479 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1480 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr,
1481 FMFSource->getFastMathFlags());
1482 return Insert(I, Name);
1483 }
1484
1485 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1486 MDNode *FPMD = nullptr) {
1487 if (IsFPConstrained)
1488 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1489 L, R, nullptr, Name, FPMD);
1490
1491 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1492 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1493 return Insert(I, Name);
1494 }
1495
1496 /// Copy fast-math-flags from an instruction rather than using the builder's
1497 /// default FMF.
1498 Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1499 const Twine &Name = "") {
1500 if (IsFPConstrained)
1501 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1502 L, R, FMFSource, Name);
1503
1504 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1505 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr,
1506 FMFSource->getFastMathFlags());
1507 return Insert(I, Name);
1508 }
1509
1510 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1511 MDNode *FPMD = nullptr) {
1512 if (IsFPConstrained)
1513 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1514 L, R, nullptr, Name, FPMD);
1515
1516 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1517 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1518 return Insert(I, Name);
1519 }
1520
1521 /// Copy fast-math-flags from an instruction rather than using the builder's
1522 /// default FMF.
1523 Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1524 const Twine &Name = "") {
1525 if (IsFPConstrained)
1526 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1527 L, R, FMFSource, Name);
1528
1529 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1530 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr,
1531 FMFSource->getFastMathFlags());
1532 return Insert(I, Name);
1533 }
1534
1535 Value *CreateBinOp(Instruction::BinaryOps Opc,
1536 Value *LHS, Value *RHS, const Twine &Name = "",
1537 MDNode *FPMathTag = nullptr) {
1538 if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V;
1539 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1540 if (isa<FPMathOperator>(BinOp))
1541 setFPAttrs(BinOp, FPMathTag, FMF);
1542 return Insert(BinOp, Name);
1543 }
1544
1545 CallInst *CreateConstrainedFPBinOp(
1546 Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1547 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1548 Optional<RoundingMode> Rounding = None,
1549 Optional<fp::ExceptionBehavior> Except = None);
1550
1551 Value *CreateNeg(Value *V, const Twine &Name = "",
1552 bool HasNUW = false, bool HasNSW = false) {
1553 if (auto *VC = dyn_cast<Constant>(V))
1554 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
1555 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
1556 if (HasNUW) BO->setHasNoUnsignedWrap();
1557 if (HasNSW) BO->setHasNoSignedWrap();
1558 return BO;
1559 }
1560
1561 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1562 return CreateNeg(V, Name, false, true);
1563 }
1564
1565 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1566 return CreateNeg(V, Name, true, false);
1567 }
1568
1569 Value *CreateFNeg(Value *V, const Twine &Name = "",
1570 MDNode *FPMathTag = nullptr) {
1571 if (auto *VC = dyn_cast<Constant>(V))
1572 return Insert(Folder.CreateFNeg(VC), Name);
1573 return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF),
1574 Name);
1575 }
1576
1577 /// Copy fast-math-flags from an instruction rather than using the builder's
1578 /// default FMF.
1579 Value *CreateFNegFMF(Value *V, Instruction *FMFSource,
1580 const Twine &Name = "") {
1581 if (auto *VC = dyn_cast<Constant>(V))
1582 return Insert(Folder.CreateFNeg(VC), Name);
1583 return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr,
1584 FMFSource->getFastMathFlags()),
1585 Name);
1586 }
1587
1588 Value *CreateNot(Value *V, const Twine &Name = "") {
1589 if (auto *VC = dyn_cast<Constant>(V))
1590 return Insert(Folder.CreateNot(VC), Name);
1591 return Insert(BinaryOperator::CreateNot(V), Name);
1592 }
1593
1594 Value *CreateUnOp(Instruction::UnaryOps Opc,
1595 Value *V, const Twine &Name = "",
1596 MDNode *FPMathTag = nullptr) {
1597 if (auto *VC = dyn_cast<Constant>(V))
1598 return Insert(Folder.CreateUnOp(Opc, VC), Name);
1599 Instruction *UnOp = UnaryOperator::Create(Opc, V);
1600 if (isa<FPMathOperator>(UnOp))
1601 setFPAttrs(UnOp, FPMathTag, FMF);
1602 return Insert(UnOp, Name);
1603 }
1604
1605 /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
1606 /// Correct number of operands must be passed accordingly.
1607 Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1608 const Twine &Name = "", MDNode *FPMathTag = nullptr);
1609
1610 //===--------------------------------------------------------------------===//
1611 // Instruction creation methods: Memory Instructions
1612 //===--------------------------------------------------------------------===//
1613
1614 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1615 Value *ArraySize = nullptr, const Twine &Name = "") {
1616 const DataLayout &DL = BB->getModule()->getDataLayout();
1617 Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1618 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1619 }
1620
1621 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1622 const Twine &Name = "") {
1623 const DataLayout &DL = BB->getModule()->getDataLayout();
1624 Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1625 unsigned AddrSpace = DL.getAllocaAddrSpace();
1626 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1627 }
1628
1629 /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1630 /// converting the string to 'bool' for the isVolatile parameter.
1631 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1632 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1633 }
1634
1635 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1636 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1637 }
1638
1639 LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1640 const Twine &Name = "") {
1641 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name);
1642 }
1643
1644 // Deprecated [opaque pointer types]
1645 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1646 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1647 }
1648
1649 // Deprecated [opaque pointer types]
1650 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1651 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1652 }
1653
1654 // Deprecated [opaque pointer types]
1655 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1656 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, isVolatile,
1657 Name);
1658 }
1659
1660 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1661 return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile);
1662 }
1663
1664 LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead"
)))
1665 unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead"
)))
1666 const char *Name),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead"
)))
1667 "Use the version that takes NaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead"
)))
{
1668 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), Name);
1669 }
1670 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1671 const char *Name) {
1672 return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1673 }
1674
1675 LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1676 unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1677 const Twine &Name = ""),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1678 "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
{
1679 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), Name);
1680 }
1681 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1682 const Twine &Name = "") {
1683 return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1684 }
1685
1686 LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, bool isVolatile, const Twine &Name = "") __attribute__(
(deprecated("Use the version that takes MaybeAlign instead"))
)
1687 unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, bool isVolatile, const Twine &Name = "") __attribute__(
(deprecated("Use the version that takes MaybeAlign instead"))
)
1688 bool isVolatile,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, bool isVolatile, const Twine &Name = "") __attribute__(
(deprecated("Use the version that takes MaybeAlign instead"))
)
1689 const Twine &Name = ""),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, bool isVolatile, const Twine &Name = "") __attribute__(
(deprecated("Use the version that takes MaybeAlign instead"))
)
1690 "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align
, bool isVolatile, const Twine &Name = "") __attribute__(
(deprecated("Use the version that takes MaybeAlign instead"))
)
{
1691 return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), isVolatile, Name);
1692 }
1693 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1694 bool isVolatile, const Twine &Name = "") {
1695 if (!Align) {
1696 const DataLayout &DL = BB->getModule()->getDataLayout();
1697 Align = DL.getABITypeAlign(Ty);
1698 }
1699 return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
1700 }
1701
1702 // Deprecated [opaque pointer types]
1703 LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const
char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1704 unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const
char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1705 const char *Name),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const
char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1706 "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const
char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
{
1707 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1708 MaybeAlign(Align), Name);
1709 }
1710 // Deprecated [opaque pointer types]
1711 LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const
Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1712 unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const
Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1713 const Twine &Name = ""),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const
Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1714 "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const
Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
{
1715 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1716 MaybeAlign(Align), Name);
1717 }
1718 // Deprecated [opaque pointer types]
1719 LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1720 unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1721 bool isVolatile,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1722 const Twine &Name = ""),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1723 "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile
, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
{
1724 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1725 MaybeAlign(Align), isVolatile, Name);
1726 }
1727 // Deprecated [opaque pointer types]
1728 LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align, const char *Name) {
1729 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1730 Align, Name);
1731 }
1732 // Deprecated [opaque pointer types]
1733 LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align,
1734 const Twine &Name = "") {
1735 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1736 Align, Name);
1737 }
1738 // Deprecated [opaque pointer types]
1739 LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align, bool isVolatile,
1740 const Twine &Name = "") {
1741 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1742 Align, isVolatile, Name);
1743 }
1744
1745 LLVM_ATTRIBUTE_DEPRECATED(StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned
Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1746 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned
Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1747 bool isVolatile = false),StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned
Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
1748 "Use the version that takes MaybeAlign instead")StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned
Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead"
)))
{
1749 return CreateAlignedStore(Val, Ptr, MaybeAlign(Align), isVolatile);
1750 }
1751 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
1752 bool isVolatile = false) {
1753 if (!Align) {
1754 const DataLayout &DL = BB->getModule()->getDataLayout();
1755 Align = DL.getABITypeAlign(Val->getType());
1756 }
1757 return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
1758 }
1759 FenceInst *CreateFence(AtomicOrdering Ordering,
1760 SyncScope::ID SSID = SyncScope::System,
1761 const Twine &Name = "") {
1762 return Insert(new FenceInst(Context, Ordering, SSID), Name);
1763 }
1764
1765 AtomicCmpXchgInst *CreateAtomicCmpXchg(
1766 Value *Ptr, Value *Cmp, Value *New, AtomicOrdering SuccessOrdering,
1767 AtomicOrdering FailureOrdering, SyncScope::ID SSID = SyncScope::System) {
1768 const DataLayout &DL = BB->getModule()->getDataLayout();
1769 Align Alignment(DL.getTypeStoreSize(New->getType()));
1770 return Insert(new AtomicCmpXchgInst(
1771 Ptr, Cmp, New, Alignment, SuccessOrdering, FailureOrdering, SSID));
1772 }
1773
1774 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1775 AtomicOrdering Ordering,
1776 SyncScope::ID SSID = SyncScope::System) {
1777 const DataLayout &DL = BB->getModule()->getDataLayout();
1778 Align Alignment(DL.getTypeStoreSize(Val->getType()));
1779 return Insert(new AtomicRMWInst(Op, Ptr, Val, Alignment, Ordering, SSID));
1780 }
1781
1782 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1783 const Twine &Name = "") {
1784 return CreateGEP(nullptr, Ptr, IdxList, Name);
1785 }
1786
1787 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1788 const Twine &Name = "") {
1789 if (auto *PC = dyn_cast<Constant>(Ptr)) {
1790 // Every index must be constant.
1791 size_t i, e;
1792 for (i = 0, e = IdxList.size(); i != e; ++i)
1793 if (!isa<Constant>(IdxList[i]))
1794 break;
1795 if (i == e)
1796 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1797 }
1798 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1799 }
1800
1801 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1802 const Twine &Name = "") {
1803 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1804 }
1805
1806 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1807 const Twine &Name = "") {
1808 if (auto *PC = dyn_cast<Constant>(Ptr)) {
1809 // Every index must be constant.
1810 size_t i, e;
1811 for (i = 0, e = IdxList.size(); i != e; ++i)
1812 if (!isa<Constant>(IdxList[i]))
1813 break;
1814 if (i == e)
1815 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1816 Name);
1817 }
1818 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1819 }
1820
1821 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1822 return CreateGEP(nullptr, Ptr, Idx, Name);
1823 }
1824
1825 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1826 if (auto *PC
27.1
'PC' is non-null
27.1
'PC' is non-null
= dyn_cast<Constant>(Ptr))
27
Assuming 'Ptr' is a 'Constant'
28
Taking true branch
1827 if (auto *IC
29.1
'IC' is non-null
29.1
'IC' is non-null
= dyn_cast<Constant>(Idx))
29
Assuming 'Idx' is a 'Constant'
30
Taking true branch
1828 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
31
Returning pointer
1829 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1830 }
1831
1832 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1833 const Twine &Name = "") {
1834 if (auto *PC = dyn_cast<Constant>(Ptr))
1835 if (auto *IC = dyn_cast<Constant>(Idx))
1836 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1837 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1838 }
1839
1840 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1841 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1842 }
1843
1844 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1845 const Twine &Name = "") {
1846 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1847
1848 if (auto *PC = dyn_cast<Constant>(Ptr))
1849 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1850
1851 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1852 }
1853
1854 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1855 const Twine &Name = "") {
1856 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1857
1858 if (auto *PC = dyn_cast<Constant>(Ptr))
1859 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1860
1861 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1862 }
1863
1864 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1865 const Twine &Name = "") {
1866 Value *Idxs[] = {
1867 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1868 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1869 };
1870
1871 if (auto *PC = dyn_cast<Constant>(Ptr))
1872 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1873
1874 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1875 }
1876
1877 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1878 unsigned Idx1, const Twine &Name = "") {
1879 Value *Idxs[] = {
1880 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1881 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1882 };
1883
1884 if (auto *PC = dyn_cast<Constant>(Ptr))
1885 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1886
1887 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1888 }
1889
1890 Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1891 const Twine &Name = "") {
1892 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1893
1894 if (auto *PC = dyn_cast<Constant>(Ptr))
1895 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1896
1897 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1898 }
1899
1900 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1901 return CreateConstGEP1_64(nullptr, Ptr, Idx0, Name);
1902 }
1903
1904 Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1905 const Twine &Name = "") {
1906 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1907
1908 if (auto *PC = dyn_cast<Constant>(Ptr))
1909 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1910
1911 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1912 }
1913
1914 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1915 const Twine &Name = "") {
1916 return CreateConstInBoundsGEP1_64(nullptr, Ptr, Idx0, Name);
1917 }
1918
1919 Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1920 const Twine &Name = "") {
1921 Value *Idxs[] = {
1922 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1923 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1924 };
1925
1926 if (auto *PC = dyn_cast<Constant>(Ptr))
1927 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1928
1929 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1930 }
1931
1932 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1933 const Twine &Name = "") {
1934 return CreateConstGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1935 }
1936
1937 Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1938 uint64_t Idx1, const Twine &Name = "") {
1939 Value *Idxs[] = {
1940 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1941 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1942 };
1943
1944 if (auto *PC = dyn_cast<Constant>(Ptr))
1945 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1946
1947 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1948 }
1949
1950 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1951 const Twine &Name = "") {
1952 return CreateConstInBoundsGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1953 }
1954
1955 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1956 const Twine &Name = "") {
1957 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1958 }
1959
1960 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1961 return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name);
1962 }
1963
1964 /// Same as CreateGlobalString, but return a pointer with "i8*" type
1965 /// instead of a pointer to array of i8.
1966 ///
1967 /// If no module is given via \p M, it is take from the insertion point basic
1968 /// block.
1969 Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1970 unsigned AddressSpace = 0,
1971 Module *M = nullptr) {
1972 GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M);
1973 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1974 Constant *Indices[] = {Zero, Zero};
1975 return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
1976 Indices);
1977 }
1978
1979 //===--------------------------------------------------------------------===//
1980 // Instruction creation methods: Cast/Conversion Operators
1981 //===--------------------------------------------------------------------===//
1982
1983 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1984 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1985 }
1986
1987 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1988 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1989 }
1990
1991 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1992 return CreateCast(Instruction::SExt, V, DestTy, Name);
1993 }
1994
1995 /// Create a ZExt or Trunc from the integer value V to DestTy. Return
1996 /// the value untouched if the type of V is already DestTy.
1997 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1998 const Twine &Name = "") {
1999 assert(V->getType()->isIntOrIntVectorTy() &&((V->getType()->isIntOrIntVectorTy() && DestTy->
isIntOrIntVectorTy() && "Can only zero extend/truncate integers!"
) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 2001, __PRETTY_FUNCTION__))
2000 DestTy->isIntOrIntVectorTy() &&((V->getType()->isIntOrIntVectorTy() && DestTy->
isIntOrIntVectorTy() && "Can only zero extend/truncate integers!"
) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 2001, __PRETTY_FUNCTION__))
2001 "Can only zero extend/truncate integers!")((V->getType()->isIntOrIntVectorTy() && DestTy->
isIntOrIntVectorTy() && "Can only zero extend/truncate integers!"
) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only zero extend/truncate integers!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 2001, __PRETTY_FUNCTION__))
;
2002 Type *VTy = V->getType();
2003 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2004 return CreateZExt(V, DestTy, Name);
2005 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2006 return CreateTrunc(V, DestTy, Name);
2007 return V;
2008 }
2009
2010 /// Create a SExt or Trunc from the integer value V to DestTy. Return
2011 /// the value untouched if the type of V is already DestTy.
2012 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
2013 const Twine &Name = "") {
2014 assert(V->getType()->isIntOrIntVectorTy() &&((V->getType()->isIntOrIntVectorTy() && DestTy->
isIntOrIntVectorTy() && "Can only sign extend/truncate integers!"
) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 2016, __PRETTY_FUNCTION__))
2015 DestTy->isIntOrIntVectorTy() &&((V->getType()->isIntOrIntVectorTy() && DestTy->
isIntOrIntVectorTy() && "Can only sign extend/truncate integers!"
) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 2016, __PRETTY_FUNCTION__))
2016 "Can only sign extend/truncate integers!")((V->getType()->isIntOrIntVectorTy() && DestTy->
isIntOrIntVectorTy() && "Can only sign extend/truncate integers!"
) ? static_cast<void> (0) : __assert_fail ("V->getType()->isIntOrIntVectorTy() && DestTy->isIntOrIntVectorTy() && \"Can only sign extend/truncate integers!\""
, "/build/llvm-toolchain-snapshot-12~++20201211111113+08280c4b734/llvm/include/llvm/IR/IRBuilder.h"
, 2016, __PRETTY_FUNCTION__))
;
2017 Type *VTy = V->getType();
2018 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2019 return CreateSExt(V, DestTy, Name);
2020 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2021 return CreateTrunc(V, DestTy, Name);
2022 return V;
2023 }
2024
2025 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
2026 if (IsFPConstrained)
2027 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
2028 V, DestTy, nullptr, Name);
2029 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
2030 }
2031
2032 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
2033 if (IsFPConstrained)
2034 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
2035 V, DestTy, nullptr, Name);
2036 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
2037 }
2038
2039 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
2040 if (IsFPConstrained)
2041 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp,
2042 V, DestTy, nullptr, Name);
2043 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
2044 }
2045
2046 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
2047 if (IsFPConstrained)
2048 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp,
2049 V, DestTy, nullptr, Name);
2050 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
2051 }
2052
2053 Value *CreateFPTrunc(Value *V, Type *DestTy,
2054 const Twine &Name = "") {
2055 if (IsFPConstrained)
2056 return CreateConstrainedFPCast(
2057 Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr,
2058 Name);
2059 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
2060 }
2061
2062 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
2063 if (IsFPConstrained)
2064 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
2065 V, DestTy, nullptr, Name);
2066 return CreateCast(Instruction::FPExt, V, DestTy, Name);
2067 }
2068
2069 Value *CreatePtrToInt(Value *V, Type *DestTy,
2070 const Twine &Name = "") {
2071 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
2072 }
2073
2074 Value *CreateIntToPtr(Value *V, Type *DestTy,
2075 const Twine &Name = "") {
2076 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
2077 }
2078
2079 Value *CreateBitCast(Value *V, Type *DestTy,
2080 const Twine &Name = "") {
2081 return CreateCast(Instruction::BitCast, V, DestTy, Name);
2082 }
2083
2084 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
2085 const Twine &Name = "") {
2086 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
2087 }
2088
2089 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
2090 const Twine &Name = "") {
2091 if (V->getType() == DestTy)
2092 return V;
2093 if (auto *VC = dyn_cast<Constant>(V))
2094 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
2095 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
2096 }
2097
2098 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
2099 const Twine &Name = "") {
2100 if (V->getType() == DestTy)
2101 return V;
2102 if (auto *VC = dyn_cast<Constant>(V))
2103 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
2104 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
2105 }
2106
2107 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
2108 const Twine &Name = "") {
2109 if (V->getType() == DestTy)
2110 return V;
2111 if (auto *VC = dyn_cast<Constant>(V))
2112 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
2113 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
2114 }
2115
2116 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2117 const Twine &Name = "") {
2118 if (V->getType() == DestTy)
2119 return V;
2120 if (auto *VC = dyn_cast<Constant>(V))
2121 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
2122 return Insert(CastInst::Create(Op, V, DestTy), Name);
2123 }
2124
2125 Value *CreatePointerCast(Value *V, Type *DestTy,
2126 const Twine &Name = "") {
2127 if (V->getType() == DestTy)
2128 return V;
2129 if (auto *VC = dyn_cast<Constant>(V))
2130 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
2131 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
2132 }
2133
2134 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2135 const Twine &Name = "") {
2136 if (V->getType() == DestTy)
2137 return V;
2138
2139 if (auto *VC = dyn_cast<Constant>(V)) {
2140 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
2141 Name);
2142 }
2143
2144 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
2145 Name);
2146 }
2147
2148 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2149 const Twine &Name = "") {
2150 if (V->getType() == DestTy)
2151 return V;
2152 if (auto *VC = dyn_cast<Constant>(V))
2153 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
2154 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
2155 }
2156
2157 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2158 const Twine &Name = "") {
2159 if (V->getType() == DestTy)
2160 return V;
2161 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2162 return CreatePtrToInt(V, DestTy, Name);
2163 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2164 return CreateIntToPtr(V, DestTy, Name);
2165
2166 return CreateBitCast(V, DestTy, Name);
2167 }
2168
2169 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
2170 if (V->getType() == DestTy)
2171 return V;
2172 if (auto *VC = dyn_cast<Constant>(V))
2173 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
2174 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
2175 }
2176
2177 CallInst *CreateConstrainedFPCast(
2178 Intrinsic::ID ID, Value *V, Type *DestTy,
2179 Instruction *FMFSource = nullptr, const Twine &Name = "",
2180 MDNode *FPMathTag = nullptr,
2181 Optional<RoundingMode> Rounding = None,
2182 Optional<fp::ExceptionBehavior> Except = None);
2183
2184 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
2185 // compile time error, instead of converting the string to bool for the
2186 // isSigned parameter.
2187 Value *CreateIntCast(Value *, Type *, const char *) = delete;
2188
2189 //===--------------------------------------------------------------------===//
2190 // Instruction creation methods: Compare Instructions
2191 //===--------------------------------------------------------------------===//
2192
2193 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2194 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
2195 }
2196
2197 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2198 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
2199 }
2200
2201 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2202 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
2203 }
2204
2205 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2206 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
2207 }
2208
2209 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2210 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
2211 }
2212
2213 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2214 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
2215 }
2216
2217 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2218 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
2219 }
2220
2221 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2222 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
2223 }
2224
2225 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
2226 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
2227 }
2228
2229 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
2230 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
2231 }
2232
2233 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2234 MDNode *FPMathTag = nullptr) {
2235 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
2236 }
2237
2238 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
2239 MDNode *FPMathTag = nullptr) {
2240 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
2241 }
2242
2243 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
2244 MDNode *FPMathTag = nullptr) {
2245 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
2246 }
2247
2248 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
2249 MDNode *FPMathTag = nullptr) {
2250 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
2251 }
2252
2253 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
2254 MDNode *FPMathTag = nullptr) {
2255 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
2256 }
2257
2258 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
2259 MDNode *FPMathTag = nullptr) {
2260 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
2261 }
2262
2263 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
2264 MDNode *FPMathTag = nullptr) {
2265 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
2266 }
2267
2268 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
2269 MDNode *FPMathTag = nullptr) {
2270 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
2271 }
2272
2273 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2274 MDNode *FPMathTag = nullptr) {
2275 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
2276 }
2277
2278 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
2279 MDNode *FPMathTag = nullptr) {
2280 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
2281 }
2282
2283 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
2284 MDNode *FPMathTag = nullptr) {
2285 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
2286 }
2287
2288 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
2289 MDNode *FPMathTag = nullptr) {
2290 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
2291 }
2292
2293 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
2294 MDNode *FPMathTag = nullptr) {
2295 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
2296 }
2297
2298 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
2299 MDNode *FPMathTag = nullptr) {
2300 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
2301 }
2302
2303 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2304 const Twine &Name = "") {
2305 if (auto *LC = dyn_cast<Constant>(LHS))
2306 if (auto *RC = dyn_cast<Constant>(RHS))
2307 return Insert(Folder.CreateICmp(P, LC, RC), Name);
2308 return Insert(new ICmpInst(P, LHS, RHS), Name);
2309 }
2310
2311 // Create a quiet floating-point comparison (i.e. one that raises an FP
2312 // exception only in the case where an input is a signaling NaN).
2313 // Note that this differs from CreateFCmpS only if IsFPConstrained is true.
2314 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2315 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2316 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, false);
2317 }
2318
2319 Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
2320 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2321 return CmpInst::isFPPredicate(Pred)
2322 ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag)
2323 : CreateICmp(Pred, LHS, RHS, Name);
2324 }
2325
2326 // Create a signaling floating-point comparison (i.e. one that raises an FP
2327 // exception whenever an input is any NaN, signaling or quiet).
2328 // Note that this differs from CreateFCmp only if IsFPConstrained is true.
2329 Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS,
2330 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2331 return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, true);
2332 }
2333
2334private:
2335 // Helper routine to create either a signaling or a quiet FP comparison.
2336 Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS,
2337 const Twine &Name, MDNode *FPMathTag,
2338 bool IsSignaling);
2339
2340public:
2341 CallInst *CreateConstrainedFPCmp(
2342 Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R,
2343 const Twine &Name = "", Optional<fp::ExceptionBehavior> Except = None);
2344
2345 //===--------------------------------------------------------------------===//
2346 // Instruction creation methods: Other Instructions
2347 //===--------------------------------------------------------------------===//
2348
2349 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2350 const Twine &Name = "") {
2351 PHINode *Phi = PHINode::Create(Ty, NumReservedValues);
2352 if (isa<FPMathOperator>(Phi))
2353 setFPAttrs(Phi, nullptr /* MDNode* */, FMF);
2354 return Insert(Phi, Name);
2355 }
2356
2357 CallInst *CreateCall(FunctionType *FTy, Value *Callee,
2358 ArrayRef<Value *> Args = None, const Twine &Name = "",
2359 MDNode *FPMathTag = nullptr) {
2360 CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2361 if (IsFPConstrained)
2362 setConstrainedFPCallAttr(CI);
2363 if (isa<FPMathOperator>(CI))
2364 setFPAttrs(CI, FPMathTag, FMF);
2365 return Insert(CI, Name);
2366 }
2367
2368 CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
2369 ArrayRef<OperandBundleDef> OpBundles,
2370 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2371 CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2372 if (IsFPConstrained)
2373 setConstrainedFPCallAttr(CI);
2374 if (isa<FPMathOperator>(CI))
2375 setFPAttrs(CI, FPMathTag, FMF);
2376 return Insert(CI, Name);
2377 }
2378
2379 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None,
2380 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2381 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2382 FPMathTag);
2383 }
2384
2385 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2386 ArrayRef<OperandBundleDef> OpBundles,
2387 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2388 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2389 OpBundles, Name, FPMathTag);
2390 }
2391
2392 CallInst *CreateConstrainedFPCall(
2393 Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "",
2394 Optional<RoundingMode> Rounding = None,
2395 Optional<fp::ExceptionBehavior> Except = None);
2396
2397 Value *CreateSelect(Value *C, Value *True, Value *False,
2398 const Twine &Name = "", Instruction *MDFrom = nullptr);
2399
2400 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2401 return Insert(new VAArgInst(List, Ty), Name);
2402 }
2403
2404 Value *CreateExtractElement(Value *Vec, Value *Idx,
2405 const Twine &Name = "") {
2406 if (auto *VC = dyn_cast<Constant>(Vec))
2407 if (auto *IC = dyn_cast<Constant>(Idx))
2408 return Insert(Folder.CreateExtractElement(VC, IC), Name);
2409 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2410 }
2411
2412 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2413 const Twine &Name = "") {
2414 return CreateExtractElement(Vec, getInt64(Idx), Name);
2415 }
2416
2417 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2418 const Twine &Name = "") {
2419 if (auto *VC = dyn_cast<Constant>(Vec))
2420 if (auto *NC = dyn_cast<Constant>(NewElt))
2421 if (auto *IC = dyn_cast<Constant>(Idx))
2422 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
2423 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2424 }
2425
2426 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2427 const Twine &Name = "") {
2428 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2429 }
2430
2431 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2432 const Twine &Name = "") {
2433 SmallVector<int, 16> IntMask;
2434 ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask);
2435 return CreateShuffleVector(V1, V2, IntMask, Name);
2436 }
2437
2438 LLVM_ATTRIBUTE_DEPRECATED(Value *CreateShuffleVector(Value *V1, Value *V2,Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<
uint32_t> Mask, const Twine &Name = "") __attribute__(
(deprecated("Pass indices as 'int' instead")))
2439 ArrayRef<uint32_t> Mask,Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<
uint32_t> Mask, const Twine &Name = "") __attribute__(
(deprecated("Pass indices as 'int' instead")))
2440 const Twine &Name = ""),Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<
uint32_t> Mask, const Twine &Name = "") __attribute__(
(deprecated("Pass indices as 'int' instead")))
2441 "Pass indices as 'int' instead")Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<
uint32_t> Mask, const Twine &Name = "") __attribute__(
(deprecated("Pass indices as 'int' instead")))
{
2442 SmallVector<int, 16> IntMask;
2443 IntMask.assign(Mask.begin(), Mask.end());
2444 return CreateShuffleVector(V1, V2, IntMask, Name);
2445 }
2446
2447 /// See class ShuffleVectorInst for a description of the mask representation.
2448 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask,
2449 const Twine &Name = "") {
2450 if (auto *V1C = dyn_cast<Constant>(V1))
2451 if (auto *V2C = dyn_cast<Constant>(V2))
2452 return Insert(Folder.CreateShuffleVector(V1C, V2C, Mask), Name);
2453 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2454 }
2455
2456 /// Create a unary shuffle. The second vector operand of the IR instruction
2457 /// is undefined.
2458 Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask,
2459 const Twine &Name = "") {
2460 return CreateShuffleVector(V, UndefValue::get(V->getType()), Mask, Name);
2461 }
2462
2463 Value *CreateExtractValue(Value *Agg,
2464 ArrayRef<unsigned> Idxs,
2465 const Twine &Name = "") {
2466 if (auto *AggC = dyn_cast<Constant>(Agg))
2467 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
2468 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2469 }
2470
2471 Value *CreateInsertValue(Value *Agg, Value *Val,
2472 ArrayRef<unsigned> Idxs,
2473 const Twine &Name = "") {
2474 if (auto *AggC = dyn_cast<Constant>(Agg))
2475 if (auto *ValC = dyn_cast<Constant>(Val))
2476 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
2477 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2478 }
2479
2480 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2481 const Twine &Name = "") {
2482 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2483 }
2484
2485 Value *CreateFreeze(Value *V, const Twine &Name = "") {
2486 return Insert(new FreezeInst(V), Name);
2487 }
2488
2489 //===--------------------------------------------------------------------===//
2490 // Utility creation methods
2491 //===--------------------------------------------------------------------===//
2492
2493 /// Return an i1 value testing if \p Arg is null.
2494 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2495 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
2496 Name);
2497 }
2498
2499 /// Return an i1 value testing if \p Arg is not null.
2500 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2501 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
2502 Name);
2503 }
2504
2505 /// Return the i64 difference between two pointer values, dividing out
2506 /// the size of the pointed-to objects.
2507 ///
2508 /// This is intended to implement C-style pointer subtraction. As such, the
2509 /// pointers must be appropriately aligned for their element types and
2510 /// pointing into the same object.
2511 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "");
2512
2513 /// Create a launder.invariant.group intrinsic call. If Ptr type is
2514 /// different from pointer to i8, it's casted to pointer to i8 in the same
2515 /// address space before call and casted back to Ptr type after call.
2516 Value *CreateLaunderInvariantGroup(Value *Ptr);
2517
2518 /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2519 /// different from pointer to i8, it's casted to pointer to i8 in the same
2520 /// address space before call and casted back to Ptr type after call.
2521 Value *CreateStripInvariantGroup(Value *Ptr);
2522
2523 /// Return a vector value that contains \arg V broadcasted to \p
2524 /// NumElts elements.
2525 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
2526
2527 /// Return a vector value that contains \arg V broadcasted to \p
2528 /// EC elements.
2529 Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
2530
2531 /// Return a value that has been extracted from a larger integer type.
2532 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
2533 IntegerType *ExtractedTy, uint64_t Offset,
2534 const Twine &Name);
2535
2536 Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base,
2537 unsigned Dimension, unsigned LastIndex,
2538 MDNode *DbgInfo);
2539
2540 Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
2541 MDNode *DbgInfo);
2542
2543 Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base,
2544 unsigned Index, unsigned FieldIndex,
2545 MDNode *DbgInfo);
2546
2547private:
2548 /// Helper function that creates an assume intrinsic call that
2549 /// represents an alignment assumption on the provided pointer \p PtrValue
2550 /// with offset \p OffsetValue and alignment value \p AlignValue.
2551 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2552 Value *PtrValue, Value *AlignValue,
2553 Value *OffsetValue);
2554
2555public:
2556 /// Create an assume intrinsic call that represents an alignment
2557 /// assumption on the provided pointer.
2558 ///
2559 /// An optional offset can be provided, and if it is provided, the offset
2560 /// must be subtracted from the provided pointer to get the pointer with the
2561 /// specified alignment.
2562 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2563 unsigned Alignment,
2564 Value *OffsetValue = nullptr);
2565
2566 /// Create an assume intrinsic call that represents an alignment
2567 /// assumption on the provided pointer.
2568 ///
2569 /// An optional offset can be provided, and if it is provided, the offset
2570 /// must be subtracted from the provided pointer to get the pointer with the
2571 /// specified alignment.
2572 ///
2573 /// This overload handles the condition where the Alignment is dependent
2574 /// on an existing value rather than a static value.
2575 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2576 Value *Alignment,
2577 Value *OffsetValue = nullptr);
2578};
2579
2580/// This provides a uniform API for creating instructions and inserting
2581/// them into a basic block: either at the end of a BasicBlock, or at a specific
2582/// iterator location in a block.
2583///
2584/// Note that the builder does not expose the full generality of LLVM
2585/// instructions. For access to extra instruction properties, use the mutators
2586/// (e.g. setVolatile) on the instructions after they have been
2587/// created. Convenience state exists to specify fast-math flags and fp-math
2588/// tags.
2589///
2590/// The first template argument specifies a class to use for creating constants.
2591/// This defaults to creating minimally folded constants. The second template
2592/// argument allows clients to specify custom insertion hooks that are called on
2593/// every newly created insertion.
2594template <typename FolderTy = ConstantFolder,
2595 typename InserterTy = IRBuilderDefaultInserter>
2596class IRBuilder : public IRBuilderBase {
2597private:
2598 FolderTy Folder;
2599 InserterTy Inserter;
2600
2601public:
2602 IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(),
2603 MDNode *FPMathTag = nullptr,
2604 ArrayRef<OperandBundleDef> OpBundles = None)
2605 : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles),
2606 Folder(Folder), Inserter(Inserter) {}
2607
2608 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
2609 ArrayRef<OperandBundleDef> OpBundles = None)
2610 : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {}
2611
2612 explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder,
2613 MDNode *FPMathTag = nullptr,
2614 ArrayRef<OperandBundleDef> OpBundles = None)
2615 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2616 FPMathTag, OpBundles), Folder(Folder) {
2617 SetInsertPoint(TheBB);
2618 }
2619
2620 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
2621 ArrayRef<OperandBundleDef> OpBundles = None)
2622 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2623 FPMathTag, OpBundles) {
2624 SetInsertPoint(TheBB);
2625 }
2626
2627 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
2628 ArrayRef<OperandBundleDef> OpBundles = None)
2629 : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter,
2630 FPMathTag, OpBundles) {
2631 SetInsertPoint(IP);
2632 }
2633
2634 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder,
2635 MDNode *FPMathTag = nullptr,
2636 ArrayRef<OperandBundleDef> OpBundles = None)
2637 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2638 FPMathTag, OpBundles), Folder(Folder) {
2639 SetInsertPoint(TheBB, IP);
2640 }
2641
2642 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
2643 MDNode *FPMathTag = nullptr,
2644 ArrayRef<OperandBundleDef> OpBundles = None)
2645 : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2646 FPMathTag, OpBundles) {
2647 SetInsertPoint(TheBB, IP);
2648 }
2649
2650 /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard
2651 /// or FastMathFlagGuard instead.
2652 IRBuilder(const IRBuilder &) = delete;
2653
2654 InserterTy &getInserter() { return Inserter; }
2655};
2656
2657// Create wrappers for C Binding types (see CBindingWrapping.h).
2658DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)inline IRBuilder<> *unwrap(LLVMBuilderRef P) { return reinterpret_cast
<IRBuilder<>*>(P); } inline LLVMBuilderRef wrap(const
IRBuilder<> *P) { return reinterpret_cast<LLVMBuilderRef
>(const_cast<IRBuilder<>*>(P)); }
2659
2660} // end namespace llvm
2661
2662#endif // LLVM_IR_IRBUILDER_H