Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -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 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/lib/Transforms/Scalar -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Transforms/Scalar -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/build-llvm/include -I /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/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-10/lib/clang/10.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-10~+201911111502510600c19528f1809/build-llvm/lib/Transforms/Scalar -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-12-09-002921-48462-1 -x c++ /build/llvm-toolchain-snapshot-10~+201911111502510600c19528f1809/llvm/lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp

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

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