Bug Summary

File:llvm/include/llvm/IR/IRBuilder.h
Warning:line 1766, column 30
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 -fhalf-no-semantic-interposition -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~++20210115100614+a14c36fe27f5/build-llvm/lib/Frontend/OpenMP -I /build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP -I /build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/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~++20210115100614+a14c36fe27f5/build-llvm/lib/Frontend/OpenMP -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5=. -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-2021-01-16-002530-32805-1 -x c++ /build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/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/ADT/Triple.h"
20#include "llvm/IR/CFG.h"
21#include "llvm/IR/DebugInfo.h"
22#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/MDBuilder.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Error.h"
26#include "llvm/Transforms/Utils/BasicBlockUtils.h"
27#include "llvm/Transforms/Utils/CodeExtractor.h"
28
29#include <sstream>
30
31#define DEBUG_TYPE"openmp-ir-builder" "openmp-ir-builder"
32
33using namespace llvm;
34using namespace omp;
35
36static cl::opt<bool>
37 OptimisticAttributes("openmp-ir-builder-optimistic-attributes", cl::Hidden,
38 cl::desc("Use optimistic attributes describing "
39 "'as-if' properties of runtime calls."),
40 cl::init(false));
41
42void OpenMPIRBuilder::addAttributes(omp::RuntimeFunction FnID, Function &Fn) {
43 LLVMContext &Ctx = Fn.getContext();
44
45#define OMP_ATTRS_SET(VarName, AttrSet) AttributeSet VarName = AttrSet;
46#include "llvm/Frontend/OpenMP/OMPKinds.def"
47
48 // Add attributes to the new declaration.
49 switch (FnID) {
50#define OMP_RTL_ATTRS(Enum, FnAttrSet, RetAttrSet, ArgAttrSets) \
51 case Enum: \
52 Fn.setAttributes( \
53 AttributeList::get(Ctx, FnAttrSet, RetAttrSet, ArgAttrSets)); \
54 break;
55#include "llvm/Frontend/OpenMP/OMPKinds.def"
56 default:
57 // Attributes are optional.
58 break;
59 }
60}
61
62FunctionCallee
63OpenMPIRBuilder::getOrCreateRuntimeFunction(Module &M, RuntimeFunction FnID) {
64 FunctionType *FnTy = nullptr;
65 Function *Fn = nullptr;
66
67 // Try to find the declation in the module first.
68 switch (FnID) {
69#define OMP_RTL(Enum, Str, IsVarArg, ReturnType, ...) \
70 case Enum: \
71 FnTy = FunctionType::get(ReturnType, ArrayRef<Type *>{__VA_ARGS__}, \
72 IsVarArg); \
73 Fn = M.getFunction(Str); \
74 break;
75#include "llvm/Frontend/OpenMP/OMPKinds.def"
76 }
77
78 if (!Fn) {
79 // Create a new declaration if we need one.
80 switch (FnID) {
81#define OMP_RTL(Enum, Str, ...) \
82 case Enum: \
83 Fn = Function::Create(FnTy, GlobalValue::ExternalLinkage, Str, M); \
84 break;
85#include "llvm/Frontend/OpenMP/OMPKinds.def"
86 }
87
88 // Add information if the runtime function takes a callback function
89 if (FnID == OMPRTL___kmpc_fork_call || FnID == OMPRTL___kmpc_fork_teams) {
90 if (!Fn->hasMetadata(LLVMContext::MD_callback)) {
91 LLVMContext &Ctx = Fn->getContext();
92 MDBuilder MDB(Ctx);
93 // Annotate the callback behavior of the runtime function:
94 // - The callback callee is argument number 2 (microtask).
95 // - The first two arguments of the callback callee are unknown (-1).
96 // - All variadic arguments to the runtime function are passed to the
97 // callback callee.
98 Fn->addMetadata(
99 LLVMContext::MD_callback,
100 *MDNode::get(Ctx, {MDB.createCallbackEncoding(
101 2, {-1, -1}, /* VarArgsArePassed */ true)}));
102 }
103 }
104
105 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)
106 << " 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)
;
107 addAttributes(FnID, *Fn);
108
109 } else {
110 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)
111 << " 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)
;
112 }
113
114 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 114, __PRETTY_FUNCTION__))
;
115
116 // Cast the function to the expected type if necessary
117 Constant *C = ConstantExpr::getBitCast(Fn, FnTy->getPointerTo());
118 return {FnTy, C};
119}
120
121Function *OpenMPIRBuilder::getOrCreateRuntimeFunctionPtr(RuntimeFunction FnID) {
122 FunctionCallee RTLFn = getOrCreateRuntimeFunction(M, FnID);
123 auto *Fn = dyn_cast<llvm::Function>(RTLFn.getCallee());
124 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 124, __PRETTY_FUNCTION__))
;
125 return Fn;
126}
127
128void OpenMPIRBuilder::initialize() { initializeTypes(M); }
129
130void OpenMPIRBuilder::finalize(bool AllowExtractorSinking) {
131 SmallPtrSet<BasicBlock *, 32> ParallelRegionBlockSet;
132 SmallVector<BasicBlock *, 32> Blocks;
133 for (OutlineInfo &OI : OutlineInfos) {
134 ParallelRegionBlockSet.clear();
135 Blocks.clear();
136 OI.collectBlocks(ParallelRegionBlockSet, Blocks);
137
138 Function *OuterFn = OI.EntryBB->getParent();
139 CodeExtractorAnalysisCache CEAC(*OuterFn);
140 CodeExtractor Extractor(Blocks, /* DominatorTree */ nullptr,
141 /* AggregateArgs */ false,
142 /* BlockFrequencyInfo */ nullptr,
143 /* BranchProbabilityInfo */ nullptr,
144 /* AssumptionCache */ nullptr,
145 /* AllowVarArgs */ true,
146 /* AllowAlloca */ true,
147 /* Suffix */ ".omp_par");
148
149 LLVM_DEBUG(dbgs() << "Before outlining: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Before outlining: "
<< *OuterFn << "\n"; } } while (false)
;
150 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)
151 << " 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)
;
152 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 153, __PRETTY_FUNCTION__))
153 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 153, __PRETTY_FUNCTION__))
;
154
155 Function *OutlinedFn = Extractor.extractCodeRegion(CEAC);
156
157 LLVM_DEBUG(dbgs() << "After outlining: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "After outlining: "
<< *OuterFn << "\n"; } } while (false)
;
158 LLVM_DEBUG(dbgs() << " Outlined function: " << *OutlinedFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << " Outlined function: "
<< *OutlinedFn << "\n"; } } while (false)
;
159 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 160, __PRETTY_FUNCTION__))
160 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 160, __PRETTY_FUNCTION__))
;
161
162 // For compability with the clang CG we move the outlined function after the
163 // one with the parallel region.
164 OutlinedFn->removeFromParent();
165 M.getFunctionList().insertAfter(OuterFn->getIterator(), OutlinedFn);
166
167 // Remove the artificial entry introduced by the extractor right away, we
168 // made our own entry block after all.
169 {
170 BasicBlock &ArtificialEntry = OutlinedFn->getEntryBlock();
171 assert(ArtificialEntry.getUniqueSuccessor() == OI.EntryBB)((ArtificialEntry.getUniqueSuccessor() == OI.EntryBB) ? static_cast
<void> (0) : __assert_fail ("ArtificialEntry.getUniqueSuccessor() == OI.EntryBB"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 171, __PRETTY_FUNCTION__))
;
172 assert(OI.EntryBB->getUniquePredecessor() == &ArtificialEntry)((OI.EntryBB->getUniquePredecessor() == &ArtificialEntry
) ? static_cast<void> (0) : __assert_fail ("OI.EntryBB->getUniquePredecessor() == &ArtificialEntry"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 172, __PRETTY_FUNCTION__))
;
173 if (AllowExtractorSinking) {
174 // Move instructions from the to-be-deleted ArtificialEntry to the entry
175 // basic block of the parallel region. CodeExtractor may have sunk
176 // allocas/bitcasts for values that are solely used in the outlined
177 // region and do not escape.
178 assert(!ArtificialEntry.empty() &&((!ArtificialEntry.empty() && "Expected instructions to sink in the outlined region"
) ? static_cast<void> (0) : __assert_fail ("!ArtificialEntry.empty() && \"Expected instructions to sink in the outlined region\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 179, __PRETTY_FUNCTION__))
179 "Expected instructions to sink in the outlined region")((!ArtificialEntry.empty() && "Expected instructions to sink in the outlined region"
) ? static_cast<void> (0) : __assert_fail ("!ArtificialEntry.empty() && \"Expected instructions to sink in the outlined region\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 179, __PRETTY_FUNCTION__))
;
180 for (BasicBlock::iterator It = ArtificialEntry.begin(),
181 End = ArtificialEntry.end();
182 It != End;) {
183 Instruction &I = *It;
184 It++;
185
186 if (I.isTerminator())
187 continue;
188
189 I.moveBefore(*OI.EntryBB, OI.EntryBB->getFirstInsertionPt());
190 }
191 }
192 OI.EntryBB->moveBefore(&ArtificialEntry);
193 ArtificialEntry.eraseFromParent();
194 }
195 assert(&OutlinedFn->getEntryBlock() == OI.EntryBB)((&OutlinedFn->getEntryBlock() == OI.EntryBB) ? static_cast
<void> (0) : __assert_fail ("&OutlinedFn->getEntryBlock() == OI.EntryBB"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 195, __PRETTY_FUNCTION__))
;
196 assert(OutlinedFn && OutlinedFn->getNumUses() == 1)((OutlinedFn && OutlinedFn->getNumUses() == 1) ? static_cast
<void> (0) : __assert_fail ("OutlinedFn && OutlinedFn->getNumUses() == 1"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 196, __PRETTY_FUNCTION__))
;
197
198 // Run a user callback, e.g. to add attributes.
199 if (OI.PostOutlineCB)
200 OI.PostOutlineCB(*OutlinedFn);
201 }
202
203 // Allow finalize to be called multiple times.
204 OutlineInfos.clear();
205}
206
207Value *OpenMPIRBuilder::getOrCreateIdent(Constant *SrcLocStr,
208 IdentFlag LocFlags,
209 unsigned Reserve2Flags) {
210 // Enable "C-mode".
211 LocFlags |= OMP_IDENT_FLAG_KMPC;
212
213 Value *&Ident =
214 IdentMap[{SrcLocStr, uint64_t(LocFlags) << 31 | Reserve2Flags}];
215 if (!Ident) {
216 Constant *I32Null = ConstantInt::getNullValue(Int32);
217 Constant *IdentData[] = {
218 I32Null, ConstantInt::get(Int32, uint32_t(LocFlags)),
219 ConstantInt::get(Int32, Reserve2Flags), I32Null, SrcLocStr};
220 Constant *Initializer = ConstantStruct::get(
221 cast<StructType>(IdentPtr->getPointerElementType()), IdentData);
222
223 // Look for existing encoding of the location + flags, not needed but
224 // minimizes the difference to the existing solution while we transition.
225 for (GlobalVariable &GV : M.getGlobalList())
226 if (GV.getType() == IdentPtr && GV.hasInitializer())
227 if (GV.getInitializer() == Initializer)
228 return Ident = &GV;
229
230 auto *GV = new GlobalVariable(M, IdentPtr->getPointerElementType(),
231 /* isConstant = */ true,
232 GlobalValue::PrivateLinkage, Initializer);
233 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
234 GV->setAlignment(Align(8));
235 Ident = GV;
236 }
237 return Builder.CreatePointerCast(Ident, IdentPtr);
238}
239
240Type *OpenMPIRBuilder::getLanemaskType() {
241 LLVMContext &Ctx = M.getContext();
242 Triple triple(M.getTargetTriple());
243
244 // This test is adequate until deviceRTL has finer grained lane widths
245 return triple.isAMDGCN() ? Type::getInt64Ty(Ctx) : Type::getInt32Ty(Ctx);
246}
247
248Constant *OpenMPIRBuilder::getOrCreateSrcLocStr(StringRef LocStr) {
249 Constant *&SrcLocStr = SrcLocStrMap[LocStr];
250 if (!SrcLocStr) {
251 Constant *Initializer =
252 ConstantDataArray::getString(M.getContext(), LocStr);
253
254 // Look for existing encoding of the location, not needed but minimizes the
255 // difference to the existing solution while we transition.
256 for (GlobalVariable &GV : M.getGlobalList())
257 if (GV.isConstant() && GV.hasInitializer() &&
258 GV.getInitializer() == Initializer)
259 return SrcLocStr = ConstantExpr::getPointerCast(&GV, Int8Ptr);
260
261 SrcLocStr = Builder.CreateGlobalStringPtr(LocStr, /* Name */ "",
262 /* AddressSpace */ 0, &M);
263 }
264 return SrcLocStr;
265}
266
267Constant *OpenMPIRBuilder::getOrCreateSrcLocStr(StringRef FunctionName,
268 StringRef FileName,
269 unsigned Line,
270 unsigned Column) {
271 SmallString<128> Buffer;
272 Buffer.push_back(';');
273 Buffer.append(FileName);
274 Buffer.push_back(';');
275 Buffer.append(FunctionName);
276 Buffer.push_back(';');
277 Buffer.append(std::to_string(Line));
278 Buffer.push_back(';');
279 Buffer.append(std::to_string(Column));
280 Buffer.push_back(';');
281 Buffer.push_back(';');
282 return getOrCreateSrcLocStr(Buffer.str());
283}
284
285Constant *OpenMPIRBuilder::getOrCreateDefaultSrcLocStr() {
286 return getOrCreateSrcLocStr(";unknown;unknown;0;0;;");
287}
288
289Constant *
290OpenMPIRBuilder::getOrCreateSrcLocStr(const LocationDescription &Loc) {
291 DILocation *DIL = Loc.DL.get();
292 if (!DIL)
293 return getOrCreateDefaultSrcLocStr();
294 StringRef FileName = M.getName();
295 if (DIFile *DIF = DIL->getFile())
296 if (Optional<StringRef> Source = DIF->getSource())
297 FileName = *Source;
298 StringRef Function = DIL->getScope()->getSubprogram()->getName();
299 Function =
300 !Function.empty() ? Function : Loc.IP.getBlock()->getParent()->getName();
301 return getOrCreateSrcLocStr(Function, FileName, DIL->getLine(),
302 DIL->getColumn());
303}
304
305Value *OpenMPIRBuilder::getOrCreateThreadID(Value *Ident) {
306 return Builder.CreateCall(
307 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_global_thread_num), Ident,
308 "omp_global_thread_num");
309}
310
311OpenMPIRBuilder::InsertPointTy
312OpenMPIRBuilder::createBarrier(const LocationDescription &Loc, Directive DK,
313 bool ForceSimpleCall, bool CheckCancelFlag) {
314 if (!updateToLocation(Loc))
315 return Loc.IP;
316 return emitBarrierImpl(Loc, DK, ForceSimpleCall, CheckCancelFlag);
317}
318
319OpenMPIRBuilder::InsertPointTy
320OpenMPIRBuilder::emitBarrierImpl(const LocationDescription &Loc, Directive Kind,
321 bool ForceSimpleCall, bool CheckCancelFlag) {
322 // Build call __kmpc_cancel_barrier(loc, thread_id) or
323 // __kmpc_barrier(loc, thread_id);
324
325 IdentFlag BarrierLocFlags;
326 switch (Kind) {
327 case OMPD_for:
328 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_FOR;
329 break;
330 case OMPD_sections:
331 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_SECTIONS;
332 break;
333 case OMPD_single:
334 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL_SINGLE;
335 break;
336 case OMPD_barrier:
337 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_EXPL;
338 break;
339 default:
340 BarrierLocFlags = OMP_IDENT_FLAG_BARRIER_IMPL;
341 break;
342 }
343
344 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
345 Value *Args[] = {getOrCreateIdent(SrcLocStr, BarrierLocFlags),
346 getOrCreateThreadID(getOrCreateIdent(SrcLocStr))};
347
348 // If we are in a cancellable parallel region, barriers are cancellation
349 // points.
350 // TODO: Check why we would force simple calls or to ignore the cancel flag.
351 bool UseCancelBarrier =
352 !ForceSimpleCall && isLastFinalizationInfoCancellable(OMPD_parallel);
353
354 Value *Result =
355 Builder.CreateCall(getOrCreateRuntimeFunctionPtr(
356 UseCancelBarrier ? OMPRTL___kmpc_cancel_barrier
357 : OMPRTL___kmpc_barrier),
358 Args);
359
360 if (UseCancelBarrier && CheckCancelFlag)
361 emitCancelationCheckImpl(Result, OMPD_parallel);
362
363 return Builder.saveIP();
364}
365
366OpenMPIRBuilder::InsertPointTy
367OpenMPIRBuilder::createCancel(const LocationDescription &Loc,
368 Value *IfCondition,
369 omp::Directive CanceledDirective) {
370 if (!updateToLocation(Loc))
371 return Loc.IP;
372
373 // LLVM utilities like blocks with terminators.
374 auto *UI = Builder.CreateUnreachable();
375
376 Instruction *ThenTI = UI, *ElseTI = nullptr;
377 if (IfCondition)
378 SplitBlockAndInsertIfThenElse(IfCondition, UI, &ThenTI, &ElseTI);
379 Builder.SetInsertPoint(ThenTI);
380
381 Value *CancelKind = nullptr;
382 switch (CanceledDirective) {
383#define OMP_CANCEL_KIND(Enum, Str, DirectiveEnum, Value) \
384 case DirectiveEnum: \
385 CancelKind = Builder.getInt32(Value); \
386 break;
387#include "llvm/Frontend/OpenMP/OMPKinds.def"
388 default:
389 llvm_unreachable("Unknown cancel kind!")::llvm::llvm_unreachable_internal("Unknown cancel kind!", "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 389)
;
390 }
391
392 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
393 Value *Ident = getOrCreateIdent(SrcLocStr);
394 Value *Args[] = {Ident, getOrCreateThreadID(Ident), CancelKind};
395 Value *Result = Builder.CreateCall(
396 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_cancel), Args);
397
398 // The actual cancel logic is shared with others, e.g., cancel_barriers.
399 emitCancelationCheckImpl(Result, CanceledDirective);
400
401 // Update the insertion point and remove the terminator we introduced.
402 Builder.SetInsertPoint(UI->getParent());
403 UI->eraseFromParent();
404
405 return Builder.saveIP();
406}
407
408void OpenMPIRBuilder::emitCancelationCheckImpl(
409 Value *CancelFlag, omp::Directive CanceledDirective) {
410 assert(isLastFinalizationInfoCancellable(CanceledDirective) &&((isLastFinalizationInfoCancellable(CanceledDirective) &&
"Unexpected cancellation!") ? static_cast<void> (0) : __assert_fail
("isLastFinalizationInfoCancellable(CanceledDirective) && \"Unexpected cancellation!\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 411, __PRETTY_FUNCTION__))
411 "Unexpected cancellation!")((isLastFinalizationInfoCancellable(CanceledDirective) &&
"Unexpected cancellation!") ? static_cast<void> (0) : __assert_fail
("isLastFinalizationInfoCancellable(CanceledDirective) && \"Unexpected cancellation!\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 411, __PRETTY_FUNCTION__))
;
412
413 // For a cancel barrier we create two new blocks.
414 BasicBlock *BB = Builder.GetInsertBlock();
415 BasicBlock *NonCancellationBlock;
416 if (Builder.GetInsertPoint() == BB->end()) {
417 // TODO: This branch will not be needed once we moved to the
418 // OpenMPIRBuilder codegen completely.
419 NonCancellationBlock = BasicBlock::Create(
420 BB->getContext(), BB->getName() + ".cont", BB->getParent());
421 } else {
422 NonCancellationBlock = SplitBlock(BB, &*Builder.GetInsertPoint());
423 BB->getTerminator()->eraseFromParent();
424 Builder.SetInsertPoint(BB);
425 }
426 BasicBlock *CancellationBlock = BasicBlock::Create(
427 BB->getContext(), BB->getName() + ".cncl", BB->getParent());
428
429 // Jump to them based on the return value.
430 Value *Cmp = Builder.CreateIsNull(CancelFlag);
431 Builder.CreateCondBr(Cmp, NonCancellationBlock, CancellationBlock,
432 /* TODO weight */ nullptr, nullptr);
433
434 // From the cancellation block we finalize all variables and go to the
435 // post finalization block that is known to the FiniCB callback.
436 Builder.SetInsertPoint(CancellationBlock);
437 auto &FI = FinalizationStack.back();
438 FI.FiniCB(Builder.saveIP());
439
440 // The continuation block is where code generation continues.
441 Builder.SetInsertPoint(NonCancellationBlock, NonCancellationBlock->begin());
442}
443
444IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel(
445 const LocationDescription &Loc, InsertPointTy OuterAllocaIP,
446 BodyGenCallbackTy BodyGenCB, PrivatizeCallbackTy PrivCB,
447 FinalizeCallbackTy FiniCB, Value *IfCondition, Value *NumThreads,
448 omp::ProcBindKind ProcBind, bool IsCancellable) {
449 if (!updateToLocation(Loc))
1
Taking false branch
450 return Loc.IP;
451
452 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
453 Value *Ident = getOrCreateIdent(SrcLocStr);
454 Value *ThreadID = getOrCreateThreadID(Ident);
455
456 if (NumThreads) {
2
Assuming 'NumThreads' is null
3
Taking false branch
457 // Build call __kmpc_push_num_threads(&Ident, global_tid, num_threads)
458 Value *Args[] = {
459 Ident, ThreadID,
460 Builder.CreateIntCast(NumThreads, Int32, /*isSigned*/ false)};
461 Builder.CreateCall(
462 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_push_num_threads), Args);
463 }
464
465 if (ProcBind != OMP_PROC_BIND_default) {
4
Assuming 'ProcBind' is equal to 'OMP_PROC_BIND_default'
5
Taking false branch
466 // Build call __kmpc_push_proc_bind(&Ident, global_tid, proc_bind)
467 Value *Args[] = {
468 Ident, ThreadID,
469 ConstantInt::get(Int32, unsigned(ProcBind), /*isSigned=*/true)};
470 Builder.CreateCall(
471 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_push_proc_bind), Args);
472 }
473
474 BasicBlock *InsertBB = Builder.GetInsertBlock();
475 Function *OuterFn = InsertBB->getParent();
476
477 // Save the outer alloca block because the insertion iterator may get
478 // invalidated and we still need this later.
479 BasicBlock *OuterAllocaBlock = OuterAllocaIP.getBlock();
480
481 // Vector to remember instructions we used only during the modeling but which
482 // we want to delete at the end.
483 SmallVector<Instruction *, 4> ToBeDeleted;
484
485 // Change the location to the outer alloca insertion point to create and
486 // initialize the allocas we pass into the parallel region.
487 Builder.restoreIP(OuterAllocaIP);
488 AllocaInst *TIDAddr = Builder.CreateAlloca(Int32, nullptr, "tid.addr");
489 AllocaInst *ZeroAddr = Builder.CreateAlloca(Int32, nullptr, "zero.addr");
490
491 // If there is an if condition we actually use the TIDAddr and ZeroAddr in the
492 // program, otherwise we only need them for modeling purposes to get the
493 // associated arguments in the outlined function. In the former case,
494 // initialize the allocas properly, in the latter case, delete them later.
495 if (IfCondition) {
6
Assuming 'IfCondition' is null
7
Taking false branch
496 Builder.CreateStore(Constant::getNullValue(Int32), TIDAddr);
497 Builder.CreateStore(Constant::getNullValue(Int32), ZeroAddr);
498 } else {
499 ToBeDeleted.push_back(TIDAddr);
500 ToBeDeleted.push_back(ZeroAddr);
501 }
502
503 // Create an artificial insertion point that will also ensure the blocks we
504 // are about to split are not degenerated.
505 auto *UI = new UnreachableInst(Builder.getContext(), InsertBB);
506
507 Instruction *ThenTI = UI, *ElseTI = nullptr;
508 if (IfCondition
7.1
'IfCondition' is null
7.1
'IfCondition' is null
7.1
'IfCondition' is null
)
8
Taking false branch
509 SplitBlockAndInsertIfThenElse(IfCondition, UI, &ThenTI, &ElseTI);
510
511 BasicBlock *ThenBB = ThenTI->getParent();
512 BasicBlock *PRegEntryBB = ThenBB->splitBasicBlock(ThenTI, "omp.par.entry");
513 BasicBlock *PRegBodyBB =
514 PRegEntryBB->splitBasicBlock(ThenTI, "omp.par.region");
515 BasicBlock *PRegPreFiniBB =
516 PRegBodyBB->splitBasicBlock(ThenTI, "omp.par.pre_finalize");
517 BasicBlock *PRegExitBB =
518 PRegPreFiniBB->splitBasicBlock(ThenTI, "omp.par.exit");
519
520 auto FiniCBWrapper = [&](InsertPointTy IP) {
521 // Hide "open-ended" blocks from the given FiniCB by setting the right jump
522 // target to the region exit block.
523 if (IP.getBlock()->end() == IP.getPoint()) {
524 IRBuilder<>::InsertPointGuard IPG(Builder);
525 Builder.restoreIP(IP);
526 Instruction *I = Builder.CreateBr(PRegExitBB);
527 IP = InsertPointTy(I->getParent(), I->getIterator());
528 }
529 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 531, __PRETTY_FUNCTION__))
530 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 531, __PRETTY_FUNCTION__))
531 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 531, __PRETTY_FUNCTION__))
;
532 return FiniCB(IP);
533 };
534
535 FinalizationStack.push_back({FiniCBWrapper, OMPD_parallel, IsCancellable});
536
537 // Generate the privatization allocas in the block that will become the entry
538 // of the outlined function.
539 Builder.SetInsertPoint(PRegEntryBB->getTerminator());
540 InsertPointTy InnerAllocaIP = Builder.saveIP();
541
542 AllocaInst *PrivTIDAddr =
543 Builder.CreateAlloca(Int32, nullptr, "tid.addr.local");
544 Instruction *PrivTID = Builder.CreateLoad(PrivTIDAddr, "tid");
545
546 // Add some fake uses for OpenMP provided arguments.
547 ToBeDeleted.push_back(Builder.CreateLoad(TIDAddr, "tid.addr.use"));
548 Instruction *ZeroAddrUse = Builder.CreateLoad(ZeroAddr, "zero.addr.use");
549 ToBeDeleted.push_back(ZeroAddrUse);
550
551 // ThenBB
552 // |
553 // V
554 // PRegionEntryBB <- Privatization allocas are placed here.
555 // |
556 // V
557 // PRegionBodyBB <- BodeGen is invoked here.
558 // |
559 // V
560 // PRegPreFiniBB <- The block we will start finalization from.
561 // |
562 // V
563 // PRegionExitBB <- A common exit to simplify block collection.
564 //
565
566 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)
;
9
Assuming 'DebugFlag' is false
10
Loop condition is false. Exiting loop
567
568 // Let the caller create the body.
569 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 569, __PRETTY_FUNCTION__))
;
11
Assuming the condition is true
12
'?' condition is true
570 InsertPointTy CodeGenIP(PRegBodyBB, PRegBodyBB->begin());
571 BodyGenCB(InnerAllocaIP, CodeGenIP, *PRegPreFiniBB);
572
573 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)
;
13
Assuming 'DebugFlag' is false
14
Loop condition is false. Exiting loop
574
575 FunctionCallee RTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_fork_call);
576 if (auto *F
15.1
'F' is null
15.1
'F' is null
15.1
'F' is null
= dyn_cast<llvm::Function>(RTLFn.getCallee())) {
15
Assuming the object is not a 'Function'
16
Taking false branch
577 if (!F->hasMetadata(llvm::LLVMContext::MD_callback)) {
578 llvm::LLVMContext &Ctx = F->getContext();
579 MDBuilder MDB(Ctx);
580 // Annotate the callback behavior of the __kmpc_fork_call:
581 // - The callback callee is argument number 2 (microtask).
582 // - The first two arguments of the callback callee are unknown (-1).
583 // - All variadic arguments to the __kmpc_fork_call are passed to the
584 // callback callee.
585 F->addMetadata(
586 llvm::LLVMContext::MD_callback,
587 *llvm::MDNode::get(
588 Ctx, {MDB.createCallbackEncoding(2, {-1, -1},
589 /* VarArgsArePassed */ true)}));
590 }
591 }
592
593 OutlineInfo OI;
594 OI.PostOutlineCB = [=](Function &OutlinedFn) {
595 // Add some known attributes.
596 OutlinedFn.addParamAttr(0, Attribute::NoAlias);
597 OutlinedFn.addParamAttr(1, Attribute::NoAlias);
598 OutlinedFn.addFnAttr(Attribute::NoUnwind);
599 OutlinedFn.addFnAttr(Attribute::NoRecurse);
600
601 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 602, __PRETTY_FUNCTION__))
602 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 602, __PRETTY_FUNCTION__))
;
603 unsigned NumCapturedVars =
604 OutlinedFn.arg_size() - /* tid & bounded tid */ 2;
605
606 CallInst *CI = cast<CallInst>(OutlinedFn.user_back());
607 CI->getParent()->setName("omp_parallel");
608 Builder.SetInsertPoint(CI);
609
610 // Build call __kmpc_fork_call(Ident, n, microtask, var1, .., varn);
611 Value *ForkCallArgs[] = {
612 Ident, Builder.getInt32(NumCapturedVars),
613 Builder.CreateBitCast(&OutlinedFn, ParallelTaskPtr)};
614
615 SmallVector<Value *, 16> RealArgs;
616 RealArgs.append(std::begin(ForkCallArgs), std::end(ForkCallArgs));
617 RealArgs.append(CI->arg_begin() + /* tid & bound tid */ 2, CI->arg_end());
618
619 Builder.CreateCall(RTLFn, RealArgs);
620
621 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)
622 << *Builder.GetInsertBlock()->getParent() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "With fork_call placed: "
<< *Builder.GetInsertBlock()->getParent() << "\n"
; } } while (false)
;
623
624 InsertPointTy ExitIP(PRegExitBB, PRegExitBB->end());
625
626 // Initialize the local TID stack location with the argument value.
627 Builder.SetInsertPoint(PrivTID);
628 Function::arg_iterator OutlinedAI = OutlinedFn.arg_begin();
629 Builder.CreateStore(Builder.CreateLoad(OutlinedAI), PrivTIDAddr);
630
631 // If no "if" clause was present we do not need the call created during
632 // outlining, otherwise we reuse it in the serialized parallel region.
633 if (!ElseTI) {
634 CI->eraseFromParent();
635 } else {
636
637 // If an "if" clause was present we are now generating the serialized
638 // version into the "else" branch.
639 Builder.SetInsertPoint(ElseTI);
640
641 // Build calls __kmpc_serialized_parallel(&Ident, GTid);
642 Value *SerializedParallelCallArgs[] = {Ident, ThreadID};
643 Builder.CreateCall(
644 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_serialized_parallel),
645 SerializedParallelCallArgs);
646
647 // OutlinedFn(&GTid, &zero, CapturedStruct);
648 CI->removeFromParent();
649 Builder.Insert(CI);
650
651 // __kmpc_end_serialized_parallel(&Ident, GTid);
652 Value *EndArgs[] = {Ident, ThreadID};
653 Builder.CreateCall(
654 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_serialized_parallel),
655 EndArgs);
656
657 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)
658 << *Builder.GetInsertBlock()->getParent() << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "With serialized parallel region: "
<< *Builder.GetInsertBlock()->getParent() << "\n"
; } } while (false)
;
659 }
660
661 for (Instruction *I : ToBeDeleted)
662 I->eraseFromParent();
663 };
664
665 // Adjust the finalization stack, verify the adjustment, and call the
666 // finalize function a last time to finalize values between the pre-fini
667 // block and the exit block if we left the parallel "the normal way".
668 auto FiniInfo = FinalizationStack.pop_back_val();
669 (void)FiniInfo;
670 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 671, __PRETTY_FUNCTION__))
17
Assuming 'OMPD_parallel' is equal to field 'DK'
18
'?' condition is true
671 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 671, __PRETTY_FUNCTION__))
;
672
673 Instruction *PRegPreFiniTI = PRegPreFiniBB->getTerminator();
674
675 InsertPointTy PreFiniIP(PRegPreFiniBB, PRegPreFiniTI->getIterator());
676 FiniCB(PreFiniIP);
677
678 OI.EntryBB = PRegEntryBB;
679 OI.ExitBB = PRegExitBB;
680
681 SmallPtrSet<BasicBlock *, 32> ParallelRegionBlockSet;
682 SmallVector<BasicBlock *, 32> Blocks;
683 OI.collectBlocks(ParallelRegionBlockSet, Blocks);
684
685 // Ensure a single exit node for the outlined region by creating one.
686 // We might have multiple incoming edges to the exit now due to finalizations,
687 // e.g., cancel calls that cause the control flow to leave the region.
688 BasicBlock *PRegOutlinedExitBB = PRegExitBB;
689 PRegExitBB = SplitBlock(PRegExitBB, &*PRegExitBB->getFirstInsertionPt());
690 PRegOutlinedExitBB->setName("omp.par.outlined.exit");
691 Blocks.push_back(PRegOutlinedExitBB);
692
693 CodeExtractorAnalysisCache CEAC(*OuterFn);
694 CodeExtractor Extractor(Blocks, /* DominatorTree */ nullptr,
695 /* AggregateArgs */ false,
696 /* BlockFrequencyInfo */ nullptr,
697 /* BranchProbabilityInfo */ nullptr,
698 /* AssumptionCache */ nullptr,
699 /* AllowVarArgs */ true,
700 /* AllowAlloca */ true,
701 /* Suffix */ ".omp_par");
702
703 // Find inputs to, outputs from the code region.
704 BasicBlock *CommonExit = nullptr;
705 SetVector<Value *> Inputs, Outputs, SinkingCands, HoistingCands;
706 Extractor.findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
707 Extractor.findInputsOutputs(Inputs, Outputs, SinkingCands);
708
709 LLVM_DEBUG(dbgs() << "Before privatization: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Before privatization: "
<< *OuterFn << "\n"; } } while (false)
;
19
Assuming 'DebugFlag' is false
20
Loop condition is false. Exiting loop
710
711 FunctionCallee TIDRTLFn =
712 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_global_thread_num);
713
714 auto PrivHelper = [&](Value &V) {
715 if (&V == TIDAddr || &V == ZeroAddr)
24
Assuming the condition is false
25
Assuming the condition is false
26
Taking false branch
716 return;
717
718 SetVector<Use *> Uses;
719 for (Use &U : V.uses())
720 if (auto *UserI = dyn_cast<Instruction>(U.getUser()))
721 if (ParallelRegionBlockSet.count(UserI->getParent()))
722 Uses.insert(&U);
723
724 // __kmpc_fork_call expects extra arguments as pointers. If the input
725 // already has a pointer type, everything is fine. Otherwise, store the
726 // value onto stack and load it back inside the to-be-outlined region. This
727 // will ensure only the pointer will be passed to the function.
728 // FIXME: if there are more than 15 trailing arguments, they must be
729 // additionally packed in a struct.
730 Value *Inner = &V;
731 if (!V.getType()->isPointerTy()) {
27
Taking true branch
732 IRBuilder<>::InsertPointGuard Guard(Builder);
733 LLVM_DEBUG(llvm::dbgs() << "Forwarding input as pointer: " << V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { llvm::dbgs() << "Forwarding input as pointer: "
<< V << "\n"; } } while (false)
;
28
Assuming 'DebugFlag' is false
29
Loop condition is false. Exiting loop
734
735 Builder.restoreIP(OuterAllocaIP);
736 Value *Ptr =
737 Builder.CreateAlloca(V.getType(), nullptr, V.getName() + ".reloaded");
738
739 // Store to stack at end of the block that currently branches to the entry
740 // block of the to-be-outlined region.
741 Builder.SetInsertPoint(InsertBB,
742 InsertBB->getTerminator()->getIterator());
743 Builder.CreateStore(&V, Ptr);
744
745 // Load back next to allocations in the to-be-outlined region.
746 Builder.restoreIP(InnerAllocaIP);
30
Calling 'IRBuilderBase::restoreIP'
35
Returning from 'IRBuilderBase::restoreIP'
747 Inner = Builder.CreateLoad(Ptr);
36
Calling 'IRBuilderBase::CreateLoad'
748 }
749
750 Value *ReplacementValue = nullptr;
751 CallInst *CI = dyn_cast<CallInst>(&V);
752 if (CI && CI->getCalledFunction() == TIDRTLFn.getCallee()) {
753 ReplacementValue = PrivTID;
754 } else {
755 Builder.restoreIP(
756 PrivCB(InnerAllocaIP, Builder.saveIP(), V, *Inner, ReplacementValue));
757 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 758, __PRETTY_FUNCTION__))
758 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 758, __PRETTY_FUNCTION__))
;
759 if (ReplacementValue == &V)
760 return;
761 }
762
763 for (Use *UPtr : Uses)
764 UPtr->set(ReplacementValue);
765 };
766
767 // Reset the inner alloca insertion as it will be used for loading the values
768 // wrapped into pointers before passing them into the to-be-outlined region.
769 // Configure it to insert immediately after the fake use of zero address so
770 // that they are available in the generated body and so that the
771 // OpenMP-related values (thread ID and zero address pointers) remain leading
772 // in the argument list.
773 InnerAllocaIP = IRBuilder<>::InsertPoint(
774 ZeroAddrUse->getParent(), ZeroAddrUse->getNextNode()->getIterator());
775
776 // Reset the outer alloca insertion point to the entry of the relevant block
777 // in case it was invalidated.
778 OuterAllocaIP = IRBuilder<>::InsertPoint(
779 OuterAllocaBlock, OuterAllocaBlock->getFirstInsertionPt());
780
781 for (Value *Input : Inputs) {
782 LLVM_DEBUG(dbgs() << "Captured input: " << *Input << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "Captured input: " <<
*Input << "\n"; } } while (false)
;
21
Assuming 'DebugFlag' is false
22
Loop condition is false. Exiting loop
783 PrivHelper(*Input);
23
Calling 'operator()'
784 }
785 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
)
786 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
)
787 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
)
788 })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
)
;
789 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 790, __PRETTY_FUNCTION__))
790 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 790, __PRETTY_FUNCTION__))
;
791
792 LLVM_DEBUG(dbgs() << "After privatization: " << *OuterFn << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { dbgs() << "After privatization: "
<< *OuterFn << "\n"; } } while (false)
;
793 LLVM_DEBUG({do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (auto *BB : Blocks) dbgs() <<
" PBR: " << BB->getName() << "\n"; }; } } while
(false)
794 for (auto *BB : Blocks)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (auto *BB : Blocks) dbgs() <<
" PBR: " << BB->getName() << "\n"; }; } } while
(false)
795 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)
796 })do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("openmp-ir-builder")) { { for (auto *BB : Blocks) dbgs() <<
" PBR: " << BB->getName() << "\n"; }; } } while
(false)
;
797
798 // Register the outlined info.
799 addOutlineInfo(std::move(OI));
800
801 InsertPointTy AfterIP(UI->getParent(), UI->getParent()->end());
802 UI->eraseFromParent();
803
804 return AfterIP;
805}
806
807void OpenMPIRBuilder::emitFlush(const LocationDescription &Loc) {
808 // Build call void __kmpc_flush(ident_t *loc)
809 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
810 Value *Args[] = {getOrCreateIdent(SrcLocStr)};
811
812 Builder.CreateCall(getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_flush), Args);
813}
814
815void OpenMPIRBuilder::createFlush(const LocationDescription &Loc) {
816 if (!updateToLocation(Loc))
817 return;
818 emitFlush(Loc);
819}
820
821void OpenMPIRBuilder::emitTaskwaitImpl(const LocationDescription &Loc) {
822 // Build call kmp_int32 __kmpc_omp_taskwait(ident_t *loc, kmp_int32
823 // global_tid);
824 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
825 Value *Ident = getOrCreateIdent(SrcLocStr);
826 Value *Args[] = {Ident, getOrCreateThreadID(Ident)};
827
828 // Ignore return result until untied tasks are supported.
829 Builder.CreateCall(getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_taskwait),
830 Args);
831}
832
833void OpenMPIRBuilder::createTaskwait(const LocationDescription &Loc) {
834 if (!updateToLocation(Loc))
835 return;
836 emitTaskwaitImpl(Loc);
837}
838
839void OpenMPIRBuilder::emitTaskyieldImpl(const LocationDescription &Loc) {
840 // Build call __kmpc_omp_taskyield(loc, thread_id, 0);
841 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
842 Value *Ident = getOrCreateIdent(SrcLocStr);
843 Constant *I32Null = ConstantInt::getNullValue(Int32);
844 Value *Args[] = {Ident, getOrCreateThreadID(Ident), I32Null};
845
846 Builder.CreateCall(getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_omp_taskyield),
847 Args);
848}
849
850void OpenMPIRBuilder::createTaskyield(const LocationDescription &Loc) {
851 if (!updateToLocation(Loc))
852 return;
853 emitTaskyieldImpl(Loc);
854}
855
856OpenMPIRBuilder::InsertPointTy
857OpenMPIRBuilder::createMaster(const LocationDescription &Loc,
858 BodyGenCallbackTy BodyGenCB,
859 FinalizeCallbackTy FiniCB) {
860
861 if (!updateToLocation(Loc))
862 return Loc.IP;
863
864 Directive OMPD = Directive::OMPD_master;
865 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
866 Value *Ident = getOrCreateIdent(SrcLocStr);
867 Value *ThreadId = getOrCreateThreadID(Ident);
868 Value *Args[] = {Ident, ThreadId};
869
870 Function *EntryRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_master);
871 Instruction *EntryCall = Builder.CreateCall(EntryRTLFn, Args);
872
873 Function *ExitRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_master);
874 Instruction *ExitCall = Builder.CreateCall(ExitRTLFn, Args);
875
876 return EmitOMPInlinedRegion(OMPD, EntryCall, ExitCall, BodyGenCB, FiniCB,
877 /*Conditional*/ true, /*hasFinalize*/ true);
878}
879
880CanonicalLoopInfo *OpenMPIRBuilder::createLoopSkeleton(
881 DebugLoc DL, Value *TripCount, Function *F, BasicBlock *PreInsertBefore,
882 BasicBlock *PostInsertBefore, const Twine &Name) {
883 Module *M = F->getParent();
884 LLVMContext &Ctx = M->getContext();
885 Type *IndVarTy = TripCount->getType();
886
887 // Create the basic block structure.
888 BasicBlock *Preheader =
889 BasicBlock::Create(Ctx, "omp_" + Name + ".preheader", F, PreInsertBefore);
890 BasicBlock *Header =
891 BasicBlock::Create(Ctx, "omp_" + Name + ".header", F, PreInsertBefore);
892 BasicBlock *Cond =
893 BasicBlock::Create(Ctx, "omp_" + Name + ".cond", F, PreInsertBefore);
894 BasicBlock *Body =
895 BasicBlock::Create(Ctx, "omp_" + Name + ".body", F, PreInsertBefore);
896 BasicBlock *Latch =
897 BasicBlock::Create(Ctx, "omp_" + Name + ".inc", F, PostInsertBefore);
898 BasicBlock *Exit =
899 BasicBlock::Create(Ctx, "omp_" + Name + ".exit", F, PostInsertBefore);
900 BasicBlock *After =
901 BasicBlock::Create(Ctx, "omp_" + Name + ".after", F, PostInsertBefore);
902
903 // Use specified DebugLoc for new instructions.
904 Builder.SetCurrentDebugLocation(DL);
905
906 Builder.SetInsertPoint(Preheader);
907 Builder.CreateBr(Header);
908
909 Builder.SetInsertPoint(Header);
910 PHINode *IndVarPHI = Builder.CreatePHI(IndVarTy, 2, "omp_" + Name + ".iv");
911 IndVarPHI->addIncoming(ConstantInt::get(IndVarTy, 0), Preheader);
912 Builder.CreateBr(Cond);
913
914 Builder.SetInsertPoint(Cond);
915 Value *Cmp =
916 Builder.CreateICmpULT(IndVarPHI, TripCount, "omp_" + Name + ".cmp");
917 Builder.CreateCondBr(Cmp, Body, Exit);
918
919 Builder.SetInsertPoint(Body);
920 Builder.CreateBr(Latch);
921
922 Builder.SetInsertPoint(Latch);
923 Value *Next = Builder.CreateAdd(IndVarPHI, ConstantInt::get(IndVarTy, 1),
924 "omp_" + Name + ".next", /*HasNUW=*/true);
925 Builder.CreateBr(Header);
926 IndVarPHI->addIncoming(Next, Latch);
927
928 Builder.SetInsertPoint(Exit);
929 Builder.CreateBr(After);
930
931 // Remember and return the canonical control flow.
932 LoopInfos.emplace_front();
933 CanonicalLoopInfo *CL = &LoopInfos.front();
934
935 CL->Preheader = Preheader;
936 CL->Header = Header;
937 CL->Cond = Cond;
938 CL->Body = Body;
939 CL->Latch = Latch;
940 CL->Exit = Exit;
941 CL->After = After;
942
943 CL->IsValid = true;
944
945#ifndef NDEBUG
946 CL->assertOK();
947#endif
948 return CL;
949}
950
951CanonicalLoopInfo *
952OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc,
953 LoopBodyGenCallbackTy BodyGenCB,
954 Value *TripCount, const Twine &Name) {
955 BasicBlock *BB = Loc.IP.getBlock();
956 BasicBlock *NextBB = BB->getNextNode();
957
958 CanonicalLoopInfo *CL = createLoopSkeleton(Loc.DL, TripCount, BB->getParent(),
959 NextBB, NextBB, Name);
960 BasicBlock *After = CL->getAfter();
961
962 // If location is not set, don't connect the loop.
963 if (updateToLocation(Loc)) {
964 // Split the loop at the insertion point: Branch to the preheader and move
965 // every following instruction to after the loop (the After BB). Also, the
966 // new successor is the loop's after block.
967 Builder.CreateBr(CL->Preheader);
968 After->getInstList().splice(After->begin(), BB->getInstList(),
969 Builder.GetInsertPoint(), BB->end());
970 After->replaceSuccessorsPhiUsesWith(BB, After);
971 }
972
973 // Emit the body content. We do it after connecting the loop to the CFG to
974 // avoid that the callback encounters degenerate BBs.
975 BodyGenCB(CL->getBodyIP(), CL->getIndVar());
976
977#ifndef NDEBUG
978 CL->assertOK();
979#endif
980 return CL;
981}
982
983CanonicalLoopInfo *OpenMPIRBuilder::createCanonicalLoop(
984 const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB,
985 Value *Start, Value *Stop, Value *Step, bool IsSigned, bool InclusiveStop,
986 InsertPointTy ComputeIP, const Twine &Name) {
987
988 // Consider the following difficulties (assuming 8-bit signed integers):
989 // * Adding \p Step to the loop counter which passes \p Stop may overflow:
990 // DO I = 1, 100, 50
991 /// * A \p Step of INT_MIN cannot not be normalized to a positive direction:
992 // DO I = 100, 0, -128
993
994 // Start, Stop and Step must be of the same integer type.
995 auto *IndVarTy = cast<IntegerType>(Start->getType());
996 assert(IndVarTy == Stop->getType() && "Stop type mismatch")((IndVarTy == Stop->getType() && "Stop type mismatch"
) ? static_cast<void> (0) : __assert_fail ("IndVarTy == Stop->getType() && \"Stop type mismatch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 996, __PRETTY_FUNCTION__))
;
997 assert(IndVarTy == Step->getType() && "Step type mismatch")((IndVarTy == Step->getType() && "Step type mismatch"
) ? static_cast<void> (0) : __assert_fail ("IndVarTy == Step->getType() && \"Step type mismatch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 997, __PRETTY_FUNCTION__))
;
998
999 LocationDescription ComputeLoc =
1000 ComputeIP.isSet() ? LocationDescription(ComputeIP, Loc.DL) : Loc;
1001 updateToLocation(ComputeLoc);
1002
1003 ConstantInt *Zero = ConstantInt::get(IndVarTy, 0);
1004 ConstantInt *One = ConstantInt::get(IndVarTy, 1);
1005
1006 // Like Step, but always positive.
1007 Value *Incr = Step;
1008
1009 // Distance between Start and Stop; always positive.
1010 Value *Span;
1011
1012 // Condition whether there are no iterations are executed at all, e.g. because
1013 // UB < LB.
1014 Value *ZeroCmp;
1015
1016 if (IsSigned) {
1017 // Ensure that increment is positive. If not, negate and invert LB and UB.
1018 Value *IsNeg = Builder.CreateICmpSLT(Step, Zero);
1019 Incr = Builder.CreateSelect(IsNeg, Builder.CreateNeg(Step), Step);
1020 Value *LB = Builder.CreateSelect(IsNeg, Stop, Start);
1021 Value *UB = Builder.CreateSelect(IsNeg, Start, Stop);
1022 Span = Builder.CreateSub(UB, LB, "", false, true);
1023 ZeroCmp = Builder.CreateICmp(
1024 InclusiveStop ? CmpInst::ICMP_SLT : CmpInst::ICMP_SLE, UB, LB);
1025 } else {
1026 Span = Builder.CreateSub(Stop, Start, "", true);
1027 ZeroCmp = Builder.CreateICmp(
1028 InclusiveStop ? CmpInst::ICMP_ULT : CmpInst::ICMP_ULE, Stop, Start);
1029 }
1030
1031 Value *CountIfLooping;
1032 if (InclusiveStop) {
1033 CountIfLooping = Builder.CreateAdd(Builder.CreateUDiv(Span, Incr), One);
1034 } else {
1035 // Avoid incrementing past stop since it could overflow.
1036 Value *CountIfTwo = Builder.CreateAdd(
1037 Builder.CreateUDiv(Builder.CreateSub(Span, One), Incr), One);
1038 Value *OneCmp = Builder.CreateICmp(
1039 InclusiveStop ? CmpInst::ICMP_ULT : CmpInst::ICMP_ULE, Span, Incr);
1040 CountIfLooping = Builder.CreateSelect(OneCmp, One, CountIfTwo);
1041 }
1042 Value *TripCount = Builder.CreateSelect(ZeroCmp, Zero, CountIfLooping,
1043 "omp_" + Name + ".tripcount");
1044
1045 auto BodyGen = [=](InsertPointTy CodeGenIP, Value *IV) {
1046 Builder.restoreIP(CodeGenIP);
1047 Value *Span = Builder.CreateMul(IV, Step);
1048 Value *IndVar = Builder.CreateAdd(Span, Start);
1049 BodyGenCB(Builder.saveIP(), IndVar);
1050 };
1051 LocationDescription LoopLoc = ComputeIP.isSet() ? Loc.IP : Builder.saveIP();
1052 return createCanonicalLoop(LoopLoc, BodyGen, TripCount, Name);
1053}
1054
1055// Returns an LLVM function to call for initializing loop bounds using OpenMP
1056// static scheduling depending on `type`. Only i32 and i64 are supported by the
1057// runtime. Always interpret integers as unsigned similarly to
1058// CanonicalLoopInfo.
1059static FunctionCallee getKmpcForStaticInitForType(Type *Ty, Module &M,
1060 OpenMPIRBuilder &OMPBuilder) {
1061 unsigned Bitwidth = Ty->getIntegerBitWidth();
1062 if (Bitwidth == 32)
1063 return OMPBuilder.getOrCreateRuntimeFunction(
1064 M, omp::RuntimeFunction::OMPRTL___kmpc_for_static_init_4u);
1065 if (Bitwidth == 64)
1066 return OMPBuilder.getOrCreateRuntimeFunction(
1067 M, omp::RuntimeFunction::OMPRTL___kmpc_for_static_init_8u);
1068 llvm_unreachable("unknown OpenMP loop iterator bitwidth")::llvm::llvm_unreachable_internal("unknown OpenMP loop iterator bitwidth"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1068)
;
1069}
1070
1071// Sets the number of loop iterations to the given value. This value must be
1072// valid in the condition block (i.e., defined in the preheader) and is
1073// interpreted as an unsigned integer.
1074void setCanonicalLoopTripCount(CanonicalLoopInfo *CLI, Value *TripCount) {
1075 Instruction *CmpI = &CLI->getCond()->front();
1076 assert(isa<CmpInst>(CmpI) && "First inst must compare IV with TripCount")((isa<CmpInst>(CmpI) && "First inst must compare IV with TripCount"
) ? static_cast<void> (0) : __assert_fail ("isa<CmpInst>(CmpI) && \"First inst must compare IV with TripCount\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1076, __PRETTY_FUNCTION__))
;
1077 CmpI->setOperand(1, TripCount);
1078 CLI->assertOK();
1079}
1080
1081CanonicalLoopInfo *OpenMPIRBuilder::createStaticWorkshareLoop(
1082 const LocationDescription &Loc, CanonicalLoopInfo *CLI,
1083 InsertPointTy AllocaIP, bool NeedsBarrier, Value *Chunk) {
1084 // Set up the source location value for OpenMP runtime.
1085 if (!updateToLocation(Loc))
1086 return nullptr;
1087
1088 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1089 Value *SrcLoc = getOrCreateIdent(SrcLocStr);
1090
1091 // Declare useful OpenMP runtime functions.
1092 Value *IV = CLI->getIndVar();
1093 Type *IVTy = IV->getType();
1094 FunctionCallee StaticInit = getKmpcForStaticInitForType(IVTy, M, *this);
1095 FunctionCallee StaticFini =
1096 getOrCreateRuntimeFunction(M, omp::OMPRTL___kmpc_for_static_fini);
1097
1098 // Allocate space for computed loop bounds as expected by the "init" function.
1099 Builder.restoreIP(AllocaIP);
1100 Type *I32Type = Type::getInt32Ty(M.getContext());
1101 Value *PLastIter = Builder.CreateAlloca(I32Type, nullptr, "p.lastiter");
1102 Value *PLowerBound = Builder.CreateAlloca(IVTy, nullptr, "p.lowerbound");
1103 Value *PUpperBound = Builder.CreateAlloca(IVTy, nullptr, "p.upperbound");
1104 Value *PStride = Builder.CreateAlloca(IVTy, nullptr, "p.stride");
1105
1106 // At the end of the preheader, prepare for calling the "init" function by
1107 // storing the current loop bounds into the allocated space. A canonical loop
1108 // always iterates from 0 to trip-count with step 1. Note that "init" expects
1109 // and produces an inclusive upper bound.
1110 Builder.SetInsertPoint(CLI->getPreheader()->getTerminator());
1111 Constant *Zero = ConstantInt::get(IVTy, 0);
1112 Constant *One = ConstantInt::get(IVTy, 1);
1113 Builder.CreateStore(Zero, PLowerBound);
1114 Value *UpperBound = Builder.CreateSub(CLI->getTripCount(), One);
1115 Builder.CreateStore(UpperBound, PUpperBound);
1116 Builder.CreateStore(One, PStride);
1117
1118 if (!Chunk)
1119 Chunk = One;
1120
1121 Value *ThreadNum = getOrCreateThreadID(SrcLoc);
1122
1123 // TODO: extract scheduling type and map it to OMP constant. This is curently
1124 // happening in kmp.h and its ilk and needs to be moved to OpenMP.td first.
1125 constexpr int StaticSchedType = 34;
1126 Constant *SchedulingType = ConstantInt::get(I32Type, StaticSchedType);
1127
1128 // Call the "init" function and update the trip count of the loop with the
1129 // value it produced.
1130 Builder.CreateCall(StaticInit,
1131 {SrcLoc, ThreadNum, SchedulingType, PLastIter, PLowerBound,
1132 PUpperBound, PStride, One, Chunk});
1133 Value *LowerBound = Builder.CreateLoad(PLowerBound);
1134 Value *InclusiveUpperBound = Builder.CreateLoad(PUpperBound);
1135 Value *TripCountMinusOne = Builder.CreateSub(InclusiveUpperBound, LowerBound);
1136 Value *TripCount = Builder.CreateAdd(TripCountMinusOne, One);
1137 setCanonicalLoopTripCount(CLI, TripCount);
1138
1139 // Update all uses of the induction variable except the one in the condition
1140 // block that compares it with the actual upper bound, and the increment in
1141 // the latch block.
1142 // TODO: this can eventually move to CanonicalLoopInfo or to a new
1143 // CanonicalLoopInfoUpdater interface.
1144 Builder.SetInsertPoint(CLI->getBody(), CLI->getBody()->getFirstInsertionPt());
1145 Value *UpdatedIV = Builder.CreateAdd(IV, LowerBound);
1146 IV->replaceUsesWithIf(UpdatedIV, [&](Use &U) {
1147 auto *Instr = dyn_cast<Instruction>(U.getUser());
1148 return !Instr ||
1149 (Instr->getParent() != CLI->getCond() &&
1150 Instr->getParent() != CLI->getLatch() && Instr != UpdatedIV);
1151 });
1152
1153 // In the "exit" block, call the "fini" function.
1154 Builder.SetInsertPoint(CLI->getExit(),
1155 CLI->getExit()->getTerminator()->getIterator());
1156 Builder.CreateCall(StaticFini, {SrcLoc, ThreadNum});
1157
1158 // Add the barrier if requested.
1159 if (NeedsBarrier)
1160 createBarrier(LocationDescription(Builder.saveIP(), Loc.DL),
1161 omp::Directive::OMPD_for, /* ForceSimpleCall */ false,
1162 /* CheckCancelFlag */ false);
1163
1164 CLI->assertOK();
1165 return CLI;
1166}
1167
1168OpenMPIRBuilder::InsertPointTy
1169OpenMPIRBuilder::createCopyPrivate(const LocationDescription &Loc,
1170 llvm::Value *BufSize, llvm::Value *CpyBuf,
1171 llvm::Value *CpyFn, llvm::Value *DidIt) {
1172 if (!updateToLocation(Loc))
1173 return Loc.IP;
1174
1175 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1176 Value *Ident = getOrCreateIdent(SrcLocStr);
1177 Value *ThreadId = getOrCreateThreadID(Ident);
1178
1179 llvm::Value *DidItLD = Builder.CreateLoad(DidIt);
1180
1181 Value *Args[] = {Ident, ThreadId, BufSize, CpyBuf, CpyFn, DidItLD};
1182
1183 Function *Fn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_copyprivate);
1184 Builder.CreateCall(Fn, Args);
1185
1186 return Builder.saveIP();
1187}
1188
1189OpenMPIRBuilder::InsertPointTy
1190OpenMPIRBuilder::createSingle(const LocationDescription &Loc,
1191 BodyGenCallbackTy BodyGenCB,
1192 FinalizeCallbackTy FiniCB, llvm::Value *DidIt) {
1193
1194 if (!updateToLocation(Loc))
1195 return Loc.IP;
1196
1197 // If needed (i.e. not null), initialize `DidIt` with 0
1198 if (DidIt) {
1199 Builder.CreateStore(Builder.getInt32(0), DidIt);
1200 }
1201
1202 Directive OMPD = Directive::OMPD_single;
1203 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1204 Value *Ident = getOrCreateIdent(SrcLocStr);
1205 Value *ThreadId = getOrCreateThreadID(Ident);
1206 Value *Args[] = {Ident, ThreadId};
1207
1208 Function *EntryRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_single);
1209 Instruction *EntryCall = Builder.CreateCall(EntryRTLFn, Args);
1210
1211 Function *ExitRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_single);
1212 Instruction *ExitCall = Builder.CreateCall(ExitRTLFn, Args);
1213
1214 // generates the following:
1215 // if (__kmpc_single()) {
1216 // .... single region ...
1217 // __kmpc_end_single
1218 // }
1219
1220 return EmitOMPInlinedRegion(OMPD, EntryCall, ExitCall, BodyGenCB, FiniCB,
1221 /*Conditional*/ true, /*hasFinalize*/ true);
1222}
1223
1224OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createCritical(
1225 const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB,
1226 FinalizeCallbackTy FiniCB, StringRef CriticalName, Value *HintInst) {
1227
1228 if (!updateToLocation(Loc))
1229 return Loc.IP;
1230
1231 Directive OMPD = Directive::OMPD_critical;
1232 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1233 Value *Ident = getOrCreateIdent(SrcLocStr);
1234 Value *ThreadId = getOrCreateThreadID(Ident);
1235 Value *LockVar = getOMPCriticalRegionLock(CriticalName);
1236 Value *Args[] = {Ident, ThreadId, LockVar};
1237
1238 SmallVector<llvm::Value *, 4> EnterArgs(std::begin(Args), std::end(Args));
1239 Function *RTFn = nullptr;
1240 if (HintInst) {
1241 // Add Hint to entry Args and create call
1242 EnterArgs.push_back(HintInst);
1243 RTFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_critical_with_hint);
1244 } else {
1245 RTFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_critical);
1246 }
1247 Instruction *EntryCall = Builder.CreateCall(RTFn, EnterArgs);
1248
1249 Function *ExitRTLFn =
1250 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_critical);
1251 Instruction *ExitCall = Builder.CreateCall(ExitRTLFn, Args);
1252
1253 return EmitOMPInlinedRegion(OMPD, EntryCall, ExitCall, BodyGenCB, FiniCB,
1254 /*Conditional*/ false, /*hasFinalize*/ true);
1255}
1256
1257OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::EmitOMPInlinedRegion(
1258 Directive OMPD, Instruction *EntryCall, Instruction *ExitCall,
1259 BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool Conditional,
1260 bool HasFinalize) {
1261
1262 if (HasFinalize)
1263 FinalizationStack.push_back({FiniCB, OMPD, /*IsCancellable*/ false});
1264
1265 // Create inlined region's entry and body blocks, in preparation
1266 // for conditional creation
1267 BasicBlock *EntryBB = Builder.GetInsertBlock();
1268 Instruction *SplitPos = EntryBB->getTerminator();
1269 if (!isa_and_nonnull<BranchInst>(SplitPos))
1270 SplitPos = new UnreachableInst(Builder.getContext(), EntryBB);
1271 BasicBlock *ExitBB = EntryBB->splitBasicBlock(SplitPos, "omp_region.end");
1272 BasicBlock *FiniBB =
1273 EntryBB->splitBasicBlock(EntryBB->getTerminator(), "omp_region.finalize");
1274
1275 Builder.SetInsertPoint(EntryBB->getTerminator());
1276 emitCommonDirectiveEntry(OMPD, EntryCall, ExitBB, Conditional);
1277
1278 // generate body
1279 BodyGenCB(/* AllocaIP */ InsertPointTy(),
1280 /* CodeGenIP */ Builder.saveIP(), *FiniBB);
1281
1282 // If we didn't emit a branch to FiniBB during body generation, it means
1283 // FiniBB is unreachable (e.g. while(1);). stop generating all the
1284 // unreachable blocks, and remove anything we are not going to use.
1285 auto SkipEmittingRegion = FiniBB->hasNPredecessors(0);
1286 if (SkipEmittingRegion) {
1287 FiniBB->eraseFromParent();
1288 ExitCall->eraseFromParent();
1289 // Discard finalization if we have it.
1290 if (HasFinalize) {
1291 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1292, __PRETTY_FUNCTION__))
1292 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1292, __PRETTY_FUNCTION__))
;
1293 FinalizationStack.pop_back();
1294 }
1295 } else {
1296 // emit exit call and do any needed finalization.
1297 auto FinIP = InsertPointTy(FiniBB, FiniBB->getFirstInsertionPt());
1298 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1300, __PRETTY_FUNCTION__))
1299 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1300, __PRETTY_FUNCTION__))
1300 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1300, __PRETTY_FUNCTION__))
;
1301 emitCommonDirectiveExit(OMPD, FinIP, ExitCall, HasFinalize);
1302 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1303, __PRETTY_FUNCTION__))
1303 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1303, __PRETTY_FUNCTION__))
;
1304 MergeBlockIntoPredecessor(FiniBB);
1305 }
1306
1307 // If we are skipping the region of a non conditional, remove the exit
1308 // block, and clear the builder's insertion point.
1309 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1310, __PRETTY_FUNCTION__))
1310 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1310, __PRETTY_FUNCTION__))
;
1311 if (!Conditional && SkipEmittingRegion) {
1312 ExitBB->eraseFromParent();
1313 Builder.ClearInsertionPoint();
1314 } else {
1315 auto merged = MergeBlockIntoPredecessor(ExitBB);
1316 BasicBlock *ExitPredBB = SplitPos->getParent();
1317 auto InsertBB = merged ? ExitPredBB : ExitBB;
1318 if (!isa_and_nonnull<BranchInst>(SplitPos))
1319 SplitPos->eraseFromParent();
1320 Builder.SetInsertPoint(InsertBB);
1321 }
1322
1323 return Builder.saveIP();
1324}
1325
1326OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitCommonDirectiveEntry(
1327 Directive OMPD, Value *EntryCall, BasicBlock *ExitBB, bool Conditional) {
1328
1329 // if nothing to do, Return current insertion point.
1330 if (!Conditional)
1331 return Builder.saveIP();
1332
1333 BasicBlock *EntryBB = Builder.GetInsertBlock();
1334 Value *CallBool = Builder.CreateIsNotNull(EntryCall);
1335 auto *ThenBB = BasicBlock::Create(M.getContext(), "omp_region.body");
1336 auto *UI = new UnreachableInst(Builder.getContext(), ThenBB);
1337
1338 // Emit thenBB and set the Builder's insertion point there for
1339 // body generation next. Place the block after the current block.
1340 Function *CurFn = EntryBB->getParent();
1341 CurFn->getBasicBlockList().insertAfter(EntryBB->getIterator(), ThenBB);
1342
1343 // Move Entry branch to end of ThenBB, and replace with conditional
1344 // branch (If-stmt)
1345 Instruction *EntryBBTI = EntryBB->getTerminator();
1346 Builder.CreateCondBr(CallBool, ThenBB, ExitBB);
1347 EntryBBTI->removeFromParent();
1348 Builder.SetInsertPoint(UI);
1349 Builder.Insert(EntryBBTI);
1350 UI->eraseFromParent();
1351 Builder.SetInsertPoint(ThenBB->getTerminator());
1352
1353 // return an insertion point to ExitBB.
1354 return IRBuilder<>::InsertPoint(ExitBB, ExitBB->getFirstInsertionPt());
1355}
1356
1357OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitCommonDirectiveExit(
1358 omp::Directive OMPD, InsertPointTy FinIP, Instruction *ExitCall,
1359 bool HasFinalize) {
1360
1361 Builder.restoreIP(FinIP);
1362
1363 // If there is finalization to do, emit it before the exit call
1364 if (HasFinalize) {
1365 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1366, __PRETTY_FUNCTION__))
1366 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1366, __PRETTY_FUNCTION__))
;
1367
1368 FinalizationInfo Fi = FinalizationStack.pop_back_val();
1369 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1369, __PRETTY_FUNCTION__))
;
1370
1371 Fi.FiniCB(FinIP);
1372
1373 BasicBlock *FiniBB = FinIP.getBlock();
1374 Instruction *FiniBBTI = FiniBB->getTerminator();
1375
1376 // set Builder IP for call creation
1377 Builder.SetInsertPoint(FiniBBTI);
1378 }
1379
1380 // place the Exitcall as last instruction before Finalization block terminator
1381 ExitCall->removeFromParent();
1382 Builder.Insert(ExitCall);
1383
1384 return IRBuilder<>::InsertPoint(ExitCall->getParent(),
1385 ExitCall->getIterator());
1386}
1387
1388OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createCopyinClauseBlocks(
1389 InsertPointTy IP, Value *MasterAddr, Value *PrivateAddr,
1390 llvm::IntegerType *IntPtrTy, bool BranchtoEnd) {
1391 if (!IP.isSet())
1392 return IP;
1393
1394 IRBuilder<>::InsertPointGuard IPG(Builder);
1395
1396 // creates the following CFG structure
1397 // OMP_Entry : (MasterAddr != PrivateAddr)?
1398 // F T
1399 // | \
1400 // | copin.not.master
1401 // | /
1402 // v /
1403 // copyin.not.master.end
1404 // |
1405 // v
1406 // OMP.Entry.Next
1407
1408 BasicBlock *OMP_Entry = IP.getBlock();
1409 Function *CurFn = OMP_Entry->getParent();
1410 BasicBlock *CopyBegin =
1411 BasicBlock::Create(M.getContext(), "copyin.not.master", CurFn);
1412 BasicBlock *CopyEnd = nullptr;
1413
1414 // If entry block is terminated, split to preserve the branch to following
1415 // basic block (i.e. OMP.Entry.Next), otherwise, leave everything as is.
1416 if (isa_and_nonnull<BranchInst>(OMP_Entry->getTerminator())) {
1417 CopyEnd = OMP_Entry->splitBasicBlock(OMP_Entry->getTerminator(),
1418 "copyin.not.master.end");
1419 OMP_Entry->getTerminator()->eraseFromParent();
1420 } else {
1421 CopyEnd =
1422 BasicBlock::Create(M.getContext(), "copyin.not.master.end", CurFn);
1423 }
1424
1425 Builder.SetInsertPoint(OMP_Entry);
1426 Value *MasterPtr = Builder.CreatePtrToInt(MasterAddr, IntPtrTy);
1427 Value *PrivatePtr = Builder.CreatePtrToInt(PrivateAddr, IntPtrTy);
1428 Value *cmp = Builder.CreateICmpNE(MasterPtr, PrivatePtr);
1429 Builder.CreateCondBr(cmp, CopyBegin, CopyEnd);
1430
1431 Builder.SetInsertPoint(CopyBegin);
1432 if (BranchtoEnd)
1433 Builder.SetInsertPoint(Builder.CreateBr(CopyEnd));
1434
1435 return Builder.saveIP();
1436}
1437
1438CallInst *OpenMPIRBuilder::createOMPAlloc(const LocationDescription &Loc,
1439 Value *Size, Value *Allocator,
1440 std::string Name) {
1441 IRBuilder<>::InsertPointGuard IPG(Builder);
1442 Builder.restoreIP(Loc.IP);
1443
1444 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1445 Value *Ident = getOrCreateIdent(SrcLocStr);
1446 Value *ThreadId = getOrCreateThreadID(Ident);
1447 Value *Args[] = {ThreadId, Size, Allocator};
1448
1449 Function *Fn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_alloc);
1450
1451 return Builder.CreateCall(Fn, Args, Name);
1452}
1453
1454CallInst *OpenMPIRBuilder::createOMPFree(const LocationDescription &Loc,
1455 Value *Addr, Value *Allocator,
1456 std::string Name) {
1457 IRBuilder<>::InsertPointGuard IPG(Builder);
1458 Builder.restoreIP(Loc.IP);
1459
1460 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1461 Value *Ident = getOrCreateIdent(SrcLocStr);
1462 Value *ThreadId = getOrCreateThreadID(Ident);
1463 Value *Args[] = {ThreadId, Addr, Allocator};
1464 Function *Fn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_free);
1465 return Builder.CreateCall(Fn, Args, Name);
1466}
1467
1468CallInst *OpenMPIRBuilder::createCachedThreadPrivate(
1469 const LocationDescription &Loc, llvm::Value *Pointer,
1470 llvm::ConstantInt *Size, const llvm::Twine &Name) {
1471 IRBuilder<>::InsertPointGuard IPG(Builder);
1472 Builder.restoreIP(Loc.IP);
1473
1474 Constant *SrcLocStr = getOrCreateSrcLocStr(Loc);
1475 Value *Ident = getOrCreateIdent(SrcLocStr);
1476 Value *ThreadId = getOrCreateThreadID(Ident);
1477 Constant *ThreadPrivateCache =
1478 getOrCreateOMPInternalVariable(Int8PtrPtr, Name);
1479 llvm::Value *Args[] = {Ident, ThreadId, Pointer, Size, ThreadPrivateCache};
1480
1481 Function *Fn =
1482 getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_threadprivate_cached);
1483
1484 return Builder.CreateCall(Fn, Args);
1485}
1486
1487std::string OpenMPIRBuilder::getNameWithSeparators(ArrayRef<StringRef> Parts,
1488 StringRef FirstSeparator,
1489 StringRef Separator) {
1490 SmallString<128> Buffer;
1491 llvm::raw_svector_ostream OS(Buffer);
1492 StringRef Sep = FirstSeparator;
1493 for (StringRef Part : Parts) {
1494 OS << Sep << Part;
1495 Sep = Separator;
1496 }
1497 return OS.str().str();
1498}
1499
1500Constant *OpenMPIRBuilder::getOrCreateOMPInternalVariable(
1501 llvm::Type *Ty, const llvm::Twine &Name, unsigned AddressSpace) {
1502 // TODO: Replace the twine arg with stringref to get rid of the conversion
1503 // logic. However This is taken from current implementation in clang as is.
1504 // Since this method is used in many places exclusively for OMP internal use
1505 // we will keep it as is for temporarily until we move all users to the
1506 // builder and then, if possible, fix it everywhere in one go.
1507 SmallString<256> Buffer;
1508 llvm::raw_svector_ostream Out(Buffer);
1509 Out << Name;
1510 StringRef RuntimeName = Out.str();
1511 auto &Elem = *InternalVars.try_emplace(RuntimeName, nullptr).first;
1512 if (Elem.second) {
1513 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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1514, __PRETTY_FUNCTION__))
1514 "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~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1514, __PRETTY_FUNCTION__))
;
1515 } else {
1516 // TODO: investigate the appropriate linkage type used for the global
1517 // variable for possibly changing that to internal or private, or maybe
1518 // create different versions of the function for different OMP internal
1519 // variables.
1520 Elem.second = new llvm::GlobalVariable(
1521 M, Ty, /*IsConstant*/ false, llvm::GlobalValue::CommonLinkage,
1522 llvm::Constant::getNullValue(Ty), Elem.first(),
1523 /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal,
1524 AddressSpace);
1525 }
1526
1527 return Elem.second;
1528}
1529
1530Value *OpenMPIRBuilder::getOMPCriticalRegionLock(StringRef CriticalName) {
1531 std::string Prefix = Twine("gomp_critical_user_", CriticalName).str();
1532 std::string Name = getNameWithSeparators({Prefix, "var"}, ".", ".");
1533 return getOrCreateOMPInternalVariable(KmpCriticalNameTy, Name);
1534}
1535
1536// Create all simple and struct types exposed by the runtime and remember
1537// the llvm::PointerTypes of them for easy access later.
1538void OpenMPIRBuilder::initializeTypes(Module &M) {
1539 LLVMContext &Ctx = M.getContext();
1540 StructType *T;
1541#define OMP_TYPE(VarName, InitValue) VarName = InitValue;
1542#define OMP_ARRAY_TYPE(VarName, ElemTy, ArraySize) \
1543 VarName##Ty = ArrayType::get(ElemTy, ArraySize); \
1544 VarName##PtrTy = PointerType::getUnqual(VarName##Ty);
1545#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...) \
1546 VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
1547 VarName##Ptr = PointerType::getUnqual(VarName);
1548#define OMP_STRUCT_TYPE(VarName, StructName, ...) \
1549 T = StructType::getTypeByName(Ctx, StructName); \
1550 if (!T) \
1551 T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \
1552 VarName = T; \
1553 VarName##Ptr = PointerType::getUnqual(T);
1554#include "llvm/Frontend/OpenMP/OMPKinds.def"
1555}
1556
1557void OpenMPIRBuilder::OutlineInfo::collectBlocks(
1558 SmallPtrSetImpl<BasicBlock *> &BlockSet,
1559 SmallVectorImpl<BasicBlock *> &BlockVector) {
1560 SmallVector<BasicBlock *, 32> Worklist;
1561 BlockSet.insert(EntryBB);
1562 BlockSet.insert(ExitBB);
1563
1564 Worklist.push_back(EntryBB);
1565 while (!Worklist.empty()) {
1566 BasicBlock *BB = Worklist.pop_back_val();
1567 BlockVector.push_back(BB);
1568 for (BasicBlock *SuccBB : successors(BB))
1569 if (BlockSet.insert(SuccBB).second)
1570 Worklist.push_back(SuccBB);
1571 }
1572}
1573
1574void CanonicalLoopInfo::assertOK() const {
1575#ifndef NDEBUG
1576 if (!IsValid)
1577 return;
1578
1579 // Verify standard control-flow we use for OpenMP loops.
1580 assert(Preheader)((Preheader) ? static_cast<void> (0) : __assert_fail ("Preheader"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1580, __PRETTY_FUNCTION__))
;
1581 assert(isa<BranchInst>(Preheader->getTerminator()) &&((isa<BranchInst>(Preheader->getTerminator()) &&
"Preheader must terminate with unconditional branch") ? static_cast
<void> (0) : __assert_fail ("isa<BranchInst>(Preheader->getTerminator()) && \"Preheader must terminate with unconditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1582, __PRETTY_FUNCTION__))
1582 "Preheader must terminate with unconditional branch")((isa<BranchInst>(Preheader->getTerminator()) &&
"Preheader must terminate with unconditional branch") ? static_cast
<void> (0) : __assert_fail ("isa<BranchInst>(Preheader->getTerminator()) && \"Preheader must terminate with unconditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1582, __PRETTY_FUNCTION__))
;
1583 assert(Preheader->getSingleSuccessor() == Header &&((Preheader->getSingleSuccessor() == Header && "Preheader must jump to header"
) ? static_cast<void> (0) : __assert_fail ("Preheader->getSingleSuccessor() == Header && \"Preheader must jump to header\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1584, __PRETTY_FUNCTION__))
1584 "Preheader must jump to header")((Preheader->getSingleSuccessor() == Header && "Preheader must jump to header"
) ? static_cast<void> (0) : __assert_fail ("Preheader->getSingleSuccessor() == Header && \"Preheader must jump to header\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1584, __PRETTY_FUNCTION__))
;
1585
1586 assert(Header)((Header) ? static_cast<void> (0) : __assert_fail ("Header"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1586, __PRETTY_FUNCTION__))
;
1587 assert(isa<BranchInst>(Header->getTerminator()) &&((isa<BranchInst>(Header->getTerminator()) &&
"Header must terminate with unconditional branch") ? static_cast
<void> (0) : __assert_fail ("isa<BranchInst>(Header->getTerminator()) && \"Header must terminate with unconditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1588, __PRETTY_FUNCTION__))
1588 "Header must terminate with unconditional branch")((isa<BranchInst>(Header->getTerminator()) &&
"Header must terminate with unconditional branch") ? static_cast
<void> (0) : __assert_fail ("isa<BranchInst>(Header->getTerminator()) && \"Header must terminate with unconditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1588, __PRETTY_FUNCTION__))
;
1589 assert(Header->getSingleSuccessor() == Cond &&((Header->getSingleSuccessor() == Cond && "Header must jump to exiting block"
) ? static_cast<void> (0) : __assert_fail ("Header->getSingleSuccessor() == Cond && \"Header must jump to exiting block\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1590, __PRETTY_FUNCTION__))
1590 "Header must jump to exiting block")((Header->getSingleSuccessor() == Cond && "Header must jump to exiting block"
) ? static_cast<void> (0) : __assert_fail ("Header->getSingleSuccessor() == Cond && \"Header must jump to exiting block\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1590, __PRETTY_FUNCTION__))
;
1591
1592 assert(Cond)((Cond) ? static_cast<void> (0) : __assert_fail ("Cond"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1592, __PRETTY_FUNCTION__))
;
1593 assert(Cond->getSinglePredecessor() == Header &&((Cond->getSinglePredecessor() == Header && "Exiting block only reachable from header"
) ? static_cast<void> (0) : __assert_fail ("Cond->getSinglePredecessor() == Header && \"Exiting block only reachable from header\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1594, __PRETTY_FUNCTION__))
1594 "Exiting block only reachable from header")((Cond->getSinglePredecessor() == Header && "Exiting block only reachable from header"
) ? static_cast<void> (0) : __assert_fail ("Cond->getSinglePredecessor() == Header && \"Exiting block only reachable from header\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1594, __PRETTY_FUNCTION__))
;
1595
1596 assert(isa<BranchInst>(Cond->getTerminator()) &&((isa<BranchInst>(Cond->getTerminator()) && "Exiting block must terminate with conditional branch"
) ? static_cast<void> (0) : __assert_fail ("isa<BranchInst>(Cond->getTerminator()) && \"Exiting block must terminate with conditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1597, __PRETTY_FUNCTION__))
1597 "Exiting block must terminate with conditional branch")((isa<BranchInst>(Cond->getTerminator()) && "Exiting block must terminate with conditional branch"
) ? static_cast<void> (0) : __assert_fail ("isa<BranchInst>(Cond->getTerminator()) && \"Exiting block must terminate with conditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1597, __PRETTY_FUNCTION__))
;
1598 assert(size(successors(Cond)) == 2 &&((size(successors(Cond)) == 2 && "Exiting block must have two successors"
) ? static_cast<void> (0) : __assert_fail ("size(successors(Cond)) == 2 && \"Exiting block must have two successors\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1599, __PRETTY_FUNCTION__))
1599 "Exiting block must have two successors")((size(successors(Cond)) == 2 && "Exiting block must have two successors"
) ? static_cast<void> (0) : __assert_fail ("size(successors(Cond)) == 2 && \"Exiting block must have two successors\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1599, __PRETTY_FUNCTION__))
;
1600 assert(cast<BranchInst>(Cond->getTerminator())->getSuccessor(0) == Body &&((cast<BranchInst>(Cond->getTerminator())->getSuccessor
(0) == Body && "Exiting block's first successor jump to the body"
) ? static_cast<void> (0) : __assert_fail ("cast<BranchInst>(Cond->getTerminator())->getSuccessor(0) == Body && \"Exiting block's first successor jump to the body\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1601, __PRETTY_FUNCTION__))
1601 "Exiting block's first successor jump to the body")((cast<BranchInst>(Cond->getTerminator())->getSuccessor
(0) == Body && "Exiting block's first successor jump to the body"
) ? static_cast<void> (0) : __assert_fail ("cast<BranchInst>(Cond->getTerminator())->getSuccessor(0) == Body && \"Exiting block's first successor jump to the body\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1601, __PRETTY_FUNCTION__))
;
1602 assert(cast<BranchInst>(Cond->getTerminator())->getSuccessor(1) == Exit &&((cast<BranchInst>(Cond->getTerminator())->getSuccessor
(1) == Exit && "Exiting block's second successor must exit the loop"
) ? static_cast<void> (0) : __assert_fail ("cast<BranchInst>(Cond->getTerminator())->getSuccessor(1) == Exit && \"Exiting block's second successor must exit the loop\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1603, __PRETTY_FUNCTION__))
1603 "Exiting block's second successor must exit the loop")((cast<BranchInst>(Cond->getTerminator())->getSuccessor
(1) == Exit && "Exiting block's second successor must exit the loop"
) ? static_cast<void> (0) : __assert_fail ("cast<BranchInst>(Cond->getTerminator())->getSuccessor(1) == Exit && \"Exiting block's second successor must exit the loop\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1603, __PRETTY_FUNCTION__))
;
1604
1605 assert(Body)((Body) ? static_cast<void> (0) : __assert_fail ("Body"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1605, __PRETTY_FUNCTION__))
;
1606 assert(Body->getSinglePredecessor() == Cond &&((Body->getSinglePredecessor() == Cond && "Body only reachable from exiting block"
) ? static_cast<void> (0) : __assert_fail ("Body->getSinglePredecessor() == Cond && \"Body only reachable from exiting block\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1607, __PRETTY_FUNCTION__))
1607 "Body only reachable from exiting block")((Body->getSinglePredecessor() == Cond && "Body only reachable from exiting block"
) ? static_cast<void> (0) : __assert_fail ("Body->getSinglePredecessor() == Cond && \"Body only reachable from exiting block\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1607, __PRETTY_FUNCTION__))
;
1608
1609 assert(Latch)((Latch) ? static_cast<void> (0) : __assert_fail ("Latch"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1609, __PRETTY_FUNCTION__))
;
1610 assert(isa<BranchInst>(Latch->getTerminator()) &&((isa<BranchInst>(Latch->getTerminator()) &&
"Latch must terminate with unconditional branch") ? static_cast
<void> (0) : __assert_fail ("isa<BranchInst>(Latch->getTerminator()) && \"Latch must terminate with unconditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1611, __PRETTY_FUNCTION__))
1611 "Latch must terminate with unconditional branch")((isa<BranchInst>(Latch->getTerminator()) &&
"Latch must terminate with unconditional branch") ? static_cast
<void> (0) : __assert_fail ("isa<BranchInst>(Latch->getTerminator()) && \"Latch must terminate with unconditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1611, __PRETTY_FUNCTION__))
;
1612 assert(Latch->getSingleSuccessor() == Header && "Latch must jump to header")((Latch->getSingleSuccessor() == Header && "Latch must jump to header"
) ? static_cast<void> (0) : __assert_fail ("Latch->getSingleSuccessor() == Header && \"Latch must jump to header\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1612, __PRETTY_FUNCTION__))
;
1613
1614 assert(Exit)((Exit) ? static_cast<void> (0) : __assert_fail ("Exit"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1614, __PRETTY_FUNCTION__))
;
1615 assert(isa<BranchInst>(Exit->getTerminator()) &&((isa<BranchInst>(Exit->getTerminator()) && "Exit block must terminate with unconditional branch"
) ? static_cast<void> (0) : __assert_fail ("isa<BranchInst>(Exit->getTerminator()) && \"Exit block must terminate with unconditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1616, __PRETTY_FUNCTION__))
1616 "Exit block must terminate with unconditional branch")((isa<BranchInst>(Exit->getTerminator()) && "Exit block must terminate with unconditional branch"
) ? static_cast<void> (0) : __assert_fail ("isa<BranchInst>(Exit->getTerminator()) && \"Exit block must terminate with unconditional branch\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1616, __PRETTY_FUNCTION__))
;
1617 assert(Exit->getSingleSuccessor() == After &&((Exit->getSingleSuccessor() == After && "Exit block must jump to after block"
) ? static_cast<void> (0) : __assert_fail ("Exit->getSingleSuccessor() == After && \"Exit block must jump to after block\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1618, __PRETTY_FUNCTION__))
1618 "Exit block must jump to after block")((Exit->getSingleSuccessor() == After && "Exit block must jump to after block"
) ? static_cast<void> (0) : __assert_fail ("Exit->getSingleSuccessor() == After && \"Exit block must jump to after block\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1618, __PRETTY_FUNCTION__))
;
1619
1620 assert(After)((After) ? static_cast<void> (0) : __assert_fail ("After"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1620, __PRETTY_FUNCTION__))
;
1621 assert(After->getSinglePredecessor() == Exit &&((After->getSinglePredecessor() == Exit && "After block only reachable from exit block"
) ? static_cast<void> (0) : __assert_fail ("After->getSinglePredecessor() == Exit && \"After block only reachable from exit block\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1622, __PRETTY_FUNCTION__))
1622 "After block only reachable from exit block")((After->getSinglePredecessor() == Exit && "After block only reachable from exit block"
) ? static_cast<void> (0) : __assert_fail ("After->getSinglePredecessor() == Exit && \"After block only reachable from exit block\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1622, __PRETTY_FUNCTION__))
;
1623
1624 Instruction *IndVar = getIndVar();
1625 assert(IndVar && "Canonical induction variable not found?")((IndVar && "Canonical induction variable not found?"
) ? static_cast<void> (0) : __assert_fail ("IndVar && \"Canonical induction variable not found?\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1625, __PRETTY_FUNCTION__))
;
1626 assert(isa<IntegerType>(IndVar->getType()) &&((isa<IntegerType>(IndVar->getType()) && "Induction variable must be an integer"
) ? static_cast<void> (0) : __assert_fail ("isa<IntegerType>(IndVar->getType()) && \"Induction variable must be an integer\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1627, __PRETTY_FUNCTION__))
1627 "Induction variable must be an integer")((isa<IntegerType>(IndVar->getType()) && "Induction variable must be an integer"
) ? static_cast<void> (0) : __assert_fail ("isa<IntegerType>(IndVar->getType()) && \"Induction variable must be an integer\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1627, __PRETTY_FUNCTION__))
;
1628 assert(cast<PHINode>(IndVar)->getParent() == Header &&((cast<PHINode>(IndVar)->getParent() == Header &&
"Induction variable must be a PHI in the loop header") ? static_cast
<void> (0) : __assert_fail ("cast<PHINode>(IndVar)->getParent() == Header && \"Induction variable must be a PHI in the loop header\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1629, __PRETTY_FUNCTION__))
1629 "Induction variable must be a PHI in the loop header")((cast<PHINode>(IndVar)->getParent() == Header &&
"Induction variable must be a PHI in the loop header") ? static_cast
<void> (0) : __assert_fail ("cast<PHINode>(IndVar)->getParent() == Header && \"Induction variable must be a PHI in the loop header\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1629, __PRETTY_FUNCTION__))
;
1630
1631 Value *TripCount = getTripCount();
1632 assert(TripCount && "Loop trip count not found?")((TripCount && "Loop trip count not found?") ? static_cast
<void> (0) : __assert_fail ("TripCount && \"Loop trip count not found?\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1632, __PRETTY_FUNCTION__))
;
1633 assert(IndVar->getType() == TripCount->getType() &&((IndVar->getType() == TripCount->getType() && "Trip count and induction variable must have the same type"
) ? static_cast<void> (0) : __assert_fail ("IndVar->getType() == TripCount->getType() && \"Trip count and induction variable must have the same type\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1634, __PRETTY_FUNCTION__))
1634 "Trip count and induction variable must have the same type")((IndVar->getType() == TripCount->getType() && "Trip count and induction variable must have the same type"
) ? static_cast<void> (0) : __assert_fail ("IndVar->getType() == TripCount->getType() && \"Trip count and induction variable must have the same type\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1634, __PRETTY_FUNCTION__))
;
1635
1636 auto *CmpI = cast<CmpInst>(&Cond->front());
1637 assert(CmpI->getPredicate() == CmpInst::ICMP_ULT &&((CmpI->getPredicate() == CmpInst::ICMP_ULT && "Exit condition must be a signed less-than comparison"
) ? static_cast<void> (0) : __assert_fail ("CmpI->getPredicate() == CmpInst::ICMP_ULT && \"Exit condition must be a signed less-than comparison\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1638, __PRETTY_FUNCTION__))
1638 "Exit condition must be a signed less-than comparison")((CmpI->getPredicate() == CmpInst::ICMP_ULT && "Exit condition must be a signed less-than comparison"
) ? static_cast<void> (0) : __assert_fail ("CmpI->getPredicate() == CmpInst::ICMP_ULT && \"Exit condition must be a signed less-than comparison\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1638, __PRETTY_FUNCTION__))
;
1639 assert(CmpI->getOperand(0) == IndVar &&((CmpI->getOperand(0) == IndVar && "Exit condition must compare the induction variable"
) ? static_cast<void> (0) : __assert_fail ("CmpI->getOperand(0) == IndVar && \"Exit condition must compare the induction variable\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1640, __PRETTY_FUNCTION__))
1640 "Exit condition must compare the induction variable")((CmpI->getOperand(0) == IndVar && "Exit condition must compare the induction variable"
) ? static_cast<void> (0) : __assert_fail ("CmpI->getOperand(0) == IndVar && \"Exit condition must compare the induction variable\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1640, __PRETTY_FUNCTION__))
;
1641 assert(CmpI->getOperand(1) == TripCount &&((CmpI->getOperand(1) == TripCount && "Exit condition must compare with the trip count"
) ? static_cast<void> (0) : __assert_fail ("CmpI->getOperand(1) == TripCount && \"Exit condition must compare with the trip count\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1642, __PRETTY_FUNCTION__))
1642 "Exit condition must compare with the trip count")((CmpI->getOperand(1) == TripCount && "Exit condition must compare with the trip count"
) ? static_cast<void> (0) : __assert_fail ("CmpI->getOperand(1) == TripCount && \"Exit condition must compare with the trip count\""
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp"
, 1642, __PRETTY_FUNCTION__))
;
1643#endif
1644}

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

/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/include/llvm/ADT/Optional.h

1//===- Optional.h - Simple variant for passing optional values --*- 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 provides Optional, a template class modeled in the spirit of
10// OCaml's 'opt' variant. The idea is to strongly type whether or not
11// a value can be optional.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_OPTIONAL_H
16#define LLVM_ADT_OPTIONAL_H
17
18#include "llvm/ADT/Hashing.h"
19#include "llvm/ADT/None.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/type_traits.h"
22#include <cassert>
23#include <memory>
24#include <new>
25#include <utility>
26
27namespace llvm {
28
29class raw_ostream;
30
31namespace optional_detail {
32
33struct in_place_t {};
34
35/// Storage for any type.
36template <typename T, bool = is_trivially_copyable<T>::value>
37class OptionalStorage {
38 union {
39 char empty;
40 T value;
41 };
42 bool hasVal;
43
44public:
45 ~OptionalStorage() { reset(); }
46
47 constexpr OptionalStorage() noexcept : empty(), hasVal(false) {}
48
49 constexpr OptionalStorage(OptionalStorage const &other) : OptionalStorage() {
50 if (other.hasValue()) {
51 emplace(other.value);
52 }
53 }
54 constexpr OptionalStorage(OptionalStorage &&other) : OptionalStorage() {
55 if (other.hasValue()) {
56 emplace(std::move(other.value));
57 }
58 }
59
60 template <class... Args>
61 constexpr explicit OptionalStorage(in_place_t, Args &&... args)
62 : value(std::forward<Args>(args)...), hasVal(true) {}
63
64 void reset() noexcept {
65 if (hasVal) {
66 value.~T();
67 hasVal = false;
68 }
69 }
70
71 constexpr bool hasValue() const noexcept { return hasVal; }
72
73 T &getValue() LLVM_LVALUE_FUNCTION& noexcept {
74 assert(hasVal)((hasVal) ? static_cast<void> (0) : __assert_fail ("hasVal"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/include/llvm/ADT/Optional.h"
, 74, __PRETTY_FUNCTION__))
;
75 return value;
76 }
77 constexpr T const &getValue() const LLVM_LVALUE_FUNCTION& noexcept {
78 assert(hasVal)((hasVal) ? static_cast<void> (0) : __assert_fail ("hasVal"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/include/llvm/ADT/Optional.h"
, 78, __PRETTY_FUNCTION__))
;
79 return value;
80 }
81#if LLVM_HAS_RVALUE_REFERENCE_THIS1
82 T &&getValue() && noexcept {
83 assert(hasVal)((hasVal) ? static_cast<void> (0) : __assert_fail ("hasVal"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/include/llvm/ADT/Optional.h"
, 83, __PRETTY_FUNCTION__))
;
84 return std::move(value);
85 }
86#endif
87
88 template <class... Args> void emplace(Args &&... args) {
89 reset();
90 ::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
91 hasVal = true;
92 }
93
94 OptionalStorage &operator=(T const &y) {
95 if (hasValue()) {
96 value = y;
97 } else {
98 ::new ((void *)std::addressof(value)) T(y);
99 hasVal = true;
100 }
101 return *this;
102 }
103 OptionalStorage &operator=(T &&y) {
104 if (hasValue()) {
105 value = std::move(y);
106 } else {
107 ::new ((void *)std::addressof(value)) T(std::move(y));
108 hasVal = true;
109 }
110 return *this;
111 }
112
113 OptionalStorage &operator=(OptionalStorage const &other) {
114 if (other.hasValue()) {
115 if (hasValue()) {
116 value = other.value;
117 } else {
118 ::new ((void *)std::addressof(value)) T(other.value);
119 hasVal = true;
120 }
121 } else {
122 reset();
123 }
124 return *this;
125 }
126
127 OptionalStorage &operator=(OptionalStorage &&other) {
128 if (other.hasValue()) {
129 if (hasValue()) {
130 value = std::move(other.value);
131 } else {
132 ::new ((void *)std::addressof(value)) T(std::move(other.value));
133 hasVal = true;
134 }
135 } else {
136 reset();
137 }
138 return *this;
139 }
140};
141
142template <typename T> class OptionalStorage<T, true> {
143 union {
144 char empty;
145 T value;
146 };
147 bool hasVal = false;
148
149public:
150 ~OptionalStorage() = default;
151
152 constexpr OptionalStorage() noexcept : empty{} {}
153
154 constexpr OptionalStorage(OptionalStorage const &other) = default;
155 constexpr OptionalStorage(OptionalStorage &&other) = default;
156
157 OptionalStorage &operator=(OptionalStorage const &other) = default;
158 OptionalStorage &operator=(OptionalStorage &&other) = default;
159
160 template <class... Args>
161 constexpr explicit OptionalStorage(in_place_t, Args &&... args)
162 : value(std::forward<Args>(args)...), hasVal(true) {}
163
164 void reset() noexcept {
165 if (hasVal) {
166 value.~T();
167 hasVal = false;
168 }
169 }
170
171 constexpr bool hasValue() const noexcept { return hasVal; }
43
Returning zero, which participates in a condition later
172
173 T &getValue() LLVM_LVALUE_FUNCTION& noexcept {
174 assert(hasVal)((hasVal) ? static_cast<void> (0) : __assert_fail ("hasVal"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/include/llvm/ADT/Optional.h"
, 174, __PRETTY_FUNCTION__))
;
175 return value;
176 }
177 constexpr T const &getValue() const LLVM_LVALUE_FUNCTION& noexcept {
178 assert(hasVal)((hasVal) ? static_cast<void> (0) : __assert_fail ("hasVal"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/include/llvm/ADT/Optional.h"
, 178, __PRETTY_FUNCTION__))
;
179 return value;
180 }
181#if LLVM_HAS_RVALUE_REFERENCE_THIS1
182 T &&getValue() && noexcept {
183 assert(hasVal)((hasVal) ? static_cast<void> (0) : __assert_fail ("hasVal"
, "/build/llvm-toolchain-snapshot-12~++20210115100614+a14c36fe27f5/llvm/include/llvm/ADT/Optional.h"
, 183, __PRETTY_FUNCTION__))
;
184 return std::move(value);
185 }
186#endif
187
188 template <class... Args> void emplace(Args &&... args) {
189 reset();
190 ::new ((void *)std::addressof(value)) T(std::forward<Args>(args)...);
191 hasVal = true;
192 }
193
194 OptionalStorage &operator=(T const &y) {
195 if (hasValue()) {
196 value = y;
197 } else {
198 ::new ((void *)std::addressof(value)) T(y);
199 hasVal = true;
200 }
201 return *this;
202 }
203 OptionalStorage &operator=(T &&y) {
204 if (hasValue()) {
205 value = std::move(y);
206 } else {
207 ::new ((void *)std::addressof(value)) T(std::move(y));
208 hasVal = true;
209 }
210 return *this;
211 }
212};
213
214} // namespace optional_detail
215
216template <typename T> class Optional {
217 optional_detail::OptionalStorage<T> Storage;
218
219public:
220 using value_type = T;
221
222 constexpr Optional() {}
223 constexpr Optional(NoneType) {}
224
225 constexpr Optional(const T &y) : Storage(optional_detail::in_place_t{}, y) {}
226 constexpr Optional(const Optional &O) = default;
227
228 constexpr Optional(T &&y)
229 : Storage(optional_detail::in_place_t{}, std::move(y)) {}
230 constexpr Optional(Optional &&O) = default;
231
232 Optional &operator=(T &&y) {
233 Storage = std::move(y);
234 return *this;
235 }
236 Optional &operator=(Optional &&O) = default;
237
238 /// Create a new object by constructing it in place with the given arguments.
239 template <typename... ArgTypes> void emplace(ArgTypes &&... Args) {
240 Storage.emplace(std::forward<ArgTypes>(Args)...);
241 }
242
243 static constexpr Optional create(const T *y) {
244 return y ? Optional(*y) : Optional();
245 }
246
247 Optional &operator=(const T &y) {
248 Storage = y;
249 return *this;
250 }
251 Optional &operator=(const Optional &O) = default;
252
253 void reset() { Storage.reset(); }
254
255 constexpr const T *getPointer() const { return &Storage.getValue(); }
256 T *getPointer() { return &Storage.getValue(); }
257 constexpr const T &getValue() const LLVM_LVALUE_FUNCTION& {
258 return Storage.getValue();
259 }
260 T &getValue() LLVM_LVALUE_FUNCTION& { return Storage.getValue(); }
261
262 constexpr explicit operator bool() const { return hasValue(); }
41
Calling 'Optional::hasValue'
46
Returning from 'Optional::hasValue'
47
Returning zero, which participates in a condition later
263 constexpr bool hasValue() const { return Storage.hasValue(); }
42
Calling 'OptionalStorage::hasValue'
44
Returning from 'OptionalStorage::hasValue'
45
Returning zero, which participates in a condition later
264 constexpr const T *operator->() const { return getPointer(); }
265 T *operator->() { return getPointer(); }
266 constexpr const T &operator*() const LLVM_LVALUE_FUNCTION& {
267 return getValue();
268 }
269 T &operator*() LLVM_LVALUE_FUNCTION& { return getValue(); }
270
271 template <typename U>
272 constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION& {
273 return hasValue() ? getValue() : std::forward<U>(value);
274 }
275
276 /// Apply a function to the value if present; otherwise return None.
277 template <class Function>
278 auto map(const Function &F) const LLVM_LVALUE_FUNCTION&
279 -> Optional<decltype(F(getValue()))> {
280 if (*this) return F(getValue());
281 return None;
282 }
283
284#if LLVM_HAS_RVALUE_REFERENCE_THIS1
285 T &&getValue() && { return std::move(Storage.getValue()); }
286 T &&operator*() && { return std::move(Storage.getValue()); }
287
288 template <typename U>
289 T getValueOr(U &&value) && {
290 return hasValue() ? std::move(getValue()) : std::forward<U>(value);
291 }
292
293 /// Apply a function to the value if present; otherwise return None.
294 template <class Function>
295 auto map(const Function &F) &&
296 -> Optional<decltype(F(std::move(*this).getValue()))> {
297 if (*this) return F(std::move(*this).getValue());
298 return None;
299 }
300#endif
301};
302
303template <class T> llvm::hash_code hash_value(const Optional<T> &O) {
304 return O ? hash_combine(true, *O) : hash_value(false);
305}
306
307template <typename T, typename U>
308constexpr bool operator==(const Optional<T> &X, const Optional<U> &Y) {
309 if (X && Y)
310 return *X == *Y;
311 return X.hasValue() == Y.hasValue();
312}
313
314template <typename T, typename U>
315constexpr bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
316 return !(X == Y);
317}
318
319template <typename T, typename U>
320constexpr bool operator<(const Optional<T> &X, const Optional<U> &Y) {
321 if (X && Y)
322 return *X < *Y;
323 return X.hasValue() < Y.hasValue();
324}
325
326template <typename T, typename U>
327constexpr bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
328 return !(Y < X);
329}
330
331template <typename T, typename U>
332constexpr bool operator>(const Optional<T> &X, const Optional<U> &Y) {
333 return Y < X;
334}
335
336template <typename T, typename U>
337constexpr bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
338 return !(X < Y);
339}
340
341template <typename T>
342constexpr bool operator==(const Optional<T> &X, NoneType) {
343 return !X;
344}
345
346template <typename T>
347constexpr bool operator==(NoneType, const Optional<T> &X) {
348 return X == None;
349}
350
351template <typename T>
352constexpr bool operator!=(const Optional<T> &X, NoneType) {
353 return !(X == None);
354}
355
356template <typename T>
357constexpr bool operator!=(NoneType, const Optional<T> &X) {
358 return X != None;
359}
360
361template <typename T> constexpr bool operator<(const Optional<T> &X, NoneType) {
362 return false;
363}
364
365template <typename T> constexpr bool operator<(NoneType, const Optional<T> &X) {
366 return X.hasValue();
367}
368
369template <typename T>
370constexpr bool operator<=(const Optional<T> &X, NoneType) {
371 return !(None < X);
372}
373
374template <typename T>
375constexpr bool operator<=(NoneType, const Optional<T> &X) {
376 return !(X < None);
377}
378
379template <typename T> constexpr bool operator>(const Optional<T> &X, NoneType) {
380 return None < X;
381}
382
383template <typename T> constexpr bool operator>(NoneType, const Optional<T> &X) {
384 return X < None;
385}
386
387template <typename T>
388constexpr bool operator>=(const Optional<T> &X, NoneType) {
389 return None <= X;
390}
391
392template <typename T>
393constexpr bool operator>=(NoneType, const Optional<T> &X) {
394 return X <= None;
395}
396
397template <typename T>
398constexpr bool operator==(const Optional<T> &X, const T &Y) {
399 return X && *X == Y;
400}
401
402template <typename T>
403constexpr bool operator==(const T &X, const Optional<T> &Y) {
404 return Y && X == *Y;
405}
406
407template <typename T>
408constexpr bool operator!=(const Optional<T> &X, const T &Y) {
409 return !(X == Y);
410}
411
412template <typename T>
413constexpr bool operator!=(const T &X, const Optional<T> &Y) {
414 return !(X == Y);
415}
416
417template <typename T>
418constexpr bool operator<(const Optional<T> &X, const T &Y) {
419 return !X || *X < Y;
420}
421
422template <typename T>
423constexpr bool operator<(const T &X, const Optional<T> &Y) {
424 return Y && X < *Y;
425}
426
427template <typename T>
428constexpr bool operator<=(const Optional<T> &X, const T &Y) {
429 return !(Y < X);
430}
431
432template <typename T>
433constexpr bool operator<=(const T &X, const Optional<T> &Y) {
434 return !(Y < X);
435}
436
437template <typename T>
438constexpr bool operator>(const Optional<T> &X, const T &Y) {
439 return Y < X;
440}
441
442template <typename T>
443constexpr bool operator>(const T &X, const Optional<T> &Y) {
444 return Y < X;
445}
446
447template <typename T>
448constexpr bool operator>=(const Optional<T> &X, const T &Y) {
449 return !(X < Y);
450}
451
452template <typename T>
453constexpr bool operator>=(const T &X, const Optional<T> &Y) {
454 return !(X < Y);
455}
456
457raw_ostream &operator<<(raw_ostream &OS, NoneType);
458
459template <typename T, typename = decltype(std::declval<raw_ostream &>()
460 << std::declval<const T &>())>
461raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
462 if (O)
463 OS << *O;
464 else
465 OS << None;
466 return OS;
467}
468
469} // end namespace llvm
470
471#endif // LLVM_ADT_OPTIONAL_H