Bug Summary

File:llvm/include/llvm/IR/IRBuilder.h
Warning:line 1601, column 28
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

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

/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

1//===- OpenMPIRBuilder.cpp - Builder for LLVM-IR for OpenMP directives ----===//
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/// \file
9///
10/// This file implements the OpenMPIRBuilder class, which is used as a
11/// convenient way to create LLVM instructions for OpenMP directives.
12///
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/IR/CFG.h"
20#include "llvm/IR/DebugInfo.h"
21#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/MDBuilder.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Error.h"
25#include "llvm/Transforms/Utils/BasicBlockUtils.h"
26#include "llvm/Transforms/Utils/CodeExtractor.h"
27
28#include <sstream>
29
30#define DEBUG_TYPE"openmp-ir-builder" "openmp-ir-builder"
31
32using namespace llvm;
33using namespace omp;
34
35static cl::opt<bool>
36 OptimisticAttributes("openmp-ir-builder-optimistic-attributes", cl::Hidden,
37 cl::desc("Use optimistic attributes describing "
38 "'as-if' properties of runtime calls."),
39 cl::init(false));
40
41void OpenMPIRBuilder::addAttributes(omp::RuntimeFunction FnID, Function &Fn) {
42 LLVMContext &Ctx = Fn.getContext();
43
44#define OMP_ATTRS_SET(VarName, AttrSet) AttributeSet VarName = AttrSet;
45#include "llvm/Frontend/OpenMP/OMPKinds.def"
46
47 // Add attributes to the new declaration.
48 switch (FnID) {
49#define OMP_RTL_ATTRS(Enum, FnAttrSet, RetAttrSet, ArgAttrSets) \
50 case Enum: \
51 Fn.setAttributes( \
52 AttributeList::get(Ctx, FnAttrSet, RetAttrSet, ArgAttrSets)); \
53 break;
54#include "llvm/Frontend/OpenMP/OMPKinds.def"
55 default:
56 // Attributes are optional.
57 break;
58 }
59}
60
61FunctionCallee
62OpenMPIRBuilder::getOrCreateRuntimeFunction(Module &M, RuntimeFunction FnID) {
63 FunctionType *FnTy = nullptr;
64 Function *Fn = nullptr;
65
66 // Try to find the declation in the module first.
67 switch (FnID) {
68#define OMP_RTL(Enum, Str, IsVarArg, ReturnType, ...) \
69 case Enum: \
70 FnTy = FunctionType::get(ReturnType, ArrayRef<Type *>{__VA_ARGS__}, \
71 IsVarArg); \
72 Fn = M.getFunction(Str); \
73 break;
74#include "llvm/Frontend/OpenMP/OMPKinds.def"
75 }
76
77 if (!Fn) {
78 // Create a new declaration if we need one.
79 switch (FnID) {
80#define OMP_RTL(Enum, Str, ...) \
81 case Enum: \
82 Fn = Function::Create(FnTy, GlobalValue::ExternalLinkage, Str, M); \
83 break;
84#include "llvm/Frontend/OpenMP/OMPKinds.def"
85 }
86
87 // Add information if the runtime function takes a callback function
88 if (FnID == OMPRTL___kmpc_fork_call || FnID == OMPRTL___kmpc_fork_teams) {
89 if (!Fn->hasMetadata(LLVMContext::MD_callback)) {
90 LLVMContext &Ctx = Fn->getContext();
91 MDBuilder MDB(Ctx);
92 // Annotate the callback behavior of the runtime function:
93 // - The callback callee is argument number 2 (microtask).
94 // - The first two arguments of the callback callee are unknown (-1).
95 // - All variadic arguments to the runtime function are passed to the
96 // callback callee.
97 Fn->addMetadata(
98 LLVMContext::MD_callback,
99 *MDNode::get(Ctx, {MDB.createCallbackEncoding(
100 2, {-1, -1}, /* VarArgsArePassed */ true)}));
101 }
102 }
103
104 LLVM_DEBUG(dbgs() << "Created OpenMP runtime function " << Fn->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Created OpenMP runtime function "
<< Fn->getName() << " with type " << *Fn
->getFunctionType() << "\n"; } } while (false)
105 << " with type " << *Fn->getFunctionType() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Created OpenMP runtime function "
<< Fn->getName() << " with type " << *Fn
->getFunctionType() << "\n"; } } while (false)
;
106 addAttributes(FnID, *Fn);
107
108 } else {
109 LLVM_DEBUG(dbgs() << "Found OpenMP runtime function " << Fn->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Found OpenMP runtime function "
<< Fn->getName() << " with type " << *Fn
->getFunctionType() << "\n"; } } while (false)
110 << " with type " << *Fn->getFunctionType() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Found OpenMP runtime function "
<< Fn->getName() << " with type " << *Fn
->getFunctionType() << "\n"; } } while (false)
;
111 }
112
113 assert(Fn && "Failed to create OpenMP runtime function")((Fn && "Failed to create OpenMP runtime function") ?
static_cast<void> (0) : __assert_fail ("Fn && \"Failed to create OpenMP runtime function\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 113, __PRETTY_FUNCTION__))
;
114
115 // Cast the function to the expected type if necessary
116 Constant *C = ConstantExpr::getBitCast(Fn, FnTy->getPointerTo());
117 return {FnTy, C};
118}
119
120Function *OpenMPIRBuilder::getOrCreateRuntimeFunctionPtr(RuntimeFunction FnID) {
121 FunctionCallee RTLFn = getOrCreateRuntimeFunction(M, FnID);
122 auto *Fn = dyn_cast<llvm::Function>(RTLFn.getCallee());
123 assert(Fn && "Failed to create OpenMP runtime function pointer")((Fn && "Failed to create OpenMP runtime function pointer"
) ? static_cast<void> (0) : __assert_fail ("Fn && \"Failed to create OpenMP runtime function pointer\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 123, __PRETTY_FUNCTION__))
;
124 return Fn;
125}
126
127void OpenMPIRBuilder::initialize() { initializeTypes(M); }
128
129void OpenMPIRBuilder::finalize() {
130 SmallPtrSet<BasicBlock *, 32> ParallelRegionBlockSet;
131 SmallVector<BasicBlock *, 32> Blocks;
132 for (OutlineInfo &OI : OutlineInfos) {
133 ParallelRegionBlockSet.clear();
134 Blocks.clear();
135 OI.collectBlocks(ParallelRegionBlockSet, Blocks);
136
137 Function *OuterFn = OI.EntryBB->getParent();
138 CodeExtractorAnalysisCache CEAC(*OuterFn);
139 CodeExtractor Extractor(Blocks, /* DominatorTree */ nullptr,
140 /* AggregateArgs */ false,
141 /* BlockFrequencyInfo */ nullptr,
142 /* BranchProbabilityInfo */ nullptr,
143 /* AssumptionCache */ nullptr,
144 /* AllowVarArgs */ true,
145 /* AllowAlloca */ true,
146 /* Suffix */ ".omp_par");
147
148 LLVM_DEBUG(dbgs() << "Before outlining: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Before outlining: "
<< *OuterFn << "\n"; } } while (false)
;
149 LLVM_DEBUG(dbgs() << "Entry " << OI.EntryBB->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Entry " << OI
.EntryBB->getName() << " Exit: " << OI.ExitBB->
getName() << "\n"; } } while (false)
150 << " Exit: " << OI.ExitBB->getName() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Entry " << OI
.EntryBB->getName() << " Exit: " << OI.ExitBB->
getName() << "\n"; } } while (false)
;
151 assert(Extractor.isEligible() &&((Extractor.isEligible() && "Expected OpenMP outlining to be possible!"
) ? static_cast<void> (0) : __assert_fail ("Extractor.isEligible() && \"Expected OpenMP outlining to be possible!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 152, __PRETTY_FUNCTION__))
152 "Expected OpenMP outlining to be possible!")((Extractor.isEligible() && "Expected OpenMP outlining to be possible!"
) ? static_cast<void> (0) : __assert_fail ("Extractor.isEligible() && \"Expected OpenMP outlining to be possible!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 152, __PRETTY_FUNCTION__))
;
153
154 Function *OutlinedFn = Extractor.extractCodeRegion(CEAC);
155
156 LLVM_DEBUG(dbgs() << "After outlining: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "After outlining: "
<< *OuterFn << "\n"; } } while (false)
;
157 LLVM_DEBUG(dbgs() << " Outlined function: " << *OutlinedFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << " Outlined function: "
<< *OutlinedFn << "\n"; } } while (false)
;
158 assert(OutlinedFn->getReturnType()->isVoidTy() &&((OutlinedFn->getReturnType()->isVoidTy() && "OpenMP outlined functions should not return a value!"
) ? static_cast<void> (0) : __assert_fail ("OutlinedFn->getReturnType()->isVoidTy() && \"OpenMP outlined functions should not return a value!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 159, __PRETTY_FUNCTION__))
159 "OpenMP outlined functions should not return a value!")((OutlinedFn->getReturnType()->isVoidTy() && "OpenMP outlined functions should not return a value!"
) ? static_cast<void> (0) : __assert_fail ("OutlinedFn->getReturnType()->isVoidTy() && \"OpenMP outlined functions should not return a value!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 159, __PRETTY_FUNCTION__))
;
160
161 // For compability with the clang CG we move the outlined function after the
162 // one with the parallel region.
163 OutlinedFn->removeFromParent();
164 M.getFunctionList().insertAfter(OuterFn->getIterator(), OutlinedFn);
165
166 // Remove the artificial entry introduced by the extractor right away, we
167 // made our own entry block after all.
168 {
169 BasicBlock &ArtificialEntry = OutlinedFn->getEntryBlock();
170 assert(ArtificialEntry.getUniqueSuccessor() == OI.EntryBB)((ArtificialEntry.getUniqueSuccessor() == OI.EntryBB) ? static_cast
<void> (0) : __assert_fail ("ArtificialEntry.getUniqueSuccessor() == OI.EntryBB"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 170, __PRETTY_FUNCTION__))
;
171 assert(OI.EntryBB->getUniquePredecessor() == &ArtificialEntry)((OI.EntryBB->getUniquePredecessor() == &ArtificialEntry
) ? static_cast<void> (0) : __assert_fail ("OI.EntryBB->getUniquePredecessor() == &ArtificialEntry"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 171, __PRETTY_FUNCTION__))
;
172 OI.EntryBB->moveBefore(&ArtificialEntry);
173 ArtificialEntry.eraseFromParent();
174 }
175 assert(&OutlinedFn->getEntryBlock() == OI.EntryBB)((&OutlinedFn->getEntryBlock() == OI.EntryBB) ? static_cast
<void> (0) : __assert_fail ("&OutlinedFn->getEntryBlock() == OI.EntryBB"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 175, __PRETTY_FUNCTION__))
;
176 assert(OutlinedFn && OutlinedFn->getNumUses() == 1)((OutlinedFn && OutlinedFn->getNumUses() == 1) ? static_cast
<void> (0) : __assert_fail ("OutlinedFn && OutlinedFn->getNumUses() == 1"
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 176, __PRETTY_FUNCTION__))
;
177
178 // Run a user callback, e.g. to add attributes.
179 if (OI.PostOutlineCB)
180 OI.PostOutlineCB(*OutlinedFn);
181 }
182
183 // Allow finalize to be called multiple times.
184 OutlineInfos.clear();
185}
186
187Value *OpenMPIRBuilder::getOrCreateIdent(Constant *SrcLocStr,
188 IdentFlag LocFlags,
189 unsigned Reserve2Flags) {
190 // Enable "C-mode".
191 LocFlags |= OMP_IDENT_FLAG_KMPC;
192
193 Value *&Ident =
194 IdentMap[{SrcLocStr, uint64_t(LocFlags) << 31 | Reserve2Flags}];
195 if (!Ident) {
196 Constant *I32Null = ConstantInt::getNullValue(Int32);
197 Constant *IdentData[] = {
198 I32Null, ConstantInt::get(Int32, uint32_t(LocFlags)),
199 ConstantInt::get(Int32, Reserve2Flags), I32Null, SrcLocStr};
200 Constant *Initializer = ConstantStruct::get(
201 cast<StructType>(IdentPtr->getPointerElementType()), IdentData);
202
203 // Look for existing encoding of the location + flags, not needed but
204 // minimizes the difference to the existing solution while we transition.
205 for (GlobalVariable &GV : M.getGlobalList())
206 if (GV.getType() == IdentPtr && GV.hasInitializer())
207 if (GV.getInitializer() == Initializer)
208 return Ident = &GV;
209
210 auto *GV = new GlobalVariable(M, IdentPtr->getPointerElementType(),
211 /* isConstant = */ true,
212 GlobalValue::PrivateLinkage, Initializer);
213 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
214 GV->setAlignment(Align(8));
215 Ident = GV;
216 }
217 return Ident;
218}
219
220Constant *OpenMPIRBuilder::getOrCreateSrcLocStr(StringRef LocStr) {
221 Constant *&SrcLocStr = SrcLocStrMap[LocStr];
222 if (!SrcLocStr) {
223 Constant *Initializer =
224 ConstantDataArray::getString(M.getContext(), LocStr);
225
226 // Look for existing encoding of the location, not needed but minimizes the
227 // difference to the existing solution while we transition.
228 for (GlobalVariable &GV : M.getGlobalList())
229 if (GV.isConstant() && GV.hasInitializer() &&
230 GV.getInitializer() == Initializer)
231 return SrcLocStr = ConstantExpr::getPointerCast(&GV, Int8Ptr);
232
233 SrcLocStr = Builder.CreateGlobalStringPtr(LocStr, /* Name */ "",
234 /* AddressSpace */ 0, &M);
235 }
236 return SrcLocStr;
237}
238
239Constant *OpenMPIRBuilder::getOrCreateSrcLocStr(StringRef FunctionName,
240 StringRef FileName,
241 unsigned Line,
242 unsigned Column) {
243 SmallString<128> Buffer;
244 Buffer.push_back(';');
245 Buffer.append(FileName);
246 Buffer.push_back(';');
247 Buffer.append(FunctionName);
248 Buffer.push_back(';');
249 Buffer.append(std::to_string(Line));
250 Buffer.push_back(';');
251 Buffer.append(std::to_string(Column));
252 Buffer.push_back(';');
253 Buffer.push_back(';');
254 return getOrCreateSrcLocStr(Buffer.str());
255}
256
257Constant *OpenMPIRBuilder::getOrCreateDefaultSrcLocStr() {
258 return getOrCreateSrcLocStr(";unknown;unknown;0;0;;");
259}
260
261Constant *
262OpenMPIRBuilder::getOrCreateSrcLocStr(const LocationDescription &Loc) {
263 DILocation *DIL = Loc.DL.get();
264 if (!DIL)
265 return getOrCreateDefaultSrcLocStr();
266 StringRef FileName = M.getName();
267 if (DIFile *DIF = DIL->getFile())
268 if (Optional<StringRef> Source = DIF->getSource())
269 FileName = *Source;
270 StringRef Function = DIL->getScope()->getSubprogram()->getName();
271 Function =
272 !Function.empty() ? Function : Loc.IP.getBlock()->getParent()->getName();
273 return getOrCreateSrcLocStr(Function, FileName, DIL->getLine(),
274 DIL->getColumn());
275}
276
277Value *OpenMPIRBuilder::getOrCreateThreadID(Value *Ident) {
278 return Builder.CreateCall(
279 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_global_thread_num), Ident,
280 "omp_global_thread_num");
281}
282
283OpenMPIRBuilder::InsertPointTy
284OpenMPIRBuilder::CreateBarrier(const LocationDescription &Loc, Directive DK,
285 bool ForceSimpleCall, bool CheckCancelFlag) {
286 if (!updateToLocation(Loc))
287 return Loc.IP;
288 return emitBarrierImpl(Loc, DK, ForceSimpleCall, CheckCancelFlag);
289}
290
291OpenMPIRBuilder::InsertPointTy
292OpenMPIRBuilder::emitBarrierImpl(const LocationDescription &Loc, Directive Kind,
293 bool ForceSimpleCall, bool CheckCancelFlag) {
294 // Build call __kmpc_cancel_barrier(loc, thread_id) or
295 // __kmpc_barrier(loc, thread_id);
296
297 IdentFlag BarrierLocFlags;
298 switch (Kind) {
299 case OMPD_for:
300 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_FOR;
301 break;
302 case OMPD_sections:
303 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_SECTIONS;
304 break;
305 case OMPD_single:
306 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_SINGLE;
307 break;
308 case OMPD_barrier:
309 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_EXPL;
310 break;
311 default:
312 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL;
313 break;
314 }
315
316 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
317 Value *Args[] = {getOrCreateIdent(SrcLocStr, BarrierLocFlags),
318 getOrCreateThreadID(getOrCreateIdent(SrcLocStr))};
319
320 // If we are in a cancellable parallel region, barriers are cancellation
321 // points.
322 // TODO: Check why we would force simple calls or to ignore the cancel flag.
323 bool UseCancelBarrier =
324 !ForceSimpleCall && isLastFinalizationInfoCancellable(OMPD_parallel);
325
326 Value *Result =
327 Builder.CreateCall(getOrCreateRuntimeFunctionPtr(
328 UseCancelBarrier ? OMPRTL___kmpc_cancel_barrier
329 : OMPRTL___kmpc_barrier),
330 Args);
331
332 if (UseCancelBarrier && CheckCancelFlag)
333 emitCancelationCheckImpl(Result, OMPD_parallel);
334
335 return Builder.saveIP();
336}
337
338OpenMPIRBuilder::InsertPointTy
339OpenMPIRBuilder::CreateCancel(const LocationDescription &Loc,
340 Value *IfCondition,
341 omp::Directive CanceledDirective) {
342 if (!updateToLocation(Loc))
343 return Loc.IP;
344
345 // LLVM utilities like blocks with terminators.
346 auto *UI = Builder.CreateUnreachable();
347
348 Instruction *ThenTI = UI, *ElseTI = nullptr;
349 if (IfCondition)
350 SplitBlockAndInsertIfThenElse(IfCondition, UI, &ThenTI, &ElseTI);
351 Builder.SetInsertPoint(ThenTI);
352
353 Value *CancelKind = nullptr;
354 switch (CanceledDirective) {
355#define OMP_CANCEL_KIND(Enum, Str, DirectiveEnum, Value) \
356 case DirectiveEnum: \
357 CancelKind = Builder.getInt32(Value); \
358 break;
359#include "llvm/Frontend/OpenMP/OMPKinds.def"
360 default:
361 llvm_unreachable("Unknown cancel kind!")::llvm::llvm_unreachable_internal("Unknown cancel kind!", "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 361)
;
362 }
363
364 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
365 Value *Ident = getOrCreateIdent(SrcLocStr);
366 Value *Args[] = {Ident, getOrCreateThreadID(Ident), CancelKind};
367 Value *Result = Builder.CreateCall(
368 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_cancel), Args);
369
370 // The actual cancel logic is shared with others, e.g., cancel_barriers.
371 emitCancelationCheckImpl(Result, CanceledDirective);
372
373 // Update the insertion point and remove the terminator we introduced.
374 Builder.SetInsertPoint(UI->getParent());
375 UI->eraseFromParent();
376
377 return Builder.saveIP();
378}
379
380void OpenMPIRBuilder::emitCancelationCheckImpl(
381 Value *CancelFlag, omp::Directive CanceledDirective) {
382 assert(isLastFinalizationInfoCancellable(CanceledDirective) &&((isLastFinalizationInfoCancellable(CanceledDirective) &&
"Unexpected cancellation!") ? static_cast<void> (0) : __assert_fail
("isLastFinalizationInfoCancellable(CanceledDirective) && \"Unexpected cancellation!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 383, __PRETTY_FUNCTION__))
383 "Unexpected cancellation!")((isLastFinalizationInfoCancellable(CanceledDirective) &&
"Unexpected cancellation!") ? static_cast<void> (0) : __assert_fail
("isLastFinalizationInfoCancellable(CanceledDirective) && \"Unexpected cancellation!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 383, __PRETTY_FUNCTION__))
;
384
385 // For a cancel barrier we create two new blocks.
386 BasicBlock *BB = Builder.GetInsertBlock();
387 BasicBlock *NonCancellationBlock;
388 if (Builder.GetInsertPoint() == BB->end()) {
389 // TODO: This branch will not be needed once we moved to the
390 // OpenMPIRBuilder codegen completely.
391 NonCancellationBlock = BasicBlock::Create(
392 BB->getContext(), BB->getName() + ".cont", BB->getParent());
393 } else {
394 NonCancellationBlock = SplitBlock(BB, &*Builder.GetInsertPoint());
395 BB->getTerminator()->eraseFromParent();
396 Builder.SetInsertPoint(BB);
397 }
398 BasicBlock *CancellationBlock = BasicBlock::Create(
399 BB->getContext(), BB->getName() + ".cncl", BB->getParent());
400
401 // Jump to them based on the return value.
402 Value *Cmp = Builder.CreateIsNull(CancelFlag);
403 Builder.CreateCondBr(Cmp, NonCancellationBlock, CancellationBlock,
404 /* TODO weight */ nullptr, nullptr);
405
406 // From the cancellation block we finalize all variables and go to the
407 // post finalization block that is known to the FiniCB callback.
408 Builder.SetInsertPoint(CancellationBlock);
409 auto &FI = FinalizationStack.back();
410 FI.FiniCB(Builder.saveIP());
411
412 // The continuation block is where code generation continues.
413 Builder.SetInsertPoint(NonCancellationBlock, NonCancellationBlock->begin());
414}
415
416IRBuilder<>::InsertPoint OpenMPIRBuilder::CreateParallel(
417 const LocationDescription &Loc, InsertPointTy OuterAllocaIP,
418 BodyGenCallbackTy BodyGenCB, PrivatizeCallbackTy PrivCB,
419 FinalizeCallbackTy FiniCB, Value *IfCondition, Value *NumThreads,
420 omp::ProcBindKind ProcBind, bool IsCancellable) {
421 if (!updateToLocation(Loc))
1
Taking false branch
422 return Loc.IP;
423
424 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
425 Value *Ident = getOrCreateIdent(SrcLocStr);
426 Value *ThreadID = getOrCreateThreadID(Ident);
427
428 if (NumThreads) {
2
Assuming 'NumThreads' is null
3
Taking false branch
429 // Build call __kmpc_push_num_threads(&Ident, global_tid, num_threads)
430 Value *Args[] = {
431 Ident, ThreadID,
432 Builder.CreateIntCast(NumThreads, Int32, /*isSigned*/ false)};
433 Builder.CreateCall(
434 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_push_num_threads), Args);
435 }
436
437 if (ProcBind != OMP_PROC_BIND_default) {
4
Assuming 'ProcBind' is equal to 'OMP_PROC_BIND_default'
5
Taking false branch
438 // Build call __kmpc_push_proc_bind(&Ident, global_tid, proc_bind)
439 Value *Args[] = {
440 Ident, ThreadID,
441 ConstantInt::get(Int32, unsigned(ProcBind), /*isSigned=*/true)};
442 Builder.CreateCall(
443 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_push_proc_bind), Args);
444 }
445
446 BasicBlock *InsertBB = Builder.GetInsertBlock();
447 Function *OuterFn = InsertBB->getParent();
448
449 // Vector to remember instructions we used only during the modeling but which
450 // we want to delete at the end.
451 SmallVector<Instruction *, 4> ToBeDeleted;
452
453 // Change the location to the outer alloca insertion point to create and
454 // initialize the allocas we pass into the parallel region.
455 Builder.restoreIP(OuterAllocaIP);
6
Calling 'IRBuilderBase::restoreIP'
11
Returning from 'IRBuilderBase::restoreIP'
456 AllocaInst *TIDAddr = Builder.CreateAlloca(Int32, nullptr, "tid.addr");
12
Calling 'IRBuilderBase::CreateAlloca'
457 AllocaInst *ZeroAddr = Builder.CreateAlloca(Int32, nullptr, "zero.addr");
458
459 // If there is an if condition we actually use the TIDAddr and ZeroAddr in the
460 // program, otherwise we only need them for modeling purposes to get the
461 // associated arguments in the outlined function. In the former case,
462 // initialize the allocas properly, in the latter case, delete them later.
463 if (IfCondition) {
464 Builder.CreateStore(Constant::getNullValue(Int32), TIDAddr);
465 Builder.CreateStore(Constant::getNullValue(Int32), ZeroAddr);
466 } else {
467 ToBeDeleted.push_back(TIDAddr);
468 ToBeDeleted.push_back(ZeroAddr);
469 }
470
471 // Create an artificial insertion point that will also ensure the blocks we
472 // are about to split are not degenerated.
473 auto *UI = new UnreachableInst(Builder.getContext(), InsertBB);
474
475 Instruction *ThenTI = UI, *ElseTI = nullptr;
476 if (IfCondition)
477 SplitBlockAndInsertIfThenElse(IfCondition, UI, &ThenTI, &ElseTI);
478
479 BasicBlock *ThenBB = ThenTI->getParent();
480 BasicBlock *PRegEntryBB = ThenBB->splitBasicBlock(ThenTI, "omp.par.entry");
481 BasicBlock *PRegBodyBB =
482 PRegEntryBB->splitBasicBlock(ThenTI, "omp.par.region");
483 BasicBlock *PRegPreFiniBB =
484 PRegBodyBB->splitBasicBlock(ThenTI, "omp.par.pre_finalize");
485 BasicBlock *PRegExitBB =
486 PRegPreFiniBB->splitBasicBlock(ThenTI, "omp.par.exit");
487
488 auto FiniCBWrapper = [&](InsertPointTy IP) {
489 // Hide "open-ended" blocks from the given FiniCB by setting the right jump
490 // target to the region exit block.
491 if (IP.getBlock()->end() == IP.getPoint()) {
492 IRBuilder<>::InsertPointGuard IPG(Builder);
493 Builder.restoreIP(IP);
494 Instruction *I = Builder.CreateBr(PRegExitBB);
495 IP = InsertPointTy(I->getParent(), I->getIterator());
496 }
497 assert(IP.getBlock()->getTerminator()->getNumSuccessors() == 1 &&((IP.getBlock()->getTerminator()->getNumSuccessors() ==
1 && IP.getBlock()->getTerminator()->getSuccessor
(0) == PRegExitBB && "Unexpected insertion point for finalization call!"
) ? static_cast<void> (0) : __assert_fail ("IP.getBlock()->getTerminator()->getNumSuccessors() == 1 && IP.getBlock()->getTerminator()->getSuccessor(0) == PRegExitBB && \"Unexpected insertion point for finalization call!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 499, __PRETTY_FUNCTION__))
498 IP.getBlock()->getTerminator()->getSuccessor(0) == PRegExitBB &&((IP.getBlock()->getTerminator()->getNumSuccessors() ==
1 && IP.getBlock()->getTerminator()->getSuccessor
(0) == PRegExitBB && "Unexpected insertion point for finalization call!"
) ? static_cast<void> (0) : __assert_fail ("IP.getBlock()->getTerminator()->getNumSuccessors() == 1 && IP.getBlock()->getTerminator()->getSuccessor(0) == PRegExitBB && \"Unexpected insertion point for finalization call!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 499, __PRETTY_FUNCTION__))
499 "Unexpected insertion point for finalization call!")((IP.getBlock()->getTerminator()->getNumSuccessors() ==
1 && IP.getBlock()->getTerminator()->getSuccessor
(0) == PRegExitBB && "Unexpected insertion point for finalization call!"
) ? static_cast<void> (0) : __assert_fail ("IP.getBlock()->getTerminator()->getNumSuccessors() == 1 && IP.getBlock()->getTerminator()->getSuccessor(0) == PRegExitBB && \"Unexpected insertion point for finalization call!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 499, __PRETTY_FUNCTION__))
;
500 return FiniCB(IP);
501 };
502
503 FinalizationStack.push_back({FiniCBWrapper, OMPD_parallel, IsCancellable});
504
505 // Generate the privatization allocas in the block that will become the entry
506 // of the outlined function.
507 Builder.SetInsertPoint(PRegEntryBB->getTerminator());
508 InsertPointTy InnerAllocaIP = Builder.saveIP();
509
510 AllocaInst *PrivTIDAddr =
511 Builder.CreateAlloca(Int32, nullptr, "tid.addr.local");
512 Instruction *PrivTID = Builder.CreateLoad(PrivTIDAddr, "tid");
513
514 // Add some fake uses for OpenMP provided arguments.
515 ToBeDeleted.push_back(Builder.CreateLoad(TIDAddr, "tid.addr.use"));
516 ToBeDeleted.push_back(Builder.CreateLoad(ZeroAddr, "zero.addr.use"));
517
518 // ThenBB
519 // |
520 // V
521 // PRegionEntryBB <- Privatization allocas are placed here.
522 // |
523 // V
524 // PRegionBodyBB <- BodeGen is invoked here.
525 // |
526 // V
527 // PRegPreFiniBB <- The block we will start finalization from.
528 // |
529 // V
530 // PRegionExitBB <- A common exit to simplify block collection.
531 //
532
533 LLVM_DEBUG(dbgs() << "Before body codegen: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Before body codegen: "
<< *OuterFn << "\n"; } } while (false)
;
534
535 // Let the caller create the body.
536 assert(BodyGenCB && "Expected body generation callback!")((BodyGenCB && "Expected body generation callback!") ?
static_cast<void> (0) : __assert_fail ("BodyGenCB && \"Expected body generation callback!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 536, __PRETTY_FUNCTION__))
;
537 InsertPointTy CodeGenIP(PRegBodyBB, PRegBodyBB->begin());
538 BodyGenCB(InnerAllocaIP, CodeGenIP, *PRegPreFiniBB);
539
540 LLVM_DEBUG(dbgs() << "After body codegen: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "After body codegen: "
<< *OuterFn << "\n"; } } while (false)
;
541
542 FunctionCallee RTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_fork_call);
543 if (auto *F = dyn_cast<llvm::Function>(RTLFn.getCallee())) {
544 if (!F->hasMetadata(llvm::LLVMContext::MD_callback)) {
545 llvm::LLVMContext &Ctx = F->getContext();
546 MDBuilder MDB(Ctx);
547 // Annotate the callback behavior of the __kmpc_fork_call:
548 // - The callback callee is argument number 2 (microtask).
549 // - The first two arguments of the callback callee are unknown (-1).
550 // - All variadic arguments to the __kmpc_fork_call are passed to the
551 // callback callee.
552 F->addMetadata(
553 llvm::LLVMContext::MD_callback,
554 *llvm::MDNode::get(
555 Ctx, {MDB.createCallbackEncoding(2, {-1, -1},
556 /* VarArgsArePassed */ true)}));
557 }
558 }
559
560 OutlineInfo OI;
561 OI.PostOutlineCB = [=](Function &OutlinedFn) {
562 // Add some known attributes.
563 OutlinedFn.addParamAttr(0, Attribute::NoAlias);
564 OutlinedFn.addParamAttr(1, Attribute::NoAlias);
565 OutlinedFn.addFnAttr(Attribute::NoUnwind);
566 OutlinedFn.addFnAttr(Attribute::NoRecurse);
567
568 assert(OutlinedFn.arg_size() >= 2 &&((OutlinedFn.arg_size() >= 2 && "Expected at least tid and bounded tid as arguments"
) ? static_cast<void> (0) : __assert_fail ("OutlinedFn.arg_size() >= 2 && \"Expected at least tid and bounded tid as arguments\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 569, __PRETTY_FUNCTION__))
569 "Expected at least tid and bounded tid as arguments")((OutlinedFn.arg_size() >= 2 && "Expected at least tid and bounded tid as arguments"
) ? static_cast<void> (0) : __assert_fail ("OutlinedFn.arg_size() >= 2 && \"Expected at least tid and bounded tid as arguments\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 569, __PRETTY_FUNCTION__))
;
570 unsigned NumCapturedVars =
571 OutlinedFn.arg_size() - /* tid & bounded tid */ 2;
572
573 CallInst *CI = cast<CallInst>(OutlinedFn.user_back());
574 CI->getParent()->setName("omp_parallel");
575 Builder.SetInsertPoint(CI);
576
577 // Build call __kmpc_fork_call(Ident, n, microtask, var1, .., varn);
578 Value *ForkCallArgs[] = {
579 Ident, Builder.getInt32(NumCapturedVars),
580 Builder.CreateBitCast(&OutlinedFn, ParallelTaskPtr)};
581
582 SmallVector<Value *, 16> RealArgs;
583 RealArgs.append(std::begin(ForkCallArgs), std::end(ForkCallArgs));
584 RealArgs.append(CI->arg_begin() + /* tid & bound tid */ 2, CI->arg_end());
585
586 Builder.CreateCall(RTLFn, RealArgs);
587
588 LLVM_DEBUG(dbgs() << "With fork_call placed: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "With fork_call placed: "
<< *Builder.GetInsertBlock()->getParent() << "\n"
; } } while (false)
589 << *Builder.GetInsertBlock()->getParent() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "With fork_call placed: "
<< *Builder.GetInsertBlock()->getParent() << "\n"
; } } while (false)
;
590
591 InsertPointTy ExitIP(PRegExitBB, PRegExitBB->end());
592
593 // Initialize the local TID stack location with the argument value.
594 Builder.SetInsertPoint(PrivTID);
595 Function::arg_iterator OutlinedAI = OutlinedFn.arg_begin();
596 Builder.CreateStore(Builder.CreateLoad(OutlinedAI), PrivTIDAddr);
597
598 // If no "if" clause was present we do not need the call created during
599 // outlining, otherwise we reuse it in the serialized parallel region.
600 if (!ElseTI) {
601 CI->eraseFromParent();
602 } else {
603
604 // If an "if" clause was present we are now generating the serialized
605 // version into the "else" branch.
606 Builder.SetInsertPoint(ElseTI);
607
608 // Build calls __kmpc_serialized_parallel(&Ident, GTid);
609 Value *SerializedParallelCallArgs[] = {Ident, ThreadID};
610 Builder.CreateCall(
611 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_serialized_parallel),
612 SerializedParallelCallArgs);
613
614 // OutlinedFn(&GTid, &zero, CapturedStruct);
615 CI->removeFromParent();
616 Builder.Insert(CI);
617
618 // __kmpc_end_serialized_parallel(&Ident, GTid);
619 Value *EndArgs[] = {Ident, ThreadID};
620 Builder.CreateCall(
621 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_serialized_parallel),
622 EndArgs);
623
624 LLVM_DEBUG(dbgs() << "With serialized parallel region: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "With serialized parallel region: "
<< *Builder.GetInsertBlock()->getParent() << "\n"
; } } while (false)
625 << *Builder.GetInsertBlock()->getParent() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "With serialized parallel region: "
<< *Builder.GetInsertBlock()->getParent() << "\n"
; } } while (false)
;
626 }
627
628 for (Instruction *I : ToBeDeleted)
629 I->eraseFromParent();
630 };
631
632 // Adjust the finalization stack, verify the adjustment, and call the
633 // finalize function a last time to finalize values between the pre-fini
634 // block and the exit block if we left the parallel "the normal way".
635 auto FiniInfo = FinalizationStack.pop_back_val();
636 (void)FiniInfo;
637 assert(FiniInfo.DK == OMPD_parallel &&((FiniInfo.DK == OMPD_parallel && "Unexpected finalization stack state!"
) ? static_cast<void> (0) : __assert_fail ("FiniInfo.DK == OMPD_parallel && \"Unexpected finalization stack state!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 638, __PRETTY_FUNCTION__))
638 "Unexpected finalization stack state!")((FiniInfo.DK == OMPD_parallel && "Unexpected finalization stack state!"
) ? static_cast<void> (0) : __assert_fail ("FiniInfo.DK == OMPD_parallel && \"Unexpected finalization stack state!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 638, __PRETTY_FUNCTION__))
;
639
640 Instruction *PRegPreFiniTI = PRegPreFiniBB->getTerminator();
641
642 InsertPointTy PreFiniIP(PRegPreFiniBB, PRegPreFiniTI->getIterator());
643 FiniCB(PreFiniIP);
644
645 OI.EntryBB = PRegEntryBB;
646 OI.ExitBB = PRegExitBB;
647
648 SmallPtrSet<BasicBlock *, 32> ParallelRegionBlockSet;
649 SmallVector<BasicBlock *, 32> Blocks;
650 OI.collectBlocks(ParallelRegionBlockSet, Blocks);
651
652 // Ensure a single exit node for the outlined region by creating one.
653 // We might have multiple incoming edges to the exit now due to finalizations,
654 // e.g., cancel calls that cause the control flow to leave the region.
655 BasicBlock *PRegOutlinedExitBB = PRegExitBB;
656 PRegExitBB = SplitBlock(PRegExitBB, &*PRegExitBB->getFirstInsertionPt());
657 PRegOutlinedExitBB->setName("omp.par.outlined.exit");
658 Blocks.push_back(PRegOutlinedExitBB);
659
660 CodeExtractorAnalysisCache CEAC(*OuterFn);
661 CodeExtractor Extractor(Blocks, /* DominatorTree */ nullptr,
662 /* AggregateArgs */ false,
663 /* BlockFrequencyInfo */ nullptr,
664 /* BranchProbabilityInfo */ nullptr,
665 /* AssumptionCache */ nullptr,
666 /* AllowVarArgs */ true,
667 /* AllowAlloca */ true,
668 /* Suffix */ ".omp_par");
669
670 // Find inputs to, outputs from the code region.
671 BasicBlock *CommonExit = nullptr;
672 SetVector<Value *> Inputs, Outputs, SinkingCands, HoistingCands;
673 Extractor.findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
674 Extractor.findInputsOutputs(Inputs, Outputs, SinkingCands);
675
676 LLVM_DEBUG(dbgs() << "Before privatization: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Before privatization: "
<< *OuterFn << "\n"; } } while (false)
;
677
678 FunctionCallee TIDRTLFn =
679 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_global_thread_num);
680
681 auto PrivHelper = [&](Value &V) {
682 if (&V == TIDAddr || &V == ZeroAddr)
683 return;
684
685 SmallVector<Use *, 8> Uses;
686 for (Use &U : V.uses())
687 if (auto *UserI = dyn_cast<Instruction>(U.getUser()))
688 if (ParallelRegionBlockSet.count(UserI->getParent()))
689 Uses.push_back(&U);
690
691 Value *ReplacementValue = nullptr;
692 CallInst *CI = dyn_cast<CallInst>(&V);
693 if (CI && CI->getCalledFunction() == TIDRTLFn.getCallee()) {
694 ReplacementValue = PrivTID;
695 } else {
696 Builder.restoreIP(
697 PrivCB(InnerAllocaIP, Builder.saveIP(), V, ReplacementValue));
698 assert(ReplacementValue &&((ReplacementValue && "Expected copy/create callback to set replacement value!"
) ? static_cast<void> (0) : __assert_fail ("ReplacementValue && \"Expected copy/create callback to set replacement value!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 699, __PRETTY_FUNCTION__))
699 "Expected copy/create callback to set replacement value!")((ReplacementValue && "Expected copy/create callback to set replacement value!"
) ? static_cast<void> (0) : __assert_fail ("ReplacementValue && \"Expected copy/create callback to set replacement value!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 699, __PRETTY_FUNCTION__))
;
700 if (ReplacementValue == &V)
701 return;
702 }
703
704 for (Use *UPtr : Uses)
705 UPtr->set(ReplacementValue);
706 };
707
708 for (Value *Input : Inputs) {
709 LLVM_DEBUG(dbgs() << "Captured input: " << *Input << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Captured input: " <<
*Input << "\n"; } } while (false)
;
710 PrivHelper(*Input);
711 }
712 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (Value *Output : Outputs) do {
if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(
"openmp-ir-builder")) { dbgs() << "Captured output: " <<
*Output << "\n"; } } while (false); }; } } while (false
)
713 for (Value *Output : Outputs)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (Value *Output : Outputs) do {
if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(
"openmp-ir-builder")) { dbgs() << "Captured output: " <<
*Output << "\n"; } } while (false); }; } } while (false
)
714 LLVM_DEBUG(dbgs() << "Captured output: " << *Output << "\n");do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (Value *Output : Outputs) do {
if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(
"openmp-ir-builder")) { dbgs() << "Captured output: " <<
*Output << "\n"; } } while (false); }; } } while (false
)
715 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (Value *Output : Outputs) do {
if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(
"openmp-ir-builder")) { dbgs() << "Captured output: " <<
*Output << "\n"; } } while (false); }; } } while (false
)
;
716 assert(Outputs.empty() &&((Outputs.empty() && "OpenMP outlining should not produce live-out values!"
) ? static_cast<void> (0) : __assert_fail ("Outputs.empty() && \"OpenMP outlining should not produce live-out values!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 717, __PRETTY_FUNCTION__))
717 "OpenMP outlining should not produce live-out values!")((Outputs.empty() && "OpenMP outlining should not produce live-out values!"
) ? static_cast<void> (0) : __assert_fail ("Outputs.empty() && \"OpenMP outlining should not produce live-out values!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 717, __PRETTY_FUNCTION__))
;
718
719 LLVM_DEBUG(dbgs() << "After privatization: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "After privatization: "
<< *OuterFn << "\n"; } } while (false)
;
720 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (auto *BB : Blocks) dbgs() <<
" PBR: " << BB->getName() << "\n"; }; } } while
(false)
721 for (auto *BB : Blocks)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (auto *BB : Blocks) dbgs() <<
" PBR: " << BB->getName() << "\n"; }; } } while
(false)
722 dbgs() << " PBR: " << BB->getName() << "\n";do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (auto *BB : Blocks) dbgs() <<
" PBR: " << BB->getName() << "\n"; }; } } while
(false)
723 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (auto *BB : Blocks) dbgs() <<
" PBR: " << BB->getName() << "\n"; }; } } while
(false)
;
724
725 // Register the outlined info.
726 addOutlineInfo(std::move(OI));
727
728 InsertPointTy AfterIP(UI->getParent(), UI->getParent()->end());
729 UI->eraseFromParent();
730
731 return AfterIP;
732}
733
734void OpenMPIRBuilder::emitFlush(const LocationDescription &Loc) {
735 // Build call void __kmpc_flush(ident_t *loc)
736 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
737 Value *Args[] = {getOrCreateIdent(SrcLocStr)};
738
739 Builder.CreateCall(getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_flush), Args);
740}
741
742void OpenMPIRBuilder::CreateFlush(const LocationDescription &Loc) {
743 if (!updateToLocation(Loc))
744 return;
745 emitFlush(Loc);
746}
747
748void OpenMPIRBuilder::emitTaskwaitImpl(const LocationDescription &Loc) {
749 // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
750 // global_tid);
751 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
752 Value *Ident = getOrCreateIdent(SrcLocStr);
753 Value *Args[] = {Ident, getOrCreateThreadID(Ident)};
754
755 // Ignore return result until untied tasks are supported.
756 Builder.CreateCall(getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_taskwait),
757 Args);
758}
759
760void OpenMPIRBuilder::CreateTaskwait(const LocationDescription &Loc) {
761 if (!updateToLocation(Loc))
762 return;
763 emitTaskwaitImpl(Loc);
764}
765
766void OpenMPIRBuilder::emitTaskyieldImpl(const LocationDescription &Loc) {
767 // Build call __kmpc_omp_taskyield(loc, thread_id, 0);
768 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
769 Value *Ident = getOrCreateIdent(SrcLocStr);
770 Constant *I32Null = ConstantInt::getNullValue(Int32);
771 Value *Args[] = {Ident, getOrCreateThreadID(Ident), I32Null};
772
773 Builder.CreateCall(getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_taskyield),
774 Args);
775}
776
777void OpenMPIRBuilder::CreateTaskyield(const LocationDescription &Loc) {
778 if (!updateToLocation(Loc))
779 return;
780 emitTaskyieldImpl(Loc);
781}
782
783OpenMPIRBuilder::InsertPointTy
784OpenMPIRBuilder::CreateMaster(const LocationDescription &Loc,
785 BodyGenCallbackTy BodyGenCB,
786 FinalizeCallbackTy FiniCB) {
787
788 if (!updateToLocation(Loc))
789 return Loc.IP;
790
791 Directive OMPD = Directive::OMPD_master;
792 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
793 Value *Ident = getOrCreateIdent(SrcLocStr);
794 Value *ThreadId = getOrCreateThreadID(Ident);
795 Value *Args[] = {Ident, ThreadId};
796
797 Function *EntryRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_master);
798 Instruction *EntryCall = Builder.CreateCall(EntryRTLFn, Args);
799
800 Function *ExitRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_master);
801 Instruction *ExitCall = Builder.CreateCall(ExitRTLFn, Args);
802
803 return EmitOMPInlinedRegion(OMPD, EntryCall, ExitCall, BodyGenCB, FiniCB,
804 /*Conditional*/ true, /*hasFinalize*/ true);
805}
806
807OpenMPIRBuilder::InsertPointTy
808OpenMPIRBuilder::CreateCopyPrivate(const LocationDescription &Loc,
809 llvm::Value *BufSize, llvm::Value *CpyBuf,
810 llvm::Value *CpyFn, llvm::Value *DidIt) {
811 if (!updateToLocation(Loc))
812 return Loc.IP;
813
814 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
815 Value *Ident = getOrCreateIdent(SrcLocStr);
816 Value *ThreadId = getOrCreateThreadID(Ident);
817
818 llvm::Value *DidItLD = Builder.CreateLoad(DidIt);
819
820 Value *Args[] = {Ident, ThreadId, BufSize, CpyBuf, CpyFn, DidItLD};
821
822 Function *Fn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_copyprivate);
823 Builder.CreateCall(Fn, Args);
824
825 return Builder.saveIP();
826}
827
828OpenMPIRBuilder::InsertPointTy
829OpenMPIRBuilder::CreateSingle(const LocationDescription &Loc,
830 BodyGenCallbackTy BodyGenCB,
831 FinalizeCallbackTy FiniCB, llvm::Value *DidIt) {
832
833 if (!updateToLocation(Loc))
834 return Loc.IP;
835
836 // If needed (i.e. not null), initialize `DidIt` with 0
837 if (DidIt) {
838 Builder.CreateStore(Builder.getInt32(0), DidIt);
839 }
840
841 Directive OMPD = Directive::OMPD_single;
842 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
843 Value *Ident = getOrCreateIdent(SrcLocStr);
844 Value *ThreadId = getOrCreateThreadID(Ident);
845 Value *Args[] = {Ident, ThreadId};
846
847 Function *EntryRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_single);
848 Instruction *EntryCall = Builder.CreateCall(EntryRTLFn, Args);
849
850 Function *ExitRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_single);
851 Instruction *ExitCall = Builder.CreateCall(ExitRTLFn, Args);
852
853 // generates the following:
854 // if (__kmpc_single()) {
855 // .... single region ...
856 // __kmpc_end_single
857 // }
858
859 return EmitOMPInlinedRegion(OMPD, EntryCall, ExitCall, BodyGenCB, FiniCB,
860 /*Conditional*/ true, /*hasFinalize*/ true);
861}
862
863OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::CreateCritical(
864 const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB,
865 FinalizeCallbackTy FiniCB, StringRef CriticalName, Value *HintInst) {
866
867 if (!updateToLocation(Loc))
868 return Loc.IP;
869
870 Directive OMPD = Directive::OMPD_critical;
871 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
872 Value *Ident = getOrCreateIdent(SrcLocStr);
873 Value *ThreadId = getOrCreateThreadID(Ident);
874 Value *LockVar = getOMPCriticalRegionLock(CriticalName);
875 Value *Args[] = {Ident, ThreadId, LockVar};
876
877 SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), std::end(Args));
878 Function *RTFn = nullptr;
879 if (HintInst) {
880 // Add Hint to entry Args and create call
881 EnterArgs.push_back(HintInst);
882 RTFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_critical_with_hint);
883 } else {
884 RTFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_critical);
885 }
886 Instruction *EntryCall = Builder.CreateCall(RTFn, EnterArgs);
887
888 Function *ExitRTLFn =
889 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_critical);
890 Instruction *ExitCall = Builder.CreateCall(ExitRTLFn, Args);
891
892 return EmitOMPInlinedRegion(OMPD, EntryCall, ExitCall, BodyGenCB, FiniCB,
893 /*Conditional*/ false, /*hasFinalize*/ true);
894}
895
896OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::EmitOMPInlinedRegion(
897 Directive OMPD, Instruction *EntryCall, Instruction *ExitCall,
898 BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool Conditional,
899 bool HasFinalize) {
900
901 if (HasFinalize)
902 FinalizationStack.push_back({FiniCB, OMPD, /*IsCancellable*/ false});
903
904 // Create inlined region's entry and body blocks, in preparation
905 // for conditional creation
906 BasicBlock *EntryBB = Builder.GetInsertBlock();
907 Instruction *SplitPos = EntryBB->getTerminator();
908 if (!isa_and_nonnull<BranchInst>(SplitPos))
909 SplitPos = new UnreachableInst(Builder.getContext(), EntryBB);
910 BasicBlock *ExitBB = EntryBB->splitBasicBlock(SplitPos, "omp_region.end");
911 BasicBlock *FiniBB =
912 EntryBB->splitBasicBlock(EntryBB->getTerminator(), "omp_region.finalize");
913
914 Builder.SetInsertPoint(EntryBB->getTerminator());
915 emitCommonDirectiveEntry(OMPD, EntryCall, ExitBB, Conditional);
916
917 // generate body
918 BodyGenCB(/* AllocaIP */ InsertPointTy(),
919 /* CodeGenIP */ Builder.saveIP(), *FiniBB);
920
921 // If we didn't emit a branch to FiniBB during body generation, it means
922 // FiniBB is unreachable (e.g. while(1);). stop generating all the
923 // unreachable blocks, and remove anything we are not going to use.
924 auto SkipEmittingRegion = FiniBB->hasNPredecessors(0);
925 if (SkipEmittingRegion) {
926 FiniBB->eraseFromParent();
927 ExitCall->eraseFromParent();
928 // Discard finalization if we have it.
929 if (HasFinalize) {
930 assert(!FinalizationStack.empty() &&((!FinalizationStack.empty() && "Unexpected finalization stack state!"
) ? static_cast<void> (0) : __assert_fail ("!FinalizationStack.empty() && \"Unexpected finalization stack state!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 931, __PRETTY_FUNCTION__))
931 "Unexpected finalization stack state!")((!FinalizationStack.empty() && "Unexpected finalization stack state!"
) ? static_cast<void> (0) : __assert_fail ("!FinalizationStack.empty() && \"Unexpected finalization stack state!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 931, __PRETTY_FUNCTION__))
;
932 FinalizationStack.pop_back();
933 }
934 } else {
935 // emit exit call and do any needed finalization.
936 auto FinIP = InsertPointTy(FiniBB, FiniBB->getFirstInsertionPt());
937 assert(FiniBB->getTerminator()->getNumSuccessors() == 1 &&((FiniBB->getTerminator()->getNumSuccessors() == 1 &&
FiniBB->getTerminator()->getSuccessor(0) == ExitBB &&
"Unexpected control flow graph state!!") ? static_cast<void
> (0) : __assert_fail ("FiniBB->getTerminator()->getNumSuccessors() == 1 && FiniBB->getTerminator()->getSuccessor(0) == ExitBB && \"Unexpected control flow graph state!!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 939, __PRETTY_FUNCTION__))
938 FiniBB->getTerminator()->getSuccessor(0) == ExitBB &&((FiniBB->getTerminator()->getNumSuccessors() == 1 &&
FiniBB->getTerminator()->getSuccessor(0) == ExitBB &&
"Unexpected control flow graph state!!") ? static_cast<void
> (0) : __assert_fail ("FiniBB->getTerminator()->getNumSuccessors() == 1 && FiniBB->getTerminator()->getSuccessor(0) == ExitBB && \"Unexpected control flow graph state!!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 939, __PRETTY_FUNCTION__))
939 "Unexpected control flow graph state!!")((FiniBB->getTerminator()->getNumSuccessors() == 1 &&
FiniBB->getTerminator()->getSuccessor(0) == ExitBB &&
"Unexpected control flow graph state!!") ? static_cast<void
> (0) : __assert_fail ("FiniBB->getTerminator()->getNumSuccessors() == 1 && FiniBB->getTerminator()->getSuccessor(0) == ExitBB && \"Unexpected control flow graph state!!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 939, __PRETTY_FUNCTION__))
;
940 emitCommonDirectiveExit(OMPD, FinIP, ExitCall, HasFinalize);
941 assert(FiniBB->getUniquePredecessor()->getUniqueSuccessor() == FiniBB &&((FiniBB->getUniquePredecessor()->getUniqueSuccessor() ==
FiniBB && "Unexpected Control Flow State!") ? static_cast
<void> (0) : __assert_fail ("FiniBB->getUniquePredecessor()->getUniqueSuccessor() == FiniBB && \"Unexpected Control Flow State!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 942, __PRETTY_FUNCTION__))
942 "Unexpected Control Flow State!")((FiniBB->getUniquePredecessor()->getUniqueSuccessor() ==
FiniBB && "Unexpected Control Flow State!") ? static_cast
<void> (0) : __assert_fail ("FiniBB->getUniquePredecessor()->getUniqueSuccessor() == FiniBB && \"Unexpected Control Flow State!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 942, __PRETTY_FUNCTION__))
;
943 MergeBlockIntoPredecessor(FiniBB);
944 }
945
946 // If we are skipping the region of a non conditional, remove the exit
947 // block, and clear the builder's insertion point.
948 assert(SplitPos->getParent() == ExitBB &&((SplitPos->getParent() == ExitBB && "Unexpected Insertion point location!"
) ? static_cast<void> (0) : __assert_fail ("SplitPos->getParent() == ExitBB && \"Unexpected Insertion point location!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 949, __PRETTY_FUNCTION__))
949 "Unexpected Insertion point location!")((SplitPos->getParent() == ExitBB && "Unexpected Insertion point location!"
) ? static_cast<void> (0) : __assert_fail ("SplitPos->getParent() == ExitBB && \"Unexpected Insertion point location!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 949, __PRETTY_FUNCTION__))
;
950 if (!Conditional && SkipEmittingRegion) {
951 ExitBB->eraseFromParent();
952 Builder.ClearInsertionPoint();
953 } else {
954 auto merged = MergeBlockIntoPredecessor(ExitBB);
955 BasicBlock *ExitPredBB = SplitPos->getParent();
956 auto InsertBB = merged ? ExitPredBB : ExitBB;
957 if (!isa_and_nonnull<BranchInst>(SplitPos))
958 SplitPos->eraseFromParent();
959 Builder.SetInsertPoint(InsertBB);
960 }
961
962 return Builder.saveIP();
963}
964
965OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitCommonDirectiveEntry(
966 Directive OMPD, Value *EntryCall, BasicBlock *ExitBB, bool Conditional) {
967
968 // if nothing to do, Return current insertion point.
969 if (!Conditional)
970 return Builder.saveIP();
971
972 BasicBlock *EntryBB = Builder.GetInsertBlock();
973 Value *CallBool = Builder.CreateIsNotNull(EntryCall);
974 auto *ThenBB = BasicBlock::Create(M.getContext(), "omp_region.body");
975 auto *UI = new UnreachableInst(Builder.getContext(), ThenBB);
976
977 // Emit thenBB and set the Builder's insertion point there for
978 // body generation next. Place the block after the current block.
979 Function *CurFn = EntryBB->getParent();
980 CurFn->getBasicBlockList().insertAfter(EntryBB->getIterator(), ThenBB);
981
982 // Move Entry branch to end of ThenBB, and replace with conditional
983 // branch (If-stmt)
984 Instruction *EntryBBTI = EntryBB->getTerminator();
985 Builder.CreateCondBr(CallBool, ThenBB, ExitBB);
986 EntryBBTI->removeFromParent();
987 Builder.SetInsertPoint(UI);
988 Builder.Insert(EntryBBTI);
989 UI->eraseFromParent();
990 Builder.SetInsertPoint(ThenBB->getTerminator());
991
992 // return an insertion point to ExitBB.
993 return IRBuilder<>::InsertPoint(ExitBB, ExitBB->getFirstInsertionPt());
994}
995
996OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitCommonDirectiveExit(
997 omp::Directive OMPD, InsertPointTy FinIP, Instruction *ExitCall,
998 bool HasFinalize) {
999
1000 Builder.restoreIP(FinIP);
1001
1002 // If there is finalization to do, emit it before the exit call
1003 if (HasFinalize) {
1004 assert(!FinalizationStack.empty() &&((!FinalizationStack.empty() && "Unexpected finalization stack state!"
) ? static_cast<void> (0) : __assert_fail ("!FinalizationStack.empty() && \"Unexpected finalization stack state!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1005, __PRETTY_FUNCTION__))
1005 "Unexpected finalization stack state!")((!FinalizationStack.empty() && "Unexpected finalization stack state!"
) ? static_cast<void> (0) : __assert_fail ("!FinalizationStack.empty() && \"Unexpected finalization stack state!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1005, __PRETTY_FUNCTION__))
;
1006
1007 FinalizationInfo Fi = FinalizationStack.pop_back_val();
1008 assert(Fi.DK == OMPD && "Unexpected Directive for Finalization call!")((Fi.DK == OMPD && "Unexpected Directive for Finalization call!"
) ? static_cast<void> (0) : __assert_fail ("Fi.DK == OMPD && \"Unexpected Directive for Finalization call!\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1008, __PRETTY_FUNCTION__))
;
1009
1010 Fi.FiniCB(FinIP);
1011
1012 BasicBlock *FiniBB = FinIP.getBlock();
1013 Instruction *FiniBBTI = FiniBB->getTerminator();
1014
1015 // set Builder IP for call creation
1016 Builder.SetInsertPoint(FiniBBTI);
1017 }
1018
1019 // place the Exitcall as last instruction before Finalization block terminator
1020 ExitCall->removeFromParent();
1021 Builder.Insert(ExitCall);
1022
1023 return IRBuilder<>::InsertPoint(ExitCall->getParent(),
1024 ExitCall->getIterator());
1025}
1026
1027OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::CreateCopyinClauseBlocks(
1028 InsertPointTy IP, Value *MasterAddr, Value *PrivateAddr,
1029 llvm::IntegerType *IntPtrTy, bool BranchtoEnd) {
1030 if (!IP.isSet())
1031 return IP;
1032
1033 IRBuilder<>::InsertPointGuard IPG(Builder);
1034
1035 // creates the following CFG structure
1036 // OMP_Entry : (MasterAddr != PrivateAddr)?
1037 // F T
1038 // | \
1039 // | copin.not.master
1040 // | /
1041 // v /
1042 // copyin.not.master.end
1043 // |
1044 // v
1045 // OMP.Entry.Next
1046
1047 BasicBlock *OMP_Entry = IP.getBlock();
1048 Function *CurFn = OMP_Entry->getParent();
1049 BasicBlock *CopyBegin =
1050 BasicBlock::Create(M.getContext(), "copyin.not.master", CurFn);
1051 BasicBlock *CopyEnd = nullptr;
1052
1053 // If entry block is terminated, split to preserve the branch to following
1054 // basic block (i.e. OMP.Entry.Next), otherwise, leave everything as is.
1055 if (isa_and_nonnull<BranchInst>(OMP_Entry->getTerminator())) {
1056 CopyEnd = OMP_Entry->splitBasicBlock(OMP_Entry->getTerminator(),
1057 "copyin.not.master.end");
1058 OMP_Entry->getTerminator()->eraseFromParent();
1059 } else {
1060 CopyEnd =
1061 BasicBlock::Create(M.getContext(), "copyin.not.master.end", CurFn);
1062 }
1063
1064 Builder.SetInsertPoint(OMP_Entry);
1065 Value *MasterPtr = Builder.CreatePtrToInt(MasterAddr, IntPtrTy);
1066 Value *PrivatePtr = Builder.CreatePtrToInt(PrivateAddr, IntPtrTy);
1067 Value *cmp = Builder.CreateICmpNE(MasterPtr, PrivatePtr);
1068 Builder.CreateCondBr(cmp, CopyBegin, CopyEnd);
1069
1070 Builder.SetInsertPoint(CopyBegin);
1071 if (BranchtoEnd)
1072 Builder.SetInsertPoint(Builder.CreateBr(CopyEnd));
1073
1074 return Builder.saveIP();
1075}
1076
1077CallInst *OpenMPIRBuilder::CreateOMPAlloc(const LocationDescription &Loc,
1078 Value *Size, Value *Allocator,
1079 std::string Name) {
1080 IRBuilder<>::InsertPointGuard IPG(Builder);
1081 Builder.restoreIP(Loc.IP);
1082
1083 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1084 Value *Ident = getOrCreateIdent(SrcLocStr);
1085 Value *ThreadId = getOrCreateThreadID(Ident);
1086 Value *Args[] = {ThreadId, Size, Allocator};
1087
1088 Function *Fn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_alloc);
1089
1090 return Builder.CreateCall(Fn, Args, Name);
1091}
1092
1093CallInst *OpenMPIRBuilder::CreateOMPFree(const LocationDescription &Loc,
1094 Value *Addr, Value *Allocator,
1095 std::string Name) {
1096 IRBuilder<>::InsertPointGuard IPG(Builder);
1097 Builder.restoreIP(Loc.IP);
1098
1099 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1100 Value *Ident = getOrCreateIdent(SrcLocStr);
1101 Value *ThreadId = getOrCreateThreadID(Ident);
1102 Value *Args[] = {ThreadId, Addr, Allocator};
1103 Function *Fn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_free);
1104 return Builder.CreateCall(Fn, Args, Name);
1105}
1106
1107CallInst *OpenMPIRBuilder::CreateCachedThreadPrivate(
1108 const LocationDescription &Loc, llvm::Value *Pointer,
1109 llvm::ConstantInt *Size, const llvm::Twine &Name) {
1110 IRBuilder<>::InsertPointGuard IPG(Builder);
1111 Builder.restoreIP(Loc.IP);
1112
1113 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1114 Value *Ident = getOrCreateIdent(SrcLocStr);
1115 Value *ThreadId = getOrCreateThreadID(Ident);
1116 Constant *ThreadPrivateCache =
1117 getOrCreateOMPInternalVariable(Int8PtrPtr, Name);
1118 llvm::Value *Args[] = {Ident, ThreadId, Pointer, Size, ThreadPrivateCache};
1119
1120 Function *Fn =
1121 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_threadprivate_cached);
1122
1123 return Builder.CreateCall(Fn, Args);
1124}
1125
1126std::string OpenMPIRBuilder::getNameWithSeparators(ArrayRef<StringRef> Parts,
1127 StringRef FirstSeparator,
1128 StringRef Separator) {
1129 SmallString<128> Buffer;
1130 llvm::raw_svector_ostream OS(Buffer);
1131 StringRef Sep = FirstSeparator;
1132 for (StringRef Part : Parts) {
1133 OS << Sep << Part;
1134 Sep = Separator;
1135 }
1136 return OS.str().str();
1137}
1138
1139Constant *OpenMPIRBuilder::getOrCreateOMPInternalVariable(
1140 llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) {
1141 // TODO: Replace the twine arg with stringref to get rid of the conversion
1142 // logic. However This is taken from current implementation in clang as is.
1143 // Since this method is used in many places exclusively for OMP internal use
1144 // we will keep it as is for temporarily until we move all users to the
1145 // builder and then, if possible, fix it everywhere in one go.
1146 SmallString<256> Buffer;
1147 llvm::raw_svector_ostream Out(Buffer);
1148 Out << Name;
1149 StringRef RuntimeName = Out.str();
1150 auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first;
1151 if (Elem.second) {
1152 assert(Elem.second->getType()->getPointerElementType() == Ty &&((Elem.second->getType()->getPointerElementType() == Ty
&& "OMP internal variable has different type than requested"
) ? static_cast<void> (0) : __assert_fail ("Elem.second->getType()->getPointerElementType() == Ty && \"OMP internal variable has different type than requested\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1153, __PRETTY_FUNCTION__))
1153 "OMP internal variable has different type than requested")((Elem.second->getType()->getPointerElementType() == Ty
&& "OMP internal variable has different type than requested"
) ? static_cast<void> (0) : __assert_fail ("Elem.second->getType()->getPointerElementType() == Ty && \"OMP internal variable has different type than requested\""
, "/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1153, __PRETTY_FUNCTION__))
;
1154 } else {
1155 // TODO: investigate the appropriate linkage type used for the global
1156 // variable for possibly changing that to internal or private, or maybe
1157 // create different versions of the function for different OMP internal
1158 // variables.
1159 Elem.second = new llvm::GlobalVariable(
1160 M, Ty, /*IsConstant*/ false, llvm::GlobalValue::CommonLinkage,
1161 llvm::Constant::getNullValue(Ty), Elem.first(),
1162 /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal,
1163 AddressSpace);
1164 }
1165
1166 return Elem.second;
1167}
1168
1169Value *OpenMPIRBuilder::getOMPCriticalRegionLock(StringRef CriticalName) {
1170 std::string Prefix = Twine("gomp_critical_user_", CriticalName).str();
1171 std::string Name = getNameWithSeparators({Prefix, "var"}, ".", ".");
1172 return getOrCreateOMPInternalVariable(KmpCriticalNameTy, Name);
1173}
1174
1175// Create all simple and struct types exposed by the runtime and remember
1176// the llvm::PointerTypes of them for easy access later.
1177void OpenMPIRBuilder::initializeTypes(Module &M) {
1178 LLVMContext &Ctx = M.getContext();
1179 StructType *T;
1180#define OMP_TYPE(VarName, InitValue) VarName = InitValue;
1181#define OMP_ARRAY_TYPE(VarName, ElemTy, ArraySize) \
1182 VarName##Ty = ArrayType::get(ElemTy, ArraySize); \
1183 VarName##PtrTy = PointerType::getUnqual(VarName##Ty);
1184#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
1185 VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
1186 VarName##Ptr = PointerType::getUnqual(VarName);
1187#define OMP_STRUCT_TYPE(VarName, StructName, ...) \
1188 T = M.getTypeByName(StructName); \
1189 if (!T) \
1190 T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \
1191 VarName = T; \
1192 VarName##Ptr = PointerType::getUnqual(T);
1193#include "llvm/Frontend/OpenMP/OMPKinds.def"
1194}
1195
1196void OpenMPIRBuilder::OutlineInfo::collectBlocks(
1197 SmallPtrSetImpl<BasicBlock *> &BlockSet,
1198 SmallVectorImpl<BasicBlock *> &BlockVector) {
1199 SmallVector<BasicBlock *, 32> Worklist;
1200 BlockSet.insert(EntryBB);
1201 BlockSet.insert(ExitBB);
1202
1203 Worklist.push_back(EntryBB);
1204 while (!Worklist.empty()) {
1205 BasicBlock *BB = Worklist.pop_back_val();
1206 BlockVector.push_back(BB);
1207 for (BasicBlock *SuccBB : successors(BB))
1208 if (BlockSet.insert(SuccBB).second)
1209 Worklist.push_back(SuccBB);
1210 }
1211}

/build/llvm-toolchain-snapshot-12~++20200926111128+c6c5629f2fb/llvm/include/llvm/IR/IRBuilder.h

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