File: | llvm/include/llvm/IR/Instructions.h |
Warning: | line 940, column 7 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- DataFlowSanitizer.cpp - dynamic data flow analysis -----------------===// | ||||||
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 | /// \file | ||||||
10 | /// This file is a part of DataFlowSanitizer, a generalised dynamic data flow | ||||||
11 | /// analysis. | ||||||
12 | /// | ||||||
13 | /// Unlike other Sanitizer tools, this tool is not designed to detect a specific | ||||||
14 | /// class of bugs on its own. Instead, it provides a generic dynamic data flow | ||||||
15 | /// analysis framework to be used by clients to help detect application-specific | ||||||
16 | /// issues within their own code. | ||||||
17 | /// | ||||||
18 | /// The analysis is based on automatic propagation of data flow labels (also | ||||||
19 | /// known as taint labels) through a program as it performs computation. Each | ||||||
20 | /// byte of application memory is backed by two bytes of shadow memory which | ||||||
21 | /// hold the label. On Linux/x86_64, memory is laid out as follows: | ||||||
22 | /// | ||||||
23 | /// +--------------------+ 0x800000000000 (top of memory) | ||||||
24 | /// | application memory | | ||||||
25 | /// +--------------------+ 0x700000008000 (kAppAddr) | ||||||
26 | /// | | | ||||||
27 | /// | unused | | ||||||
28 | /// | | | ||||||
29 | /// +--------------------+ 0x200200000000 (kUnusedAddr) | ||||||
30 | /// | union table | | ||||||
31 | /// +--------------------+ 0x200000000000 (kUnionTableAddr) | ||||||
32 | /// | shadow memory | | ||||||
33 | /// +--------------------+ 0x000000010000 (kShadowAddr) | ||||||
34 | /// | reserved by kernel | | ||||||
35 | /// +--------------------+ 0x000000000000 | ||||||
36 | /// | ||||||
37 | /// To derive a shadow memory address from an application memory address, | ||||||
38 | /// bits 44-46 are cleared to bring the address into the range | ||||||
39 | /// [0x000000008000,0x100000000000). Then the address is shifted left by 1 to | ||||||
40 | /// account for the double byte representation of shadow labels and move the | ||||||
41 | /// address into the shadow memory range. See the function | ||||||
42 | /// DataFlowSanitizer::getShadowAddress below. | ||||||
43 | /// | ||||||
44 | /// For more information, please refer to the design document: | ||||||
45 | /// http://clang.llvm.org/docs/DataFlowSanitizerDesign.html | ||||||
46 | // | ||||||
47 | //===----------------------------------------------------------------------===// | ||||||
48 | |||||||
49 | #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h" | ||||||
50 | #include "llvm/ADT/DenseMap.h" | ||||||
51 | #include "llvm/ADT/DenseSet.h" | ||||||
52 | #include "llvm/ADT/DepthFirstIterator.h" | ||||||
53 | #include "llvm/ADT/None.h" | ||||||
54 | #include "llvm/ADT/SmallPtrSet.h" | ||||||
55 | #include "llvm/ADT/SmallVector.h" | ||||||
56 | #include "llvm/ADT/StringExtras.h" | ||||||
57 | #include "llvm/ADT/StringRef.h" | ||||||
58 | #include "llvm/ADT/Triple.h" | ||||||
59 | #include "llvm/Analysis/ValueTracking.h" | ||||||
60 | #include "llvm/IR/Argument.h" | ||||||
61 | #include "llvm/IR/Attributes.h" | ||||||
62 | #include "llvm/IR/BasicBlock.h" | ||||||
63 | #include "llvm/IR/Constant.h" | ||||||
64 | #include "llvm/IR/Constants.h" | ||||||
65 | #include "llvm/IR/DataLayout.h" | ||||||
66 | #include "llvm/IR/DerivedTypes.h" | ||||||
67 | #include "llvm/IR/Dominators.h" | ||||||
68 | #include "llvm/IR/Function.h" | ||||||
69 | #include "llvm/IR/GlobalAlias.h" | ||||||
70 | #include "llvm/IR/GlobalValue.h" | ||||||
71 | #include "llvm/IR/GlobalVariable.h" | ||||||
72 | #include "llvm/IR/IRBuilder.h" | ||||||
73 | #include "llvm/IR/InlineAsm.h" | ||||||
74 | #include "llvm/IR/InstVisitor.h" | ||||||
75 | #include "llvm/IR/InstrTypes.h" | ||||||
76 | #include "llvm/IR/Instruction.h" | ||||||
77 | #include "llvm/IR/Instructions.h" | ||||||
78 | #include "llvm/IR/IntrinsicInst.h" | ||||||
79 | #include "llvm/IR/LLVMContext.h" | ||||||
80 | #include "llvm/IR/MDBuilder.h" | ||||||
81 | #include "llvm/IR/Module.h" | ||||||
82 | #include "llvm/IR/PassManager.h" | ||||||
83 | #include "llvm/IR/Type.h" | ||||||
84 | #include "llvm/IR/User.h" | ||||||
85 | #include "llvm/IR/Value.h" | ||||||
86 | #include "llvm/InitializePasses.h" | ||||||
87 | #include "llvm/Pass.h" | ||||||
88 | #include "llvm/Support/Casting.h" | ||||||
89 | #include "llvm/Support/CommandLine.h" | ||||||
90 | #include "llvm/Support/ErrorHandling.h" | ||||||
91 | #include "llvm/Support/SpecialCaseList.h" | ||||||
92 | #include "llvm/Support/VirtualFileSystem.h" | ||||||
93 | #include "llvm/Transforms/Instrumentation.h" | ||||||
94 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | ||||||
95 | #include "llvm/Transforms/Utils/Local.h" | ||||||
96 | #include <algorithm> | ||||||
97 | #include <cassert> | ||||||
98 | #include <cstddef> | ||||||
99 | #include <cstdint> | ||||||
100 | #include <iterator> | ||||||
101 | #include <memory> | ||||||
102 | #include <set> | ||||||
103 | #include <string> | ||||||
104 | #include <utility> | ||||||
105 | #include <vector> | ||||||
106 | |||||||
107 | using namespace llvm; | ||||||
108 | |||||||
109 | // External symbol to be used when generating the shadow address for | ||||||
110 | // architectures with multiple VMAs. Instead of using a constant integer | ||||||
111 | // the runtime will set the external mask based on the VMA range. | ||||||
112 | const char kDFSanExternShadowPtrMask[] = "__dfsan_shadow_ptr_mask"; | ||||||
113 | |||||||
114 | // The -dfsan-preserve-alignment flag controls whether this pass assumes that | ||||||
115 | // alignment requirements provided by the input IR are correct. For example, | ||||||
116 | // if the input IR contains a load with alignment 8, this flag will cause | ||||||
117 | // the shadow load to have alignment 16. This flag is disabled by default as | ||||||
118 | // we have unfortunately encountered too much code (including Clang itself; | ||||||
119 | // see PR14291) which performs misaligned access. | ||||||
120 | static cl::opt<bool> ClPreserveAlignment( | ||||||
121 | "dfsan-preserve-alignment", | ||||||
122 | cl::desc("respect alignment requirements provided by input IR"), cl::Hidden, | ||||||
123 | cl::init(false)); | ||||||
124 | |||||||
125 | // The ABI list files control how shadow parameters are passed. The pass treats | ||||||
126 | // every function labelled "uninstrumented" in the ABI list file as conforming | ||||||
127 | // to the "native" (i.e. unsanitized) ABI. Unless the ABI list contains | ||||||
128 | // additional annotations for those functions, a call to one of those functions | ||||||
129 | // will produce a warning message, as the labelling behaviour of the function is | ||||||
130 | // unknown. The other supported annotations are "functional" and "discard", | ||||||
131 | // which are described below under DataFlowSanitizer::WrapperKind. | ||||||
132 | static cl::list<std::string> ClABIListFiles( | ||||||
133 | "dfsan-abilist", | ||||||
134 | cl::desc("File listing native ABI functions and how the pass treats them"), | ||||||
135 | cl::Hidden); | ||||||
136 | |||||||
137 | // Controls whether the pass uses IA_Args or IA_TLS as the ABI for instrumented | ||||||
138 | // functions (see DataFlowSanitizer::InstrumentedABI below). | ||||||
139 | static cl::opt<bool> ClArgsABI( | ||||||
140 | "dfsan-args-abi", | ||||||
141 | cl::desc("Use the argument ABI rather than the TLS ABI"), | ||||||
142 | cl::Hidden); | ||||||
143 | |||||||
144 | // Controls whether the pass includes or ignores the labels of pointers in load | ||||||
145 | // instructions. | ||||||
146 | static cl::opt<bool> ClCombinePointerLabelsOnLoad( | ||||||
147 | "dfsan-combine-pointer-labels-on-load", | ||||||
148 | cl::desc("Combine the label of the pointer with the label of the data when " | ||||||
149 | "loading from memory."), | ||||||
150 | cl::Hidden, cl::init(true)); | ||||||
151 | |||||||
152 | // Controls whether the pass includes or ignores the labels of pointers in | ||||||
153 | // stores instructions. | ||||||
154 | static cl::opt<bool> ClCombinePointerLabelsOnStore( | ||||||
155 | "dfsan-combine-pointer-labels-on-store", | ||||||
156 | cl::desc("Combine the label of the pointer with the label of the data when " | ||||||
157 | "storing in memory."), | ||||||
158 | cl::Hidden, cl::init(false)); | ||||||
159 | |||||||
160 | static cl::opt<bool> ClDebugNonzeroLabels( | ||||||
161 | "dfsan-debug-nonzero-labels", | ||||||
162 | cl::desc("Insert calls to __dfsan_nonzero_label on observing a parameter, " | ||||||
163 | "load or return with a nonzero label"), | ||||||
164 | cl::Hidden); | ||||||
165 | |||||||
166 | // Experimental feature that inserts callbacks for certain data events. | ||||||
167 | // Currently callbacks are only inserted for loads, stores, memory transfers | ||||||
168 | // (i.e. memcpy and memmove), and comparisons. | ||||||
169 | // | ||||||
170 | // If this flag is set to true, the user must provide definitions for the | ||||||
171 | // following callback functions: | ||||||
172 | // void __dfsan_load_callback(dfsan_label Label, void* addr); | ||||||
173 | // void __dfsan_store_callback(dfsan_label Label, void* addr); | ||||||
174 | // void __dfsan_mem_transfer_callback(dfsan_label *Start, size_t Len); | ||||||
175 | // void __dfsan_cmp_callback(dfsan_label CombinedLabel); | ||||||
176 | static cl::opt<bool> ClEventCallbacks( | ||||||
177 | "dfsan-event-callbacks", | ||||||
178 | cl::desc("Insert calls to __dfsan_*_callback functions on data events."), | ||||||
179 | cl::Hidden, cl::init(false)); | ||||||
180 | |||||||
181 | // Use a distinct bit for each base label, enabling faster unions with less | ||||||
182 | // instrumentation. Limits the max number of base labels to 16. | ||||||
183 | static cl::opt<bool> ClFast16Labels( | ||||||
184 | "dfsan-fast-16-labels", | ||||||
185 | cl::desc("Use more efficient instrumentation, limiting the number of " | ||||||
186 | "labels to 16."), | ||||||
187 | cl::Hidden, cl::init(false)); | ||||||
188 | |||||||
189 | // Controls whether the pass tracks the control flow of select instructions. | ||||||
190 | static cl::opt<bool> ClTrackSelectControlFlow( | ||||||
191 | "dfsan-track-select-control-flow", | ||||||
192 | cl::desc("Propagate labels from condition values of select instructions " | ||||||
193 | "to results."), | ||||||
194 | cl::Hidden, cl::init(true)); | ||||||
195 | |||||||
196 | static StringRef GetGlobalTypeString(const GlobalValue &G) { | ||||||
197 | // Types of GlobalVariables are always pointer types. | ||||||
198 | Type *GType = G.getValueType(); | ||||||
199 | // For now we support excluding struct types only. | ||||||
200 | if (StructType *SGType = dyn_cast<StructType>(GType)) { | ||||||
201 | if (!SGType->isLiteral()) | ||||||
202 | return SGType->getName(); | ||||||
203 | } | ||||||
204 | return "<unknown type>"; | ||||||
205 | } | ||||||
206 | |||||||
207 | namespace { | ||||||
208 | |||||||
209 | class DFSanABIList { | ||||||
210 | std::unique_ptr<SpecialCaseList> SCL; | ||||||
211 | |||||||
212 | public: | ||||||
213 | DFSanABIList() = default; | ||||||
214 | |||||||
215 | void set(std::unique_ptr<SpecialCaseList> List) { SCL = std::move(List); } | ||||||
216 | |||||||
217 | /// Returns whether either this function or its source file are listed in the | ||||||
218 | /// given category. | ||||||
219 | bool isIn(const Function &F, StringRef Category) const { | ||||||
220 | return isIn(*F.getParent(), Category) || | ||||||
221 | SCL->inSection("dataflow", "fun", F.getName(), Category); | ||||||
222 | } | ||||||
223 | |||||||
224 | /// Returns whether this global alias is listed in the given category. | ||||||
225 | /// | ||||||
226 | /// If GA aliases a function, the alias's name is matched as a function name | ||||||
227 | /// would be. Similarly, aliases of globals are matched like globals. | ||||||
228 | bool isIn(const GlobalAlias &GA, StringRef Category) const { | ||||||
229 | if (isIn(*GA.getParent(), Category)) | ||||||
230 | return true; | ||||||
231 | |||||||
232 | if (isa<FunctionType>(GA.getValueType())) | ||||||
233 | return SCL->inSection("dataflow", "fun", GA.getName(), Category); | ||||||
234 | |||||||
235 | return SCL->inSection("dataflow", "global", GA.getName(), Category) || | ||||||
236 | SCL->inSection("dataflow", "type", GetGlobalTypeString(GA), | ||||||
237 | Category); | ||||||
238 | } | ||||||
239 | |||||||
240 | /// Returns whether this module is listed in the given category. | ||||||
241 | bool isIn(const Module &M, StringRef Category) const { | ||||||
242 | return SCL->inSection("dataflow", "src", M.getModuleIdentifier(), Category); | ||||||
243 | } | ||||||
244 | }; | ||||||
245 | |||||||
246 | /// TransformedFunction is used to express the result of transforming one | ||||||
247 | /// function type into another. This struct is immutable. It holds metadata | ||||||
248 | /// useful for updating calls of the old function to the new type. | ||||||
249 | struct TransformedFunction { | ||||||
250 | TransformedFunction(FunctionType* OriginalType, | ||||||
251 | FunctionType* TransformedType, | ||||||
252 | std::vector<unsigned> ArgumentIndexMapping) | ||||||
253 | : OriginalType(OriginalType), | ||||||
254 | TransformedType(TransformedType), | ||||||
255 | ArgumentIndexMapping(ArgumentIndexMapping) {} | ||||||
256 | |||||||
257 | // Disallow copies. | ||||||
258 | TransformedFunction(const TransformedFunction&) = delete; | ||||||
259 | TransformedFunction& operator=(const TransformedFunction&) = delete; | ||||||
260 | |||||||
261 | // Allow moves. | ||||||
262 | TransformedFunction(TransformedFunction&&) = default; | ||||||
263 | TransformedFunction& operator=(TransformedFunction&&) = default; | ||||||
264 | |||||||
265 | /// Type of the function before the transformation. | ||||||
266 | FunctionType *OriginalType; | ||||||
267 | |||||||
268 | /// Type of the function after the transformation. | ||||||
269 | FunctionType *TransformedType; | ||||||
270 | |||||||
271 | /// Transforming a function may change the position of arguments. This | ||||||
272 | /// member records the mapping from each argument's old position to its new | ||||||
273 | /// position. Argument positions are zero-indexed. If the transformation | ||||||
274 | /// from F to F' made the first argument of F into the third argument of F', | ||||||
275 | /// then ArgumentIndexMapping[0] will equal 2. | ||||||
276 | std::vector<unsigned> ArgumentIndexMapping; | ||||||
277 | }; | ||||||
278 | |||||||
279 | /// Given function attributes from a call site for the original function, | ||||||
280 | /// return function attributes appropriate for a call to the transformed | ||||||
281 | /// function. | ||||||
282 | AttributeList TransformFunctionAttributes( | ||||||
283 | const TransformedFunction& TransformedFunction, | ||||||
284 | LLVMContext& Ctx, AttributeList CallSiteAttrs) { | ||||||
285 | |||||||
286 | // Construct a vector of AttributeSet for each function argument. | ||||||
287 | std::vector<llvm::AttributeSet> ArgumentAttributes( | ||||||
288 | TransformedFunction.TransformedType->getNumParams()); | ||||||
289 | |||||||
290 | // Copy attributes from the parameter of the original function to the | ||||||
291 | // transformed version. 'ArgumentIndexMapping' holds the mapping from | ||||||
292 | // old argument position to new. | ||||||
293 | for (unsigned i=0, ie = TransformedFunction.ArgumentIndexMapping.size(); | ||||||
294 | i < ie; ++i) { | ||||||
295 | unsigned TransformedIndex = TransformedFunction.ArgumentIndexMapping[i]; | ||||||
296 | ArgumentAttributes[TransformedIndex] = CallSiteAttrs.getParamAttributes(i); | ||||||
297 | } | ||||||
298 | |||||||
299 | // Copy annotations on varargs arguments. | ||||||
300 | for (unsigned i = TransformedFunction.OriginalType->getNumParams(), | ||||||
301 | ie = CallSiteAttrs.getNumAttrSets(); i<ie; ++i) { | ||||||
302 | ArgumentAttributes.push_back(CallSiteAttrs.getParamAttributes(i)); | ||||||
303 | } | ||||||
304 | |||||||
305 | return AttributeList::get( | ||||||
306 | Ctx, | ||||||
307 | CallSiteAttrs.getFnAttributes(), | ||||||
308 | CallSiteAttrs.getRetAttributes(), | ||||||
309 | llvm::makeArrayRef(ArgumentAttributes)); | ||||||
310 | } | ||||||
311 | |||||||
312 | class DataFlowSanitizer { | ||||||
313 | friend struct DFSanFunction; | ||||||
314 | friend class DFSanVisitor; | ||||||
315 | |||||||
316 | enum { ShadowWidthBits = 16, ShadowWidthBytes = ShadowWidthBits / 8 }; | ||||||
317 | |||||||
318 | /// Which ABI should be used for instrumented functions? | ||||||
319 | enum InstrumentedABI { | ||||||
320 | /// Argument and return value labels are passed through additional | ||||||
321 | /// arguments and by modifying the return type. | ||||||
322 | IA_Args, | ||||||
323 | |||||||
324 | /// Argument and return value labels are passed through TLS variables | ||||||
325 | /// __dfsan_arg_tls and __dfsan_retval_tls. | ||||||
326 | IA_TLS | ||||||
327 | }; | ||||||
328 | |||||||
329 | /// How should calls to uninstrumented functions be handled? | ||||||
330 | enum WrapperKind { | ||||||
331 | /// This function is present in an uninstrumented form but we don't know | ||||||
332 | /// how it should be handled. Print a warning and call the function anyway. | ||||||
333 | /// Don't label the return value. | ||||||
334 | WK_Warning, | ||||||
335 | |||||||
336 | /// This function does not write to (user-accessible) memory, and its return | ||||||
337 | /// value is unlabelled. | ||||||
338 | WK_Discard, | ||||||
339 | |||||||
340 | /// This function does not write to (user-accessible) memory, and the label | ||||||
341 | /// of its return value is the union of the label of its arguments. | ||||||
342 | WK_Functional, | ||||||
343 | |||||||
344 | /// Instead of calling the function, a custom wrapper __dfsw_F is called, | ||||||
345 | /// where F is the name of the function. This function may wrap the | ||||||
346 | /// original function or provide its own implementation. This is similar to | ||||||
347 | /// the IA_Args ABI, except that IA_Args uses a struct return type to | ||||||
348 | /// pass the return value shadow in a register, while WK_Custom uses an | ||||||
349 | /// extra pointer argument to return the shadow. This allows the wrapped | ||||||
350 | /// form of the function type to be expressed in C. | ||||||
351 | WK_Custom | ||||||
352 | }; | ||||||
353 | |||||||
354 | Module *Mod; | ||||||
355 | LLVMContext *Ctx; | ||||||
356 | Type *Int8Ptr; | ||||||
357 | IntegerType *ShadowTy; | ||||||
358 | PointerType *ShadowPtrTy; | ||||||
359 | IntegerType *IntptrTy; | ||||||
360 | ConstantInt *ZeroShadow; | ||||||
361 | ConstantInt *ShadowPtrMask; | ||||||
362 | ConstantInt *ShadowPtrMul; | ||||||
363 | Constant *ArgTLS; | ||||||
364 | Constant *RetvalTLS; | ||||||
365 | Constant *ExternalShadowMask; | ||||||
366 | FunctionType *DFSanUnionFnTy; | ||||||
367 | FunctionType *DFSanUnionLoadFnTy; | ||||||
368 | FunctionType *DFSanUnimplementedFnTy; | ||||||
369 | FunctionType *DFSanSetLabelFnTy; | ||||||
370 | FunctionType *DFSanNonzeroLabelFnTy; | ||||||
371 | FunctionType *DFSanVarargWrapperFnTy; | ||||||
372 | FunctionType *DFSanCmpCallbackFnTy; | ||||||
373 | FunctionType *DFSanLoadStoreCallbackFnTy; | ||||||
374 | FunctionType *DFSanMemTransferCallbackFnTy; | ||||||
375 | FunctionCallee DFSanUnionFn; | ||||||
376 | FunctionCallee DFSanCheckedUnionFn; | ||||||
377 | FunctionCallee DFSanUnionLoadFn; | ||||||
378 | FunctionCallee DFSanUnionLoadFast16LabelsFn; | ||||||
379 | FunctionCallee DFSanUnimplementedFn; | ||||||
380 | FunctionCallee DFSanSetLabelFn; | ||||||
381 | FunctionCallee DFSanNonzeroLabelFn; | ||||||
382 | FunctionCallee DFSanVarargWrapperFn; | ||||||
383 | FunctionCallee DFSanLoadCallbackFn; | ||||||
384 | FunctionCallee DFSanStoreCallbackFn; | ||||||
385 | FunctionCallee DFSanMemTransferCallbackFn; | ||||||
386 | FunctionCallee DFSanCmpCallbackFn; | ||||||
387 | MDNode *ColdCallWeights; | ||||||
388 | DFSanABIList ABIList; | ||||||
389 | DenseMap<Value *, Function *> UnwrappedFnMap; | ||||||
390 | AttrBuilder ReadOnlyNoneAttrs; | ||||||
391 | bool DFSanRuntimeShadowMask = false; | ||||||
392 | |||||||
393 | Value *getShadowAddress(Value *Addr, Instruction *Pos); | ||||||
394 | bool isInstrumented(const Function *F); | ||||||
395 | bool isInstrumented(const GlobalAlias *GA); | ||||||
396 | FunctionType *getArgsFunctionType(FunctionType *T); | ||||||
397 | FunctionType *getTrampolineFunctionType(FunctionType *T); | ||||||
398 | TransformedFunction getCustomFunctionType(FunctionType *T); | ||||||
399 | InstrumentedABI getInstrumentedABI(); | ||||||
400 | WrapperKind getWrapperKind(Function *F); | ||||||
401 | void addGlobalNamePrefix(GlobalValue *GV); | ||||||
402 | Function *buildWrapperFunction(Function *F, StringRef NewFName, | ||||||
403 | GlobalValue::LinkageTypes NewFLink, | ||||||
404 | FunctionType *NewFT); | ||||||
405 | Constant *getOrBuildTrampolineFunction(FunctionType *FT, StringRef FName); | ||||||
406 | void initializeCallbackFunctions(Module &M); | ||||||
407 | void initializeRuntimeFunctions(Module &M); | ||||||
408 | |||||||
409 | bool init(Module &M); | ||||||
410 | |||||||
411 | public: | ||||||
412 | DataFlowSanitizer(const std::vector<std::string> &ABIListFiles); | ||||||
413 | |||||||
414 | bool runImpl(Module &M); | ||||||
415 | }; | ||||||
416 | |||||||
417 | struct DFSanFunction { | ||||||
418 | DataFlowSanitizer &DFS; | ||||||
419 | Function *F; | ||||||
420 | DominatorTree DT; | ||||||
421 | DataFlowSanitizer::InstrumentedABI IA; | ||||||
422 | bool IsNativeABI; | ||||||
423 | AllocaInst *LabelReturnAlloca = nullptr; | ||||||
424 | DenseMap<Value *, Value *> ValShadowMap; | ||||||
425 | DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap; | ||||||
426 | std::vector<std::pair<PHINode *, PHINode *>> PHIFixups; | ||||||
427 | DenseSet<Instruction *> SkipInsts; | ||||||
428 | std::vector<Value *> NonZeroChecks; | ||||||
429 | bool AvoidNewBlocks; | ||||||
430 | |||||||
431 | struct CachedCombinedShadow { | ||||||
432 | BasicBlock *Block; | ||||||
433 | Value *Shadow; | ||||||
434 | }; | ||||||
435 | DenseMap<std::pair<Value *, Value *>, CachedCombinedShadow> | ||||||
436 | CachedCombinedShadows; | ||||||
437 | DenseMap<Value *, std::set<Value *>> ShadowElements; | ||||||
438 | |||||||
439 | DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI) | ||||||
440 | : DFS(DFS), F(F), IA(DFS.getInstrumentedABI()), IsNativeABI(IsNativeABI) { | ||||||
441 | DT.recalculate(*F); | ||||||
442 | // FIXME: Need to track down the register allocator issue which causes poor | ||||||
443 | // performance in pathological cases with large numbers of basic blocks. | ||||||
444 | AvoidNewBlocks = F->size() > 1000; | ||||||
445 | } | ||||||
446 | |||||||
447 | Value *getArgTLS(unsigned Index, Instruction *Pos); | ||||||
448 | Value *getShadow(Value *V); | ||||||
449 | void setShadow(Instruction *I, Value *Shadow); | ||||||
450 | Value *combineShadows(Value *V1, Value *V2, Instruction *Pos); | ||||||
451 | Value *combineOperandShadows(Instruction *Inst); | ||||||
452 | Value *loadShadow(Value *ShadowAddr, uint64_t Size, uint64_t Align, | ||||||
453 | Instruction *Pos); | ||||||
454 | void storeShadow(Value *Addr, uint64_t Size, Align Alignment, Value *Shadow, | ||||||
455 | Instruction *Pos); | ||||||
456 | }; | ||||||
457 | |||||||
458 | class DFSanVisitor : public InstVisitor<DFSanVisitor> { | ||||||
459 | public: | ||||||
460 | DFSanFunction &DFSF; | ||||||
461 | |||||||
462 | DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {} | ||||||
463 | |||||||
464 | const DataLayout &getDataLayout() const { | ||||||
465 | return DFSF.F->getParent()->getDataLayout(); | ||||||
466 | } | ||||||
467 | |||||||
468 | // Combines shadow values for all of I's operands. Returns the combined shadow | ||||||
469 | // value. | ||||||
470 | Value *visitOperandShadowInst(Instruction &I); | ||||||
471 | |||||||
472 | void visitUnaryOperator(UnaryOperator &UO); | ||||||
473 | void visitBinaryOperator(BinaryOperator &BO); | ||||||
474 | void visitCastInst(CastInst &CI); | ||||||
475 | void visitCmpInst(CmpInst &CI); | ||||||
476 | void visitGetElementPtrInst(GetElementPtrInst &GEPI); | ||||||
477 | void visitLoadInst(LoadInst &LI); | ||||||
478 | void visitStoreInst(StoreInst &SI); | ||||||
479 | void visitReturnInst(ReturnInst &RI); | ||||||
480 | void visitCallBase(CallBase &CB); | ||||||
481 | void visitPHINode(PHINode &PN); | ||||||
482 | void visitExtractElementInst(ExtractElementInst &I); | ||||||
483 | void visitInsertElementInst(InsertElementInst &I); | ||||||
484 | void visitShuffleVectorInst(ShuffleVectorInst &I); | ||||||
485 | void visitExtractValueInst(ExtractValueInst &I); | ||||||
486 | void visitInsertValueInst(InsertValueInst &I); | ||||||
487 | void visitAllocaInst(AllocaInst &I); | ||||||
488 | void visitSelectInst(SelectInst &I); | ||||||
489 | void visitMemSetInst(MemSetInst &I); | ||||||
490 | void visitMemTransferInst(MemTransferInst &I); | ||||||
491 | }; | ||||||
492 | |||||||
493 | } // end anonymous namespace | ||||||
494 | |||||||
495 | DataFlowSanitizer::DataFlowSanitizer( | ||||||
496 | const std::vector<std::string> &ABIListFiles) { | ||||||
497 | std::vector<std::string> AllABIListFiles(std::move(ABIListFiles)); | ||||||
498 | AllABIListFiles.insert(AllABIListFiles.end(), ClABIListFiles.begin(), | ||||||
499 | ClABIListFiles.end()); | ||||||
500 | // FIXME: should we propagate vfs::FileSystem to this constructor? | ||||||
501 | ABIList.set( | ||||||
502 | SpecialCaseList::createOrDie(AllABIListFiles, *vfs::getRealFileSystem())); | ||||||
503 | } | ||||||
504 | |||||||
505 | FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) { | ||||||
506 | SmallVector<Type *, 4> ArgTypes(T->param_begin(), T->param_end()); | ||||||
507 | ArgTypes.append(T->getNumParams(), ShadowTy); | ||||||
508 | if (T->isVarArg()) | ||||||
509 | ArgTypes.push_back(ShadowPtrTy); | ||||||
510 | Type *RetType = T->getReturnType(); | ||||||
511 | if (!RetType->isVoidTy()) | ||||||
512 | RetType = StructType::get(RetType, ShadowTy); | ||||||
513 | return FunctionType::get(RetType, ArgTypes, T->isVarArg()); | ||||||
514 | } | ||||||
515 | |||||||
516 | FunctionType *DataFlowSanitizer::getTrampolineFunctionType(FunctionType *T) { | ||||||
517 | assert(!T->isVarArg())((!T->isVarArg()) ? static_cast<void> (0) : __assert_fail ("!T->isVarArg()", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp" , 517, __PRETTY_FUNCTION__)); | ||||||
518 | SmallVector<Type *, 4> ArgTypes; | ||||||
519 | ArgTypes.push_back(T->getPointerTo()); | ||||||
520 | ArgTypes.append(T->param_begin(), T->param_end()); | ||||||
521 | ArgTypes.append(T->getNumParams(), ShadowTy); | ||||||
522 | Type *RetType = T->getReturnType(); | ||||||
523 | if (!RetType->isVoidTy()) | ||||||
524 | ArgTypes.push_back(ShadowPtrTy); | ||||||
525 | return FunctionType::get(T->getReturnType(), ArgTypes, false); | ||||||
526 | } | ||||||
527 | |||||||
528 | TransformedFunction DataFlowSanitizer::getCustomFunctionType(FunctionType *T) { | ||||||
529 | SmallVector<Type *, 4> ArgTypes; | ||||||
530 | |||||||
531 | // Some parameters of the custom function being constructed are | ||||||
532 | // parameters of T. Record the mapping from parameters of T to | ||||||
533 | // parameters of the custom function, so that parameter attributes | ||||||
534 | // at call sites can be updated. | ||||||
535 | std::vector<unsigned> ArgumentIndexMapping; | ||||||
536 | for (unsigned i = 0, ie = T->getNumParams(); i != ie; ++i) { | ||||||
537 | Type* param_type = T->getParamType(i); | ||||||
538 | FunctionType *FT; | ||||||
539 | if (isa<PointerType>(param_type) && (FT = dyn_cast<FunctionType>( | ||||||
540 | cast<PointerType>(param_type)->getElementType()))) { | ||||||
541 | ArgumentIndexMapping.push_back(ArgTypes.size()); | ||||||
542 | ArgTypes.push_back(getTrampolineFunctionType(FT)->getPointerTo()); | ||||||
543 | ArgTypes.push_back(Type::getInt8PtrTy(*Ctx)); | ||||||
544 | } else { | ||||||
545 | ArgumentIndexMapping.push_back(ArgTypes.size()); | ||||||
546 | ArgTypes.push_back(param_type); | ||||||
547 | } | ||||||
548 | } | ||||||
549 | for (unsigned i = 0, e = T->getNumParams(); i != e; ++i) | ||||||
550 | ArgTypes.push_back(ShadowTy); | ||||||
551 | if (T->isVarArg()) | ||||||
552 | ArgTypes.push_back(ShadowPtrTy); | ||||||
553 | Type *RetType = T->getReturnType(); | ||||||
554 | if (!RetType->isVoidTy()) | ||||||
555 | ArgTypes.push_back(ShadowPtrTy); | ||||||
556 | return TransformedFunction( | ||||||
557 | T, FunctionType::get(T->getReturnType(), ArgTypes, T->isVarArg()), | ||||||
558 | ArgumentIndexMapping); | ||||||
559 | } | ||||||
560 | |||||||
561 | bool DataFlowSanitizer::init(Module &M) { | ||||||
562 | Triple TargetTriple(M.getTargetTriple()); | ||||||
563 | bool IsX86_64 = TargetTriple.getArch() == Triple::x86_64; | ||||||
564 | bool IsMIPS64 = TargetTriple.isMIPS64(); | ||||||
565 | bool IsAArch64 = TargetTriple.getArch() == Triple::aarch64 || | ||||||
566 | TargetTriple.getArch() == Triple::aarch64_be; | ||||||
567 | |||||||
568 | const DataLayout &DL = M.getDataLayout(); | ||||||
569 | |||||||
570 | Mod = &M; | ||||||
571 | Ctx = &M.getContext(); | ||||||
572 | Int8Ptr = Type::getInt8PtrTy(*Ctx); | ||||||
573 | ShadowTy = IntegerType::get(*Ctx, ShadowWidthBits); | ||||||
574 | ShadowPtrTy = PointerType::getUnqual(ShadowTy); | ||||||
575 | IntptrTy = DL.getIntPtrType(*Ctx); | ||||||
576 | ZeroShadow = ConstantInt::getSigned(ShadowTy, 0); | ||||||
577 | ShadowPtrMul = ConstantInt::getSigned(IntptrTy, ShadowWidthBytes); | ||||||
578 | if (IsX86_64) | ||||||
579 | ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0x700000000000LL); | ||||||
580 | else if (IsMIPS64) | ||||||
581 | ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0xF000000000LL); | ||||||
582 | // AArch64 supports multiple VMAs and the shadow mask is set at runtime. | ||||||
583 | else if (IsAArch64) | ||||||
584 | DFSanRuntimeShadowMask = true; | ||||||
585 | else | ||||||
586 | report_fatal_error("unsupported triple"); | ||||||
587 | |||||||
588 | Type *DFSanUnionArgs[2] = { ShadowTy, ShadowTy }; | ||||||
589 | DFSanUnionFnTy = | ||||||
590 | FunctionType::get(ShadowTy, DFSanUnionArgs, /*isVarArg=*/ false); | ||||||
591 | Type *DFSanUnionLoadArgs[2] = { ShadowPtrTy, IntptrTy }; | ||||||
592 | DFSanUnionLoadFnTy = | ||||||
593 | FunctionType::get(ShadowTy, DFSanUnionLoadArgs, /*isVarArg=*/ false); | ||||||
594 | DFSanUnimplementedFnTy = FunctionType::get( | ||||||
595 | Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false); | ||||||
596 | Type *DFSanSetLabelArgs[3] = { ShadowTy, Type::getInt8PtrTy(*Ctx), IntptrTy }; | ||||||
597 | DFSanSetLabelFnTy = FunctionType::get(Type::getVoidTy(*Ctx), | ||||||
598 | DFSanSetLabelArgs, /*isVarArg=*/false); | ||||||
599 | DFSanNonzeroLabelFnTy = FunctionType::get( | ||||||
600 | Type::getVoidTy(*Ctx), None, /*isVarArg=*/false); | ||||||
601 | DFSanVarargWrapperFnTy = FunctionType::get( | ||||||
602 | Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false); | ||||||
603 | DFSanCmpCallbackFnTy = FunctionType::get(Type::getVoidTy(*Ctx), ShadowTy, | ||||||
604 | /*isVarArg=*/false); | ||||||
605 | Type *DFSanLoadStoreCallbackArgs[2] = {ShadowTy, Int8Ptr}; | ||||||
606 | DFSanLoadStoreCallbackFnTy = | ||||||
607 | FunctionType::get(Type::getVoidTy(*Ctx), DFSanLoadStoreCallbackArgs, | ||||||
608 | /*isVarArg=*/false); | ||||||
609 | Type *DFSanMemTransferCallbackArgs[2] = {ShadowPtrTy, IntptrTy}; | ||||||
610 | DFSanMemTransferCallbackFnTy = | ||||||
611 | FunctionType::get(Type::getVoidTy(*Ctx), DFSanMemTransferCallbackArgs, | ||||||
612 | /*isVarArg=*/false); | ||||||
613 | |||||||
614 | ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000); | ||||||
615 | return true; | ||||||
616 | } | ||||||
617 | |||||||
618 | bool DataFlowSanitizer::isInstrumented(const Function *F) { | ||||||
619 | return !ABIList.isIn(*F, "uninstrumented"); | ||||||
620 | } | ||||||
621 | |||||||
622 | bool DataFlowSanitizer::isInstrumented(const GlobalAlias *GA) { | ||||||
623 | return !ABIList.isIn(*GA, "uninstrumented"); | ||||||
624 | } | ||||||
625 | |||||||
626 | DataFlowSanitizer::InstrumentedABI DataFlowSanitizer::getInstrumentedABI() { | ||||||
627 | return ClArgsABI ? IA_Args : IA_TLS; | ||||||
628 | } | ||||||
629 | |||||||
630 | DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) { | ||||||
631 | if (ABIList.isIn(*F, "functional")) | ||||||
632 | return WK_Functional; | ||||||
633 | if (ABIList.isIn(*F, "discard")) | ||||||
634 | return WK_Discard; | ||||||
635 | if (ABIList.isIn(*F, "custom")) | ||||||
636 | return WK_Custom; | ||||||
637 | |||||||
638 | return WK_Warning; | ||||||
639 | } | ||||||
640 | |||||||
641 | void DataFlowSanitizer::addGlobalNamePrefix(GlobalValue *GV) { | ||||||
642 | std::string GVName = std::string(GV->getName()), Prefix = "dfs$"; | ||||||
643 | GV->setName(Prefix + GVName); | ||||||
644 | |||||||
645 | // Try to change the name of the function in module inline asm. We only do | ||||||
646 | // this for specific asm directives, currently only ".symver", to try to avoid | ||||||
647 | // corrupting asm which happens to contain the symbol name as a substring. | ||||||
648 | // Note that the substitution for .symver assumes that the versioned symbol | ||||||
649 | // also has an instrumented name. | ||||||
650 | std::string Asm = GV->getParent()->getModuleInlineAsm(); | ||||||
651 | std::string SearchStr = ".symver " + GVName + ","; | ||||||
652 | size_t Pos = Asm.find(SearchStr); | ||||||
653 | if (Pos != std::string::npos) { | ||||||
654 | Asm.replace(Pos, SearchStr.size(), | ||||||
655 | ".symver " + Prefix + GVName + "," + Prefix); | ||||||
656 | GV->getParent()->setModuleInlineAsm(Asm); | ||||||
657 | } | ||||||
658 | } | ||||||
659 | |||||||
660 | Function * | ||||||
661 | DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName, | ||||||
662 | GlobalValue::LinkageTypes NewFLink, | ||||||
663 | FunctionType *NewFT) { | ||||||
664 | FunctionType *FT = F->getFunctionType(); | ||||||
665 | Function *NewF = Function::Create(NewFT, NewFLink, F->getAddressSpace(), | ||||||
666 | NewFName, F->getParent()); | ||||||
667 | NewF->copyAttributesFrom(F); | ||||||
668 | NewF->removeAttributes( | ||||||
669 | AttributeList::ReturnIndex, | ||||||
670 | AttributeFuncs::typeIncompatible(NewFT->getReturnType())); | ||||||
671 | |||||||
672 | BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF); | ||||||
673 | if (F->isVarArg()) { | ||||||
674 | NewF->removeAttributes(AttributeList::FunctionIndex, | ||||||
675 | AttrBuilder().addAttribute("split-stack")); | ||||||
676 | CallInst::Create(DFSanVarargWrapperFn, | ||||||
677 | IRBuilder<>(BB).CreateGlobalStringPtr(F->getName()), "", | ||||||
678 | BB); | ||||||
679 | new UnreachableInst(*Ctx, BB); | ||||||
680 | } else { | ||||||
681 | std::vector<Value *> Args; | ||||||
682 | unsigned n = FT->getNumParams(); | ||||||
683 | for (Function::arg_iterator ai = NewF->arg_begin(); n != 0; ++ai, --n) | ||||||
684 | Args.push_back(&*ai); | ||||||
685 | CallInst *CI = CallInst::Create(F, Args, "", BB); | ||||||
686 | if (FT->getReturnType()->isVoidTy()) | ||||||
687 | ReturnInst::Create(*Ctx, BB); | ||||||
688 | else | ||||||
689 | ReturnInst::Create(*Ctx, CI, BB); | ||||||
690 | } | ||||||
691 | |||||||
692 | return NewF; | ||||||
693 | } | ||||||
694 | |||||||
695 | Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT, | ||||||
696 | StringRef FName) { | ||||||
697 | FunctionType *FTT = getTrampolineFunctionType(FT); | ||||||
698 | FunctionCallee C = Mod->getOrInsertFunction(FName, FTT); | ||||||
699 | Function *F = dyn_cast<Function>(C.getCallee()); | ||||||
700 | if (F && F->isDeclaration()) { | ||||||
701 | F->setLinkage(GlobalValue::LinkOnceODRLinkage); | ||||||
702 | BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F); | ||||||
703 | std::vector<Value *> Args; | ||||||
704 | Function::arg_iterator AI = F->arg_begin(); ++AI; | ||||||
705 | for (unsigned N = FT->getNumParams(); N != 0; ++AI, --N) | ||||||
706 | Args.push_back(&*AI); | ||||||
707 | CallInst *CI = CallInst::Create(FT, &*F->arg_begin(), Args, "", BB); | ||||||
708 | ReturnInst *RI; | ||||||
709 | if (FT->getReturnType()->isVoidTy()) | ||||||
710 | RI = ReturnInst::Create(*Ctx, BB); | ||||||
711 | else | ||||||
712 | RI = ReturnInst::Create(*Ctx, CI, BB); | ||||||
713 | |||||||
714 | DFSanFunction DFSF(*this, F, /*IsNativeABI=*/true); | ||||||
715 | Function::arg_iterator ValAI = F->arg_begin(), ShadowAI = AI; ++ValAI; | ||||||
716 | for (unsigned N = FT->getNumParams(); N != 0; ++ValAI, ++ShadowAI, --N) | ||||||
717 | DFSF.ValShadowMap[&*ValAI] = &*ShadowAI; | ||||||
718 | DFSanVisitor(DFSF).visitCallInst(*CI); | ||||||
719 | if (!FT->getReturnType()->isVoidTy()) | ||||||
720 | new StoreInst(DFSF.getShadow(RI->getReturnValue()), | ||||||
721 | &*std::prev(F->arg_end()), RI); | ||||||
722 | } | ||||||
723 | |||||||
724 | return cast<Constant>(C.getCallee()); | ||||||
725 | } | ||||||
726 | |||||||
727 | // Initialize DataFlowSanitizer runtime functions and declare them in the module | ||||||
728 | void DataFlowSanitizer::initializeRuntimeFunctions(Module &M) { | ||||||
729 | { | ||||||
730 | AttributeList AL; | ||||||
731 | AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex, | ||||||
732 | Attribute::NoUnwind); | ||||||
733 | AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex, | ||||||
734 | Attribute::ReadNone); | ||||||
735 | AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex, | ||||||
736 | Attribute::ZExt); | ||||||
737 | AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt); | ||||||
738 | AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt); | ||||||
739 | DFSanUnionFn = | ||||||
740 | Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy, AL); | ||||||
741 | } | ||||||
742 | { | ||||||
743 | AttributeList AL; | ||||||
744 | AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex, | ||||||
745 | Attribute::NoUnwind); | ||||||
746 | AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex, | ||||||
747 | Attribute::ReadNone); | ||||||
748 | AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex, | ||||||
749 | Attribute::ZExt); | ||||||
750 | AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt); | ||||||
751 | AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt); | ||||||
752 | DFSanCheckedUnionFn = | ||||||
753 | Mod->getOrInsertFunction("dfsan_union", DFSanUnionFnTy, AL); | ||||||
754 | } | ||||||
755 | { | ||||||
756 | AttributeList AL; | ||||||
757 | AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex, | ||||||
758 | Attribute::NoUnwind); | ||||||
759 | AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex, | ||||||
760 | Attribute::ReadOnly); | ||||||
761 | AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex, | ||||||
762 | Attribute::ZExt); | ||||||
763 | DFSanUnionLoadFn = | ||||||
764 | Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy, AL); | ||||||
765 | } | ||||||
766 | { | ||||||
767 | AttributeList AL; | ||||||
768 | AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex, | ||||||
769 | Attribute::NoUnwind); | ||||||
770 | AL = AL.addAttribute(M.getContext(), AttributeList::FunctionIndex, | ||||||
771 | Attribute::ReadOnly); | ||||||
772 | AL = AL.addAttribute(M.getContext(), AttributeList::ReturnIndex, | ||||||
773 | Attribute::ZExt); | ||||||
774 | DFSanUnionLoadFast16LabelsFn = Mod->getOrInsertFunction( | ||||||
775 | "__dfsan_union_load_fast16labels", DFSanUnionLoadFnTy, AL); | ||||||
776 | } | ||||||
777 | DFSanUnimplementedFn = | ||||||
778 | Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy); | ||||||
779 | { | ||||||
780 | AttributeList AL; | ||||||
781 | AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt); | ||||||
782 | DFSanSetLabelFn = | ||||||
783 | Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy, AL); | ||||||
784 | } | ||||||
785 | DFSanNonzeroLabelFn = | ||||||
786 | Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy); | ||||||
787 | DFSanVarargWrapperFn = Mod->getOrInsertFunction("__dfsan_vararg_wrapper", | ||||||
788 | DFSanVarargWrapperFnTy); | ||||||
789 | } | ||||||
790 | |||||||
791 | // Initializes event callback functions and declare them in the module | ||||||
792 | void DataFlowSanitizer::initializeCallbackFunctions(Module &M) { | ||||||
793 | DFSanLoadCallbackFn = Mod->getOrInsertFunction("__dfsan_load_callback", | ||||||
794 | DFSanLoadStoreCallbackFnTy); | ||||||
795 | DFSanStoreCallbackFn = Mod->getOrInsertFunction("__dfsan_store_callback", | ||||||
796 | DFSanLoadStoreCallbackFnTy); | ||||||
797 | DFSanMemTransferCallbackFn = Mod->getOrInsertFunction( | ||||||
798 | "__dfsan_mem_transfer_callback", DFSanMemTransferCallbackFnTy); | ||||||
799 | DFSanCmpCallbackFn = | ||||||
800 | Mod->getOrInsertFunction("__dfsan_cmp_callback", DFSanCmpCallbackFnTy); | ||||||
801 | } | ||||||
802 | |||||||
803 | bool DataFlowSanitizer::runImpl(Module &M) { | ||||||
804 | init(M); | ||||||
805 | |||||||
806 | if (ABIList.isIn(M, "skip")) | ||||||
| |||||||
807 | return false; | ||||||
808 | |||||||
809 | const unsigned InitialGlobalSize = M.global_size(); | ||||||
810 | const unsigned InitialModuleSize = M.size(); | ||||||
811 | |||||||
812 | bool Changed = false; | ||||||
813 | |||||||
814 | Type *ArgTLSTy = ArrayType::get(ShadowTy, 64); | ||||||
815 | ArgTLS = Mod->getOrInsertGlobal("__dfsan_arg_tls", ArgTLSTy); | ||||||
816 | if (GlobalVariable *G
| ||||||
817 | Changed |= G->getThreadLocalMode() != GlobalVariable::InitialExecTLSModel; | ||||||
818 | G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); | ||||||
819 | } | ||||||
820 | RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", ShadowTy); | ||||||
821 | if (GlobalVariable *G
| ||||||
822 | Changed |= G->getThreadLocalMode() != GlobalVariable::InitialExecTLSModel; | ||||||
823 | G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel); | ||||||
824 | } | ||||||
825 | |||||||
826 | ExternalShadowMask = | ||||||
827 | Mod->getOrInsertGlobal(kDFSanExternShadowPtrMask, IntptrTy); | ||||||
828 | |||||||
829 | initializeCallbackFunctions(M); | ||||||
830 | initializeRuntimeFunctions(M); | ||||||
831 | |||||||
832 | std::vector<Function *> FnsToInstrument; | ||||||
833 | SmallPtrSet<Function *, 2> FnsWithNativeABI; | ||||||
834 | for (Function &i : M) { | ||||||
835 | if (!i.isIntrinsic() && | ||||||
836 | &i != DFSanUnionFn.getCallee()->stripPointerCasts() && | ||||||
837 | &i != DFSanCheckedUnionFn.getCallee()->stripPointerCasts() && | ||||||
838 | &i != DFSanUnionLoadFn.getCallee()->stripPointerCasts() && | ||||||
839 | &i != DFSanUnionLoadFast16LabelsFn.getCallee()->stripPointerCasts() && | ||||||
840 | &i != DFSanUnimplementedFn.getCallee()->stripPointerCasts() && | ||||||
841 | &i != DFSanSetLabelFn.getCallee()->stripPointerCasts() && | ||||||
842 | &i != DFSanNonzeroLabelFn.getCallee()->stripPointerCasts() && | ||||||
843 | &i != DFSanVarargWrapperFn.getCallee()->stripPointerCasts() && | ||||||
844 | &i != DFSanLoadCallbackFn.getCallee()->stripPointerCasts() && | ||||||
845 | &i != DFSanStoreCallbackFn.getCallee()->stripPointerCasts() && | ||||||
846 | &i != DFSanMemTransferCallbackFn.getCallee()->stripPointerCasts() && | ||||||
847 | &i != DFSanCmpCallbackFn.getCallee()->stripPointerCasts()) | ||||||
848 | FnsToInstrument.push_back(&i); | ||||||
849 | } | ||||||
850 | |||||||
851 | // Give function aliases prefixes when necessary, and build wrappers where the | ||||||
852 | // instrumentedness is inconsistent. | ||||||
853 | for (Module::alias_iterator i = M.alias_begin(), e = M.alias_end(); i != e;) { | ||||||
854 | GlobalAlias *GA = &*i; | ||||||
855 | ++i; | ||||||
856 | // Don't stop on weak. We assume people aren't playing games with the | ||||||
857 | // instrumentedness of overridden weak aliases. | ||||||
858 | if (auto F = dyn_cast<Function>(GA->getBaseObject())) { | ||||||
859 | bool GAInst = isInstrumented(GA), FInst = isInstrumented(F); | ||||||
860 | if (GAInst && FInst) { | ||||||
861 | addGlobalNamePrefix(GA); | ||||||
862 | } else if (GAInst != FInst) { | ||||||
863 | // Non-instrumented alias of an instrumented function, or vice versa. | ||||||
864 | // Replace the alias with a native-ABI wrapper of the aliasee. The pass | ||||||
865 | // below will take care of instrumenting it. | ||||||
866 | Function *NewF = | ||||||
867 | buildWrapperFunction(F, "", GA->getLinkage(), F->getFunctionType()); | ||||||
868 | GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewF, GA->getType())); | ||||||
869 | NewF->takeName(GA); | ||||||
870 | GA->eraseFromParent(); | ||||||
871 | FnsToInstrument.push_back(NewF); | ||||||
872 | } | ||||||
873 | } | ||||||
874 | } | ||||||
875 | |||||||
876 | ReadOnlyNoneAttrs.addAttribute(Attribute::ReadOnly) | ||||||
877 | .addAttribute(Attribute::ReadNone); | ||||||
878 | |||||||
879 | // First, change the ABI of every function in the module. ABI-listed | ||||||
880 | // functions keep their original ABI and get a wrapper function. | ||||||
881 | for (std::vector<Function *>::iterator i = FnsToInstrument.begin(), | ||||||
882 | e = FnsToInstrument.end(); | ||||||
883 | i != e; ++i) { | ||||||
884 | Function &F = **i; | ||||||
885 | FunctionType *FT = F.getFunctionType(); | ||||||
886 | |||||||
887 | bool IsZeroArgsVoidRet = (FT->getNumParams() == 0 && !FT->isVarArg() && | ||||||
888 | FT->getReturnType()->isVoidTy()); | ||||||
889 | |||||||
890 | if (isInstrumented(&F)) { | ||||||
891 | // Instrumented functions get a 'dfs$' prefix. This allows us to more | ||||||
892 | // easily identify cases of mismatching ABIs. | ||||||
893 | if (getInstrumentedABI() == IA_Args && !IsZeroArgsVoidRet) { | ||||||
894 | FunctionType *NewFT = getArgsFunctionType(FT); | ||||||
895 | Function *NewF = Function::Create(NewFT, F.getLinkage(), | ||||||
896 | F.getAddressSpace(), "", &M); | ||||||
897 | NewF->copyAttributesFrom(&F); | ||||||
898 | NewF->removeAttributes( | ||||||
899 | AttributeList::ReturnIndex, | ||||||
900 | AttributeFuncs::typeIncompatible(NewFT->getReturnType())); | ||||||
901 | for (Function::arg_iterator FArg = F.arg_begin(), | ||||||
902 | NewFArg = NewF->arg_begin(), | ||||||
903 | FArgEnd = F.arg_end(); | ||||||
904 | FArg != FArgEnd; ++FArg, ++NewFArg) { | ||||||
905 | FArg->replaceAllUsesWith(&*NewFArg); | ||||||
906 | } | ||||||
907 | NewF->getBasicBlockList().splice(NewF->begin(), F.getBasicBlockList()); | ||||||
908 | |||||||
909 | for (Function::user_iterator UI = F.user_begin(), UE = F.user_end(); | ||||||
910 | UI != UE;) { | ||||||
911 | BlockAddress *BA = dyn_cast<BlockAddress>(*UI); | ||||||
912 | ++UI; | ||||||
913 | if (BA) { | ||||||
914 | BA->replaceAllUsesWith( | ||||||
915 | BlockAddress::get(NewF, BA->getBasicBlock())); | ||||||
916 | delete BA; | ||||||
917 | } | ||||||
918 | } | ||||||
919 | F.replaceAllUsesWith( | ||||||
920 | ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT))); | ||||||
921 | NewF->takeName(&F); | ||||||
922 | F.eraseFromParent(); | ||||||
923 | *i = NewF; | ||||||
924 | addGlobalNamePrefix(NewF); | ||||||
925 | } else { | ||||||
926 | addGlobalNamePrefix(&F); | ||||||
927 | } | ||||||
928 | } else if (!IsZeroArgsVoidRet || getWrapperKind(&F) == WK_Custom) { | ||||||
929 | // Build a wrapper function for F. The wrapper simply calls F, and is | ||||||
930 | // added to FnsToInstrument so that any instrumentation according to its | ||||||
931 | // WrapperKind is done in the second pass below. | ||||||
932 | FunctionType *NewFT = getInstrumentedABI() == IA_Args | ||||||
933 | ? getArgsFunctionType(FT) | ||||||
934 | : FT; | ||||||
935 | |||||||
936 | // If the function being wrapped has local linkage, then preserve the | ||||||
937 | // function's linkage in the wrapper function. | ||||||
938 | GlobalValue::LinkageTypes wrapperLinkage = | ||||||
939 | F.hasLocalLinkage() | ||||||
940 | ? F.getLinkage() | ||||||
941 | : GlobalValue::LinkOnceODRLinkage; | ||||||
942 | |||||||
943 | Function *NewF = buildWrapperFunction( | ||||||
944 | &F, std::string("dfsw$") + std::string(F.getName()), | ||||||
945 | wrapperLinkage, NewFT); | ||||||
946 | if (getInstrumentedABI() == IA_TLS) | ||||||
947 | NewF->removeAttributes(AttributeList::FunctionIndex, ReadOnlyNoneAttrs); | ||||||
948 | |||||||
949 | Value *WrappedFnCst = | ||||||
950 | ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT)); | ||||||
951 | F.replaceAllUsesWith(WrappedFnCst); | ||||||
952 | |||||||
953 | UnwrappedFnMap[WrappedFnCst] = &F; | ||||||
954 | *i = NewF; | ||||||
955 | |||||||
956 | if (!F.isDeclaration()) { | ||||||
957 | // This function is probably defining an interposition of an | ||||||
958 | // uninstrumented function and hence needs to keep the original ABI. | ||||||
959 | // But any functions it may call need to use the instrumented ABI, so | ||||||
960 | // we instrument it in a mode which preserves the original ABI. | ||||||
961 | FnsWithNativeABI.insert(&F); | ||||||
962 | |||||||
963 | // This code needs to rebuild the iterators, as they may be invalidated | ||||||
964 | // by the push_back, taking care that the new range does not include | ||||||
965 | // any functions added by this code. | ||||||
966 | size_t N = i - FnsToInstrument.begin(), | ||||||
967 | Count = e - FnsToInstrument.begin(); | ||||||
968 | FnsToInstrument.push_back(&F); | ||||||
969 | i = FnsToInstrument.begin() + N; | ||||||
970 | e = FnsToInstrument.begin() + Count; | ||||||
971 | } | ||||||
972 | // Hopefully, nobody will try to indirectly call a vararg | ||||||
973 | // function... yet. | ||||||
974 | } else if (FT->isVarArg()) { | ||||||
975 | UnwrappedFnMap[&F] = &F; | ||||||
976 | *i = nullptr; | ||||||
977 | } | ||||||
978 | } | ||||||
979 | |||||||
980 | for (Function *i : FnsToInstrument) { | ||||||
981 | if (!i || i->isDeclaration()) | ||||||
982 | continue; | ||||||
983 | |||||||
984 | removeUnreachableBlocks(*i); | ||||||
985 | |||||||
986 | DFSanFunction DFSF(*this, i, FnsWithNativeABI.count(i)); | ||||||
987 | |||||||
988 | // DFSanVisitor may create new basic blocks, which confuses df_iterator. | ||||||
989 | // Build a copy of the list before iterating over it. | ||||||
990 | SmallVector<BasicBlock *, 4> BBList(depth_first(&i->getEntryBlock())); | ||||||
991 | |||||||
992 | for (BasicBlock *i : BBList) { | ||||||
993 | Instruction *Inst = &i->front(); | ||||||
994 | while (true) { | ||||||
995 | // DFSanVisitor may split the current basic block, changing the current | ||||||
996 | // instruction's next pointer and moving the next instruction to the | ||||||
997 | // tail block from which we should continue. | ||||||
998 | Instruction *Next = Inst->getNextNode(); | ||||||
999 | // DFSanVisitor may delete Inst, so keep track of whether it was a | ||||||
1000 | // terminator. | ||||||
1001 | bool IsTerminator = Inst->isTerminator(); | ||||||
1002 | if (!DFSF.SkipInsts.count(Inst)) | ||||||
1003 | DFSanVisitor(DFSF).visit(Inst); | ||||||
1004 | if (IsTerminator) | ||||||
1005 | break; | ||||||
1006 | Inst = Next; | ||||||
1007 | } | ||||||
1008 | } | ||||||
1009 | |||||||
1010 | // We will not necessarily be able to compute the shadow for every phi node | ||||||
1011 | // until we have visited every block. Therefore, the code that handles phi | ||||||
1012 | // nodes adds them to the PHIFixups list so that they can be properly | ||||||
1013 | // handled here. | ||||||
1014 | for (std::vector<std::pair<PHINode *, PHINode *>>::iterator | ||||||
1015 | i = DFSF.PHIFixups.begin(), | ||||||
1016 | e = DFSF.PHIFixups.end(); | ||||||
1017 | i != e; ++i) { | ||||||
1018 | for (unsigned val = 0, n = i->first->getNumIncomingValues(); val != n; | ||||||
1019 | ++val) { | ||||||
1020 | i->second->setIncomingValue( | ||||||
1021 | val, DFSF.getShadow(i->first->getIncomingValue(val))); | ||||||
1022 | } | ||||||
1023 | } | ||||||
1024 | |||||||
1025 | // -dfsan-debug-nonzero-labels will split the CFG in all kinds of crazy | ||||||
1026 | // places (i.e. instructions in basic blocks we haven't even begun visiting | ||||||
1027 | // yet). To make our life easier, do this work in a pass after the main | ||||||
1028 | // instrumentation. | ||||||
1029 | if (ClDebugNonzeroLabels) { | ||||||
1030 | for (Value *V : DFSF.NonZeroChecks) { | ||||||
1031 | Instruction *Pos; | ||||||
1032 | if (Instruction *I = dyn_cast<Instruction>(V)) | ||||||
1033 | Pos = I->getNextNode(); | ||||||
1034 | else | ||||||
1035 | Pos = &DFSF.F->getEntryBlock().front(); | ||||||
1036 | while (isa<PHINode>(Pos) || isa<AllocaInst>(Pos)) | ||||||
1037 | Pos = Pos->getNextNode(); | ||||||
1038 | IRBuilder<> IRB(Pos); | ||||||
1039 | Value *Ne = IRB.CreateICmpNE(V, DFSF.DFS.ZeroShadow); | ||||||
1040 | BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen( | ||||||
1041 | Ne, Pos, /*Unreachable=*/false, ColdCallWeights)); | ||||||
1042 | IRBuilder<> ThenIRB(BI); | ||||||
1043 | ThenIRB.CreateCall(DFSF.DFS.DFSanNonzeroLabelFn, {}); | ||||||
1044 | } | ||||||
1045 | } | ||||||
1046 | } | ||||||
1047 | |||||||
1048 | return Changed || !FnsToInstrument.empty() || | ||||||
1049 | M.global_size() != InitialGlobalSize || M.size() != InitialModuleSize; | ||||||
1050 | } | ||||||
1051 | |||||||
1052 | Value *DFSanFunction::getArgTLS(unsigned Idx, Instruction *Pos) { | ||||||
1053 | IRBuilder<> IRB(Pos); | ||||||
1054 | return IRB.CreateConstGEP2_64(ArrayType::get(DFS.ShadowTy, 64), DFS.ArgTLS, 0, | ||||||
1055 | Idx); | ||||||
1056 | } | ||||||
1057 | |||||||
1058 | Value *DFSanFunction::getShadow(Value *V) { | ||||||
1059 | if (!isa<Argument>(V) && !isa<Instruction>(V)) | ||||||
1060 | return DFS.ZeroShadow; | ||||||
1061 | Value *&Shadow = ValShadowMap[V]; | ||||||
1062 | if (!Shadow) { | ||||||
1063 | if (Argument *A
| ||||||
1064 | if (IsNativeABI) | ||||||
1065 | return DFS.ZeroShadow; | ||||||
1066 | switch (IA) { | ||||||
1067 | case DataFlowSanitizer::IA_TLS: { | ||||||
1068 | Value *ArgTLSPtr = DFS.ArgTLS; | ||||||
1069 | Instruction *ArgTLSPos = | ||||||
1070 | DFS.ArgTLS ? &*F->getEntryBlock().begin() | ||||||
1071 | : cast<Instruction>(ArgTLSPtr)->getNextNode(); | ||||||
1072 | IRBuilder<> IRB(ArgTLSPos); | ||||||
1073 | Shadow = | ||||||
1074 | IRB.CreateLoad(DFS.ShadowTy, getArgTLS(A->getArgNo(), ArgTLSPos)); | ||||||
1075 | break; | ||||||
1076 | } | ||||||
1077 | case DataFlowSanitizer::IA_Args: { | ||||||
1078 | unsigned ArgIdx = A->getArgNo() + F->arg_size() / 2; | ||||||
1079 | Function::arg_iterator i = F->arg_begin(); | ||||||
1080 | while (ArgIdx--) | ||||||
1081 | ++i; | ||||||
1082 | Shadow = &*i; | ||||||
1083 | assert(Shadow->getType() == DFS.ShadowTy)((Shadow->getType() == DFS.ShadowTy) ? static_cast<void > (0) : __assert_fail ("Shadow->getType() == DFS.ShadowTy" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp" , 1083, __PRETTY_FUNCTION__)); | ||||||
1084 | break; | ||||||
1085 | } | ||||||
1086 | } | ||||||
1087 | NonZeroChecks.push_back(Shadow); | ||||||
1088 | } else { | ||||||
1089 | Shadow = DFS.ZeroShadow; | ||||||
1090 | } | ||||||
1091 | } | ||||||
1092 | return Shadow; | ||||||
1093 | } | ||||||
1094 | |||||||
1095 | void DFSanFunction::setShadow(Instruction *I, Value *Shadow) { | ||||||
1096 | assert(!ValShadowMap.count(I))((!ValShadowMap.count(I)) ? static_cast<void> (0) : __assert_fail ("!ValShadowMap.count(I)", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp" , 1096, __PRETTY_FUNCTION__)); | ||||||
1097 | assert(Shadow->getType() == DFS.ShadowTy)((Shadow->getType() == DFS.ShadowTy) ? static_cast<void > (0) : __assert_fail ("Shadow->getType() == DFS.ShadowTy" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp" , 1097, __PRETTY_FUNCTION__)); | ||||||
1098 | ValShadowMap[I] = Shadow; | ||||||
1099 | } | ||||||
1100 | |||||||
1101 | Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) { | ||||||
1102 | assert(Addr != RetvalTLS && "Reinstrumenting?")((Addr != RetvalTLS && "Reinstrumenting?") ? static_cast <void> (0) : __assert_fail ("Addr != RetvalTLS && \"Reinstrumenting?\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp" , 1102, __PRETTY_FUNCTION__)); | ||||||
1103 | IRBuilder<> IRB(Pos); | ||||||
1104 | Value *ShadowPtrMaskValue; | ||||||
1105 | if (DFSanRuntimeShadowMask) | ||||||
1106 | ShadowPtrMaskValue = IRB.CreateLoad(IntptrTy, ExternalShadowMask); | ||||||
1107 | else | ||||||
1108 | ShadowPtrMaskValue = ShadowPtrMask; | ||||||
1109 | return IRB.CreateIntToPtr( | ||||||
1110 | IRB.CreateMul( | ||||||
1111 | IRB.CreateAnd(IRB.CreatePtrToInt(Addr, IntptrTy), | ||||||
1112 | IRB.CreatePtrToInt(ShadowPtrMaskValue, IntptrTy)), | ||||||
1113 | ShadowPtrMul), | ||||||
1114 | ShadowPtrTy); | ||||||
1115 | } | ||||||
1116 | |||||||
1117 | // Generates IR to compute the union of the two given shadows, inserting it | ||||||
1118 | // before Pos. Returns the computed union Value. | ||||||
1119 | Value *DFSanFunction::combineShadows(Value *V1, Value *V2, Instruction *Pos) { | ||||||
1120 | if (V1 == DFS.ZeroShadow) | ||||||
1121 | return V2; | ||||||
1122 | if (V2 == DFS.ZeroShadow) | ||||||
1123 | return V1; | ||||||
1124 | if (V1 == V2) | ||||||
1125 | return V1; | ||||||
1126 | |||||||
1127 | auto V1Elems = ShadowElements.find(V1); | ||||||
1128 | auto V2Elems = ShadowElements.find(V2); | ||||||
1129 | if (V1Elems != ShadowElements.end() && V2Elems != ShadowElements.end()) { | ||||||
1130 | if (std::includes(V1Elems->second.begin(), V1Elems->second.end(), | ||||||
1131 | V2Elems->second.begin(), V2Elems->second.end())) { | ||||||
1132 | return V1; | ||||||
1133 | } else if (std::includes(V2Elems->second.begin(), V2Elems->second.end(), | ||||||
1134 | V1Elems->second.begin(), V1Elems->second.end())) { | ||||||
1135 | return V2; | ||||||
1136 | } | ||||||
1137 | } else if (V1Elems != ShadowElements.end()) { | ||||||
1138 | if (V1Elems->second.count(V2)) | ||||||
1139 | return V1; | ||||||
1140 | } else if (V2Elems != ShadowElements.end()) { | ||||||
1141 | if (V2Elems->second.count(V1)) | ||||||
1142 | return V2; | ||||||
1143 | } | ||||||
1144 | |||||||
1145 | auto Key = std::make_pair(V1, V2); | ||||||
1146 | if (V1 > V2) | ||||||
1147 | std::swap(Key.first, Key.second); | ||||||
1148 | CachedCombinedShadow &CCS = CachedCombinedShadows[Key]; | ||||||
1149 | if (CCS.Block && DT.dominates(CCS.Block, Pos->getParent())) | ||||||
1150 | return CCS.Shadow; | ||||||
1151 | |||||||
1152 | IRBuilder<> IRB(Pos); | ||||||
1153 | if (ClFast16Labels) { | ||||||
1154 | CCS.Block = Pos->getParent(); | ||||||
1155 | CCS.Shadow = IRB.CreateOr(V1, V2); | ||||||
1156 | } else if (AvoidNewBlocks) { | ||||||
1157 | CallInst *Call = IRB.CreateCall(DFS.DFSanCheckedUnionFn, {V1, V2}); | ||||||
1158 | Call->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt); | ||||||
1159 | Call->addParamAttr(0, Attribute::ZExt); | ||||||
1160 | Call->addParamAttr(1, Attribute::ZExt); | ||||||
1161 | |||||||
1162 | CCS.Block = Pos->getParent(); | ||||||
1163 | CCS.Shadow = Call; | ||||||
1164 | } else { | ||||||
1165 | BasicBlock *Head = Pos->getParent(); | ||||||
1166 | Value *Ne = IRB.CreateICmpNE(V1, V2); | ||||||
1167 | BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen( | ||||||
1168 | Ne, Pos, /*Unreachable=*/false, DFS.ColdCallWeights, &DT)); | ||||||
1169 | IRBuilder<> ThenIRB(BI); | ||||||
1170 | CallInst *Call = ThenIRB.CreateCall(DFS.DFSanUnionFn, {V1, V2}); | ||||||
1171 | Call->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt); | ||||||
1172 | Call->addParamAttr(0, Attribute::ZExt); | ||||||
1173 | Call->addParamAttr(1, Attribute::ZExt); | ||||||
1174 | |||||||
1175 | BasicBlock *Tail = BI->getSuccessor(0); | ||||||
1176 | PHINode *Phi = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front()); | ||||||
1177 | Phi->addIncoming(Call, Call->getParent()); | ||||||
1178 | Phi->addIncoming(V1, Head); | ||||||
1179 | |||||||
1180 | CCS.Block = Tail; | ||||||
1181 | CCS.Shadow = Phi; | ||||||
1182 | } | ||||||
1183 | |||||||
1184 | std::set<Value *> UnionElems; | ||||||
1185 | if (V1Elems != ShadowElements.end()) { | ||||||
1186 | UnionElems = V1Elems->second; | ||||||
1187 | } else { | ||||||
1188 | UnionElems.insert(V1); | ||||||
1189 | } | ||||||
1190 | if (V2Elems != ShadowElements.end()) { | ||||||
1191 | UnionElems.insert(V2Elems->second.begin(), V2Elems->second.end()); | ||||||
1192 | } else { | ||||||
1193 | UnionElems.insert(V2); | ||||||
1194 | } | ||||||
1195 | ShadowElements[CCS.Shadow] = std::move(UnionElems); | ||||||
1196 | |||||||
1197 | return CCS.Shadow; | ||||||
1198 | } | ||||||
1199 | |||||||
1200 | // A convenience function which folds the shadows of each of the operands | ||||||
1201 | // of the provided instruction Inst, inserting the IR before Inst. Returns | ||||||
1202 | // the computed union Value. | ||||||
1203 | Value *DFSanFunction::combineOperandShadows(Instruction *Inst) { | ||||||
1204 | if (Inst->getNumOperands() == 0) | ||||||
1205 | return DFS.ZeroShadow; | ||||||
1206 | |||||||
1207 | Value *Shadow = getShadow(Inst->getOperand(0)); | ||||||
1208 | for (unsigned i = 1, n = Inst->getNumOperands(); i != n; ++i) { | ||||||
1209 | Shadow = combineShadows(Shadow, getShadow(Inst->getOperand(i)), Inst); | ||||||
1210 | } | ||||||
1211 | return Shadow; | ||||||
1212 | } | ||||||
1213 | |||||||
1214 | Value *DFSanVisitor::visitOperandShadowInst(Instruction &I) { | ||||||
1215 | Value *CombinedShadow = DFSF.combineOperandShadows(&I); | ||||||
1216 | DFSF.setShadow(&I, CombinedShadow); | ||||||
1217 | return CombinedShadow; | ||||||
1218 | } | ||||||
1219 | |||||||
1220 | // Generates IR to load shadow corresponding to bytes [Addr, Addr+Size), where | ||||||
1221 | // Addr has alignment Align, and take the union of each of those shadows. | ||||||
1222 | Value *DFSanFunction::loadShadow(Value *Addr, uint64_t Size, uint64_t Align, | ||||||
1223 | Instruction *Pos) { | ||||||
1224 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) { | ||||||
1225 | const auto i = AllocaShadowMap.find(AI); | ||||||
1226 | if (i != AllocaShadowMap.end()) { | ||||||
1227 | IRBuilder<> IRB(Pos); | ||||||
1228 | return IRB.CreateLoad(DFS.ShadowTy, i->second); | ||||||
1229 | } | ||||||
1230 | } | ||||||
1231 | |||||||
1232 | const llvm::Align ShadowAlign(Align * DFS.ShadowWidthBytes); | ||||||
1233 | SmallVector<const Value *, 2> Objs; | ||||||
1234 | getUnderlyingObjects(Addr, Objs); | ||||||
1235 | bool AllConstants = true; | ||||||
1236 | for (const Value *Obj : Objs) { | ||||||
1237 | if (isa<Function>(Obj) || isa<BlockAddress>(Obj)) | ||||||
1238 | continue; | ||||||
1239 | if (isa<GlobalVariable>(Obj) && cast<GlobalVariable>(Obj)->isConstant()) | ||||||
1240 | continue; | ||||||
1241 | |||||||
1242 | AllConstants = false; | ||||||
1243 | break; | ||||||
1244 | } | ||||||
1245 | if (AllConstants) | ||||||
1246 | return DFS.ZeroShadow; | ||||||
1247 | |||||||
1248 | Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos); | ||||||
1249 | switch (Size) { | ||||||
1250 | case 0: | ||||||
1251 | return DFS.ZeroShadow; | ||||||
1252 | case 1: { | ||||||
1253 | LoadInst *LI = new LoadInst(DFS.ShadowTy, ShadowAddr, "", Pos); | ||||||
1254 | LI->setAlignment(ShadowAlign); | ||||||
1255 | return LI; | ||||||
1256 | } | ||||||
1257 | case 2: { | ||||||
1258 | IRBuilder<> IRB(Pos); | ||||||
1259 | Value *ShadowAddr1 = IRB.CreateGEP(DFS.ShadowTy, ShadowAddr, | ||||||
1260 | ConstantInt::get(DFS.IntptrTy, 1)); | ||||||
1261 | return combineShadows( | ||||||
1262 | IRB.CreateAlignedLoad(DFS.ShadowTy, ShadowAddr, ShadowAlign), | ||||||
1263 | IRB.CreateAlignedLoad(DFS.ShadowTy, ShadowAddr1, ShadowAlign), Pos); | ||||||
1264 | } | ||||||
1265 | } | ||||||
1266 | |||||||
1267 | if (ClFast16Labels && Size % (64 / DFS.ShadowWidthBits) == 0) { | ||||||
1268 | // First OR all the WideShadows, then OR individual shadows within the | ||||||
1269 | // combined WideShadow. This is fewer instructions than ORing shadows | ||||||
1270 | // individually. | ||||||
1271 | IRBuilder<> IRB(Pos); | ||||||
1272 | Value *WideAddr = | ||||||
1273 | IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx)); | ||||||
1274 | Value *CombinedWideShadow = | ||||||
1275 | IRB.CreateAlignedLoad(IRB.getInt64Ty(), WideAddr, ShadowAlign); | ||||||
1276 | for (uint64_t Ofs = 64 / DFS.ShadowWidthBits; Ofs != Size; | ||||||
1277 | Ofs += 64 / DFS.ShadowWidthBits) { | ||||||
1278 | WideAddr = IRB.CreateGEP(Type::getInt64Ty(*DFS.Ctx), WideAddr, | ||||||
1279 | ConstantInt::get(DFS.IntptrTy, 1)); | ||||||
1280 | Value *NextWideShadow = | ||||||
1281 | IRB.CreateAlignedLoad(IRB.getInt64Ty(), WideAddr, ShadowAlign); | ||||||
1282 | CombinedWideShadow = IRB.CreateOr(CombinedWideShadow, NextWideShadow); | ||||||
1283 | } | ||||||
1284 | for (unsigned Width = 32; Width >= DFS.ShadowWidthBits; Width >>= 1) { | ||||||
1285 | Value *ShrShadow = IRB.CreateLShr(CombinedWideShadow, Width); | ||||||
1286 | CombinedWideShadow = IRB.CreateOr(CombinedWideShadow, ShrShadow); | ||||||
1287 | } | ||||||
1288 | return IRB.CreateTrunc(CombinedWideShadow, DFS.ShadowTy); | ||||||
1289 | } | ||||||
1290 | if (!AvoidNewBlocks && Size % (64 / DFS.ShadowWidthBits) == 0) { | ||||||
1291 | // Fast path for the common case where each byte has identical shadow: load | ||||||
1292 | // shadow 64 bits at a time, fall out to a __dfsan_union_load call if any | ||||||
1293 | // shadow is non-equal. | ||||||
1294 | BasicBlock *FallbackBB = BasicBlock::Create(*DFS.Ctx, "", F); | ||||||
1295 | IRBuilder<> FallbackIRB(FallbackBB); | ||||||
1296 | CallInst *FallbackCall = FallbackIRB.CreateCall( | ||||||
1297 | DFS.DFSanUnionLoadFn, | ||||||
1298 | {ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size)}); | ||||||
1299 | FallbackCall->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt); | ||||||
1300 | |||||||
1301 | // Compare each of the shadows stored in the loaded 64 bits to each other, | ||||||
1302 | // by computing (WideShadow rotl ShadowWidthBits) == WideShadow. | ||||||
1303 | IRBuilder<> IRB(Pos); | ||||||
1304 | Value *WideAddr = | ||||||
1305 | IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx)); | ||||||
1306 | Value *WideShadow = | ||||||
1307 | IRB.CreateAlignedLoad(IRB.getInt64Ty(), WideAddr, ShadowAlign); | ||||||
1308 | Value *TruncShadow = IRB.CreateTrunc(WideShadow, DFS.ShadowTy); | ||||||
1309 | Value *ShlShadow = IRB.CreateShl(WideShadow, DFS.ShadowWidthBits); | ||||||
1310 | Value *ShrShadow = IRB.CreateLShr(WideShadow, 64 - DFS.ShadowWidthBits); | ||||||
1311 | Value *RotShadow = IRB.CreateOr(ShlShadow, ShrShadow); | ||||||
1312 | Value *ShadowsEq = IRB.CreateICmpEQ(WideShadow, RotShadow); | ||||||
1313 | |||||||
1314 | BasicBlock *Head = Pos->getParent(); | ||||||
1315 | BasicBlock *Tail = Head->splitBasicBlock(Pos->getIterator()); | ||||||
1316 | |||||||
1317 | if (DomTreeNode *OldNode = DT.getNode(Head)) { | ||||||
1318 | std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end()); | ||||||
1319 | |||||||
1320 | DomTreeNode *NewNode = DT.addNewBlock(Tail, Head); | ||||||
1321 | for (auto Child : Children) | ||||||
1322 | DT.changeImmediateDominator(Child, NewNode); | ||||||
1323 | } | ||||||
1324 | |||||||
1325 | // In the following code LastBr will refer to the previous basic block's | ||||||
1326 | // conditional branch instruction, whose true successor is fixed up to point | ||||||
1327 | // to the next block during the loop below or to the tail after the final | ||||||
1328 | // iteration. | ||||||
1329 | BranchInst *LastBr = BranchInst::Create(FallbackBB, FallbackBB, ShadowsEq); | ||||||
1330 | ReplaceInstWithInst(Head->getTerminator(), LastBr); | ||||||
1331 | DT.addNewBlock(FallbackBB, Head); | ||||||
1332 | |||||||
1333 | for (uint64_t Ofs = 64 / DFS.ShadowWidthBits; Ofs != Size; | ||||||
1334 | Ofs += 64 / DFS.ShadowWidthBits) { | ||||||
1335 | BasicBlock *NextBB = BasicBlock::Create(*DFS.Ctx, "", F); | ||||||
1336 | DT.addNewBlock(NextBB, LastBr->getParent()); | ||||||
1337 | IRBuilder<> NextIRB(NextBB); | ||||||
1338 | WideAddr = NextIRB.CreateGEP(Type::getInt64Ty(*DFS.Ctx), WideAddr, | ||||||
1339 | ConstantInt::get(DFS.IntptrTy, 1)); | ||||||
1340 | Value *NextWideShadow = NextIRB.CreateAlignedLoad(NextIRB.getInt64Ty(), | ||||||
1341 | WideAddr, ShadowAlign); | ||||||
1342 | ShadowsEq = NextIRB.CreateICmpEQ(WideShadow, NextWideShadow); | ||||||
1343 | LastBr->setSuccessor(0, NextBB); | ||||||
1344 | LastBr = NextIRB.CreateCondBr(ShadowsEq, FallbackBB, FallbackBB); | ||||||
1345 | } | ||||||
1346 | |||||||
1347 | LastBr->setSuccessor(0, Tail); | ||||||
1348 | FallbackIRB.CreateBr(Tail); | ||||||
1349 | PHINode *Shadow = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front()); | ||||||
1350 | Shadow->addIncoming(FallbackCall, FallbackBB); | ||||||
1351 | Shadow->addIncoming(TruncShadow, LastBr->getParent()); | ||||||
1352 | return Shadow; | ||||||
1353 | } | ||||||
1354 | |||||||
1355 | IRBuilder<> IRB(Pos); | ||||||
1356 | FunctionCallee &UnionLoadFn = | ||||||
1357 | ClFast16Labels ? DFS.DFSanUnionLoadFast16LabelsFn : DFS.DFSanUnionLoadFn; | ||||||
1358 | CallInst *FallbackCall = IRB.CreateCall( | ||||||
1359 | UnionLoadFn, {ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size)}); | ||||||
1360 | FallbackCall->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt); | ||||||
1361 | return FallbackCall; | ||||||
1362 | } | ||||||
1363 | |||||||
1364 | void DFSanVisitor::visitLoadInst(LoadInst &LI) { | ||||||
1365 | auto &DL = LI.getModule()->getDataLayout(); | ||||||
1366 | uint64_t Size = DL.getTypeStoreSize(LI.getType()); | ||||||
1367 | if (Size == 0) { | ||||||
1368 | DFSF.setShadow(&LI, DFSF.DFS.ZeroShadow); | ||||||
1369 | return; | ||||||
1370 | } | ||||||
1371 | |||||||
1372 | Align Alignment = ClPreserveAlignment ? LI.getAlign() : Align(1); | ||||||
1373 | Value *Shadow = | ||||||
1374 | DFSF.loadShadow(LI.getPointerOperand(), Size, Alignment.value(), &LI); | ||||||
1375 | if (ClCombinePointerLabelsOnLoad) { | ||||||
1376 | Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand()); | ||||||
1377 | Shadow = DFSF.combineShadows(Shadow, PtrShadow, &LI); | ||||||
1378 | } | ||||||
1379 | if (Shadow != DFSF.DFS.ZeroShadow) | ||||||
1380 | DFSF.NonZeroChecks.push_back(Shadow); | ||||||
1381 | |||||||
1382 | DFSF.setShadow(&LI, Shadow); | ||||||
1383 | if (ClEventCallbacks) { | ||||||
1384 | IRBuilder<> IRB(&LI); | ||||||
1385 | Value *Addr8 = IRB.CreateBitCast(LI.getPointerOperand(), DFSF.DFS.Int8Ptr); | ||||||
1386 | IRB.CreateCall(DFSF.DFS.DFSanLoadCallbackFn, {Shadow, Addr8}); | ||||||
1387 | } | ||||||
1388 | } | ||||||
1389 | |||||||
1390 | void DFSanFunction::storeShadow(Value *Addr, uint64_t Size, Align Alignment, | ||||||
1391 | Value *Shadow, Instruction *Pos) { | ||||||
1392 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) { | ||||||
1393 | const auto i = AllocaShadowMap.find(AI); | ||||||
1394 | if (i != AllocaShadowMap.end()) { | ||||||
1395 | IRBuilder<> IRB(Pos); | ||||||
1396 | IRB.CreateStore(Shadow, i->second); | ||||||
1397 | return; | ||||||
1398 | } | ||||||
1399 | } | ||||||
1400 | |||||||
1401 | const Align ShadowAlign(Alignment.value() * DFS.ShadowWidthBytes); | ||||||
1402 | IRBuilder<> IRB(Pos); | ||||||
1403 | Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos); | ||||||
1404 | if (Shadow == DFS.ZeroShadow) { | ||||||
1405 | IntegerType *ShadowTy = | ||||||
1406 | IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidthBits); | ||||||
1407 | Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0); | ||||||
1408 | Value *ExtShadowAddr = | ||||||
1409 | IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy)); | ||||||
1410 | IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign); | ||||||
1411 | return; | ||||||
1412 | } | ||||||
1413 | |||||||
1414 | const unsigned ShadowVecSize = 128 / DFS.ShadowWidthBits; | ||||||
1415 | uint64_t Offset = 0; | ||||||
1416 | if (Size >= ShadowVecSize) { | ||||||
1417 | auto *ShadowVecTy = FixedVectorType::get(DFS.ShadowTy, ShadowVecSize); | ||||||
1418 | Value *ShadowVec = UndefValue::get(ShadowVecTy); | ||||||
1419 | for (unsigned i = 0; i != ShadowVecSize; ++i) { | ||||||
1420 | ShadowVec = IRB.CreateInsertElement( | ||||||
1421 | ShadowVec, Shadow, ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), i)); | ||||||
1422 | } | ||||||
1423 | Value *ShadowVecAddr = | ||||||
1424 | IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy)); | ||||||
1425 | do { | ||||||
1426 | Value *CurShadowVecAddr = | ||||||
1427 | IRB.CreateConstGEP1_32(ShadowVecTy, ShadowVecAddr, Offset); | ||||||
1428 | IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign); | ||||||
1429 | Size -= ShadowVecSize; | ||||||
1430 | ++Offset; | ||||||
1431 | } while (Size >= ShadowVecSize); | ||||||
1432 | Offset *= ShadowVecSize; | ||||||
1433 | } | ||||||
1434 | while (Size > 0) { | ||||||
1435 | Value *CurShadowAddr = | ||||||
1436 | IRB.CreateConstGEP1_32(DFS.ShadowTy, ShadowAddr, Offset); | ||||||
1437 | IRB.CreateAlignedStore(Shadow, CurShadowAddr, ShadowAlign); | ||||||
1438 | --Size; | ||||||
1439 | ++Offset; | ||||||
1440 | } | ||||||
1441 | } | ||||||
1442 | |||||||
1443 | void DFSanVisitor::visitStoreInst(StoreInst &SI) { | ||||||
1444 | auto &DL = SI.getModule()->getDataLayout(); | ||||||
1445 | uint64_t Size = DL.getTypeStoreSize(SI.getValueOperand()->getType()); | ||||||
1446 | if (Size == 0) | ||||||
1447 | return; | ||||||
1448 | |||||||
1449 | const Align Alignment = ClPreserveAlignment ? SI.getAlign() : Align(1); | ||||||
1450 | |||||||
1451 | Value* Shadow = DFSF.getShadow(SI.getValueOperand()); | ||||||
1452 | if (ClCombinePointerLabelsOnStore) { | ||||||
1453 | Value *PtrShadow = DFSF.getShadow(SI.getPointerOperand()); | ||||||
1454 | Shadow = DFSF.combineShadows(Shadow, PtrShadow, &SI); | ||||||
1455 | } | ||||||
1456 | DFSF.storeShadow(SI.getPointerOperand(), Size, Alignment, Shadow, &SI); | ||||||
1457 | if (ClEventCallbacks) { | ||||||
1458 | IRBuilder<> IRB(&SI); | ||||||
1459 | Value *Addr8 = IRB.CreateBitCast(SI.getPointerOperand(), DFSF.DFS.Int8Ptr); | ||||||
1460 | IRB.CreateCall(DFSF.DFS.DFSanStoreCallbackFn, {Shadow, Addr8}); | ||||||
1461 | } | ||||||
1462 | } | ||||||
1463 | |||||||
1464 | void DFSanVisitor::visitUnaryOperator(UnaryOperator &UO) { | ||||||
1465 | visitOperandShadowInst(UO); | ||||||
1466 | } | ||||||
1467 | |||||||
1468 | void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) { | ||||||
1469 | visitOperandShadowInst(BO); | ||||||
1470 | } | ||||||
1471 | |||||||
1472 | void DFSanVisitor::visitCastInst(CastInst &CI) { visitOperandShadowInst(CI); } | ||||||
1473 | |||||||
1474 | void DFSanVisitor::visitCmpInst(CmpInst &CI) { | ||||||
1475 | Value *CombinedShadow = visitOperandShadowInst(CI); | ||||||
1476 | if (ClEventCallbacks) { | ||||||
1477 | IRBuilder<> IRB(&CI); | ||||||
1478 | IRB.CreateCall(DFSF.DFS.DFSanCmpCallbackFn, CombinedShadow); | ||||||
1479 | } | ||||||
1480 | } | ||||||
1481 | |||||||
1482 | void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) { | ||||||
1483 | visitOperandShadowInst(GEPI); | ||||||
1484 | } | ||||||
1485 | |||||||
1486 | void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) { | ||||||
1487 | visitOperandShadowInst(I); | ||||||
1488 | } | ||||||
1489 | |||||||
1490 | void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) { | ||||||
1491 | visitOperandShadowInst(I); | ||||||
1492 | } | ||||||
1493 | |||||||
1494 | void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) { | ||||||
1495 | visitOperandShadowInst(I); | ||||||
1496 | } | ||||||
1497 | |||||||
1498 | void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) { | ||||||
1499 | visitOperandShadowInst(I); | ||||||
1500 | } | ||||||
1501 | |||||||
1502 | void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) { | ||||||
1503 | visitOperandShadowInst(I); | ||||||
1504 | } | ||||||
1505 | |||||||
1506 | void DFSanVisitor::visitAllocaInst(AllocaInst &I) { | ||||||
1507 | bool AllLoadsStores = true; | ||||||
1508 | for (User *U : I.users()) { | ||||||
1509 | if (isa<LoadInst>(U)) | ||||||
1510 | continue; | ||||||
1511 | |||||||
1512 | if (StoreInst *SI = dyn_cast<StoreInst>(U)) { | ||||||
1513 | if (SI->getPointerOperand() == &I) | ||||||
1514 | continue; | ||||||
1515 | } | ||||||
1516 | |||||||
1517 | AllLoadsStores = false; | ||||||
1518 | break; | ||||||
1519 | } | ||||||
1520 | if (AllLoadsStores) { | ||||||
1521 | IRBuilder<> IRB(&I); | ||||||
1522 | DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.ShadowTy); | ||||||
1523 | } | ||||||
1524 | DFSF.setShadow(&I, DFSF.DFS.ZeroShadow); | ||||||
1525 | } | ||||||
1526 | |||||||
1527 | void DFSanVisitor::visitSelectInst(SelectInst &I) { | ||||||
1528 | Value *CondShadow = DFSF.getShadow(I.getCondition()); | ||||||
1529 | Value *TrueShadow = DFSF.getShadow(I.getTrueValue()); | ||||||
1530 | Value *FalseShadow = DFSF.getShadow(I.getFalseValue()); | ||||||
1531 | Value *ShadowSel = nullptr; | ||||||
1532 | |||||||
1533 | if (isa<VectorType>(I.getCondition()->getType())) { | ||||||
1534 | ShadowSel = DFSF.combineShadows(TrueShadow, FalseShadow, &I); | ||||||
1535 | } else { | ||||||
1536 | if (TrueShadow == FalseShadow) { | ||||||
1537 | ShadowSel = TrueShadow; | ||||||
1538 | } else { | ||||||
1539 | ShadowSel = | ||||||
1540 | SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I); | ||||||
1541 | } | ||||||
1542 | } | ||||||
1543 | DFSF.setShadow(&I, ClTrackSelectControlFlow | ||||||
1544 | ? DFSF.combineShadows(CondShadow, ShadowSel, &I) | ||||||
1545 | : ShadowSel); | ||||||
1546 | } | ||||||
1547 | |||||||
1548 | void DFSanVisitor::visitMemSetInst(MemSetInst &I) { | ||||||
1549 | IRBuilder<> IRB(&I); | ||||||
1550 | Value *ValShadow = DFSF.getShadow(I.getValue()); | ||||||
1551 | IRB.CreateCall(DFSF.DFS.DFSanSetLabelFn, | ||||||
1552 | {ValShadow, IRB.CreateBitCast(I.getDest(), Type::getInt8PtrTy( | ||||||
1553 | *DFSF.DFS.Ctx)), | ||||||
1554 | IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy)}); | ||||||
1555 | } | ||||||
1556 | |||||||
1557 | void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) { | ||||||
1558 | IRBuilder<> IRB(&I); | ||||||
1559 | Value *RawDestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I); | ||||||
1560 | Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I); | ||||||
1561 | Value *LenShadow = | ||||||
1562 | IRB.CreateMul(I.getLength(), ConstantInt::get(I.getLength()->getType(), | ||||||
1563 | DFSF.DFS.ShadowWidthBytes)); | ||||||
1564 | Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx); | ||||||
1565 | Value *DestShadow = IRB.CreateBitCast(RawDestShadow, Int8Ptr); | ||||||
1566 | SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr); | ||||||
1567 | auto *MTI = cast<MemTransferInst>( | ||||||
1568 | IRB.CreateCall(I.getFunctionType(), I.getCalledOperand(), | ||||||
1569 | {DestShadow, SrcShadow, LenShadow, I.getVolatileCst()})); | ||||||
1570 | if (ClPreserveAlignment) { | ||||||
1571 | MTI->setDestAlignment(I.getDestAlign() * DFSF.DFS.ShadowWidthBytes); | ||||||
1572 | MTI->setSourceAlignment(I.getSourceAlign() * DFSF.DFS.ShadowWidthBytes); | ||||||
1573 | } else { | ||||||
1574 | MTI->setDestAlignment(Align(DFSF.DFS.ShadowWidthBytes)); | ||||||
1575 | MTI->setSourceAlignment(Align(DFSF.DFS.ShadowWidthBytes)); | ||||||
1576 | } | ||||||
1577 | if (ClEventCallbacks) { | ||||||
1578 | IRB.CreateCall(DFSF.DFS.DFSanMemTransferCallbackFn, | ||||||
1579 | {RawDestShadow, I.getLength()}); | ||||||
1580 | } | ||||||
1581 | } | ||||||
1582 | |||||||
1583 | void DFSanVisitor::visitReturnInst(ReturnInst &RI) { | ||||||
1584 | if (!DFSF.IsNativeABI && RI.getReturnValue()) { | ||||||
1585 | switch (DFSF.IA) { | ||||||
1586 | case DataFlowSanitizer::IA_TLS: { | ||||||
1587 | Value *S = DFSF.getShadow(RI.getReturnValue()); | ||||||
1588 | IRBuilder<> IRB(&RI); | ||||||
1589 | IRB.CreateStore(S, DFSF.DFS.RetvalTLS); | ||||||
1590 | break; | ||||||
1591 | } | ||||||
1592 | case DataFlowSanitizer::IA_Args: { | ||||||
1593 | IRBuilder<> IRB(&RI); | ||||||
1594 | Type *RT = DFSF.F->getFunctionType()->getReturnType(); | ||||||
1595 | Value *InsVal = | ||||||
1596 | IRB.CreateInsertValue(UndefValue::get(RT), RI.getReturnValue(), 0); | ||||||
1597 | Value *InsShadow = | ||||||
1598 | IRB.CreateInsertValue(InsVal, DFSF.getShadow(RI.getReturnValue()), 1); | ||||||
1599 | RI.setOperand(0, InsShadow); | ||||||
1600 | break; | ||||||
1601 | } | ||||||
1602 | } | ||||||
1603 | } | ||||||
1604 | } | ||||||
1605 | |||||||
1606 | void DFSanVisitor::visitCallBase(CallBase &CB) { | ||||||
1607 | Function *F = CB.getCalledFunction(); | ||||||
1608 | if ((F && F->isIntrinsic()) || CB.isInlineAsm()) { | ||||||
1609 | visitOperandShadowInst(CB); | ||||||
1610 | return; | ||||||
1611 | } | ||||||
1612 | |||||||
1613 | // Calls to this function are synthesized in wrappers, and we shouldn't | ||||||
1614 | // instrument them. | ||||||
1615 | if (F == DFSF.DFS.DFSanVarargWrapperFn.getCallee()->stripPointerCasts()) | ||||||
1616 | return; | ||||||
1617 | |||||||
1618 | IRBuilder<> IRB(&CB); | ||||||
1619 | |||||||
1620 | DenseMap<Value *, Function *>::iterator i = | ||||||
1621 | DFSF.DFS.UnwrappedFnMap.find(CB.getCalledOperand()); | ||||||
1622 | if (i != DFSF.DFS.UnwrappedFnMap.end()) { | ||||||
1623 | Function *F = i->second; | ||||||
1624 | switch (DFSF.DFS.getWrapperKind(F)) { | ||||||
1625 | case DataFlowSanitizer::WK_Warning: | ||||||
1626 | CB.setCalledFunction(F); | ||||||
1627 | IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn, | ||||||
1628 | IRB.CreateGlobalStringPtr(F->getName())); | ||||||
1629 | DFSF.setShadow(&CB, DFSF.DFS.ZeroShadow); | ||||||
1630 | return; | ||||||
1631 | case DataFlowSanitizer::WK_Discard: | ||||||
1632 | CB.setCalledFunction(F); | ||||||
1633 | DFSF.setShadow(&CB, DFSF.DFS.ZeroShadow); | ||||||
1634 | return; | ||||||
1635 | case DataFlowSanitizer::WK_Functional: | ||||||
1636 | CB.setCalledFunction(F); | ||||||
1637 | visitOperandShadowInst(CB); | ||||||
1638 | return; | ||||||
1639 | case DataFlowSanitizer::WK_Custom: | ||||||
1640 | // Don't try to handle invokes of custom functions, it's too complicated. | ||||||
1641 | // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_ | ||||||
1642 | // wrapper. | ||||||
1643 | if (CallInst *CI = dyn_cast<CallInst>(&CB)) { | ||||||
1644 | FunctionType *FT = F->getFunctionType(); | ||||||
1645 | TransformedFunction CustomFn = DFSF.DFS.getCustomFunctionType(FT); | ||||||
1646 | std::string CustomFName = "__dfsw_"; | ||||||
1647 | CustomFName += F->getName(); | ||||||
1648 | FunctionCallee CustomF = DFSF.DFS.Mod->getOrInsertFunction( | ||||||
1649 | CustomFName, CustomFn.TransformedType); | ||||||
1650 | if (Function *CustomFn = dyn_cast<Function>(CustomF.getCallee())) { | ||||||
1651 | CustomFn->copyAttributesFrom(F); | ||||||
1652 | |||||||
1653 | // Custom functions returning non-void will write to the return label. | ||||||
1654 | if (!FT->getReturnType()->isVoidTy()) { | ||||||
1655 | CustomFn->removeAttributes(AttributeList::FunctionIndex, | ||||||
1656 | DFSF.DFS.ReadOnlyNoneAttrs); | ||||||
1657 | } | ||||||
1658 | } | ||||||
1659 | |||||||
1660 | std::vector<Value *> Args; | ||||||
1661 | |||||||
1662 | auto i = CB.arg_begin(); | ||||||
1663 | for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) { | ||||||
1664 | Type *T = (*i)->getType(); | ||||||
1665 | FunctionType *ParamFT; | ||||||
1666 | if (isa<PointerType>(T) && | ||||||
1667 | (ParamFT = dyn_cast<FunctionType>( | ||||||
1668 | cast<PointerType>(T)->getElementType()))) { | ||||||
1669 | std::string TName = "dfst"; | ||||||
1670 | TName += utostr(FT->getNumParams() - n); | ||||||
1671 | TName += "$"; | ||||||
1672 | TName += F->getName(); | ||||||
1673 | Constant *T = DFSF.DFS.getOrBuildTrampolineFunction(ParamFT, TName); | ||||||
1674 | Args.push_back(T); | ||||||
1675 | Args.push_back( | ||||||
1676 | IRB.CreateBitCast(*i, Type::getInt8PtrTy(*DFSF.DFS.Ctx))); | ||||||
1677 | } else { | ||||||
1678 | Args.push_back(*i); | ||||||
1679 | } | ||||||
1680 | } | ||||||
1681 | |||||||
1682 | i = CB.arg_begin(); | ||||||
1683 | const unsigned ShadowArgStart = Args.size(); | ||||||
1684 | for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) | ||||||
1685 | Args.push_back(DFSF.getShadow(*i)); | ||||||
1686 | |||||||
1687 | if (FT->isVarArg()) { | ||||||
1688 | auto *LabelVATy = ArrayType::get(DFSF.DFS.ShadowTy, | ||||||
1689 | CB.arg_size() - FT->getNumParams()); | ||||||
1690 | auto *LabelVAAlloca = new AllocaInst( | ||||||
1691 | LabelVATy, getDataLayout().getAllocaAddrSpace(), | ||||||
1692 | "labelva", &DFSF.F->getEntryBlock().front()); | ||||||
1693 | |||||||
1694 | for (unsigned n = 0; i != CB.arg_end(); ++i, ++n) { | ||||||
1695 | auto LabelVAPtr = IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, n); | ||||||
1696 | IRB.CreateStore(DFSF.getShadow(*i), LabelVAPtr); | ||||||
1697 | } | ||||||
1698 | |||||||
1699 | Args.push_back(IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, 0)); | ||||||
1700 | } | ||||||
1701 | |||||||
1702 | if (!FT->getReturnType()->isVoidTy()) { | ||||||
1703 | if (!DFSF.LabelReturnAlloca) { | ||||||
1704 | DFSF.LabelReturnAlloca = | ||||||
1705 | new AllocaInst(DFSF.DFS.ShadowTy, | ||||||
1706 | getDataLayout().getAllocaAddrSpace(), | ||||||
1707 | "labelreturn", &DFSF.F->getEntryBlock().front()); | ||||||
1708 | } | ||||||
1709 | Args.push_back(DFSF.LabelReturnAlloca); | ||||||
1710 | } | ||||||
1711 | |||||||
1712 | for (i = CB.arg_begin() + FT->getNumParams(); i != CB.arg_end(); ++i) | ||||||
1713 | Args.push_back(*i); | ||||||
1714 | |||||||
1715 | CallInst *CustomCI = IRB.CreateCall(CustomF, Args); | ||||||
1716 | CustomCI->setCallingConv(CI->getCallingConv()); | ||||||
1717 | CustomCI->setAttributes(TransformFunctionAttributes(CustomFn, | ||||||
1718 | CI->getContext(), CI->getAttributes())); | ||||||
1719 | |||||||
1720 | // Update the parameter attributes of the custom call instruction to | ||||||
1721 | // zero extend the shadow parameters. This is required for targets | ||||||
1722 | // which consider ShadowTy an illegal type. | ||||||
1723 | for (unsigned n = 0; n < FT->getNumParams(); n++) { | ||||||
1724 | const unsigned ArgNo = ShadowArgStart + n; | ||||||
1725 | if (CustomCI->getArgOperand(ArgNo)->getType() == DFSF.DFS.ShadowTy) | ||||||
1726 | CustomCI->addParamAttr(ArgNo, Attribute::ZExt); | ||||||
1727 | } | ||||||
1728 | |||||||
1729 | if (!FT->getReturnType()->isVoidTy()) { | ||||||
1730 | LoadInst *LabelLoad = | ||||||
1731 | IRB.CreateLoad(DFSF.DFS.ShadowTy, DFSF.LabelReturnAlloca); | ||||||
1732 | DFSF.setShadow(CustomCI, LabelLoad); | ||||||
1733 | } | ||||||
1734 | |||||||
1735 | CI->replaceAllUsesWith(CustomCI); | ||||||
1736 | CI->eraseFromParent(); | ||||||
1737 | return; | ||||||
1738 | } | ||||||
1739 | break; | ||||||
1740 | } | ||||||
1741 | } | ||||||
1742 | |||||||
1743 | FunctionType *FT = CB.getFunctionType(); | ||||||
1744 | if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) { | ||||||
1745 | for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) { | ||||||
1746 | IRB.CreateStore(DFSF.getShadow(CB.getArgOperand(i)), | ||||||
1747 | DFSF.getArgTLS(i, &CB)); | ||||||
1748 | } | ||||||
1749 | } | ||||||
1750 | |||||||
1751 | Instruction *Next = nullptr; | ||||||
1752 | if (!CB.getType()->isVoidTy()) { | ||||||
1753 | if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) { | ||||||
1754 | if (II->getNormalDest()->getSinglePredecessor()) { | ||||||
1755 | Next = &II->getNormalDest()->front(); | ||||||
1756 | } else { | ||||||
1757 | BasicBlock *NewBB = | ||||||
1758 | SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DT); | ||||||
1759 | Next = &NewBB->front(); | ||||||
1760 | } | ||||||
1761 | } else { | ||||||
1762 | assert(CB.getIterator() != CB.getParent()->end())((CB.getIterator() != CB.getParent()->end()) ? static_cast <void> (0) : __assert_fail ("CB.getIterator() != CB.getParent()->end()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp" , 1762, __PRETTY_FUNCTION__)); | ||||||
1763 | Next = CB.getNextNode(); | ||||||
1764 | } | ||||||
1765 | |||||||
1766 | if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) { | ||||||
1767 | IRBuilder<> NextIRB(Next); | ||||||
1768 | LoadInst *LI = NextIRB.CreateLoad(DFSF.DFS.ShadowTy, DFSF.DFS.RetvalTLS); | ||||||
1769 | DFSF.SkipInsts.insert(LI); | ||||||
1770 | DFSF.setShadow(&CB, LI); | ||||||
1771 | DFSF.NonZeroChecks.push_back(LI); | ||||||
1772 | } | ||||||
1773 | } | ||||||
1774 | |||||||
1775 | // Do all instrumentation for IA_Args down here to defer tampering with the | ||||||
1776 | // CFG in a way that SplitEdge may be able to detect. | ||||||
1777 | if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) { | ||||||
1778 | FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT); | ||||||
1779 | Value *Func = | ||||||
1780 | IRB.CreateBitCast(CB.getCalledOperand(), PointerType::getUnqual(NewFT)); | ||||||
1781 | std::vector<Value *> Args; | ||||||
1782 | |||||||
1783 | auto i = CB.arg_begin(), E = CB.arg_end(); | ||||||
1784 | for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) | ||||||
1785 | Args.push_back(*i); | ||||||
1786 | |||||||
1787 | i = CB.arg_begin(); | ||||||
1788 | for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) | ||||||
1789 | Args.push_back(DFSF.getShadow(*i)); | ||||||
1790 | |||||||
1791 | if (FT->isVarArg()) { | ||||||
1792 | unsigned VarArgSize = CB.arg_size() - FT->getNumParams(); | ||||||
1793 | ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize); | ||||||
1794 | AllocaInst *VarArgShadow = | ||||||
1795 | new AllocaInst(VarArgArrayTy, getDataLayout().getAllocaAddrSpace(), | ||||||
1796 | "", &DFSF.F->getEntryBlock().front()); | ||||||
1797 | Args.push_back(IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, 0)); | ||||||
1798 | for (unsigned n = 0; i != E; ++i, ++n) { | ||||||
1799 | IRB.CreateStore( | ||||||
1800 | DFSF.getShadow(*i), | ||||||
1801 | IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, n)); | ||||||
1802 | Args.push_back(*i); | ||||||
1803 | } | ||||||
1804 | } | ||||||
1805 | |||||||
1806 | CallBase *NewCB; | ||||||
1807 | if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) { | ||||||
1808 | NewCB = IRB.CreateInvoke(NewFT, Func, II->getNormalDest(), | ||||||
1809 | II->getUnwindDest(), Args); | ||||||
1810 | } else { | ||||||
1811 | NewCB = IRB.CreateCall(NewFT, Func, Args); | ||||||
1812 | } | ||||||
1813 | NewCB->setCallingConv(CB.getCallingConv()); | ||||||
1814 | NewCB->setAttributes(CB.getAttributes().removeAttributes( | ||||||
1815 | *DFSF.DFS.Ctx, AttributeList::ReturnIndex, | ||||||
1816 | AttributeFuncs::typeIncompatible(NewCB->getType()))); | ||||||
1817 | |||||||
1818 | if (Next) { | ||||||
1819 | ExtractValueInst *ExVal = ExtractValueInst::Create(NewCB, 0, "", Next); | ||||||
1820 | DFSF.SkipInsts.insert(ExVal); | ||||||
1821 | ExtractValueInst *ExShadow = ExtractValueInst::Create(NewCB, 1, "", Next); | ||||||
1822 | DFSF.SkipInsts.insert(ExShadow); | ||||||
1823 | DFSF.setShadow(ExVal, ExShadow); | ||||||
1824 | DFSF.NonZeroChecks.push_back(ExShadow); | ||||||
1825 | |||||||
1826 | CB.replaceAllUsesWith(ExVal); | ||||||
1827 | } | ||||||
1828 | |||||||
1829 | CB.eraseFromParent(); | ||||||
1830 | } | ||||||
1831 | } | ||||||
1832 | |||||||
1833 | void DFSanVisitor::visitPHINode(PHINode &PN) { | ||||||
1834 | PHINode *ShadowPN = | ||||||
1835 | PHINode::Create(DFSF.DFS.ShadowTy, PN.getNumIncomingValues(), "", &PN); | ||||||
1836 | |||||||
1837 | // Give the shadow phi node valid predecessors to fool SplitEdge into working. | ||||||
1838 | Value *UndefShadow = UndefValue::get(DFSF.DFS.ShadowTy); | ||||||
1839 | for (PHINode::block_iterator i = PN.block_begin(), e = PN.block_end(); i != e; | ||||||
1840 | ++i) { | ||||||
1841 | ShadowPN->addIncoming(UndefShadow, *i); | ||||||
1842 | } | ||||||
1843 | |||||||
1844 | DFSF.PHIFixups.push_back(std::make_pair(&PN, ShadowPN)); | ||||||
1845 | DFSF.setShadow(&PN, ShadowPN); | ||||||
1846 | } | ||||||
1847 | |||||||
1848 | namespace { | ||||||
1849 | class DataFlowSanitizerLegacyPass : public ModulePass { | ||||||
1850 | private: | ||||||
1851 | std::vector<std::string> ABIListFiles; | ||||||
1852 | |||||||
1853 | public: | ||||||
1854 | static char ID; | ||||||
1855 | |||||||
1856 | DataFlowSanitizerLegacyPass( | ||||||
1857 | const std::vector<std::string> &ABIListFiles = std::vector<std::string>()) | ||||||
1858 | : ModulePass(ID), ABIListFiles(ABIListFiles) {} | ||||||
1859 | |||||||
1860 | bool runOnModule(Module &M) override { | ||||||
1861 | return DataFlowSanitizer(ABIListFiles).runImpl(M); | ||||||
1862 | } | ||||||
1863 | }; | ||||||
1864 | } // namespace | ||||||
1865 | |||||||
1866 | char DataFlowSanitizerLegacyPass::ID; | ||||||
1867 | |||||||
1868 | INITIALIZE_PASS(DataFlowSanitizerLegacyPass, "dfsan",static void *initializeDataFlowSanitizerLegacyPassPassOnce(PassRegistry &Registry) { PassInfo *PI = new PassInfo( "DataFlowSanitizer: dynamic data flow analysis." , "dfsan", &DataFlowSanitizerLegacyPass::ID, PassInfo::NormalCtor_t (callDefaultCtor<DataFlowSanitizerLegacyPass>), false, false ); Registry.registerPass(*PI, true); return PI; } static llvm ::once_flag InitializeDataFlowSanitizerLegacyPassPassFlag; void llvm::initializeDataFlowSanitizerLegacyPassPass(PassRegistry &Registry) { llvm::call_once(InitializeDataFlowSanitizerLegacyPassPassFlag , initializeDataFlowSanitizerLegacyPassPassOnce, std::ref(Registry )); } | ||||||
1869 | "DataFlowSanitizer: dynamic data flow analysis.", false, false)static void *initializeDataFlowSanitizerLegacyPassPassOnce(PassRegistry &Registry) { PassInfo *PI = new PassInfo( "DataFlowSanitizer: dynamic data flow analysis." , "dfsan", &DataFlowSanitizerLegacyPass::ID, PassInfo::NormalCtor_t (callDefaultCtor<DataFlowSanitizerLegacyPass>), false, false ); Registry.registerPass(*PI, true); return PI; } static llvm ::once_flag InitializeDataFlowSanitizerLegacyPassPassFlag; void llvm::initializeDataFlowSanitizerLegacyPassPass(PassRegistry &Registry) { llvm::call_once(InitializeDataFlowSanitizerLegacyPassPassFlag , initializeDataFlowSanitizerLegacyPassPassOnce, std::ref(Registry )); } | ||||||
1870 | |||||||
1871 | ModulePass *llvm::createDataFlowSanitizerLegacyPassPass( | ||||||
1872 | const std::vector<std::string> &ABIListFiles) { | ||||||
1873 | return new DataFlowSanitizerLegacyPass(ABIListFiles); | ||||||
1874 | } | ||||||
1875 | |||||||
1876 | PreservedAnalyses DataFlowSanitizerPass::run(Module &M, | ||||||
1877 | ModuleAnalysisManager &AM) { | ||||||
1878 | if (DataFlowSanitizer(ABIListFiles).runImpl(M)) { | ||||||
1879 | return PreservedAnalyses::none(); | ||||||
1880 | } | ||||||
1881 | return PreservedAnalyses::all(); | ||||||
1882 | } |
1 | //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines the IRBuilder class, which is used as a convenient way |
10 | // to create LLVM instructions with a consistent and simplified interface. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_IR_IRBUILDER_H |
15 | #define LLVM_IR_IRBUILDER_H |
16 | |
17 | #include "llvm-c/Types.h" |
18 | #include "llvm/ADT/ArrayRef.h" |
19 | #include "llvm/ADT/None.h" |
20 | #include "llvm/ADT/StringRef.h" |
21 | #include "llvm/ADT/Twine.h" |
22 | #include "llvm/IR/BasicBlock.h" |
23 | #include "llvm/IR/Constant.h" |
24 | #include "llvm/IR/ConstantFolder.h" |
25 | #include "llvm/IR/Constants.h" |
26 | #include "llvm/IR/DataLayout.h" |
27 | #include "llvm/IR/DebugLoc.h" |
28 | #include "llvm/IR/DerivedTypes.h" |
29 | #include "llvm/IR/Function.h" |
30 | #include "llvm/IR/GlobalVariable.h" |
31 | #include "llvm/IR/InstrTypes.h" |
32 | #include "llvm/IR/Instruction.h" |
33 | #include "llvm/IR/Instructions.h" |
34 | #include "llvm/IR/IntrinsicInst.h" |
35 | #include "llvm/IR/LLVMContext.h" |
36 | #include "llvm/IR/Module.h" |
37 | #include "llvm/IR/Operator.h" |
38 | #include "llvm/IR/Type.h" |
39 | #include "llvm/IR/Value.h" |
40 | #include "llvm/IR/ValueHandle.h" |
41 | #include "llvm/Support/AtomicOrdering.h" |
42 | #include "llvm/Support/CBindingWrapping.h" |
43 | #include "llvm/Support/Casting.h" |
44 | #include <cassert> |
45 | #include <cstddef> |
46 | #include <cstdint> |
47 | #include <functional> |
48 | #include <utility> |
49 | |
50 | namespace llvm { |
51 | |
52 | class APInt; |
53 | class MDNode; |
54 | class Use; |
55 | |
56 | /// This provides the default implementation of the IRBuilder |
57 | /// 'InsertHelper' method that is called whenever an instruction is created by |
58 | /// IRBuilder and needs to be inserted. |
59 | /// |
60 | /// By default, this inserts the instruction at the insertion point. |
61 | class IRBuilderDefaultInserter { |
62 | public: |
63 | virtual ~IRBuilderDefaultInserter(); |
64 | |
65 | virtual void InsertHelper(Instruction *I, const Twine &Name, |
66 | BasicBlock *BB, |
67 | BasicBlock::iterator InsertPt) const { |
68 | if (BB) BB->getInstList().insert(InsertPt, I); |
69 | I->setName(Name); |
70 | } |
71 | }; |
72 | |
73 | /// Provides an 'InsertHelper' that calls a user-provided callback after |
74 | /// performing the default insertion. |
75 | class IRBuilderCallbackInserter : public IRBuilderDefaultInserter { |
76 | std::function<void(Instruction *)> Callback; |
77 | |
78 | public: |
79 | virtual ~IRBuilderCallbackInserter(); |
80 | |
81 | IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback) |
82 | : Callback(std::move(Callback)) {} |
83 | |
84 | void InsertHelper(Instruction *I, const Twine &Name, |
85 | BasicBlock *BB, |
86 | BasicBlock::iterator InsertPt) const override { |
87 | IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt); |
88 | Callback(I); |
89 | } |
90 | }; |
91 | |
92 | /// Common base class shared among various IRBuilders. |
93 | class IRBuilderBase { |
94 | DebugLoc CurDbgLocation; |
95 | |
96 | protected: |
97 | BasicBlock *BB; |
98 | BasicBlock::iterator InsertPt; |
99 | LLVMContext &Context; |
100 | const IRBuilderFolder &Folder; |
101 | const IRBuilderDefaultInserter &Inserter; |
102 | |
103 | MDNode *DefaultFPMathTag; |
104 | FastMathFlags FMF; |
105 | |
106 | bool IsFPConstrained; |
107 | fp::ExceptionBehavior DefaultConstrainedExcept; |
108 | RoundingMode DefaultConstrainedRounding; |
109 | |
110 | ArrayRef<OperandBundleDef> DefaultOperandBundles; |
111 | |
112 | public: |
113 | IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder, |
114 | const IRBuilderDefaultInserter &Inserter, |
115 | MDNode *FPMathTag, ArrayRef<OperandBundleDef> OpBundles) |
116 | : Context(context), Folder(Folder), Inserter(Inserter), |
117 | DefaultFPMathTag(FPMathTag), IsFPConstrained(false), |
118 | DefaultConstrainedExcept(fp::ebStrict), |
119 | DefaultConstrainedRounding(RoundingMode::Dynamic), |
120 | DefaultOperandBundles(OpBundles) { |
121 | ClearInsertionPoint(); |
122 | } |
123 | |
124 | /// Insert and return the specified instruction. |
125 | template<typename InstTy> |
126 | InstTy *Insert(InstTy *I, const Twine &Name = "") const { |
127 | Inserter.InsertHelper(I, Name, BB, InsertPt); |
128 | SetInstDebugLocation(I); |
129 | return I; |
130 | } |
131 | |
132 | /// No-op overload to handle constants. |
133 | Constant *Insert(Constant *C, const Twine& = "") const { |
134 | return C; |
135 | } |
136 | |
137 | Value *Insert(Value *V, const Twine &Name = "") const { |
138 | if (Instruction *I = dyn_cast<Instruction>(V)) |
139 | return Insert(I, Name); |
140 | assert(isa<Constant>(V))((isa<Constant>(V)) ? static_cast<void> (0) : __assert_fail ("isa<Constant>(V)", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 140, __PRETTY_FUNCTION__)); |
141 | return V; |
142 | } |
143 | |
144 | //===--------------------------------------------------------------------===// |
145 | // Builder configuration methods |
146 | //===--------------------------------------------------------------------===// |
147 | |
148 | /// Clear the insertion point: created instructions will not be |
149 | /// inserted into a block. |
150 | void ClearInsertionPoint() { |
151 | BB = nullptr; |
152 | InsertPt = BasicBlock::iterator(); |
153 | } |
154 | |
155 | BasicBlock *GetInsertBlock() const { return BB; } |
156 | BasicBlock::iterator GetInsertPoint() const { return InsertPt; } |
157 | LLVMContext &getContext() const { return Context; } |
158 | |
159 | /// This specifies that created instructions should be appended to the |
160 | /// end of the specified block. |
161 | void SetInsertPoint(BasicBlock *TheBB) { |
162 | BB = TheBB; |
163 | InsertPt = BB->end(); |
164 | } |
165 | |
166 | /// This specifies that created instructions should be inserted before |
167 | /// the specified instruction. |
168 | void SetInsertPoint(Instruction *I) { |
169 | BB = I->getParent(); |
170 | InsertPt = I->getIterator(); |
171 | assert(InsertPt != BB->end() && "Can't read debug loc from end()")((InsertPt != BB->end() && "Can't read debug loc from end()" ) ? static_cast<void> (0) : __assert_fail ("InsertPt != BB->end() && \"Can't read debug loc from end()\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 171, __PRETTY_FUNCTION__)); |
172 | SetCurrentDebugLocation(I->getDebugLoc()); |
173 | } |
174 | |
175 | /// This specifies that created instructions should be inserted at the |
176 | /// specified point. |
177 | void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) { |
178 | BB = TheBB; |
179 | InsertPt = IP; |
180 | if (IP != TheBB->end()) |
181 | SetCurrentDebugLocation(IP->getDebugLoc()); |
182 | } |
183 | |
184 | /// Set location information used by debugging information. |
185 | void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); } |
186 | |
187 | /// Get location information used by debugging information. |
188 | const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; } |
189 | |
190 | /// If this builder has a current debug location, set it on the |
191 | /// specified instruction. |
192 | void SetInstDebugLocation(Instruction *I) const { |
193 | if (CurDbgLocation) |
194 | I->setDebugLoc(CurDbgLocation); |
195 | } |
196 | |
197 | /// Get the return type of the current function that we're emitting |
198 | /// into. |
199 | Type *getCurrentFunctionReturnType() const; |
200 | |
201 | /// InsertPoint - A saved insertion point. |
202 | class InsertPoint { |
203 | BasicBlock *Block = nullptr; |
204 | BasicBlock::iterator Point; |
205 | |
206 | public: |
207 | /// Creates a new insertion point which doesn't point to anything. |
208 | InsertPoint() = default; |
209 | |
210 | /// Creates a new insertion point at the given location. |
211 | InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint) |
212 | : Block(InsertBlock), Point(InsertPoint) {} |
213 | |
214 | /// Returns true if this insert point is set. |
215 | bool isSet() const { return (Block != nullptr); } |
216 | |
217 | BasicBlock *getBlock() const { return Block; } |
218 | BasicBlock::iterator getPoint() const { return Point; } |
219 | }; |
220 | |
221 | /// Returns the current insert point. |
222 | InsertPoint saveIP() const { |
223 | return InsertPoint(GetInsertBlock(), GetInsertPoint()); |
224 | } |
225 | |
226 | /// Returns the current insert point, clearing it in the process. |
227 | InsertPoint saveAndClearIP() { |
228 | InsertPoint IP(GetInsertBlock(), GetInsertPoint()); |
229 | ClearInsertionPoint(); |
230 | return IP; |
231 | } |
232 | |
233 | /// Sets the current insert point to a previously-saved location. |
234 | void restoreIP(InsertPoint IP) { |
235 | if (IP.isSet()) |
236 | SetInsertPoint(IP.getBlock(), IP.getPoint()); |
237 | else |
238 | ClearInsertionPoint(); |
239 | } |
240 | |
241 | /// Get the floating point math metadata being used. |
242 | MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; } |
243 | |
244 | /// Get the flags to be applied to created floating point ops |
245 | FastMathFlags getFastMathFlags() const { return FMF; } |
246 | |
247 | FastMathFlags &getFastMathFlags() { return FMF; } |
248 | |
249 | /// Clear the fast-math flags. |
250 | void clearFastMathFlags() { FMF.clear(); } |
251 | |
252 | /// Set the floating point math metadata to be used. |
253 | void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; } |
254 | |
255 | /// Set the fast-math flags to be used with generated fp-math operators |
256 | void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; } |
257 | |
258 | /// Enable/Disable use of constrained floating point math. When |
259 | /// enabled the CreateF<op>() calls instead create constrained |
260 | /// floating point intrinsic calls. Fast math flags are unaffected |
261 | /// by this setting. |
262 | void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; } |
263 | |
264 | /// Query for the use of constrained floating point math |
265 | bool getIsFPConstrained() { return IsFPConstrained; } |
266 | |
267 | /// Set the exception handling to be used with constrained floating point |
268 | void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) { |
269 | #ifndef NDEBUG |
270 | Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(NewExcept); |
271 | assert(ExceptStr.hasValue() && "Garbage strict exception behavior!")((ExceptStr.hasValue() && "Garbage strict exception behavior!" ) ? static_cast<void> (0) : __assert_fail ("ExceptStr.hasValue() && \"Garbage strict exception behavior!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 271, __PRETTY_FUNCTION__)); |
272 | #endif |
273 | DefaultConstrainedExcept = NewExcept; |
274 | } |
275 | |
276 | /// Set the rounding mode handling to be used with constrained floating point |
277 | void setDefaultConstrainedRounding(RoundingMode NewRounding) { |
278 | #ifndef NDEBUG |
279 | Optional<StringRef> RoundingStr = RoundingModeToStr(NewRounding); |
280 | assert(RoundingStr.hasValue() && "Garbage strict rounding mode!")((RoundingStr.hasValue() && "Garbage strict rounding mode!" ) ? static_cast<void> (0) : __assert_fail ("RoundingStr.hasValue() && \"Garbage strict rounding mode!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 280, __PRETTY_FUNCTION__)); |
281 | #endif |
282 | DefaultConstrainedRounding = NewRounding; |
283 | } |
284 | |
285 | /// Get the exception handling used with constrained floating point |
286 | fp::ExceptionBehavior getDefaultConstrainedExcept() { |
287 | return DefaultConstrainedExcept; |
288 | } |
289 | |
290 | /// Get the rounding mode handling used with constrained floating point |
291 | RoundingMode getDefaultConstrainedRounding() { |
292 | return DefaultConstrainedRounding; |
293 | } |
294 | |
295 | void setConstrainedFPFunctionAttr() { |
296 | assert(BB && "Must have a basic block to set any function attributes!")((BB && "Must have a basic block to set any function attributes!" ) ? static_cast<void> (0) : __assert_fail ("BB && \"Must have a basic block to set any function attributes!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 296, __PRETTY_FUNCTION__)); |
297 | |
298 | Function *F = BB->getParent(); |
299 | if (!F->hasFnAttribute(Attribute::StrictFP)) { |
300 | F->addFnAttr(Attribute::StrictFP); |
301 | } |
302 | } |
303 | |
304 | void setConstrainedFPCallAttr(CallInst *I) { |
305 | I->addAttribute(AttributeList::FunctionIndex, Attribute::StrictFP); |
306 | } |
307 | |
308 | void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) { |
309 | DefaultOperandBundles = OpBundles; |
310 | } |
311 | |
312 | //===--------------------------------------------------------------------===// |
313 | // RAII helpers. |
314 | //===--------------------------------------------------------------------===// |
315 | |
316 | // RAII object that stores the current insertion point and restores it |
317 | // when the object is destroyed. This includes the debug location. |
318 | class InsertPointGuard { |
319 | IRBuilderBase &Builder; |
320 | AssertingVH<BasicBlock> Block; |
321 | BasicBlock::iterator Point; |
322 | DebugLoc DbgLoc; |
323 | |
324 | public: |
325 | InsertPointGuard(IRBuilderBase &B) |
326 | : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()), |
327 | DbgLoc(B.getCurrentDebugLocation()) {} |
328 | |
329 | InsertPointGuard(const InsertPointGuard &) = delete; |
330 | InsertPointGuard &operator=(const InsertPointGuard &) = delete; |
331 | |
332 | ~InsertPointGuard() { |
333 | Builder.restoreIP(InsertPoint(Block, Point)); |
334 | Builder.SetCurrentDebugLocation(DbgLoc); |
335 | } |
336 | }; |
337 | |
338 | // RAII object that stores the current fast math settings and restores |
339 | // them when the object is destroyed. |
340 | class FastMathFlagGuard { |
341 | IRBuilderBase &Builder; |
342 | FastMathFlags FMF; |
343 | MDNode *FPMathTag; |
344 | bool IsFPConstrained; |
345 | fp::ExceptionBehavior DefaultConstrainedExcept; |
346 | RoundingMode DefaultConstrainedRounding; |
347 | |
348 | public: |
349 | FastMathFlagGuard(IRBuilderBase &B) |
350 | : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag), |
351 | IsFPConstrained(B.IsFPConstrained), |
352 | DefaultConstrainedExcept(B.DefaultConstrainedExcept), |
353 | DefaultConstrainedRounding(B.DefaultConstrainedRounding) {} |
354 | |
355 | FastMathFlagGuard(const FastMathFlagGuard &) = delete; |
356 | FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete; |
357 | |
358 | ~FastMathFlagGuard() { |
359 | Builder.FMF = FMF; |
360 | Builder.DefaultFPMathTag = FPMathTag; |
361 | Builder.IsFPConstrained = IsFPConstrained; |
362 | Builder.DefaultConstrainedExcept = DefaultConstrainedExcept; |
363 | Builder.DefaultConstrainedRounding = DefaultConstrainedRounding; |
364 | } |
365 | }; |
366 | |
367 | // RAII object that stores the current default operand bundles and restores |
368 | // them when the object is destroyed. |
369 | class OperandBundlesGuard { |
370 | IRBuilderBase &Builder; |
371 | ArrayRef<OperandBundleDef> DefaultOperandBundles; |
372 | |
373 | public: |
374 | OperandBundlesGuard(IRBuilderBase &B) |
375 | : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {} |
376 | |
377 | OperandBundlesGuard(const OperandBundlesGuard &) = delete; |
378 | OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete; |
379 | |
380 | ~OperandBundlesGuard() { |
381 | Builder.DefaultOperandBundles = DefaultOperandBundles; |
382 | } |
383 | }; |
384 | |
385 | |
386 | //===--------------------------------------------------------------------===// |
387 | // Miscellaneous creation methods. |
388 | //===--------------------------------------------------------------------===// |
389 | |
390 | /// Make a new global variable with initializer type i8* |
391 | /// |
392 | /// Make a new global variable with an initializer that has array of i8 type |
393 | /// filled in with the null terminated string value specified. The new global |
394 | /// variable will be marked mergable with any others of the same contents. If |
395 | /// Name is specified, it is the name of the global variable created. |
396 | /// |
397 | /// If no module is given via \p M, it is take from the insertion point basic |
398 | /// block. |
399 | GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "", |
400 | unsigned AddressSpace = 0, |
401 | Module *M = nullptr); |
402 | |
403 | /// Get a constant value representing either true or false. |
404 | ConstantInt *getInt1(bool V) { |
405 | return ConstantInt::get(getInt1Ty(), V); |
406 | } |
407 | |
408 | /// Get the constant value for i1 true. |
409 | ConstantInt *getTrue() { |
410 | return ConstantInt::getTrue(Context); |
411 | } |
412 | |
413 | /// Get the constant value for i1 false. |
414 | ConstantInt *getFalse() { |
415 | return ConstantInt::getFalse(Context); |
416 | } |
417 | |
418 | /// Get a constant 8-bit value. |
419 | ConstantInt *getInt8(uint8_t C) { |
420 | return ConstantInt::get(getInt8Ty(), C); |
421 | } |
422 | |
423 | /// Get a constant 16-bit value. |
424 | ConstantInt *getInt16(uint16_t C) { |
425 | return ConstantInt::get(getInt16Ty(), C); |
426 | } |
427 | |
428 | /// Get a constant 32-bit value. |
429 | ConstantInt *getInt32(uint32_t C) { |
430 | return ConstantInt::get(getInt32Ty(), C); |
431 | } |
432 | |
433 | /// Get a constant 64-bit value. |
434 | ConstantInt *getInt64(uint64_t C) { |
435 | return ConstantInt::get(getInt64Ty(), C); |
436 | } |
437 | |
438 | /// Get a constant N-bit value, zero extended or truncated from |
439 | /// a 64-bit value. |
440 | ConstantInt *getIntN(unsigned N, uint64_t C) { |
441 | return ConstantInt::get(getIntNTy(N), C); |
442 | } |
443 | |
444 | /// Get a constant integer value. |
445 | ConstantInt *getInt(const APInt &AI) { |
446 | return ConstantInt::get(Context, AI); |
447 | } |
448 | |
449 | //===--------------------------------------------------------------------===// |
450 | // Type creation methods |
451 | //===--------------------------------------------------------------------===// |
452 | |
453 | /// Fetch the type representing a single bit |
454 | IntegerType *getInt1Ty() { |
455 | return Type::getInt1Ty(Context); |
456 | } |
457 | |
458 | /// Fetch the type representing an 8-bit integer. |
459 | IntegerType *getInt8Ty() { |
460 | return Type::getInt8Ty(Context); |
461 | } |
462 | |
463 | /// Fetch the type representing a 16-bit integer. |
464 | IntegerType *getInt16Ty() { |
465 | return Type::getInt16Ty(Context); |
466 | } |
467 | |
468 | /// Fetch the type representing a 32-bit integer. |
469 | IntegerType *getInt32Ty() { |
470 | return Type::getInt32Ty(Context); |
471 | } |
472 | |
473 | /// Fetch the type representing a 64-bit integer. |
474 | IntegerType *getInt64Ty() { |
475 | return Type::getInt64Ty(Context); |
476 | } |
477 | |
478 | /// Fetch the type representing a 128-bit integer. |
479 | IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); } |
480 | |
481 | /// Fetch the type representing an N-bit integer. |
482 | IntegerType *getIntNTy(unsigned N) { |
483 | return Type::getIntNTy(Context, N); |
484 | } |
485 | |
486 | /// Fetch the type representing a 16-bit floating point value. |
487 | Type *getHalfTy() { |
488 | return Type::getHalfTy(Context); |
489 | } |
490 | |
491 | /// Fetch the type representing a 16-bit brain floating point value. |
492 | Type *getBFloatTy() { |
493 | return Type::getBFloatTy(Context); |
494 | } |
495 | |
496 | /// Fetch the type representing a 32-bit floating point value. |
497 | Type *getFloatTy() { |
498 | return Type::getFloatTy(Context); |
499 | } |
500 | |
501 | /// Fetch the type representing a 64-bit floating point value. |
502 | Type *getDoubleTy() { |
503 | return Type::getDoubleTy(Context); |
504 | } |
505 | |
506 | /// Fetch the type representing void. |
507 | Type *getVoidTy() { |
508 | return Type::getVoidTy(Context); |
509 | } |
510 | |
511 | /// Fetch the type representing a pointer to an 8-bit integer value. |
512 | PointerType *getInt8PtrTy(unsigned AddrSpace = 0) { |
513 | return Type::getInt8PtrTy(Context, AddrSpace); |
514 | } |
515 | |
516 | /// Fetch the type representing a pointer to an integer value. |
517 | IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) { |
518 | return DL.getIntPtrType(Context, AddrSpace); |
519 | } |
520 | |
521 | //===--------------------------------------------------------------------===// |
522 | // Intrinsic creation methods |
523 | //===--------------------------------------------------------------------===// |
524 | |
525 | /// Create and insert a memset to the specified pointer and the |
526 | /// specified value. |
527 | /// |
528 | /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is |
529 | /// specified, it will be added to the instruction. Likewise with alias.scope |
530 | /// and noalias tags. |
531 | CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, |
532 | MaybeAlign Align, bool isVolatile = false, |
533 | MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr, |
534 | MDNode *NoAliasTag = nullptr) { |
535 | return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, |
536 | TBAATag, ScopeTag, NoAliasTag); |
537 | } |
538 | |
539 | CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align, |
540 | bool isVolatile = false, MDNode *TBAATag = nullptr, |
541 | MDNode *ScopeTag = nullptr, |
542 | MDNode *NoAliasTag = nullptr); |
543 | |
544 | /// Create and insert an element unordered-atomic memset of the region of |
545 | /// memory starting at the given pointer to the given value. |
546 | /// |
547 | /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is |
548 | /// specified, it will be added to the instruction. Likewise with alias.scope |
549 | /// and noalias tags. |
550 | CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, |
551 | uint64_t Size, Align Alignment, |
552 | uint32_t ElementSize, |
553 | MDNode *TBAATag = nullptr, |
554 | MDNode *ScopeTag = nullptr, |
555 | MDNode *NoAliasTag = nullptr) { |
556 | return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), |
557 | Align(Alignment), ElementSize, |
558 | TBAATag, ScopeTag, NoAliasTag); |
559 | } |
560 | |
561 | CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val, |
562 | Value *Size, Align Alignment, |
563 | uint32_t ElementSize, |
564 | MDNode *TBAATag = nullptr, |
565 | MDNode *ScopeTag = nullptr, |
566 | MDNode *NoAliasTag = nullptr); |
567 | |
568 | /// Create and insert a memcpy between the specified pointers. |
569 | /// |
570 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is |
571 | /// specified, it will be added to the instruction. Likewise with alias.scope |
572 | /// and noalias tags. |
573 | CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, |
574 | MaybeAlign SrcAlign, uint64_t Size, |
575 | bool isVolatile = false, MDNode *TBAATag = nullptr, |
576 | MDNode *TBAAStructTag = nullptr, |
577 | MDNode *ScopeTag = nullptr, |
578 | MDNode *NoAliasTag = nullptr) { |
579 | return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size), |
580 | isVolatile, TBAATag, TBAAStructTag, ScopeTag, |
581 | NoAliasTag); |
582 | } |
583 | |
584 | CallInst *CreateMemTransferInst( |
585 | Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src, |
586 | MaybeAlign SrcAlign, Value *Size, bool isVolatile = false, |
587 | MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, |
588 | MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr); |
589 | |
590 | CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src, |
591 | MaybeAlign SrcAlign, Value *Size, |
592 | bool isVolatile = false, MDNode *TBAATag = nullptr, |
593 | MDNode *TBAAStructTag = nullptr, |
594 | MDNode *ScopeTag = nullptr, |
595 | MDNode *NoAliasTag = nullptr) { |
596 | return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src, |
597 | SrcAlign, Size, isVolatile, TBAATag, |
598 | TBAAStructTag, ScopeTag, NoAliasTag); |
599 | } |
600 | |
601 | CallInst *CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src, |
602 | MaybeAlign SrcAlign, Value *Size); |
603 | |
604 | /// Create and insert an element unordered-atomic memcpy between the |
605 | /// specified pointers. |
606 | /// |
607 | /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively. |
608 | /// |
609 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is |
610 | /// specified, it will be added to the instruction. Likewise with alias.scope |
611 | /// and noalias tags. |
612 | CallInst *CreateElementUnorderedAtomicMemCpy( |
613 | Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, |
614 | uint32_t ElementSize, MDNode *TBAATag = nullptr, |
615 | MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, |
616 | MDNode *NoAliasTag = nullptr); |
617 | |
618 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemCpy(CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
619 | Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
620 | unsigned SrcAlign, uint64_t Size,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
621 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
622 | MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
623 | MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
624 | MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
625 | "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) { |
626 | return CreateElementUnorderedAtomicMemCpy( |
627 | Dst, Align(DstAlign), Src, Align(SrcAlign), getInt64(Size), ElementSize, |
628 | TBAATag, TBAAStructTag, ScopeTag, NoAliasTag); |
629 | } |
630 | |
631 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemCpy(CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
632 | Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
633 | unsigned SrcAlign, Value *Size,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
634 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
635 | MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
636 | MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
637 | MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
638 | "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemCpy( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) { |
639 | return CreateElementUnorderedAtomicMemCpy( |
640 | Dst, Align(DstAlign), Src, Align(SrcAlign), Size, ElementSize, TBAATag, |
641 | TBAAStructTag, ScopeTag, NoAliasTag); |
642 | } |
643 | |
644 | CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src, |
645 | MaybeAlign SrcAlign, uint64_t Size, |
646 | bool isVolatile = false, MDNode *TBAATag = nullptr, |
647 | MDNode *ScopeTag = nullptr, |
648 | MDNode *NoAliasTag = nullptr) { |
649 | return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), |
650 | isVolatile, TBAATag, ScopeTag, NoAliasTag); |
651 | } |
652 | |
653 | CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src, |
654 | MaybeAlign SrcAlign, Value *Size, |
655 | bool isVolatile = false, MDNode *TBAATag = nullptr, |
656 | MDNode *ScopeTag = nullptr, |
657 | MDNode *NoAliasTag = nullptr); |
658 | |
659 | /// \brief Create and insert an element unordered-atomic memmove between the |
660 | /// specified pointers. |
661 | /// |
662 | /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, |
663 | /// respectively. |
664 | /// |
665 | /// If the pointers aren't i8*, they will be converted. If a TBAA tag is |
666 | /// specified, it will be added to the instruction. Likewise with alias.scope |
667 | /// and noalias tags. |
668 | CallInst *CreateElementUnorderedAtomicMemMove( |
669 | Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size, |
670 | uint32_t ElementSize, MDNode *TBAATag = nullptr, |
671 | MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, |
672 | MDNode *NoAliasTag = nullptr); |
673 | |
674 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemMove(CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
675 | Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
676 | unsigned SrcAlign, uint64_t Size,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
677 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
678 | MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
679 | MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
680 | MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
681 | "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) { |
682 | return CreateElementUnorderedAtomicMemMove( |
683 | Dst, Align(DstAlign), Src, Align(SrcAlign), getInt64(Size), ElementSize, |
684 | TBAATag, TBAAStructTag, ScopeTag, NoAliasTag); |
685 | } |
686 | |
687 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateElementUnorderedAtomicMemMove(CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
688 | Value *Dst, unsigned DstAlign, Value *Src,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
689 | unsigned SrcAlign, Value *Size,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
690 | uint32_t ElementSize, MDNode *TBAATag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
691 | MDNode *TBAAStructTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
692 | MDNode *ScopeTag = nullptr,CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
693 | MDNode *NoAliasTag = nullptr),CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
694 | "Use the version that takes Align instead")CallInst *CreateElementUnorderedAtomicMemMove( Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size, uint32_t ElementSize, MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr ) __attribute__((deprecated("Use the version that takes Align instead" ))) { |
695 | return CreateElementUnorderedAtomicMemMove( |
696 | Dst, Align(DstAlign), Src, Align(SrcAlign), Size, ElementSize, TBAATag, |
697 | TBAAStructTag, ScopeTag, NoAliasTag); |
698 | } |
699 | |
700 | /// Create a vector fadd reduction intrinsic of the source vector. |
701 | /// The first parameter is a scalar accumulator value for ordered reductions. |
702 | CallInst *CreateFAddReduce(Value *Acc, Value *Src); |
703 | |
704 | /// Create a vector fmul reduction intrinsic of the source vector. |
705 | /// The first parameter is a scalar accumulator value for ordered reductions. |
706 | CallInst *CreateFMulReduce(Value *Acc, Value *Src); |
707 | |
708 | /// Create a vector int add reduction intrinsic of the source vector. |
709 | CallInst *CreateAddReduce(Value *Src); |
710 | |
711 | /// Create a vector int mul reduction intrinsic of the source vector. |
712 | CallInst *CreateMulReduce(Value *Src); |
713 | |
714 | /// Create a vector int AND reduction intrinsic of the source vector. |
715 | CallInst *CreateAndReduce(Value *Src); |
716 | |
717 | /// Create a vector int OR reduction intrinsic of the source vector. |
718 | CallInst *CreateOrReduce(Value *Src); |
719 | |
720 | /// Create a vector int XOR reduction intrinsic of the source vector. |
721 | CallInst *CreateXorReduce(Value *Src); |
722 | |
723 | /// Create a vector integer max reduction intrinsic of the source |
724 | /// vector. |
725 | CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false); |
726 | |
727 | /// Create a vector integer min reduction intrinsic of the source |
728 | /// vector. |
729 | CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false); |
730 | |
731 | /// Create a vector float max reduction intrinsic of the source |
732 | /// vector. |
733 | CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false); |
734 | |
735 | /// Create a vector float min reduction intrinsic of the source |
736 | /// vector. |
737 | CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false); |
738 | |
739 | /// Create a lifetime.start intrinsic. |
740 | /// |
741 | /// If the pointer isn't i8* it will be converted. |
742 | CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr); |
743 | |
744 | /// Create a lifetime.end intrinsic. |
745 | /// |
746 | /// If the pointer isn't i8* it will be converted. |
747 | CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr); |
748 | |
749 | /// Create a call to invariant.start intrinsic. |
750 | /// |
751 | /// If the pointer isn't i8* it will be converted. |
752 | CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr); |
753 | |
754 | /// Create a call to Masked Load intrinsic |
755 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
756 | CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask,CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
757 | Value *PassThru = nullptr,CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
758 | const Twine &Name = ""),CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) |
759 | "Use the version that takes Align instead")CallInst *CreateMaskedLoad(Value *Ptr, unsigned Alignment, Value *Mask, Value *PassThru = nullptr, const Twine &Name = "" ) __attribute__((deprecated("Use the version that takes Align instead" ))) { |
760 | return CreateMaskedLoad(Ptr, assumeAligned(Alignment), Mask, PassThru, |
761 | Name); |
762 | } |
763 | CallInst *CreateMaskedLoad(Value *Ptr, Align Alignment, Value *Mask, |
764 | Value *PassThru = nullptr, const Twine &Name = ""); |
765 | |
766 | /// Create a call to Masked Store intrinsic |
767 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedStore(Value *Val, Value *Ptr,CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment , Value *Mask) __attribute__((deprecated("Use the version that takes Align instead" ))) |
768 | unsigned Alignment,CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment , Value *Mask) __attribute__((deprecated("Use the version that takes Align instead" ))) |
769 | Value *Mask),CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment , Value *Mask) __attribute__((deprecated("Use the version that takes Align instead" ))) |
770 | "Use the version that takes Align instead")CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Alignment , Value *Mask) __attribute__((deprecated("Use the version that takes Align instead" ))) { |
771 | return CreateMaskedStore(Val, Ptr, assumeAligned(Alignment), Mask); |
772 | } |
773 | |
774 | CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment, |
775 | Value *Mask); |
776 | |
777 | /// Create a call to Masked Gather intrinsic |
778 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) |
779 | CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) |
780 | Value *Mask = nullptr,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) |
781 | Value *PassThru = nullptr,CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) |
782 | const Twine &Name = ""),CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) |
783 | "Use the version that takes Align instead")CallInst *CreateMaskedGather(Value *Ptrs, unsigned Alignment, Value *Mask = nullptr, Value *PassThru = nullptr, const Twine &Name = "") __attribute__((deprecated("Use the version that takes Align instead" ))) { |
784 | return CreateMaskedGather(Ptrs, Align(Alignment), Mask, PassThru, Name); |
785 | } |
786 | |
787 | /// Create a call to Masked Gather intrinsic |
788 | CallInst *CreateMaskedGather(Value *Ptrs, Align Alignment, |
789 | Value *Mask = nullptr, Value *PassThru = nullptr, |
790 | const Twine &Name = ""); |
791 | |
792 | /// Create a call to Masked Scatter intrinsic |
793 | LLVM_ATTRIBUTE_DEPRECATED(CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment, Value *Mask = nullptr) __attribute__((deprecated( "Use the version that takes Align instead"))) |
794 | CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment,CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment, Value *Mask = nullptr) __attribute__((deprecated( "Use the version that takes Align instead"))) |
795 | Value *Mask = nullptr),CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment, Value *Mask = nullptr) __attribute__((deprecated( "Use the version that takes Align instead"))) |
796 | "Use the version that takes Align instead")CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Alignment, Value *Mask = nullptr) __attribute__((deprecated( "Use the version that takes Align instead"))) { |
797 | return CreateMaskedScatter(Val, Ptrs, Align(Alignment), Mask); |
798 | } |
799 | |
800 | /// Create a call to Masked Scatter intrinsic |
801 | CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment, |
802 | Value *Mask = nullptr); |
803 | |
804 | /// Create an assume intrinsic call that allows the optimizer to |
805 | /// assume that the provided condition will be true. |
806 | /// |
807 | /// The optional argument \p OpBundles specifies operand bundles that are |
808 | /// added to the call instruction. |
809 | CallInst *CreateAssumption(Value *Cond, |
810 | ArrayRef<OperandBundleDef> OpBundles = llvm::None); |
811 | |
812 | /// Create a call to the experimental.gc.statepoint intrinsic to |
813 | /// start a new statepoint sequence. |
814 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, |
815 | Value *ActualCallee, |
816 | ArrayRef<Value *> CallArgs, |
817 | Optional<ArrayRef<Value *>> DeoptArgs, |
818 | ArrayRef<Value *> GCArgs, |
819 | const Twine &Name = ""); |
820 | |
821 | /// Create a call to the experimental.gc.statepoint intrinsic to |
822 | /// start a new statepoint sequence. |
823 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, |
824 | Value *ActualCallee, uint32_t Flags, |
825 | ArrayRef<Value *> CallArgs, |
826 | Optional<ArrayRef<Use>> TransitionArgs, |
827 | Optional<ArrayRef<Use>> DeoptArgs, |
828 | ArrayRef<Value *> GCArgs, |
829 | const Twine &Name = ""); |
830 | |
831 | /// Conveninence function for the common case when CallArgs are filled |
832 | /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be |
833 | /// .get()'ed to get the Value pointer. |
834 | CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes, |
835 | Value *ActualCallee, ArrayRef<Use> CallArgs, |
836 | Optional<ArrayRef<Value *>> DeoptArgs, |
837 | ArrayRef<Value *> GCArgs, |
838 | const Twine &Name = ""); |
839 | |
840 | /// Create an invoke to the experimental.gc.statepoint intrinsic to |
841 | /// start a new statepoint sequence. |
842 | InvokeInst * |
843 | CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, |
844 | Value *ActualInvokee, BasicBlock *NormalDest, |
845 | BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs, |
846 | Optional<ArrayRef<Value *>> DeoptArgs, |
847 | ArrayRef<Value *> GCArgs, const Twine &Name = ""); |
848 | |
849 | /// Create an invoke to the experimental.gc.statepoint intrinsic to |
850 | /// start a new statepoint sequence. |
851 | InvokeInst *CreateGCStatepointInvoke( |
852 | uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee, |
853 | BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags, |
854 | ArrayRef<Value *> InvokeArgs, Optional<ArrayRef<Use>> TransitionArgs, |
855 | Optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs, |
856 | const Twine &Name = ""); |
857 | |
858 | // Convenience function for the common case when CallArgs are filled in using |
859 | // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to |
860 | // get the Value *. |
861 | InvokeInst * |
862 | CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes, |
863 | Value *ActualInvokee, BasicBlock *NormalDest, |
864 | BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs, |
865 | Optional<ArrayRef<Value *>> DeoptArgs, |
866 | ArrayRef<Value *> GCArgs, const Twine &Name = ""); |
867 | |
868 | /// Create a call to the experimental.gc.result intrinsic to extract |
869 | /// the result from a call wrapped in a statepoint. |
870 | CallInst *CreateGCResult(Instruction *Statepoint, |
871 | Type *ResultType, |
872 | const Twine &Name = ""); |
873 | |
874 | /// Create a call to the experimental.gc.relocate intrinsics to |
875 | /// project the relocated value of one pointer from the statepoint. |
876 | CallInst *CreateGCRelocate(Instruction *Statepoint, |
877 | int BaseOffset, |
878 | int DerivedOffset, |
879 | Type *ResultType, |
880 | const Twine &Name = ""); |
881 | |
882 | /// Create a call to intrinsic \p ID with 1 operand which is mangled on its |
883 | /// type. |
884 | CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, |
885 | Instruction *FMFSource = nullptr, |
886 | const Twine &Name = ""); |
887 | |
888 | /// Create a call to intrinsic \p ID with 2 operands which is mangled on the |
889 | /// first type. |
890 | CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, |
891 | Instruction *FMFSource = nullptr, |
892 | const Twine &Name = ""); |
893 | |
894 | /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If |
895 | /// \p FMFSource is provided, copy fast-math-flags from that instruction to |
896 | /// the intrinsic. |
897 | CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types, |
898 | ArrayRef<Value *> Args, |
899 | Instruction *FMFSource = nullptr, |
900 | const Twine &Name = ""); |
901 | |
902 | /// Create call to the minnum intrinsic. |
903 | CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") { |
904 | return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name); |
905 | } |
906 | |
907 | /// Create call to the maxnum intrinsic. |
908 | CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") { |
909 | return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name); |
910 | } |
911 | |
912 | /// Create call to the minimum intrinsic. |
913 | CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") { |
914 | return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name); |
915 | } |
916 | |
917 | /// Create call to the maximum intrinsic. |
918 | CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") { |
919 | return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name); |
920 | } |
921 | |
922 | private: |
923 | /// Create a call to a masked intrinsic with given Id. |
924 | CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops, |
925 | ArrayRef<Type *> OverloadedTypes, |
926 | const Twine &Name = ""); |
927 | |
928 | Value *getCastedInt8PtrValue(Value *Ptr); |
929 | |
930 | //===--------------------------------------------------------------------===// |
931 | // Instruction creation methods: Terminators |
932 | //===--------------------------------------------------------------------===// |
933 | |
934 | private: |
935 | /// Helper to add branch weight and unpredictable metadata onto an |
936 | /// instruction. |
937 | /// \returns The annotated instruction. |
938 | template <typename InstTy> |
939 | InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) { |
940 | if (Weights) |
941 | I->setMetadata(LLVMContext::MD_prof, Weights); |
942 | if (Unpredictable) |
943 | I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable); |
944 | return I; |
945 | } |
946 | |
947 | public: |
948 | /// Create a 'ret void' instruction. |
949 | ReturnInst *CreateRetVoid() { |
950 | return Insert(ReturnInst::Create(Context)); |
951 | } |
952 | |
953 | /// Create a 'ret <val>' instruction. |
954 | ReturnInst *CreateRet(Value *V) { |
955 | return Insert(ReturnInst::Create(Context, V)); |
956 | } |
957 | |
958 | /// Create a sequence of N insertvalue instructions, |
959 | /// with one Value from the retVals array each, that build a aggregate |
960 | /// return value one value at a time, and a ret instruction to return |
961 | /// the resulting aggregate value. |
962 | /// |
963 | /// This is a convenience function for code that uses aggregate return values |
964 | /// as a vehicle for having multiple return values. |
965 | ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) { |
966 | Value *V = UndefValue::get(getCurrentFunctionReturnType()); |
967 | for (unsigned i = 0; i != N; ++i) |
968 | V = CreateInsertValue(V, retVals[i], i, "mrv"); |
969 | return Insert(ReturnInst::Create(Context, V)); |
970 | } |
971 | |
972 | /// Create an unconditional 'br label X' instruction. |
973 | BranchInst *CreateBr(BasicBlock *Dest) { |
974 | return Insert(BranchInst::Create(Dest)); |
975 | } |
976 | |
977 | /// Create a conditional 'br Cond, TrueDest, FalseDest' |
978 | /// instruction. |
979 | BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, |
980 | MDNode *BranchWeights = nullptr, |
981 | MDNode *Unpredictable = nullptr) { |
982 | return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond), |
983 | BranchWeights, Unpredictable)); |
984 | } |
985 | |
986 | /// Create a conditional 'br Cond, TrueDest, FalseDest' |
987 | /// instruction. Copy branch meta data if available. |
988 | BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, |
989 | Instruction *MDSrc) { |
990 | BranchInst *Br = BranchInst::Create(True, False, Cond); |
991 | if (MDSrc) { |
992 | unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable, |
993 | LLVMContext::MD_make_implicit, LLVMContext::MD_dbg}; |
994 | Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4)); |
995 | } |
996 | return Insert(Br); |
997 | } |
998 | |
999 | /// Create a switch instruction with the specified value, default dest, |
1000 | /// and with a hint for the number of cases that will be added (for efficient |
1001 | /// allocation). |
1002 | SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10, |
1003 | MDNode *BranchWeights = nullptr, |
1004 | MDNode *Unpredictable = nullptr) { |
1005 | return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases), |
1006 | BranchWeights, Unpredictable)); |
1007 | } |
1008 | |
1009 | /// Create an indirect branch instruction with the specified address |
1010 | /// operand, with an optional hint for the number of destinations that will be |
1011 | /// added (for efficient allocation). |
1012 | IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) { |
1013 | return Insert(IndirectBrInst::Create(Addr, NumDests)); |
1014 | } |
1015 | |
1016 | /// Create an invoke instruction. |
1017 | InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee, |
1018 | BasicBlock *NormalDest, BasicBlock *UnwindDest, |
1019 | ArrayRef<Value *> Args, |
1020 | ArrayRef<OperandBundleDef> OpBundles, |
1021 | const Twine &Name = "") { |
1022 | return Insert( |
1023 | InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles), |
1024 | Name); |
1025 | } |
1026 | InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee, |
1027 | BasicBlock *NormalDest, BasicBlock *UnwindDest, |
1028 | ArrayRef<Value *> Args = None, |
1029 | const Twine &Name = "") { |
1030 | return Insert(InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args), |
1031 | Name); |
1032 | } |
1033 | |
1034 | InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, |
1035 | BasicBlock *UnwindDest, ArrayRef<Value *> Args, |
1036 | ArrayRef<OperandBundleDef> OpBundles, |
1037 | const Twine &Name = "") { |
1038 | return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(), |
1039 | NormalDest, UnwindDest, Args, OpBundles, Name); |
1040 | } |
1041 | |
1042 | InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest, |
1043 | BasicBlock *UnwindDest, |
1044 | ArrayRef<Value *> Args = None, |
1045 | const Twine &Name = "") { |
1046 | return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(), |
1047 | NormalDest, UnwindDest, Args, Name); |
1048 | } |
1049 | |
1050 | /// \brief Create a callbr instruction. |
1051 | CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee, |
1052 | BasicBlock *DefaultDest, |
1053 | ArrayRef<BasicBlock *> IndirectDests, |
1054 | ArrayRef<Value *> Args = None, |
1055 | const Twine &Name = "") { |
1056 | return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, |
1057 | Args), Name); |
1058 | } |
1059 | CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee, |
1060 | BasicBlock *DefaultDest, |
1061 | ArrayRef<BasicBlock *> IndirectDests, |
1062 | ArrayRef<Value *> Args, |
1063 | ArrayRef<OperandBundleDef> OpBundles, |
1064 | const Twine &Name = "") { |
1065 | return Insert( |
1066 | CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args, |
1067 | OpBundles), Name); |
1068 | } |
1069 | |
1070 | CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, |
1071 | ArrayRef<BasicBlock *> IndirectDests, |
1072 | ArrayRef<Value *> Args = None, |
1073 | const Twine &Name = "") { |
1074 | return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(), |
1075 | DefaultDest, IndirectDests, Args, Name); |
1076 | } |
1077 | CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest, |
1078 | ArrayRef<BasicBlock *> IndirectDests, |
1079 | ArrayRef<Value *> Args, |
1080 | ArrayRef<OperandBundleDef> OpBundles, |
1081 | const Twine &Name = "") { |
1082 | return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(), |
1083 | DefaultDest, IndirectDests, Args, Name); |
1084 | } |
1085 | |
1086 | ResumeInst *CreateResume(Value *Exn) { |
1087 | return Insert(ResumeInst::Create(Exn)); |
1088 | } |
1089 | |
1090 | CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad, |
1091 | BasicBlock *UnwindBB = nullptr) { |
1092 | return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB)); |
1093 | } |
1094 | |
1095 | CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB, |
1096 | unsigned NumHandlers, |
1097 | const Twine &Name = "") { |
1098 | return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers), |
1099 | Name); |
1100 | } |
1101 | |
1102 | CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args, |
1103 | const Twine &Name = "") { |
1104 | return Insert(CatchPadInst::Create(ParentPad, Args), Name); |
1105 | } |
1106 | |
1107 | CleanupPadInst *CreateCleanupPad(Value *ParentPad, |
1108 | ArrayRef<Value *> Args = None, |
1109 | const Twine &Name = "") { |
1110 | return Insert(CleanupPadInst::Create(ParentPad, Args), Name); |
1111 | } |
1112 | |
1113 | CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) { |
1114 | return Insert(CatchReturnInst::Create(CatchPad, BB)); |
1115 | } |
1116 | |
1117 | UnreachableInst *CreateUnreachable() { |
1118 | return Insert(new UnreachableInst(Context)); |
1119 | } |
1120 | |
1121 | //===--------------------------------------------------------------------===// |
1122 | // Instruction creation methods: Binary Operators |
1123 | //===--------------------------------------------------------------------===// |
1124 | private: |
1125 | BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc, |
1126 | Value *LHS, Value *RHS, |
1127 | const Twine &Name, |
1128 | bool HasNUW, bool HasNSW) { |
1129 | BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name); |
1130 | if (HasNUW) BO->setHasNoUnsignedWrap(); |
1131 | if (HasNSW) BO->setHasNoSignedWrap(); |
1132 | return BO; |
1133 | } |
1134 | |
1135 | Instruction *setFPAttrs(Instruction *I, MDNode *FPMD, |
1136 | FastMathFlags FMF) const { |
1137 | if (!FPMD) |
1138 | FPMD = DefaultFPMathTag; |
1139 | if (FPMD) |
1140 | I->setMetadata(LLVMContext::MD_fpmath, FPMD); |
1141 | I->setFastMathFlags(FMF); |
1142 | return I; |
1143 | } |
1144 | |
1145 | Value *foldConstant(Instruction::BinaryOps Opc, Value *L, |
1146 | Value *R, const Twine &Name) const { |
1147 | auto *LC = dyn_cast<Constant>(L); |
1148 | auto *RC = dyn_cast<Constant>(R); |
1149 | return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr; |
1150 | } |
1151 | |
1152 | Value *getConstrainedFPRounding(Optional<RoundingMode> Rounding) { |
1153 | RoundingMode UseRounding = DefaultConstrainedRounding; |
1154 | |
1155 | if (Rounding.hasValue()) |
1156 | UseRounding = Rounding.getValue(); |
1157 | |
1158 | Optional<StringRef> RoundingStr = RoundingModeToStr(UseRounding); |
1159 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1159, __PRETTY_FUNCTION__)); |
1160 | auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue()); |
1161 | |
1162 | return MetadataAsValue::get(Context, RoundingMDS); |
1163 | } |
1164 | |
1165 | Value *getConstrainedFPExcept(Optional<fp::ExceptionBehavior> Except) { |
1166 | fp::ExceptionBehavior UseExcept = DefaultConstrainedExcept; |
1167 | |
1168 | if (Except.hasValue()) |
1169 | UseExcept = Except.getValue(); |
1170 | |
1171 | Optional<StringRef> ExceptStr = ExceptionBehaviorToStr(UseExcept); |
1172 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1172, __PRETTY_FUNCTION__)); |
1173 | auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue()); |
1174 | |
1175 | return MetadataAsValue::get(Context, ExceptMDS); |
1176 | } |
1177 | |
1178 | Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) { |
1179 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1182, __PRETTY_FUNCTION__)) |
1180 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1182, __PRETTY_FUNCTION__)) |
1181 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1182, __PRETTY_FUNCTION__)) |
1182 | "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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1182, __PRETTY_FUNCTION__)); |
1183 | |
1184 | StringRef PredicateStr = CmpInst::getPredicateName(Predicate); |
1185 | auto *PredicateMDS = MDString::get(Context, PredicateStr); |
1186 | |
1187 | return MetadataAsValue::get(Context, PredicateMDS); |
1188 | } |
1189 | |
1190 | public: |
1191 | Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "", |
1192 | bool HasNUW = false, bool HasNSW = false) { |
1193 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1194 | if (auto *RC = dyn_cast<Constant>(RHS)) |
1195 | return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name); |
1196 | return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, |
1197 | HasNUW, HasNSW); |
1198 | } |
1199 | |
1200 | Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { |
1201 | return CreateAdd(LHS, RHS, Name, false, true); |
1202 | } |
1203 | |
1204 | Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") { |
1205 | return CreateAdd(LHS, RHS, Name, true, false); |
1206 | } |
1207 | |
1208 | Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "", |
1209 | bool HasNUW = false, bool HasNSW = false) { |
1210 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1211 | if (auto *RC = dyn_cast<Constant>(RHS)) |
1212 | return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name); |
1213 | return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, |
1214 | HasNUW, HasNSW); |
1215 | } |
1216 | |
1217 | Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") { |
1218 | return CreateSub(LHS, RHS, Name, false, true); |
1219 | } |
1220 | |
1221 | Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") { |
1222 | return CreateSub(LHS, RHS, Name, true, false); |
1223 | } |
1224 | |
1225 | Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "", |
1226 | bool HasNUW = false, bool HasNSW = false) { |
1227 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1228 | if (auto *RC = dyn_cast<Constant>(RHS)) |
1229 | return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name); |
1230 | return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, |
1231 | HasNUW, HasNSW); |
1232 | } |
1233 | |
1234 | Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") { |
1235 | return CreateMul(LHS, RHS, Name, false, true); |
1236 | } |
1237 | |
1238 | Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") { |
1239 | return CreateMul(LHS, RHS, Name, true, false); |
1240 | } |
1241 | |
1242 | Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "", |
1243 | bool isExact = false) { |
1244 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1245 | if (auto *RC = dyn_cast<Constant>(RHS)) |
1246 | return Insert(Folder.CreateUDiv(LC, RC, isExact), Name); |
1247 | if (!isExact) |
1248 | return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name); |
1249 | return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name); |
1250 | } |
1251 | |
1252 | Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") { |
1253 | return CreateUDiv(LHS, RHS, Name, true); |
1254 | } |
1255 | |
1256 | Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "", |
1257 | bool isExact = false) { |
1258 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1259 | if (auto *RC = dyn_cast<Constant>(RHS)) |
1260 | return Insert(Folder.CreateSDiv(LC, RC, isExact), Name); |
1261 | if (!isExact) |
1262 | return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name); |
1263 | return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name); |
1264 | } |
1265 | |
1266 | Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") { |
1267 | return CreateSDiv(LHS, RHS, Name, true); |
1268 | } |
1269 | |
1270 | Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") { |
1271 | if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V; |
1272 | return Insert(BinaryOperator::CreateURem(LHS, RHS), Name); |
1273 | } |
1274 | |
1275 | Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") { |
1276 | if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V; |
1277 | return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name); |
1278 | } |
1279 | |
1280 | Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "", |
1281 | bool HasNUW = false, bool HasNSW = false) { |
1282 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1283 | if (auto *RC = dyn_cast<Constant>(RHS)) |
1284 | return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name); |
1285 | return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name, |
1286 | HasNUW, HasNSW); |
1287 | } |
1288 | |
1289 | Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "", |
1290 | bool HasNUW = false, bool HasNSW = false) { |
1291 | return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, |
1292 | HasNUW, HasNSW); |
1293 | } |
1294 | |
1295 | Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "", |
1296 | bool HasNUW = false, bool HasNSW = false) { |
1297 | return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name, |
1298 | HasNUW, HasNSW); |
1299 | } |
1300 | |
1301 | Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "", |
1302 | bool isExact = false) { |
1303 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1304 | if (auto *RC = dyn_cast<Constant>(RHS)) |
1305 | return Insert(Folder.CreateLShr(LC, RC, isExact), Name); |
1306 | if (!isExact) |
1307 | return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name); |
1308 | return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name); |
1309 | } |
1310 | |
1311 | Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "", |
1312 | bool isExact = false) { |
1313 | return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); |
1314 | } |
1315 | |
1316 | Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "", |
1317 | bool isExact = false) { |
1318 | return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); |
1319 | } |
1320 | |
1321 | Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "", |
1322 | bool isExact = false) { |
1323 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1324 | if (auto *RC = dyn_cast<Constant>(RHS)) |
1325 | return Insert(Folder.CreateAShr(LC, RC, isExact), Name); |
1326 | if (!isExact) |
1327 | return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name); |
1328 | return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name); |
1329 | } |
1330 | |
1331 | Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "", |
1332 | bool isExact = false) { |
1333 | return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); |
1334 | } |
1335 | |
1336 | Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "", |
1337 | bool isExact = false) { |
1338 | return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact); |
1339 | } |
1340 | |
1341 | Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") { |
1342 | if (auto *RC = dyn_cast<Constant>(RHS)) { |
1343 | if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne()) |
1344 | return LHS; // LHS & -1 -> LHS |
1345 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1346 | return Insert(Folder.CreateAnd(LC, RC), Name); |
1347 | } |
1348 | return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name); |
1349 | } |
1350 | |
1351 | Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") { |
1352 | return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); |
1353 | } |
1354 | |
1355 | Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") { |
1356 | return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name); |
1357 | } |
1358 | |
1359 | Value *CreateAnd(ArrayRef<Value*> Ops) { |
1360 | assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail ("!Ops.empty()", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1360, __PRETTY_FUNCTION__)); |
1361 | Value *Accum = Ops[0]; |
1362 | for (unsigned i = 1; i < Ops.size(); i++) |
1363 | Accum = CreateAnd(Accum, Ops[i]); |
1364 | return Accum; |
1365 | } |
1366 | |
1367 | Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") { |
1368 | if (auto *RC = dyn_cast<Constant>(RHS)) { |
1369 | if (RC->isNullValue()) |
1370 | return LHS; // LHS | 0 -> LHS |
1371 | if (auto *LC = dyn_cast<Constant>(LHS)) |
1372 | return Insert(Folder.CreateOr(LC, RC), Name); |
1373 | } |
1374 | return Insert(BinaryOperator::CreateOr(LHS, RHS), Name); |
1375 | } |
1376 | |
1377 | Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") { |
1378 | return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); |
1379 | } |
1380 | |
1381 | Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") { |
1382 | return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name); |
1383 | } |
1384 | |
1385 | Value *CreateOr(ArrayRef<Value*> Ops) { |
1386 | assert(!Ops.empty())((!Ops.empty()) ? static_cast<void> (0) : __assert_fail ("!Ops.empty()", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1386, __PRETTY_FUNCTION__)); |
1387 | Value *Accum = Ops[0]; |
1388 | for (unsigned i = 1; i < Ops.size(); i++) |
1389 | Accum = CreateOr(Accum, Ops[i]); |
1390 | return Accum; |
1391 | } |
1392 | |
1393 | Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") { |
1394 | if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V; |
1395 | return Insert(BinaryOperator::CreateXor(LHS, RHS), Name); |
1396 | } |
1397 | |
1398 | Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") { |
1399 | return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); |
1400 | } |
1401 | |
1402 | Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") { |
1403 | return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name); |
1404 | } |
1405 | |
1406 | Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "", |
1407 | MDNode *FPMD = nullptr) { |
1408 | if (IsFPConstrained) |
1409 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd, |
1410 | L, R, nullptr, Name, FPMD); |
1411 | |
1412 | if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V; |
1413 | Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF); |
1414 | return Insert(I, Name); |
1415 | } |
1416 | |
1417 | /// Copy fast-math-flags from an instruction rather than using the builder's |
1418 | /// default FMF. |
1419 | Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource, |
1420 | const Twine &Name = "") { |
1421 | if (IsFPConstrained) |
1422 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd, |
1423 | L, R, FMFSource, Name); |
1424 | |
1425 | if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V; |
1426 | Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr, |
1427 | FMFSource->getFastMathFlags()); |
1428 | return Insert(I, Name); |
1429 | } |
1430 | |
1431 | Value *CreateFSub(Value *L, Value *R, const Twine &Name = "", |
1432 | MDNode *FPMD = nullptr) { |
1433 | if (IsFPConstrained) |
1434 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub, |
1435 | L, R, nullptr, Name, FPMD); |
1436 | |
1437 | if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V; |
1438 | Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF); |
1439 | return Insert(I, Name); |
1440 | } |
1441 | |
1442 | /// Copy fast-math-flags from an instruction rather than using the builder's |
1443 | /// default FMF. |
1444 | Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource, |
1445 | const Twine &Name = "") { |
1446 | if (IsFPConstrained) |
1447 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub, |
1448 | L, R, FMFSource, Name); |
1449 | |
1450 | if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V; |
1451 | Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr, |
1452 | FMFSource->getFastMathFlags()); |
1453 | return Insert(I, Name); |
1454 | } |
1455 | |
1456 | Value *CreateFMul(Value *L, Value *R, const Twine &Name = "", |
1457 | MDNode *FPMD = nullptr) { |
1458 | if (IsFPConstrained) |
1459 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul, |
1460 | L, R, nullptr, Name, FPMD); |
1461 | |
1462 | if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V; |
1463 | Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF); |
1464 | return Insert(I, Name); |
1465 | } |
1466 | |
1467 | /// Copy fast-math-flags from an instruction rather than using the builder's |
1468 | /// default FMF. |
1469 | Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource, |
1470 | const Twine &Name = "") { |
1471 | if (IsFPConstrained) |
1472 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul, |
1473 | L, R, FMFSource, Name); |
1474 | |
1475 | if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V; |
1476 | Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr, |
1477 | FMFSource->getFastMathFlags()); |
1478 | return Insert(I, Name); |
1479 | } |
1480 | |
1481 | Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "", |
1482 | MDNode *FPMD = nullptr) { |
1483 | if (IsFPConstrained) |
1484 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv, |
1485 | L, R, nullptr, Name, FPMD); |
1486 | |
1487 | if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V; |
1488 | Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF); |
1489 | return Insert(I, Name); |
1490 | } |
1491 | |
1492 | /// Copy fast-math-flags from an instruction rather than using the builder's |
1493 | /// default FMF. |
1494 | Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource, |
1495 | const Twine &Name = "") { |
1496 | if (IsFPConstrained) |
1497 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv, |
1498 | L, R, FMFSource, Name); |
1499 | |
1500 | if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V; |
1501 | Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr, |
1502 | FMFSource->getFastMathFlags()); |
1503 | return Insert(I, Name); |
1504 | } |
1505 | |
1506 | Value *CreateFRem(Value *L, Value *R, const Twine &Name = "", |
1507 | MDNode *FPMD = nullptr) { |
1508 | if (IsFPConstrained) |
1509 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem, |
1510 | L, R, nullptr, Name, FPMD); |
1511 | |
1512 | if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V; |
1513 | Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF); |
1514 | return Insert(I, Name); |
1515 | } |
1516 | |
1517 | /// Copy fast-math-flags from an instruction rather than using the builder's |
1518 | /// default FMF. |
1519 | Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource, |
1520 | const Twine &Name = "") { |
1521 | if (IsFPConstrained) |
1522 | return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem, |
1523 | L, R, FMFSource, Name); |
1524 | |
1525 | if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V; |
1526 | Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr, |
1527 | FMFSource->getFastMathFlags()); |
1528 | return Insert(I, Name); |
1529 | } |
1530 | |
1531 | Value *CreateBinOp(Instruction::BinaryOps Opc, |
1532 | Value *LHS, Value *RHS, const Twine &Name = "", |
1533 | MDNode *FPMathTag = nullptr) { |
1534 | if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V; |
1535 | Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS); |
1536 | if (isa<FPMathOperator>(BinOp)) |
1537 | setFPAttrs(BinOp, FPMathTag, FMF); |
1538 | return Insert(BinOp, Name); |
1539 | } |
1540 | |
1541 | CallInst *CreateConstrainedFPBinOp( |
1542 | Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr, |
1543 | const Twine &Name = "", MDNode *FPMathTag = nullptr, |
1544 | Optional<RoundingMode> Rounding = None, |
1545 | Optional<fp::ExceptionBehavior> Except = None); |
1546 | |
1547 | Value *CreateNeg(Value *V, const Twine &Name = "", |
1548 | bool HasNUW = false, bool HasNSW = false) { |
1549 | if (auto *VC = dyn_cast<Constant>(V)) |
1550 | return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name); |
1551 | BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name); |
1552 | if (HasNUW) BO->setHasNoUnsignedWrap(); |
1553 | if (HasNSW) BO->setHasNoSignedWrap(); |
1554 | return BO; |
1555 | } |
1556 | |
1557 | Value *CreateNSWNeg(Value *V, const Twine &Name = "") { |
1558 | return CreateNeg(V, Name, false, true); |
1559 | } |
1560 | |
1561 | Value *CreateNUWNeg(Value *V, const Twine &Name = "") { |
1562 | return CreateNeg(V, Name, true, false); |
1563 | } |
1564 | |
1565 | Value *CreateFNeg(Value *V, const Twine &Name = "", |
1566 | MDNode *FPMathTag = nullptr) { |
1567 | if (auto *VC = dyn_cast<Constant>(V)) |
1568 | return Insert(Folder.CreateFNeg(VC), Name); |
1569 | return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF), |
1570 | Name); |
1571 | } |
1572 | |
1573 | /// Copy fast-math-flags from an instruction rather than using the builder's |
1574 | /// default FMF. |
1575 | Value *CreateFNegFMF(Value *V, Instruction *FMFSource, |
1576 | const Twine &Name = "") { |
1577 | if (auto *VC = dyn_cast<Constant>(V)) |
1578 | return Insert(Folder.CreateFNeg(VC), Name); |
1579 | return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr, |
1580 | FMFSource->getFastMathFlags()), |
1581 | Name); |
1582 | } |
1583 | |
1584 | Value *CreateNot(Value *V, const Twine &Name = "") { |
1585 | if (auto *VC = dyn_cast<Constant>(V)) |
1586 | return Insert(Folder.CreateNot(VC), Name); |
1587 | return Insert(BinaryOperator::CreateNot(V), Name); |
1588 | } |
1589 | |
1590 | Value *CreateUnOp(Instruction::UnaryOps Opc, |
1591 | Value *V, const Twine &Name = "", |
1592 | MDNode *FPMathTag = nullptr) { |
1593 | if (auto *VC = dyn_cast<Constant>(V)) |
1594 | return Insert(Folder.CreateUnOp(Opc, VC), Name); |
1595 | Instruction *UnOp = UnaryOperator::Create(Opc, V); |
1596 | if (isa<FPMathOperator>(UnOp)) |
1597 | setFPAttrs(UnOp, FPMathTag, FMF); |
1598 | return Insert(UnOp, Name); |
1599 | } |
1600 | |
1601 | /// Create either a UnaryOperator or BinaryOperator depending on \p Opc. |
1602 | /// Correct number of operands must be passed accordingly. |
1603 | Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops, |
1604 | const Twine &Name = "", MDNode *FPMathTag = nullptr); |
1605 | |
1606 | //===--------------------------------------------------------------------===// |
1607 | // Instruction creation methods: Memory Instructions |
1608 | //===--------------------------------------------------------------------===// |
1609 | |
1610 | AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace, |
1611 | Value *ArraySize = nullptr, const Twine &Name = "") { |
1612 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
1613 | Align AllocaAlign = DL.getPrefTypeAlign(Ty); |
1614 | return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name); |
1615 | } |
1616 | |
1617 | AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr, |
1618 | const Twine &Name = "") { |
1619 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
1620 | Align AllocaAlign = DL.getPrefTypeAlign(Ty); |
1621 | unsigned AddrSpace = DL.getAllocaAddrSpace(); |
1622 | return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name); |
1623 | } |
1624 | |
1625 | /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of |
1626 | /// converting the string to 'bool' for the isVolatile parameter. |
1627 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) { |
1628 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name); |
1629 | } |
1630 | |
1631 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") { |
1632 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name); |
1633 | } |
1634 | |
1635 | LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile, |
1636 | const Twine &Name = "") { |
1637 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name); |
1638 | } |
1639 | |
1640 | // Deprecated [opaque pointer types] |
1641 | LoadInst *CreateLoad(Value *Ptr, const char *Name) { |
1642 | return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name); |
1643 | } |
1644 | |
1645 | // Deprecated [opaque pointer types] |
1646 | LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") { |
1647 | return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name); |
1648 | } |
1649 | |
1650 | // Deprecated [opaque pointer types] |
1651 | LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") { |
1652 | return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, isVolatile, |
1653 | Name); |
1654 | } |
1655 | |
1656 | StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) { |
1657 | return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile); |
1658 | } |
1659 | |
1660 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead" ))) |
1661 | unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead" ))) |
1662 | const char *Name),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead" ))) |
1663 | "Use the version that takes NaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const char *Name) __attribute__((deprecated("Use the version that takes NaybeAlign instead" ))) { |
1664 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), Name); |
1665 | } |
1666 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, |
1667 | const char *Name) { |
1668 | return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name); |
1669 | } |
1670 | |
1671 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1672 | unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1673 | const Twine &Name = ""),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1674 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { |
1675 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), Name); |
1676 | } |
1677 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, |
1678 | const Twine &Name = "") { |
1679 | return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name); |
1680 | } |
1681 | |
1682 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) |
1683 | unsigned Align,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) |
1684 | bool isVolatile,LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) |
1685 | const Twine &Name = ""),LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) |
1686 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align , bool isVolatile, const Twine &Name = "") __attribute__( (deprecated("Use the version that takes MaybeAlign instead")) ) { |
1687 | return CreateAlignedLoad(Ty, Ptr, MaybeAlign(Align), isVolatile, Name); |
1688 | } |
1689 | LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, |
1690 | bool isVolatile, const Twine &Name = "") { |
1691 | if (!Align) { |
1692 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
1693 | Align = DL.getABITypeAlign(Ty); |
1694 | } |
1695 | return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name); |
1696 | } |
1697 | |
1698 | // Deprecated [opaque pointer types] |
1699 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1700 | unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1701 | const char *Name),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1702 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { |
1703 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, |
1704 | MaybeAlign(Align), Name); |
1705 | } |
1706 | // Deprecated [opaque pointer types] |
1707 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1708 | unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1709 | const Twine &Name = ""),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1710 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { |
1711 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, |
1712 | MaybeAlign(Align), Name); |
1713 | } |
1714 | // Deprecated [opaque pointer types] |
1715 | LLVM_ATTRIBUTE_DEPRECATED(LoadInst *CreateAlignedLoad(Value *Ptr,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1716 | unsigned Align,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1717 | bool isVolatile,LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1718 | const Twine &Name = ""),LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1719 | "Use the version that takes MaybeAlign instead")LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile , const Twine &Name = "") __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { |
1720 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, |
1721 | MaybeAlign(Align), isVolatile, Name); |
1722 | } |
1723 | // Deprecated [opaque pointer types] |
1724 | LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align, const char *Name) { |
1725 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, |
1726 | Align, Name); |
1727 | } |
1728 | // Deprecated [opaque pointer types] |
1729 | LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align, |
1730 | const Twine &Name = "") { |
1731 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, |
1732 | Align, Name); |
1733 | } |
1734 | // Deprecated [opaque pointer types] |
1735 | LoadInst *CreateAlignedLoad(Value *Ptr, MaybeAlign Align, bool isVolatile, |
1736 | const Twine &Name = "") { |
1737 | return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr, |
1738 | Align, isVolatile, Name); |
1739 | } |
1740 | |
1741 | LLVM_ATTRIBUTE_DEPRECATED(StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1742 | StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1743 | bool isVolatile = false),StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) |
1744 | "Use the version that takes MaybeAlign instead")StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align, bool isVolatile = false) __attribute__((deprecated("Use the version that takes MaybeAlign instead" ))) { |
1745 | return CreateAlignedStore(Val, Ptr, MaybeAlign(Align), isVolatile); |
1746 | } |
1747 | StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align, |
1748 | bool isVolatile = false) { |
1749 | if (!Align) { |
1750 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
1751 | Align = DL.getABITypeAlign(Val->getType()); |
1752 | } |
1753 | return Insert(new StoreInst(Val, Ptr, isVolatile, *Align)); |
1754 | } |
1755 | FenceInst *CreateFence(AtomicOrdering Ordering, |
1756 | SyncScope::ID SSID = SyncScope::System, |
1757 | const Twine &Name = "") { |
1758 | return Insert(new FenceInst(Context, Ordering, SSID), Name); |
1759 | } |
1760 | |
1761 | AtomicCmpXchgInst *CreateAtomicCmpXchg( |
1762 | Value *Ptr, Value *Cmp, Value *New, AtomicOrdering SuccessOrdering, |
1763 | AtomicOrdering FailureOrdering, SyncScope::ID SSID = SyncScope::System) { |
1764 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
1765 | Align Alignment(DL.getTypeStoreSize(New->getType())); |
1766 | return Insert(new AtomicCmpXchgInst( |
1767 | Ptr, Cmp, New, Alignment, SuccessOrdering, FailureOrdering, SSID)); |
1768 | } |
1769 | |
1770 | AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val, |
1771 | AtomicOrdering Ordering, |
1772 | SyncScope::ID SSID = SyncScope::System) { |
1773 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
1774 | Align Alignment(DL.getTypeStoreSize(Val->getType())); |
1775 | return Insert(new AtomicRMWInst(Op, Ptr, Val, Alignment, Ordering, SSID)); |
1776 | } |
1777 | |
1778 | Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList, |
1779 | const Twine &Name = "") { |
1780 | return CreateGEP(nullptr, Ptr, IdxList, Name); |
1781 | } |
1782 | |
1783 | Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, |
1784 | const Twine &Name = "") { |
1785 | if (auto *PC = dyn_cast<Constant>(Ptr)) { |
1786 | // Every index must be constant. |
1787 | size_t i, e; |
1788 | for (i = 0, e = IdxList.size(); i != e; ++i) |
1789 | if (!isa<Constant>(IdxList[i])) |
1790 | break; |
1791 | if (i == e) |
1792 | return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name); |
1793 | } |
1794 | return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name); |
1795 | } |
1796 | |
1797 | Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList, |
1798 | const Twine &Name = "") { |
1799 | return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name); |
1800 | } |
1801 | |
1802 | Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList, |
1803 | const Twine &Name = "") { |
1804 | if (auto *PC = dyn_cast<Constant>(Ptr)) { |
1805 | // Every index must be constant. |
1806 | size_t i, e; |
1807 | for (i = 0, e = IdxList.size(); i != e; ++i) |
1808 | if (!isa<Constant>(IdxList[i])) |
1809 | break; |
1810 | if (i == e) |
1811 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList), |
1812 | Name); |
1813 | } |
1814 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name); |
1815 | } |
1816 | |
1817 | Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") { |
1818 | return CreateGEP(nullptr, Ptr, Idx, Name); |
1819 | } |
1820 | |
1821 | Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") { |
1822 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1823 | if (auto *IC = dyn_cast<Constant>(Idx)) |
1824 | return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name); |
1825 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); |
1826 | } |
1827 | |
1828 | Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx, |
1829 | const Twine &Name = "") { |
1830 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1831 | if (auto *IC = dyn_cast<Constant>(Idx)) |
1832 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name); |
1833 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); |
1834 | } |
1835 | |
1836 | Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") { |
1837 | return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name); |
1838 | } |
1839 | |
1840 | Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, |
1841 | const Twine &Name = "") { |
1842 | Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); |
1843 | |
1844 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1845 | return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name); |
1846 | |
1847 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); |
1848 | } |
1849 | |
1850 | Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0, |
1851 | const Twine &Name = "") { |
1852 | Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0); |
1853 | |
1854 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1855 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name); |
1856 | |
1857 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); |
1858 | } |
1859 | |
1860 | Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1, |
1861 | const Twine &Name = "") { |
1862 | Value *Idxs[] = { |
1863 | ConstantInt::get(Type::getInt32Ty(Context), Idx0), |
1864 | ConstantInt::get(Type::getInt32Ty(Context), Idx1) |
1865 | }; |
1866 | |
1867 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1868 | return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name); |
1869 | |
1870 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); |
1871 | } |
1872 | |
1873 | Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, |
1874 | unsigned Idx1, const Twine &Name = "") { |
1875 | Value *Idxs[] = { |
1876 | ConstantInt::get(Type::getInt32Ty(Context), Idx0), |
1877 | ConstantInt::get(Type::getInt32Ty(Context), Idx1) |
1878 | }; |
1879 | |
1880 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1881 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name); |
1882 | |
1883 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); |
1884 | } |
1885 | |
1886 | Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, |
1887 | const Twine &Name = "") { |
1888 | Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); |
1889 | |
1890 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1891 | return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name); |
1892 | |
1893 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name); |
1894 | } |
1895 | |
1896 | Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") { |
1897 | return CreateConstGEP1_64(nullptr, Ptr, Idx0, Name); |
1898 | } |
1899 | |
1900 | Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, |
1901 | const Twine &Name = "") { |
1902 | Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0); |
1903 | |
1904 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1905 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name); |
1906 | |
1907 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name); |
1908 | } |
1909 | |
1910 | Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0, |
1911 | const Twine &Name = "") { |
1912 | return CreateConstInBoundsGEP1_64(nullptr, Ptr, Idx0, Name); |
1913 | } |
1914 | |
1915 | Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1, |
1916 | const Twine &Name = "") { |
1917 | Value *Idxs[] = { |
1918 | ConstantInt::get(Type::getInt64Ty(Context), Idx0), |
1919 | ConstantInt::get(Type::getInt64Ty(Context), Idx1) |
1920 | }; |
1921 | |
1922 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1923 | return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name); |
1924 | |
1925 | return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name); |
1926 | } |
1927 | |
1928 | Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, |
1929 | const Twine &Name = "") { |
1930 | return CreateConstGEP2_64(nullptr, Ptr, Idx0, Idx1, Name); |
1931 | } |
1932 | |
1933 | Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, |
1934 | uint64_t Idx1, const Twine &Name = "") { |
1935 | Value *Idxs[] = { |
1936 | ConstantInt::get(Type::getInt64Ty(Context), Idx0), |
1937 | ConstantInt::get(Type::getInt64Ty(Context), Idx1) |
1938 | }; |
1939 | |
1940 | if (auto *PC = dyn_cast<Constant>(Ptr)) |
1941 | return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name); |
1942 | |
1943 | return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name); |
1944 | } |
1945 | |
1946 | Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, |
1947 | const Twine &Name = "") { |
1948 | return CreateConstInBoundsGEP2_64(nullptr, Ptr, Idx0, Idx1, Name); |
1949 | } |
1950 | |
1951 | Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx, |
1952 | const Twine &Name = "") { |
1953 | return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name); |
1954 | } |
1955 | |
1956 | Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") { |
1957 | return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name); |
1958 | } |
1959 | |
1960 | /// Same as CreateGlobalString, but return a pointer with "i8*" type |
1961 | /// instead of a pointer to array of i8. |
1962 | /// |
1963 | /// If no module is given via \p M, it is take from the insertion point basic |
1964 | /// block. |
1965 | Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "", |
1966 | unsigned AddressSpace = 0, |
1967 | Module *M = nullptr) { |
1968 | GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M); |
1969 | Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0); |
1970 | Constant *Indices[] = {Zero, Zero}; |
1971 | return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV, |
1972 | Indices); |
1973 | } |
1974 | |
1975 | //===--------------------------------------------------------------------===// |
1976 | // Instruction creation methods: Cast/Conversion Operators |
1977 | //===--------------------------------------------------------------------===// |
1978 | |
1979 | Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") { |
1980 | return CreateCast(Instruction::Trunc, V, DestTy, Name); |
1981 | } |
1982 | |
1983 | Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") { |
1984 | return CreateCast(Instruction::ZExt, V, DestTy, Name); |
1985 | } |
1986 | |
1987 | Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") { |
1988 | return CreateCast(Instruction::SExt, V, DestTy, Name); |
1989 | } |
1990 | |
1991 | /// Create a ZExt or Trunc from the integer value V to DestTy. Return |
1992 | /// the value untouched if the type of V is already DestTy. |
1993 | Value *CreateZExtOrTrunc(Value *V, Type *DestTy, |
1994 | const Twine &Name = "") { |
1995 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1997, __PRETTY_FUNCTION__)) |
1996 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1997, __PRETTY_FUNCTION__)) |
1997 | "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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 1997, __PRETTY_FUNCTION__)); |
1998 | Type *VTy = V->getType(); |
1999 | if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) |
2000 | return CreateZExt(V, DestTy, Name); |
2001 | if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) |
2002 | return CreateTrunc(V, DestTy, Name); |
2003 | return V; |
2004 | } |
2005 | |
2006 | /// Create a SExt or Trunc from the integer value V to DestTy. Return |
2007 | /// the value untouched if the type of V is already DestTy. |
2008 | Value *CreateSExtOrTrunc(Value *V, Type *DestTy, |
2009 | const Twine &Name = "") { |
2010 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 2012, __PRETTY_FUNCTION__)) |
2011 | 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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 2012, __PRETTY_FUNCTION__)) |
2012 | "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~++20201202100633+b276bf5a572/llvm/include/llvm/IR/IRBuilder.h" , 2012, __PRETTY_FUNCTION__)); |
2013 | Type *VTy = V->getType(); |
2014 | if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits()) |
2015 | return CreateSExt(V, DestTy, Name); |
2016 | if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits()) |
2017 | return CreateTrunc(V, DestTy, Name); |
2018 | return V; |
2019 | } |
2020 | |
2021 | Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") { |
2022 | if (IsFPConstrained) |
2023 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui, |
2024 | V, DestTy, nullptr, Name); |
2025 | return CreateCast(Instruction::FPToUI, V, DestTy, Name); |
2026 | } |
2027 | |
2028 | Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") { |
2029 | if (IsFPConstrained) |
2030 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi, |
2031 | V, DestTy, nullptr, Name); |
2032 | return CreateCast(Instruction::FPToSI, V, DestTy, Name); |
2033 | } |
2034 | |
2035 | Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ |
2036 | if (IsFPConstrained) |
2037 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp, |
2038 | V, DestTy, nullptr, Name); |
2039 | return CreateCast(Instruction::UIToFP, V, DestTy, Name); |
2040 | } |
2041 | |
2042 | Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){ |
2043 | if (IsFPConstrained) |
2044 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp, |
2045 | V, DestTy, nullptr, Name); |
2046 | return CreateCast(Instruction::SIToFP, V, DestTy, Name); |
2047 | } |
2048 | |
2049 | Value *CreateFPTrunc(Value *V, Type *DestTy, |
2050 | const Twine &Name = "") { |
2051 | if (IsFPConstrained) |
2052 | return CreateConstrainedFPCast( |
2053 | Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr, |
2054 | Name); |
2055 | return CreateCast(Instruction::FPTrunc, V, DestTy, Name); |
2056 | } |
2057 | |
2058 | Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") { |
2059 | if (IsFPConstrained) |
2060 | return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext, |
2061 | V, DestTy, nullptr, Name); |
2062 | return CreateCast(Instruction::FPExt, V, DestTy, Name); |
2063 | } |
2064 | |
2065 | Value *CreatePtrToInt(Value *V, Type *DestTy, |
2066 | const Twine &Name = "") { |
2067 | return CreateCast(Instruction::PtrToInt, V, DestTy, Name); |
2068 | } |
2069 | |
2070 | Value *CreateIntToPtr(Value *V, Type *DestTy, |
2071 | const Twine &Name = "") { |
2072 | return CreateCast(Instruction::IntToPtr, V, DestTy, Name); |
2073 | } |
2074 | |
2075 | Value *CreateBitCast(Value *V, Type *DestTy, |
2076 | const Twine &Name = "") { |
2077 | return CreateCast(Instruction::BitCast, V, DestTy, Name); |
2078 | } |
2079 | |
2080 | Value *CreateAddrSpaceCast(Value *V, Type *DestTy, |
2081 | const Twine &Name = "") { |
2082 | return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name); |
2083 | } |
2084 | |
2085 | Value *CreateZExtOrBitCast(Value *V, Type *DestTy, |
2086 | const Twine &Name = "") { |
2087 | if (V->getType() == DestTy) |
2088 | return V; |
2089 | if (auto *VC = dyn_cast<Constant>(V)) |
2090 | return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name); |
2091 | return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name); |
2092 | } |
2093 | |
2094 | Value *CreateSExtOrBitCast(Value *V, Type *DestTy, |
2095 | const Twine &Name = "") { |
2096 | if (V->getType() == DestTy) |
2097 | return V; |
2098 | if (auto *VC = dyn_cast<Constant>(V)) |
2099 | return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name); |
2100 | return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name); |
2101 | } |
2102 | |
2103 | Value *CreateTruncOrBitCast(Value *V, Type *DestTy, |
2104 | const Twine &Name = "") { |
2105 | if (V->getType() == DestTy) |
2106 | return V; |
2107 | if (auto *VC = dyn_cast<Constant>(V)) |
2108 | return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name); |
2109 | return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name); |
2110 | } |
2111 | |
2112 | Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, |
2113 | const Twine &Name = "") { |
2114 | if (V->getType() == DestTy) |
2115 | return V; |
2116 | if (auto *VC = dyn_cast<Constant>(V)) |
2117 | return Insert(Folder.CreateCast(Op, VC, DestTy), Name); |
2118 | return Insert(CastInst::Create(Op, V, DestTy), Name); |
2119 | } |
2120 | |
2121 | Value *CreatePointerCast(Value *V, Type *DestTy, |
2122 | const Twine &Name = "") { |
2123 | if (V->getType() == DestTy) |
2124 | return V; |
2125 | if (auto *VC = dyn_cast<Constant>(V)) |
2126 | return Insert(Folder.CreatePointerCast(VC, DestTy), Name); |
2127 | return Insert(CastInst::CreatePointerCast(V, DestTy), Name); |
2128 | } |
2129 | |
2130 | Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy, |
2131 | const Twine &Name = "") { |
2132 | if (V->getType() == DestTy) |
2133 | return V; |
2134 | |
2135 | if (auto *VC = dyn_cast<Constant>(V)) { |
2136 | return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy), |
2137 | Name); |
2138 | } |
2139 | |
2140 | return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy), |
2141 | Name); |
2142 | } |
2143 | |
2144 | Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned, |
2145 | const Twine &Name = "") { |
2146 | if (V->getType() == DestTy) |
2147 | return V; |
2148 | if (auto *VC = dyn_cast<Constant>(V)) |
2149 | return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name); |
2150 | return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name); |
2151 | } |
2152 | |
2153 | Value *CreateBitOrPointerCast(Value *V, Type *DestTy, |
2154 | const Twine &Name = "") { |
2155 | if (V->getType() == DestTy) |
2156 | return V; |
2157 | if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy()) |
2158 | return CreatePtrToInt(V, DestTy, Name); |
2159 | if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy()) |
2160 | return CreateIntToPtr(V, DestTy, Name); |
2161 | |
2162 | return CreateBitCast(V, DestTy, Name); |
2163 | } |
2164 | |
2165 | Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") { |
2166 | if (V->getType() == DestTy) |
2167 | return V; |
2168 | if (auto *VC = dyn_cast<Constant>(V)) |
2169 | return Insert(Folder.CreateFPCast(VC, DestTy), Name); |
2170 | return Insert(CastInst::CreateFPCast(V, DestTy), Name); |
2171 | } |
2172 | |
2173 | CallInst *CreateConstrainedFPCast( |
2174 | Intrinsic::ID ID, Value *V, Type *DestTy, |
2175 | Instruction *FMFSource = nullptr, const Twine &Name = "", |
2176 | MDNode *FPMathTag = nullptr, |
2177 | Optional<RoundingMode> Rounding = None, |
2178 | Optional<fp::ExceptionBehavior> Except = None); |
2179 | |
2180 | // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a |
2181 | // compile time error, instead of converting the string to bool for the |
2182 | // isSigned parameter. |
2183 | Value *CreateIntCast(Value *, Type *, const char *) = delete; |
2184 | |
2185 | //===--------------------------------------------------------------------===// |
2186 | // Instruction creation methods: Compare Instructions |
2187 | //===--------------------------------------------------------------------===// |
2188 | |
2189 | Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") { |
2190 | return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name); |
2191 | } |
2192 | |
2193 | Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") { |
2194 | return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name); |
2195 | } |
2196 | |
2197 | Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") { |
2198 | return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name); |
2199 | } |
2200 | |
2201 | Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") { |
2202 | return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name); |
2203 | } |
2204 | |
2205 | Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") { |
2206 | return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name); |
2207 | } |
2208 | |
2209 | Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") { |
2210 | return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name); |
2211 | } |
2212 | |
2213 | Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") { |
2214 | return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name); |
2215 | } |
2216 | |
2217 | Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") { |
2218 | return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name); |
2219 | } |
2220 | |
2221 | Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") { |
2222 | return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name); |
2223 | } |
2224 | |
2225 | Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") { |
2226 | return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name); |
2227 | } |
2228 | |
2229 | Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "", |
2230 | MDNode *FPMathTag = nullptr) { |
2231 | return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag); |
2232 | } |
2233 | |
2234 | Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "", |
2235 | MDNode *FPMathTag = nullptr) { |
2236 | return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag); |
2237 | } |
2238 | |
2239 | Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "", |
2240 | MDNode *FPMathTag = nullptr) { |
2241 | return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag); |
2242 | } |
2243 | |
2244 | Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "", |
2245 | MDNode *FPMathTag = nullptr) { |
2246 | return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag); |
2247 | } |
2248 | |
2249 | Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "", |
2250 | MDNode *FPMathTag = nullptr) { |
2251 | return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag); |
2252 | } |
2253 | |
2254 | Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "", |
2255 | MDNode *FPMathTag = nullptr) { |
2256 | return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag); |
2257 | } |
2258 | |
2259 | Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "", |
2260 | MDNode *FPMathTag = nullptr) { |
2261 | return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag); |
2262 | } |
2263 | |
2264 | Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "", |
2265 | MDNode *FPMathTag = nullptr) { |
2266 | return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag); |
2267 | } |
2268 | |
2269 | Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "", |
2270 | MDNode *FPMathTag = nullptr) { |
2271 | return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag); |
2272 | } |
2273 | |
2274 | Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "", |
2275 | MDNode *FPMathTag = nullptr) { |
2276 | return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag); |
2277 | } |
2278 | |
2279 | Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "", |
2280 | MDNode *FPMathTag = nullptr) { |
2281 | return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag); |
2282 | } |
2283 | |
2284 | Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "", |
2285 | MDNode *FPMathTag = nullptr) { |
2286 | return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag); |
2287 | } |
2288 | |
2289 | Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "", |
2290 | MDNode *FPMathTag = nullptr) { |
2291 | return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag); |
2292 | } |
2293 | |
2294 | Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "", |
2295 | MDNode *FPMathTag = nullptr) { |
2296 | return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag); |
2297 | } |
2298 | |
2299 | Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, |
2300 | const Twine &Name = "") { |
2301 | if (auto *LC = dyn_cast<Constant>(LHS)) |
2302 | if (auto *RC = dyn_cast<Constant>(RHS)) |
2303 | return Insert(Folder.CreateICmp(P, LC, RC), Name); |
2304 | return Insert(new ICmpInst(P, LHS, RHS), Name); |
2305 | } |
2306 | |
2307 | // Create a quiet floating-point comparison (i.e. one that raises an FP |
2308 | // exception only in the case where an input is a signaling NaN). |
2309 | // Note that this differs from CreateFCmpS only if IsFPConstrained is true. |
2310 | Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, |
2311 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { |
2312 | return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, false); |
2313 | } |
2314 | |
2315 | Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
2316 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { |
2317 | return CmpInst::isFPPredicate(Pred) |
2318 | ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag) |
2319 | : CreateICmp(Pred, LHS, RHS, Name); |
2320 | } |
2321 | |
2322 | // Create a signaling floating-point comparison (i.e. one that raises an FP |
2323 | // exception whenever an input is any NaN, signaling or quiet). |
2324 | // Note that this differs from CreateFCmp only if IsFPConstrained is true. |
2325 | Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS, |
2326 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { |
2327 | return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, true); |
2328 | } |
2329 | |
2330 | private: |
2331 | // Helper routine to create either a signaling or a quiet FP comparison. |
2332 | Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS, |
2333 | const Twine &Name, MDNode *FPMathTag, |
2334 | bool IsSignaling); |
2335 | |
2336 | public: |
2337 | CallInst *CreateConstrainedFPCmp( |
2338 | Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R, |
2339 | const Twine &Name = "", Optional<fp::ExceptionBehavior> Except = None); |
2340 | |
2341 | //===--------------------------------------------------------------------===// |
2342 | // Instruction creation methods: Other Instructions |
2343 | //===--------------------------------------------------------------------===// |
2344 | |
2345 | PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues, |
2346 | const Twine &Name = "") { |
2347 | PHINode *Phi = PHINode::Create(Ty, NumReservedValues); |
2348 | if (isa<FPMathOperator>(Phi)) |
2349 | setFPAttrs(Phi, nullptr /* MDNode* */, FMF); |
2350 | return Insert(Phi, Name); |
2351 | } |
2352 | |
2353 | CallInst *CreateCall(FunctionType *FTy, Value *Callee, |
2354 | ArrayRef<Value *> Args = None, const Twine &Name = "", |
2355 | MDNode *FPMathTag = nullptr) { |
2356 | CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles); |
2357 | if (IsFPConstrained) |
2358 | setConstrainedFPCallAttr(CI); |
2359 | if (isa<FPMathOperator>(CI)) |
2360 | setFPAttrs(CI, FPMathTag, FMF); |
2361 | return Insert(CI, Name); |
2362 | } |
2363 | |
2364 | CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args, |
2365 | ArrayRef<OperandBundleDef> OpBundles, |
2366 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { |
2367 | CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles); |
2368 | if (IsFPConstrained) |
2369 | setConstrainedFPCallAttr(CI); |
2370 | if (isa<FPMathOperator>(CI)) |
2371 | setFPAttrs(CI, FPMathTag, FMF); |
2372 | return Insert(CI, Name); |
2373 | } |
2374 | |
2375 | CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None, |
2376 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { |
2377 | return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name, |
2378 | FPMathTag); |
2379 | } |
2380 | |
2381 | CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args, |
2382 | ArrayRef<OperandBundleDef> OpBundles, |
2383 | const Twine &Name = "", MDNode *FPMathTag = nullptr) { |
2384 | return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, |
2385 | OpBundles, Name, FPMathTag); |
2386 | } |
2387 | |
2388 | CallInst *CreateConstrainedFPCall( |
2389 | Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "", |
2390 | Optional<RoundingMode> Rounding = None, |
2391 | Optional<fp::ExceptionBehavior> Except = None); |
2392 | |
2393 | Value *CreateSelect(Value *C, Value *True, Value *False, |
2394 | const Twine &Name = "", Instruction *MDFrom = nullptr); |
2395 | |
2396 | VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") { |
2397 | return Insert(new VAArgInst(List, Ty), Name); |
2398 | } |
2399 | |
2400 | Value *CreateExtractElement(Value *Vec, Value *Idx, |
2401 | const Twine &Name = "") { |
2402 | if (auto *VC = dyn_cast<Constant>(Vec)) |
2403 | if (auto *IC = dyn_cast<Constant>(Idx)) |
2404 | return Insert(Folder.CreateExtractElement(VC, IC), Name); |
2405 | return Insert(ExtractElementInst::Create(Vec, Idx), Name); |
2406 | } |
2407 | |
2408 | Value *CreateExtractElement(Value *Vec, uint64_t Idx, |
2409 | const Twine &Name = "") { |
2410 | return CreateExtractElement(Vec, getInt64(Idx), Name); |
2411 | } |
2412 | |
2413 | Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, |
2414 | const Twine &Name = "") { |
2415 | if (auto *VC = dyn_cast<Constant>(Vec)) |
2416 | if (auto *NC = dyn_cast<Constant>(NewElt)) |
2417 | if (auto *IC = dyn_cast<Constant>(Idx)) |
2418 | return Insert(Folder.CreateInsertElement(VC, NC, IC), Name); |
2419 | return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name); |
2420 | } |
2421 | |
2422 | Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx, |
2423 | const Twine &Name = "") { |
2424 | return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name); |
2425 | } |
2426 | |
2427 | Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask, |
2428 | const Twine &Name = "") { |
2429 | SmallVector<int, 16> IntMask; |
2430 | ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask); |
2431 | return CreateShuffleVector(V1, V2, IntMask, Name); |
2432 | } |
2433 | |
2434 | LLVM_ATTRIBUTE_DEPRECATED(Value *CreateShuffleVector(Value *V1, Value *V2,Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef< uint32_t> Mask, const Twine &Name = "") __attribute__( (deprecated("Pass indices as 'int' instead"))) |
2435 | ArrayRef<uint32_t> Mask,Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef< uint32_t> Mask, const Twine &Name = "") __attribute__( (deprecated("Pass indices as 'int' instead"))) |
2436 | const Twine &Name = ""),Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef< uint32_t> Mask, const Twine &Name = "") __attribute__( (deprecated("Pass indices as 'int' instead"))) |
2437 | "Pass indices as 'int' instead")Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef< uint32_t> Mask, const Twine &Name = "") __attribute__( (deprecated("Pass indices as 'int' instead"))) { |
2438 | SmallVector<int, 16> IntMask; |
2439 | IntMask.assign(Mask.begin(), Mask.end()); |
2440 | return CreateShuffleVector(V1, V2, IntMask, Name); |
2441 | } |
2442 | |
2443 | /// See class ShuffleVectorInst for a description of the mask representation. |
2444 | Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask, |
2445 | const Twine &Name = "") { |
2446 | if (auto *V1C = dyn_cast<Constant>(V1)) |
2447 | if (auto *V2C = dyn_cast<Constant>(V2)) |
2448 | return Insert(Folder.CreateShuffleVector(V1C, V2C, Mask), Name); |
2449 | return Insert(new ShuffleVectorInst(V1, V2, Mask), Name); |
2450 | } |
2451 | |
2452 | /// Create a unary shuffle. The second vector operand of the IR instruction |
2453 | /// is undefined. |
2454 | Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask, |
2455 | const Twine &Name = "") { |
2456 | return CreateShuffleVector(V, UndefValue::get(V->getType()), Mask, Name); |
2457 | } |
2458 | |
2459 | Value *CreateExtractValue(Value *Agg, |
2460 | ArrayRef<unsigned> Idxs, |
2461 | const Twine &Name = "") { |
2462 | if (auto *AggC = dyn_cast<Constant>(Agg)) |
2463 | return Insert(Folder.CreateExtractValue(AggC, Idxs), Name); |
2464 | return Insert(ExtractValueInst::Create(Agg, Idxs), Name); |
2465 | } |
2466 | |
2467 | Value *CreateInsertValue(Value *Agg, Value *Val, |
2468 | ArrayRef<unsigned> Idxs, |
2469 | const Twine &Name = "") { |
2470 | if (auto *AggC = dyn_cast<Constant>(Agg)) |
2471 | if (auto *ValC = dyn_cast<Constant>(Val)) |
2472 | return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name); |
2473 | return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name); |
2474 | } |
2475 | |
2476 | LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses, |
2477 | const Twine &Name = "") { |
2478 | return Insert(LandingPadInst::Create(Ty, NumClauses), Name); |
2479 | } |
2480 | |
2481 | Value *CreateFreeze(Value *V, const Twine &Name = "") { |
2482 | return Insert(new FreezeInst(V), Name); |
2483 | } |
2484 | |
2485 | //===--------------------------------------------------------------------===// |
2486 | // Utility creation methods |
2487 | //===--------------------------------------------------------------------===// |
2488 | |
2489 | /// Return an i1 value testing if \p Arg is null. |
2490 | Value *CreateIsNull(Value *Arg, const Twine &Name = "") { |
2491 | return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), |
2492 | Name); |
2493 | } |
2494 | |
2495 | /// Return an i1 value testing if \p Arg is not null. |
2496 | Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") { |
2497 | return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), |
2498 | Name); |
2499 | } |
2500 | |
2501 | /// Return the i64 difference between two pointer values, dividing out |
2502 | /// the size of the pointed-to objects. |
2503 | /// |
2504 | /// This is intended to implement C-style pointer subtraction. As such, the |
2505 | /// pointers must be appropriately aligned for their element types and |
2506 | /// pointing into the same object. |
2507 | Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = ""); |
2508 | |
2509 | /// Create a launder.invariant.group intrinsic call. If Ptr type is |
2510 | /// different from pointer to i8, it's casted to pointer to i8 in the same |
2511 | /// address space before call and casted back to Ptr type after call. |
2512 | Value *CreateLaunderInvariantGroup(Value *Ptr); |
2513 | |
2514 | /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is |
2515 | /// different from pointer to i8, it's casted to pointer to i8 in the same |
2516 | /// address space before call and casted back to Ptr type after call. |
2517 | Value *CreateStripInvariantGroup(Value *Ptr); |
2518 | |
2519 | /// Return a vector value that contains \arg V broadcasted to \p |
2520 | /// NumElts elements. |
2521 | Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = ""); |
2522 | |
2523 | /// Return a vector value that contains \arg V broadcasted to \p |
2524 | /// EC elements. |
2525 | Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = ""); |
2526 | |
2527 | /// Return a value that has been extracted from a larger integer type. |
2528 | Value *CreateExtractInteger(const DataLayout &DL, Value *From, |
2529 | IntegerType *ExtractedTy, uint64_t Offset, |
2530 | const Twine &Name); |
2531 | |
2532 | Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base, |
2533 | unsigned Dimension, unsigned LastIndex, |
2534 | MDNode *DbgInfo); |
2535 | |
2536 | Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex, |
2537 | MDNode *DbgInfo); |
2538 | |
2539 | Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base, |
2540 | unsigned Index, unsigned FieldIndex, |
2541 | MDNode *DbgInfo); |
2542 | |
2543 | private: |
2544 | /// Helper function that creates an assume intrinsic call that |
2545 | /// represents an alignment assumption on the provided pointer \p PtrValue |
2546 | /// with offset \p OffsetValue and alignment value \p AlignValue. |
2547 | CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL, |
2548 | Value *PtrValue, Value *AlignValue, |
2549 | Value *OffsetValue); |
2550 | |
2551 | public: |
2552 | /// Create an assume intrinsic call that represents an alignment |
2553 | /// assumption on the provided pointer. |
2554 | /// |
2555 | /// An optional offset can be provided, and if it is provided, the offset |
2556 | /// must be subtracted from the provided pointer to get the pointer with the |
2557 | /// specified alignment. |
2558 | CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, |
2559 | unsigned Alignment, |
2560 | Value *OffsetValue = nullptr); |
2561 | |
2562 | /// Create an assume intrinsic call that represents an alignment |
2563 | /// assumption on the provided pointer. |
2564 | /// |
2565 | /// An optional offset can be provided, and if it is provided, the offset |
2566 | /// must be subtracted from the provided pointer to get the pointer with the |
2567 | /// specified alignment. |
2568 | /// |
2569 | /// This overload handles the condition where the Alignment is dependent |
2570 | /// on an existing value rather than a static value. |
2571 | CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue, |
2572 | Value *Alignment, |
2573 | Value *OffsetValue = nullptr); |
2574 | }; |
2575 | |
2576 | /// This provides a uniform API for creating instructions and inserting |
2577 | /// them into a basic block: either at the end of a BasicBlock, or at a specific |
2578 | /// iterator location in a block. |
2579 | /// |
2580 | /// Note that the builder does not expose the full generality of LLVM |
2581 | /// instructions. For access to extra instruction properties, use the mutators |
2582 | /// (e.g. setVolatile) on the instructions after they have been |
2583 | /// created. Convenience state exists to specify fast-math flags and fp-math |
2584 | /// tags. |
2585 | /// |
2586 | /// The first template argument specifies a class to use for creating constants. |
2587 | /// This defaults to creating minimally folded constants. The second template |
2588 | /// argument allows clients to specify custom insertion hooks that are called on |
2589 | /// every newly created insertion. |
2590 | template <typename FolderTy = ConstantFolder, |
2591 | typename InserterTy = IRBuilderDefaultInserter> |
2592 | class IRBuilder : public IRBuilderBase { |
2593 | private: |
2594 | FolderTy Folder; |
2595 | InserterTy Inserter; |
2596 | |
2597 | public: |
2598 | IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(), |
2599 | MDNode *FPMathTag = nullptr, |
2600 | ArrayRef<OperandBundleDef> OpBundles = None) |
2601 | : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles), |
2602 | Folder(Folder), Inserter(Inserter) {} |
2603 | |
2604 | explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr, |
2605 | ArrayRef<OperandBundleDef> OpBundles = None) |
2606 | : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {} |
2607 | |
2608 | explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder, |
2609 | MDNode *FPMathTag = nullptr, |
2610 | ArrayRef<OperandBundleDef> OpBundles = None) |
2611 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, |
2612 | FPMathTag, OpBundles), Folder(Folder) { |
2613 | SetInsertPoint(TheBB); |
2614 | } |
2615 | |
2616 | explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr, |
2617 | ArrayRef<OperandBundleDef> OpBundles = None) |
2618 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, |
2619 | FPMathTag, OpBundles) { |
2620 | SetInsertPoint(TheBB); |
2621 | } |
2622 | |
2623 | explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr, |
2624 | ArrayRef<OperandBundleDef> OpBundles = None) |
2625 | : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, |
2626 | FPMathTag, OpBundles) { |
2627 | SetInsertPoint(IP); |
2628 | } |
2629 | |
2630 | IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder, |
2631 | MDNode *FPMathTag = nullptr, |
2632 | ArrayRef<OperandBundleDef> OpBundles = None) |
2633 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, |
2634 | FPMathTag, OpBundles), Folder(Folder) { |
2635 | SetInsertPoint(TheBB, IP); |
2636 | } |
2637 | |
2638 | IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, |
2639 | MDNode *FPMathTag = nullptr, |
2640 | ArrayRef<OperandBundleDef> OpBundles = None) |
2641 | : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter, |
2642 | FPMathTag, OpBundles) { |
2643 | SetInsertPoint(TheBB, IP); |
2644 | } |
2645 | |
2646 | /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard |
2647 | /// or FastMathFlagGuard instead. |
2648 | IRBuilder(const IRBuilder &) = delete; |
2649 | |
2650 | InserterTy &getInserter() { return Inserter; } |
2651 | }; |
2652 | |
2653 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
2654 | DEFINE_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)); } |
2655 | |
2656 | } // end namespace llvm |
2657 | |
2658 | #endif // LLVM_IR_IRBUILDER_H |
1 | //===- llvm/Instructions.h - Instruction subclass definitions ---*- 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 exposes the class definitions of all of the subclasses of the | |||
10 | // Instruction class. This is meant to be an easy way to get access to all | |||
11 | // instruction subclasses. | |||
12 | // | |||
13 | //===----------------------------------------------------------------------===// | |||
14 | ||||
15 | #ifndef LLVM_IR_INSTRUCTIONS_H | |||
16 | #define LLVM_IR_INSTRUCTIONS_H | |||
17 | ||||
18 | #include "llvm/ADT/ArrayRef.h" | |||
19 | #include "llvm/ADT/Bitfields.h" | |||
20 | #include "llvm/ADT/None.h" | |||
21 | #include "llvm/ADT/STLExtras.h" | |||
22 | #include "llvm/ADT/SmallVector.h" | |||
23 | #include "llvm/ADT/StringRef.h" | |||
24 | #include "llvm/ADT/Twine.h" | |||
25 | #include "llvm/ADT/iterator.h" | |||
26 | #include "llvm/ADT/iterator_range.h" | |||
27 | #include "llvm/IR/Attributes.h" | |||
28 | #include "llvm/IR/BasicBlock.h" | |||
29 | #include "llvm/IR/CallingConv.h" | |||
30 | #include "llvm/IR/CFG.h" | |||
31 | #include "llvm/IR/Constant.h" | |||
32 | #include "llvm/IR/DerivedTypes.h" | |||
33 | #include "llvm/IR/Function.h" | |||
34 | #include "llvm/IR/InstrTypes.h" | |||
35 | #include "llvm/IR/Instruction.h" | |||
36 | #include "llvm/IR/OperandTraits.h" | |||
37 | #include "llvm/IR/Type.h" | |||
38 | #include "llvm/IR/Use.h" | |||
39 | #include "llvm/IR/User.h" | |||
40 | #include "llvm/IR/Value.h" | |||
41 | #include "llvm/Support/AtomicOrdering.h" | |||
42 | #include "llvm/Support/Casting.h" | |||
43 | #include "llvm/Support/ErrorHandling.h" | |||
44 | #include <cassert> | |||
45 | #include <cstddef> | |||
46 | #include <cstdint> | |||
47 | #include <iterator> | |||
48 | ||||
49 | namespace llvm { | |||
50 | ||||
51 | class APInt; | |||
52 | class ConstantInt; | |||
53 | class DataLayout; | |||
54 | class LLVMContext; | |||
55 | ||||
56 | //===----------------------------------------------------------------------===// | |||
57 | // AllocaInst Class | |||
58 | //===----------------------------------------------------------------------===// | |||
59 | ||||
60 | /// an instruction to allocate memory on the stack | |||
61 | class AllocaInst : public UnaryInstruction { | |||
62 | Type *AllocatedType; | |||
63 | ||||
64 | using AlignmentField = AlignmentBitfieldElementT<0>; | |||
65 | using UsedWithInAllocaField = BoolBitfieldElementT<AlignmentField::NextBit>; | |||
66 | using SwiftErrorField = BoolBitfieldElementT<UsedWithInAllocaField::NextBit>; | |||
67 | static_assert(Bitfield::areContiguous<AlignmentField, UsedWithInAllocaField, | |||
68 | SwiftErrorField>(), | |||
69 | "Bitfields must be contiguous"); | |||
70 | ||||
71 | protected: | |||
72 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
73 | friend class Instruction; | |||
74 | ||||
75 | AllocaInst *cloneImpl() const; | |||
76 | ||||
77 | public: | |||
78 | explicit AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
79 | const Twine &Name, Instruction *InsertBefore); | |||
80 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, | |||
81 | const Twine &Name, BasicBlock *InsertAtEnd); | |||
82 | ||||
83 | AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, | |||
84 | Instruction *InsertBefore); | |||
85 | AllocaInst(Type *Ty, unsigned AddrSpace, | |||
86 | const Twine &Name, BasicBlock *InsertAtEnd); | |||
87 | ||||
88 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align, | |||
89 | const Twine &Name = "", Instruction *InsertBefore = nullptr); | |||
90 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align, | |||
91 | const Twine &Name, BasicBlock *InsertAtEnd); | |||
92 | ||||
93 | /// Return true if there is an allocation size parameter to the allocation | |||
94 | /// instruction that is not 1. | |||
95 | bool isArrayAllocation() const; | |||
96 | ||||
97 | /// Get the number of elements allocated. For a simple allocation of a single | |||
98 | /// element, this will return a constant 1 value. | |||
99 | const Value *getArraySize() const { return getOperand(0); } | |||
100 | Value *getArraySize() { return getOperand(0); } | |||
101 | ||||
102 | /// Overload to return most specific pointer type. | |||
103 | PointerType *getType() const { | |||
104 | return cast<PointerType>(Instruction::getType()); | |||
105 | } | |||
106 | ||||
107 | /// Get allocation size in bits. Returns None if size can't be determined, | |||
108 | /// e.g. in case of a VLA. | |||
109 | Optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const; | |||
110 | ||||
111 | /// Return the type that is being allocated by the instruction. | |||
112 | Type *getAllocatedType() const { return AllocatedType; } | |||
113 | /// for use only in special circumstances that need to generically | |||
114 | /// transform a whole instruction (eg: IR linking and vectorization). | |||
115 | void setAllocatedType(Type *Ty) { AllocatedType = Ty; } | |||
116 | ||||
117 | /// Return the alignment of the memory that is being allocated by the | |||
118 | /// instruction. | |||
119 | Align getAlign() const { | |||
120 | return Align(1ULL << getSubclassData<AlignmentField>()); | |||
121 | } | |||
122 | ||||
123 | void setAlignment(Align Align) { | |||
124 | setSubclassData<AlignmentField>(Log2(Align)); | |||
125 | } | |||
126 | ||||
127 | // FIXME: Remove this one transition to Align is over. | |||
128 | unsigned getAlignment() const { return getAlign().value(); } | |||
129 | ||||
130 | /// Return true if this alloca is in the entry block of the function and is a | |||
131 | /// constant size. If so, the code generator will fold it into the | |||
132 | /// prolog/epilog code, so it is basically free. | |||
133 | bool isStaticAlloca() const; | |||
134 | ||||
135 | /// Return true if this alloca is used as an inalloca argument to a call. Such | |||
136 | /// allocas are never considered static even if they are in the entry block. | |||
137 | bool isUsedWithInAlloca() const { | |||
138 | return getSubclassData<UsedWithInAllocaField>(); | |||
139 | } | |||
140 | ||||
141 | /// Specify whether this alloca is used to represent the arguments to a call. | |||
142 | void setUsedWithInAlloca(bool V) { | |||
143 | setSubclassData<UsedWithInAllocaField>(V); | |||
144 | } | |||
145 | ||||
146 | /// Return true if this alloca is used as a swifterror argument to a call. | |||
147 | bool isSwiftError() const { return getSubclassData<SwiftErrorField>(); } | |||
148 | /// Specify whether this alloca is used to represent a swifterror. | |||
149 | void setSwiftError(bool V) { setSubclassData<SwiftErrorField>(V); } | |||
150 | ||||
151 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
152 | static bool classof(const Instruction *I) { | |||
153 | return (I->getOpcode() == Instruction::Alloca); | |||
154 | } | |||
155 | static bool classof(const Value *V) { | |||
156 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
157 | } | |||
158 | ||||
159 | private: | |||
160 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
161 | // method so that subclasses cannot accidentally use it. | |||
162 | template <typename Bitfield> | |||
163 | void setSubclassData(typename Bitfield::Type Value) { | |||
164 | Instruction::setSubclassData<Bitfield>(Value); | |||
165 | } | |||
166 | }; | |||
167 | ||||
168 | //===----------------------------------------------------------------------===// | |||
169 | // LoadInst Class | |||
170 | //===----------------------------------------------------------------------===// | |||
171 | ||||
172 | /// An instruction for reading from memory. This uses the SubclassData field in | |||
173 | /// Value to store whether or not the load is volatile. | |||
174 | class LoadInst : public UnaryInstruction { | |||
175 | using VolatileField = BoolBitfieldElementT<0>; | |||
176 | using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>; | |||
177 | using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>; | |||
178 | static_assert( | |||
179 | Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(), | |||
180 | "Bitfields must be contiguous"); | |||
181 | ||||
182 | void AssertOK(); | |||
183 | ||||
184 | protected: | |||
185 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
186 | friend class Instruction; | |||
187 | ||||
188 | LoadInst *cloneImpl() const; | |||
189 | ||||
190 | public: | |||
191 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, | |||
192 | Instruction *InsertBefore); | |||
193 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
194 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
195 | Instruction *InsertBefore); | |||
196 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
197 | BasicBlock *InsertAtEnd); | |||
198 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
199 | Align Align, Instruction *InsertBefore = nullptr); | |||
200 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
201 | Align Align, BasicBlock *InsertAtEnd); | |||
202 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
203 | Align Align, AtomicOrdering Order, | |||
204 | SyncScope::ID SSID = SyncScope::System, | |||
205 | Instruction *InsertBefore = nullptr); | |||
206 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, | |||
207 | Align Align, AtomicOrdering Order, SyncScope::ID SSID, | |||
208 | BasicBlock *InsertAtEnd); | |||
209 | ||||
210 | /// Return true if this is a load from a volatile memory location. | |||
211 | bool isVolatile() const { return getSubclassData<VolatileField>(); } | |||
212 | ||||
213 | /// Specify whether this is a volatile load or not. | |||
214 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } | |||
215 | ||||
216 | /// Return the alignment of the access that is being performed. | |||
217 | /// FIXME: Remove this function once transition to Align is over. | |||
218 | /// Use getAlign() instead. | |||
219 | unsigned getAlignment() const { return getAlign().value(); } | |||
220 | ||||
221 | /// Return the alignment of the access that is being performed. | |||
222 | Align getAlign() const { | |||
223 | return Align(1ULL << (getSubclassData<AlignmentField>())); | |||
224 | } | |||
225 | ||||
226 | void setAlignment(Align Align) { | |||
227 | setSubclassData<AlignmentField>(Log2(Align)); | |||
228 | } | |||
229 | ||||
230 | /// Returns the ordering constraint of this load instruction. | |||
231 | AtomicOrdering getOrdering() const { | |||
232 | return getSubclassData<OrderingField>(); | |||
233 | } | |||
234 | /// Sets the ordering constraint of this load instruction. May not be Release | |||
235 | /// or AcquireRelease. | |||
236 | void setOrdering(AtomicOrdering Ordering) { | |||
237 | setSubclassData<OrderingField>(Ordering); | |||
238 | } | |||
239 | ||||
240 | /// Returns the synchronization scope ID of this load instruction. | |||
241 | SyncScope::ID getSyncScopeID() const { | |||
242 | return SSID; | |||
243 | } | |||
244 | ||||
245 | /// Sets the synchronization scope ID of this load instruction. | |||
246 | void setSyncScopeID(SyncScope::ID SSID) { | |||
247 | this->SSID = SSID; | |||
248 | } | |||
249 | ||||
250 | /// Sets the ordering constraint and the synchronization scope ID of this load | |||
251 | /// instruction. | |||
252 | void setAtomic(AtomicOrdering Ordering, | |||
253 | SyncScope::ID SSID = SyncScope::System) { | |||
254 | setOrdering(Ordering); | |||
255 | setSyncScopeID(SSID); | |||
256 | } | |||
257 | ||||
258 | bool isSimple() const { return !isAtomic() && !isVolatile(); } | |||
259 | ||||
260 | bool isUnordered() const { | |||
261 | return (getOrdering() == AtomicOrdering::NotAtomic || | |||
262 | getOrdering() == AtomicOrdering::Unordered) && | |||
263 | !isVolatile(); | |||
264 | } | |||
265 | ||||
266 | Value *getPointerOperand() { return getOperand(0); } | |||
267 | const Value *getPointerOperand() const { return getOperand(0); } | |||
268 | static unsigned getPointerOperandIndex() { return 0U; } | |||
269 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } | |||
270 | ||||
271 | /// Returns the address space of the pointer operand. | |||
272 | unsigned getPointerAddressSpace() const { | |||
273 | return getPointerOperandType()->getPointerAddressSpace(); | |||
274 | } | |||
275 | ||||
276 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
277 | static bool classof(const Instruction *I) { | |||
278 | return I->getOpcode() == Instruction::Load; | |||
279 | } | |||
280 | static bool classof(const Value *V) { | |||
281 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
282 | } | |||
283 | ||||
284 | private: | |||
285 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
286 | // method so that subclasses cannot accidentally use it. | |||
287 | template <typename Bitfield> | |||
288 | void setSubclassData(typename Bitfield::Type Value) { | |||
289 | Instruction::setSubclassData<Bitfield>(Value); | |||
290 | } | |||
291 | ||||
292 | /// The synchronization scope ID of this load instruction. Not quite enough | |||
293 | /// room in SubClassData for everything, so synchronization scope ID gets its | |||
294 | /// own field. | |||
295 | SyncScope::ID SSID; | |||
296 | }; | |||
297 | ||||
298 | //===----------------------------------------------------------------------===// | |||
299 | // StoreInst Class | |||
300 | //===----------------------------------------------------------------------===// | |||
301 | ||||
302 | /// An instruction for storing to memory. | |||
303 | class StoreInst : public Instruction { | |||
304 | using VolatileField = BoolBitfieldElementT<0>; | |||
305 | using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>; | |||
306 | using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>; | |||
307 | static_assert( | |||
308 | Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(), | |||
309 | "Bitfields must be contiguous"); | |||
310 | ||||
311 | void AssertOK(); | |||
312 | ||||
313 | protected: | |||
314 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
315 | friend class Instruction; | |||
316 | ||||
317 | StoreInst *cloneImpl() const; | |||
318 | ||||
319 | public: | |||
320 | StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); | |||
321 | StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); | |||
322 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Instruction *InsertBefore); | |||
323 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); | |||
324 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, | |||
325 | Instruction *InsertBefore = nullptr); | |||
326 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, | |||
327 | BasicBlock *InsertAtEnd); | |||
328 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, | |||
329 | AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System, | |||
330 | Instruction *InsertBefore = nullptr); | |||
331 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, | |||
332 | AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd); | |||
333 | ||||
334 | // allocate space for exactly two operands | |||
335 | void *operator new(size_t s) { | |||
336 | return User::operator new(s, 2); | |||
337 | } | |||
338 | ||||
339 | /// Return true if this is a store to a volatile memory location. | |||
340 | bool isVolatile() const { return getSubclassData<VolatileField>(); } | |||
341 | ||||
342 | /// Specify whether this is a volatile store or not. | |||
343 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } | |||
344 | ||||
345 | /// Transparently provide more efficient getOperand methods. | |||
346 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
347 | ||||
348 | /// Return the alignment of the access that is being performed | |||
349 | /// FIXME: Remove this function once transition to Align is over. | |||
350 | /// Use getAlign() instead. | |||
351 | unsigned getAlignment() const { return getAlign().value(); } | |||
352 | ||||
353 | Align getAlign() const { | |||
354 | return Align(1ULL << (getSubclassData<AlignmentField>())); | |||
355 | } | |||
356 | ||||
357 | void setAlignment(Align Align) { | |||
358 | setSubclassData<AlignmentField>(Log2(Align)); | |||
359 | } | |||
360 | ||||
361 | /// Returns the ordering constraint of this store instruction. | |||
362 | AtomicOrdering getOrdering() const { | |||
363 | return getSubclassData<OrderingField>(); | |||
364 | } | |||
365 | ||||
366 | /// Sets the ordering constraint of this store instruction. May not be | |||
367 | /// Acquire or AcquireRelease. | |||
368 | void setOrdering(AtomicOrdering Ordering) { | |||
369 | setSubclassData<OrderingField>(Ordering); | |||
370 | } | |||
371 | ||||
372 | /// Returns the synchronization scope ID of this store instruction. | |||
373 | SyncScope::ID getSyncScopeID() const { | |||
374 | return SSID; | |||
375 | } | |||
376 | ||||
377 | /// Sets the synchronization scope ID of this store instruction. | |||
378 | void setSyncScopeID(SyncScope::ID SSID) { | |||
379 | this->SSID = SSID; | |||
380 | } | |||
381 | ||||
382 | /// Sets the ordering constraint and the synchronization scope ID of this | |||
383 | /// store instruction. | |||
384 | void setAtomic(AtomicOrdering Ordering, | |||
385 | SyncScope::ID SSID = SyncScope::System) { | |||
386 | setOrdering(Ordering); | |||
387 | setSyncScopeID(SSID); | |||
388 | } | |||
389 | ||||
390 | bool isSimple() const { return !isAtomic() && !isVolatile(); } | |||
391 | ||||
392 | bool isUnordered() const { | |||
393 | return (getOrdering() == AtomicOrdering::NotAtomic || | |||
394 | getOrdering() == AtomicOrdering::Unordered) && | |||
395 | !isVolatile(); | |||
396 | } | |||
397 | ||||
398 | Value *getValueOperand() { return getOperand(0); } | |||
399 | const Value *getValueOperand() const { return getOperand(0); } | |||
400 | ||||
401 | Value *getPointerOperand() { return getOperand(1); } | |||
402 | const Value *getPointerOperand() const { return getOperand(1); } | |||
403 | static unsigned getPointerOperandIndex() { return 1U; } | |||
404 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } | |||
405 | ||||
406 | /// Returns the address space of the pointer operand. | |||
407 | unsigned getPointerAddressSpace() const { | |||
408 | return getPointerOperandType()->getPointerAddressSpace(); | |||
409 | } | |||
410 | ||||
411 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
412 | static bool classof(const Instruction *I) { | |||
413 | return I->getOpcode() == Instruction::Store; | |||
414 | } | |||
415 | static bool classof(const Value *V) { | |||
416 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
417 | } | |||
418 | ||||
419 | private: | |||
420 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
421 | // method so that subclasses cannot accidentally use it. | |||
422 | template <typename Bitfield> | |||
423 | void setSubclassData(typename Bitfield::Type Value) { | |||
424 | Instruction::setSubclassData<Bitfield>(Value); | |||
425 | } | |||
426 | ||||
427 | /// The synchronization scope ID of this store instruction. Not quite enough | |||
428 | /// room in SubClassData for everything, so synchronization scope ID gets its | |||
429 | /// own field. | |||
430 | SyncScope::ID SSID; | |||
431 | }; | |||
432 | ||||
433 | template <> | |||
434 | struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { | |||
435 | }; | |||
436 | ||||
437 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)StoreInst::op_iterator StoreInst::op_begin() { return OperandTraits <StoreInst>::op_begin(this); } StoreInst::const_op_iterator StoreInst::op_begin() const { return OperandTraits<StoreInst >::op_begin(const_cast<StoreInst*>(this)); } StoreInst ::op_iterator StoreInst::op_end() { return OperandTraits<StoreInst >::op_end(this); } StoreInst::const_op_iterator StoreInst:: op_end() const { return OperandTraits<StoreInst>::op_end (const_cast<StoreInst*>(this)); } Value *StoreInst::getOperand (unsigned i_nocapture) const { ((i_nocapture < OperandTraits <StoreInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 437, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<StoreInst>::op_begin(const_cast<StoreInst *>(this))[i_nocapture].get()); } void StoreInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<StoreInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 437, __PRETTY_FUNCTION__)); OperandTraits<StoreInst>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned StoreInst ::getNumOperands() const { return OperandTraits<StoreInst> ::operands(this); } template <int Idx_nocapture> Use & StoreInst::Op() { return this->OpFrom<Idx_nocapture> (this); } template <int Idx_nocapture> const Use &StoreInst ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } | |||
438 | ||||
439 | //===----------------------------------------------------------------------===// | |||
440 | // FenceInst Class | |||
441 | //===----------------------------------------------------------------------===// | |||
442 | ||||
443 | /// An instruction for ordering other memory operations. | |||
444 | class FenceInst : public Instruction { | |||
445 | using OrderingField = AtomicOrderingBitfieldElementT<0>; | |||
446 | ||||
447 | void Init(AtomicOrdering Ordering, SyncScope::ID SSID); | |||
448 | ||||
449 | protected: | |||
450 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
451 | friend class Instruction; | |||
452 | ||||
453 | FenceInst *cloneImpl() const; | |||
454 | ||||
455 | public: | |||
456 | // Ordering may only be Acquire, Release, AcquireRelease, or | |||
457 | // SequentiallyConsistent. | |||
458 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, | |||
459 | SyncScope::ID SSID = SyncScope::System, | |||
460 | Instruction *InsertBefore = nullptr); | |||
461 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID, | |||
462 | BasicBlock *InsertAtEnd); | |||
463 | ||||
464 | // allocate space for exactly zero operands | |||
465 | void *operator new(size_t s) { | |||
466 | return User::operator new(s, 0); | |||
467 | } | |||
468 | ||||
469 | /// Returns the ordering constraint of this fence instruction. | |||
470 | AtomicOrdering getOrdering() const { | |||
471 | return getSubclassData<OrderingField>(); | |||
472 | } | |||
473 | ||||
474 | /// Sets the ordering constraint of this fence instruction. May only be | |||
475 | /// Acquire, Release, AcquireRelease, or SequentiallyConsistent. | |||
476 | void setOrdering(AtomicOrdering Ordering) { | |||
477 | setSubclassData<OrderingField>(Ordering); | |||
478 | } | |||
479 | ||||
480 | /// Returns the synchronization scope ID of this fence instruction. | |||
481 | SyncScope::ID getSyncScopeID() const { | |||
482 | return SSID; | |||
483 | } | |||
484 | ||||
485 | /// Sets the synchronization scope ID of this fence instruction. | |||
486 | void setSyncScopeID(SyncScope::ID SSID) { | |||
487 | this->SSID = SSID; | |||
488 | } | |||
489 | ||||
490 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
491 | static bool classof(const Instruction *I) { | |||
492 | return I->getOpcode() == Instruction::Fence; | |||
493 | } | |||
494 | static bool classof(const Value *V) { | |||
495 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
496 | } | |||
497 | ||||
498 | private: | |||
499 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
500 | // method so that subclasses cannot accidentally use it. | |||
501 | template <typename Bitfield> | |||
502 | void setSubclassData(typename Bitfield::Type Value) { | |||
503 | Instruction::setSubclassData<Bitfield>(Value); | |||
504 | } | |||
505 | ||||
506 | /// The synchronization scope ID of this fence instruction. Not quite enough | |||
507 | /// room in SubClassData for everything, so synchronization scope ID gets its | |||
508 | /// own field. | |||
509 | SyncScope::ID SSID; | |||
510 | }; | |||
511 | ||||
512 | //===----------------------------------------------------------------------===// | |||
513 | // AtomicCmpXchgInst Class | |||
514 | //===----------------------------------------------------------------------===// | |||
515 | ||||
516 | /// An instruction that atomically checks whether a | |||
517 | /// specified value is in a memory location, and, if it is, stores a new value | |||
518 | /// there. The value returned by this instruction is a pair containing the | |||
519 | /// original value as first element, and an i1 indicating success (true) or | |||
520 | /// failure (false) as second element. | |||
521 | /// | |||
522 | class AtomicCmpXchgInst : public Instruction { | |||
523 | void Init(Value *Ptr, Value *Cmp, Value *NewVal, Align Align, | |||
524 | AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, | |||
525 | SyncScope::ID SSID); | |||
526 | ||||
527 | template <unsigned Offset> | |||
528 | using AtomicOrderingBitfieldElement = | |||
529 | typename Bitfield::Element<AtomicOrdering, Offset, 3, | |||
530 | AtomicOrdering::LAST>; | |||
531 | ||||
532 | protected: | |||
533 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
534 | friend class Instruction; | |||
535 | ||||
536 | AtomicCmpXchgInst *cloneImpl() const; | |||
537 | ||||
538 | public: | |||
539 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment, | |||
540 | AtomicOrdering SuccessOrdering, | |||
541 | AtomicOrdering FailureOrdering, SyncScope::ID SSID, | |||
542 | Instruction *InsertBefore = nullptr); | |||
543 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment, | |||
544 | AtomicOrdering SuccessOrdering, | |||
545 | AtomicOrdering FailureOrdering, SyncScope::ID SSID, | |||
546 | BasicBlock *InsertAtEnd); | |||
547 | ||||
548 | // allocate space for exactly three operands | |||
549 | void *operator new(size_t s) { | |||
550 | return User::operator new(s, 3); | |||
551 | } | |||
552 | ||||
553 | using VolatileField = BoolBitfieldElementT<0>; | |||
554 | using WeakField = BoolBitfieldElementT<VolatileField::NextBit>; | |||
555 | using SuccessOrderingField = | |||
556 | AtomicOrderingBitfieldElementT<WeakField::NextBit>; | |||
557 | using FailureOrderingField = | |||
558 | AtomicOrderingBitfieldElementT<SuccessOrderingField::NextBit>; | |||
559 | using AlignmentField = | |||
560 | AlignmentBitfieldElementT<FailureOrderingField::NextBit>; | |||
561 | static_assert( | |||
562 | Bitfield::areContiguous<VolatileField, WeakField, SuccessOrderingField, | |||
563 | FailureOrderingField, AlignmentField>(), | |||
564 | "Bitfields must be contiguous"); | |||
565 | ||||
566 | /// Return the alignment of the memory that is being allocated by the | |||
567 | /// instruction. | |||
568 | Align getAlign() const { | |||
569 | return Align(1ULL << getSubclassData<AlignmentField>()); | |||
570 | } | |||
571 | ||||
572 | void setAlignment(Align Align) { | |||
573 | setSubclassData<AlignmentField>(Log2(Align)); | |||
574 | } | |||
575 | ||||
576 | /// Return true if this is a cmpxchg from a volatile memory | |||
577 | /// location. | |||
578 | /// | |||
579 | bool isVolatile() const { return getSubclassData<VolatileField>(); } | |||
580 | ||||
581 | /// Specify whether this is a volatile cmpxchg. | |||
582 | /// | |||
583 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } | |||
584 | ||||
585 | /// Return true if this cmpxchg may spuriously fail. | |||
586 | bool isWeak() const { return getSubclassData<WeakField>(); } | |||
587 | ||||
588 | void setWeak(bool IsWeak) { setSubclassData<WeakField>(IsWeak); } | |||
589 | ||||
590 | /// Transparently provide more efficient getOperand methods. | |||
591 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
592 | ||||
593 | /// Returns the success ordering constraint of this cmpxchg instruction. | |||
594 | AtomicOrdering getSuccessOrdering() const { | |||
595 | return getSubclassData<SuccessOrderingField>(); | |||
596 | } | |||
597 | ||||
598 | /// Sets the success ordering constraint of this cmpxchg instruction. | |||
599 | void setSuccessOrdering(AtomicOrdering Ordering) { | |||
600 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 601, __PRETTY_FUNCTION__)) | |||
601 | "CmpXchg instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 601, __PRETTY_FUNCTION__)); | |||
602 | setSubclassData<SuccessOrderingField>(Ordering); | |||
603 | } | |||
604 | ||||
605 | /// Returns the failure ordering constraint of this cmpxchg instruction. | |||
606 | AtomicOrdering getFailureOrdering() const { | |||
607 | return getSubclassData<FailureOrderingField>(); | |||
608 | } | |||
609 | ||||
610 | /// Sets the failure ordering constraint of this cmpxchg instruction. | |||
611 | void setFailureOrdering(AtomicOrdering Ordering) { | |||
612 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 613, __PRETTY_FUNCTION__)) | |||
613 | "CmpXchg instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "CmpXchg instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"CmpXchg instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 613, __PRETTY_FUNCTION__)); | |||
614 | setSubclassData<FailureOrderingField>(Ordering); | |||
615 | } | |||
616 | ||||
617 | /// Returns the synchronization scope ID of this cmpxchg instruction. | |||
618 | SyncScope::ID getSyncScopeID() const { | |||
619 | return SSID; | |||
620 | } | |||
621 | ||||
622 | /// Sets the synchronization scope ID of this cmpxchg instruction. | |||
623 | void setSyncScopeID(SyncScope::ID SSID) { | |||
624 | this->SSID = SSID; | |||
625 | } | |||
626 | ||||
627 | Value *getPointerOperand() { return getOperand(0); } | |||
628 | const Value *getPointerOperand() const { return getOperand(0); } | |||
629 | static unsigned getPointerOperandIndex() { return 0U; } | |||
630 | ||||
631 | Value *getCompareOperand() { return getOperand(1); } | |||
632 | const Value *getCompareOperand() const { return getOperand(1); } | |||
633 | ||||
634 | Value *getNewValOperand() { return getOperand(2); } | |||
635 | const Value *getNewValOperand() const { return getOperand(2); } | |||
636 | ||||
637 | /// Returns the address space of the pointer operand. | |||
638 | unsigned getPointerAddressSpace() const { | |||
639 | return getPointerOperand()->getType()->getPointerAddressSpace(); | |||
640 | } | |||
641 | ||||
642 | /// Returns the strongest permitted ordering on failure, given the | |||
643 | /// desired ordering on success. | |||
644 | /// | |||
645 | /// If the comparison in a cmpxchg operation fails, there is no atomic store | |||
646 | /// so release semantics cannot be provided. So this function drops explicit | |||
647 | /// Release requests from the AtomicOrdering. A SequentiallyConsistent | |||
648 | /// operation would remain SequentiallyConsistent. | |||
649 | static AtomicOrdering | |||
650 | getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) { | |||
651 | switch (SuccessOrdering) { | |||
652 | default: | |||
653 | llvm_unreachable("invalid cmpxchg success ordering")::llvm::llvm_unreachable_internal("invalid cmpxchg success ordering" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 653); | |||
654 | case AtomicOrdering::Release: | |||
655 | case AtomicOrdering::Monotonic: | |||
656 | return AtomicOrdering::Monotonic; | |||
657 | case AtomicOrdering::AcquireRelease: | |||
658 | case AtomicOrdering::Acquire: | |||
659 | return AtomicOrdering::Acquire; | |||
660 | case AtomicOrdering::SequentiallyConsistent: | |||
661 | return AtomicOrdering::SequentiallyConsistent; | |||
662 | } | |||
663 | } | |||
664 | ||||
665 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
666 | static bool classof(const Instruction *I) { | |||
667 | return I->getOpcode() == Instruction::AtomicCmpXchg; | |||
668 | } | |||
669 | static bool classof(const Value *V) { | |||
670 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
671 | } | |||
672 | ||||
673 | private: | |||
674 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
675 | // method so that subclasses cannot accidentally use it. | |||
676 | template <typename Bitfield> | |||
677 | void setSubclassData(typename Bitfield::Type Value) { | |||
678 | Instruction::setSubclassData<Bitfield>(Value); | |||
679 | } | |||
680 | ||||
681 | /// The synchronization scope ID of this cmpxchg instruction. Not quite | |||
682 | /// enough room in SubClassData for everything, so synchronization scope ID | |||
683 | /// gets its own field. | |||
684 | SyncScope::ID SSID; | |||
685 | }; | |||
686 | ||||
687 | template <> | |||
688 | struct OperandTraits<AtomicCmpXchgInst> : | |||
689 | public FixedNumOperandTraits<AtomicCmpXchgInst, 3> { | |||
690 | }; | |||
691 | ||||
692 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)AtomicCmpXchgInst::op_iterator AtomicCmpXchgInst::op_begin() { return OperandTraits<AtomicCmpXchgInst>::op_begin(this ); } AtomicCmpXchgInst::const_op_iterator AtomicCmpXchgInst:: op_begin() const { return OperandTraits<AtomicCmpXchgInst> ::op_begin(const_cast<AtomicCmpXchgInst*>(this)); } AtomicCmpXchgInst ::op_iterator AtomicCmpXchgInst::op_end() { return OperandTraits <AtomicCmpXchgInst>::op_end(this); } AtomicCmpXchgInst:: const_op_iterator AtomicCmpXchgInst::op_end() const { return OperandTraits <AtomicCmpXchgInst>::op_end(const_cast<AtomicCmpXchgInst *>(this)); } Value *AtomicCmpXchgInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<AtomicCmpXchgInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 692, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<AtomicCmpXchgInst>::op_begin(const_cast <AtomicCmpXchgInst*>(this))[i_nocapture].get()); } void AtomicCmpXchgInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<AtomicCmpXchgInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 692, __PRETTY_FUNCTION__)); OperandTraits<AtomicCmpXchgInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned AtomicCmpXchgInst::getNumOperands() const { return OperandTraits <AtomicCmpXchgInst>::operands(this); } template <int Idx_nocapture> Use &AtomicCmpXchgInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &AtomicCmpXchgInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
693 | ||||
694 | //===----------------------------------------------------------------------===// | |||
695 | // AtomicRMWInst Class | |||
696 | //===----------------------------------------------------------------------===// | |||
697 | ||||
698 | /// an instruction that atomically reads a memory location, | |||
699 | /// combines it with another value, and then stores the result back. Returns | |||
700 | /// the old value. | |||
701 | /// | |||
702 | class AtomicRMWInst : public Instruction { | |||
703 | protected: | |||
704 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
705 | friend class Instruction; | |||
706 | ||||
707 | AtomicRMWInst *cloneImpl() const; | |||
708 | ||||
709 | public: | |||
710 | /// This enumeration lists the possible modifications atomicrmw can make. In | |||
711 | /// the descriptions, 'p' is the pointer to the instruction's memory location, | |||
712 | /// 'old' is the initial value of *p, and 'v' is the other value passed to the | |||
713 | /// instruction. These instructions always return 'old'. | |||
714 | enum BinOp : unsigned { | |||
715 | /// *p = v | |||
716 | Xchg, | |||
717 | /// *p = old + v | |||
718 | Add, | |||
719 | /// *p = old - v | |||
720 | Sub, | |||
721 | /// *p = old & v | |||
722 | And, | |||
723 | /// *p = ~(old & v) | |||
724 | Nand, | |||
725 | /// *p = old | v | |||
726 | Or, | |||
727 | /// *p = old ^ v | |||
728 | Xor, | |||
729 | /// *p = old >signed v ? old : v | |||
730 | Max, | |||
731 | /// *p = old <signed v ? old : v | |||
732 | Min, | |||
733 | /// *p = old >unsigned v ? old : v | |||
734 | UMax, | |||
735 | /// *p = old <unsigned v ? old : v | |||
736 | UMin, | |||
737 | ||||
738 | /// *p = old + v | |||
739 | FAdd, | |||
740 | ||||
741 | /// *p = old - v | |||
742 | FSub, | |||
743 | ||||
744 | FIRST_BINOP = Xchg, | |||
745 | LAST_BINOP = FSub, | |||
746 | BAD_BINOP | |||
747 | }; | |||
748 | ||||
749 | private: | |||
750 | template <unsigned Offset> | |||
751 | using AtomicOrderingBitfieldElement = | |||
752 | typename Bitfield::Element<AtomicOrdering, Offset, 3, | |||
753 | AtomicOrdering::LAST>; | |||
754 | ||||
755 | template <unsigned Offset> | |||
756 | using BinOpBitfieldElement = | |||
757 | typename Bitfield::Element<BinOp, Offset, 4, BinOp::LAST_BINOP>; | |||
758 | ||||
759 | public: | |||
760 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment, | |||
761 | AtomicOrdering Ordering, SyncScope::ID SSID, | |||
762 | Instruction *InsertBefore = nullptr); | |||
763 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment, | |||
764 | AtomicOrdering Ordering, SyncScope::ID SSID, | |||
765 | BasicBlock *InsertAtEnd); | |||
766 | ||||
767 | // allocate space for exactly two operands | |||
768 | void *operator new(size_t s) { | |||
769 | return User::operator new(s, 2); | |||
770 | } | |||
771 | ||||
772 | using VolatileField = BoolBitfieldElementT<0>; | |||
773 | using AtomicOrderingField = | |||
774 | AtomicOrderingBitfieldElementT<VolatileField::NextBit>; | |||
775 | using OperationField = BinOpBitfieldElement<AtomicOrderingField::NextBit>; | |||
776 | using AlignmentField = AlignmentBitfieldElementT<OperationField::NextBit>; | |||
777 | static_assert(Bitfield::areContiguous<VolatileField, AtomicOrderingField, | |||
778 | OperationField, AlignmentField>(), | |||
779 | "Bitfields must be contiguous"); | |||
780 | ||||
781 | BinOp getOperation() const { return getSubclassData<OperationField>(); } | |||
782 | ||||
783 | static StringRef getOperationName(BinOp Op); | |||
784 | ||||
785 | static bool isFPOperation(BinOp Op) { | |||
786 | switch (Op) { | |||
787 | case AtomicRMWInst::FAdd: | |||
788 | case AtomicRMWInst::FSub: | |||
789 | return true; | |||
790 | default: | |||
791 | return false; | |||
792 | } | |||
793 | } | |||
794 | ||||
795 | void setOperation(BinOp Operation) { | |||
796 | setSubclassData<OperationField>(Operation); | |||
797 | } | |||
798 | ||||
799 | /// Return the alignment of the memory that is being allocated by the | |||
800 | /// instruction. | |||
801 | Align getAlign() const { | |||
802 | return Align(1ULL << getSubclassData<AlignmentField>()); | |||
803 | } | |||
804 | ||||
805 | void setAlignment(Align Align) { | |||
806 | setSubclassData<AlignmentField>(Log2(Align)); | |||
807 | } | |||
808 | ||||
809 | /// Return true if this is a RMW on a volatile memory location. | |||
810 | /// | |||
811 | bool isVolatile() const { return getSubclassData<VolatileField>(); } | |||
812 | ||||
813 | /// Specify whether this is a volatile RMW or not. | |||
814 | /// | |||
815 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } | |||
816 | ||||
817 | /// Transparently provide more efficient getOperand methods. | |||
818 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
819 | ||||
820 | /// Returns the ordering constraint of this rmw instruction. | |||
821 | AtomicOrdering getOrdering() const { | |||
822 | return getSubclassData<AtomicOrderingField>(); | |||
823 | } | |||
824 | ||||
825 | /// Sets the ordering constraint of this rmw instruction. | |||
826 | void setOrdering(AtomicOrdering Ordering) { | |||
827 | assert(Ordering != AtomicOrdering::NotAtomic &&((Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 828, __PRETTY_FUNCTION__)) | |||
828 | "atomicrmw instructions can only be atomic.")((Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic." ) ? static_cast<void> (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 828, __PRETTY_FUNCTION__)); | |||
829 | setSubclassData<AtomicOrderingField>(Ordering); | |||
830 | } | |||
831 | ||||
832 | /// Returns the synchronization scope ID of this rmw instruction. | |||
833 | SyncScope::ID getSyncScopeID() const { | |||
834 | return SSID; | |||
835 | } | |||
836 | ||||
837 | /// Sets the synchronization scope ID of this rmw instruction. | |||
838 | void setSyncScopeID(SyncScope::ID SSID) { | |||
839 | this->SSID = SSID; | |||
840 | } | |||
841 | ||||
842 | Value *getPointerOperand() { return getOperand(0); } | |||
843 | const Value *getPointerOperand() const { return getOperand(0); } | |||
844 | static unsigned getPointerOperandIndex() { return 0U; } | |||
845 | ||||
846 | Value *getValOperand() { return getOperand(1); } | |||
847 | const Value *getValOperand() const { return getOperand(1); } | |||
848 | ||||
849 | /// Returns the address space of the pointer operand. | |||
850 | unsigned getPointerAddressSpace() const { | |||
851 | return getPointerOperand()->getType()->getPointerAddressSpace(); | |||
852 | } | |||
853 | ||||
854 | bool isFloatingPointOperation() const { | |||
855 | return isFPOperation(getOperation()); | |||
856 | } | |||
857 | ||||
858 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
859 | static bool classof(const Instruction *I) { | |||
860 | return I->getOpcode() == Instruction::AtomicRMW; | |||
861 | } | |||
862 | static bool classof(const Value *V) { | |||
863 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
864 | } | |||
865 | ||||
866 | private: | |||
867 | void Init(BinOp Operation, Value *Ptr, Value *Val, Align Align, | |||
868 | AtomicOrdering Ordering, SyncScope::ID SSID); | |||
869 | ||||
870 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
871 | // method so that subclasses cannot accidentally use it. | |||
872 | template <typename Bitfield> | |||
873 | void setSubclassData(typename Bitfield::Type Value) { | |||
874 | Instruction::setSubclassData<Bitfield>(Value); | |||
875 | } | |||
876 | ||||
877 | /// The synchronization scope ID of this rmw instruction. Not quite enough | |||
878 | /// room in SubClassData for everything, so synchronization scope ID gets its | |||
879 | /// own field. | |||
880 | SyncScope::ID SSID; | |||
881 | }; | |||
882 | ||||
883 | template <> | |||
884 | struct OperandTraits<AtomicRMWInst> | |||
885 | : public FixedNumOperandTraits<AtomicRMWInst,2> { | |||
886 | }; | |||
887 | ||||
888 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)AtomicRMWInst::op_iterator AtomicRMWInst::op_begin() { return OperandTraits<AtomicRMWInst>::op_begin(this); } AtomicRMWInst ::const_op_iterator AtomicRMWInst::op_begin() const { return OperandTraits <AtomicRMWInst>::op_begin(const_cast<AtomicRMWInst*> (this)); } AtomicRMWInst::op_iterator AtomicRMWInst::op_end() { return OperandTraits<AtomicRMWInst>::op_end(this); } AtomicRMWInst::const_op_iterator AtomicRMWInst::op_end() const { return OperandTraits<AtomicRMWInst>::op_end(const_cast <AtomicRMWInst*>(this)); } Value *AtomicRMWInst::getOperand (unsigned i_nocapture) const { ((i_nocapture < OperandTraits <AtomicRMWInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 888, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<AtomicRMWInst>::op_begin(const_cast< AtomicRMWInst*>(this))[i_nocapture].get()); } void AtomicRMWInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<AtomicRMWInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 888, __PRETTY_FUNCTION__)); OperandTraits<AtomicRMWInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned AtomicRMWInst ::getNumOperands() const { return OperandTraits<AtomicRMWInst >::operands(this); } template <int Idx_nocapture> Use &AtomicRMWInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & AtomicRMWInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
889 | ||||
890 | //===----------------------------------------------------------------------===// | |||
891 | // GetElementPtrInst Class | |||
892 | //===----------------------------------------------------------------------===// | |||
893 | ||||
894 | // checkGEPType - Simple wrapper function to give a better assertion failure | |||
895 | // message on bad indexes for a gep instruction. | |||
896 | // | |||
897 | inline Type *checkGEPType(Type *Ty) { | |||
898 | assert(Ty && "Invalid GetElementPtrInst indices for type!")((Ty && "Invalid GetElementPtrInst indices for type!" ) ? static_cast<void> (0) : __assert_fail ("Ty && \"Invalid GetElementPtrInst indices for type!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 898, __PRETTY_FUNCTION__)); | |||
899 | return Ty; | |||
900 | } | |||
901 | ||||
902 | /// an instruction for type-safe pointer arithmetic to | |||
903 | /// access elements of arrays and structs | |||
904 | /// | |||
905 | class GetElementPtrInst : public Instruction { | |||
906 | Type *SourceElementType; | |||
907 | Type *ResultElementType; | |||
908 | ||||
909 | GetElementPtrInst(const GetElementPtrInst &GEPI); | |||
910 | ||||
911 | /// Constructors - Create a getelementptr instruction with a base pointer an | |||
912 | /// list of indices. The first ctor can optionally insert before an existing | |||
913 | /// instruction, the second appends the new instruction to the specified | |||
914 | /// BasicBlock. | |||
915 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, | |||
916 | ArrayRef<Value *> IdxList, unsigned Values, | |||
917 | const Twine &NameStr, Instruction *InsertBefore); | |||
918 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, | |||
919 | ArrayRef<Value *> IdxList, unsigned Values, | |||
920 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
921 | ||||
922 | void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr); | |||
923 | ||||
924 | protected: | |||
925 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
926 | friend class Instruction; | |||
927 | ||||
928 | GetElementPtrInst *cloneImpl() const; | |||
929 | ||||
930 | public: | |||
931 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, | |||
932 | ArrayRef<Value *> IdxList, | |||
933 | const Twine &NameStr = "", | |||
934 | Instruction *InsertBefore = nullptr) { | |||
935 | unsigned Values = 1 + unsigned(IdxList.size()); | |||
936 | if (!PointeeType) | |||
937 | PointeeType = | |||
938 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(); | |||
939 | else | |||
940 | assert(((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 942, __PRETTY_FUNCTION__)) | |||
| ||||
941 | PointeeType ==((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 942, __PRETTY_FUNCTION__)) | |||
942 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType())((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 942, __PRETTY_FUNCTION__)); | |||
943 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, | |||
944 | NameStr, InsertBefore); | |||
945 | } | |||
946 | ||||
947 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, | |||
948 | ArrayRef<Value *> IdxList, | |||
949 | const Twine &NameStr, | |||
950 | BasicBlock *InsertAtEnd) { | |||
951 | unsigned Values = 1 + unsigned(IdxList.size()); | |||
952 | if (!PointeeType) | |||
953 | PointeeType = | |||
954 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(); | |||
955 | else | |||
956 | assert(((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 958, __PRETTY_FUNCTION__)) | |||
957 | PointeeType ==((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 958, __PRETTY_FUNCTION__)) | |||
958 | cast<PointerType>(Ptr->getType()->getScalarType())->getElementType())((PointeeType == cast<PointerType>(Ptr->getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("PointeeType == cast<PointerType>(Ptr->getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 958, __PRETTY_FUNCTION__)); | |||
959 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, | |||
960 | NameStr, InsertAtEnd); | |||
961 | } | |||
962 | ||||
963 | /// Create an "inbounds" getelementptr. See the documentation for the | |||
964 | /// "inbounds" flag in LangRef.html for details. | |||
965 | static GetElementPtrInst *CreateInBounds(Value *Ptr, | |||
966 | ArrayRef<Value *> IdxList, | |||
967 | const Twine &NameStr = "", | |||
968 | Instruction *InsertBefore = nullptr){ | |||
969 | return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore); | |||
970 | } | |||
971 | ||||
972 | static GetElementPtrInst * | |||
973 | CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList, | |||
974 | const Twine &NameStr = "", | |||
975 | Instruction *InsertBefore = nullptr) { | |||
976 | GetElementPtrInst *GEP = | |||
977 | Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore); | |||
978 | GEP->setIsInBounds(true); | |||
979 | return GEP; | |||
980 | } | |||
981 | ||||
982 | static GetElementPtrInst *CreateInBounds(Value *Ptr, | |||
983 | ArrayRef<Value *> IdxList, | |||
984 | const Twine &NameStr, | |||
985 | BasicBlock *InsertAtEnd) { | |||
986 | return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd); | |||
987 | } | |||
988 | ||||
989 | static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr, | |||
990 | ArrayRef<Value *> IdxList, | |||
991 | const Twine &NameStr, | |||
992 | BasicBlock *InsertAtEnd) { | |||
993 | GetElementPtrInst *GEP = | |||
994 | Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd); | |||
995 | GEP->setIsInBounds(true); | |||
996 | return GEP; | |||
997 | } | |||
998 | ||||
999 | /// Transparently provide more efficient getOperand methods. | |||
1000 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
1001 | ||||
1002 | Type *getSourceElementType() const { return SourceElementType; } | |||
1003 | ||||
1004 | void setSourceElementType(Type *Ty) { SourceElementType = Ty; } | |||
1005 | void setResultElementType(Type *Ty) { ResultElementType = Ty; } | |||
1006 | ||||
1007 | Type *getResultElementType() const { | |||
1008 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1009, __PRETTY_FUNCTION__)) | |||
1009 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1009, __PRETTY_FUNCTION__)); | |||
1010 | return ResultElementType; | |||
1011 | } | |||
1012 | ||||
1013 | /// Returns the address space of this instruction's pointer type. | |||
1014 | unsigned getAddressSpace() const { | |||
1015 | // Note that this is always the same as the pointer operand's address space | |||
1016 | // and that is cheaper to compute, so cheat here. | |||
1017 | return getPointerAddressSpace(); | |||
1018 | } | |||
1019 | ||||
1020 | /// Returns the result type of a getelementptr with the given source | |||
1021 | /// element type and indexes. | |||
1022 | /// | |||
1023 | /// Null is returned if the indices are invalid for the specified | |||
1024 | /// source element type. | |||
1025 | static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList); | |||
1026 | static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList); | |||
1027 | static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList); | |||
1028 | ||||
1029 | /// Return the type of the element at the given index of an indexable | |||
1030 | /// type. This is equivalent to "getIndexedType(Agg, {Zero, Idx})". | |||
1031 | /// | |||
1032 | /// Returns null if the type can't be indexed, or the given index is not | |||
1033 | /// legal for the given type. | |||
1034 | static Type *getTypeAtIndex(Type *Ty, Value *Idx); | |||
1035 | static Type *getTypeAtIndex(Type *Ty, uint64_t Idx); | |||
1036 | ||||
1037 | inline op_iterator idx_begin() { return op_begin()+1; } | |||
1038 | inline const_op_iterator idx_begin() const { return op_begin()+1; } | |||
1039 | inline op_iterator idx_end() { return op_end(); } | |||
1040 | inline const_op_iterator idx_end() const { return op_end(); } | |||
1041 | ||||
1042 | inline iterator_range<op_iterator> indices() { | |||
1043 | return make_range(idx_begin(), idx_end()); | |||
1044 | } | |||
1045 | ||||
1046 | inline iterator_range<const_op_iterator> indices() const { | |||
1047 | return make_range(idx_begin(), idx_end()); | |||
1048 | } | |||
1049 | ||||
1050 | Value *getPointerOperand() { | |||
1051 | return getOperand(0); | |||
1052 | } | |||
1053 | const Value *getPointerOperand() const { | |||
1054 | return getOperand(0); | |||
1055 | } | |||
1056 | static unsigned getPointerOperandIndex() { | |||
1057 | return 0U; // get index for modifying correct operand. | |||
1058 | } | |||
1059 | ||||
1060 | /// Method to return the pointer operand as a | |||
1061 | /// PointerType. | |||
1062 | Type *getPointerOperandType() const { | |||
1063 | return getPointerOperand()->getType(); | |||
1064 | } | |||
1065 | ||||
1066 | /// Returns the address space of the pointer operand. | |||
1067 | unsigned getPointerAddressSpace() const { | |||
1068 | return getPointerOperandType()->getPointerAddressSpace(); | |||
1069 | } | |||
1070 | ||||
1071 | /// Returns the pointer type returned by the GEP | |||
1072 | /// instruction, which may be a vector of pointers. | |||
1073 | static Type *getGEPReturnType(Type *ElTy, Value *Ptr, | |||
1074 | ArrayRef<Value *> IdxList) { | |||
1075 | Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)), | |||
1076 | Ptr->getType()->getPointerAddressSpace()); | |||
1077 | // Vector GEP | |||
1078 | if (auto *PtrVTy = dyn_cast<VectorType>(Ptr->getType())) { | |||
1079 | ElementCount EltCount = PtrVTy->getElementCount(); | |||
1080 | return VectorType::get(PtrTy, EltCount); | |||
1081 | } | |||
1082 | for (Value *Index : IdxList) | |||
1083 | if (auto *IndexVTy = dyn_cast<VectorType>(Index->getType())) { | |||
1084 | ElementCount EltCount = IndexVTy->getElementCount(); | |||
1085 | return VectorType::get(PtrTy, EltCount); | |||
1086 | } | |||
1087 | // Scalar GEP | |||
1088 | return PtrTy; | |||
1089 | } | |||
1090 | ||||
1091 | unsigned getNumIndices() const { // Note: always non-negative | |||
1092 | return getNumOperands() - 1; | |||
1093 | } | |||
1094 | ||||
1095 | bool hasIndices() const { | |||
1096 | return getNumOperands() > 1; | |||
1097 | } | |||
1098 | ||||
1099 | /// Return true if all of the indices of this GEP are | |||
1100 | /// zeros. If so, the result pointer and the first operand have the same | |||
1101 | /// value, just potentially different types. | |||
1102 | bool hasAllZeroIndices() const; | |||
1103 | ||||
1104 | /// Return true if all of the indices of this GEP are | |||
1105 | /// constant integers. If so, the result pointer and the first operand have | |||
1106 | /// a constant offset between them. | |||
1107 | bool hasAllConstantIndices() const; | |||
1108 | ||||
1109 | /// Set or clear the inbounds flag on this GEP instruction. | |||
1110 | /// See LangRef.html for the meaning of inbounds on a getelementptr. | |||
1111 | void setIsInBounds(bool b = true); | |||
1112 | ||||
1113 | /// Determine whether the GEP has the inbounds flag. | |||
1114 | bool isInBounds() const; | |||
1115 | ||||
1116 | /// Accumulate the constant address offset of this GEP if possible. | |||
1117 | /// | |||
1118 | /// This routine accepts an APInt into which it will accumulate the constant | |||
1119 | /// offset of this GEP if the GEP is in fact constant. If the GEP is not | |||
1120 | /// all-constant, it returns false and the value of the offset APInt is | |||
1121 | /// undefined (it is *not* preserved!). The APInt passed into this routine | |||
1122 | /// must be at least as wide as the IntPtr type for the address space of | |||
1123 | /// the base GEP pointer. | |||
1124 | bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const; | |||
1125 | ||||
1126 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1127 | static bool classof(const Instruction *I) { | |||
1128 | return (I->getOpcode() == Instruction::GetElementPtr); | |||
1129 | } | |||
1130 | static bool classof(const Value *V) { | |||
1131 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1132 | } | |||
1133 | }; | |||
1134 | ||||
1135 | template <> | |||
1136 | struct OperandTraits<GetElementPtrInst> : | |||
1137 | public VariadicOperandTraits<GetElementPtrInst, 1> { | |||
1138 | }; | |||
1139 | ||||
1140 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, | |||
1141 | ArrayRef<Value *> IdxList, unsigned Values, | |||
1142 | const Twine &NameStr, | |||
1143 | Instruction *InsertBefore) | |||
1144 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, | |||
1145 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, | |||
1146 | Values, InsertBefore), | |||
1147 | SourceElementType(PointeeType), | |||
1148 | ResultElementType(getIndexedType(PointeeType, IdxList)) { | |||
1149 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1150, __PRETTY_FUNCTION__)) | |||
1150 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1150, __PRETTY_FUNCTION__)); | |||
1151 | init(Ptr, IdxList, NameStr); | |||
1152 | } | |||
1153 | ||||
1154 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, | |||
1155 | ArrayRef<Value *> IdxList, unsigned Values, | |||
1156 | const Twine &NameStr, | |||
1157 | BasicBlock *InsertAtEnd) | |||
1158 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, | |||
1159 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, | |||
1160 | Values, InsertAtEnd), | |||
1161 | SourceElementType(PointeeType), | |||
1162 | ResultElementType(getIndexedType(PointeeType, IdxList)) { | |||
1163 | assert(ResultElementType ==((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1164, __PRETTY_FUNCTION__)) | |||
1164 | cast<PointerType>(getType()->getScalarType())->getElementType())((ResultElementType == cast<PointerType>(getType()-> getScalarType())->getElementType()) ? static_cast<void> (0) : __assert_fail ("ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType()" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1164, __PRETTY_FUNCTION__)); | |||
1165 | init(Ptr, IdxList, NameStr); | |||
1166 | } | |||
1167 | ||||
1168 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)GetElementPtrInst::op_iterator GetElementPtrInst::op_begin() { return OperandTraits<GetElementPtrInst>::op_begin(this ); } GetElementPtrInst::const_op_iterator GetElementPtrInst:: op_begin() const { return OperandTraits<GetElementPtrInst> ::op_begin(const_cast<GetElementPtrInst*>(this)); } GetElementPtrInst ::op_iterator GetElementPtrInst::op_end() { return OperandTraits <GetElementPtrInst>::op_end(this); } GetElementPtrInst:: const_op_iterator GetElementPtrInst::op_end() const { return OperandTraits <GetElementPtrInst>::op_end(const_cast<GetElementPtrInst *>(this)); } Value *GetElementPtrInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<GetElementPtrInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1168, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<GetElementPtrInst>::op_begin(const_cast <GetElementPtrInst*>(this))[i_nocapture].get()); } void GetElementPtrInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<GetElementPtrInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1168, __PRETTY_FUNCTION__)); OperandTraits<GetElementPtrInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned GetElementPtrInst::getNumOperands() const { return OperandTraits <GetElementPtrInst>::operands(this); } template <int Idx_nocapture> Use &GetElementPtrInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &GetElementPtrInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
1169 | ||||
1170 | //===----------------------------------------------------------------------===// | |||
1171 | // ICmpInst Class | |||
1172 | //===----------------------------------------------------------------------===// | |||
1173 | ||||
1174 | /// This instruction compares its operands according to the predicate given | |||
1175 | /// to the constructor. It only operates on integers or pointers. The operands | |||
1176 | /// must be identical types. | |||
1177 | /// Represent an integer comparison operator. | |||
1178 | class ICmpInst: public CmpInst { | |||
1179 | void AssertOK() { | |||
1180 | assert(isIntPredicate() &&((isIntPredicate() && "Invalid ICmp predicate value") ? static_cast<void> (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1181, __PRETTY_FUNCTION__)) | |||
1181 | "Invalid ICmp predicate value")((isIntPredicate() && "Invalid ICmp predicate value") ? static_cast<void> (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1181, __PRETTY_FUNCTION__)); | |||
1182 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to ICmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1183, __PRETTY_FUNCTION__)) | |||
1183 | "Both operands to ICmp instruction are not of the same type!")((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to ICmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1183, __PRETTY_FUNCTION__)); | |||
1184 | // Check that the operands are the right type | |||
1185 | assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1187, __PRETTY_FUNCTION__)) | |||
1186 | getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1187, __PRETTY_FUNCTION__)) | |||
1187 | "Invalid operand types for ICmp instruction")(((getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand (0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction" ) ? static_cast<void> (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1187, __PRETTY_FUNCTION__)); | |||
1188 | } | |||
1189 | ||||
1190 | protected: | |||
1191 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1192 | friend class Instruction; | |||
1193 | ||||
1194 | /// Clone an identical ICmpInst | |||
1195 | ICmpInst *cloneImpl() const; | |||
1196 | ||||
1197 | public: | |||
1198 | /// Constructor with insert-before-instruction semantics. | |||
1199 | ICmpInst( | |||
1200 | Instruction *InsertBefore, ///< Where to insert | |||
1201 | Predicate pred, ///< The predicate to use for the comparison | |||
1202 | Value *LHS, ///< The left-hand-side of the expression | |||
1203 | Value *RHS, ///< The right-hand-side of the expression | |||
1204 | const Twine &NameStr = "" ///< Name of the instruction | |||
1205 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1206 | Instruction::ICmp, pred, LHS, RHS, NameStr, | |||
1207 | InsertBefore) { | |||
1208 | #ifndef NDEBUG | |||
1209 | AssertOK(); | |||
1210 | #endif | |||
1211 | } | |||
1212 | ||||
1213 | /// Constructor with insert-at-end semantics. | |||
1214 | ICmpInst( | |||
1215 | BasicBlock &InsertAtEnd, ///< Block to insert into. | |||
1216 | Predicate pred, ///< The predicate to use for the comparison | |||
1217 | Value *LHS, ///< The left-hand-side of the expression | |||
1218 | Value *RHS, ///< The right-hand-side of the expression | |||
1219 | const Twine &NameStr = "" ///< Name of the instruction | |||
1220 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1221 | Instruction::ICmp, pred, LHS, RHS, NameStr, | |||
1222 | &InsertAtEnd) { | |||
1223 | #ifndef NDEBUG | |||
1224 | AssertOK(); | |||
1225 | #endif | |||
1226 | } | |||
1227 | ||||
1228 | /// Constructor with no-insertion semantics | |||
1229 | ICmpInst( | |||
1230 | Predicate pred, ///< The predicate to use for the comparison | |||
1231 | Value *LHS, ///< The left-hand-side of the expression | |||
1232 | Value *RHS, ///< The right-hand-side of the expression | |||
1233 | const Twine &NameStr = "" ///< Name of the instruction | |||
1234 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1235 | Instruction::ICmp, pred, LHS, RHS, NameStr) { | |||
1236 | #ifndef NDEBUG | |||
1237 | AssertOK(); | |||
1238 | #endif | |||
1239 | } | |||
1240 | ||||
1241 | /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. | |||
1242 | /// @returns the predicate that would be the result if the operand were | |||
1243 | /// regarded as signed. | |||
1244 | /// Return the signed version of the predicate | |||
1245 | Predicate getSignedPredicate() const { | |||
1246 | return getSignedPredicate(getPredicate()); | |||
1247 | } | |||
1248 | ||||
1249 | /// This is a static version that you can use without an instruction. | |||
1250 | /// Return the signed version of the predicate. | |||
1251 | static Predicate getSignedPredicate(Predicate pred); | |||
1252 | ||||
1253 | /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. | |||
1254 | /// @returns the predicate that would be the result if the operand were | |||
1255 | /// regarded as unsigned. | |||
1256 | /// Return the unsigned version of the predicate | |||
1257 | Predicate getUnsignedPredicate() const { | |||
1258 | return getUnsignedPredicate(getPredicate()); | |||
1259 | } | |||
1260 | ||||
1261 | /// This is a static version that you can use without an instruction. | |||
1262 | /// Return the unsigned version of the predicate. | |||
1263 | static Predicate getUnsignedPredicate(Predicate pred); | |||
1264 | ||||
1265 | /// Return true if this predicate is either EQ or NE. This also | |||
1266 | /// tests for commutativity. | |||
1267 | static bool isEquality(Predicate P) { | |||
1268 | return P == ICMP_EQ || P == ICMP_NE; | |||
1269 | } | |||
1270 | ||||
1271 | /// Return true if this predicate is either EQ or NE. This also | |||
1272 | /// tests for commutativity. | |||
1273 | bool isEquality() const { | |||
1274 | return isEquality(getPredicate()); | |||
1275 | } | |||
1276 | ||||
1277 | /// @returns true if the predicate of this ICmpInst is commutative | |||
1278 | /// Determine if this relation is commutative. | |||
1279 | bool isCommutative() const { return isEquality(); } | |||
1280 | ||||
1281 | /// Return true if the predicate is relational (not EQ or NE). | |||
1282 | /// | |||
1283 | bool isRelational() const { | |||
1284 | return !isEquality(); | |||
1285 | } | |||
1286 | ||||
1287 | /// Return true if the predicate is relational (not EQ or NE). | |||
1288 | /// | |||
1289 | static bool isRelational(Predicate P) { | |||
1290 | return !isEquality(P); | |||
1291 | } | |||
1292 | ||||
1293 | /// Return true if the predicate is SGT or UGT. | |||
1294 | /// | |||
1295 | static bool isGT(Predicate P) { | |||
1296 | return P == ICMP_SGT || P == ICMP_UGT; | |||
1297 | } | |||
1298 | ||||
1299 | /// Return true if the predicate is SLT or ULT. | |||
1300 | /// | |||
1301 | static bool isLT(Predicate P) { | |||
1302 | return P == ICMP_SLT || P == ICMP_ULT; | |||
1303 | } | |||
1304 | ||||
1305 | /// Return true if the predicate is SGE or UGE. | |||
1306 | /// | |||
1307 | static bool isGE(Predicate P) { | |||
1308 | return P == ICMP_SGE || P == ICMP_UGE; | |||
1309 | } | |||
1310 | ||||
1311 | /// Return true if the predicate is SLE or ULE. | |||
1312 | /// | |||
1313 | static bool isLE(Predicate P) { | |||
1314 | return P == ICMP_SLE || P == ICMP_ULE; | |||
1315 | } | |||
1316 | ||||
1317 | /// Exchange the two operands to this instruction in such a way that it does | |||
1318 | /// not modify the semantics of the instruction. The predicate value may be | |||
1319 | /// changed to retain the same result if the predicate is order dependent | |||
1320 | /// (e.g. ult). | |||
1321 | /// Swap operands and adjust predicate. | |||
1322 | void swapOperands() { | |||
1323 | setPredicate(getSwappedPredicate()); | |||
1324 | Op<0>().swap(Op<1>()); | |||
1325 | } | |||
1326 | ||||
1327 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1328 | static bool classof(const Instruction *I) { | |||
1329 | return I->getOpcode() == Instruction::ICmp; | |||
1330 | } | |||
1331 | static bool classof(const Value *V) { | |||
1332 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1333 | } | |||
1334 | }; | |||
1335 | ||||
1336 | //===----------------------------------------------------------------------===// | |||
1337 | // FCmpInst Class | |||
1338 | //===----------------------------------------------------------------------===// | |||
1339 | ||||
1340 | /// This instruction compares its operands according to the predicate given | |||
1341 | /// to the constructor. It only operates on floating point values or packed | |||
1342 | /// vectors of floating point values. The operands must be identical types. | |||
1343 | /// Represents a floating point comparison operator. | |||
1344 | class FCmpInst: public CmpInst { | |||
1345 | void AssertOK() { | |||
1346 | assert(isFPPredicate() && "Invalid FCmp predicate value")((isFPPredicate() && "Invalid FCmp predicate value") ? static_cast<void> (0) : __assert_fail ("isFPPredicate() && \"Invalid FCmp predicate value\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1346, __PRETTY_FUNCTION__)); | |||
1347 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to FCmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1348, __PRETTY_FUNCTION__)) | |||
1348 | "Both operands to FCmp instruction are not of the same type!")((getOperand(0)->getType() == getOperand(1)->getType() && "Both operands to FCmp instruction are not of the same type!" ) ? static_cast<void> (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1348, __PRETTY_FUNCTION__)); | |||
1349 | // Check that the operands are the right type | |||
1350 | assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&((getOperand(0)->getType()->isFPOrFPVectorTy() && "Invalid operand types for FCmp instruction") ? static_cast< void> (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1351, __PRETTY_FUNCTION__)) | |||
1351 | "Invalid operand types for FCmp instruction")((getOperand(0)->getType()->isFPOrFPVectorTy() && "Invalid operand types for FCmp instruction") ? static_cast< void> (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1351, __PRETTY_FUNCTION__)); | |||
1352 | } | |||
1353 | ||||
1354 | protected: | |||
1355 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1356 | friend class Instruction; | |||
1357 | ||||
1358 | /// Clone an identical FCmpInst | |||
1359 | FCmpInst *cloneImpl() const; | |||
1360 | ||||
1361 | public: | |||
1362 | /// Constructor with insert-before-instruction semantics. | |||
1363 | FCmpInst( | |||
1364 | Instruction *InsertBefore, ///< Where to insert | |||
1365 | Predicate pred, ///< The predicate to use for the comparison | |||
1366 | Value *LHS, ///< The left-hand-side of the expression | |||
1367 | Value *RHS, ///< The right-hand-side of the expression | |||
1368 | const Twine &NameStr = "" ///< Name of the instruction | |||
1369 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1370 | Instruction::FCmp, pred, LHS, RHS, NameStr, | |||
1371 | InsertBefore) { | |||
1372 | AssertOK(); | |||
1373 | } | |||
1374 | ||||
1375 | /// Constructor with insert-at-end semantics. | |||
1376 | FCmpInst( | |||
1377 | BasicBlock &InsertAtEnd, ///< Block to insert into. | |||
1378 | Predicate pred, ///< The predicate to use for the comparison | |||
1379 | Value *LHS, ///< The left-hand-side of the expression | |||
1380 | Value *RHS, ///< The right-hand-side of the expression | |||
1381 | const Twine &NameStr = "" ///< Name of the instruction | |||
1382 | ) : CmpInst(makeCmpResultType(LHS->getType()), | |||
1383 | Instruction::FCmp, pred, LHS, RHS, NameStr, | |||
1384 | &InsertAtEnd) { | |||
1385 | AssertOK(); | |||
1386 | } | |||
1387 | ||||
1388 | /// Constructor with no-insertion semantics | |||
1389 | FCmpInst( | |||
1390 | Predicate Pred, ///< The predicate to use for the comparison | |||
1391 | Value *LHS, ///< The left-hand-side of the expression | |||
1392 | Value *RHS, ///< The right-hand-side of the expression | |||
1393 | const Twine &NameStr = "", ///< Name of the instruction | |||
1394 | Instruction *FlagsSource = nullptr | |||
1395 | ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS, | |||
1396 | RHS, NameStr, nullptr, FlagsSource) { | |||
1397 | AssertOK(); | |||
1398 | } | |||
1399 | ||||
1400 | /// @returns true if the predicate of this instruction is EQ or NE. | |||
1401 | /// Determine if this is an equality predicate. | |||
1402 | static bool isEquality(Predicate Pred) { | |||
1403 | return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ || | |||
1404 | Pred == FCMP_UNE; | |||
1405 | } | |||
1406 | ||||
1407 | /// @returns true if the predicate of this instruction is EQ or NE. | |||
1408 | /// Determine if this is an equality predicate. | |||
1409 | bool isEquality() const { return isEquality(getPredicate()); } | |||
1410 | ||||
1411 | /// @returns true if the predicate of this instruction is commutative. | |||
1412 | /// Determine if this is a commutative predicate. | |||
1413 | bool isCommutative() const { | |||
1414 | return isEquality() || | |||
1415 | getPredicate() == FCMP_FALSE || | |||
1416 | getPredicate() == FCMP_TRUE || | |||
1417 | getPredicate() == FCMP_ORD || | |||
1418 | getPredicate() == FCMP_UNO; | |||
1419 | } | |||
1420 | ||||
1421 | /// @returns true if the predicate is relational (not EQ or NE). | |||
1422 | /// Determine if this a relational predicate. | |||
1423 | bool isRelational() const { return !isEquality(); } | |||
1424 | ||||
1425 | /// Exchange the two operands to this instruction in such a way that it does | |||
1426 | /// not modify the semantics of the instruction. The predicate value may be | |||
1427 | /// changed to retain the same result if the predicate is order dependent | |||
1428 | /// (e.g. ult). | |||
1429 | /// Swap operands and adjust predicate. | |||
1430 | void swapOperands() { | |||
1431 | setPredicate(getSwappedPredicate()); | |||
1432 | Op<0>().swap(Op<1>()); | |||
1433 | } | |||
1434 | ||||
1435 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1436 | static bool classof(const Instruction *I) { | |||
1437 | return I->getOpcode() == Instruction::FCmp; | |||
1438 | } | |||
1439 | static bool classof(const Value *V) { | |||
1440 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1441 | } | |||
1442 | }; | |||
1443 | ||||
1444 | //===----------------------------------------------------------------------===// | |||
1445 | /// This class represents a function call, abstracting a target | |||
1446 | /// machine's calling convention. This class uses low bit of the SubClassData | |||
1447 | /// field to indicate whether or not this is a tail call. The rest of the bits | |||
1448 | /// hold the calling convention of the call. | |||
1449 | /// | |||
1450 | class CallInst : public CallBase { | |||
1451 | CallInst(const CallInst &CI); | |||
1452 | ||||
1453 | /// Construct a CallInst given a range of arguments. | |||
1454 | /// Construct a CallInst from a range of arguments | |||
1455 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1456 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, | |||
1457 | Instruction *InsertBefore); | |||
1458 | ||||
1459 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1460 | const Twine &NameStr, Instruction *InsertBefore) | |||
1461 | : CallInst(Ty, Func, Args, None, NameStr, InsertBefore) {} | |||
1462 | ||||
1463 | /// Construct a CallInst given a range of arguments. | |||
1464 | /// Construct a CallInst from a range of arguments | |||
1465 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1466 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, | |||
1467 | BasicBlock *InsertAtEnd); | |||
1468 | ||||
1469 | explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr, | |||
1470 | Instruction *InsertBefore); | |||
1471 | ||||
1472 | CallInst(FunctionType *ty, Value *F, const Twine &NameStr, | |||
1473 | BasicBlock *InsertAtEnd); | |||
1474 | ||||
1475 | void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, | |||
1476 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); | |||
1477 | void init(FunctionType *FTy, Value *Func, const Twine &NameStr); | |||
1478 | ||||
1479 | /// Compute the number of operands to allocate. | |||
1480 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { | |||
1481 | // We need one operand for the called function, plus the input operand | |||
1482 | // counts provided. | |||
1483 | return 1 + NumArgs + NumBundleInputs; | |||
1484 | } | |||
1485 | ||||
1486 | protected: | |||
1487 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1488 | friend class Instruction; | |||
1489 | ||||
1490 | CallInst *cloneImpl() const; | |||
1491 | ||||
1492 | public: | |||
1493 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "", | |||
1494 | Instruction *InsertBefore = nullptr) { | |||
1495 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore); | |||
1496 | } | |||
1497 | ||||
1498 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1499 | const Twine &NameStr, | |||
1500 | Instruction *InsertBefore = nullptr) { | |||
1501 | return new (ComputeNumOperands(Args.size())) | |||
1502 | CallInst(Ty, Func, Args, None, NameStr, InsertBefore); | |||
1503 | } | |||
1504 | ||||
1505 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1506 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1507 | const Twine &NameStr = "", | |||
1508 | Instruction *InsertBefore = nullptr) { | |||
1509 | const int NumOperands = | |||
1510 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); | |||
1511 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
1512 | ||||
1513 | return new (NumOperands, DescriptorBytes) | |||
1514 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore); | |||
1515 | } | |||
1516 | ||||
1517 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr, | |||
1518 | BasicBlock *InsertAtEnd) { | |||
1519 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd); | |||
1520 | } | |||
1521 | ||||
1522 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1523 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1524 | return new (ComputeNumOperands(Args.size())) | |||
1525 | CallInst(Ty, Func, Args, None, NameStr, InsertAtEnd); | |||
1526 | } | |||
1527 | ||||
1528 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1529 | ArrayRef<OperandBundleDef> Bundles, | |||
1530 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1531 | const int NumOperands = | |||
1532 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); | |||
1533 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
1534 | ||||
1535 | return new (NumOperands, DescriptorBytes) | |||
1536 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd); | |||
1537 | } | |||
1538 | ||||
1539 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "", | |||
1540 | Instruction *InsertBefore = nullptr) { | |||
1541 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, | |||
1542 | InsertBefore); | |||
1543 | } | |||
1544 | ||||
1545 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, | |||
1546 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1547 | const Twine &NameStr = "", | |||
1548 | Instruction *InsertBefore = nullptr) { | |||
1549 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, | |||
1550 | NameStr, InsertBefore); | |||
1551 | } | |||
1552 | ||||
1553 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, | |||
1554 | const Twine &NameStr, | |||
1555 | Instruction *InsertBefore = nullptr) { | |||
1556 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, | |||
1557 | InsertBefore); | |||
1558 | } | |||
1559 | ||||
1560 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr, | |||
1561 | BasicBlock *InsertAtEnd) { | |||
1562 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, | |||
1563 | InsertAtEnd); | |||
1564 | } | |||
1565 | ||||
1566 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, | |||
1567 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1568 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, | |||
1569 | InsertAtEnd); | |||
1570 | } | |||
1571 | ||||
1572 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, | |||
1573 | ArrayRef<OperandBundleDef> Bundles, | |||
1574 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
1575 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, | |||
1576 | NameStr, InsertAtEnd); | |||
1577 | } | |||
1578 | ||||
1579 | /// Create a clone of \p CI with a different set of operand bundles and | |||
1580 | /// insert it before \p InsertPt. | |||
1581 | /// | |||
1582 | /// The returned call instruction is identical \p CI in every way except that | |||
1583 | /// the operand bundles for the new instruction are set to the operand bundles | |||
1584 | /// in \p Bundles. | |||
1585 | static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles, | |||
1586 | Instruction *InsertPt = nullptr); | |||
1587 | ||||
1588 | /// Create a clone of \p CI with a different set of operand bundles and | |||
1589 | /// insert it before \p InsertPt. | |||
1590 | /// | |||
1591 | /// The returned call instruction is identical \p CI in every way except that | |||
1592 | /// the operand bundle for the new instruction is set to the operand bundle | |||
1593 | /// in \p Bundle. | |||
1594 | static CallInst *CreateWithReplacedBundle(CallInst *CI, | |||
1595 | OperandBundleDef Bundle, | |||
1596 | Instruction *InsertPt = nullptr); | |||
1597 | ||||
1598 | /// Generate the IR for a call to malloc: | |||
1599 | /// 1. Compute the malloc call's argument as the specified type's size, | |||
1600 | /// possibly multiplied by the array size if the array size is not | |||
1601 | /// constant 1. | |||
1602 | /// 2. Call malloc with that argument. | |||
1603 | /// 3. Bitcast the result of the malloc call to the specified type. | |||
1604 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, | |||
1605 | Type *AllocTy, Value *AllocSize, | |||
1606 | Value *ArraySize = nullptr, | |||
1607 | Function *MallocF = nullptr, | |||
1608 | const Twine &Name = ""); | |||
1609 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, | |||
1610 | Type *AllocTy, Value *AllocSize, | |||
1611 | Value *ArraySize = nullptr, | |||
1612 | Function *MallocF = nullptr, | |||
1613 | const Twine &Name = ""); | |||
1614 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, | |||
1615 | Type *AllocTy, Value *AllocSize, | |||
1616 | Value *ArraySize = nullptr, | |||
1617 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1618 | Function *MallocF = nullptr, | |||
1619 | const Twine &Name = ""); | |||
1620 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, | |||
1621 | Type *AllocTy, Value *AllocSize, | |||
1622 | Value *ArraySize = nullptr, | |||
1623 | ArrayRef<OperandBundleDef> Bundles = None, | |||
1624 | Function *MallocF = nullptr, | |||
1625 | const Twine &Name = ""); | |||
1626 | /// Generate the IR for a call to the builtin free function. | |||
1627 | static Instruction *CreateFree(Value *Source, Instruction *InsertBefore); | |||
1628 | static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd); | |||
1629 | static Instruction *CreateFree(Value *Source, | |||
1630 | ArrayRef<OperandBundleDef> Bundles, | |||
1631 | Instruction *InsertBefore); | |||
1632 | static Instruction *CreateFree(Value *Source, | |||
1633 | ArrayRef<OperandBundleDef> Bundles, | |||
1634 | BasicBlock *InsertAtEnd); | |||
1635 | ||||
1636 | // Note that 'musttail' implies 'tail'. | |||
1637 | enum TailCallKind : unsigned { | |||
1638 | TCK_None = 0, | |||
1639 | TCK_Tail = 1, | |||
1640 | TCK_MustTail = 2, | |||
1641 | TCK_NoTail = 3, | |||
1642 | TCK_LAST = TCK_NoTail | |||
1643 | }; | |||
1644 | ||||
1645 | using TailCallKindField = Bitfield::Element<TailCallKind, 0, 2, TCK_LAST>; | |||
1646 | static_assert( | |||
1647 | Bitfield::areContiguous<TailCallKindField, CallBase::CallingConvField>(), | |||
1648 | "Bitfields must be contiguous"); | |||
1649 | ||||
1650 | TailCallKind getTailCallKind() const { | |||
1651 | return getSubclassData<TailCallKindField>(); | |||
1652 | } | |||
1653 | ||||
1654 | bool isTailCall() const { | |||
1655 | TailCallKind Kind = getTailCallKind(); | |||
1656 | return Kind == TCK_Tail || Kind == TCK_MustTail; | |||
1657 | } | |||
1658 | ||||
1659 | bool isMustTailCall() const { return getTailCallKind() == TCK_MustTail; } | |||
1660 | ||||
1661 | bool isNoTailCall() const { return getTailCallKind() == TCK_NoTail; } | |||
1662 | ||||
1663 | void setTailCallKind(TailCallKind TCK) { | |||
1664 | setSubclassData<TailCallKindField>(TCK); | |||
1665 | } | |||
1666 | ||||
1667 | void setTailCall(bool IsTc = true) { | |||
1668 | setTailCallKind(IsTc ? TCK_Tail : TCK_None); | |||
1669 | } | |||
1670 | ||||
1671 | /// Return true if the call can return twice | |||
1672 | bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); } | |||
1673 | void setCanReturnTwice() { | |||
1674 | addAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice); | |||
1675 | } | |||
1676 | ||||
1677 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1678 | static bool classof(const Instruction *I) { | |||
1679 | return I->getOpcode() == Instruction::Call; | |||
1680 | } | |||
1681 | static bool classof(const Value *V) { | |||
1682 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1683 | } | |||
1684 | ||||
1685 | /// Updates profile metadata by scaling it by \p S / \p T. | |||
1686 | void updateProfWeight(uint64_t S, uint64_t T); | |||
1687 | ||||
1688 | private: | |||
1689 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
1690 | // method so that subclasses cannot accidentally use it. | |||
1691 | template <typename Bitfield> | |||
1692 | void setSubclassData(typename Bitfield::Type Value) { | |||
1693 | Instruction::setSubclassData<Bitfield>(Value); | |||
1694 | } | |||
1695 | }; | |||
1696 | ||||
1697 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1698 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, | |||
1699 | BasicBlock *InsertAtEnd) | |||
1700 | : CallBase(Ty->getReturnType(), Instruction::Call, | |||
1701 | OperandTraits<CallBase>::op_end(this) - | |||
1702 | (Args.size() + CountBundleInputs(Bundles) + 1), | |||
1703 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), | |||
1704 | InsertAtEnd) { | |||
1705 | init(Ty, Func, Args, Bundles, NameStr); | |||
1706 | } | |||
1707 | ||||
1708 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, | |||
1709 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, | |||
1710 | Instruction *InsertBefore) | |||
1711 | : CallBase(Ty->getReturnType(), Instruction::Call, | |||
1712 | OperandTraits<CallBase>::op_end(this) - | |||
1713 | (Args.size() + CountBundleInputs(Bundles) + 1), | |||
1714 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), | |||
1715 | InsertBefore) { | |||
1716 | init(Ty, Func, Args, Bundles, NameStr); | |||
1717 | } | |||
1718 | ||||
1719 | //===----------------------------------------------------------------------===// | |||
1720 | // SelectInst Class | |||
1721 | //===----------------------------------------------------------------------===// | |||
1722 | ||||
1723 | /// This class represents the LLVM 'select' instruction. | |||
1724 | /// | |||
1725 | class SelectInst : public Instruction { | |||
1726 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, | |||
1727 | Instruction *InsertBefore) | |||
1728 | : Instruction(S1->getType(), Instruction::Select, | |||
1729 | &Op<0>(), 3, InsertBefore) { | |||
1730 | init(C, S1, S2); | |||
1731 | setName(NameStr); | |||
1732 | } | |||
1733 | ||||
1734 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, | |||
1735 | BasicBlock *InsertAtEnd) | |||
1736 | : Instruction(S1->getType(), Instruction::Select, | |||
1737 | &Op<0>(), 3, InsertAtEnd) { | |||
1738 | init(C, S1, S2); | |||
1739 | setName(NameStr); | |||
1740 | } | |||
1741 | ||||
1742 | void init(Value *C, Value *S1, Value *S2) { | |||
1743 | assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select")((!areInvalidOperands(C, S1, S2) && "Invalid operands for select" ) ? static_cast<void> (0) : __assert_fail ("!areInvalidOperands(C, S1, S2) && \"Invalid operands for select\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1743, __PRETTY_FUNCTION__)); | |||
1744 | Op<0>() = C; | |||
1745 | Op<1>() = S1; | |||
1746 | Op<2>() = S2; | |||
1747 | } | |||
1748 | ||||
1749 | protected: | |||
1750 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1751 | friend class Instruction; | |||
1752 | ||||
1753 | SelectInst *cloneImpl() const; | |||
1754 | ||||
1755 | public: | |||
1756 | static SelectInst *Create(Value *C, Value *S1, Value *S2, | |||
1757 | const Twine &NameStr = "", | |||
1758 | Instruction *InsertBefore = nullptr, | |||
1759 | Instruction *MDFrom = nullptr) { | |||
1760 | SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore); | |||
1761 | if (MDFrom) | |||
1762 | Sel->copyMetadata(*MDFrom); | |||
1763 | return Sel; | |||
1764 | } | |||
1765 | ||||
1766 | static SelectInst *Create(Value *C, Value *S1, Value *S2, | |||
1767 | const Twine &NameStr, | |||
1768 | BasicBlock *InsertAtEnd) { | |||
1769 | return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd); | |||
1770 | } | |||
1771 | ||||
1772 | const Value *getCondition() const { return Op<0>(); } | |||
1773 | const Value *getTrueValue() const { return Op<1>(); } | |||
1774 | const Value *getFalseValue() const { return Op<2>(); } | |||
1775 | Value *getCondition() { return Op<0>(); } | |||
1776 | Value *getTrueValue() { return Op<1>(); } | |||
1777 | Value *getFalseValue() { return Op<2>(); } | |||
1778 | ||||
1779 | void setCondition(Value *V) { Op<0>() = V; } | |||
1780 | void setTrueValue(Value *V) { Op<1>() = V; } | |||
1781 | void setFalseValue(Value *V) { Op<2>() = V; } | |||
1782 | ||||
1783 | /// Swap the true and false values of the select instruction. | |||
1784 | /// This doesn't swap prof metadata. | |||
1785 | void swapValues() { Op<1>().swap(Op<2>()); } | |||
1786 | ||||
1787 | /// Return a string if the specified operands are invalid | |||
1788 | /// for a select operation, otherwise return null. | |||
1789 | static const char *areInvalidOperands(Value *Cond, Value *True, Value *False); | |||
1790 | ||||
1791 | /// Transparently provide more efficient getOperand methods. | |||
1792 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
1793 | ||||
1794 | OtherOps getOpcode() const { | |||
1795 | return static_cast<OtherOps>(Instruction::getOpcode()); | |||
1796 | } | |||
1797 | ||||
1798 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1799 | static bool classof(const Instruction *I) { | |||
1800 | return I->getOpcode() == Instruction::Select; | |||
1801 | } | |||
1802 | static bool classof(const Value *V) { | |||
1803 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1804 | } | |||
1805 | }; | |||
1806 | ||||
1807 | template <> | |||
1808 | struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { | |||
1809 | }; | |||
1810 | ||||
1811 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)SelectInst::op_iterator SelectInst::op_begin() { return OperandTraits <SelectInst>::op_begin(this); } SelectInst::const_op_iterator SelectInst::op_begin() const { return OperandTraits<SelectInst >::op_begin(const_cast<SelectInst*>(this)); } SelectInst ::op_iterator SelectInst::op_end() { return OperandTraits< SelectInst>::op_end(this); } SelectInst::const_op_iterator SelectInst::op_end() const { return OperandTraits<SelectInst >::op_end(const_cast<SelectInst*>(this)); } Value *SelectInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<SelectInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1811, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<SelectInst>::op_begin(const_cast<SelectInst *>(this))[i_nocapture].get()); } void SelectInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<SelectInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1811, __PRETTY_FUNCTION__)); OperandTraits<SelectInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned SelectInst ::getNumOperands() const { return OperandTraits<SelectInst >::operands(this); } template <int Idx_nocapture> Use &SelectInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & SelectInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
1812 | ||||
1813 | //===----------------------------------------------------------------------===// | |||
1814 | // VAArgInst Class | |||
1815 | //===----------------------------------------------------------------------===// | |||
1816 | ||||
1817 | /// This class represents the va_arg llvm instruction, which returns | |||
1818 | /// an argument of the specified type given a va_list and increments that list | |||
1819 | /// | |||
1820 | class VAArgInst : public UnaryInstruction { | |||
1821 | protected: | |||
1822 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1823 | friend class Instruction; | |||
1824 | ||||
1825 | VAArgInst *cloneImpl() const; | |||
1826 | ||||
1827 | public: | |||
1828 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "", | |||
1829 | Instruction *InsertBefore = nullptr) | |||
1830 | : UnaryInstruction(Ty, VAArg, List, InsertBefore) { | |||
1831 | setName(NameStr); | |||
1832 | } | |||
1833 | ||||
1834 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr, | |||
1835 | BasicBlock *InsertAtEnd) | |||
1836 | : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) { | |||
1837 | setName(NameStr); | |||
1838 | } | |||
1839 | ||||
1840 | Value *getPointerOperand() { return getOperand(0); } | |||
1841 | const Value *getPointerOperand() const { return getOperand(0); } | |||
1842 | static unsigned getPointerOperandIndex() { return 0U; } | |||
1843 | ||||
1844 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1845 | static bool classof(const Instruction *I) { | |||
1846 | return I->getOpcode() == VAArg; | |||
1847 | } | |||
1848 | static bool classof(const Value *V) { | |||
1849 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1850 | } | |||
1851 | }; | |||
1852 | ||||
1853 | //===----------------------------------------------------------------------===// | |||
1854 | // ExtractElementInst Class | |||
1855 | //===----------------------------------------------------------------------===// | |||
1856 | ||||
1857 | /// This instruction extracts a single (scalar) | |||
1858 | /// element from a VectorType value | |||
1859 | /// | |||
1860 | class ExtractElementInst : public Instruction { | |||
1861 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "", | |||
1862 | Instruction *InsertBefore = nullptr); | |||
1863 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr, | |||
1864 | BasicBlock *InsertAtEnd); | |||
1865 | ||||
1866 | protected: | |||
1867 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1868 | friend class Instruction; | |||
1869 | ||||
1870 | ExtractElementInst *cloneImpl() const; | |||
1871 | ||||
1872 | public: | |||
1873 | static ExtractElementInst *Create(Value *Vec, Value *Idx, | |||
1874 | const Twine &NameStr = "", | |||
1875 | Instruction *InsertBefore = nullptr) { | |||
1876 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore); | |||
1877 | } | |||
1878 | ||||
1879 | static ExtractElementInst *Create(Value *Vec, Value *Idx, | |||
1880 | const Twine &NameStr, | |||
1881 | BasicBlock *InsertAtEnd) { | |||
1882 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd); | |||
1883 | } | |||
1884 | ||||
1885 | /// Return true if an extractelement instruction can be | |||
1886 | /// formed with the specified operands. | |||
1887 | static bool isValidOperands(const Value *Vec, const Value *Idx); | |||
1888 | ||||
1889 | Value *getVectorOperand() { return Op<0>(); } | |||
1890 | Value *getIndexOperand() { return Op<1>(); } | |||
1891 | const Value *getVectorOperand() const { return Op<0>(); } | |||
1892 | const Value *getIndexOperand() const { return Op<1>(); } | |||
1893 | ||||
1894 | VectorType *getVectorOperandType() const { | |||
1895 | return cast<VectorType>(getVectorOperand()->getType()); | |||
1896 | } | |||
1897 | ||||
1898 | /// Transparently provide more efficient getOperand methods. | |||
1899 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
1900 | ||||
1901 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1902 | static bool classof(const Instruction *I) { | |||
1903 | return I->getOpcode() == Instruction::ExtractElement; | |||
1904 | } | |||
1905 | static bool classof(const Value *V) { | |||
1906 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1907 | } | |||
1908 | }; | |||
1909 | ||||
1910 | template <> | |||
1911 | struct OperandTraits<ExtractElementInst> : | |||
1912 | public FixedNumOperandTraits<ExtractElementInst, 2> { | |||
1913 | }; | |||
1914 | ||||
1915 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)ExtractElementInst::op_iterator ExtractElementInst::op_begin( ) { return OperandTraits<ExtractElementInst>::op_begin( this); } ExtractElementInst::const_op_iterator ExtractElementInst ::op_begin() const { return OperandTraits<ExtractElementInst >::op_begin(const_cast<ExtractElementInst*>(this)); } ExtractElementInst::op_iterator ExtractElementInst::op_end() { return OperandTraits<ExtractElementInst>::op_end(this ); } ExtractElementInst::const_op_iterator ExtractElementInst ::op_end() const { return OperandTraits<ExtractElementInst >::op_end(const_cast<ExtractElementInst*>(this)); } Value *ExtractElementInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ExtractElementInst>:: operands(this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1915, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ExtractElementInst>::op_begin(const_cast <ExtractElementInst*>(this))[i_nocapture].get()); } void ExtractElementInst::setOperand(unsigned i_nocapture, Value * Val_nocapture) { ((i_nocapture < OperandTraits<ExtractElementInst >::operands(this) && "setOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1915, __PRETTY_FUNCTION__)); OperandTraits<ExtractElementInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ExtractElementInst::getNumOperands() const { return OperandTraits <ExtractElementInst>::operands(this); } template <int Idx_nocapture> Use &ExtractElementInst::Op() { return this->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture> const Use &ExtractElementInst::Op() const { return this->OpFrom<Idx_nocapture>(this); } | |||
1916 | ||||
1917 | //===----------------------------------------------------------------------===// | |||
1918 | // InsertElementInst Class | |||
1919 | //===----------------------------------------------------------------------===// | |||
1920 | ||||
1921 | /// This instruction inserts a single (scalar) | |||
1922 | /// element into a VectorType value | |||
1923 | /// | |||
1924 | class InsertElementInst : public Instruction { | |||
1925 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, | |||
1926 | const Twine &NameStr = "", | |||
1927 | Instruction *InsertBefore = nullptr); | |||
1928 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr, | |||
1929 | BasicBlock *InsertAtEnd); | |||
1930 | ||||
1931 | protected: | |||
1932 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
1933 | friend class Instruction; | |||
1934 | ||||
1935 | InsertElementInst *cloneImpl() const; | |||
1936 | ||||
1937 | public: | |||
1938 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, | |||
1939 | const Twine &NameStr = "", | |||
1940 | Instruction *InsertBefore = nullptr) { | |||
1941 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore); | |||
1942 | } | |||
1943 | ||||
1944 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, | |||
1945 | const Twine &NameStr, | |||
1946 | BasicBlock *InsertAtEnd) { | |||
1947 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd); | |||
1948 | } | |||
1949 | ||||
1950 | /// Return true if an insertelement instruction can be | |||
1951 | /// formed with the specified operands. | |||
1952 | static bool isValidOperands(const Value *Vec, const Value *NewElt, | |||
1953 | const Value *Idx); | |||
1954 | ||||
1955 | /// Overload to return most specific vector type. | |||
1956 | /// | |||
1957 | VectorType *getType() const { | |||
1958 | return cast<VectorType>(Instruction::getType()); | |||
1959 | } | |||
1960 | ||||
1961 | /// Transparently provide more efficient getOperand methods. | |||
1962 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
1963 | ||||
1964 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
1965 | static bool classof(const Instruction *I) { | |||
1966 | return I->getOpcode() == Instruction::InsertElement; | |||
1967 | } | |||
1968 | static bool classof(const Value *V) { | |||
1969 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
1970 | } | |||
1971 | }; | |||
1972 | ||||
1973 | template <> | |||
1974 | struct OperandTraits<InsertElementInst> : | |||
1975 | public FixedNumOperandTraits<InsertElementInst, 3> { | |||
1976 | }; | |||
1977 | ||||
1978 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)InsertElementInst::op_iterator InsertElementInst::op_begin() { return OperandTraits<InsertElementInst>::op_begin(this ); } InsertElementInst::const_op_iterator InsertElementInst:: op_begin() const { return OperandTraits<InsertElementInst> ::op_begin(const_cast<InsertElementInst*>(this)); } InsertElementInst ::op_iterator InsertElementInst::op_end() { return OperandTraits <InsertElementInst>::op_end(this); } InsertElementInst:: const_op_iterator InsertElementInst::op_end() const { return OperandTraits <InsertElementInst>::op_end(const_cast<InsertElementInst *>(this)); } Value *InsertElementInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<InsertElementInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1978, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<InsertElementInst>::op_begin(const_cast <InsertElementInst*>(this))[i_nocapture].get()); } void InsertElementInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<InsertElementInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 1978, __PRETTY_FUNCTION__)); OperandTraits<InsertElementInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned InsertElementInst::getNumOperands() const { return OperandTraits <InsertElementInst>::operands(this); } template <int Idx_nocapture> Use &InsertElementInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &InsertElementInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
1979 | ||||
1980 | //===----------------------------------------------------------------------===// | |||
1981 | // ShuffleVectorInst Class | |||
1982 | //===----------------------------------------------------------------------===// | |||
1983 | ||||
1984 | constexpr int UndefMaskElem = -1; | |||
1985 | ||||
1986 | /// This instruction constructs a fixed permutation of two | |||
1987 | /// input vectors. | |||
1988 | /// | |||
1989 | /// For each element of the result vector, the shuffle mask selects an element | |||
1990 | /// from one of the input vectors to copy to the result. Non-negative elements | |||
1991 | /// in the mask represent an index into the concatenated pair of input vectors. | |||
1992 | /// UndefMaskElem (-1) specifies that the result element is undefined. | |||
1993 | /// | |||
1994 | /// For scalable vectors, all the elements of the mask must be 0 or -1. This | |||
1995 | /// requirement may be relaxed in the future. | |||
1996 | class ShuffleVectorInst : public Instruction { | |||
1997 | SmallVector<int, 4> ShuffleMask; | |||
1998 | Constant *ShuffleMaskForBitcode; | |||
1999 | ||||
2000 | protected: | |||
2001 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2002 | friend class Instruction; | |||
2003 | ||||
2004 | ShuffleVectorInst *cloneImpl() const; | |||
2005 | ||||
2006 | public: | |||
2007 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, | |||
2008 | const Twine &NameStr = "", | |||
2009 | Instruction *InsertBefor = nullptr); | |||
2010 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, | |||
2011 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2012 | ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, | |||
2013 | const Twine &NameStr = "", | |||
2014 | Instruction *InsertBefor = nullptr); | |||
2015 | ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, | |||
2016 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2017 | ||||
2018 | void *operator new(size_t s) { return User::operator new(s, 2); } | |||
2019 | ||||
2020 | /// Swap the operands and adjust the mask to preserve the semantics | |||
2021 | /// of the instruction. | |||
2022 | void commute(); | |||
2023 | ||||
2024 | /// Return true if a shufflevector instruction can be | |||
2025 | /// formed with the specified operands. | |||
2026 | static bool isValidOperands(const Value *V1, const Value *V2, | |||
2027 | const Value *Mask); | |||
2028 | static bool isValidOperands(const Value *V1, const Value *V2, | |||
2029 | ArrayRef<int> Mask); | |||
2030 | ||||
2031 | /// Overload to return most specific vector type. | |||
2032 | /// | |||
2033 | VectorType *getType() const { | |||
2034 | return cast<VectorType>(Instruction::getType()); | |||
2035 | } | |||
2036 | ||||
2037 | /// Transparently provide more efficient getOperand methods. | |||
2038 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
2039 | ||||
2040 | /// Return the shuffle mask value of this instruction for the given element | |||
2041 | /// index. Return UndefMaskElem if the element is undef. | |||
2042 | int getMaskValue(unsigned Elt) const { return ShuffleMask[Elt]; } | |||
2043 | ||||
2044 | /// Convert the input shuffle mask operand to a vector of integers. Undefined | |||
2045 | /// elements of the mask are returned as UndefMaskElem. | |||
2046 | static void getShuffleMask(const Constant *Mask, | |||
2047 | SmallVectorImpl<int> &Result); | |||
2048 | ||||
2049 | /// Return the mask for this instruction as a vector of integers. Undefined | |||
2050 | /// elements of the mask are returned as UndefMaskElem. | |||
2051 | void getShuffleMask(SmallVectorImpl<int> &Result) const { | |||
2052 | Result.assign(ShuffleMask.begin(), ShuffleMask.end()); | |||
2053 | } | |||
2054 | ||||
2055 | /// Return the mask for this instruction, for use in bitcode. | |||
2056 | /// | |||
2057 | /// TODO: This is temporary until we decide a new bitcode encoding for | |||
2058 | /// shufflevector. | |||
2059 | Constant *getShuffleMaskForBitcode() const { return ShuffleMaskForBitcode; } | |||
2060 | ||||
2061 | static Constant *convertShuffleMaskForBitcode(ArrayRef<int> Mask, | |||
2062 | Type *ResultTy); | |||
2063 | ||||
2064 | void setShuffleMask(ArrayRef<int> Mask); | |||
2065 | ||||
2066 | ArrayRef<int> getShuffleMask() const { return ShuffleMask; } | |||
2067 | ||||
2068 | /// Return true if this shuffle returns a vector with a different number of | |||
2069 | /// elements than its source vectors. | |||
2070 | /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3> | |||
2071 | /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5> | |||
2072 | bool changesLength() const { | |||
2073 | unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType()) | |||
2074 | ->getElementCount() | |||
2075 | .getKnownMinValue(); | |||
2076 | unsigned NumMaskElts = ShuffleMask.size(); | |||
2077 | return NumSourceElts != NumMaskElts; | |||
2078 | } | |||
2079 | ||||
2080 | /// Return true if this shuffle returns a vector with a greater number of | |||
2081 | /// elements than its source vectors. | |||
2082 | /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3> | |||
2083 | bool increasesLength() const { | |||
2084 | unsigned NumSourceElts = | |||
2085 | cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
2086 | unsigned NumMaskElts = ShuffleMask.size(); | |||
2087 | return NumSourceElts < NumMaskElts; | |||
2088 | } | |||
2089 | ||||
2090 | /// Return true if this shuffle mask chooses elements from exactly one source | |||
2091 | /// vector. | |||
2092 | /// Example: <7,5,undef,7> | |||
2093 | /// This assumes that vector operands are the same length as the mask. | |||
2094 | static bool isSingleSourceMask(ArrayRef<int> Mask); | |||
2095 | static bool isSingleSourceMask(const Constant *Mask) { | |||
2096 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2096, __PRETTY_FUNCTION__)); | |||
2097 | SmallVector<int, 16> MaskAsInts; | |||
2098 | getShuffleMask(Mask, MaskAsInts); | |||
2099 | return isSingleSourceMask(MaskAsInts); | |||
2100 | } | |||
2101 | ||||
2102 | /// Return true if this shuffle chooses elements from exactly one source | |||
2103 | /// vector without changing the length of that vector. | |||
2104 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3> | |||
2105 | /// TODO: Optionally allow length-changing shuffles. | |||
2106 | bool isSingleSource() const { | |||
2107 | return !changesLength() && isSingleSourceMask(ShuffleMask); | |||
2108 | } | |||
2109 | ||||
2110 | /// Return true if this shuffle mask chooses elements from exactly one source | |||
2111 | /// vector without lane crossings. A shuffle using this mask is not | |||
2112 | /// necessarily a no-op because it may change the number of elements from its | |||
2113 | /// input vectors or it may provide demanded bits knowledge via undef lanes. | |||
2114 | /// Example: <undef,undef,2,3> | |||
2115 | static bool isIdentityMask(ArrayRef<int> Mask); | |||
2116 | static bool isIdentityMask(const Constant *Mask) { | |||
2117 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2117, __PRETTY_FUNCTION__)); | |||
2118 | SmallVector<int, 16> MaskAsInts; | |||
2119 | getShuffleMask(Mask, MaskAsInts); | |||
2120 | return isIdentityMask(MaskAsInts); | |||
2121 | } | |||
2122 | ||||
2123 | /// Return true if this shuffle chooses elements from exactly one source | |||
2124 | /// vector without lane crossings and does not change the number of elements | |||
2125 | /// from its input vectors. | |||
2126 | /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef> | |||
2127 | bool isIdentity() const { | |||
2128 | return !changesLength() && isIdentityMask(ShuffleMask); | |||
2129 | } | |||
2130 | ||||
2131 | /// Return true if this shuffle lengthens exactly one source vector with | |||
2132 | /// undefs in the high elements. | |||
2133 | bool isIdentityWithPadding() const; | |||
2134 | ||||
2135 | /// Return true if this shuffle extracts the first N elements of exactly one | |||
2136 | /// source vector. | |||
2137 | bool isIdentityWithExtract() const; | |||
2138 | ||||
2139 | /// Return true if this shuffle concatenates its 2 source vectors. This | |||
2140 | /// returns false if either input is undefined. In that case, the shuffle is | |||
2141 | /// is better classified as an identity with padding operation. | |||
2142 | bool isConcat() const; | |||
2143 | ||||
2144 | /// Return true if this shuffle mask chooses elements from its source vectors | |||
2145 | /// without lane crossings. A shuffle using this mask would be | |||
2146 | /// equivalent to a vector select with a constant condition operand. | |||
2147 | /// Example: <4,1,6,undef> | |||
2148 | /// This returns false if the mask does not choose from both input vectors. | |||
2149 | /// In that case, the shuffle is better classified as an identity shuffle. | |||
2150 | /// This assumes that vector operands are the same length as the mask | |||
2151 | /// (a length-changing shuffle can never be equivalent to a vector select). | |||
2152 | static bool isSelectMask(ArrayRef<int> Mask); | |||
2153 | static bool isSelectMask(const Constant *Mask) { | |||
2154 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2154, __PRETTY_FUNCTION__)); | |||
2155 | SmallVector<int, 16> MaskAsInts; | |||
2156 | getShuffleMask(Mask, MaskAsInts); | |||
2157 | return isSelectMask(MaskAsInts); | |||
2158 | } | |||
2159 | ||||
2160 | /// Return true if this shuffle chooses elements from its source vectors | |||
2161 | /// without lane crossings and all operands have the same number of elements. | |||
2162 | /// In other words, this shuffle is equivalent to a vector select with a | |||
2163 | /// constant condition operand. | |||
2164 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3> | |||
2165 | /// This returns false if the mask does not choose from both input vectors. | |||
2166 | /// In that case, the shuffle is better classified as an identity shuffle. | |||
2167 | /// TODO: Optionally allow length-changing shuffles. | |||
2168 | bool isSelect() const { | |||
2169 | return !changesLength() && isSelectMask(ShuffleMask); | |||
2170 | } | |||
2171 | ||||
2172 | /// Return true if this shuffle mask swaps the order of elements from exactly | |||
2173 | /// one source vector. | |||
2174 | /// Example: <7,6,undef,4> | |||
2175 | /// This assumes that vector operands are the same length as the mask. | |||
2176 | static bool isReverseMask(ArrayRef<int> Mask); | |||
2177 | static bool isReverseMask(const Constant *Mask) { | |||
2178 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2178, __PRETTY_FUNCTION__)); | |||
2179 | SmallVector<int, 16> MaskAsInts; | |||
2180 | getShuffleMask(Mask, MaskAsInts); | |||
2181 | return isReverseMask(MaskAsInts); | |||
2182 | } | |||
2183 | ||||
2184 | /// Return true if this shuffle swaps the order of elements from exactly | |||
2185 | /// one source vector. | |||
2186 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef> | |||
2187 | /// TODO: Optionally allow length-changing shuffles. | |||
2188 | bool isReverse() const { | |||
2189 | return !changesLength() && isReverseMask(ShuffleMask); | |||
2190 | } | |||
2191 | ||||
2192 | /// Return true if this shuffle mask chooses all elements with the same value | |||
2193 | /// as the first element of exactly one source vector. | |||
2194 | /// Example: <4,undef,undef,4> | |||
2195 | /// This assumes that vector operands are the same length as the mask. | |||
2196 | static bool isZeroEltSplatMask(ArrayRef<int> Mask); | |||
2197 | static bool isZeroEltSplatMask(const Constant *Mask) { | |||
2198 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2198, __PRETTY_FUNCTION__)); | |||
2199 | SmallVector<int, 16> MaskAsInts; | |||
2200 | getShuffleMask(Mask, MaskAsInts); | |||
2201 | return isZeroEltSplatMask(MaskAsInts); | |||
2202 | } | |||
2203 | ||||
2204 | /// Return true if all elements of this shuffle are the same value as the | |||
2205 | /// first element of exactly one source vector without changing the length | |||
2206 | /// of that vector. | |||
2207 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0> | |||
2208 | /// TODO: Optionally allow length-changing shuffles. | |||
2209 | /// TODO: Optionally allow splats from other elements. | |||
2210 | bool isZeroEltSplat() const { | |||
2211 | return !changesLength() && isZeroEltSplatMask(ShuffleMask); | |||
2212 | } | |||
2213 | ||||
2214 | /// Return true if this shuffle mask is a transpose mask. | |||
2215 | /// Transpose vector masks transpose a 2xn matrix. They read corresponding | |||
2216 | /// even- or odd-numbered vector elements from two n-dimensional source | |||
2217 | /// vectors and write each result into consecutive elements of an | |||
2218 | /// n-dimensional destination vector. Two shuffles are necessary to complete | |||
2219 | /// the transpose, one for the even elements and another for the odd elements. | |||
2220 | /// This description closely follows how the TRN1 and TRN2 AArch64 | |||
2221 | /// instructions operate. | |||
2222 | /// | |||
2223 | /// For example, a simple 2x2 matrix can be transposed with: | |||
2224 | /// | |||
2225 | /// ; Original matrix | |||
2226 | /// m0 = < a, b > | |||
2227 | /// m1 = < c, d > | |||
2228 | /// | |||
2229 | /// ; Transposed matrix | |||
2230 | /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 > | |||
2231 | /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 > | |||
2232 | /// | |||
2233 | /// For matrices having greater than n columns, the resulting nx2 transposed | |||
2234 | /// matrix is stored in two result vectors such that one vector contains | |||
2235 | /// interleaved elements from all the even-numbered rows and the other vector | |||
2236 | /// contains interleaved elements from all the odd-numbered rows. For example, | |||
2237 | /// a 2x4 matrix can be transposed with: | |||
2238 | /// | |||
2239 | /// ; Original matrix | |||
2240 | /// m0 = < a, b, c, d > | |||
2241 | /// m1 = < e, f, g, h > | |||
2242 | /// | |||
2243 | /// ; Transposed matrix | |||
2244 | /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 > | |||
2245 | /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 > | |||
2246 | static bool isTransposeMask(ArrayRef<int> Mask); | |||
2247 | static bool isTransposeMask(const Constant *Mask) { | |||
2248 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2248, __PRETTY_FUNCTION__)); | |||
2249 | SmallVector<int, 16> MaskAsInts; | |||
2250 | getShuffleMask(Mask, MaskAsInts); | |||
2251 | return isTransposeMask(MaskAsInts); | |||
2252 | } | |||
2253 | ||||
2254 | /// Return true if this shuffle transposes the elements of its inputs without | |||
2255 | /// changing the length of the vectors. This operation may also be known as a | |||
2256 | /// merge or interleave. See the description for isTransposeMask() for the | |||
2257 | /// exact specification. | |||
2258 | /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6> | |||
2259 | bool isTranspose() const { | |||
2260 | return !changesLength() && isTransposeMask(ShuffleMask); | |||
2261 | } | |||
2262 | ||||
2263 | /// Return true if this shuffle mask is an extract subvector mask. | |||
2264 | /// A valid extract subvector mask returns a smaller vector from a single | |||
2265 | /// source operand. The base extraction index is returned as well. | |||
2266 | static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts, | |||
2267 | int &Index); | |||
2268 | static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts, | |||
2269 | int &Index) { | |||
2270 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")((Mask->getType()->isVectorTy() && "Shuffle needs vector constant." ) ? static_cast<void> (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2270, __PRETTY_FUNCTION__)); | |||
2271 | SmallVector<int, 16> MaskAsInts; | |||
2272 | getShuffleMask(Mask, MaskAsInts); | |||
2273 | return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index); | |||
2274 | } | |||
2275 | ||||
2276 | /// Return true if this shuffle mask is an extract subvector mask. | |||
2277 | bool isExtractSubvectorMask(int &Index) const { | |||
2278 | int NumSrcElts = | |||
2279 | cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); | |||
2280 | return isExtractSubvectorMask(ShuffleMask, NumSrcElts, Index); | |||
2281 | } | |||
2282 | ||||
2283 | /// Change values in a shuffle permute mask assuming the two vector operands | |||
2284 | /// of length InVecNumElts have swapped position. | |||
2285 | static void commuteShuffleMask(MutableArrayRef<int> Mask, | |||
2286 | unsigned InVecNumElts) { | |||
2287 | for (int &Idx : Mask) { | |||
2288 | if (Idx == -1) | |||
2289 | continue; | |||
2290 | Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts; | |||
2291 | assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&((Idx >= 0 && Idx < (int)InVecNumElts * 2 && "shufflevector mask index out of range") ? static_cast<void > (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2292, __PRETTY_FUNCTION__)) | |||
2292 | "shufflevector mask index out of range")((Idx >= 0 && Idx < (int)InVecNumElts * 2 && "shufflevector mask index out of range") ? static_cast<void > (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2292, __PRETTY_FUNCTION__)); | |||
2293 | } | |||
2294 | } | |||
2295 | ||||
2296 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2297 | static bool classof(const Instruction *I) { | |||
2298 | return I->getOpcode() == Instruction::ShuffleVector; | |||
2299 | } | |||
2300 | static bool classof(const Value *V) { | |||
2301 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2302 | } | |||
2303 | }; | |||
2304 | ||||
2305 | template <> | |||
2306 | struct OperandTraits<ShuffleVectorInst> | |||
2307 | : public FixedNumOperandTraits<ShuffleVectorInst, 2> {}; | |||
2308 | ||||
2309 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)ShuffleVectorInst::op_iterator ShuffleVectorInst::op_begin() { return OperandTraits<ShuffleVectorInst>::op_begin(this ); } ShuffleVectorInst::const_op_iterator ShuffleVectorInst:: op_begin() const { return OperandTraits<ShuffleVectorInst> ::op_begin(const_cast<ShuffleVectorInst*>(this)); } ShuffleVectorInst ::op_iterator ShuffleVectorInst::op_end() { return OperandTraits <ShuffleVectorInst>::op_end(this); } ShuffleVectorInst:: const_op_iterator ShuffleVectorInst::op_end() const { return OperandTraits <ShuffleVectorInst>::op_end(const_cast<ShuffleVectorInst *>(this)); } Value *ShuffleVectorInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ShuffleVectorInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2309, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ShuffleVectorInst>::op_begin(const_cast <ShuffleVectorInst*>(this))[i_nocapture].get()); } void ShuffleVectorInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<ShuffleVectorInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2309, __PRETTY_FUNCTION__)); OperandTraits<ShuffleVectorInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ShuffleVectorInst::getNumOperands() const { return OperandTraits <ShuffleVectorInst>::operands(this); } template <int Idx_nocapture> Use &ShuffleVectorInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &ShuffleVectorInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
2310 | ||||
2311 | //===----------------------------------------------------------------------===// | |||
2312 | // ExtractValueInst Class | |||
2313 | //===----------------------------------------------------------------------===// | |||
2314 | ||||
2315 | /// This instruction extracts a struct member or array | |||
2316 | /// element value from an aggregate value. | |||
2317 | /// | |||
2318 | class ExtractValueInst : public UnaryInstruction { | |||
2319 | SmallVector<unsigned, 4> Indices; | |||
2320 | ||||
2321 | ExtractValueInst(const ExtractValueInst &EVI); | |||
2322 | ||||
2323 | /// Constructors - Create a extractvalue instruction with a base aggregate | |||
2324 | /// value and a list of indices. The first ctor can optionally insert before | |||
2325 | /// an existing instruction, the second appends the new instruction to the | |||
2326 | /// specified BasicBlock. | |||
2327 | inline ExtractValueInst(Value *Agg, | |||
2328 | ArrayRef<unsigned> Idxs, | |||
2329 | const Twine &NameStr, | |||
2330 | Instruction *InsertBefore); | |||
2331 | inline ExtractValueInst(Value *Agg, | |||
2332 | ArrayRef<unsigned> Idxs, | |||
2333 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2334 | ||||
2335 | void init(ArrayRef<unsigned> Idxs, const Twine &NameStr); | |||
2336 | ||||
2337 | protected: | |||
2338 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2339 | friend class Instruction; | |||
2340 | ||||
2341 | ExtractValueInst *cloneImpl() const; | |||
2342 | ||||
2343 | public: | |||
2344 | static ExtractValueInst *Create(Value *Agg, | |||
2345 | ArrayRef<unsigned> Idxs, | |||
2346 | const Twine &NameStr = "", | |||
2347 | Instruction *InsertBefore = nullptr) { | |||
2348 | return new | |||
2349 | ExtractValueInst(Agg, Idxs, NameStr, InsertBefore); | |||
2350 | } | |||
2351 | ||||
2352 | static ExtractValueInst *Create(Value *Agg, | |||
2353 | ArrayRef<unsigned> Idxs, | |||
2354 | const Twine &NameStr, | |||
2355 | BasicBlock *InsertAtEnd) { | |||
2356 | return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd); | |||
2357 | } | |||
2358 | ||||
2359 | /// Returns the type of the element that would be extracted | |||
2360 | /// with an extractvalue instruction with the specified parameters. | |||
2361 | /// | |||
2362 | /// Null is returned if the indices are invalid for the specified type. | |||
2363 | static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs); | |||
2364 | ||||
2365 | using idx_iterator = const unsigned*; | |||
2366 | ||||
2367 | inline idx_iterator idx_begin() const { return Indices.begin(); } | |||
2368 | inline idx_iterator idx_end() const { return Indices.end(); } | |||
2369 | inline iterator_range<idx_iterator> indices() const { | |||
2370 | return make_range(idx_begin(), idx_end()); | |||
2371 | } | |||
2372 | ||||
2373 | Value *getAggregateOperand() { | |||
2374 | return getOperand(0); | |||
2375 | } | |||
2376 | const Value *getAggregateOperand() const { | |||
2377 | return getOperand(0); | |||
2378 | } | |||
2379 | static unsigned getAggregateOperandIndex() { | |||
2380 | return 0U; // get index for modifying correct operand | |||
2381 | } | |||
2382 | ||||
2383 | ArrayRef<unsigned> getIndices() const { | |||
2384 | return Indices; | |||
2385 | } | |||
2386 | ||||
2387 | unsigned getNumIndices() const { | |||
2388 | return (unsigned)Indices.size(); | |||
2389 | } | |||
2390 | ||||
2391 | bool hasIndices() const { | |||
2392 | return true; | |||
2393 | } | |||
2394 | ||||
2395 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2396 | static bool classof(const Instruction *I) { | |||
2397 | return I->getOpcode() == Instruction::ExtractValue; | |||
2398 | } | |||
2399 | static bool classof(const Value *V) { | |||
2400 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2401 | } | |||
2402 | }; | |||
2403 | ||||
2404 | ExtractValueInst::ExtractValueInst(Value *Agg, | |||
2405 | ArrayRef<unsigned> Idxs, | |||
2406 | const Twine &NameStr, | |||
2407 | Instruction *InsertBefore) | |||
2408 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), | |||
2409 | ExtractValue, Agg, InsertBefore) { | |||
2410 | init(Idxs, NameStr); | |||
2411 | } | |||
2412 | ||||
2413 | ExtractValueInst::ExtractValueInst(Value *Agg, | |||
2414 | ArrayRef<unsigned> Idxs, | |||
2415 | const Twine &NameStr, | |||
2416 | BasicBlock *InsertAtEnd) | |||
2417 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), | |||
2418 | ExtractValue, Agg, InsertAtEnd) { | |||
2419 | init(Idxs, NameStr); | |||
2420 | } | |||
2421 | ||||
2422 | //===----------------------------------------------------------------------===// | |||
2423 | // InsertValueInst Class | |||
2424 | //===----------------------------------------------------------------------===// | |||
2425 | ||||
2426 | /// This instruction inserts a struct field of array element | |||
2427 | /// value into an aggregate value. | |||
2428 | /// | |||
2429 | class InsertValueInst : public Instruction { | |||
2430 | SmallVector<unsigned, 4> Indices; | |||
2431 | ||||
2432 | InsertValueInst(const InsertValueInst &IVI); | |||
2433 | ||||
2434 | /// Constructors - Create a insertvalue instruction with a base aggregate | |||
2435 | /// value, a value to insert, and a list of indices. The first ctor can | |||
2436 | /// optionally insert before an existing instruction, the second appends | |||
2437 | /// the new instruction to the specified BasicBlock. | |||
2438 | inline InsertValueInst(Value *Agg, Value *Val, | |||
2439 | ArrayRef<unsigned> Idxs, | |||
2440 | const Twine &NameStr, | |||
2441 | Instruction *InsertBefore); | |||
2442 | inline InsertValueInst(Value *Agg, Value *Val, | |||
2443 | ArrayRef<unsigned> Idxs, | |||
2444 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2445 | ||||
2446 | /// Constructors - These two constructors are convenience methods because one | |||
2447 | /// and two index insertvalue instructions are so common. | |||
2448 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, | |||
2449 | const Twine &NameStr = "", | |||
2450 | Instruction *InsertBefore = nullptr); | |||
2451 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr, | |||
2452 | BasicBlock *InsertAtEnd); | |||
2453 | ||||
2454 | void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, | |||
2455 | const Twine &NameStr); | |||
2456 | ||||
2457 | protected: | |||
2458 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2459 | friend class Instruction; | |||
2460 | ||||
2461 | InsertValueInst *cloneImpl() const; | |||
2462 | ||||
2463 | public: | |||
2464 | // allocate space for exactly two operands | |||
2465 | void *operator new(size_t s) { | |||
2466 | return User::operator new(s, 2); | |||
2467 | } | |||
2468 | ||||
2469 | static InsertValueInst *Create(Value *Agg, Value *Val, | |||
2470 | ArrayRef<unsigned> Idxs, | |||
2471 | const Twine &NameStr = "", | |||
2472 | Instruction *InsertBefore = nullptr) { | |||
2473 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore); | |||
2474 | } | |||
2475 | ||||
2476 | static InsertValueInst *Create(Value *Agg, Value *Val, | |||
2477 | ArrayRef<unsigned> Idxs, | |||
2478 | const Twine &NameStr, | |||
2479 | BasicBlock *InsertAtEnd) { | |||
2480 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd); | |||
2481 | } | |||
2482 | ||||
2483 | /// Transparently provide more efficient getOperand methods. | |||
2484 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
2485 | ||||
2486 | using idx_iterator = const unsigned*; | |||
2487 | ||||
2488 | inline idx_iterator idx_begin() const { return Indices.begin(); } | |||
2489 | inline idx_iterator idx_end() const { return Indices.end(); } | |||
2490 | inline iterator_range<idx_iterator> indices() const { | |||
2491 | return make_range(idx_begin(), idx_end()); | |||
2492 | } | |||
2493 | ||||
2494 | Value *getAggregateOperand() { | |||
2495 | return getOperand(0); | |||
2496 | } | |||
2497 | const Value *getAggregateOperand() const { | |||
2498 | return getOperand(0); | |||
2499 | } | |||
2500 | static unsigned getAggregateOperandIndex() { | |||
2501 | return 0U; // get index for modifying correct operand | |||
2502 | } | |||
2503 | ||||
2504 | Value *getInsertedValueOperand() { | |||
2505 | return getOperand(1); | |||
2506 | } | |||
2507 | const Value *getInsertedValueOperand() const { | |||
2508 | return getOperand(1); | |||
2509 | } | |||
2510 | static unsigned getInsertedValueOperandIndex() { | |||
2511 | return 1U; // get index for modifying correct operand | |||
2512 | } | |||
2513 | ||||
2514 | ArrayRef<unsigned> getIndices() const { | |||
2515 | return Indices; | |||
2516 | } | |||
2517 | ||||
2518 | unsigned getNumIndices() const { | |||
2519 | return (unsigned)Indices.size(); | |||
2520 | } | |||
2521 | ||||
2522 | bool hasIndices() const { | |||
2523 | return true; | |||
2524 | } | |||
2525 | ||||
2526 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2527 | static bool classof(const Instruction *I) { | |||
2528 | return I->getOpcode() == Instruction::InsertValue; | |||
2529 | } | |||
2530 | static bool classof(const Value *V) { | |||
2531 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2532 | } | |||
2533 | }; | |||
2534 | ||||
2535 | template <> | |||
2536 | struct OperandTraits<InsertValueInst> : | |||
2537 | public FixedNumOperandTraits<InsertValueInst, 2> { | |||
2538 | }; | |||
2539 | ||||
2540 | InsertValueInst::InsertValueInst(Value *Agg, | |||
2541 | Value *Val, | |||
2542 | ArrayRef<unsigned> Idxs, | |||
2543 | const Twine &NameStr, | |||
2544 | Instruction *InsertBefore) | |||
2545 | : Instruction(Agg->getType(), InsertValue, | |||
2546 | OperandTraits<InsertValueInst>::op_begin(this), | |||
2547 | 2, InsertBefore) { | |||
2548 | init(Agg, Val, Idxs, NameStr); | |||
2549 | } | |||
2550 | ||||
2551 | InsertValueInst::InsertValueInst(Value *Agg, | |||
2552 | Value *Val, | |||
2553 | ArrayRef<unsigned> Idxs, | |||
2554 | const Twine &NameStr, | |||
2555 | BasicBlock *InsertAtEnd) | |||
2556 | : Instruction(Agg->getType(), InsertValue, | |||
2557 | OperandTraits<InsertValueInst>::op_begin(this), | |||
2558 | 2, InsertAtEnd) { | |||
2559 | init(Agg, Val, Idxs, NameStr); | |||
2560 | } | |||
2561 | ||||
2562 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)InsertValueInst::op_iterator InsertValueInst::op_begin() { return OperandTraits<InsertValueInst>::op_begin(this); } InsertValueInst ::const_op_iterator InsertValueInst::op_begin() const { return OperandTraits<InsertValueInst>::op_begin(const_cast< InsertValueInst*>(this)); } InsertValueInst::op_iterator InsertValueInst ::op_end() { return OperandTraits<InsertValueInst>::op_end (this); } InsertValueInst::const_op_iterator InsertValueInst:: op_end() const { return OperandTraits<InsertValueInst>:: op_end(const_cast<InsertValueInst*>(this)); } Value *InsertValueInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<InsertValueInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2562, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<InsertValueInst>::op_begin(const_cast< InsertValueInst*>(this))[i_nocapture].get()); } void InsertValueInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<InsertValueInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2562, __PRETTY_FUNCTION__)); OperandTraits<InsertValueInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned InsertValueInst::getNumOperands() const { return OperandTraits <InsertValueInst>::operands(this); } template <int Idx_nocapture > Use &InsertValueInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &InsertValueInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
2563 | ||||
2564 | //===----------------------------------------------------------------------===// | |||
2565 | // PHINode Class | |||
2566 | //===----------------------------------------------------------------------===// | |||
2567 | ||||
2568 | // PHINode - The PHINode class is used to represent the magical mystical PHI | |||
2569 | // node, that can not exist in nature, but can be synthesized in a computer | |||
2570 | // scientist's overactive imagination. | |||
2571 | // | |||
2572 | class PHINode : public Instruction { | |||
2573 | /// The number of operands actually allocated. NumOperands is | |||
2574 | /// the number actually in use. | |||
2575 | unsigned ReservedSpace; | |||
2576 | ||||
2577 | PHINode(const PHINode &PN); | |||
2578 | ||||
2579 | explicit PHINode(Type *Ty, unsigned NumReservedValues, | |||
2580 | const Twine &NameStr = "", | |||
2581 | Instruction *InsertBefore = nullptr) | |||
2582 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore), | |||
2583 | ReservedSpace(NumReservedValues) { | |||
2584 | setName(NameStr); | |||
2585 | allocHungoffUses(ReservedSpace); | |||
2586 | } | |||
2587 | ||||
2588 | PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr, | |||
2589 | BasicBlock *InsertAtEnd) | |||
2590 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd), | |||
2591 | ReservedSpace(NumReservedValues) { | |||
2592 | setName(NameStr); | |||
2593 | allocHungoffUses(ReservedSpace); | |||
2594 | } | |||
2595 | ||||
2596 | protected: | |||
2597 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2598 | friend class Instruction; | |||
2599 | ||||
2600 | PHINode *cloneImpl() const; | |||
2601 | ||||
2602 | // allocHungoffUses - this is more complicated than the generic | |||
2603 | // User::allocHungoffUses, because we have to allocate Uses for the incoming | |||
2604 | // values and pointers to the incoming blocks, all in one allocation. | |||
2605 | void allocHungoffUses(unsigned N) { | |||
2606 | User::allocHungoffUses(N, /* IsPhi */ true); | |||
2607 | } | |||
2608 | ||||
2609 | public: | |||
2610 | /// Constructors - NumReservedValues is a hint for the number of incoming | |||
2611 | /// edges that this phi node will have (use 0 if you really have no idea). | |||
2612 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, | |||
2613 | const Twine &NameStr = "", | |||
2614 | Instruction *InsertBefore = nullptr) { | |||
2615 | return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore); | |||
2616 | } | |||
2617 | ||||
2618 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, | |||
2619 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
2620 | return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd); | |||
2621 | } | |||
2622 | ||||
2623 | /// Provide fast operand accessors | |||
2624 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
2625 | ||||
2626 | // Block iterator interface. This provides access to the list of incoming | |||
2627 | // basic blocks, which parallels the list of incoming values. | |||
2628 | ||||
2629 | using block_iterator = BasicBlock **; | |||
2630 | using const_block_iterator = BasicBlock * const *; | |||
2631 | ||||
2632 | block_iterator block_begin() { | |||
2633 | return reinterpret_cast<block_iterator>(op_begin() + ReservedSpace); | |||
2634 | } | |||
2635 | ||||
2636 | const_block_iterator block_begin() const { | |||
2637 | return reinterpret_cast<const_block_iterator>(op_begin() + ReservedSpace); | |||
2638 | } | |||
2639 | ||||
2640 | block_iterator block_end() { | |||
2641 | return block_begin() + getNumOperands(); | |||
2642 | } | |||
2643 | ||||
2644 | const_block_iterator block_end() const { | |||
2645 | return block_begin() + getNumOperands(); | |||
2646 | } | |||
2647 | ||||
2648 | iterator_range<block_iterator> blocks() { | |||
2649 | return make_range(block_begin(), block_end()); | |||
2650 | } | |||
2651 | ||||
2652 | iterator_range<const_block_iterator> blocks() const { | |||
2653 | return make_range(block_begin(), block_end()); | |||
2654 | } | |||
2655 | ||||
2656 | op_range incoming_values() { return operands(); } | |||
2657 | ||||
2658 | const_op_range incoming_values() const { return operands(); } | |||
2659 | ||||
2660 | /// Return the number of incoming edges | |||
2661 | /// | |||
2662 | unsigned getNumIncomingValues() const { return getNumOperands(); } | |||
2663 | ||||
2664 | /// Return incoming value number x | |||
2665 | /// | |||
2666 | Value *getIncomingValue(unsigned i) const { | |||
2667 | return getOperand(i); | |||
2668 | } | |||
2669 | void setIncomingValue(unsigned i, Value *V) { | |||
2670 | assert(V && "PHI node got a null value!")((V && "PHI node got a null value!") ? static_cast< void> (0) : __assert_fail ("V && \"PHI node got a null value!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2670, __PRETTY_FUNCTION__)); | |||
2671 | assert(getType() == V->getType() &&((getType() == V->getType() && "All operands to PHI node must be the same type as the PHI node!" ) ? static_cast<void> (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2672, __PRETTY_FUNCTION__)) | |||
2672 | "All operands to PHI node must be the same type as the PHI node!")((getType() == V->getType() && "All operands to PHI node must be the same type as the PHI node!" ) ? static_cast<void> (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2672, __PRETTY_FUNCTION__)); | |||
2673 | setOperand(i, V); | |||
2674 | } | |||
2675 | ||||
2676 | static unsigned getOperandNumForIncomingValue(unsigned i) { | |||
2677 | return i; | |||
2678 | } | |||
2679 | ||||
2680 | static unsigned getIncomingValueNumForOperand(unsigned i) { | |||
2681 | return i; | |||
2682 | } | |||
2683 | ||||
2684 | /// Return incoming basic block number @p i. | |||
2685 | /// | |||
2686 | BasicBlock *getIncomingBlock(unsigned i) const { | |||
2687 | return block_begin()[i]; | |||
2688 | } | |||
2689 | ||||
2690 | /// Return incoming basic block corresponding | |||
2691 | /// to an operand of the PHI. | |||
2692 | /// | |||
2693 | BasicBlock *getIncomingBlock(const Use &U) const { | |||
2694 | assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?")((this == U.getUser() && "Iterator doesn't point to PHI's Uses?" ) ? static_cast<void> (0) : __assert_fail ("this == U.getUser() && \"Iterator doesn't point to PHI's Uses?\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2694, __PRETTY_FUNCTION__)); | |||
2695 | return getIncomingBlock(unsigned(&U - op_begin())); | |||
2696 | } | |||
2697 | ||||
2698 | /// Return incoming basic block corresponding | |||
2699 | /// to value use iterator. | |||
2700 | /// | |||
2701 | BasicBlock *getIncomingBlock(Value::const_user_iterator I) const { | |||
2702 | return getIncomingBlock(I.getUse()); | |||
2703 | } | |||
2704 | ||||
2705 | void setIncomingBlock(unsigned i, BasicBlock *BB) { | |||
2706 | assert(BB && "PHI node got a null basic block!")((BB && "PHI node got a null basic block!") ? static_cast <void> (0) : __assert_fail ("BB && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2706, __PRETTY_FUNCTION__)); | |||
2707 | block_begin()[i] = BB; | |||
2708 | } | |||
2709 | ||||
2710 | /// Replace every incoming basic block \p Old to basic block \p New. | |||
2711 | void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) { | |||
2712 | assert(New && Old && "PHI node got a null basic block!")((New && Old && "PHI node got a null basic block!" ) ? static_cast<void> (0) : __assert_fail ("New && Old && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2712, __PRETTY_FUNCTION__)); | |||
2713 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) | |||
2714 | if (getIncomingBlock(Op) == Old) | |||
2715 | setIncomingBlock(Op, New); | |||
2716 | } | |||
2717 | ||||
2718 | /// Add an incoming value to the end of the PHI list | |||
2719 | /// | |||
2720 | void addIncoming(Value *V, BasicBlock *BB) { | |||
2721 | if (getNumOperands() == ReservedSpace) | |||
2722 | growOperands(); // Get more space! | |||
2723 | // Initialize some new operands. | |||
2724 | setNumHungOffUseOperands(getNumOperands() + 1); | |||
2725 | setIncomingValue(getNumOperands() - 1, V); | |||
2726 | setIncomingBlock(getNumOperands() - 1, BB); | |||
2727 | } | |||
2728 | ||||
2729 | /// Remove an incoming value. This is useful if a | |||
2730 | /// predecessor basic block is deleted. The value removed is returned. | |||
2731 | /// | |||
2732 | /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty | |||
2733 | /// is true), the PHI node is destroyed and any uses of it are replaced with | |||
2734 | /// dummy values. The only time there should be zero incoming values to a PHI | |||
2735 | /// node is when the block is dead, so this strategy is sound. | |||
2736 | /// | |||
2737 | Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); | |||
2738 | ||||
2739 | Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) { | |||
2740 | int Idx = getBasicBlockIndex(BB); | |||
2741 | assert(Idx >= 0 && "Invalid basic block argument to remove!")((Idx >= 0 && "Invalid basic block argument to remove!" ) ? static_cast<void> (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument to remove!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2741, __PRETTY_FUNCTION__)); | |||
2742 | return removeIncomingValue(Idx, DeletePHIIfEmpty); | |||
2743 | } | |||
2744 | ||||
2745 | /// Return the first index of the specified basic | |||
2746 | /// block in the value list for this PHI. Returns -1 if no instance. | |||
2747 | /// | |||
2748 | int getBasicBlockIndex(const BasicBlock *BB) const { | |||
2749 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) | |||
2750 | if (block_begin()[i] == BB) | |||
2751 | return i; | |||
2752 | return -1; | |||
2753 | } | |||
2754 | ||||
2755 | Value *getIncomingValueForBlock(const BasicBlock *BB) const { | |||
2756 | int Idx = getBasicBlockIndex(BB); | |||
2757 | assert(Idx >= 0 && "Invalid basic block argument!")((Idx >= 0 && "Invalid basic block argument!") ? static_cast <void> (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2757, __PRETTY_FUNCTION__)); | |||
2758 | return getIncomingValue(Idx); | |||
2759 | } | |||
2760 | ||||
2761 | /// Set every incoming value(s) for block \p BB to \p V. | |||
2762 | void setIncomingValueForBlock(const BasicBlock *BB, Value *V) { | |||
2763 | assert(BB && "PHI node got a null basic block!")((BB && "PHI node got a null basic block!") ? static_cast <void> (0) : __assert_fail ("BB && \"PHI node got a null basic block!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2763, __PRETTY_FUNCTION__)); | |||
2764 | bool Found = false; | |||
2765 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) | |||
2766 | if (getIncomingBlock(Op) == BB) { | |||
2767 | Found = true; | |||
2768 | setIncomingValue(Op, V); | |||
2769 | } | |||
2770 | (void)Found; | |||
2771 | assert(Found && "Invalid basic block argument to set!")((Found && "Invalid basic block argument to set!") ? static_cast <void> (0) : __assert_fail ("Found && \"Invalid basic block argument to set!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2771, __PRETTY_FUNCTION__)); | |||
2772 | } | |||
2773 | ||||
2774 | /// If the specified PHI node always merges together the | |||
2775 | /// same value, return the value, otherwise return null. | |||
2776 | Value *hasConstantValue() const; | |||
2777 | ||||
2778 | /// Whether the specified PHI node always merges | |||
2779 | /// together the same value, assuming undefs are equal to a unique | |||
2780 | /// non-undef value. | |||
2781 | bool hasConstantOrUndefValue() const; | |||
2782 | ||||
2783 | /// If the PHI node is complete which means all of its parent's predecessors | |||
2784 | /// have incoming value in this PHI, return true, otherwise return false. | |||
2785 | bool isComplete() const { | |||
2786 | return llvm::all_of(predecessors(getParent()), | |||
2787 | [this](const BasicBlock *Pred) { | |||
2788 | return getBasicBlockIndex(Pred) >= 0; | |||
2789 | }); | |||
2790 | } | |||
2791 | ||||
2792 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2793 | static bool classof(const Instruction *I) { | |||
2794 | return I->getOpcode() == Instruction::PHI; | |||
2795 | } | |||
2796 | static bool classof(const Value *V) { | |||
2797 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2798 | } | |||
2799 | ||||
2800 | private: | |||
2801 | void growOperands(); | |||
2802 | }; | |||
2803 | ||||
2804 | template <> | |||
2805 | struct OperandTraits<PHINode> : public HungoffOperandTraits<2> { | |||
2806 | }; | |||
2807 | ||||
2808 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)PHINode::op_iterator PHINode::op_begin() { return OperandTraits <PHINode>::op_begin(this); } PHINode::const_op_iterator PHINode::op_begin() const { return OperandTraits<PHINode> ::op_begin(const_cast<PHINode*>(this)); } PHINode::op_iterator PHINode::op_end() { return OperandTraits<PHINode>::op_end (this); } PHINode::const_op_iterator PHINode::op_end() const { return OperandTraits<PHINode>::op_end(const_cast<PHINode *>(this)); } Value *PHINode::getOperand(unsigned i_nocapture ) const { ((i_nocapture < OperandTraits<PHINode>::operands (this) && "getOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2808, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<PHINode>::op_begin(const_cast<PHINode *>(this))[i_nocapture].get()); } void PHINode::setOperand( unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<PHINode>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2808, __PRETTY_FUNCTION__)); OperandTraits<PHINode>:: op_begin(this)[i_nocapture] = Val_nocapture; } unsigned PHINode ::getNumOperands() const { return OperandTraits<PHINode> ::operands(this); } template <int Idx_nocapture> Use & PHINode::Op() { return this->OpFrom<Idx_nocapture>(this ); } template <int Idx_nocapture> const Use &PHINode ::Op() const { return this->OpFrom<Idx_nocapture>(this ); } | |||
2809 | ||||
2810 | //===----------------------------------------------------------------------===// | |||
2811 | // LandingPadInst Class | |||
2812 | //===----------------------------------------------------------------------===// | |||
2813 | ||||
2814 | //===--------------------------------------------------------------------------- | |||
2815 | /// The landingpad instruction holds all of the information | |||
2816 | /// necessary to generate correct exception handling. The landingpad instruction | |||
2817 | /// cannot be moved from the top of a landing pad block, which itself is | |||
2818 | /// accessible only from the 'unwind' edge of an invoke. This uses the | |||
2819 | /// SubclassData field in Value to store whether or not the landingpad is a | |||
2820 | /// cleanup. | |||
2821 | /// | |||
2822 | class LandingPadInst : public Instruction { | |||
2823 | using CleanupField = BoolBitfieldElementT<0>; | |||
2824 | ||||
2825 | /// The number of operands actually allocated. NumOperands is | |||
2826 | /// the number actually in use. | |||
2827 | unsigned ReservedSpace; | |||
2828 | ||||
2829 | LandingPadInst(const LandingPadInst &LP); | |||
2830 | ||||
2831 | public: | |||
2832 | enum ClauseType { Catch, Filter }; | |||
2833 | ||||
2834 | private: | |||
2835 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, | |||
2836 | const Twine &NameStr, Instruction *InsertBefore); | |||
2837 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, | |||
2838 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2839 | ||||
2840 | // Allocate space for exactly zero operands. | |||
2841 | void *operator new(size_t s) { | |||
2842 | return User::operator new(s); | |||
2843 | } | |||
2844 | ||||
2845 | void growOperands(unsigned Size); | |||
2846 | void init(unsigned NumReservedValues, const Twine &NameStr); | |||
2847 | ||||
2848 | protected: | |||
2849 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2850 | friend class Instruction; | |||
2851 | ||||
2852 | LandingPadInst *cloneImpl() const; | |||
2853 | ||||
2854 | public: | |||
2855 | /// Constructors - NumReservedClauses is a hint for the number of incoming | |||
2856 | /// clauses that this landingpad will have (use 0 if you really have no idea). | |||
2857 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, | |||
2858 | const Twine &NameStr = "", | |||
2859 | Instruction *InsertBefore = nullptr); | |||
2860 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, | |||
2861 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
2862 | ||||
2863 | /// Provide fast operand accessors | |||
2864 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
2865 | ||||
2866 | /// Return 'true' if this landingpad instruction is a | |||
2867 | /// cleanup. I.e., it should be run when unwinding even if its landing pad | |||
2868 | /// doesn't catch the exception. | |||
2869 | bool isCleanup() const { return getSubclassData<CleanupField>(); } | |||
2870 | ||||
2871 | /// Indicate that this landingpad instruction is a cleanup. | |||
2872 | void setCleanup(bool V) { setSubclassData<CleanupField>(V); } | |||
2873 | ||||
2874 | /// Add a catch or filter clause to the landing pad. | |||
2875 | void addClause(Constant *ClauseVal); | |||
2876 | ||||
2877 | /// Get the value of the clause at index Idx. Use isCatch/isFilter to | |||
2878 | /// determine what type of clause this is. | |||
2879 | Constant *getClause(unsigned Idx) const { | |||
2880 | return cast<Constant>(getOperandList()[Idx]); | |||
2881 | } | |||
2882 | ||||
2883 | /// Return 'true' if the clause and index Idx is a catch clause. | |||
2884 | bool isCatch(unsigned Idx) const { | |||
2885 | return !isa<ArrayType>(getOperandList()[Idx]->getType()); | |||
2886 | } | |||
2887 | ||||
2888 | /// Return 'true' if the clause and index Idx is a filter clause. | |||
2889 | bool isFilter(unsigned Idx) const { | |||
2890 | return isa<ArrayType>(getOperandList()[Idx]->getType()); | |||
2891 | } | |||
2892 | ||||
2893 | /// Get the number of clauses for this landing pad. | |||
2894 | unsigned getNumClauses() const { return getNumOperands(); } | |||
2895 | ||||
2896 | /// Grow the size of the operand list to accommodate the new | |||
2897 | /// number of clauses. | |||
2898 | void reserveClauses(unsigned Size) { growOperands(Size); } | |||
2899 | ||||
2900 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2901 | static bool classof(const Instruction *I) { | |||
2902 | return I->getOpcode() == Instruction::LandingPad; | |||
2903 | } | |||
2904 | static bool classof(const Value *V) { | |||
2905 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2906 | } | |||
2907 | }; | |||
2908 | ||||
2909 | template <> | |||
2910 | struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> { | |||
2911 | }; | |||
2912 | ||||
2913 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)LandingPadInst::op_iterator LandingPadInst::op_begin() { return OperandTraits<LandingPadInst>::op_begin(this); } LandingPadInst ::const_op_iterator LandingPadInst::op_begin() const { return OperandTraits<LandingPadInst>::op_begin(const_cast< LandingPadInst*>(this)); } LandingPadInst::op_iterator LandingPadInst ::op_end() { return OperandTraits<LandingPadInst>::op_end (this); } LandingPadInst::const_op_iterator LandingPadInst::op_end () const { return OperandTraits<LandingPadInst>::op_end (const_cast<LandingPadInst*>(this)); } Value *LandingPadInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<LandingPadInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2913, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<LandingPadInst>::op_begin(const_cast< LandingPadInst*>(this))[i_nocapture].get()); } void LandingPadInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<LandingPadInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2913, __PRETTY_FUNCTION__)); OperandTraits<LandingPadInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned LandingPadInst::getNumOperands() const { return OperandTraits <LandingPadInst>::operands(this); } template <int Idx_nocapture > Use &LandingPadInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &LandingPadInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
2914 | ||||
2915 | //===----------------------------------------------------------------------===// | |||
2916 | // ReturnInst Class | |||
2917 | //===----------------------------------------------------------------------===// | |||
2918 | ||||
2919 | //===--------------------------------------------------------------------------- | |||
2920 | /// Return a value (possibly void), from a function. Execution | |||
2921 | /// does not continue in this function any longer. | |||
2922 | /// | |||
2923 | class ReturnInst : public Instruction { | |||
2924 | ReturnInst(const ReturnInst &RI); | |||
2925 | ||||
2926 | private: | |||
2927 | // ReturnInst constructors: | |||
2928 | // ReturnInst() - 'ret void' instruction | |||
2929 | // ReturnInst( null) - 'ret void' instruction | |||
2930 | // ReturnInst(Value* X) - 'ret X' instruction | |||
2931 | // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I | |||
2932 | // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I | |||
2933 | // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B | |||
2934 | // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B | |||
2935 | // | |||
2936 | // NOTE: If the Value* passed is of type void then the constructor behaves as | |||
2937 | // if it was passed NULL. | |||
2938 | explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr, | |||
2939 | Instruction *InsertBefore = nullptr); | |||
2940 | ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd); | |||
2941 | explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd); | |||
2942 | ||||
2943 | protected: | |||
2944 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
2945 | friend class Instruction; | |||
2946 | ||||
2947 | ReturnInst *cloneImpl() const; | |||
2948 | ||||
2949 | public: | |||
2950 | static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr, | |||
2951 | Instruction *InsertBefore = nullptr) { | |||
2952 | return new(!!retVal) ReturnInst(C, retVal, InsertBefore); | |||
2953 | } | |||
2954 | ||||
2955 | static ReturnInst* Create(LLVMContext &C, Value *retVal, | |||
2956 | BasicBlock *InsertAtEnd) { | |||
2957 | return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd); | |||
2958 | } | |||
2959 | ||||
2960 | static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) { | |||
2961 | return new(0) ReturnInst(C, InsertAtEnd); | |||
2962 | } | |||
2963 | ||||
2964 | /// Provide fast operand accessors | |||
2965 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
2966 | ||||
2967 | /// Convenience accessor. Returns null if there is no return value. | |||
2968 | Value *getReturnValue() const { | |||
2969 | return getNumOperands() != 0 ? getOperand(0) : nullptr; | |||
2970 | } | |||
2971 | ||||
2972 | unsigned getNumSuccessors() const { return 0; } | |||
2973 | ||||
2974 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
2975 | static bool classof(const Instruction *I) { | |||
2976 | return (I->getOpcode() == Instruction::Ret); | |||
2977 | } | |||
2978 | static bool classof(const Value *V) { | |||
2979 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
2980 | } | |||
2981 | ||||
2982 | private: | |||
2983 | BasicBlock *getSuccessor(unsigned idx) const { | |||
2984 | llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2984); | |||
2985 | } | |||
2986 | ||||
2987 | void setSuccessor(unsigned idx, BasicBlock *B) { | |||
2988 | llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2988); | |||
2989 | } | |||
2990 | }; | |||
2991 | ||||
2992 | template <> | |||
2993 | struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { | |||
2994 | }; | |||
2995 | ||||
2996 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)ReturnInst::op_iterator ReturnInst::op_begin() { return OperandTraits <ReturnInst>::op_begin(this); } ReturnInst::const_op_iterator ReturnInst::op_begin() const { return OperandTraits<ReturnInst >::op_begin(const_cast<ReturnInst*>(this)); } ReturnInst ::op_iterator ReturnInst::op_end() { return OperandTraits< ReturnInst>::op_end(this); } ReturnInst::const_op_iterator ReturnInst::op_end() const { return OperandTraits<ReturnInst >::op_end(const_cast<ReturnInst*>(this)); } Value *ReturnInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ReturnInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2996, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ReturnInst>::op_begin(const_cast<ReturnInst *>(this))[i_nocapture].get()); } void ReturnInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<ReturnInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 2996, __PRETTY_FUNCTION__)); OperandTraits<ReturnInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ReturnInst ::getNumOperands() const { return OperandTraits<ReturnInst >::operands(this); } template <int Idx_nocapture> Use &ReturnInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & ReturnInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
2997 | ||||
2998 | //===----------------------------------------------------------------------===// | |||
2999 | // BranchInst Class | |||
3000 | //===----------------------------------------------------------------------===// | |||
3001 | ||||
3002 | //===--------------------------------------------------------------------------- | |||
3003 | /// Conditional or Unconditional Branch instruction. | |||
3004 | /// | |||
3005 | class BranchInst : public Instruction { | |||
3006 | /// Ops list - Branches are strange. The operands are ordered: | |||
3007 | /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because | |||
3008 | /// they don't have to check for cond/uncond branchness. These are mostly | |||
3009 | /// accessed relative from op_end(). | |||
3010 | BranchInst(const BranchInst &BI); | |||
3011 | // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): | |||
3012 | // BranchInst(BB *B) - 'br B' | |||
3013 | // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' | |||
3014 | // BranchInst(BB* B, Inst *I) - 'br B' insert before I | |||
3015 | // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I | |||
3016 | // BranchInst(BB* B, BB *I) - 'br B' insert at end | |||
3017 | // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end | |||
3018 | explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr); | |||
3019 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, | |||
3020 | Instruction *InsertBefore = nullptr); | |||
3021 | BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd); | |||
3022 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, | |||
3023 | BasicBlock *InsertAtEnd); | |||
3024 | ||||
3025 | void AssertOK(); | |||
3026 | ||||
3027 | protected: | |||
3028 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3029 | friend class Instruction; | |||
3030 | ||||
3031 | BranchInst *cloneImpl() const; | |||
3032 | ||||
3033 | public: | |||
3034 | /// Iterator type that casts an operand to a basic block. | |||
3035 | /// | |||
3036 | /// This only makes sense because the successors are stored as adjacent | |||
3037 | /// operands for branch instructions. | |||
3038 | struct succ_op_iterator | |||
3039 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, | |||
3040 | std::random_access_iterator_tag, BasicBlock *, | |||
3041 | ptrdiff_t, BasicBlock *, BasicBlock *> { | |||
3042 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} | |||
3043 | ||||
3044 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } | |||
3045 | BasicBlock *operator->() const { return operator*(); } | |||
3046 | }; | |||
3047 | ||||
3048 | /// The const version of `succ_op_iterator`. | |||
3049 | struct const_succ_op_iterator | |||
3050 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, | |||
3051 | std::random_access_iterator_tag, | |||
3052 | const BasicBlock *, ptrdiff_t, const BasicBlock *, | |||
3053 | const BasicBlock *> { | |||
3054 | explicit const_succ_op_iterator(const_value_op_iterator I) | |||
3055 | : iterator_adaptor_base(I) {} | |||
3056 | ||||
3057 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } | |||
3058 | const BasicBlock *operator->() const { return operator*(); } | |||
3059 | }; | |||
3060 | ||||
3061 | static BranchInst *Create(BasicBlock *IfTrue, | |||
3062 | Instruction *InsertBefore = nullptr) { | |||
3063 | return new(1) BranchInst(IfTrue, InsertBefore); | |||
3064 | } | |||
3065 | ||||
3066 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, | |||
3067 | Value *Cond, Instruction *InsertBefore = nullptr) { | |||
3068 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore); | |||
3069 | } | |||
3070 | ||||
3071 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) { | |||
3072 | return new(1) BranchInst(IfTrue, InsertAtEnd); | |||
3073 | } | |||
3074 | ||||
3075 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, | |||
3076 | Value *Cond, BasicBlock *InsertAtEnd) { | |||
3077 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd); | |||
3078 | } | |||
3079 | ||||
3080 | /// Transparently provide more efficient getOperand methods. | |||
3081 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
3082 | ||||
3083 | bool isUnconditional() const { return getNumOperands() == 1; } | |||
3084 | bool isConditional() const { return getNumOperands() == 3; } | |||
3085 | ||||
3086 | Value *getCondition() const { | |||
3087 | assert(isConditional() && "Cannot get condition of an uncond branch!")((isConditional() && "Cannot get condition of an uncond branch!" ) ? static_cast<void> (0) : __assert_fail ("isConditional() && \"Cannot get condition of an uncond branch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3087, __PRETTY_FUNCTION__)); | |||
3088 | return Op<-3>(); | |||
3089 | } | |||
3090 | ||||
3091 | void setCondition(Value *V) { | |||
3092 | assert(isConditional() && "Cannot set condition of unconditional branch!")((isConditional() && "Cannot set condition of unconditional branch!" ) ? static_cast<void> (0) : __assert_fail ("isConditional() && \"Cannot set condition of unconditional branch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3092, __PRETTY_FUNCTION__)); | |||
3093 | Op<-3>() = V; | |||
3094 | } | |||
3095 | ||||
3096 | unsigned getNumSuccessors() const { return 1+isConditional(); } | |||
3097 | ||||
3098 | BasicBlock *getSuccessor(unsigned i) const { | |||
3099 | assert(i < getNumSuccessors() && "Successor # out of range for Branch!")((i < getNumSuccessors() && "Successor # out of range for Branch!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() && \"Successor # out of range for Branch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3099, __PRETTY_FUNCTION__)); | |||
3100 | return cast_or_null<BasicBlock>((&Op<-1>() - i)->get()); | |||
3101 | } | |||
3102 | ||||
3103 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { | |||
3104 | assert(idx < getNumSuccessors() && "Successor # out of range for Branch!")((idx < getNumSuccessors() && "Successor # out of range for Branch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() && \"Successor # out of range for Branch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3104, __PRETTY_FUNCTION__)); | |||
3105 | *(&Op<-1>() - idx) = NewSucc; | |||
3106 | } | |||
3107 | ||||
3108 | /// Swap the successors of this branch instruction. | |||
3109 | /// | |||
3110 | /// Swaps the successors of the branch instruction. This also swaps any | |||
3111 | /// branch weight metadata associated with the instruction so that it | |||
3112 | /// continues to map correctly to each operand. | |||
3113 | void swapSuccessors(); | |||
3114 | ||||
3115 | iterator_range<succ_op_iterator> successors() { | |||
3116 | return make_range( | |||
3117 | succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)), | |||
3118 | succ_op_iterator(value_op_end())); | |||
3119 | } | |||
3120 | ||||
3121 | iterator_range<const_succ_op_iterator> successors() const { | |||
3122 | return make_range(const_succ_op_iterator( | |||
3123 | std::next(value_op_begin(), isConditional() ? 1 : 0)), | |||
3124 | const_succ_op_iterator(value_op_end())); | |||
3125 | } | |||
3126 | ||||
3127 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
3128 | static bool classof(const Instruction *I) { | |||
3129 | return (I->getOpcode() == Instruction::Br); | |||
3130 | } | |||
3131 | static bool classof(const Value *V) { | |||
3132 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
3133 | } | |||
3134 | }; | |||
3135 | ||||
3136 | template <> | |||
3137 | struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { | |||
3138 | }; | |||
3139 | ||||
3140 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)BranchInst::op_iterator BranchInst::op_begin() { return OperandTraits <BranchInst>::op_begin(this); } BranchInst::const_op_iterator BranchInst::op_begin() const { return OperandTraits<BranchInst >::op_begin(const_cast<BranchInst*>(this)); } BranchInst ::op_iterator BranchInst::op_end() { return OperandTraits< BranchInst>::op_end(this); } BranchInst::const_op_iterator BranchInst::op_end() const { return OperandTraits<BranchInst >::op_end(const_cast<BranchInst*>(this)); } Value *BranchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<BranchInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3140, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<BranchInst>::op_begin(const_cast<BranchInst *>(this))[i_nocapture].get()); } void BranchInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<BranchInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3140, __PRETTY_FUNCTION__)); OperandTraits<BranchInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned BranchInst ::getNumOperands() const { return OperandTraits<BranchInst >::operands(this); } template <int Idx_nocapture> Use &BranchInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & BranchInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
3141 | ||||
3142 | //===----------------------------------------------------------------------===// | |||
3143 | // SwitchInst Class | |||
3144 | //===----------------------------------------------------------------------===// | |||
3145 | ||||
3146 | //===--------------------------------------------------------------------------- | |||
3147 | /// Multiway switch | |||
3148 | /// | |||
3149 | class SwitchInst : public Instruction { | |||
3150 | unsigned ReservedSpace; | |||
3151 | ||||
3152 | // Operand[0] = Value to switch on | |||
3153 | // Operand[1] = Default basic block destination | |||
3154 | // Operand[2n ] = Value to match | |||
3155 | // Operand[2n+1] = BasicBlock to go to on match | |||
3156 | SwitchInst(const SwitchInst &SI); | |||
3157 | ||||
3158 | /// Create a new switch instruction, specifying a value to switch on and a | |||
3159 | /// default destination. The number of additional cases can be specified here | |||
3160 | /// to make memory allocation more efficient. This constructor can also | |||
3161 | /// auto-insert before another instruction. | |||
3162 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, | |||
3163 | Instruction *InsertBefore); | |||
3164 | ||||
3165 | /// Create a new switch instruction, specifying a value to switch on and a | |||
3166 | /// default destination. The number of additional cases can be specified here | |||
3167 | /// to make memory allocation more efficient. This constructor also | |||
3168 | /// auto-inserts at the end of the specified BasicBlock. | |||
3169 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, | |||
3170 | BasicBlock *InsertAtEnd); | |||
3171 | ||||
3172 | // allocate space for exactly zero operands | |||
3173 | void *operator new(size_t s) { | |||
3174 | return User::operator new(s); | |||
3175 | } | |||
3176 | ||||
3177 | void init(Value *Value, BasicBlock *Default, unsigned NumReserved); | |||
3178 | void growOperands(); | |||
3179 | ||||
3180 | protected: | |||
3181 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3182 | friend class Instruction; | |||
3183 | ||||
3184 | SwitchInst *cloneImpl() const; | |||
3185 | ||||
3186 | public: | |||
3187 | // -2 | |||
3188 | static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1); | |||
3189 | ||||
3190 | template <typename CaseHandleT> class CaseIteratorImpl; | |||
3191 | ||||
3192 | /// A handle to a particular switch case. It exposes a convenient interface | |||
3193 | /// to both the case value and the successor block. | |||
3194 | /// | |||
3195 | /// We define this as a template and instantiate it to form both a const and | |||
3196 | /// non-const handle. | |||
3197 | template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT> | |||
3198 | class CaseHandleImpl { | |||
3199 | // Directly befriend both const and non-const iterators. | |||
3200 | friend class SwitchInst::CaseIteratorImpl< | |||
3201 | CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>; | |||
3202 | ||||
3203 | protected: | |||
3204 | // Expose the switch type we're parameterized with to the iterator. | |||
3205 | using SwitchInstType = SwitchInstT; | |||
3206 | ||||
3207 | SwitchInstT *SI; | |||
3208 | ptrdiff_t Index; | |||
3209 | ||||
3210 | CaseHandleImpl() = default; | |||
3211 | CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {} | |||
3212 | ||||
3213 | public: | |||
3214 | /// Resolves case value for current case. | |||
3215 | ConstantIntT *getCaseValue() const { | |||
3216 | assert((unsigned)Index < SI->getNumCases() &&(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3217, __PRETTY_FUNCTION__)) | |||
3217 | "Index out the number of cases.")(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3217, __PRETTY_FUNCTION__)); | |||
3218 | return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2)); | |||
3219 | } | |||
3220 | ||||
3221 | /// Resolves successor for current case. | |||
3222 | BasicBlockT *getCaseSuccessor() const { | |||
3223 | assert(((unsigned)Index < SI->getNumCases() ||((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3225, __PRETTY_FUNCTION__)) | |||
3224 | (unsigned)Index == DefaultPseudoIndex) &&((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3225, __PRETTY_FUNCTION__)) | |||
3225 | "Index out the number of cases.")((((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3225, __PRETTY_FUNCTION__)); | |||
3226 | return SI->getSuccessor(getSuccessorIndex()); | |||
3227 | } | |||
3228 | ||||
3229 | /// Returns number of current case. | |||
3230 | unsigned getCaseIndex() const { return Index; } | |||
3231 | ||||
3232 | /// Returns successor index for current case successor. | |||
3233 | unsigned getSuccessorIndex() const { | |||
3234 | assert(((unsigned)Index == DefaultPseudoIndex ||((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3236, __PRETTY_FUNCTION__)) | |||
3235 | (unsigned)Index < SI->getNumCases()) &&((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3236, __PRETTY_FUNCTION__)) | |||
3236 | "Index out the number of cases.")((((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3236, __PRETTY_FUNCTION__)); | |||
3237 | return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0; | |||
3238 | } | |||
3239 | ||||
3240 | bool operator==(const CaseHandleImpl &RHS) const { | |||
3241 | assert(SI == RHS.SI && "Incompatible operators.")((SI == RHS.SI && "Incompatible operators.") ? static_cast <void> (0) : __assert_fail ("SI == RHS.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3241, __PRETTY_FUNCTION__)); | |||
3242 | return Index == RHS.Index; | |||
3243 | } | |||
3244 | }; | |||
3245 | ||||
3246 | using ConstCaseHandle = | |||
3247 | CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>; | |||
3248 | ||||
3249 | class CaseHandle | |||
3250 | : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> { | |||
3251 | friend class SwitchInst::CaseIteratorImpl<CaseHandle>; | |||
3252 | ||||
3253 | public: | |||
3254 | CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {} | |||
3255 | ||||
3256 | /// Sets the new value for current case. | |||
3257 | void setValue(ConstantInt *V) { | |||
3258 | assert((unsigned)Index < SI->getNumCases() &&(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3259, __PRETTY_FUNCTION__)) | |||
3259 | "Index out the number of cases.")(((unsigned)Index < SI->getNumCases() && "Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3259, __PRETTY_FUNCTION__)); | |||
3260 | SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V)); | |||
3261 | } | |||
3262 | ||||
3263 | /// Sets the new successor for current case. | |||
3264 | void setSuccessor(BasicBlock *S) { | |||
3265 | SI->setSuccessor(getSuccessorIndex(), S); | |||
3266 | } | |||
3267 | }; | |||
3268 | ||||
3269 | template <typename CaseHandleT> | |||
3270 | class CaseIteratorImpl | |||
3271 | : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>, | |||
3272 | std::random_access_iterator_tag, | |||
3273 | CaseHandleT> { | |||
3274 | using SwitchInstT = typename CaseHandleT::SwitchInstType; | |||
3275 | ||||
3276 | CaseHandleT Case; | |||
3277 | ||||
3278 | public: | |||
3279 | /// Default constructed iterator is in an invalid state until assigned to | |||
3280 | /// a case for a particular switch. | |||
3281 | CaseIteratorImpl() = default; | |||
3282 | ||||
3283 | /// Initializes case iterator for given SwitchInst and for given | |||
3284 | /// case number. | |||
3285 | CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {} | |||
3286 | ||||
3287 | /// Initializes case iterator for given SwitchInst and for given | |||
3288 | /// successor index. | |||
3289 | static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI, | |||
3290 | unsigned SuccessorIndex) { | |||
3291 | assert(SuccessorIndex < SI->getNumSuccessors() &&((SuccessorIndex < SI->getNumSuccessors() && "Successor index # out of range!" ) ? static_cast<void> (0) : __assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3292, __PRETTY_FUNCTION__)) | |||
3292 | "Successor index # out of range!")((SuccessorIndex < SI->getNumSuccessors() && "Successor index # out of range!" ) ? static_cast<void> (0) : __assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3292, __PRETTY_FUNCTION__)); | |||
3293 | return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1) | |||
3294 | : CaseIteratorImpl(SI, DefaultPseudoIndex); | |||
3295 | } | |||
3296 | ||||
3297 | /// Support converting to the const variant. This will be a no-op for const | |||
3298 | /// variant. | |||
3299 | operator CaseIteratorImpl<ConstCaseHandle>() const { | |||
3300 | return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index); | |||
3301 | } | |||
3302 | ||||
3303 | CaseIteratorImpl &operator+=(ptrdiff_t N) { | |||
3304 | // Check index correctness after addition. | |||
3305 | // Note: Index == getNumCases() means end(). | |||
3306 | assert(Case.Index + N >= 0 &&((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3308, __PRETTY_FUNCTION__)) | |||
3307 | (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3308, __PRETTY_FUNCTION__)) | |||
3308 | "Case.Index out the number of cases.")((Case.Index + N >= 0 && (unsigned)(Case.Index + N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3308, __PRETTY_FUNCTION__)); | |||
3309 | Case.Index += N; | |||
3310 | return *this; | |||
3311 | } | |||
3312 | CaseIteratorImpl &operator-=(ptrdiff_t N) { | |||
3313 | // Check index correctness after subtraction. | |||
3314 | // Note: Case.Index == getNumCases() means end(). | |||
3315 | assert(Case.Index - N >= 0 &&((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3317, __PRETTY_FUNCTION__)) | |||
3316 | (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3317, __PRETTY_FUNCTION__)) | |||
3317 | "Case.Index out the number of cases.")((Case.Index - N >= 0 && (unsigned)(Case.Index - N ) <= Case.SI->getNumCases() && "Case.Index out the number of cases." ) ? static_cast<void> (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3317, __PRETTY_FUNCTION__)); | |||
3318 | Case.Index -= N; | |||
3319 | return *this; | |||
3320 | } | |||
3321 | ptrdiff_t operator-(const CaseIteratorImpl &RHS) const { | |||
3322 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((Case.SI == RHS.Case.SI && "Incompatible operators." ) ? static_cast<void> (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3322, __PRETTY_FUNCTION__)); | |||
3323 | return Case.Index - RHS.Case.Index; | |||
3324 | } | |||
3325 | bool operator==(const CaseIteratorImpl &RHS) const { | |||
3326 | return Case == RHS.Case; | |||
3327 | } | |||
3328 | bool operator<(const CaseIteratorImpl &RHS) const { | |||
3329 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")((Case.SI == RHS.Case.SI && "Incompatible operators." ) ? static_cast<void> (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3329, __PRETTY_FUNCTION__)); | |||
3330 | return Case.Index < RHS.Case.Index; | |||
3331 | } | |||
3332 | CaseHandleT &operator*() { return Case; } | |||
3333 | const CaseHandleT &operator*() const { return Case; } | |||
3334 | }; | |||
3335 | ||||
3336 | using CaseIt = CaseIteratorImpl<CaseHandle>; | |||
3337 | using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>; | |||
3338 | ||||
3339 | static SwitchInst *Create(Value *Value, BasicBlock *Default, | |||
3340 | unsigned NumCases, | |||
3341 | Instruction *InsertBefore = nullptr) { | |||
3342 | return new SwitchInst(Value, Default, NumCases, InsertBefore); | |||
3343 | } | |||
3344 | ||||
3345 | static SwitchInst *Create(Value *Value, BasicBlock *Default, | |||
3346 | unsigned NumCases, BasicBlock *InsertAtEnd) { | |||
3347 | return new SwitchInst(Value, Default, NumCases, InsertAtEnd); | |||
3348 | } | |||
3349 | ||||
3350 | /// Provide fast operand accessors | |||
3351 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
3352 | ||||
3353 | // Accessor Methods for Switch stmt | |||
3354 | Value *getCondition() const { return getOperand(0); } | |||
3355 | void setCondition(Value *V) { setOperand(0, V); } | |||
3356 | ||||
3357 | BasicBlock *getDefaultDest() const { | |||
3358 | return cast<BasicBlock>(getOperand(1)); | |||
3359 | } | |||
3360 | ||||
3361 | void setDefaultDest(BasicBlock *DefaultCase) { | |||
3362 | setOperand(1, reinterpret_cast<Value*>(DefaultCase)); | |||
3363 | } | |||
3364 | ||||
3365 | /// Return the number of 'cases' in this switch instruction, excluding the | |||
3366 | /// default case. | |||
3367 | unsigned getNumCases() const { | |||
3368 | return getNumOperands()/2 - 1; | |||
3369 | } | |||
3370 | ||||
3371 | /// Returns a read/write iterator that points to the first case in the | |||
3372 | /// SwitchInst. | |||
3373 | CaseIt case_begin() { | |||
3374 | return CaseIt(this, 0); | |||
3375 | } | |||
3376 | ||||
3377 | /// Returns a read-only iterator that points to the first case in the | |||
3378 | /// SwitchInst. | |||
3379 | ConstCaseIt case_begin() const { | |||
3380 | return ConstCaseIt(this, 0); | |||
3381 | } | |||
3382 | ||||
3383 | /// Returns a read/write iterator that points one past the last in the | |||
3384 | /// SwitchInst. | |||
3385 | CaseIt case_end() { | |||
3386 | return CaseIt(this, getNumCases()); | |||
3387 | } | |||
3388 | ||||
3389 | /// Returns a read-only iterator that points one past the last in the | |||
3390 | /// SwitchInst. | |||
3391 | ConstCaseIt case_end() const { | |||
3392 | return ConstCaseIt(this, getNumCases()); | |||
3393 | } | |||
3394 | ||||
3395 | /// Iteration adapter for range-for loops. | |||
3396 | iterator_range<CaseIt> cases() { | |||
3397 | return make_range(case_begin(), case_end()); | |||
3398 | } | |||
3399 | ||||
3400 | /// Constant iteration adapter for range-for loops. | |||
3401 | iterator_range<ConstCaseIt> cases() const { | |||
3402 | return make_range(case_begin(), case_end()); | |||
3403 | } | |||
3404 | ||||
3405 | /// Returns an iterator that points to the default case. | |||
3406 | /// Note: this iterator allows to resolve successor only. Attempt | |||
3407 | /// to resolve case value causes an assertion. | |||
3408 | /// Also note, that increment and decrement also causes an assertion and | |||
3409 | /// makes iterator invalid. | |||
3410 | CaseIt case_default() { | |||
3411 | return CaseIt(this, DefaultPseudoIndex); | |||
3412 | } | |||
3413 | ConstCaseIt case_default() const { | |||
3414 | return ConstCaseIt(this, DefaultPseudoIndex); | |||
3415 | } | |||
3416 | ||||
3417 | /// Search all of the case values for the specified constant. If it is | |||
3418 | /// explicitly handled, return the case iterator of it, otherwise return | |||
3419 | /// default case iterator to indicate that it is handled by the default | |||
3420 | /// handler. | |||
3421 | CaseIt findCaseValue(const ConstantInt *C) { | |||
3422 | CaseIt I = llvm::find_if( | |||
3423 | cases(), [C](CaseHandle &Case) { return Case.getCaseValue() == C; }); | |||
3424 | if (I != case_end()) | |||
3425 | return I; | |||
3426 | ||||
3427 | return case_default(); | |||
3428 | } | |||
3429 | ConstCaseIt findCaseValue(const ConstantInt *C) const { | |||
3430 | ConstCaseIt I = llvm::find_if(cases(), [C](ConstCaseHandle &Case) { | |||
3431 | return Case.getCaseValue() == C; | |||
3432 | }); | |||
3433 | if (I != case_end()) | |||
3434 | return I; | |||
3435 | ||||
3436 | return case_default(); | |||
3437 | } | |||
3438 | ||||
3439 | /// Finds the unique case value for a given successor. Returns null if the | |||
3440 | /// successor is not found, not unique, or is the default case. | |||
3441 | ConstantInt *findCaseDest(BasicBlock *BB) { | |||
3442 | if (BB == getDefaultDest()) | |||
3443 | return nullptr; | |||
3444 | ||||
3445 | ConstantInt *CI = nullptr; | |||
3446 | for (auto Case : cases()) { | |||
3447 | if (Case.getCaseSuccessor() != BB) | |||
3448 | continue; | |||
3449 | ||||
3450 | if (CI) | |||
3451 | return nullptr; // Multiple cases lead to BB. | |||
3452 | ||||
3453 | CI = Case.getCaseValue(); | |||
3454 | } | |||
3455 | ||||
3456 | return CI; | |||
3457 | } | |||
3458 | ||||
3459 | /// Add an entry to the switch instruction. | |||
3460 | /// Note: | |||
3461 | /// This action invalidates case_end(). Old case_end() iterator will | |||
3462 | /// point to the added case. | |||
3463 | void addCase(ConstantInt *OnVal, BasicBlock *Dest); | |||
3464 | ||||
3465 | /// This method removes the specified case and its successor from the switch | |||
3466 | /// instruction. Note that this operation may reorder the remaining cases at | |||
3467 | /// index idx and above. | |||
3468 | /// Note: | |||
3469 | /// This action invalidates iterators for all cases following the one removed, | |||
3470 | /// including the case_end() iterator. It returns an iterator for the next | |||
3471 | /// case. | |||
3472 | CaseIt removeCase(CaseIt I); | |||
3473 | ||||
3474 | unsigned getNumSuccessors() const { return getNumOperands()/2; } | |||
3475 | BasicBlock *getSuccessor(unsigned idx) const { | |||
3476 | assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!")((idx < getNumSuccessors() &&"Successor idx out of range for switch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() &&\"Successor idx out of range for switch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3476, __PRETTY_FUNCTION__)); | |||
3477 | return cast<BasicBlock>(getOperand(idx*2+1)); | |||
3478 | } | |||
3479 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { | |||
3480 | assert(idx < getNumSuccessors() && "Successor # out of range for switch!")((idx < getNumSuccessors() && "Successor # out of range for switch!" ) ? static_cast<void> (0) : __assert_fail ("idx < getNumSuccessors() && \"Successor # out of range for switch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3480, __PRETTY_FUNCTION__)); | |||
3481 | setOperand(idx * 2 + 1, NewSucc); | |||
3482 | } | |||
3483 | ||||
3484 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
3485 | static bool classof(const Instruction *I) { | |||
3486 | return I->getOpcode() == Instruction::Switch; | |||
3487 | } | |||
3488 | static bool classof(const Value *V) { | |||
3489 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
3490 | } | |||
3491 | }; | |||
3492 | ||||
3493 | /// A wrapper class to simplify modification of SwitchInst cases along with | |||
3494 | /// their prof branch_weights metadata. | |||
3495 | class SwitchInstProfUpdateWrapper { | |||
3496 | SwitchInst &SI; | |||
3497 | Optional<SmallVector<uint32_t, 8> > Weights = None; | |||
3498 | bool Changed = false; | |||
3499 | ||||
3500 | protected: | |||
3501 | static MDNode *getProfBranchWeightsMD(const SwitchInst &SI); | |||
3502 | ||||
3503 | MDNode *buildProfBranchWeightsMD(); | |||
3504 | ||||
3505 | void init(); | |||
3506 | ||||
3507 | public: | |||
3508 | using CaseWeightOpt = Optional<uint32_t>; | |||
3509 | SwitchInst *operator->() { return &SI; } | |||
3510 | SwitchInst &operator*() { return SI; } | |||
3511 | operator SwitchInst *() { return &SI; } | |||
3512 | ||||
3513 | SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); } | |||
3514 | ||||
3515 | ~SwitchInstProfUpdateWrapper() { | |||
3516 | if (Changed) | |||
3517 | SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD()); | |||
3518 | } | |||
3519 | ||||
3520 | /// Delegate the call to the underlying SwitchInst::removeCase() and remove | |||
3521 | /// correspondent branch weight. | |||
3522 | SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I); | |||
3523 | ||||
3524 | /// Delegate the call to the underlying SwitchInst::addCase() and set the | |||
3525 | /// specified branch weight for the added case. | |||
3526 | void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W); | |||
3527 | ||||
3528 | /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark | |||
3529 | /// this object to not touch the underlying SwitchInst in destructor. | |||
3530 | SymbolTableList<Instruction>::iterator eraseFromParent(); | |||
3531 | ||||
3532 | void setSuccessorWeight(unsigned idx, CaseWeightOpt W); | |||
3533 | CaseWeightOpt getSuccessorWeight(unsigned idx); | |||
3534 | ||||
3535 | static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx); | |||
3536 | }; | |||
3537 | ||||
3538 | template <> | |||
3539 | struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> { | |||
3540 | }; | |||
3541 | ||||
3542 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)SwitchInst::op_iterator SwitchInst::op_begin() { return OperandTraits <SwitchInst>::op_begin(this); } SwitchInst::const_op_iterator SwitchInst::op_begin() const { return OperandTraits<SwitchInst >::op_begin(const_cast<SwitchInst*>(this)); } SwitchInst ::op_iterator SwitchInst::op_end() { return OperandTraits< SwitchInst>::op_end(this); } SwitchInst::const_op_iterator SwitchInst::op_end() const { return OperandTraits<SwitchInst >::op_end(const_cast<SwitchInst*>(this)); } Value *SwitchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<SwitchInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3542, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<SwitchInst>::op_begin(const_cast<SwitchInst *>(this))[i_nocapture].get()); } void SwitchInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<SwitchInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3542, __PRETTY_FUNCTION__)); OperandTraits<SwitchInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned SwitchInst ::getNumOperands() const { return OperandTraits<SwitchInst >::operands(this); } template <int Idx_nocapture> Use &SwitchInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & SwitchInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
3543 | ||||
3544 | //===----------------------------------------------------------------------===// | |||
3545 | // IndirectBrInst Class | |||
3546 | //===----------------------------------------------------------------------===// | |||
3547 | ||||
3548 | //===--------------------------------------------------------------------------- | |||
3549 | /// Indirect Branch Instruction. | |||
3550 | /// | |||
3551 | class IndirectBrInst : public Instruction { | |||
3552 | unsigned ReservedSpace; | |||
3553 | ||||
3554 | // Operand[0] = Address to jump to | |||
3555 | // Operand[n+1] = n-th destination | |||
3556 | IndirectBrInst(const IndirectBrInst &IBI); | |||
3557 | ||||
3558 | /// Create a new indirectbr instruction, specifying an | |||
3559 | /// Address to jump to. The number of expected destinations can be specified | |||
3560 | /// here to make memory allocation more efficient. This constructor can also | |||
3561 | /// autoinsert before another instruction. | |||
3562 | IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore); | |||
3563 | ||||
3564 | /// Create a new indirectbr instruction, specifying an | |||
3565 | /// Address to jump to. The number of expected destinations can be specified | |||
3566 | /// here to make memory allocation more efficient. This constructor also | |||
3567 | /// autoinserts at the end of the specified BasicBlock. | |||
3568 | IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd); | |||
3569 | ||||
3570 | // allocate space for exactly zero operands | |||
3571 | void *operator new(size_t s) { | |||
3572 | return User::operator new(s); | |||
3573 | } | |||
3574 | ||||
3575 | void init(Value *Address, unsigned NumDests); | |||
3576 | void growOperands(); | |||
3577 | ||||
3578 | protected: | |||
3579 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3580 | friend class Instruction; | |||
3581 | ||||
3582 | IndirectBrInst *cloneImpl() const; | |||
3583 | ||||
3584 | public: | |||
3585 | /// Iterator type that casts an operand to a basic block. | |||
3586 | /// | |||
3587 | /// This only makes sense because the successors are stored as adjacent | |||
3588 | /// operands for indirectbr instructions. | |||
3589 | struct succ_op_iterator | |||
3590 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, | |||
3591 | std::random_access_iterator_tag, BasicBlock *, | |||
3592 | ptrdiff_t, BasicBlock *, BasicBlock *> { | |||
3593 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} | |||
3594 | ||||
3595 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } | |||
3596 | BasicBlock *operator->() const { return operator*(); } | |||
3597 | }; | |||
3598 | ||||
3599 | /// The const version of `succ_op_iterator`. | |||
3600 | struct const_succ_op_iterator | |||
3601 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, | |||
3602 | std::random_access_iterator_tag, | |||
3603 | const BasicBlock *, ptrdiff_t, const BasicBlock *, | |||
3604 | const BasicBlock *> { | |||
3605 | explicit const_succ_op_iterator(const_value_op_iterator I) | |||
3606 | : iterator_adaptor_base(I) {} | |||
3607 | ||||
3608 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } | |||
3609 | const BasicBlock *operator->() const { return operator*(); } | |||
3610 | }; | |||
3611 | ||||
3612 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, | |||
3613 | Instruction *InsertBefore = nullptr) { | |||
3614 | return new IndirectBrInst(Address, NumDests, InsertBefore); | |||
3615 | } | |||
3616 | ||||
3617 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, | |||
3618 | BasicBlock *InsertAtEnd) { | |||
3619 | return new IndirectBrInst(Address, NumDests, InsertAtEnd); | |||
3620 | } | |||
3621 | ||||
3622 | /// Provide fast operand accessors. | |||
3623 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
3624 | ||||
3625 | // Accessor Methods for IndirectBrInst instruction. | |||
3626 | Value *getAddress() { return getOperand(0); } | |||
3627 | const Value *getAddress() const { return getOperand(0); } | |||
3628 | void setAddress(Value *V) { setOperand(0, V); } | |||
3629 | ||||
3630 | /// return the number of possible destinations in this | |||
3631 | /// indirectbr instruction. | |||
3632 | unsigned getNumDestinations() const { return getNumOperands()-1; } | |||
3633 | ||||
3634 | /// Return the specified destination. | |||
3635 | BasicBlock *getDestination(unsigned i) { return getSuccessor(i); } | |||
3636 | const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); } | |||
3637 | ||||
3638 | /// Add a destination. | |||
3639 | /// | |||
3640 | void addDestination(BasicBlock *Dest); | |||
3641 | ||||
3642 | /// This method removes the specified successor from the | |||
3643 | /// indirectbr instruction. | |||
3644 | void removeDestination(unsigned i); | |||
3645 | ||||
3646 | unsigned getNumSuccessors() const { return getNumOperands()-1; } | |||
3647 | BasicBlock *getSuccessor(unsigned i) const { | |||
3648 | return cast<BasicBlock>(getOperand(i+1)); | |||
3649 | } | |||
3650 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { | |||
3651 | setOperand(i + 1, NewSucc); | |||
3652 | } | |||
3653 | ||||
3654 | iterator_range<succ_op_iterator> successors() { | |||
3655 | return make_range(succ_op_iterator(std::next(value_op_begin())), | |||
3656 | succ_op_iterator(value_op_end())); | |||
3657 | } | |||
3658 | ||||
3659 | iterator_range<const_succ_op_iterator> successors() const { | |||
3660 | return make_range(const_succ_op_iterator(std::next(value_op_begin())), | |||
3661 | const_succ_op_iterator(value_op_end())); | |||
3662 | } | |||
3663 | ||||
3664 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
3665 | static bool classof(const Instruction *I) { | |||
3666 | return I->getOpcode() == Instruction::IndirectBr; | |||
3667 | } | |||
3668 | static bool classof(const Value *V) { | |||
3669 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
3670 | } | |||
3671 | }; | |||
3672 | ||||
3673 | template <> | |||
3674 | struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> { | |||
3675 | }; | |||
3676 | ||||
3677 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)IndirectBrInst::op_iterator IndirectBrInst::op_begin() { return OperandTraits<IndirectBrInst>::op_begin(this); } IndirectBrInst ::const_op_iterator IndirectBrInst::op_begin() const { return OperandTraits<IndirectBrInst>::op_begin(const_cast< IndirectBrInst*>(this)); } IndirectBrInst::op_iterator IndirectBrInst ::op_end() { return OperandTraits<IndirectBrInst>::op_end (this); } IndirectBrInst::const_op_iterator IndirectBrInst::op_end () const { return OperandTraits<IndirectBrInst>::op_end (const_cast<IndirectBrInst*>(this)); } Value *IndirectBrInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3677, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<IndirectBrInst>::op_begin(const_cast< IndirectBrInst*>(this))[i_nocapture].get()); } void IndirectBrInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<IndirectBrInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3677, __PRETTY_FUNCTION__)); OperandTraits<IndirectBrInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned IndirectBrInst::getNumOperands() const { return OperandTraits <IndirectBrInst>::operands(this); } template <int Idx_nocapture > Use &IndirectBrInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &IndirectBrInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
3678 | ||||
3679 | //===----------------------------------------------------------------------===// | |||
3680 | // InvokeInst Class | |||
3681 | //===----------------------------------------------------------------------===// | |||
3682 | ||||
3683 | /// Invoke instruction. The SubclassData field is used to hold the | |||
3684 | /// calling convention of the call. | |||
3685 | /// | |||
3686 | class InvokeInst : public CallBase { | |||
3687 | /// The number of operands for this call beyond the called function, | |||
3688 | /// arguments, and operand bundles. | |||
3689 | static constexpr int NumExtraOperands = 2; | |||
3690 | ||||
3691 | /// The index from the end of the operand array to the normal destination. | |||
3692 | static constexpr int NormalDestOpEndIdx = -3; | |||
3693 | ||||
3694 | /// The index from the end of the operand array to the unwind destination. | |||
3695 | static constexpr int UnwindDestOpEndIdx = -2; | |||
3696 | ||||
3697 | InvokeInst(const InvokeInst &BI); | |||
3698 | ||||
3699 | /// Construct an InvokeInst given a range of arguments. | |||
3700 | /// | |||
3701 | /// Construct an InvokeInst from a range of arguments | |||
3702 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3703 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3704 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3705 | const Twine &NameStr, Instruction *InsertBefore); | |||
3706 | ||||
3707 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3708 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3709 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3710 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
3711 | ||||
3712 | void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3713 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3714 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); | |||
3715 | ||||
3716 | /// Compute the number of operands to allocate. | |||
3717 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { | |||
3718 | // We need one operand for the called function, plus our extra operands and | |||
3719 | // the input operand counts provided. | |||
3720 | return 1 + NumExtraOperands + NumArgs + NumBundleInputs; | |||
3721 | } | |||
3722 | ||||
3723 | protected: | |||
3724 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3725 | friend class Instruction; | |||
3726 | ||||
3727 | InvokeInst *cloneImpl() const; | |||
3728 | ||||
3729 | public: | |||
3730 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3731 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3732 | const Twine &NameStr, | |||
3733 | Instruction *InsertBefore = nullptr) { | |||
3734 | int NumOperands = ComputeNumOperands(Args.size()); | |||
3735 | return new (NumOperands) | |||
3736 | InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands, | |||
3737 | NameStr, InsertBefore); | |||
3738 | } | |||
3739 | ||||
3740 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3741 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3742 | ArrayRef<OperandBundleDef> Bundles = None, | |||
3743 | const Twine &NameStr = "", | |||
3744 | Instruction *InsertBefore = nullptr) { | |||
3745 | int NumOperands = | |||
3746 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); | |||
3747 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
3748 | ||||
3749 | return new (NumOperands, DescriptorBytes) | |||
3750 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, | |||
3751 | NameStr, InsertBefore); | |||
3752 | } | |||
3753 | ||||
3754 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3755 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3756 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3757 | int NumOperands = ComputeNumOperands(Args.size()); | |||
3758 | return new (NumOperands) | |||
3759 | InvokeInst(Ty, Func, IfNormal, IfException, Args, None, NumOperands, | |||
3760 | NameStr, InsertAtEnd); | |||
3761 | } | |||
3762 | ||||
3763 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3764 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3765 | ArrayRef<OperandBundleDef> Bundles, | |||
3766 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3767 | int NumOperands = | |||
3768 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); | |||
3769 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
3770 | ||||
3771 | return new (NumOperands, DescriptorBytes) | |||
3772 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, | |||
3773 | NameStr, InsertAtEnd); | |||
3774 | } | |||
3775 | ||||
3776 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, | |||
3777 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3778 | const Twine &NameStr, | |||
3779 | Instruction *InsertBefore = nullptr) { | |||
3780 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, | |||
3781 | IfException, Args, None, NameStr, InsertBefore); | |||
3782 | } | |||
3783 | ||||
3784 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, | |||
3785 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3786 | ArrayRef<OperandBundleDef> Bundles = None, | |||
3787 | const Twine &NameStr = "", | |||
3788 | Instruction *InsertBefore = nullptr) { | |||
3789 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, | |||
3790 | IfException, Args, Bundles, NameStr, InsertBefore); | |||
3791 | } | |||
3792 | ||||
3793 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, | |||
3794 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3795 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3796 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, | |||
3797 | IfException, Args, NameStr, InsertAtEnd); | |||
3798 | } | |||
3799 | ||||
3800 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, | |||
3801 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3802 | ArrayRef<OperandBundleDef> Bundles, | |||
3803 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3804 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, | |||
3805 | IfException, Args, Bundles, NameStr, InsertAtEnd); | |||
3806 | } | |||
3807 | ||||
3808 | /// Create a clone of \p II with a different set of operand bundles and | |||
3809 | /// insert it before \p InsertPt. | |||
3810 | /// | |||
3811 | /// The returned invoke instruction is identical to \p II in every way except | |||
3812 | /// that the operand bundles for the new instruction are set to the operand | |||
3813 | /// bundles in \p Bundles. | |||
3814 | static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles, | |||
3815 | Instruction *InsertPt = nullptr); | |||
3816 | ||||
3817 | /// Create a clone of \p II with a different set of operand bundles and | |||
3818 | /// insert it before \p InsertPt. | |||
3819 | /// | |||
3820 | /// The returned invoke instruction is identical to \p II in every way except | |||
3821 | /// that the operand bundle for the new instruction is set to the operand | |||
3822 | /// bundle in \p Bundle. | |||
3823 | static InvokeInst *CreateWithReplacedBundle(InvokeInst *II, | |||
3824 | OperandBundleDef Bundles, | |||
3825 | Instruction *InsertPt = nullptr); | |||
3826 | ||||
3827 | // get*Dest - Return the destination basic blocks... | |||
3828 | BasicBlock *getNormalDest() const { | |||
3829 | return cast<BasicBlock>(Op<NormalDestOpEndIdx>()); | |||
3830 | } | |||
3831 | BasicBlock *getUnwindDest() const { | |||
3832 | return cast<BasicBlock>(Op<UnwindDestOpEndIdx>()); | |||
3833 | } | |||
3834 | void setNormalDest(BasicBlock *B) { | |||
3835 | Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B); | |||
3836 | } | |||
3837 | void setUnwindDest(BasicBlock *B) { | |||
3838 | Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B); | |||
3839 | } | |||
3840 | ||||
3841 | /// Get the landingpad instruction from the landing pad | |||
3842 | /// block (the unwind destination). | |||
3843 | LandingPadInst *getLandingPadInst() const; | |||
3844 | ||||
3845 | BasicBlock *getSuccessor(unsigned i) const { | |||
3846 | assert(i < 2 && "Successor # out of range for invoke!")((i < 2 && "Successor # out of range for invoke!") ? static_cast<void> (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3846, __PRETTY_FUNCTION__)); | |||
3847 | return i == 0 ? getNormalDest() : getUnwindDest(); | |||
3848 | } | |||
3849 | ||||
3850 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { | |||
3851 | assert(i < 2 && "Successor # out of range for invoke!")((i < 2 && "Successor # out of range for invoke!") ? static_cast<void> (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 3851, __PRETTY_FUNCTION__)); | |||
3852 | if (i == 0) | |||
3853 | setNormalDest(NewSucc); | |||
3854 | else | |||
3855 | setUnwindDest(NewSucc); | |||
3856 | } | |||
3857 | ||||
3858 | unsigned getNumSuccessors() const { return 2; } | |||
3859 | ||||
3860 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
3861 | static bool classof(const Instruction *I) { | |||
3862 | return (I->getOpcode() == Instruction::Invoke); | |||
3863 | } | |||
3864 | static bool classof(const Value *V) { | |||
3865 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
3866 | } | |||
3867 | ||||
3868 | private: | |||
3869 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
3870 | // method so that subclasses cannot accidentally use it. | |||
3871 | template <typename Bitfield> | |||
3872 | void setSubclassData(typename Bitfield::Type Value) { | |||
3873 | Instruction::setSubclassData<Bitfield>(Value); | |||
3874 | } | |||
3875 | }; | |||
3876 | ||||
3877 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3878 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3879 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3880 | const Twine &NameStr, Instruction *InsertBefore) | |||
3881 | : CallBase(Ty->getReturnType(), Instruction::Invoke, | |||
3882 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, | |||
3883 | InsertBefore) { | |||
3884 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); | |||
3885 | } | |||
3886 | ||||
3887 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, | |||
3888 | BasicBlock *IfException, ArrayRef<Value *> Args, | |||
3889 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3890 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
3891 | : CallBase(Ty->getReturnType(), Instruction::Invoke, | |||
3892 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, | |||
3893 | InsertAtEnd) { | |||
3894 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); | |||
3895 | } | |||
3896 | ||||
3897 | //===----------------------------------------------------------------------===// | |||
3898 | // CallBrInst Class | |||
3899 | //===----------------------------------------------------------------------===// | |||
3900 | ||||
3901 | /// CallBr instruction, tracking function calls that may not return control but | |||
3902 | /// instead transfer it to a third location. The SubclassData field is used to | |||
3903 | /// hold the calling convention of the call. | |||
3904 | /// | |||
3905 | class CallBrInst : public CallBase { | |||
3906 | ||||
3907 | unsigned NumIndirectDests; | |||
3908 | ||||
3909 | CallBrInst(const CallBrInst &BI); | |||
3910 | ||||
3911 | /// Construct a CallBrInst given a range of arguments. | |||
3912 | /// | |||
3913 | /// Construct a CallBrInst from a range of arguments | |||
3914 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, | |||
3915 | ArrayRef<BasicBlock *> IndirectDests, | |||
3916 | ArrayRef<Value *> Args, | |||
3917 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3918 | const Twine &NameStr, Instruction *InsertBefore); | |||
3919 | ||||
3920 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, | |||
3921 | ArrayRef<BasicBlock *> IndirectDests, | |||
3922 | ArrayRef<Value *> Args, | |||
3923 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
3924 | const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
3925 | ||||
3926 | void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, | |||
3927 | ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args, | |||
3928 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); | |||
3929 | ||||
3930 | /// Should the Indirect Destinations change, scan + update the Arg list. | |||
3931 | void updateArgBlockAddresses(unsigned i, BasicBlock *B); | |||
3932 | ||||
3933 | /// Compute the number of operands to allocate. | |||
3934 | static int ComputeNumOperands(int NumArgs, int NumIndirectDests, | |||
3935 | int NumBundleInputs = 0) { | |||
3936 | // We need one operand for the called function, plus our extra operands and | |||
3937 | // the input operand counts provided. | |||
3938 | return 2 + NumIndirectDests + NumArgs + NumBundleInputs; | |||
3939 | } | |||
3940 | ||||
3941 | protected: | |||
3942 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
3943 | friend class Instruction; | |||
3944 | ||||
3945 | CallBrInst *cloneImpl() const; | |||
3946 | ||||
3947 | public: | |||
3948 | static CallBrInst *Create(FunctionType *Ty, Value *Func, | |||
3949 | BasicBlock *DefaultDest, | |||
3950 | ArrayRef<BasicBlock *> IndirectDests, | |||
3951 | ArrayRef<Value *> Args, const Twine &NameStr, | |||
3952 | Instruction *InsertBefore = nullptr) { | |||
3953 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); | |||
3954 | return new (NumOperands) | |||
3955 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None, | |||
3956 | NumOperands, NameStr, InsertBefore); | |||
3957 | } | |||
3958 | ||||
3959 | static CallBrInst *Create(FunctionType *Ty, Value *Func, | |||
3960 | BasicBlock *DefaultDest, | |||
3961 | ArrayRef<BasicBlock *> IndirectDests, | |||
3962 | ArrayRef<Value *> Args, | |||
3963 | ArrayRef<OperandBundleDef> Bundles = None, | |||
3964 | const Twine &NameStr = "", | |||
3965 | Instruction *InsertBefore = nullptr) { | |||
3966 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), | |||
3967 | CountBundleInputs(Bundles)); | |||
3968 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
3969 | ||||
3970 | return new (NumOperands, DescriptorBytes) | |||
3971 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, | |||
3972 | NumOperands, NameStr, InsertBefore); | |||
3973 | } | |||
3974 | ||||
3975 | static CallBrInst *Create(FunctionType *Ty, Value *Func, | |||
3976 | BasicBlock *DefaultDest, | |||
3977 | ArrayRef<BasicBlock *> IndirectDests, | |||
3978 | ArrayRef<Value *> Args, const Twine &NameStr, | |||
3979 | BasicBlock *InsertAtEnd) { | |||
3980 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); | |||
3981 | return new (NumOperands) | |||
3982 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, None, | |||
3983 | NumOperands, NameStr, InsertAtEnd); | |||
3984 | } | |||
3985 | ||||
3986 | static CallBrInst *Create(FunctionType *Ty, Value *Func, | |||
3987 | BasicBlock *DefaultDest, | |||
3988 | ArrayRef<BasicBlock *> IndirectDests, | |||
3989 | ArrayRef<Value *> Args, | |||
3990 | ArrayRef<OperandBundleDef> Bundles, | |||
3991 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
3992 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), | |||
3993 | CountBundleInputs(Bundles)); | |||
3994 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); | |||
3995 | ||||
3996 | return new (NumOperands, DescriptorBytes) | |||
3997 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, | |||
3998 | NumOperands, NameStr, InsertAtEnd); | |||
3999 | } | |||
4000 | ||||
4001 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, | |||
4002 | ArrayRef<BasicBlock *> IndirectDests, | |||
4003 | ArrayRef<Value *> Args, const Twine &NameStr, | |||
4004 | Instruction *InsertBefore = nullptr) { | |||
4005 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, | |||
4006 | IndirectDests, Args, NameStr, InsertBefore); | |||
4007 | } | |||
4008 | ||||
4009 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, | |||
4010 | ArrayRef<BasicBlock *> IndirectDests, | |||
4011 | ArrayRef<Value *> Args, | |||
4012 | ArrayRef<OperandBundleDef> Bundles = None, | |||
4013 | const Twine &NameStr = "", | |||
4014 | Instruction *InsertBefore = nullptr) { | |||
4015 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, | |||
4016 | IndirectDests, Args, Bundles, NameStr, InsertBefore); | |||
4017 | } | |||
4018 | ||||
4019 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, | |||
4020 | ArrayRef<BasicBlock *> IndirectDests, | |||
4021 | ArrayRef<Value *> Args, const Twine &NameStr, | |||
4022 | BasicBlock *InsertAtEnd) { | |||
4023 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, | |||
4024 | IndirectDests, Args, NameStr, InsertAtEnd); | |||
4025 | } | |||
4026 | ||||
4027 | static CallBrInst *Create(FunctionCallee Func, | |||
4028 | BasicBlock *DefaultDest, | |||
4029 | ArrayRef<BasicBlock *> IndirectDests, | |||
4030 | ArrayRef<Value *> Args, | |||
4031 | ArrayRef<OperandBundleDef> Bundles, | |||
4032 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
4033 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, | |||
4034 | IndirectDests, Args, Bundles, NameStr, InsertAtEnd); | |||
4035 | } | |||
4036 | ||||
4037 | /// Create a clone of \p CBI with a different set of operand bundles and | |||
4038 | /// insert it before \p InsertPt. | |||
4039 | /// | |||
4040 | /// The returned callbr instruction is identical to \p CBI in every way | |||
4041 | /// except that the operand bundles for the new instruction are set to the | |||
4042 | /// operand bundles in \p Bundles. | |||
4043 | static CallBrInst *Create(CallBrInst *CBI, | |||
4044 | ArrayRef<OperandBundleDef> Bundles, | |||
4045 | Instruction *InsertPt = nullptr); | |||
4046 | ||||
4047 | /// Return the number of callbr indirect dest labels. | |||
4048 | /// | |||
4049 | unsigned getNumIndirectDests() const { return NumIndirectDests; } | |||
4050 | ||||
4051 | /// getIndirectDestLabel - Return the i-th indirect dest label. | |||
4052 | /// | |||
4053 | Value *getIndirectDestLabel(unsigned i) const { | |||
4054 | assert(i < getNumIndirectDests() && "Out of bounds!")((i < getNumIndirectDests() && "Out of bounds!") ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4054, __PRETTY_FUNCTION__)); | |||
4055 | return getOperand(i + getNumArgOperands() + getNumTotalBundleOperands() + | |||
4056 | 1); | |||
4057 | } | |||
4058 | ||||
4059 | Value *getIndirectDestLabelUse(unsigned i) const { | |||
4060 | assert(i < getNumIndirectDests() && "Out of bounds!")((i < getNumIndirectDests() && "Out of bounds!") ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() && \"Out of bounds!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4060, __PRETTY_FUNCTION__)); | |||
4061 | return getOperandUse(i + getNumArgOperands() + getNumTotalBundleOperands() + | |||
4062 | 1); | |||
4063 | } | |||
4064 | ||||
4065 | // Return the destination basic blocks... | |||
4066 | BasicBlock *getDefaultDest() const { | |||
4067 | return cast<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() - 1)); | |||
4068 | } | |||
4069 | BasicBlock *getIndirectDest(unsigned i) const { | |||
4070 | return cast_or_null<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() + i)); | |||
4071 | } | |||
4072 | SmallVector<BasicBlock *, 16> getIndirectDests() const { | |||
4073 | SmallVector<BasicBlock *, 16> IndirectDests; | |||
4074 | for (unsigned i = 0, e = getNumIndirectDests(); i < e; ++i) | |||
4075 | IndirectDests.push_back(getIndirectDest(i)); | |||
4076 | return IndirectDests; | |||
4077 | } | |||
4078 | void setDefaultDest(BasicBlock *B) { | |||
4079 | *(&Op<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value *>(B); | |||
4080 | } | |||
4081 | void setIndirectDest(unsigned i, BasicBlock *B) { | |||
4082 | updateArgBlockAddresses(i, B); | |||
4083 | *(&Op<-1>() - getNumIndirectDests() + i) = reinterpret_cast<Value *>(B); | |||
4084 | } | |||
4085 | ||||
4086 | BasicBlock *getSuccessor(unsigned i) const { | |||
4087 | assert(i < getNumSuccessors() + 1 &&((i < getNumSuccessors() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4088, __PRETTY_FUNCTION__)) | |||
4088 | "Successor # out of range for callbr!")((i < getNumSuccessors() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumSuccessors() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4088, __PRETTY_FUNCTION__)); | |||
4089 | return i == 0 ? getDefaultDest() : getIndirectDest(i - 1); | |||
4090 | } | |||
4091 | ||||
4092 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { | |||
4093 | assert(i < getNumIndirectDests() + 1 &&((i < getNumIndirectDests() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4094, __PRETTY_FUNCTION__)) | |||
4094 | "Successor # out of range for callbr!")((i < getNumIndirectDests() + 1 && "Successor # out of range for callbr!" ) ? static_cast<void> (0) : __assert_fail ("i < getNumIndirectDests() + 1 && \"Successor # out of range for callbr!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4094, __PRETTY_FUNCTION__)); | |||
4095 | return i == 0 ? setDefaultDest(NewSucc) : setIndirectDest(i - 1, NewSucc); | |||
4096 | } | |||
4097 | ||||
4098 | unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; } | |||
4099 | ||||
4100 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4101 | static bool classof(const Instruction *I) { | |||
4102 | return (I->getOpcode() == Instruction::CallBr); | |||
4103 | } | |||
4104 | static bool classof(const Value *V) { | |||
4105 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4106 | } | |||
4107 | ||||
4108 | private: | |||
4109 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
4110 | // method so that subclasses cannot accidentally use it. | |||
4111 | template <typename Bitfield> | |||
4112 | void setSubclassData(typename Bitfield::Type Value) { | |||
4113 | Instruction::setSubclassData<Bitfield>(Value); | |||
4114 | } | |||
4115 | }; | |||
4116 | ||||
4117 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, | |||
4118 | ArrayRef<BasicBlock *> IndirectDests, | |||
4119 | ArrayRef<Value *> Args, | |||
4120 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
4121 | const Twine &NameStr, Instruction *InsertBefore) | |||
4122 | : CallBase(Ty->getReturnType(), Instruction::CallBr, | |||
4123 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, | |||
4124 | InsertBefore) { | |||
4125 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); | |||
4126 | } | |||
4127 | ||||
4128 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, | |||
4129 | ArrayRef<BasicBlock *> IndirectDests, | |||
4130 | ArrayRef<Value *> Args, | |||
4131 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, | |||
4132 | const Twine &NameStr, BasicBlock *InsertAtEnd) | |||
4133 | : CallBase(Ty->getReturnType(), Instruction::CallBr, | |||
4134 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, | |||
4135 | InsertAtEnd) { | |||
4136 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); | |||
4137 | } | |||
4138 | ||||
4139 | //===----------------------------------------------------------------------===// | |||
4140 | // ResumeInst Class | |||
4141 | //===----------------------------------------------------------------------===// | |||
4142 | ||||
4143 | //===--------------------------------------------------------------------------- | |||
4144 | /// Resume the propagation of an exception. | |||
4145 | /// | |||
4146 | class ResumeInst : public Instruction { | |||
4147 | ResumeInst(const ResumeInst &RI); | |||
4148 | ||||
4149 | explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr); | |||
4150 | ResumeInst(Value *Exn, BasicBlock *InsertAtEnd); | |||
4151 | ||||
4152 | protected: | |||
4153 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4154 | friend class Instruction; | |||
4155 | ||||
4156 | ResumeInst *cloneImpl() const; | |||
4157 | ||||
4158 | public: | |||
4159 | static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) { | |||
4160 | return new(1) ResumeInst(Exn, InsertBefore); | |||
4161 | } | |||
4162 | ||||
4163 | static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) { | |||
4164 | return new(1) ResumeInst(Exn, InsertAtEnd); | |||
4165 | } | |||
4166 | ||||
4167 | /// Provide fast operand accessors | |||
4168 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
4169 | ||||
4170 | /// Convenience accessor. | |||
4171 | Value *getValue() const { return Op<0>(); } | |||
4172 | ||||
4173 | unsigned getNumSuccessors() const { return 0; } | |||
4174 | ||||
4175 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4176 | static bool classof(const Instruction *I) { | |||
4177 | return I->getOpcode() == Instruction::Resume; | |||
4178 | } | |||
4179 | static bool classof(const Value *V) { | |||
4180 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4181 | } | |||
4182 | ||||
4183 | private: | |||
4184 | BasicBlock *getSuccessor(unsigned idx) const { | |||
4185 | llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4185); | |||
4186 | } | |||
4187 | ||||
4188 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { | |||
4189 | llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4189); | |||
4190 | } | |||
4191 | }; | |||
4192 | ||||
4193 | template <> | |||
4194 | struct OperandTraits<ResumeInst> : | |||
4195 | public FixedNumOperandTraits<ResumeInst, 1> { | |||
4196 | }; | |||
4197 | ||||
4198 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)ResumeInst::op_iterator ResumeInst::op_begin() { return OperandTraits <ResumeInst>::op_begin(this); } ResumeInst::const_op_iterator ResumeInst::op_begin() const { return OperandTraits<ResumeInst >::op_begin(const_cast<ResumeInst*>(this)); } ResumeInst ::op_iterator ResumeInst::op_end() { return OperandTraits< ResumeInst>::op_end(this); } ResumeInst::const_op_iterator ResumeInst::op_end() const { return OperandTraits<ResumeInst >::op_end(const_cast<ResumeInst*>(this)); } Value *ResumeInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<ResumeInst>::operands(this) && "getOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ResumeInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4198, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<ResumeInst>::op_begin(const_cast<ResumeInst *>(this))[i_nocapture].get()); } void ResumeInst::setOperand (unsigned i_nocapture, Value *Val_nocapture) { ((i_nocapture < OperandTraits<ResumeInst>::operands(this) && "setOperand() out of range!" ) ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<ResumeInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4198, __PRETTY_FUNCTION__)); OperandTraits<ResumeInst> ::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned ResumeInst ::getNumOperands() const { return OperandTraits<ResumeInst >::operands(this); } template <int Idx_nocapture> Use &ResumeInst::Op() { return this->OpFrom<Idx_nocapture >(this); } template <int Idx_nocapture> const Use & ResumeInst::Op() const { return this->OpFrom<Idx_nocapture >(this); } | |||
4199 | ||||
4200 | //===----------------------------------------------------------------------===// | |||
4201 | // CatchSwitchInst Class | |||
4202 | //===----------------------------------------------------------------------===// | |||
4203 | class CatchSwitchInst : public Instruction { | |||
4204 | using UnwindDestField = BoolBitfieldElementT<0>; | |||
4205 | ||||
4206 | /// The number of operands actually allocated. NumOperands is | |||
4207 | /// the number actually in use. | |||
4208 | unsigned ReservedSpace; | |||
4209 | ||||
4210 | // Operand[0] = Outer scope | |||
4211 | // Operand[1] = Unwind block destination | |||
4212 | // Operand[n] = BasicBlock to go to on match | |||
4213 | CatchSwitchInst(const CatchSwitchInst &CSI); | |||
4214 | ||||
4215 | /// Create a new switch instruction, specifying a | |||
4216 | /// default destination. The number of additional handlers can be specified | |||
4217 | /// here to make memory allocation more efficient. | |||
4218 | /// This constructor can also autoinsert before another instruction. | |||
4219 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, | |||
4220 | unsigned NumHandlers, const Twine &NameStr, | |||
4221 | Instruction *InsertBefore); | |||
4222 | ||||
4223 | /// Create a new switch instruction, specifying a | |||
4224 | /// default destination. The number of additional handlers can be specified | |||
4225 | /// here to make memory allocation more efficient. | |||
4226 | /// This constructor also autoinserts at the end of the specified BasicBlock. | |||
4227 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, | |||
4228 | unsigned NumHandlers, const Twine &NameStr, | |||
4229 | BasicBlock *InsertAtEnd); | |||
4230 | ||||
4231 | // allocate space for exactly zero operands | |||
4232 | void *operator new(size_t s) { return User::operator new(s); } | |||
4233 | ||||
4234 | void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved); | |||
4235 | void growOperands(unsigned Size); | |||
4236 | ||||
4237 | protected: | |||
4238 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4239 | friend class Instruction; | |||
4240 | ||||
4241 | CatchSwitchInst *cloneImpl() const; | |||
4242 | ||||
4243 | public: | |||
4244 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, | |||
4245 | unsigned NumHandlers, | |||
4246 | const Twine &NameStr = "", | |||
4247 | Instruction *InsertBefore = nullptr) { | |||
4248 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, | |||
4249 | InsertBefore); | |||
4250 | } | |||
4251 | ||||
4252 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, | |||
4253 | unsigned NumHandlers, const Twine &NameStr, | |||
4254 | BasicBlock *InsertAtEnd) { | |||
4255 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, | |||
4256 | InsertAtEnd); | |||
4257 | } | |||
4258 | ||||
4259 | /// Provide fast operand accessors | |||
4260 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
4261 | ||||
4262 | // Accessor Methods for CatchSwitch stmt | |||
4263 | Value *getParentPad() const { return getOperand(0); } | |||
4264 | void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); } | |||
4265 | ||||
4266 | // Accessor Methods for CatchSwitch stmt | |||
4267 | bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); } | |||
4268 | bool unwindsToCaller() const { return !hasUnwindDest(); } | |||
4269 | BasicBlock *getUnwindDest() const { | |||
4270 | if (hasUnwindDest()) | |||
4271 | return cast<BasicBlock>(getOperand(1)); | |||
4272 | return nullptr; | |||
4273 | } | |||
4274 | void setUnwindDest(BasicBlock *UnwindDest) { | |||
4275 | assert(UnwindDest)((UnwindDest) ? static_cast<void> (0) : __assert_fail ( "UnwindDest", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4275, __PRETTY_FUNCTION__)); | |||
4276 | assert(hasUnwindDest())((hasUnwindDest()) ? static_cast<void> (0) : __assert_fail ("hasUnwindDest()", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4276, __PRETTY_FUNCTION__)); | |||
4277 | setOperand(1, UnwindDest); | |||
4278 | } | |||
4279 | ||||
4280 | /// return the number of 'handlers' in this catchswitch | |||
4281 | /// instruction, except the default handler | |||
4282 | unsigned getNumHandlers() const { | |||
4283 | if (hasUnwindDest()) | |||
4284 | return getNumOperands() - 2; | |||
4285 | return getNumOperands() - 1; | |||
4286 | } | |||
4287 | ||||
4288 | private: | |||
4289 | static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); } | |||
4290 | static const BasicBlock *handler_helper(const Value *V) { | |||
4291 | return cast<BasicBlock>(V); | |||
4292 | } | |||
4293 | ||||
4294 | public: | |||
4295 | using DerefFnTy = BasicBlock *(*)(Value *); | |||
4296 | using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>; | |||
4297 | using handler_range = iterator_range<handler_iterator>; | |||
4298 | using ConstDerefFnTy = const BasicBlock *(*)(const Value *); | |||
4299 | using const_handler_iterator = | |||
4300 | mapped_iterator<const_op_iterator, ConstDerefFnTy>; | |||
4301 | using const_handler_range = iterator_range<const_handler_iterator>; | |||
4302 | ||||
4303 | /// Returns an iterator that points to the first handler in CatchSwitchInst. | |||
4304 | handler_iterator handler_begin() { | |||
4305 | op_iterator It = op_begin() + 1; | |||
4306 | if (hasUnwindDest()) | |||
4307 | ++It; | |||
4308 | return handler_iterator(It, DerefFnTy(handler_helper)); | |||
4309 | } | |||
4310 | ||||
4311 | /// Returns an iterator that points to the first handler in the | |||
4312 | /// CatchSwitchInst. | |||
4313 | const_handler_iterator handler_begin() const { | |||
4314 | const_op_iterator It = op_begin() + 1; | |||
4315 | if (hasUnwindDest()) | |||
4316 | ++It; | |||
4317 | return const_handler_iterator(It, ConstDerefFnTy(handler_helper)); | |||
4318 | } | |||
4319 | ||||
4320 | /// Returns a read-only iterator that points one past the last | |||
4321 | /// handler in the CatchSwitchInst. | |||
4322 | handler_iterator handler_end() { | |||
4323 | return handler_iterator(op_end(), DerefFnTy(handler_helper)); | |||
4324 | } | |||
4325 | ||||
4326 | /// Returns an iterator that points one past the last handler in the | |||
4327 | /// CatchSwitchInst. | |||
4328 | const_handler_iterator handler_end() const { | |||
4329 | return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper)); | |||
4330 | } | |||
4331 | ||||
4332 | /// iteration adapter for range-for loops. | |||
4333 | handler_range handlers() { | |||
4334 | return make_range(handler_begin(), handler_end()); | |||
4335 | } | |||
4336 | ||||
4337 | /// iteration adapter for range-for loops. | |||
4338 | const_handler_range handlers() const { | |||
4339 | return make_range(handler_begin(), handler_end()); | |||
4340 | } | |||
4341 | ||||
4342 | /// Add an entry to the switch instruction... | |||
4343 | /// Note: | |||
4344 | /// This action invalidates handler_end(). Old handler_end() iterator will | |||
4345 | /// point to the added handler. | |||
4346 | void addHandler(BasicBlock *Dest); | |||
4347 | ||||
4348 | void removeHandler(handler_iterator HI); | |||
4349 | ||||
4350 | unsigned getNumSuccessors() const { return getNumOperands() - 1; } | |||
4351 | BasicBlock *getSuccessor(unsigned Idx) const { | |||
4352 | assert(Idx < getNumSuccessors() &&((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4353, __PRETTY_FUNCTION__)) | |||
4353 | "Successor # out of range for catchswitch!")((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4353, __PRETTY_FUNCTION__)); | |||
4354 | return cast<BasicBlock>(getOperand(Idx + 1)); | |||
4355 | } | |||
4356 | void setSuccessor(unsigned Idx, BasicBlock *NewSucc) { | |||
4357 | assert(Idx < getNumSuccessors() &&((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4358, __PRETTY_FUNCTION__)) | |||
4358 | "Successor # out of range for catchswitch!")((Idx < getNumSuccessors() && "Successor # out of range for catchswitch!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4358, __PRETTY_FUNCTION__)); | |||
4359 | setOperand(Idx + 1, NewSucc); | |||
4360 | } | |||
4361 | ||||
4362 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4363 | static bool classof(const Instruction *I) { | |||
4364 | return I->getOpcode() == Instruction::CatchSwitch; | |||
4365 | } | |||
4366 | static bool classof(const Value *V) { | |||
4367 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4368 | } | |||
4369 | }; | |||
4370 | ||||
4371 | template <> | |||
4372 | struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {}; | |||
4373 | ||||
4374 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)CatchSwitchInst::op_iterator CatchSwitchInst::op_begin() { return OperandTraits<CatchSwitchInst>::op_begin(this); } CatchSwitchInst ::const_op_iterator CatchSwitchInst::op_begin() const { return OperandTraits<CatchSwitchInst>::op_begin(const_cast< CatchSwitchInst*>(this)); } CatchSwitchInst::op_iterator CatchSwitchInst ::op_end() { return OperandTraits<CatchSwitchInst>::op_end (this); } CatchSwitchInst::const_op_iterator CatchSwitchInst:: op_end() const { return OperandTraits<CatchSwitchInst>:: op_end(const_cast<CatchSwitchInst*>(this)); } Value *CatchSwitchInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4374, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CatchSwitchInst>::op_begin(const_cast< CatchSwitchInst*>(this))[i_nocapture].get()); } void CatchSwitchInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<CatchSwitchInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4374, __PRETTY_FUNCTION__)); OperandTraits<CatchSwitchInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CatchSwitchInst::getNumOperands() const { return OperandTraits <CatchSwitchInst>::operands(this); } template <int Idx_nocapture > Use &CatchSwitchInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CatchSwitchInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
4375 | ||||
4376 | //===----------------------------------------------------------------------===// | |||
4377 | // CleanupPadInst Class | |||
4378 | //===----------------------------------------------------------------------===// | |||
4379 | class CleanupPadInst : public FuncletPadInst { | |||
4380 | private: | |||
4381 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, | |||
4382 | unsigned Values, const Twine &NameStr, | |||
4383 | Instruction *InsertBefore) | |||
4384 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, | |||
4385 | NameStr, InsertBefore) {} | |||
4386 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, | |||
4387 | unsigned Values, const Twine &NameStr, | |||
4388 | BasicBlock *InsertAtEnd) | |||
4389 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, | |||
4390 | NameStr, InsertAtEnd) {} | |||
4391 | ||||
4392 | public: | |||
4393 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None, | |||
4394 | const Twine &NameStr = "", | |||
4395 | Instruction *InsertBefore = nullptr) { | |||
4396 | unsigned Values = 1 + Args.size(); | |||
4397 | return new (Values) | |||
4398 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore); | |||
4399 | } | |||
4400 | ||||
4401 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args, | |||
4402 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
4403 | unsigned Values = 1 + Args.size(); | |||
4404 | return new (Values) | |||
4405 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd); | |||
4406 | } | |||
4407 | ||||
4408 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4409 | static bool classof(const Instruction *I) { | |||
4410 | return I->getOpcode() == Instruction::CleanupPad; | |||
4411 | } | |||
4412 | static bool classof(const Value *V) { | |||
4413 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4414 | } | |||
4415 | }; | |||
4416 | ||||
4417 | //===----------------------------------------------------------------------===// | |||
4418 | // CatchPadInst Class | |||
4419 | //===----------------------------------------------------------------------===// | |||
4420 | class CatchPadInst : public FuncletPadInst { | |||
4421 | private: | |||
4422 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, | |||
4423 | unsigned Values, const Twine &NameStr, | |||
4424 | Instruction *InsertBefore) | |||
4425 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, | |||
4426 | NameStr, InsertBefore) {} | |||
4427 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, | |||
4428 | unsigned Values, const Twine &NameStr, | |||
4429 | BasicBlock *InsertAtEnd) | |||
4430 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, | |||
4431 | NameStr, InsertAtEnd) {} | |||
4432 | ||||
4433 | public: | |||
4434 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, | |||
4435 | const Twine &NameStr = "", | |||
4436 | Instruction *InsertBefore = nullptr) { | |||
4437 | unsigned Values = 1 + Args.size(); | |||
4438 | return new (Values) | |||
4439 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore); | |||
4440 | } | |||
4441 | ||||
4442 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, | |||
4443 | const Twine &NameStr, BasicBlock *InsertAtEnd) { | |||
4444 | unsigned Values = 1 + Args.size(); | |||
4445 | return new (Values) | |||
4446 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd); | |||
4447 | } | |||
4448 | ||||
4449 | /// Convenience accessors | |||
4450 | CatchSwitchInst *getCatchSwitch() const { | |||
4451 | return cast<CatchSwitchInst>(Op<-1>()); | |||
4452 | } | |||
4453 | void setCatchSwitch(Value *CatchSwitch) { | |||
4454 | assert(CatchSwitch)((CatchSwitch) ? static_cast<void> (0) : __assert_fail ( "CatchSwitch", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4454, __PRETTY_FUNCTION__)); | |||
4455 | Op<-1>() = CatchSwitch; | |||
4456 | } | |||
4457 | ||||
4458 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4459 | static bool classof(const Instruction *I) { | |||
4460 | return I->getOpcode() == Instruction::CatchPad; | |||
4461 | } | |||
4462 | static bool classof(const Value *V) { | |||
4463 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4464 | } | |||
4465 | }; | |||
4466 | ||||
4467 | //===----------------------------------------------------------------------===// | |||
4468 | // CatchReturnInst Class | |||
4469 | //===----------------------------------------------------------------------===// | |||
4470 | ||||
4471 | class CatchReturnInst : public Instruction { | |||
4472 | CatchReturnInst(const CatchReturnInst &RI); | |||
4473 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore); | |||
4474 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd); | |||
4475 | ||||
4476 | void init(Value *CatchPad, BasicBlock *BB); | |||
4477 | ||||
4478 | protected: | |||
4479 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4480 | friend class Instruction; | |||
4481 | ||||
4482 | CatchReturnInst *cloneImpl() const; | |||
4483 | ||||
4484 | public: | |||
4485 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, | |||
4486 | Instruction *InsertBefore = nullptr) { | |||
4487 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4487, __PRETTY_FUNCTION__)); | |||
4488 | assert(BB)((BB) ? static_cast<void> (0) : __assert_fail ("BB", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4488, __PRETTY_FUNCTION__)); | |||
4489 | return new (2) CatchReturnInst(CatchPad, BB, InsertBefore); | |||
4490 | } | |||
4491 | ||||
4492 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, | |||
4493 | BasicBlock *InsertAtEnd) { | |||
4494 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4494, __PRETTY_FUNCTION__)); | |||
4495 | assert(BB)((BB) ? static_cast<void> (0) : __assert_fail ("BB", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4495, __PRETTY_FUNCTION__)); | |||
4496 | return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd); | |||
4497 | } | |||
4498 | ||||
4499 | /// Provide fast operand accessors | |||
4500 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
4501 | ||||
4502 | /// Convenience accessors. | |||
4503 | CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); } | |||
4504 | void setCatchPad(CatchPadInst *CatchPad) { | |||
4505 | assert(CatchPad)((CatchPad) ? static_cast<void> (0) : __assert_fail ("CatchPad" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4505, __PRETTY_FUNCTION__)); | |||
4506 | Op<0>() = CatchPad; | |||
4507 | } | |||
4508 | ||||
4509 | BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); } | |||
4510 | void setSuccessor(BasicBlock *NewSucc) { | |||
4511 | assert(NewSucc)((NewSucc) ? static_cast<void> (0) : __assert_fail ("NewSucc" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4511, __PRETTY_FUNCTION__)); | |||
4512 | Op<1>() = NewSucc; | |||
4513 | } | |||
4514 | unsigned getNumSuccessors() const { return 1; } | |||
4515 | ||||
4516 | /// Get the parentPad of this catchret's catchpad's catchswitch. | |||
4517 | /// The successor block is implicitly a member of this funclet. | |||
4518 | Value *getCatchSwitchParentPad() const { | |||
4519 | return getCatchPad()->getCatchSwitch()->getParentPad(); | |||
4520 | } | |||
4521 | ||||
4522 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4523 | static bool classof(const Instruction *I) { | |||
4524 | return (I->getOpcode() == Instruction::CatchRet); | |||
4525 | } | |||
4526 | static bool classof(const Value *V) { | |||
4527 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4528 | } | |||
4529 | ||||
4530 | private: | |||
4531 | BasicBlock *getSuccessor(unsigned Idx) const { | |||
4532 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((Idx < getNumSuccessors() && "Successor # out of range for catchret!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchret!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4532, __PRETTY_FUNCTION__)); | |||
4533 | return getSuccessor(); | |||
4534 | } | |||
4535 | ||||
4536 | void setSuccessor(unsigned Idx, BasicBlock *B) { | |||
4537 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")((Idx < getNumSuccessors() && "Successor # out of range for catchret!" ) ? static_cast<void> (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchret!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4537, __PRETTY_FUNCTION__)); | |||
4538 | setSuccessor(B); | |||
4539 | } | |||
4540 | }; | |||
4541 | ||||
4542 | template <> | |||
4543 | struct OperandTraits<CatchReturnInst> | |||
4544 | : public FixedNumOperandTraits<CatchReturnInst, 2> {}; | |||
4545 | ||||
4546 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)CatchReturnInst::op_iterator CatchReturnInst::op_begin() { return OperandTraits<CatchReturnInst>::op_begin(this); } CatchReturnInst ::const_op_iterator CatchReturnInst::op_begin() const { return OperandTraits<CatchReturnInst>::op_begin(const_cast< CatchReturnInst*>(this)); } CatchReturnInst::op_iterator CatchReturnInst ::op_end() { return OperandTraits<CatchReturnInst>::op_end (this); } CatchReturnInst::const_op_iterator CatchReturnInst:: op_end() const { return OperandTraits<CatchReturnInst>:: op_end(const_cast<CatchReturnInst*>(this)); } Value *CatchReturnInst ::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4546, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CatchReturnInst>::op_begin(const_cast< CatchReturnInst*>(this))[i_nocapture].get()); } void CatchReturnInst ::setOperand(unsigned i_nocapture, Value *Val_nocapture) { (( i_nocapture < OperandTraits<CatchReturnInst>::operands (this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4546, __PRETTY_FUNCTION__)); OperandTraits<CatchReturnInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CatchReturnInst::getNumOperands() const { return OperandTraits <CatchReturnInst>::operands(this); } template <int Idx_nocapture > Use &CatchReturnInst::Op() { return this->OpFrom< Idx_nocapture>(this); } template <int Idx_nocapture> const Use &CatchReturnInst::Op() const { return this-> OpFrom<Idx_nocapture>(this); } | |||
4547 | ||||
4548 | //===----------------------------------------------------------------------===// | |||
4549 | // CleanupReturnInst Class | |||
4550 | //===----------------------------------------------------------------------===// | |||
4551 | ||||
4552 | class CleanupReturnInst : public Instruction { | |||
4553 | using UnwindDestField = BoolBitfieldElementT<0>; | |||
4554 | ||||
4555 | private: | |||
4556 | CleanupReturnInst(const CleanupReturnInst &RI); | |||
4557 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, | |||
4558 | Instruction *InsertBefore = nullptr); | |||
4559 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, | |||
4560 | BasicBlock *InsertAtEnd); | |||
4561 | ||||
4562 | void init(Value *CleanupPad, BasicBlock *UnwindBB); | |||
4563 | ||||
4564 | protected: | |||
4565 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4566 | friend class Instruction; | |||
4567 | ||||
4568 | CleanupReturnInst *cloneImpl() const; | |||
4569 | ||||
4570 | public: | |||
4571 | static CleanupReturnInst *Create(Value *CleanupPad, | |||
4572 | BasicBlock *UnwindBB = nullptr, | |||
4573 | Instruction *InsertBefore = nullptr) { | |||
4574 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4574, __PRETTY_FUNCTION__)); | |||
4575 | unsigned Values = 1; | |||
4576 | if (UnwindBB) | |||
4577 | ++Values; | |||
4578 | return new (Values) | |||
4579 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore); | |||
4580 | } | |||
4581 | ||||
4582 | static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB, | |||
4583 | BasicBlock *InsertAtEnd) { | |||
4584 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4584, __PRETTY_FUNCTION__)); | |||
4585 | unsigned Values = 1; | |||
4586 | if (UnwindBB) | |||
4587 | ++Values; | |||
4588 | return new (Values) | |||
4589 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd); | |||
4590 | } | |||
4591 | ||||
4592 | /// Provide fast operand accessors | |||
4593 | DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)public: inline Value *getOperand(unsigned) const; inline void setOperand(unsigned, Value*); inline op_iterator op_begin(); inline const_op_iterator op_begin() const; inline op_iterator op_end(); inline const_op_iterator op_end() const; protected : template <int> inline Use &Op(); template <int > inline const Use &Op() const; public: inline unsigned getNumOperands() const; | |||
4594 | ||||
4595 | bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); } | |||
4596 | bool unwindsToCaller() const { return !hasUnwindDest(); } | |||
4597 | ||||
4598 | /// Convenience accessor. | |||
4599 | CleanupPadInst *getCleanupPad() const { | |||
4600 | return cast<CleanupPadInst>(Op<0>()); | |||
4601 | } | |||
4602 | void setCleanupPad(CleanupPadInst *CleanupPad) { | |||
4603 | assert(CleanupPad)((CleanupPad) ? static_cast<void> (0) : __assert_fail ( "CleanupPad", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4603, __PRETTY_FUNCTION__)); | |||
4604 | Op<0>() = CleanupPad; | |||
4605 | } | |||
4606 | ||||
4607 | unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; } | |||
4608 | ||||
4609 | BasicBlock *getUnwindDest() const { | |||
4610 | return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr; | |||
4611 | } | |||
4612 | void setUnwindDest(BasicBlock *NewDest) { | |||
4613 | assert(NewDest)((NewDest) ? static_cast<void> (0) : __assert_fail ("NewDest" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4613, __PRETTY_FUNCTION__)); | |||
4614 | assert(hasUnwindDest())((hasUnwindDest()) ? static_cast<void> (0) : __assert_fail ("hasUnwindDest()", "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4614, __PRETTY_FUNCTION__)); | |||
4615 | Op<1>() = NewDest; | |||
4616 | } | |||
4617 | ||||
4618 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4619 | static bool classof(const Instruction *I) { | |||
4620 | return (I->getOpcode() == Instruction::CleanupRet); | |||
4621 | } | |||
4622 | static bool classof(const Value *V) { | |||
4623 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4624 | } | |||
4625 | ||||
4626 | private: | |||
4627 | BasicBlock *getSuccessor(unsigned Idx) const { | |||
4628 | assert(Idx == 0)((Idx == 0) ? static_cast<void> (0) : __assert_fail ("Idx == 0" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4628, __PRETTY_FUNCTION__)); | |||
4629 | return getUnwindDest(); | |||
4630 | } | |||
4631 | ||||
4632 | void setSuccessor(unsigned Idx, BasicBlock *B) { | |||
4633 | assert(Idx == 0)((Idx == 0) ? static_cast<void> (0) : __assert_fail ("Idx == 0" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4633, __PRETTY_FUNCTION__)); | |||
4634 | setUnwindDest(B); | |||
4635 | } | |||
4636 | ||||
4637 | // Shadow Instruction::setInstructionSubclassData with a private forwarding | |||
4638 | // method so that subclasses cannot accidentally use it. | |||
4639 | template <typename Bitfield> | |||
4640 | void setSubclassData(typename Bitfield::Type Value) { | |||
4641 | Instruction::setSubclassData<Bitfield>(Value); | |||
4642 | } | |||
4643 | }; | |||
4644 | ||||
4645 | template <> | |||
4646 | struct OperandTraits<CleanupReturnInst> | |||
4647 | : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {}; | |||
4648 | ||||
4649 | DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)CleanupReturnInst::op_iterator CleanupReturnInst::op_begin() { return OperandTraits<CleanupReturnInst>::op_begin(this ); } CleanupReturnInst::const_op_iterator CleanupReturnInst:: op_begin() const { return OperandTraits<CleanupReturnInst> ::op_begin(const_cast<CleanupReturnInst*>(this)); } CleanupReturnInst ::op_iterator CleanupReturnInst::op_end() { return OperandTraits <CleanupReturnInst>::op_end(this); } CleanupReturnInst:: const_op_iterator CleanupReturnInst::op_end() const { return OperandTraits <CleanupReturnInst>::op_end(const_cast<CleanupReturnInst *>(this)); } Value *CleanupReturnInst::getOperand(unsigned i_nocapture) const { ((i_nocapture < OperandTraits<CleanupReturnInst >::operands(this) && "getOperand() out of range!") ? static_cast<void> (0) : __assert_fail ("i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && \"getOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4649, __PRETTY_FUNCTION__)); return cast_or_null<Value> ( OperandTraits<CleanupReturnInst>::op_begin(const_cast <CleanupReturnInst*>(this))[i_nocapture].get()); } void CleanupReturnInst::setOperand(unsigned i_nocapture, Value *Val_nocapture ) { ((i_nocapture < OperandTraits<CleanupReturnInst> ::operands(this) && "setOperand() out of range!") ? static_cast <void> (0) : __assert_fail ("i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && \"setOperand() out of range!\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4649, __PRETTY_FUNCTION__)); OperandTraits<CleanupReturnInst >::op_begin(this)[i_nocapture] = Val_nocapture; } unsigned CleanupReturnInst::getNumOperands() const { return OperandTraits <CleanupReturnInst>::operands(this); } template <int Idx_nocapture> Use &CleanupReturnInst::Op() { return this ->OpFrom<Idx_nocapture>(this); } template <int Idx_nocapture > const Use &CleanupReturnInst::Op() const { return this ->OpFrom<Idx_nocapture>(this); } | |||
4650 | ||||
4651 | //===----------------------------------------------------------------------===// | |||
4652 | // UnreachableInst Class | |||
4653 | //===----------------------------------------------------------------------===// | |||
4654 | ||||
4655 | //===--------------------------------------------------------------------------- | |||
4656 | /// This function has undefined behavior. In particular, the | |||
4657 | /// presence of this instruction indicates some higher level knowledge that the | |||
4658 | /// end of the block cannot be reached. | |||
4659 | /// | |||
4660 | class UnreachableInst : public Instruction { | |||
4661 | protected: | |||
4662 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4663 | friend class Instruction; | |||
4664 | ||||
4665 | UnreachableInst *cloneImpl() const; | |||
4666 | ||||
4667 | public: | |||
4668 | explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr); | |||
4669 | explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd); | |||
4670 | ||||
4671 | // allocate space for exactly zero operands | |||
4672 | void *operator new(size_t s) { | |||
4673 | return User::operator new(s, 0); | |||
4674 | } | |||
4675 | ||||
4676 | unsigned getNumSuccessors() const { return 0; } | |||
4677 | ||||
4678 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4679 | static bool classof(const Instruction *I) { | |||
4680 | return I->getOpcode() == Instruction::Unreachable; | |||
4681 | } | |||
4682 | static bool classof(const Value *V) { | |||
4683 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4684 | } | |||
4685 | ||||
4686 | private: | |||
4687 | BasicBlock *getSuccessor(unsigned idx) const { | |||
4688 | llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4688); | |||
4689 | } | |||
4690 | ||||
4691 | void setSuccessor(unsigned idx, BasicBlock *B) { | |||
4692 | llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 4692); | |||
4693 | } | |||
4694 | }; | |||
4695 | ||||
4696 | //===----------------------------------------------------------------------===// | |||
4697 | // TruncInst Class | |||
4698 | //===----------------------------------------------------------------------===// | |||
4699 | ||||
4700 | /// This class represents a truncation of integer types. | |||
4701 | class TruncInst : public CastInst { | |||
4702 | protected: | |||
4703 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4704 | friend class Instruction; | |||
4705 | ||||
4706 | /// Clone an identical TruncInst | |||
4707 | TruncInst *cloneImpl() const; | |||
4708 | ||||
4709 | public: | |||
4710 | /// Constructor with insert-before-instruction semantics | |||
4711 | TruncInst( | |||
4712 | Value *S, ///< The value to be truncated | |||
4713 | Type *Ty, ///< The (smaller) type to truncate to | |||
4714 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4715 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4716 | ); | |||
4717 | ||||
4718 | /// Constructor with insert-at-end-of-block semantics | |||
4719 | TruncInst( | |||
4720 | Value *S, ///< The value to be truncated | |||
4721 | Type *Ty, ///< The (smaller) type to truncate to | |||
4722 | const Twine &NameStr, ///< A name for the new instruction | |||
4723 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4724 | ); | |||
4725 | ||||
4726 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4727 | static bool classof(const Instruction *I) { | |||
4728 | return I->getOpcode() == Trunc; | |||
4729 | } | |||
4730 | static bool classof(const Value *V) { | |||
4731 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4732 | } | |||
4733 | }; | |||
4734 | ||||
4735 | //===----------------------------------------------------------------------===// | |||
4736 | // ZExtInst Class | |||
4737 | //===----------------------------------------------------------------------===// | |||
4738 | ||||
4739 | /// This class represents zero extension of integer types. | |||
4740 | class ZExtInst : public CastInst { | |||
4741 | protected: | |||
4742 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4743 | friend class Instruction; | |||
4744 | ||||
4745 | /// Clone an identical ZExtInst | |||
4746 | ZExtInst *cloneImpl() const; | |||
4747 | ||||
4748 | public: | |||
4749 | /// Constructor with insert-before-instruction semantics | |||
4750 | ZExtInst( | |||
4751 | Value *S, ///< The value to be zero extended | |||
4752 | Type *Ty, ///< The type to zero extend to | |||
4753 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4754 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4755 | ); | |||
4756 | ||||
4757 | /// Constructor with insert-at-end semantics. | |||
4758 | ZExtInst( | |||
4759 | Value *S, ///< The value to be zero extended | |||
4760 | Type *Ty, ///< The type to zero extend to | |||
4761 | const Twine &NameStr, ///< A name for the new instruction | |||
4762 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4763 | ); | |||
4764 | ||||
4765 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4766 | static bool classof(const Instruction *I) { | |||
4767 | return I->getOpcode() == ZExt; | |||
4768 | } | |||
4769 | static bool classof(const Value *V) { | |||
4770 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4771 | } | |||
4772 | }; | |||
4773 | ||||
4774 | //===----------------------------------------------------------------------===// | |||
4775 | // SExtInst Class | |||
4776 | //===----------------------------------------------------------------------===// | |||
4777 | ||||
4778 | /// This class represents a sign extension of integer types. | |||
4779 | class SExtInst : public CastInst { | |||
4780 | protected: | |||
4781 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4782 | friend class Instruction; | |||
4783 | ||||
4784 | /// Clone an identical SExtInst | |||
4785 | SExtInst *cloneImpl() const; | |||
4786 | ||||
4787 | public: | |||
4788 | /// Constructor with insert-before-instruction semantics | |||
4789 | SExtInst( | |||
4790 | Value *S, ///< The value to be sign extended | |||
4791 | Type *Ty, ///< The type to sign extend to | |||
4792 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4793 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4794 | ); | |||
4795 | ||||
4796 | /// Constructor with insert-at-end-of-block semantics | |||
4797 | SExtInst( | |||
4798 | Value *S, ///< The value to be sign extended | |||
4799 | Type *Ty, ///< The type to sign extend to | |||
4800 | const Twine &NameStr, ///< A name for the new instruction | |||
4801 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4802 | ); | |||
4803 | ||||
4804 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4805 | static bool classof(const Instruction *I) { | |||
4806 | return I->getOpcode() == SExt; | |||
4807 | } | |||
4808 | static bool classof(const Value *V) { | |||
4809 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4810 | } | |||
4811 | }; | |||
4812 | ||||
4813 | //===----------------------------------------------------------------------===// | |||
4814 | // FPTruncInst Class | |||
4815 | //===----------------------------------------------------------------------===// | |||
4816 | ||||
4817 | /// This class represents a truncation of floating point types. | |||
4818 | class FPTruncInst : public CastInst { | |||
4819 | protected: | |||
4820 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4821 | friend class Instruction; | |||
4822 | ||||
4823 | /// Clone an identical FPTruncInst | |||
4824 | FPTruncInst *cloneImpl() const; | |||
4825 | ||||
4826 | public: | |||
4827 | /// Constructor with insert-before-instruction semantics | |||
4828 | FPTruncInst( | |||
4829 | Value *S, ///< The value to be truncated | |||
4830 | Type *Ty, ///< The type to truncate to | |||
4831 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4832 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4833 | ); | |||
4834 | ||||
4835 | /// Constructor with insert-before-instruction semantics | |||
4836 | FPTruncInst( | |||
4837 | Value *S, ///< The value to be truncated | |||
4838 | Type *Ty, ///< The type to truncate to | |||
4839 | const Twine &NameStr, ///< A name for the new instruction | |||
4840 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4841 | ); | |||
4842 | ||||
4843 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4844 | static bool classof(const Instruction *I) { | |||
4845 | return I->getOpcode() == FPTrunc; | |||
4846 | } | |||
4847 | static bool classof(const Value *V) { | |||
4848 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4849 | } | |||
4850 | }; | |||
4851 | ||||
4852 | //===----------------------------------------------------------------------===// | |||
4853 | // FPExtInst Class | |||
4854 | //===----------------------------------------------------------------------===// | |||
4855 | ||||
4856 | /// This class represents an extension of floating point types. | |||
4857 | class FPExtInst : public CastInst { | |||
4858 | protected: | |||
4859 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4860 | friend class Instruction; | |||
4861 | ||||
4862 | /// Clone an identical FPExtInst | |||
4863 | FPExtInst *cloneImpl() const; | |||
4864 | ||||
4865 | public: | |||
4866 | /// Constructor with insert-before-instruction semantics | |||
4867 | FPExtInst( | |||
4868 | Value *S, ///< The value to be extended | |||
4869 | Type *Ty, ///< The type to extend to | |||
4870 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4871 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4872 | ); | |||
4873 | ||||
4874 | /// Constructor with insert-at-end-of-block semantics | |||
4875 | FPExtInst( | |||
4876 | Value *S, ///< The value to be extended | |||
4877 | Type *Ty, ///< The type to extend to | |||
4878 | const Twine &NameStr, ///< A name for the new instruction | |||
4879 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4880 | ); | |||
4881 | ||||
4882 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4883 | static bool classof(const Instruction *I) { | |||
4884 | return I->getOpcode() == FPExt; | |||
4885 | } | |||
4886 | static bool classof(const Value *V) { | |||
4887 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4888 | } | |||
4889 | }; | |||
4890 | ||||
4891 | //===----------------------------------------------------------------------===// | |||
4892 | // UIToFPInst Class | |||
4893 | //===----------------------------------------------------------------------===// | |||
4894 | ||||
4895 | /// This class represents a cast unsigned integer to floating point. | |||
4896 | class UIToFPInst : public CastInst { | |||
4897 | protected: | |||
4898 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4899 | friend class Instruction; | |||
4900 | ||||
4901 | /// Clone an identical UIToFPInst | |||
4902 | UIToFPInst *cloneImpl() const; | |||
4903 | ||||
4904 | public: | |||
4905 | /// Constructor with insert-before-instruction semantics | |||
4906 | UIToFPInst( | |||
4907 | Value *S, ///< The value to be converted | |||
4908 | Type *Ty, ///< The type to convert to | |||
4909 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4910 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4911 | ); | |||
4912 | ||||
4913 | /// Constructor with insert-at-end-of-block semantics | |||
4914 | UIToFPInst( | |||
4915 | Value *S, ///< The value to be converted | |||
4916 | Type *Ty, ///< The type to convert to | |||
4917 | const Twine &NameStr, ///< A name for the new instruction | |||
4918 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4919 | ); | |||
4920 | ||||
4921 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4922 | static bool classof(const Instruction *I) { | |||
4923 | return I->getOpcode() == UIToFP; | |||
4924 | } | |||
4925 | static bool classof(const Value *V) { | |||
4926 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4927 | } | |||
4928 | }; | |||
4929 | ||||
4930 | //===----------------------------------------------------------------------===// | |||
4931 | // SIToFPInst Class | |||
4932 | //===----------------------------------------------------------------------===// | |||
4933 | ||||
4934 | /// This class represents a cast from signed integer to floating point. | |||
4935 | class SIToFPInst : public CastInst { | |||
4936 | protected: | |||
4937 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4938 | friend class Instruction; | |||
4939 | ||||
4940 | /// Clone an identical SIToFPInst | |||
4941 | SIToFPInst *cloneImpl() const; | |||
4942 | ||||
4943 | public: | |||
4944 | /// Constructor with insert-before-instruction semantics | |||
4945 | SIToFPInst( | |||
4946 | Value *S, ///< The value to be converted | |||
4947 | Type *Ty, ///< The type to convert to | |||
4948 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4949 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4950 | ); | |||
4951 | ||||
4952 | /// Constructor with insert-at-end-of-block semantics | |||
4953 | SIToFPInst( | |||
4954 | Value *S, ///< The value to be converted | |||
4955 | Type *Ty, ///< The type to convert to | |||
4956 | const Twine &NameStr, ///< A name for the new instruction | |||
4957 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
4958 | ); | |||
4959 | ||||
4960 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
4961 | static bool classof(const Instruction *I) { | |||
4962 | return I->getOpcode() == SIToFP; | |||
4963 | } | |||
4964 | static bool classof(const Value *V) { | |||
4965 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
4966 | } | |||
4967 | }; | |||
4968 | ||||
4969 | //===----------------------------------------------------------------------===// | |||
4970 | // FPToUIInst Class | |||
4971 | //===----------------------------------------------------------------------===// | |||
4972 | ||||
4973 | /// This class represents a cast from floating point to unsigned integer | |||
4974 | class FPToUIInst : public CastInst { | |||
4975 | protected: | |||
4976 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
4977 | friend class Instruction; | |||
4978 | ||||
4979 | /// Clone an identical FPToUIInst | |||
4980 | FPToUIInst *cloneImpl() const; | |||
4981 | ||||
4982 | public: | |||
4983 | /// Constructor with insert-before-instruction semantics | |||
4984 | FPToUIInst( | |||
4985 | Value *S, ///< The value to be converted | |||
4986 | Type *Ty, ///< The type to convert to | |||
4987 | const Twine &NameStr = "", ///< A name for the new instruction | |||
4988 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
4989 | ); | |||
4990 | ||||
4991 | /// Constructor with insert-at-end-of-block semantics | |||
4992 | FPToUIInst( | |||
4993 | Value *S, ///< The value to be converted | |||
4994 | Type *Ty, ///< The type to convert to | |||
4995 | const Twine &NameStr, ///< A name for the new instruction | |||
4996 | BasicBlock *InsertAtEnd ///< Where to insert the new instruction | |||
4997 | ); | |||
4998 | ||||
4999 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5000 | static bool classof(const Instruction *I) { | |||
5001 | return I->getOpcode() == FPToUI; | |||
5002 | } | |||
5003 | static bool classof(const Value *V) { | |||
5004 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5005 | } | |||
5006 | }; | |||
5007 | ||||
5008 | //===----------------------------------------------------------------------===// | |||
5009 | // FPToSIInst Class | |||
5010 | //===----------------------------------------------------------------------===// | |||
5011 | ||||
5012 | /// This class represents a cast from floating point to signed integer. | |||
5013 | class FPToSIInst : public CastInst { | |||
5014 | protected: | |||
5015 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5016 | friend class Instruction; | |||
5017 | ||||
5018 | /// Clone an identical FPToSIInst | |||
5019 | FPToSIInst *cloneImpl() const; | |||
5020 | ||||
5021 | public: | |||
5022 | /// Constructor with insert-before-instruction semantics | |||
5023 | FPToSIInst( | |||
5024 | Value *S, ///< The value to be converted | |||
5025 | Type *Ty, ///< The type to convert to | |||
5026 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5027 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5028 | ); | |||
5029 | ||||
5030 | /// Constructor with insert-at-end-of-block semantics | |||
5031 | FPToSIInst( | |||
5032 | Value *S, ///< The value to be converted | |||
5033 | Type *Ty, ///< The type to convert to | |||
5034 | const Twine &NameStr, ///< A name for the new instruction | |||
5035 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5036 | ); | |||
5037 | ||||
5038 | /// Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5039 | static bool classof(const Instruction *I) { | |||
5040 | return I->getOpcode() == FPToSI; | |||
5041 | } | |||
5042 | static bool classof(const Value *V) { | |||
5043 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5044 | } | |||
5045 | }; | |||
5046 | ||||
5047 | //===----------------------------------------------------------------------===// | |||
5048 | // IntToPtrInst Class | |||
5049 | //===----------------------------------------------------------------------===// | |||
5050 | ||||
5051 | /// This class represents a cast from an integer to a pointer. | |||
5052 | class IntToPtrInst : public CastInst { | |||
5053 | public: | |||
5054 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5055 | friend class Instruction; | |||
5056 | ||||
5057 | /// Constructor with insert-before-instruction semantics | |||
5058 | IntToPtrInst( | |||
5059 | Value *S, ///< The value to be converted | |||
5060 | Type *Ty, ///< The type to convert to | |||
5061 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5062 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5063 | ); | |||
5064 | ||||
5065 | /// Constructor with insert-at-end-of-block semantics | |||
5066 | IntToPtrInst( | |||
5067 | Value *S, ///< The value to be converted | |||
5068 | Type *Ty, ///< The type to convert to | |||
5069 | const Twine &NameStr, ///< A name for the new instruction | |||
5070 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5071 | ); | |||
5072 | ||||
5073 | /// Clone an identical IntToPtrInst. | |||
5074 | IntToPtrInst *cloneImpl() const; | |||
5075 | ||||
5076 | /// Returns the address space of this instruction's pointer type. | |||
5077 | unsigned getAddressSpace() const { | |||
5078 | return getType()->getPointerAddressSpace(); | |||
5079 | } | |||
5080 | ||||
5081 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5082 | static bool classof(const Instruction *I) { | |||
5083 | return I->getOpcode() == IntToPtr; | |||
5084 | } | |||
5085 | static bool classof(const Value *V) { | |||
5086 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5087 | } | |||
5088 | }; | |||
5089 | ||||
5090 | //===----------------------------------------------------------------------===// | |||
5091 | // PtrToIntInst Class | |||
5092 | //===----------------------------------------------------------------------===// | |||
5093 | ||||
5094 | /// This class represents a cast from a pointer to an integer. | |||
5095 | class PtrToIntInst : public CastInst { | |||
5096 | protected: | |||
5097 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5098 | friend class Instruction; | |||
5099 | ||||
5100 | /// Clone an identical PtrToIntInst. | |||
5101 | PtrToIntInst *cloneImpl() const; | |||
5102 | ||||
5103 | public: | |||
5104 | /// Constructor with insert-before-instruction semantics | |||
5105 | PtrToIntInst( | |||
5106 | Value *S, ///< The value to be converted | |||
5107 | Type *Ty, ///< The type to convert to | |||
5108 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5109 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5110 | ); | |||
5111 | ||||
5112 | /// Constructor with insert-at-end-of-block semantics | |||
5113 | PtrToIntInst( | |||
5114 | Value *S, ///< The value to be converted | |||
5115 | Type *Ty, ///< The type to convert to | |||
5116 | const Twine &NameStr, ///< A name for the new instruction | |||
5117 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5118 | ); | |||
5119 | ||||
5120 | /// Gets the pointer operand. | |||
5121 | Value *getPointerOperand() { return getOperand(0); } | |||
5122 | /// Gets the pointer operand. | |||
5123 | const Value *getPointerOperand() const { return getOperand(0); } | |||
5124 | /// Gets the operand index of the pointer operand. | |||
5125 | static unsigned getPointerOperandIndex() { return 0U; } | |||
5126 | ||||
5127 | /// Returns the address space of the pointer operand. | |||
5128 | unsigned getPointerAddressSpace() const { | |||
5129 | return getPointerOperand()->getType()->getPointerAddressSpace(); | |||
5130 | } | |||
5131 | ||||
5132 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5133 | static bool classof(const Instruction *I) { | |||
5134 | return I->getOpcode() == PtrToInt; | |||
5135 | } | |||
5136 | static bool classof(const Value *V) { | |||
5137 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5138 | } | |||
5139 | }; | |||
5140 | ||||
5141 | //===----------------------------------------------------------------------===// | |||
5142 | // BitCastInst Class | |||
5143 | //===----------------------------------------------------------------------===// | |||
5144 | ||||
5145 | /// This class represents a no-op cast from one type to another. | |||
5146 | class BitCastInst : public CastInst { | |||
5147 | protected: | |||
5148 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5149 | friend class Instruction; | |||
5150 | ||||
5151 | /// Clone an identical BitCastInst. | |||
5152 | BitCastInst *cloneImpl() const; | |||
5153 | ||||
5154 | public: | |||
5155 | /// Constructor with insert-before-instruction semantics | |||
5156 | BitCastInst( | |||
5157 | Value *S, ///< The value to be casted | |||
5158 | Type *Ty, ///< The type to casted to | |||
5159 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5160 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5161 | ); | |||
5162 | ||||
5163 | /// Constructor with insert-at-end-of-block semantics | |||
5164 | BitCastInst( | |||
5165 | Value *S, ///< The value to be casted | |||
5166 | Type *Ty, ///< The type to casted to | |||
5167 | const Twine &NameStr, ///< A name for the new instruction | |||
5168 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5169 | ); | |||
5170 | ||||
5171 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5172 | static bool classof(const Instruction *I) { | |||
5173 | return I->getOpcode() == BitCast; | |||
5174 | } | |||
5175 | static bool classof(const Value *V) { | |||
5176 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5177 | } | |||
5178 | }; | |||
5179 | ||||
5180 | //===----------------------------------------------------------------------===// | |||
5181 | // AddrSpaceCastInst Class | |||
5182 | //===----------------------------------------------------------------------===// | |||
5183 | ||||
5184 | /// This class represents a conversion between pointers from one address space | |||
5185 | /// to another. | |||
5186 | class AddrSpaceCastInst : public CastInst { | |||
5187 | protected: | |||
5188 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5189 | friend class Instruction; | |||
5190 | ||||
5191 | /// Clone an identical AddrSpaceCastInst. | |||
5192 | AddrSpaceCastInst *cloneImpl() const; | |||
5193 | ||||
5194 | public: | |||
5195 | /// Constructor with insert-before-instruction semantics | |||
5196 | AddrSpaceCastInst( | |||
5197 | Value *S, ///< The value to be casted | |||
5198 | Type *Ty, ///< The type to casted to | |||
5199 | const Twine &NameStr = "", ///< A name for the new instruction | |||
5200 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction | |||
5201 | ); | |||
5202 | ||||
5203 | /// Constructor with insert-at-end-of-block semantics | |||
5204 | AddrSpaceCastInst( | |||
5205 | Value *S, ///< The value to be casted | |||
5206 | Type *Ty, ///< The type to casted to | |||
5207 | const Twine &NameStr, ///< A name for the new instruction | |||
5208 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into | |||
5209 | ); | |||
5210 | ||||
5211 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5212 | static bool classof(const Instruction *I) { | |||
5213 | return I->getOpcode() == AddrSpaceCast; | |||
5214 | } | |||
5215 | static bool classof(const Value *V) { | |||
5216 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5217 | } | |||
5218 | ||||
5219 | /// Gets the pointer operand. | |||
5220 | Value *getPointerOperand() { | |||
5221 | return getOperand(0); | |||
5222 | } | |||
5223 | ||||
5224 | /// Gets the pointer operand. | |||
5225 | const Value *getPointerOperand() const { | |||
5226 | return getOperand(0); | |||
5227 | } | |||
5228 | ||||
5229 | /// Gets the operand index of the pointer operand. | |||
5230 | static unsigned getPointerOperandIndex() { | |||
5231 | return 0U; | |||
5232 | } | |||
5233 | ||||
5234 | /// Returns the address space of the pointer operand. | |||
5235 | unsigned getSrcAddressSpace() const { | |||
5236 | return getPointerOperand()->getType()->getPointerAddressSpace(); | |||
5237 | } | |||
5238 | ||||
5239 | /// Returns the address space of the result. | |||
5240 | unsigned getDestAddressSpace() const { | |||
5241 | return getType()->getPointerAddressSpace(); | |||
5242 | } | |||
5243 | }; | |||
5244 | ||||
5245 | /// A helper function that returns the pointer operand of a load or store | |||
5246 | /// instruction. Returns nullptr if not load or store. | |||
5247 | inline const Value *getLoadStorePointerOperand(const Value *V) { | |||
5248 | if (auto *Load = dyn_cast<LoadInst>(V)) | |||
5249 | return Load->getPointerOperand(); | |||
5250 | if (auto *Store = dyn_cast<StoreInst>(V)) | |||
5251 | return Store->getPointerOperand(); | |||
5252 | return nullptr; | |||
5253 | } | |||
5254 | inline Value *getLoadStorePointerOperand(Value *V) { | |||
5255 | return const_cast<Value *>( | |||
5256 | getLoadStorePointerOperand(static_cast<const Value *>(V))); | |||
5257 | } | |||
5258 | ||||
5259 | /// A helper function that returns the pointer operand of a load, store | |||
5260 | /// or GEP instruction. Returns nullptr if not load, store, or GEP. | |||
5261 | inline const Value *getPointerOperand(const Value *V) { | |||
5262 | if (auto *Ptr = getLoadStorePointerOperand(V)) | |||
5263 | return Ptr; | |||
5264 | if (auto *Gep = dyn_cast<GetElementPtrInst>(V)) | |||
5265 | return Gep->getPointerOperand(); | |||
5266 | return nullptr; | |||
5267 | } | |||
5268 | inline Value *getPointerOperand(Value *V) { | |||
5269 | return const_cast<Value *>(getPointerOperand(static_cast<const Value *>(V))); | |||
5270 | } | |||
5271 | ||||
5272 | /// A helper function that returns the alignment of load or store instruction. | |||
5273 | inline Align getLoadStoreAlignment(Value *I) { | |||
5274 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 5275, __PRETTY_FUNCTION__)) | |||
5275 | "Expected Load or Store instruction")(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 5275, __PRETTY_FUNCTION__)); | |||
5276 | if (auto *LI = dyn_cast<LoadInst>(I)) | |||
5277 | return LI->getAlign(); | |||
5278 | return cast<StoreInst>(I)->getAlign(); | |||
5279 | } | |||
5280 | ||||
5281 | /// A helper function that returns the address space of the pointer operand of | |||
5282 | /// load or store instruction. | |||
5283 | inline unsigned getLoadStoreAddressSpace(Value *I) { | |||
5284 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 5285, __PRETTY_FUNCTION__)) | |||
5285 | "Expected Load or Store instruction")(((isa<LoadInst>(I) || isa<StoreInst>(I)) && "Expected Load or Store instruction") ? static_cast<void> (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "/build/llvm-toolchain-snapshot-12~++20201202100633+b276bf5a572/llvm/include/llvm/IR/Instructions.h" , 5285, __PRETTY_FUNCTION__)); | |||
5286 | if (auto *LI = dyn_cast<LoadInst>(I)) | |||
5287 | return LI->getPointerAddressSpace(); | |||
5288 | return cast<StoreInst>(I)->getPointerAddressSpace(); | |||
5289 | } | |||
5290 | ||||
5291 | //===----------------------------------------------------------------------===// | |||
5292 | // FreezeInst Class | |||
5293 | //===----------------------------------------------------------------------===// | |||
5294 | ||||
5295 | /// This class represents a freeze function that returns random concrete | |||
5296 | /// value if an operand is either a poison value or an undef value | |||
5297 | class FreezeInst : public UnaryInstruction { | |||
5298 | protected: | |||
5299 | // Note: Instruction needs to be a friend here to call cloneImpl. | |||
5300 | friend class Instruction; | |||
5301 | ||||
5302 | /// Clone an identical FreezeInst | |||
5303 | FreezeInst *cloneImpl() const; | |||
5304 | ||||
5305 | public: | |||
5306 | explicit FreezeInst(Value *S, | |||
5307 | const Twine &NameStr = "", | |||
5308 | Instruction *InsertBefore = nullptr); | |||
5309 | FreezeInst(Value *S, const Twine &NameStr, BasicBlock *InsertAtEnd); | |||
5310 | ||||
5311 | // Methods for support type inquiry through isa, cast, and dyn_cast: | |||
5312 | static inline bool classof(const Instruction *I) { | |||
5313 | return I->getOpcode() == Freeze; | |||
5314 | } | |||
5315 | static inline bool classof(const Value *V) { | |||
5316 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); | |||
5317 | } | |||
5318 | }; | |||
5319 | ||||
5320 | } // end namespace llvm | |||
5321 | ||||
5322 | #endif // LLVM_IR_INSTRUCTIONS_H |