File: | build/source/llvm/lib/CodeGen/CodeGenPrepare.cpp |
Warning: | line 2474, column 10 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- CodeGenPrepare.cpp - Prepare a function for code generation --------===// | ||||
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 pass munges the code in the input function to better prepare it for | ||||
10 | // SelectionDAG-based code generation. This works around limitations in it's | ||||
11 | // basic-block-at-a-time approach. It should eventually be removed. | ||||
12 | // | ||||
13 | //===----------------------------------------------------------------------===// | ||||
14 | |||||
15 | #include "llvm/ADT/APInt.h" | ||||
16 | #include "llvm/ADT/ArrayRef.h" | ||||
17 | #include "llvm/ADT/DenseMap.h" | ||||
18 | #include "llvm/ADT/MapVector.h" | ||||
19 | #include "llvm/ADT/PointerIntPair.h" | ||||
20 | #include "llvm/ADT/STLExtras.h" | ||||
21 | #include "llvm/ADT/SmallPtrSet.h" | ||||
22 | #include "llvm/ADT/SmallVector.h" | ||||
23 | #include "llvm/ADT/Statistic.h" | ||||
24 | #include "llvm/Analysis/BlockFrequencyInfo.h" | ||||
25 | #include "llvm/Analysis/BranchProbabilityInfo.h" | ||||
26 | #include "llvm/Analysis/InstructionSimplify.h" | ||||
27 | #include "llvm/Analysis/LoopInfo.h" | ||||
28 | #include "llvm/Analysis/ProfileSummaryInfo.h" | ||||
29 | #include "llvm/Analysis/TargetLibraryInfo.h" | ||||
30 | #include "llvm/Analysis/TargetTransformInfo.h" | ||||
31 | #include "llvm/Analysis/ValueTracking.h" | ||||
32 | #include "llvm/Analysis/VectorUtils.h" | ||||
33 | #include "llvm/CodeGen/Analysis.h" | ||||
34 | #include "llvm/CodeGen/BasicBlockSectionsProfileReader.h" | ||||
35 | #include "llvm/CodeGen/ISDOpcodes.h" | ||||
36 | #include "llvm/CodeGen/MachineValueType.h" | ||||
37 | #include "llvm/CodeGen/SelectionDAGNodes.h" | ||||
38 | #include "llvm/CodeGen/TargetLowering.h" | ||||
39 | #include "llvm/CodeGen/TargetPassConfig.h" | ||||
40 | #include "llvm/CodeGen/TargetSubtargetInfo.h" | ||||
41 | #include "llvm/CodeGen/ValueTypes.h" | ||||
42 | #include "llvm/Config/llvm-config.h" | ||||
43 | #include "llvm/IR/Argument.h" | ||||
44 | #include "llvm/IR/Attributes.h" | ||||
45 | #include "llvm/IR/BasicBlock.h" | ||||
46 | #include "llvm/IR/Constant.h" | ||||
47 | #include "llvm/IR/Constants.h" | ||||
48 | #include "llvm/IR/DataLayout.h" | ||||
49 | #include "llvm/IR/DebugInfo.h" | ||||
50 | #include "llvm/IR/DerivedTypes.h" | ||||
51 | #include "llvm/IR/Dominators.h" | ||||
52 | #include "llvm/IR/Function.h" | ||||
53 | #include "llvm/IR/GetElementPtrTypeIterator.h" | ||||
54 | #include "llvm/IR/GlobalValue.h" | ||||
55 | #include "llvm/IR/GlobalVariable.h" | ||||
56 | #include "llvm/IR/IRBuilder.h" | ||||
57 | #include "llvm/IR/InlineAsm.h" | ||||
58 | #include "llvm/IR/InstrTypes.h" | ||||
59 | #include "llvm/IR/Instruction.h" | ||||
60 | #include "llvm/IR/Instructions.h" | ||||
61 | #include "llvm/IR/IntrinsicInst.h" | ||||
62 | #include "llvm/IR/Intrinsics.h" | ||||
63 | #include "llvm/IR/IntrinsicsAArch64.h" | ||||
64 | #include "llvm/IR/LLVMContext.h" | ||||
65 | #include "llvm/IR/MDBuilder.h" | ||||
66 | #include "llvm/IR/Module.h" | ||||
67 | #include "llvm/IR/Operator.h" | ||||
68 | #include "llvm/IR/PatternMatch.h" | ||||
69 | #include "llvm/IR/ProfDataUtils.h" | ||||
70 | #include "llvm/IR/Statepoint.h" | ||||
71 | #include "llvm/IR/Type.h" | ||||
72 | #include "llvm/IR/Use.h" | ||||
73 | #include "llvm/IR/User.h" | ||||
74 | #include "llvm/IR/Value.h" | ||||
75 | #include "llvm/IR/ValueHandle.h" | ||||
76 | #include "llvm/IR/ValueMap.h" | ||||
77 | #include "llvm/InitializePasses.h" | ||||
78 | #include "llvm/Pass.h" | ||||
79 | #include "llvm/Support/BlockFrequency.h" | ||||
80 | #include "llvm/Support/BranchProbability.h" | ||||
81 | #include "llvm/Support/Casting.h" | ||||
82 | #include "llvm/Support/CommandLine.h" | ||||
83 | #include "llvm/Support/Compiler.h" | ||||
84 | #include "llvm/Support/Debug.h" | ||||
85 | #include "llvm/Support/ErrorHandling.h" | ||||
86 | #include "llvm/Support/MathExtras.h" | ||||
87 | #include "llvm/Support/raw_ostream.h" | ||||
88 | #include "llvm/Target/TargetMachine.h" | ||||
89 | #include "llvm/Target/TargetOptions.h" | ||||
90 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" | ||||
91 | #include "llvm/Transforms/Utils/BypassSlowDivision.h" | ||||
92 | #include "llvm/Transforms/Utils/Local.h" | ||||
93 | #include "llvm/Transforms/Utils/SimplifyLibCalls.h" | ||||
94 | #include "llvm/Transforms/Utils/SizeOpts.h" | ||||
95 | #include <algorithm> | ||||
96 | #include <cassert> | ||||
97 | #include <cstdint> | ||||
98 | #include <iterator> | ||||
99 | #include <limits> | ||||
100 | #include <memory> | ||||
101 | #include <optional> | ||||
102 | #include <utility> | ||||
103 | #include <vector> | ||||
104 | |||||
105 | using namespace llvm; | ||||
106 | using namespace llvm::PatternMatch; | ||||
107 | |||||
108 | #define DEBUG_TYPE"codegenprepare" "codegenprepare" | ||||
109 | |||||
110 | STATISTIC(NumBlocksElim, "Number of blocks eliminated")static llvm::Statistic NumBlocksElim = {"codegenprepare", "NumBlocksElim" , "Number of blocks eliminated"}; | ||||
111 | STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated")static llvm::Statistic NumPHIsElim = {"codegenprepare", "NumPHIsElim" , "Number of trivial PHIs eliminated"}; | ||||
112 | STATISTIC(NumGEPsElim, "Number of GEPs converted to casts")static llvm::Statistic NumGEPsElim = {"codegenprepare", "NumGEPsElim" , "Number of GEPs converted to casts"}; | ||||
113 | STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "static llvm::Statistic NumCmpUses = {"codegenprepare", "NumCmpUses" , "Number of uses of Cmp expressions replaced with uses of " "sunken Cmps" } | ||||
114 | "sunken Cmps")static llvm::Statistic NumCmpUses = {"codegenprepare", "NumCmpUses" , "Number of uses of Cmp expressions replaced with uses of " "sunken Cmps" }; | ||||
115 | STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "static llvm::Statistic NumCastUses = {"codegenprepare", "NumCastUses" , "Number of uses of Cast expressions replaced with uses " "of sunken Casts" } | ||||
116 | "of sunken Casts")static llvm::Statistic NumCastUses = {"codegenprepare", "NumCastUses" , "Number of uses of Cast expressions replaced with uses " "of sunken Casts" }; | ||||
117 | STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "static llvm::Statistic NumMemoryInsts = {"codegenprepare", "NumMemoryInsts" , "Number of memory instructions whose address " "computations were sunk" } | ||||
118 | "computations were sunk")static llvm::Statistic NumMemoryInsts = {"codegenprepare", "NumMemoryInsts" , "Number of memory instructions whose address " "computations were sunk" }; | ||||
119 | STATISTIC(NumMemoryInstsPhiCreated,static llvm::Statistic NumMemoryInstsPhiCreated = {"codegenprepare" , "NumMemoryInstsPhiCreated", "Number of phis created when address " "computations were sunk to memory instructions"} | ||||
120 | "Number of phis created when address "static llvm::Statistic NumMemoryInstsPhiCreated = {"codegenprepare" , "NumMemoryInstsPhiCreated", "Number of phis created when address " "computations were sunk to memory instructions"} | ||||
121 | "computations were sunk to memory instructions")static llvm::Statistic NumMemoryInstsPhiCreated = {"codegenprepare" , "NumMemoryInstsPhiCreated", "Number of phis created when address " "computations were sunk to memory instructions"}; | ||||
122 | STATISTIC(NumMemoryInstsSelectCreated,static llvm::Statistic NumMemoryInstsSelectCreated = {"codegenprepare" , "NumMemoryInstsSelectCreated", "Number of select created when address " "computations were sunk to memory instructions"} | ||||
123 | "Number of select created when address "static llvm::Statistic NumMemoryInstsSelectCreated = {"codegenprepare" , "NumMemoryInstsSelectCreated", "Number of select created when address " "computations were sunk to memory instructions"} | ||||
124 | "computations were sunk to memory instructions")static llvm::Statistic NumMemoryInstsSelectCreated = {"codegenprepare" , "NumMemoryInstsSelectCreated", "Number of select created when address " "computations were sunk to memory instructions"}; | ||||
125 | STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads")static llvm::Statistic NumExtsMoved = {"codegenprepare", "NumExtsMoved" , "Number of [s|z]ext instructions combined with loads"}; | ||||
126 | STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized")static llvm::Statistic NumExtUses = {"codegenprepare", "NumExtUses" , "Number of uses of [s|z]ext instructions optimized"}; | ||||
127 | STATISTIC(NumAndsAdded,static llvm::Statistic NumAndsAdded = {"codegenprepare", "NumAndsAdded" , "Number of and mask instructions added to form ext loads"} | ||||
128 | "Number of and mask instructions added to form ext loads")static llvm::Statistic NumAndsAdded = {"codegenprepare", "NumAndsAdded" , "Number of and mask instructions added to form ext loads"}; | ||||
129 | STATISTIC(NumAndUses, "Number of uses of and mask instructions optimized")static llvm::Statistic NumAndUses = {"codegenprepare", "NumAndUses" , "Number of uses of and mask instructions optimized"}; | ||||
130 | STATISTIC(NumRetsDup, "Number of return instructions duplicated")static llvm::Statistic NumRetsDup = {"codegenprepare", "NumRetsDup" , "Number of return instructions duplicated"}; | ||||
131 | STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved")static llvm::Statistic NumDbgValueMoved = {"codegenprepare", "NumDbgValueMoved" , "Number of debug value instructions moved"}; | ||||
132 | STATISTIC(NumSelectsExpanded, "Number of selects turned into branches")static llvm::Statistic NumSelectsExpanded = {"codegenprepare" , "NumSelectsExpanded", "Number of selects turned into branches" }; | ||||
133 | STATISTIC(NumStoreExtractExposed, "Number of store(extractelement) exposed")static llvm::Statistic NumStoreExtractExposed = {"codegenprepare" , "NumStoreExtractExposed", "Number of store(extractelement) exposed" }; | ||||
134 | |||||
135 | static cl::opt<bool> DisableBranchOpts( | ||||
136 | "disable-cgp-branch-opts", cl::Hidden, cl::init(false), | ||||
137 | cl::desc("Disable branch optimizations in CodeGenPrepare")); | ||||
138 | |||||
139 | static cl::opt<bool> | ||||
140 | DisableGCOpts("disable-cgp-gc-opts", cl::Hidden, cl::init(false), | ||||
141 | cl::desc("Disable GC optimizations in CodeGenPrepare")); | ||||
142 | |||||
143 | static cl::opt<bool> | ||||
144 | DisableSelectToBranch("disable-cgp-select2branch", cl::Hidden, | ||||
145 | cl::init(false), | ||||
146 | cl::desc("Disable select to branch conversion.")); | ||||
147 | |||||
148 | static cl::opt<bool> | ||||
149 | AddrSinkUsingGEPs("addr-sink-using-gep", cl::Hidden, cl::init(true), | ||||
150 | cl::desc("Address sinking in CGP using GEPs.")); | ||||
151 | |||||
152 | static cl::opt<bool> | ||||
153 | EnableAndCmpSinking("enable-andcmp-sinking", cl::Hidden, cl::init(true), | ||||
154 | cl::desc("Enable sinkinig and/cmp into branches.")); | ||||
155 | |||||
156 | static cl::opt<bool> DisableStoreExtract( | ||||
157 | "disable-cgp-store-extract", cl::Hidden, cl::init(false), | ||||
158 | cl::desc("Disable store(extract) optimizations in CodeGenPrepare")); | ||||
159 | |||||
160 | static cl::opt<bool> StressStoreExtract( | ||||
161 | "stress-cgp-store-extract", cl::Hidden, cl::init(false), | ||||
162 | cl::desc("Stress test store(extract) optimizations in CodeGenPrepare")); | ||||
163 | |||||
164 | static cl::opt<bool> DisableExtLdPromotion( | ||||
165 | "disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), | ||||
166 | cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in " | ||||
167 | "CodeGenPrepare")); | ||||
168 | |||||
169 | static cl::opt<bool> StressExtLdPromotion( | ||||
170 | "stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), | ||||
171 | cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) " | ||||
172 | "optimization in CodeGenPrepare")); | ||||
173 | |||||
174 | static cl::opt<bool> DisablePreheaderProtect( | ||||
175 | "disable-preheader-prot", cl::Hidden, cl::init(false), | ||||
176 | cl::desc("Disable protection against removing loop preheaders")); | ||||
177 | |||||
178 | static cl::opt<bool> ProfileGuidedSectionPrefix( | ||||
179 | "profile-guided-section-prefix", cl::Hidden, cl::init(true), | ||||
180 | cl::desc("Use profile info to add section prefix for hot/cold functions")); | ||||
181 | |||||
182 | static cl::opt<bool> ProfileUnknownInSpecialSection( | ||||
183 | "profile-unknown-in-special-section", cl::Hidden, | ||||
184 | cl::desc("In profiling mode like sampleFDO, if a function doesn't have " | ||||
185 | "profile, we cannot tell the function is cold for sure because " | ||||
186 | "it may be a function newly added without ever being sampled. " | ||||
187 | "With the flag enabled, compiler can put such profile unknown " | ||||
188 | "functions into a special section, so runtime system can choose " | ||||
189 | "to handle it in a different way than .text section, to save " | ||||
190 | "RAM for example. ")); | ||||
191 | |||||
192 | static cl::opt<bool> BBSectionsGuidedSectionPrefix( | ||||
193 | "bbsections-guided-section-prefix", cl::Hidden, cl::init(true), | ||||
194 | cl::desc("Use the basic-block-sections profile to determine the text " | ||||
195 | "section prefix for hot functions. Functions with " | ||||
196 | "basic-block-sections profile will be placed in `.text.hot` " | ||||
197 | "regardless of their FDO profile info. Other functions won't be " | ||||
198 | "impacted, i.e., their prefixes will be decided by FDO/sampleFDO " | ||||
199 | "profiles.")); | ||||
200 | |||||
201 | static cl::opt<unsigned> FreqRatioToSkipMerge( | ||||
202 | "cgp-freq-ratio-to-skip-merge", cl::Hidden, cl::init(2), | ||||
203 | cl::desc("Skip merging empty blocks if (frequency of empty block) / " | ||||
204 | "(frequency of destination block) is greater than this ratio")); | ||||
205 | |||||
206 | static cl::opt<bool> ForceSplitStore( | ||||
207 | "force-split-store", cl::Hidden, cl::init(false), | ||||
208 | cl::desc("Force store splitting no matter what the target query says.")); | ||||
209 | |||||
210 | static cl::opt<bool> EnableTypePromotionMerge( | ||||
211 | "cgp-type-promotion-merge", cl::Hidden, | ||||
212 | cl::desc("Enable merging of redundant sexts when one is dominating" | ||||
213 | " the other."), | ||||
214 | cl::init(true)); | ||||
215 | |||||
216 | static cl::opt<bool> DisableComplexAddrModes( | ||||
217 | "disable-complex-addr-modes", cl::Hidden, cl::init(false), | ||||
218 | cl::desc("Disables combining addressing modes with different parts " | ||||
219 | "in optimizeMemoryInst.")); | ||||
220 | |||||
221 | static cl::opt<bool> | ||||
222 | AddrSinkNewPhis("addr-sink-new-phis", cl::Hidden, cl::init(false), | ||||
223 | cl::desc("Allow creation of Phis in Address sinking.")); | ||||
224 | |||||
225 | static cl::opt<bool> AddrSinkNewSelects( | ||||
226 | "addr-sink-new-select", cl::Hidden, cl::init(true), | ||||
227 | cl::desc("Allow creation of selects in Address sinking.")); | ||||
228 | |||||
229 | static cl::opt<bool> AddrSinkCombineBaseReg( | ||||
230 | "addr-sink-combine-base-reg", cl::Hidden, cl::init(true), | ||||
231 | cl::desc("Allow combining of BaseReg field in Address sinking.")); | ||||
232 | |||||
233 | static cl::opt<bool> AddrSinkCombineBaseGV( | ||||
234 | "addr-sink-combine-base-gv", cl::Hidden, cl::init(true), | ||||
235 | cl::desc("Allow combining of BaseGV field in Address sinking.")); | ||||
236 | |||||
237 | static cl::opt<bool> AddrSinkCombineBaseOffs( | ||||
238 | "addr-sink-combine-base-offs", cl::Hidden, cl::init(true), | ||||
239 | cl::desc("Allow combining of BaseOffs field in Address sinking.")); | ||||
240 | |||||
241 | static cl::opt<bool> AddrSinkCombineScaledReg( | ||||
242 | "addr-sink-combine-scaled-reg", cl::Hidden, cl::init(true), | ||||
243 | cl::desc("Allow combining of ScaledReg field in Address sinking.")); | ||||
244 | |||||
245 | static cl::opt<bool> | ||||
246 | EnableGEPOffsetSplit("cgp-split-large-offset-gep", cl::Hidden, | ||||
247 | cl::init(true), | ||||
248 | cl::desc("Enable splitting large offset of GEP.")); | ||||
249 | |||||
250 | static cl::opt<bool> EnableICMP_EQToICMP_ST( | ||||
251 | "cgp-icmp-eq2icmp-st", cl::Hidden, cl::init(false), | ||||
252 | cl::desc("Enable ICMP_EQ to ICMP_S(L|G)T conversion.")); | ||||
253 | |||||
254 | static cl::opt<bool> | ||||
255 | VerifyBFIUpdates("cgp-verify-bfi-updates", cl::Hidden, cl::init(false), | ||||
256 | cl::desc("Enable BFI update verification for " | ||||
257 | "CodeGenPrepare.")); | ||||
258 | |||||
259 | static cl::opt<bool> | ||||
260 | OptimizePhiTypes("cgp-optimize-phi-types", cl::Hidden, cl::init(false), | ||||
261 | cl::desc("Enable converting phi types in CodeGenPrepare")); | ||||
262 | |||||
263 | static cl::opt<unsigned> | ||||
264 | HugeFuncThresholdInCGPP("cgpp-huge-func", cl::init(10000), cl::Hidden, | ||||
265 | cl::desc("Least BB number of huge function.")); | ||||
266 | |||||
267 | static cl::opt<unsigned> | ||||
268 | MaxAddressUsersToScan("cgp-max-address-users-to-scan", cl::init(100), | ||||
269 | cl::Hidden, | ||||
270 | cl::desc("Max number of address users to look at")); | ||||
271 | namespace { | ||||
272 | |||||
273 | enum ExtType { | ||||
274 | ZeroExtension, // Zero extension has been seen. | ||||
275 | SignExtension, // Sign extension has been seen. | ||||
276 | BothExtension // This extension type is used if we saw sext after | ||||
277 | // ZeroExtension had been set, or if we saw zext after | ||||
278 | // SignExtension had been set. It makes the type | ||||
279 | // information of a promoted instruction invalid. | ||||
280 | }; | ||||
281 | |||||
282 | enum ModifyDT { | ||||
283 | NotModifyDT, // Not Modify any DT. | ||||
284 | ModifyBBDT, // Modify the Basic Block Dominator Tree. | ||||
285 | ModifyInstDT // Modify the Instruction Dominator in a Basic Block, | ||||
286 | // This usually means we move/delete/insert instruction | ||||
287 | // in a Basic Block. So we should re-iterate instructions | ||||
288 | // in such Basic Block. | ||||
289 | }; | ||||
290 | |||||
291 | using SetOfInstrs = SmallPtrSet<Instruction *, 16>; | ||||
292 | using TypeIsSExt = PointerIntPair<Type *, 2, ExtType>; | ||||
293 | using InstrToOrigTy = DenseMap<Instruction *, TypeIsSExt>; | ||||
294 | using SExts = SmallVector<Instruction *, 16>; | ||||
295 | using ValueToSExts = MapVector<Value *, SExts>; | ||||
296 | |||||
297 | class TypePromotionTransaction; | ||||
298 | |||||
299 | class CodeGenPrepare : public FunctionPass { | ||||
300 | const TargetMachine *TM = nullptr; | ||||
301 | const TargetSubtargetInfo *SubtargetInfo = nullptr; | ||||
302 | const TargetLowering *TLI = nullptr; | ||||
303 | const TargetRegisterInfo *TRI = nullptr; | ||||
304 | const TargetTransformInfo *TTI = nullptr; | ||||
305 | const BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr; | ||||
306 | const TargetLibraryInfo *TLInfo = nullptr; | ||||
307 | const LoopInfo *LI = nullptr; | ||||
308 | std::unique_ptr<BlockFrequencyInfo> BFI; | ||||
309 | std::unique_ptr<BranchProbabilityInfo> BPI; | ||||
310 | ProfileSummaryInfo *PSI = nullptr; | ||||
311 | |||||
312 | /// As we scan instructions optimizing them, this is the next instruction | ||||
313 | /// to optimize. Transforms that can invalidate this should update it. | ||||
314 | BasicBlock::iterator CurInstIterator; | ||||
315 | |||||
316 | /// Keeps track of non-local addresses that have been sunk into a block. | ||||
317 | /// This allows us to avoid inserting duplicate code for blocks with | ||||
318 | /// multiple load/stores of the same address. The usage of WeakTrackingVH | ||||
319 | /// enables SunkAddrs to be treated as a cache whose entries can be | ||||
320 | /// invalidated if a sunken address computation has been erased. | ||||
321 | ValueMap<Value *, WeakTrackingVH> SunkAddrs; | ||||
322 | |||||
323 | /// Keeps track of all instructions inserted for the current function. | ||||
324 | SetOfInstrs InsertedInsts; | ||||
325 | |||||
326 | /// Keeps track of the type of the related instruction before their | ||||
327 | /// promotion for the current function. | ||||
328 | InstrToOrigTy PromotedInsts; | ||||
329 | |||||
330 | /// Keep track of instructions removed during promotion. | ||||
331 | SetOfInstrs RemovedInsts; | ||||
332 | |||||
333 | /// Keep track of sext chains based on their initial value. | ||||
334 | DenseMap<Value *, Instruction *> SeenChainsForSExt; | ||||
335 | |||||
336 | /// Keep track of GEPs accessing the same data structures such as structs or | ||||
337 | /// arrays that are candidates to be split later because of their large | ||||
338 | /// size. | ||||
339 | MapVector<AssertingVH<Value>, | ||||
340 | SmallVector<std::pair<AssertingVH<GetElementPtrInst>, int64_t>, 32>> | ||||
341 | LargeOffsetGEPMap; | ||||
342 | |||||
343 | /// Keep track of new GEP base after splitting the GEPs having large offset. | ||||
344 | SmallSet<AssertingVH<Value>, 2> NewGEPBases; | ||||
345 | |||||
346 | /// Map serial numbers to Large offset GEPs. | ||||
347 | DenseMap<AssertingVH<GetElementPtrInst>, int> LargeOffsetGEPID; | ||||
348 | |||||
349 | /// Keep track of SExt promoted. | ||||
350 | ValueToSExts ValToSExtendedUses; | ||||
351 | |||||
352 | /// True if the function has the OptSize attribute. | ||||
353 | bool OptSize; | ||||
354 | |||||
355 | /// DataLayout for the Function being processed. | ||||
356 | const DataLayout *DL = nullptr; | ||||
357 | |||||
358 | /// Building the dominator tree can be expensive, so we only build it | ||||
359 | /// lazily and update it when required. | ||||
360 | std::unique_ptr<DominatorTree> DT; | ||||
361 | |||||
362 | public: | ||||
363 | /// If encounter huge function, we need to limit the build time. | ||||
364 | bool IsHugeFunc = false; | ||||
365 | |||||
366 | /// FreshBBs is like worklist, it collected the updated BBs which need | ||||
367 | /// to be optimized again. | ||||
368 | /// Note: Consider building time in this pass, when a BB updated, we need | ||||
369 | /// to insert such BB into FreshBBs for huge function. | ||||
370 | SmallSet<BasicBlock *, 32> FreshBBs; | ||||
371 | |||||
372 | static char ID; // Pass identification, replacement for typeid | ||||
373 | |||||
374 | CodeGenPrepare() : FunctionPass(ID) { | ||||
375 | initializeCodeGenPreparePass(*PassRegistry::getPassRegistry()); | ||||
376 | } | ||||
377 | |||||
378 | bool runOnFunction(Function &F) override; | ||||
379 | |||||
380 | StringRef getPassName() const override { return "CodeGen Prepare"; } | ||||
381 | |||||
382 | void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||
383 | // FIXME: When we can selectively preserve passes, preserve the domtree. | ||||
384 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); | ||||
385 | AU.addRequired<TargetLibraryInfoWrapperPass>(); | ||||
386 | AU.addRequired<TargetPassConfig>(); | ||||
387 | AU.addRequired<TargetTransformInfoWrapperPass>(); | ||||
388 | AU.addRequired<LoopInfoWrapperPass>(); | ||||
389 | AU.addUsedIfAvailable<BasicBlockSectionsProfileReader>(); | ||||
390 | } | ||||
391 | |||||
392 | private: | ||||
393 | template <typename F> | ||||
394 | void resetIteratorIfInvalidatedWhileCalling(BasicBlock *BB, F f) { | ||||
395 | // Substituting can cause recursive simplifications, which can invalidate | ||||
396 | // our iterator. Use a WeakTrackingVH to hold onto it in case this | ||||
397 | // happens. | ||||
398 | Value *CurValue = &*CurInstIterator; | ||||
399 | WeakTrackingVH IterHandle(CurValue); | ||||
400 | |||||
401 | f(); | ||||
402 | |||||
403 | // If the iterator instruction was recursively deleted, start over at the | ||||
404 | // start of the block. | ||||
405 | if (IterHandle != CurValue) { | ||||
406 | CurInstIterator = BB->begin(); | ||||
407 | SunkAddrs.clear(); | ||||
408 | } | ||||
409 | } | ||||
410 | |||||
411 | // Get the DominatorTree, building if necessary. | ||||
412 | DominatorTree &getDT(Function &F) { | ||||
413 | if (!DT) | ||||
414 | DT = std::make_unique<DominatorTree>(F); | ||||
415 | return *DT; | ||||
416 | } | ||||
417 | |||||
418 | void removeAllAssertingVHReferences(Value *V); | ||||
419 | bool eliminateAssumptions(Function &F); | ||||
420 | bool eliminateFallThrough(Function &F); | ||||
421 | bool eliminateMostlyEmptyBlocks(Function &F); | ||||
422 | BasicBlock *findDestBlockOfMergeableEmptyBlock(BasicBlock *BB); | ||||
423 | bool canMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const; | ||||
424 | void eliminateMostlyEmptyBlock(BasicBlock *BB); | ||||
425 | bool isMergingEmptyBlockProfitable(BasicBlock *BB, BasicBlock *DestBB, | ||||
426 | bool isPreheader); | ||||
427 | bool makeBitReverse(Instruction &I); | ||||
428 | bool optimizeBlock(BasicBlock &BB, ModifyDT &ModifiedDT); | ||||
429 | bool optimizeInst(Instruction *I, ModifyDT &ModifiedDT); | ||||
430 | bool optimizeMemoryInst(Instruction *MemoryInst, Value *Addr, Type *AccessTy, | ||||
431 | unsigned AddrSpace); | ||||
432 | bool optimizeGatherScatterInst(Instruction *MemoryInst, Value *Ptr); | ||||
433 | bool optimizeInlineAsmInst(CallInst *CS); | ||||
434 | bool optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT); | ||||
435 | bool optimizeExt(Instruction *&I); | ||||
436 | bool optimizeExtUses(Instruction *I); | ||||
437 | bool optimizeLoadExt(LoadInst *Load); | ||||
438 | bool optimizeShiftInst(BinaryOperator *BO); | ||||
439 | bool optimizeFunnelShift(IntrinsicInst *Fsh); | ||||
440 | bool optimizeSelectInst(SelectInst *SI); | ||||
441 | bool optimizeShuffleVectorInst(ShuffleVectorInst *SVI); | ||||
442 | bool optimizeSwitchType(SwitchInst *SI); | ||||
443 | bool optimizeSwitchPhiConstants(SwitchInst *SI); | ||||
444 | bool optimizeSwitchInst(SwitchInst *SI); | ||||
445 | bool optimizeExtractElementInst(Instruction *Inst); | ||||
446 | bool dupRetToEnableTailCallOpts(BasicBlock *BB, ModifyDT &ModifiedDT); | ||||
447 | bool fixupDbgValue(Instruction *I); | ||||
448 | bool placeDbgValues(Function &F); | ||||
449 | bool placePseudoProbes(Function &F); | ||||
450 | bool canFormExtLd(const SmallVectorImpl<Instruction *> &MovedExts, | ||||
451 | LoadInst *&LI, Instruction *&Inst, bool HasPromoted); | ||||
452 | bool tryToPromoteExts(TypePromotionTransaction &TPT, | ||||
453 | const SmallVectorImpl<Instruction *> &Exts, | ||||
454 | SmallVectorImpl<Instruction *> &ProfitablyMovedExts, | ||||
455 | unsigned CreatedInstsCost = 0); | ||||
456 | bool mergeSExts(Function &F); | ||||
457 | bool splitLargeGEPOffsets(); | ||||
458 | bool optimizePhiType(PHINode *Inst, SmallPtrSetImpl<PHINode *> &Visited, | ||||
459 | SmallPtrSetImpl<Instruction *> &DeletedInstrs); | ||||
460 | bool optimizePhiTypes(Function &F); | ||||
461 | bool performAddressTypePromotion( | ||||
462 | Instruction *&Inst, bool AllowPromotionWithoutCommonHeader, | ||||
463 | bool HasPromoted, TypePromotionTransaction &TPT, | ||||
464 | SmallVectorImpl<Instruction *> &SpeculativelyMovedExts); | ||||
465 | bool splitBranchCondition(Function &F, ModifyDT &ModifiedDT); | ||||
466 | bool simplifyOffsetableRelocate(GCStatepointInst &I); | ||||
467 | |||||
468 | bool tryToSinkFreeOperands(Instruction *I); | ||||
469 | bool replaceMathCmpWithIntrinsic(BinaryOperator *BO, Value *Arg0, Value *Arg1, | ||||
470 | CmpInst *Cmp, Intrinsic::ID IID); | ||||
471 | bool optimizeCmp(CmpInst *Cmp, ModifyDT &ModifiedDT); | ||||
472 | bool combineToUSubWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT); | ||||
473 | bool combineToUAddWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT); | ||||
474 | void verifyBFIUpdates(Function &F); | ||||
475 | }; | ||||
476 | |||||
477 | } // end anonymous namespace | ||||
478 | |||||
479 | char CodeGenPrepare::ID = 0; | ||||
480 | |||||
481 | INITIALIZE_PASS_BEGIN(CodeGenPrepare, DEBUG_TYPE,static void *initializeCodeGenPreparePassOnce(PassRegistry & Registry) { | ||||
482 | "Optimize for code generation", false, false)static void *initializeCodeGenPreparePassOnce(PassRegistry & Registry) { | ||||
483 | INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReader)initializeBasicBlockSectionsProfileReaderPass(Registry); | ||||
484 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)initializeLoopInfoWrapperPassPass(Registry); | ||||
485 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)initializeProfileSummaryInfoWrapperPassPass(Registry); | ||||
486 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)initializeTargetLibraryInfoWrapperPassPass(Registry); | ||||
487 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)initializeTargetPassConfigPass(Registry); | ||||
488 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)initializeTargetTransformInfoWrapperPassPass(Registry); | ||||
489 | INITIALIZE_PASS_END(CodeGenPrepare, DEBUG_TYPE, "Optimize for code generation",PassInfo *PI = new PassInfo( "Optimize for code generation", "codegenprepare" , &CodeGenPrepare::ID, PassInfo::NormalCtor_t(callDefaultCtor <CodeGenPrepare>), false, false); Registry.registerPass (*PI, true); return PI; } static llvm::once_flag InitializeCodeGenPreparePassFlag ; void llvm::initializeCodeGenPreparePass(PassRegistry &Registry ) { llvm::call_once(InitializeCodeGenPreparePassFlag, initializeCodeGenPreparePassOnce , std::ref(Registry)); } | ||||
490 | false, false)PassInfo *PI = new PassInfo( "Optimize for code generation", "codegenprepare" , &CodeGenPrepare::ID, PassInfo::NormalCtor_t(callDefaultCtor <CodeGenPrepare>), false, false); Registry.registerPass (*PI, true); return PI; } static llvm::once_flag InitializeCodeGenPreparePassFlag ; void llvm::initializeCodeGenPreparePass(PassRegistry &Registry ) { llvm::call_once(InitializeCodeGenPreparePassFlag, initializeCodeGenPreparePassOnce , std::ref(Registry)); } | ||||
491 | |||||
492 | FunctionPass *llvm::createCodeGenPreparePass() { return new CodeGenPrepare(); } | ||||
493 | |||||
494 | bool CodeGenPrepare::runOnFunction(Function &F) { | ||||
495 | if (skipFunction(F)) | ||||
496 | return false; | ||||
497 | |||||
498 | DL = &F.getParent()->getDataLayout(); | ||||
499 | |||||
500 | bool EverMadeChange = false; | ||||
501 | // Clear per function information. | ||||
502 | InsertedInsts.clear(); | ||||
503 | PromotedInsts.clear(); | ||||
504 | FreshBBs.clear(); | ||||
505 | |||||
506 | TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>(); | ||||
507 | SubtargetInfo = TM->getSubtargetImpl(F); | ||||
508 | TLI = SubtargetInfo->getTargetLowering(); | ||||
509 | TRI = SubtargetInfo->getRegisterInfo(); | ||||
510 | TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); | ||||
511 | TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); | ||||
512 | LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); | ||||
513 | BPI.reset(new BranchProbabilityInfo(F, *LI)); | ||||
514 | BFI.reset(new BlockFrequencyInfo(F, *BPI, *LI)); | ||||
515 | PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); | ||||
516 | BBSectionsProfileReader = | ||||
517 | getAnalysisIfAvailable<BasicBlockSectionsProfileReader>(); | ||||
518 | OptSize = F.hasOptSize(); | ||||
519 | // Use the basic-block-sections profile to promote hot functions to .text.hot | ||||
520 | // if requested. | ||||
521 | if (BBSectionsGuidedSectionPrefix && BBSectionsProfileReader && | ||||
522 | BBSectionsProfileReader->isFunctionHot(F.getName())) { | ||||
523 | F.setSectionPrefix("hot"); | ||||
524 | } else if (ProfileGuidedSectionPrefix) { | ||||
525 | // The hot attribute overwrites profile count based hotness while profile | ||||
526 | // counts based hotness overwrite the cold attribute. | ||||
527 | // This is a conservative behabvior. | ||||
528 | if (F.hasFnAttribute(Attribute::Hot) || | ||||
529 | PSI->isFunctionHotInCallGraph(&F, *BFI)) | ||||
530 | F.setSectionPrefix("hot"); | ||||
531 | // If PSI shows this function is not hot, we will placed the function | ||||
532 | // into unlikely section if (1) PSI shows this is a cold function, or | ||||
533 | // (2) the function has a attribute of cold. | ||||
534 | else if (PSI->isFunctionColdInCallGraph(&F, *BFI) || | ||||
535 | F.hasFnAttribute(Attribute::Cold)) | ||||
536 | F.setSectionPrefix("unlikely"); | ||||
537 | else if (ProfileUnknownInSpecialSection && PSI->hasPartialSampleProfile() && | ||||
538 | PSI->isFunctionHotnessUnknown(F)) | ||||
539 | F.setSectionPrefix("unknown"); | ||||
540 | } | ||||
541 | |||||
542 | /// This optimization identifies DIV instructions that can be | ||||
543 | /// profitably bypassed and carried out with a shorter, faster divide. | ||||
544 | if (!OptSize && !PSI->hasHugeWorkingSetSize() && TLI->isSlowDivBypassed()) { | ||||
545 | const DenseMap<unsigned int, unsigned int> &BypassWidths = | ||||
546 | TLI->getBypassSlowDivWidths(); | ||||
547 | BasicBlock *BB = &*F.begin(); | ||||
548 | while (BB != nullptr) { | ||||
549 | // bypassSlowDivision may create new BBs, but we don't want to reapply the | ||||
550 | // optimization to those blocks. | ||||
551 | BasicBlock *Next = BB->getNextNode(); | ||||
552 | // F.hasOptSize is already checked in the outer if statement. | ||||
553 | if (!llvm::shouldOptimizeForSize(BB, PSI, BFI.get())) | ||||
554 | EverMadeChange |= bypassSlowDivision(BB, BypassWidths); | ||||
555 | BB = Next; | ||||
556 | } | ||||
557 | } | ||||
558 | |||||
559 | // Get rid of @llvm.assume builtins before attempting to eliminate empty | ||||
560 | // blocks, since there might be blocks that only contain @llvm.assume calls | ||||
561 | // (plus arguments that we can get rid of). | ||||
562 | EverMadeChange |= eliminateAssumptions(F); | ||||
563 | |||||
564 | // Eliminate blocks that contain only PHI nodes and an | ||||
565 | // unconditional branch. | ||||
566 | EverMadeChange |= eliminateMostlyEmptyBlocks(F); | ||||
567 | |||||
568 | ModifyDT ModifiedDT = ModifyDT::NotModifyDT; | ||||
569 | if (!DisableBranchOpts) | ||||
570 | EverMadeChange |= splitBranchCondition(F, ModifiedDT); | ||||
571 | |||||
572 | // Split some critical edges where one of the sources is an indirect branch, | ||||
573 | // to help generate sane code for PHIs involving such edges. | ||||
574 | EverMadeChange |= | ||||
575 | SplitIndirectBrCriticalEdges(F, /*IgnoreBlocksWithoutPHI=*/true); | ||||
576 | |||||
577 | // If we are optimzing huge function, we need to consider the build time. | ||||
578 | // Because the basic algorithm's complex is near O(N!). | ||||
579 | IsHugeFunc = F.size() > HugeFuncThresholdInCGPP; | ||||
580 | |||||
581 | bool MadeChange = true; | ||||
582 | bool FuncIterated = false; | ||||
583 | while (MadeChange) { | ||||
584 | MadeChange = false; | ||||
585 | DT.reset(); | ||||
586 | |||||
587 | for (BasicBlock &BB : llvm::make_early_inc_range(F)) { | ||||
588 | if (FuncIterated && !FreshBBs.contains(&BB)) | ||||
589 | continue; | ||||
590 | |||||
591 | ModifyDT ModifiedDTOnIteration = ModifyDT::NotModifyDT; | ||||
592 | bool Changed = optimizeBlock(BB, ModifiedDTOnIteration); | ||||
593 | |||||
594 | MadeChange |= Changed; | ||||
595 | if (IsHugeFunc) { | ||||
596 | // If the BB is updated, it may still has chance to be optimized. | ||||
597 | // This usually happen at sink optimization. | ||||
598 | // For example: | ||||
599 | // | ||||
600 | // bb0: | ||||
601 | // %and = and i32 %a, 4 | ||||
602 | // %cmp = icmp eq i32 %and, 0 | ||||
603 | // | ||||
604 | // If the %cmp sink to other BB, the %and will has chance to sink. | ||||
605 | if (Changed) | ||||
606 | FreshBBs.insert(&BB); | ||||
607 | else if (FuncIterated) | ||||
608 | FreshBBs.erase(&BB); | ||||
609 | |||||
610 | if (ModifiedDTOnIteration == ModifyDT::ModifyBBDT) | ||||
611 | DT.reset(); | ||||
612 | } else { | ||||
613 | // For small/normal functions, we restart BB iteration if the dominator | ||||
614 | // tree of the Function was changed. | ||||
615 | if (ModifiedDTOnIteration != ModifyDT::NotModifyDT) | ||||
616 | break; | ||||
617 | } | ||||
618 | } | ||||
619 | // We have iterated all the BB in the (only work for huge) function. | ||||
620 | FuncIterated = IsHugeFunc; | ||||
621 | |||||
622 | if (EnableTypePromotionMerge && !ValToSExtendedUses.empty()) | ||||
623 | MadeChange |= mergeSExts(F); | ||||
624 | if (!LargeOffsetGEPMap.empty()) | ||||
625 | MadeChange |= splitLargeGEPOffsets(); | ||||
626 | MadeChange |= optimizePhiTypes(F); | ||||
627 | |||||
628 | if (MadeChange) | ||||
629 | eliminateFallThrough(F); | ||||
630 | |||||
631 | // Really free removed instructions during promotion. | ||||
632 | for (Instruction *I : RemovedInsts) | ||||
633 | I->deleteValue(); | ||||
634 | |||||
635 | EverMadeChange |= MadeChange; | ||||
636 | SeenChainsForSExt.clear(); | ||||
637 | ValToSExtendedUses.clear(); | ||||
638 | RemovedInsts.clear(); | ||||
639 | LargeOffsetGEPMap.clear(); | ||||
640 | LargeOffsetGEPID.clear(); | ||||
641 | } | ||||
642 | |||||
643 | NewGEPBases.clear(); | ||||
644 | SunkAddrs.clear(); | ||||
645 | |||||
646 | if (!DisableBranchOpts) { | ||||
647 | MadeChange = false; | ||||
648 | // Use a set vector to get deterministic iteration order. The order the | ||||
649 | // blocks are removed may affect whether or not PHI nodes in successors | ||||
650 | // are removed. | ||||
651 | SmallSetVector<BasicBlock *, 8> WorkList; | ||||
652 | for (BasicBlock &BB : F) { | ||||
653 | SmallVector<BasicBlock *, 2> Successors(successors(&BB)); | ||||
654 | MadeChange |= ConstantFoldTerminator(&BB, true); | ||||
655 | if (!MadeChange) | ||||
656 | continue; | ||||
657 | |||||
658 | for (BasicBlock *Succ : Successors) | ||||
659 | if (pred_empty(Succ)) | ||||
660 | WorkList.insert(Succ); | ||||
661 | } | ||||
662 | |||||
663 | // Delete the dead blocks and any of their dead successors. | ||||
664 | MadeChange |= !WorkList.empty(); | ||||
665 | while (!WorkList.empty()) { | ||||
666 | BasicBlock *BB = WorkList.pop_back_val(); | ||||
667 | SmallVector<BasicBlock *, 2> Successors(successors(BB)); | ||||
668 | |||||
669 | DeleteDeadBlock(BB); | ||||
670 | |||||
671 | for (BasicBlock *Succ : Successors) | ||||
672 | if (pred_empty(Succ)) | ||||
673 | WorkList.insert(Succ); | ||||
674 | } | ||||
675 | |||||
676 | // Merge pairs of basic blocks with unconditional branches, connected by | ||||
677 | // a single edge. | ||||
678 | if (EverMadeChange || MadeChange) | ||||
679 | MadeChange |= eliminateFallThrough(F); | ||||
680 | |||||
681 | EverMadeChange |= MadeChange; | ||||
682 | } | ||||
683 | |||||
684 | if (!DisableGCOpts) { | ||||
685 | SmallVector<GCStatepointInst *, 2> Statepoints; | ||||
686 | for (BasicBlock &BB : F) | ||||
687 | for (Instruction &I : BB) | ||||
688 | if (auto *SP = dyn_cast<GCStatepointInst>(&I)) | ||||
689 | Statepoints.push_back(SP); | ||||
690 | for (auto &I : Statepoints) | ||||
691 | EverMadeChange |= simplifyOffsetableRelocate(*I); | ||||
692 | } | ||||
693 | |||||
694 | // Do this last to clean up use-before-def scenarios introduced by other | ||||
695 | // preparatory transforms. | ||||
696 | EverMadeChange |= placeDbgValues(F); | ||||
697 | EverMadeChange |= placePseudoProbes(F); | ||||
698 | |||||
699 | #ifndef NDEBUG | ||||
700 | if (VerifyBFIUpdates) | ||||
701 | verifyBFIUpdates(F); | ||||
702 | #endif | ||||
703 | |||||
704 | return EverMadeChange; | ||||
705 | } | ||||
706 | |||||
707 | bool CodeGenPrepare::eliminateAssumptions(Function &F) { | ||||
708 | bool MadeChange = false; | ||||
709 | for (BasicBlock &BB : F) { | ||||
710 | CurInstIterator = BB.begin(); | ||||
711 | while (CurInstIterator != BB.end()) { | ||||
712 | Instruction *I = &*(CurInstIterator++); | ||||
713 | if (auto *Assume = dyn_cast<AssumeInst>(I)) { | ||||
714 | MadeChange = true; | ||||
715 | Value *Operand = Assume->getOperand(0); | ||||
716 | Assume->eraseFromParent(); | ||||
717 | |||||
718 | resetIteratorIfInvalidatedWhileCalling(&BB, [&]() { | ||||
719 | RecursivelyDeleteTriviallyDeadInstructions(Operand, TLInfo, nullptr); | ||||
720 | }); | ||||
721 | } | ||||
722 | } | ||||
723 | } | ||||
724 | return MadeChange; | ||||
725 | } | ||||
726 | |||||
727 | /// An instruction is about to be deleted, so remove all references to it in our | ||||
728 | /// GEP-tracking data strcutures. | ||||
729 | void CodeGenPrepare::removeAllAssertingVHReferences(Value *V) { | ||||
730 | LargeOffsetGEPMap.erase(V); | ||||
731 | NewGEPBases.erase(V); | ||||
732 | |||||
733 | auto GEP = dyn_cast<GetElementPtrInst>(V); | ||||
734 | if (!GEP) | ||||
735 | return; | ||||
736 | |||||
737 | LargeOffsetGEPID.erase(GEP); | ||||
738 | |||||
739 | auto VecI = LargeOffsetGEPMap.find(GEP->getPointerOperand()); | ||||
740 | if (VecI == LargeOffsetGEPMap.end()) | ||||
741 | return; | ||||
742 | |||||
743 | auto &GEPVector = VecI->second; | ||||
744 | llvm::erase_if(GEPVector, [=](auto &Elt) { return Elt.first == GEP; }); | ||||
745 | |||||
746 | if (GEPVector.empty()) | ||||
747 | LargeOffsetGEPMap.erase(VecI); | ||||
748 | } | ||||
749 | |||||
750 | // Verify BFI has been updated correctly by recomputing BFI and comparing them. | ||||
751 | void LLVM_ATTRIBUTE_UNUSED__attribute__((__unused__)) CodeGenPrepare::verifyBFIUpdates(Function &F) { | ||||
752 | DominatorTree NewDT(F); | ||||
753 | LoopInfo NewLI(NewDT); | ||||
754 | BranchProbabilityInfo NewBPI(F, NewLI, TLInfo); | ||||
755 | BlockFrequencyInfo NewBFI(F, NewBPI, NewLI); | ||||
756 | NewBFI.verifyMatch(*BFI); | ||||
757 | } | ||||
758 | |||||
759 | /// Merge basic blocks which are connected by a single edge, where one of the | ||||
760 | /// basic blocks has a single successor pointing to the other basic block, | ||||
761 | /// which has a single predecessor. | ||||
762 | bool CodeGenPrepare::eliminateFallThrough(Function &F) { | ||||
763 | bool Changed = false; | ||||
764 | // Scan all of the blocks in the function, except for the entry block. | ||||
765 | // Use a temporary array to avoid iterator being invalidated when | ||||
766 | // deleting blocks. | ||||
767 | SmallVector<WeakTrackingVH, 16> Blocks; | ||||
768 | for (auto &Block : llvm::drop_begin(F)) | ||||
769 | Blocks.push_back(&Block); | ||||
770 | |||||
771 | SmallSet<WeakTrackingVH, 16> Preds; | ||||
772 | for (auto &Block : Blocks) { | ||||
773 | auto *BB = cast_or_null<BasicBlock>(Block); | ||||
774 | if (!BB) | ||||
775 | continue; | ||||
776 | // If the destination block has a single pred, then this is a trivial | ||||
777 | // edge, just collapse it. | ||||
778 | BasicBlock *SinglePred = BB->getSinglePredecessor(); | ||||
779 | |||||
780 | // Don't merge if BB's address is taken. | ||||
781 | if (!SinglePred || SinglePred == BB || BB->hasAddressTaken()) | ||||
782 | continue; | ||||
783 | |||||
784 | BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator()); | ||||
785 | if (Term && !Term->isConditional()) { | ||||
786 | Changed = true; | ||||
787 | LLVM_DEBUG(dbgs() << "To merge:\n" << *BB << "\n\n\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "To merge:\n" << * BB << "\n\n\n"; } } while (false); | ||||
788 | |||||
789 | // Merge BB into SinglePred and delete it. | ||||
790 | MergeBlockIntoPredecessor(BB); | ||||
791 | Preds.insert(SinglePred); | ||||
792 | |||||
793 | if (IsHugeFunc) { | ||||
794 | // Update FreshBBs to optimize the merged BB. | ||||
795 | FreshBBs.insert(SinglePred); | ||||
796 | FreshBBs.erase(BB); | ||||
797 | } | ||||
798 | } | ||||
799 | } | ||||
800 | |||||
801 | // (Repeatedly) merging blocks into their predecessors can create redundant | ||||
802 | // debug intrinsics. | ||||
803 | for (const auto &Pred : Preds) | ||||
804 | if (auto *BB = cast_or_null<BasicBlock>(Pred)) | ||||
805 | RemoveRedundantDbgInstrs(BB); | ||||
806 | |||||
807 | return Changed; | ||||
808 | } | ||||
809 | |||||
810 | /// Find a destination block from BB if BB is mergeable empty block. | ||||
811 | BasicBlock *CodeGenPrepare::findDestBlockOfMergeableEmptyBlock(BasicBlock *BB) { | ||||
812 | // If this block doesn't end with an uncond branch, ignore it. | ||||
813 | BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator()); | ||||
814 | if (!BI || !BI->isUnconditional()) | ||||
815 | return nullptr; | ||||
816 | |||||
817 | // If the instruction before the branch (skipping debug info) isn't a phi | ||||
818 | // node, then other stuff is happening here. | ||||
819 | BasicBlock::iterator BBI = BI->getIterator(); | ||||
820 | if (BBI != BB->begin()) { | ||||
821 | --BBI; | ||||
822 | while (isa<DbgInfoIntrinsic>(BBI)) { | ||||
823 | if (BBI == BB->begin()) | ||||
824 | break; | ||||
825 | --BBI; | ||||
826 | } | ||||
827 | if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI)) | ||||
828 | return nullptr; | ||||
829 | } | ||||
830 | |||||
831 | // Do not break infinite loops. | ||||
832 | BasicBlock *DestBB = BI->getSuccessor(0); | ||||
833 | if (DestBB == BB) | ||||
834 | return nullptr; | ||||
835 | |||||
836 | if (!canMergeBlocks(BB, DestBB)) | ||||
837 | DestBB = nullptr; | ||||
838 | |||||
839 | return DestBB; | ||||
840 | } | ||||
841 | |||||
842 | /// Eliminate blocks that contain only PHI nodes, debug info directives, and an | ||||
843 | /// unconditional branch. Passes before isel (e.g. LSR/loopsimplify) often split | ||||
844 | /// edges in ways that are non-optimal for isel. Start by eliminating these | ||||
845 | /// blocks so we can split them the way we want them. | ||||
846 | bool CodeGenPrepare::eliminateMostlyEmptyBlocks(Function &F) { | ||||
847 | SmallPtrSet<BasicBlock *, 16> Preheaders; | ||||
848 | SmallVector<Loop *, 16> LoopList(LI->begin(), LI->end()); | ||||
849 | while (!LoopList.empty()) { | ||||
850 | Loop *L = LoopList.pop_back_val(); | ||||
851 | llvm::append_range(LoopList, *L); | ||||
852 | if (BasicBlock *Preheader = L->getLoopPreheader()) | ||||
853 | Preheaders.insert(Preheader); | ||||
854 | } | ||||
855 | |||||
856 | bool MadeChange = false; | ||||
857 | // Copy blocks into a temporary array to avoid iterator invalidation issues | ||||
858 | // as we remove them. | ||||
859 | // Note that this intentionally skips the entry block. | ||||
860 | SmallVector<WeakTrackingVH, 16> Blocks; | ||||
861 | for (auto &Block : llvm::drop_begin(F)) | ||||
862 | Blocks.push_back(&Block); | ||||
863 | |||||
864 | for (auto &Block : Blocks) { | ||||
865 | BasicBlock *BB = cast_or_null<BasicBlock>(Block); | ||||
866 | if (!BB) | ||||
867 | continue; | ||||
868 | BasicBlock *DestBB = findDestBlockOfMergeableEmptyBlock(BB); | ||||
869 | if (!DestBB || | ||||
870 | !isMergingEmptyBlockProfitable(BB, DestBB, Preheaders.count(BB))) | ||||
871 | continue; | ||||
872 | |||||
873 | eliminateMostlyEmptyBlock(BB); | ||||
874 | MadeChange = true; | ||||
875 | } | ||||
876 | return MadeChange; | ||||
877 | } | ||||
878 | |||||
879 | bool CodeGenPrepare::isMergingEmptyBlockProfitable(BasicBlock *BB, | ||||
880 | BasicBlock *DestBB, | ||||
881 | bool isPreheader) { | ||||
882 | // Do not delete loop preheaders if doing so would create a critical edge. | ||||
883 | // Loop preheaders can be good locations to spill registers. If the | ||||
884 | // preheader is deleted and we create a critical edge, registers may be | ||||
885 | // spilled in the loop body instead. | ||||
886 | if (!DisablePreheaderProtect && isPreheader && | ||||
887 | !(BB->getSinglePredecessor() && | ||||
888 | BB->getSinglePredecessor()->getSingleSuccessor())) | ||||
889 | return false; | ||||
890 | |||||
891 | // Skip merging if the block's successor is also a successor to any callbr | ||||
892 | // that leads to this block. | ||||
893 | // FIXME: Is this really needed? Is this a correctness issue? | ||||
894 | for (BasicBlock *Pred : predecessors(BB)) { | ||||
895 | if (auto *CBI = dyn_cast<CallBrInst>((Pred)->getTerminator())) | ||||
896 | for (unsigned i = 0, e = CBI->getNumSuccessors(); i != e; ++i) | ||||
897 | if (DestBB == CBI->getSuccessor(i)) | ||||
898 | return false; | ||||
899 | } | ||||
900 | |||||
901 | // Try to skip merging if the unique predecessor of BB is terminated by a | ||||
902 | // switch or indirect branch instruction, and BB is used as an incoming block | ||||
903 | // of PHIs in DestBB. In such case, merging BB and DestBB would cause ISel to | ||||
904 | // add COPY instructions in the predecessor of BB instead of BB (if it is not | ||||
905 | // merged). Note that the critical edge created by merging such blocks wont be | ||||
906 | // split in MachineSink because the jump table is not analyzable. By keeping | ||||
907 | // such empty block (BB), ISel will place COPY instructions in BB, not in the | ||||
908 | // predecessor of BB. | ||||
909 | BasicBlock *Pred = BB->getUniquePredecessor(); | ||||
910 | if (!Pred || !(isa<SwitchInst>(Pred->getTerminator()) || | ||||
911 | isa<IndirectBrInst>(Pred->getTerminator()))) | ||||
912 | return true; | ||||
913 | |||||
914 | if (BB->getTerminator() != BB->getFirstNonPHIOrDbg()) | ||||
915 | return true; | ||||
916 | |||||
917 | // We use a simple cost heuristic which determine skipping merging is | ||||
918 | // profitable if the cost of skipping merging is less than the cost of | ||||
919 | // merging : Cost(skipping merging) < Cost(merging BB), where the | ||||
920 | // Cost(skipping merging) is Freq(BB) * (Cost(Copy) + Cost(Branch)), and | ||||
921 | // the Cost(merging BB) is Freq(Pred) * Cost(Copy). | ||||
922 | // Assuming Cost(Copy) == Cost(Branch), we could simplify it to : | ||||
923 | // Freq(Pred) / Freq(BB) > 2. | ||||
924 | // Note that if there are multiple empty blocks sharing the same incoming | ||||
925 | // value for the PHIs in the DestBB, we consider them together. In such | ||||
926 | // case, Cost(merging BB) will be the sum of their frequencies. | ||||
927 | |||||
928 | if (!isa<PHINode>(DestBB->begin())) | ||||
929 | return true; | ||||
930 | |||||
931 | SmallPtrSet<BasicBlock *, 16> SameIncomingValueBBs; | ||||
932 | |||||
933 | // Find all other incoming blocks from which incoming values of all PHIs in | ||||
934 | // DestBB are the same as the ones from BB. | ||||
935 | for (BasicBlock *DestBBPred : predecessors(DestBB)) { | ||||
936 | if (DestBBPred == BB) | ||||
937 | continue; | ||||
938 | |||||
939 | if (llvm::all_of(DestBB->phis(), [&](const PHINode &DestPN) { | ||||
940 | return DestPN.getIncomingValueForBlock(BB) == | ||||
941 | DestPN.getIncomingValueForBlock(DestBBPred); | ||||
942 | })) | ||||
943 | SameIncomingValueBBs.insert(DestBBPred); | ||||
944 | } | ||||
945 | |||||
946 | // See if all BB's incoming values are same as the value from Pred. In this | ||||
947 | // case, no reason to skip merging because COPYs are expected to be place in | ||||
948 | // Pred already. | ||||
949 | if (SameIncomingValueBBs.count(Pred)) | ||||
950 | return true; | ||||
951 | |||||
952 | BlockFrequency PredFreq = BFI->getBlockFreq(Pred); | ||||
953 | BlockFrequency BBFreq = BFI->getBlockFreq(BB); | ||||
954 | |||||
955 | for (auto *SameValueBB : SameIncomingValueBBs) | ||||
956 | if (SameValueBB->getUniquePredecessor() == Pred && | ||||
957 | DestBB == findDestBlockOfMergeableEmptyBlock(SameValueBB)) | ||||
958 | BBFreq += BFI->getBlockFreq(SameValueBB); | ||||
959 | |||||
960 | return PredFreq.getFrequency() <= | ||||
961 | BBFreq.getFrequency() * FreqRatioToSkipMerge; | ||||
962 | } | ||||
963 | |||||
964 | /// Return true if we can merge BB into DestBB if there is a single | ||||
965 | /// unconditional branch between them, and BB contains no other non-phi | ||||
966 | /// instructions. | ||||
967 | bool CodeGenPrepare::canMergeBlocks(const BasicBlock *BB, | ||||
968 | const BasicBlock *DestBB) const { | ||||
969 | // We only want to eliminate blocks whose phi nodes are used by phi nodes in | ||||
970 | // the successor. If there are more complex condition (e.g. preheaders), | ||||
971 | // don't mess around with them. | ||||
972 | for (const PHINode &PN : BB->phis()) { | ||||
973 | for (const User *U : PN.users()) { | ||||
974 | const Instruction *UI = cast<Instruction>(U); | ||||
975 | if (UI->getParent() != DestBB || !isa<PHINode>(UI)) | ||||
976 | return false; | ||||
977 | // If User is inside DestBB block and it is a PHINode then check | ||||
978 | // incoming value. If incoming value is not from BB then this is | ||||
979 | // a complex condition (e.g. preheaders) we want to avoid here. | ||||
980 | if (UI->getParent() == DestBB) { | ||||
981 | if (const PHINode *UPN = dyn_cast<PHINode>(UI)) | ||||
982 | for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) { | ||||
983 | Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I)); | ||||
984 | if (Insn && Insn->getParent() == BB && | ||||
985 | Insn->getParent() != UPN->getIncomingBlock(I)) | ||||
986 | return false; | ||||
987 | } | ||||
988 | } | ||||
989 | } | ||||
990 | } | ||||
991 | |||||
992 | // If BB and DestBB contain any common predecessors, then the phi nodes in BB | ||||
993 | // and DestBB may have conflicting incoming values for the block. If so, we | ||||
994 | // can't merge the block. | ||||
995 | const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin()); | ||||
996 | if (!DestBBPN) | ||||
997 | return true; // no conflict. | ||||
998 | |||||
999 | // Collect the preds of BB. | ||||
1000 | SmallPtrSet<const BasicBlock *, 16> BBPreds; | ||||
1001 | if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { | ||||
1002 | // It is faster to get preds from a PHI than with pred_iterator. | ||||
1003 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) | ||||
1004 | BBPreds.insert(BBPN->getIncomingBlock(i)); | ||||
1005 | } else { | ||||
1006 | BBPreds.insert(pred_begin(BB), pred_end(BB)); | ||||
1007 | } | ||||
1008 | |||||
1009 | // Walk the preds of DestBB. | ||||
1010 | for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) { | ||||
1011 | BasicBlock *Pred = DestBBPN->getIncomingBlock(i); | ||||
1012 | if (BBPreds.count(Pred)) { // Common predecessor? | ||||
1013 | for (const PHINode &PN : DestBB->phis()) { | ||||
1014 | const Value *V1 = PN.getIncomingValueForBlock(Pred); | ||||
1015 | const Value *V2 = PN.getIncomingValueForBlock(BB); | ||||
1016 | |||||
1017 | // If V2 is a phi node in BB, look up what the mapped value will be. | ||||
1018 | if (const PHINode *V2PN = dyn_cast<PHINode>(V2)) | ||||
1019 | if (V2PN->getParent() == BB) | ||||
1020 | V2 = V2PN->getIncomingValueForBlock(Pred); | ||||
1021 | |||||
1022 | // If there is a conflict, bail out. | ||||
1023 | if (V1 != V2) | ||||
1024 | return false; | ||||
1025 | } | ||||
1026 | } | ||||
1027 | } | ||||
1028 | |||||
1029 | return true; | ||||
1030 | } | ||||
1031 | |||||
1032 | /// Replace all old uses with new ones, and push the updated BBs into FreshBBs. | ||||
1033 | static void replaceAllUsesWith(Value *Old, Value *New, | ||||
1034 | SmallSet<BasicBlock *, 32> &FreshBBs, | ||||
1035 | bool IsHuge) { | ||||
1036 | auto *OldI = dyn_cast<Instruction>(Old); | ||||
1037 | if (OldI) { | ||||
1038 | for (Value::user_iterator UI = OldI->user_begin(), E = OldI->user_end(); | ||||
1039 | UI != E; ++UI) { | ||||
1040 | Instruction *User = cast<Instruction>(*UI); | ||||
1041 | if (IsHuge) | ||||
1042 | FreshBBs.insert(User->getParent()); | ||||
1043 | } | ||||
1044 | } | ||||
1045 | Old->replaceAllUsesWith(New); | ||||
1046 | } | ||||
1047 | |||||
1048 | /// Eliminate a basic block that has only phi's and an unconditional branch in | ||||
1049 | /// it. | ||||
1050 | void CodeGenPrepare::eliminateMostlyEmptyBlock(BasicBlock *BB) { | ||||
1051 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); | ||||
1052 | BasicBlock *DestBB = BI->getSuccessor(0); | ||||
1053 | |||||
1054 | LLVM_DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB; } } while (false) | ||||
1055 | << *BB << *DestBB)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB; } } while (false); | ||||
1056 | |||||
1057 | // If the destination block has a single pred, then this is a trivial edge, | ||||
1058 | // just collapse it. | ||||
1059 | if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) { | ||||
1060 | if (SinglePred != DestBB) { | ||||
1061 | assert(SinglePred == BB &&(static_cast <bool> (SinglePred == BB && "Single predecessor not the same as predecessor" ) ? void (0) : __assert_fail ("SinglePred == BB && \"Single predecessor not the same as predecessor\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1062, __extension__ __PRETTY_FUNCTION__ )) | ||||
1062 | "Single predecessor not the same as predecessor")(static_cast <bool> (SinglePred == BB && "Single predecessor not the same as predecessor" ) ? void (0) : __assert_fail ("SinglePred == BB && \"Single predecessor not the same as predecessor\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1062, __extension__ __PRETTY_FUNCTION__ )); | ||||
1063 | // Merge DestBB into SinglePred/BB and delete it. | ||||
1064 | MergeBlockIntoPredecessor(DestBB); | ||||
1065 | // Note: BB(=SinglePred) will not be deleted on this path. | ||||
1066 | // DestBB(=its single successor) is the one that was deleted. | ||||
1067 | LLVM_DEBUG(dbgs() << "AFTER:\n" << *SinglePred << "\n\n\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "AFTER:\n" << *SinglePred << "\n\n\n"; } } while (false); | ||||
1068 | |||||
1069 | if (IsHugeFunc) { | ||||
1070 | // Update FreshBBs to optimize the merged BB. | ||||
1071 | FreshBBs.insert(SinglePred); | ||||
1072 | FreshBBs.erase(DestBB); | ||||
1073 | } | ||||
1074 | return; | ||||
1075 | } | ||||
1076 | } | ||||
1077 | |||||
1078 | // Otherwise, we have multiple predecessors of BB. Update the PHIs in DestBB | ||||
1079 | // to handle the new incoming edges it is about to have. | ||||
1080 | for (PHINode &PN : DestBB->phis()) { | ||||
1081 | // Remove the incoming value for BB, and remember it. | ||||
1082 | Value *InVal = PN.removeIncomingValue(BB, false); | ||||
1083 | |||||
1084 | // Two options: either the InVal is a phi node defined in BB or it is some | ||||
1085 | // value that dominates BB. | ||||
1086 | PHINode *InValPhi = dyn_cast<PHINode>(InVal); | ||||
1087 | if (InValPhi && InValPhi->getParent() == BB) { | ||||
1088 | // Add all of the input values of the input PHI as inputs of this phi. | ||||
1089 | for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i) | ||||
1090 | PN.addIncoming(InValPhi->getIncomingValue(i), | ||||
1091 | InValPhi->getIncomingBlock(i)); | ||||
1092 | } else { | ||||
1093 | // Otherwise, add one instance of the dominating value for each edge that | ||||
1094 | // we will be adding. | ||||
1095 | if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) { | ||||
1096 | for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i) | ||||
1097 | PN.addIncoming(InVal, BBPN->getIncomingBlock(i)); | ||||
1098 | } else { | ||||
1099 | for (BasicBlock *Pred : predecessors(BB)) | ||||
1100 | PN.addIncoming(InVal, Pred); | ||||
1101 | } | ||||
1102 | } | ||||
1103 | } | ||||
1104 | |||||
1105 | // The PHIs are now updated, change everything that refers to BB to use | ||||
1106 | // DestBB and remove BB. | ||||
1107 | BB->replaceAllUsesWith(DestBB); | ||||
1108 | BB->eraseFromParent(); | ||||
1109 | ++NumBlocksElim; | ||||
1110 | |||||
1111 | LLVM_DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "AFTER:\n" << *DestBB << "\n\n\n"; } } while (false); | ||||
1112 | } | ||||
1113 | |||||
1114 | // Computes a map of base pointer relocation instructions to corresponding | ||||
1115 | // derived pointer relocation instructions given a vector of all relocate calls | ||||
1116 | static void computeBaseDerivedRelocateMap( | ||||
1117 | const SmallVectorImpl<GCRelocateInst *> &AllRelocateCalls, | ||||
1118 | DenseMap<GCRelocateInst *, SmallVector<GCRelocateInst *, 2>> | ||||
1119 | &RelocateInstMap) { | ||||
1120 | // Collect information in two maps: one primarily for locating the base object | ||||
1121 | // while filling the second map; the second map is the final structure holding | ||||
1122 | // a mapping between Base and corresponding Derived relocate calls | ||||
1123 | DenseMap<std::pair<unsigned, unsigned>, GCRelocateInst *> RelocateIdxMap; | ||||
1124 | for (auto *ThisRelocate : AllRelocateCalls) { | ||||
1125 | auto K = std::make_pair(ThisRelocate->getBasePtrIndex(), | ||||
1126 | ThisRelocate->getDerivedPtrIndex()); | ||||
1127 | RelocateIdxMap.insert(std::make_pair(K, ThisRelocate)); | ||||
1128 | } | ||||
1129 | for (auto &Item : RelocateIdxMap) { | ||||
1130 | std::pair<unsigned, unsigned> Key = Item.first; | ||||
1131 | if (Key.first == Key.second) | ||||
1132 | // Base relocation: nothing to insert | ||||
1133 | continue; | ||||
1134 | |||||
1135 | GCRelocateInst *I = Item.second; | ||||
1136 | auto BaseKey = std::make_pair(Key.first, Key.first); | ||||
1137 | |||||
1138 | // We're iterating over RelocateIdxMap so we cannot modify it. | ||||
1139 | auto MaybeBase = RelocateIdxMap.find(BaseKey); | ||||
1140 | if (MaybeBase == RelocateIdxMap.end()) | ||||
1141 | // TODO: We might want to insert a new base object relocate and gep off | ||||
1142 | // that, if there are enough derived object relocates. | ||||
1143 | continue; | ||||
1144 | |||||
1145 | RelocateInstMap[MaybeBase->second].push_back(I); | ||||
1146 | } | ||||
1147 | } | ||||
1148 | |||||
1149 | // Accepts a GEP and extracts the operands into a vector provided they're all | ||||
1150 | // small integer constants | ||||
1151 | static bool getGEPSmallConstantIntOffsetV(GetElementPtrInst *GEP, | ||||
1152 | SmallVectorImpl<Value *> &OffsetV) { | ||||
1153 | for (unsigned i = 1; i < GEP->getNumOperands(); i++) { | ||||
1154 | // Only accept small constant integer operands | ||||
1155 | auto *Op = dyn_cast<ConstantInt>(GEP->getOperand(i)); | ||||
1156 | if (!Op || Op->getZExtValue() > 20) | ||||
1157 | return false; | ||||
1158 | } | ||||
1159 | |||||
1160 | for (unsigned i = 1; i < GEP->getNumOperands(); i++) | ||||
1161 | OffsetV.push_back(GEP->getOperand(i)); | ||||
1162 | return true; | ||||
1163 | } | ||||
1164 | |||||
1165 | // Takes a RelocatedBase (base pointer relocation instruction) and Targets to | ||||
1166 | // replace, computes a replacement, and affects it. | ||||
1167 | static bool | ||||
1168 | simplifyRelocatesOffABase(GCRelocateInst *RelocatedBase, | ||||
1169 | const SmallVectorImpl<GCRelocateInst *> &Targets) { | ||||
1170 | bool MadeChange = false; | ||||
1171 | // We must ensure the relocation of derived pointer is defined after | ||||
1172 | // relocation of base pointer. If we find a relocation corresponding to base | ||||
1173 | // defined earlier than relocation of base then we move relocation of base | ||||
1174 | // right before found relocation. We consider only relocation in the same | ||||
1175 | // basic block as relocation of base. Relocations from other basic block will | ||||
1176 | // be skipped by optimization and we do not care about them. | ||||
1177 | for (auto R = RelocatedBase->getParent()->getFirstInsertionPt(); | ||||
1178 | &*R != RelocatedBase; ++R) | ||||
1179 | if (auto *RI = dyn_cast<GCRelocateInst>(R)) | ||||
1180 | if (RI->getStatepoint() == RelocatedBase->getStatepoint()) | ||||
1181 | if (RI->getBasePtrIndex() == RelocatedBase->getBasePtrIndex()) { | ||||
1182 | RelocatedBase->moveBefore(RI); | ||||
1183 | break; | ||||
1184 | } | ||||
1185 | |||||
1186 | for (GCRelocateInst *ToReplace : Targets) { | ||||
1187 | assert(ToReplace->getBasePtrIndex() == RelocatedBase->getBasePtrIndex() &&(static_cast <bool> (ToReplace->getBasePtrIndex() == RelocatedBase->getBasePtrIndex() && "Not relocating a derived object of the original base object" ) ? void (0) : __assert_fail ("ToReplace->getBasePtrIndex() == RelocatedBase->getBasePtrIndex() && \"Not relocating a derived object of the original base object\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1188, __extension__ __PRETTY_FUNCTION__ )) | ||||
1188 | "Not relocating a derived object of the original base object")(static_cast <bool> (ToReplace->getBasePtrIndex() == RelocatedBase->getBasePtrIndex() && "Not relocating a derived object of the original base object" ) ? void (0) : __assert_fail ("ToReplace->getBasePtrIndex() == RelocatedBase->getBasePtrIndex() && \"Not relocating a derived object of the original base object\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1188, __extension__ __PRETTY_FUNCTION__ )); | ||||
1189 | if (ToReplace->getBasePtrIndex() == ToReplace->getDerivedPtrIndex()) { | ||||
1190 | // A duplicate relocate call. TODO: coalesce duplicates. | ||||
1191 | continue; | ||||
1192 | } | ||||
1193 | |||||
1194 | if (RelocatedBase->getParent() != ToReplace->getParent()) { | ||||
1195 | // Base and derived relocates are in different basic blocks. | ||||
1196 | // In this case transform is only valid when base dominates derived | ||||
1197 | // relocate. However it would be too expensive to check dominance | ||||
1198 | // for each such relocate, so we skip the whole transformation. | ||||
1199 | continue; | ||||
1200 | } | ||||
1201 | |||||
1202 | Value *Base = ToReplace->getBasePtr(); | ||||
1203 | auto *Derived = dyn_cast<GetElementPtrInst>(ToReplace->getDerivedPtr()); | ||||
1204 | if (!Derived || Derived->getPointerOperand() != Base) | ||||
1205 | continue; | ||||
1206 | |||||
1207 | SmallVector<Value *, 2> OffsetV; | ||||
1208 | if (!getGEPSmallConstantIntOffsetV(Derived, OffsetV)) | ||||
1209 | continue; | ||||
1210 | |||||
1211 | // Create a Builder and replace the target callsite with a gep | ||||
1212 | assert(RelocatedBase->getNextNode() &&(static_cast <bool> (RelocatedBase->getNextNode() && "Should always have one since it's not a terminator") ? void (0) : __assert_fail ("RelocatedBase->getNextNode() && \"Should always have one since it's not a terminator\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1213, __extension__ __PRETTY_FUNCTION__ )) | ||||
1213 | "Should always have one since it's not a terminator")(static_cast <bool> (RelocatedBase->getNextNode() && "Should always have one since it's not a terminator") ? void (0) : __assert_fail ("RelocatedBase->getNextNode() && \"Should always have one since it's not a terminator\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1213, __extension__ __PRETTY_FUNCTION__ )); | ||||
1214 | |||||
1215 | // Insert after RelocatedBase | ||||
1216 | IRBuilder<> Builder(RelocatedBase->getNextNode()); | ||||
1217 | Builder.SetCurrentDebugLocation(ToReplace->getDebugLoc()); | ||||
1218 | |||||
1219 | // If gc_relocate does not match the actual type, cast it to the right type. | ||||
1220 | // In theory, there must be a bitcast after gc_relocate if the type does not | ||||
1221 | // match, and we should reuse it to get the derived pointer. But it could be | ||||
1222 | // cases like this: | ||||
1223 | // bb1: | ||||
1224 | // ... | ||||
1225 | // %g1 = call coldcc i8 addrspace(1)* | ||||
1226 | // @llvm.experimental.gc.relocate.p1i8(...) br label %merge | ||||
1227 | // | ||||
1228 | // bb2: | ||||
1229 | // ... | ||||
1230 | // %g2 = call coldcc i8 addrspace(1)* | ||||
1231 | // @llvm.experimental.gc.relocate.p1i8(...) br label %merge | ||||
1232 | // | ||||
1233 | // merge: | ||||
1234 | // %p1 = phi i8 addrspace(1)* [ %g1, %bb1 ], [ %g2, %bb2 ] | ||||
1235 | // %cast = bitcast i8 addrspace(1)* %p1 in to i32 addrspace(1)* | ||||
1236 | // | ||||
1237 | // In this case, we can not find the bitcast any more. So we insert a new | ||||
1238 | // bitcast no matter there is already one or not. In this way, we can handle | ||||
1239 | // all cases, and the extra bitcast should be optimized away in later | ||||
1240 | // passes. | ||||
1241 | Value *ActualRelocatedBase = RelocatedBase; | ||||
1242 | if (RelocatedBase->getType() != Base->getType()) { | ||||
1243 | ActualRelocatedBase = | ||||
1244 | Builder.CreateBitCast(RelocatedBase, Base->getType()); | ||||
1245 | } | ||||
1246 | Value *Replacement = | ||||
1247 | Builder.CreateGEP(Derived->getSourceElementType(), ActualRelocatedBase, | ||||
1248 | ArrayRef(OffsetV)); | ||||
1249 | Replacement->takeName(ToReplace); | ||||
1250 | // If the newly generated derived pointer's type does not match the original | ||||
1251 | // derived pointer's type, cast the new derived pointer to match it. Same | ||||
1252 | // reasoning as above. | ||||
1253 | Value *ActualReplacement = Replacement; | ||||
1254 | if (Replacement->getType() != ToReplace->getType()) { | ||||
1255 | ActualReplacement = | ||||
1256 | Builder.CreateBitCast(Replacement, ToReplace->getType()); | ||||
1257 | } | ||||
1258 | ToReplace->replaceAllUsesWith(ActualReplacement); | ||||
1259 | ToReplace->eraseFromParent(); | ||||
1260 | |||||
1261 | MadeChange = true; | ||||
1262 | } | ||||
1263 | return MadeChange; | ||||
1264 | } | ||||
1265 | |||||
1266 | // Turns this: | ||||
1267 | // | ||||
1268 | // %base = ... | ||||
1269 | // %ptr = gep %base + 15 | ||||
1270 | // %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr) | ||||
1271 | // %base' = relocate(%tok, i32 4, i32 4) | ||||
1272 | // %ptr' = relocate(%tok, i32 4, i32 5) | ||||
1273 | // %val = load %ptr' | ||||
1274 | // | ||||
1275 | // into this: | ||||
1276 | // | ||||
1277 | // %base = ... | ||||
1278 | // %ptr = gep %base + 15 | ||||
1279 | // %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr) | ||||
1280 | // %base' = gc.relocate(%tok, i32 4, i32 4) | ||||
1281 | // %ptr' = gep %base' + 15 | ||||
1282 | // %val = load %ptr' | ||||
1283 | bool CodeGenPrepare::simplifyOffsetableRelocate(GCStatepointInst &I) { | ||||
1284 | bool MadeChange = false; | ||||
1285 | SmallVector<GCRelocateInst *, 2> AllRelocateCalls; | ||||
1286 | for (auto *U : I.users()) | ||||
1287 | if (GCRelocateInst *Relocate = dyn_cast<GCRelocateInst>(U)) | ||||
1288 | // Collect all the relocate calls associated with a statepoint | ||||
1289 | AllRelocateCalls.push_back(Relocate); | ||||
1290 | |||||
1291 | // We need at least one base pointer relocation + one derived pointer | ||||
1292 | // relocation to mangle | ||||
1293 | if (AllRelocateCalls.size() < 2) | ||||
1294 | return false; | ||||
1295 | |||||
1296 | // RelocateInstMap is a mapping from the base relocate instruction to the | ||||
1297 | // corresponding derived relocate instructions | ||||
1298 | DenseMap<GCRelocateInst *, SmallVector<GCRelocateInst *, 2>> RelocateInstMap; | ||||
1299 | computeBaseDerivedRelocateMap(AllRelocateCalls, RelocateInstMap); | ||||
1300 | if (RelocateInstMap.empty()) | ||||
1301 | return false; | ||||
1302 | |||||
1303 | for (auto &Item : RelocateInstMap) | ||||
1304 | // Item.first is the RelocatedBase to offset against | ||||
1305 | // Item.second is the vector of Targets to replace | ||||
1306 | MadeChange = simplifyRelocatesOffABase(Item.first, Item.second); | ||||
1307 | return MadeChange; | ||||
1308 | } | ||||
1309 | |||||
1310 | /// Sink the specified cast instruction into its user blocks. | ||||
1311 | static bool SinkCast(CastInst *CI) { | ||||
1312 | BasicBlock *DefBB = CI->getParent(); | ||||
1313 | |||||
1314 | /// InsertedCasts - Only insert a cast in each block once. | ||||
1315 | DenseMap<BasicBlock *, CastInst *> InsertedCasts; | ||||
1316 | |||||
1317 | bool MadeChange = false; | ||||
1318 | for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end(); | ||||
1319 | UI != E;) { | ||||
1320 | Use &TheUse = UI.getUse(); | ||||
1321 | Instruction *User = cast<Instruction>(*UI); | ||||
1322 | |||||
1323 | // Figure out which BB this cast is used in. For PHI's this is the | ||||
1324 | // appropriate predecessor block. | ||||
1325 | BasicBlock *UserBB = User->getParent(); | ||||
1326 | if (PHINode *PN = dyn_cast<PHINode>(User)) { | ||||
1327 | UserBB = PN->getIncomingBlock(TheUse); | ||||
1328 | } | ||||
1329 | |||||
1330 | // Preincrement use iterator so we don't invalidate it. | ||||
1331 | ++UI; | ||||
1332 | |||||
1333 | // The first insertion point of a block containing an EH pad is after the | ||||
1334 | // pad. If the pad is the user, we cannot sink the cast past the pad. | ||||
1335 | if (User->isEHPad()) | ||||
1336 | continue; | ||||
1337 | |||||
1338 | // If the block selected to receive the cast is an EH pad that does not | ||||
1339 | // allow non-PHI instructions before the terminator, we can't sink the | ||||
1340 | // cast. | ||||
1341 | if (UserBB->getTerminator()->isEHPad()) | ||||
1342 | continue; | ||||
1343 | |||||
1344 | // If this user is in the same block as the cast, don't change the cast. | ||||
1345 | if (UserBB == DefBB) | ||||
1346 | continue; | ||||
1347 | |||||
1348 | // If we have already inserted a cast into this block, use it. | ||||
1349 | CastInst *&InsertedCast = InsertedCasts[UserBB]; | ||||
1350 | |||||
1351 | if (!InsertedCast) { | ||||
1352 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); | ||||
1353 | assert(InsertPt != UserBB->end())(static_cast <bool> (InsertPt != UserBB->end()) ? void (0) : __assert_fail ("InsertPt != UserBB->end()", "llvm/lib/CodeGen/CodeGenPrepare.cpp" , 1353, __extension__ __PRETTY_FUNCTION__)); | ||||
1354 | InsertedCast = CastInst::Create(CI->getOpcode(), CI->getOperand(0), | ||||
1355 | CI->getType(), "", &*InsertPt); | ||||
1356 | InsertedCast->setDebugLoc(CI->getDebugLoc()); | ||||
1357 | } | ||||
1358 | |||||
1359 | // Replace a use of the cast with a use of the new cast. | ||||
1360 | TheUse = InsertedCast; | ||||
1361 | MadeChange = true; | ||||
1362 | ++NumCastUses; | ||||
1363 | } | ||||
1364 | |||||
1365 | // If we removed all uses, nuke the cast. | ||||
1366 | if (CI->use_empty()) { | ||||
1367 | salvageDebugInfo(*CI); | ||||
1368 | CI->eraseFromParent(); | ||||
1369 | MadeChange = true; | ||||
1370 | } | ||||
1371 | |||||
1372 | return MadeChange; | ||||
1373 | } | ||||
1374 | |||||
1375 | /// If the specified cast instruction is a noop copy (e.g. it's casting from | ||||
1376 | /// one pointer type to another, i32->i8 on PPC), sink it into user blocks to | ||||
1377 | /// reduce the number of virtual registers that must be created and coalesced. | ||||
1378 | /// | ||||
1379 | /// Return true if any changes are made. | ||||
1380 | static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI, | ||||
1381 | const DataLayout &DL) { | ||||
1382 | // Sink only "cheap" (or nop) address-space casts. This is a weaker condition | ||||
1383 | // than sinking only nop casts, but is helpful on some platforms. | ||||
1384 | if (auto *ASC = dyn_cast<AddrSpaceCastInst>(CI)) { | ||||
1385 | if (!TLI.isFreeAddrSpaceCast(ASC->getSrcAddressSpace(), | ||||
1386 | ASC->getDestAddressSpace())) | ||||
1387 | return false; | ||||
1388 | } | ||||
1389 | |||||
1390 | // If this is a noop copy, | ||||
1391 | EVT SrcVT = TLI.getValueType(DL, CI->getOperand(0)->getType()); | ||||
1392 | EVT DstVT = TLI.getValueType(DL, CI->getType()); | ||||
1393 | |||||
1394 | // This is an fp<->int conversion? | ||||
1395 | if (SrcVT.isInteger() != DstVT.isInteger()) | ||||
1396 | return false; | ||||
1397 | |||||
1398 | // If this is an extension, it will be a zero or sign extension, which | ||||
1399 | // isn't a noop. | ||||
1400 | if (SrcVT.bitsLT(DstVT)) | ||||
1401 | return false; | ||||
1402 | |||||
1403 | // If these values will be promoted, find out what they will be promoted | ||||
1404 | // to. This helps us consider truncates on PPC as noop copies when they | ||||
1405 | // are. | ||||
1406 | if (TLI.getTypeAction(CI->getContext(), SrcVT) == | ||||
1407 | TargetLowering::TypePromoteInteger) | ||||
1408 | SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT); | ||||
1409 | if (TLI.getTypeAction(CI->getContext(), DstVT) == | ||||
1410 | TargetLowering::TypePromoteInteger) | ||||
1411 | DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT); | ||||
1412 | |||||
1413 | // If, after promotion, these are the same types, this is a noop copy. | ||||
1414 | if (SrcVT != DstVT) | ||||
1415 | return false; | ||||
1416 | |||||
1417 | return SinkCast(CI); | ||||
1418 | } | ||||
1419 | |||||
1420 | // Match a simple increment by constant operation. Note that if a sub is | ||||
1421 | // matched, the step is negated (as if the step had been canonicalized to | ||||
1422 | // an add, even though we leave the instruction alone.) | ||||
1423 | bool matchIncrement(const Instruction *IVInc, Instruction *&LHS, | ||||
1424 | Constant *&Step) { | ||||
1425 | if (match(IVInc, m_Add(m_Instruction(LHS), m_Constant(Step))) || | ||||
1426 | match(IVInc, m_ExtractValue<0>(m_Intrinsic<Intrinsic::uadd_with_overflow>( | ||||
1427 | m_Instruction(LHS), m_Constant(Step))))) | ||||
1428 | return true; | ||||
1429 | if (match(IVInc, m_Sub(m_Instruction(LHS), m_Constant(Step))) || | ||||
1430 | match(IVInc, m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>( | ||||
1431 | m_Instruction(LHS), m_Constant(Step))))) { | ||||
1432 | Step = ConstantExpr::getNeg(Step); | ||||
1433 | return true; | ||||
1434 | } | ||||
1435 | return false; | ||||
1436 | } | ||||
1437 | |||||
1438 | /// If given \p PN is an inductive variable with value IVInc coming from the | ||||
1439 | /// backedge, and on each iteration it gets increased by Step, return pair | ||||
1440 | /// <IVInc, Step>. Otherwise, return std::nullopt. | ||||
1441 | static std::optional<std::pair<Instruction *, Constant *>> | ||||
1442 | getIVIncrement(const PHINode *PN, const LoopInfo *LI) { | ||||
1443 | const Loop *L = LI->getLoopFor(PN->getParent()); | ||||
1444 | if (!L || L->getHeader() != PN->getParent() || !L->getLoopLatch()) | ||||
1445 | return std::nullopt; | ||||
1446 | auto *IVInc = | ||||
1447 | dyn_cast<Instruction>(PN->getIncomingValueForBlock(L->getLoopLatch())); | ||||
1448 | if (!IVInc || LI->getLoopFor(IVInc->getParent()) != L) | ||||
1449 | return std::nullopt; | ||||
1450 | Instruction *LHS = nullptr; | ||||
1451 | Constant *Step = nullptr; | ||||
1452 | if (matchIncrement(IVInc, LHS, Step) && LHS == PN) | ||||
1453 | return std::make_pair(IVInc, Step); | ||||
1454 | return std::nullopt; | ||||
1455 | } | ||||
1456 | |||||
1457 | static bool isIVIncrement(const Value *V, const LoopInfo *LI) { | ||||
1458 | auto *I = dyn_cast<Instruction>(V); | ||||
1459 | if (!I) | ||||
1460 | return false; | ||||
1461 | Instruction *LHS = nullptr; | ||||
1462 | Constant *Step = nullptr; | ||||
1463 | if (!matchIncrement(I, LHS, Step)) | ||||
1464 | return false; | ||||
1465 | if (auto *PN = dyn_cast<PHINode>(LHS)) | ||||
1466 | if (auto IVInc = getIVIncrement(PN, LI)) | ||||
1467 | return IVInc->first == I; | ||||
1468 | return false; | ||||
1469 | } | ||||
1470 | |||||
1471 | bool CodeGenPrepare::replaceMathCmpWithIntrinsic(BinaryOperator *BO, | ||||
1472 | Value *Arg0, Value *Arg1, | ||||
1473 | CmpInst *Cmp, | ||||
1474 | Intrinsic::ID IID) { | ||||
1475 | auto IsReplacableIVIncrement = [this, &Cmp](BinaryOperator *BO) { | ||||
1476 | if (!isIVIncrement(BO, LI)) | ||||
1477 | return false; | ||||
1478 | const Loop *L = LI->getLoopFor(BO->getParent()); | ||||
1479 | assert(L && "L should not be null after isIVIncrement()")(static_cast <bool> (L && "L should not be null after isIVIncrement()" ) ? void (0) : __assert_fail ("L && \"L should not be null after isIVIncrement()\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1479, __extension__ __PRETTY_FUNCTION__ )); | ||||
1480 | // Do not risk on moving increment into a child loop. | ||||
1481 | if (LI->getLoopFor(Cmp->getParent()) != L) | ||||
1482 | return false; | ||||
1483 | |||||
1484 | // Finally, we need to ensure that the insert point will dominate all | ||||
1485 | // existing uses of the increment. | ||||
1486 | |||||
1487 | auto &DT = getDT(*BO->getParent()->getParent()); | ||||
1488 | if (DT.dominates(Cmp->getParent(), BO->getParent())) | ||||
1489 | // If we're moving up the dom tree, all uses are trivially dominated. | ||||
1490 | // (This is the common case for code produced by LSR.) | ||||
1491 | return true; | ||||
1492 | |||||
1493 | // Otherwise, special case the single use in the phi recurrence. | ||||
1494 | return BO->hasOneUse() && DT.dominates(Cmp->getParent(), L->getLoopLatch()); | ||||
1495 | }; | ||||
1496 | if (BO->getParent() != Cmp->getParent() && !IsReplacableIVIncrement(BO)) { | ||||
1497 | // We used to use a dominator tree here to allow multi-block optimization. | ||||
1498 | // But that was problematic because: | ||||
1499 | // 1. It could cause a perf regression by hoisting the math op into the | ||||
1500 | // critical path. | ||||
1501 | // 2. It could cause a perf regression by creating a value that was live | ||||
1502 | // across multiple blocks and increasing register pressure. | ||||
1503 | // 3. Use of a dominator tree could cause large compile-time regression. | ||||
1504 | // This is because we recompute the DT on every change in the main CGP | ||||
1505 | // run-loop. The recomputing is probably unnecessary in many cases, so if | ||||
1506 | // that was fixed, using a DT here would be ok. | ||||
1507 | // | ||||
1508 | // There is one important particular case we still want to handle: if BO is | ||||
1509 | // the IV increment. Important properties that make it profitable: | ||||
1510 | // - We can speculate IV increment anywhere in the loop (as long as the | ||||
1511 | // indvar Phi is its only user); | ||||
1512 | // - Upon computing Cmp, we effectively compute something equivalent to the | ||||
1513 | // IV increment (despite it loops differently in the IR). So moving it up | ||||
1514 | // to the cmp point does not really increase register pressure. | ||||
1515 | return false; | ||||
1516 | } | ||||
1517 | |||||
1518 | // We allow matching the canonical IR (add X, C) back to (usubo X, -C). | ||||
1519 | if (BO->getOpcode() == Instruction::Add && | ||||
1520 | IID == Intrinsic::usub_with_overflow) { | ||||
1521 | assert(isa<Constant>(Arg1) && "Unexpected input for usubo")(static_cast <bool> (isa<Constant>(Arg1) && "Unexpected input for usubo") ? void (0) : __assert_fail ("isa<Constant>(Arg1) && \"Unexpected input for usubo\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1521, __extension__ __PRETTY_FUNCTION__ )); | ||||
1522 | Arg1 = ConstantExpr::getNeg(cast<Constant>(Arg1)); | ||||
1523 | } | ||||
1524 | |||||
1525 | // Insert at the first instruction of the pair. | ||||
1526 | Instruction *InsertPt = nullptr; | ||||
1527 | for (Instruction &Iter : *Cmp->getParent()) { | ||||
1528 | // If BO is an XOR, it is not guaranteed that it comes after both inputs to | ||||
1529 | // the overflow intrinsic are defined. | ||||
1530 | if ((BO->getOpcode() != Instruction::Xor && &Iter == BO) || &Iter == Cmp) { | ||||
1531 | InsertPt = &Iter; | ||||
1532 | break; | ||||
1533 | } | ||||
1534 | } | ||||
1535 | assert(InsertPt != nullptr && "Parent block did not contain cmp or binop")(static_cast <bool> (InsertPt != nullptr && "Parent block did not contain cmp or binop" ) ? void (0) : __assert_fail ("InsertPt != nullptr && \"Parent block did not contain cmp or binop\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1535, __extension__ __PRETTY_FUNCTION__ )); | ||||
1536 | |||||
1537 | IRBuilder<> Builder(InsertPt); | ||||
1538 | Value *MathOV = Builder.CreateBinaryIntrinsic(IID, Arg0, Arg1); | ||||
1539 | if (BO->getOpcode() != Instruction::Xor) { | ||||
1540 | Value *Math = Builder.CreateExtractValue(MathOV, 0, "math"); | ||||
1541 | replaceAllUsesWith(BO, Math, FreshBBs, IsHugeFunc); | ||||
1542 | } else | ||||
1543 | assert(BO->hasOneUse() &&(static_cast <bool> (BO->hasOneUse() && "Patterns with XOr should use the BO only in the compare" ) ? void (0) : __assert_fail ("BO->hasOneUse() && \"Patterns with XOr should use the BO only in the compare\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1544, __extension__ __PRETTY_FUNCTION__ )) | ||||
1544 | "Patterns with XOr should use the BO only in the compare")(static_cast <bool> (BO->hasOneUse() && "Patterns with XOr should use the BO only in the compare" ) ? void (0) : __assert_fail ("BO->hasOneUse() && \"Patterns with XOr should use the BO only in the compare\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1544, __extension__ __PRETTY_FUNCTION__ )); | ||||
1545 | Value *OV = Builder.CreateExtractValue(MathOV, 1, "ov"); | ||||
1546 | replaceAllUsesWith(Cmp, OV, FreshBBs, IsHugeFunc); | ||||
1547 | Cmp->eraseFromParent(); | ||||
1548 | BO->eraseFromParent(); | ||||
1549 | return true; | ||||
1550 | } | ||||
1551 | |||||
1552 | /// Match special-case patterns that check for unsigned add overflow. | ||||
1553 | static bool matchUAddWithOverflowConstantEdgeCases(CmpInst *Cmp, | ||||
1554 | BinaryOperator *&Add) { | ||||
1555 | // Add = add A, 1; Cmp = icmp eq A,-1 (overflow if A is max val) | ||||
1556 | // Add = add A,-1; Cmp = icmp ne A, 0 (overflow if A is non-zero) | ||||
1557 | Value *A = Cmp->getOperand(0), *B = Cmp->getOperand(1); | ||||
1558 | |||||
1559 | // We are not expecting non-canonical/degenerate code. Just bail out. | ||||
1560 | if (isa<Constant>(A)) | ||||
1561 | return false; | ||||
1562 | |||||
1563 | ICmpInst::Predicate Pred = Cmp->getPredicate(); | ||||
1564 | if (Pred == ICmpInst::ICMP_EQ && match(B, m_AllOnes())) | ||||
1565 | B = ConstantInt::get(B->getType(), 1); | ||||
1566 | else if (Pred == ICmpInst::ICMP_NE && match(B, m_ZeroInt())) | ||||
1567 | B = ConstantInt::get(B->getType(), -1); | ||||
1568 | else | ||||
1569 | return false; | ||||
1570 | |||||
1571 | // Check the users of the variable operand of the compare looking for an add | ||||
1572 | // with the adjusted constant. | ||||
1573 | for (User *U : A->users()) { | ||||
1574 | if (match(U, m_Add(m_Specific(A), m_Specific(B)))) { | ||||
1575 | Add = cast<BinaryOperator>(U); | ||||
1576 | return true; | ||||
1577 | } | ||||
1578 | } | ||||
1579 | return false; | ||||
1580 | } | ||||
1581 | |||||
1582 | /// Try to combine the compare into a call to the llvm.uadd.with.overflow | ||||
1583 | /// intrinsic. Return true if any changes were made. | ||||
1584 | bool CodeGenPrepare::combineToUAddWithOverflow(CmpInst *Cmp, | ||||
1585 | ModifyDT &ModifiedDT) { | ||||
1586 | bool EdgeCase = false; | ||||
1587 | Value *A, *B; | ||||
1588 | BinaryOperator *Add; | ||||
1589 | if (!match(Cmp, m_UAddWithOverflow(m_Value(A), m_Value(B), m_BinOp(Add)))) { | ||||
1590 | if (!matchUAddWithOverflowConstantEdgeCases(Cmp, Add)) | ||||
1591 | return false; | ||||
1592 | // Set A and B in case we match matchUAddWithOverflowConstantEdgeCases. | ||||
1593 | A = Add->getOperand(0); | ||||
1594 | B = Add->getOperand(1); | ||||
1595 | EdgeCase = true; | ||||
1596 | } | ||||
1597 | |||||
1598 | if (!TLI->shouldFormOverflowOp(ISD::UADDO, | ||||
1599 | TLI->getValueType(*DL, Add->getType()), | ||||
1600 | Add->hasNUsesOrMore(EdgeCase ? 1 : 2))) | ||||
1601 | return false; | ||||
1602 | |||||
1603 | // We don't want to move around uses of condition values this late, so we | ||||
1604 | // check if it is legal to create the call to the intrinsic in the basic | ||||
1605 | // block containing the icmp. | ||||
1606 | if (Add->getParent() != Cmp->getParent() && !Add->hasOneUse()) | ||||
1607 | return false; | ||||
1608 | |||||
1609 | if (!replaceMathCmpWithIntrinsic(Add, A, B, Cmp, | ||||
1610 | Intrinsic::uadd_with_overflow)) | ||||
1611 | return false; | ||||
1612 | |||||
1613 | // Reset callers - do not crash by iterating over a dead instruction. | ||||
1614 | ModifiedDT = ModifyDT::ModifyInstDT; | ||||
1615 | return true; | ||||
1616 | } | ||||
1617 | |||||
1618 | bool CodeGenPrepare::combineToUSubWithOverflow(CmpInst *Cmp, | ||||
1619 | ModifyDT &ModifiedDT) { | ||||
1620 | // We are not expecting non-canonical/degenerate code. Just bail out. | ||||
1621 | Value *A = Cmp->getOperand(0), *B = Cmp->getOperand(1); | ||||
1622 | if (isa<Constant>(A) && isa<Constant>(B)) | ||||
1623 | return false; | ||||
1624 | |||||
1625 | // Convert (A u> B) to (A u< B) to simplify pattern matching. | ||||
1626 | ICmpInst::Predicate Pred = Cmp->getPredicate(); | ||||
1627 | if (Pred == ICmpInst::ICMP_UGT) { | ||||
1628 | std::swap(A, B); | ||||
1629 | Pred = ICmpInst::ICMP_ULT; | ||||
1630 | } | ||||
1631 | // Convert special-case: (A == 0) is the same as (A u< 1). | ||||
1632 | if (Pred == ICmpInst::ICMP_EQ && match(B, m_ZeroInt())) { | ||||
1633 | B = ConstantInt::get(B->getType(), 1); | ||||
1634 | Pred = ICmpInst::ICMP_ULT; | ||||
1635 | } | ||||
1636 | // Convert special-case: (A != 0) is the same as (0 u< A). | ||||
1637 | if (Pred == ICmpInst::ICMP_NE && match(B, m_ZeroInt())) { | ||||
1638 | std::swap(A, B); | ||||
1639 | Pred = ICmpInst::ICMP_ULT; | ||||
1640 | } | ||||
1641 | if (Pred != ICmpInst::ICMP_ULT) | ||||
1642 | return false; | ||||
1643 | |||||
1644 | // Walk the users of a variable operand of a compare looking for a subtract or | ||||
1645 | // add with that same operand. Also match the 2nd operand of the compare to | ||||
1646 | // the add/sub, but that may be a negated constant operand of an add. | ||||
1647 | Value *CmpVariableOperand = isa<Constant>(A) ? B : A; | ||||
1648 | BinaryOperator *Sub = nullptr; | ||||
1649 | for (User *U : CmpVariableOperand->users()) { | ||||
1650 | // A - B, A u< B --> usubo(A, B) | ||||
1651 | if (match(U, m_Sub(m_Specific(A), m_Specific(B)))) { | ||||
1652 | Sub = cast<BinaryOperator>(U); | ||||
1653 | break; | ||||
1654 | } | ||||
1655 | |||||
1656 | // A + (-C), A u< C (canonicalized form of (sub A, C)) | ||||
1657 | const APInt *CmpC, *AddC; | ||||
1658 | if (match(U, m_Add(m_Specific(A), m_APInt(AddC))) && | ||||
1659 | match(B, m_APInt(CmpC)) && *AddC == -(*CmpC)) { | ||||
1660 | Sub = cast<BinaryOperator>(U); | ||||
1661 | break; | ||||
1662 | } | ||||
1663 | } | ||||
1664 | if (!Sub) | ||||
1665 | return false; | ||||
1666 | |||||
1667 | if (!TLI->shouldFormOverflowOp(ISD::USUBO, | ||||
1668 | TLI->getValueType(*DL, Sub->getType()), | ||||
1669 | Sub->hasNUsesOrMore(1))) | ||||
1670 | return false; | ||||
1671 | |||||
1672 | if (!replaceMathCmpWithIntrinsic(Sub, Sub->getOperand(0), Sub->getOperand(1), | ||||
1673 | Cmp, Intrinsic::usub_with_overflow)) | ||||
1674 | return false; | ||||
1675 | |||||
1676 | // Reset callers - do not crash by iterating over a dead instruction. | ||||
1677 | ModifiedDT = ModifyDT::ModifyInstDT; | ||||
1678 | return true; | ||||
1679 | } | ||||
1680 | |||||
1681 | /// Sink the given CmpInst into user blocks to reduce the number of virtual | ||||
1682 | /// registers that must be created and coalesced. This is a clear win except on | ||||
1683 | /// targets with multiple condition code registers (PowerPC), where it might | ||||
1684 | /// lose; some adjustment may be wanted there. | ||||
1685 | /// | ||||
1686 | /// Return true if any changes are made. | ||||
1687 | static bool sinkCmpExpression(CmpInst *Cmp, const TargetLowering &TLI) { | ||||
1688 | if (TLI.hasMultipleConditionRegisters()) | ||||
1689 | return false; | ||||
1690 | |||||
1691 | // Avoid sinking soft-FP comparisons, since this can move them into a loop. | ||||
1692 | if (TLI.useSoftFloat() && isa<FCmpInst>(Cmp)) | ||||
1693 | return false; | ||||
1694 | |||||
1695 | // Only insert a cmp in each block once. | ||||
1696 | DenseMap<BasicBlock *, CmpInst *> InsertedCmps; | ||||
1697 | |||||
1698 | bool MadeChange = false; | ||||
1699 | for (Value::user_iterator UI = Cmp->user_begin(), E = Cmp->user_end(); | ||||
1700 | UI != E;) { | ||||
1701 | Use &TheUse = UI.getUse(); | ||||
1702 | Instruction *User = cast<Instruction>(*UI); | ||||
1703 | |||||
1704 | // Preincrement use iterator so we don't invalidate it. | ||||
1705 | ++UI; | ||||
1706 | |||||
1707 | // Don't bother for PHI nodes. | ||||
1708 | if (isa<PHINode>(User)) | ||||
1709 | continue; | ||||
1710 | |||||
1711 | // Figure out which BB this cmp is used in. | ||||
1712 | BasicBlock *UserBB = User->getParent(); | ||||
1713 | BasicBlock *DefBB = Cmp->getParent(); | ||||
1714 | |||||
1715 | // If this user is in the same block as the cmp, don't change the cmp. | ||||
1716 | if (UserBB == DefBB) | ||||
1717 | continue; | ||||
1718 | |||||
1719 | // If we have already inserted a cmp into this block, use it. | ||||
1720 | CmpInst *&InsertedCmp = InsertedCmps[UserBB]; | ||||
1721 | |||||
1722 | if (!InsertedCmp) { | ||||
1723 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); | ||||
1724 | assert(InsertPt != UserBB->end())(static_cast <bool> (InsertPt != UserBB->end()) ? void (0) : __assert_fail ("InsertPt != UserBB->end()", "llvm/lib/CodeGen/CodeGenPrepare.cpp" , 1724, __extension__ __PRETTY_FUNCTION__)); | ||||
1725 | InsertedCmp = CmpInst::Create(Cmp->getOpcode(), Cmp->getPredicate(), | ||||
1726 | Cmp->getOperand(0), Cmp->getOperand(1), "", | ||||
1727 | &*InsertPt); | ||||
1728 | // Propagate the debug info. | ||||
1729 | InsertedCmp->setDebugLoc(Cmp->getDebugLoc()); | ||||
1730 | } | ||||
1731 | |||||
1732 | // Replace a use of the cmp with a use of the new cmp. | ||||
1733 | TheUse = InsertedCmp; | ||||
1734 | MadeChange = true; | ||||
1735 | ++NumCmpUses; | ||||
1736 | } | ||||
1737 | |||||
1738 | // If we removed all uses, nuke the cmp. | ||||
1739 | if (Cmp->use_empty()) { | ||||
1740 | Cmp->eraseFromParent(); | ||||
1741 | MadeChange = true; | ||||
1742 | } | ||||
1743 | |||||
1744 | return MadeChange; | ||||
1745 | } | ||||
1746 | |||||
1747 | /// For pattern like: | ||||
1748 | /// | ||||
1749 | /// DomCond = icmp sgt/slt CmpOp0, CmpOp1 (might not be in DomBB) | ||||
1750 | /// ... | ||||
1751 | /// DomBB: | ||||
1752 | /// ... | ||||
1753 | /// br DomCond, TrueBB, CmpBB | ||||
1754 | /// CmpBB: (with DomBB being the single predecessor) | ||||
1755 | /// ... | ||||
1756 | /// Cmp = icmp eq CmpOp0, CmpOp1 | ||||
1757 | /// ... | ||||
1758 | /// | ||||
1759 | /// It would use two comparison on targets that lowering of icmp sgt/slt is | ||||
1760 | /// different from lowering of icmp eq (PowerPC). This function try to convert | ||||
1761 | /// 'Cmp = icmp eq CmpOp0, CmpOp1' to ' Cmp = icmp slt/sgt CmpOp0, CmpOp1'. | ||||
1762 | /// After that, DomCond and Cmp can use the same comparison so reduce one | ||||
1763 | /// comparison. | ||||
1764 | /// | ||||
1765 | /// Return true if any changes are made. | ||||
1766 | static bool foldICmpWithDominatingICmp(CmpInst *Cmp, | ||||
1767 | const TargetLowering &TLI) { | ||||
1768 | if (!EnableICMP_EQToICMP_ST && TLI.isEqualityCmpFoldedWithSignedCmp()) | ||||
1769 | return false; | ||||
1770 | |||||
1771 | ICmpInst::Predicate Pred = Cmp->getPredicate(); | ||||
1772 | if (Pred != ICmpInst::ICMP_EQ) | ||||
1773 | return false; | ||||
1774 | |||||
1775 | // If icmp eq has users other than BranchInst and SelectInst, converting it to | ||||
1776 | // icmp slt/sgt would introduce more redundant LLVM IR. | ||||
1777 | for (User *U : Cmp->users()) { | ||||
1778 | if (isa<BranchInst>(U)) | ||||
1779 | continue; | ||||
1780 | if (isa<SelectInst>(U) && cast<SelectInst>(U)->getCondition() == Cmp) | ||||
1781 | continue; | ||||
1782 | return false; | ||||
1783 | } | ||||
1784 | |||||
1785 | // This is a cheap/incomplete check for dominance - just match a single | ||||
1786 | // predecessor with a conditional branch. | ||||
1787 | BasicBlock *CmpBB = Cmp->getParent(); | ||||
1788 | BasicBlock *DomBB = CmpBB->getSinglePredecessor(); | ||||
1789 | if (!DomBB) | ||||
1790 | return false; | ||||
1791 | |||||
1792 | // We want to ensure that the only way control gets to the comparison of | ||||
1793 | // interest is that a less/greater than comparison on the same operands is | ||||
1794 | // false. | ||||
1795 | Value *DomCond; | ||||
1796 | BasicBlock *TrueBB, *FalseBB; | ||||
1797 | if (!match(DomBB->getTerminator(), m_Br(m_Value(DomCond), TrueBB, FalseBB))) | ||||
1798 | return false; | ||||
1799 | if (CmpBB != FalseBB) | ||||
1800 | return false; | ||||
1801 | |||||
1802 | Value *CmpOp0 = Cmp->getOperand(0), *CmpOp1 = Cmp->getOperand(1); | ||||
1803 | ICmpInst::Predicate DomPred; | ||||
1804 | if (!match(DomCond, m_ICmp(DomPred, m_Specific(CmpOp0), m_Specific(CmpOp1)))) | ||||
1805 | return false; | ||||
1806 | if (DomPred != ICmpInst::ICMP_SGT && DomPred != ICmpInst::ICMP_SLT) | ||||
1807 | return false; | ||||
1808 | |||||
1809 | // Convert the equality comparison to the opposite of the dominating | ||||
1810 | // comparison and swap the direction for all branch/select users. | ||||
1811 | // We have conceptually converted: | ||||
1812 | // Res = (a < b) ? <LT_RES> : (a == b) ? <EQ_RES> : <GT_RES>; | ||||
1813 | // to | ||||
1814 | // Res = (a < b) ? <LT_RES> : (a > b) ? <GT_RES> : <EQ_RES>; | ||||
1815 | // And similarly for branches. | ||||
1816 | for (User *U : Cmp->users()) { | ||||
1817 | if (auto *BI = dyn_cast<BranchInst>(U)) { | ||||
1818 | assert(BI->isConditional() && "Must be conditional")(static_cast <bool> (BI->isConditional() && "Must be conditional" ) ? void (0) : __assert_fail ("BI->isConditional() && \"Must be conditional\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1818, __extension__ __PRETTY_FUNCTION__ )); | ||||
1819 | BI->swapSuccessors(); | ||||
1820 | continue; | ||||
1821 | } | ||||
1822 | if (auto *SI = dyn_cast<SelectInst>(U)) { | ||||
1823 | // Swap operands | ||||
1824 | SI->swapValues(); | ||||
1825 | SI->swapProfMetadata(); | ||||
1826 | continue; | ||||
1827 | } | ||||
1828 | llvm_unreachable("Must be a branch or a select")::llvm::llvm_unreachable_internal("Must be a branch or a select" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1828); | ||||
1829 | } | ||||
1830 | Cmp->setPredicate(CmpInst::getSwappedPredicate(DomPred)); | ||||
1831 | return true; | ||||
1832 | } | ||||
1833 | |||||
1834 | bool CodeGenPrepare::optimizeCmp(CmpInst *Cmp, ModifyDT &ModifiedDT) { | ||||
1835 | if (sinkCmpExpression(Cmp, *TLI)) | ||||
1836 | return true; | ||||
1837 | |||||
1838 | if (combineToUAddWithOverflow(Cmp, ModifiedDT)) | ||||
1839 | return true; | ||||
1840 | |||||
1841 | if (combineToUSubWithOverflow(Cmp, ModifiedDT)) | ||||
1842 | return true; | ||||
1843 | |||||
1844 | if (foldICmpWithDominatingICmp(Cmp, *TLI)) | ||||
1845 | return true; | ||||
1846 | |||||
1847 | return false; | ||||
1848 | } | ||||
1849 | |||||
1850 | /// Duplicate and sink the given 'and' instruction into user blocks where it is | ||||
1851 | /// used in a compare to allow isel to generate better code for targets where | ||||
1852 | /// this operation can be combined. | ||||
1853 | /// | ||||
1854 | /// Return true if any changes are made. | ||||
1855 | static bool sinkAndCmp0Expression(Instruction *AndI, const TargetLowering &TLI, | ||||
1856 | SetOfInstrs &InsertedInsts) { | ||||
1857 | // Double-check that we're not trying to optimize an instruction that was | ||||
1858 | // already optimized by some other part of this pass. | ||||
1859 | assert(!InsertedInsts.count(AndI) &&(static_cast <bool> (!InsertedInsts.count(AndI) && "Attempting to optimize already optimized and instruction") ? void (0) : __assert_fail ("!InsertedInsts.count(AndI) && \"Attempting to optimize already optimized and instruction\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1860, __extension__ __PRETTY_FUNCTION__ )) | ||||
1860 | "Attempting to optimize already optimized and instruction")(static_cast <bool> (!InsertedInsts.count(AndI) && "Attempting to optimize already optimized and instruction") ? void (0) : __assert_fail ("!InsertedInsts.count(AndI) && \"Attempting to optimize already optimized and instruction\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1860, __extension__ __PRETTY_FUNCTION__ )); | ||||
1861 | (void)InsertedInsts; | ||||
1862 | |||||
1863 | // Nothing to do for single use in same basic block. | ||||
1864 | if (AndI->hasOneUse() && | ||||
1865 | AndI->getParent() == cast<Instruction>(*AndI->user_begin())->getParent()) | ||||
1866 | return false; | ||||
1867 | |||||
1868 | // Try to avoid cases where sinking/duplicating is likely to increase register | ||||
1869 | // pressure. | ||||
1870 | if (!isa<ConstantInt>(AndI->getOperand(0)) && | ||||
1871 | !isa<ConstantInt>(AndI->getOperand(1)) && | ||||
1872 | AndI->getOperand(0)->hasOneUse() && AndI->getOperand(1)->hasOneUse()) | ||||
1873 | return false; | ||||
1874 | |||||
1875 | for (auto *U : AndI->users()) { | ||||
1876 | Instruction *User = cast<Instruction>(U); | ||||
1877 | |||||
1878 | // Only sink 'and' feeding icmp with 0. | ||||
1879 | if (!isa<ICmpInst>(User)) | ||||
1880 | return false; | ||||
1881 | |||||
1882 | auto *CmpC = dyn_cast<ConstantInt>(User->getOperand(1)); | ||||
1883 | if (!CmpC || !CmpC->isZero()) | ||||
1884 | return false; | ||||
1885 | } | ||||
1886 | |||||
1887 | if (!TLI.isMaskAndCmp0FoldingBeneficial(*AndI)) | ||||
1888 | return false; | ||||
1889 | |||||
1890 | LLVM_DEBUG(dbgs() << "found 'and' feeding only icmp 0;\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "found 'and' feeding only icmp 0;\n" ; } } while (false); | ||||
1891 | LLVM_DEBUG(AndI->getParent()->dump())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { AndI->getParent()->dump(); } } while (false); | ||||
1892 | |||||
1893 | // Push the 'and' into the same block as the icmp 0. There should only be | ||||
1894 | // one (icmp (and, 0)) in each block, since CSE/GVN should have removed any | ||||
1895 | // others, so we don't need to keep track of which BBs we insert into. | ||||
1896 | for (Value::user_iterator UI = AndI->user_begin(), E = AndI->user_end(); | ||||
1897 | UI != E;) { | ||||
1898 | Use &TheUse = UI.getUse(); | ||||
1899 | Instruction *User = cast<Instruction>(*UI); | ||||
1900 | |||||
1901 | // Preincrement use iterator so we don't invalidate it. | ||||
1902 | ++UI; | ||||
1903 | |||||
1904 | LLVM_DEBUG(dbgs() << "sinking 'and' use: " << *User << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "sinking 'and' use: " << *User << "\n"; } } while (false); | ||||
1905 | |||||
1906 | // Keep the 'and' in the same place if the use is already in the same block. | ||||
1907 | Instruction *InsertPt = | ||||
1908 | User->getParent() == AndI->getParent() ? AndI : User; | ||||
1909 | Instruction *InsertedAnd = | ||||
1910 | BinaryOperator::Create(Instruction::And, AndI->getOperand(0), | ||||
1911 | AndI->getOperand(1), "", InsertPt); | ||||
1912 | // Propagate the debug info. | ||||
1913 | InsertedAnd->setDebugLoc(AndI->getDebugLoc()); | ||||
1914 | |||||
1915 | // Replace a use of the 'and' with a use of the new 'and'. | ||||
1916 | TheUse = InsertedAnd; | ||||
1917 | ++NumAndUses; | ||||
1918 | LLVM_DEBUG(User->getParent()->dump())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { User->getParent()->dump(); } } while (false); | ||||
1919 | } | ||||
1920 | |||||
1921 | // We removed all uses, nuke the and. | ||||
1922 | AndI->eraseFromParent(); | ||||
1923 | return true; | ||||
1924 | } | ||||
1925 | |||||
1926 | /// Check if the candidates could be combined with a shift instruction, which | ||||
1927 | /// includes: | ||||
1928 | /// 1. Truncate instruction | ||||
1929 | /// 2. And instruction and the imm is a mask of the low bits: | ||||
1930 | /// imm & (imm+1) == 0 | ||||
1931 | static bool isExtractBitsCandidateUse(Instruction *User) { | ||||
1932 | if (!isa<TruncInst>(User)) { | ||||
1933 | if (User->getOpcode() != Instruction::And || | ||||
1934 | !isa<ConstantInt>(User->getOperand(1))) | ||||
1935 | return false; | ||||
1936 | |||||
1937 | const APInt &Cimm = cast<ConstantInt>(User->getOperand(1))->getValue(); | ||||
1938 | |||||
1939 | if ((Cimm & (Cimm + 1)).getBoolValue()) | ||||
1940 | return false; | ||||
1941 | } | ||||
1942 | return true; | ||||
1943 | } | ||||
1944 | |||||
1945 | /// Sink both shift and truncate instruction to the use of truncate's BB. | ||||
1946 | static bool | ||||
1947 | SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI, | ||||
1948 | DenseMap<BasicBlock *, BinaryOperator *> &InsertedShifts, | ||||
1949 | const TargetLowering &TLI, const DataLayout &DL) { | ||||
1950 | BasicBlock *UserBB = User->getParent(); | ||||
1951 | DenseMap<BasicBlock *, CastInst *> InsertedTruncs; | ||||
1952 | auto *TruncI = cast<TruncInst>(User); | ||||
1953 | bool MadeChange = false; | ||||
1954 | |||||
1955 | for (Value::user_iterator TruncUI = TruncI->user_begin(), | ||||
1956 | TruncE = TruncI->user_end(); | ||||
1957 | TruncUI != TruncE;) { | ||||
1958 | |||||
1959 | Use &TruncTheUse = TruncUI.getUse(); | ||||
1960 | Instruction *TruncUser = cast<Instruction>(*TruncUI); | ||||
1961 | // Preincrement use iterator so we don't invalidate it. | ||||
1962 | |||||
1963 | ++TruncUI; | ||||
1964 | |||||
1965 | int ISDOpcode = TLI.InstructionOpcodeToISD(TruncUser->getOpcode()); | ||||
1966 | if (!ISDOpcode) | ||||
1967 | continue; | ||||
1968 | |||||
1969 | // If the use is actually a legal node, there will not be an | ||||
1970 | // implicit truncate. | ||||
1971 | // FIXME: always querying the result type is just an | ||||
1972 | // approximation; some nodes' legality is determined by the | ||||
1973 | // operand or other means. There's no good way to find out though. | ||||
1974 | if (TLI.isOperationLegalOrCustom( | ||||
1975 | ISDOpcode, TLI.getValueType(DL, TruncUser->getType(), true))) | ||||
1976 | continue; | ||||
1977 | |||||
1978 | // Don't bother for PHI nodes. | ||||
1979 | if (isa<PHINode>(TruncUser)) | ||||
1980 | continue; | ||||
1981 | |||||
1982 | BasicBlock *TruncUserBB = TruncUser->getParent(); | ||||
1983 | |||||
1984 | if (UserBB == TruncUserBB) | ||||
1985 | continue; | ||||
1986 | |||||
1987 | BinaryOperator *&InsertedShift = InsertedShifts[TruncUserBB]; | ||||
1988 | CastInst *&InsertedTrunc = InsertedTruncs[TruncUserBB]; | ||||
1989 | |||||
1990 | if (!InsertedShift && !InsertedTrunc) { | ||||
1991 | BasicBlock::iterator InsertPt = TruncUserBB->getFirstInsertionPt(); | ||||
1992 | assert(InsertPt != TruncUserBB->end())(static_cast <bool> (InsertPt != TruncUserBB->end()) ? void (0) : __assert_fail ("InsertPt != TruncUserBB->end()" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 1992, __extension__ __PRETTY_FUNCTION__ )); | ||||
1993 | // Sink the shift | ||||
1994 | if (ShiftI->getOpcode() == Instruction::AShr) | ||||
1995 | InsertedShift = BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, | ||||
1996 | "", &*InsertPt); | ||||
1997 | else | ||||
1998 | InsertedShift = BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, | ||||
1999 | "", &*InsertPt); | ||||
2000 | InsertedShift->setDebugLoc(ShiftI->getDebugLoc()); | ||||
2001 | |||||
2002 | // Sink the trunc | ||||
2003 | BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt(); | ||||
2004 | TruncInsertPt++; | ||||
2005 | assert(TruncInsertPt != TruncUserBB->end())(static_cast <bool> (TruncInsertPt != TruncUserBB->end ()) ? void (0) : __assert_fail ("TruncInsertPt != TruncUserBB->end()" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 2005, __extension__ __PRETTY_FUNCTION__ )); | ||||
2006 | |||||
2007 | InsertedTrunc = CastInst::Create(TruncI->getOpcode(), InsertedShift, | ||||
2008 | TruncI->getType(), "", &*TruncInsertPt); | ||||
2009 | InsertedTrunc->setDebugLoc(TruncI->getDebugLoc()); | ||||
2010 | |||||
2011 | MadeChange = true; | ||||
2012 | |||||
2013 | TruncTheUse = InsertedTrunc; | ||||
2014 | } | ||||
2015 | } | ||||
2016 | return MadeChange; | ||||
2017 | } | ||||
2018 | |||||
2019 | /// Sink the shift *right* instruction into user blocks if the uses could | ||||
2020 | /// potentially be combined with this shift instruction and generate BitExtract | ||||
2021 | /// instruction. It will only be applied if the architecture supports BitExtract | ||||
2022 | /// instruction. Here is an example: | ||||
2023 | /// BB1: | ||||
2024 | /// %x.extract.shift = lshr i64 %arg1, 32 | ||||
2025 | /// BB2: | ||||
2026 | /// %x.extract.trunc = trunc i64 %x.extract.shift to i16 | ||||
2027 | /// ==> | ||||
2028 | /// | ||||
2029 | /// BB2: | ||||
2030 | /// %x.extract.shift.1 = lshr i64 %arg1, 32 | ||||
2031 | /// %x.extract.trunc = trunc i64 %x.extract.shift.1 to i16 | ||||
2032 | /// | ||||
2033 | /// CodeGen will recognize the pattern in BB2 and generate BitExtract | ||||
2034 | /// instruction. | ||||
2035 | /// Return true if any changes are made. | ||||
2036 | static bool OptimizeExtractBits(BinaryOperator *ShiftI, ConstantInt *CI, | ||||
2037 | const TargetLowering &TLI, | ||||
2038 | const DataLayout &DL) { | ||||
2039 | BasicBlock *DefBB = ShiftI->getParent(); | ||||
2040 | |||||
2041 | /// Only insert instructions in each block once. | ||||
2042 | DenseMap<BasicBlock *, BinaryOperator *> InsertedShifts; | ||||
2043 | |||||
2044 | bool shiftIsLegal = TLI.isTypeLegal(TLI.getValueType(DL, ShiftI->getType())); | ||||
2045 | |||||
2046 | bool MadeChange = false; | ||||
2047 | for (Value::user_iterator UI = ShiftI->user_begin(), E = ShiftI->user_end(); | ||||
2048 | UI != E;) { | ||||
2049 | Use &TheUse = UI.getUse(); | ||||
2050 | Instruction *User = cast<Instruction>(*UI); | ||||
2051 | // Preincrement use iterator so we don't invalidate it. | ||||
2052 | ++UI; | ||||
2053 | |||||
2054 | // Don't bother for PHI nodes. | ||||
2055 | if (isa<PHINode>(User)) | ||||
2056 | continue; | ||||
2057 | |||||
2058 | if (!isExtractBitsCandidateUse(User)) | ||||
2059 | continue; | ||||
2060 | |||||
2061 | BasicBlock *UserBB = User->getParent(); | ||||
2062 | |||||
2063 | if (UserBB == DefBB) { | ||||
2064 | // If the shift and truncate instruction are in the same BB. The use of | ||||
2065 | // the truncate(TruncUse) may still introduce another truncate if not | ||||
2066 | // legal. In this case, we would like to sink both shift and truncate | ||||
2067 | // instruction to the BB of TruncUse. | ||||
2068 | // for example: | ||||
2069 | // BB1: | ||||
2070 | // i64 shift.result = lshr i64 opnd, imm | ||||
2071 | // trunc.result = trunc shift.result to i16 | ||||
2072 | // | ||||
2073 | // BB2: | ||||
2074 | // ----> We will have an implicit truncate here if the architecture does | ||||
2075 | // not have i16 compare. | ||||
2076 | // cmp i16 trunc.result, opnd2 | ||||
2077 | // | ||||
2078 | if (isa<TruncInst>(User) && | ||||
2079 | shiftIsLegal | ||||
2080 | // If the type of the truncate is legal, no truncate will be | ||||
2081 | // introduced in other basic blocks. | ||||
2082 | && (!TLI.isTypeLegal(TLI.getValueType(DL, User->getType())))) | ||||
2083 | MadeChange = | ||||
2084 | SinkShiftAndTruncate(ShiftI, User, CI, InsertedShifts, TLI, DL); | ||||
2085 | |||||
2086 | continue; | ||||
2087 | } | ||||
2088 | // If we have already inserted a shift into this block, use it. | ||||
2089 | BinaryOperator *&InsertedShift = InsertedShifts[UserBB]; | ||||
2090 | |||||
2091 | if (!InsertedShift) { | ||||
2092 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); | ||||
2093 | assert(InsertPt != UserBB->end())(static_cast <bool> (InsertPt != UserBB->end()) ? void (0) : __assert_fail ("InsertPt != UserBB->end()", "llvm/lib/CodeGen/CodeGenPrepare.cpp" , 2093, __extension__ __PRETTY_FUNCTION__)); | ||||
2094 | |||||
2095 | if (ShiftI->getOpcode() == Instruction::AShr) | ||||
2096 | InsertedShift = BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, | ||||
2097 | "", &*InsertPt); | ||||
2098 | else | ||||
2099 | InsertedShift = BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, | ||||
2100 | "", &*InsertPt); | ||||
2101 | InsertedShift->setDebugLoc(ShiftI->getDebugLoc()); | ||||
2102 | |||||
2103 | MadeChange = true; | ||||
2104 | } | ||||
2105 | |||||
2106 | // Replace a use of the shift with a use of the new shift. | ||||
2107 | TheUse = InsertedShift; | ||||
2108 | } | ||||
2109 | |||||
2110 | // If we removed all uses, or there are none, nuke the shift. | ||||
2111 | if (ShiftI->use_empty()) { | ||||
2112 | salvageDebugInfo(*ShiftI); | ||||
2113 | ShiftI->eraseFromParent(); | ||||
2114 | MadeChange = true; | ||||
2115 | } | ||||
2116 | |||||
2117 | return MadeChange; | ||||
2118 | } | ||||
2119 | |||||
2120 | /// If counting leading or trailing zeros is an expensive operation and a zero | ||||
2121 | /// input is defined, add a check for zero to avoid calling the intrinsic. | ||||
2122 | /// | ||||
2123 | /// We want to transform: | ||||
2124 | /// %z = call i64 @llvm.cttz.i64(i64 %A, i1 false) | ||||
2125 | /// | ||||
2126 | /// into: | ||||
2127 | /// entry: | ||||
2128 | /// %cmpz = icmp eq i64 %A, 0 | ||||
2129 | /// br i1 %cmpz, label %cond.end, label %cond.false | ||||
2130 | /// cond.false: | ||||
2131 | /// %z = call i64 @llvm.cttz.i64(i64 %A, i1 true) | ||||
2132 | /// br label %cond.end | ||||
2133 | /// cond.end: | ||||
2134 | /// %ctz = phi i64 [ 64, %entry ], [ %z, %cond.false ] | ||||
2135 | /// | ||||
2136 | /// If the transform is performed, return true and set ModifiedDT to true. | ||||
2137 | static bool despeculateCountZeros(IntrinsicInst *CountZeros, | ||||
2138 | const TargetLowering *TLI, | ||||
2139 | const DataLayout *DL, ModifyDT &ModifiedDT, | ||||
2140 | SmallSet<BasicBlock *, 32> &FreshBBs, | ||||
2141 | bool IsHugeFunc) { | ||||
2142 | // If a zero input is undefined, it doesn't make sense to despeculate that. | ||||
2143 | if (match(CountZeros->getOperand(1), m_One())) | ||||
2144 | return false; | ||||
2145 | |||||
2146 | // If it's cheap to speculate, there's nothing to do. | ||||
2147 | Type *Ty = CountZeros->getType(); | ||||
2148 | auto IntrinsicID = CountZeros->getIntrinsicID(); | ||||
2149 | if ((IntrinsicID == Intrinsic::cttz && TLI->isCheapToSpeculateCttz(Ty)) || | ||||
2150 | (IntrinsicID == Intrinsic::ctlz && TLI->isCheapToSpeculateCtlz(Ty))) | ||||
2151 | return false; | ||||
2152 | |||||
2153 | // Only handle legal scalar cases. Anything else requires too much work. | ||||
2154 | unsigned SizeInBits = Ty->getScalarSizeInBits(); | ||||
2155 | if (Ty->isVectorTy() || SizeInBits > DL->getLargestLegalIntTypeSizeInBits()) | ||||
2156 | return false; | ||||
2157 | |||||
2158 | // Bail if the value is never zero. | ||||
2159 | Use &Op = CountZeros->getOperandUse(0); | ||||
2160 | if (isKnownNonZero(Op, *DL)) | ||||
2161 | return false; | ||||
2162 | |||||
2163 | // The intrinsic will be sunk behind a compare against zero and branch. | ||||
2164 | BasicBlock *StartBlock = CountZeros->getParent(); | ||||
2165 | BasicBlock *CallBlock = StartBlock->splitBasicBlock(CountZeros, "cond.false"); | ||||
2166 | if (IsHugeFunc) | ||||
2167 | FreshBBs.insert(CallBlock); | ||||
2168 | |||||
2169 | // Create another block after the count zero intrinsic. A PHI will be added | ||||
2170 | // in this block to select the result of the intrinsic or the bit-width | ||||
2171 | // constant if the input to the intrinsic is zero. | ||||
2172 | BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(CountZeros)); | ||||
2173 | BasicBlock *EndBlock = CallBlock->splitBasicBlock(SplitPt, "cond.end"); | ||||
2174 | if (IsHugeFunc) | ||||
2175 | FreshBBs.insert(EndBlock); | ||||
2176 | |||||
2177 | // Set up a builder to create a compare, conditional branch, and PHI. | ||||
2178 | IRBuilder<> Builder(CountZeros->getContext()); | ||||
2179 | Builder.SetInsertPoint(StartBlock->getTerminator()); | ||||
2180 | Builder.SetCurrentDebugLocation(CountZeros->getDebugLoc()); | ||||
2181 | |||||
2182 | // Replace the unconditional branch that was created by the first split with | ||||
2183 | // a compare against zero and a conditional branch. | ||||
2184 | Value *Zero = Constant::getNullValue(Ty); | ||||
2185 | // Avoid introducing branch on poison. This also replaces the ctz operand. | ||||
2186 | if (!isGuaranteedNotToBeUndefOrPoison(Op)) | ||||
2187 | Op = Builder.CreateFreeze(Op, Op->getName() + ".fr"); | ||||
2188 | Value *Cmp = Builder.CreateICmpEQ(Op, Zero, "cmpz"); | ||||
2189 | Builder.CreateCondBr(Cmp, EndBlock, CallBlock); | ||||
2190 | StartBlock->getTerminator()->eraseFromParent(); | ||||
2191 | |||||
2192 | // Create a PHI in the end block to select either the output of the intrinsic | ||||
2193 | // or the bit width of the operand. | ||||
2194 | Builder.SetInsertPoint(&EndBlock->front()); | ||||
2195 | PHINode *PN = Builder.CreatePHI(Ty, 2, "ctz"); | ||||
2196 | replaceAllUsesWith(CountZeros, PN, FreshBBs, IsHugeFunc); | ||||
2197 | Value *BitWidth = Builder.getInt(APInt(SizeInBits, SizeInBits)); | ||||
2198 | PN->addIncoming(BitWidth, StartBlock); | ||||
2199 | PN->addIncoming(CountZeros, CallBlock); | ||||
2200 | |||||
2201 | // We are explicitly handling the zero case, so we can set the intrinsic's | ||||
2202 | // undefined zero argument to 'true'. This will also prevent reprocessing the | ||||
2203 | // intrinsic; we only despeculate when a zero input is defined. | ||||
2204 | CountZeros->setArgOperand(1, Builder.getTrue()); | ||||
2205 | ModifiedDT = ModifyDT::ModifyBBDT; | ||||
2206 | return true; | ||||
2207 | } | ||||
2208 | |||||
2209 | bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) { | ||||
2210 | BasicBlock *BB = CI->getParent(); | ||||
2211 | |||||
2212 | // Lower inline assembly if we can. | ||||
2213 | // If we found an inline asm expession, and if the target knows how to | ||||
2214 | // lower it to normal LLVM code, do so now. | ||||
2215 | if (CI->isInlineAsm()) { | ||||
2216 | if (TLI->ExpandInlineAsm(CI)) { | ||||
2217 | // Avoid invalidating the iterator. | ||||
2218 | CurInstIterator = BB->begin(); | ||||
2219 | // Avoid processing instructions out of order, which could cause | ||||
2220 | // reuse before a value is defined. | ||||
2221 | SunkAddrs.clear(); | ||||
2222 | return true; | ||||
2223 | } | ||||
2224 | // Sink address computing for memory operands into the block. | ||||
2225 | if (optimizeInlineAsmInst(CI)) | ||||
2226 | return true; | ||||
2227 | } | ||||
2228 | |||||
2229 | // Align the pointer arguments to this call if the target thinks it's a good | ||||
2230 | // idea | ||||
2231 | unsigned MinSize; | ||||
2232 | Align PrefAlign; | ||||
2233 | if (TLI->shouldAlignPointerArgs(CI, MinSize, PrefAlign)) { | ||||
2234 | for (auto &Arg : CI->args()) { | ||||
2235 | // We want to align both objects whose address is used directly and | ||||
2236 | // objects whose address is used in casts and GEPs, though it only makes | ||||
2237 | // sense for GEPs if the offset is a multiple of the desired alignment and | ||||
2238 | // if size - offset meets the size threshold. | ||||
2239 | if (!Arg->getType()->isPointerTy()) | ||||
2240 | continue; | ||||
2241 | APInt Offset(DL->getIndexSizeInBits( | ||||
2242 | cast<PointerType>(Arg->getType())->getAddressSpace()), | ||||
2243 | 0); | ||||
2244 | Value *Val = Arg->stripAndAccumulateInBoundsConstantOffsets(*DL, Offset); | ||||
2245 | uint64_t Offset2 = Offset.getLimitedValue(); | ||||
2246 | if (!isAligned(PrefAlign, Offset2)) | ||||
2247 | continue; | ||||
2248 | AllocaInst *AI; | ||||
2249 | if ((AI = dyn_cast<AllocaInst>(Val)) && AI->getAlign() < PrefAlign && | ||||
2250 | DL->getTypeAllocSize(AI->getAllocatedType()) >= MinSize + Offset2) | ||||
2251 | AI->setAlignment(PrefAlign); | ||||
2252 | // Global variables can only be aligned if they are defined in this | ||||
2253 | // object (i.e. they are uniquely initialized in this object), and | ||||
2254 | // over-aligning global variables that have an explicit section is | ||||
2255 | // forbidden. | ||||
2256 | GlobalVariable *GV; | ||||
2257 | if ((GV = dyn_cast<GlobalVariable>(Val)) && GV->canIncreaseAlignment() && | ||||
2258 | GV->getPointerAlignment(*DL) < PrefAlign && | ||||
2259 | DL->getTypeAllocSize(GV->getValueType()) >= MinSize + Offset2) | ||||
2260 | GV->setAlignment(PrefAlign); | ||||
2261 | } | ||||
2262 | } | ||||
2263 | // If this is a memcpy (or similar) then we may be able to improve the | ||||
2264 | // alignment. | ||||
2265 | if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(CI)) { | ||||
2266 | Align DestAlign = getKnownAlignment(MI->getDest(), *DL); | ||||
2267 | MaybeAlign MIDestAlign = MI->getDestAlign(); | ||||
2268 | if (!MIDestAlign || DestAlign > *MIDestAlign) | ||||
2269 | MI->setDestAlignment(DestAlign); | ||||
2270 | if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) { | ||||
2271 | MaybeAlign MTISrcAlign = MTI->getSourceAlign(); | ||||
2272 | Align SrcAlign = getKnownAlignment(MTI->getSource(), *DL); | ||||
2273 | if (!MTISrcAlign || SrcAlign > *MTISrcAlign) | ||||
2274 | MTI->setSourceAlignment(SrcAlign); | ||||
2275 | } | ||||
2276 | } | ||||
2277 | |||||
2278 | // If we have a cold call site, try to sink addressing computation into the | ||||
2279 | // cold block. This interacts with our handling for loads and stores to | ||||
2280 | // ensure that we can fold all uses of a potential addressing computation | ||||
2281 | // into their uses. TODO: generalize this to work over profiling data | ||||
2282 | if (CI->hasFnAttr(Attribute::Cold) && !OptSize && | ||||
2283 | !llvm::shouldOptimizeForSize(BB, PSI, BFI.get())) | ||||
2284 | for (auto &Arg : CI->args()) { | ||||
2285 | if (!Arg->getType()->isPointerTy()) | ||||
2286 | continue; | ||||
2287 | unsigned AS = Arg->getType()->getPointerAddressSpace(); | ||||
2288 | if (optimizeMemoryInst(CI, Arg, Arg->getType(), AS)) | ||||
2289 | return true; | ||||
2290 | } | ||||
2291 | |||||
2292 | IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI); | ||||
2293 | if (II) { | ||||
2294 | switch (II->getIntrinsicID()) { | ||||
2295 | default: | ||||
2296 | break; | ||||
2297 | case Intrinsic::assume: | ||||
2298 | llvm_unreachable("llvm.assume should have been removed already")::llvm::llvm_unreachable_internal("llvm.assume should have been removed already" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 2298); | ||||
2299 | case Intrinsic::experimental_widenable_condition: { | ||||
2300 | // Give up on future widening oppurtunties so that we can fold away dead | ||||
2301 | // paths and merge blocks before going into block-local instruction | ||||
2302 | // selection. | ||||
2303 | if (II->use_empty()) { | ||||
2304 | II->eraseFromParent(); | ||||
2305 | return true; | ||||
2306 | } | ||||
2307 | Constant *RetVal = ConstantInt::getTrue(II->getContext()); | ||||
2308 | resetIteratorIfInvalidatedWhileCalling(BB, [&]() { | ||||
2309 | replaceAndRecursivelySimplify(CI, RetVal, TLInfo, nullptr); | ||||
2310 | }); | ||||
2311 | return true; | ||||
2312 | } | ||||
2313 | case Intrinsic::objectsize: | ||||
2314 | llvm_unreachable("llvm.objectsize.* should have been lowered already")::llvm::llvm_unreachable_internal("llvm.objectsize.* should have been lowered already" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 2314); | ||||
2315 | case Intrinsic::is_constant: | ||||
2316 | llvm_unreachable("llvm.is.constant.* should have been lowered already")::llvm::llvm_unreachable_internal("llvm.is.constant.* should have been lowered already" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 2316); | ||||
2317 | case Intrinsic::aarch64_stlxr: | ||||
2318 | case Intrinsic::aarch64_stxr: { | ||||
2319 | ZExtInst *ExtVal = dyn_cast<ZExtInst>(CI->getArgOperand(0)); | ||||
2320 | if (!ExtVal || !ExtVal->hasOneUse() || | ||||
2321 | ExtVal->getParent() == CI->getParent()) | ||||
2322 | return false; | ||||
2323 | // Sink a zext feeding stlxr/stxr before it, so it can be folded into it. | ||||
2324 | ExtVal->moveBefore(CI); | ||||
2325 | // Mark this instruction as "inserted by CGP", so that other | ||||
2326 | // optimizations don't touch it. | ||||
2327 | InsertedInsts.insert(ExtVal); | ||||
2328 | return true; | ||||
2329 | } | ||||
2330 | |||||
2331 | case Intrinsic::launder_invariant_group: | ||||
2332 | case Intrinsic::strip_invariant_group: { | ||||
2333 | Value *ArgVal = II->getArgOperand(0); | ||||
2334 | auto it = LargeOffsetGEPMap.find(II); | ||||
2335 | if (it != LargeOffsetGEPMap.end()) { | ||||
2336 | // Merge entries in LargeOffsetGEPMap to reflect the RAUW. | ||||
2337 | // Make sure not to have to deal with iterator invalidation | ||||
2338 | // after possibly adding ArgVal to LargeOffsetGEPMap. | ||||
2339 | auto GEPs = std::move(it->second); | ||||
2340 | LargeOffsetGEPMap[ArgVal].append(GEPs.begin(), GEPs.end()); | ||||
2341 | LargeOffsetGEPMap.erase(II); | ||||
2342 | } | ||||
2343 | |||||
2344 | replaceAllUsesWith(II, ArgVal, FreshBBs, IsHugeFunc); | ||||
2345 | II->eraseFromParent(); | ||||
2346 | return true; | ||||
2347 | } | ||||
2348 | case Intrinsic::cttz: | ||||
2349 | case Intrinsic::ctlz: | ||||
2350 | // If counting zeros is expensive, try to avoid it. | ||||
2351 | return despeculateCountZeros(II, TLI, DL, ModifiedDT, FreshBBs, | ||||
2352 | IsHugeFunc); | ||||
2353 | case Intrinsic::fshl: | ||||
2354 | case Intrinsic::fshr: | ||||
2355 | return optimizeFunnelShift(II); | ||||
2356 | case Intrinsic::dbg_assign: | ||||
2357 | case Intrinsic::dbg_value: | ||||
2358 | return fixupDbgValue(II); | ||||
2359 | case Intrinsic::masked_gather: | ||||
2360 | return optimizeGatherScatterInst(II, II->getArgOperand(0)); | ||||
2361 | case Intrinsic::masked_scatter: | ||||
2362 | return optimizeGatherScatterInst(II, II->getArgOperand(1)); | ||||
2363 | } | ||||
2364 | |||||
2365 | SmallVector<Value *, 2> PtrOps; | ||||
2366 | Type *AccessTy; | ||||
2367 | if (TLI->getAddrModeArguments(II, PtrOps, AccessTy)) | ||||
2368 | while (!PtrOps.empty()) { | ||||
2369 | Value *PtrVal = PtrOps.pop_back_val(); | ||||
2370 | unsigned AS = PtrVal->getType()->getPointerAddressSpace(); | ||||
2371 | if (optimizeMemoryInst(II, PtrVal, AccessTy, AS)) | ||||
2372 | return true; | ||||
2373 | } | ||||
2374 | } | ||||
2375 | |||||
2376 | // From here on out we're working with named functions. | ||||
2377 | if (!CI->getCalledFunction()) | ||||
2378 | return false; | ||||
2379 | |||||
2380 | // Lower all default uses of _chk calls. This is very similar | ||||
2381 | // to what InstCombineCalls does, but here we are only lowering calls | ||||
2382 | // to fortified library functions (e.g. __memcpy_chk) that have the default | ||||
2383 | // "don't know" as the objectsize. Anything else should be left alone. | ||||
2384 | FortifiedLibCallSimplifier Simplifier(TLInfo, true); | ||||
2385 | IRBuilder<> Builder(CI); | ||||
2386 | if (Value *V = Simplifier.optimizeCall(CI, Builder)) { | ||||
2387 | replaceAllUsesWith(CI, V, FreshBBs, IsHugeFunc); | ||||
2388 | CI->eraseFromParent(); | ||||
2389 | return true; | ||||
2390 | } | ||||
2391 | |||||
2392 | return false; | ||||
2393 | } | ||||
2394 | |||||
2395 | /// Look for opportunities to duplicate return instructions to the predecessor | ||||
2396 | /// to enable tail call optimizations. The case it is currently looking for is: | ||||
2397 | /// @code | ||||
2398 | /// bb0: | ||||
2399 | /// %tmp0 = tail call i32 @f0() | ||||
2400 | /// br label %return | ||||
2401 | /// bb1: | ||||
2402 | /// %tmp1 = tail call i32 @f1() | ||||
2403 | /// br label %return | ||||
2404 | /// bb2: | ||||
2405 | /// %tmp2 = tail call i32 @f2() | ||||
2406 | /// br label %return | ||||
2407 | /// return: | ||||
2408 | /// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ] | ||||
2409 | /// ret i32 %retval | ||||
2410 | /// @endcode | ||||
2411 | /// | ||||
2412 | /// => | ||||
2413 | /// | ||||
2414 | /// @code | ||||
2415 | /// bb0: | ||||
2416 | /// %tmp0 = tail call i32 @f0() | ||||
2417 | /// ret i32 %tmp0 | ||||
2418 | /// bb1: | ||||
2419 | /// %tmp1 = tail call i32 @f1() | ||||
2420 | /// ret i32 %tmp1 | ||||
2421 | /// bb2: | ||||
2422 | /// %tmp2 = tail call i32 @f2() | ||||
2423 | /// ret i32 %tmp2 | ||||
2424 | /// @endcode | ||||
2425 | bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB, | ||||
2426 | ModifyDT &ModifiedDT) { | ||||
2427 | if (!BB->getTerminator()) | ||||
| |||||
2428 | return false; | ||||
2429 | |||||
2430 | ReturnInst *RetI = dyn_cast<ReturnInst>(BB->getTerminator()); | ||||
2431 | if (!RetI
| ||||
2432 | return false; | ||||
2433 | |||||
2434 | PHINode *PN = nullptr; | ||||
2435 | ExtractValueInst *EVI = nullptr; | ||||
2436 | BitCastInst *BCI = nullptr; | ||||
2437 | Value *V = RetI->getReturnValue(); | ||||
2438 | if (V
| ||||
2439 | BCI = dyn_cast<BitCastInst>(V); | ||||
2440 | if (BCI) | ||||
2441 | V = BCI->getOperand(0); | ||||
2442 | |||||
2443 | EVI = dyn_cast<ExtractValueInst>(V); | ||||
2444 | if (EVI) { | ||||
2445 | V = EVI->getOperand(0); | ||||
2446 | if (!llvm::all_of(EVI->indices(), [](unsigned idx) { return idx == 0; })) | ||||
2447 | return false; | ||||
2448 | } | ||||
2449 | |||||
2450 | PN = dyn_cast<PHINode>(V); | ||||
2451 | if (!PN) | ||||
2452 | return false; | ||||
2453 | } | ||||
2454 | |||||
2455 | if (PN
| ||||
2456 | return false; | ||||
2457 | |||||
2458 | auto isLifetimeEndOrBitCastFor = [](const Instruction *Inst) { | ||||
2459 | const BitCastInst *BC = dyn_cast<BitCastInst>(Inst); | ||||
2460 | if (BC && BC->hasOneUse()) | ||||
2461 | Inst = BC->user_back(); | ||||
2462 | |||||
2463 | if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) | ||||
2464 | return II->getIntrinsicID() == Intrinsic::lifetime_end; | ||||
2465 | return false; | ||||
2466 | }; | ||||
2467 | |||||
2468 | // Make sure there are no instructions between the first instruction | ||||
2469 | // and return. | ||||
2470 | const Instruction *BI = BB->getFirstNonPHI(); | ||||
2471 | // Skip over debug and the bitcast. | ||||
2472 | while (isa<DbgInfoIntrinsic>(BI) || BI == BCI || BI == EVI || | ||||
2473 | isa<PseudoProbeInst>(BI) || isLifetimeEndOrBitCastFor(BI)) | ||||
2474 | BI = BI->getNextNode(); | ||||
| |||||
2475 | if (BI != RetI) | ||||
2476 | return false; | ||||
2477 | |||||
2478 | /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail | ||||
2479 | /// call. | ||||
2480 | const Function *F = BB->getParent(); | ||||
2481 | SmallVector<BasicBlock *, 4> TailCallBBs; | ||||
2482 | if (PN) { | ||||
2483 | for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) { | ||||
2484 | // Look through bitcasts. | ||||
2485 | Value *IncomingVal = PN->getIncomingValue(I)->stripPointerCasts(); | ||||
2486 | CallInst *CI = dyn_cast<CallInst>(IncomingVal); | ||||
2487 | BasicBlock *PredBB = PN->getIncomingBlock(I); | ||||
2488 | // Make sure the phi value is indeed produced by the tail call. | ||||
2489 | if (CI && CI->hasOneUse() && CI->getParent() == PredBB && | ||||
2490 | TLI->mayBeEmittedAsTailCall(CI) && | ||||
2491 | attributesPermitTailCall(F, CI, RetI, *TLI)) | ||||
2492 | TailCallBBs.push_back(PredBB); | ||||
2493 | } | ||||
2494 | } else { | ||||
2495 | SmallPtrSet<BasicBlock *, 4> VisitedBBs; | ||||
2496 | for (BasicBlock *Pred : predecessors(BB)) { | ||||
2497 | if (!VisitedBBs.insert(Pred).second) | ||||
2498 | continue; | ||||
2499 | if (Instruction *I = Pred->rbegin()->getPrevNonDebugInstruction(true)) { | ||||
2500 | CallInst *CI = dyn_cast<CallInst>(I); | ||||
2501 | if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI) && | ||||
2502 | attributesPermitTailCall(F, CI, RetI, *TLI)) | ||||
2503 | TailCallBBs.push_back(Pred); | ||||
2504 | } | ||||
2505 | } | ||||
2506 | } | ||||
2507 | |||||
2508 | bool Changed = false; | ||||
2509 | for (auto const &TailCallBB : TailCallBBs) { | ||||
2510 | // Make sure the call instruction is followed by an unconditional branch to | ||||
2511 | // the return block. | ||||
2512 | BranchInst *BI = dyn_cast<BranchInst>(TailCallBB->getTerminator()); | ||||
2513 | if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB) | ||||
2514 | continue; | ||||
2515 | |||||
2516 | // Duplicate the return into TailCallBB. | ||||
2517 | (void)FoldReturnIntoUncondBranch(RetI, BB, TailCallBB); | ||||
2518 | assert(!VerifyBFIUpdates ||(static_cast <bool> (!VerifyBFIUpdates || BFI->getBlockFreq (BB) >= BFI->getBlockFreq(TailCallBB)) ? void (0) : __assert_fail ("!VerifyBFIUpdates || BFI->getBlockFreq(BB) >= BFI->getBlockFreq(TailCallBB)" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 2519, __extension__ __PRETTY_FUNCTION__ )) | ||||
2519 | BFI->getBlockFreq(BB) >= BFI->getBlockFreq(TailCallBB))(static_cast <bool> (!VerifyBFIUpdates || BFI->getBlockFreq (BB) >= BFI->getBlockFreq(TailCallBB)) ? void (0) : __assert_fail ("!VerifyBFIUpdates || BFI->getBlockFreq(BB) >= BFI->getBlockFreq(TailCallBB)" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 2519, __extension__ __PRETTY_FUNCTION__ )); | ||||
2520 | BFI->setBlockFreq( | ||||
2521 | BB, | ||||
2522 | (BFI->getBlockFreq(BB) - BFI->getBlockFreq(TailCallBB)).getFrequency()); | ||||
2523 | ModifiedDT = ModifyDT::ModifyBBDT; | ||||
2524 | Changed = true; | ||||
2525 | ++NumRetsDup; | ||||
2526 | } | ||||
2527 | |||||
2528 | // If we eliminated all predecessors of the block, delete the block now. | ||||
2529 | if (Changed && !BB->hasAddressTaken() && pred_empty(BB)) | ||||
2530 | BB->eraseFromParent(); | ||||
2531 | |||||
2532 | return Changed; | ||||
2533 | } | ||||
2534 | |||||
2535 | //===----------------------------------------------------------------------===// | ||||
2536 | // Memory Optimization | ||||
2537 | //===----------------------------------------------------------------------===// | ||||
2538 | |||||
2539 | namespace { | ||||
2540 | |||||
2541 | /// This is an extended version of TargetLowering::AddrMode | ||||
2542 | /// which holds actual Value*'s for register values. | ||||
2543 | struct ExtAddrMode : public TargetLowering::AddrMode { | ||||
2544 | Value *BaseReg = nullptr; | ||||
2545 | Value *ScaledReg = nullptr; | ||||
2546 | Value *OriginalValue = nullptr; | ||||
2547 | bool InBounds = true; | ||||
2548 | |||||
2549 | enum FieldName { | ||||
2550 | NoField = 0x00, | ||||
2551 | BaseRegField = 0x01, | ||||
2552 | BaseGVField = 0x02, | ||||
2553 | BaseOffsField = 0x04, | ||||
2554 | ScaledRegField = 0x08, | ||||
2555 | ScaleField = 0x10, | ||||
2556 | MultipleFields = 0xff | ||||
2557 | }; | ||||
2558 | |||||
2559 | ExtAddrMode() = default; | ||||
2560 | |||||
2561 | void print(raw_ostream &OS) const; | ||||
2562 | void dump() const; | ||||
2563 | |||||
2564 | FieldName compare(const ExtAddrMode &other) { | ||||
2565 | // First check that the types are the same on each field, as differing types | ||||
2566 | // is something we can't cope with later on. | ||||
2567 | if (BaseReg && other.BaseReg && | ||||
2568 | BaseReg->getType() != other.BaseReg->getType()) | ||||
2569 | return MultipleFields; | ||||
2570 | if (BaseGV && other.BaseGV && BaseGV->getType() != other.BaseGV->getType()) | ||||
2571 | return MultipleFields; | ||||
2572 | if (ScaledReg && other.ScaledReg && | ||||
2573 | ScaledReg->getType() != other.ScaledReg->getType()) | ||||
2574 | return MultipleFields; | ||||
2575 | |||||
2576 | // Conservatively reject 'inbounds' mismatches. | ||||
2577 | if (InBounds != other.InBounds) | ||||
2578 | return MultipleFields; | ||||
2579 | |||||
2580 | // Check each field to see if it differs. | ||||
2581 | unsigned Result = NoField; | ||||
2582 | if (BaseReg != other.BaseReg) | ||||
2583 | Result |= BaseRegField; | ||||
2584 | if (BaseGV != other.BaseGV) | ||||
2585 | Result |= BaseGVField; | ||||
2586 | if (BaseOffs != other.BaseOffs) | ||||
2587 | Result |= BaseOffsField; | ||||
2588 | if (ScaledReg != other.ScaledReg) | ||||
2589 | Result |= ScaledRegField; | ||||
2590 | // Don't count 0 as being a different scale, because that actually means | ||||
2591 | // unscaled (which will already be counted by having no ScaledReg). | ||||
2592 | if (Scale && other.Scale && Scale != other.Scale) | ||||
2593 | Result |= ScaleField; | ||||
2594 | |||||
2595 | if (llvm::popcount(Result) > 1) | ||||
2596 | return MultipleFields; | ||||
2597 | else | ||||
2598 | return static_cast<FieldName>(Result); | ||||
2599 | } | ||||
2600 | |||||
2601 | // An AddrMode is trivial if it involves no calculation i.e. it is just a base | ||||
2602 | // with no offset. | ||||
2603 | bool isTrivial() { | ||||
2604 | // An AddrMode is (BaseGV + BaseReg + BaseOffs + ScaleReg * Scale) so it is | ||||
2605 | // trivial if at most one of these terms is nonzero, except that BaseGV and | ||||
2606 | // BaseReg both being zero actually means a null pointer value, which we | ||||
2607 | // consider to be 'non-zero' here. | ||||
2608 | return !BaseOffs && !Scale && !(BaseGV && BaseReg); | ||||
2609 | } | ||||
2610 | |||||
2611 | Value *GetFieldAsValue(FieldName Field, Type *IntPtrTy) { | ||||
2612 | switch (Field) { | ||||
2613 | default: | ||||
2614 | return nullptr; | ||||
2615 | case BaseRegField: | ||||
2616 | return BaseReg; | ||||
2617 | case BaseGVField: | ||||
2618 | return BaseGV; | ||||
2619 | case ScaledRegField: | ||||
2620 | return ScaledReg; | ||||
2621 | case BaseOffsField: | ||||
2622 | return ConstantInt::get(IntPtrTy, BaseOffs); | ||||
2623 | } | ||||
2624 | } | ||||
2625 | |||||
2626 | void SetCombinedField(FieldName Field, Value *V, | ||||
2627 | const SmallVectorImpl<ExtAddrMode> &AddrModes) { | ||||
2628 | switch (Field) { | ||||
2629 | default: | ||||
2630 | llvm_unreachable("Unhandled fields are expected to be rejected earlier")::llvm::llvm_unreachable_internal("Unhandled fields are expected to be rejected earlier" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 2630); | ||||
2631 | break; | ||||
2632 | case ExtAddrMode::BaseRegField: | ||||
2633 | BaseReg = V; | ||||
2634 | break; | ||||
2635 | case ExtAddrMode::BaseGVField: | ||||
2636 | // A combined BaseGV is an Instruction, not a GlobalValue, so it goes | ||||
2637 | // in the BaseReg field. | ||||
2638 | assert(BaseReg == nullptr)(static_cast <bool> (BaseReg == nullptr) ? void (0) : __assert_fail ("BaseReg == nullptr", "llvm/lib/CodeGen/CodeGenPrepare.cpp" , 2638, __extension__ __PRETTY_FUNCTION__)); | ||||
2639 | BaseReg = V; | ||||
2640 | BaseGV = nullptr; | ||||
2641 | break; | ||||
2642 | case ExtAddrMode::ScaledRegField: | ||||
2643 | ScaledReg = V; | ||||
2644 | // If we have a mix of scaled and unscaled addrmodes then we want scale | ||||
2645 | // to be the scale and not zero. | ||||
2646 | if (!Scale) | ||||
2647 | for (const ExtAddrMode &AM : AddrModes) | ||||
2648 | if (AM.Scale) { | ||||
2649 | Scale = AM.Scale; | ||||
2650 | break; | ||||
2651 | } | ||||
2652 | break; | ||||
2653 | case ExtAddrMode::BaseOffsField: | ||||
2654 | // The offset is no longer a constant, so it goes in ScaledReg with a | ||||
2655 | // scale of 1. | ||||
2656 | assert(ScaledReg == nullptr)(static_cast <bool> (ScaledReg == nullptr) ? void (0) : __assert_fail ("ScaledReg == nullptr", "llvm/lib/CodeGen/CodeGenPrepare.cpp" , 2656, __extension__ __PRETTY_FUNCTION__)); | ||||
2657 | ScaledReg = V; | ||||
2658 | Scale = 1; | ||||
2659 | BaseOffs = 0; | ||||
2660 | break; | ||||
2661 | } | ||||
2662 | } | ||||
2663 | }; | ||||
2664 | |||||
2665 | #ifndef NDEBUG | ||||
2666 | static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) { | ||||
2667 | AM.print(OS); | ||||
2668 | return OS; | ||||
2669 | } | ||||
2670 | #endif | ||||
2671 | |||||
2672 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) | ||||
2673 | void ExtAddrMode::print(raw_ostream &OS) const { | ||||
2674 | bool NeedPlus = false; | ||||
2675 | OS << "["; | ||||
2676 | if (InBounds) | ||||
2677 | OS << "inbounds "; | ||||
2678 | if (BaseGV) { | ||||
2679 | OS << "GV:"; | ||||
2680 | BaseGV->printAsOperand(OS, /*PrintType=*/false); | ||||
2681 | NeedPlus = true; | ||||
2682 | } | ||||
2683 | |||||
2684 | if (BaseOffs) { | ||||
2685 | OS << (NeedPlus ? " + " : "") << BaseOffs; | ||||
2686 | NeedPlus = true; | ||||
2687 | } | ||||
2688 | |||||
2689 | if (BaseReg) { | ||||
2690 | OS << (NeedPlus ? " + " : "") << "Base:"; | ||||
2691 | BaseReg->printAsOperand(OS, /*PrintType=*/false); | ||||
2692 | NeedPlus = true; | ||||
2693 | } | ||||
2694 | if (Scale) { | ||||
2695 | OS << (NeedPlus ? " + " : "") << Scale << "*"; | ||||
2696 | ScaledReg->printAsOperand(OS, /*PrintType=*/false); | ||||
2697 | } | ||||
2698 | |||||
2699 | OS << ']'; | ||||
2700 | } | ||||
2701 | |||||
2702 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void ExtAddrMode::dump() const { | ||||
2703 | print(dbgs()); | ||||
2704 | dbgs() << '\n'; | ||||
2705 | } | ||||
2706 | #endif | ||||
2707 | |||||
2708 | } // end anonymous namespace | ||||
2709 | |||||
2710 | namespace { | ||||
2711 | |||||
2712 | /// This class provides transaction based operation on the IR. | ||||
2713 | /// Every change made through this class is recorded in the internal state and | ||||
2714 | /// can be undone (rollback) until commit is called. | ||||
2715 | /// CGP does not check if instructions could be speculatively executed when | ||||
2716 | /// moved. Preserving the original location would pessimize the debugging | ||||
2717 | /// experience, as well as negatively impact the quality of sample PGO. | ||||
2718 | class TypePromotionTransaction { | ||||
2719 | /// This represents the common interface of the individual transaction. | ||||
2720 | /// Each class implements the logic for doing one specific modification on | ||||
2721 | /// the IR via the TypePromotionTransaction. | ||||
2722 | class TypePromotionAction { | ||||
2723 | protected: | ||||
2724 | /// The Instruction modified. | ||||
2725 | Instruction *Inst; | ||||
2726 | |||||
2727 | public: | ||||
2728 | /// Constructor of the action. | ||||
2729 | /// The constructor performs the related action on the IR. | ||||
2730 | TypePromotionAction(Instruction *Inst) : Inst(Inst) {} | ||||
2731 | |||||
2732 | virtual ~TypePromotionAction() = default; | ||||
2733 | |||||
2734 | /// Undo the modification done by this action. | ||||
2735 | /// When this method is called, the IR must be in the same state as it was | ||||
2736 | /// before this action was applied. | ||||
2737 | /// \pre Undoing the action works if and only if the IR is in the exact same | ||||
2738 | /// state as it was directly after this action was applied. | ||||
2739 | virtual void undo() = 0; | ||||
2740 | |||||
2741 | /// Advocate every change made by this action. | ||||
2742 | /// When the results on the IR of the action are to be kept, it is important | ||||
2743 | /// to call this function, otherwise hidden information may be kept forever. | ||||
2744 | virtual void commit() { | ||||
2745 | // Nothing to be done, this action is not doing anything. | ||||
2746 | } | ||||
2747 | }; | ||||
2748 | |||||
2749 | /// Utility to remember the position of an instruction. | ||||
2750 | class InsertionHandler { | ||||
2751 | /// Position of an instruction. | ||||
2752 | /// Either an instruction: | ||||
2753 | /// - Is the first in a basic block: BB is used. | ||||
2754 | /// - Has a previous instruction: PrevInst is used. | ||||
2755 | union { | ||||
2756 | Instruction *PrevInst; | ||||
2757 | BasicBlock *BB; | ||||
2758 | } Point; | ||||
2759 | |||||
2760 | /// Remember whether or not the instruction had a previous instruction. | ||||
2761 | bool HasPrevInstruction; | ||||
2762 | |||||
2763 | public: | ||||
2764 | /// Record the position of \p Inst. | ||||
2765 | InsertionHandler(Instruction *Inst) { | ||||
2766 | BasicBlock::iterator It = Inst->getIterator(); | ||||
2767 | HasPrevInstruction = (It != (Inst->getParent()->begin())); | ||||
2768 | if (HasPrevInstruction) | ||||
2769 | Point.PrevInst = &*--It; | ||||
2770 | else | ||||
2771 | Point.BB = Inst->getParent(); | ||||
2772 | } | ||||
2773 | |||||
2774 | /// Insert \p Inst at the recorded position. | ||||
2775 | void insert(Instruction *Inst) { | ||||
2776 | if (HasPrevInstruction) { | ||||
2777 | if (Inst->getParent()) | ||||
2778 | Inst->removeFromParent(); | ||||
2779 | Inst->insertAfter(Point.PrevInst); | ||||
2780 | } else { | ||||
2781 | Instruction *Position = &*Point.BB->getFirstInsertionPt(); | ||||
2782 | if (Inst->getParent()) | ||||
2783 | Inst->moveBefore(Position); | ||||
2784 | else | ||||
2785 | Inst->insertBefore(Position); | ||||
2786 | } | ||||
2787 | } | ||||
2788 | }; | ||||
2789 | |||||
2790 | /// Move an instruction before another. | ||||
2791 | class InstructionMoveBefore : public TypePromotionAction { | ||||
2792 | /// Original position of the instruction. | ||||
2793 | InsertionHandler Position; | ||||
2794 | |||||
2795 | public: | ||||
2796 | /// Move \p Inst before \p Before. | ||||
2797 | InstructionMoveBefore(Instruction *Inst, Instruction *Before) | ||||
2798 | : TypePromotionAction(Inst), Position(Inst) { | ||||
2799 | LLVM_DEBUG(dbgs() << "Do: move: " << *Inst << "\nbefore: " << *Beforedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: move: " << * Inst << "\nbefore: " << *Before << "\n"; } } while (false) | ||||
2800 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: move: " << * Inst << "\nbefore: " << *Before << "\n"; } } while (false); | ||||
2801 | Inst->moveBefore(Before); | ||||
2802 | } | ||||
2803 | |||||
2804 | /// Move the instruction back to its original position. | ||||
2805 | void undo() override { | ||||
2806 | LLVM_DEBUG(dbgs() << "Undo: moveBefore: " << *Inst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: moveBefore: " << *Inst << "\n"; } } while (false); | ||||
2807 | Position.insert(Inst); | ||||
2808 | } | ||||
2809 | }; | ||||
2810 | |||||
2811 | /// Set the operand of an instruction with a new value. | ||||
2812 | class OperandSetter : public TypePromotionAction { | ||||
2813 | /// Original operand of the instruction. | ||||
2814 | Value *Origin; | ||||
2815 | |||||
2816 | /// Index of the modified instruction. | ||||
2817 | unsigned Idx; | ||||
2818 | |||||
2819 | public: | ||||
2820 | /// Set \p Idx operand of \p Inst with \p NewVal. | ||||
2821 | OperandSetter(Instruction *Inst, unsigned Idx, Value *NewVal) | ||||
2822 | : TypePromotionAction(Inst), Idx(Idx) { | ||||
2823 | LLVM_DEBUG(dbgs() << "Do: setOperand: " << Idx << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: setOperand: " << Idx << "\n" << "for:" << *Inst << "\n" << "with:" << *NewVal << "\n"; } } while ( false) | ||||
2824 | << "for:" << *Inst << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: setOperand: " << Idx << "\n" << "for:" << *Inst << "\n" << "with:" << *NewVal << "\n"; } } while ( false) | ||||
2825 | << "with:" << *NewVal << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: setOperand: " << Idx << "\n" << "for:" << *Inst << "\n" << "with:" << *NewVal << "\n"; } } while ( false); | ||||
2826 | Origin = Inst->getOperand(Idx); | ||||
2827 | Inst->setOperand(Idx, NewVal); | ||||
2828 | } | ||||
2829 | |||||
2830 | /// Restore the original value of the instruction. | ||||
2831 | void undo() override { | ||||
2832 | LLVM_DEBUG(dbgs() << "Undo: setOperand:" << Idx << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: setOperand:" << Idx << "\n" << "for: " << *Inst << "\n" << "with: " << *Origin << "\n"; } } while ( false) | ||||
2833 | << "for: " << *Inst << "\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: setOperand:" << Idx << "\n" << "for: " << *Inst << "\n" << "with: " << *Origin << "\n"; } } while ( false) | ||||
2834 | << "with: " << *Origin << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: setOperand:" << Idx << "\n" << "for: " << *Inst << "\n" << "with: " << *Origin << "\n"; } } while ( false); | ||||
2835 | Inst->setOperand(Idx, Origin); | ||||
2836 | } | ||||
2837 | }; | ||||
2838 | |||||
2839 | /// Hide the operands of an instruction. | ||||
2840 | /// Do as if this instruction was not using any of its operands. | ||||
2841 | class OperandsHider : public TypePromotionAction { | ||||
2842 | /// The list of original operands. | ||||
2843 | SmallVector<Value *, 4> OriginalValues; | ||||
2844 | |||||
2845 | public: | ||||
2846 | /// Remove \p Inst from the uses of the operands of \p Inst. | ||||
2847 | OperandsHider(Instruction *Inst) : TypePromotionAction(Inst) { | ||||
2848 | LLVM_DEBUG(dbgs() << "Do: OperandsHider: " << *Inst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: OperandsHider: " << *Inst << "\n"; } } while (false); | ||||
2849 | unsigned NumOpnds = Inst->getNumOperands(); | ||||
2850 | OriginalValues.reserve(NumOpnds); | ||||
2851 | for (unsigned It = 0; It < NumOpnds; ++It) { | ||||
2852 | // Save the current operand. | ||||
2853 | Value *Val = Inst->getOperand(It); | ||||
2854 | OriginalValues.push_back(Val); | ||||
2855 | // Set a dummy one. | ||||
2856 | // We could use OperandSetter here, but that would imply an overhead | ||||
2857 | // that we are not willing to pay. | ||||
2858 | Inst->setOperand(It, UndefValue::get(Val->getType())); | ||||
2859 | } | ||||
2860 | } | ||||
2861 | |||||
2862 | /// Restore the original list of uses. | ||||
2863 | void undo() override { | ||||
2864 | LLVM_DEBUG(dbgs() << "Undo: OperandsHider: " << *Inst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: OperandsHider: " << *Inst << "\n"; } } while (false); | ||||
2865 | for (unsigned It = 0, EndIt = OriginalValues.size(); It != EndIt; ++It) | ||||
2866 | Inst->setOperand(It, OriginalValues[It]); | ||||
2867 | } | ||||
2868 | }; | ||||
2869 | |||||
2870 | /// Build a truncate instruction. | ||||
2871 | class TruncBuilder : public TypePromotionAction { | ||||
2872 | Value *Val; | ||||
2873 | |||||
2874 | public: | ||||
2875 | /// Build a truncate instruction of \p Opnd producing a \p Ty | ||||
2876 | /// result. | ||||
2877 | /// trunc Opnd to Ty. | ||||
2878 | TruncBuilder(Instruction *Opnd, Type *Ty) : TypePromotionAction(Opnd) { | ||||
2879 | IRBuilder<> Builder(Opnd); | ||||
2880 | Builder.SetCurrentDebugLocation(DebugLoc()); | ||||
2881 | Val = Builder.CreateTrunc(Opnd, Ty, "promoted"); | ||||
2882 | LLVM_DEBUG(dbgs() << "Do: TruncBuilder: " << *Val << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: TruncBuilder: " << *Val << "\n"; } } while (false); | ||||
2883 | } | ||||
2884 | |||||
2885 | /// Get the built value. | ||||
2886 | Value *getBuiltValue() { return Val; } | ||||
2887 | |||||
2888 | /// Remove the built instruction. | ||||
2889 | void undo() override { | ||||
2890 | LLVM_DEBUG(dbgs() << "Undo: TruncBuilder: " << *Val << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: TruncBuilder: " << *Val << "\n"; } } while (false); | ||||
2891 | if (Instruction *IVal = dyn_cast<Instruction>(Val)) | ||||
2892 | IVal->eraseFromParent(); | ||||
2893 | } | ||||
2894 | }; | ||||
2895 | |||||
2896 | /// Build a sign extension instruction. | ||||
2897 | class SExtBuilder : public TypePromotionAction { | ||||
2898 | Value *Val; | ||||
2899 | |||||
2900 | public: | ||||
2901 | /// Build a sign extension instruction of \p Opnd producing a \p Ty | ||||
2902 | /// result. | ||||
2903 | /// sext Opnd to Ty. | ||||
2904 | SExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty) | ||||
2905 | : TypePromotionAction(InsertPt) { | ||||
2906 | IRBuilder<> Builder(InsertPt); | ||||
2907 | Val = Builder.CreateSExt(Opnd, Ty, "promoted"); | ||||
2908 | LLVM_DEBUG(dbgs() << "Do: SExtBuilder: " << *Val << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: SExtBuilder: " << *Val << "\n"; } } while (false); | ||||
2909 | } | ||||
2910 | |||||
2911 | /// Get the built value. | ||||
2912 | Value *getBuiltValue() { return Val; } | ||||
2913 | |||||
2914 | /// Remove the built instruction. | ||||
2915 | void undo() override { | ||||
2916 | LLVM_DEBUG(dbgs() << "Undo: SExtBuilder: " << *Val << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: SExtBuilder: " << *Val << "\n"; } } while (false); | ||||
2917 | if (Instruction *IVal = dyn_cast<Instruction>(Val)) | ||||
2918 | IVal->eraseFromParent(); | ||||
2919 | } | ||||
2920 | }; | ||||
2921 | |||||
2922 | /// Build a zero extension instruction. | ||||
2923 | class ZExtBuilder : public TypePromotionAction { | ||||
2924 | Value *Val; | ||||
2925 | |||||
2926 | public: | ||||
2927 | /// Build a zero extension instruction of \p Opnd producing a \p Ty | ||||
2928 | /// result. | ||||
2929 | /// zext Opnd to Ty. | ||||
2930 | ZExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty) | ||||
2931 | : TypePromotionAction(InsertPt) { | ||||
2932 | IRBuilder<> Builder(InsertPt); | ||||
2933 | Builder.SetCurrentDebugLocation(DebugLoc()); | ||||
2934 | Val = Builder.CreateZExt(Opnd, Ty, "promoted"); | ||||
2935 | LLVM_DEBUG(dbgs() << "Do: ZExtBuilder: " << *Val << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: ZExtBuilder: " << *Val << "\n"; } } while (false); | ||||
2936 | } | ||||
2937 | |||||
2938 | /// Get the built value. | ||||
2939 | Value *getBuiltValue() { return Val; } | ||||
2940 | |||||
2941 | /// Remove the built instruction. | ||||
2942 | void undo() override { | ||||
2943 | LLVM_DEBUG(dbgs() << "Undo: ZExtBuilder: " << *Val << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: ZExtBuilder: " << *Val << "\n"; } } while (false); | ||||
2944 | if (Instruction *IVal = dyn_cast<Instruction>(Val)) | ||||
2945 | IVal->eraseFromParent(); | ||||
2946 | } | ||||
2947 | }; | ||||
2948 | |||||
2949 | /// Mutate an instruction to another type. | ||||
2950 | class TypeMutator : public TypePromotionAction { | ||||
2951 | /// Record the original type. | ||||
2952 | Type *OrigTy; | ||||
2953 | |||||
2954 | public: | ||||
2955 | /// Mutate the type of \p Inst into \p NewTy. | ||||
2956 | TypeMutator(Instruction *Inst, Type *NewTy) | ||||
2957 | : TypePromotionAction(Inst), OrigTy(Inst->getType()) { | ||||
2958 | LLVM_DEBUG(dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTydo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTy << "\n"; } } while (false) | ||||
2959 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTy << "\n"; } } while (false); | ||||
2960 | Inst->mutateType(NewTy); | ||||
2961 | } | ||||
2962 | |||||
2963 | /// Mutate the instruction back to its original type. | ||||
2964 | void undo() override { | ||||
2965 | LLVM_DEBUG(dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTydo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTy << "\n"; } } while (false) | ||||
2966 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTy << "\n"; } } while (false); | ||||
2967 | Inst->mutateType(OrigTy); | ||||
2968 | } | ||||
2969 | }; | ||||
2970 | |||||
2971 | /// Replace the uses of an instruction by another instruction. | ||||
2972 | class UsesReplacer : public TypePromotionAction { | ||||
2973 | /// Helper structure to keep track of the replaced uses. | ||||
2974 | struct InstructionAndIdx { | ||||
2975 | /// The instruction using the instruction. | ||||
2976 | Instruction *Inst; | ||||
2977 | |||||
2978 | /// The index where this instruction is used for Inst. | ||||
2979 | unsigned Idx; | ||||
2980 | |||||
2981 | InstructionAndIdx(Instruction *Inst, unsigned Idx) | ||||
2982 | : Inst(Inst), Idx(Idx) {} | ||||
2983 | }; | ||||
2984 | |||||
2985 | /// Keep track of the original uses (pair Instruction, Index). | ||||
2986 | SmallVector<InstructionAndIdx, 4> OriginalUses; | ||||
2987 | /// Keep track of the debug users. | ||||
2988 | SmallVector<DbgValueInst *, 1> DbgValues; | ||||
2989 | |||||
2990 | /// Keep track of the new value so that we can undo it by replacing | ||||
2991 | /// instances of the new value with the original value. | ||||
2992 | Value *New; | ||||
2993 | |||||
2994 | using use_iterator = SmallVectorImpl<InstructionAndIdx>::iterator; | ||||
2995 | |||||
2996 | public: | ||||
2997 | /// Replace all the use of \p Inst by \p New. | ||||
2998 | UsesReplacer(Instruction *Inst, Value *New) | ||||
2999 | : TypePromotionAction(Inst), New(New) { | ||||
3000 | LLVM_DEBUG(dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *Newdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *New << "\n"; } } while (false) | ||||
3001 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *New << "\n"; } } while (false); | ||||
3002 | // Record the original uses. | ||||
3003 | for (Use &U : Inst->uses()) { | ||||
3004 | Instruction *UserI = cast<Instruction>(U.getUser()); | ||||
3005 | OriginalUses.push_back(InstructionAndIdx(UserI, U.getOperandNo())); | ||||
3006 | } | ||||
3007 | // Record the debug uses separately. They are not in the instruction's | ||||
3008 | // use list, but they are replaced by RAUW. | ||||
3009 | findDbgValues(DbgValues, Inst); | ||||
3010 | |||||
3011 | // Now, we can replace the uses. | ||||
3012 | Inst->replaceAllUsesWith(New); | ||||
3013 | } | ||||
3014 | |||||
3015 | /// Reassign the original uses of Inst to Inst. | ||||
3016 | void undo() override { | ||||
3017 | LLVM_DEBUG(dbgs() << "Undo: UsersReplacer: " << *Inst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: UsersReplacer: " << *Inst << "\n"; } } while (false); | ||||
3018 | for (InstructionAndIdx &Use : OriginalUses) | ||||
3019 | Use.Inst->setOperand(Use.Idx, Inst); | ||||
3020 | // RAUW has replaced all original uses with references to the new value, | ||||
3021 | // including the debug uses. Since we are undoing the replacements, | ||||
3022 | // the original debug uses must also be reinstated to maintain the | ||||
3023 | // correctness and utility of debug value instructions. | ||||
3024 | for (auto *DVI : DbgValues) | ||||
3025 | DVI->replaceVariableLocationOp(New, Inst); | ||||
3026 | } | ||||
3027 | }; | ||||
3028 | |||||
3029 | /// Remove an instruction from the IR. | ||||
3030 | class InstructionRemover : public TypePromotionAction { | ||||
3031 | /// Original position of the instruction. | ||||
3032 | InsertionHandler Inserter; | ||||
3033 | |||||
3034 | /// Helper structure to hide all the link to the instruction. In other | ||||
3035 | /// words, this helps to do as if the instruction was removed. | ||||
3036 | OperandsHider Hider; | ||||
3037 | |||||
3038 | /// Keep track of the uses replaced, if any. | ||||
3039 | UsesReplacer *Replacer = nullptr; | ||||
3040 | |||||
3041 | /// Keep track of instructions removed. | ||||
3042 | SetOfInstrs &RemovedInsts; | ||||
3043 | |||||
3044 | public: | ||||
3045 | /// Remove all reference of \p Inst and optionally replace all its | ||||
3046 | /// uses with New. | ||||
3047 | /// \p RemovedInsts Keep track of the instructions removed by this Action. | ||||
3048 | /// \pre If !Inst->use_empty(), then New != nullptr | ||||
3049 | InstructionRemover(Instruction *Inst, SetOfInstrs &RemovedInsts, | ||||
3050 | Value *New = nullptr) | ||||
3051 | : TypePromotionAction(Inst), Inserter(Inst), Hider(Inst), | ||||
3052 | RemovedInsts(RemovedInsts) { | ||||
3053 | if (New) | ||||
3054 | Replacer = new UsesReplacer(Inst, New); | ||||
3055 | LLVM_DEBUG(dbgs() << "Do: InstructionRemover: " << *Inst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Do: InstructionRemover: " << *Inst << "\n"; } } while (false); | ||||
3056 | RemovedInsts.insert(Inst); | ||||
3057 | /// The instructions removed here will be freed after completing | ||||
3058 | /// optimizeBlock() for all blocks as we need to keep track of the | ||||
3059 | /// removed instructions during promotion. | ||||
3060 | Inst->removeFromParent(); | ||||
3061 | } | ||||
3062 | |||||
3063 | ~InstructionRemover() override { delete Replacer; } | ||||
3064 | |||||
3065 | /// Resurrect the instruction and reassign it to the proper uses if | ||||
3066 | /// new value was provided when build this action. | ||||
3067 | void undo() override { | ||||
3068 | LLVM_DEBUG(dbgs() << "Undo: InstructionRemover: " << *Inst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Undo: InstructionRemover: " << *Inst << "\n"; } } while (false); | ||||
3069 | Inserter.insert(Inst); | ||||
3070 | if (Replacer) | ||||
3071 | Replacer->undo(); | ||||
3072 | Hider.undo(); | ||||
3073 | RemovedInsts.erase(Inst); | ||||
3074 | } | ||||
3075 | }; | ||||
3076 | |||||
3077 | public: | ||||
3078 | /// Restoration point. | ||||
3079 | /// The restoration point is a pointer to an action instead of an iterator | ||||
3080 | /// because the iterator may be invalidated but not the pointer. | ||||
3081 | using ConstRestorationPt = const TypePromotionAction *; | ||||
3082 | |||||
3083 | TypePromotionTransaction(SetOfInstrs &RemovedInsts) | ||||
3084 | : RemovedInsts(RemovedInsts) {} | ||||
3085 | |||||
3086 | /// Advocate every changes made in that transaction. Return true if any change | ||||
3087 | /// happen. | ||||
3088 | bool commit(); | ||||
3089 | |||||
3090 | /// Undo all the changes made after the given point. | ||||
3091 | void rollback(ConstRestorationPt Point); | ||||
3092 | |||||
3093 | /// Get the current restoration point. | ||||
3094 | ConstRestorationPt getRestorationPoint() const; | ||||
3095 | |||||
3096 | /// \name API for IR modification with state keeping to support rollback. | ||||
3097 | /// @{ | ||||
3098 | /// Same as Instruction::setOperand. | ||||
3099 | void setOperand(Instruction *Inst, unsigned Idx, Value *NewVal); | ||||
3100 | |||||
3101 | /// Same as Instruction::eraseFromParent. | ||||
3102 | void eraseInstruction(Instruction *Inst, Value *NewVal = nullptr); | ||||
3103 | |||||
3104 | /// Same as Value::replaceAllUsesWith. | ||||
3105 | void replaceAllUsesWith(Instruction *Inst, Value *New); | ||||
3106 | |||||
3107 | /// Same as Value::mutateType. | ||||
3108 | void mutateType(Instruction *Inst, Type *NewTy); | ||||
3109 | |||||
3110 | /// Same as IRBuilder::createTrunc. | ||||
3111 | Value *createTrunc(Instruction *Opnd, Type *Ty); | ||||
3112 | |||||
3113 | /// Same as IRBuilder::createSExt. | ||||
3114 | Value *createSExt(Instruction *Inst, Value *Opnd, Type *Ty); | ||||
3115 | |||||
3116 | /// Same as IRBuilder::createZExt. | ||||
3117 | Value *createZExt(Instruction *Inst, Value *Opnd, Type *Ty); | ||||
3118 | |||||
3119 | /// Same as Instruction::moveBefore. | ||||
3120 | void moveBefore(Instruction *Inst, Instruction *Before); | ||||
3121 | /// @} | ||||
3122 | |||||
3123 | private: | ||||
3124 | /// The ordered list of actions made so far. | ||||
3125 | SmallVector<std::unique_ptr<TypePromotionAction>, 16> Actions; | ||||
3126 | |||||
3127 | using CommitPt = | ||||
3128 | SmallVectorImpl<std::unique_ptr<TypePromotionAction>>::iterator; | ||||
3129 | |||||
3130 | SetOfInstrs &RemovedInsts; | ||||
3131 | }; | ||||
3132 | |||||
3133 | } // end anonymous namespace | ||||
3134 | |||||
3135 | void TypePromotionTransaction::setOperand(Instruction *Inst, unsigned Idx, | ||||
3136 | Value *NewVal) { | ||||
3137 | Actions.push_back(std::make_unique<TypePromotionTransaction::OperandSetter>( | ||||
3138 | Inst, Idx, NewVal)); | ||||
3139 | } | ||||
3140 | |||||
3141 | void TypePromotionTransaction::eraseInstruction(Instruction *Inst, | ||||
3142 | Value *NewVal) { | ||||
3143 | Actions.push_back( | ||||
3144 | std::make_unique<TypePromotionTransaction::InstructionRemover>( | ||||
3145 | Inst, RemovedInsts, NewVal)); | ||||
3146 | } | ||||
3147 | |||||
3148 | void TypePromotionTransaction::replaceAllUsesWith(Instruction *Inst, | ||||
3149 | Value *New) { | ||||
3150 | Actions.push_back( | ||||
3151 | std::make_unique<TypePromotionTransaction::UsesReplacer>(Inst, New)); | ||||
3152 | } | ||||
3153 | |||||
3154 | void TypePromotionTransaction::mutateType(Instruction *Inst, Type *NewTy) { | ||||
3155 | Actions.push_back( | ||||
3156 | std::make_unique<TypePromotionTransaction::TypeMutator>(Inst, NewTy)); | ||||
3157 | } | ||||
3158 | |||||
3159 | Value *TypePromotionTransaction::createTrunc(Instruction *Opnd, Type *Ty) { | ||||
3160 | std::unique_ptr<TruncBuilder> Ptr(new TruncBuilder(Opnd, Ty)); | ||||
3161 | Value *Val = Ptr->getBuiltValue(); | ||||
3162 | Actions.push_back(std::move(Ptr)); | ||||
3163 | return Val; | ||||
3164 | } | ||||
3165 | |||||
3166 | Value *TypePromotionTransaction::createSExt(Instruction *Inst, Value *Opnd, | ||||
3167 | Type *Ty) { | ||||
3168 | std::unique_ptr<SExtBuilder> Ptr(new SExtBuilder(Inst, Opnd, Ty)); | ||||
3169 | Value *Val = Ptr->getBuiltValue(); | ||||
3170 | Actions.push_back(std::move(Ptr)); | ||||
3171 | return Val; | ||||
3172 | } | ||||
3173 | |||||
3174 | Value *TypePromotionTransaction::createZExt(Instruction *Inst, Value *Opnd, | ||||
3175 | Type *Ty) { | ||||
3176 | std::unique_ptr<ZExtBuilder> Ptr(new ZExtBuilder(Inst, Opnd, Ty)); | ||||
3177 | Value *Val = Ptr->getBuiltValue(); | ||||
3178 | Actions.push_back(std::move(Ptr)); | ||||
3179 | return Val; | ||||
3180 | } | ||||
3181 | |||||
3182 | void TypePromotionTransaction::moveBefore(Instruction *Inst, | ||||
3183 | Instruction *Before) { | ||||
3184 | Actions.push_back( | ||||
3185 | std::make_unique<TypePromotionTransaction::InstructionMoveBefore>( | ||||
3186 | Inst, Before)); | ||||
3187 | } | ||||
3188 | |||||
3189 | TypePromotionTransaction::ConstRestorationPt | ||||
3190 | TypePromotionTransaction::getRestorationPoint() const { | ||||
3191 | return !Actions.empty() ? Actions.back().get() : nullptr; | ||||
3192 | } | ||||
3193 | |||||
3194 | bool TypePromotionTransaction::commit() { | ||||
3195 | for (std::unique_ptr<TypePromotionAction> &Action : Actions) | ||||
3196 | Action->commit(); | ||||
3197 | bool Modified = !Actions.empty(); | ||||
3198 | Actions.clear(); | ||||
3199 | return Modified; | ||||
3200 | } | ||||
3201 | |||||
3202 | void TypePromotionTransaction::rollback( | ||||
3203 | TypePromotionTransaction::ConstRestorationPt Point) { | ||||
3204 | while (!Actions.empty() && Point != Actions.back().get()) { | ||||
3205 | std::unique_ptr<TypePromotionAction> Curr = Actions.pop_back_val(); | ||||
3206 | Curr->undo(); | ||||
3207 | } | ||||
3208 | } | ||||
3209 | |||||
3210 | namespace { | ||||
3211 | |||||
3212 | /// A helper class for matching addressing modes. | ||||
3213 | /// | ||||
3214 | /// This encapsulates the logic for matching the target-legal addressing modes. | ||||
3215 | class AddressingModeMatcher { | ||||
3216 | SmallVectorImpl<Instruction *> &AddrModeInsts; | ||||
3217 | const TargetLowering &TLI; | ||||
3218 | const TargetRegisterInfo &TRI; | ||||
3219 | const DataLayout &DL; | ||||
3220 | const LoopInfo &LI; | ||||
3221 | const std::function<const DominatorTree &()> getDTFn; | ||||
3222 | |||||
3223 | /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and | ||||
3224 | /// the memory instruction that we're computing this address for. | ||||
3225 | Type *AccessTy; | ||||
3226 | unsigned AddrSpace; | ||||
3227 | Instruction *MemoryInst; | ||||
3228 | |||||
3229 | /// This is the addressing mode that we're building up. This is | ||||
3230 | /// part of the return value of this addressing mode matching stuff. | ||||
3231 | ExtAddrMode &AddrMode; | ||||
3232 | |||||
3233 | /// The instructions inserted by other CodeGenPrepare optimizations. | ||||
3234 | const SetOfInstrs &InsertedInsts; | ||||
3235 | |||||
3236 | /// A map from the instructions to their type before promotion. | ||||
3237 | InstrToOrigTy &PromotedInsts; | ||||
3238 | |||||
3239 | /// The ongoing transaction where every action should be registered. | ||||
3240 | TypePromotionTransaction &TPT; | ||||
3241 | |||||
3242 | // A GEP which has too large offset to be folded into the addressing mode. | ||||
3243 | std::pair<AssertingVH<GetElementPtrInst>, int64_t> &LargeOffsetGEP; | ||||
3244 | |||||
3245 | /// This is set to true when we should not do profitability checks. | ||||
3246 | /// When true, IsProfitableToFoldIntoAddressingMode always returns true. | ||||
3247 | bool IgnoreProfitability; | ||||
3248 | |||||
3249 | /// True if we are optimizing for size. | ||||
3250 | bool OptSize = false; | ||||
3251 | |||||
3252 | ProfileSummaryInfo *PSI; | ||||
3253 | BlockFrequencyInfo *BFI; | ||||
3254 | |||||
3255 | AddressingModeMatcher( | ||||
3256 | SmallVectorImpl<Instruction *> &AMI, const TargetLowering &TLI, | ||||
3257 | const TargetRegisterInfo &TRI, const LoopInfo &LI, | ||||
3258 | const std::function<const DominatorTree &()> getDTFn, Type *AT, | ||||
3259 | unsigned AS, Instruction *MI, ExtAddrMode &AM, | ||||
3260 | const SetOfInstrs &InsertedInsts, InstrToOrigTy &PromotedInsts, | ||||
3261 | TypePromotionTransaction &TPT, | ||||
3262 | std::pair<AssertingVH<GetElementPtrInst>, int64_t> &LargeOffsetGEP, | ||||
3263 | bool OptSize, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) | ||||
3264 | : AddrModeInsts(AMI), TLI(TLI), TRI(TRI), | ||||
3265 | DL(MI->getModule()->getDataLayout()), LI(LI), getDTFn(getDTFn), | ||||
3266 | AccessTy(AT), AddrSpace(AS), MemoryInst(MI), AddrMode(AM), | ||||
3267 | InsertedInsts(InsertedInsts), PromotedInsts(PromotedInsts), TPT(TPT), | ||||
3268 | LargeOffsetGEP(LargeOffsetGEP), OptSize(OptSize), PSI(PSI), BFI(BFI) { | ||||
3269 | IgnoreProfitability = false; | ||||
3270 | } | ||||
3271 | |||||
3272 | public: | ||||
3273 | /// Find the maximal addressing mode that a load/store of V can fold, | ||||
3274 | /// give an access type of AccessTy. This returns a list of involved | ||||
3275 | /// instructions in AddrModeInsts. | ||||
3276 | /// \p InsertedInsts The instructions inserted by other CodeGenPrepare | ||||
3277 | /// optimizations. | ||||
3278 | /// \p PromotedInsts maps the instructions to their type before promotion. | ||||
3279 | /// \p The ongoing transaction where every action should be registered. | ||||
3280 | static ExtAddrMode | ||||
3281 | Match(Value *V, Type *AccessTy, unsigned AS, Instruction *MemoryInst, | ||||
3282 | SmallVectorImpl<Instruction *> &AddrModeInsts, | ||||
3283 | const TargetLowering &TLI, const LoopInfo &LI, | ||||
3284 | const std::function<const DominatorTree &()> getDTFn, | ||||
3285 | const TargetRegisterInfo &TRI, const SetOfInstrs &InsertedInsts, | ||||
3286 | InstrToOrigTy &PromotedInsts, TypePromotionTransaction &TPT, | ||||
3287 | std::pair<AssertingVH<GetElementPtrInst>, int64_t> &LargeOffsetGEP, | ||||
3288 | bool OptSize, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) { | ||||
3289 | ExtAddrMode Result; | ||||
3290 | |||||
3291 | bool Success = AddressingModeMatcher(AddrModeInsts, TLI, TRI, LI, getDTFn, | ||||
3292 | AccessTy, AS, MemoryInst, Result, | ||||
3293 | InsertedInsts, PromotedInsts, TPT, | ||||
3294 | LargeOffsetGEP, OptSize, PSI, BFI) | ||||
3295 | .matchAddr(V, 0); | ||||
3296 | (void)Success; | ||||
3297 | assert(Success && "Couldn't select *anything*?")(static_cast <bool> (Success && "Couldn't select *anything*?" ) ? void (0) : __assert_fail ("Success && \"Couldn't select *anything*?\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3297, __extension__ __PRETTY_FUNCTION__ )); | ||||
3298 | return Result; | ||||
3299 | } | ||||
3300 | |||||
3301 | private: | ||||
3302 | bool matchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth); | ||||
3303 | bool matchAddr(Value *Addr, unsigned Depth); | ||||
3304 | bool matchOperationAddr(User *AddrInst, unsigned Opcode, unsigned Depth, | ||||
3305 | bool *MovedAway = nullptr); | ||||
3306 | bool isProfitableToFoldIntoAddressingMode(Instruction *I, | ||||
3307 | ExtAddrMode &AMBefore, | ||||
3308 | ExtAddrMode &AMAfter); | ||||
3309 | bool valueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2); | ||||
3310 | bool isPromotionProfitable(unsigned NewCost, unsigned OldCost, | ||||
3311 | Value *PromotedOperand) const; | ||||
3312 | }; | ||||
3313 | |||||
3314 | class PhiNodeSet; | ||||
3315 | |||||
3316 | /// An iterator for PhiNodeSet. | ||||
3317 | class PhiNodeSetIterator { | ||||
3318 | PhiNodeSet *const Set; | ||||
3319 | size_t CurrentIndex = 0; | ||||
3320 | |||||
3321 | public: | ||||
3322 | /// The constructor. Start should point to either a valid element, or be equal | ||||
3323 | /// to the size of the underlying SmallVector of the PhiNodeSet. | ||||
3324 | PhiNodeSetIterator(PhiNodeSet *const Set, size_t Start); | ||||
3325 | PHINode *operator*() const; | ||||
3326 | PhiNodeSetIterator &operator++(); | ||||
3327 | bool operator==(const PhiNodeSetIterator &RHS) const; | ||||
3328 | bool operator!=(const PhiNodeSetIterator &RHS) const; | ||||
3329 | }; | ||||
3330 | |||||
3331 | /// Keeps a set of PHINodes. | ||||
3332 | /// | ||||
3333 | /// This is a minimal set implementation for a specific use case: | ||||
3334 | /// It is very fast when there are very few elements, but also provides good | ||||
3335 | /// performance when there are many. It is similar to SmallPtrSet, but also | ||||
3336 | /// provides iteration by insertion order, which is deterministic and stable | ||||
3337 | /// across runs. It is also similar to SmallSetVector, but provides removing | ||||
3338 | /// elements in O(1) time. This is achieved by not actually removing the element | ||||
3339 | /// from the underlying vector, so comes at the cost of using more memory, but | ||||
3340 | /// that is fine, since PhiNodeSets are used as short lived objects. | ||||
3341 | class PhiNodeSet { | ||||
3342 | friend class PhiNodeSetIterator; | ||||
3343 | |||||
3344 | using MapType = SmallDenseMap<PHINode *, size_t, 32>; | ||||
3345 | using iterator = PhiNodeSetIterator; | ||||
3346 | |||||
3347 | /// Keeps the elements in the order of their insertion in the underlying | ||||
3348 | /// vector. To achieve constant time removal, it never deletes any element. | ||||
3349 | SmallVector<PHINode *, 32> NodeList; | ||||
3350 | |||||
3351 | /// Keeps the elements in the underlying set implementation. This (and not the | ||||
3352 | /// NodeList defined above) is the source of truth on whether an element | ||||
3353 | /// is actually in the collection. | ||||
3354 | MapType NodeMap; | ||||
3355 | |||||
3356 | /// Points to the first valid (not deleted) element when the set is not empty | ||||
3357 | /// and the value is not zero. Equals to the size of the underlying vector | ||||
3358 | /// when the set is empty. When the value is 0, as in the beginning, the | ||||
3359 | /// first element may or may not be valid. | ||||
3360 | size_t FirstValidElement = 0; | ||||
3361 | |||||
3362 | public: | ||||
3363 | /// Inserts a new element to the collection. | ||||
3364 | /// \returns true if the element is actually added, i.e. was not in the | ||||
3365 | /// collection before the operation. | ||||
3366 | bool insert(PHINode *Ptr) { | ||||
3367 | if (NodeMap.insert(std::make_pair(Ptr, NodeList.size())).second) { | ||||
3368 | NodeList.push_back(Ptr); | ||||
3369 | return true; | ||||
3370 | } | ||||
3371 | return false; | ||||
3372 | } | ||||
3373 | |||||
3374 | /// Removes the element from the collection. | ||||
3375 | /// \returns whether the element is actually removed, i.e. was in the | ||||
3376 | /// collection before the operation. | ||||
3377 | bool erase(PHINode *Ptr) { | ||||
3378 | if (NodeMap.erase(Ptr)) { | ||||
3379 | SkipRemovedElements(FirstValidElement); | ||||
3380 | return true; | ||||
3381 | } | ||||
3382 | return false; | ||||
3383 | } | ||||
3384 | |||||
3385 | /// Removes all elements and clears the collection. | ||||
3386 | void clear() { | ||||
3387 | NodeMap.clear(); | ||||
3388 | NodeList.clear(); | ||||
3389 | FirstValidElement = 0; | ||||
3390 | } | ||||
3391 | |||||
3392 | /// \returns an iterator that will iterate the elements in the order of | ||||
3393 | /// insertion. | ||||
3394 | iterator begin() { | ||||
3395 | if (FirstValidElement == 0) | ||||
3396 | SkipRemovedElements(FirstValidElement); | ||||
3397 | return PhiNodeSetIterator(this, FirstValidElement); | ||||
3398 | } | ||||
3399 | |||||
3400 | /// \returns an iterator that points to the end of the collection. | ||||
3401 | iterator end() { return PhiNodeSetIterator(this, NodeList.size()); } | ||||
3402 | |||||
3403 | /// Returns the number of elements in the collection. | ||||
3404 | size_t size() const { return NodeMap.size(); } | ||||
3405 | |||||
3406 | /// \returns 1 if the given element is in the collection, and 0 if otherwise. | ||||
3407 | size_t count(PHINode *Ptr) const { return NodeMap.count(Ptr); } | ||||
3408 | |||||
3409 | private: | ||||
3410 | /// Updates the CurrentIndex so that it will point to a valid element. | ||||
3411 | /// | ||||
3412 | /// If the element of NodeList at CurrentIndex is valid, it does not | ||||
3413 | /// change it. If there are no more valid elements, it updates CurrentIndex | ||||
3414 | /// to point to the end of the NodeList. | ||||
3415 | void SkipRemovedElements(size_t &CurrentIndex) { | ||||
3416 | while (CurrentIndex < NodeList.size()) { | ||||
3417 | auto it = NodeMap.find(NodeList[CurrentIndex]); | ||||
3418 | // If the element has been deleted and added again later, NodeMap will | ||||
3419 | // point to a different index, so CurrentIndex will still be invalid. | ||||
3420 | if (it != NodeMap.end() && it->second == CurrentIndex) | ||||
3421 | break; | ||||
3422 | ++CurrentIndex; | ||||
3423 | } | ||||
3424 | } | ||||
3425 | }; | ||||
3426 | |||||
3427 | PhiNodeSetIterator::PhiNodeSetIterator(PhiNodeSet *const Set, size_t Start) | ||||
3428 | : Set(Set), CurrentIndex(Start) {} | ||||
3429 | |||||
3430 | PHINode *PhiNodeSetIterator::operator*() const { | ||||
3431 | assert(CurrentIndex < Set->NodeList.size() &&(static_cast <bool> (CurrentIndex < Set->NodeList .size() && "PhiNodeSet access out of range") ? void ( 0) : __assert_fail ("CurrentIndex < Set->NodeList.size() && \"PhiNodeSet access out of range\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3432, __extension__ __PRETTY_FUNCTION__ )) | ||||
3432 | "PhiNodeSet access out of range")(static_cast <bool> (CurrentIndex < Set->NodeList .size() && "PhiNodeSet access out of range") ? void ( 0) : __assert_fail ("CurrentIndex < Set->NodeList.size() && \"PhiNodeSet access out of range\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3432, __extension__ __PRETTY_FUNCTION__ )); | ||||
3433 | return Set->NodeList[CurrentIndex]; | ||||
3434 | } | ||||
3435 | |||||
3436 | PhiNodeSetIterator &PhiNodeSetIterator::operator++() { | ||||
3437 | assert(CurrentIndex < Set->NodeList.size() &&(static_cast <bool> (CurrentIndex < Set->NodeList .size() && "PhiNodeSet access out of range") ? void ( 0) : __assert_fail ("CurrentIndex < Set->NodeList.size() && \"PhiNodeSet access out of range\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3438, __extension__ __PRETTY_FUNCTION__ )) | ||||
3438 | "PhiNodeSet access out of range")(static_cast <bool> (CurrentIndex < Set->NodeList .size() && "PhiNodeSet access out of range") ? void ( 0) : __assert_fail ("CurrentIndex < Set->NodeList.size() && \"PhiNodeSet access out of range\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3438, __extension__ __PRETTY_FUNCTION__ )); | ||||
3439 | ++CurrentIndex; | ||||
3440 | Set->SkipRemovedElements(CurrentIndex); | ||||
3441 | return *this; | ||||
3442 | } | ||||
3443 | |||||
3444 | bool PhiNodeSetIterator::operator==(const PhiNodeSetIterator &RHS) const { | ||||
3445 | return CurrentIndex == RHS.CurrentIndex; | ||||
3446 | } | ||||
3447 | |||||
3448 | bool PhiNodeSetIterator::operator!=(const PhiNodeSetIterator &RHS) const { | ||||
3449 | return !((*this) == RHS); | ||||
3450 | } | ||||
3451 | |||||
3452 | /// Keep track of simplification of Phi nodes. | ||||
3453 | /// Accept the set of all phi nodes and erase phi node from this set | ||||
3454 | /// if it is simplified. | ||||
3455 | class SimplificationTracker { | ||||
3456 | DenseMap<Value *, Value *> Storage; | ||||
3457 | const SimplifyQuery &SQ; | ||||
3458 | // Tracks newly created Phi nodes. The elements are iterated by insertion | ||||
3459 | // order. | ||||
3460 | PhiNodeSet AllPhiNodes; | ||||
3461 | // Tracks newly created Select nodes. | ||||
3462 | SmallPtrSet<SelectInst *, 32> AllSelectNodes; | ||||
3463 | |||||
3464 | public: | ||||
3465 | SimplificationTracker(const SimplifyQuery &sq) : SQ(sq) {} | ||||
3466 | |||||
3467 | Value *Get(Value *V) { | ||||
3468 | do { | ||||
3469 | auto SV = Storage.find(V); | ||||
3470 | if (SV == Storage.end()) | ||||
3471 | return V; | ||||
3472 | V = SV->second; | ||||
3473 | } while (true); | ||||
3474 | } | ||||
3475 | |||||
3476 | Value *Simplify(Value *Val) { | ||||
3477 | SmallVector<Value *, 32> WorkList; | ||||
3478 | SmallPtrSet<Value *, 32> Visited; | ||||
3479 | WorkList.push_back(Val); | ||||
3480 | while (!WorkList.empty()) { | ||||
3481 | auto *P = WorkList.pop_back_val(); | ||||
3482 | if (!Visited.insert(P).second) | ||||
3483 | continue; | ||||
3484 | if (auto *PI = dyn_cast<Instruction>(P)) | ||||
3485 | if (Value *V = simplifyInstruction(cast<Instruction>(PI), SQ)) { | ||||
3486 | for (auto *U : PI->users()) | ||||
3487 | WorkList.push_back(cast<Value>(U)); | ||||
3488 | Put(PI, V); | ||||
3489 | PI->replaceAllUsesWith(V); | ||||
3490 | if (auto *PHI = dyn_cast<PHINode>(PI)) | ||||
3491 | AllPhiNodes.erase(PHI); | ||||
3492 | if (auto *Select = dyn_cast<SelectInst>(PI)) | ||||
3493 | AllSelectNodes.erase(Select); | ||||
3494 | PI->eraseFromParent(); | ||||
3495 | } | ||||
3496 | } | ||||
3497 | return Get(Val); | ||||
3498 | } | ||||
3499 | |||||
3500 | void Put(Value *From, Value *To) { Storage.insert({From, To}); } | ||||
3501 | |||||
3502 | void ReplacePhi(PHINode *From, PHINode *To) { | ||||
3503 | Value *OldReplacement = Get(From); | ||||
3504 | while (OldReplacement != From) { | ||||
3505 | From = To; | ||||
3506 | To = dyn_cast<PHINode>(OldReplacement); | ||||
3507 | OldReplacement = Get(From); | ||||
3508 | } | ||||
3509 | assert(To && Get(To) == To && "Replacement PHI node is already replaced.")(static_cast <bool> (To && Get(To) == To && "Replacement PHI node is already replaced.") ? void (0) : __assert_fail ("To && Get(To) == To && \"Replacement PHI node is already replaced.\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3509, __extension__ __PRETTY_FUNCTION__ )); | ||||
3510 | Put(From, To); | ||||
3511 | From->replaceAllUsesWith(To); | ||||
3512 | AllPhiNodes.erase(From); | ||||
3513 | From->eraseFromParent(); | ||||
3514 | } | ||||
3515 | |||||
3516 | PhiNodeSet &newPhiNodes() { return AllPhiNodes; } | ||||
3517 | |||||
3518 | void insertNewPhi(PHINode *PN) { AllPhiNodes.insert(PN); } | ||||
3519 | |||||
3520 | void insertNewSelect(SelectInst *SI) { AllSelectNodes.insert(SI); } | ||||
3521 | |||||
3522 | unsigned countNewPhiNodes() const { return AllPhiNodes.size(); } | ||||
3523 | |||||
3524 | unsigned countNewSelectNodes() const { return AllSelectNodes.size(); } | ||||
3525 | |||||
3526 | void destroyNewNodes(Type *CommonType) { | ||||
3527 | // For safe erasing, replace the uses with dummy value first. | ||||
3528 | auto *Dummy = PoisonValue::get(CommonType); | ||||
3529 | for (auto *I : AllPhiNodes) { | ||||
3530 | I->replaceAllUsesWith(Dummy); | ||||
3531 | I->eraseFromParent(); | ||||
3532 | } | ||||
3533 | AllPhiNodes.clear(); | ||||
3534 | for (auto *I : AllSelectNodes) { | ||||
3535 | I->replaceAllUsesWith(Dummy); | ||||
3536 | I->eraseFromParent(); | ||||
3537 | } | ||||
3538 | AllSelectNodes.clear(); | ||||
3539 | } | ||||
3540 | }; | ||||
3541 | |||||
3542 | /// A helper class for combining addressing modes. | ||||
3543 | class AddressingModeCombiner { | ||||
3544 | typedef DenseMap<Value *, Value *> FoldAddrToValueMapping; | ||||
3545 | typedef std::pair<PHINode *, PHINode *> PHIPair; | ||||
3546 | |||||
3547 | private: | ||||
3548 | /// The addressing modes we've collected. | ||||
3549 | SmallVector<ExtAddrMode, 16> AddrModes; | ||||
3550 | |||||
3551 | /// The field in which the AddrModes differ, when we have more than one. | ||||
3552 | ExtAddrMode::FieldName DifferentField = ExtAddrMode::NoField; | ||||
3553 | |||||
3554 | /// Are the AddrModes that we have all just equal to their original values? | ||||
3555 | bool AllAddrModesTrivial = true; | ||||
3556 | |||||
3557 | /// Common Type for all different fields in addressing modes. | ||||
3558 | Type *CommonType = nullptr; | ||||
3559 | |||||
3560 | /// SimplifyQuery for simplifyInstruction utility. | ||||
3561 | const SimplifyQuery &SQ; | ||||
3562 | |||||
3563 | /// Original Address. | ||||
3564 | Value *Original; | ||||
3565 | |||||
3566 | /// Common value among addresses | ||||
3567 | Value *CommonValue = nullptr; | ||||
3568 | |||||
3569 | public: | ||||
3570 | AddressingModeCombiner(const SimplifyQuery &_SQ, Value *OriginalValue) | ||||
3571 | : SQ(_SQ), Original(OriginalValue) {} | ||||
3572 | |||||
3573 | ~AddressingModeCombiner() { eraseCommonValueIfDead(); } | ||||
3574 | |||||
3575 | /// Get the combined AddrMode | ||||
3576 | const ExtAddrMode &getAddrMode() const { return AddrModes[0]; } | ||||
3577 | |||||
3578 | /// Add a new AddrMode if it's compatible with the AddrModes we already | ||||
3579 | /// have. | ||||
3580 | /// \return True iff we succeeded in doing so. | ||||
3581 | bool addNewAddrMode(ExtAddrMode &NewAddrMode) { | ||||
3582 | // Take note of if we have any non-trivial AddrModes, as we need to detect | ||||
3583 | // when all AddrModes are trivial as then we would introduce a phi or select | ||||
3584 | // which just duplicates what's already there. | ||||
3585 | AllAddrModesTrivial = AllAddrModesTrivial && NewAddrMode.isTrivial(); | ||||
3586 | |||||
3587 | // If this is the first addrmode then everything is fine. | ||||
3588 | if (AddrModes.empty()) { | ||||
3589 | AddrModes.emplace_back(NewAddrMode); | ||||
3590 | return true; | ||||
3591 | } | ||||
3592 | |||||
3593 | // Figure out how different this is from the other address modes, which we | ||||
3594 | // can do just by comparing against the first one given that we only care | ||||
3595 | // about the cumulative difference. | ||||
3596 | ExtAddrMode::FieldName ThisDifferentField = | ||||
3597 | AddrModes[0].compare(NewAddrMode); | ||||
3598 | if (DifferentField == ExtAddrMode::NoField) | ||||
3599 | DifferentField = ThisDifferentField; | ||||
3600 | else if (DifferentField != ThisDifferentField) | ||||
3601 | DifferentField = ExtAddrMode::MultipleFields; | ||||
3602 | |||||
3603 | // If NewAddrMode differs in more than one dimension we cannot handle it. | ||||
3604 | bool CanHandle = DifferentField != ExtAddrMode::MultipleFields; | ||||
3605 | |||||
3606 | // If Scale Field is different then we reject. | ||||
3607 | CanHandle = CanHandle && DifferentField != ExtAddrMode::ScaleField; | ||||
3608 | |||||
3609 | // We also must reject the case when base offset is different and | ||||
3610 | // scale reg is not null, we cannot handle this case due to merge of | ||||
3611 | // different offsets will be used as ScaleReg. | ||||
3612 | CanHandle = CanHandle && (DifferentField != ExtAddrMode::BaseOffsField || | ||||
3613 | !NewAddrMode.ScaledReg); | ||||
3614 | |||||
3615 | // We also must reject the case when GV is different and BaseReg installed | ||||
3616 | // due to we want to use base reg as a merge of GV values. | ||||
3617 | CanHandle = CanHandle && (DifferentField != ExtAddrMode::BaseGVField || | ||||
3618 | !NewAddrMode.HasBaseReg); | ||||
3619 | |||||
3620 | // Even if NewAddMode is the same we still need to collect it due to | ||||
3621 | // original value is different. And later we will need all original values | ||||
3622 | // as anchors during finding the common Phi node. | ||||
3623 | if (CanHandle) | ||||
3624 | AddrModes.emplace_back(NewAddrMode); | ||||
3625 | else | ||||
3626 | AddrModes.clear(); | ||||
3627 | |||||
3628 | return CanHandle; | ||||
3629 | } | ||||
3630 | |||||
3631 | /// Combine the addressing modes we've collected into a single | ||||
3632 | /// addressing mode. | ||||
3633 | /// \return True iff we successfully combined them or we only had one so | ||||
3634 | /// didn't need to combine them anyway. | ||||
3635 | bool combineAddrModes() { | ||||
3636 | // If we have no AddrModes then they can't be combined. | ||||
3637 | if (AddrModes.size() == 0) | ||||
3638 | return false; | ||||
3639 | |||||
3640 | // A single AddrMode can trivially be combined. | ||||
3641 | if (AddrModes.size() == 1 || DifferentField == ExtAddrMode::NoField) | ||||
3642 | return true; | ||||
3643 | |||||
3644 | // If the AddrModes we collected are all just equal to the value they are | ||||
3645 | // derived from then combining them wouldn't do anything useful. | ||||
3646 | if (AllAddrModesTrivial) | ||||
3647 | return false; | ||||
3648 | |||||
3649 | if (!addrModeCombiningAllowed()) | ||||
3650 | return false; | ||||
3651 | |||||
3652 | // Build a map between <original value, basic block where we saw it> to | ||||
3653 | // value of base register. | ||||
3654 | // Bail out if there is no common type. | ||||
3655 | FoldAddrToValueMapping Map; | ||||
3656 | if (!initializeMap(Map)) | ||||
3657 | return false; | ||||
3658 | |||||
3659 | CommonValue = findCommon(Map); | ||||
3660 | if (CommonValue) | ||||
3661 | AddrModes[0].SetCombinedField(DifferentField, CommonValue, AddrModes); | ||||
3662 | return CommonValue != nullptr; | ||||
3663 | } | ||||
3664 | |||||
3665 | private: | ||||
3666 | /// `CommonValue` may be a placeholder inserted by us. | ||||
3667 | /// If the placeholder is not used, we should remove this dead instruction. | ||||
3668 | void eraseCommonValueIfDead() { | ||||
3669 | if (CommonValue && CommonValue->getNumUses() == 0) | ||||
3670 | if (Instruction *CommonInst = dyn_cast<Instruction>(CommonValue)) | ||||
3671 | CommonInst->eraseFromParent(); | ||||
3672 | } | ||||
3673 | |||||
3674 | /// Initialize Map with anchor values. For address seen | ||||
3675 | /// we set the value of different field saw in this address. | ||||
3676 | /// At the same time we find a common type for different field we will | ||||
3677 | /// use to create new Phi/Select nodes. Keep it in CommonType field. | ||||
3678 | /// Return false if there is no common type found. | ||||
3679 | bool initializeMap(FoldAddrToValueMapping &Map) { | ||||
3680 | // Keep track of keys where the value is null. We will need to replace it | ||||
3681 | // with constant null when we know the common type. | ||||
3682 | SmallVector<Value *, 2> NullValue; | ||||
3683 | Type *IntPtrTy = SQ.DL.getIntPtrType(AddrModes[0].OriginalValue->getType()); | ||||
3684 | for (auto &AM : AddrModes) { | ||||
3685 | Value *DV = AM.GetFieldAsValue(DifferentField, IntPtrTy); | ||||
3686 | if (DV) { | ||||
3687 | auto *Type = DV->getType(); | ||||
3688 | if (CommonType && CommonType != Type) | ||||
3689 | return false; | ||||
3690 | CommonType = Type; | ||||
3691 | Map[AM.OriginalValue] = DV; | ||||
3692 | } else { | ||||
3693 | NullValue.push_back(AM.OriginalValue); | ||||
3694 | } | ||||
3695 | } | ||||
3696 | assert(CommonType && "At least one non-null value must be!")(static_cast <bool> (CommonType && "At least one non-null value must be!" ) ? void (0) : __assert_fail ("CommonType && \"At least one non-null value must be!\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3696, __extension__ __PRETTY_FUNCTION__ )); | ||||
3697 | for (auto *V : NullValue) | ||||
3698 | Map[V] = Constant::getNullValue(CommonType); | ||||
3699 | return true; | ||||
3700 | } | ||||
3701 | |||||
3702 | /// We have mapping between value A and other value B where B was a field in | ||||
3703 | /// addressing mode represented by A. Also we have an original value C | ||||
3704 | /// representing an address we start with. Traversing from C through phi and | ||||
3705 | /// selects we ended up with A's in a map. This utility function tries to find | ||||
3706 | /// a value V which is a field in addressing mode C and traversing through phi | ||||
3707 | /// nodes and selects we will end up in corresponded values B in a map. | ||||
3708 | /// The utility will create a new Phi/Selects if needed. | ||||
3709 | // The simple example looks as follows: | ||||
3710 | // BB1: | ||||
3711 | // p1 = b1 + 40 | ||||
3712 | // br cond BB2, BB3 | ||||
3713 | // BB2: | ||||
3714 | // p2 = b2 + 40 | ||||
3715 | // br BB3 | ||||
3716 | // BB3: | ||||
3717 | // p = phi [p1, BB1], [p2, BB2] | ||||
3718 | // v = load p | ||||
3719 | // Map is | ||||
3720 | // p1 -> b1 | ||||
3721 | // p2 -> b2 | ||||
3722 | // Request is | ||||
3723 | // p -> ? | ||||
3724 | // The function tries to find or build phi [b1, BB1], [b2, BB2] in BB3. | ||||
3725 | Value *findCommon(FoldAddrToValueMapping &Map) { | ||||
3726 | // Tracks the simplification of newly created phi nodes. The reason we use | ||||
3727 | // this mapping is because we will add new created Phi nodes in AddrToBase. | ||||
3728 | // Simplification of Phi nodes is recursive, so some Phi node may | ||||
3729 | // be simplified after we added it to AddrToBase. In reality this | ||||
3730 | // simplification is possible only if original phi/selects were not | ||||
3731 | // simplified yet. | ||||
3732 | // Using this mapping we can find the current value in AddrToBase. | ||||
3733 | SimplificationTracker ST(SQ); | ||||
3734 | |||||
3735 | // First step, DFS to create PHI nodes for all intermediate blocks. | ||||
3736 | // Also fill traverse order for the second step. | ||||
3737 | SmallVector<Value *, 32> TraverseOrder; | ||||
3738 | InsertPlaceholders(Map, TraverseOrder, ST); | ||||
3739 | |||||
3740 | // Second Step, fill new nodes by merged values and simplify if possible. | ||||
3741 | FillPlaceholders(Map, TraverseOrder, ST); | ||||
3742 | |||||
3743 | if (!AddrSinkNewSelects && ST.countNewSelectNodes() > 0) { | ||||
3744 | ST.destroyNewNodes(CommonType); | ||||
3745 | return nullptr; | ||||
3746 | } | ||||
3747 | |||||
3748 | // Now we'd like to match New Phi nodes to existed ones. | ||||
3749 | unsigned PhiNotMatchedCount = 0; | ||||
3750 | if (!MatchPhiSet(ST, AddrSinkNewPhis, PhiNotMatchedCount)) { | ||||
3751 | ST.destroyNewNodes(CommonType); | ||||
3752 | return nullptr; | ||||
3753 | } | ||||
3754 | |||||
3755 | auto *Result = ST.Get(Map.find(Original)->second); | ||||
3756 | if (Result) { | ||||
3757 | NumMemoryInstsPhiCreated += ST.countNewPhiNodes() + PhiNotMatchedCount; | ||||
3758 | NumMemoryInstsSelectCreated += ST.countNewSelectNodes(); | ||||
3759 | } | ||||
3760 | return Result; | ||||
3761 | } | ||||
3762 | |||||
3763 | /// Try to match PHI node to Candidate. | ||||
3764 | /// Matcher tracks the matched Phi nodes. | ||||
3765 | bool MatchPhiNode(PHINode *PHI, PHINode *Candidate, | ||||
3766 | SmallSetVector<PHIPair, 8> &Matcher, | ||||
3767 | PhiNodeSet &PhiNodesToMatch) { | ||||
3768 | SmallVector<PHIPair, 8> WorkList; | ||||
3769 | Matcher.insert({PHI, Candidate}); | ||||
3770 | SmallSet<PHINode *, 8> MatchedPHIs; | ||||
3771 | MatchedPHIs.insert(PHI); | ||||
3772 | WorkList.push_back({PHI, Candidate}); | ||||
3773 | SmallSet<PHIPair, 8> Visited; | ||||
3774 | while (!WorkList.empty()) { | ||||
3775 | auto Item = WorkList.pop_back_val(); | ||||
3776 | if (!Visited.insert(Item).second) | ||||
3777 | continue; | ||||
3778 | // We iterate over all incoming values to Phi to compare them. | ||||
3779 | // If values are different and both of them Phi and the first one is a | ||||
3780 | // Phi we added (subject to match) and both of them is in the same basic | ||||
3781 | // block then we can match our pair if values match. So we state that | ||||
3782 | // these values match and add it to work list to verify that. | ||||
3783 | for (auto *B : Item.first->blocks()) { | ||||
3784 | Value *FirstValue = Item.first->getIncomingValueForBlock(B); | ||||
3785 | Value *SecondValue = Item.second->getIncomingValueForBlock(B); | ||||
3786 | if (FirstValue == SecondValue) | ||||
3787 | continue; | ||||
3788 | |||||
3789 | PHINode *FirstPhi = dyn_cast<PHINode>(FirstValue); | ||||
3790 | PHINode *SecondPhi = dyn_cast<PHINode>(SecondValue); | ||||
3791 | |||||
3792 | // One of them is not Phi or | ||||
3793 | // The first one is not Phi node from the set we'd like to match or | ||||
3794 | // Phi nodes from different basic blocks then | ||||
3795 | // we will not be able to match. | ||||
3796 | if (!FirstPhi || !SecondPhi || !PhiNodesToMatch.count(FirstPhi) || | ||||
3797 | FirstPhi->getParent() != SecondPhi->getParent()) | ||||
3798 | return false; | ||||
3799 | |||||
3800 | // If we already matched them then continue. | ||||
3801 | if (Matcher.count({FirstPhi, SecondPhi})) | ||||
3802 | continue; | ||||
3803 | // So the values are different and does not match. So we need them to | ||||
3804 | // match. (But we register no more than one match per PHI node, so that | ||||
3805 | // we won't later try to replace them twice.) | ||||
3806 | if (MatchedPHIs.insert(FirstPhi).second) | ||||
3807 | Matcher.insert({FirstPhi, SecondPhi}); | ||||
3808 | // But me must check it. | ||||
3809 | WorkList.push_back({FirstPhi, SecondPhi}); | ||||
3810 | } | ||||
3811 | } | ||||
3812 | return true; | ||||
3813 | } | ||||
3814 | |||||
3815 | /// For the given set of PHI nodes (in the SimplificationTracker) try | ||||
3816 | /// to find their equivalents. | ||||
3817 | /// Returns false if this matching fails and creation of new Phi is disabled. | ||||
3818 | bool MatchPhiSet(SimplificationTracker &ST, bool AllowNewPhiNodes, | ||||
3819 | unsigned &PhiNotMatchedCount) { | ||||
3820 | // Matched and PhiNodesToMatch iterate their elements in a deterministic | ||||
3821 | // order, so the replacements (ReplacePhi) are also done in a deterministic | ||||
3822 | // order. | ||||
3823 | SmallSetVector<PHIPair, 8> Matched; | ||||
3824 | SmallPtrSet<PHINode *, 8> WillNotMatch; | ||||
3825 | PhiNodeSet &PhiNodesToMatch = ST.newPhiNodes(); | ||||
3826 | while (PhiNodesToMatch.size()) { | ||||
3827 | PHINode *PHI = *PhiNodesToMatch.begin(); | ||||
3828 | |||||
3829 | // Add us, if no Phi nodes in the basic block we do not match. | ||||
3830 | WillNotMatch.clear(); | ||||
3831 | WillNotMatch.insert(PHI); | ||||
3832 | |||||
3833 | // Traverse all Phis until we found equivalent or fail to do that. | ||||
3834 | bool IsMatched = false; | ||||
3835 | for (auto &P : PHI->getParent()->phis()) { | ||||
3836 | // Skip new Phi nodes. | ||||
3837 | if (PhiNodesToMatch.count(&P)) | ||||
3838 | continue; | ||||
3839 | if ((IsMatched = MatchPhiNode(PHI, &P, Matched, PhiNodesToMatch))) | ||||
3840 | break; | ||||
3841 | // If it does not match, collect all Phi nodes from matcher. | ||||
3842 | // if we end up with no match, them all these Phi nodes will not match | ||||
3843 | // later. | ||||
3844 | for (auto M : Matched) | ||||
3845 | WillNotMatch.insert(M.first); | ||||
3846 | Matched.clear(); | ||||
3847 | } | ||||
3848 | if (IsMatched) { | ||||
3849 | // Replace all matched values and erase them. | ||||
3850 | for (auto MV : Matched) | ||||
3851 | ST.ReplacePhi(MV.first, MV.second); | ||||
3852 | Matched.clear(); | ||||
3853 | continue; | ||||
3854 | } | ||||
3855 | // If we are not allowed to create new nodes then bail out. | ||||
3856 | if (!AllowNewPhiNodes) | ||||
3857 | return false; | ||||
3858 | // Just remove all seen values in matcher. They will not match anything. | ||||
3859 | PhiNotMatchedCount += WillNotMatch.size(); | ||||
3860 | for (auto *P : WillNotMatch) | ||||
3861 | PhiNodesToMatch.erase(P); | ||||
3862 | } | ||||
3863 | return true; | ||||
3864 | } | ||||
3865 | /// Fill the placeholders with values from predecessors and simplify them. | ||||
3866 | void FillPlaceholders(FoldAddrToValueMapping &Map, | ||||
3867 | SmallVectorImpl<Value *> &TraverseOrder, | ||||
3868 | SimplificationTracker &ST) { | ||||
3869 | while (!TraverseOrder.empty()) { | ||||
3870 | Value *Current = TraverseOrder.pop_back_val(); | ||||
3871 | assert(Map.contains(Current) && "No node to fill!!!")(static_cast <bool> (Map.contains(Current) && "No node to fill!!!" ) ? void (0) : __assert_fail ("Map.contains(Current) && \"No node to fill!!!\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3871, __extension__ __PRETTY_FUNCTION__ )); | ||||
3872 | Value *V = Map[Current]; | ||||
3873 | |||||
3874 | if (SelectInst *Select = dyn_cast<SelectInst>(V)) { | ||||
3875 | // CurrentValue also must be Select. | ||||
3876 | auto *CurrentSelect = cast<SelectInst>(Current); | ||||
3877 | auto *TrueValue = CurrentSelect->getTrueValue(); | ||||
3878 | assert(Map.contains(TrueValue) && "No True Value!")(static_cast <bool> (Map.contains(TrueValue) && "No True Value!") ? void (0) : __assert_fail ("Map.contains(TrueValue) && \"No True Value!\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3878, __extension__ __PRETTY_FUNCTION__ )); | ||||
3879 | Select->setTrueValue(ST.Get(Map[TrueValue])); | ||||
3880 | auto *FalseValue = CurrentSelect->getFalseValue(); | ||||
3881 | assert(Map.contains(FalseValue) && "No False Value!")(static_cast <bool> (Map.contains(FalseValue) && "No False Value!") ? void (0) : __assert_fail ("Map.contains(FalseValue) && \"No False Value!\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3881, __extension__ __PRETTY_FUNCTION__ )); | ||||
3882 | Select->setFalseValue(ST.Get(Map[FalseValue])); | ||||
3883 | } else { | ||||
3884 | // Must be a Phi node then. | ||||
3885 | auto *PHI = cast<PHINode>(V); | ||||
3886 | // Fill the Phi node with values from predecessors. | ||||
3887 | for (auto *B : predecessors(PHI->getParent())) { | ||||
3888 | Value *PV = cast<PHINode>(Current)->getIncomingValueForBlock(B); | ||||
3889 | assert(Map.contains(PV) && "No predecessor Value!")(static_cast <bool> (Map.contains(PV) && "No predecessor Value!" ) ? void (0) : __assert_fail ("Map.contains(PV) && \"No predecessor Value!\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3889, __extension__ __PRETTY_FUNCTION__ )); | ||||
3890 | PHI->addIncoming(ST.Get(Map[PV]), B); | ||||
3891 | } | ||||
3892 | } | ||||
3893 | Map[Current] = ST.Simplify(V); | ||||
3894 | } | ||||
3895 | } | ||||
3896 | |||||
3897 | /// Starting from original value recursively iterates over def-use chain up to | ||||
3898 | /// known ending values represented in a map. For each traversed phi/select | ||||
3899 | /// inserts a placeholder Phi or Select. | ||||
3900 | /// Reports all new created Phi/Select nodes by adding them to set. | ||||
3901 | /// Also reports and order in what values have been traversed. | ||||
3902 | void InsertPlaceholders(FoldAddrToValueMapping &Map, | ||||
3903 | SmallVectorImpl<Value *> &TraverseOrder, | ||||
3904 | SimplificationTracker &ST) { | ||||
3905 | SmallVector<Value *, 32> Worklist; | ||||
3906 | assert((isa<PHINode>(Original) || isa<SelectInst>(Original)) &&(static_cast <bool> ((isa<PHINode>(Original) || isa <SelectInst>(Original)) && "Address must be a Phi or Select node" ) ? void (0) : __assert_fail ("(isa<PHINode>(Original) || isa<SelectInst>(Original)) && \"Address must be a Phi or Select node\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3907, __extension__ __PRETTY_FUNCTION__ )) | ||||
3907 | "Address must be a Phi or Select node")(static_cast <bool> ((isa<PHINode>(Original) || isa <SelectInst>(Original)) && "Address must be a Phi or Select node" ) ? void (0) : __assert_fail ("(isa<PHINode>(Original) || isa<SelectInst>(Original)) && \"Address must be a Phi or Select node\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 3907, __extension__ __PRETTY_FUNCTION__ )); | ||||
3908 | auto *Dummy = PoisonValue::get(CommonType); | ||||
3909 | Worklist.push_back(Original); | ||||
3910 | while (!Worklist.empty()) { | ||||
3911 | Value *Current = Worklist.pop_back_val(); | ||||
3912 | // if it is already visited or it is an ending value then skip it. | ||||
3913 | if (Map.contains(Current)) | ||||
3914 | continue; | ||||
3915 | TraverseOrder.push_back(Current); | ||||
3916 | |||||
3917 | // CurrentValue must be a Phi node or select. All others must be covered | ||||
3918 | // by anchors. | ||||
3919 | if (SelectInst *CurrentSelect = dyn_cast<SelectInst>(Current)) { | ||||
3920 | // Is it OK to get metadata from OrigSelect?! | ||||
3921 | // Create a Select placeholder with dummy value. | ||||
3922 | SelectInst *Select = SelectInst::Create( | ||||
3923 | CurrentSelect->getCondition(), Dummy, Dummy, | ||||
3924 | CurrentSelect->getName(), CurrentSelect, CurrentSelect); | ||||
3925 | Map[Current] = Select; | ||||
3926 | ST.insertNewSelect(Select); | ||||
3927 | // We are interested in True and False values. | ||||
3928 | Worklist.push_back(CurrentSelect->getTrueValue()); | ||||
3929 | Worklist.push_back(CurrentSelect->getFalseValue()); | ||||
3930 | } else { | ||||
3931 | // It must be a Phi node then. | ||||
3932 | PHINode *CurrentPhi = cast<PHINode>(Current); | ||||
3933 | unsigned PredCount = CurrentPhi->getNumIncomingValues(); | ||||
3934 | PHINode *PHI = | ||||
3935 | PHINode::Create(CommonType, PredCount, "sunk_phi", CurrentPhi); | ||||
3936 | Map[Current] = PHI; | ||||
3937 | ST.insertNewPhi(PHI); | ||||
3938 | append_range(Worklist, CurrentPhi->incoming_values()); | ||||
3939 | } | ||||
3940 | } | ||||
3941 | } | ||||
3942 | |||||
3943 | bool addrModeCombiningAllowed() { | ||||
3944 | if (DisableComplexAddrModes) | ||||
3945 | return false; | ||||
3946 | switch (DifferentField) { | ||||
3947 | default: | ||||
3948 | return false; | ||||
3949 | case ExtAddrMode::BaseRegField: | ||||
3950 | return AddrSinkCombineBaseReg; | ||||
3951 | case ExtAddrMode::BaseGVField: | ||||
3952 | return AddrSinkCombineBaseGV; | ||||
3953 | case ExtAddrMode::BaseOffsField: | ||||
3954 | return AddrSinkCombineBaseOffs; | ||||
3955 | case ExtAddrMode::ScaledRegField: | ||||
3956 | return AddrSinkCombineScaledReg; | ||||
3957 | } | ||||
3958 | } | ||||
3959 | }; | ||||
3960 | } // end anonymous namespace | ||||
3961 | |||||
3962 | /// Try adding ScaleReg*Scale to the current addressing mode. | ||||
3963 | /// Return true and update AddrMode if this addr mode is legal for the target, | ||||
3964 | /// false if not. | ||||
3965 | bool AddressingModeMatcher::matchScaledValue(Value *ScaleReg, int64_t Scale, | ||||
3966 | unsigned Depth) { | ||||
3967 | // If Scale is 1, then this is the same as adding ScaleReg to the addressing | ||||
3968 | // mode. Just process that directly. | ||||
3969 | if (Scale == 1) | ||||
3970 | return matchAddr(ScaleReg, Depth); | ||||
3971 | |||||
3972 | // If the scale is 0, it takes nothing to add this. | ||||
3973 | if (Scale == 0) | ||||
3974 | return true; | ||||
3975 | |||||
3976 | // If we already have a scale of this value, we can add to it, otherwise, we | ||||
3977 | // need an available scale field. | ||||
3978 | if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg) | ||||
3979 | return false; | ||||
3980 | |||||
3981 | ExtAddrMode TestAddrMode = AddrMode; | ||||
3982 | |||||
3983 | // Add scale to turn X*4+X*3 -> X*7. This could also do things like | ||||
3984 | // [A+B + A*7] -> [B+A*8]. | ||||
3985 | TestAddrMode.Scale += Scale; | ||||
3986 | TestAddrMode.ScaledReg = ScaleReg; | ||||
3987 | |||||
3988 | // If the new address isn't legal, bail out. | ||||
3989 | if (!TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace)) | ||||
3990 | return false; | ||||
3991 | |||||
3992 | // It was legal, so commit it. | ||||
3993 | AddrMode = TestAddrMode; | ||||
3994 | |||||
3995 | // Okay, we decided that we can add ScaleReg+Scale to AddrMode. Check now | ||||
3996 | // to see if ScaleReg is actually X+C. If so, we can turn this into adding | ||||
3997 | // X*Scale + C*Scale to addr mode. If we found available IV increment, do not | ||||
3998 | // go any further: we can reuse it and cannot eliminate it. | ||||
3999 | ConstantInt *CI = nullptr; | ||||
4000 | Value *AddLHS = nullptr; | ||||
4001 | if (isa<Instruction>(ScaleReg) && // not a constant expr. | ||||
4002 | match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI))) && | ||||
4003 | !isIVIncrement(ScaleReg, &LI) && CI->getValue().isSignedIntN(64)) { | ||||
4004 | TestAddrMode.InBounds = false; | ||||
4005 | TestAddrMode.ScaledReg = AddLHS; | ||||
4006 | TestAddrMode.BaseOffs += CI->getSExtValue() * TestAddrMode.Scale; | ||||
4007 | |||||
4008 | // If this addressing mode is legal, commit it and remember that we folded | ||||
4009 | // this instruction. | ||||
4010 | if (TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace)) { | ||||
4011 | AddrModeInsts.push_back(cast<Instruction>(ScaleReg)); | ||||
4012 | AddrMode = TestAddrMode; | ||||
4013 | return true; | ||||
4014 | } | ||||
4015 | // Restore status quo. | ||||
4016 | TestAddrMode = AddrMode; | ||||
4017 | } | ||||
4018 | |||||
4019 | // If this is an add recurrence with a constant step, return the increment | ||||
4020 | // instruction and the canonicalized step. | ||||
4021 | auto GetConstantStep = | ||||
4022 | [this](const Value *V) -> std::optional<std::pair<Instruction *, APInt>> { | ||||
4023 | auto *PN = dyn_cast<PHINode>(V); | ||||
4024 | if (!PN) | ||||
4025 | return std::nullopt; | ||||
4026 | auto IVInc = getIVIncrement(PN, &LI); | ||||
4027 | if (!IVInc) | ||||
4028 | return std::nullopt; | ||||
4029 | // TODO: The result of the intrinsics above is two-complement. However when | ||||
4030 | // IV inc is expressed as add or sub, iv.next is potentially a poison value. | ||||
4031 | // If it has nuw or nsw flags, we need to make sure that these flags are | ||||
4032 | // inferrable at the point of memory instruction. Otherwise we are replacing | ||||
4033 | // well-defined two-complement computation with poison. Currently, to avoid | ||||
4034 | // potentially complex analysis needed to prove this, we reject such cases. | ||||
4035 | if (auto *OIVInc = dyn_cast<OverflowingBinaryOperator>(IVInc->first)) | ||||
4036 | if (OIVInc->hasNoSignedWrap() || OIVInc->hasNoUnsignedWrap()) | ||||
4037 | return std::nullopt; | ||||
4038 | if (auto *ConstantStep = dyn_cast<ConstantInt>(IVInc->second)) | ||||
4039 | return std::make_pair(IVInc->first, ConstantStep->getValue()); | ||||
4040 | return std::nullopt; | ||||
4041 | }; | ||||
4042 | |||||
4043 | // Try to account for the following special case: | ||||
4044 | // 1. ScaleReg is an inductive variable; | ||||
4045 | // 2. We use it with non-zero offset; | ||||
4046 | // 3. IV's increment is available at the point of memory instruction. | ||||
4047 | // | ||||
4048 | // In this case, we may reuse the IV increment instead of the IV Phi to | ||||
4049 | // achieve the following advantages: | ||||
4050 | // 1. If IV step matches the offset, we will have no need in the offset; | ||||
4051 | // 2. Even if they don't match, we will reduce the overlap of living IV | ||||
4052 | // and IV increment, that will potentially lead to better register | ||||
4053 | // assignment. | ||||
4054 | if (AddrMode.BaseOffs) { | ||||
4055 | if (auto IVStep = GetConstantStep(ScaleReg)) { | ||||
4056 | Instruction *IVInc = IVStep->first; | ||||
4057 | // The following assert is important to ensure a lack of infinite loops. | ||||
4058 | // This transforms is (intentionally) the inverse of the one just above. | ||||
4059 | // If they don't agree on the definition of an increment, we'd alternate | ||||
4060 | // back and forth indefinitely. | ||||
4061 | assert(isIVIncrement(IVInc, &LI) && "implied by GetConstantStep")(static_cast <bool> (isIVIncrement(IVInc, &LI) && "implied by GetConstantStep") ? void (0) : __assert_fail ("isIVIncrement(IVInc, &LI) && \"implied by GetConstantStep\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 4061, __extension__ __PRETTY_FUNCTION__ )); | ||||
4062 | APInt Step = IVStep->second; | ||||
4063 | APInt Offset = Step * AddrMode.Scale; | ||||
4064 | if (Offset.isSignedIntN(64)) { | ||||
4065 | TestAddrMode.InBounds = false; | ||||
4066 | TestAddrMode.ScaledReg = IVInc; | ||||
4067 | TestAddrMode.BaseOffs -= Offset.getLimitedValue(); | ||||
4068 | // If this addressing mode is legal, commit it.. | ||||
4069 | // (Note that we defer the (expensive) domtree base legality check | ||||
4070 | // to the very last possible point.) | ||||
4071 | if (TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace) && | ||||
4072 | getDTFn().dominates(IVInc, MemoryInst)) { | ||||
4073 | AddrModeInsts.push_back(cast<Instruction>(IVInc)); | ||||
4074 | AddrMode = TestAddrMode; | ||||
4075 | return true; | ||||
4076 | } | ||||
4077 | // Restore status quo. | ||||
4078 | TestAddrMode = AddrMode; | ||||
4079 | } | ||||
4080 | } | ||||
4081 | } | ||||
4082 | |||||
4083 | // Otherwise, just return what we have. | ||||
4084 | return true; | ||||
4085 | } | ||||
4086 | |||||
4087 | /// This is a little filter, which returns true if an addressing computation | ||||
4088 | /// involving I might be folded into a load/store accessing it. | ||||
4089 | /// This doesn't need to be perfect, but needs to accept at least | ||||
4090 | /// the set of instructions that MatchOperationAddr can. | ||||
4091 | static bool MightBeFoldableInst(Instruction *I) { | ||||
4092 | switch (I->getOpcode()) { | ||||
4093 | case Instruction::BitCast: | ||||
4094 | case Instruction::AddrSpaceCast: | ||||
4095 | // Don't touch identity bitcasts. | ||||
4096 | if (I->getType() == I->getOperand(0)->getType()) | ||||
4097 | return false; | ||||
4098 | return I->getType()->isIntOrPtrTy(); | ||||
4099 | case Instruction::PtrToInt: | ||||
4100 | // PtrToInt is always a noop, as we know that the int type is pointer sized. | ||||
4101 | return true; | ||||
4102 | case Instruction::IntToPtr: | ||||
4103 | // We know the input is intptr_t, so this is foldable. | ||||
4104 | return true; | ||||
4105 | case Instruction::Add: | ||||
4106 | return true; | ||||
4107 | case Instruction::Mul: | ||||
4108 | case Instruction::Shl: | ||||
4109 | // Can only handle X*C and X << C. | ||||
4110 | return isa<ConstantInt>(I->getOperand(1)); | ||||
4111 | case Instruction::GetElementPtr: | ||||
4112 | return true; | ||||
4113 | default: | ||||
4114 | return false; | ||||
4115 | } | ||||
4116 | } | ||||
4117 | |||||
4118 | /// Check whether or not \p Val is a legal instruction for \p TLI. | ||||
4119 | /// \note \p Val is assumed to be the product of some type promotion. | ||||
4120 | /// Therefore if \p Val has an undefined state in \p TLI, this is assumed | ||||
4121 | /// to be legal, as the non-promoted value would have had the same state. | ||||
4122 | static bool isPromotedInstructionLegal(const TargetLowering &TLI, | ||||
4123 | const DataLayout &DL, Value *Val) { | ||||
4124 | Instruction *PromotedInst = dyn_cast<Instruction>(Val); | ||||
4125 | if (!PromotedInst) | ||||
4126 | return false; | ||||
4127 | int ISDOpcode = TLI.InstructionOpcodeToISD(PromotedInst->getOpcode()); | ||||
4128 | // If the ISDOpcode is undefined, it was undefined before the promotion. | ||||
4129 | if (!ISDOpcode) | ||||
4130 | return true; | ||||
4131 | // Otherwise, check if the promoted instruction is legal or not. | ||||
4132 | return TLI.isOperationLegalOrCustom( | ||||
4133 | ISDOpcode, TLI.getValueType(DL, PromotedInst->getType())); | ||||
4134 | } | ||||
4135 | |||||
4136 | namespace { | ||||
4137 | |||||
4138 | /// Hepler class to perform type promotion. | ||||
4139 | class TypePromotionHelper { | ||||
4140 | /// Utility function to add a promoted instruction \p ExtOpnd to | ||||
4141 | /// \p PromotedInsts and record the type of extension we have seen. | ||||
4142 | static void addPromotedInst(InstrToOrigTy &PromotedInsts, | ||||
4143 | Instruction *ExtOpnd, bool IsSExt) { | ||||
4144 | ExtType ExtTy = IsSExt ? SignExtension : ZeroExtension; | ||||
4145 | InstrToOrigTy::iterator It = PromotedInsts.find(ExtOpnd); | ||||
4146 | if (It != PromotedInsts.end()) { | ||||
4147 | // If the new extension is same as original, the information in | ||||
4148 | // PromotedInsts[ExtOpnd] is still correct. | ||||
4149 | if (It->second.getInt() == ExtTy) | ||||
4150 | return; | ||||
4151 | |||||
4152 | // Now the new extension is different from old extension, we make | ||||
4153 | // the type information invalid by setting extension type to | ||||
4154 | // BothExtension. | ||||
4155 | ExtTy = BothExtension; | ||||
4156 | } | ||||
4157 | PromotedInsts[ExtOpnd] = TypeIsSExt(ExtOpnd->getType(), ExtTy); | ||||
4158 | } | ||||
4159 | |||||
4160 | /// Utility function to query the original type of instruction \p Opnd | ||||
4161 | /// with a matched extension type. If the extension doesn't match, we | ||||
4162 | /// cannot use the information we had on the original type. | ||||
4163 | /// BothExtension doesn't match any extension type. | ||||
4164 | static const Type *getOrigType(const InstrToOrigTy &PromotedInsts, | ||||
4165 | Instruction *Opnd, bool IsSExt) { | ||||
4166 | ExtType ExtTy = IsSExt ? SignExtension : ZeroExtension; | ||||
4167 | InstrToOrigTy::const_iterator It = PromotedInsts.find(Opnd); | ||||
4168 | if (It != PromotedInsts.end() && It->second.getInt() == ExtTy) | ||||
4169 | return It->second.getPointer(); | ||||
4170 | return nullptr; | ||||
4171 | } | ||||
4172 | |||||
4173 | /// Utility function to check whether or not a sign or zero extension | ||||
4174 | /// of \p Inst with \p ConsideredExtType can be moved through \p Inst by | ||||
4175 | /// either using the operands of \p Inst or promoting \p Inst. | ||||
4176 | /// The type of the extension is defined by \p IsSExt. | ||||
4177 | /// In other words, check if: | ||||
4178 | /// ext (Ty Inst opnd1 opnd2 ... opndN) to ConsideredExtType. | ||||
4179 | /// #1 Promotion applies: | ||||
4180 | /// ConsideredExtType Inst (ext opnd1 to ConsideredExtType, ...). | ||||
4181 | /// #2 Operand reuses: | ||||
4182 | /// ext opnd1 to ConsideredExtType. | ||||
4183 | /// \p PromotedInsts maps the instructions to their type before promotion. | ||||
4184 | static bool canGetThrough(const Instruction *Inst, Type *ConsideredExtType, | ||||
4185 | const InstrToOrigTy &PromotedInsts, bool IsSExt); | ||||
4186 | |||||
4187 | /// Utility function to determine if \p OpIdx should be promoted when | ||||
4188 | /// promoting \p Inst. | ||||
4189 | static bool shouldExtOperand(const Instruction *Inst, int OpIdx) { | ||||
4190 | return !(isa<SelectInst>(Inst) && OpIdx == 0); | ||||
4191 | } | ||||
4192 | |||||
4193 | /// Utility function to promote the operand of \p Ext when this | ||||
4194 | /// operand is a promotable trunc or sext or zext. | ||||
4195 | /// \p PromotedInsts maps the instructions to their type before promotion. | ||||
4196 | /// \p CreatedInstsCost[out] contains the cost of all instructions | ||||
4197 | /// created to promote the operand of Ext. | ||||
4198 | /// Newly added extensions are inserted in \p Exts. | ||||
4199 | /// Newly added truncates are inserted in \p Truncs. | ||||
4200 | /// Should never be called directly. | ||||
4201 | /// \return The promoted value which is used instead of Ext. | ||||
4202 | static Value *promoteOperandForTruncAndAnyExt( | ||||
4203 | Instruction *Ext, TypePromotionTransaction &TPT, | ||||
4204 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, | ||||
4205 | SmallVectorImpl<Instruction *> *Exts, | ||||
4206 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI); | ||||
4207 | |||||
4208 | /// Utility function to promote the operand of \p Ext when this | ||||
4209 | /// operand is promotable and is not a supported trunc or sext. | ||||
4210 | /// \p PromotedInsts maps the instructions to their type before promotion. | ||||
4211 | /// \p CreatedInstsCost[out] contains the cost of all the instructions | ||||
4212 | /// created to promote the operand of Ext. | ||||
4213 | /// Newly added extensions are inserted in \p Exts. | ||||
4214 | /// Newly added truncates are inserted in \p Truncs. | ||||
4215 | /// Should never be called directly. | ||||
4216 | /// \return The promoted value which is used instead of Ext. | ||||
4217 | static Value *promoteOperandForOther(Instruction *Ext, | ||||
4218 | TypePromotionTransaction &TPT, | ||||
4219 | InstrToOrigTy &PromotedInsts, | ||||
4220 | unsigned &CreatedInstsCost, | ||||
4221 | SmallVectorImpl<Instruction *> *Exts, | ||||
4222 | SmallVectorImpl<Instruction *> *Truncs, | ||||
4223 | const TargetLowering &TLI, bool IsSExt); | ||||
4224 | |||||
4225 | /// \see promoteOperandForOther. | ||||
4226 | static Value *signExtendOperandForOther( | ||||
4227 | Instruction *Ext, TypePromotionTransaction &TPT, | ||||
4228 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, | ||||
4229 | SmallVectorImpl<Instruction *> *Exts, | ||||
4230 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) { | ||||
4231 | return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost, | ||||
4232 | Exts, Truncs, TLI, true); | ||||
4233 | } | ||||
4234 | |||||
4235 | /// \see promoteOperandForOther. | ||||
4236 | static Value *zeroExtendOperandForOther( | ||||
4237 | Instruction *Ext, TypePromotionTransaction &TPT, | ||||
4238 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, | ||||
4239 | SmallVectorImpl<Instruction *> *Exts, | ||||
4240 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) { | ||||
4241 | return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost, | ||||
4242 | Exts, Truncs, TLI, false); | ||||
4243 | } | ||||
4244 | |||||
4245 | public: | ||||
4246 | /// Type for the utility function that promotes the operand of Ext. | ||||
4247 | using Action = Value *(*)(Instruction *Ext, TypePromotionTransaction &TPT, | ||||
4248 | InstrToOrigTy &PromotedInsts, | ||||
4249 | unsigned &CreatedInstsCost, | ||||
4250 | SmallVectorImpl<Instruction *> *Exts, | ||||
4251 | SmallVectorImpl<Instruction *> *Truncs, | ||||
4252 | const TargetLowering &TLI); | ||||
4253 | |||||
4254 | /// Given a sign/zero extend instruction \p Ext, return the appropriate | ||||
4255 | /// action to promote the operand of \p Ext instead of using Ext. | ||||
4256 | /// \return NULL if no promotable action is possible with the current | ||||
4257 | /// sign extension. | ||||
4258 | /// \p InsertedInsts keeps track of all the instructions inserted by the | ||||
4259 | /// other CodeGenPrepare optimizations. This information is important | ||||
4260 | /// because we do not want to promote these instructions as CodeGenPrepare | ||||
4261 | /// will reinsert them later. Thus creating an infinite loop: create/remove. | ||||
4262 | /// \p PromotedInsts maps the instructions to their type before promotion. | ||||
4263 | static Action getAction(Instruction *Ext, const SetOfInstrs &InsertedInsts, | ||||
4264 | const TargetLowering &TLI, | ||||
4265 | const InstrToOrigTy &PromotedInsts); | ||||
4266 | }; | ||||
4267 | |||||
4268 | } // end anonymous namespace | ||||
4269 | |||||
4270 | bool TypePromotionHelper::canGetThrough(const Instruction *Inst, | ||||
4271 | Type *ConsideredExtType, | ||||
4272 | const InstrToOrigTy &PromotedInsts, | ||||
4273 | bool IsSExt) { | ||||
4274 | // The promotion helper does not know how to deal with vector types yet. | ||||
4275 | // To be able to fix that, we would need to fix the places where we | ||||
4276 | // statically extend, e.g., constants and such. | ||||
4277 | if (Inst->getType()->isVectorTy()) | ||||
4278 | return false; | ||||
4279 | |||||
4280 | // We can always get through zext. | ||||
4281 | if (isa<ZExtInst>(Inst)) | ||||
4282 | return true; | ||||
4283 | |||||
4284 | // sext(sext) is ok too. | ||||
4285 | if (IsSExt && isa<SExtInst>(Inst)) | ||||
4286 | return true; | ||||
4287 | |||||
4288 | // We can get through binary operator, if it is legal. In other words, the | ||||
4289 | // binary operator must have a nuw or nsw flag. | ||||
4290 | if (const auto *BinOp = dyn_cast<BinaryOperator>(Inst)) | ||||
4291 | if (isa<OverflowingBinaryOperator>(BinOp) && | ||||
4292 | ((!IsSExt && BinOp->hasNoUnsignedWrap()) || | ||||
4293 | (IsSExt && BinOp->hasNoSignedWrap()))) | ||||
4294 | return true; | ||||
4295 | |||||
4296 | // ext(and(opnd, cst)) --> and(ext(opnd), ext(cst)) | ||||
4297 | if ((Inst->getOpcode() == Instruction::And || | ||||
4298 | Inst->getOpcode() == Instruction::Or)) | ||||
4299 | return true; | ||||
4300 | |||||
4301 | // ext(xor(opnd, cst)) --> xor(ext(opnd), ext(cst)) | ||||
4302 | if (Inst->getOpcode() == Instruction::Xor) { | ||||
4303 | // Make sure it is not a NOT. | ||||
4304 | if (const auto *Cst = dyn_cast<ConstantInt>(Inst->getOperand(1))) | ||||
4305 | if (!Cst->getValue().isAllOnes()) | ||||
4306 | return true; | ||||
4307 | } | ||||
4308 | |||||
4309 | // zext(shrl(opnd, cst)) --> shrl(zext(opnd), zext(cst)) | ||||
4310 | // It may change a poisoned value into a regular value, like | ||||
4311 | // zext i32 (shrl i8 %val, 12) --> shrl i32 (zext i8 %val), 12 | ||||
4312 | // poisoned value regular value | ||||
4313 | // It should be OK since undef covers valid value. | ||||
4314 | if (Inst->getOpcode() == Instruction::LShr && !IsSExt) | ||||
4315 | return true; | ||||
4316 | |||||
4317 | // and(ext(shl(opnd, cst)), cst) --> and(shl(ext(opnd), ext(cst)), cst) | ||||
4318 | // It may change a poisoned value into a regular value, like | ||||
4319 | // zext i32 (shl i8 %val, 12) --> shl i32 (zext i8 %val), 12 | ||||
4320 | // poisoned value regular value | ||||
4321 | // It should be OK since undef covers valid value. | ||||
4322 | if (Inst->getOpcode() == Instruction::Shl && Inst->hasOneUse()) { | ||||
4323 | const auto *ExtInst = cast<const Instruction>(*Inst->user_begin()); | ||||
4324 | if (ExtInst->hasOneUse()) { | ||||
4325 | const auto *AndInst = dyn_cast<const Instruction>(*ExtInst->user_begin()); | ||||
4326 | if (AndInst && AndInst->getOpcode() == Instruction::And) { | ||||
4327 | const auto *Cst = dyn_cast<ConstantInt>(AndInst->getOperand(1)); | ||||
4328 | if (Cst && | ||||
4329 | Cst->getValue().isIntN(Inst->getType()->getIntegerBitWidth())) | ||||
4330 | return true; | ||||
4331 | } | ||||
4332 | } | ||||
4333 | } | ||||
4334 | |||||
4335 | // Check if we can do the following simplification. | ||||
4336 | // ext(trunc(opnd)) --> ext(opnd) | ||||
4337 | if (!isa<TruncInst>(Inst)) | ||||
4338 | return false; | ||||
4339 | |||||
4340 | Value *OpndVal = Inst->getOperand(0); | ||||
4341 | // Check if we can use this operand in the extension. | ||||
4342 | // If the type is larger than the result type of the extension, we cannot. | ||||
4343 | if (!OpndVal->getType()->isIntegerTy() || | ||||
4344 | OpndVal->getType()->getIntegerBitWidth() > | ||||
4345 | ConsideredExtType->getIntegerBitWidth()) | ||||
4346 | return false; | ||||
4347 | |||||
4348 | // If the operand of the truncate is not an instruction, we will not have | ||||
4349 | // any information on the dropped bits. | ||||
4350 | // (Actually we could for constant but it is not worth the extra logic). | ||||
4351 | Instruction *Opnd = dyn_cast<Instruction>(OpndVal); | ||||
4352 | if (!Opnd) | ||||
4353 | return false; | ||||
4354 | |||||
4355 | // Check if the source of the type is narrow enough. | ||||
4356 | // I.e., check that trunc just drops extended bits of the same kind of | ||||
4357 | // the extension. | ||||
4358 | // #1 get the type of the operand and check the kind of the extended bits. | ||||
4359 | const Type *OpndType = getOrigType(PromotedInsts, Opnd, IsSExt); | ||||
4360 | if (OpndType) | ||||
4361 | ; | ||||
4362 | else if ((IsSExt && isa<SExtInst>(Opnd)) || (!IsSExt && isa<ZExtInst>(Opnd))) | ||||
4363 | OpndType = Opnd->getOperand(0)->getType(); | ||||
4364 | else | ||||
4365 | return false; | ||||
4366 | |||||
4367 | // #2 check that the truncate just drops extended bits. | ||||
4368 | return Inst->getType()->getIntegerBitWidth() >= | ||||
4369 | OpndType->getIntegerBitWidth(); | ||||
4370 | } | ||||
4371 | |||||
4372 | TypePromotionHelper::Action TypePromotionHelper::getAction( | ||||
4373 | Instruction *Ext, const SetOfInstrs &InsertedInsts, | ||||
4374 | const TargetLowering &TLI, const InstrToOrigTy &PromotedInsts) { | ||||
4375 | assert((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&(static_cast <bool> ((isa<SExtInst>(Ext) || isa< ZExtInst>(Ext)) && "Unexpected instruction type") ? void (0) : __assert_fail ("(isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) && \"Unexpected instruction type\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 4376, __extension__ __PRETTY_FUNCTION__ )) | ||||
4376 | "Unexpected instruction type")(static_cast <bool> ((isa<SExtInst>(Ext) || isa< ZExtInst>(Ext)) && "Unexpected instruction type") ? void (0) : __assert_fail ("(isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) && \"Unexpected instruction type\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 4376, __extension__ __PRETTY_FUNCTION__ )); | ||||
4377 | Instruction *ExtOpnd = dyn_cast<Instruction>(Ext->getOperand(0)); | ||||
4378 | Type *ExtTy = Ext->getType(); | ||||
4379 | bool IsSExt = isa<SExtInst>(Ext); | ||||
4380 | // If the operand of the extension is not an instruction, we cannot | ||||
4381 | // get through. | ||||
4382 | // If it, check we can get through. | ||||
4383 | if (!ExtOpnd || !canGetThrough(ExtOpnd, ExtTy, PromotedInsts, IsSExt)) | ||||
4384 | return nullptr; | ||||
4385 | |||||
4386 | // Do not promote if the operand has been added by codegenprepare. | ||||
4387 | // Otherwise, it means we are undoing an optimization that is likely to be | ||||
4388 | // redone, thus causing potential infinite loop. | ||||
4389 | if (isa<TruncInst>(ExtOpnd) && InsertedInsts.count(ExtOpnd)) | ||||
4390 | return nullptr; | ||||
4391 | |||||
4392 | // SExt or Trunc instructions. | ||||
4393 | // Return the related handler. | ||||
4394 | if (isa<SExtInst>(ExtOpnd) || isa<TruncInst>(ExtOpnd) || | ||||
4395 | isa<ZExtInst>(ExtOpnd)) | ||||
4396 | return promoteOperandForTruncAndAnyExt; | ||||
4397 | |||||
4398 | // Regular instruction. | ||||
4399 | // Abort early if we will have to insert non-free instructions. | ||||
4400 | if (!ExtOpnd->hasOneUse() && !TLI.isTruncateFree(ExtTy, ExtOpnd->getType())) | ||||
4401 | return nullptr; | ||||
4402 | return IsSExt ? signExtendOperandForOther : zeroExtendOperandForOther; | ||||
4403 | } | ||||
4404 | |||||
4405 | Value *TypePromotionHelper::promoteOperandForTruncAndAnyExt( | ||||
4406 | Instruction *SExt, TypePromotionTransaction &TPT, | ||||
4407 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, | ||||
4408 | SmallVectorImpl<Instruction *> *Exts, | ||||
4409 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) { | ||||
4410 | // By construction, the operand of SExt is an instruction. Otherwise we cannot | ||||
4411 | // get through it and this method should not be called. | ||||
4412 | Instruction *SExtOpnd = cast<Instruction>(SExt->getOperand(0)); | ||||
4413 | Value *ExtVal = SExt; | ||||
4414 | bool HasMergedNonFreeExt = false; | ||||
4415 | if (isa<ZExtInst>(SExtOpnd)) { | ||||
4416 | // Replace s|zext(zext(opnd)) | ||||
4417 | // => zext(opnd). | ||||
4418 | HasMergedNonFreeExt = !TLI.isExtFree(SExtOpnd); | ||||
4419 | Value *ZExt = | ||||
4420 | TPT.createZExt(SExt, SExtOpnd->getOperand(0), SExt->getType()); | ||||
4421 | TPT.replaceAllUsesWith(SExt, ZExt); | ||||
4422 | TPT.eraseInstruction(SExt); | ||||
4423 | ExtVal = ZExt; | ||||
4424 | } else { | ||||
4425 | // Replace z|sext(trunc(opnd)) or sext(sext(opnd)) | ||||
4426 | // => z|sext(opnd). | ||||
4427 | TPT.setOperand(SExt, 0, SExtOpnd->getOperand(0)); | ||||
4428 | } | ||||
4429 | CreatedInstsCost = 0; | ||||
4430 | |||||
4431 | // Remove dead code. | ||||
4432 | if (SExtOpnd->use_empty()) | ||||
4433 | TPT.eraseInstruction(SExtOpnd); | ||||
4434 | |||||
4435 | // Check if the extension is still needed. | ||||
4436 | Instruction *ExtInst = dyn_cast<Instruction>(ExtVal); | ||||
4437 | if (!ExtInst || ExtInst->getType() != ExtInst->getOperand(0)->getType()) { | ||||
4438 | if (ExtInst) { | ||||
4439 | if (Exts) | ||||
4440 | Exts->push_back(ExtInst); | ||||
4441 | CreatedInstsCost = !TLI.isExtFree(ExtInst) && !HasMergedNonFreeExt; | ||||
4442 | } | ||||
4443 | return ExtVal; | ||||
4444 | } | ||||
4445 | |||||
4446 | // At this point we have: ext ty opnd to ty. | ||||
4447 | // Reassign the uses of ExtInst to the opnd and remove ExtInst. | ||||
4448 | Value *NextVal = ExtInst->getOperand(0); | ||||
4449 | TPT.eraseInstruction(ExtInst, NextVal); | ||||
4450 | return NextVal; | ||||
4451 | } | ||||
4452 | |||||
4453 | Value *TypePromotionHelper::promoteOperandForOther( | ||||
4454 | Instruction *Ext, TypePromotionTransaction &TPT, | ||||
4455 | InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost, | ||||
4456 | SmallVectorImpl<Instruction *> *Exts, | ||||
4457 | SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI, | ||||
4458 | bool IsSExt) { | ||||
4459 | // By construction, the operand of Ext is an instruction. Otherwise we cannot | ||||
4460 | // get through it and this method should not be called. | ||||
4461 | Instruction *ExtOpnd = cast<Instruction>(Ext->getOperand(0)); | ||||
4462 | CreatedInstsCost = 0; | ||||
4463 | if (!ExtOpnd->hasOneUse()) { | ||||
4464 | // ExtOpnd will be promoted. | ||||
4465 | // All its uses, but Ext, will need to use a truncated value of the | ||||
4466 | // promoted version. | ||||
4467 | // Create the truncate now. | ||||
4468 | Value *Trunc = TPT.createTrunc(Ext, ExtOpnd->getType()); | ||||
4469 | if (Instruction *ITrunc = dyn_cast<Instruction>(Trunc)) { | ||||
4470 | // Insert it just after the definition. | ||||
4471 | ITrunc->moveAfter(ExtOpnd); | ||||
4472 | if (Truncs) | ||||
4473 | Truncs->push_back(ITrunc); | ||||
4474 | } | ||||
4475 | |||||
4476 | TPT.replaceAllUsesWith(ExtOpnd, Trunc); | ||||
4477 | // Restore the operand of Ext (which has been replaced by the previous call | ||||
4478 | // to replaceAllUsesWith) to avoid creating a cycle trunc <-> sext. | ||||
4479 | TPT.setOperand(Ext, 0, ExtOpnd); | ||||
4480 | } | ||||
4481 | |||||
4482 | // Get through the Instruction: | ||||
4483 | // 1. Update its type. | ||||
4484 | // 2. Replace the uses of Ext by Inst. | ||||
4485 | // 3. Extend each operand that needs to be extended. | ||||
4486 | |||||
4487 | // Remember the original type of the instruction before promotion. | ||||
4488 | // This is useful to know that the high bits are sign extended bits. | ||||
4489 | addPromotedInst(PromotedInsts, ExtOpnd, IsSExt); | ||||
4490 | // Step #1. | ||||
4491 | TPT.mutateType(ExtOpnd, Ext->getType()); | ||||
4492 | // Step #2. | ||||
4493 | TPT.replaceAllUsesWith(Ext, ExtOpnd); | ||||
4494 | // Step #3. | ||||
4495 | Instruction *ExtForOpnd = Ext; | ||||
4496 | |||||
4497 | LLVM_DEBUG(dbgs() << "Propagate Ext to operands\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Propagate Ext to operands\n" ; } } while (false); | ||||
4498 | for (int OpIdx = 0, EndOpIdx = ExtOpnd->getNumOperands(); OpIdx != EndOpIdx; | ||||
4499 | ++OpIdx) { | ||||
4500 | LLVM_DEBUG(dbgs() << "Operand:\n" << *(ExtOpnd->getOperand(OpIdx)) << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Operand:\n" << * (ExtOpnd->getOperand(OpIdx)) << '\n'; } } while (false ); | ||||
4501 | if (ExtOpnd->getOperand(OpIdx)->getType() == Ext->getType() || | ||||
4502 | !shouldExtOperand(ExtOpnd, OpIdx)) { | ||||
4503 | LLVM_DEBUG(dbgs() << "No need to propagate\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "No need to propagate\n" ; } } while (false); | ||||
4504 | continue; | ||||
4505 | } | ||||
4506 | // Check if we can statically extend the operand. | ||||
4507 | Value *Opnd = ExtOpnd->getOperand(OpIdx); | ||||
4508 | if (const ConstantInt *Cst = dyn_cast<ConstantInt>(Opnd)) { | ||||
4509 | LLVM_DEBUG(dbgs() << "Statically extend\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Statically extend\n"; } } while (false); | ||||
4510 | unsigned BitWidth = Ext->getType()->getIntegerBitWidth(); | ||||
4511 | APInt CstVal = IsSExt ? Cst->getValue().sext(BitWidth) | ||||
4512 | : Cst->getValue().zext(BitWidth); | ||||
4513 | TPT.setOperand(ExtOpnd, OpIdx, ConstantInt::get(Ext->getType(), CstVal)); | ||||
4514 | continue; | ||||
4515 | } | ||||
4516 | // UndefValue are typed, so we have to statically sign extend them. | ||||
4517 | if (isa<UndefValue>(Opnd)) { | ||||
4518 | LLVM_DEBUG(dbgs() << "Statically extend\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Statically extend\n"; } } while (false); | ||||
4519 | TPT.setOperand(ExtOpnd, OpIdx, UndefValue::get(Ext->getType())); | ||||
4520 | continue; | ||||
4521 | } | ||||
4522 | |||||
4523 | // Otherwise we have to explicitly sign extend the operand. | ||||
4524 | // Check if Ext was reused to extend an operand. | ||||
4525 | if (!ExtForOpnd) { | ||||
4526 | // If yes, create a new one. | ||||
4527 | LLVM_DEBUG(dbgs() << "More operands to ext\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "More operands to ext\n" ; } } while (false); | ||||
4528 | Value *ValForExtOpnd = IsSExt ? TPT.createSExt(Ext, Opnd, Ext->getType()) | ||||
4529 | : TPT.createZExt(Ext, Opnd, Ext->getType()); | ||||
4530 | if (!isa<Instruction>(ValForExtOpnd)) { | ||||
4531 | TPT.setOperand(ExtOpnd, OpIdx, ValForExtOpnd); | ||||
4532 | continue; | ||||
4533 | } | ||||
4534 | ExtForOpnd = cast<Instruction>(ValForExtOpnd); | ||||
4535 | } | ||||
4536 | if (Exts) | ||||
4537 | Exts->push_back(ExtForOpnd); | ||||
4538 | TPT.setOperand(ExtForOpnd, 0, Opnd); | ||||
4539 | |||||
4540 | // Move the sign extension before the insertion point. | ||||
4541 | TPT.moveBefore(ExtForOpnd, ExtOpnd); | ||||
4542 | TPT.setOperand(ExtOpnd, OpIdx, ExtForOpnd); | ||||
4543 | CreatedInstsCost += !TLI.isExtFree(ExtForOpnd); | ||||
4544 | // If more sext are required, new instructions will have to be created. | ||||
4545 | ExtForOpnd = nullptr; | ||||
4546 | } | ||||
4547 | if (ExtForOpnd == Ext) { | ||||
4548 | LLVM_DEBUG(dbgs() << "Extension is useless now\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Extension is useless now\n" ; } } while (false); | ||||
4549 | TPT.eraseInstruction(Ext); | ||||
4550 | } | ||||
4551 | return ExtOpnd; | ||||
4552 | } | ||||
4553 | |||||
4554 | /// Check whether or not promoting an instruction to a wider type is profitable. | ||||
4555 | /// \p NewCost gives the cost of extension instructions created by the | ||||
4556 | /// promotion. | ||||
4557 | /// \p OldCost gives the cost of extension instructions before the promotion | ||||
4558 | /// plus the number of instructions that have been | ||||
4559 | /// matched in the addressing mode the promotion. | ||||
4560 | /// \p PromotedOperand is the value that has been promoted. | ||||
4561 | /// \return True if the promotion is profitable, false otherwise. | ||||
4562 | bool AddressingModeMatcher::isPromotionProfitable( | ||||
4563 | unsigned NewCost, unsigned OldCost, Value *PromotedOperand) const { | ||||
4564 | LLVM_DEBUG(dbgs() << "OldCost: " << OldCost << "\tNewCost: " << NewCostdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "OldCost: " << OldCost << "\tNewCost: " << NewCost << '\n'; } } while (false) | ||||
4565 | << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "OldCost: " << OldCost << "\tNewCost: " << NewCost << '\n'; } } while (false); | ||||
4566 | // The cost of the new extensions is greater than the cost of the | ||||
4567 | // old extension plus what we folded. | ||||
4568 | // This is not profitable. | ||||
4569 | if (NewCost > OldCost) | ||||
4570 | return false; | ||||
4571 | if (NewCost < OldCost) | ||||
4572 | return true; | ||||
4573 | // The promotion is neutral but it may help folding the sign extension in | ||||
4574 | // loads for instance. | ||||
4575 | // Check that we did not create an illegal instruction. | ||||
4576 | return isPromotedInstructionLegal(TLI, DL, PromotedOperand); | ||||
4577 | } | ||||
4578 | |||||
4579 | /// Given an instruction or constant expr, see if we can fold the operation | ||||
4580 | /// into the addressing mode. If so, update the addressing mode and return | ||||
4581 | /// true, otherwise return false without modifying AddrMode. | ||||
4582 | /// If \p MovedAway is not NULL, it contains the information of whether or | ||||
4583 | /// not AddrInst has to be folded into the addressing mode on success. | ||||
4584 | /// If \p MovedAway == true, \p AddrInst will not be part of the addressing | ||||
4585 | /// because it has been moved away. | ||||
4586 | /// Thus AddrInst must not be added in the matched instructions. | ||||
4587 | /// This state can happen when AddrInst is a sext, since it may be moved away. | ||||
4588 | /// Therefore, AddrInst may not be valid when MovedAway is true and it must | ||||
4589 | /// not be referenced anymore. | ||||
4590 | bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode, | ||||
4591 | unsigned Depth, | ||||
4592 | bool *MovedAway) { | ||||
4593 | // Avoid exponential behavior on extremely deep expression trees. | ||||
4594 | if (Depth >= 5) | ||||
4595 | return false; | ||||
4596 | |||||
4597 | // By default, all matched instructions stay in place. | ||||
4598 | if (MovedAway) | ||||
4599 | *MovedAway = false; | ||||
4600 | |||||
4601 | switch (Opcode) { | ||||
4602 | case Instruction::PtrToInt: | ||||
4603 | // PtrToInt is always a noop, as we know that the int type is pointer sized. | ||||
4604 | return matchAddr(AddrInst->getOperand(0), Depth); | ||||
4605 | case Instruction::IntToPtr: { | ||||
4606 | auto AS = AddrInst->getType()->getPointerAddressSpace(); | ||||
4607 | auto PtrTy = MVT::getIntegerVT(DL.getPointerSizeInBits(AS)); | ||||
4608 | // This inttoptr is a no-op if the integer type is pointer sized. | ||||
4609 | if (TLI.getValueType(DL, AddrInst->getOperand(0)->getType()) == PtrTy) | ||||
4610 | return matchAddr(AddrInst->getOperand(0), Depth); | ||||
4611 | return false; | ||||
4612 | } | ||||
4613 | case Instruction::BitCast: | ||||
4614 | // BitCast is always a noop, and we can handle it as long as it is | ||||
4615 | // int->int or pointer->pointer (we don't want int<->fp or something). | ||||
4616 | if (AddrInst->getOperand(0)->getType()->isIntOrPtrTy() && | ||||
4617 | // Don't touch identity bitcasts. These were probably put here by LSR, | ||||
4618 | // and we don't want to mess around with them. Assume it knows what it | ||||
4619 | // is doing. | ||||
4620 | AddrInst->getOperand(0)->getType() != AddrInst->getType()) | ||||
4621 | return matchAddr(AddrInst->getOperand(0), Depth); | ||||
4622 | return false; | ||||
4623 | case Instruction::AddrSpaceCast: { | ||||
4624 | unsigned SrcAS = | ||||
4625 | AddrInst->getOperand(0)->getType()->getPointerAddressSpace(); | ||||
4626 | unsigned DestAS = AddrInst->getType()->getPointerAddressSpace(); | ||||
4627 | if (TLI.getTargetMachine().isNoopAddrSpaceCast(SrcAS, DestAS)) | ||||
4628 | return matchAddr(AddrInst->getOperand(0), Depth); | ||||
4629 | return false; | ||||
4630 | } | ||||
4631 | case Instruction::Add: { | ||||
4632 | // Check to see if we can merge in one operand, then the other. If so, we | ||||
4633 | // win. | ||||
4634 | ExtAddrMode BackupAddrMode = AddrMode; | ||||
4635 | unsigned OldSize = AddrModeInsts.size(); | ||||
4636 | // Start a transaction at this point. | ||||
4637 | // The LHS may match but not the RHS. | ||||
4638 | // Therefore, we need a higher level restoration point to undo partially | ||||
4639 | // matched operation. | ||||
4640 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = | ||||
4641 | TPT.getRestorationPoint(); | ||||
4642 | |||||
4643 | // Try to match an integer constant second to increase its chance of ending | ||||
4644 | // up in `BaseOffs`, resp. decrease its chance of ending up in `BaseReg`. | ||||
4645 | int First = 0, Second = 1; | ||||
4646 | if (isa<ConstantInt>(AddrInst->getOperand(First)) | ||||
4647 | && !isa<ConstantInt>(AddrInst->getOperand(Second))) | ||||
4648 | std::swap(First, Second); | ||||
4649 | AddrMode.InBounds = false; | ||||
4650 | if (matchAddr(AddrInst->getOperand(First), Depth + 1) && | ||||
4651 | matchAddr(AddrInst->getOperand(Second), Depth + 1)) | ||||
4652 | return true; | ||||
4653 | |||||
4654 | // Restore the old addr mode info. | ||||
4655 | AddrMode = BackupAddrMode; | ||||
4656 | AddrModeInsts.resize(OldSize); | ||||
4657 | TPT.rollback(LastKnownGood); | ||||
4658 | |||||
4659 | // Otherwise this was over-aggressive. Try merging operands in the opposite | ||||
4660 | // order. | ||||
4661 | if (matchAddr(AddrInst->getOperand(Second), Depth + 1) && | ||||
4662 | matchAddr(AddrInst->getOperand(First), Depth + 1)) | ||||
4663 | return true; | ||||
4664 | |||||
4665 | // Otherwise we definitely can't merge the ADD in. | ||||
4666 | AddrMode = BackupAddrMode; | ||||
4667 | AddrModeInsts.resize(OldSize); | ||||
4668 | TPT.rollback(LastKnownGood); | ||||
4669 | break; | ||||
4670 | } | ||||
4671 | // case Instruction::Or: | ||||
4672 | // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD. | ||||
4673 | // break; | ||||
4674 | case Instruction::Mul: | ||||
4675 | case Instruction::Shl: { | ||||
4676 | // Can only handle X*C and X << C. | ||||
4677 | AddrMode.InBounds = false; | ||||
4678 | ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1)); | ||||
4679 | if (!RHS || RHS->getBitWidth() > 64) | ||||
4680 | return false; | ||||
4681 | int64_t Scale = Opcode == Instruction::Shl | ||||
4682 | ? 1LL << RHS->getLimitedValue(RHS->getBitWidth() - 1) | ||||
4683 | : RHS->getSExtValue(); | ||||
4684 | |||||
4685 | return matchScaledValue(AddrInst->getOperand(0), Scale, Depth); | ||||
4686 | } | ||||
4687 | case Instruction::GetElementPtr: { | ||||
4688 | // Scan the GEP. We check it if it contains constant offsets and at most | ||||
4689 | // one variable offset. | ||||
4690 | int VariableOperand = -1; | ||||
4691 | unsigned VariableScale = 0; | ||||
4692 | |||||
4693 | int64_t ConstantOffset = 0; | ||||
4694 | gep_type_iterator GTI = gep_type_begin(AddrInst); | ||||
4695 | for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) { | ||||
4696 | if (StructType *STy = GTI.getStructTypeOrNull()) { | ||||
4697 | const StructLayout *SL = DL.getStructLayout(STy); | ||||
4698 | unsigned Idx = | ||||
4699 | cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue(); | ||||
4700 | ConstantOffset += SL->getElementOffset(Idx); | ||||
4701 | } else { | ||||
4702 | TypeSize TS = DL.getTypeAllocSize(GTI.getIndexedType()); | ||||
4703 | if (TS.isNonZero()) { | ||||
4704 | // The optimisations below currently only work for fixed offsets. | ||||
4705 | if (TS.isScalable()) | ||||
4706 | return false; | ||||
4707 | int64_t TypeSize = TS.getFixedValue(); | ||||
4708 | if (ConstantInt *CI = | ||||
4709 | dyn_cast<ConstantInt>(AddrInst->getOperand(i))) { | ||||
4710 | const APInt &CVal = CI->getValue(); | ||||
4711 | if (CVal.getSignificantBits() <= 64) { | ||||
4712 | ConstantOffset += CVal.getSExtValue() * TypeSize; | ||||
4713 | continue; | ||||
4714 | } | ||||
4715 | } | ||||
4716 | // We only allow one variable index at the moment. | ||||
4717 | if (VariableOperand != -1) | ||||
4718 | return false; | ||||
4719 | |||||
4720 | // Remember the variable index. | ||||
4721 | VariableOperand = i; | ||||
4722 | VariableScale = TypeSize; | ||||
4723 | } | ||||
4724 | } | ||||
4725 | } | ||||
4726 | |||||
4727 | // A common case is for the GEP to only do a constant offset. In this case, | ||||
4728 | // just add it to the disp field and check validity. | ||||
4729 | if (VariableOperand == -1) { | ||||
4730 | AddrMode.BaseOffs += ConstantOffset; | ||||
4731 | if (matchAddr(AddrInst->getOperand(0), Depth + 1)) { | ||||
4732 | if (!cast<GEPOperator>(AddrInst)->isInBounds()) | ||||
4733 | AddrMode.InBounds = false; | ||||
4734 | return true; | ||||
4735 | } | ||||
4736 | AddrMode.BaseOffs -= ConstantOffset; | ||||
4737 | |||||
4738 | if (EnableGEPOffsetSplit && isa<GetElementPtrInst>(AddrInst) && | ||||
4739 | TLI.shouldConsiderGEPOffsetSplit() && Depth == 0 && | ||||
4740 | ConstantOffset > 0) { | ||||
4741 | // Record GEPs with non-zero offsets as candidates for splitting in | ||||
4742 | // the event that the offset cannot fit into the r+i addressing mode. | ||||
4743 | // Simple and common case that only one GEP is used in calculating the | ||||
4744 | // address for the memory access. | ||||
4745 | Value *Base = AddrInst->getOperand(0); | ||||
4746 | auto *BaseI = dyn_cast<Instruction>(Base); | ||||
4747 | auto *GEP = cast<GetElementPtrInst>(AddrInst); | ||||
4748 | if (isa<Argument>(Base) || isa<GlobalValue>(Base) || | ||||
4749 | (BaseI && !isa<CastInst>(BaseI) && | ||||
4750 | !isa<GetElementPtrInst>(BaseI))) { | ||||
4751 | // Make sure the parent block allows inserting non-PHI instructions | ||||
4752 | // before the terminator. | ||||
4753 | BasicBlock *Parent = BaseI ? BaseI->getParent() | ||||
4754 | : &GEP->getFunction()->getEntryBlock(); | ||||
4755 | if (!Parent->getTerminator()->isEHPad()) | ||||
4756 | LargeOffsetGEP = std::make_pair(GEP, ConstantOffset); | ||||
4757 | } | ||||
4758 | } | ||||
4759 | |||||
4760 | return false; | ||||
4761 | } | ||||
4762 | |||||
4763 | // Save the valid addressing mode in case we can't match. | ||||
4764 | ExtAddrMode BackupAddrMode = AddrMode; | ||||
4765 | unsigned OldSize = AddrModeInsts.size(); | ||||
4766 | |||||
4767 | // See if the scale and offset amount is valid for this target. | ||||
4768 | AddrMode.BaseOffs += ConstantOffset; | ||||
4769 | if (!cast<GEPOperator>(AddrInst)->isInBounds()) | ||||
4770 | AddrMode.InBounds = false; | ||||
4771 | |||||
4772 | // Match the base operand of the GEP. | ||||
4773 | if (!matchAddr(AddrInst->getOperand(0), Depth + 1)) { | ||||
4774 | // If it couldn't be matched, just stuff the value in a register. | ||||
4775 | if (AddrMode.HasBaseReg) { | ||||
4776 | AddrMode = BackupAddrMode; | ||||
4777 | AddrModeInsts.resize(OldSize); | ||||
4778 | return false; | ||||
4779 | } | ||||
4780 | AddrMode.HasBaseReg = true; | ||||
4781 | AddrMode.BaseReg = AddrInst->getOperand(0); | ||||
4782 | } | ||||
4783 | |||||
4784 | // Match the remaining variable portion of the GEP. | ||||
4785 | if (!matchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale, | ||||
4786 | Depth)) { | ||||
4787 | // If it couldn't be matched, try stuffing the base into a register | ||||
4788 | // instead of matching it, and retrying the match of the scale. | ||||
4789 | AddrMode = BackupAddrMode; | ||||
4790 | AddrModeInsts.resize(OldSize); | ||||
4791 | if (AddrMode.HasBaseReg) | ||||
4792 | return false; | ||||
4793 | AddrMode.HasBaseReg = true; | ||||
4794 | AddrMode.BaseReg = AddrInst->getOperand(0); | ||||
4795 | AddrMode.BaseOffs += ConstantOffset; | ||||
4796 | if (!matchScaledValue(AddrInst->getOperand(VariableOperand), | ||||
4797 | VariableScale, Depth)) { | ||||
4798 | // If even that didn't work, bail. | ||||
4799 | AddrMode = BackupAddrMode; | ||||
4800 | AddrModeInsts.resize(OldSize); | ||||
4801 | return false; | ||||
4802 | } | ||||
4803 | } | ||||
4804 | |||||
4805 | return true; | ||||
4806 | } | ||||
4807 | case Instruction::SExt: | ||||
4808 | case Instruction::ZExt: { | ||||
4809 | Instruction *Ext = dyn_cast<Instruction>(AddrInst); | ||||
4810 | if (!Ext) | ||||
4811 | return false; | ||||
4812 | |||||
4813 | // Try to move this ext out of the way of the addressing mode. | ||||
4814 | // Ask for a method for doing so. | ||||
4815 | TypePromotionHelper::Action TPH = | ||||
4816 | TypePromotionHelper::getAction(Ext, InsertedInsts, TLI, PromotedInsts); | ||||
4817 | if (!TPH) | ||||
4818 | return false; | ||||
4819 | |||||
4820 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = | ||||
4821 | TPT.getRestorationPoint(); | ||||
4822 | unsigned CreatedInstsCost = 0; | ||||
4823 | unsigned ExtCost = !TLI.isExtFree(Ext); | ||||
4824 | Value *PromotedOperand = | ||||
4825 | TPH(Ext, TPT, PromotedInsts, CreatedInstsCost, nullptr, nullptr, TLI); | ||||
4826 | // SExt has been moved away. | ||||
4827 | // Thus either it will be rematched later in the recursive calls or it is | ||||
4828 | // gone. Anyway, we must not fold it into the addressing mode at this point. | ||||
4829 | // E.g., | ||||
4830 | // op = add opnd, 1 | ||||
4831 | // idx = ext op | ||||
4832 | // addr = gep base, idx | ||||
4833 | // is now: | ||||
4834 | // promotedOpnd = ext opnd <- no match here | ||||
4835 | // op = promoted_add promotedOpnd, 1 <- match (later in recursive calls) | ||||
4836 | // addr = gep base, op <- match | ||||
4837 | if (MovedAway) | ||||
4838 | *MovedAway = true; | ||||
4839 | |||||
4840 | assert(PromotedOperand &&(static_cast <bool> (PromotedOperand && "TypePromotionHelper should have filtered out those cases" ) ? void (0) : __assert_fail ("PromotedOperand && \"TypePromotionHelper should have filtered out those cases\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 4841, __extension__ __PRETTY_FUNCTION__ )) | ||||
4841 | "TypePromotionHelper should have filtered out those cases")(static_cast <bool> (PromotedOperand && "TypePromotionHelper should have filtered out those cases" ) ? void (0) : __assert_fail ("PromotedOperand && \"TypePromotionHelper should have filtered out those cases\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 4841, __extension__ __PRETTY_FUNCTION__ )); | ||||
4842 | |||||
4843 | ExtAddrMode BackupAddrMode = AddrMode; | ||||
4844 | unsigned OldSize = AddrModeInsts.size(); | ||||
4845 | |||||
4846 | if (!matchAddr(PromotedOperand, Depth) || | ||||
4847 | // The total of the new cost is equal to the cost of the created | ||||
4848 | // instructions. | ||||
4849 | // The total of the old cost is equal to the cost of the extension plus | ||||
4850 | // what we have saved in the addressing mode. | ||||
4851 | !isPromotionProfitable(CreatedInstsCost, | ||||
4852 | ExtCost + (AddrModeInsts.size() - OldSize), | ||||
4853 | PromotedOperand)) { | ||||
4854 | AddrMode = BackupAddrMode; | ||||
4855 | AddrModeInsts.resize(OldSize); | ||||
4856 | LLVM_DEBUG(dbgs() << "Sign extension does not pay off: rollback\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Sign extension does not pay off: rollback\n" ; } } while (false); | ||||
4857 | TPT.rollback(LastKnownGood); | ||||
4858 | return false; | ||||
4859 | } | ||||
4860 | return true; | ||||
4861 | } | ||||
4862 | } | ||||
4863 | return false; | ||||
4864 | } | ||||
4865 | |||||
4866 | /// If we can, try to add the value of 'Addr' into the current addressing mode. | ||||
4867 | /// If Addr can't be added to AddrMode this returns false and leaves AddrMode | ||||
4868 | /// unmodified. This assumes that Addr is either a pointer type or intptr_t | ||||
4869 | /// for the target. | ||||
4870 | /// | ||||
4871 | bool AddressingModeMatcher::matchAddr(Value *Addr, unsigned Depth) { | ||||
4872 | // Start a transaction at this point that we will rollback if the matching | ||||
4873 | // fails. | ||||
4874 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = | ||||
4875 | TPT.getRestorationPoint(); | ||||
4876 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) { | ||||
4877 | if (CI->getValue().isSignedIntN(64)) { | ||||
4878 | // Fold in immediates if legal for the target. | ||||
4879 | AddrMode.BaseOffs += CI->getSExtValue(); | ||||
4880 | if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) | ||||
4881 | return true; | ||||
4882 | AddrMode.BaseOffs -= CI->getSExtValue(); | ||||
4883 | } | ||||
4884 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) { | ||||
4885 | // If this is a global variable, try to fold it into the addressing mode. | ||||
4886 | if (!AddrMode.BaseGV) { | ||||
4887 | AddrMode.BaseGV = GV; | ||||
4888 | if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) | ||||
4889 | return true; | ||||
4890 | AddrMode.BaseGV = nullptr; | ||||
4891 | } | ||||
4892 | } else if (Instruction *I = dyn_cast<Instruction>(Addr)) { | ||||
4893 | ExtAddrMode BackupAddrMode = AddrMode; | ||||
4894 | unsigned OldSize = AddrModeInsts.size(); | ||||
4895 | |||||
4896 | // Check to see if it is possible to fold this operation. | ||||
4897 | bool MovedAway = false; | ||||
4898 | if (matchOperationAddr(I, I->getOpcode(), Depth, &MovedAway)) { | ||||
4899 | // This instruction may have been moved away. If so, there is nothing | ||||
4900 | // to check here. | ||||
4901 | if (MovedAway) | ||||
4902 | return true; | ||||
4903 | // Okay, it's possible to fold this. Check to see if it is actually | ||||
4904 | // *profitable* to do so. We use a simple cost model to avoid increasing | ||||
4905 | // register pressure too much. | ||||
4906 | if (I->hasOneUse() || | ||||
4907 | isProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) { | ||||
4908 | AddrModeInsts.push_back(I); | ||||
4909 | return true; | ||||
4910 | } | ||||
4911 | |||||
4912 | // It isn't profitable to do this, roll back. | ||||
4913 | AddrMode = BackupAddrMode; | ||||
4914 | AddrModeInsts.resize(OldSize); | ||||
4915 | TPT.rollback(LastKnownGood); | ||||
4916 | } | ||||
4917 | } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) { | ||||
4918 | if (matchOperationAddr(CE, CE->getOpcode(), Depth)) | ||||
4919 | return true; | ||||
4920 | TPT.rollback(LastKnownGood); | ||||
4921 | } else if (isa<ConstantPointerNull>(Addr)) { | ||||
4922 | // Null pointer gets folded without affecting the addressing mode. | ||||
4923 | return true; | ||||
4924 | } | ||||
4925 | |||||
4926 | // Worse case, the target should support [reg] addressing modes. :) | ||||
4927 | if (!AddrMode.HasBaseReg) { | ||||
4928 | AddrMode.HasBaseReg = true; | ||||
4929 | AddrMode.BaseReg = Addr; | ||||
4930 | // Still check for legality in case the target supports [imm] but not [i+r]. | ||||
4931 | if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) | ||||
4932 | return true; | ||||
4933 | AddrMode.HasBaseReg = false; | ||||
4934 | AddrMode.BaseReg = nullptr; | ||||
4935 | } | ||||
4936 | |||||
4937 | // If the base register is already taken, see if we can do [r+r]. | ||||
4938 | if (AddrMode.Scale == 0) { | ||||
4939 | AddrMode.Scale = 1; | ||||
4940 | AddrMode.ScaledReg = Addr; | ||||
4941 | if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace)) | ||||
4942 | return true; | ||||
4943 | AddrMode.Scale = 0; | ||||
4944 | AddrMode.ScaledReg = nullptr; | ||||
4945 | } | ||||
4946 | // Couldn't match. | ||||
4947 | TPT.rollback(LastKnownGood); | ||||
4948 | return false; | ||||
4949 | } | ||||
4950 | |||||
4951 | /// Check to see if all uses of OpVal by the specified inline asm call are due | ||||
4952 | /// to memory operands. If so, return true, otherwise return false. | ||||
4953 | static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal, | ||||
4954 | const TargetLowering &TLI, | ||||
4955 | const TargetRegisterInfo &TRI) { | ||||
4956 | const Function *F = CI->getFunction(); | ||||
4957 | TargetLowering::AsmOperandInfoVector TargetConstraints = | ||||
4958 | TLI.ParseConstraints(F->getParent()->getDataLayout(), &TRI, *CI); | ||||
4959 | |||||
4960 | for (TargetLowering::AsmOperandInfo &OpInfo : TargetConstraints) { | ||||
4961 | // Compute the constraint code and ConstraintType to use. | ||||
4962 | TLI.ComputeConstraintToUse(OpInfo, SDValue()); | ||||
4963 | |||||
4964 | // If this asm operand is our Value*, and if it isn't an indirect memory | ||||
4965 | // operand, we can't fold it! TODO: Also handle C_Address? | ||||
4966 | if (OpInfo.CallOperandVal == OpVal && | ||||
4967 | (OpInfo.ConstraintType != TargetLowering::C_Memory || | ||||
4968 | !OpInfo.isIndirect)) | ||||
4969 | return false; | ||||
4970 | } | ||||
4971 | |||||
4972 | return true; | ||||
4973 | } | ||||
4974 | |||||
4975 | /// Recursively walk all the uses of I until we find a memory use. | ||||
4976 | /// If we find an obviously non-foldable instruction, return true. | ||||
4977 | /// Add accessed addresses and types to MemoryUses. | ||||
4978 | static bool FindAllMemoryUses( | ||||
4979 | Instruction *I, SmallVectorImpl<std::pair<Use *, Type *>> &MemoryUses, | ||||
4980 | SmallPtrSetImpl<Instruction *> &ConsideredInsts, const TargetLowering &TLI, | ||||
4981 | const TargetRegisterInfo &TRI, bool OptSize, ProfileSummaryInfo *PSI, | ||||
4982 | BlockFrequencyInfo *BFI, unsigned &SeenInsts) { | ||||
4983 | // If we already considered this instruction, we're done. | ||||
4984 | if (!ConsideredInsts.insert(I).second) | ||||
4985 | return false; | ||||
4986 | |||||
4987 | // If this is an obviously unfoldable instruction, bail out. | ||||
4988 | if (!MightBeFoldableInst(I)) | ||||
4989 | return true; | ||||
4990 | |||||
4991 | // Loop over all the uses, recursively processing them. | ||||
4992 | for (Use &U : I->uses()) { | ||||
4993 | // Conservatively return true if we're seeing a large number or a deep chain | ||||
4994 | // of users. This avoids excessive compilation times in pathological cases. | ||||
4995 | if (SeenInsts++ >= MaxAddressUsersToScan) | ||||
4996 | return true; | ||||
4997 | |||||
4998 | Instruction *UserI = cast<Instruction>(U.getUser()); | ||||
4999 | if (LoadInst *LI = dyn_cast<LoadInst>(UserI)) { | ||||
5000 | MemoryUses.push_back({&U, LI->getType()}); | ||||
5001 | continue; | ||||
5002 | } | ||||
5003 | |||||
5004 | if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) { | ||||
5005 | if (U.getOperandNo() != StoreInst::getPointerOperandIndex()) | ||||
5006 | return true; // Storing addr, not into addr. | ||||
5007 | MemoryUses.push_back({&U, SI->getValueOperand()->getType()}); | ||||
5008 | continue; | ||||
5009 | } | ||||
5010 | |||||
5011 | if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(UserI)) { | ||||
5012 | if (U.getOperandNo() != AtomicRMWInst::getPointerOperandIndex()) | ||||
5013 | return true; // Storing addr, not into addr. | ||||
5014 | MemoryUses.push_back({&U, RMW->getValOperand()->getType()}); | ||||
5015 | continue; | ||||
5016 | } | ||||
5017 | |||||
5018 | if (AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(UserI)) { | ||||
5019 | if (U.getOperandNo() != AtomicCmpXchgInst::getPointerOperandIndex()) | ||||
5020 | return true; // Storing addr, not into addr. | ||||
5021 | MemoryUses.push_back({&U, CmpX->getCompareOperand()->getType()}); | ||||
5022 | continue; | ||||
5023 | } | ||||
5024 | |||||
5025 | if (CallInst *CI = dyn_cast<CallInst>(UserI)) { | ||||
5026 | if (CI->hasFnAttr(Attribute::Cold)) { | ||||
5027 | // If this is a cold call, we can sink the addressing calculation into | ||||
5028 | // the cold path. See optimizeCallInst | ||||
5029 | bool OptForSize = | ||||
5030 | OptSize || llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI); | ||||
5031 | if (!OptForSize) | ||||
5032 | continue; | ||||
5033 | } | ||||
5034 | |||||
5035 | InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledOperand()); | ||||
5036 | if (!IA) | ||||
5037 | return true; | ||||
5038 | |||||
5039 | // If this is a memory operand, we're cool, otherwise bail out. | ||||
5040 | if (!IsOperandAMemoryOperand(CI, IA, I, TLI, TRI)) | ||||
5041 | return true; | ||||
5042 | continue; | ||||
5043 | } | ||||
5044 | |||||
5045 | if (FindAllMemoryUses(UserI, MemoryUses, ConsideredInsts, TLI, TRI, OptSize, | ||||
5046 | PSI, BFI, SeenInsts)) | ||||
5047 | return true; | ||||
5048 | } | ||||
5049 | |||||
5050 | return false; | ||||
5051 | } | ||||
5052 | |||||
5053 | static bool FindAllMemoryUses( | ||||
5054 | Instruction *I, SmallVectorImpl<std::pair<Use *, Type *>> &MemoryUses, | ||||
5055 | const TargetLowering &TLI, const TargetRegisterInfo &TRI, bool OptSize, | ||||
5056 | ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) { | ||||
5057 | unsigned SeenInsts = 0; | ||||
5058 | SmallPtrSet<Instruction *, 16> ConsideredInsts; | ||||
5059 | return FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TLI, TRI, OptSize, | ||||
5060 | PSI, BFI, SeenInsts); | ||||
5061 | } | ||||
5062 | |||||
5063 | |||||
5064 | /// Return true if Val is already known to be live at the use site that we're | ||||
5065 | /// folding it into. If so, there is no cost to include it in the addressing | ||||
5066 | /// mode. KnownLive1 and KnownLive2 are two values that we know are live at the | ||||
5067 | /// instruction already. | ||||
5068 | bool AddressingModeMatcher::valueAlreadyLiveAtInst(Value *Val, | ||||
5069 | Value *KnownLive1, | ||||
5070 | Value *KnownLive2) { | ||||
5071 | // If Val is either of the known-live values, we know it is live! | ||||
5072 | if (Val == nullptr || Val == KnownLive1 || Val == KnownLive2) | ||||
5073 | return true; | ||||
5074 | |||||
5075 | // All values other than instructions and arguments (e.g. constants) are live. | ||||
5076 | if (!isa<Instruction>(Val) && !isa<Argument>(Val)) | ||||
5077 | return true; | ||||
5078 | |||||
5079 | // If Val is a constant sized alloca in the entry block, it is live, this is | ||||
5080 | // true because it is just a reference to the stack/frame pointer, which is | ||||
5081 | // live for the whole function. | ||||
5082 | if (AllocaInst *AI = dyn_cast<AllocaInst>(Val)) | ||||
5083 | if (AI->isStaticAlloca()) | ||||
5084 | return true; | ||||
5085 | |||||
5086 | // Check to see if this value is already used in the memory instruction's | ||||
5087 | // block. If so, it's already live into the block at the very least, so we | ||||
5088 | // can reasonably fold it. | ||||
5089 | return Val->isUsedInBasicBlock(MemoryInst->getParent()); | ||||
5090 | } | ||||
5091 | |||||
5092 | /// It is possible for the addressing mode of the machine to fold the specified | ||||
5093 | /// instruction into a load or store that ultimately uses it. | ||||
5094 | /// However, the specified instruction has multiple uses. | ||||
5095 | /// Given this, it may actually increase register pressure to fold it | ||||
5096 | /// into the load. For example, consider this code: | ||||
5097 | /// | ||||
5098 | /// X = ... | ||||
5099 | /// Y = X+1 | ||||
5100 | /// use(Y) -> nonload/store | ||||
5101 | /// Z = Y+1 | ||||
5102 | /// load Z | ||||
5103 | /// | ||||
5104 | /// In this case, Y has multiple uses, and can be folded into the load of Z | ||||
5105 | /// (yielding load [X+2]). However, doing this will cause both "X" and "X+1" to | ||||
5106 | /// be live at the use(Y) line. If we don't fold Y into load Z, we use one | ||||
5107 | /// fewer register. Since Y can't be folded into "use(Y)" we don't increase the | ||||
5108 | /// number of computations either. | ||||
5109 | /// | ||||
5110 | /// Note that this (like most of CodeGenPrepare) is just a rough heuristic. If | ||||
5111 | /// X was live across 'load Z' for other reasons, we actually *would* want to | ||||
5112 | /// fold the addressing mode in the Z case. This would make Y die earlier. | ||||
5113 | bool AddressingModeMatcher::isProfitableToFoldIntoAddressingMode( | ||||
5114 | Instruction *I, ExtAddrMode &AMBefore, ExtAddrMode &AMAfter) { | ||||
5115 | if (IgnoreProfitability) | ||||
5116 | return true; | ||||
5117 | |||||
5118 | // AMBefore is the addressing mode before this instruction was folded into it, | ||||
5119 | // and AMAfter is the addressing mode after the instruction was folded. Get | ||||
5120 | // the set of registers referenced by AMAfter and subtract out those | ||||
5121 | // referenced by AMBefore: this is the set of values which folding in this | ||||
5122 | // address extends the lifetime of. | ||||
5123 | // | ||||
5124 | // Note that there are only two potential values being referenced here, | ||||
5125 | // BaseReg and ScaleReg (global addresses are always available, as are any | ||||
5126 | // folded immediates). | ||||
5127 | Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg; | ||||
5128 | |||||
5129 | // If the BaseReg or ScaledReg was referenced by the previous addrmode, their | ||||
5130 | // lifetime wasn't extended by adding this instruction. | ||||
5131 | if (valueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg)) | ||||
5132 | BaseReg = nullptr; | ||||
5133 | if (valueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg)) | ||||
5134 | ScaledReg = nullptr; | ||||
5135 | |||||
5136 | // If folding this instruction (and it's subexprs) didn't extend any live | ||||
5137 | // ranges, we're ok with it. | ||||
5138 | if (!BaseReg && !ScaledReg) | ||||
5139 | return true; | ||||
5140 | |||||
5141 | // If all uses of this instruction can have the address mode sunk into them, | ||||
5142 | // we can remove the addressing mode and effectively trade one live register | ||||
5143 | // for another (at worst.) In this context, folding an addressing mode into | ||||
5144 | // the use is just a particularly nice way of sinking it. | ||||
5145 | SmallVector<std::pair<Use *, Type *>, 16> MemoryUses; | ||||
5146 | if (FindAllMemoryUses(I, MemoryUses, TLI, TRI, OptSize, PSI, BFI)) | ||||
5147 | return false; // Has a non-memory, non-foldable use! | ||||
5148 | |||||
5149 | // Now that we know that all uses of this instruction are part of a chain of | ||||
5150 | // computation involving only operations that could theoretically be folded | ||||
5151 | // into a memory use, loop over each of these memory operation uses and see | ||||
5152 | // if they could *actually* fold the instruction. The assumption is that | ||||
5153 | // addressing modes are cheap and that duplicating the computation involved | ||||
5154 | // many times is worthwhile, even on a fastpath. For sinking candidates | ||||
5155 | // (i.e. cold call sites), this serves as a way to prevent excessive code | ||||
5156 | // growth since most architectures have some reasonable small and fast way to | ||||
5157 | // compute an effective address. (i.e LEA on x86) | ||||
5158 | SmallVector<Instruction *, 32> MatchedAddrModeInsts; | ||||
5159 | for (const std::pair<Use *, Type *> &Pair : MemoryUses) { | ||||
5160 | Value *Address = Pair.first->get(); | ||||
5161 | Instruction *UserI = cast<Instruction>(Pair.first->getUser()); | ||||
5162 | Type *AddressAccessTy = Pair.second; | ||||
5163 | unsigned AS = Address->getType()->getPointerAddressSpace(); | ||||
5164 | |||||
5165 | // Do a match against the root of this address, ignoring profitability. This | ||||
5166 | // will tell us if the addressing mode for the memory operation will | ||||
5167 | // *actually* cover the shared instruction. | ||||
5168 | ExtAddrMode Result; | ||||
5169 | std::pair<AssertingVH<GetElementPtrInst>, int64_t> LargeOffsetGEP(nullptr, | ||||
5170 | 0); | ||||
5171 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = | ||||
5172 | TPT.getRestorationPoint(); | ||||
5173 | AddressingModeMatcher Matcher(MatchedAddrModeInsts, TLI, TRI, LI, getDTFn, | ||||
5174 | AddressAccessTy, AS, UserI, Result, | ||||
5175 | InsertedInsts, PromotedInsts, TPT, | ||||
5176 | LargeOffsetGEP, OptSize, PSI, BFI); | ||||
5177 | Matcher.IgnoreProfitability = true; | ||||
5178 | bool Success = Matcher.matchAddr(Address, 0); | ||||
5179 | (void)Success; | ||||
5180 | assert(Success && "Couldn't select *anything*?")(static_cast <bool> (Success && "Couldn't select *anything*?" ) ? void (0) : __assert_fail ("Success && \"Couldn't select *anything*?\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5180, __extension__ __PRETTY_FUNCTION__ )); | ||||
5181 | |||||
5182 | // The match was to check the profitability, the changes made are not | ||||
5183 | // part of the original matcher. Therefore, they should be dropped | ||||
5184 | // otherwise the original matcher will not present the right state. | ||||
5185 | TPT.rollback(LastKnownGood); | ||||
5186 | |||||
5187 | // If the match didn't cover I, then it won't be shared by it. | ||||
5188 | if (!is_contained(MatchedAddrModeInsts, I)) | ||||
5189 | return false; | ||||
5190 | |||||
5191 | MatchedAddrModeInsts.clear(); | ||||
5192 | } | ||||
5193 | |||||
5194 | return true; | ||||
5195 | } | ||||
5196 | |||||
5197 | /// Return true if the specified values are defined in a | ||||
5198 | /// different basic block than BB. | ||||
5199 | static bool IsNonLocalValue(Value *V, BasicBlock *BB) { | ||||
5200 | if (Instruction *I = dyn_cast<Instruction>(V)) | ||||
5201 | return I->getParent() != BB; | ||||
5202 | return false; | ||||
5203 | } | ||||
5204 | |||||
5205 | /// Sink addressing mode computation immediate before MemoryInst if doing so | ||||
5206 | /// can be done without increasing register pressure. The need for the | ||||
5207 | /// register pressure constraint means this can end up being an all or nothing | ||||
5208 | /// decision for all uses of the same addressing computation. | ||||
5209 | /// | ||||
5210 | /// Load and Store Instructions often have addressing modes that can do | ||||
5211 | /// significant amounts of computation. As such, instruction selection will try | ||||
5212 | /// to get the load or store to do as much computation as possible for the | ||||
5213 | /// program. The problem is that isel can only see within a single block. As | ||||
5214 | /// such, we sink as much legal addressing mode work into the block as possible. | ||||
5215 | /// | ||||
5216 | /// This method is used to optimize both load/store and inline asms with memory | ||||
5217 | /// operands. It's also used to sink addressing computations feeding into cold | ||||
5218 | /// call sites into their (cold) basic block. | ||||
5219 | /// | ||||
5220 | /// The motivation for handling sinking into cold blocks is that doing so can | ||||
5221 | /// both enable other address mode sinking (by satisfying the register pressure | ||||
5222 | /// constraint above), and reduce register pressure globally (by removing the | ||||
5223 | /// addressing mode computation from the fast path entirely.). | ||||
5224 | bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr, | ||||
5225 | Type *AccessTy, unsigned AddrSpace) { | ||||
5226 | Value *Repl = Addr; | ||||
5227 | |||||
5228 | // Try to collapse single-value PHI nodes. This is necessary to undo | ||||
5229 | // unprofitable PRE transformations. | ||||
5230 | SmallVector<Value *, 8> worklist; | ||||
5231 | SmallPtrSet<Value *, 16> Visited; | ||||
5232 | worklist.push_back(Addr); | ||||
5233 | |||||
5234 | // Use a worklist to iteratively look through PHI and select nodes, and | ||||
5235 | // ensure that the addressing mode obtained from the non-PHI/select roots of | ||||
5236 | // the graph are compatible. | ||||
5237 | bool PhiOrSelectSeen = false; | ||||
5238 | SmallVector<Instruction *, 16> AddrModeInsts; | ||||
5239 | const SimplifyQuery SQ(*DL, TLInfo); | ||||
5240 | AddressingModeCombiner AddrModes(SQ, Addr); | ||||
5241 | TypePromotionTransaction TPT(RemovedInsts); | ||||
5242 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = | ||||
5243 | TPT.getRestorationPoint(); | ||||
5244 | while (!worklist.empty()) { | ||||
5245 | Value *V = worklist.pop_back_val(); | ||||
5246 | |||||
5247 | // We allow traversing cyclic Phi nodes. | ||||
5248 | // In case of success after this loop we ensure that traversing through | ||||
5249 | // Phi nodes ends up with all cases to compute address of the form | ||||
5250 | // BaseGV + Base + Scale * Index + Offset | ||||
5251 | // where Scale and Offset are constans and BaseGV, Base and Index | ||||
5252 | // are exactly the same Values in all cases. | ||||
5253 | // It means that BaseGV, Scale and Offset dominate our memory instruction | ||||
5254 | // and have the same value as they had in address computation represented | ||||
5255 | // as Phi. So we can safely sink address computation to memory instruction. | ||||
5256 | if (!Visited.insert(V).second) | ||||
5257 | continue; | ||||
5258 | |||||
5259 | // For a PHI node, push all of its incoming values. | ||||
5260 | if (PHINode *P = dyn_cast<PHINode>(V)) { | ||||
5261 | append_range(worklist, P->incoming_values()); | ||||
5262 | PhiOrSelectSeen = true; | ||||
5263 | continue; | ||||
5264 | } | ||||
5265 | // Similar for select. | ||||
5266 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) { | ||||
5267 | worklist.push_back(SI->getFalseValue()); | ||||
5268 | worklist.push_back(SI->getTrueValue()); | ||||
5269 | PhiOrSelectSeen = true; | ||||
5270 | continue; | ||||
5271 | } | ||||
5272 | |||||
5273 | // For non-PHIs, determine the addressing mode being computed. Note that | ||||
5274 | // the result may differ depending on what other uses our candidate | ||||
5275 | // addressing instructions might have. | ||||
5276 | AddrModeInsts.clear(); | ||||
5277 | std::pair<AssertingVH<GetElementPtrInst>, int64_t> LargeOffsetGEP(nullptr, | ||||
5278 | 0); | ||||
5279 | // Defer the query (and possible computation of) the dom tree to point of | ||||
5280 | // actual use. It's expected that most address matches don't actually need | ||||
5281 | // the domtree. | ||||
5282 | auto getDTFn = [MemoryInst, this]() -> const DominatorTree & { | ||||
5283 | Function *F = MemoryInst->getParent()->getParent(); | ||||
5284 | return this->getDT(*F); | ||||
5285 | }; | ||||
5286 | ExtAddrMode NewAddrMode = AddressingModeMatcher::Match( | ||||
5287 | V, AccessTy, AddrSpace, MemoryInst, AddrModeInsts, *TLI, *LI, getDTFn, | ||||
5288 | *TRI, InsertedInsts, PromotedInsts, TPT, LargeOffsetGEP, OptSize, PSI, | ||||
5289 | BFI.get()); | ||||
5290 | |||||
5291 | GetElementPtrInst *GEP = LargeOffsetGEP.first; | ||||
5292 | if (GEP && !NewGEPBases.count(GEP)) { | ||||
5293 | // If splitting the underlying data structure can reduce the offset of a | ||||
5294 | // GEP, collect the GEP. Skip the GEPs that are the new bases of | ||||
5295 | // previously split data structures. | ||||
5296 | LargeOffsetGEPMap[GEP->getPointerOperand()].push_back(LargeOffsetGEP); | ||||
5297 | LargeOffsetGEPID.insert(std::make_pair(GEP, LargeOffsetGEPID.size())); | ||||
5298 | } | ||||
5299 | |||||
5300 | NewAddrMode.OriginalValue = V; | ||||
5301 | if (!AddrModes.addNewAddrMode(NewAddrMode)) | ||||
5302 | break; | ||||
5303 | } | ||||
5304 | |||||
5305 | // Try to combine the AddrModes we've collected. If we couldn't collect any, | ||||
5306 | // or we have multiple but either couldn't combine them or combining them | ||||
5307 | // wouldn't do anything useful, bail out now. | ||||
5308 | if (!AddrModes.combineAddrModes()) { | ||||
5309 | TPT.rollback(LastKnownGood); | ||||
5310 | return false; | ||||
5311 | } | ||||
5312 | bool Modified = TPT.commit(); | ||||
5313 | |||||
5314 | // Get the combined AddrMode (or the only AddrMode, if we only had one). | ||||
5315 | ExtAddrMode AddrMode = AddrModes.getAddrMode(); | ||||
5316 | |||||
5317 | // If all the instructions matched are already in this BB, don't do anything. | ||||
5318 | // If we saw a Phi node then it is not local definitely, and if we saw a | ||||
5319 | // select then we want to push the address calculation past it even if it's | ||||
5320 | // already in this BB. | ||||
5321 | if (!PhiOrSelectSeen && none_of(AddrModeInsts, [&](Value *V) { | ||||
5322 | return IsNonLocalValue(V, MemoryInst->getParent()); | ||||
5323 | })) { | ||||
5324 | LLVM_DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrModedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"; } } while (false) | ||||
5325 | << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"; } } while (false); | ||||
5326 | return Modified; | ||||
5327 | } | ||||
5328 | |||||
5329 | // Insert this computation right after this user. Since our caller is | ||||
5330 | // scanning from the top of the BB to the bottom, reuse of the expr are | ||||
5331 | // guaranteed to happen later. | ||||
5332 | IRBuilder<> Builder(MemoryInst); | ||||
5333 | |||||
5334 | // Now that we determined the addressing expression we want to use and know | ||||
5335 | // that we have to sink it into this block. Check to see if we have already | ||||
5336 | // done this for some other load/store instr in this block. If so, reuse | ||||
5337 | // the computation. Before attempting reuse, check if the address is valid | ||||
5338 | // as it may have been erased. | ||||
5339 | |||||
5340 | WeakTrackingVH SunkAddrVH = SunkAddrs[Addr]; | ||||
5341 | |||||
5342 | Value *SunkAddr = SunkAddrVH.pointsToAliveValue() ? SunkAddrVH : nullptr; | ||||
5343 | Type *IntPtrTy = DL->getIntPtrType(Addr->getType()); | ||||
5344 | if (SunkAddr) { | ||||
5345 | LLVM_DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrModedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " << *MemoryInst << "\n"; } } while (false) | ||||
5346 | << " for " << *MemoryInst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " << *MemoryInst << "\n"; } } while (false); | ||||
5347 | if (SunkAddr->getType() != Addr->getType()) { | ||||
5348 | if (SunkAddr->getType()->getPointerAddressSpace() != | ||||
5349 | Addr->getType()->getPointerAddressSpace() && | ||||
5350 | !DL->isNonIntegralPointerType(Addr->getType())) { | ||||
5351 | // There are two reasons the address spaces might not match: a no-op | ||||
5352 | // addrspacecast, or a ptrtoint/inttoptr pair. Either way, we emit a | ||||
5353 | // ptrtoint/inttoptr pair to ensure we match the original semantics. | ||||
5354 | // TODO: allow bitcast between different address space pointers with the | ||||
5355 | // same size. | ||||
5356 | SunkAddr = Builder.CreatePtrToInt(SunkAddr, IntPtrTy, "sunkaddr"); | ||||
5357 | SunkAddr = | ||||
5358 | Builder.CreateIntToPtr(SunkAddr, Addr->getType(), "sunkaddr"); | ||||
5359 | } else | ||||
5360 | SunkAddr = Builder.CreatePointerCast(SunkAddr, Addr->getType()); | ||||
5361 | } | ||||
5362 | } else if (AddrSinkUsingGEPs || (!AddrSinkUsingGEPs.getNumOccurrences() && | ||||
5363 | SubtargetInfo->addrSinkUsingGEPs())) { | ||||
5364 | // By default, we use the GEP-based method when AA is used later. This | ||||
5365 | // prevents new inttoptr/ptrtoint pairs from degrading AA capabilities. | ||||
5366 | LLVM_DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrModedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " << *MemoryInst << "\n"; } } while (false) | ||||
5367 | << " for " << *MemoryInst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " << *MemoryInst << "\n"; } } while (false); | ||||
5368 | Value *ResultPtr = nullptr, *ResultIndex = nullptr; | ||||
5369 | |||||
5370 | // First, find the pointer. | ||||
5371 | if (AddrMode.BaseReg && AddrMode.BaseReg->getType()->isPointerTy()) { | ||||
5372 | ResultPtr = AddrMode.BaseReg; | ||||
5373 | AddrMode.BaseReg = nullptr; | ||||
5374 | } | ||||
5375 | |||||
5376 | if (AddrMode.Scale && AddrMode.ScaledReg->getType()->isPointerTy()) { | ||||
5377 | // We can't add more than one pointer together, nor can we scale a | ||||
5378 | // pointer (both of which seem meaningless). | ||||
5379 | if (ResultPtr || AddrMode.Scale != 1) | ||||
5380 | return Modified; | ||||
5381 | |||||
5382 | ResultPtr = AddrMode.ScaledReg; | ||||
5383 | AddrMode.Scale = 0; | ||||
5384 | } | ||||
5385 | |||||
5386 | // It is only safe to sign extend the BaseReg if we know that the math | ||||
5387 | // required to create it did not overflow before we extend it. Since | ||||
5388 | // the original IR value was tossed in favor of a constant back when | ||||
5389 | // the AddrMode was created we need to bail out gracefully if widths | ||||
5390 | // do not match instead of extending it. | ||||
5391 | // | ||||
5392 | // (See below for code to add the scale.) | ||||
5393 | if (AddrMode.Scale) { | ||||
5394 | Type *ScaledRegTy = AddrMode.ScaledReg->getType(); | ||||
5395 | if (cast<IntegerType>(IntPtrTy)->getBitWidth() > | ||||
5396 | cast<IntegerType>(ScaledRegTy)->getBitWidth()) | ||||
5397 | return Modified; | ||||
5398 | } | ||||
5399 | |||||
5400 | if (AddrMode.BaseGV) { | ||||
5401 | if (ResultPtr) | ||||
5402 | return Modified; | ||||
5403 | |||||
5404 | ResultPtr = AddrMode.BaseGV; | ||||
5405 | } | ||||
5406 | |||||
5407 | // If the real base value actually came from an inttoptr, then the matcher | ||||
5408 | // will look through it and provide only the integer value. In that case, | ||||
5409 | // use it here. | ||||
5410 | if (!DL->isNonIntegralPointerType(Addr->getType())) { | ||||
5411 | if (!ResultPtr && AddrMode.BaseReg) { | ||||
5412 | ResultPtr = Builder.CreateIntToPtr(AddrMode.BaseReg, Addr->getType(), | ||||
5413 | "sunkaddr"); | ||||
5414 | AddrMode.BaseReg = nullptr; | ||||
5415 | } else if (!ResultPtr && AddrMode.Scale == 1) { | ||||
5416 | ResultPtr = Builder.CreateIntToPtr(AddrMode.ScaledReg, Addr->getType(), | ||||
5417 | "sunkaddr"); | ||||
5418 | AddrMode.Scale = 0; | ||||
5419 | } | ||||
5420 | } | ||||
5421 | |||||
5422 | if (!ResultPtr && !AddrMode.BaseReg && !AddrMode.Scale && | ||||
5423 | !AddrMode.BaseOffs) { | ||||
5424 | SunkAddr = Constant::getNullValue(Addr->getType()); | ||||
5425 | } else if (!ResultPtr) { | ||||
5426 | return Modified; | ||||
5427 | } else { | ||||
5428 | Type *I8PtrTy = | ||||
5429 | Builder.getInt8PtrTy(Addr->getType()->getPointerAddressSpace()); | ||||
5430 | Type *I8Ty = Builder.getInt8Ty(); | ||||
5431 | |||||
5432 | // Start with the base register. Do this first so that subsequent address | ||||
5433 | // matching finds it last, which will prevent it from trying to match it | ||||
5434 | // as the scaled value in case it happens to be a mul. That would be | ||||
5435 | // problematic if we've sunk a different mul for the scale, because then | ||||
5436 | // we'd end up sinking both muls. | ||||
5437 | if (AddrMode.BaseReg) { | ||||
5438 | Value *V = AddrMode.BaseReg; | ||||
5439 | if (V->getType() != IntPtrTy) | ||||
5440 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); | ||||
5441 | |||||
5442 | ResultIndex = V; | ||||
5443 | } | ||||
5444 | |||||
5445 | // Add the scale value. | ||||
5446 | if (AddrMode.Scale) { | ||||
5447 | Value *V = AddrMode.ScaledReg; | ||||
5448 | if (V->getType() == IntPtrTy) { | ||||
5449 | // done. | ||||
5450 | } else { | ||||
5451 | assert(cast<IntegerType>(IntPtrTy)->getBitWidth() <(static_cast <bool> (cast<IntegerType>(IntPtrTy)-> getBitWidth() < cast<IntegerType>(V->getType())-> getBitWidth() && "We can't transform if ScaledReg is too narrow" ) ? void (0) : __assert_fail ("cast<IntegerType>(IntPtrTy)->getBitWidth() < cast<IntegerType>(V->getType())->getBitWidth() && \"We can't transform if ScaledReg is too narrow\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5453, __extension__ __PRETTY_FUNCTION__ )) | ||||
5452 | cast<IntegerType>(V->getType())->getBitWidth() &&(static_cast <bool> (cast<IntegerType>(IntPtrTy)-> getBitWidth() < cast<IntegerType>(V->getType())-> getBitWidth() && "We can't transform if ScaledReg is too narrow" ) ? void (0) : __assert_fail ("cast<IntegerType>(IntPtrTy)->getBitWidth() < cast<IntegerType>(V->getType())->getBitWidth() && \"We can't transform if ScaledReg is too narrow\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5453, __extension__ __PRETTY_FUNCTION__ )) | ||||
5453 | "We can't transform if ScaledReg is too narrow")(static_cast <bool> (cast<IntegerType>(IntPtrTy)-> getBitWidth() < cast<IntegerType>(V->getType())-> getBitWidth() && "We can't transform if ScaledReg is too narrow" ) ? void (0) : __assert_fail ("cast<IntegerType>(IntPtrTy)->getBitWidth() < cast<IntegerType>(V->getType())->getBitWidth() && \"We can't transform if ScaledReg is too narrow\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5453, __extension__ __PRETTY_FUNCTION__ )); | ||||
5454 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); | ||||
5455 | } | ||||
5456 | |||||
5457 | if (AddrMode.Scale != 1) | ||||
5458 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), | ||||
5459 | "sunkaddr"); | ||||
5460 | if (ResultIndex) | ||||
5461 | ResultIndex = Builder.CreateAdd(ResultIndex, V, "sunkaddr"); | ||||
5462 | else | ||||
5463 | ResultIndex = V; | ||||
5464 | } | ||||
5465 | |||||
5466 | // Add in the Base Offset if present. | ||||
5467 | if (AddrMode.BaseOffs) { | ||||
5468 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); | ||||
5469 | if (ResultIndex) { | ||||
5470 | // We need to add this separately from the scale above to help with | ||||
5471 | // SDAG consecutive load/store merging. | ||||
5472 | if (ResultPtr->getType() != I8PtrTy) | ||||
5473 | ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy); | ||||
5474 | ResultPtr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, | ||||
5475 | "sunkaddr", AddrMode.InBounds); | ||||
5476 | } | ||||
5477 | |||||
5478 | ResultIndex = V; | ||||
5479 | } | ||||
5480 | |||||
5481 | if (!ResultIndex) { | ||||
5482 | SunkAddr = ResultPtr; | ||||
5483 | } else { | ||||
5484 | if (ResultPtr->getType() != I8PtrTy) | ||||
5485 | ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy); | ||||
5486 | SunkAddr = Builder.CreateGEP(I8Ty, ResultPtr, ResultIndex, "sunkaddr", | ||||
5487 | AddrMode.InBounds); | ||||
5488 | } | ||||
5489 | |||||
5490 | if (SunkAddr->getType() != Addr->getType()) { | ||||
5491 | if (SunkAddr->getType()->getPointerAddressSpace() != | ||||
5492 | Addr->getType()->getPointerAddressSpace() && | ||||
5493 | !DL->isNonIntegralPointerType(Addr->getType())) { | ||||
5494 | // There are two reasons the address spaces might not match: a no-op | ||||
5495 | // addrspacecast, or a ptrtoint/inttoptr pair. Either way, we emit a | ||||
5496 | // ptrtoint/inttoptr pair to ensure we match the original semantics. | ||||
5497 | // TODO: allow bitcast between different address space pointers with | ||||
5498 | // the same size. | ||||
5499 | SunkAddr = Builder.CreatePtrToInt(SunkAddr, IntPtrTy, "sunkaddr"); | ||||
5500 | SunkAddr = | ||||
5501 | Builder.CreateIntToPtr(SunkAddr, Addr->getType(), "sunkaddr"); | ||||
5502 | } else | ||||
5503 | SunkAddr = Builder.CreatePointerCast(SunkAddr, Addr->getType()); | ||||
5504 | } | ||||
5505 | } | ||||
5506 | } else { | ||||
5507 | // We'd require a ptrtoint/inttoptr down the line, which we can't do for | ||||
5508 | // non-integral pointers, so in that case bail out now. | ||||
5509 | Type *BaseTy = AddrMode.BaseReg ? AddrMode.BaseReg->getType() : nullptr; | ||||
5510 | Type *ScaleTy = AddrMode.Scale ? AddrMode.ScaledReg->getType() : nullptr; | ||||
5511 | PointerType *BasePtrTy = dyn_cast_or_null<PointerType>(BaseTy); | ||||
5512 | PointerType *ScalePtrTy = dyn_cast_or_null<PointerType>(ScaleTy); | ||||
5513 | if (DL->isNonIntegralPointerType(Addr->getType()) || | ||||
5514 | (BasePtrTy && DL->isNonIntegralPointerType(BasePtrTy)) || | ||||
5515 | (ScalePtrTy && DL->isNonIntegralPointerType(ScalePtrTy)) || | ||||
5516 | (AddrMode.BaseGV && | ||||
5517 | DL->isNonIntegralPointerType(AddrMode.BaseGV->getType()))) | ||||
5518 | return Modified; | ||||
5519 | |||||
5520 | LLVM_DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrModedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " << *MemoryInst << "\n"; } } while (false) | ||||
5521 | << " for " << *MemoryInst << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for " << *MemoryInst << "\n"; } } while (false); | ||||
5522 | Type *IntPtrTy = DL->getIntPtrType(Addr->getType()); | ||||
5523 | Value *Result = nullptr; | ||||
5524 | |||||
5525 | // Start with the base register. Do this first so that subsequent address | ||||
5526 | // matching finds it last, which will prevent it from trying to match it | ||||
5527 | // as the scaled value in case it happens to be a mul. That would be | ||||
5528 | // problematic if we've sunk a different mul for the scale, because then | ||||
5529 | // we'd end up sinking both muls. | ||||
5530 | if (AddrMode.BaseReg) { | ||||
5531 | Value *V = AddrMode.BaseReg; | ||||
5532 | if (V->getType()->isPointerTy()) | ||||
5533 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); | ||||
5534 | if (V->getType() != IntPtrTy) | ||||
5535 | V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr"); | ||||
5536 | Result = V; | ||||
5537 | } | ||||
5538 | |||||
5539 | // Add the scale value. | ||||
5540 | if (AddrMode.Scale) { | ||||
5541 | Value *V = AddrMode.ScaledReg; | ||||
5542 | if (V->getType() == IntPtrTy) { | ||||
5543 | // done. | ||||
5544 | } else if (V->getType()->isPointerTy()) { | ||||
5545 | V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr"); | ||||
5546 | } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() < | ||||
5547 | cast<IntegerType>(V->getType())->getBitWidth()) { | ||||
5548 | V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr"); | ||||
5549 | } else { | ||||
5550 | // It is only safe to sign extend the BaseReg if we know that the math | ||||
5551 | // required to create it did not overflow before we extend it. Since | ||||
5552 | // the original IR value was tossed in favor of a constant back when | ||||
5553 | // the AddrMode was created we need to bail out gracefully if widths | ||||
5554 | // do not match instead of extending it. | ||||
5555 | Instruction *I = dyn_cast_or_null<Instruction>(Result); | ||||
5556 | if (I && (Result != AddrMode.BaseReg)) | ||||
5557 | I->eraseFromParent(); | ||||
5558 | return Modified; | ||||
5559 | } | ||||
5560 | if (AddrMode.Scale != 1) | ||||
5561 | V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale), | ||||
5562 | "sunkaddr"); | ||||
5563 | if (Result) | ||||
5564 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); | ||||
5565 | else | ||||
5566 | Result = V; | ||||
5567 | } | ||||
5568 | |||||
5569 | // Add in the BaseGV if present. | ||||
5570 | if (AddrMode.BaseGV) { | ||||
5571 | Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr"); | ||||
5572 | if (Result) | ||||
5573 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); | ||||
5574 | else | ||||
5575 | Result = V; | ||||
5576 | } | ||||
5577 | |||||
5578 | // Add in the Base Offset if present. | ||||
5579 | if (AddrMode.BaseOffs) { | ||||
5580 | Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs); | ||||
5581 | if (Result) | ||||
5582 | Result = Builder.CreateAdd(Result, V, "sunkaddr"); | ||||
5583 | else | ||||
5584 | Result = V; | ||||
5585 | } | ||||
5586 | |||||
5587 | if (!Result) | ||||
5588 | SunkAddr = Constant::getNullValue(Addr->getType()); | ||||
5589 | else | ||||
5590 | SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr"); | ||||
5591 | } | ||||
5592 | |||||
5593 | MemoryInst->replaceUsesOfWith(Repl, SunkAddr); | ||||
5594 | // Store the newly computed address into the cache. In the case we reused a | ||||
5595 | // value, this should be idempotent. | ||||
5596 | SunkAddrs[Addr] = WeakTrackingVH(SunkAddr); | ||||
5597 | |||||
5598 | // If we have no uses, recursively delete the value and all dead instructions | ||||
5599 | // using it. | ||||
5600 | if (Repl->use_empty()) { | ||||
5601 | resetIteratorIfInvalidatedWhileCalling(CurInstIterator->getParent(), [&]() { | ||||
5602 | RecursivelyDeleteTriviallyDeadInstructions( | ||||
5603 | Repl, TLInfo, nullptr, | ||||
5604 | [&](Value *V) { removeAllAssertingVHReferences(V); }); | ||||
5605 | }); | ||||
5606 | } | ||||
5607 | ++NumMemoryInsts; | ||||
5608 | return true; | ||||
5609 | } | ||||
5610 | |||||
5611 | /// Rewrite GEP input to gather/scatter to enable SelectionDAGBuilder to find | ||||
5612 | /// a uniform base to use for ISD::MGATHER/MSCATTER. SelectionDAGBuilder can | ||||
5613 | /// only handle a 2 operand GEP in the same basic block or a splat constant | ||||
5614 | /// vector. The 2 operands to the GEP must have a scalar pointer and a vector | ||||
5615 | /// index. | ||||
5616 | /// | ||||
5617 | /// If the existing GEP has a vector base pointer that is splat, we can look | ||||
5618 | /// through the splat to find the scalar pointer. If we can't find a scalar | ||||
5619 | /// pointer there's nothing we can do. | ||||
5620 | /// | ||||
5621 | /// If we have a GEP with more than 2 indices where the middle indices are all | ||||
5622 | /// zeroes, we can replace it with 2 GEPs where the second has 2 operands. | ||||
5623 | /// | ||||
5624 | /// If the final index isn't a vector or is a splat, we can emit a scalar GEP | ||||
5625 | /// followed by a GEP with an all zeroes vector index. This will enable | ||||
5626 | /// SelectionDAGBuilder to use the scalar GEP as the uniform base and have a | ||||
5627 | /// zero index. | ||||
5628 | bool CodeGenPrepare::optimizeGatherScatterInst(Instruction *MemoryInst, | ||||
5629 | Value *Ptr) { | ||||
5630 | Value *NewAddr; | ||||
5631 | |||||
5632 | if (const auto *GEP = dyn_cast<GetElementPtrInst>(Ptr)) { | ||||
5633 | // Don't optimize GEPs that don't have indices. | ||||
5634 | if (!GEP->hasIndices()) | ||||
5635 | return false; | ||||
5636 | |||||
5637 | // If the GEP and the gather/scatter aren't in the same BB, don't optimize. | ||||
5638 | // FIXME: We should support this by sinking the GEP. | ||||
5639 | if (MemoryInst->getParent() != GEP->getParent()) | ||||
5640 | return false; | ||||
5641 | |||||
5642 | SmallVector<Value *, 2> Ops(GEP->operands()); | ||||
5643 | |||||
5644 | bool RewriteGEP = false; | ||||
5645 | |||||
5646 | if (Ops[0]->getType()->isVectorTy()) { | ||||
5647 | Ops[0] = getSplatValue(Ops[0]); | ||||
5648 | if (!Ops[0]) | ||||
5649 | return false; | ||||
5650 | RewriteGEP = true; | ||||
5651 | } | ||||
5652 | |||||
5653 | unsigned FinalIndex = Ops.size() - 1; | ||||
5654 | |||||
5655 | // Ensure all but the last index is 0. | ||||
5656 | // FIXME: This isn't strictly required. All that's required is that they are | ||||
5657 | // all scalars or splats. | ||||
5658 | for (unsigned i = 1; i < FinalIndex; ++i) { | ||||
5659 | auto *C = dyn_cast<Constant>(Ops[i]); | ||||
5660 | if (!C) | ||||
5661 | return false; | ||||
5662 | if (isa<VectorType>(C->getType())) | ||||
5663 | C = C->getSplatValue(); | ||||
5664 | auto *CI = dyn_cast_or_null<ConstantInt>(C); | ||||
5665 | if (!CI || !CI->isZero()) | ||||
5666 | return false; | ||||
5667 | // Scalarize the index if needed. | ||||
5668 | Ops[i] = CI; | ||||
5669 | } | ||||
5670 | |||||
5671 | // Try to scalarize the final index. | ||||
5672 | if (Ops[FinalIndex]->getType()->isVectorTy()) { | ||||
5673 | if (Value *V = getSplatValue(Ops[FinalIndex])) { | ||||
5674 | auto *C = dyn_cast<ConstantInt>(V); | ||||
5675 | // Don't scalarize all zeros vector. | ||||
5676 | if (!C || !C->isZero()) { | ||||
5677 | Ops[FinalIndex] = V; | ||||
5678 | RewriteGEP = true; | ||||
5679 | } | ||||
5680 | } | ||||
5681 | } | ||||
5682 | |||||
5683 | // If we made any changes or the we have extra operands, we need to generate | ||||
5684 | // new instructions. | ||||
5685 | if (!RewriteGEP && Ops.size() == 2) | ||||
5686 | return false; | ||||
5687 | |||||
5688 | auto NumElts = cast<VectorType>(Ptr->getType())->getElementCount(); | ||||
5689 | |||||
5690 | IRBuilder<> Builder(MemoryInst); | ||||
5691 | |||||
5692 | Type *SourceTy = GEP->getSourceElementType(); | ||||
5693 | Type *ScalarIndexTy = DL->getIndexType(Ops[0]->getType()->getScalarType()); | ||||
5694 | |||||
5695 | // If the final index isn't a vector, emit a scalar GEP containing all ops | ||||
5696 | // and a vector GEP with all zeroes final index. | ||||
5697 | if (!Ops[FinalIndex]->getType()->isVectorTy()) { | ||||
5698 | NewAddr = Builder.CreateGEP(SourceTy, Ops[0], ArrayRef(Ops).drop_front()); | ||||
5699 | auto *IndexTy = VectorType::get(ScalarIndexTy, NumElts); | ||||
5700 | auto *SecondTy = GetElementPtrInst::getIndexedType( | ||||
5701 | SourceTy, ArrayRef(Ops).drop_front()); | ||||
5702 | NewAddr = | ||||
5703 | Builder.CreateGEP(SecondTy, NewAddr, Constant::getNullValue(IndexTy)); | ||||
5704 | } else { | ||||
5705 | Value *Base = Ops[0]; | ||||
5706 | Value *Index = Ops[FinalIndex]; | ||||
5707 | |||||
5708 | // Create a scalar GEP if there are more than 2 operands. | ||||
5709 | if (Ops.size() != 2) { | ||||
5710 | // Replace the last index with 0. | ||||
5711 | Ops[FinalIndex] = Constant::getNullValue(ScalarIndexTy); | ||||
5712 | Base = Builder.CreateGEP(SourceTy, Base, ArrayRef(Ops).drop_front()); | ||||
5713 | SourceTy = GetElementPtrInst::getIndexedType( | ||||
5714 | SourceTy, ArrayRef(Ops).drop_front()); | ||||
5715 | } | ||||
5716 | |||||
5717 | // Now create the GEP with scalar pointer and vector index. | ||||
5718 | NewAddr = Builder.CreateGEP(SourceTy, Base, Index); | ||||
5719 | } | ||||
5720 | } else if (!isa<Constant>(Ptr)) { | ||||
5721 | // Not a GEP, maybe its a splat and we can create a GEP to enable | ||||
5722 | // SelectionDAGBuilder to use it as a uniform base. | ||||
5723 | Value *V = getSplatValue(Ptr); | ||||
5724 | if (!V) | ||||
5725 | return false; | ||||
5726 | |||||
5727 | auto NumElts = cast<VectorType>(Ptr->getType())->getElementCount(); | ||||
5728 | |||||
5729 | IRBuilder<> Builder(MemoryInst); | ||||
5730 | |||||
5731 | // Emit a vector GEP with a scalar pointer and all 0s vector index. | ||||
5732 | Type *ScalarIndexTy = DL->getIndexType(V->getType()->getScalarType()); | ||||
5733 | auto *IndexTy = VectorType::get(ScalarIndexTy, NumElts); | ||||
5734 | Type *ScalarTy; | ||||
5735 | if (cast<IntrinsicInst>(MemoryInst)->getIntrinsicID() == | ||||
5736 | Intrinsic::masked_gather) { | ||||
5737 | ScalarTy = MemoryInst->getType()->getScalarType(); | ||||
5738 | } else { | ||||
5739 | assert(cast<IntrinsicInst>(MemoryInst)->getIntrinsicID() ==(static_cast <bool> (cast<IntrinsicInst>(MemoryInst )->getIntrinsicID() == Intrinsic::masked_scatter) ? void ( 0) : __assert_fail ("cast<IntrinsicInst>(MemoryInst)->getIntrinsicID() == Intrinsic::masked_scatter" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5740, __extension__ __PRETTY_FUNCTION__ )) | ||||
5740 | Intrinsic::masked_scatter)(static_cast <bool> (cast<IntrinsicInst>(MemoryInst )->getIntrinsicID() == Intrinsic::masked_scatter) ? void ( 0) : __assert_fail ("cast<IntrinsicInst>(MemoryInst)->getIntrinsicID() == Intrinsic::masked_scatter" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5740, __extension__ __PRETTY_FUNCTION__ )); | ||||
5741 | ScalarTy = MemoryInst->getOperand(0)->getType()->getScalarType(); | ||||
5742 | } | ||||
5743 | NewAddr = Builder.CreateGEP(ScalarTy, V, Constant::getNullValue(IndexTy)); | ||||
5744 | } else { | ||||
5745 | // Constant, SelectionDAGBuilder knows to check if its a splat. | ||||
5746 | return false; | ||||
5747 | } | ||||
5748 | |||||
5749 | MemoryInst->replaceUsesOfWith(Ptr, NewAddr); | ||||
5750 | |||||
5751 | // If we have no uses, recursively delete the value and all dead instructions | ||||
5752 | // using it. | ||||
5753 | if (Ptr->use_empty()) | ||||
5754 | RecursivelyDeleteTriviallyDeadInstructions( | ||||
5755 | Ptr, TLInfo, nullptr, | ||||
5756 | [&](Value *V) { removeAllAssertingVHReferences(V); }); | ||||
5757 | |||||
5758 | return true; | ||||
5759 | } | ||||
5760 | |||||
5761 | /// If there are any memory operands, use OptimizeMemoryInst to sink their | ||||
5762 | /// address computing into the block when possible / profitable. | ||||
5763 | bool CodeGenPrepare::optimizeInlineAsmInst(CallInst *CS) { | ||||
5764 | bool MadeChange = false; | ||||
5765 | |||||
5766 | const TargetRegisterInfo *TRI = | ||||
5767 | TM->getSubtargetImpl(*CS->getFunction())->getRegisterInfo(); | ||||
5768 | TargetLowering::AsmOperandInfoVector TargetConstraints = | ||||
5769 | TLI->ParseConstraints(*DL, TRI, *CS); | ||||
5770 | unsigned ArgNo = 0; | ||||
5771 | for (TargetLowering::AsmOperandInfo &OpInfo : TargetConstraints) { | ||||
5772 | // Compute the constraint code and ConstraintType to use. | ||||
5773 | TLI->ComputeConstraintToUse(OpInfo, SDValue()); | ||||
5774 | |||||
5775 | // TODO: Also handle C_Address? | ||||
5776 | if (OpInfo.ConstraintType == TargetLowering::C_Memory && | ||||
5777 | OpInfo.isIndirect) { | ||||
5778 | Value *OpVal = CS->getArgOperand(ArgNo++); | ||||
5779 | MadeChange |= optimizeMemoryInst(CS, OpVal, OpVal->getType(), ~0u); | ||||
5780 | } else if (OpInfo.Type == InlineAsm::isInput) | ||||
5781 | ArgNo++; | ||||
5782 | } | ||||
5783 | |||||
5784 | return MadeChange; | ||||
5785 | } | ||||
5786 | |||||
5787 | /// Check if all the uses of \p Val are equivalent (or free) zero or | ||||
5788 | /// sign extensions. | ||||
5789 | static bool hasSameExtUse(Value *Val, const TargetLowering &TLI) { | ||||
5790 | assert(!Val->use_empty() && "Input must have at least one use")(static_cast <bool> (!Val->use_empty() && "Input must have at least one use" ) ? void (0) : __assert_fail ("!Val->use_empty() && \"Input must have at least one use\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5790, __extension__ __PRETTY_FUNCTION__ )); | ||||
5791 | const Instruction *FirstUser = cast<Instruction>(*Val->user_begin()); | ||||
5792 | bool IsSExt = isa<SExtInst>(FirstUser); | ||||
5793 | Type *ExtTy = FirstUser->getType(); | ||||
5794 | for (const User *U : Val->users()) { | ||||
5795 | const Instruction *UI = cast<Instruction>(U); | ||||
5796 | if ((IsSExt && !isa<SExtInst>(UI)) || (!IsSExt && !isa<ZExtInst>(UI))) | ||||
5797 | return false; | ||||
5798 | Type *CurTy = UI->getType(); | ||||
5799 | // Same input and output types: Same instruction after CSE. | ||||
5800 | if (CurTy == ExtTy) | ||||
5801 | continue; | ||||
5802 | |||||
5803 | // If IsSExt is true, we are in this situation: | ||||
5804 | // a = Val | ||||
5805 | // b = sext ty1 a to ty2 | ||||
5806 | // c = sext ty1 a to ty3 | ||||
5807 | // Assuming ty2 is shorter than ty3, this could be turned into: | ||||
5808 | // a = Val | ||||
5809 | // b = sext ty1 a to ty2 | ||||
5810 | // c = sext ty2 b to ty3 | ||||
5811 | // However, the last sext is not free. | ||||
5812 | if (IsSExt) | ||||
5813 | return false; | ||||
5814 | |||||
5815 | // This is a ZExt, maybe this is free to extend from one type to another. | ||||
5816 | // In that case, we would not account for a different use. | ||||
5817 | Type *NarrowTy; | ||||
5818 | Type *LargeTy; | ||||
5819 | if (ExtTy->getScalarType()->getIntegerBitWidth() > | ||||
5820 | CurTy->getScalarType()->getIntegerBitWidth()) { | ||||
5821 | NarrowTy = CurTy; | ||||
5822 | LargeTy = ExtTy; | ||||
5823 | } else { | ||||
5824 | NarrowTy = ExtTy; | ||||
5825 | LargeTy = CurTy; | ||||
5826 | } | ||||
5827 | |||||
5828 | if (!TLI.isZExtFree(NarrowTy, LargeTy)) | ||||
5829 | return false; | ||||
5830 | } | ||||
5831 | // All uses are the same or can be derived from one another for free. | ||||
5832 | return true; | ||||
5833 | } | ||||
5834 | |||||
5835 | /// Try to speculatively promote extensions in \p Exts and continue | ||||
5836 | /// promoting through newly promoted operands recursively as far as doing so is | ||||
5837 | /// profitable. Save extensions profitably moved up, in \p ProfitablyMovedExts. | ||||
5838 | /// When some promotion happened, \p TPT contains the proper state to revert | ||||
5839 | /// them. | ||||
5840 | /// | ||||
5841 | /// \return true if some promotion happened, false otherwise. | ||||
5842 | bool CodeGenPrepare::tryToPromoteExts( | ||||
5843 | TypePromotionTransaction &TPT, const SmallVectorImpl<Instruction *> &Exts, | ||||
5844 | SmallVectorImpl<Instruction *> &ProfitablyMovedExts, | ||||
5845 | unsigned CreatedInstsCost) { | ||||
5846 | bool Promoted = false; | ||||
5847 | |||||
5848 | // Iterate over all the extensions to try to promote them. | ||||
5849 | for (auto *I : Exts) { | ||||
5850 | // Early check if we directly have ext(load). | ||||
5851 | if (isa<LoadInst>(I->getOperand(0))) { | ||||
5852 | ProfitablyMovedExts.push_back(I); | ||||
5853 | continue; | ||||
5854 | } | ||||
5855 | |||||
5856 | // Check whether or not we want to do any promotion. The reason we have | ||||
5857 | // this check inside the for loop is to catch the case where an extension | ||||
5858 | // is directly fed by a load because in such case the extension can be moved | ||||
5859 | // up without any promotion on its operands. | ||||
5860 | if (!TLI->enableExtLdPromotion() || DisableExtLdPromotion) | ||||
5861 | return false; | ||||
5862 | |||||
5863 | // Get the action to perform the promotion. | ||||
5864 | TypePromotionHelper::Action TPH = | ||||
5865 | TypePromotionHelper::getAction(I, InsertedInsts, *TLI, PromotedInsts); | ||||
5866 | // Check if we can promote. | ||||
5867 | if (!TPH) { | ||||
5868 | // Save the current extension as we cannot move up through its operand. | ||||
5869 | ProfitablyMovedExts.push_back(I); | ||||
5870 | continue; | ||||
5871 | } | ||||
5872 | |||||
5873 | // Save the current state. | ||||
5874 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = | ||||
5875 | TPT.getRestorationPoint(); | ||||
5876 | SmallVector<Instruction *, 4> NewExts; | ||||
5877 | unsigned NewCreatedInstsCost = 0; | ||||
5878 | unsigned ExtCost = !TLI->isExtFree(I); | ||||
5879 | // Promote. | ||||
5880 | Value *PromotedVal = TPH(I, TPT, PromotedInsts, NewCreatedInstsCost, | ||||
5881 | &NewExts, nullptr, *TLI); | ||||
5882 | assert(PromotedVal &&(static_cast <bool> (PromotedVal && "TypePromotionHelper should have filtered out those cases" ) ? void (0) : __assert_fail ("PromotedVal && \"TypePromotionHelper should have filtered out those cases\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5883, __extension__ __PRETTY_FUNCTION__ )) | ||||
5883 | "TypePromotionHelper should have filtered out those cases")(static_cast <bool> (PromotedVal && "TypePromotionHelper should have filtered out those cases" ) ? void (0) : __assert_fail ("PromotedVal && \"TypePromotionHelper should have filtered out those cases\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 5883, __extension__ __PRETTY_FUNCTION__ )); | ||||
5884 | |||||
5885 | // We would be able to merge only one extension in a load. | ||||
5886 | // Therefore, if we have more than 1 new extension we heuristically | ||||
5887 | // cut this search path, because it means we degrade the code quality. | ||||
5888 | // With exactly 2, the transformation is neutral, because we will merge | ||||
5889 | // one extension but leave one. However, we optimistically keep going, | ||||
5890 | // because the new extension may be removed too. | ||||
5891 | long long TotalCreatedInstsCost = CreatedInstsCost + NewCreatedInstsCost; | ||||
5892 | // FIXME: It would be possible to propagate a negative value instead of | ||||
5893 | // conservatively ceiling it to 0. | ||||
5894 | TotalCreatedInstsCost = | ||||
5895 | std::max((long long)0, (TotalCreatedInstsCost - ExtCost)); | ||||
5896 | if (!StressExtLdPromotion && | ||||
5897 | (TotalCreatedInstsCost > 1 || | ||||
5898 | !isPromotedInstructionLegal(*TLI, *DL, PromotedVal))) { | ||||
5899 | // This promotion is not profitable, rollback to the previous state, and | ||||
5900 | // save the current extension in ProfitablyMovedExts as the latest | ||||
5901 | // speculative promotion turned out to be unprofitable. | ||||
5902 | TPT.rollback(LastKnownGood); | ||||
5903 | ProfitablyMovedExts.push_back(I); | ||||
5904 | continue; | ||||
5905 | } | ||||
5906 | // Continue promoting NewExts as far as doing so is profitable. | ||||
5907 | SmallVector<Instruction *, 2> NewlyMovedExts; | ||||
5908 | (void)tryToPromoteExts(TPT, NewExts, NewlyMovedExts, TotalCreatedInstsCost); | ||||
5909 | bool NewPromoted = false; | ||||
5910 | for (auto *ExtInst : NewlyMovedExts) { | ||||
5911 | Instruction *MovedExt = cast<Instruction>(ExtInst); | ||||
5912 | Value *ExtOperand = MovedExt->getOperand(0); | ||||
5913 | // If we have reached to a load, we need this extra profitability check | ||||
5914 | // as it could potentially be merged into an ext(load). | ||||
5915 | if (isa<LoadInst>(ExtOperand) && | ||||
5916 | !(StressExtLdPromotion || NewCreatedInstsCost <= ExtCost || | ||||
5917 | (ExtOperand->hasOneUse() || hasSameExtUse(ExtOperand, *TLI)))) | ||||
5918 | continue; | ||||
5919 | |||||
5920 | ProfitablyMovedExts.push_back(MovedExt); | ||||
5921 | NewPromoted = true; | ||||
5922 | } | ||||
5923 | |||||
5924 | // If none of speculative promotions for NewExts is profitable, rollback | ||||
5925 | // and save the current extension (I) as the last profitable extension. | ||||
5926 | if (!NewPromoted) { | ||||
5927 | TPT.rollback(LastKnownGood); | ||||
5928 | ProfitablyMovedExts.push_back(I); | ||||
5929 | continue; | ||||
5930 | } | ||||
5931 | // The promotion is profitable. | ||||
5932 | Promoted = true; | ||||
5933 | } | ||||
5934 | return Promoted; | ||||
5935 | } | ||||
5936 | |||||
5937 | /// Merging redundant sexts when one is dominating the other. | ||||
5938 | bool CodeGenPrepare::mergeSExts(Function &F) { | ||||
5939 | bool Changed = false; | ||||
5940 | for (auto &Entry : ValToSExtendedUses) { | ||||
5941 | SExts &Insts = Entry.second; | ||||
5942 | SExts CurPts; | ||||
5943 | for (Instruction *Inst : Insts) { | ||||
5944 | if (RemovedInsts.count(Inst) || !isa<SExtInst>(Inst) || | ||||
5945 | Inst->getOperand(0) != Entry.first) | ||||
5946 | continue; | ||||
5947 | bool inserted = false; | ||||
5948 | for (auto &Pt : CurPts) { | ||||
5949 | if (getDT(F).dominates(Inst, Pt)) { | ||||
5950 | replaceAllUsesWith(Pt, Inst, FreshBBs, IsHugeFunc); | ||||
5951 | RemovedInsts.insert(Pt); | ||||
5952 | Pt->removeFromParent(); | ||||
5953 | Pt = Inst; | ||||
5954 | inserted = true; | ||||
5955 | Changed = true; | ||||
5956 | break; | ||||
5957 | } | ||||
5958 | if (!getDT(F).dominates(Pt, Inst)) | ||||
5959 | // Give up if we need to merge in a common dominator as the | ||||
5960 | // experiments show it is not profitable. | ||||
5961 | continue; | ||||
5962 | replaceAllUsesWith(Inst, Pt, FreshBBs, IsHugeFunc); | ||||
5963 | RemovedInsts.insert(Inst); | ||||
5964 | Inst->removeFromParent(); | ||||
5965 | inserted = true; | ||||
5966 | Changed = true; | ||||
5967 | break; | ||||
5968 | } | ||||
5969 | if (!inserted) | ||||
5970 | CurPts.push_back(Inst); | ||||
5971 | } | ||||
5972 | } | ||||
5973 | return Changed; | ||||
5974 | } | ||||
5975 | |||||
5976 | // Splitting large data structures so that the GEPs accessing them can have | ||||
5977 | // smaller offsets so that they can be sunk to the same blocks as their users. | ||||
5978 | // For example, a large struct starting from %base is split into two parts | ||||
5979 | // where the second part starts from %new_base. | ||||
5980 | // | ||||
5981 | // Before: | ||||
5982 | // BB0: | ||||
5983 | // %base = | ||||
5984 | // | ||||
5985 | // BB1: | ||||
5986 | // %gep0 = gep %base, off0 | ||||
5987 | // %gep1 = gep %base, off1 | ||||
5988 | // %gep2 = gep %base, off2 | ||||
5989 | // | ||||
5990 | // BB2: | ||||
5991 | // %load1 = load %gep0 | ||||
5992 | // %load2 = load %gep1 | ||||
5993 | // %load3 = load %gep2 | ||||
5994 | // | ||||
5995 | // After: | ||||
5996 | // BB0: | ||||
5997 | // %base = | ||||
5998 | // %new_base = gep %base, off0 | ||||
5999 | // | ||||
6000 | // BB1: | ||||
6001 | // %new_gep0 = %new_base | ||||
6002 | // %new_gep1 = gep %new_base, off1 - off0 | ||||
6003 | // %new_gep2 = gep %new_base, off2 - off0 | ||||
6004 | // | ||||
6005 | // BB2: | ||||
6006 | // %load1 = load i32, i32* %new_gep0 | ||||
6007 | // %load2 = load i32, i32* %new_gep1 | ||||
6008 | // %load3 = load i32, i32* %new_gep2 | ||||
6009 | // | ||||
6010 | // %new_gep1 and %new_gep2 can be sunk to BB2 now after the splitting because | ||||
6011 | // their offsets are smaller enough to fit into the addressing mode. | ||||
6012 | bool CodeGenPrepare::splitLargeGEPOffsets() { | ||||
6013 | bool Changed = false; | ||||
6014 | for (auto &Entry : LargeOffsetGEPMap) { | ||||
6015 | Value *OldBase = Entry.first; | ||||
6016 | SmallVectorImpl<std::pair<AssertingVH<GetElementPtrInst>, int64_t>> | ||||
6017 | &LargeOffsetGEPs = Entry.second; | ||||
6018 | auto compareGEPOffset = | ||||
6019 | [&](const std::pair<GetElementPtrInst *, int64_t> &LHS, | ||||
6020 | const std::pair<GetElementPtrInst *, int64_t> &RHS) { | ||||
6021 | if (LHS.first == RHS.first) | ||||
6022 | return false; | ||||
6023 | if (LHS.second != RHS.second) | ||||
6024 | return LHS.second < RHS.second; | ||||
6025 | return LargeOffsetGEPID[LHS.first] < LargeOffsetGEPID[RHS.first]; | ||||
6026 | }; | ||||
6027 | // Sorting all the GEPs of the same data structures based on the offsets. | ||||
6028 | llvm::sort(LargeOffsetGEPs, compareGEPOffset); | ||||
6029 | LargeOffsetGEPs.erase( | ||||
6030 | std::unique(LargeOffsetGEPs.begin(), LargeOffsetGEPs.end()), | ||||
6031 | LargeOffsetGEPs.end()); | ||||
6032 | // Skip if all the GEPs have the same offsets. | ||||
6033 | if (LargeOffsetGEPs.front().second == LargeOffsetGEPs.back().second) | ||||
6034 | continue; | ||||
6035 | GetElementPtrInst *BaseGEP = LargeOffsetGEPs.begin()->first; | ||||
6036 | int64_t BaseOffset = LargeOffsetGEPs.begin()->second; | ||||
6037 | Value *NewBaseGEP = nullptr; | ||||
6038 | |||||
6039 | auto *LargeOffsetGEP = LargeOffsetGEPs.begin(); | ||||
6040 | while (LargeOffsetGEP != LargeOffsetGEPs.end()) { | ||||
6041 | GetElementPtrInst *GEP = LargeOffsetGEP->first; | ||||
6042 | int64_t Offset = LargeOffsetGEP->second; | ||||
6043 | if (Offset != BaseOffset) { | ||||
6044 | TargetLowering::AddrMode AddrMode; | ||||
6045 | AddrMode.HasBaseReg = true; | ||||
6046 | AddrMode.BaseOffs = Offset - BaseOffset; | ||||
6047 | // The result type of the GEP might not be the type of the memory | ||||
6048 | // access. | ||||
6049 | if (!TLI->isLegalAddressingMode(*DL, AddrMode, | ||||
6050 | GEP->getResultElementType(), | ||||
6051 | GEP->getAddressSpace())) { | ||||
6052 | // We need to create a new base if the offset to the current base is | ||||
6053 | // too large to fit into the addressing mode. So, a very large struct | ||||
6054 | // may be split into several parts. | ||||
6055 | BaseGEP = GEP; | ||||
6056 | BaseOffset = Offset; | ||||
6057 | NewBaseGEP = nullptr; | ||||
6058 | } | ||||
6059 | } | ||||
6060 | |||||
6061 | // Generate a new GEP to replace the current one. | ||||
6062 | LLVMContext &Ctx = GEP->getContext(); | ||||
6063 | Type *IntPtrTy = DL->getIntPtrType(GEP->getType()); | ||||
6064 | Type *I8PtrTy = | ||||
6065 | Type::getInt8PtrTy(Ctx, GEP->getType()->getPointerAddressSpace()); | ||||
6066 | Type *I8Ty = Type::getInt8Ty(Ctx); | ||||
6067 | |||||
6068 | if (!NewBaseGEP) { | ||||
6069 | // Create a new base if we don't have one yet. Find the insertion | ||||
6070 | // pointer for the new base first. | ||||
6071 | BasicBlock::iterator NewBaseInsertPt; | ||||
6072 | BasicBlock *NewBaseInsertBB; | ||||
6073 | if (auto *BaseI = dyn_cast<Instruction>(OldBase)) { | ||||
6074 | // If the base of the struct is an instruction, the new base will be | ||||
6075 | // inserted close to it. | ||||
6076 | NewBaseInsertBB = BaseI->getParent(); | ||||
6077 | if (isa<PHINode>(BaseI)) | ||||
6078 | NewBaseInsertPt = NewBaseInsertBB->getFirstInsertionPt(); | ||||
6079 | else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(BaseI)) { | ||||
6080 | NewBaseInsertBB = | ||||
6081 | SplitEdge(NewBaseInsertBB, Invoke->getNormalDest()); | ||||
6082 | NewBaseInsertPt = NewBaseInsertBB->getFirstInsertionPt(); | ||||
6083 | } else | ||||
6084 | NewBaseInsertPt = std::next(BaseI->getIterator()); | ||||
6085 | } else { | ||||
6086 | // If the current base is an argument or global value, the new base | ||||
6087 | // will be inserted to the entry block. | ||||
6088 | NewBaseInsertBB = &BaseGEP->getFunction()->getEntryBlock(); | ||||
6089 | NewBaseInsertPt = NewBaseInsertBB->getFirstInsertionPt(); | ||||
6090 | } | ||||
6091 | IRBuilder<> NewBaseBuilder(NewBaseInsertBB, NewBaseInsertPt); | ||||
6092 | // Create a new base. | ||||
6093 | Value *BaseIndex = ConstantInt::get(IntPtrTy, BaseOffset); | ||||
6094 | NewBaseGEP = OldBase; | ||||
6095 | if (NewBaseGEP->getType() != I8PtrTy) | ||||
6096 | NewBaseGEP = NewBaseBuilder.CreatePointerCast(NewBaseGEP, I8PtrTy); | ||||
6097 | NewBaseGEP = | ||||
6098 | NewBaseBuilder.CreateGEP(I8Ty, NewBaseGEP, BaseIndex, "splitgep"); | ||||
6099 | NewGEPBases.insert(NewBaseGEP); | ||||
6100 | } | ||||
6101 | |||||
6102 | IRBuilder<> Builder(GEP); | ||||
6103 | Value *NewGEP = NewBaseGEP; | ||||
6104 | if (Offset == BaseOffset) { | ||||
6105 | if (GEP->getType() != I8PtrTy) | ||||
6106 | NewGEP = Builder.CreatePointerCast(NewGEP, GEP->getType()); | ||||
6107 | } else { | ||||
6108 | // Calculate the new offset for the new GEP. | ||||
6109 | Value *Index = ConstantInt::get(IntPtrTy, Offset - BaseOffset); | ||||
6110 | NewGEP = Builder.CreateGEP(I8Ty, NewBaseGEP, Index); | ||||
6111 | |||||
6112 | if (GEP->getType() != I8PtrTy) | ||||
6113 | NewGEP = Builder.CreatePointerCast(NewGEP, GEP->getType()); | ||||
6114 | } | ||||
6115 | replaceAllUsesWith(GEP, NewGEP, FreshBBs, IsHugeFunc); | ||||
6116 | LargeOffsetGEPID.erase(GEP); | ||||
6117 | LargeOffsetGEP = LargeOffsetGEPs.erase(LargeOffsetGEP); | ||||
6118 | GEP->eraseFromParent(); | ||||
6119 | Changed = true; | ||||
6120 | } | ||||
6121 | } | ||||
6122 | return Changed; | ||||
6123 | } | ||||
6124 | |||||
6125 | bool CodeGenPrepare::optimizePhiType( | ||||
6126 | PHINode *I, SmallPtrSetImpl<PHINode *> &Visited, | ||||
6127 | SmallPtrSetImpl<Instruction *> &DeletedInstrs) { | ||||
6128 | // We are looking for a collection on interconnected phi nodes that together | ||||
6129 | // only use loads/bitcasts and are used by stores/bitcasts, and the bitcasts | ||||
6130 | // are of the same type. Convert the whole set of nodes to the type of the | ||||
6131 | // bitcast. | ||||
6132 | Type *PhiTy = I->getType(); | ||||
6133 | Type *ConvertTy = nullptr; | ||||
6134 | if (Visited.count(I) || | ||||
6135 | (!I->getType()->isIntegerTy() && !I->getType()->isFloatingPointTy())) | ||||
6136 | return false; | ||||
6137 | |||||
6138 | SmallVector<Instruction *, 4> Worklist; | ||||
6139 | Worklist.push_back(cast<Instruction>(I)); | ||||
6140 | SmallPtrSet<PHINode *, 4> PhiNodes; | ||||
6141 | SmallPtrSet<ConstantData *, 4> Constants; | ||||
6142 | PhiNodes.insert(I); | ||||
6143 | Visited.insert(I); | ||||
6144 | SmallPtrSet<Instruction *, 4> Defs; | ||||
6145 | SmallPtrSet<Instruction *, 4> Uses; | ||||
6146 | // This works by adding extra bitcasts between load/stores and removing | ||||
6147 | // existing bicasts. If we have a phi(bitcast(load)) or a store(bitcast(phi)) | ||||
6148 | // we can get in the situation where we remove a bitcast in one iteration | ||||
6149 | // just to add it again in the next. We need to ensure that at least one | ||||
6150 | // bitcast we remove are anchored to something that will not change back. | ||||
6151 | bool AnyAnchored = false; | ||||
6152 | |||||
6153 | while (!Worklist.empty()) { | ||||
6154 | Instruction *II = Worklist.pop_back_val(); | ||||
6155 | |||||
6156 | if (auto *Phi = dyn_cast<PHINode>(II)) { | ||||
6157 | // Handle Defs, which might also be PHI's | ||||
6158 | for (Value *V : Phi->incoming_values()) { | ||||
6159 | if (auto *OpPhi = dyn_cast<PHINode>(V)) { | ||||
6160 | if (!PhiNodes.count(OpPhi)) { | ||||
6161 | if (!Visited.insert(OpPhi).second) | ||||
6162 | return false; | ||||
6163 | PhiNodes.insert(OpPhi); | ||||
6164 | Worklist.push_back(OpPhi); | ||||
6165 | } | ||||
6166 | } else if (auto *OpLoad = dyn_cast<LoadInst>(V)) { | ||||
6167 | if (!OpLoad->isSimple()) | ||||
6168 | return false; | ||||
6169 | if (Defs.insert(OpLoad).second) | ||||
6170 | Worklist.push_back(OpLoad); | ||||
6171 | } else if (auto *OpEx = dyn_cast<ExtractElementInst>(V)) { | ||||
6172 | if (Defs.insert(OpEx).second) | ||||
6173 | Worklist.push_back(OpEx); | ||||
6174 | } else if (auto *OpBC = dyn_cast<BitCastInst>(V)) { | ||||
6175 | if (!ConvertTy) | ||||
6176 | ConvertTy = OpBC->getOperand(0)->getType(); | ||||
6177 | if (OpBC->getOperand(0)->getType() != ConvertTy) | ||||
6178 | return false; | ||||
6179 | if (Defs.insert(OpBC).second) { | ||||
6180 | Worklist.push_back(OpBC); | ||||
6181 | AnyAnchored |= !isa<LoadInst>(OpBC->getOperand(0)) && | ||||
6182 | !isa<ExtractElementInst>(OpBC->getOperand(0)); | ||||
6183 | } | ||||
6184 | } else if (auto *OpC = dyn_cast<ConstantData>(V)) | ||||
6185 | Constants.insert(OpC); | ||||
6186 | else | ||||
6187 | return false; | ||||
6188 | } | ||||
6189 | } | ||||
6190 | |||||
6191 | // Handle uses which might also be phi's | ||||
6192 | for (User *V : II->users()) { | ||||
6193 | if (auto *OpPhi = dyn_cast<PHINode>(V)) { | ||||
6194 | if (!PhiNodes.count(OpPhi)) { | ||||
6195 | if (Visited.count(OpPhi)) | ||||
6196 | return false; | ||||
6197 | PhiNodes.insert(OpPhi); | ||||
6198 | Visited.insert(OpPhi); | ||||
6199 | Worklist.push_back(OpPhi); | ||||
6200 | } | ||||
6201 | } else if (auto *OpStore = dyn_cast<StoreInst>(V)) { | ||||
6202 | if (!OpStore->isSimple() || OpStore->getOperand(0) != II) | ||||
6203 | return false; | ||||
6204 | Uses.insert(OpStore); | ||||
6205 | } else if (auto *OpBC = dyn_cast<BitCastInst>(V)) { | ||||
6206 | if (!ConvertTy) | ||||
6207 | ConvertTy = OpBC->getType(); | ||||
6208 | if (OpBC->getType() != ConvertTy) | ||||
6209 | return false; | ||||
6210 | Uses.insert(OpBC); | ||||
6211 | AnyAnchored |= | ||||
6212 | any_of(OpBC->users(), [](User *U) { return !isa<StoreInst>(U); }); | ||||
6213 | } else { | ||||
6214 | return false; | ||||
6215 | } | ||||
6216 | } | ||||
6217 | } | ||||
6218 | |||||
6219 | if (!ConvertTy || !AnyAnchored || | ||||
6220 | !TLI->shouldConvertPhiType(PhiTy, ConvertTy)) | ||||
6221 | return false; | ||||
6222 | |||||
6223 | LLVM_DEBUG(dbgs() << "Converting " << *I << "\n and connected nodes to "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Converting " << * I << "\n and connected nodes to " << *ConvertTy << "\n"; } } while (false) | ||||
6224 | << *ConvertTy << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Converting " << * I << "\n and connected nodes to " << *ConvertTy << "\n"; } } while (false); | ||||
6225 | |||||
6226 | // Create all the new phi nodes of the new type, and bitcast any loads to the | ||||
6227 | // correct type. | ||||
6228 | ValueToValueMap ValMap; | ||||
6229 | for (ConstantData *C : Constants) | ||||
6230 | ValMap[C] = ConstantExpr::getCast(Instruction::BitCast, C, ConvertTy); | ||||
6231 | for (Instruction *D : Defs) { | ||||
6232 | if (isa<BitCastInst>(D)) { | ||||
6233 | ValMap[D] = D->getOperand(0); | ||||
6234 | DeletedInstrs.insert(D); | ||||
6235 | } else { | ||||
6236 | ValMap[D] = | ||||
6237 | new BitCastInst(D, ConvertTy, D->getName() + ".bc", D->getNextNode()); | ||||
6238 | } | ||||
6239 | } | ||||
6240 | for (PHINode *Phi : PhiNodes) | ||||
6241 | ValMap[Phi] = PHINode::Create(ConvertTy, Phi->getNumIncomingValues(), | ||||
6242 | Phi->getName() + ".tc", Phi); | ||||
6243 | // Pipe together all the PhiNodes. | ||||
6244 | for (PHINode *Phi : PhiNodes) { | ||||
6245 | PHINode *NewPhi = cast<PHINode>(ValMap[Phi]); | ||||
6246 | for (int i = 0, e = Phi->getNumIncomingValues(); i < e; i++) | ||||
6247 | NewPhi->addIncoming(ValMap[Phi->getIncomingValue(i)], | ||||
6248 | Phi->getIncomingBlock(i)); | ||||
6249 | Visited.insert(NewPhi); | ||||
6250 | } | ||||
6251 | // And finally pipe up the stores and bitcasts | ||||
6252 | for (Instruction *U : Uses) { | ||||
6253 | if (isa<BitCastInst>(U)) { | ||||
6254 | DeletedInstrs.insert(U); | ||||
6255 | replaceAllUsesWith(U, ValMap[U->getOperand(0)], FreshBBs, IsHugeFunc); | ||||
6256 | } else { | ||||
6257 | U->setOperand(0, | ||||
6258 | new BitCastInst(ValMap[U->getOperand(0)], PhiTy, "bc", U)); | ||||
6259 | } | ||||
6260 | } | ||||
6261 | |||||
6262 | // Save the removed phis to be deleted later. | ||||
6263 | for (PHINode *Phi : PhiNodes) | ||||
6264 | DeletedInstrs.insert(Phi); | ||||
6265 | return true; | ||||
6266 | } | ||||
6267 | |||||
6268 | bool CodeGenPrepare::optimizePhiTypes(Function &F) { | ||||
6269 | if (!OptimizePhiTypes) | ||||
6270 | return false; | ||||
6271 | |||||
6272 | bool Changed = false; | ||||
6273 | SmallPtrSet<PHINode *, 4> Visited; | ||||
6274 | SmallPtrSet<Instruction *, 4> DeletedInstrs; | ||||
6275 | |||||
6276 | // Attempt to optimize all the phis in the functions to the correct type. | ||||
6277 | for (auto &BB : F) | ||||
6278 | for (auto &Phi : BB.phis()) | ||||
6279 | Changed |= optimizePhiType(&Phi, Visited, DeletedInstrs); | ||||
6280 | |||||
6281 | // Remove any old phi's that have been converted. | ||||
6282 | for (auto *I : DeletedInstrs) { | ||||
6283 | replaceAllUsesWith(I, PoisonValue::get(I->getType()), FreshBBs, IsHugeFunc); | ||||
6284 | I->eraseFromParent(); | ||||
6285 | } | ||||
6286 | |||||
6287 | return Changed; | ||||
6288 | } | ||||
6289 | |||||
6290 | /// Return true, if an ext(load) can be formed from an extension in | ||||
6291 | /// \p MovedExts. | ||||
6292 | bool CodeGenPrepare::canFormExtLd( | ||||
6293 | const SmallVectorImpl<Instruction *> &MovedExts, LoadInst *&LI, | ||||
6294 | Instruction *&Inst, bool HasPromoted) { | ||||
6295 | for (auto *MovedExtInst : MovedExts) { | ||||
6296 | if (isa<LoadInst>(MovedExtInst->getOperand(0))) { | ||||
6297 | LI = cast<LoadInst>(MovedExtInst->getOperand(0)); | ||||
6298 | Inst = MovedExtInst; | ||||
6299 | break; | ||||
6300 | } | ||||
6301 | } | ||||
6302 | if (!LI) | ||||
6303 | return false; | ||||
6304 | |||||
6305 | // If they're already in the same block, there's nothing to do. | ||||
6306 | // Make the cheap checks first if we did not promote. | ||||
6307 | // If we promoted, we need to check if it is indeed profitable. | ||||
6308 | if (!HasPromoted && LI->getParent() == Inst->getParent()) | ||||
6309 | return false; | ||||
6310 | |||||
6311 | return TLI->isExtLoad(LI, Inst, *DL); | ||||
6312 | } | ||||
6313 | |||||
6314 | /// Move a zext or sext fed by a load into the same basic block as the load, | ||||
6315 | /// unless conditions are unfavorable. This allows SelectionDAG to fold the | ||||
6316 | /// extend into the load. | ||||
6317 | /// | ||||
6318 | /// E.g., | ||||
6319 | /// \code | ||||
6320 | /// %ld = load i32* %addr | ||||
6321 | /// %add = add nuw i32 %ld, 4 | ||||
6322 | /// %zext = zext i32 %add to i64 | ||||
6323 | // \endcode | ||||
6324 | /// => | ||||
6325 | /// \code | ||||
6326 | /// %ld = load i32* %addr | ||||
6327 | /// %zext = zext i32 %ld to i64 | ||||
6328 | /// %add = add nuw i64 %zext, 4 | ||||
6329 | /// \encode | ||||
6330 | /// Note that the promotion in %add to i64 is done in tryToPromoteExts(), which | ||||
6331 | /// allow us to match zext(load i32*) to i64. | ||||
6332 | /// | ||||
6333 | /// Also, try to promote the computations used to obtain a sign extended | ||||
6334 | /// value used into memory accesses. | ||||
6335 | /// E.g., | ||||
6336 | /// \code | ||||
6337 | /// a = add nsw i32 b, 3 | ||||
6338 | /// d = sext i32 a to i64 | ||||
6339 | /// e = getelementptr ..., i64 d | ||||
6340 | /// \endcode | ||||
6341 | /// => | ||||
6342 | /// \code | ||||
6343 | /// f = sext i32 b to i64 | ||||
6344 | /// a = add nsw i64 f, 3 | ||||
6345 | /// e = getelementptr ..., i64 a | ||||
6346 | /// \endcode | ||||
6347 | /// | ||||
6348 | /// \p Inst[in/out] the extension may be modified during the process if some | ||||
6349 | /// promotions apply. | ||||
6350 | bool CodeGenPrepare::optimizeExt(Instruction *&Inst) { | ||||
6351 | bool AllowPromotionWithoutCommonHeader = false; | ||||
6352 | /// See if it is an interesting sext operations for the address type | ||||
6353 | /// promotion before trying to promote it, e.g., the ones with the right | ||||
6354 | /// type and used in memory accesses. | ||||
6355 | bool ATPConsiderable = TTI->shouldConsiderAddressTypePromotion( | ||||
6356 | *Inst, AllowPromotionWithoutCommonHeader); | ||||
6357 | TypePromotionTransaction TPT(RemovedInsts); | ||||
6358 | TypePromotionTransaction::ConstRestorationPt LastKnownGood = | ||||
6359 | TPT.getRestorationPoint(); | ||||
6360 | SmallVector<Instruction *, 1> Exts; | ||||
6361 | SmallVector<Instruction *, 2> SpeculativelyMovedExts; | ||||
6362 | Exts.push_back(Inst); | ||||
6363 | |||||
6364 | bool HasPromoted = tryToPromoteExts(TPT, Exts, SpeculativelyMovedExts); | ||||
6365 | |||||
6366 | // Look for a load being extended. | ||||
6367 | LoadInst *LI = nullptr; | ||||
6368 | Instruction *ExtFedByLoad; | ||||
6369 | |||||
6370 | // Try to promote a chain of computation if it allows to form an extended | ||||
6371 | // load. | ||||
6372 | if (canFormExtLd(SpeculativelyMovedExts, LI, ExtFedByLoad, HasPromoted)) { | ||||
6373 | assert(LI && ExtFedByLoad && "Expect a valid load and extension")(static_cast <bool> (LI && ExtFedByLoad && "Expect a valid load and extension") ? void (0) : __assert_fail ("LI && ExtFedByLoad && \"Expect a valid load and extension\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6373, __extension__ __PRETTY_FUNCTION__ )); | ||||
6374 | TPT.commit(); | ||||
6375 | // Move the extend into the same block as the load. | ||||
6376 | ExtFedByLoad->moveAfter(LI); | ||||
6377 | ++NumExtsMoved; | ||||
6378 | Inst = ExtFedByLoad; | ||||
6379 | return true; | ||||
6380 | } | ||||
6381 | |||||
6382 | // Continue promoting SExts if known as considerable depending on targets. | ||||
6383 | if (ATPConsiderable && | ||||
6384 | performAddressTypePromotion(Inst, AllowPromotionWithoutCommonHeader, | ||||
6385 | HasPromoted, TPT, SpeculativelyMovedExts)) | ||||
6386 | return true; | ||||
6387 | |||||
6388 | TPT.rollback(LastKnownGood); | ||||
6389 | return false; | ||||
6390 | } | ||||
6391 | |||||
6392 | // Perform address type promotion if doing so is profitable. | ||||
6393 | // If AllowPromotionWithoutCommonHeader == false, we should find other sext | ||||
6394 | // instructions that sign extended the same initial value. However, if | ||||
6395 | // AllowPromotionWithoutCommonHeader == true, we expect promoting the | ||||
6396 | // extension is just profitable. | ||||
6397 | bool CodeGenPrepare::performAddressTypePromotion( | ||||
6398 | Instruction *&Inst, bool AllowPromotionWithoutCommonHeader, | ||||
6399 | bool HasPromoted, TypePromotionTransaction &TPT, | ||||
6400 | SmallVectorImpl<Instruction *> &SpeculativelyMovedExts) { | ||||
6401 | bool Promoted = false; | ||||
6402 | SmallPtrSet<Instruction *, 1> UnhandledExts; | ||||
6403 | bool AllSeenFirst = true; | ||||
6404 | for (auto *I : SpeculativelyMovedExts) { | ||||
6405 | Value *HeadOfChain = I->getOperand(0); | ||||
6406 | DenseMap<Value *, Instruction *>::iterator AlreadySeen = | ||||
6407 | SeenChainsForSExt.find(HeadOfChain); | ||||
6408 | // If there is an unhandled SExt which has the same header, try to promote | ||||
6409 | // it as well. | ||||
6410 | if (AlreadySeen != SeenChainsForSExt.end()) { | ||||
6411 | if (AlreadySeen->second != nullptr) | ||||
6412 | UnhandledExts.insert(AlreadySeen->second); | ||||
6413 | AllSeenFirst = false; | ||||
6414 | } | ||||
6415 | } | ||||
6416 | |||||
6417 | if (!AllSeenFirst || (AllowPromotionWithoutCommonHeader && | ||||
6418 | SpeculativelyMovedExts.size() == 1)) { | ||||
6419 | TPT.commit(); | ||||
6420 | if (HasPromoted) | ||||
6421 | Promoted = true; | ||||
6422 | for (auto *I : SpeculativelyMovedExts) { | ||||
6423 | Value *HeadOfChain = I->getOperand(0); | ||||
6424 | SeenChainsForSExt[HeadOfChain] = nullptr; | ||||
6425 | ValToSExtendedUses[HeadOfChain].push_back(I); | ||||
6426 | } | ||||
6427 | // Update Inst as promotion happen. | ||||
6428 | Inst = SpeculativelyMovedExts.pop_back_val(); | ||||
6429 | } else { | ||||
6430 | // This is the first chain visited from the header, keep the current chain | ||||
6431 | // as unhandled. Defer to promote this until we encounter another SExt | ||||
6432 | // chain derived from the same header. | ||||
6433 | for (auto *I : SpeculativelyMovedExts) { | ||||
6434 | Value *HeadOfChain = I->getOperand(0); | ||||
6435 | SeenChainsForSExt[HeadOfChain] = Inst; | ||||
6436 | } | ||||
6437 | return false; | ||||
6438 | } | ||||
6439 | |||||
6440 | if (!AllSeenFirst && !UnhandledExts.empty()) | ||||
6441 | for (auto *VisitedSExt : UnhandledExts) { | ||||
6442 | if (RemovedInsts.count(VisitedSExt)) | ||||
6443 | continue; | ||||
6444 | TypePromotionTransaction TPT(RemovedInsts); | ||||
6445 | SmallVector<Instruction *, 1> Exts; | ||||
6446 | SmallVector<Instruction *, 2> Chains; | ||||
6447 | Exts.push_back(VisitedSExt); | ||||
6448 | bool HasPromoted = tryToPromoteExts(TPT, Exts, Chains); | ||||
6449 | TPT.commit(); | ||||
6450 | if (HasPromoted) | ||||
6451 | Promoted = true; | ||||
6452 | for (auto *I : Chains) { | ||||
6453 | Value *HeadOfChain = I->getOperand(0); | ||||
6454 | // Mark this as handled. | ||||
6455 | SeenChainsForSExt[HeadOfChain] = nullptr; | ||||
6456 | ValToSExtendedUses[HeadOfChain].push_back(I); | ||||
6457 | } | ||||
6458 | } | ||||
6459 | return Promoted; | ||||
6460 | } | ||||
6461 | |||||
6462 | bool CodeGenPrepare::optimizeExtUses(Instruction *I) { | ||||
6463 | BasicBlock *DefBB = I->getParent(); | ||||
6464 | |||||
6465 | // If the result of a {s|z}ext and its source are both live out, rewrite all | ||||
6466 | // other uses of the source with result of extension. | ||||
6467 | Value *Src = I->getOperand(0); | ||||
6468 | if (Src->hasOneUse()) | ||||
6469 | return false; | ||||
6470 | |||||
6471 | // Only do this xform if truncating is free. | ||||
6472 | if (!TLI->isTruncateFree(I->getType(), Src->getType())) | ||||
6473 | return false; | ||||
6474 | |||||
6475 | // Only safe to perform the optimization if the source is also defined in | ||||
6476 | // this block. | ||||
6477 | if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent()) | ||||
6478 | return false; | ||||
6479 | |||||
6480 | bool DefIsLiveOut = false; | ||||
6481 | for (User *U : I->users()) { | ||||
6482 | Instruction *UI = cast<Instruction>(U); | ||||
6483 | |||||
6484 | // Figure out which BB this ext is used in. | ||||
6485 | BasicBlock *UserBB = UI->getParent(); | ||||
6486 | if (UserBB == DefBB) | ||||
6487 | continue; | ||||
6488 | DefIsLiveOut = true; | ||||
6489 | break; | ||||
6490 | } | ||||
6491 | if (!DefIsLiveOut) | ||||
6492 | return false; | ||||
6493 | |||||
6494 | // Make sure none of the uses are PHI nodes. | ||||
6495 | for (User *U : Src->users()) { | ||||
6496 | Instruction *UI = cast<Instruction>(U); | ||||
6497 | BasicBlock *UserBB = UI->getParent(); | ||||
6498 | if (UserBB == DefBB) | ||||
6499 | continue; | ||||
6500 | // Be conservative. We don't want this xform to end up introducing | ||||
6501 | // reloads just before load / store instructions. | ||||
6502 | if (isa<PHINode>(UI) || isa<LoadInst>(UI) || isa<StoreInst>(UI)) | ||||
6503 | return false; | ||||
6504 | } | ||||
6505 | |||||
6506 | // InsertedTruncs - Only insert one trunc in each block once. | ||||
6507 | DenseMap<BasicBlock *, Instruction *> InsertedTruncs; | ||||
6508 | |||||
6509 | bool MadeChange = false; | ||||
6510 | for (Use &U : Src->uses()) { | ||||
6511 | Instruction *User = cast<Instruction>(U.getUser()); | ||||
6512 | |||||
6513 | // Figure out which BB this ext is used in. | ||||
6514 | BasicBlock *UserBB = User->getParent(); | ||||
6515 | if (UserBB == DefBB) | ||||
6516 | continue; | ||||
6517 | |||||
6518 | // Both src and def are live in this block. Rewrite the use. | ||||
6519 | Instruction *&InsertedTrunc = InsertedTruncs[UserBB]; | ||||
6520 | |||||
6521 | if (!InsertedTrunc) { | ||||
6522 | BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt(); | ||||
6523 | assert(InsertPt != UserBB->end())(static_cast <bool> (InsertPt != UserBB->end()) ? void (0) : __assert_fail ("InsertPt != UserBB->end()", "llvm/lib/CodeGen/CodeGenPrepare.cpp" , 6523, __extension__ __PRETTY_FUNCTION__)); | ||||
6524 | InsertedTrunc = new TruncInst(I, Src->getType(), "", &*InsertPt); | ||||
6525 | InsertedInsts.insert(InsertedTrunc); | ||||
6526 | } | ||||
6527 | |||||
6528 | // Replace a use of the {s|z}ext source with a use of the result. | ||||
6529 | U = InsertedTrunc; | ||||
6530 | ++NumExtUses; | ||||
6531 | MadeChange = true; | ||||
6532 | } | ||||
6533 | |||||
6534 | return MadeChange; | ||||
6535 | } | ||||
6536 | |||||
6537 | // Find loads whose uses only use some of the loaded value's bits. Add an "and" | ||||
6538 | // just after the load if the target can fold this into one extload instruction, | ||||
6539 | // with the hope of eliminating some of the other later "and" instructions using | ||||
6540 | // the loaded value. "and"s that are made trivially redundant by the insertion | ||||
6541 | // of the new "and" are removed by this function, while others (e.g. those whose | ||||
6542 | // path from the load goes through a phi) are left for isel to potentially | ||||
6543 | // remove. | ||||
6544 | // | ||||
6545 | // For example: | ||||
6546 | // | ||||
6547 | // b0: | ||||
6548 | // x = load i32 | ||||
6549 | // ... | ||||
6550 | // b1: | ||||
6551 | // y = and x, 0xff | ||||
6552 | // z = use y | ||||
6553 | // | ||||
6554 | // becomes: | ||||
6555 | // | ||||
6556 | // b0: | ||||
6557 | // x = load i32 | ||||
6558 | // x' = and x, 0xff | ||||
6559 | // ... | ||||
6560 | // b1: | ||||
6561 | // z = use x' | ||||
6562 | // | ||||
6563 | // whereas: | ||||
6564 | // | ||||
6565 | // b0: | ||||
6566 | // x1 = load i32 | ||||
6567 | // ... | ||||
6568 | // b1: | ||||
6569 | // x2 = load i32 | ||||
6570 | // ... | ||||
6571 | // b2: | ||||
6572 | // x = phi x1, x2 | ||||
6573 | // y = and x, 0xff | ||||
6574 | // | ||||
6575 | // becomes (after a call to optimizeLoadExt for each load): | ||||
6576 | // | ||||
6577 | // b0: | ||||
6578 | // x1 = load i32 | ||||
6579 | // x1' = and x1, 0xff | ||||
6580 | // ... | ||||
6581 | // b1: | ||||
6582 | // x2 = load i32 | ||||
6583 | // x2' = and x2, 0xff | ||||
6584 | // ... | ||||
6585 | // b2: | ||||
6586 | // x = phi x1', x2' | ||||
6587 | // y = and x, 0xff | ||||
6588 | bool CodeGenPrepare::optimizeLoadExt(LoadInst *Load) { | ||||
6589 | if (!Load->isSimple() || !Load->getType()->isIntOrPtrTy()) | ||||
6590 | return false; | ||||
6591 | |||||
6592 | // Skip loads we've already transformed. | ||||
6593 | if (Load->hasOneUse() && | ||||
6594 | InsertedInsts.count(cast<Instruction>(*Load->user_begin()))) | ||||
6595 | return false; | ||||
6596 | |||||
6597 | // Look at all uses of Load, looking through phis, to determine how many bits | ||||
6598 | // of the loaded value are needed. | ||||
6599 | SmallVector<Instruction *, 8> WorkList; | ||||
6600 | SmallPtrSet<Instruction *, 16> Visited; | ||||
6601 | SmallVector<Instruction *, 8> AndsToMaybeRemove; | ||||
6602 | for (auto *U : Load->users()) | ||||
6603 | WorkList.push_back(cast<Instruction>(U)); | ||||
6604 | |||||
6605 | EVT LoadResultVT = TLI->getValueType(*DL, Load->getType()); | ||||
6606 | unsigned BitWidth = LoadResultVT.getSizeInBits(); | ||||
6607 | // If the BitWidth is 0, do not try to optimize the type | ||||
6608 | if (BitWidth == 0) | ||||
6609 | return false; | ||||
6610 | |||||
6611 | APInt DemandBits(BitWidth, 0); | ||||
6612 | APInt WidestAndBits(BitWidth, 0); | ||||
6613 | |||||
6614 | while (!WorkList.empty()) { | ||||
6615 | Instruction *I = WorkList.pop_back_val(); | ||||
6616 | |||||
6617 | // Break use-def graph loops. | ||||
6618 | if (!Visited.insert(I).second) | ||||
6619 | continue; | ||||
6620 | |||||
6621 | // For a PHI node, push all of its users. | ||||
6622 | if (auto *Phi = dyn_cast<PHINode>(I)) { | ||||
6623 | for (auto *U : Phi->users()) | ||||
6624 | WorkList.push_back(cast<Instruction>(U)); | ||||
6625 | continue; | ||||
6626 | } | ||||
6627 | |||||
6628 | switch (I->getOpcode()) { | ||||
6629 | case Instruction::And: { | ||||
6630 | auto *AndC = dyn_cast<ConstantInt>(I->getOperand(1)); | ||||
6631 | if (!AndC) | ||||
6632 | return false; | ||||
6633 | APInt AndBits = AndC->getValue(); | ||||
6634 | DemandBits |= AndBits; | ||||
6635 | // Keep track of the widest and mask we see. | ||||
6636 | if (AndBits.ugt(WidestAndBits)) | ||||
6637 | WidestAndBits = AndBits; | ||||
6638 | if (AndBits == WidestAndBits && I->getOperand(0) == Load) | ||||
6639 | AndsToMaybeRemove.push_back(I); | ||||
6640 | break; | ||||
6641 | } | ||||
6642 | |||||
6643 | case Instruction::Shl: { | ||||
6644 | auto *ShlC = dyn_cast<ConstantInt>(I->getOperand(1)); | ||||
6645 | if (!ShlC) | ||||
6646 | return false; | ||||
6647 | uint64_t ShiftAmt = ShlC->getLimitedValue(BitWidth - 1); | ||||
6648 | DemandBits.setLowBits(BitWidth - ShiftAmt); | ||||
6649 | break; | ||||
6650 | } | ||||
6651 | |||||
6652 | case Instruction::Trunc: { | ||||
6653 | EVT TruncVT = TLI->getValueType(*DL, I->getType()); | ||||
6654 | unsigned TruncBitWidth = TruncVT.getSizeInBits(); | ||||
6655 | DemandBits.setLowBits(TruncBitWidth); | ||||
6656 | break; | ||||
6657 | } | ||||
6658 | |||||
6659 | default: | ||||
6660 | return false; | ||||
6661 | } | ||||
6662 | } | ||||
6663 | |||||
6664 | uint32_t ActiveBits = DemandBits.getActiveBits(); | ||||
6665 | // Avoid hoisting (and (load x) 1) since it is unlikely to be folded by the | ||||
6666 | // target even if isLoadExtLegal says an i1 EXTLOAD is valid. For example, | ||||
6667 | // for the AArch64 target isLoadExtLegal(ZEXTLOAD, i32, i1) returns true, but | ||||
6668 | // (and (load x) 1) is not matched as a single instruction, rather as a LDR | ||||
6669 | // followed by an AND. | ||||
6670 | // TODO: Look into removing this restriction by fixing backends to either | ||||
6671 | // return false for isLoadExtLegal for i1 or have them select this pattern to | ||||
6672 | // a single instruction. | ||||
6673 | // | ||||
6674 | // Also avoid hoisting if we didn't see any ands with the exact DemandBits | ||||
6675 | // mask, since these are the only ands that will be removed by isel. | ||||
6676 | if (ActiveBits <= 1 || !DemandBits.isMask(ActiveBits) || | ||||
6677 | WidestAndBits != DemandBits) | ||||
6678 | return false; | ||||
6679 | |||||
6680 | LLVMContext &Ctx = Load->getType()->getContext(); | ||||
6681 | Type *TruncTy = Type::getIntNTy(Ctx, ActiveBits); | ||||
6682 | EVT TruncVT = TLI->getValueType(*DL, TruncTy); | ||||
6683 | |||||
6684 | // Reject cases that won't be matched as extloads. | ||||
6685 | if (!LoadResultVT.bitsGT(TruncVT) || !TruncVT.isRound() || | ||||
6686 | !TLI->isLoadExtLegal(ISD::ZEXTLOAD, LoadResultVT, TruncVT)) | ||||
6687 | return false; | ||||
6688 | |||||
6689 | IRBuilder<> Builder(Load->getNextNode()); | ||||
6690 | auto *NewAnd = cast<Instruction>( | ||||
6691 | Builder.CreateAnd(Load, ConstantInt::get(Ctx, DemandBits))); | ||||
6692 | // Mark this instruction as "inserted by CGP", so that other | ||||
6693 | // optimizations don't touch it. | ||||
6694 | InsertedInsts.insert(NewAnd); | ||||
6695 | |||||
6696 | // Replace all uses of load with new and (except for the use of load in the | ||||
6697 | // new and itself). | ||||
6698 | replaceAllUsesWith(Load, NewAnd, FreshBBs, IsHugeFunc); | ||||
6699 | NewAnd->setOperand(0, Load); | ||||
6700 | |||||
6701 | // Remove any and instructions that are now redundant. | ||||
6702 | for (auto *And : AndsToMaybeRemove) | ||||
6703 | // Check that the and mask is the same as the one we decided to put on the | ||||
6704 | // new and. | ||||
6705 | if (cast<ConstantInt>(And->getOperand(1))->getValue() == DemandBits) { | ||||
6706 | replaceAllUsesWith(And, NewAnd, FreshBBs, IsHugeFunc); | ||||
6707 | if (&*CurInstIterator == And) | ||||
6708 | CurInstIterator = std::next(And->getIterator()); | ||||
6709 | And->eraseFromParent(); | ||||
6710 | ++NumAndUses; | ||||
6711 | } | ||||
6712 | |||||
6713 | ++NumAndsAdded; | ||||
6714 | return true; | ||||
6715 | } | ||||
6716 | |||||
6717 | /// Check if V (an operand of a select instruction) is an expensive instruction | ||||
6718 | /// that is only used once. | ||||
6719 | static bool sinkSelectOperand(const TargetTransformInfo *TTI, Value *V) { | ||||
6720 | auto *I = dyn_cast<Instruction>(V); | ||||
6721 | // If it's safe to speculatively execute, then it should not have side | ||||
6722 | // effects; therefore, it's safe to sink and possibly *not* execute. | ||||
6723 | return I && I->hasOneUse() && isSafeToSpeculativelyExecute(I) && | ||||
6724 | TTI->isExpensiveToSpeculativelyExecute(I); | ||||
6725 | } | ||||
6726 | |||||
6727 | /// Returns true if a SelectInst should be turned into an explicit branch. | ||||
6728 | static bool isFormingBranchFromSelectProfitable(const TargetTransformInfo *TTI, | ||||
6729 | const TargetLowering *TLI, | ||||
6730 | SelectInst *SI) { | ||||
6731 | // If even a predictable select is cheap, then a branch can't be cheaper. | ||||
6732 | if (!TLI->isPredictableSelectExpensive()) | ||||
6733 | return false; | ||||
6734 | |||||
6735 | // FIXME: This should use the same heuristics as IfConversion to determine | ||||
6736 | // whether a select is better represented as a branch. | ||||
6737 | |||||
6738 | // If metadata tells us that the select condition is obviously predictable, | ||||
6739 | // then we want to replace the select with a branch. | ||||
6740 | uint64_t TrueWeight, FalseWeight; | ||||
6741 | if (extractBranchWeights(*SI, TrueWeight, FalseWeight)) { | ||||
6742 | uint64_t Max = std::max(TrueWeight, FalseWeight); | ||||
6743 | uint64_t Sum = TrueWeight + FalseWeight; | ||||
6744 | if (Sum != 0) { | ||||
6745 | auto Probability = BranchProbability::getBranchProbability(Max, Sum); | ||||
6746 | if (Probability > TTI->getPredictableBranchThreshold()) | ||||
6747 | return true; | ||||
6748 | } | ||||
6749 | } | ||||
6750 | |||||
6751 | CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition()); | ||||
6752 | |||||
6753 | // If a branch is predictable, an out-of-order CPU can avoid blocking on its | ||||
6754 | // comparison condition. If the compare has more than one use, there's | ||||
6755 | // probably another cmov or setcc around, so it's not worth emitting a branch. | ||||
6756 | if (!Cmp || !Cmp->hasOneUse()) | ||||
6757 | return false; | ||||
6758 | |||||
6759 | // If either operand of the select is expensive and only needed on one side | ||||
6760 | // of the select, we should form a branch. | ||||
6761 | if (sinkSelectOperand(TTI, SI->getTrueValue()) || | ||||
6762 | sinkSelectOperand(TTI, SI->getFalseValue())) | ||||
6763 | return true; | ||||
6764 | |||||
6765 | return false; | ||||
6766 | } | ||||
6767 | |||||
6768 | /// If \p isTrue is true, return the true value of \p SI, otherwise return | ||||
6769 | /// false value of \p SI. If the true/false value of \p SI is defined by any | ||||
6770 | /// select instructions in \p Selects, look through the defining select | ||||
6771 | /// instruction until the true/false value is not defined in \p Selects. | ||||
6772 | static Value * | ||||
6773 | getTrueOrFalseValue(SelectInst *SI, bool isTrue, | ||||
6774 | const SmallPtrSet<const Instruction *, 2> &Selects) { | ||||
6775 | Value *V = nullptr; | ||||
6776 | |||||
6777 | for (SelectInst *DefSI = SI; DefSI != nullptr && Selects.count(DefSI); | ||||
6778 | DefSI = dyn_cast<SelectInst>(V)) { | ||||
6779 | assert(DefSI->getCondition() == SI->getCondition() &&(static_cast <bool> (DefSI->getCondition() == SI-> getCondition() && "The condition of DefSI does not match with SI" ) ? void (0) : __assert_fail ("DefSI->getCondition() == SI->getCondition() && \"The condition of DefSI does not match with SI\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6780, __extension__ __PRETTY_FUNCTION__ )) | ||||
6780 | "The condition of DefSI does not match with SI")(static_cast <bool> (DefSI->getCondition() == SI-> getCondition() && "The condition of DefSI does not match with SI" ) ? void (0) : __assert_fail ("DefSI->getCondition() == SI->getCondition() && \"The condition of DefSI does not match with SI\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6780, __extension__ __PRETTY_FUNCTION__ )); | ||||
6781 | V = (isTrue ? DefSI->getTrueValue() : DefSI->getFalseValue()); | ||||
6782 | } | ||||
6783 | |||||
6784 | assert(V && "Failed to get select true/false value")(static_cast <bool> (V && "Failed to get select true/false value" ) ? void (0) : __assert_fail ("V && \"Failed to get select true/false value\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6784, __extension__ __PRETTY_FUNCTION__ )); | ||||
6785 | return V; | ||||
6786 | } | ||||
6787 | |||||
6788 | bool CodeGenPrepare::optimizeShiftInst(BinaryOperator *Shift) { | ||||
6789 | assert(Shift->isShift() && "Expected a shift")(static_cast <bool> (Shift->isShift() && "Expected a shift" ) ? void (0) : __assert_fail ("Shift->isShift() && \"Expected a shift\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6789, __extension__ __PRETTY_FUNCTION__ )); | ||||
6790 | |||||
6791 | // If this is (1) a vector shift, (2) shifts by scalars are cheaper than | ||||
6792 | // general vector shifts, and (3) the shift amount is a select-of-splatted | ||||
6793 | // values, hoist the shifts before the select: | ||||
6794 | // shift Op0, (select Cond, TVal, FVal) --> | ||||
6795 | // select Cond, (shift Op0, TVal), (shift Op0, FVal) | ||||
6796 | // | ||||
6797 | // This is inverting a generic IR transform when we know that the cost of a | ||||
6798 | // general vector shift is more than the cost of 2 shift-by-scalars. | ||||
6799 | // We can't do this effectively in SDAG because we may not be able to | ||||
6800 | // determine if the select operands are splats from within a basic block. | ||||
6801 | Type *Ty = Shift->getType(); | ||||
6802 | if (!Ty->isVectorTy() || !TLI->isVectorShiftByScalarCheap(Ty)) | ||||
6803 | return false; | ||||
6804 | Value *Cond, *TVal, *FVal; | ||||
6805 | if (!match(Shift->getOperand(1), | ||||
6806 | m_OneUse(m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal))))) | ||||
6807 | return false; | ||||
6808 | if (!isSplatValue(TVal) || !isSplatValue(FVal)) | ||||
6809 | return false; | ||||
6810 | |||||
6811 | IRBuilder<> Builder(Shift); | ||||
6812 | BinaryOperator::BinaryOps Opcode = Shift->getOpcode(); | ||||
6813 | Value *NewTVal = Builder.CreateBinOp(Opcode, Shift->getOperand(0), TVal); | ||||
6814 | Value *NewFVal = Builder.CreateBinOp(Opcode, Shift->getOperand(0), FVal); | ||||
6815 | Value *NewSel = Builder.CreateSelect(Cond, NewTVal, NewFVal); | ||||
6816 | replaceAllUsesWith(Shift, NewSel, FreshBBs, IsHugeFunc); | ||||
6817 | Shift->eraseFromParent(); | ||||
6818 | return true; | ||||
6819 | } | ||||
6820 | |||||
6821 | bool CodeGenPrepare::optimizeFunnelShift(IntrinsicInst *Fsh) { | ||||
6822 | Intrinsic::ID Opcode = Fsh->getIntrinsicID(); | ||||
6823 | assert((Opcode == Intrinsic::fshl || Opcode == Intrinsic::fshr) &&(static_cast <bool> ((Opcode == Intrinsic::fshl || Opcode == Intrinsic::fshr) && "Expected a funnel shift") ? void (0) : __assert_fail ("(Opcode == Intrinsic::fshl || Opcode == Intrinsic::fshr) && \"Expected a funnel shift\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6824, __extension__ __PRETTY_FUNCTION__ )) | ||||
6824 | "Expected a funnel shift")(static_cast <bool> ((Opcode == Intrinsic::fshl || Opcode == Intrinsic::fshr) && "Expected a funnel shift") ? void (0) : __assert_fail ("(Opcode == Intrinsic::fshl || Opcode == Intrinsic::fshr) && \"Expected a funnel shift\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6824, __extension__ __PRETTY_FUNCTION__ )); | ||||
6825 | |||||
6826 | // If this is (1) a vector funnel shift, (2) shifts by scalars are cheaper | ||||
6827 | // than general vector shifts, and (3) the shift amount is select-of-splatted | ||||
6828 | // values, hoist the funnel shifts before the select: | ||||
6829 | // fsh Op0, Op1, (select Cond, TVal, FVal) --> | ||||
6830 | // select Cond, (fsh Op0, Op1, TVal), (fsh Op0, Op1, FVal) | ||||
6831 | // | ||||
6832 | // This is inverting a generic IR transform when we know that the cost of a | ||||
6833 | // general vector shift is more than the cost of 2 shift-by-scalars. | ||||
6834 | // We can't do this effectively in SDAG because we may not be able to | ||||
6835 | // determine if the select operands are splats from within a basic block. | ||||
6836 | Type *Ty = Fsh->getType(); | ||||
6837 | if (!Ty->isVectorTy() || !TLI->isVectorShiftByScalarCheap(Ty)) | ||||
6838 | return false; | ||||
6839 | Value *Cond, *TVal, *FVal; | ||||
6840 | if (!match(Fsh->getOperand(2), | ||||
6841 | m_OneUse(m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal))))) | ||||
6842 | return false; | ||||
6843 | if (!isSplatValue(TVal) || !isSplatValue(FVal)) | ||||
6844 | return false; | ||||
6845 | |||||
6846 | IRBuilder<> Builder(Fsh); | ||||
6847 | Value *X = Fsh->getOperand(0), *Y = Fsh->getOperand(1); | ||||
6848 | Value *NewTVal = Builder.CreateIntrinsic(Opcode, Ty, {X, Y, TVal}); | ||||
6849 | Value *NewFVal = Builder.CreateIntrinsic(Opcode, Ty, {X, Y, FVal}); | ||||
6850 | Value *NewSel = Builder.CreateSelect(Cond, NewTVal, NewFVal); | ||||
6851 | replaceAllUsesWith(Fsh, NewSel, FreshBBs, IsHugeFunc); | ||||
6852 | Fsh->eraseFromParent(); | ||||
6853 | return true; | ||||
6854 | } | ||||
6855 | |||||
6856 | /// If we have a SelectInst that will likely profit from branch prediction, | ||||
6857 | /// turn it into a branch. | ||||
6858 | bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) { | ||||
6859 | if (DisableSelectToBranch) | ||||
6860 | return false; | ||||
6861 | |||||
6862 | // If the SelectOptimize pass is enabled, selects have already been optimized. | ||||
6863 | if (!getCGPassBuilderOption().DisableSelectOptimize) | ||||
6864 | return false; | ||||
6865 | |||||
6866 | // Find all consecutive select instructions that share the same condition. | ||||
6867 | SmallVector<SelectInst *, 2> ASI; | ||||
6868 | ASI.push_back(SI); | ||||
6869 | for (BasicBlock::iterator It = ++BasicBlock::iterator(SI); | ||||
6870 | It != SI->getParent()->end(); ++It) { | ||||
6871 | SelectInst *I = dyn_cast<SelectInst>(&*It); | ||||
6872 | if (I && SI->getCondition() == I->getCondition()) { | ||||
6873 | ASI.push_back(I); | ||||
6874 | } else { | ||||
6875 | break; | ||||
6876 | } | ||||
6877 | } | ||||
6878 | |||||
6879 | SelectInst *LastSI = ASI.back(); | ||||
6880 | // Increment the current iterator to skip all the rest of select instructions | ||||
6881 | // because they will be either "not lowered" or "all lowered" to branch. | ||||
6882 | CurInstIterator = std::next(LastSI->getIterator()); | ||||
6883 | |||||
6884 | bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1); | ||||
6885 | |||||
6886 | // Can we convert the 'select' to CF ? | ||||
6887 | if (VectorCond || SI->getMetadata(LLVMContext::MD_unpredictable)) | ||||
6888 | return false; | ||||
6889 | |||||
6890 | TargetLowering::SelectSupportKind SelectKind; | ||||
6891 | if (SI->getType()->isVectorTy()) | ||||
6892 | SelectKind = TargetLowering::ScalarCondVectorVal; | ||||
6893 | else | ||||
6894 | SelectKind = TargetLowering::ScalarValSelect; | ||||
6895 | |||||
6896 | if (TLI->isSelectSupported(SelectKind) && | ||||
6897 | (!isFormingBranchFromSelectProfitable(TTI, TLI, SI) || OptSize || | ||||
6898 | llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI.get()))) | ||||
6899 | return false; | ||||
6900 | |||||
6901 | // The DominatorTree needs to be rebuilt by any consumers after this | ||||
6902 | // transformation. We simply reset here rather than setting the ModifiedDT | ||||
6903 | // flag to avoid restarting the function walk in runOnFunction for each | ||||
6904 | // select optimized. | ||||
6905 | DT.reset(); | ||||
6906 | |||||
6907 | // Transform a sequence like this: | ||||
6908 | // start: | ||||
6909 | // %cmp = cmp uge i32 %a, %b | ||||
6910 | // %sel = select i1 %cmp, i32 %c, i32 %d | ||||
6911 | // | ||||
6912 | // Into: | ||||
6913 | // start: | ||||
6914 | // %cmp = cmp uge i32 %a, %b | ||||
6915 | // %cmp.frozen = freeze %cmp | ||||
6916 | // br i1 %cmp.frozen, label %select.true, label %select.false | ||||
6917 | // select.true: | ||||
6918 | // br label %select.end | ||||
6919 | // select.false: | ||||
6920 | // br label %select.end | ||||
6921 | // select.end: | ||||
6922 | // %sel = phi i32 [ %c, %select.true ], [ %d, %select.false ] | ||||
6923 | // | ||||
6924 | // %cmp should be frozen, otherwise it may introduce undefined behavior. | ||||
6925 | // In addition, we may sink instructions that produce %c or %d from | ||||
6926 | // the entry block into the destination(s) of the new branch. | ||||
6927 | // If the true or false blocks do not contain a sunken instruction, that | ||||
6928 | // block and its branch may be optimized away. In that case, one side of the | ||||
6929 | // first branch will point directly to select.end, and the corresponding PHI | ||||
6930 | // predecessor block will be the start block. | ||||
6931 | |||||
6932 | // First, we split the block containing the select into 2 blocks. | ||||
6933 | BasicBlock *StartBlock = SI->getParent(); | ||||
6934 | BasicBlock::iterator SplitPt = ++(BasicBlock::iterator(LastSI)); | ||||
6935 | BasicBlock *EndBlock = StartBlock->splitBasicBlock(SplitPt, "select.end"); | ||||
6936 | if (IsHugeFunc) | ||||
6937 | FreshBBs.insert(EndBlock); | ||||
6938 | BFI->setBlockFreq(EndBlock, BFI->getBlockFreq(StartBlock).getFrequency()); | ||||
6939 | |||||
6940 | // Delete the unconditional branch that was just created by the split. | ||||
6941 | StartBlock->getTerminator()->eraseFromParent(); | ||||
6942 | |||||
6943 | // These are the new basic blocks for the conditional branch. | ||||
6944 | // At least one will become an actual new basic block. | ||||
6945 | BasicBlock *TrueBlock = nullptr; | ||||
6946 | BasicBlock *FalseBlock = nullptr; | ||||
6947 | BranchInst *TrueBranch = nullptr; | ||||
6948 | BranchInst *FalseBranch = nullptr; | ||||
6949 | |||||
6950 | // Sink expensive instructions into the conditional blocks to avoid executing | ||||
6951 | // them speculatively. | ||||
6952 | for (SelectInst *SI : ASI) { | ||||
6953 | if (sinkSelectOperand(TTI, SI->getTrueValue())) { | ||||
6954 | if (TrueBlock == nullptr) { | ||||
6955 | TrueBlock = BasicBlock::Create(SI->getContext(), "select.true.sink", | ||||
6956 | EndBlock->getParent(), EndBlock); | ||||
6957 | TrueBranch = BranchInst::Create(EndBlock, TrueBlock); | ||||
6958 | if (IsHugeFunc) | ||||
6959 | FreshBBs.insert(TrueBlock); | ||||
6960 | TrueBranch->setDebugLoc(SI->getDebugLoc()); | ||||
6961 | } | ||||
6962 | auto *TrueInst = cast<Instruction>(SI->getTrueValue()); | ||||
6963 | TrueInst->moveBefore(TrueBranch); | ||||
6964 | } | ||||
6965 | if (sinkSelectOperand(TTI, SI->getFalseValue())) { | ||||
6966 | if (FalseBlock == nullptr) { | ||||
6967 | FalseBlock = BasicBlock::Create(SI->getContext(), "select.false.sink", | ||||
6968 | EndBlock->getParent(), EndBlock); | ||||
6969 | if (IsHugeFunc) | ||||
6970 | FreshBBs.insert(FalseBlock); | ||||
6971 | FalseBranch = BranchInst::Create(EndBlock, FalseBlock); | ||||
6972 | FalseBranch->setDebugLoc(SI->getDebugLoc()); | ||||
6973 | } | ||||
6974 | auto *FalseInst = cast<Instruction>(SI->getFalseValue()); | ||||
6975 | FalseInst->moveBefore(FalseBranch); | ||||
6976 | } | ||||
6977 | } | ||||
6978 | |||||
6979 | // If there was nothing to sink, then arbitrarily choose the 'false' side | ||||
6980 | // for a new input value to the PHI. | ||||
6981 | if (TrueBlock == FalseBlock) { | ||||
6982 | assert(TrueBlock == nullptr &&(static_cast <bool> (TrueBlock == nullptr && "Unexpected basic block transform while optimizing select" ) ? void (0) : __assert_fail ("TrueBlock == nullptr && \"Unexpected basic block transform while optimizing select\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6983, __extension__ __PRETTY_FUNCTION__ )) | ||||
6983 | "Unexpected basic block transform while optimizing select")(static_cast <bool> (TrueBlock == nullptr && "Unexpected basic block transform while optimizing select" ) ? void (0) : __assert_fail ("TrueBlock == nullptr && \"Unexpected basic block transform while optimizing select\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 6983, __extension__ __PRETTY_FUNCTION__ )); | ||||
6984 | |||||
6985 | FalseBlock = BasicBlock::Create(SI->getContext(), "select.false", | ||||
6986 | EndBlock->getParent(), EndBlock); | ||||
6987 | if (IsHugeFunc) | ||||
6988 | FreshBBs.insert(FalseBlock); | ||||
6989 | auto *FalseBranch = BranchInst::Create(EndBlock, FalseBlock); | ||||
6990 | FalseBranch->setDebugLoc(SI->getDebugLoc()); | ||||
6991 | } | ||||
6992 | |||||
6993 | // Insert the real conditional branch based on the original condition. | ||||
6994 | // If we did not create a new block for one of the 'true' or 'false' paths | ||||
6995 | // of the condition, it means that side of the branch goes to the end block | ||||
6996 | // directly and the path originates from the start block from the point of | ||||
6997 | // view of the new PHI. | ||||
6998 | BasicBlock *TT, *FT; | ||||
6999 | if (TrueBlock == nullptr) { | ||||
7000 | TT = EndBlock; | ||||
7001 | FT = FalseBlock; | ||||
7002 | TrueBlock = StartBlock; | ||||
7003 | } else if (FalseBlock == nullptr) { | ||||
7004 | TT = TrueBlock; | ||||
7005 | FT = EndBlock; | ||||
7006 | FalseBlock = StartBlock; | ||||
7007 | } else { | ||||
7008 | TT = TrueBlock; | ||||
7009 | FT = FalseBlock; | ||||
7010 | } | ||||
7011 | IRBuilder<> IB(SI); | ||||
7012 | auto *CondFr = IB.CreateFreeze(SI->getCondition(), SI->getName() + ".frozen"); | ||||
7013 | IB.CreateCondBr(CondFr, TT, FT, SI); | ||||
7014 | |||||
7015 | SmallPtrSet<const Instruction *, 2> INS; | ||||
7016 | INS.insert(ASI.begin(), ASI.end()); | ||||
7017 | // Use reverse iterator because later select may use the value of the | ||||
7018 | // earlier select, and we need to propagate value through earlier select | ||||
7019 | // to get the PHI operand. | ||||
7020 | for (SelectInst *SI : llvm::reverse(ASI)) { | ||||
7021 | // The select itself is replaced with a PHI Node. | ||||
7022 | PHINode *PN = PHINode::Create(SI->getType(), 2, "", &EndBlock->front()); | ||||
7023 | PN->takeName(SI); | ||||
7024 | PN->addIncoming(getTrueOrFalseValue(SI, true, INS), TrueBlock); | ||||
7025 | PN->addIncoming(getTrueOrFalseValue(SI, false, INS), FalseBlock); | ||||
7026 | PN->setDebugLoc(SI->getDebugLoc()); | ||||
7027 | |||||
7028 | replaceAllUsesWith(SI, PN, FreshBBs, IsHugeFunc); | ||||
7029 | SI->eraseFromParent(); | ||||
7030 | INS.erase(SI); | ||||
7031 | ++NumSelectsExpanded; | ||||
7032 | } | ||||
7033 | |||||
7034 | // Instruct OptimizeBlock to skip to the next block. | ||||
7035 | CurInstIterator = StartBlock->end(); | ||||
7036 | return true; | ||||
7037 | } | ||||
7038 | |||||
7039 | /// Some targets only accept certain types for splat inputs. For example a VDUP | ||||
7040 | /// in MVE takes a GPR (integer) register, and the instruction that incorporate | ||||
7041 | /// a VDUP (such as a VADD qd, qm, rm) also require a gpr register. | ||||
7042 | bool CodeGenPrepare::optimizeShuffleVectorInst(ShuffleVectorInst *SVI) { | ||||
7043 | // Accept shuf(insertelem(undef/poison, val, 0), undef/poison, <0,0,..>) only | ||||
7044 | if (!match(SVI, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_ZeroInt()), | ||||
7045 | m_Undef(), m_ZeroMask()))) | ||||
7046 | return false; | ||||
7047 | Type *NewType = TLI->shouldConvertSplatType(SVI); | ||||
7048 | if (!NewType) | ||||
7049 | return false; | ||||
7050 | |||||
7051 | auto *SVIVecType = cast<FixedVectorType>(SVI->getType()); | ||||
7052 | assert(!NewType->isVectorTy() && "Expected a scalar type!")(static_cast <bool> (!NewType->isVectorTy() && "Expected a scalar type!") ? void (0) : __assert_fail ("!NewType->isVectorTy() && \"Expected a scalar type!\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7052, __extension__ __PRETTY_FUNCTION__ )); | ||||
7053 | assert(NewType->getScalarSizeInBits() == SVIVecType->getScalarSizeInBits() &&(static_cast <bool> (NewType->getScalarSizeInBits() == SVIVecType->getScalarSizeInBits() && "Expected a type of the same size!" ) ? void (0) : __assert_fail ("NewType->getScalarSizeInBits() == SVIVecType->getScalarSizeInBits() && \"Expected a type of the same size!\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7054, __extension__ __PRETTY_FUNCTION__ )) | ||||
7054 | "Expected a type of the same size!")(static_cast <bool> (NewType->getScalarSizeInBits() == SVIVecType->getScalarSizeInBits() && "Expected a type of the same size!" ) ? void (0) : __assert_fail ("NewType->getScalarSizeInBits() == SVIVecType->getScalarSizeInBits() && \"Expected a type of the same size!\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7054, __extension__ __PRETTY_FUNCTION__ )); | ||||
7055 | auto *NewVecType = | ||||
7056 | FixedVectorType::get(NewType, SVIVecType->getNumElements()); | ||||
7057 | |||||
7058 | // Create a bitcast (shuffle (insert (bitcast(..)))) | ||||
7059 | IRBuilder<> Builder(SVI->getContext()); | ||||
7060 | Builder.SetInsertPoint(SVI); | ||||
7061 | Value *BC1 = Builder.CreateBitCast( | ||||
7062 | cast<Instruction>(SVI->getOperand(0))->getOperand(1), NewType); | ||||
7063 | Value *Shuffle = Builder.CreateVectorSplat(NewVecType->getNumElements(), BC1); | ||||
7064 | Value *BC2 = Builder.CreateBitCast(Shuffle, SVIVecType); | ||||
7065 | |||||
7066 | replaceAllUsesWith(SVI, BC2, FreshBBs, IsHugeFunc); | ||||
7067 | RecursivelyDeleteTriviallyDeadInstructions( | ||||
7068 | SVI, TLInfo, nullptr, | ||||
7069 | [&](Value *V) { removeAllAssertingVHReferences(V); }); | ||||
7070 | |||||
7071 | // Also hoist the bitcast up to its operand if it they are not in the same | ||||
7072 | // block. | ||||
7073 | if (auto *BCI = dyn_cast<Instruction>(BC1)) | ||||
7074 | if (auto *Op = dyn_cast<Instruction>(BCI->getOperand(0))) | ||||
7075 | if (BCI->getParent() != Op->getParent() && !isa<PHINode>(Op) && | ||||
7076 | !Op->isTerminator() && !Op->isEHPad()) | ||||
7077 | BCI->moveAfter(Op); | ||||
7078 | |||||
7079 | return true; | ||||
7080 | } | ||||
7081 | |||||
7082 | bool CodeGenPrepare::tryToSinkFreeOperands(Instruction *I) { | ||||
7083 | // If the operands of I can be folded into a target instruction together with | ||||
7084 | // I, duplicate and sink them. | ||||
7085 | SmallVector<Use *, 4> OpsToSink; | ||||
7086 | if (!TLI->shouldSinkOperands(I, OpsToSink)) | ||||
7087 | return false; | ||||
7088 | |||||
7089 | // OpsToSink can contain multiple uses in a use chain (e.g. | ||||
7090 | // (%u1 with %u1 = shufflevector), (%u2 with %u2 = zext %u1)). The dominating | ||||
7091 | // uses must come first, so we process the ops in reverse order so as to not | ||||
7092 | // create invalid IR. | ||||
7093 | BasicBlock *TargetBB = I->getParent(); | ||||
7094 | bool Changed = false; | ||||
7095 | SmallVector<Use *, 4> ToReplace; | ||||
7096 | Instruction *InsertPoint = I; | ||||
7097 | DenseMap<const Instruction *, unsigned long> InstOrdering; | ||||
7098 | unsigned long InstNumber = 0; | ||||
7099 | for (const auto &I : *TargetBB) | ||||
7100 | InstOrdering[&I] = InstNumber++; | ||||
7101 | |||||
7102 | for (Use *U : reverse(OpsToSink)) { | ||||
7103 | auto *UI = cast<Instruction>(U->get()); | ||||
7104 | if (isa<PHINode>(UI)) | ||||
7105 | continue; | ||||
7106 | if (UI->getParent() == TargetBB) { | ||||
7107 | if (InstOrdering[UI] < InstOrdering[InsertPoint]) | ||||
7108 | InsertPoint = UI; | ||||
7109 | continue; | ||||
7110 | } | ||||
7111 | ToReplace.push_back(U); | ||||
7112 | } | ||||
7113 | |||||
7114 | SetVector<Instruction *> MaybeDead; | ||||
7115 | DenseMap<Instruction *, Instruction *> NewInstructions; | ||||
7116 | for (Use *U : ToReplace) { | ||||
7117 | auto *UI = cast<Instruction>(U->get()); | ||||
7118 | Instruction *NI = UI->clone(); | ||||
7119 | |||||
7120 | if (IsHugeFunc) { | ||||
7121 | // Now we clone an instruction, its operands' defs may sink to this BB | ||||
7122 | // now. So we put the operands defs' BBs into FreshBBs to do optmization. | ||||
7123 | for (unsigned I = 0; I < NI->getNumOperands(); ++I) { | ||||
7124 | auto *OpDef = dyn_cast<Instruction>(NI->getOperand(I)); | ||||
7125 | if (!OpDef) | ||||
7126 | continue; | ||||
7127 | FreshBBs.insert(OpDef->getParent()); | ||||
7128 | } | ||||
7129 | } | ||||
7130 | |||||
7131 | NewInstructions[UI] = NI; | ||||
7132 | MaybeDead.insert(UI); | ||||
7133 | LLVM_DEBUG(dbgs() << "Sinking " << *UI << " to user " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Sinking " << *UI << " to user " << *I << "\n"; } } while (false ); | ||||
7134 | NI->insertBefore(InsertPoint); | ||||
7135 | InsertPoint = NI; | ||||
7136 | InsertedInsts.insert(NI); | ||||
7137 | |||||
7138 | // Update the use for the new instruction, making sure that we update the | ||||
7139 | // sunk instruction uses, if it is part of a chain that has already been | ||||
7140 | // sunk. | ||||
7141 | Instruction *OldI = cast<Instruction>(U->getUser()); | ||||
7142 | if (NewInstructions.count(OldI)) | ||||
7143 | NewInstructions[OldI]->setOperand(U->getOperandNo(), NI); | ||||
7144 | else | ||||
7145 | U->set(NI); | ||||
7146 | Changed = true; | ||||
7147 | } | ||||
7148 | |||||
7149 | // Remove instructions that are dead after sinking. | ||||
7150 | for (auto *I : MaybeDead) { | ||||
7151 | if (!I->hasNUsesOrMore(1)) { | ||||
7152 | LLVM_DEBUG(dbgs() << "Removing dead instruction: " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Removing dead instruction: " << *I << "\n"; } } while (false); | ||||
7153 | I->eraseFromParent(); | ||||
7154 | } | ||||
7155 | } | ||||
7156 | |||||
7157 | return Changed; | ||||
7158 | } | ||||
7159 | |||||
7160 | bool CodeGenPrepare::optimizeSwitchType(SwitchInst *SI) { | ||||
7161 | Value *Cond = SI->getCondition(); | ||||
7162 | Type *OldType = Cond->getType(); | ||||
7163 | LLVMContext &Context = Cond->getContext(); | ||||
7164 | EVT OldVT = TLI->getValueType(*DL, OldType); | ||||
7165 | MVT RegType = TLI->getPreferredSwitchConditionType(Context, OldVT); | ||||
7166 | unsigned RegWidth = RegType.getSizeInBits(); | ||||
7167 | |||||
7168 | if (RegWidth <= cast<IntegerType>(OldType)->getBitWidth()) | ||||
7169 | return false; | ||||
7170 | |||||
7171 | // If the register width is greater than the type width, expand the condition | ||||
7172 | // of the switch instruction and each case constant to the width of the | ||||
7173 | // register. By widening the type of the switch condition, subsequent | ||||
7174 | // comparisons (for case comparisons) will not need to be extended to the | ||||
7175 | // preferred register width, so we will potentially eliminate N-1 extends, | ||||
7176 | // where N is the number of cases in the switch. | ||||
7177 | auto *NewType = Type::getIntNTy(Context, RegWidth); | ||||
7178 | |||||
7179 | // Extend the switch condition and case constants using the target preferred | ||||
7180 | // extend unless the switch condition is a function argument with an extend | ||||
7181 | // attribute. In that case, we can avoid an unnecessary mask/extension by | ||||
7182 | // matching the argument extension instead. | ||||
7183 | Instruction::CastOps ExtType = Instruction::ZExt; | ||||
7184 | // Some targets prefer SExt over ZExt. | ||||
7185 | if (TLI->isSExtCheaperThanZExt(OldVT, RegType)) | ||||
7186 | ExtType = Instruction::SExt; | ||||
7187 | |||||
7188 | if (auto *Arg = dyn_cast<Argument>(Cond)) { | ||||
7189 | if (Arg->hasSExtAttr()) | ||||
7190 | ExtType = Instruction::SExt; | ||||
7191 | if (Arg->hasZExtAttr()) | ||||
7192 | ExtType = Instruction::ZExt; | ||||
7193 | } | ||||
7194 | |||||
7195 | auto *ExtInst = CastInst::Create(ExtType, Cond, NewType); | ||||
7196 | ExtInst->insertBefore(SI); | ||||
7197 | ExtInst->setDebugLoc(SI->getDebugLoc()); | ||||
7198 | SI->setCondition(ExtInst); | ||||
7199 | for (auto Case : SI->cases()) { | ||||
7200 | const APInt &NarrowConst = Case.getCaseValue()->getValue(); | ||||
7201 | APInt WideConst = (ExtType == Instruction::ZExt) | ||||
7202 | ? NarrowConst.zext(RegWidth) | ||||
7203 | : NarrowConst.sext(RegWidth); | ||||
7204 | Case.setValue(ConstantInt::get(Context, WideConst)); | ||||
7205 | } | ||||
7206 | |||||
7207 | return true; | ||||
7208 | } | ||||
7209 | |||||
7210 | bool CodeGenPrepare::optimizeSwitchPhiConstants(SwitchInst *SI) { | ||||
7211 | // The SCCP optimization tends to produce code like this: | ||||
7212 | // switch(x) { case 42: phi(42, ...) } | ||||
7213 | // Materializing the constant for the phi-argument needs instructions; So we | ||||
7214 | // change the code to: | ||||
7215 | // switch(x) { case 42: phi(x, ...) } | ||||
7216 | |||||
7217 | Value *Condition = SI->getCondition(); | ||||
7218 | // Avoid endless loop in degenerate case. | ||||
7219 | if (isa<ConstantInt>(*Condition)) | ||||
7220 | return false; | ||||
7221 | |||||
7222 | bool Changed = false; | ||||
7223 | BasicBlock *SwitchBB = SI->getParent(); | ||||
7224 | Type *ConditionType = Condition->getType(); | ||||
7225 | |||||
7226 | for (const SwitchInst::CaseHandle &Case : SI->cases()) { | ||||
7227 | ConstantInt *CaseValue = Case.getCaseValue(); | ||||
7228 | BasicBlock *CaseBB = Case.getCaseSuccessor(); | ||||
7229 | // Set to true if we previously checked that `CaseBB` is only reached by | ||||
7230 | // a single case from this switch. | ||||
7231 | bool CheckedForSinglePred = false; | ||||
7232 | for (PHINode &PHI : CaseBB->phis()) { | ||||
7233 | Type *PHIType = PHI.getType(); | ||||
7234 | // If ZExt is free then we can also catch patterns like this: | ||||
7235 | // switch((i32)x) { case 42: phi((i64)42, ...); } | ||||
7236 | // and replace `(i64)42` with `zext i32 %x to i64`. | ||||
7237 | bool TryZExt = | ||||
7238 | PHIType->isIntegerTy() && | ||||
7239 | PHIType->getIntegerBitWidth() > ConditionType->getIntegerBitWidth() && | ||||
7240 | TLI->isZExtFree(ConditionType, PHIType); | ||||
7241 | if (PHIType == ConditionType || TryZExt) { | ||||
7242 | // Set to true to skip this case because of multiple preds. | ||||
7243 | bool SkipCase = false; | ||||
7244 | Value *Replacement = nullptr; | ||||
7245 | for (unsigned I = 0, E = PHI.getNumIncomingValues(); I != E; I++) { | ||||
7246 | Value *PHIValue = PHI.getIncomingValue(I); | ||||
7247 | if (PHIValue != CaseValue) { | ||||
7248 | if (!TryZExt) | ||||
7249 | continue; | ||||
7250 | ConstantInt *PHIValueInt = dyn_cast<ConstantInt>(PHIValue); | ||||
7251 | if (!PHIValueInt || | ||||
7252 | PHIValueInt->getValue() != | ||||
7253 | CaseValue->getValue().zext(PHIType->getIntegerBitWidth())) | ||||
7254 | continue; | ||||
7255 | } | ||||
7256 | if (PHI.getIncomingBlock(I) != SwitchBB) | ||||
7257 | continue; | ||||
7258 | // We cannot optimize if there are multiple case labels jumping to | ||||
7259 | // this block. This check may get expensive when there are many | ||||
7260 | // case labels so we test for it last. | ||||
7261 | if (!CheckedForSinglePred) { | ||||
7262 | CheckedForSinglePred = true; | ||||
7263 | if (SI->findCaseDest(CaseBB) == nullptr) { | ||||
7264 | SkipCase = true; | ||||
7265 | break; | ||||
7266 | } | ||||
7267 | } | ||||
7268 | |||||
7269 | if (Replacement == nullptr) { | ||||
7270 | if (PHIValue == CaseValue) { | ||||
7271 | Replacement = Condition; | ||||
7272 | } else { | ||||
7273 | IRBuilder<> Builder(SI); | ||||
7274 | Replacement = Builder.CreateZExt(Condition, PHIType); | ||||
7275 | } | ||||
7276 | } | ||||
7277 | PHI.setIncomingValue(I, Replacement); | ||||
7278 | Changed = true; | ||||
7279 | } | ||||
7280 | if (SkipCase) | ||||
7281 | break; | ||||
7282 | } | ||||
7283 | } | ||||
7284 | } | ||||
7285 | return Changed; | ||||
7286 | } | ||||
7287 | |||||
7288 | bool CodeGenPrepare::optimizeSwitchInst(SwitchInst *SI) { | ||||
7289 | bool Changed = optimizeSwitchType(SI); | ||||
7290 | Changed |= optimizeSwitchPhiConstants(SI); | ||||
7291 | return Changed; | ||||
7292 | } | ||||
7293 | |||||
7294 | namespace { | ||||
7295 | |||||
7296 | /// Helper class to promote a scalar operation to a vector one. | ||||
7297 | /// This class is used to move downward extractelement transition. | ||||
7298 | /// E.g., | ||||
7299 | /// a = vector_op <2 x i32> | ||||
7300 | /// b = extractelement <2 x i32> a, i32 0 | ||||
7301 | /// c = scalar_op b | ||||
7302 | /// store c | ||||
7303 | /// | ||||
7304 | /// => | ||||
7305 | /// a = vector_op <2 x i32> | ||||
7306 | /// c = vector_op a (equivalent to scalar_op on the related lane) | ||||
7307 | /// * d = extractelement <2 x i32> c, i32 0 | ||||
7308 | /// * store d | ||||
7309 | /// Assuming both extractelement and store can be combine, we get rid of the | ||||
7310 | /// transition. | ||||
7311 | class VectorPromoteHelper { | ||||
7312 | /// DataLayout associated with the current module. | ||||
7313 | const DataLayout &DL; | ||||
7314 | |||||
7315 | /// Used to perform some checks on the legality of vector operations. | ||||
7316 | const TargetLowering &TLI; | ||||
7317 | |||||
7318 | /// Used to estimated the cost of the promoted chain. | ||||
7319 | const TargetTransformInfo &TTI; | ||||
7320 | |||||
7321 | /// The transition being moved downwards. | ||||
7322 | Instruction *Transition; | ||||
7323 | |||||
7324 | /// The sequence of instructions to be promoted. | ||||
7325 | SmallVector<Instruction *, 4> InstsToBePromoted; | ||||
7326 | |||||
7327 | /// Cost of combining a store and an extract. | ||||
7328 | unsigned StoreExtractCombineCost; | ||||
7329 | |||||
7330 | /// Instruction that will be combined with the transition. | ||||
7331 | Instruction *CombineInst = nullptr; | ||||
7332 | |||||
7333 | /// The instruction that represents the current end of the transition. | ||||
7334 | /// Since we are faking the promotion until we reach the end of the chain | ||||
7335 | /// of computation, we need a way to get the current end of the transition. | ||||
7336 | Instruction *getEndOfTransition() const { | ||||
7337 | if (InstsToBePromoted.empty()) | ||||
7338 | return Transition; | ||||
7339 | return InstsToBePromoted.back(); | ||||
7340 | } | ||||
7341 | |||||
7342 | /// Return the index of the original value in the transition. | ||||
7343 | /// E.g., for "extractelement <2 x i32> c, i32 1" the original value, | ||||
7344 | /// c, is at index 0. | ||||
7345 | unsigned getTransitionOriginalValueIdx() const { | ||||
7346 | assert(isa<ExtractElementInst>(Transition) &&(static_cast <bool> (isa<ExtractElementInst>(Transition ) && "Other kind of transitions are not supported yet" ) ? void (0) : __assert_fail ("isa<ExtractElementInst>(Transition) && \"Other kind of transitions are not supported yet\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7347, __extension__ __PRETTY_FUNCTION__ )) | ||||
7347 | "Other kind of transitions are not supported yet")(static_cast <bool> (isa<ExtractElementInst>(Transition ) && "Other kind of transitions are not supported yet" ) ? void (0) : __assert_fail ("isa<ExtractElementInst>(Transition) && \"Other kind of transitions are not supported yet\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7347, __extension__ __PRETTY_FUNCTION__ )); | ||||
7348 | return 0; | ||||
7349 | } | ||||
7350 | |||||
7351 | /// Return the index of the index in the transition. | ||||
7352 | /// E.g., for "extractelement <2 x i32> c, i32 0" the index | ||||
7353 | /// is at index 1. | ||||
7354 | unsigned getTransitionIdx() const { | ||||
7355 | assert(isa<ExtractElementInst>(Transition) &&(static_cast <bool> (isa<ExtractElementInst>(Transition ) && "Other kind of transitions are not supported yet" ) ? void (0) : __assert_fail ("isa<ExtractElementInst>(Transition) && \"Other kind of transitions are not supported yet\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7356, __extension__ __PRETTY_FUNCTION__ )) | ||||
7356 | "Other kind of transitions are not supported yet")(static_cast <bool> (isa<ExtractElementInst>(Transition ) && "Other kind of transitions are not supported yet" ) ? void (0) : __assert_fail ("isa<ExtractElementInst>(Transition) && \"Other kind of transitions are not supported yet\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7356, __extension__ __PRETTY_FUNCTION__ )); | ||||
7357 | return 1; | ||||
7358 | } | ||||
7359 | |||||
7360 | /// Get the type of the transition. | ||||
7361 | /// This is the type of the original value. | ||||
7362 | /// E.g., for "extractelement <2 x i32> c, i32 1" the type of the | ||||
7363 | /// transition is <2 x i32>. | ||||
7364 | Type *getTransitionType() const { | ||||
7365 | return Transition->getOperand(getTransitionOriginalValueIdx())->getType(); | ||||
7366 | } | ||||
7367 | |||||
7368 | /// Promote \p ToBePromoted by moving \p Def downward through. | ||||
7369 | /// I.e., we have the following sequence: | ||||
7370 | /// Def = Transition <ty1> a to <ty2> | ||||
7371 | /// b = ToBePromoted <ty2> Def, ... | ||||
7372 | /// => | ||||
7373 | /// b = ToBePromoted <ty1> a, ... | ||||
7374 | /// Def = Transition <ty1> ToBePromoted to <ty2> | ||||
7375 | void promoteImpl(Instruction *ToBePromoted); | ||||
7376 | |||||
7377 | /// Check whether or not it is profitable to promote all the | ||||
7378 | /// instructions enqueued to be promoted. | ||||
7379 | bool isProfitableToPromote() { | ||||
7380 | Value *ValIdx = Transition->getOperand(getTransitionOriginalValueIdx()); | ||||
7381 | unsigned Index = isa<ConstantInt>(ValIdx) | ||||
7382 | ? cast<ConstantInt>(ValIdx)->getZExtValue() | ||||
7383 | : -1; | ||||
7384 | Type *PromotedType = getTransitionType(); | ||||
7385 | |||||
7386 | StoreInst *ST = cast<StoreInst>(CombineInst); | ||||
7387 | unsigned AS = ST->getPointerAddressSpace(); | ||||
7388 | // Check if this store is supported. | ||||
7389 | if (!TLI.allowsMisalignedMemoryAccesses( | ||||
7390 | TLI.getValueType(DL, ST->getValueOperand()->getType()), AS, | ||||
7391 | ST->getAlign())) { | ||||
7392 | // If this is not supported, there is no way we can combine | ||||
7393 | // the extract with the store. | ||||
7394 | return false; | ||||
7395 | } | ||||
7396 | |||||
7397 | // The scalar chain of computation has to pay for the transition | ||||
7398 | // scalar to vector. | ||||
7399 | // The vector chain has to account for the combining cost. | ||||
7400 | enum TargetTransformInfo::TargetCostKind CostKind = | ||||
7401 | TargetTransformInfo::TCK_RecipThroughput; | ||||
7402 | InstructionCost ScalarCost = | ||||
7403 | TTI.getVectorInstrCost(*Transition, PromotedType, CostKind, Index); | ||||
7404 | InstructionCost VectorCost = StoreExtractCombineCost; | ||||
7405 | for (const auto &Inst : InstsToBePromoted) { | ||||
7406 | // Compute the cost. | ||||
7407 | // By construction, all instructions being promoted are arithmetic ones. | ||||
7408 | // Moreover, one argument is a constant that can be viewed as a splat | ||||
7409 | // constant. | ||||
7410 | Value *Arg0 = Inst->getOperand(0); | ||||
7411 | bool IsArg0Constant = isa<UndefValue>(Arg0) || isa<ConstantInt>(Arg0) || | ||||
7412 | isa<ConstantFP>(Arg0); | ||||
7413 | TargetTransformInfo::OperandValueInfo Arg0Info, Arg1Info; | ||||
7414 | if (IsArg0Constant) | ||||
7415 | Arg0Info.Kind = TargetTransformInfo::OK_UniformConstantValue; | ||||
7416 | else | ||||
7417 | Arg1Info.Kind = TargetTransformInfo::OK_UniformConstantValue; | ||||
7418 | |||||
7419 | ScalarCost += TTI.getArithmeticInstrCost( | ||||
7420 | Inst->getOpcode(), Inst->getType(), CostKind, Arg0Info, Arg1Info); | ||||
7421 | VectorCost += TTI.getArithmeticInstrCost(Inst->getOpcode(), PromotedType, | ||||
7422 | CostKind, Arg0Info, Arg1Info); | ||||
7423 | } | ||||
7424 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Estimated cost of computation to be promoted:\nScalar: " << ScalarCost << "\nVector: " << VectorCost << '\n'; } } while (false) | ||||
7425 | dbgs() << "Estimated cost of computation to be promoted:\nScalar: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Estimated cost of computation to be promoted:\nScalar: " << ScalarCost << "\nVector: " << VectorCost << '\n'; } } while (false) | ||||
7426 | << ScalarCost << "\nVector: " << VectorCost << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Estimated cost of computation to be promoted:\nScalar: " << ScalarCost << "\nVector: " << VectorCost << '\n'; } } while (false); | ||||
7427 | return ScalarCost > VectorCost; | ||||
7428 | } | ||||
7429 | |||||
7430 | /// Generate a constant vector with \p Val with the same | ||||
7431 | /// number of elements as the transition. | ||||
7432 | /// \p UseSplat defines whether or not \p Val should be replicated | ||||
7433 | /// across the whole vector. | ||||
7434 | /// In other words, if UseSplat == true, we generate <Val, Val, ..., Val>, | ||||
7435 | /// otherwise we generate a vector with as many undef as possible: | ||||
7436 | /// <undef, ..., undef, Val, undef, ..., undef> where \p Val is only | ||||
7437 | /// used at the index of the extract. | ||||
7438 | Value *getConstantVector(Constant *Val, bool UseSplat) const { | ||||
7439 | unsigned ExtractIdx = std::numeric_limits<unsigned>::max(); | ||||
7440 | if (!UseSplat) { | ||||
7441 | // If we cannot determine where the constant must be, we have to | ||||
7442 | // use a splat constant. | ||||
7443 | Value *ValExtractIdx = Transition->getOperand(getTransitionIdx()); | ||||
7444 | if (ConstantInt *CstVal = dyn_cast<ConstantInt>(ValExtractIdx)) | ||||
7445 | ExtractIdx = CstVal->getSExtValue(); | ||||
7446 | else | ||||
7447 | UseSplat = true; | ||||
7448 | } | ||||
7449 | |||||
7450 | ElementCount EC = cast<VectorType>(getTransitionType())->getElementCount(); | ||||
7451 | if (UseSplat) | ||||
7452 | return ConstantVector::getSplat(EC, Val); | ||||
7453 | |||||
7454 | if (!EC.isScalable()) { | ||||
7455 | SmallVector<Constant *, 4> ConstVec; | ||||
7456 | UndefValue *UndefVal = UndefValue::get(Val->getType()); | ||||
7457 | for (unsigned Idx = 0; Idx != EC.getKnownMinValue(); ++Idx) { | ||||
7458 | if (Idx == ExtractIdx) | ||||
7459 | ConstVec.push_back(Val); | ||||
7460 | else | ||||
7461 | ConstVec.push_back(UndefVal); | ||||
7462 | } | ||||
7463 | return ConstantVector::get(ConstVec); | ||||
7464 | } else | ||||
7465 | llvm_unreachable(::llvm::llvm_unreachable_internal("Generate scalable vector for non-splat is unimplemented" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7466) | ||||
7466 | "Generate scalable vector for non-splat is unimplemented")::llvm::llvm_unreachable_internal("Generate scalable vector for non-splat is unimplemented" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7466); | ||||
7467 | } | ||||
7468 | |||||
7469 | /// Check if promoting to a vector type an operand at \p OperandIdx | ||||
7470 | /// in \p Use can trigger undefined behavior. | ||||
7471 | static bool canCauseUndefinedBehavior(const Instruction *Use, | ||||
7472 | unsigned OperandIdx) { | ||||
7473 | // This is not safe to introduce undef when the operand is on | ||||
7474 | // the right hand side of a division-like instruction. | ||||
7475 | if (OperandIdx != 1) | ||||
7476 | return false; | ||||
7477 | switch (Use->getOpcode()) { | ||||
7478 | default: | ||||
7479 | return false; | ||||
7480 | case Instruction::SDiv: | ||||
7481 | case Instruction::UDiv: | ||||
7482 | case Instruction::SRem: | ||||
7483 | case Instruction::URem: | ||||
7484 | return true; | ||||
7485 | case Instruction::FDiv: | ||||
7486 | case Instruction::FRem: | ||||
7487 | return !Use->hasNoNaNs(); | ||||
7488 | } | ||||
7489 | llvm_unreachable(nullptr)::llvm::llvm_unreachable_internal(nullptr, "llvm/lib/CodeGen/CodeGenPrepare.cpp" , 7489); | ||||
7490 | } | ||||
7491 | |||||
7492 | public: | ||||
7493 | VectorPromoteHelper(const DataLayout &DL, const TargetLowering &TLI, | ||||
7494 | const TargetTransformInfo &TTI, Instruction *Transition, | ||||
7495 | unsigned CombineCost) | ||||
7496 | : DL(DL), TLI(TLI), TTI(TTI), Transition(Transition), | ||||
7497 | StoreExtractCombineCost(CombineCost) { | ||||
7498 | assert(Transition && "Do not know how to promote null")(static_cast <bool> (Transition && "Do not know how to promote null" ) ? void (0) : __assert_fail ("Transition && \"Do not know how to promote null\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7498, __extension__ __PRETTY_FUNCTION__ )); | ||||
7499 | } | ||||
7500 | |||||
7501 | /// Check if we can promote \p ToBePromoted to \p Type. | ||||
7502 | bool canPromote(const Instruction *ToBePromoted) const { | ||||
7503 | // We could support CastInst too. | ||||
7504 | return isa<BinaryOperator>(ToBePromoted); | ||||
7505 | } | ||||
7506 | |||||
7507 | /// Check if it is profitable to promote \p ToBePromoted | ||||
7508 | /// by moving downward the transition through. | ||||
7509 | bool shouldPromote(const Instruction *ToBePromoted) const { | ||||
7510 | // Promote only if all the operands can be statically expanded. | ||||
7511 | // Indeed, we do not want to introduce any new kind of transitions. | ||||
7512 | for (const Use &U : ToBePromoted->operands()) { | ||||
7513 | const Value *Val = U.get(); | ||||
7514 | if (Val == getEndOfTransition()) { | ||||
7515 | // If the use is a division and the transition is on the rhs, | ||||
7516 | // we cannot promote the operation, otherwise we may create a | ||||
7517 | // division by zero. | ||||
7518 | if (canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo())) | ||||
7519 | return false; | ||||
7520 | continue; | ||||
7521 | } | ||||
7522 | if (!isa<ConstantInt>(Val) && !isa<UndefValue>(Val) && | ||||
7523 | !isa<ConstantFP>(Val)) | ||||
7524 | return false; | ||||
7525 | } | ||||
7526 | // Check that the resulting operation is legal. | ||||
7527 | int ISDOpcode = TLI.InstructionOpcodeToISD(ToBePromoted->getOpcode()); | ||||
7528 | if (!ISDOpcode) | ||||
7529 | return false; | ||||
7530 | return StressStoreExtract || | ||||
7531 | TLI.isOperationLegalOrCustom( | ||||
7532 | ISDOpcode, TLI.getValueType(DL, getTransitionType(), true)); | ||||
7533 | } | ||||
7534 | |||||
7535 | /// Check whether or not \p Use can be combined | ||||
7536 | /// with the transition. | ||||
7537 | /// I.e., is it possible to do Use(Transition) => AnotherUse? | ||||
7538 | bool canCombine(const Instruction *Use) { return isa<StoreInst>(Use); } | ||||
7539 | |||||
7540 | /// Record \p ToBePromoted as part of the chain to be promoted. | ||||
7541 | void enqueueForPromotion(Instruction *ToBePromoted) { | ||||
7542 | InstsToBePromoted.push_back(ToBePromoted); | ||||
7543 | } | ||||
7544 | |||||
7545 | /// Set the instruction that will be combined with the transition. | ||||
7546 | void recordCombineInstruction(Instruction *ToBeCombined) { | ||||
7547 | assert(canCombine(ToBeCombined) && "Unsupported instruction to combine")(static_cast <bool> (canCombine(ToBeCombined) && "Unsupported instruction to combine") ? void (0) : __assert_fail ("canCombine(ToBeCombined) && \"Unsupported instruction to combine\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7547, __extension__ __PRETTY_FUNCTION__ )); | ||||
7548 | CombineInst = ToBeCombined; | ||||
7549 | } | ||||
7550 | |||||
7551 | /// Promote all the instructions enqueued for promotion if it is | ||||
7552 | /// is profitable. | ||||
7553 | /// \return True if the promotion happened, false otherwise. | ||||
7554 | bool promote() { | ||||
7555 | // Check if there is something to promote. | ||||
7556 | // Right now, if we do not have anything to combine with, | ||||
7557 | // we assume the promotion is not profitable. | ||||
7558 | if (InstsToBePromoted.empty() || !CombineInst) | ||||
7559 | return false; | ||||
7560 | |||||
7561 | // Check cost. | ||||
7562 | if (!StressStoreExtract && !isProfitableToPromote()) | ||||
7563 | return false; | ||||
7564 | |||||
7565 | // Promote. | ||||
7566 | for (auto &ToBePromoted : InstsToBePromoted) | ||||
7567 | promoteImpl(ToBePromoted); | ||||
7568 | InstsToBePromoted.clear(); | ||||
7569 | return true; | ||||
7570 | } | ||||
7571 | }; | ||||
7572 | |||||
7573 | } // end anonymous namespace | ||||
7574 | |||||
7575 | void VectorPromoteHelper::promoteImpl(Instruction *ToBePromoted) { | ||||
7576 | // At this point, we know that all the operands of ToBePromoted but Def | ||||
7577 | // can be statically promoted. | ||||
7578 | // For Def, we need to use its parameter in ToBePromoted: | ||||
7579 | // b = ToBePromoted ty1 a | ||||
7580 | // Def = Transition ty1 b to ty2 | ||||
7581 | // Move the transition down. | ||||
7582 | // 1. Replace all uses of the promoted operation by the transition. | ||||
7583 | // = ... b => = ... Def. | ||||
7584 | assert(ToBePromoted->getType() == Transition->getType() &&(static_cast <bool> (ToBePromoted->getType() == Transition ->getType() && "The type of the result of the transition does not match " "the final type") ? void (0) : __assert_fail ("ToBePromoted->getType() == Transition->getType() && \"The type of the result of the transition does not match \" \"the final type\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7586, __extension__ __PRETTY_FUNCTION__ )) | ||||
7585 | "The type of the result of the transition does not match "(static_cast <bool> (ToBePromoted->getType() == Transition ->getType() && "The type of the result of the transition does not match " "the final type") ? void (0) : __assert_fail ("ToBePromoted->getType() == Transition->getType() && \"The type of the result of the transition does not match \" \"the final type\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7586, __extension__ __PRETTY_FUNCTION__ )) | ||||
7586 | "the final type")(static_cast <bool> (ToBePromoted->getType() == Transition ->getType() && "The type of the result of the transition does not match " "the final type") ? void (0) : __assert_fail ("ToBePromoted->getType() == Transition->getType() && \"The type of the result of the transition does not match \" \"the final type\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7586, __extension__ __PRETTY_FUNCTION__ )); | ||||
7587 | ToBePromoted->replaceAllUsesWith(Transition); | ||||
7588 | // 2. Update the type of the uses. | ||||
7589 | // b = ToBePromoted ty2 Def => b = ToBePromoted ty1 Def. | ||||
7590 | Type *TransitionTy = getTransitionType(); | ||||
7591 | ToBePromoted->mutateType(TransitionTy); | ||||
7592 | // 3. Update all the operands of the promoted operation with promoted | ||||
7593 | // operands. | ||||
7594 | // b = ToBePromoted ty1 Def => b = ToBePromoted ty1 a. | ||||
7595 | for (Use &U : ToBePromoted->operands()) { | ||||
7596 | Value *Val = U.get(); | ||||
7597 | Value *NewVal = nullptr; | ||||
7598 | if (Val == Transition) | ||||
7599 | NewVal = Transition->getOperand(getTransitionOriginalValueIdx()); | ||||
7600 | else if (isa<UndefValue>(Val) || isa<ConstantInt>(Val) || | ||||
7601 | isa<ConstantFP>(Val)) { | ||||
7602 | // Use a splat constant if it is not safe to use undef. | ||||
7603 | NewVal = getConstantVector( | ||||
7604 | cast<Constant>(Val), | ||||
7605 | isa<UndefValue>(Val) || | ||||
7606 | canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo())); | ||||
7607 | } else | ||||
7608 | llvm_unreachable("Did you modified shouldPromote and forgot to update "::llvm::llvm_unreachable_internal("Did you modified shouldPromote and forgot to update " "this?", "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7609) | ||||
7609 | "this?")::llvm::llvm_unreachable_internal("Did you modified shouldPromote and forgot to update " "this?", "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7609); | ||||
7610 | ToBePromoted->setOperand(U.getOperandNo(), NewVal); | ||||
7611 | } | ||||
7612 | Transition->moveAfter(ToBePromoted); | ||||
7613 | Transition->setOperand(getTransitionOriginalValueIdx(), ToBePromoted); | ||||
7614 | } | ||||
7615 | |||||
7616 | /// Some targets can do store(extractelement) with one instruction. | ||||
7617 | /// Try to push the extractelement towards the stores when the target | ||||
7618 | /// has this feature and this is profitable. | ||||
7619 | bool CodeGenPrepare::optimizeExtractElementInst(Instruction *Inst) { | ||||
7620 | unsigned CombineCost = std::numeric_limits<unsigned>::max(); | ||||
7621 | if (DisableStoreExtract || | ||||
7622 | (!StressStoreExtract && | ||||
7623 | !TLI->canCombineStoreAndExtract(Inst->getOperand(0)->getType(), | ||||
7624 | Inst->getOperand(1), CombineCost))) | ||||
7625 | return false; | ||||
7626 | |||||
7627 | // At this point we know that Inst is a vector to scalar transition. | ||||
7628 | // Try to move it down the def-use chain, until: | ||||
7629 | // - We can combine the transition with its single use | ||||
7630 | // => we got rid of the transition. | ||||
7631 | // - We escape the current basic block | ||||
7632 | // => we would need to check that we are moving it at a cheaper place and | ||||
7633 | // we do not do that for now. | ||||
7634 | BasicBlock *Parent = Inst->getParent(); | ||||
7635 | LLVM_DEBUG(dbgs() << "Found an interesting transition: " << *Inst << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Found an interesting transition: " << *Inst << '\n'; } } while (false); | ||||
7636 | VectorPromoteHelper VPH(*DL, *TLI, *TTI, Inst, CombineCost); | ||||
7637 | // If the transition has more than one use, assume this is not going to be | ||||
7638 | // beneficial. | ||||
7639 | while (Inst->hasOneUse()) { | ||||
7640 | Instruction *ToBePromoted = cast<Instruction>(*Inst->user_begin()); | ||||
7641 | LLVM_DEBUG(dbgs() << "Use: " << *ToBePromoted << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Use: " << *ToBePromoted << '\n'; } } while (false); | ||||
7642 | |||||
7643 | if (ToBePromoted->getParent() != Parent) { | ||||
7644 | LLVM_DEBUG(dbgs() << "Instruction to promote is in a different block ("do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Instruction to promote is in a different block (" << ToBePromoted->getParent()->getName() << ") than the transition (" << Parent->getName() << ").\n"; } } while (false) | ||||
7645 | << ToBePromoted->getParent()->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Instruction to promote is in a different block (" << ToBePromoted->getParent()->getName() << ") than the transition (" << Parent->getName() << ").\n"; } } while (false) | ||||
7646 | << ") than the transition (" << Parent->getName()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Instruction to promote is in a different block (" << ToBePromoted->getParent()->getName() << ") than the transition (" << Parent->getName() << ").\n"; } } while (false) | ||||
7647 | << ").\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Instruction to promote is in a different block (" << ToBePromoted->getParent()->getName() << ") than the transition (" << Parent->getName() << ").\n"; } } while (false); | ||||
7648 | return false; | ||||
7649 | } | ||||
7650 | |||||
7651 | if (VPH.canCombine(ToBePromoted)) { | ||||
7652 | LLVM_DEBUG(dbgs() << "Assume " << *Inst << '\n'do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Assume " << *Inst << '\n' << "will be combined with: " << *ToBePromoted << '\n'; } } while (false) | ||||
7653 | << "will be combined with: " << *ToBePromoted << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Assume " << *Inst << '\n' << "will be combined with: " << *ToBePromoted << '\n'; } } while (false); | ||||
7654 | VPH.recordCombineInstruction(ToBePromoted); | ||||
7655 | bool Changed = VPH.promote(); | ||||
7656 | NumStoreExtractExposed += Changed; | ||||
7657 | return Changed; | ||||
7658 | } | ||||
7659 | |||||
7660 | LLVM_DEBUG(dbgs() << "Try promoting.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Try promoting.\n"; } } while (false); | ||||
7661 | if (!VPH.canPromote(ToBePromoted) || !VPH.shouldPromote(ToBePromoted)) | ||||
7662 | return false; | ||||
7663 | |||||
7664 | LLVM_DEBUG(dbgs() << "Promoting is possible... Enqueue for promotion!\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Promoting is possible... Enqueue for promotion!\n" ; } } while (false); | ||||
7665 | |||||
7666 | VPH.enqueueForPromotion(ToBePromoted); | ||||
7667 | Inst = ToBePromoted; | ||||
7668 | } | ||||
7669 | return false; | ||||
7670 | } | ||||
7671 | |||||
7672 | /// For the instruction sequence of store below, F and I values | ||||
7673 | /// are bundled together as an i64 value before being stored into memory. | ||||
7674 | /// Sometimes it is more efficient to generate separate stores for F and I, | ||||
7675 | /// which can remove the bitwise instructions or sink them to colder places. | ||||
7676 | /// | ||||
7677 | /// (store (or (zext (bitcast F to i32) to i64), | ||||
7678 | /// (shl (zext I to i64), 32)), addr) --> | ||||
7679 | /// (store F, addr) and (store I, addr+4) | ||||
7680 | /// | ||||
7681 | /// Similarly, splitting for other merged store can also be beneficial, like: | ||||
7682 | /// For pair of {i32, i32}, i64 store --> two i32 stores. | ||||
7683 | /// For pair of {i32, i16}, i64 store --> two i32 stores. | ||||
7684 | /// For pair of {i16, i16}, i32 store --> two i16 stores. | ||||
7685 | /// For pair of {i16, i8}, i32 store --> two i16 stores. | ||||
7686 | /// For pair of {i8, i8}, i16 store --> two i8 stores. | ||||
7687 | /// | ||||
7688 | /// We allow each target to determine specifically which kind of splitting is | ||||
7689 | /// supported. | ||||
7690 | /// | ||||
7691 | /// The store patterns are commonly seen from the simple code snippet below | ||||
7692 | /// if only std::make_pair(...) is sroa transformed before inlined into hoo. | ||||
7693 | /// void goo(const std::pair<int, float> &); | ||||
7694 | /// hoo() { | ||||
7695 | /// ... | ||||
7696 | /// goo(std::make_pair(tmp, ftmp)); | ||||
7697 | /// ... | ||||
7698 | /// } | ||||
7699 | /// | ||||
7700 | /// Although we already have similar splitting in DAG Combine, we duplicate | ||||
7701 | /// it in CodeGenPrepare to catch the case in which pattern is across | ||||
7702 | /// multiple BBs. The logic in DAG Combine is kept to catch case generated | ||||
7703 | /// during code expansion. | ||||
7704 | static bool splitMergedValStore(StoreInst &SI, const DataLayout &DL, | ||||
7705 | const TargetLowering &TLI) { | ||||
7706 | // Handle simple but common cases only. | ||||
7707 | Type *StoreType = SI.getValueOperand()->getType(); | ||||
7708 | |||||
7709 | // The code below assumes shifting a value by <number of bits>, | ||||
7710 | // whereas scalable vectors would have to be shifted by | ||||
7711 | // <2log(vscale) + number of bits> in order to store the | ||||
7712 | // low/high parts. Bailing out for now. | ||||
7713 | if (StoreType->isScalableTy()) | ||||
7714 | return false; | ||||
7715 | |||||
7716 | if (!DL.typeSizeEqualsStoreSize(StoreType) || | ||||
7717 | DL.getTypeSizeInBits(StoreType) == 0) | ||||
7718 | return false; | ||||
7719 | |||||
7720 | unsigned HalfValBitSize = DL.getTypeSizeInBits(StoreType) / 2; | ||||
7721 | Type *SplitStoreType = Type::getIntNTy(SI.getContext(), HalfValBitSize); | ||||
7722 | if (!DL.typeSizeEqualsStoreSize(SplitStoreType)) | ||||
7723 | return false; | ||||
7724 | |||||
7725 | // Don't split the store if it is volatile. | ||||
7726 | if (SI.isVolatile()) | ||||
7727 | return false; | ||||
7728 | |||||
7729 | // Match the following patterns: | ||||
7730 | // (store (or (zext LValue to i64), | ||||
7731 | // (shl (zext HValue to i64), 32)), HalfValBitSize) | ||||
7732 | // or | ||||
7733 | // (store (or (shl (zext HValue to i64), 32)), HalfValBitSize) | ||||
7734 | // (zext LValue to i64), | ||||
7735 | // Expect both operands of OR and the first operand of SHL have only | ||||
7736 | // one use. | ||||
7737 | Value *LValue, *HValue; | ||||
7738 | if (!match(SI.getValueOperand(), | ||||
7739 | m_c_Or(m_OneUse(m_ZExt(m_Value(LValue))), | ||||
7740 | m_OneUse(m_Shl(m_OneUse(m_ZExt(m_Value(HValue))), | ||||
7741 | m_SpecificInt(HalfValBitSize)))))) | ||||
7742 | return false; | ||||
7743 | |||||
7744 | // Check LValue and HValue are int with size less or equal than 32. | ||||
7745 | if (!LValue->getType()->isIntegerTy() || | ||||
7746 | DL.getTypeSizeInBits(LValue->getType()) > HalfValBitSize || | ||||
7747 | !HValue->getType()->isIntegerTy() || | ||||
7748 | DL.getTypeSizeInBits(HValue->getType()) > HalfValBitSize) | ||||
7749 | return false; | ||||
7750 | |||||
7751 | // If LValue/HValue is a bitcast instruction, use the EVT before bitcast | ||||
7752 | // as the input of target query. | ||||
7753 | auto *LBC = dyn_cast<BitCastInst>(LValue); | ||||
7754 | auto *HBC = dyn_cast<BitCastInst>(HValue); | ||||
7755 | EVT LowTy = LBC ? EVT::getEVT(LBC->getOperand(0)->getType()) | ||||
7756 | : EVT::getEVT(LValue->getType()); | ||||
7757 | EVT HighTy = HBC ? EVT::getEVT(HBC->getOperand(0)->getType()) | ||||
7758 | : EVT::getEVT(HValue->getType()); | ||||
7759 | if (!ForceSplitStore && !TLI.isMultiStoresCheaperThanBitsMerge(LowTy, HighTy)) | ||||
7760 | return false; | ||||
7761 | |||||
7762 | // Start to split store. | ||||
7763 | IRBuilder<> Builder(SI.getContext()); | ||||
7764 | Builder.SetInsertPoint(&SI); | ||||
7765 | |||||
7766 | // If LValue/HValue is a bitcast in another BB, create a new one in current | ||||
7767 | // BB so it may be merged with the splitted stores by dag combiner. | ||||
7768 | if (LBC && LBC->getParent() != SI.getParent()) | ||||
7769 | LValue = Builder.CreateBitCast(LBC->getOperand(0), LBC->getType()); | ||||
7770 | if (HBC && HBC->getParent() != SI.getParent()) | ||||
7771 | HValue = Builder.CreateBitCast(HBC->getOperand(0), HBC->getType()); | ||||
7772 | |||||
7773 | bool IsLE = SI.getModule()->getDataLayout().isLittleEndian(); | ||||
7774 | auto CreateSplitStore = [&](Value *V, bool Upper) { | ||||
7775 | V = Builder.CreateZExtOrBitCast(V, SplitStoreType); | ||||
7776 | Value *Addr = Builder.CreateBitCast( | ||||
7777 | SI.getOperand(1), | ||||
7778 | SplitStoreType->getPointerTo(SI.getPointerAddressSpace())); | ||||
7779 | Align Alignment = SI.getAlign(); | ||||
7780 | const bool IsOffsetStore = (IsLE && Upper) || (!IsLE && !Upper); | ||||
7781 | if (IsOffsetStore) { | ||||
7782 | Addr = Builder.CreateGEP( | ||||
7783 | SplitStoreType, Addr, | ||||
7784 | ConstantInt::get(Type::getInt32Ty(SI.getContext()), 1)); | ||||
7785 | |||||
7786 | // When splitting the store in half, naturally one half will retain the | ||||
7787 | // alignment of the original wider store, regardless of whether it was | ||||
7788 | // over-aligned or not, while the other will require adjustment. | ||||
7789 | Alignment = commonAlignment(Alignment, HalfValBitSize / 8); | ||||
7790 | } | ||||
7791 | Builder.CreateAlignedStore(V, Addr, Alignment); | ||||
7792 | }; | ||||
7793 | |||||
7794 | CreateSplitStore(LValue, false); | ||||
7795 | CreateSplitStore(HValue, true); | ||||
7796 | |||||
7797 | // Delete the old store. | ||||
7798 | SI.eraseFromParent(); | ||||
7799 | return true; | ||||
7800 | } | ||||
7801 | |||||
7802 | // Return true if the GEP has two operands, the first operand is of a sequential | ||||
7803 | // type, and the second operand is a constant. | ||||
7804 | static bool GEPSequentialConstIndexed(GetElementPtrInst *GEP) { | ||||
7805 | gep_type_iterator I = gep_type_begin(*GEP); | ||||
7806 | return GEP->getNumOperands() == 2 && I.isSequential() && | ||||
7807 | isa<ConstantInt>(GEP->getOperand(1)); | ||||
7808 | } | ||||
7809 | |||||
7810 | // Try unmerging GEPs to reduce liveness interference (register pressure) across | ||||
7811 | // IndirectBr edges. Since IndirectBr edges tend to touch on many blocks, | ||||
7812 | // reducing liveness interference across those edges benefits global register | ||||
7813 | // allocation. Currently handles only certain cases. | ||||
7814 | // | ||||
7815 | // For example, unmerge %GEPI and %UGEPI as below. | ||||
7816 | // | ||||
7817 | // ---------- BEFORE ---------- | ||||
7818 | // SrcBlock: | ||||
7819 | // ... | ||||
7820 | // %GEPIOp = ... | ||||
7821 | // ... | ||||
7822 | // %GEPI = gep %GEPIOp, Idx | ||||
7823 | // ... | ||||
7824 | // indirectbr ... [ label %DstB0, label %DstB1, ... label %DstBi ... ] | ||||
7825 | // (* %GEPI is alive on the indirectbr edges due to other uses ahead) | ||||
7826 | // (* %GEPIOp is alive on the indirectbr edges only because of it's used by | ||||
7827 | // %UGEPI) | ||||
7828 | // | ||||
7829 | // DstB0: ... (there may be a gep similar to %UGEPI to be unmerged) | ||||
7830 | // DstB1: ... (there may be a gep similar to %UGEPI to be unmerged) | ||||
7831 | // ... | ||||
7832 | // | ||||
7833 | // DstBi: | ||||
7834 | // ... | ||||
7835 | // %UGEPI = gep %GEPIOp, UIdx | ||||
7836 | // ... | ||||
7837 | // --------------------------- | ||||
7838 | // | ||||
7839 | // ---------- AFTER ---------- | ||||
7840 | // SrcBlock: | ||||
7841 | // ... (same as above) | ||||
7842 | // (* %GEPI is still alive on the indirectbr edges) | ||||
7843 | // (* %GEPIOp is no longer alive on the indirectbr edges as a result of the | ||||
7844 | // unmerging) | ||||
7845 | // ... | ||||
7846 | // | ||||
7847 | // DstBi: | ||||
7848 | // ... | ||||
7849 | // %UGEPI = gep %GEPI, (UIdx-Idx) | ||||
7850 | // ... | ||||
7851 | // --------------------------- | ||||
7852 | // | ||||
7853 | // The register pressure on the IndirectBr edges is reduced because %GEPIOp is | ||||
7854 | // no longer alive on them. | ||||
7855 | // | ||||
7856 | // We try to unmerge GEPs here in CodGenPrepare, as opposed to limiting merging | ||||
7857 | // of GEPs in the first place in InstCombiner::visitGetElementPtrInst() so as | ||||
7858 | // not to disable further simplications and optimizations as a result of GEP | ||||
7859 | // merging. | ||||
7860 | // | ||||
7861 | // Note this unmerging may increase the length of the data flow critical path | ||||
7862 | // (the path from %GEPIOp to %UGEPI would go through %GEPI), which is a tradeoff | ||||
7863 | // between the register pressure and the length of data-flow critical | ||||
7864 | // path. Restricting this to the uncommon IndirectBr case would minimize the | ||||
7865 | // impact of potentially longer critical path, if any, and the impact on compile | ||||
7866 | // time. | ||||
7867 | static bool tryUnmergingGEPsAcrossIndirectBr(GetElementPtrInst *GEPI, | ||||
7868 | const TargetTransformInfo *TTI) { | ||||
7869 | BasicBlock *SrcBlock = GEPI->getParent(); | ||||
7870 | // Check that SrcBlock ends with an IndirectBr. If not, give up. The common | ||||
7871 | // (non-IndirectBr) cases exit early here. | ||||
7872 | if (!isa<IndirectBrInst>(SrcBlock->getTerminator())) | ||||
7873 | return false; | ||||
7874 | // Check that GEPI is a simple gep with a single constant index. | ||||
7875 | if (!GEPSequentialConstIndexed(GEPI)) | ||||
7876 | return false; | ||||
7877 | ConstantInt *GEPIIdx = cast<ConstantInt>(GEPI->getOperand(1)); | ||||
7878 | // Check that GEPI is a cheap one. | ||||
7879 | if (TTI->getIntImmCost(GEPIIdx->getValue(), GEPIIdx->getType(), | ||||
7880 | TargetTransformInfo::TCK_SizeAndLatency) > | ||||
7881 | TargetTransformInfo::TCC_Basic) | ||||
7882 | return false; | ||||
7883 | Value *GEPIOp = GEPI->getOperand(0); | ||||
7884 | // Check that GEPIOp is an instruction that's also defined in SrcBlock. | ||||
7885 | if (!isa<Instruction>(GEPIOp)) | ||||
7886 | return false; | ||||
7887 | auto *GEPIOpI = cast<Instruction>(GEPIOp); | ||||
7888 | if (GEPIOpI->getParent() != SrcBlock) | ||||
7889 | return false; | ||||
7890 | // Check that GEP is used outside the block, meaning it's alive on the | ||||
7891 | // IndirectBr edge(s). | ||||
7892 | if (llvm::none_of(GEPI->users(), [&](User *Usr) { | ||||
7893 | if (auto *I = dyn_cast<Instruction>(Usr)) { | ||||
7894 | if (I->getParent() != SrcBlock) { | ||||
7895 | return true; | ||||
7896 | } | ||||
7897 | } | ||||
7898 | return false; | ||||
7899 | })) | ||||
7900 | return false; | ||||
7901 | // The second elements of the GEP chains to be unmerged. | ||||
7902 | std::vector<GetElementPtrInst *> UGEPIs; | ||||
7903 | // Check each user of GEPIOp to check if unmerging would make GEPIOp not alive | ||||
7904 | // on IndirectBr edges. | ||||
7905 | for (User *Usr : GEPIOp->users()) { | ||||
7906 | if (Usr == GEPI) | ||||
7907 | continue; | ||||
7908 | // Check if Usr is an Instruction. If not, give up. | ||||
7909 | if (!isa<Instruction>(Usr)) | ||||
7910 | return false; | ||||
7911 | auto *UI = cast<Instruction>(Usr); | ||||
7912 | // Check if Usr in the same block as GEPIOp, which is fine, skip. | ||||
7913 | if (UI->getParent() == SrcBlock) | ||||
7914 | continue; | ||||
7915 | // Check if Usr is a GEP. If not, give up. | ||||
7916 | if (!isa<GetElementPtrInst>(Usr)) | ||||
7917 | return false; | ||||
7918 | auto *UGEPI = cast<GetElementPtrInst>(Usr); | ||||
7919 | // Check if UGEPI is a simple gep with a single constant index and GEPIOp is | ||||
7920 | // the pointer operand to it. If so, record it in the vector. If not, give | ||||
7921 | // up. | ||||
7922 | if (!GEPSequentialConstIndexed(UGEPI)) | ||||
7923 | return false; | ||||
7924 | if (UGEPI->getOperand(0) != GEPIOp) | ||||
7925 | return false; | ||||
7926 | if (GEPIIdx->getType() != | ||||
7927 | cast<ConstantInt>(UGEPI->getOperand(1))->getType()) | ||||
7928 | return false; | ||||
7929 | ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1)); | ||||
7930 | if (TTI->getIntImmCost(UGEPIIdx->getValue(), UGEPIIdx->getType(), | ||||
7931 | TargetTransformInfo::TCK_SizeAndLatency) > | ||||
7932 | TargetTransformInfo::TCC_Basic) | ||||
7933 | return false; | ||||
7934 | UGEPIs.push_back(UGEPI); | ||||
7935 | } | ||||
7936 | if (UGEPIs.size() == 0) | ||||
7937 | return false; | ||||
7938 | // Check the materializing cost of (Uidx-Idx). | ||||
7939 | for (GetElementPtrInst *UGEPI : UGEPIs) { | ||||
7940 | ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1)); | ||||
7941 | APInt NewIdx = UGEPIIdx->getValue() - GEPIIdx->getValue(); | ||||
7942 | InstructionCost ImmCost = TTI->getIntImmCost( | ||||
7943 | NewIdx, GEPIIdx->getType(), TargetTransformInfo::TCK_SizeAndLatency); | ||||
7944 | if (ImmCost > TargetTransformInfo::TCC_Basic) | ||||
7945 | return false; | ||||
7946 | } | ||||
7947 | // Now unmerge between GEPI and UGEPIs. | ||||
7948 | for (GetElementPtrInst *UGEPI : UGEPIs) { | ||||
7949 | UGEPI->setOperand(0, GEPI); | ||||
7950 | ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1)); | ||||
7951 | Constant *NewUGEPIIdx = ConstantInt::get( | ||||
7952 | GEPIIdx->getType(), UGEPIIdx->getValue() - GEPIIdx->getValue()); | ||||
7953 | UGEPI->setOperand(1, NewUGEPIIdx); | ||||
7954 | // If GEPI is not inbounds but UGEPI is inbounds, change UGEPI to not | ||||
7955 | // inbounds to avoid UB. | ||||
7956 | if (!GEPI->isInBounds()) { | ||||
7957 | UGEPI->setIsInBounds(false); | ||||
7958 | } | ||||
7959 | } | ||||
7960 | // After unmerging, verify that GEPIOp is actually only used in SrcBlock (not | ||||
7961 | // alive on IndirectBr edges). | ||||
7962 | assert(llvm::none_of(GEPIOp->users(),(static_cast <bool> (llvm::none_of(GEPIOp->users(), [ &](User *Usr) { return cast<Instruction>(Usr)->getParent () != SrcBlock; }) && "GEPIOp is used outside SrcBlock" ) ? void (0) : __assert_fail ("llvm::none_of(GEPIOp->users(), [&](User *Usr) { return cast<Instruction>(Usr)->getParent() != SrcBlock; }) && \"GEPIOp is used outside SrcBlock\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7966, __extension__ __PRETTY_FUNCTION__ )) | ||||
7963 | [&](User *Usr) {(static_cast <bool> (llvm::none_of(GEPIOp->users(), [ &](User *Usr) { return cast<Instruction>(Usr)->getParent () != SrcBlock; }) && "GEPIOp is used outside SrcBlock" ) ? void (0) : __assert_fail ("llvm::none_of(GEPIOp->users(), [&](User *Usr) { return cast<Instruction>(Usr)->getParent() != SrcBlock; }) && \"GEPIOp is used outside SrcBlock\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7966, __extension__ __PRETTY_FUNCTION__ )) | ||||
7964 | return cast<Instruction>(Usr)->getParent() != SrcBlock;(static_cast <bool> (llvm::none_of(GEPIOp->users(), [ &](User *Usr) { return cast<Instruction>(Usr)->getParent () != SrcBlock; }) && "GEPIOp is used outside SrcBlock" ) ? void (0) : __assert_fail ("llvm::none_of(GEPIOp->users(), [&](User *Usr) { return cast<Instruction>(Usr)->getParent() != SrcBlock; }) && \"GEPIOp is used outside SrcBlock\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7966, __extension__ __PRETTY_FUNCTION__ )) | ||||
7965 | }) &&(static_cast <bool> (llvm::none_of(GEPIOp->users(), [ &](User *Usr) { return cast<Instruction>(Usr)->getParent () != SrcBlock; }) && "GEPIOp is used outside SrcBlock" ) ? void (0) : __assert_fail ("llvm::none_of(GEPIOp->users(), [&](User *Usr) { return cast<Instruction>(Usr)->getParent() != SrcBlock; }) && \"GEPIOp is used outside SrcBlock\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7966, __extension__ __PRETTY_FUNCTION__ )) | ||||
7966 | "GEPIOp is used outside SrcBlock")(static_cast <bool> (llvm::none_of(GEPIOp->users(), [ &](User *Usr) { return cast<Instruction>(Usr)->getParent () != SrcBlock; }) && "GEPIOp is used outside SrcBlock" ) ? void (0) : __assert_fail ("llvm::none_of(GEPIOp->users(), [&](User *Usr) { return cast<Instruction>(Usr)->getParent() != SrcBlock; }) && \"GEPIOp is used outside SrcBlock\"" , "llvm/lib/CodeGen/CodeGenPrepare.cpp", 7966, __extension__ __PRETTY_FUNCTION__ )); | ||||
7967 | return true; | ||||
7968 | } | ||||
7969 | |||||
7970 | static bool optimizeBranch(BranchInst *Branch, const TargetLowering &TLI, | ||||
7971 | SmallSet<BasicBlock *, 32> &FreshBBs, | ||||
7972 | bool IsHugeFunc) { | ||||
7973 | // Try and convert | ||||
7974 | // %c = icmp ult %x, 8 | ||||
7975 | // br %c, bla, blb | ||||
7976 | // %tc = lshr %x, 3 | ||||
7977 | // to | ||||
7978 | // %tc = lshr %x, 3 | ||||
7979 | // %c = icmp eq %tc, 0 | ||||
7980 | // br %c, bla, blb | ||||
7981 | // Creating the cmp to zero can be better for the backend, especially if the | ||||
7982 | // lshr produces flags that can be used automatically. | ||||
7983 | if (!TLI.preferZeroCompareBranch() || !Branch->isConditional()) | ||||
7984 | return false; | ||||
7985 | |||||
7986 | ICmpInst *Cmp = dyn_cast<ICmpInst>(Branch->getCondition()); | ||||
7987 | if (!Cmp || !isa<ConstantInt>(Cmp->getOperand(1)) || !Cmp->hasOneUse()) | ||||
7988 | return false; | ||||
7989 | |||||
7990 | Value *X = Cmp->getOperand(0); | ||||
7991 | APInt CmpC = cast<ConstantInt>(Cmp->getOperand(1))->getValue(); | ||||
7992 | |||||
7993 | for (auto *U : X->users()) { | ||||
7994 | Instruction *UI = dyn_cast<Instruction>(U); | ||||
7995 | // A quick dominance check | ||||
7996 | if (!UI || | ||||
7997 | (UI->getParent() != Branch->getParent() && | ||||
7998 | UI->getParent() != Branch->getSuccessor(0) && | ||||
7999 | UI->getParent() != Branch->getSuccessor(1)) || | ||||
8000 | (UI->getParent() != Branch->getParent() && | ||||
8001 | !UI->getParent()->getSinglePredecessor())) | ||||
8002 | continue; | ||||
8003 | |||||
8004 | if (CmpC.isPowerOf2() && Cmp->getPredicate() == ICmpInst::ICMP_ULT && | ||||
8005 | match(UI, m_Shr(m_Specific(X), m_SpecificInt(CmpC.logBase2())))) { | ||||
8006 | IRBuilder<> Builder(Branch); | ||||
8007 | if (UI->getParent() != Branch->getParent()) | ||||
8008 | UI->moveBefore(Branch); | ||||
8009 | Value *NewCmp = Builder.CreateCmp(ICmpInst::ICMP_EQ, UI, | ||||
8010 | ConstantInt::get(UI->getType(), 0)); | ||||
8011 | LLVM_DEBUG(dbgs() << "Converting " << *Cmp << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Converting " << * Cmp << "\n"; } } while (false); | ||||
8012 | LLVM_DEBUG(dbgs() << " to compare on zero: " << *NewCmp << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << " to compare on zero: " << *NewCmp << "\n"; } } while (false); | ||||
8013 | replaceAllUsesWith(Cmp, NewCmp, FreshBBs, IsHugeFunc); | ||||
8014 | return true; | ||||
8015 | } | ||||
8016 | if (Cmp->isEquality() && | ||||
8017 | (match(UI, m_Add(m_Specific(X), m_SpecificInt(-CmpC))) || | ||||
8018 | match(UI, m_Sub(m_Specific(X), m_SpecificInt(CmpC))))) { | ||||
8019 | IRBuilder<> Builder(Branch); | ||||
8020 | if (UI->getParent() != Branch->getParent()) | ||||
8021 | UI->moveBefore(Branch); | ||||
8022 | Value *NewCmp = Builder.CreateCmp(Cmp->getPredicate(), UI, | ||||
8023 | ConstantInt::get(UI->getType(), 0)); | ||||
8024 | LLVM_DEBUG(dbgs() << "Converting " << *Cmp << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Converting " << * Cmp << "\n"; } } while (false); | ||||
8025 | LLVM_DEBUG(dbgs() << " to compare on zero: " << *NewCmp << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << " to compare on zero: " << *NewCmp << "\n"; } } while (false); | ||||
8026 | replaceAllUsesWith(Cmp, NewCmp, FreshBBs, IsHugeFunc); | ||||
8027 | return true; | ||||
8028 | } | ||||
8029 | } | ||||
8030 | return false; | ||||
8031 | } | ||||
8032 | |||||
8033 | bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) { | ||||
8034 | // Bail out if we inserted the instruction to prevent optimizations from | ||||
8035 | // stepping on each other's toes. | ||||
8036 | if (InsertedInsts.count(I)) | ||||
8037 | return false; | ||||
8038 | |||||
8039 | // TODO: Move into the switch on opcode below here. | ||||
8040 | if (PHINode *P = dyn_cast<PHINode>(I)) { | ||||
8041 | // It is possible for very late stage optimizations (such as SimplifyCFG) | ||||
8042 | // to introduce PHI nodes too late to be cleaned up. If we detect such a | ||||
8043 | // trivial PHI, go ahead and zap it here. | ||||
8044 | if (Value *V = simplifyInstruction(P, {*DL, TLInfo})) { | ||||
8045 | LargeOffsetGEPMap.erase(P); | ||||
8046 | replaceAllUsesWith(P, V, FreshBBs, IsHugeFunc); | ||||
8047 | P->eraseFromParent(); | ||||
8048 | ++NumPHIsElim; | ||||
8049 | return true; | ||||
8050 | } | ||||
8051 | return false; | ||||
8052 | } | ||||
8053 | |||||
8054 | if (CastInst *CI = dyn_cast<CastInst>(I)) { | ||||
8055 | // If the source of the cast is a constant, then this should have | ||||
8056 | // already been constant folded. The only reason NOT to constant fold | ||||
8057 | // it is if something (e.g. LSR) was careful to place the constant | ||||
8058 | // evaluation in a block other than then one that uses it (e.g. to hoist | ||||
8059 | // the address of globals out of a loop). If this is the case, we don't | ||||
8060 | // want to forward-subst the cast. | ||||
8061 | if (isa<Constant>(CI->getOperand(0))) | ||||
8062 | return false; | ||||
8063 | |||||
8064 | if (OptimizeNoopCopyExpression(CI, *TLI, *DL)) | ||||
8065 | return true; | ||||
8066 | |||||
8067 | if ((isa<UIToFPInst>(I) || isa<FPToUIInst>(I) || isa<TruncInst>(I)) && | ||||
8068 | TLI->optimizeExtendOrTruncateConversion(I, | ||||
8069 | LI->getLoopFor(I->getParent()))) | ||||
8070 | return true; | ||||
8071 | |||||
8072 | if (isa<ZExtInst>(I) || isa<SExtInst>(I)) { | ||||
8073 | /// Sink a zext or sext into its user blocks if the target type doesn't | ||||
8074 | /// fit in one register | ||||
8075 | if (TLI->getTypeAction(CI->getContext(), | ||||
8076 | TLI->getValueType(*DL, CI->getType())) == | ||||
8077 | TargetLowering::TypeExpandInteger) { | ||||
8078 | return SinkCast(CI); | ||||
8079 | } else { | ||||
8080 | if (TLI->optimizeExtendOrTruncateConversion( | ||||
8081 | I, LI->getLoopFor(I->getParent()))) | ||||
8082 | return true; | ||||
8083 | |||||
8084 | bool MadeChange = optimizeExt(I); | ||||
8085 | return MadeChange | optimizeExtUses(I); | ||||
8086 | } | ||||
8087 | } | ||||
8088 | return false; | ||||
8089 | } | ||||
8090 | |||||
8091 | if (auto *Cmp = dyn_cast<CmpInst>(I)) | ||||
8092 | if (optimizeCmp(Cmp, ModifiedDT)) | ||||
8093 | return true; | ||||
8094 | |||||
8095 | if (LoadInst *LI = dyn_cast<LoadInst>(I)) { | ||||
8096 | LI->setMetadata(LLVMContext::MD_invariant_group, nullptr); | ||||
8097 | bool Modified = optimizeLoadExt(LI); | ||||
8098 | unsigned AS = LI->getPointerAddressSpace(); | ||||
8099 | Modified |= optimizeMemoryInst(I, I->getOperand(0), LI->getType(), AS); | ||||
8100 | return Modified; | ||||
8101 | } | ||||
8102 | |||||
8103 | if (StoreInst *SI = dyn_cast<StoreInst>(I)) { | ||||
8104 | if (splitMergedValStore(*SI, *DL, *TLI)) | ||||
8105 | return true; | ||||
8106 | SI->setMetadata(LLVMContext::MD_invariant_group, nullptr); | ||||
8107 | unsigned AS = SI->getPointerAddressSpace(); | ||||
8108 | return optimizeMemoryInst(I, SI->getOperand(1), | ||||
8109 | SI->getOperand(0)->getType(), AS); | ||||
8110 | } | ||||
8111 | |||||
8112 | if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) { | ||||
8113 | unsigned AS = RMW->getPointerAddressSpace(); | ||||
8114 | return optimizeMemoryInst(I, RMW->getPointerOperand(), RMW->getType(), AS); | ||||
8115 | } | ||||
8116 | |||||
8117 | if (AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(I)) { | ||||
8118 | unsigned AS = CmpX->getPointerAddressSpace(); | ||||
8119 | return optimizeMemoryInst(I, CmpX->getPointerOperand(), | ||||
8120 | CmpX->getCompareOperand()->getType(), AS); | ||||
8121 | } | ||||
8122 | |||||
8123 | BinaryOperator *BinOp = dyn_cast<BinaryOperator>(I); | ||||
8124 | |||||
8125 | if (BinOp && BinOp->getOpcode() == Instruction::And && EnableAndCmpSinking && | ||||
8126 | sinkAndCmp0Expression(BinOp, *TLI, InsertedInsts)) | ||||
8127 | return true; | ||||
8128 | |||||
8129 | // TODO: Move this into the switch on opcode - it handles shifts already. | ||||
8130 | if (BinOp && (BinOp->getOpcode() == Instruction::AShr || | ||||
8131 | BinOp->getOpcode() == Instruction::LShr)) { | ||||
8132 | ConstantInt *CI = dyn_cast<ConstantInt>(BinOp->getOperand(1)); | ||||
8133 | if (CI && TLI->hasExtractBitsInsn()) | ||||
8134 | if (OptimizeExtractBits(BinOp, CI, *TLI, *DL)) | ||||
8135 | return true; | ||||
8136 | } | ||||
8137 | |||||
8138 | if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) { | ||||
8139 | if (GEPI->hasAllZeroIndices()) { | ||||
8140 | /// The GEP operand must be a pointer, so must its result -> BitCast | ||||
8141 | Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(), | ||||
8142 | GEPI->getName(), GEPI); | ||||
8143 | NC->setDebugLoc(GEPI->getDebugLoc()); | ||||
8144 | replaceAllUsesWith(GEPI, NC, FreshBBs, IsHugeFunc); | ||||
8145 | GEPI->eraseFromParent(); | ||||
8146 | ++NumGEPsElim; | ||||
8147 | optimizeInst(NC, ModifiedDT); | ||||
8148 | return true; | ||||
8149 | } | ||||
8150 | if (tryUnmergingGEPsAcrossIndirectBr(GEPI, TTI)) { | ||||
8151 | return true; | ||||
8152 | } | ||||
8153 | return false; | ||||
8154 | } | ||||
8155 | |||||
8156 | if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) { | ||||
8157 | // freeze(icmp a, const)) -> icmp (freeze a), const | ||||
8158 | // This helps generate efficient conditional jumps. | ||||
8159 | Instruction *CmpI = nullptr; | ||||
8160 | if (ICmpInst *II = dyn_cast<ICmpInst>(FI->getOperand(0))) | ||||
8161 | CmpI = II; | ||||
8162 | else if (FCmpInst *F = dyn_cast<FCmpInst>(FI->getOperand(0))) | ||||
8163 | CmpI = F->getFastMathFlags().none() ? F : nullptr; | ||||
8164 | |||||
8165 | if (CmpI && CmpI->hasOneUse()) { | ||||
8166 | auto Op0 = CmpI->getOperand(0), Op1 = CmpI->getOperand(1); | ||||
8167 | bool Const0 = isa<ConstantInt>(Op0) || isa<ConstantFP>(Op0) || | ||||
8168 | isa<ConstantPointerNull>(Op0); | ||||
8169 | bool Const1 = isa<ConstantInt>(Op1) || isa<ConstantFP>(Op1) || | ||||
8170 | isa<ConstantPointerNull>(Op1); | ||||
8171 | if (Const0 || Const1) { | ||||
8172 | if (!Const0 || !Const1) { | ||||
8173 | auto *F = new FreezeInst(Const0 ? Op1 : Op0, "", CmpI); | ||||
8174 | F->takeName(FI); | ||||
8175 | CmpI->setOperand(Const0 ? 1 : 0, F); | ||||
8176 | } | ||||
8177 | replaceAllUsesWith(FI, CmpI, FreshBBs, IsHugeFunc); | ||||
8178 | FI->eraseFromParent(); | ||||
8179 | return true; | ||||
8180 | } | ||||
8181 | } | ||||
8182 | return false; | ||||
8183 | } | ||||
8184 | |||||
8185 | if (tryToSinkFreeOperands(I)) | ||||
8186 | return true; | ||||
8187 | |||||
8188 | switch (I->getOpcode()) { | ||||
8189 | case Instruction::Shl: | ||||
8190 | case Instruction::LShr: | ||||
8191 | case Instruction::AShr: | ||||
8192 | return optimizeShiftInst(cast<BinaryOperator>(I)); | ||||
8193 | case Instruction::Call: | ||||
8194 | return optimizeCallInst(cast<CallInst>(I), ModifiedDT); | ||||
8195 | case Instruction::Select: | ||||
8196 | return optimizeSelectInst(cast<SelectInst>(I)); | ||||
8197 | case Instruction::ShuffleVector: | ||||
8198 | return optimizeShuffleVectorInst(cast<ShuffleVectorInst>(I)); | ||||
8199 | case Instruction::Switch: | ||||
8200 | return optimizeSwitchInst(cast<SwitchInst>(I)); | ||||
8201 | case Instruction::ExtractElement: | ||||
8202 | return optimizeExtractElementInst(cast<ExtractElementInst>(I)); | ||||
8203 | case Instruction::Br: | ||||
8204 | return optimizeBranch(cast<BranchInst>(I), *TLI, FreshBBs, IsHugeFunc); | ||||
8205 | } | ||||
8206 | |||||
8207 | return false; | ||||
8208 | } | ||||
8209 | |||||
8210 | /// Given an OR instruction, check to see if this is a bitreverse | ||||
8211 | /// idiom. If so, insert the new intrinsic and return true. | ||||
8212 | bool CodeGenPrepare::makeBitReverse(Instruction &I) { | ||||
8213 | if (!I.getType()->isIntegerTy() || | ||||
8214 | !TLI->isOperationLegalOrCustom(ISD::BITREVERSE, | ||||
8215 | TLI->getValueType(*DL, I.getType(), true))) | ||||
8216 | return false; | ||||
8217 | |||||
8218 | SmallVector<Instruction *, 4> Insts; | ||||
8219 | if (!recognizeBSwapOrBitReverseIdiom(&I, false, true, Insts)) | ||||
8220 | return false; | ||||
8221 | Instruction *LastInst = Insts.back(); | ||||
8222 | replaceAllUsesWith(&I, LastInst, FreshBBs, IsHugeFunc); | ||||
8223 | RecursivelyDeleteTriviallyDeadInstructions( | ||||
8224 | &I, TLInfo, nullptr, | ||||
8225 | [&](Value *V) { removeAllAssertingVHReferences(V); }); | ||||
8226 | return true; | ||||
8227 | } | ||||
8228 | |||||
8229 | // In this pass we look for GEP and cast instructions that are used | ||||
8230 | // across basic blocks and rewrite them to improve basic-block-at-a-time | ||||
8231 | // selection. | ||||
8232 | bool CodeGenPrepare::optimizeBlock(BasicBlock &BB, ModifyDT &ModifiedDT) { | ||||
8233 | SunkAddrs.clear(); | ||||
8234 | bool MadeChange = false; | ||||
8235 | |||||
8236 | do { | ||||
8237 | CurInstIterator = BB.begin(); | ||||
8238 | ModifiedDT = ModifyDT::NotModifyDT; | ||||
8239 | while (CurInstIterator != BB.end()) { | ||||
8240 | MadeChange |= optimizeInst(&*CurInstIterator++, ModifiedDT); | ||||
8241 | if (ModifiedDT != ModifyDT::NotModifyDT) { | ||||
8242 | // For huge function we tend to quickly go though the inner optmization | ||||
8243 | // opportunities in the BB. So we go back to the BB head to re-optimize | ||||
8244 | // each instruction instead of go back to the function head. | ||||
8245 | if (IsHugeFunc) { | ||||
8246 | DT.reset(); | ||||
8247 | getDT(*BB.getParent()); | ||||
8248 | break; | ||||
8249 | } else { | ||||
8250 | return true; | ||||
8251 | } | ||||
8252 | } | ||||
8253 | } | ||||
8254 | } while (ModifiedDT == ModifyDT::ModifyInstDT); | ||||
8255 | |||||
8256 | bool MadeBitReverse = true; | ||||
8257 | while (MadeBitReverse) { | ||||
8258 | MadeBitReverse = false; | ||||
8259 | for (auto &I : reverse(BB)) { | ||||
8260 | if (makeBitReverse(I)) { | ||||
8261 | MadeBitReverse = MadeChange = true; | ||||
8262 | break; | ||||
8263 | } | ||||
8264 | } | ||||
8265 | } | ||||
8266 | MadeChange |= dupRetToEnableTailCallOpts(&BB, ModifiedDT); | ||||
8267 | |||||
8268 | return MadeChange; | ||||
8269 | } | ||||
8270 | |||||
8271 | // Some CGP optimizations may move or alter what's computed in a block. Check | ||||
8272 | // whether a dbg.value intrinsic could be pointed at a more appropriate operand. | ||||
8273 | bool CodeGenPrepare::fixupDbgValue(Instruction *I) { | ||||
8274 | assert(isa<DbgValueInst>(I))(static_cast <bool> (isa<DbgValueInst>(I)) ? void (0) : __assert_fail ("isa<DbgValueInst>(I)", "llvm/lib/CodeGen/CodeGenPrepare.cpp" , 8274, __extension__ __PRETTY_FUNCTION__)); | ||||
8275 | DbgValueInst &DVI = *cast<DbgValueInst>(I); | ||||
8276 | |||||
8277 | // Does this dbg.value refer to a sunk address calculation? | ||||
8278 | bool AnyChange = false; | ||||
8279 | SmallDenseSet<Value *> LocationOps(DVI.location_ops().begin(), | ||||
8280 | DVI.location_ops().end()); | ||||
8281 | for (Value *Location : LocationOps) { | ||||
8282 | WeakTrackingVH SunkAddrVH = SunkAddrs[Location]; | ||||
8283 | Value *SunkAddr = SunkAddrVH.pointsToAliveValue() ? SunkAddrVH : nullptr; | ||||
8284 | if (SunkAddr) { | ||||
8285 | // Point dbg.value at locally computed address, which should give the best | ||||
8286 | // opportunity to be accurately lowered. This update may change the type | ||||
8287 | // of pointer being referred to; however this makes no difference to | ||||
8288 | // debugging information, and we can't generate bitcasts that may affect | ||||
8289 | // codegen. | ||||
8290 | DVI.replaceVariableLocationOp(Location, SunkAddr); | ||||
8291 | AnyChange = true; | ||||
8292 | } | ||||
8293 | } | ||||
8294 | return AnyChange; | ||||
8295 | } | ||||
8296 | |||||
8297 | // A llvm.dbg.value may be using a value before its definition, due to | ||||
8298 | // optimizations in this pass and others. Scan for such dbg.values, and rescue | ||||
8299 | // them by moving the dbg.value to immediately after the value definition. | ||||
8300 | // FIXME: Ideally this should never be necessary, and this has the potential | ||||
8301 | // to re-order dbg.value intrinsics. | ||||
8302 | bool CodeGenPrepare::placeDbgValues(Function &F) { | ||||
8303 | bool MadeChange = false; | ||||
8304 | DominatorTree DT(F); | ||||
8305 | |||||
8306 | for (BasicBlock &BB : F) { | ||||
8307 | for (Instruction &Insn : llvm::make_early_inc_range(BB)) { | ||||
8308 | DbgValueInst *DVI = dyn_cast<DbgValueInst>(&Insn); | ||||
8309 | if (!DVI) | ||||
8310 | continue; | ||||
8311 | |||||
8312 | SmallVector<Instruction *, 4> VIs; | ||||
8313 | for (Value *V : DVI->getValues()) | ||||
8314 | if (Instruction *VI = dyn_cast_or_null<Instruction>(V)) | ||||
8315 | VIs.push_back(VI); | ||||
8316 | |||||
8317 | // This DVI may depend on multiple instructions, complicating any | ||||
8318 | // potential sink. This block takes the defensive approach, opting to | ||||
8319 | // "undef" the DVI if it has more than one instruction and any of them do | ||||
8320 | // not dominate DVI. | ||||
8321 | for (Instruction *VI : VIs) { | ||||
8322 | if (VI->isTerminator()) | ||||
8323 | continue; | ||||
8324 | |||||
8325 | // If VI is a phi in a block with an EHPad terminator, we can't insert | ||||
8326 | // after it. | ||||
8327 | if (isa<PHINode>(VI) && VI->getParent()->getTerminator()->isEHPad()) | ||||
8328 | continue; | ||||
8329 | |||||
8330 | // If the defining instruction dominates the dbg.value, we do not need | ||||
8331 | // to move the dbg.value. | ||||
8332 | if (DT.dominates(VI, DVI)) | ||||
8333 | continue; | ||||
8334 | |||||
8335 | // If we depend on multiple instructions and any of them doesn't | ||||
8336 | // dominate this DVI, we probably can't salvage it: moving it to | ||||
8337 | // after any of the instructions could cause us to lose the others. | ||||
8338 | if (VIs.size() > 1) { | ||||
8339 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Unable to find valid location for Debug Value, undefing:\n" << *DVI; } } while (false) | ||||
8340 | dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Unable to find valid location for Debug Value, undefing:\n" << *DVI; } } while (false) | ||||
8341 | << "Unable to find valid location for Debug Value, undefing:\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Unable to find valid location for Debug Value, undefing:\n" << *DVI; } } while (false) | ||||
8342 | << *DVI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Unable to find valid location for Debug Value, undefing:\n" << *DVI; } } while (false); | ||||
8343 | DVI->setKillLocation(); | ||||
8344 | break; | ||||
8345 | } | ||||
8346 | |||||
8347 | LLVM_DEBUG(dbgs() << "Moving Debug Value before :\n"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI; } } while (false) | ||||
8348 | << *DVI << ' ' << *VI)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Moving Debug Value before :\n" << *DVI << ' ' << *VI; } } while (false); | ||||
8349 | DVI->removeFromParent(); | ||||
8350 | if (isa<PHINode>(VI)) | ||||
8351 | DVI->insertBefore(&*VI->getParent()->getFirstInsertionPt()); | ||||
8352 | else | ||||
8353 | DVI->insertAfter(VI); | ||||
8354 | MadeChange = true; | ||||
8355 | ++NumDbgValueMoved; | ||||
8356 | } | ||||
8357 | } | ||||
8358 | } | ||||
8359 | return MadeChange; | ||||
8360 | } | ||||
8361 | |||||
8362 | // Group scattered pseudo probes in a block to favor SelectionDAG. Scattered | ||||
8363 | // probes can be chained dependencies of other regular DAG nodes and block DAG | ||||
8364 | // combine optimizations. | ||||
8365 | bool CodeGenPrepare::placePseudoProbes(Function &F) { | ||||
8366 | bool MadeChange = false; | ||||
8367 | for (auto &Block : F) { | ||||
8368 | // Move the rest probes to the beginning of the block. | ||||
8369 | auto FirstInst = Block.getFirstInsertionPt(); | ||||
8370 | while (FirstInst != Block.end() && FirstInst->isDebugOrPseudoInst()) | ||||
8371 | ++FirstInst; | ||||
8372 | BasicBlock::iterator I(FirstInst); | ||||
8373 | I++; | ||||
8374 | while (I != Block.end()) { | ||||
8375 | if (auto *II = dyn_cast<PseudoProbeInst>(I++)) { | ||||
8376 | II->moveBefore(&*FirstInst); | ||||
8377 | MadeChange = true; | ||||
8378 | } | ||||
8379 | } | ||||
8380 | } | ||||
8381 | return MadeChange; | ||||
8382 | } | ||||
8383 | |||||
8384 | /// Scale down both weights to fit into uint32_t. | ||||
8385 | static void scaleWeights(uint64_t &NewTrue, uint64_t &NewFalse) { | ||||
8386 | uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse; | ||||
8387 | uint32_t Scale = (NewMax / std::numeric_limits<uint32_t>::max()) + 1; | ||||
8388 | NewTrue = NewTrue / Scale; | ||||
8389 | NewFalse = NewFalse / Scale; | ||||
8390 | } | ||||
8391 | |||||
8392 | /// Some targets prefer to split a conditional branch like: | ||||
8393 | /// \code | ||||
8394 | /// %0 = icmp ne i32 %a, 0 | ||||
8395 | /// %1 = icmp ne i32 %b, 0 | ||||
8396 | /// %or.cond = or i1 %0, %1 | ||||
8397 | /// br i1 %or.cond, label %TrueBB, label %FalseBB | ||||
8398 | /// \endcode | ||||
8399 | /// into multiple branch instructions like: | ||||
8400 | /// \code | ||||
8401 | /// bb1: | ||||
8402 | /// %0 = icmp ne i32 %a, 0 | ||||
8403 | /// br i1 %0, label %TrueBB, label %bb2 | ||||
8404 | /// bb2: | ||||
8405 | /// %1 = icmp ne i32 %b, 0 | ||||
8406 | /// br i1 %1, label %TrueBB, label %FalseBB | ||||
8407 | /// \endcode | ||||
8408 | /// This usually allows instruction selection to do even further optimizations | ||||
8409 | /// and combine the compare with the branch instruction. Currently this is | ||||
8410 | /// applied for targets which have "cheap" jump instructions. | ||||
8411 | /// | ||||
8412 | /// FIXME: Remove the (equivalent?) implementation in SelectionDAG. | ||||
8413 | /// | ||||
8414 | bool CodeGenPrepare::splitBranchCondition(Function &F, ModifyDT &ModifiedDT) { | ||||
8415 | if (!TM->Options.EnableFastISel || TLI->isJumpExpensive()) | ||||
8416 | return false; | ||||
8417 | |||||
8418 | bool MadeChange = false; | ||||
8419 | for (auto &BB : F) { | ||||
8420 | // Does this BB end with the following? | ||||
8421 | // %cond1 = icmp|fcmp|binary instruction ... | ||||
8422 | // %cond2 = icmp|fcmp|binary instruction ... | ||||
8423 | // %cond.or = or|and i1 %cond1, cond2 | ||||
8424 | // br i1 %cond.or label %dest1, label %dest2" | ||||
8425 | Instruction *LogicOp; | ||||
8426 | BasicBlock *TBB, *FBB; | ||||
8427 | if (!match(BB.getTerminator(), | ||||
8428 | m_Br(m_OneUse(m_Instruction(LogicOp)), TBB, FBB))) | ||||
8429 | continue; | ||||
8430 | |||||
8431 | auto *Br1 = cast<BranchInst>(BB.getTerminator()); | ||||
8432 | if (Br1->getMetadata(LLVMContext::MD_unpredictable)) | ||||
8433 | continue; | ||||
8434 | |||||
8435 | // The merging of mostly empty BB can cause a degenerate branch. | ||||
8436 | if (TBB == FBB) | ||||
8437 | continue; | ||||
8438 | |||||
8439 | unsigned Opc; | ||||
8440 | Value *Cond1, *Cond2; | ||||
8441 | if (match(LogicOp, | ||||
8442 | m_LogicalAnd(m_OneUse(m_Value(Cond1)), m_OneUse(m_Value(Cond2))))) | ||||
8443 | Opc = Instruction::And; | ||||
8444 | else if (match(LogicOp, m_LogicalOr(m_OneUse(m_Value(Cond1)), | ||||
8445 | m_OneUse(m_Value(Cond2))))) | ||||
8446 | Opc = Instruction::Or; | ||||
8447 | else | ||||
8448 | continue; | ||||
8449 | |||||
8450 | auto IsGoodCond = [](Value *Cond) { | ||||
8451 | return match( | ||||
8452 | Cond, | ||||
8453 | m_CombineOr(m_Cmp(), m_CombineOr(m_LogicalAnd(m_Value(), m_Value()), | ||||
8454 | m_LogicalOr(m_Value(), m_Value())))); | ||||
8455 | }; | ||||
8456 | if (!IsGoodCond(Cond1) || !IsGoodCond(Cond2)) | ||||
8457 | continue; | ||||
8458 | |||||
8459 | LLVM_DEBUG(dbgs() << "Before branch condition splitting\n"; BB.dump())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "Before branch condition splitting\n" ; BB.dump(); } } while (false); | ||||
8460 | |||||
8461 | // Create a new BB. | ||||
8462 | auto *TmpBB = | ||||
8463 | BasicBlock::Create(BB.getContext(), BB.getName() + ".cond.split", | ||||
8464 | BB.getParent(), BB.getNextNode()); | ||||
8465 | if (IsHugeFunc) | ||||
8466 | FreshBBs.insert(TmpBB); | ||||
8467 | |||||
8468 | // Update original basic block by using the first condition directly by the | ||||
8469 | // branch instruction and removing the no longer needed and/or instruction. | ||||
8470 | Br1->setCondition(Cond1); | ||||
8471 | LogicOp->eraseFromParent(); | ||||
8472 | |||||
8473 | // Depending on the condition we have to either replace the true or the | ||||
8474 | // false successor of the original branch instruction. | ||||
8475 | if (Opc == Instruction::And) | ||||
8476 | Br1->setSuccessor(0, TmpBB); | ||||
8477 | else | ||||
8478 | Br1->setSuccessor(1, TmpBB); | ||||
8479 | |||||
8480 | // Fill in the new basic block. | ||||
8481 | auto *Br2 = IRBuilder<>(TmpBB).CreateCondBr(Cond2, TBB, FBB); | ||||
8482 | if (auto *I = dyn_cast<Instruction>(Cond2)) { | ||||
8483 | I->removeFromParent(); | ||||
8484 | I->insertBefore(Br2); | ||||
8485 | } | ||||
8486 | |||||
8487 | // Update PHI nodes in both successors. The original BB needs to be | ||||
8488 | // replaced in one successor's PHI nodes, because the branch comes now from | ||||
8489 | // the newly generated BB (NewBB). In the other successor we need to add one | ||||
8490 | // incoming edge to the PHI nodes, because both branch instructions target | ||||
8491 | // now the same successor. Depending on the original branch condition | ||||
8492 | // (and/or) we have to swap the successors (TrueDest, FalseDest), so that | ||||
8493 | // we perform the correct update for the PHI nodes. | ||||
8494 | // This doesn't change the successor order of the just created branch | ||||
8495 | // instruction (or any other instruction). | ||||
8496 | if (Opc == Instruction::Or) | ||||
8497 | std::swap(TBB, FBB); | ||||
8498 | |||||
8499 | // Replace the old BB with the new BB. | ||||
8500 | TBB->replacePhiUsesWith(&BB, TmpBB); | ||||
8501 | |||||
8502 | // Add another incoming edge from the new BB. | ||||
8503 | for (PHINode &PN : FBB->phis()) { | ||||
8504 | auto *Val = PN.getIncomingValueForBlock(&BB); | ||||
8505 | PN.addIncoming(Val, TmpBB); | ||||
8506 | } | ||||
8507 | |||||
8508 | // Update the branch weights (from SelectionDAGBuilder:: | ||||
8509 | // FindMergedConditions). | ||||
8510 | if (Opc == Instruction::Or) { | ||||
8511 | // Codegen X | Y as: | ||||
8512 | // BB1: | ||||
8513 | // jmp_if_X TBB | ||||
8514 | // jmp TmpBB | ||||
8515 | // TmpBB: | ||||
8516 | // jmp_if_Y TBB | ||||
8517 | // jmp FBB | ||||
8518 | // | ||||
8519 | |||||
8520 | // We have flexibility in setting Prob for BB1 and Prob for NewBB. | ||||
8521 | // The requirement is that | ||||
8522 | // TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB) | ||||
8523 | // = TrueProb for original BB. | ||||
8524 | // Assuming the original weights are A and B, one choice is to set BB1's | ||||
8525 | // weights to A and A+2B, and set TmpBB's weights to A and 2B. This choice | ||||
8526 | // assumes that | ||||
8527 | // TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB. | ||||
8528 | // Another choice is to assume TrueProb for BB1 equals to TrueProb for | ||||
8529 | // TmpBB, but the math is more complicated. | ||||
8530 | uint64_t TrueWeight, FalseWeight; | ||||
8531 | if (extractBranchWeights(*Br1, TrueWeight, FalseWeight)) { | ||||
8532 | uint64_t NewTrueWeight = TrueWeight; | ||||
8533 | uint64_t NewFalseWeight = TrueWeight + 2 * FalseWeight; | ||||
8534 | scaleWeights(NewTrueWeight, NewFalseWeight); | ||||
8535 | Br1->setMetadata(LLVMContext::MD_prof, | ||||
8536 | MDBuilder(Br1->getContext()) | ||||
8537 | .createBranchWeights(TrueWeight, FalseWeight)); | ||||
8538 | |||||
8539 | NewTrueWeight = TrueWeight; | ||||
8540 | NewFalseWeight = 2 * FalseWeight; | ||||
8541 | scaleWeights(NewTrueWeight, NewFalseWeight); | ||||
8542 | Br2->setMetadata(LLVMContext::MD_prof, | ||||
8543 | MDBuilder(Br2->getContext()) | ||||
8544 | .createBranchWeights(TrueWeight, FalseWeight)); | ||||
8545 | } | ||||
8546 | } else { | ||||
8547 | // Codegen X & Y as: | ||||
8548 | // BB1: | ||||
8549 | // jmp_if_X TmpBB | ||||
8550 | // jmp FBB | ||||
8551 | // TmpBB: | ||||
8552 | // jmp_if_Y TBB | ||||
8553 | // jmp FBB | ||||
8554 | // | ||||
8555 | // This requires creation of TmpBB after CurBB. | ||||
8556 | |||||
8557 | // We have flexibility in setting Prob for BB1 and Prob for TmpBB. | ||||
8558 | // The requirement is that | ||||
8559 | // FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB) | ||||
8560 | // = FalseProb for original BB. | ||||
8561 | // Assuming the original weights are A and B, one choice is to set BB1's | ||||
8562 | // weights to 2A+B and B, and set TmpBB's weights to 2A and B. This choice | ||||
8563 | // assumes that | ||||
8564 | // FalseProb for BB1 == TrueProb for BB1 * FalseProb for TmpBB. | ||||
8565 | uint64_t TrueWeight, FalseWeight; | ||||
8566 | if (extractBranchWeights(*Br1, TrueWeight, FalseWeight)) { | ||||
8567 | uint64_t NewTrueWeight = 2 * TrueWeight + FalseWeight; | ||||
8568 | uint64_t NewFalseWeight = FalseWeight; | ||||
8569 | scaleWeights(NewTrueWeight, NewFalseWeight); | ||||
8570 | Br1->setMetadata(LLVMContext::MD_prof, | ||||
8571 | MDBuilder(Br1->getContext()) | ||||
8572 | .createBranchWeights(TrueWeight, FalseWeight)); | ||||
8573 | |||||
8574 | NewTrueWeight = 2 * TrueWeight; | ||||
8575 | NewFalseWeight = FalseWeight; | ||||
8576 | scaleWeights(NewTrueWeight, NewFalseWeight); | ||||
8577 | Br2->setMetadata(LLVMContext::MD_prof, | ||||
8578 | MDBuilder(Br2->getContext()) | ||||
8579 | .createBranchWeights(TrueWeight, FalseWeight)); | ||||
8580 | } | ||||
8581 | } | ||||
8582 | |||||
8583 | ModifiedDT = ModifyDT::ModifyBBDT; | ||||
8584 | MadeChange = true; | ||||
8585 | |||||
8586 | LLVM_DEBUG(dbgs() << "After branch condition splitting\n"; BB.dump();do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "After branch condition splitting\n" ; BB.dump(); TmpBB->dump(); } } while (false) | ||||
8587 | TmpBB->dump())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("codegenprepare")) { dbgs() << "After branch condition splitting\n" ; BB.dump(); TmpBB->dump(); } } while (false); | ||||
8588 | } | ||||
8589 | return MadeChange; | ||||
8590 | } |
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/MapVector.h" |
21 | #include "llvm/ADT/STLExtras.h" |
22 | #include "llvm/ADT/SmallVector.h" |
23 | #include "llvm/ADT/Twine.h" |
24 | #include "llvm/ADT/iterator.h" |
25 | #include "llvm/ADT/iterator_range.h" |
26 | #include "llvm/IR/CFG.h" |
27 | #include "llvm/IR/Constant.h" |
28 | #include "llvm/IR/DerivedTypes.h" |
29 | #include "llvm/IR/InstrTypes.h" |
30 | #include "llvm/IR/Instruction.h" |
31 | #include "llvm/IR/OperandTraits.h" |
32 | #include "llvm/IR/Use.h" |
33 | #include "llvm/IR/User.h" |
34 | #include "llvm/Support/AtomicOrdering.h" |
35 | #include "llvm/Support/ErrorHandling.h" |
36 | #include <cassert> |
37 | #include <cstddef> |
38 | #include <cstdint> |
39 | #include <iterator> |
40 | #include <optional> |
41 | |
42 | namespace llvm { |
43 | |
44 | class APFloat; |
45 | class APInt; |
46 | class BasicBlock; |
47 | class ConstantInt; |
48 | class DataLayout; |
49 | class StringRef; |
50 | class Type; |
51 | class Value; |
52 | |
53 | //===----------------------------------------------------------------------===// |
54 | // AllocaInst Class |
55 | //===----------------------------------------------------------------------===// |
56 | |
57 | /// an instruction to allocate memory on the stack |
58 | class AllocaInst : public UnaryInstruction { |
59 | Type *AllocatedType; |
60 | |
61 | using AlignmentField = AlignmentBitfieldElementT<0>; |
62 | using UsedWithInAllocaField = BoolBitfieldElementT<AlignmentField::NextBit>; |
63 | using SwiftErrorField = BoolBitfieldElementT<UsedWithInAllocaField::NextBit>; |
64 | static_assert(Bitfield::areContiguous<AlignmentField, UsedWithInAllocaField, |
65 | SwiftErrorField>(), |
66 | "Bitfields must be contiguous"); |
67 | |
68 | protected: |
69 | // Note: Instruction needs to be a friend here to call cloneImpl. |
70 | friend class Instruction; |
71 | |
72 | AllocaInst *cloneImpl() const; |
73 | |
74 | public: |
75 | explicit AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
76 | const Twine &Name, Instruction *InsertBefore); |
77 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, |
78 | const Twine &Name, BasicBlock *InsertAtEnd); |
79 | |
80 | AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name, |
81 | Instruction *InsertBefore); |
82 | AllocaInst(Type *Ty, unsigned AddrSpace, |
83 | const Twine &Name, BasicBlock *InsertAtEnd); |
84 | |
85 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align, |
86 | const Twine &Name = "", Instruction *InsertBefore = nullptr); |
87 | AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align, |
88 | const Twine &Name, BasicBlock *InsertAtEnd); |
89 | |
90 | /// Return true if there is an allocation size parameter to the allocation |
91 | /// instruction that is not 1. |
92 | bool isArrayAllocation() const; |
93 | |
94 | /// Get the number of elements allocated. For a simple allocation of a single |
95 | /// element, this will return a constant 1 value. |
96 | const Value *getArraySize() const { return getOperand(0); } |
97 | Value *getArraySize() { return getOperand(0); } |
98 | |
99 | /// Overload to return most specific pointer type. |
100 | PointerType *getType() const { |
101 | return cast<PointerType>(Instruction::getType()); |
102 | } |
103 | |
104 | /// Return the address space for the allocation. |
105 | unsigned getAddressSpace() const { |
106 | return getType()->getAddressSpace(); |
107 | } |
108 | |
109 | /// Get allocation size in bytes. Returns std::nullopt if size can't be |
110 | /// determined, e.g. in case of a VLA. |
111 | std::optional<TypeSize> getAllocationSize(const DataLayout &DL) const; |
112 | |
113 | /// Get allocation size in bits. Returns std::nullopt if size can't be |
114 | /// determined, e.g. in case of a VLA. |
115 | std::optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const; |
116 | |
117 | /// Return the type that is being allocated by the instruction. |
118 | Type *getAllocatedType() const { return AllocatedType; } |
119 | /// for use only in special circumstances that need to generically |
120 | /// transform a whole instruction (eg: IR linking and vectorization). |
121 | void setAllocatedType(Type *Ty) { AllocatedType = Ty; } |
122 | |
123 | /// Return the alignment of the memory that is being allocated by the |
124 | /// instruction. |
125 | Align getAlign() const { |
126 | return Align(1ULL << getSubclassData<AlignmentField>()); |
127 | } |
128 | |
129 | void setAlignment(Align Align) { |
130 | setSubclassData<AlignmentField>(Log2(Align)); |
131 | } |
132 | |
133 | /// Return true if this alloca is in the entry block of the function and is a |
134 | /// constant size. If so, the code generator will fold it into the |
135 | /// prolog/epilog code, so it is basically free. |
136 | bool isStaticAlloca() const; |
137 | |
138 | /// Return true if this alloca is used as an inalloca argument to a call. Such |
139 | /// allocas are never considered static even if they are in the entry block. |
140 | bool isUsedWithInAlloca() const { |
141 | return getSubclassData<UsedWithInAllocaField>(); |
142 | } |
143 | |
144 | /// Specify whether this alloca is used to represent the arguments to a call. |
145 | void setUsedWithInAlloca(bool V) { |
146 | setSubclassData<UsedWithInAllocaField>(V); |
147 | } |
148 | |
149 | /// Return true if this alloca is used as a swifterror argument to a call. |
150 | bool isSwiftError() const { return getSubclassData<SwiftErrorField>(); } |
151 | /// Specify whether this alloca is used to represent a swifterror. |
152 | void setSwiftError(bool V) { setSubclassData<SwiftErrorField>(V); } |
153 | |
154 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
155 | static bool classof(const Instruction *I) { |
156 | return (I->getOpcode() == Instruction::Alloca); |
157 | } |
158 | static bool classof(const Value *V) { |
159 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
160 | } |
161 | |
162 | private: |
163 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
164 | // method so that subclasses cannot accidentally use it. |
165 | template <typename Bitfield> |
166 | void setSubclassData(typename Bitfield::Type Value) { |
167 | Instruction::setSubclassData<Bitfield>(Value); |
168 | } |
169 | }; |
170 | |
171 | //===----------------------------------------------------------------------===// |
172 | // LoadInst Class |
173 | //===----------------------------------------------------------------------===// |
174 | |
175 | /// An instruction for reading from memory. This uses the SubclassData field in |
176 | /// Value to store whether or not the load is volatile. |
177 | class LoadInst : public UnaryInstruction { |
178 | using VolatileField = BoolBitfieldElementT<0>; |
179 | using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>; |
180 | using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>; |
181 | static_assert( |
182 | Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(), |
183 | "Bitfields must be contiguous"); |
184 | |
185 | void AssertOK(); |
186 | |
187 | protected: |
188 | // Note: Instruction needs to be a friend here to call cloneImpl. |
189 | friend class Instruction; |
190 | |
191 | LoadInst *cloneImpl() const; |
192 | |
193 | public: |
194 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, |
195 | Instruction *InsertBefore); |
196 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd); |
197 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
198 | Instruction *InsertBefore); |
199 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
200 | BasicBlock *InsertAtEnd); |
201 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
202 | Align Align, Instruction *InsertBefore = nullptr); |
203 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
204 | Align Align, BasicBlock *InsertAtEnd); |
205 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
206 | Align Align, AtomicOrdering Order, |
207 | SyncScope::ID SSID = SyncScope::System, |
208 | Instruction *InsertBefore = nullptr); |
209 | LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile, |
210 | Align Align, AtomicOrdering Order, SyncScope::ID SSID, |
211 | BasicBlock *InsertAtEnd); |
212 | |
213 | /// Return true if this is a load from a volatile memory location. |
214 | bool isVolatile() const { return getSubclassData<VolatileField>(); } |
215 | |
216 | /// Specify whether this is a volatile load or not. |
217 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } |
218 | |
219 | /// Return the alignment of the access that is being performed. |
220 | Align getAlign() const { |
221 | return Align(1ULL << (getSubclassData<AlignmentField>())); |
222 | } |
223 | |
224 | void setAlignment(Align Align) { |
225 | setSubclassData<AlignmentField>(Log2(Align)); |
226 | } |
227 | |
228 | /// Returns the ordering constraint of this load instruction. |
229 | AtomicOrdering getOrdering() const { |
230 | return getSubclassData<OrderingField>(); |
231 | } |
232 | /// Sets the ordering constraint of this load instruction. May not be Release |
233 | /// or AcquireRelease. |
234 | void setOrdering(AtomicOrdering Ordering) { |
235 | setSubclassData<OrderingField>(Ordering); |
236 | } |
237 | |
238 | /// Returns the synchronization scope ID of this load instruction. |
239 | SyncScope::ID getSyncScopeID() const { |
240 | return SSID; |
241 | } |
242 | |
243 | /// Sets the synchronization scope ID of this load instruction. |
244 | void setSyncScopeID(SyncScope::ID SSID) { |
245 | this->SSID = SSID; |
246 | } |
247 | |
248 | /// Sets the ordering constraint and the synchronization scope ID of this load |
249 | /// instruction. |
250 | void setAtomic(AtomicOrdering Ordering, |
251 | SyncScope::ID SSID = SyncScope::System) { |
252 | setOrdering(Ordering); |
253 | setSyncScopeID(SSID); |
254 | } |
255 | |
256 | bool isSimple() const { return !isAtomic() && !isVolatile(); } |
257 | |
258 | bool isUnordered() const { |
259 | return (getOrdering() == AtomicOrdering::NotAtomic || |
260 | getOrdering() == AtomicOrdering::Unordered) && |
261 | !isVolatile(); |
262 | } |
263 | |
264 | Value *getPointerOperand() { return getOperand(0); } |
265 | const Value *getPointerOperand() const { return getOperand(0); } |
266 | static unsigned getPointerOperandIndex() { return 0U; } |
267 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } |
268 | |
269 | /// Returns the address space of the pointer operand. |
270 | unsigned getPointerAddressSpace() const { |
271 | return getPointerOperandType()->getPointerAddressSpace(); |
272 | } |
273 | |
274 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
275 | static bool classof(const Instruction *I) { |
276 | return I->getOpcode() == Instruction::Load; |
277 | } |
278 | static bool classof(const Value *V) { |
279 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
280 | } |
281 | |
282 | private: |
283 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
284 | // method so that subclasses cannot accidentally use it. |
285 | template <typename Bitfield> |
286 | void setSubclassData(typename Bitfield::Type Value) { |
287 | Instruction::setSubclassData<Bitfield>(Value); |
288 | } |
289 | |
290 | /// The synchronization scope ID of this load instruction. Not quite enough |
291 | /// room in SubClassData for everything, so synchronization scope ID gets its |
292 | /// own field. |
293 | SyncScope::ID SSID; |
294 | }; |
295 | |
296 | //===----------------------------------------------------------------------===// |
297 | // StoreInst Class |
298 | //===----------------------------------------------------------------------===// |
299 | |
300 | /// An instruction for storing to memory. |
301 | class StoreInst : public Instruction { |
302 | using VolatileField = BoolBitfieldElementT<0>; |
303 | using AlignmentField = AlignmentBitfieldElementT<VolatileField::NextBit>; |
304 | using OrderingField = AtomicOrderingBitfieldElementT<AlignmentField::NextBit>; |
305 | static_assert( |
306 | Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(), |
307 | "Bitfields must be contiguous"); |
308 | |
309 | void AssertOK(); |
310 | |
311 | protected: |
312 | // Note: Instruction needs to be a friend here to call cloneImpl. |
313 | friend class Instruction; |
314 | |
315 | StoreInst *cloneImpl() const; |
316 | |
317 | public: |
318 | StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore); |
319 | StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd); |
320 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Instruction *InsertBefore); |
321 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd); |
322 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, |
323 | Instruction *InsertBefore = nullptr); |
324 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, |
325 | BasicBlock *InsertAtEnd); |
326 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, |
327 | AtomicOrdering Order, SyncScope::ID SSID = SyncScope::System, |
328 | Instruction *InsertBefore = nullptr); |
329 | StoreInst(Value *Val, Value *Ptr, bool isVolatile, Align Align, |
330 | AtomicOrdering Order, SyncScope::ID SSID, BasicBlock *InsertAtEnd); |
331 | |
332 | // allocate space for exactly two operands |
333 | void *operator new(size_t S) { return User::operator new(S, 2); } |
334 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
335 | |
336 | /// Return true if this is a store to a volatile memory location. |
337 | bool isVolatile() const { return getSubclassData<VolatileField>(); } |
338 | |
339 | /// Specify whether this is a volatile store or not. |
340 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } |
341 | |
342 | /// Transparently provide more efficient getOperand methods. |
343 | 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; |
344 | |
345 | Align getAlign() const { |
346 | return Align(1ULL << (getSubclassData<AlignmentField>())); |
347 | } |
348 | |
349 | void setAlignment(Align Align) { |
350 | setSubclassData<AlignmentField>(Log2(Align)); |
351 | } |
352 | |
353 | /// Returns the ordering constraint of this store instruction. |
354 | AtomicOrdering getOrdering() const { |
355 | return getSubclassData<OrderingField>(); |
356 | } |
357 | |
358 | /// Sets the ordering constraint of this store instruction. May not be |
359 | /// Acquire or AcquireRelease. |
360 | void setOrdering(AtomicOrdering Ordering) { |
361 | setSubclassData<OrderingField>(Ordering); |
362 | } |
363 | |
364 | /// Returns the synchronization scope ID of this store instruction. |
365 | SyncScope::ID getSyncScopeID() const { |
366 | return SSID; |
367 | } |
368 | |
369 | /// Sets the synchronization scope ID of this store instruction. |
370 | void setSyncScopeID(SyncScope::ID SSID) { |
371 | this->SSID = SSID; |
372 | } |
373 | |
374 | /// Sets the ordering constraint and the synchronization scope ID of this |
375 | /// store instruction. |
376 | void setAtomic(AtomicOrdering Ordering, |
377 | SyncScope::ID SSID = SyncScope::System) { |
378 | setOrdering(Ordering); |
379 | setSyncScopeID(SSID); |
380 | } |
381 | |
382 | bool isSimple() const { return !isAtomic() && !isVolatile(); } |
383 | |
384 | bool isUnordered() const { |
385 | return (getOrdering() == AtomicOrdering::NotAtomic || |
386 | getOrdering() == AtomicOrdering::Unordered) && |
387 | !isVolatile(); |
388 | } |
389 | |
390 | Value *getValueOperand() { return getOperand(0); } |
391 | const Value *getValueOperand() const { return getOperand(0); } |
392 | |
393 | Value *getPointerOperand() { return getOperand(1); } |
394 | const Value *getPointerOperand() const { return getOperand(1); } |
395 | static unsigned getPointerOperandIndex() { return 1U; } |
396 | Type *getPointerOperandType() const { return getPointerOperand()->getType(); } |
397 | |
398 | /// Returns the address space of the pointer operand. |
399 | unsigned getPointerAddressSpace() const { |
400 | return getPointerOperandType()->getPointerAddressSpace(); |
401 | } |
402 | |
403 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
404 | static bool classof(const Instruction *I) { |
405 | return I->getOpcode() == Instruction::Store; |
406 | } |
407 | static bool classof(const Value *V) { |
408 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
409 | } |
410 | |
411 | private: |
412 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
413 | // method so that subclasses cannot accidentally use it. |
414 | template <typename Bitfield> |
415 | void setSubclassData(typename Bitfield::Type Value) { |
416 | Instruction::setSubclassData<Bitfield>(Value); |
417 | } |
418 | |
419 | /// The synchronization scope ID of this store instruction. Not quite enough |
420 | /// room in SubClassData for everything, so synchronization scope ID gets its |
421 | /// own field. |
422 | SyncScope::ID SSID; |
423 | }; |
424 | |
425 | template <> |
426 | struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> { |
427 | }; |
428 | |
429 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<StoreInst>::operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 429, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<StoreInst>::operands(this) && "setOperand() out of range!" ) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<StoreInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 429, __extension__ __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); } |
430 | |
431 | //===----------------------------------------------------------------------===// |
432 | // FenceInst Class |
433 | //===----------------------------------------------------------------------===// |
434 | |
435 | /// An instruction for ordering other memory operations. |
436 | class FenceInst : public Instruction { |
437 | using OrderingField = AtomicOrderingBitfieldElementT<0>; |
438 | |
439 | void Init(AtomicOrdering Ordering, SyncScope::ID SSID); |
440 | |
441 | protected: |
442 | // Note: Instruction needs to be a friend here to call cloneImpl. |
443 | friend class Instruction; |
444 | |
445 | FenceInst *cloneImpl() const; |
446 | |
447 | public: |
448 | // Ordering may only be Acquire, Release, AcquireRelease, or |
449 | // SequentiallyConsistent. |
450 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
451 | SyncScope::ID SSID = SyncScope::System, |
452 | Instruction *InsertBefore = nullptr); |
453 | FenceInst(LLVMContext &C, AtomicOrdering Ordering, SyncScope::ID SSID, |
454 | BasicBlock *InsertAtEnd); |
455 | |
456 | // allocate space for exactly zero operands |
457 | void *operator new(size_t S) { return User::operator new(S, 0); } |
458 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
459 | |
460 | /// Returns the ordering constraint of this fence instruction. |
461 | AtomicOrdering getOrdering() const { |
462 | return getSubclassData<OrderingField>(); |
463 | } |
464 | |
465 | /// Sets the ordering constraint of this fence instruction. May only be |
466 | /// Acquire, Release, AcquireRelease, or SequentiallyConsistent. |
467 | void setOrdering(AtomicOrdering Ordering) { |
468 | setSubclassData<OrderingField>(Ordering); |
469 | } |
470 | |
471 | /// Returns the synchronization scope ID of this fence instruction. |
472 | SyncScope::ID getSyncScopeID() const { |
473 | return SSID; |
474 | } |
475 | |
476 | /// Sets the synchronization scope ID of this fence instruction. |
477 | void setSyncScopeID(SyncScope::ID SSID) { |
478 | this->SSID = SSID; |
479 | } |
480 | |
481 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
482 | static bool classof(const Instruction *I) { |
483 | return I->getOpcode() == Instruction::Fence; |
484 | } |
485 | static bool classof(const Value *V) { |
486 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
487 | } |
488 | |
489 | private: |
490 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
491 | // method so that subclasses cannot accidentally use it. |
492 | template <typename Bitfield> |
493 | void setSubclassData(typename Bitfield::Type Value) { |
494 | Instruction::setSubclassData<Bitfield>(Value); |
495 | } |
496 | |
497 | /// The synchronization scope ID of this fence instruction. Not quite enough |
498 | /// room in SubClassData for everything, so synchronization scope ID gets its |
499 | /// own field. |
500 | SyncScope::ID SSID; |
501 | }; |
502 | |
503 | //===----------------------------------------------------------------------===// |
504 | // AtomicCmpXchgInst Class |
505 | //===----------------------------------------------------------------------===// |
506 | |
507 | /// An instruction that atomically checks whether a |
508 | /// specified value is in a memory location, and, if it is, stores a new value |
509 | /// there. The value returned by this instruction is a pair containing the |
510 | /// original value as first element, and an i1 indicating success (true) or |
511 | /// failure (false) as second element. |
512 | /// |
513 | class AtomicCmpXchgInst : public Instruction { |
514 | void Init(Value *Ptr, Value *Cmp, Value *NewVal, Align Align, |
515 | AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering, |
516 | SyncScope::ID SSID); |
517 | |
518 | template <unsigned Offset> |
519 | using AtomicOrderingBitfieldElement = |
520 | typename Bitfield::Element<AtomicOrdering, Offset, 3, |
521 | AtomicOrdering::LAST>; |
522 | |
523 | protected: |
524 | // Note: Instruction needs to be a friend here to call cloneImpl. |
525 | friend class Instruction; |
526 | |
527 | AtomicCmpXchgInst *cloneImpl() const; |
528 | |
529 | public: |
530 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment, |
531 | AtomicOrdering SuccessOrdering, |
532 | AtomicOrdering FailureOrdering, SyncScope::ID SSID, |
533 | Instruction *InsertBefore = nullptr); |
534 | AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment, |
535 | AtomicOrdering SuccessOrdering, |
536 | AtomicOrdering FailureOrdering, SyncScope::ID SSID, |
537 | BasicBlock *InsertAtEnd); |
538 | |
539 | // allocate space for exactly three operands |
540 | void *operator new(size_t S) { return User::operator new(S, 3); } |
541 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
542 | |
543 | using VolatileField = BoolBitfieldElementT<0>; |
544 | using WeakField = BoolBitfieldElementT<VolatileField::NextBit>; |
545 | using SuccessOrderingField = |
546 | AtomicOrderingBitfieldElementT<WeakField::NextBit>; |
547 | using FailureOrderingField = |
548 | AtomicOrderingBitfieldElementT<SuccessOrderingField::NextBit>; |
549 | using AlignmentField = |
550 | AlignmentBitfieldElementT<FailureOrderingField::NextBit>; |
551 | static_assert( |
552 | Bitfield::areContiguous<VolatileField, WeakField, SuccessOrderingField, |
553 | FailureOrderingField, AlignmentField>(), |
554 | "Bitfields must be contiguous"); |
555 | |
556 | /// Return the alignment of the memory that is being allocated by the |
557 | /// instruction. |
558 | Align getAlign() const { |
559 | return Align(1ULL << getSubclassData<AlignmentField>()); |
560 | } |
561 | |
562 | void setAlignment(Align Align) { |
563 | setSubclassData<AlignmentField>(Log2(Align)); |
564 | } |
565 | |
566 | /// Return true if this is a cmpxchg from a volatile memory |
567 | /// location. |
568 | /// |
569 | bool isVolatile() const { return getSubclassData<VolatileField>(); } |
570 | |
571 | /// Specify whether this is a volatile cmpxchg. |
572 | /// |
573 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } |
574 | |
575 | /// Return true if this cmpxchg may spuriously fail. |
576 | bool isWeak() const { return getSubclassData<WeakField>(); } |
577 | |
578 | void setWeak(bool IsWeak) { setSubclassData<WeakField>(IsWeak); } |
579 | |
580 | /// Transparently provide more efficient getOperand methods. |
581 | 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; |
582 | |
583 | static bool isValidSuccessOrdering(AtomicOrdering Ordering) { |
584 | return Ordering != AtomicOrdering::NotAtomic && |
585 | Ordering != AtomicOrdering::Unordered; |
586 | } |
587 | |
588 | static bool isValidFailureOrdering(AtomicOrdering Ordering) { |
589 | return Ordering != AtomicOrdering::NotAtomic && |
590 | Ordering != AtomicOrdering::Unordered && |
591 | Ordering != AtomicOrdering::AcquireRelease && |
592 | Ordering != AtomicOrdering::Release; |
593 | } |
594 | |
595 | /// Returns the success ordering constraint of this cmpxchg instruction. |
596 | AtomicOrdering getSuccessOrdering() const { |
597 | return getSubclassData<SuccessOrderingField>(); |
598 | } |
599 | |
600 | /// Sets the success ordering constraint of this cmpxchg instruction. |
601 | void setSuccessOrdering(AtomicOrdering Ordering) { |
602 | assert(isValidSuccessOrdering(Ordering) &&(static_cast <bool> (isValidSuccessOrdering(Ordering) && "invalid CmpXchg success ordering") ? void (0) : __assert_fail ("isValidSuccessOrdering(Ordering) && \"invalid CmpXchg success ordering\"" , "llvm/include/llvm/IR/Instructions.h", 603, __extension__ __PRETTY_FUNCTION__ )) |
603 | "invalid CmpXchg success ordering")(static_cast <bool> (isValidSuccessOrdering(Ordering) && "invalid CmpXchg success ordering") ? void (0) : __assert_fail ("isValidSuccessOrdering(Ordering) && \"invalid CmpXchg success ordering\"" , "llvm/include/llvm/IR/Instructions.h", 603, __extension__ __PRETTY_FUNCTION__ )); |
604 | setSubclassData<SuccessOrderingField>(Ordering); |
605 | } |
606 | |
607 | /// Returns the failure ordering constraint of this cmpxchg instruction. |
608 | AtomicOrdering getFailureOrdering() const { |
609 | return getSubclassData<FailureOrderingField>(); |
610 | } |
611 | |
612 | /// Sets the failure ordering constraint of this cmpxchg instruction. |
613 | void setFailureOrdering(AtomicOrdering Ordering) { |
614 | assert(isValidFailureOrdering(Ordering) &&(static_cast <bool> (isValidFailureOrdering(Ordering) && "invalid CmpXchg failure ordering") ? void (0) : __assert_fail ("isValidFailureOrdering(Ordering) && \"invalid CmpXchg failure ordering\"" , "llvm/include/llvm/IR/Instructions.h", 615, __extension__ __PRETTY_FUNCTION__ )) |
615 | "invalid CmpXchg failure ordering")(static_cast <bool> (isValidFailureOrdering(Ordering) && "invalid CmpXchg failure ordering") ? void (0) : __assert_fail ("isValidFailureOrdering(Ordering) && \"invalid CmpXchg failure ordering\"" , "llvm/include/llvm/IR/Instructions.h", 615, __extension__ __PRETTY_FUNCTION__ )); |
616 | setSubclassData<FailureOrderingField>(Ordering); |
617 | } |
618 | |
619 | /// Returns a single ordering which is at least as strong as both the |
620 | /// success and failure orderings for this cmpxchg. |
621 | AtomicOrdering getMergedOrdering() const { |
622 | if (getFailureOrdering() == AtomicOrdering::SequentiallyConsistent) |
623 | return AtomicOrdering::SequentiallyConsistent; |
624 | if (getFailureOrdering() == AtomicOrdering::Acquire) { |
625 | if (getSuccessOrdering() == AtomicOrdering::Monotonic) |
626 | return AtomicOrdering::Acquire; |
627 | if (getSuccessOrdering() == AtomicOrdering::Release) |
628 | return AtomicOrdering::AcquireRelease; |
629 | } |
630 | return getSuccessOrdering(); |
631 | } |
632 | |
633 | /// Returns the synchronization scope ID of this cmpxchg instruction. |
634 | SyncScope::ID getSyncScopeID() const { |
635 | return SSID; |
636 | } |
637 | |
638 | /// Sets the synchronization scope ID of this cmpxchg instruction. |
639 | void setSyncScopeID(SyncScope::ID SSID) { |
640 | this->SSID = SSID; |
641 | } |
642 | |
643 | Value *getPointerOperand() { return getOperand(0); } |
644 | const Value *getPointerOperand() const { return getOperand(0); } |
645 | static unsigned getPointerOperandIndex() { return 0U; } |
646 | |
647 | Value *getCompareOperand() { return getOperand(1); } |
648 | const Value *getCompareOperand() const { return getOperand(1); } |
649 | |
650 | Value *getNewValOperand() { return getOperand(2); } |
651 | const Value *getNewValOperand() const { return getOperand(2); } |
652 | |
653 | /// Returns the address space of the pointer operand. |
654 | unsigned getPointerAddressSpace() const { |
655 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
656 | } |
657 | |
658 | /// Returns the strongest permitted ordering on failure, given the |
659 | /// desired ordering on success. |
660 | /// |
661 | /// If the comparison in a cmpxchg operation fails, there is no atomic store |
662 | /// so release semantics cannot be provided. So this function drops explicit |
663 | /// Release requests from the AtomicOrdering. A SequentiallyConsistent |
664 | /// operation would remain SequentiallyConsistent. |
665 | static AtomicOrdering |
666 | getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) { |
667 | switch (SuccessOrdering) { |
668 | default: |
669 | llvm_unreachable("invalid cmpxchg success ordering")::llvm::llvm_unreachable_internal("invalid cmpxchg success ordering" , "llvm/include/llvm/IR/Instructions.h", 669); |
670 | case AtomicOrdering::Release: |
671 | case AtomicOrdering::Monotonic: |
672 | return AtomicOrdering::Monotonic; |
673 | case AtomicOrdering::AcquireRelease: |
674 | case AtomicOrdering::Acquire: |
675 | return AtomicOrdering::Acquire; |
676 | case AtomicOrdering::SequentiallyConsistent: |
677 | return AtomicOrdering::SequentiallyConsistent; |
678 | } |
679 | } |
680 | |
681 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
682 | static bool classof(const Instruction *I) { |
683 | return I->getOpcode() == Instruction::AtomicCmpXchg; |
684 | } |
685 | static bool classof(const Value *V) { |
686 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
687 | } |
688 | |
689 | private: |
690 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
691 | // method so that subclasses cannot accidentally use it. |
692 | template <typename Bitfield> |
693 | void setSubclassData(typename Bitfield::Type Value) { |
694 | Instruction::setSubclassData<Bitfield>(Value); |
695 | } |
696 | |
697 | /// The synchronization scope ID of this cmpxchg instruction. Not quite |
698 | /// enough room in SubClassData for everything, so synchronization scope ID |
699 | /// gets its own field. |
700 | SyncScope::ID SSID; |
701 | }; |
702 | |
703 | template <> |
704 | struct OperandTraits<AtomicCmpXchgInst> : |
705 | public FixedNumOperandTraits<AtomicCmpXchgInst, 3> { |
706 | }; |
707 | |
708 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 708, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicCmpXchgInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 708, __extension__ __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 ); } |
709 | |
710 | //===----------------------------------------------------------------------===// |
711 | // AtomicRMWInst Class |
712 | //===----------------------------------------------------------------------===// |
713 | |
714 | /// an instruction that atomically reads a memory location, |
715 | /// combines it with another value, and then stores the result back. Returns |
716 | /// the old value. |
717 | /// |
718 | class AtomicRMWInst : public Instruction { |
719 | protected: |
720 | // Note: Instruction needs to be a friend here to call cloneImpl. |
721 | friend class Instruction; |
722 | |
723 | AtomicRMWInst *cloneImpl() const; |
724 | |
725 | public: |
726 | /// This enumeration lists the possible modifications atomicrmw can make. In |
727 | /// the descriptions, 'p' is the pointer to the instruction's memory location, |
728 | /// 'old' is the initial value of *p, and 'v' is the other value passed to the |
729 | /// instruction. These instructions always return 'old'. |
730 | enum BinOp : unsigned { |
731 | /// *p = v |
732 | Xchg, |
733 | /// *p = old + v |
734 | Add, |
735 | /// *p = old - v |
736 | Sub, |
737 | /// *p = old & v |
738 | And, |
739 | /// *p = ~(old & v) |
740 | Nand, |
741 | /// *p = old | v |
742 | Or, |
743 | /// *p = old ^ v |
744 | Xor, |
745 | /// *p = old >signed v ? old : v |
746 | Max, |
747 | /// *p = old <signed v ? old : v |
748 | Min, |
749 | /// *p = old >unsigned v ? old : v |
750 | UMax, |
751 | /// *p = old <unsigned v ? old : v |
752 | UMin, |
753 | |
754 | /// *p = old + v |
755 | FAdd, |
756 | |
757 | /// *p = old - v |
758 | FSub, |
759 | |
760 | /// *p = maxnum(old, v) |
761 | /// \p maxnum matches the behavior of \p llvm.maxnum.*. |
762 | FMax, |
763 | |
764 | /// *p = minnum(old, v) |
765 | /// \p minnum matches the behavior of \p llvm.minnum.*. |
766 | FMin, |
767 | |
768 | /// Increment one up to a maximum value. |
769 | /// *p = (old u>= v) ? 0 : (old + 1) |
770 | UIncWrap, |
771 | |
772 | /// Decrement one until a minimum value or zero. |
773 | /// *p = ((old == 0) || (old u> v)) ? v : (old - 1) |
774 | UDecWrap, |
775 | |
776 | FIRST_BINOP = Xchg, |
777 | LAST_BINOP = UDecWrap, |
778 | BAD_BINOP |
779 | }; |
780 | |
781 | private: |
782 | template <unsigned Offset> |
783 | using AtomicOrderingBitfieldElement = |
784 | typename Bitfield::Element<AtomicOrdering, Offset, 3, |
785 | AtomicOrdering::LAST>; |
786 | |
787 | template <unsigned Offset> |
788 | using BinOpBitfieldElement = |
789 | typename Bitfield::Element<BinOp, Offset, 5, BinOp::LAST_BINOP>; |
790 | |
791 | public: |
792 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment, |
793 | AtomicOrdering Ordering, SyncScope::ID SSID, |
794 | Instruction *InsertBefore = nullptr); |
795 | AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment, |
796 | AtomicOrdering Ordering, SyncScope::ID SSID, |
797 | BasicBlock *InsertAtEnd); |
798 | |
799 | // allocate space for exactly two operands |
800 | void *operator new(size_t S) { return User::operator new(S, 2); } |
801 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
802 | |
803 | using VolatileField = BoolBitfieldElementT<0>; |
804 | using AtomicOrderingField = |
805 | AtomicOrderingBitfieldElementT<VolatileField::NextBit>; |
806 | using OperationField = BinOpBitfieldElement<AtomicOrderingField::NextBit>; |
807 | using AlignmentField = AlignmentBitfieldElementT<OperationField::NextBit>; |
808 | static_assert(Bitfield::areContiguous<VolatileField, AtomicOrderingField, |
809 | OperationField, AlignmentField>(), |
810 | "Bitfields must be contiguous"); |
811 | |
812 | BinOp getOperation() const { return getSubclassData<OperationField>(); } |
813 | |
814 | static StringRef getOperationName(BinOp Op); |
815 | |
816 | static bool isFPOperation(BinOp Op) { |
817 | switch (Op) { |
818 | case AtomicRMWInst::FAdd: |
819 | case AtomicRMWInst::FSub: |
820 | case AtomicRMWInst::FMax: |
821 | case AtomicRMWInst::FMin: |
822 | return true; |
823 | default: |
824 | return false; |
825 | } |
826 | } |
827 | |
828 | void setOperation(BinOp Operation) { |
829 | setSubclassData<OperationField>(Operation); |
830 | } |
831 | |
832 | /// Return the alignment of the memory that is being allocated by the |
833 | /// instruction. |
834 | Align getAlign() const { |
835 | return Align(1ULL << getSubclassData<AlignmentField>()); |
836 | } |
837 | |
838 | void setAlignment(Align Align) { |
839 | setSubclassData<AlignmentField>(Log2(Align)); |
840 | } |
841 | |
842 | /// Return true if this is a RMW on a volatile memory location. |
843 | /// |
844 | bool isVolatile() const { return getSubclassData<VolatileField>(); } |
845 | |
846 | /// Specify whether this is a volatile RMW or not. |
847 | /// |
848 | void setVolatile(bool V) { setSubclassData<VolatileField>(V); } |
849 | |
850 | /// Transparently provide more efficient getOperand methods. |
851 | 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; |
852 | |
853 | /// Returns the ordering constraint of this rmw instruction. |
854 | AtomicOrdering getOrdering() const { |
855 | return getSubclassData<AtomicOrderingField>(); |
856 | } |
857 | |
858 | /// Sets the ordering constraint of this rmw instruction. |
859 | void setOrdering(AtomicOrdering Ordering) { |
860 | assert(Ordering != AtomicOrdering::NotAtomic &&(static_cast <bool> (Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic.") ? void (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\"" , "llvm/include/llvm/IR/Instructions.h", 861, __extension__ __PRETTY_FUNCTION__ )) |
861 | "atomicrmw instructions can only be atomic.")(static_cast <bool> (Ordering != AtomicOrdering::NotAtomic && "atomicrmw instructions can only be atomic.") ? void (0) : __assert_fail ("Ordering != AtomicOrdering::NotAtomic && \"atomicrmw instructions can only be atomic.\"" , "llvm/include/llvm/IR/Instructions.h", 861, __extension__ __PRETTY_FUNCTION__ )); |
862 | assert(Ordering != AtomicOrdering::Unordered &&(static_cast <bool> (Ordering != AtomicOrdering::Unordered && "atomicrmw instructions cannot be unordered.") ? void (0) : __assert_fail ("Ordering != AtomicOrdering::Unordered && \"atomicrmw instructions cannot be unordered.\"" , "llvm/include/llvm/IR/Instructions.h", 863, __extension__ __PRETTY_FUNCTION__ )) |
863 | "atomicrmw instructions cannot be unordered.")(static_cast <bool> (Ordering != AtomicOrdering::Unordered && "atomicrmw instructions cannot be unordered.") ? void (0) : __assert_fail ("Ordering != AtomicOrdering::Unordered && \"atomicrmw instructions cannot be unordered.\"" , "llvm/include/llvm/IR/Instructions.h", 863, __extension__ __PRETTY_FUNCTION__ )); |
864 | setSubclassData<AtomicOrderingField>(Ordering); |
865 | } |
866 | |
867 | /// Returns the synchronization scope ID of this rmw instruction. |
868 | SyncScope::ID getSyncScopeID() const { |
869 | return SSID; |
870 | } |
871 | |
872 | /// Sets the synchronization scope ID of this rmw instruction. |
873 | void setSyncScopeID(SyncScope::ID SSID) { |
874 | this->SSID = SSID; |
875 | } |
876 | |
877 | Value *getPointerOperand() { return getOperand(0); } |
878 | const Value *getPointerOperand() const { return getOperand(0); } |
879 | static unsigned getPointerOperandIndex() { return 0U; } |
880 | |
881 | Value *getValOperand() { return getOperand(1); } |
882 | const Value *getValOperand() const { return getOperand(1); } |
883 | |
884 | /// Returns the address space of the pointer operand. |
885 | unsigned getPointerAddressSpace() const { |
886 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
887 | } |
888 | |
889 | bool isFloatingPointOperation() const { |
890 | return isFPOperation(getOperation()); |
891 | } |
892 | |
893 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
894 | static bool classof(const Instruction *I) { |
895 | return I->getOpcode() == Instruction::AtomicRMW; |
896 | } |
897 | static bool classof(const Value *V) { |
898 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
899 | } |
900 | |
901 | private: |
902 | void Init(BinOp Operation, Value *Ptr, Value *Val, Align Align, |
903 | AtomicOrdering Ordering, SyncScope::ID SSID); |
904 | |
905 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
906 | // method so that subclasses cannot accidentally use it. |
907 | template <typename Bitfield> |
908 | void setSubclassData(typename Bitfield::Type Value) { |
909 | Instruction::setSubclassData<Bitfield>(Value); |
910 | } |
911 | |
912 | /// The synchronization scope ID of this rmw instruction. Not quite enough |
913 | /// room in SubClassData for everything, so synchronization scope ID gets its |
914 | /// own field. |
915 | SyncScope::ID SSID; |
916 | }; |
917 | |
918 | template <> |
919 | struct OperandTraits<AtomicRMWInst> |
920 | : public FixedNumOperandTraits<AtomicRMWInst,2> { |
921 | }; |
922 | |
923 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 923, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<AtomicRMWInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 923, __extension__ __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 ); } |
924 | |
925 | //===----------------------------------------------------------------------===// |
926 | // GetElementPtrInst Class |
927 | //===----------------------------------------------------------------------===// |
928 | |
929 | // checkGEPType - Simple wrapper function to give a better assertion failure |
930 | // message on bad indexes for a gep instruction. |
931 | // |
932 | inline Type *checkGEPType(Type *Ty) { |
933 | assert(Ty && "Invalid GetElementPtrInst indices for type!")(static_cast <bool> (Ty && "Invalid GetElementPtrInst indices for type!" ) ? void (0) : __assert_fail ("Ty && \"Invalid GetElementPtrInst indices for type!\"" , "llvm/include/llvm/IR/Instructions.h", 933, __extension__ __PRETTY_FUNCTION__ )); |
934 | return Ty; |
935 | } |
936 | |
937 | /// an instruction for type-safe pointer arithmetic to |
938 | /// access elements of arrays and structs |
939 | /// |
940 | class GetElementPtrInst : public Instruction { |
941 | Type *SourceElementType; |
942 | Type *ResultElementType; |
943 | |
944 | GetElementPtrInst(const GetElementPtrInst &GEPI); |
945 | |
946 | /// Constructors - Create a getelementptr instruction with a base pointer an |
947 | /// list of indices. The first ctor can optionally insert before an existing |
948 | /// instruction, the second appends the new instruction to the specified |
949 | /// BasicBlock. |
950 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, |
951 | ArrayRef<Value *> IdxList, unsigned Values, |
952 | const Twine &NameStr, Instruction *InsertBefore); |
953 | inline GetElementPtrInst(Type *PointeeType, Value *Ptr, |
954 | ArrayRef<Value *> IdxList, unsigned Values, |
955 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
956 | |
957 | void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr); |
958 | |
959 | protected: |
960 | // Note: Instruction needs to be a friend here to call cloneImpl. |
961 | friend class Instruction; |
962 | |
963 | GetElementPtrInst *cloneImpl() const; |
964 | |
965 | public: |
966 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, |
967 | ArrayRef<Value *> IdxList, |
968 | const Twine &NameStr = "", |
969 | Instruction *InsertBefore = nullptr) { |
970 | unsigned Values = 1 + unsigned(IdxList.size()); |
971 | assert(PointeeType && "Must specify element type")(static_cast <bool> (PointeeType && "Must specify element type" ) ? void (0) : __assert_fail ("PointeeType && \"Must specify element type\"" , "llvm/include/llvm/IR/Instructions.h", 971, __extension__ __PRETTY_FUNCTION__ )); |
972 | assert(cast<PointerType>(Ptr->getType()->getScalarType())(static_cast <bool> (cast<PointerType>(Ptr->getType ()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType )) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)" , "llvm/include/llvm/IR/Instructions.h", 973, __extension__ __PRETTY_FUNCTION__ )) |
973 | ->isOpaqueOrPointeeTypeMatches(PointeeType))(static_cast <bool> (cast<PointerType>(Ptr->getType ()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType )) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)" , "llvm/include/llvm/IR/Instructions.h", 973, __extension__ __PRETTY_FUNCTION__ )); |
974 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, |
975 | NameStr, InsertBefore); |
976 | } |
977 | |
978 | static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr, |
979 | ArrayRef<Value *> IdxList, |
980 | const Twine &NameStr, |
981 | BasicBlock *InsertAtEnd) { |
982 | unsigned Values = 1 + unsigned(IdxList.size()); |
983 | assert(PointeeType && "Must specify element type")(static_cast <bool> (PointeeType && "Must specify element type" ) ? void (0) : __assert_fail ("PointeeType && \"Must specify element type\"" , "llvm/include/llvm/IR/Instructions.h", 983, __extension__ __PRETTY_FUNCTION__ )); |
984 | assert(cast<PointerType>(Ptr->getType()->getScalarType())(static_cast <bool> (cast<PointerType>(Ptr->getType ()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType )) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)" , "llvm/include/llvm/IR/Instructions.h", 985, __extension__ __PRETTY_FUNCTION__ )) |
985 | ->isOpaqueOrPointeeTypeMatches(PointeeType))(static_cast <bool> (cast<PointerType>(Ptr->getType ()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType )) ? void (0) : __assert_fail ("cast<PointerType>(Ptr->getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(PointeeType)" , "llvm/include/llvm/IR/Instructions.h", 985, __extension__ __PRETTY_FUNCTION__ )); |
986 | return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values, |
987 | NameStr, InsertAtEnd); |
988 | } |
989 | |
990 | /// Create an "inbounds" getelementptr. See the documentation for the |
991 | /// "inbounds" flag in LangRef.html for details. |
992 | static GetElementPtrInst * |
993 | CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList, |
994 | const Twine &NameStr = "", |
995 | Instruction *InsertBefore = nullptr) { |
996 | GetElementPtrInst *GEP = |
997 | Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore); |
998 | GEP->setIsInBounds(true); |
999 | return GEP; |
1000 | } |
1001 | |
1002 | static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr, |
1003 | ArrayRef<Value *> IdxList, |
1004 | const Twine &NameStr, |
1005 | BasicBlock *InsertAtEnd) { |
1006 | GetElementPtrInst *GEP = |
1007 | Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd); |
1008 | GEP->setIsInBounds(true); |
1009 | return GEP; |
1010 | } |
1011 | |
1012 | /// Transparently provide more efficient getOperand methods. |
1013 | 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; |
1014 | |
1015 | Type *getSourceElementType() const { return SourceElementType; } |
1016 | |
1017 | void setSourceElementType(Type *Ty) { SourceElementType = Ty; } |
1018 | void setResultElementType(Type *Ty) { ResultElementType = Ty; } |
1019 | |
1020 | Type *getResultElementType() const { |
1021 | assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()-> getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType )) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)" , "llvm/include/llvm/IR/Instructions.h", 1022, __extension__ __PRETTY_FUNCTION__ )) |
1022 | ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()-> getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType )) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)" , "llvm/include/llvm/IR/Instructions.h", 1022, __extension__ __PRETTY_FUNCTION__ )); |
1023 | return ResultElementType; |
1024 | } |
1025 | |
1026 | /// Returns the address space of this instruction's pointer type. |
1027 | unsigned getAddressSpace() const { |
1028 | // Note that this is always the same as the pointer operand's address space |
1029 | // and that is cheaper to compute, so cheat here. |
1030 | return getPointerAddressSpace(); |
1031 | } |
1032 | |
1033 | /// Returns the result type of a getelementptr with the given source |
1034 | /// element type and indexes. |
1035 | /// |
1036 | /// Null is returned if the indices are invalid for the specified |
1037 | /// source element type. |
1038 | static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList); |
1039 | static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList); |
1040 | static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList); |
1041 | |
1042 | /// Return the type of the element at the given index of an indexable |
1043 | /// type. This is equivalent to "getIndexedType(Agg, {Zero, Idx})". |
1044 | /// |
1045 | /// Returns null if the type can't be indexed, or the given index is not |
1046 | /// legal for the given type. |
1047 | static Type *getTypeAtIndex(Type *Ty, Value *Idx); |
1048 | static Type *getTypeAtIndex(Type *Ty, uint64_t Idx); |
1049 | |
1050 | inline op_iterator idx_begin() { return op_begin()+1; } |
1051 | inline const_op_iterator idx_begin() const { return op_begin()+1; } |
1052 | inline op_iterator idx_end() { return op_end(); } |
1053 | inline const_op_iterator idx_end() const { return op_end(); } |
1054 | |
1055 | inline iterator_range<op_iterator> indices() { |
1056 | return make_range(idx_begin(), idx_end()); |
1057 | } |
1058 | |
1059 | inline iterator_range<const_op_iterator> indices() const { |
1060 | return make_range(idx_begin(), idx_end()); |
1061 | } |
1062 | |
1063 | Value *getPointerOperand() { |
1064 | return getOperand(0); |
1065 | } |
1066 | const Value *getPointerOperand() const { |
1067 | return getOperand(0); |
1068 | } |
1069 | static unsigned getPointerOperandIndex() { |
1070 | return 0U; // get index for modifying correct operand. |
1071 | } |
1072 | |
1073 | /// Method to return the pointer operand as a |
1074 | /// PointerType. |
1075 | Type *getPointerOperandType() const { |
1076 | return getPointerOperand()->getType(); |
1077 | } |
1078 | |
1079 | /// Returns the address space of the pointer operand. |
1080 | unsigned getPointerAddressSpace() const { |
1081 | return getPointerOperandType()->getPointerAddressSpace(); |
1082 | } |
1083 | |
1084 | /// Returns the pointer type returned by the GEP |
1085 | /// instruction, which may be a vector of pointers. |
1086 | static Type *getGEPReturnType(Type *ElTy, Value *Ptr, |
1087 | ArrayRef<Value *> IdxList) { |
1088 | PointerType *OrigPtrTy = cast<PointerType>(Ptr->getType()->getScalarType()); |
1089 | unsigned AddrSpace = OrigPtrTy->getAddressSpace(); |
1090 | Type *ResultElemTy = checkGEPType(getIndexedType(ElTy, IdxList)); |
1091 | Type *PtrTy = OrigPtrTy->isOpaque() |
1092 | ? PointerType::get(OrigPtrTy->getContext(), AddrSpace) |
1093 | : PointerType::get(ResultElemTy, AddrSpace); |
1094 | // Vector GEP |
1095 | if (auto *PtrVTy = dyn_cast<VectorType>(Ptr->getType())) { |
1096 | ElementCount EltCount = PtrVTy->getElementCount(); |
1097 | return VectorType::get(PtrTy, EltCount); |
1098 | } |
1099 | for (Value *Index : IdxList) |
1100 | if (auto *IndexVTy = dyn_cast<VectorType>(Index->getType())) { |
1101 | ElementCount EltCount = IndexVTy->getElementCount(); |
1102 | return VectorType::get(PtrTy, EltCount); |
1103 | } |
1104 | // Scalar GEP |
1105 | return PtrTy; |
1106 | } |
1107 | |
1108 | unsigned getNumIndices() const { // Note: always non-negative |
1109 | return getNumOperands() - 1; |
1110 | } |
1111 | |
1112 | bool hasIndices() const { |
1113 | return getNumOperands() > 1; |
1114 | } |
1115 | |
1116 | /// Return true if all of the indices of this GEP are |
1117 | /// zeros. If so, the result pointer and the first operand have the same |
1118 | /// value, just potentially different types. |
1119 | bool hasAllZeroIndices() const; |
1120 | |
1121 | /// Return true if all of the indices of this GEP are |
1122 | /// constant integers. If so, the result pointer and the first operand have |
1123 | /// a constant offset between them. |
1124 | bool hasAllConstantIndices() const; |
1125 | |
1126 | /// Set or clear the inbounds flag on this GEP instruction. |
1127 | /// See LangRef.html for the meaning of inbounds on a getelementptr. |
1128 | void setIsInBounds(bool b = true); |
1129 | |
1130 | /// Determine whether the GEP has the inbounds flag. |
1131 | bool isInBounds() const; |
1132 | |
1133 | /// Accumulate the constant address offset of this GEP if possible. |
1134 | /// |
1135 | /// This routine accepts an APInt into which it will accumulate the constant |
1136 | /// offset of this GEP if the GEP is in fact constant. If the GEP is not |
1137 | /// all-constant, it returns false and the value of the offset APInt is |
1138 | /// undefined (it is *not* preserved!). The APInt passed into this routine |
1139 | /// must be at least as wide as the IntPtr type for the address space of |
1140 | /// the base GEP pointer. |
1141 | bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const; |
1142 | bool collectOffset(const DataLayout &DL, unsigned BitWidth, |
1143 | MapVector<Value *, APInt> &VariableOffsets, |
1144 | APInt &ConstantOffset) const; |
1145 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1146 | static bool classof(const Instruction *I) { |
1147 | return (I->getOpcode() == Instruction::GetElementPtr); |
1148 | } |
1149 | static bool classof(const Value *V) { |
1150 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1151 | } |
1152 | }; |
1153 | |
1154 | template <> |
1155 | struct OperandTraits<GetElementPtrInst> : |
1156 | public VariadicOperandTraits<GetElementPtrInst, 1> { |
1157 | }; |
1158 | |
1159 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, |
1160 | ArrayRef<Value *> IdxList, unsigned Values, |
1161 | const Twine &NameStr, |
1162 | Instruction *InsertBefore) |
1163 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, |
1164 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, |
1165 | Values, InsertBefore), |
1166 | SourceElementType(PointeeType), |
1167 | ResultElementType(getIndexedType(PointeeType, IdxList)) { |
1168 | assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()-> getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType )) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)" , "llvm/include/llvm/IR/Instructions.h", 1169, __extension__ __PRETTY_FUNCTION__ )) |
1169 | ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()-> getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType )) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)" , "llvm/include/llvm/IR/Instructions.h", 1169, __extension__ __PRETTY_FUNCTION__ )); |
1170 | init(Ptr, IdxList, NameStr); |
1171 | } |
1172 | |
1173 | GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr, |
1174 | ArrayRef<Value *> IdxList, unsigned Values, |
1175 | const Twine &NameStr, |
1176 | BasicBlock *InsertAtEnd) |
1177 | : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr, |
1178 | OperandTraits<GetElementPtrInst>::op_end(this) - Values, |
1179 | Values, InsertAtEnd), |
1180 | SourceElementType(PointeeType), |
1181 | ResultElementType(getIndexedType(PointeeType, IdxList)) { |
1182 | assert(cast<PointerType>(getType()->getScalarType())(static_cast <bool> (cast<PointerType>(getType()-> getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType )) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)" , "llvm/include/llvm/IR/Instructions.h", 1183, __extension__ __PRETTY_FUNCTION__ )) |
1183 | ->isOpaqueOrPointeeTypeMatches(ResultElementType))(static_cast <bool> (cast<PointerType>(getType()-> getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType )) ? void (0) : __assert_fail ("cast<PointerType>(getType()->getScalarType()) ->isOpaqueOrPointeeTypeMatches(ResultElementType)" , "llvm/include/llvm/IR/Instructions.h", 1183, __extension__ __PRETTY_FUNCTION__ )); |
1184 | init(Ptr, IdxList, NameStr); |
1185 | } |
1186 | |
1187 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 1187, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<GetElementPtrInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 1187, __extension__ __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 ); } |
1188 | |
1189 | //===----------------------------------------------------------------------===// |
1190 | // ICmpInst Class |
1191 | //===----------------------------------------------------------------------===// |
1192 | |
1193 | /// This instruction compares its operands according to the predicate given |
1194 | /// to the constructor. It only operates on integers or pointers. The operands |
1195 | /// must be identical types. |
1196 | /// Represent an integer comparison operator. |
1197 | class ICmpInst: public CmpInst { |
1198 | void AssertOK() { |
1199 | assert(isIntPredicate() &&(static_cast <bool> (isIntPredicate() && "Invalid ICmp predicate value" ) ? void (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\"" , "llvm/include/llvm/IR/Instructions.h", 1200, __extension__ __PRETTY_FUNCTION__ )) |
1200 | "Invalid ICmp predicate value")(static_cast <bool> (isIntPredicate() && "Invalid ICmp predicate value" ) ? void (0) : __assert_fail ("isIntPredicate() && \"Invalid ICmp predicate value\"" , "llvm/include/llvm/IR/Instructions.h", 1200, __extension__ __PRETTY_FUNCTION__ )); |
1201 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&(static_cast <bool> (getOperand(0)->getType() == getOperand (1)->getType() && "Both operands to ICmp instruction are not of the same type!" ) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\"" , "llvm/include/llvm/IR/Instructions.h", 1202, __extension__ __PRETTY_FUNCTION__ )) |
1202 | "Both operands to ICmp instruction are not of the same type!")(static_cast <bool> (getOperand(0)->getType() == getOperand (1)->getType() && "Both operands to ICmp instruction are not of the same type!" ) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to ICmp instruction are not of the same type!\"" , "llvm/include/llvm/IR/Instructions.h", 1202, __extension__ __PRETTY_FUNCTION__ )); |
1203 | // Check that the operands are the right type |
1204 | assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy () || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction") ? void (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "llvm/include/llvm/IR/Instructions.h", 1206, __extension__ __PRETTY_FUNCTION__ )) |
1205 | getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy () || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction") ? void (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "llvm/include/llvm/IR/Instructions.h", 1206, __extension__ __PRETTY_FUNCTION__ )) |
1206 | "Invalid operand types for ICmp instruction")(static_cast <bool> ((getOperand(0)->getType()->isIntOrIntVectorTy () || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && "Invalid operand types for ICmp instruction") ? void (0) : __assert_fail ("(getOperand(0)->getType()->isIntOrIntVectorTy() || getOperand(0)->getType()->isPtrOrPtrVectorTy()) && \"Invalid operand types for ICmp instruction\"" , "llvm/include/llvm/IR/Instructions.h", 1206, __extension__ __PRETTY_FUNCTION__ )); |
1207 | } |
1208 | |
1209 | protected: |
1210 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1211 | friend class Instruction; |
1212 | |
1213 | /// Clone an identical ICmpInst |
1214 | ICmpInst *cloneImpl() const; |
1215 | |
1216 | public: |
1217 | /// Constructor with insert-before-instruction semantics. |
1218 | ICmpInst( |
1219 | Instruction *InsertBefore, ///< Where to insert |
1220 | Predicate pred, ///< The predicate to use for the comparison |
1221 | Value *LHS, ///< The left-hand-side of the expression |
1222 | Value *RHS, ///< The right-hand-side of the expression |
1223 | const Twine &NameStr = "" ///< Name of the instruction |
1224 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1225 | Instruction::ICmp, pred, LHS, RHS, NameStr, |
1226 | InsertBefore) { |
1227 | #ifndef NDEBUG |
1228 | AssertOK(); |
1229 | #endif |
1230 | } |
1231 | |
1232 | /// Constructor with insert-at-end semantics. |
1233 | ICmpInst( |
1234 | BasicBlock &InsertAtEnd, ///< Block to insert into. |
1235 | Predicate pred, ///< The predicate to use for the comparison |
1236 | Value *LHS, ///< The left-hand-side of the expression |
1237 | Value *RHS, ///< The right-hand-side of the expression |
1238 | const Twine &NameStr = "" ///< Name of the instruction |
1239 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1240 | Instruction::ICmp, pred, LHS, RHS, NameStr, |
1241 | &InsertAtEnd) { |
1242 | #ifndef NDEBUG |
1243 | AssertOK(); |
1244 | #endif |
1245 | } |
1246 | |
1247 | /// Constructor with no-insertion semantics |
1248 | ICmpInst( |
1249 | Predicate pred, ///< The predicate to use for the comparison |
1250 | Value *LHS, ///< The left-hand-side of the expression |
1251 | Value *RHS, ///< The right-hand-side of the expression |
1252 | const Twine &NameStr = "" ///< Name of the instruction |
1253 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1254 | Instruction::ICmp, pred, LHS, RHS, NameStr) { |
1255 | #ifndef NDEBUG |
1256 | AssertOK(); |
1257 | #endif |
1258 | } |
1259 | |
1260 | /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc. |
1261 | /// @returns the predicate that would be the result if the operand were |
1262 | /// regarded as signed. |
1263 | /// Return the signed version of the predicate |
1264 | Predicate getSignedPredicate() const { |
1265 | return getSignedPredicate(getPredicate()); |
1266 | } |
1267 | |
1268 | /// This is a static version that you can use without an instruction. |
1269 | /// Return the signed version of the predicate. |
1270 | static Predicate getSignedPredicate(Predicate pred); |
1271 | |
1272 | /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc. |
1273 | /// @returns the predicate that would be the result if the operand were |
1274 | /// regarded as unsigned. |
1275 | /// Return the unsigned version of the predicate |
1276 | Predicate getUnsignedPredicate() const { |
1277 | return getUnsignedPredicate(getPredicate()); |
1278 | } |
1279 | |
1280 | /// This is a static version that you can use without an instruction. |
1281 | /// Return the unsigned version of the predicate. |
1282 | static Predicate getUnsignedPredicate(Predicate pred); |
1283 | |
1284 | /// Return true if this predicate is either EQ or NE. This also |
1285 | /// tests for commutativity. |
1286 | static bool isEquality(Predicate P) { |
1287 | return P == ICMP_EQ || P == ICMP_NE; |
1288 | } |
1289 | |
1290 | /// Return true if this predicate is either EQ or NE. This also |
1291 | /// tests for commutativity. |
1292 | bool isEquality() const { |
1293 | return isEquality(getPredicate()); |
1294 | } |
1295 | |
1296 | /// @returns true if the predicate of this ICmpInst is commutative |
1297 | /// Determine if this relation is commutative. |
1298 | bool isCommutative() const { return isEquality(); } |
1299 | |
1300 | /// Return true if the predicate is relational (not EQ or NE). |
1301 | /// |
1302 | bool isRelational() const { |
1303 | return !isEquality(); |
1304 | } |
1305 | |
1306 | /// Return true if the predicate is relational (not EQ or NE). |
1307 | /// |
1308 | static bool isRelational(Predicate P) { |
1309 | return !isEquality(P); |
1310 | } |
1311 | |
1312 | /// Return true if the predicate is SGT or UGT. |
1313 | /// |
1314 | static bool isGT(Predicate P) { |
1315 | return P == ICMP_SGT || P == ICMP_UGT; |
1316 | } |
1317 | |
1318 | /// Return true if the predicate is SLT or ULT. |
1319 | /// |
1320 | static bool isLT(Predicate P) { |
1321 | return P == ICMP_SLT || P == ICMP_ULT; |
1322 | } |
1323 | |
1324 | /// Return true if the predicate is SGE or UGE. |
1325 | /// |
1326 | static bool isGE(Predicate P) { |
1327 | return P == ICMP_SGE || P == ICMP_UGE; |
1328 | } |
1329 | |
1330 | /// Return true if the predicate is SLE or ULE. |
1331 | /// |
1332 | static bool isLE(Predicate P) { |
1333 | return P == ICMP_SLE || P == ICMP_ULE; |
1334 | } |
1335 | |
1336 | /// Returns the sequence of all ICmp predicates. |
1337 | /// |
1338 | static auto predicates() { return ICmpPredicates(); } |
1339 | |
1340 | /// Exchange the two operands to this instruction in such a way that it does |
1341 | /// not modify the semantics of the instruction. The predicate value may be |
1342 | /// changed to retain the same result if the predicate is order dependent |
1343 | /// (e.g. ult). |
1344 | /// Swap operands and adjust predicate. |
1345 | void swapOperands() { |
1346 | setPredicate(getSwappedPredicate()); |
1347 | Op<0>().swap(Op<1>()); |
1348 | } |
1349 | |
1350 | /// Return result of `LHS Pred RHS` comparison. |
1351 | static bool compare(const APInt &LHS, const APInt &RHS, |
1352 | ICmpInst::Predicate Pred); |
1353 | |
1354 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1355 | static bool classof(const Instruction *I) { |
1356 | return I->getOpcode() == Instruction::ICmp; |
1357 | } |
1358 | static bool classof(const Value *V) { |
1359 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1360 | } |
1361 | }; |
1362 | |
1363 | //===----------------------------------------------------------------------===// |
1364 | // FCmpInst Class |
1365 | //===----------------------------------------------------------------------===// |
1366 | |
1367 | /// This instruction compares its operands according to the predicate given |
1368 | /// to the constructor. It only operates on floating point values or packed |
1369 | /// vectors of floating point values. The operands must be identical types. |
1370 | /// Represents a floating point comparison operator. |
1371 | class FCmpInst: public CmpInst { |
1372 | void AssertOK() { |
1373 | assert(isFPPredicate() && "Invalid FCmp predicate value")(static_cast <bool> (isFPPredicate() && "Invalid FCmp predicate value" ) ? void (0) : __assert_fail ("isFPPredicate() && \"Invalid FCmp predicate value\"" , "llvm/include/llvm/IR/Instructions.h", 1373, __extension__ __PRETTY_FUNCTION__ )); |
1374 | assert(getOperand(0)->getType() == getOperand(1)->getType() &&(static_cast <bool> (getOperand(0)->getType() == getOperand (1)->getType() && "Both operands to FCmp instruction are not of the same type!" ) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\"" , "llvm/include/llvm/IR/Instructions.h", 1375, __extension__ __PRETTY_FUNCTION__ )) |
1375 | "Both operands to FCmp instruction are not of the same type!")(static_cast <bool> (getOperand(0)->getType() == getOperand (1)->getType() && "Both operands to FCmp instruction are not of the same type!" ) ? void (0) : __assert_fail ("getOperand(0)->getType() == getOperand(1)->getType() && \"Both operands to FCmp instruction are not of the same type!\"" , "llvm/include/llvm/IR/Instructions.h", 1375, __extension__ __PRETTY_FUNCTION__ )); |
1376 | // Check that the operands are the right type |
1377 | assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&(static_cast <bool> (getOperand(0)->getType()->isFPOrFPVectorTy () && "Invalid operand types for FCmp instruction") ? void (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\"" , "llvm/include/llvm/IR/Instructions.h", 1378, __extension__ __PRETTY_FUNCTION__ )) |
1378 | "Invalid operand types for FCmp instruction")(static_cast <bool> (getOperand(0)->getType()->isFPOrFPVectorTy () && "Invalid operand types for FCmp instruction") ? void (0) : __assert_fail ("getOperand(0)->getType()->isFPOrFPVectorTy() && \"Invalid operand types for FCmp instruction\"" , "llvm/include/llvm/IR/Instructions.h", 1378, __extension__ __PRETTY_FUNCTION__ )); |
1379 | } |
1380 | |
1381 | protected: |
1382 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1383 | friend class Instruction; |
1384 | |
1385 | /// Clone an identical FCmpInst |
1386 | FCmpInst *cloneImpl() const; |
1387 | |
1388 | public: |
1389 | /// Constructor with insert-before-instruction semantics. |
1390 | FCmpInst( |
1391 | Instruction *InsertBefore, ///< Where to insert |
1392 | Predicate pred, ///< The predicate to use for the comparison |
1393 | Value *LHS, ///< The left-hand-side of the expression |
1394 | Value *RHS, ///< The right-hand-side of the expression |
1395 | const Twine &NameStr = "" ///< Name of the instruction |
1396 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1397 | Instruction::FCmp, pred, LHS, RHS, NameStr, |
1398 | InsertBefore) { |
1399 | AssertOK(); |
1400 | } |
1401 | |
1402 | /// Constructor with insert-at-end semantics. |
1403 | FCmpInst( |
1404 | BasicBlock &InsertAtEnd, ///< Block to insert into. |
1405 | Predicate pred, ///< The predicate to use for the comparison |
1406 | Value *LHS, ///< The left-hand-side of the expression |
1407 | Value *RHS, ///< The right-hand-side of the expression |
1408 | const Twine &NameStr = "" ///< Name of the instruction |
1409 | ) : CmpInst(makeCmpResultType(LHS->getType()), |
1410 | Instruction::FCmp, pred, LHS, RHS, NameStr, |
1411 | &InsertAtEnd) { |
1412 | AssertOK(); |
1413 | } |
1414 | |
1415 | /// Constructor with no-insertion semantics |
1416 | FCmpInst( |
1417 | Predicate Pred, ///< The predicate to use for the comparison |
1418 | Value *LHS, ///< The left-hand-side of the expression |
1419 | Value *RHS, ///< The right-hand-side of the expression |
1420 | const Twine &NameStr = "", ///< Name of the instruction |
1421 | Instruction *FlagsSource = nullptr |
1422 | ) : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS, |
1423 | RHS, NameStr, nullptr, FlagsSource) { |
1424 | AssertOK(); |
1425 | } |
1426 | |
1427 | /// @returns true if the predicate of this instruction is EQ or NE. |
1428 | /// Determine if this is an equality predicate. |
1429 | static bool isEquality(Predicate Pred) { |
1430 | return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ || |
1431 | Pred == FCMP_UNE; |
1432 | } |
1433 | |
1434 | /// @returns true if the predicate of this instruction is EQ or NE. |
1435 | /// Determine if this is an equality predicate. |
1436 | bool isEquality() const { return isEquality(getPredicate()); } |
1437 | |
1438 | /// @returns true if the predicate of this instruction is commutative. |
1439 | /// Determine if this is a commutative predicate. |
1440 | bool isCommutative() const { |
1441 | return isEquality() || |
1442 | getPredicate() == FCMP_FALSE || |
1443 | getPredicate() == FCMP_TRUE || |
1444 | getPredicate() == FCMP_ORD || |
1445 | getPredicate() == FCMP_UNO; |
1446 | } |
1447 | |
1448 | /// @returns true if the predicate is relational (not EQ or NE). |
1449 | /// Determine if this a relational predicate. |
1450 | bool isRelational() const { return !isEquality(); } |
1451 | |
1452 | /// Exchange the two operands to this instruction in such a way that it does |
1453 | /// not modify the semantics of the instruction. The predicate value may be |
1454 | /// changed to retain the same result if the predicate is order dependent |
1455 | /// (e.g. ult). |
1456 | /// Swap operands and adjust predicate. |
1457 | void swapOperands() { |
1458 | setPredicate(getSwappedPredicate()); |
1459 | Op<0>().swap(Op<1>()); |
1460 | } |
1461 | |
1462 | /// Returns the sequence of all FCmp predicates. |
1463 | /// |
1464 | static auto predicates() { return FCmpPredicates(); } |
1465 | |
1466 | /// Return result of `LHS Pred RHS` comparison. |
1467 | static bool compare(const APFloat &LHS, const APFloat &RHS, |
1468 | FCmpInst::Predicate Pred); |
1469 | |
1470 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
1471 | static bool classof(const Instruction *I) { |
1472 | return I->getOpcode() == Instruction::FCmp; |
1473 | } |
1474 | static bool classof(const Value *V) { |
1475 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1476 | } |
1477 | }; |
1478 | |
1479 | //===----------------------------------------------------------------------===// |
1480 | /// This class represents a function call, abstracting a target |
1481 | /// machine's calling convention. This class uses low bit of the SubClassData |
1482 | /// field to indicate whether or not this is a tail call. The rest of the bits |
1483 | /// hold the calling convention of the call. |
1484 | /// |
1485 | class CallInst : public CallBase { |
1486 | CallInst(const CallInst &CI); |
1487 | |
1488 | /// Construct a CallInst given a range of arguments. |
1489 | /// Construct a CallInst from a range of arguments |
1490 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1491 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
1492 | Instruction *InsertBefore); |
1493 | |
1494 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1495 | const Twine &NameStr, Instruction *InsertBefore) |
1496 | : CallInst(Ty, Func, Args, std::nullopt, NameStr, InsertBefore) {} |
1497 | |
1498 | /// Construct a CallInst given a range of arguments. |
1499 | /// Construct a CallInst from a range of arguments |
1500 | inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1501 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
1502 | BasicBlock *InsertAtEnd); |
1503 | |
1504 | explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr, |
1505 | Instruction *InsertBefore); |
1506 | |
1507 | CallInst(FunctionType *ty, Value *F, const Twine &NameStr, |
1508 | BasicBlock *InsertAtEnd); |
1509 | |
1510 | void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
1511 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
1512 | void init(FunctionType *FTy, Value *Func, const Twine &NameStr); |
1513 | |
1514 | /// Compute the number of operands to allocate. |
1515 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { |
1516 | // We need one operand for the called function, plus the input operand |
1517 | // counts provided. |
1518 | return 1 + NumArgs + NumBundleInputs; |
1519 | } |
1520 | |
1521 | protected: |
1522 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1523 | friend class Instruction; |
1524 | |
1525 | CallInst *cloneImpl() const; |
1526 | |
1527 | public: |
1528 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "", |
1529 | Instruction *InsertBefore = nullptr) { |
1530 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore); |
1531 | } |
1532 | |
1533 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1534 | const Twine &NameStr, |
1535 | Instruction *InsertBefore = nullptr) { |
1536 | return new (ComputeNumOperands(Args.size())) |
1537 | CallInst(Ty, Func, Args, std::nullopt, NameStr, InsertBefore); |
1538 | } |
1539 | |
1540 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1541 | ArrayRef<OperandBundleDef> Bundles = std::nullopt, |
1542 | const Twine &NameStr = "", |
1543 | Instruction *InsertBefore = nullptr) { |
1544 | const int NumOperands = |
1545 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
1546 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
1547 | |
1548 | return new (NumOperands, DescriptorBytes) |
1549 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore); |
1550 | } |
1551 | |
1552 | static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr, |
1553 | BasicBlock *InsertAtEnd) { |
1554 | return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertAtEnd); |
1555 | } |
1556 | |
1557 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1558 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1559 | return new (ComputeNumOperands(Args.size())) |
1560 | CallInst(Ty, Func, Args, std::nullopt, NameStr, InsertAtEnd); |
1561 | } |
1562 | |
1563 | static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1564 | ArrayRef<OperandBundleDef> Bundles, |
1565 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1566 | const int NumOperands = |
1567 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
1568 | const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
1569 | |
1570 | return new (NumOperands, DescriptorBytes) |
1571 | CallInst(Ty, Func, Args, Bundles, NameStr, InsertAtEnd); |
1572 | } |
1573 | |
1574 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "", |
1575 | Instruction *InsertBefore = nullptr) { |
1576 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, |
1577 | InsertBefore); |
1578 | } |
1579 | |
1580 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
1581 | ArrayRef<OperandBundleDef> Bundles = std::nullopt, |
1582 | const Twine &NameStr = "", |
1583 | Instruction *InsertBefore = nullptr) { |
1584 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, |
1585 | NameStr, InsertBefore); |
1586 | } |
1587 | |
1588 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
1589 | const Twine &NameStr, |
1590 | Instruction *InsertBefore = nullptr) { |
1591 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, |
1592 | InsertBefore); |
1593 | } |
1594 | |
1595 | static CallInst *Create(FunctionCallee Func, const Twine &NameStr, |
1596 | BasicBlock *InsertAtEnd) { |
1597 | return Create(Func.getFunctionType(), Func.getCallee(), NameStr, |
1598 | InsertAtEnd); |
1599 | } |
1600 | |
1601 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
1602 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1603 | return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr, |
1604 | InsertAtEnd); |
1605 | } |
1606 | |
1607 | static CallInst *Create(FunctionCallee Func, ArrayRef<Value *> Args, |
1608 | ArrayRef<OperandBundleDef> Bundles, |
1609 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
1610 | return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles, |
1611 | NameStr, InsertAtEnd); |
1612 | } |
1613 | |
1614 | /// Create a clone of \p CI with a different set of operand bundles and |
1615 | /// insert it before \p InsertPt. |
1616 | /// |
1617 | /// The returned call instruction is identical \p CI in every way except that |
1618 | /// the operand bundles for the new instruction are set to the operand bundles |
1619 | /// in \p Bundles. |
1620 | static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles, |
1621 | Instruction *InsertPt = nullptr); |
1622 | |
1623 | /// Generate the IR for a call to malloc: |
1624 | /// 1. Compute the malloc call's argument as the specified type's size, |
1625 | /// possibly multiplied by the array size if the array size is not |
1626 | /// constant 1. |
1627 | /// 2. Call malloc with that argument. |
1628 | /// 3. Bitcast the result of the malloc call to the specified type. |
1629 | static Instruction *CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, |
1630 | Type *AllocTy, Value *AllocSize, |
1631 | Value *ArraySize = nullptr, |
1632 | Function *MallocF = nullptr, |
1633 | const Twine &Name = ""); |
1634 | static Instruction *CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, |
1635 | Type *AllocTy, Value *AllocSize, |
1636 | Value *ArraySize = nullptr, |
1637 | Function *MallocF = nullptr, |
1638 | const Twine &Name = ""); |
1639 | static Instruction * |
1640 | CreateMalloc(Instruction *InsertBefore, Type *IntPtrTy, Type *AllocTy, |
1641 | Value *AllocSize, Value *ArraySize = nullptr, |
1642 | ArrayRef<OperandBundleDef> Bundles = std::nullopt, |
1643 | Function *MallocF = nullptr, const Twine &Name = ""); |
1644 | static Instruction * |
1645 | CreateMalloc(BasicBlock *InsertAtEnd, Type *IntPtrTy, Type *AllocTy, |
1646 | Value *AllocSize, Value *ArraySize = nullptr, |
1647 | ArrayRef<OperandBundleDef> Bundles = std::nullopt, |
1648 | Function *MallocF = nullptr, const Twine &Name = ""); |
1649 | /// Generate the IR for a call to the builtin free function. |
1650 | static Instruction *CreateFree(Value *Source, Instruction *InsertBefore); |
1651 | static Instruction *CreateFree(Value *Source, BasicBlock *InsertAtEnd); |
1652 | static Instruction *CreateFree(Value *Source, |
1653 | ArrayRef<OperandBundleDef> Bundles, |
1654 | Instruction *InsertBefore); |
1655 | static Instruction *CreateFree(Value *Source, |
1656 | ArrayRef<OperandBundleDef> Bundles, |
1657 | BasicBlock *InsertAtEnd); |
1658 | |
1659 | // Note that 'musttail' implies 'tail'. |
1660 | enum TailCallKind : unsigned { |
1661 | TCK_None = 0, |
1662 | TCK_Tail = 1, |
1663 | TCK_MustTail = 2, |
1664 | TCK_NoTail = 3, |
1665 | TCK_LAST = TCK_NoTail |
1666 | }; |
1667 | |
1668 | using TailCallKindField = Bitfield::Element<TailCallKind, 0, 2, TCK_LAST>; |
1669 | static_assert( |
1670 | Bitfield::areContiguous<TailCallKindField, CallBase::CallingConvField>(), |
1671 | "Bitfields must be contiguous"); |
1672 | |
1673 | TailCallKind getTailCallKind() const { |
1674 | return getSubclassData<TailCallKindField>(); |
1675 | } |
1676 | |
1677 | bool isTailCall() const { |
1678 | TailCallKind Kind = getTailCallKind(); |
1679 | return Kind == TCK_Tail || Kind == TCK_MustTail; |
1680 | } |
1681 | |
1682 | bool isMustTailCall() const { return getTailCallKind() == TCK_MustTail; } |
1683 | |
1684 | bool isNoTailCall() const { return getTailCallKind() == TCK_NoTail; } |
1685 | |
1686 | void setTailCallKind(TailCallKind TCK) { |
1687 | setSubclassData<TailCallKindField>(TCK); |
1688 | } |
1689 | |
1690 | void setTailCall(bool IsTc = true) { |
1691 | setTailCallKind(IsTc ? TCK_Tail : TCK_None); |
1692 | } |
1693 | |
1694 | /// Return true if the call can return twice |
1695 | bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); } |
1696 | void setCanReturnTwice() { addFnAttr(Attribute::ReturnsTwice); } |
1697 | |
1698 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1699 | static bool classof(const Instruction *I) { |
1700 | return I->getOpcode() == Instruction::Call; |
1701 | } |
1702 | static bool classof(const Value *V) { |
1703 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1704 | } |
1705 | |
1706 | /// Updates profile metadata by scaling it by \p S / \p T. |
1707 | void updateProfWeight(uint64_t S, uint64_t T); |
1708 | |
1709 | private: |
1710 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
1711 | // method so that subclasses cannot accidentally use it. |
1712 | template <typename Bitfield> |
1713 | void setSubclassData(typename Bitfield::Type Value) { |
1714 | Instruction::setSubclassData<Bitfield>(Value); |
1715 | } |
1716 | }; |
1717 | |
1718 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1719 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
1720 | BasicBlock *InsertAtEnd) |
1721 | : CallBase(Ty->getReturnType(), Instruction::Call, |
1722 | OperandTraits<CallBase>::op_end(this) - |
1723 | (Args.size() + CountBundleInputs(Bundles) + 1), |
1724 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), |
1725 | InsertAtEnd) { |
1726 | init(Ty, Func, Args, Bundles, NameStr); |
1727 | } |
1728 | |
1729 | CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args, |
1730 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr, |
1731 | Instruction *InsertBefore) |
1732 | : CallBase(Ty->getReturnType(), Instruction::Call, |
1733 | OperandTraits<CallBase>::op_end(this) - |
1734 | (Args.size() + CountBundleInputs(Bundles) + 1), |
1735 | unsigned(Args.size() + CountBundleInputs(Bundles) + 1), |
1736 | InsertBefore) { |
1737 | init(Ty, Func, Args, Bundles, NameStr); |
1738 | } |
1739 | |
1740 | //===----------------------------------------------------------------------===// |
1741 | // SelectInst Class |
1742 | //===----------------------------------------------------------------------===// |
1743 | |
1744 | /// This class represents the LLVM 'select' instruction. |
1745 | /// |
1746 | class SelectInst : public Instruction { |
1747 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, |
1748 | Instruction *InsertBefore) |
1749 | : Instruction(S1->getType(), Instruction::Select, |
1750 | &Op<0>(), 3, InsertBefore) { |
1751 | init(C, S1, S2); |
1752 | setName(NameStr); |
1753 | } |
1754 | |
1755 | SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr, |
1756 | BasicBlock *InsertAtEnd) |
1757 | : Instruction(S1->getType(), Instruction::Select, |
1758 | &Op<0>(), 3, InsertAtEnd) { |
1759 | init(C, S1, S2); |
1760 | setName(NameStr); |
1761 | } |
1762 | |
1763 | void init(Value *C, Value *S1, Value *S2) { |
1764 | assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select")(static_cast <bool> (!areInvalidOperands(C, S1, S2) && "Invalid operands for select") ? void (0) : __assert_fail ("!areInvalidOperands(C, S1, S2) && \"Invalid operands for select\"" , "llvm/include/llvm/IR/Instructions.h", 1764, __extension__ __PRETTY_FUNCTION__ )); |
1765 | Op<0>() = C; |
1766 | Op<1>() = S1; |
1767 | Op<2>() = S2; |
1768 | } |
1769 | |
1770 | protected: |
1771 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1772 | friend class Instruction; |
1773 | |
1774 | SelectInst *cloneImpl() const; |
1775 | |
1776 | public: |
1777 | static SelectInst *Create(Value *C, Value *S1, Value *S2, |
1778 | const Twine &NameStr = "", |
1779 | Instruction *InsertBefore = nullptr, |
1780 | Instruction *MDFrom = nullptr) { |
1781 | SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore); |
1782 | if (MDFrom) |
1783 | Sel->copyMetadata(*MDFrom); |
1784 | return Sel; |
1785 | } |
1786 | |
1787 | static SelectInst *Create(Value *C, Value *S1, Value *S2, |
1788 | const Twine &NameStr, |
1789 | BasicBlock *InsertAtEnd) { |
1790 | return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd); |
1791 | } |
1792 | |
1793 | const Value *getCondition() const { return Op<0>(); } |
1794 | const Value *getTrueValue() const { return Op<1>(); } |
1795 | const Value *getFalseValue() const { return Op<2>(); } |
1796 | Value *getCondition() { return Op<0>(); } |
1797 | Value *getTrueValue() { return Op<1>(); } |
1798 | Value *getFalseValue() { return Op<2>(); } |
1799 | |
1800 | void setCondition(Value *V) { Op<0>() = V; } |
1801 | void setTrueValue(Value *V) { Op<1>() = V; } |
1802 | void setFalseValue(Value *V) { Op<2>() = V; } |
1803 | |
1804 | /// Swap the true and false values of the select instruction. |
1805 | /// This doesn't swap prof metadata. |
1806 | void swapValues() { Op<1>().swap(Op<2>()); } |
1807 | |
1808 | /// Return a string if the specified operands are invalid |
1809 | /// for a select operation, otherwise return null. |
1810 | static const char *areInvalidOperands(Value *Cond, Value *True, Value *False); |
1811 | |
1812 | /// Transparently provide more efficient getOperand methods. |
1813 | 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; |
1814 | |
1815 | OtherOps getOpcode() const { |
1816 | return static_cast<OtherOps>(Instruction::getOpcode()); |
1817 | } |
1818 | |
1819 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1820 | static bool classof(const Instruction *I) { |
1821 | return I->getOpcode() == Instruction::Select; |
1822 | } |
1823 | static bool classof(const Value *V) { |
1824 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1825 | } |
1826 | }; |
1827 | |
1828 | template <> |
1829 | struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> { |
1830 | }; |
1831 | |
1832 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<SelectInst>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 1832, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<SelectInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<SelectInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 1832, __extension__ __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); } |
1833 | |
1834 | //===----------------------------------------------------------------------===// |
1835 | // VAArgInst Class |
1836 | //===----------------------------------------------------------------------===// |
1837 | |
1838 | /// This class represents the va_arg llvm instruction, which returns |
1839 | /// an argument of the specified type given a va_list and increments that list |
1840 | /// |
1841 | class VAArgInst : public UnaryInstruction { |
1842 | protected: |
1843 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1844 | friend class Instruction; |
1845 | |
1846 | VAArgInst *cloneImpl() const; |
1847 | |
1848 | public: |
1849 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "", |
1850 | Instruction *InsertBefore = nullptr) |
1851 | : UnaryInstruction(Ty, VAArg, List, InsertBefore) { |
1852 | setName(NameStr); |
1853 | } |
1854 | |
1855 | VAArgInst(Value *List, Type *Ty, const Twine &NameStr, |
1856 | BasicBlock *InsertAtEnd) |
1857 | : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) { |
1858 | setName(NameStr); |
1859 | } |
1860 | |
1861 | Value *getPointerOperand() { return getOperand(0); } |
1862 | const Value *getPointerOperand() const { return getOperand(0); } |
1863 | static unsigned getPointerOperandIndex() { return 0U; } |
1864 | |
1865 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1866 | static bool classof(const Instruction *I) { |
1867 | return I->getOpcode() == VAArg; |
1868 | } |
1869 | static bool classof(const Value *V) { |
1870 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1871 | } |
1872 | }; |
1873 | |
1874 | //===----------------------------------------------------------------------===// |
1875 | // ExtractElementInst Class |
1876 | //===----------------------------------------------------------------------===// |
1877 | |
1878 | /// This instruction extracts a single (scalar) |
1879 | /// element from a VectorType value |
1880 | /// |
1881 | class ExtractElementInst : public Instruction { |
1882 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "", |
1883 | Instruction *InsertBefore = nullptr); |
1884 | ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr, |
1885 | BasicBlock *InsertAtEnd); |
1886 | |
1887 | protected: |
1888 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1889 | friend class Instruction; |
1890 | |
1891 | ExtractElementInst *cloneImpl() const; |
1892 | |
1893 | public: |
1894 | static ExtractElementInst *Create(Value *Vec, Value *Idx, |
1895 | const Twine &NameStr = "", |
1896 | Instruction *InsertBefore = nullptr) { |
1897 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore); |
1898 | } |
1899 | |
1900 | static ExtractElementInst *Create(Value *Vec, Value *Idx, |
1901 | const Twine &NameStr, |
1902 | BasicBlock *InsertAtEnd) { |
1903 | return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd); |
1904 | } |
1905 | |
1906 | /// Return true if an extractelement instruction can be |
1907 | /// formed with the specified operands. |
1908 | static bool isValidOperands(const Value *Vec, const Value *Idx); |
1909 | |
1910 | Value *getVectorOperand() { return Op<0>(); } |
1911 | Value *getIndexOperand() { return Op<1>(); } |
1912 | const Value *getVectorOperand() const { return Op<0>(); } |
1913 | const Value *getIndexOperand() const { return Op<1>(); } |
1914 | |
1915 | VectorType *getVectorOperandType() const { |
1916 | return cast<VectorType>(getVectorOperand()->getType()); |
1917 | } |
1918 | |
1919 | /// Transparently provide more efficient getOperand methods. |
1920 | 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; |
1921 | |
1922 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1923 | static bool classof(const Instruction *I) { |
1924 | return I->getOpcode() == Instruction::ExtractElement; |
1925 | } |
1926 | static bool classof(const Value *V) { |
1927 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1928 | } |
1929 | }; |
1930 | |
1931 | template <> |
1932 | struct OperandTraits<ExtractElementInst> : |
1933 | public FixedNumOperandTraits<ExtractElementInst, 2> { |
1934 | }; |
1935 | |
1936 | 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 { (static_cast <bool> (i_nocapture < OperandTraits< ExtractElementInst>::operands(this) && "getOperand() out of range!" ) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 1936, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ExtractElementInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 1936, __extension__ __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 ); } |
1937 | |
1938 | //===----------------------------------------------------------------------===// |
1939 | // InsertElementInst Class |
1940 | //===----------------------------------------------------------------------===// |
1941 | |
1942 | /// This instruction inserts a single (scalar) |
1943 | /// element into a VectorType value |
1944 | /// |
1945 | class InsertElementInst : public Instruction { |
1946 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, |
1947 | const Twine &NameStr = "", |
1948 | Instruction *InsertBefore = nullptr); |
1949 | InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr, |
1950 | BasicBlock *InsertAtEnd); |
1951 | |
1952 | protected: |
1953 | // Note: Instruction needs to be a friend here to call cloneImpl. |
1954 | friend class Instruction; |
1955 | |
1956 | InsertElementInst *cloneImpl() const; |
1957 | |
1958 | public: |
1959 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, |
1960 | const Twine &NameStr = "", |
1961 | Instruction *InsertBefore = nullptr) { |
1962 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore); |
1963 | } |
1964 | |
1965 | static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx, |
1966 | const Twine &NameStr, |
1967 | BasicBlock *InsertAtEnd) { |
1968 | return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd); |
1969 | } |
1970 | |
1971 | /// Return true if an insertelement instruction can be |
1972 | /// formed with the specified operands. |
1973 | static bool isValidOperands(const Value *Vec, const Value *NewElt, |
1974 | const Value *Idx); |
1975 | |
1976 | /// Overload to return most specific vector type. |
1977 | /// |
1978 | VectorType *getType() const { |
1979 | return cast<VectorType>(Instruction::getType()); |
1980 | } |
1981 | |
1982 | /// Transparently provide more efficient getOperand methods. |
1983 | 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; |
1984 | |
1985 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
1986 | static bool classof(const Instruction *I) { |
1987 | return I->getOpcode() == Instruction::InsertElement; |
1988 | } |
1989 | static bool classof(const Value *V) { |
1990 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
1991 | } |
1992 | }; |
1993 | |
1994 | template <> |
1995 | struct OperandTraits<InsertElementInst> : |
1996 | public FixedNumOperandTraits<InsertElementInst, 3> { |
1997 | }; |
1998 | |
1999 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<InsertElementInst>::operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 1999, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<InsertElementInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<InsertElementInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 1999, __extension__ __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 ); } |
2000 | |
2001 | //===----------------------------------------------------------------------===// |
2002 | // ShuffleVectorInst Class |
2003 | //===----------------------------------------------------------------------===// |
2004 | |
2005 | constexpr int PoisonMaskElem = -1; |
2006 | |
2007 | /// This instruction constructs a fixed permutation of two |
2008 | /// input vectors. |
2009 | /// |
2010 | /// For each element of the result vector, the shuffle mask selects an element |
2011 | /// from one of the input vectors to copy to the result. Non-negative elements |
2012 | /// in the mask represent an index into the concatenated pair of input vectors. |
2013 | /// PoisonMaskElem (-1) specifies that the result element is poison. |
2014 | /// |
2015 | /// For scalable vectors, all the elements of the mask must be 0 or -1. This |
2016 | /// requirement may be relaxed in the future. |
2017 | class ShuffleVectorInst : public Instruction { |
2018 | SmallVector<int, 4> ShuffleMask; |
2019 | Constant *ShuffleMaskForBitcode; |
2020 | |
2021 | protected: |
2022 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2023 | friend class Instruction; |
2024 | |
2025 | ShuffleVectorInst *cloneImpl() const; |
2026 | |
2027 | public: |
2028 | ShuffleVectorInst(Value *V1, Value *Mask, const Twine &NameStr = "", |
2029 | Instruction *InsertBefore = nullptr); |
2030 | ShuffleVectorInst(Value *V1, Value *Mask, const Twine &NameStr, |
2031 | BasicBlock *InsertAtEnd); |
2032 | ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, const Twine &NameStr = "", |
2033 | Instruction *InsertBefore = nullptr); |
2034 | ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, const Twine &NameStr, |
2035 | BasicBlock *InsertAtEnd); |
2036 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
2037 | const Twine &NameStr = "", |
2038 | Instruction *InsertBefor = nullptr); |
2039 | ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
2040 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2041 | ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, |
2042 | const Twine &NameStr = "", |
2043 | Instruction *InsertBefor = nullptr); |
2044 | ShuffleVectorInst(Value *V1, Value *V2, ArrayRef<int> Mask, |
2045 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2046 | |
2047 | void *operator new(size_t S) { return User::operator new(S, 2); } |
2048 | void operator delete(void *Ptr) { return User::operator delete(Ptr); } |
2049 | |
2050 | /// Swap the operands and adjust the mask to preserve the semantics |
2051 | /// of the instruction. |
2052 | void commute(); |
2053 | |
2054 | /// Return true if a shufflevector instruction can be |
2055 | /// formed with the specified operands. |
2056 | static bool isValidOperands(const Value *V1, const Value *V2, |
2057 | const Value *Mask); |
2058 | static bool isValidOperands(const Value *V1, const Value *V2, |
2059 | ArrayRef<int> Mask); |
2060 | |
2061 | /// Overload to return most specific vector type. |
2062 | /// |
2063 | VectorType *getType() const { |
2064 | return cast<VectorType>(Instruction::getType()); |
2065 | } |
2066 | |
2067 | /// Transparently provide more efficient getOperand methods. |
2068 | 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; |
2069 | |
2070 | /// Return the shuffle mask value of this instruction for the given element |
2071 | /// index. Return PoisonMaskElem if the element is undef. |
2072 | int getMaskValue(unsigned Elt) const { return ShuffleMask[Elt]; } |
2073 | |
2074 | /// Convert the input shuffle mask operand to a vector of integers. Undefined |
2075 | /// elements of the mask are returned as PoisonMaskElem. |
2076 | static void getShuffleMask(const Constant *Mask, |
2077 | SmallVectorImpl<int> &Result); |
2078 | |
2079 | /// Return the mask for this instruction as a vector of integers. Undefined |
2080 | /// elements of the mask are returned as PoisonMaskElem. |
2081 | void getShuffleMask(SmallVectorImpl<int> &Result) const { |
2082 | Result.assign(ShuffleMask.begin(), ShuffleMask.end()); |
2083 | } |
2084 | |
2085 | /// Return the mask for this instruction, for use in bitcode. |
2086 | /// |
2087 | /// TODO: This is temporary until we decide a new bitcode encoding for |
2088 | /// shufflevector. |
2089 | Constant *getShuffleMaskForBitcode() const { return ShuffleMaskForBitcode; } |
2090 | |
2091 | static Constant *convertShuffleMaskForBitcode(ArrayRef<int> Mask, |
2092 | Type *ResultTy); |
2093 | |
2094 | void setShuffleMask(ArrayRef<int> Mask); |
2095 | |
2096 | ArrayRef<int> getShuffleMask() const { return ShuffleMask; } |
2097 | |
2098 | /// Return true if this shuffle returns a vector with a different number of |
2099 | /// elements than its source vectors. |
2100 | /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3> |
2101 | /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5> |
2102 | bool changesLength() const { |
2103 | unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType()) |
2104 | ->getElementCount() |
2105 | .getKnownMinValue(); |
2106 | unsigned NumMaskElts = ShuffleMask.size(); |
2107 | return NumSourceElts != NumMaskElts; |
2108 | } |
2109 | |
2110 | /// Return true if this shuffle returns a vector with a greater number of |
2111 | /// elements than its source vectors. |
2112 | /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3> |
2113 | bool increasesLength() const { |
2114 | unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType()) |
2115 | ->getElementCount() |
2116 | .getKnownMinValue(); |
2117 | unsigned NumMaskElts = ShuffleMask.size(); |
2118 | return NumSourceElts < NumMaskElts; |
2119 | } |
2120 | |
2121 | /// Return true if this shuffle mask chooses elements from exactly one source |
2122 | /// vector. |
2123 | /// Example: <7,5,undef,7> |
2124 | /// This assumes that vector operands are the same length as the mask. |
2125 | static bool isSingleSourceMask(ArrayRef<int> Mask); |
2126 | static bool isSingleSourceMask(const Constant *Mask) { |
2127 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2127, __extension__ __PRETTY_FUNCTION__ )); |
2128 | SmallVector<int, 16> MaskAsInts; |
2129 | getShuffleMask(Mask, MaskAsInts); |
2130 | return isSingleSourceMask(MaskAsInts); |
2131 | } |
2132 | |
2133 | /// Return true if this shuffle chooses elements from exactly one source |
2134 | /// vector without changing the length of that vector. |
2135 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3> |
2136 | /// TODO: Optionally allow length-changing shuffles. |
2137 | bool isSingleSource() const { |
2138 | return !changesLength() && isSingleSourceMask(ShuffleMask); |
2139 | } |
2140 | |
2141 | /// Return true if this shuffle mask chooses elements from exactly one source |
2142 | /// vector without lane crossings. A shuffle using this mask is not |
2143 | /// necessarily a no-op because it may change the number of elements from its |
2144 | /// input vectors or it may provide demanded bits knowledge via undef lanes. |
2145 | /// Example: <undef,undef,2,3> |
2146 | static bool isIdentityMask(ArrayRef<int> Mask); |
2147 | static bool isIdentityMask(const Constant *Mask) { |
2148 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2148, __extension__ __PRETTY_FUNCTION__ )); |
2149 | |
2150 | // Not possible to express a shuffle mask for a scalable vector for this |
2151 | // case. |
2152 | if (isa<ScalableVectorType>(Mask->getType())) |
2153 | return false; |
2154 | |
2155 | SmallVector<int, 16> MaskAsInts; |
2156 | getShuffleMask(Mask, MaskAsInts); |
2157 | return isIdentityMask(MaskAsInts); |
2158 | } |
2159 | |
2160 | /// Return true if this shuffle chooses elements from exactly one source |
2161 | /// vector without lane crossings and does not change the number of elements |
2162 | /// from its input vectors. |
2163 | /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef> |
2164 | bool isIdentity() const { |
2165 | // Not possible to express a shuffle mask for a scalable vector for this |
2166 | // case. |
2167 | if (isa<ScalableVectorType>(getType())) |
2168 | return false; |
2169 | |
2170 | return !changesLength() && isIdentityMask(ShuffleMask); |
2171 | } |
2172 | |
2173 | /// Return true if this shuffle lengthens exactly one source vector with |
2174 | /// undefs in the high elements. |
2175 | bool isIdentityWithPadding() const; |
2176 | |
2177 | /// Return true if this shuffle extracts the first N elements of exactly one |
2178 | /// source vector. |
2179 | bool isIdentityWithExtract() const; |
2180 | |
2181 | /// Return true if this shuffle concatenates its 2 source vectors. This |
2182 | /// returns false if either input is undefined. In that case, the shuffle is |
2183 | /// is better classified as an identity with padding operation. |
2184 | bool isConcat() const; |
2185 | |
2186 | /// Return true if this shuffle mask chooses elements from its source vectors |
2187 | /// without lane crossings. A shuffle using this mask would be |
2188 | /// equivalent to a vector select with a constant condition operand. |
2189 | /// Example: <4,1,6,undef> |
2190 | /// This returns false if the mask does not choose from both input vectors. |
2191 | /// In that case, the shuffle is better classified as an identity shuffle. |
2192 | /// This assumes that vector operands are the same length as the mask |
2193 | /// (a length-changing shuffle can never be equivalent to a vector select). |
2194 | static bool isSelectMask(ArrayRef<int> Mask); |
2195 | static bool isSelectMask(const Constant *Mask) { |
2196 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2196, __extension__ __PRETTY_FUNCTION__ )); |
2197 | SmallVector<int, 16> MaskAsInts; |
2198 | getShuffleMask(Mask, MaskAsInts); |
2199 | return isSelectMask(MaskAsInts); |
2200 | } |
2201 | |
2202 | /// Return true if this shuffle chooses elements from its source vectors |
2203 | /// without lane crossings and all operands have the same number of elements. |
2204 | /// In other words, this shuffle is equivalent to a vector select with a |
2205 | /// constant condition operand. |
2206 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3> |
2207 | /// This returns false if the mask does not choose from both input vectors. |
2208 | /// In that case, the shuffle is better classified as an identity shuffle. |
2209 | /// TODO: Optionally allow length-changing shuffles. |
2210 | bool isSelect() const { |
2211 | return !changesLength() && isSelectMask(ShuffleMask); |
2212 | } |
2213 | |
2214 | /// Return true if this shuffle mask swaps the order of elements from exactly |
2215 | /// one source vector. |
2216 | /// Example: <7,6,undef,4> |
2217 | /// This assumes that vector operands are the same length as the mask. |
2218 | static bool isReverseMask(ArrayRef<int> Mask); |
2219 | static bool isReverseMask(const Constant *Mask) { |
2220 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2220, __extension__ __PRETTY_FUNCTION__ )); |
2221 | SmallVector<int, 16> MaskAsInts; |
2222 | getShuffleMask(Mask, MaskAsInts); |
2223 | return isReverseMask(MaskAsInts); |
2224 | } |
2225 | |
2226 | /// Return true if this shuffle swaps the order of elements from exactly |
2227 | /// one source vector. |
2228 | /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef> |
2229 | /// TODO: Optionally allow length-changing shuffles. |
2230 | bool isReverse() const { |
2231 | return !changesLength() && isReverseMask(ShuffleMask); |
2232 | } |
2233 | |
2234 | /// Return true if this shuffle mask chooses all elements with the same value |
2235 | /// as the first element of exactly one source vector. |
2236 | /// Example: <4,undef,undef,4> |
2237 | /// This assumes that vector operands are the same length as the mask. |
2238 | static bool isZeroEltSplatMask(ArrayRef<int> Mask); |
2239 | static bool isZeroEltSplatMask(const Constant *Mask) { |
2240 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2240, __extension__ __PRETTY_FUNCTION__ )); |
2241 | SmallVector<int, 16> MaskAsInts; |
2242 | getShuffleMask(Mask, MaskAsInts); |
2243 | return isZeroEltSplatMask(MaskAsInts); |
2244 | } |
2245 | |
2246 | /// Return true if all elements of this shuffle are the same value as the |
2247 | /// first element of exactly one source vector without changing the length |
2248 | /// of that vector. |
2249 | /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0> |
2250 | /// TODO: Optionally allow length-changing shuffles. |
2251 | /// TODO: Optionally allow splats from other elements. |
2252 | bool isZeroEltSplat() const { |
2253 | return !changesLength() && isZeroEltSplatMask(ShuffleMask); |
2254 | } |
2255 | |
2256 | /// Return true if this shuffle mask is a transpose mask. |
2257 | /// Transpose vector masks transpose a 2xn matrix. They read corresponding |
2258 | /// even- or odd-numbered vector elements from two n-dimensional source |
2259 | /// vectors and write each result into consecutive elements of an |
2260 | /// n-dimensional destination vector. Two shuffles are necessary to complete |
2261 | /// the transpose, one for the even elements and another for the odd elements. |
2262 | /// This description closely follows how the TRN1 and TRN2 AArch64 |
2263 | /// instructions operate. |
2264 | /// |
2265 | /// For example, a simple 2x2 matrix can be transposed with: |
2266 | /// |
2267 | /// ; Original matrix |
2268 | /// m0 = < a, b > |
2269 | /// m1 = < c, d > |
2270 | /// |
2271 | /// ; Transposed matrix |
2272 | /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 > |
2273 | /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 > |
2274 | /// |
2275 | /// For matrices having greater than n columns, the resulting nx2 transposed |
2276 | /// matrix is stored in two result vectors such that one vector contains |
2277 | /// interleaved elements from all the even-numbered rows and the other vector |
2278 | /// contains interleaved elements from all the odd-numbered rows. For example, |
2279 | /// a 2x4 matrix can be transposed with: |
2280 | /// |
2281 | /// ; Original matrix |
2282 | /// m0 = < a, b, c, d > |
2283 | /// m1 = < e, f, g, h > |
2284 | /// |
2285 | /// ; Transposed matrix |
2286 | /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 > |
2287 | /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 > |
2288 | static bool isTransposeMask(ArrayRef<int> Mask); |
2289 | static bool isTransposeMask(const Constant *Mask) { |
2290 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2290, __extension__ __PRETTY_FUNCTION__ )); |
2291 | SmallVector<int, 16> MaskAsInts; |
2292 | getShuffleMask(Mask, MaskAsInts); |
2293 | return isTransposeMask(MaskAsInts); |
2294 | } |
2295 | |
2296 | /// Return true if this shuffle transposes the elements of its inputs without |
2297 | /// changing the length of the vectors. This operation may also be known as a |
2298 | /// merge or interleave. See the description for isTransposeMask() for the |
2299 | /// exact specification. |
2300 | /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6> |
2301 | bool isTranspose() const { |
2302 | return !changesLength() && isTransposeMask(ShuffleMask); |
2303 | } |
2304 | |
2305 | /// Return true if this shuffle mask is a splice mask, concatenating the two |
2306 | /// inputs together and then extracts an original width vector starting from |
2307 | /// the splice index. |
2308 | /// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4> |
2309 | static bool isSpliceMask(ArrayRef<int> Mask, int &Index); |
2310 | static bool isSpliceMask(const Constant *Mask, int &Index) { |
2311 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2311, __extension__ __PRETTY_FUNCTION__ )); |
2312 | SmallVector<int, 16> MaskAsInts; |
2313 | getShuffleMask(Mask, MaskAsInts); |
2314 | return isSpliceMask(MaskAsInts, Index); |
2315 | } |
2316 | |
2317 | /// Return true if this shuffle splices two inputs without changing the length |
2318 | /// of the vectors. This operation concatenates the two inputs together and |
2319 | /// then extracts an original width vector starting from the splice index. |
2320 | /// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4> |
2321 | bool isSplice(int &Index) const { |
2322 | return !changesLength() && isSpliceMask(ShuffleMask, Index); |
2323 | } |
2324 | |
2325 | /// Return true if this shuffle mask is an extract subvector mask. |
2326 | /// A valid extract subvector mask returns a smaller vector from a single |
2327 | /// source operand. The base extraction index is returned as well. |
2328 | static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts, |
2329 | int &Index); |
2330 | static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts, |
2331 | int &Index) { |
2332 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2332, __extension__ __PRETTY_FUNCTION__ )); |
2333 | // Not possible to express a shuffle mask for a scalable vector for this |
2334 | // case. |
2335 | if (isa<ScalableVectorType>(Mask->getType())) |
2336 | return false; |
2337 | SmallVector<int, 16> MaskAsInts; |
2338 | getShuffleMask(Mask, MaskAsInts); |
2339 | return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index); |
2340 | } |
2341 | |
2342 | /// Return true if this shuffle mask is an extract subvector mask. |
2343 | bool isExtractSubvectorMask(int &Index) const { |
2344 | // Not possible to express a shuffle mask for a scalable vector for this |
2345 | // case. |
2346 | if (isa<ScalableVectorType>(getType())) |
2347 | return false; |
2348 | |
2349 | int NumSrcElts = |
2350 | cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); |
2351 | return isExtractSubvectorMask(ShuffleMask, NumSrcElts, Index); |
2352 | } |
2353 | |
2354 | /// Return true if this shuffle mask is an insert subvector mask. |
2355 | /// A valid insert subvector mask inserts the lowest elements of a second |
2356 | /// source operand into an in-place first source operand operand. |
2357 | /// Both the sub vector width and the insertion index is returned. |
2358 | static bool isInsertSubvectorMask(ArrayRef<int> Mask, int NumSrcElts, |
2359 | int &NumSubElts, int &Index); |
2360 | static bool isInsertSubvectorMask(const Constant *Mask, int NumSrcElts, |
2361 | int &NumSubElts, int &Index) { |
2362 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2362, __extension__ __PRETTY_FUNCTION__ )); |
2363 | // Not possible to express a shuffle mask for a scalable vector for this |
2364 | // case. |
2365 | if (isa<ScalableVectorType>(Mask->getType())) |
2366 | return false; |
2367 | SmallVector<int, 16> MaskAsInts; |
2368 | getShuffleMask(Mask, MaskAsInts); |
2369 | return isInsertSubvectorMask(MaskAsInts, NumSrcElts, NumSubElts, Index); |
2370 | } |
2371 | |
2372 | /// Return true if this shuffle mask is an insert subvector mask. |
2373 | bool isInsertSubvectorMask(int &NumSubElts, int &Index) const { |
2374 | // Not possible to express a shuffle mask for a scalable vector for this |
2375 | // case. |
2376 | if (isa<ScalableVectorType>(getType())) |
2377 | return false; |
2378 | |
2379 | int NumSrcElts = |
2380 | cast<FixedVectorType>(Op<0>()->getType())->getNumElements(); |
2381 | return isInsertSubvectorMask(ShuffleMask, NumSrcElts, NumSubElts, Index); |
2382 | } |
2383 | |
2384 | /// Return true if this shuffle mask replicates each of the \p VF elements |
2385 | /// in a vector \p ReplicationFactor times. |
2386 | /// For example, the mask for \p ReplicationFactor=3 and \p VF=4 is: |
2387 | /// <0,0,0,1,1,1,2,2,2,3,3,3> |
2388 | static bool isReplicationMask(ArrayRef<int> Mask, int &ReplicationFactor, |
2389 | int &VF); |
2390 | static bool isReplicationMask(const Constant *Mask, int &ReplicationFactor, |
2391 | int &VF) { |
2392 | assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.")(static_cast <bool> (Mask->getType()->isVectorTy( ) && "Shuffle needs vector constant.") ? void (0) : __assert_fail ("Mask->getType()->isVectorTy() && \"Shuffle needs vector constant.\"" , "llvm/include/llvm/IR/Instructions.h", 2392, __extension__ __PRETTY_FUNCTION__ )); |
2393 | // Not possible to express a shuffle mask for a scalable vector for this |
2394 | // case. |
2395 | if (isa<ScalableVectorType>(Mask->getType())) |
2396 | return false; |
2397 | SmallVector<int, 16> MaskAsInts; |
2398 | getShuffleMask(Mask, MaskAsInts); |
2399 | return isReplicationMask(MaskAsInts, ReplicationFactor, VF); |
2400 | } |
2401 | |
2402 | /// Return true if this shuffle mask is a replication mask. |
2403 | bool isReplicationMask(int &ReplicationFactor, int &VF) const; |
2404 | |
2405 | /// Return true if this shuffle mask represents "clustered" mask of size VF, |
2406 | /// i.e. each index between [0..VF) is used exactly once in each submask of |
2407 | /// size VF. |
2408 | /// For example, the mask for \p VF=4 is: |
2409 | /// 0, 1, 2, 3, 3, 2, 0, 1 - "clustered", because each submask of size 4 |
2410 | /// (0,1,2,3 and 3,2,0,1) uses indices [0..VF) exactly one time. |
2411 | /// 0, 1, 2, 3, 3, 3, 1, 0 - not "clustered", because |
2412 | /// element 3 is used twice in the second submask |
2413 | /// (3,3,1,0) and index 2 is not used at all. |
2414 | static bool isOneUseSingleSourceMask(ArrayRef<int> Mask, int VF); |
2415 | |
2416 | /// Return true if this shuffle mask is a one-use-single-source("clustered") |
2417 | /// mask. |
2418 | bool isOneUseSingleSourceMask(int VF) const; |
2419 | |
2420 | /// Change values in a shuffle permute mask assuming the two vector operands |
2421 | /// of length InVecNumElts have swapped position. |
2422 | static void commuteShuffleMask(MutableArrayRef<int> Mask, |
2423 | unsigned InVecNumElts) { |
2424 | for (int &Idx : Mask) { |
2425 | if (Idx == -1) |
2426 | continue; |
2427 | Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts; |
2428 | assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&(static_cast <bool> (Idx >= 0 && Idx < (int )InVecNumElts * 2 && "shufflevector mask index out of range" ) ? void (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\"" , "llvm/include/llvm/IR/Instructions.h", 2429, __extension__ __PRETTY_FUNCTION__ )) |
2429 | "shufflevector mask index out of range")(static_cast <bool> (Idx >= 0 && Idx < (int )InVecNumElts * 2 && "shufflevector mask index out of range" ) ? void (0) : __assert_fail ("Idx >= 0 && Idx < (int)InVecNumElts * 2 && \"shufflevector mask index out of range\"" , "llvm/include/llvm/IR/Instructions.h", 2429, __extension__ __PRETTY_FUNCTION__ )); |
2430 | } |
2431 | } |
2432 | |
2433 | /// Return if this shuffle interleaves its two input vectors together. |
2434 | bool isInterleave(unsigned Factor); |
2435 | |
2436 | /// Return true if the mask interleaves one or more input vectors together. |
2437 | /// |
2438 | /// I.e. <0, LaneLen, ... , LaneLen*(Factor - 1), 1, LaneLen + 1, ...> |
2439 | /// E.g. For a Factor of 2 (LaneLen=4): |
2440 | /// <0, 4, 1, 5, 2, 6, 3, 7> |
2441 | /// E.g. For a Factor of 3 (LaneLen=4): |
2442 | /// <4, 0, 9, 5, 1, 10, 6, 2, 11, 7, 3, 12> |
2443 | /// E.g. For a Factor of 4 (LaneLen=2): |
2444 | /// <0, 2, 6, 4, 1, 3, 7, 5> |
2445 | /// |
2446 | /// NumInputElts is the total number of elements in the input vectors. |
2447 | /// |
2448 | /// StartIndexes are the first indexes of each vector being interleaved, |
2449 | /// substituting any indexes that were undef |
2450 | /// E.g. <4, -1, 2, 5, 1, 3> (Factor=3): StartIndexes=<4, 0, 2> |
2451 | /// |
2452 | /// Note that this does not check if the input vectors are consecutive: |
2453 | /// It will return true for masks such as |
2454 | /// <0, 4, 6, 1, 5, 7> (Factor=3, LaneLen=2) |
2455 | static bool isInterleaveMask(ArrayRef<int> Mask, unsigned Factor, |
2456 | unsigned NumInputElts, |
2457 | SmallVectorImpl<unsigned> &StartIndexes); |
2458 | static bool isInterleaveMask(ArrayRef<int> Mask, unsigned Factor, |
2459 | unsigned NumInputElts) { |
2460 | SmallVector<unsigned, 8> StartIndexes; |
2461 | return isInterleaveMask(Mask, Factor, NumInputElts, StartIndexes); |
2462 | } |
2463 | |
2464 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2465 | static bool classof(const Instruction *I) { |
2466 | return I->getOpcode() == Instruction::ShuffleVector; |
2467 | } |
2468 | static bool classof(const Value *V) { |
2469 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2470 | } |
2471 | }; |
2472 | |
2473 | template <> |
2474 | struct OperandTraits<ShuffleVectorInst> |
2475 | : public FixedNumOperandTraits<ShuffleVectorInst, 2> {}; |
2476 | |
2477 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 2477, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ShuffleVectorInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 2477, __extension__ __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 ); } |
2478 | |
2479 | //===----------------------------------------------------------------------===// |
2480 | // ExtractValueInst Class |
2481 | //===----------------------------------------------------------------------===// |
2482 | |
2483 | /// This instruction extracts a struct member or array |
2484 | /// element value from an aggregate value. |
2485 | /// |
2486 | class ExtractValueInst : public UnaryInstruction { |
2487 | SmallVector<unsigned, 4> Indices; |
2488 | |
2489 | ExtractValueInst(const ExtractValueInst &EVI); |
2490 | |
2491 | /// Constructors - Create a extractvalue instruction with a base aggregate |
2492 | /// value and a list of indices. The first ctor can optionally insert before |
2493 | /// an existing instruction, the second appends the new instruction to the |
2494 | /// specified BasicBlock. |
2495 | inline ExtractValueInst(Value *Agg, |
2496 | ArrayRef<unsigned> Idxs, |
2497 | const Twine &NameStr, |
2498 | Instruction *InsertBefore); |
2499 | inline ExtractValueInst(Value *Agg, |
2500 | ArrayRef<unsigned> Idxs, |
2501 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2502 | |
2503 | void init(ArrayRef<unsigned> Idxs, const Twine &NameStr); |
2504 | |
2505 | protected: |
2506 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2507 | friend class Instruction; |
2508 | |
2509 | ExtractValueInst *cloneImpl() const; |
2510 | |
2511 | public: |
2512 | static ExtractValueInst *Create(Value *Agg, |
2513 | ArrayRef<unsigned> Idxs, |
2514 | const Twine &NameStr = "", |
2515 | Instruction *InsertBefore = nullptr) { |
2516 | return new |
2517 | ExtractValueInst(Agg, Idxs, NameStr, InsertBefore); |
2518 | } |
2519 | |
2520 | static ExtractValueInst *Create(Value *Agg, |
2521 | ArrayRef<unsigned> Idxs, |
2522 | const Twine &NameStr, |
2523 | BasicBlock *InsertAtEnd) { |
2524 | return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd); |
2525 | } |
2526 | |
2527 | /// Returns the type of the element that would be extracted |
2528 | /// with an extractvalue instruction with the specified parameters. |
2529 | /// |
2530 | /// Null is returned if the indices are invalid for the specified type. |
2531 | static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs); |
2532 | |
2533 | using idx_iterator = const unsigned*; |
2534 | |
2535 | inline idx_iterator idx_begin() const { return Indices.begin(); } |
2536 | inline idx_iterator idx_end() const { return Indices.end(); } |
2537 | inline iterator_range<idx_iterator> indices() const { |
2538 | return make_range(idx_begin(), idx_end()); |
2539 | } |
2540 | |
2541 | Value *getAggregateOperand() { |
2542 | return getOperand(0); |
2543 | } |
2544 | const Value *getAggregateOperand() const { |
2545 | return getOperand(0); |
2546 | } |
2547 | static unsigned getAggregateOperandIndex() { |
2548 | return 0U; // get index for modifying correct operand |
2549 | } |
2550 | |
2551 | ArrayRef<unsigned> getIndices() const { |
2552 | return Indices; |
2553 | } |
2554 | |
2555 | unsigned getNumIndices() const { |
2556 | return (unsigned)Indices.size(); |
2557 | } |
2558 | |
2559 | bool hasIndices() const { |
2560 | return true; |
2561 | } |
2562 | |
2563 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2564 | static bool classof(const Instruction *I) { |
2565 | return I->getOpcode() == Instruction::ExtractValue; |
2566 | } |
2567 | static bool classof(const Value *V) { |
2568 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2569 | } |
2570 | }; |
2571 | |
2572 | ExtractValueInst::ExtractValueInst(Value *Agg, |
2573 | ArrayRef<unsigned> Idxs, |
2574 | const Twine &NameStr, |
2575 | Instruction *InsertBefore) |
2576 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), |
2577 | ExtractValue, Agg, InsertBefore) { |
2578 | init(Idxs, NameStr); |
2579 | } |
2580 | |
2581 | ExtractValueInst::ExtractValueInst(Value *Agg, |
2582 | ArrayRef<unsigned> Idxs, |
2583 | const Twine &NameStr, |
2584 | BasicBlock *InsertAtEnd) |
2585 | : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)), |
2586 | ExtractValue, Agg, InsertAtEnd) { |
2587 | init(Idxs, NameStr); |
2588 | } |
2589 | |
2590 | //===----------------------------------------------------------------------===// |
2591 | // InsertValueInst Class |
2592 | //===----------------------------------------------------------------------===// |
2593 | |
2594 | /// This instruction inserts a struct field of array element |
2595 | /// value into an aggregate value. |
2596 | /// |
2597 | class InsertValueInst : public Instruction { |
2598 | SmallVector<unsigned, 4> Indices; |
2599 | |
2600 | InsertValueInst(const InsertValueInst &IVI); |
2601 | |
2602 | /// Constructors - Create a insertvalue instruction with a base aggregate |
2603 | /// value, a value to insert, and a list of indices. The first ctor can |
2604 | /// optionally insert before an existing instruction, the second appends |
2605 | /// the new instruction to the specified BasicBlock. |
2606 | inline InsertValueInst(Value *Agg, Value *Val, |
2607 | ArrayRef<unsigned> Idxs, |
2608 | const Twine &NameStr, |
2609 | Instruction *InsertBefore); |
2610 | inline InsertValueInst(Value *Agg, Value *Val, |
2611 | ArrayRef<unsigned> Idxs, |
2612 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
2613 | |
2614 | /// Constructors - These two constructors are convenience methods because one |
2615 | /// and two index insertvalue instructions are so common. |
2616 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, |
2617 | const Twine &NameStr = "", |
2618 | Instruction *InsertBefore = nullptr); |
2619 | InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr, |
2620 | BasicBlock *InsertAtEnd); |
2621 | |
2622 | void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
2623 | const Twine &NameStr); |
2624 | |
2625 | protected: |
2626 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2627 | friend class Instruction; |
2628 | |
2629 | InsertValueInst *cloneImpl() const; |
2630 | |
2631 | public: |
2632 | // allocate space for exactly two operands |
2633 | void *operator new(size_t S) { return User::operator new(S, 2); } |
2634 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
2635 | |
2636 | static InsertValueInst *Create(Value *Agg, Value *Val, |
2637 | ArrayRef<unsigned> Idxs, |
2638 | const Twine &NameStr = "", |
2639 | Instruction *InsertBefore = nullptr) { |
2640 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore); |
2641 | } |
2642 | |
2643 | static InsertValueInst *Create(Value *Agg, Value *Val, |
2644 | ArrayRef<unsigned> Idxs, |
2645 | const Twine &NameStr, |
2646 | BasicBlock *InsertAtEnd) { |
2647 | return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd); |
2648 | } |
2649 | |
2650 | /// Transparently provide more efficient getOperand methods. |
2651 | 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; |
2652 | |
2653 | using idx_iterator = const unsigned*; |
2654 | |
2655 | inline idx_iterator idx_begin() const { return Indices.begin(); } |
2656 | inline idx_iterator idx_end() const { return Indices.end(); } |
2657 | inline iterator_range<idx_iterator> indices() const { |
2658 | return make_range(idx_begin(), idx_end()); |
2659 | } |
2660 | |
2661 | Value *getAggregateOperand() { |
2662 | return getOperand(0); |
2663 | } |
2664 | const Value *getAggregateOperand() const { |
2665 | return getOperand(0); |
2666 | } |
2667 | static unsigned getAggregateOperandIndex() { |
2668 | return 0U; // get index for modifying correct operand |
2669 | } |
2670 | |
2671 | Value *getInsertedValueOperand() { |
2672 | return getOperand(1); |
2673 | } |
2674 | const Value *getInsertedValueOperand() const { |
2675 | return getOperand(1); |
2676 | } |
2677 | static unsigned getInsertedValueOperandIndex() { |
2678 | return 1U; // get index for modifying correct operand |
2679 | } |
2680 | |
2681 | ArrayRef<unsigned> getIndices() const { |
2682 | return Indices; |
2683 | } |
2684 | |
2685 | unsigned getNumIndices() const { |
2686 | return (unsigned)Indices.size(); |
2687 | } |
2688 | |
2689 | bool hasIndices() const { |
2690 | return true; |
2691 | } |
2692 | |
2693 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
2694 | static bool classof(const Instruction *I) { |
2695 | return I->getOpcode() == Instruction::InsertValue; |
2696 | } |
2697 | static bool classof(const Value *V) { |
2698 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2699 | } |
2700 | }; |
2701 | |
2702 | template <> |
2703 | struct OperandTraits<InsertValueInst> : |
2704 | public FixedNumOperandTraits<InsertValueInst, 2> { |
2705 | }; |
2706 | |
2707 | InsertValueInst::InsertValueInst(Value *Agg, |
2708 | Value *Val, |
2709 | ArrayRef<unsigned> Idxs, |
2710 | const Twine &NameStr, |
2711 | Instruction *InsertBefore) |
2712 | : Instruction(Agg->getType(), InsertValue, |
2713 | OperandTraits<InsertValueInst>::op_begin(this), |
2714 | 2, InsertBefore) { |
2715 | init(Agg, Val, Idxs, NameStr); |
2716 | } |
2717 | |
2718 | InsertValueInst::InsertValueInst(Value *Agg, |
2719 | Value *Val, |
2720 | ArrayRef<unsigned> Idxs, |
2721 | const Twine &NameStr, |
2722 | BasicBlock *InsertAtEnd) |
2723 | : Instruction(Agg->getType(), InsertValue, |
2724 | OperandTraits<InsertValueInst>::op_begin(this), |
2725 | 2, InsertAtEnd) { |
2726 | init(Agg, Val, Idxs, NameStr); |
2727 | } |
2728 | |
2729 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<InsertValueInst>:: operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 2729, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<InsertValueInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<InsertValueInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 2729, __extension__ __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 ); } |
2730 | |
2731 | //===----------------------------------------------------------------------===// |
2732 | // PHINode Class |
2733 | //===----------------------------------------------------------------------===// |
2734 | |
2735 | // PHINode - The PHINode class is used to represent the magical mystical PHI |
2736 | // node, that can not exist in nature, but can be synthesized in a computer |
2737 | // scientist's overactive imagination. |
2738 | // |
2739 | class PHINode : public Instruction { |
2740 | /// The number of operands actually allocated. NumOperands is |
2741 | /// the number actually in use. |
2742 | unsigned ReservedSpace; |
2743 | |
2744 | PHINode(const PHINode &PN); |
2745 | |
2746 | explicit PHINode(Type *Ty, unsigned NumReservedValues, |
2747 | const Twine &NameStr = "", |
2748 | Instruction *InsertBefore = nullptr) |
2749 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore), |
2750 | ReservedSpace(NumReservedValues) { |
2751 | assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!")(static_cast <bool> (!Ty->isTokenTy() && "PHI nodes cannot have token type!" ) ? void (0) : __assert_fail ("!Ty->isTokenTy() && \"PHI nodes cannot have token type!\"" , "llvm/include/llvm/IR/Instructions.h", 2751, __extension__ __PRETTY_FUNCTION__ )); |
2752 | setName(NameStr); |
2753 | allocHungoffUses(ReservedSpace); |
2754 | } |
2755 | |
2756 | PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr, |
2757 | BasicBlock *InsertAtEnd) |
2758 | : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd), |
2759 | ReservedSpace(NumReservedValues) { |
2760 | assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!")(static_cast <bool> (!Ty->isTokenTy() && "PHI nodes cannot have token type!" ) ? void (0) : __assert_fail ("!Ty->isTokenTy() && \"PHI nodes cannot have token type!\"" , "llvm/include/llvm/IR/Instructions.h", 2760, __extension__ __PRETTY_FUNCTION__ )); |
2761 | setName(NameStr); |
2762 | allocHungoffUses(ReservedSpace); |
2763 | } |
2764 | |
2765 | protected: |
2766 | // Note: Instruction needs to be a friend here to call cloneImpl. |
2767 | friend class Instruction; |
2768 | |
2769 | PHINode *cloneImpl() const; |
2770 | |
2771 | // allocHungoffUses - this is more complicated than the generic |
2772 | // User::allocHungoffUses, because we have to allocate Uses for the incoming |
2773 | // values and pointers to the incoming blocks, all in one allocation. |
2774 | void allocHungoffUses(unsigned N) { |
2775 | User::allocHungoffUses(N, /* IsPhi */ true); |
2776 | } |
2777 | |
2778 | public: |
2779 | /// Constructors - NumReservedValues is a hint for the number of incoming |
2780 | /// edges that this phi node will have (use 0 if you really have no idea). |
2781 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, |
2782 | const Twine &NameStr = "", |
2783 | Instruction *InsertBefore = nullptr) { |
2784 | return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore); |
2785 | } |
2786 | |
2787 | static PHINode *Create(Type *Ty, unsigned NumReservedValues, |
2788 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
2789 | return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd); |
2790 | } |
2791 | |
2792 | /// Provide fast operand accessors |
2793 | 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; |
2794 | |
2795 | // Block iterator interface. This provides access to the list of incoming |
2796 | // basic blocks, which parallels the list of incoming values. |
2797 | // Please note that we are not providing non-const iterators for blocks to |
2798 | // force all updates go through an interface function. |
2799 | |
2800 | using block_iterator = BasicBlock **; |
2801 | using const_block_iterator = BasicBlock * const *; |
2802 | |
2803 | const_block_iterator block_begin() const { |
2804 | return reinterpret_cast<const_block_iterator>(op_begin() + ReservedSpace); |
2805 | } |
2806 | |
2807 | const_block_iterator block_end() const { |
2808 | return block_begin() + getNumOperands(); |
2809 | } |
2810 | |
2811 | iterator_range<const_block_iterator> blocks() const { |
2812 | return make_range(block_begin(), block_end()); |
2813 | } |
2814 | |
2815 | op_range incoming_values() { return operands(); } |
2816 | |
2817 | const_op_range incoming_values() const { return operands(); } |
2818 | |
2819 | /// Return the number of incoming edges |
2820 | /// |
2821 | unsigned getNumIncomingValues() const { return getNumOperands(); } |
2822 | |
2823 | /// Return incoming value number x |
2824 | /// |
2825 | Value *getIncomingValue(unsigned i) const { |
2826 | return getOperand(i); |
2827 | } |
2828 | void setIncomingValue(unsigned i, Value *V) { |
2829 | assert(V && "PHI node got a null value!")(static_cast <bool> (V && "PHI node got a null value!" ) ? void (0) : __assert_fail ("V && \"PHI node got a null value!\"" , "llvm/include/llvm/IR/Instructions.h", 2829, __extension__ __PRETTY_FUNCTION__ )); |
2830 | assert(getType() == V->getType() &&(static_cast <bool> (getType() == V->getType() && "All operands to PHI node must be the same type as the PHI node!" ) ? void (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\"" , "llvm/include/llvm/IR/Instructions.h", 2831, __extension__ __PRETTY_FUNCTION__ )) |
2831 | "All operands to PHI node must be the same type as the PHI node!")(static_cast <bool> (getType() == V->getType() && "All operands to PHI node must be the same type as the PHI node!" ) ? void (0) : __assert_fail ("getType() == V->getType() && \"All operands to PHI node must be the same type as the PHI node!\"" , "llvm/include/llvm/IR/Instructions.h", 2831, __extension__ __PRETTY_FUNCTION__ )); |
2832 | setOperand(i, V); |
2833 | } |
2834 | |
2835 | static unsigned getOperandNumForIncomingValue(unsigned i) { |
2836 | return i; |
2837 | } |
2838 | |
2839 | static unsigned getIncomingValueNumForOperand(unsigned i) { |
2840 | return i; |
2841 | } |
2842 | |
2843 | /// Return incoming basic block number @p i. |
2844 | /// |
2845 | BasicBlock *getIncomingBlock(unsigned i) const { |
2846 | return block_begin()[i]; |
2847 | } |
2848 | |
2849 | /// Return incoming basic block corresponding |
2850 | /// to an operand of the PHI. |
2851 | /// |
2852 | BasicBlock *getIncomingBlock(const Use &U) const { |
2853 | assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?")(static_cast <bool> (this == U.getUser() && "Iterator doesn't point to PHI's Uses?" ) ? void (0) : __assert_fail ("this == U.getUser() && \"Iterator doesn't point to PHI's Uses?\"" , "llvm/include/llvm/IR/Instructions.h", 2853, __extension__ __PRETTY_FUNCTION__ )); |
2854 | return getIncomingBlock(unsigned(&U - op_begin())); |
2855 | } |
2856 | |
2857 | /// Return incoming basic block corresponding |
2858 | /// to value use iterator. |
2859 | /// |
2860 | BasicBlock *getIncomingBlock(Value::const_user_iterator I) const { |
2861 | return getIncomingBlock(I.getUse()); |
2862 | } |
2863 | |
2864 | void setIncomingBlock(unsigned i, BasicBlock *BB) { |
2865 | const_cast<block_iterator>(block_begin())[i] = BB; |
2866 | } |
2867 | |
2868 | /// Copies the basic blocks from \p BBRange to the incoming basic block list |
2869 | /// of this PHINode, starting at \p ToIdx. |
2870 | void copyIncomingBlocks(iterator_range<const_block_iterator> BBRange, |
2871 | uint32_t ToIdx = 0) { |
2872 | copy(BBRange, const_cast<block_iterator>(block_begin()) + ToIdx); |
2873 | } |
2874 | |
2875 | /// Replace every incoming basic block \p Old to basic block \p New. |
2876 | void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New) { |
2877 | assert(New && Old && "PHI node got a null basic block!")(static_cast <bool> (New && Old && "PHI node got a null basic block!" ) ? void (0) : __assert_fail ("New && Old && \"PHI node got a null basic block!\"" , "llvm/include/llvm/IR/Instructions.h", 2877, __extension__ __PRETTY_FUNCTION__ )); |
2878 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) |
2879 | if (getIncomingBlock(Op) == Old) |
2880 | setIncomingBlock(Op, New); |
2881 | } |
2882 | |
2883 | /// Add an incoming value to the end of the PHI list |
2884 | /// |
2885 | void addIncoming(Value *V, BasicBlock *BB) { |
2886 | if (getNumOperands() == ReservedSpace) |
2887 | growOperands(); // Get more space! |
2888 | // Initialize some new operands. |
2889 | setNumHungOffUseOperands(getNumOperands() + 1); |
2890 | setIncomingValue(getNumOperands() - 1, V); |
2891 | setIncomingBlock(getNumOperands() - 1, BB); |
2892 | } |
2893 | |
2894 | /// Remove an incoming value. This is useful if a |
2895 | /// predecessor basic block is deleted. The value removed is returned. |
2896 | /// |
2897 | /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty |
2898 | /// is true), the PHI node is destroyed and any uses of it are replaced with |
2899 | /// dummy values. The only time there should be zero incoming values to a PHI |
2900 | /// node is when the block is dead, so this strategy is sound. |
2901 | /// |
2902 | Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true); |
2903 | |
2904 | Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) { |
2905 | int Idx = getBasicBlockIndex(BB); |
2906 | assert(Idx >= 0 && "Invalid basic block argument to remove!")(static_cast <bool> (Idx >= 0 && "Invalid basic block argument to remove!" ) ? void (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument to remove!\"" , "llvm/include/llvm/IR/Instructions.h", 2906, __extension__ __PRETTY_FUNCTION__ )); |
2907 | return removeIncomingValue(Idx, DeletePHIIfEmpty); |
2908 | } |
2909 | |
2910 | /// Return the first index of the specified basic |
2911 | /// block in the value list for this PHI. Returns -1 if no instance. |
2912 | /// |
2913 | int getBasicBlockIndex(const BasicBlock *BB) const { |
2914 | for (unsigned i = 0, e = getNumOperands(); i != e; ++i) |
2915 | if (block_begin()[i] == BB) |
2916 | return i; |
2917 | return -1; |
2918 | } |
2919 | |
2920 | Value *getIncomingValueForBlock(const BasicBlock *BB) const { |
2921 | int Idx = getBasicBlockIndex(BB); |
2922 | assert(Idx >= 0 && "Invalid basic block argument!")(static_cast <bool> (Idx >= 0 && "Invalid basic block argument!" ) ? void (0) : __assert_fail ("Idx >= 0 && \"Invalid basic block argument!\"" , "llvm/include/llvm/IR/Instructions.h", 2922, __extension__ __PRETTY_FUNCTION__ )); |
2923 | return getIncomingValue(Idx); |
2924 | } |
2925 | |
2926 | /// Set every incoming value(s) for block \p BB to \p V. |
2927 | void setIncomingValueForBlock(const BasicBlock *BB, Value *V) { |
2928 | assert(BB && "PHI node got a null basic block!")(static_cast <bool> (BB && "PHI node got a null basic block!" ) ? void (0) : __assert_fail ("BB && \"PHI node got a null basic block!\"" , "llvm/include/llvm/IR/Instructions.h", 2928, __extension__ __PRETTY_FUNCTION__ )); |
2929 | bool Found = false; |
2930 | for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op) |
2931 | if (getIncomingBlock(Op) == BB) { |
2932 | Found = true; |
2933 | setIncomingValue(Op, V); |
2934 | } |
2935 | (void)Found; |
2936 | assert(Found && "Invalid basic block argument to set!")(static_cast <bool> (Found && "Invalid basic block argument to set!" ) ? void (0) : __assert_fail ("Found && \"Invalid basic block argument to set!\"" , "llvm/include/llvm/IR/Instructions.h", 2936, __extension__ __PRETTY_FUNCTION__ )); |
2937 | } |
2938 | |
2939 | /// If the specified PHI node always merges together the |
2940 | /// same value, return the value, otherwise return null. |
2941 | Value *hasConstantValue() const; |
2942 | |
2943 | /// Whether the specified PHI node always merges |
2944 | /// together the same value, assuming undefs are equal to a unique |
2945 | /// non-undef value. |
2946 | bool hasConstantOrUndefValue() const; |
2947 | |
2948 | /// If the PHI node is complete which means all of its parent's predecessors |
2949 | /// have incoming value in this PHI, return true, otherwise return false. |
2950 | bool isComplete() const { |
2951 | return llvm::all_of(predecessors(getParent()), |
2952 | [this](const BasicBlock *Pred) { |
2953 | return getBasicBlockIndex(Pred) >= 0; |
2954 | }); |
2955 | } |
2956 | |
2957 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
2958 | static bool classof(const Instruction *I) { |
2959 | return I->getOpcode() == Instruction::PHI; |
2960 | } |
2961 | static bool classof(const Value *V) { |
2962 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
2963 | } |
2964 | |
2965 | private: |
2966 | void growOperands(); |
2967 | }; |
2968 | |
2969 | template <> |
2970 | struct OperandTraits<PHINode> : public HungoffOperandTraits<2> { |
2971 | }; |
2972 | |
2973 | 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 { (static_cast <bool> (i_nocapture < OperandTraits <PHINode>::operands(this) && "getOperand() out of range!" ) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 2973, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<PHINode>::operands(this) && "setOperand() out of range!" ) ? void (0) : __assert_fail ("i_nocapture < OperandTraits<PHINode>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 2973, __extension__ __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); } |
2974 | |
2975 | //===----------------------------------------------------------------------===// |
2976 | // LandingPadInst Class |
2977 | //===----------------------------------------------------------------------===// |
2978 | |
2979 | //===--------------------------------------------------------------------------- |
2980 | /// The landingpad instruction holds all of the information |
2981 | /// necessary to generate correct exception handling. The landingpad instruction |
2982 | /// cannot be moved from the top of a landing pad block, which itself is |
2983 | /// accessible only from the 'unwind' edge of an invoke. This uses the |
2984 | /// SubclassData field in Value to store whether or not the landingpad is a |
2985 | /// cleanup. |
2986 | /// |
2987 | class LandingPadInst : public Instruction { |
2988 | using CleanupField = BoolBitfieldElementT<0>; |
2989 | |
2990 | /// The number of operands actually allocated. NumOperands is |
2991 | /// the number actually in use. |
2992 | unsigned ReservedSpace; |
2993 | |
2994 | LandingPadInst(const LandingPadInst &LP); |
2995 | |
2996 | public: |
2997 | enum ClauseType { Catch, Filter }; |
2998 | |
2999 | private: |
3000 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
3001 | const Twine &NameStr, Instruction *InsertBefore); |
3002 | explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
3003 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
3004 | |
3005 | // Allocate space for exactly zero operands. |
3006 | void *operator new(size_t S) { return User::operator new(S); } |
3007 | |
3008 | void growOperands(unsigned Size); |
3009 | void init(unsigned NumReservedValues, const Twine &NameStr); |
3010 | |
3011 | protected: |
3012 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3013 | friend class Instruction; |
3014 | |
3015 | LandingPadInst *cloneImpl() const; |
3016 | |
3017 | public: |
3018 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
3019 | |
3020 | /// Constructors - NumReservedClauses is a hint for the number of incoming |
3021 | /// clauses that this landingpad will have (use 0 if you really have no idea). |
3022 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, |
3023 | const Twine &NameStr = "", |
3024 | Instruction *InsertBefore = nullptr); |
3025 | static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses, |
3026 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
3027 | |
3028 | /// Provide fast operand accessors |
3029 | 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; |
3030 | |
3031 | /// Return 'true' if this landingpad instruction is a |
3032 | /// cleanup. I.e., it should be run when unwinding even if its landing pad |
3033 | /// doesn't catch the exception. |
3034 | bool isCleanup() const { return getSubclassData<CleanupField>(); } |
3035 | |
3036 | /// Indicate that this landingpad instruction is a cleanup. |
3037 | void setCleanup(bool V) { setSubclassData<CleanupField>(V); } |
3038 | |
3039 | /// Add a catch or filter clause to the landing pad. |
3040 | void addClause(Constant *ClauseVal); |
3041 | |
3042 | /// Get the value of the clause at index Idx. Use isCatch/isFilter to |
3043 | /// determine what type of clause this is. |
3044 | Constant *getClause(unsigned Idx) const { |
3045 | return cast<Constant>(getOperandList()[Idx]); |
3046 | } |
3047 | |
3048 | /// Return 'true' if the clause and index Idx is a catch clause. |
3049 | bool isCatch(unsigned Idx) const { |
3050 | return !isa<ArrayType>(getOperandList()[Idx]->getType()); |
3051 | } |
3052 | |
3053 | /// Return 'true' if the clause and index Idx is a filter clause. |
3054 | bool isFilter(unsigned Idx) const { |
3055 | return isa<ArrayType>(getOperandList()[Idx]->getType()); |
3056 | } |
3057 | |
3058 | /// Get the number of clauses for this landing pad. |
3059 | unsigned getNumClauses() const { return getNumOperands(); } |
3060 | |
3061 | /// Grow the size of the operand list to accommodate the new |
3062 | /// number of clauses. |
3063 | void reserveClauses(unsigned Size) { growOperands(Size); } |
3064 | |
3065 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3066 | static bool classof(const Instruction *I) { |
3067 | return I->getOpcode() == Instruction::LandingPad; |
3068 | } |
3069 | static bool classof(const Value *V) { |
3070 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3071 | } |
3072 | }; |
3073 | |
3074 | template <> |
3075 | struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> { |
3076 | }; |
3077 | |
3078 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<LandingPadInst>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3078, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<LandingPadInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<LandingPadInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3078, __extension__ __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 ); } |
3079 | |
3080 | //===----------------------------------------------------------------------===// |
3081 | // ReturnInst Class |
3082 | //===----------------------------------------------------------------------===// |
3083 | |
3084 | //===--------------------------------------------------------------------------- |
3085 | /// Return a value (possibly void), from a function. Execution |
3086 | /// does not continue in this function any longer. |
3087 | /// |
3088 | class ReturnInst : public Instruction { |
3089 | ReturnInst(const ReturnInst &RI); |
3090 | |
3091 | private: |
3092 | // ReturnInst constructors: |
3093 | // ReturnInst() - 'ret void' instruction |
3094 | // ReturnInst( null) - 'ret void' instruction |
3095 | // ReturnInst(Value* X) - 'ret X' instruction |
3096 | // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I |
3097 | // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I |
3098 | // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B |
3099 | // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B |
3100 | // |
3101 | // NOTE: If the Value* passed is of type void then the constructor behaves as |
3102 | // if it was passed NULL. |
3103 | explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr, |
3104 | Instruction *InsertBefore = nullptr); |
3105 | ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd); |
3106 | explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd); |
3107 | |
3108 | protected: |
3109 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3110 | friend class Instruction; |
3111 | |
3112 | ReturnInst *cloneImpl() const; |
3113 | |
3114 | public: |
3115 | static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr, |
3116 | Instruction *InsertBefore = nullptr) { |
3117 | return new(!!retVal) ReturnInst(C, retVal, InsertBefore); |
3118 | } |
3119 | |
3120 | static ReturnInst* Create(LLVMContext &C, Value *retVal, |
3121 | BasicBlock *InsertAtEnd) { |
3122 | return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd); |
3123 | } |
3124 | |
3125 | static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) { |
3126 | return new(0) ReturnInst(C, InsertAtEnd); |
3127 | } |
3128 | |
3129 | /// Provide fast operand accessors |
3130 | 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; |
3131 | |
3132 | /// Convenience accessor. Returns null if there is no return value. |
3133 | Value *getReturnValue() const { |
3134 | return getNumOperands() != 0 ? getOperand(0) : nullptr; |
3135 | } |
3136 | |
3137 | unsigned getNumSuccessors() const { return 0; } |
3138 | |
3139 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3140 | static bool classof(const Instruction *I) { |
3141 | return (I->getOpcode() == Instruction::Ret); |
3142 | } |
3143 | static bool classof(const Value *V) { |
3144 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3145 | } |
3146 | |
3147 | private: |
3148 | BasicBlock *getSuccessor(unsigned idx) const { |
3149 | llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!" , "llvm/include/llvm/IR/Instructions.h", 3149); |
3150 | } |
3151 | |
3152 | void setSuccessor(unsigned idx, BasicBlock *B) { |
3153 | llvm_unreachable("ReturnInst has no successors!")::llvm::llvm_unreachable_internal("ReturnInst has no successors!" , "llvm/include/llvm/IR/Instructions.h", 3153); |
3154 | } |
3155 | }; |
3156 | |
3157 | template <> |
3158 | struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> { |
3159 | }; |
3160 | |
3161 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<ReturnInst>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3161, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<ReturnInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ReturnInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3161, __extension__ __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); } |
3162 | |
3163 | //===----------------------------------------------------------------------===// |
3164 | // BranchInst Class |
3165 | //===----------------------------------------------------------------------===// |
3166 | |
3167 | //===--------------------------------------------------------------------------- |
3168 | /// Conditional or Unconditional Branch instruction. |
3169 | /// |
3170 | class BranchInst : public Instruction { |
3171 | /// Ops list - Branches are strange. The operands are ordered: |
3172 | /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because |
3173 | /// they don't have to check for cond/uncond branchness. These are mostly |
3174 | /// accessed relative from op_end(). |
3175 | BranchInst(const BranchInst &BI); |
3176 | // BranchInst constructors (where {B, T, F} are blocks, and C is a condition): |
3177 | // BranchInst(BB *B) - 'br B' |
3178 | // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F' |
3179 | // BranchInst(BB* B, Inst *I) - 'br B' insert before I |
3180 | // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I |
3181 | // BranchInst(BB* B, BB *I) - 'br B' insert at end |
3182 | // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end |
3183 | explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr); |
3184 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
3185 | Instruction *InsertBefore = nullptr); |
3186 | BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd); |
3187 | BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
3188 | BasicBlock *InsertAtEnd); |
3189 | |
3190 | void AssertOK(); |
3191 | |
3192 | protected: |
3193 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3194 | friend class Instruction; |
3195 | |
3196 | BranchInst *cloneImpl() const; |
3197 | |
3198 | public: |
3199 | /// Iterator type that casts an operand to a basic block. |
3200 | /// |
3201 | /// This only makes sense because the successors are stored as adjacent |
3202 | /// operands for branch instructions. |
3203 | struct succ_op_iterator |
3204 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, |
3205 | std::random_access_iterator_tag, BasicBlock *, |
3206 | ptrdiff_t, BasicBlock *, BasicBlock *> { |
3207 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} |
3208 | |
3209 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
3210 | BasicBlock *operator->() const { return operator*(); } |
3211 | }; |
3212 | |
3213 | /// The const version of `succ_op_iterator`. |
3214 | struct const_succ_op_iterator |
3215 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, |
3216 | std::random_access_iterator_tag, |
3217 | const BasicBlock *, ptrdiff_t, const BasicBlock *, |
3218 | const BasicBlock *> { |
3219 | explicit const_succ_op_iterator(const_value_op_iterator I) |
3220 | : iterator_adaptor_base(I) {} |
3221 | |
3222 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
3223 | const BasicBlock *operator->() const { return operator*(); } |
3224 | }; |
3225 | |
3226 | static BranchInst *Create(BasicBlock *IfTrue, |
3227 | Instruction *InsertBefore = nullptr) { |
3228 | return new(1) BranchInst(IfTrue, InsertBefore); |
3229 | } |
3230 | |
3231 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, |
3232 | Value *Cond, Instruction *InsertBefore = nullptr) { |
3233 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore); |
3234 | } |
3235 | |
3236 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) { |
3237 | return new(1) BranchInst(IfTrue, InsertAtEnd); |
3238 | } |
3239 | |
3240 | static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse, |
3241 | Value *Cond, BasicBlock *InsertAtEnd) { |
3242 | return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd); |
3243 | } |
3244 | |
3245 | /// Transparently provide more efficient getOperand methods. |
3246 | 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; |
3247 | |
3248 | bool isUnconditional() const { return getNumOperands() == 1; } |
3249 | bool isConditional() const { return getNumOperands() == 3; } |
3250 | |
3251 | Value *getCondition() const { |
3252 | assert(isConditional() && "Cannot get condition of an uncond branch!")(static_cast <bool> (isConditional() && "Cannot get condition of an uncond branch!" ) ? void (0) : __assert_fail ("isConditional() && \"Cannot get condition of an uncond branch!\"" , "llvm/include/llvm/IR/Instructions.h", 3252, __extension__ __PRETTY_FUNCTION__ )); |
3253 | return Op<-3>(); |
3254 | } |
3255 | |
3256 | void setCondition(Value *V) { |
3257 | assert(isConditional() && "Cannot set condition of unconditional branch!")(static_cast <bool> (isConditional() && "Cannot set condition of unconditional branch!" ) ? void (0) : __assert_fail ("isConditional() && \"Cannot set condition of unconditional branch!\"" , "llvm/include/llvm/IR/Instructions.h", 3257, __extension__ __PRETTY_FUNCTION__ )); |
3258 | Op<-3>() = V; |
3259 | } |
3260 | |
3261 | unsigned getNumSuccessors() const { return 1+isConditional(); } |
3262 | |
3263 | BasicBlock *getSuccessor(unsigned i) const { |
3264 | assert(i < getNumSuccessors() && "Successor # out of range for Branch!")(static_cast <bool> (i < getNumSuccessors() && "Successor # out of range for Branch!") ? void (0) : __assert_fail ("i < getNumSuccessors() && \"Successor # out of range for Branch!\"" , "llvm/include/llvm/IR/Instructions.h", 3264, __extension__ __PRETTY_FUNCTION__ )); |
3265 | return cast_or_null<BasicBlock>((&Op<-1>() - i)->get()); |
3266 | } |
3267 | |
3268 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
3269 | assert(idx < getNumSuccessors() && "Successor # out of range for Branch!")(static_cast <bool> (idx < getNumSuccessors() && "Successor # out of range for Branch!") ? void (0) : __assert_fail ("idx < getNumSuccessors() && \"Successor # out of range for Branch!\"" , "llvm/include/llvm/IR/Instructions.h", 3269, __extension__ __PRETTY_FUNCTION__ )); |
3270 | *(&Op<-1>() - idx) = NewSucc; |
3271 | } |
3272 | |
3273 | /// Swap the successors of this branch instruction. |
3274 | /// |
3275 | /// Swaps the successors of the branch instruction. This also swaps any |
3276 | /// branch weight metadata associated with the instruction so that it |
3277 | /// continues to map correctly to each operand. |
3278 | void swapSuccessors(); |
3279 | |
3280 | iterator_range<succ_op_iterator> successors() { |
3281 | return make_range( |
3282 | succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)), |
3283 | succ_op_iterator(value_op_end())); |
3284 | } |
3285 | |
3286 | iterator_range<const_succ_op_iterator> successors() const { |
3287 | return make_range(const_succ_op_iterator( |
3288 | std::next(value_op_begin(), isConditional() ? 1 : 0)), |
3289 | const_succ_op_iterator(value_op_end())); |
3290 | } |
3291 | |
3292 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3293 | static bool classof(const Instruction *I) { |
3294 | return (I->getOpcode() == Instruction::Br); |
3295 | } |
3296 | static bool classof(const Value *V) { |
3297 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3298 | } |
3299 | }; |
3300 | |
3301 | template <> |
3302 | struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> { |
3303 | }; |
3304 | |
3305 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<BranchInst>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3305, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<BranchInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<BranchInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3305, __extension__ __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); } |
3306 | |
3307 | //===----------------------------------------------------------------------===// |
3308 | // SwitchInst Class |
3309 | //===----------------------------------------------------------------------===// |
3310 | |
3311 | //===--------------------------------------------------------------------------- |
3312 | /// Multiway switch |
3313 | /// |
3314 | class SwitchInst : public Instruction { |
3315 | unsigned ReservedSpace; |
3316 | |
3317 | // Operand[0] = Value to switch on |
3318 | // Operand[1] = Default basic block destination |
3319 | // Operand[2n ] = Value to match |
3320 | // Operand[2n+1] = BasicBlock to go to on match |
3321 | SwitchInst(const SwitchInst &SI); |
3322 | |
3323 | /// Create a new switch instruction, specifying a value to switch on and a |
3324 | /// default destination. The number of additional cases can be specified here |
3325 | /// to make memory allocation more efficient. This constructor can also |
3326 | /// auto-insert before another instruction. |
3327 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
3328 | Instruction *InsertBefore); |
3329 | |
3330 | /// Create a new switch instruction, specifying a value to switch on and a |
3331 | /// default destination. The number of additional cases can be specified here |
3332 | /// to make memory allocation more efficient. This constructor also |
3333 | /// auto-inserts at the end of the specified BasicBlock. |
3334 | SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
3335 | BasicBlock *InsertAtEnd); |
3336 | |
3337 | // allocate space for exactly zero operands |
3338 | void *operator new(size_t S) { return User::operator new(S); } |
3339 | |
3340 | void init(Value *Value, BasicBlock *Default, unsigned NumReserved); |
3341 | void growOperands(); |
3342 | |
3343 | protected: |
3344 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3345 | friend class Instruction; |
3346 | |
3347 | SwitchInst *cloneImpl() const; |
3348 | |
3349 | public: |
3350 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
3351 | |
3352 | // -2 |
3353 | static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1); |
3354 | |
3355 | template <typename CaseHandleT> class CaseIteratorImpl; |
3356 | |
3357 | /// A handle to a particular switch case. It exposes a convenient interface |
3358 | /// to both the case value and the successor block. |
3359 | /// |
3360 | /// We define this as a template and instantiate it to form both a const and |
3361 | /// non-const handle. |
3362 | template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT> |
3363 | class CaseHandleImpl { |
3364 | // Directly befriend both const and non-const iterators. |
3365 | friend class SwitchInst::CaseIteratorImpl< |
3366 | CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>; |
3367 | |
3368 | protected: |
3369 | // Expose the switch type we're parameterized with to the iterator. |
3370 | using SwitchInstType = SwitchInstT; |
3371 | |
3372 | SwitchInstT *SI; |
3373 | ptrdiff_t Index; |
3374 | |
3375 | CaseHandleImpl() = default; |
3376 | CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index) : SI(SI), Index(Index) {} |
3377 | |
3378 | public: |
3379 | /// Resolves case value for current case. |
3380 | ConstantIntT *getCaseValue() const { |
3381 | assert((unsigned)Index < SI->getNumCases() &&(static_cast <bool> ((unsigned)Index < SI->getNumCases () && "Index out the number of cases.") ? void (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3382, __extension__ __PRETTY_FUNCTION__ )) |
3382 | "Index out the number of cases.")(static_cast <bool> ((unsigned)Index < SI->getNumCases () && "Index out the number of cases.") ? void (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3382, __extension__ __PRETTY_FUNCTION__ )); |
3383 | return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2)); |
3384 | } |
3385 | |
3386 | /// Resolves successor for current case. |
3387 | BasicBlockT *getCaseSuccessor() const { |
3388 | assert(((unsigned)Index < SI->getNumCases() ||(static_cast <bool> (((unsigned)Index < SI->getNumCases () || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? void (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3390, __extension__ __PRETTY_FUNCTION__ )) |
3389 | (unsigned)Index == DefaultPseudoIndex) &&(static_cast <bool> (((unsigned)Index < SI->getNumCases () || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? void (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3390, __extension__ __PRETTY_FUNCTION__ )) |
3390 | "Index out the number of cases.")(static_cast <bool> (((unsigned)Index < SI->getNumCases () || (unsigned)Index == DefaultPseudoIndex) && "Index out the number of cases." ) ? void (0) : __assert_fail ("((unsigned)Index < SI->getNumCases() || (unsigned)Index == DefaultPseudoIndex) && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3390, __extension__ __PRETTY_FUNCTION__ )); |
3391 | return SI->getSuccessor(getSuccessorIndex()); |
3392 | } |
3393 | |
3394 | /// Returns number of current case. |
3395 | unsigned getCaseIndex() const { return Index; } |
3396 | |
3397 | /// Returns successor index for current case successor. |
3398 | unsigned getSuccessorIndex() const { |
3399 | assert(((unsigned)Index == DefaultPseudoIndex ||(static_cast <bool> (((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? void (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3401, __extension__ __PRETTY_FUNCTION__ )) |
3400 | (unsigned)Index < SI->getNumCases()) &&(static_cast <bool> (((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? void (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3401, __extension__ __PRETTY_FUNCTION__ )) |
3401 | "Index out the number of cases.")(static_cast <bool> (((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && "Index out the number of cases." ) ? void (0) : __assert_fail ("((unsigned)Index == DefaultPseudoIndex || (unsigned)Index < SI->getNumCases()) && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3401, __extension__ __PRETTY_FUNCTION__ )); |
3402 | return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0; |
3403 | } |
3404 | |
3405 | bool operator==(const CaseHandleImpl &RHS) const { |
3406 | assert(SI == RHS.SI && "Incompatible operators.")(static_cast <bool> (SI == RHS.SI && "Incompatible operators." ) ? void (0) : __assert_fail ("SI == RHS.SI && \"Incompatible operators.\"" , "llvm/include/llvm/IR/Instructions.h", 3406, __extension__ __PRETTY_FUNCTION__ )); |
3407 | return Index == RHS.Index; |
3408 | } |
3409 | }; |
3410 | |
3411 | using ConstCaseHandle = |
3412 | CaseHandleImpl<const SwitchInst, const ConstantInt, const BasicBlock>; |
3413 | |
3414 | class CaseHandle |
3415 | : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> { |
3416 | friend class SwitchInst::CaseIteratorImpl<CaseHandle>; |
3417 | |
3418 | public: |
3419 | CaseHandle(SwitchInst *SI, ptrdiff_t Index) : CaseHandleImpl(SI, Index) {} |
3420 | |
3421 | /// Sets the new value for current case. |
3422 | void setValue(ConstantInt *V) const { |
3423 | assert((unsigned)Index < SI->getNumCases() &&(static_cast <bool> ((unsigned)Index < SI->getNumCases () && "Index out the number of cases.") ? void (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3424, __extension__ __PRETTY_FUNCTION__ )) |
3424 | "Index out the number of cases.")(static_cast <bool> ((unsigned)Index < SI->getNumCases () && "Index out the number of cases.") ? void (0) : __assert_fail ("(unsigned)Index < SI->getNumCases() && \"Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3424, __extension__ __PRETTY_FUNCTION__ )); |
3425 | SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V)); |
3426 | } |
3427 | |
3428 | /// Sets the new successor for current case. |
3429 | void setSuccessor(BasicBlock *S) const { |
3430 | SI->setSuccessor(getSuccessorIndex(), S); |
3431 | } |
3432 | }; |
3433 | |
3434 | template <typename CaseHandleT> |
3435 | class CaseIteratorImpl |
3436 | : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>, |
3437 | std::random_access_iterator_tag, |
3438 | const CaseHandleT> { |
3439 | using SwitchInstT = typename CaseHandleT::SwitchInstType; |
3440 | |
3441 | CaseHandleT Case; |
3442 | |
3443 | public: |
3444 | /// Default constructed iterator is in an invalid state until assigned to |
3445 | /// a case for a particular switch. |
3446 | CaseIteratorImpl() = default; |
3447 | |
3448 | /// Initializes case iterator for given SwitchInst and for given |
3449 | /// case number. |
3450 | CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {} |
3451 | |
3452 | /// Initializes case iterator for given SwitchInst and for given |
3453 | /// successor index. |
3454 | static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI, |
3455 | unsigned SuccessorIndex) { |
3456 | assert(SuccessorIndex < SI->getNumSuccessors() &&(static_cast <bool> (SuccessorIndex < SI->getNumSuccessors () && "Successor index # out of range!") ? void (0) : __assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3457, __extension__ __PRETTY_FUNCTION__ )) |
3457 | "Successor index # out of range!")(static_cast <bool> (SuccessorIndex < SI->getNumSuccessors () && "Successor index # out of range!") ? void (0) : __assert_fail ("SuccessorIndex < SI->getNumSuccessors() && \"Successor index # out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3457, __extension__ __PRETTY_FUNCTION__ )); |
3458 | return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1) |
3459 | : CaseIteratorImpl(SI, DefaultPseudoIndex); |
3460 | } |
3461 | |
3462 | /// Support converting to the const variant. This will be a no-op for const |
3463 | /// variant. |
3464 | operator CaseIteratorImpl<ConstCaseHandle>() const { |
3465 | return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index); |
3466 | } |
3467 | |
3468 | CaseIteratorImpl &operator+=(ptrdiff_t N) { |
3469 | // Check index correctness after addition. |
3470 | // Note: Index == getNumCases() means end(). |
3471 | assert(Case.Index + N >= 0 &&(static_cast <bool> (Case.Index + N >= 0 && ( unsigned)(Case.Index + N) <= Case.SI->getNumCases() && "Case.Index out the number of cases.") ? void (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3473, __extension__ __PRETTY_FUNCTION__ )) |
3472 | (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&(static_cast <bool> (Case.Index + N >= 0 && ( unsigned)(Case.Index + N) <= Case.SI->getNumCases() && "Case.Index out the number of cases.") ? void (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3473, __extension__ __PRETTY_FUNCTION__ )) |
3473 | "Case.Index out the number of cases.")(static_cast <bool> (Case.Index + N >= 0 && ( unsigned)(Case.Index + N) <= Case.SI->getNumCases() && "Case.Index out the number of cases.") ? void (0) : __assert_fail ("Case.Index + N >= 0 && (unsigned)(Case.Index + N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3473, __extension__ __PRETTY_FUNCTION__ )); |
3474 | Case.Index += N; |
3475 | return *this; |
3476 | } |
3477 | CaseIteratorImpl &operator-=(ptrdiff_t N) { |
3478 | // Check index correctness after subtraction. |
3479 | // Note: Case.Index == getNumCases() means end(). |
3480 | assert(Case.Index - N >= 0 &&(static_cast <bool> (Case.Index - N >= 0 && ( unsigned)(Case.Index - N) <= Case.SI->getNumCases() && "Case.Index out the number of cases.") ? void (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3482, __extension__ __PRETTY_FUNCTION__ )) |
3481 | (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&(static_cast <bool> (Case.Index - N >= 0 && ( unsigned)(Case.Index - N) <= Case.SI->getNumCases() && "Case.Index out the number of cases.") ? void (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3482, __extension__ __PRETTY_FUNCTION__ )) |
3482 | "Case.Index out the number of cases.")(static_cast <bool> (Case.Index - N >= 0 && ( unsigned)(Case.Index - N) <= Case.SI->getNumCases() && "Case.Index out the number of cases.") ? void (0) : __assert_fail ("Case.Index - N >= 0 && (unsigned)(Case.Index - N) <= Case.SI->getNumCases() && \"Case.Index out the number of cases.\"" , "llvm/include/llvm/IR/Instructions.h", 3482, __extension__ __PRETTY_FUNCTION__ )); |
3483 | Case.Index -= N; |
3484 | return *this; |
3485 | } |
3486 | ptrdiff_t operator-(const CaseIteratorImpl &RHS) const { |
3487 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")(static_cast <bool> (Case.SI == RHS.Case.SI && "Incompatible operators." ) ? void (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\"" , "llvm/include/llvm/IR/Instructions.h", 3487, __extension__ __PRETTY_FUNCTION__ )); |
3488 | return Case.Index - RHS.Case.Index; |
3489 | } |
3490 | bool operator==(const CaseIteratorImpl &RHS) const { |
3491 | return Case == RHS.Case; |
3492 | } |
3493 | bool operator<(const CaseIteratorImpl &RHS) const { |
3494 | assert(Case.SI == RHS.Case.SI && "Incompatible operators.")(static_cast <bool> (Case.SI == RHS.Case.SI && "Incompatible operators." ) ? void (0) : __assert_fail ("Case.SI == RHS.Case.SI && \"Incompatible operators.\"" , "llvm/include/llvm/IR/Instructions.h", 3494, __extension__ __PRETTY_FUNCTION__ )); |
3495 | return Case.Index < RHS.Case.Index; |
3496 | } |
3497 | const CaseHandleT &operator*() const { return Case; } |
3498 | }; |
3499 | |
3500 | using CaseIt = CaseIteratorImpl<CaseHandle>; |
3501 | using ConstCaseIt = CaseIteratorImpl<ConstCaseHandle>; |
3502 | |
3503 | static SwitchInst *Create(Value *Value, BasicBlock *Default, |
3504 | unsigned NumCases, |
3505 | Instruction *InsertBefore = nullptr) { |
3506 | return new SwitchInst(Value, Default, NumCases, InsertBefore); |
3507 | } |
3508 | |
3509 | static SwitchInst *Create(Value *Value, BasicBlock *Default, |
3510 | unsigned NumCases, BasicBlock *InsertAtEnd) { |
3511 | return new SwitchInst(Value, Default, NumCases, InsertAtEnd); |
3512 | } |
3513 | |
3514 | /// Provide fast operand accessors |
3515 | 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; |
3516 | |
3517 | // Accessor Methods for Switch stmt |
3518 | Value *getCondition() const { return getOperand(0); } |
3519 | void setCondition(Value *V) { setOperand(0, V); } |
3520 | |
3521 | BasicBlock *getDefaultDest() const { |
3522 | return cast<BasicBlock>(getOperand(1)); |
3523 | } |
3524 | |
3525 | void setDefaultDest(BasicBlock *DefaultCase) { |
3526 | setOperand(1, reinterpret_cast<Value*>(DefaultCase)); |
3527 | } |
3528 | |
3529 | /// Return the number of 'cases' in this switch instruction, excluding the |
3530 | /// default case. |
3531 | unsigned getNumCases() const { |
3532 | return getNumOperands()/2 - 1; |
3533 | } |
3534 | |
3535 | /// Returns a read/write iterator that points to the first case in the |
3536 | /// SwitchInst. |
3537 | CaseIt case_begin() { |
3538 | return CaseIt(this, 0); |
3539 | } |
3540 | |
3541 | /// Returns a read-only iterator that points to the first case in the |
3542 | /// SwitchInst. |
3543 | ConstCaseIt case_begin() const { |
3544 | return ConstCaseIt(this, 0); |
3545 | } |
3546 | |
3547 | /// Returns a read/write iterator that points one past the last in the |
3548 | /// SwitchInst. |
3549 | CaseIt case_end() { |
3550 | return CaseIt(this, getNumCases()); |
3551 | } |
3552 | |
3553 | /// Returns a read-only iterator that points one past the last in the |
3554 | /// SwitchInst. |
3555 | ConstCaseIt case_end() const { |
3556 | return ConstCaseIt(this, getNumCases()); |
3557 | } |
3558 | |
3559 | /// Iteration adapter for range-for loops. |
3560 | iterator_range<CaseIt> cases() { |
3561 | return make_range(case_begin(), case_end()); |
3562 | } |
3563 | |
3564 | /// Constant iteration adapter for range-for loops. |
3565 | iterator_range<ConstCaseIt> cases() const { |
3566 | return make_range(case_begin(), case_end()); |
3567 | } |
3568 | |
3569 | /// Returns an iterator that points to the default case. |
3570 | /// Note: this iterator allows to resolve successor only. Attempt |
3571 | /// to resolve case value causes an assertion. |
3572 | /// Also note, that increment and decrement also causes an assertion and |
3573 | /// makes iterator invalid. |
3574 | CaseIt case_default() { |
3575 | return CaseIt(this, DefaultPseudoIndex); |
3576 | } |
3577 | ConstCaseIt case_default() const { |
3578 | return ConstCaseIt(this, DefaultPseudoIndex); |
3579 | } |
3580 | |
3581 | /// Search all of the case values for the specified constant. If it is |
3582 | /// explicitly handled, return the case iterator of it, otherwise return |
3583 | /// default case iterator to indicate that it is handled by the default |
3584 | /// handler. |
3585 | CaseIt findCaseValue(const ConstantInt *C) { |
3586 | return CaseIt( |
3587 | this, |
3588 | const_cast<const SwitchInst *>(this)->findCaseValue(C)->getCaseIndex()); |
3589 | } |
3590 | ConstCaseIt findCaseValue(const ConstantInt *C) const { |
3591 | ConstCaseIt I = llvm::find_if(cases(), [C](const ConstCaseHandle &Case) { |
3592 | return Case.getCaseValue() == C; |
3593 | }); |
3594 | if (I != case_end()) |
3595 | return I; |
3596 | |
3597 | return case_default(); |
3598 | } |
3599 | |
3600 | /// Finds the unique case value for a given successor. Returns null if the |
3601 | /// successor is not found, not unique, or is the default case. |
3602 | ConstantInt *findCaseDest(BasicBlock *BB) { |
3603 | if (BB == getDefaultDest()) |
3604 | return nullptr; |
3605 | |
3606 | ConstantInt *CI = nullptr; |
3607 | for (auto Case : cases()) { |
3608 | if (Case.getCaseSuccessor() != BB) |
3609 | continue; |
3610 | |
3611 | if (CI) |
3612 | return nullptr; // Multiple cases lead to BB. |
3613 | |
3614 | CI = Case.getCaseValue(); |
3615 | } |
3616 | |
3617 | return CI; |
3618 | } |
3619 | |
3620 | /// Add an entry to the switch instruction. |
3621 | /// Note: |
3622 | /// This action invalidates case_end(). Old case_end() iterator will |
3623 | /// point to the added case. |
3624 | void addCase(ConstantInt *OnVal, BasicBlock *Dest); |
3625 | |
3626 | /// This method removes the specified case and its successor from the switch |
3627 | /// instruction. Note that this operation may reorder the remaining cases at |
3628 | /// index idx and above. |
3629 | /// Note: |
3630 | /// This action invalidates iterators for all cases following the one removed, |
3631 | /// including the case_end() iterator. It returns an iterator for the next |
3632 | /// case. |
3633 | CaseIt removeCase(CaseIt I); |
3634 | |
3635 | unsigned getNumSuccessors() const { return getNumOperands()/2; } |
3636 | BasicBlock *getSuccessor(unsigned idx) const { |
3637 | assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!")(static_cast <bool> (idx < getNumSuccessors() && "Successor idx out of range for switch!") ? void (0) : __assert_fail ("idx < getNumSuccessors() &&\"Successor idx out of range for switch!\"" , "llvm/include/llvm/IR/Instructions.h", 3637, __extension__ __PRETTY_FUNCTION__ )); |
3638 | return cast<BasicBlock>(getOperand(idx*2+1)); |
3639 | } |
3640 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
3641 | assert(idx < getNumSuccessors() && "Successor # out of range for switch!")(static_cast <bool> (idx < getNumSuccessors() && "Successor # out of range for switch!") ? void (0) : __assert_fail ("idx < getNumSuccessors() && \"Successor # out of range for switch!\"" , "llvm/include/llvm/IR/Instructions.h", 3641, __extension__ __PRETTY_FUNCTION__ )); |
3642 | setOperand(idx * 2 + 1, NewSucc); |
3643 | } |
3644 | |
3645 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3646 | static bool classof(const Instruction *I) { |
3647 | return I->getOpcode() == Instruction::Switch; |
3648 | } |
3649 | static bool classof(const Value *V) { |
3650 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3651 | } |
3652 | }; |
3653 | |
3654 | /// A wrapper class to simplify modification of SwitchInst cases along with |
3655 | /// their prof branch_weights metadata. |
3656 | class SwitchInstProfUpdateWrapper { |
3657 | SwitchInst &SI; |
3658 | std::optional<SmallVector<uint32_t, 8>> Weights; |
3659 | bool Changed = false; |
3660 | |
3661 | protected: |
3662 | MDNode *buildProfBranchWeightsMD(); |
3663 | |
3664 | void init(); |
3665 | |
3666 | public: |
3667 | using CaseWeightOpt = std::optional<uint32_t>; |
3668 | SwitchInst *operator->() { return &SI; } |
3669 | SwitchInst &operator*() { return SI; } |
3670 | operator SwitchInst *() { return &SI; } |
3671 | |
3672 | SwitchInstProfUpdateWrapper(SwitchInst &SI) : SI(SI) { init(); } |
3673 | |
3674 | ~SwitchInstProfUpdateWrapper() { |
3675 | if (Changed) |
3676 | SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD()); |
3677 | } |
3678 | |
3679 | /// Delegate the call to the underlying SwitchInst::removeCase() and remove |
3680 | /// correspondent branch weight. |
3681 | SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I); |
3682 | |
3683 | /// Delegate the call to the underlying SwitchInst::addCase() and set the |
3684 | /// specified branch weight for the added case. |
3685 | void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W); |
3686 | |
3687 | /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark |
3688 | /// this object to not touch the underlying SwitchInst in destructor. |
3689 | SymbolTableList<Instruction>::iterator eraseFromParent(); |
3690 | |
3691 | void setSuccessorWeight(unsigned idx, CaseWeightOpt W); |
3692 | CaseWeightOpt getSuccessorWeight(unsigned idx); |
3693 | |
3694 | static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx); |
3695 | }; |
3696 | |
3697 | template <> |
3698 | struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> { |
3699 | }; |
3700 | |
3701 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<SwitchInst>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3701, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<SwitchInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<SwitchInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3701, __extension__ __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); } |
3702 | |
3703 | //===----------------------------------------------------------------------===// |
3704 | // IndirectBrInst Class |
3705 | //===----------------------------------------------------------------------===// |
3706 | |
3707 | //===--------------------------------------------------------------------------- |
3708 | /// Indirect Branch Instruction. |
3709 | /// |
3710 | class IndirectBrInst : public Instruction { |
3711 | unsigned ReservedSpace; |
3712 | |
3713 | // Operand[0] = Address to jump to |
3714 | // Operand[n+1] = n-th destination |
3715 | IndirectBrInst(const IndirectBrInst &IBI); |
3716 | |
3717 | /// Create a new indirectbr instruction, specifying an |
3718 | /// Address to jump to. The number of expected destinations can be specified |
3719 | /// here to make memory allocation more efficient. This constructor can also |
3720 | /// autoinsert before another instruction. |
3721 | IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore); |
3722 | |
3723 | /// Create a new indirectbr instruction, specifying an |
3724 | /// Address to jump to. The number of expected destinations can be specified |
3725 | /// here to make memory allocation more efficient. This constructor also |
3726 | /// autoinserts at the end of the specified BasicBlock. |
3727 | IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd); |
3728 | |
3729 | // allocate space for exactly zero operands |
3730 | void *operator new(size_t S) { return User::operator new(S); } |
3731 | |
3732 | void init(Value *Address, unsigned NumDests); |
3733 | void growOperands(); |
3734 | |
3735 | protected: |
3736 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3737 | friend class Instruction; |
3738 | |
3739 | IndirectBrInst *cloneImpl() const; |
3740 | |
3741 | public: |
3742 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
3743 | |
3744 | /// Iterator type that casts an operand to a basic block. |
3745 | /// |
3746 | /// This only makes sense because the successors are stored as adjacent |
3747 | /// operands for indirectbr instructions. |
3748 | struct succ_op_iterator |
3749 | : iterator_adaptor_base<succ_op_iterator, value_op_iterator, |
3750 | std::random_access_iterator_tag, BasicBlock *, |
3751 | ptrdiff_t, BasicBlock *, BasicBlock *> { |
3752 | explicit succ_op_iterator(value_op_iterator I) : iterator_adaptor_base(I) {} |
3753 | |
3754 | BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
3755 | BasicBlock *operator->() const { return operator*(); } |
3756 | }; |
3757 | |
3758 | /// The const version of `succ_op_iterator`. |
3759 | struct const_succ_op_iterator |
3760 | : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator, |
3761 | std::random_access_iterator_tag, |
3762 | const BasicBlock *, ptrdiff_t, const BasicBlock *, |
3763 | const BasicBlock *> { |
3764 | explicit const_succ_op_iterator(const_value_op_iterator I) |
3765 | : iterator_adaptor_base(I) {} |
3766 | |
3767 | const BasicBlock *operator*() const { return cast<BasicBlock>(*I); } |
3768 | const BasicBlock *operator->() const { return operator*(); } |
3769 | }; |
3770 | |
3771 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, |
3772 | Instruction *InsertBefore = nullptr) { |
3773 | return new IndirectBrInst(Address, NumDests, InsertBefore); |
3774 | } |
3775 | |
3776 | static IndirectBrInst *Create(Value *Address, unsigned NumDests, |
3777 | BasicBlock *InsertAtEnd) { |
3778 | return new IndirectBrInst(Address, NumDests, InsertAtEnd); |
3779 | } |
3780 | |
3781 | /// Provide fast operand accessors. |
3782 | 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; |
3783 | |
3784 | // Accessor Methods for IndirectBrInst instruction. |
3785 | Value *getAddress() { return getOperand(0); } |
3786 | const Value *getAddress() const { return getOperand(0); } |
3787 | void setAddress(Value *V) { setOperand(0, V); } |
3788 | |
3789 | /// return the number of possible destinations in this |
3790 | /// indirectbr instruction. |
3791 | unsigned getNumDestinations() const { return getNumOperands()-1; } |
3792 | |
3793 | /// Return the specified destination. |
3794 | BasicBlock *getDestination(unsigned i) { return getSuccessor(i); } |
3795 | const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); } |
3796 | |
3797 | /// Add a destination. |
3798 | /// |
3799 | void addDestination(BasicBlock *Dest); |
3800 | |
3801 | /// This method removes the specified successor from the |
3802 | /// indirectbr instruction. |
3803 | void removeDestination(unsigned i); |
3804 | |
3805 | unsigned getNumSuccessors() const { return getNumOperands()-1; } |
3806 | BasicBlock *getSuccessor(unsigned i) const { |
3807 | return cast<BasicBlock>(getOperand(i+1)); |
3808 | } |
3809 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
3810 | setOperand(i + 1, NewSucc); |
3811 | } |
3812 | |
3813 | iterator_range<succ_op_iterator> successors() { |
3814 | return make_range(succ_op_iterator(std::next(value_op_begin())), |
3815 | succ_op_iterator(value_op_end())); |
3816 | } |
3817 | |
3818 | iterator_range<const_succ_op_iterator> successors() const { |
3819 | return make_range(const_succ_op_iterator(std::next(value_op_begin())), |
3820 | const_succ_op_iterator(value_op_end())); |
3821 | } |
3822 | |
3823 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
3824 | static bool classof(const Instruction *I) { |
3825 | return I->getOpcode() == Instruction::IndirectBr; |
3826 | } |
3827 | static bool classof(const Value *V) { |
3828 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
3829 | } |
3830 | }; |
3831 | |
3832 | template <> |
3833 | struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> { |
3834 | }; |
3835 | |
3836 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<IndirectBrInst>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3836, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<IndirectBrInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 3836, __extension__ __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 ); } |
3837 | |
3838 | //===----------------------------------------------------------------------===// |
3839 | // InvokeInst Class |
3840 | //===----------------------------------------------------------------------===// |
3841 | |
3842 | /// Invoke instruction. The SubclassData field is used to hold the |
3843 | /// calling convention of the call. |
3844 | /// |
3845 | class InvokeInst : public CallBase { |
3846 | /// The number of operands for this call beyond the called function, |
3847 | /// arguments, and operand bundles. |
3848 | static constexpr int NumExtraOperands = 2; |
3849 | |
3850 | /// The index from the end of the operand array to the normal destination. |
3851 | static constexpr int NormalDestOpEndIdx = -3; |
3852 | |
3853 | /// The index from the end of the operand array to the unwind destination. |
3854 | static constexpr int UnwindDestOpEndIdx = -2; |
3855 | |
3856 | InvokeInst(const InvokeInst &BI); |
3857 | |
3858 | /// Construct an InvokeInst given a range of arguments. |
3859 | /// |
3860 | /// Construct an InvokeInst from a range of arguments |
3861 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3862 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3863 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
3864 | const Twine &NameStr, Instruction *InsertBefore); |
3865 | |
3866 | inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3867 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3868 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
3869 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
3870 | |
3871 | void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3872 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3873 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
3874 | |
3875 | /// Compute the number of operands to allocate. |
3876 | static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) { |
3877 | // We need one operand for the called function, plus our extra operands and |
3878 | // the input operand counts provided. |
3879 | return 1 + NumExtraOperands + NumArgs + NumBundleInputs; |
3880 | } |
3881 | |
3882 | protected: |
3883 | // Note: Instruction needs to be a friend here to call cloneImpl. |
3884 | friend class Instruction; |
3885 | |
3886 | InvokeInst *cloneImpl() const; |
3887 | |
3888 | public: |
3889 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3890 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3891 | const Twine &NameStr, |
3892 | Instruction *InsertBefore = nullptr) { |
3893 | int NumOperands = ComputeNumOperands(Args.size()); |
3894 | return new (NumOperands) |
3895 | InvokeInst(Ty, Func, IfNormal, IfException, Args, std::nullopt, |
3896 | NumOperands, NameStr, InsertBefore); |
3897 | } |
3898 | |
3899 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3900 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3901 | ArrayRef<OperandBundleDef> Bundles = std::nullopt, |
3902 | const Twine &NameStr = "", |
3903 | Instruction *InsertBefore = nullptr) { |
3904 | int NumOperands = |
3905 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
3906 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
3907 | |
3908 | return new (NumOperands, DescriptorBytes) |
3909 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, |
3910 | NameStr, InsertBefore); |
3911 | } |
3912 | |
3913 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3914 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3915 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3916 | int NumOperands = ComputeNumOperands(Args.size()); |
3917 | return new (NumOperands) |
3918 | InvokeInst(Ty, Func, IfNormal, IfException, Args, std::nullopt, |
3919 | NumOperands, NameStr, InsertAtEnd); |
3920 | } |
3921 | |
3922 | static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
3923 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3924 | ArrayRef<OperandBundleDef> Bundles, |
3925 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3926 | int NumOperands = |
3927 | ComputeNumOperands(Args.size(), CountBundleInputs(Bundles)); |
3928 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
3929 | |
3930 | return new (NumOperands, DescriptorBytes) |
3931 | InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands, |
3932 | NameStr, InsertAtEnd); |
3933 | } |
3934 | |
3935 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
3936 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3937 | const Twine &NameStr, |
3938 | Instruction *InsertBefore = nullptr) { |
3939 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
3940 | IfException, Args, std::nullopt, NameStr, InsertBefore); |
3941 | } |
3942 | |
3943 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
3944 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3945 | ArrayRef<OperandBundleDef> Bundles = std::nullopt, |
3946 | const Twine &NameStr = "", |
3947 | Instruction *InsertBefore = nullptr) { |
3948 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
3949 | IfException, Args, Bundles, NameStr, InsertBefore); |
3950 | } |
3951 | |
3952 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
3953 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3954 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3955 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
3956 | IfException, Args, NameStr, InsertAtEnd); |
3957 | } |
3958 | |
3959 | static InvokeInst *Create(FunctionCallee Func, BasicBlock *IfNormal, |
3960 | BasicBlock *IfException, ArrayRef<Value *> Args, |
3961 | ArrayRef<OperandBundleDef> Bundles, |
3962 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
3963 | return Create(Func.getFunctionType(), Func.getCallee(), IfNormal, |
3964 | IfException, Args, Bundles, NameStr, InsertAtEnd); |
3965 | } |
3966 | |
3967 | /// Create a clone of \p II with a different set of operand bundles and |
3968 | /// insert it before \p InsertPt. |
3969 | /// |
3970 | /// The returned invoke instruction is identical to \p II in every way except |
3971 | /// that the operand bundles for the new instruction are set to the operand |
3972 | /// bundles in \p Bundles. |
3973 | static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles, |
3974 | Instruction *InsertPt = nullptr); |
3975 | |
3976 | // get*Dest - Return the destination basic blocks... |
3977 | BasicBlock *getNormalDest() const { |
3978 | return cast<BasicBlock>(Op<NormalDestOpEndIdx>()); |
3979 | } |
3980 | BasicBlock *getUnwindDest() const { |
3981 | return cast<BasicBlock>(Op<UnwindDestOpEndIdx>()); |
3982 | } |
3983 | void setNormalDest(BasicBlock *B) { |
3984 | Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B); |
3985 | } |
3986 | void setUnwindDest(BasicBlock *B) { |
3987 | Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B); |
3988 | } |
3989 | |
3990 | /// Get the landingpad instruction from the landing pad |
3991 | /// block (the unwind destination). |
3992 | LandingPadInst *getLandingPadInst() const; |
3993 | |
3994 | BasicBlock *getSuccessor(unsigned i) const { |
3995 | assert(i < 2 && "Successor # out of range for invoke!")(static_cast <bool> (i < 2 && "Successor # out of range for invoke!" ) ? void (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\"" , "llvm/include/llvm/IR/Instructions.h", 3995, __extension__ __PRETTY_FUNCTION__ )); |
3996 | return i == 0 ? getNormalDest() : getUnwindDest(); |
3997 | } |
3998 | |
3999 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
4000 | assert(i < 2 && "Successor # out of range for invoke!")(static_cast <bool> (i < 2 && "Successor # out of range for invoke!" ) ? void (0) : __assert_fail ("i < 2 && \"Successor # out of range for invoke!\"" , "llvm/include/llvm/IR/Instructions.h", 4000, __extension__ __PRETTY_FUNCTION__ )); |
4001 | if (i == 0) |
4002 | setNormalDest(NewSucc); |
4003 | else |
4004 | setUnwindDest(NewSucc); |
4005 | } |
4006 | |
4007 | unsigned getNumSuccessors() const { return 2; } |
4008 | |
4009 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4010 | static bool classof(const Instruction *I) { |
4011 | return (I->getOpcode() == Instruction::Invoke); |
4012 | } |
4013 | static bool classof(const Value *V) { |
4014 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4015 | } |
4016 | |
4017 | private: |
4018 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
4019 | // method so that subclasses cannot accidentally use it. |
4020 | template <typename Bitfield> |
4021 | void setSubclassData(typename Bitfield::Type Value) { |
4022 | Instruction::setSubclassData<Bitfield>(Value); |
4023 | } |
4024 | }; |
4025 | |
4026 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
4027 | BasicBlock *IfException, ArrayRef<Value *> Args, |
4028 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
4029 | const Twine &NameStr, Instruction *InsertBefore) |
4030 | : CallBase(Ty->getReturnType(), Instruction::Invoke, |
4031 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
4032 | InsertBefore) { |
4033 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); |
4034 | } |
4035 | |
4036 | InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, |
4037 | BasicBlock *IfException, ArrayRef<Value *> Args, |
4038 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
4039 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
4040 | : CallBase(Ty->getReturnType(), Instruction::Invoke, |
4041 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
4042 | InsertAtEnd) { |
4043 | init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr); |
4044 | } |
4045 | |
4046 | //===----------------------------------------------------------------------===// |
4047 | // CallBrInst Class |
4048 | //===----------------------------------------------------------------------===// |
4049 | |
4050 | /// CallBr instruction, tracking function calls that may not return control but |
4051 | /// instead transfer it to a third location. The SubclassData field is used to |
4052 | /// hold the calling convention of the call. |
4053 | /// |
4054 | class CallBrInst : public CallBase { |
4055 | |
4056 | unsigned NumIndirectDests; |
4057 | |
4058 | CallBrInst(const CallBrInst &BI); |
4059 | |
4060 | /// Construct a CallBrInst given a range of arguments. |
4061 | /// |
4062 | /// Construct a CallBrInst from a range of arguments |
4063 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
4064 | ArrayRef<BasicBlock *> IndirectDests, |
4065 | ArrayRef<Value *> Args, |
4066 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
4067 | const Twine &NameStr, Instruction *InsertBefore); |
4068 | |
4069 | inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
4070 | ArrayRef<BasicBlock *> IndirectDests, |
4071 | ArrayRef<Value *> Args, |
4072 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
4073 | const Twine &NameStr, BasicBlock *InsertAtEnd); |
4074 | |
4075 | void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest, |
4076 | ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args, |
4077 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr); |
4078 | |
4079 | /// Compute the number of operands to allocate. |
4080 | static int ComputeNumOperands(int NumArgs, int NumIndirectDests, |
4081 | int NumBundleInputs = 0) { |
4082 | // We need one operand for the called function, plus our extra operands and |
4083 | // the input operand counts provided. |
4084 | return 2 + NumIndirectDests + NumArgs + NumBundleInputs; |
4085 | } |
4086 | |
4087 | protected: |
4088 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4089 | friend class Instruction; |
4090 | |
4091 | CallBrInst *cloneImpl() const; |
4092 | |
4093 | public: |
4094 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
4095 | BasicBlock *DefaultDest, |
4096 | ArrayRef<BasicBlock *> IndirectDests, |
4097 | ArrayRef<Value *> Args, const Twine &NameStr, |
4098 | Instruction *InsertBefore = nullptr) { |
4099 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); |
4100 | return new (NumOperands) |
4101 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, std::nullopt, |
4102 | NumOperands, NameStr, InsertBefore); |
4103 | } |
4104 | |
4105 | static CallBrInst * |
4106 | Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
4107 | ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args, |
4108 | ArrayRef<OperandBundleDef> Bundles = std::nullopt, |
4109 | const Twine &NameStr = "", Instruction *InsertBefore = nullptr) { |
4110 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), |
4111 | CountBundleInputs(Bundles)); |
4112 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
4113 | |
4114 | return new (NumOperands, DescriptorBytes) |
4115 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, |
4116 | NumOperands, NameStr, InsertBefore); |
4117 | } |
4118 | |
4119 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
4120 | BasicBlock *DefaultDest, |
4121 | ArrayRef<BasicBlock *> IndirectDests, |
4122 | ArrayRef<Value *> Args, const Twine &NameStr, |
4123 | BasicBlock *InsertAtEnd) { |
4124 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size()); |
4125 | return new (NumOperands) |
4126 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, std::nullopt, |
4127 | NumOperands, NameStr, InsertAtEnd); |
4128 | } |
4129 | |
4130 | static CallBrInst *Create(FunctionType *Ty, Value *Func, |
4131 | BasicBlock *DefaultDest, |
4132 | ArrayRef<BasicBlock *> IndirectDests, |
4133 | ArrayRef<Value *> Args, |
4134 | ArrayRef<OperandBundleDef> Bundles, |
4135 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
4136 | int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(), |
4137 | CountBundleInputs(Bundles)); |
4138 | unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo); |
4139 | |
4140 | return new (NumOperands, DescriptorBytes) |
4141 | CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, |
4142 | NumOperands, NameStr, InsertAtEnd); |
4143 | } |
4144 | |
4145 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
4146 | ArrayRef<BasicBlock *> IndirectDests, |
4147 | ArrayRef<Value *> Args, const Twine &NameStr, |
4148 | Instruction *InsertBefore = nullptr) { |
4149 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
4150 | IndirectDests, Args, NameStr, InsertBefore); |
4151 | } |
4152 | |
4153 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
4154 | ArrayRef<BasicBlock *> IndirectDests, |
4155 | ArrayRef<Value *> Args, |
4156 | ArrayRef<OperandBundleDef> Bundles = std::nullopt, |
4157 | const Twine &NameStr = "", |
4158 | Instruction *InsertBefore = nullptr) { |
4159 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
4160 | IndirectDests, Args, Bundles, NameStr, InsertBefore); |
4161 | } |
4162 | |
4163 | static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest, |
4164 | ArrayRef<BasicBlock *> IndirectDests, |
4165 | ArrayRef<Value *> Args, const Twine &NameStr, |
4166 | BasicBlock *InsertAtEnd) { |
4167 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
4168 | IndirectDests, Args, NameStr, InsertAtEnd); |
4169 | } |
4170 | |
4171 | static CallBrInst *Create(FunctionCallee Func, |
4172 | BasicBlock *DefaultDest, |
4173 | ArrayRef<BasicBlock *> IndirectDests, |
4174 | ArrayRef<Value *> Args, |
4175 | ArrayRef<OperandBundleDef> Bundles, |
4176 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
4177 | return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest, |
4178 | IndirectDests, Args, Bundles, NameStr, InsertAtEnd); |
4179 | } |
4180 | |
4181 | /// Create a clone of \p CBI with a different set of operand bundles and |
4182 | /// insert it before \p InsertPt. |
4183 | /// |
4184 | /// The returned callbr instruction is identical to \p CBI in every way |
4185 | /// except that the operand bundles for the new instruction are set to the |
4186 | /// operand bundles in \p Bundles. |
4187 | static CallBrInst *Create(CallBrInst *CBI, |
4188 | ArrayRef<OperandBundleDef> Bundles, |
4189 | Instruction *InsertPt = nullptr); |
4190 | |
4191 | /// Return the number of callbr indirect dest labels. |
4192 | /// |
4193 | unsigned getNumIndirectDests() const { return NumIndirectDests; } |
4194 | |
4195 | /// getIndirectDestLabel - Return the i-th indirect dest label. |
4196 | /// |
4197 | Value *getIndirectDestLabel(unsigned i) const { |
4198 | assert(i < getNumIndirectDests() && "Out of bounds!")(static_cast <bool> (i < getNumIndirectDests() && "Out of bounds!") ? void (0) : __assert_fail ("i < getNumIndirectDests() && \"Out of bounds!\"" , "llvm/include/llvm/IR/Instructions.h", 4198, __extension__ __PRETTY_FUNCTION__ )); |
4199 | return getOperand(i + arg_size() + getNumTotalBundleOperands() + 1); |
4200 | } |
4201 | |
4202 | Value *getIndirectDestLabelUse(unsigned i) const { |
4203 | assert(i < getNumIndirectDests() && "Out of bounds!")(static_cast <bool> (i < getNumIndirectDests() && "Out of bounds!") ? void (0) : __assert_fail ("i < getNumIndirectDests() && \"Out of bounds!\"" , "llvm/include/llvm/IR/Instructions.h", 4203, __extension__ __PRETTY_FUNCTION__ )); |
4204 | return getOperandUse(i + arg_size() + getNumTotalBundleOperands() + 1); |
4205 | } |
4206 | |
4207 | // Return the destination basic blocks... |
4208 | BasicBlock *getDefaultDest() const { |
4209 | return cast<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() - 1)); |
4210 | } |
4211 | BasicBlock *getIndirectDest(unsigned i) const { |
4212 | return cast_or_null<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() + i)); |
4213 | } |
4214 | SmallVector<BasicBlock *, 16> getIndirectDests() const { |
4215 | SmallVector<BasicBlock *, 16> IndirectDests; |
4216 | for (unsigned i = 0, e = getNumIndirectDests(); i < e; ++i) |
4217 | IndirectDests.push_back(getIndirectDest(i)); |
4218 | return IndirectDests; |
4219 | } |
4220 | void setDefaultDest(BasicBlock *B) { |
4221 | *(&Op<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value *>(B); |
4222 | } |
4223 | void setIndirectDest(unsigned i, BasicBlock *B) { |
4224 | *(&Op<-1>() - getNumIndirectDests() + i) = reinterpret_cast<Value *>(B); |
4225 | } |
4226 | |
4227 | BasicBlock *getSuccessor(unsigned i) const { |
4228 | assert(i < getNumSuccessors() + 1 &&(static_cast <bool> (i < getNumSuccessors() + 1 && "Successor # out of range for callbr!") ? void (0) : __assert_fail ("i < getNumSuccessors() + 1 && \"Successor # out of range for callbr!\"" , "llvm/include/llvm/IR/Instructions.h", 4229, __extension__ __PRETTY_FUNCTION__ )) |
4229 | "Successor # out of range for callbr!")(static_cast <bool> (i < getNumSuccessors() + 1 && "Successor # out of range for callbr!") ? void (0) : __assert_fail ("i < getNumSuccessors() + 1 && \"Successor # out of range for callbr!\"" , "llvm/include/llvm/IR/Instructions.h", 4229, __extension__ __PRETTY_FUNCTION__ )); |
4230 | return i == 0 ? getDefaultDest() : getIndirectDest(i - 1); |
4231 | } |
4232 | |
4233 | void setSuccessor(unsigned i, BasicBlock *NewSucc) { |
4234 | assert(i < getNumIndirectDests() + 1 &&(static_cast <bool> (i < getNumIndirectDests() + 1 && "Successor # out of range for callbr!") ? void (0) : __assert_fail ("i < getNumIndirectDests() + 1 && \"Successor # out of range for callbr!\"" , "llvm/include/llvm/IR/Instructions.h", 4235, __extension__ __PRETTY_FUNCTION__ )) |
4235 | "Successor # out of range for callbr!")(static_cast <bool> (i < getNumIndirectDests() + 1 && "Successor # out of range for callbr!") ? void (0) : __assert_fail ("i < getNumIndirectDests() + 1 && \"Successor # out of range for callbr!\"" , "llvm/include/llvm/IR/Instructions.h", 4235, __extension__ __PRETTY_FUNCTION__ )); |
4236 | return i == 0 ? setDefaultDest(NewSucc) : setIndirectDest(i - 1, NewSucc); |
4237 | } |
4238 | |
4239 | unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; } |
4240 | |
4241 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4242 | static bool classof(const Instruction *I) { |
4243 | return (I->getOpcode() == Instruction::CallBr); |
4244 | } |
4245 | static bool classof(const Value *V) { |
4246 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4247 | } |
4248 | |
4249 | private: |
4250 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
4251 | // method so that subclasses cannot accidentally use it. |
4252 | template <typename Bitfield> |
4253 | void setSubclassData(typename Bitfield::Type Value) { |
4254 | Instruction::setSubclassData<Bitfield>(Value); |
4255 | } |
4256 | }; |
4257 | |
4258 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
4259 | ArrayRef<BasicBlock *> IndirectDests, |
4260 | ArrayRef<Value *> Args, |
4261 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
4262 | const Twine &NameStr, Instruction *InsertBefore) |
4263 | : CallBase(Ty->getReturnType(), Instruction::CallBr, |
4264 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
4265 | InsertBefore) { |
4266 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); |
4267 | } |
4268 | |
4269 | CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, |
4270 | ArrayRef<BasicBlock *> IndirectDests, |
4271 | ArrayRef<Value *> Args, |
4272 | ArrayRef<OperandBundleDef> Bundles, int NumOperands, |
4273 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
4274 | : CallBase(Ty->getReturnType(), Instruction::CallBr, |
4275 | OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands, |
4276 | InsertAtEnd) { |
4277 | init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr); |
4278 | } |
4279 | |
4280 | //===----------------------------------------------------------------------===// |
4281 | // ResumeInst Class |
4282 | //===----------------------------------------------------------------------===// |
4283 | |
4284 | //===--------------------------------------------------------------------------- |
4285 | /// Resume the propagation of an exception. |
4286 | /// |
4287 | class ResumeInst : public Instruction { |
4288 | ResumeInst(const ResumeInst &RI); |
4289 | |
4290 | explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr); |
4291 | ResumeInst(Value *Exn, BasicBlock *InsertAtEnd); |
4292 | |
4293 | protected: |
4294 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4295 | friend class Instruction; |
4296 | |
4297 | ResumeInst *cloneImpl() const; |
4298 | |
4299 | public: |
4300 | static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) { |
4301 | return new(1) ResumeInst(Exn, InsertBefore); |
4302 | } |
4303 | |
4304 | static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) { |
4305 | return new(1) ResumeInst(Exn, InsertAtEnd); |
4306 | } |
4307 | |
4308 | /// Provide fast operand accessors |
4309 | 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; |
4310 | |
4311 | /// Convenience accessor. |
4312 | Value *getValue() const { return Op<0>(); } |
4313 | |
4314 | unsigned getNumSuccessors() const { return 0; } |
4315 | |
4316 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4317 | static bool classof(const Instruction *I) { |
4318 | return I->getOpcode() == Instruction::Resume; |
4319 | } |
4320 | static bool classof(const Value *V) { |
4321 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4322 | } |
4323 | |
4324 | private: |
4325 | BasicBlock *getSuccessor(unsigned idx) const { |
4326 | llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!" , "llvm/include/llvm/IR/Instructions.h", 4326); |
4327 | } |
4328 | |
4329 | void setSuccessor(unsigned idx, BasicBlock *NewSucc) { |
4330 | llvm_unreachable("ResumeInst has no successors!")::llvm::llvm_unreachable_internal("ResumeInst has no successors!" , "llvm/include/llvm/IR/Instructions.h", 4330); |
4331 | } |
4332 | }; |
4333 | |
4334 | template <> |
4335 | struct OperandTraits<ResumeInst> : |
4336 | public FixedNumOperandTraits<ResumeInst, 1> { |
4337 | }; |
4338 | |
4339 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<ResumeInst>::operands (this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ResumeInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 4339, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<ResumeInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<ResumeInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 4339, __extension__ __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); } |
4340 | |
4341 | //===----------------------------------------------------------------------===// |
4342 | // CatchSwitchInst Class |
4343 | //===----------------------------------------------------------------------===// |
4344 | class CatchSwitchInst : public Instruction { |
4345 | using UnwindDestField = BoolBitfieldElementT<0>; |
4346 | |
4347 | /// The number of operands actually allocated. NumOperands is |
4348 | /// the number actually in use. |
4349 | unsigned ReservedSpace; |
4350 | |
4351 | // Operand[0] = Outer scope |
4352 | // Operand[1] = Unwind block destination |
4353 | // Operand[n] = BasicBlock to go to on match |
4354 | CatchSwitchInst(const CatchSwitchInst &CSI); |
4355 | |
4356 | /// Create a new switch instruction, specifying a |
4357 | /// default destination. The number of additional handlers can be specified |
4358 | /// here to make memory allocation more efficient. |
4359 | /// This constructor can also autoinsert before another instruction. |
4360 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
4361 | unsigned NumHandlers, const Twine &NameStr, |
4362 | Instruction *InsertBefore); |
4363 | |
4364 | /// Create a new switch instruction, specifying a |
4365 | /// default destination. The number of additional handlers can be specified |
4366 | /// here to make memory allocation more efficient. |
4367 | /// This constructor also autoinserts at the end of the specified BasicBlock. |
4368 | CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest, |
4369 | unsigned NumHandlers, const Twine &NameStr, |
4370 | BasicBlock *InsertAtEnd); |
4371 | |
4372 | // allocate space for exactly zero operands |
4373 | void *operator new(size_t S) { return User::operator new(S); } |
4374 | |
4375 | void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved); |
4376 | void growOperands(unsigned Size); |
4377 | |
4378 | protected: |
4379 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4380 | friend class Instruction; |
4381 | |
4382 | CatchSwitchInst *cloneImpl() const; |
4383 | |
4384 | public: |
4385 | void operator delete(void *Ptr) { return User::operator delete(Ptr); } |
4386 | |
4387 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, |
4388 | unsigned NumHandlers, |
4389 | const Twine &NameStr = "", |
4390 | Instruction *InsertBefore = nullptr) { |
4391 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, |
4392 | InsertBefore); |
4393 | } |
4394 | |
4395 | static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest, |
4396 | unsigned NumHandlers, const Twine &NameStr, |
4397 | BasicBlock *InsertAtEnd) { |
4398 | return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr, |
4399 | InsertAtEnd); |
4400 | } |
4401 | |
4402 | /// Provide fast operand accessors |
4403 | 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; |
4404 | |
4405 | // Accessor Methods for CatchSwitch stmt |
4406 | Value *getParentPad() const { return getOperand(0); } |
4407 | void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); } |
4408 | |
4409 | // Accessor Methods for CatchSwitch stmt |
4410 | bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); } |
4411 | bool unwindsToCaller() const { return !hasUnwindDest(); } |
4412 | BasicBlock *getUnwindDest() const { |
4413 | if (hasUnwindDest()) |
4414 | return cast<BasicBlock>(getOperand(1)); |
4415 | return nullptr; |
4416 | } |
4417 | void setUnwindDest(BasicBlock *UnwindDest) { |
4418 | assert(UnwindDest)(static_cast <bool> (UnwindDest) ? void (0) : __assert_fail ("UnwindDest", "llvm/include/llvm/IR/Instructions.h", 4418, __extension__ __PRETTY_FUNCTION__)); |
4419 | assert(hasUnwindDest())(static_cast <bool> (hasUnwindDest()) ? void (0) : __assert_fail ("hasUnwindDest()", "llvm/include/llvm/IR/Instructions.h", 4419 , __extension__ __PRETTY_FUNCTION__)); |
4420 | setOperand(1, UnwindDest); |
4421 | } |
4422 | |
4423 | /// return the number of 'handlers' in this catchswitch |
4424 | /// instruction, except the default handler |
4425 | unsigned getNumHandlers() const { |
4426 | if (hasUnwindDest()) |
4427 | return getNumOperands() - 2; |
4428 | return getNumOperands() - 1; |
4429 | } |
4430 | |
4431 | private: |
4432 | static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); } |
4433 | static const BasicBlock *handler_helper(const Value *V) { |
4434 | return cast<BasicBlock>(V); |
4435 | } |
4436 | |
4437 | public: |
4438 | using DerefFnTy = BasicBlock *(*)(Value *); |
4439 | using handler_iterator = mapped_iterator<op_iterator, DerefFnTy>; |
4440 | using handler_range = iterator_range<handler_iterator>; |
4441 | using ConstDerefFnTy = const BasicBlock *(*)(const Value *); |
4442 | using const_handler_iterator = |
4443 | mapped_iterator<const_op_iterator, ConstDerefFnTy>; |
4444 | using const_handler_range = iterator_range<const_handler_iterator>; |
4445 | |
4446 | /// Returns an iterator that points to the first handler in CatchSwitchInst. |
4447 | handler_iterator handler_begin() { |
4448 | op_iterator It = op_begin() + 1; |
4449 | if (hasUnwindDest()) |
4450 | ++It; |
4451 | return handler_iterator(It, DerefFnTy(handler_helper)); |
4452 | } |
4453 | |
4454 | /// Returns an iterator that points to the first handler in the |
4455 | /// CatchSwitchInst. |
4456 | const_handler_iterator handler_begin() const { |
4457 | const_op_iterator It = op_begin() + 1; |
4458 | if (hasUnwindDest()) |
4459 | ++It; |
4460 | return const_handler_iterator(It, ConstDerefFnTy(handler_helper)); |
4461 | } |
4462 | |
4463 | /// Returns a read-only iterator that points one past the last |
4464 | /// handler in the CatchSwitchInst. |
4465 | handler_iterator handler_end() { |
4466 | return handler_iterator(op_end(), DerefFnTy(handler_helper)); |
4467 | } |
4468 | |
4469 | /// Returns an iterator that points one past the last handler in the |
4470 | /// CatchSwitchInst. |
4471 | const_handler_iterator handler_end() const { |
4472 | return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper)); |
4473 | } |
4474 | |
4475 | /// iteration adapter for range-for loops. |
4476 | handler_range handlers() { |
4477 | return make_range(handler_begin(), handler_end()); |
4478 | } |
4479 | |
4480 | /// iteration adapter for range-for loops. |
4481 | const_handler_range handlers() const { |
4482 | return make_range(handler_begin(), handler_end()); |
4483 | } |
4484 | |
4485 | /// Add an entry to the switch instruction... |
4486 | /// Note: |
4487 | /// This action invalidates handler_end(). Old handler_end() iterator will |
4488 | /// point to the added handler. |
4489 | void addHandler(BasicBlock *Dest); |
4490 | |
4491 | void removeHandler(handler_iterator HI); |
4492 | |
4493 | unsigned getNumSuccessors() const { return getNumOperands() - 1; } |
4494 | BasicBlock *getSuccessor(unsigned Idx) const { |
4495 | assert(Idx < getNumSuccessors() &&(static_cast <bool> (Idx < getNumSuccessors() && "Successor # out of range for catchswitch!") ? void (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "llvm/include/llvm/IR/Instructions.h", 4496, __extension__ __PRETTY_FUNCTION__ )) |
4496 | "Successor # out of range for catchswitch!")(static_cast <bool> (Idx < getNumSuccessors() && "Successor # out of range for catchswitch!") ? void (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "llvm/include/llvm/IR/Instructions.h", 4496, __extension__ __PRETTY_FUNCTION__ )); |
4497 | return cast<BasicBlock>(getOperand(Idx + 1)); |
4498 | } |
4499 | void setSuccessor(unsigned Idx, BasicBlock *NewSucc) { |
4500 | assert(Idx < getNumSuccessors() &&(static_cast <bool> (Idx < getNumSuccessors() && "Successor # out of range for catchswitch!") ? void (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "llvm/include/llvm/IR/Instructions.h", 4501, __extension__ __PRETTY_FUNCTION__ )) |
4501 | "Successor # out of range for catchswitch!")(static_cast <bool> (Idx < getNumSuccessors() && "Successor # out of range for catchswitch!") ? void (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchswitch!\"" , "llvm/include/llvm/IR/Instructions.h", 4501, __extension__ __PRETTY_FUNCTION__ )); |
4502 | setOperand(Idx + 1, NewSucc); |
4503 | } |
4504 | |
4505 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4506 | static bool classof(const Instruction *I) { |
4507 | return I->getOpcode() == Instruction::CatchSwitch; |
4508 | } |
4509 | static bool classof(const Value *V) { |
4510 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4511 | } |
4512 | }; |
4513 | |
4514 | template <> |
4515 | struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {}; |
4516 | |
4517 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<CatchSwitchInst>:: operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 4517, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CatchSwitchInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 4517, __extension__ __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 ); } |
4518 | |
4519 | //===----------------------------------------------------------------------===// |
4520 | // CleanupPadInst Class |
4521 | //===----------------------------------------------------------------------===// |
4522 | class CleanupPadInst : public FuncletPadInst { |
4523 | private: |
4524 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, |
4525 | unsigned Values, const Twine &NameStr, |
4526 | Instruction *InsertBefore) |
4527 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, |
4528 | NameStr, InsertBefore) {} |
4529 | explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args, |
4530 | unsigned Values, const Twine &NameStr, |
4531 | BasicBlock *InsertAtEnd) |
4532 | : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values, |
4533 | NameStr, InsertAtEnd) {} |
4534 | |
4535 | public: |
4536 | static CleanupPadInst *Create(Value *ParentPad, |
4537 | ArrayRef<Value *> Args = std::nullopt, |
4538 | const Twine &NameStr = "", |
4539 | Instruction *InsertBefore = nullptr) { |
4540 | unsigned Values = 1 + Args.size(); |
4541 | return new (Values) |
4542 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore); |
4543 | } |
4544 | |
4545 | static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args, |
4546 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
4547 | unsigned Values = 1 + Args.size(); |
4548 | return new (Values) |
4549 | CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd); |
4550 | } |
4551 | |
4552 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4553 | static bool classof(const Instruction *I) { |
4554 | return I->getOpcode() == Instruction::CleanupPad; |
4555 | } |
4556 | static bool classof(const Value *V) { |
4557 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4558 | } |
4559 | }; |
4560 | |
4561 | //===----------------------------------------------------------------------===// |
4562 | // CatchPadInst Class |
4563 | //===----------------------------------------------------------------------===// |
4564 | class CatchPadInst : public FuncletPadInst { |
4565 | private: |
4566 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, |
4567 | unsigned Values, const Twine &NameStr, |
4568 | Instruction *InsertBefore) |
4569 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, |
4570 | NameStr, InsertBefore) {} |
4571 | explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args, |
4572 | unsigned Values, const Twine &NameStr, |
4573 | BasicBlock *InsertAtEnd) |
4574 | : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values, |
4575 | NameStr, InsertAtEnd) {} |
4576 | |
4577 | public: |
4578 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, |
4579 | const Twine &NameStr = "", |
4580 | Instruction *InsertBefore = nullptr) { |
4581 | unsigned Values = 1 + Args.size(); |
4582 | return new (Values) |
4583 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore); |
4584 | } |
4585 | |
4586 | static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args, |
4587 | const Twine &NameStr, BasicBlock *InsertAtEnd) { |
4588 | unsigned Values = 1 + Args.size(); |
4589 | return new (Values) |
4590 | CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd); |
4591 | } |
4592 | |
4593 | /// Convenience accessors |
4594 | CatchSwitchInst *getCatchSwitch() const { |
4595 | return cast<CatchSwitchInst>(Op<-1>()); |
4596 | } |
4597 | void setCatchSwitch(Value *CatchSwitch) { |
4598 | assert(CatchSwitch)(static_cast <bool> (CatchSwitch) ? void (0) : __assert_fail ("CatchSwitch", "llvm/include/llvm/IR/Instructions.h", 4598, __extension__ __PRETTY_FUNCTION__)); |
4599 | Op<-1>() = CatchSwitch; |
4600 | } |
4601 | |
4602 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4603 | static bool classof(const Instruction *I) { |
4604 | return I->getOpcode() == Instruction::CatchPad; |
4605 | } |
4606 | static bool classof(const Value *V) { |
4607 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4608 | } |
4609 | }; |
4610 | |
4611 | //===----------------------------------------------------------------------===// |
4612 | // CatchReturnInst Class |
4613 | //===----------------------------------------------------------------------===// |
4614 | |
4615 | class CatchReturnInst : public Instruction { |
4616 | CatchReturnInst(const CatchReturnInst &RI); |
4617 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore); |
4618 | CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd); |
4619 | |
4620 | void init(Value *CatchPad, BasicBlock *BB); |
4621 | |
4622 | protected: |
4623 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4624 | friend class Instruction; |
4625 | |
4626 | CatchReturnInst *cloneImpl() const; |
4627 | |
4628 | public: |
4629 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, |
4630 | Instruction *InsertBefore = nullptr) { |
4631 | assert(CatchPad)(static_cast <bool> (CatchPad) ? void (0) : __assert_fail ("CatchPad", "llvm/include/llvm/IR/Instructions.h", 4631, __extension__ __PRETTY_FUNCTION__)); |
4632 | assert(BB)(static_cast <bool> (BB) ? void (0) : __assert_fail ("BB" , "llvm/include/llvm/IR/Instructions.h", 4632, __extension__ __PRETTY_FUNCTION__ )); |
4633 | return new (2) CatchReturnInst(CatchPad, BB, InsertBefore); |
4634 | } |
4635 | |
4636 | static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB, |
4637 | BasicBlock *InsertAtEnd) { |
4638 | assert(CatchPad)(static_cast <bool> (CatchPad) ? void (0) : __assert_fail ("CatchPad", "llvm/include/llvm/IR/Instructions.h", 4638, __extension__ __PRETTY_FUNCTION__)); |
4639 | assert(BB)(static_cast <bool> (BB) ? void (0) : __assert_fail ("BB" , "llvm/include/llvm/IR/Instructions.h", 4639, __extension__ __PRETTY_FUNCTION__ )); |
4640 | return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd); |
4641 | } |
4642 | |
4643 | /// Provide fast operand accessors |
4644 | 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; |
4645 | |
4646 | /// Convenience accessors. |
4647 | CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); } |
4648 | void setCatchPad(CatchPadInst *CatchPad) { |
4649 | assert(CatchPad)(static_cast <bool> (CatchPad) ? void (0) : __assert_fail ("CatchPad", "llvm/include/llvm/IR/Instructions.h", 4649, __extension__ __PRETTY_FUNCTION__)); |
4650 | Op<0>() = CatchPad; |
4651 | } |
4652 | |
4653 | BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); } |
4654 | void setSuccessor(BasicBlock *NewSucc) { |
4655 | assert(NewSucc)(static_cast <bool> (NewSucc) ? void (0) : __assert_fail ("NewSucc", "llvm/include/llvm/IR/Instructions.h", 4655, __extension__ __PRETTY_FUNCTION__)); |
4656 | Op<1>() = NewSucc; |
4657 | } |
4658 | unsigned getNumSuccessors() const { return 1; } |
4659 | |
4660 | /// Get the parentPad of this catchret's catchpad's catchswitch. |
4661 | /// The successor block is implicitly a member of this funclet. |
4662 | Value *getCatchSwitchParentPad() const { |
4663 | return getCatchPad()->getCatchSwitch()->getParentPad(); |
4664 | } |
4665 | |
4666 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4667 | static bool classof(const Instruction *I) { |
4668 | return (I->getOpcode() == Instruction::CatchRet); |
4669 | } |
4670 | static bool classof(const Value *V) { |
4671 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4672 | } |
4673 | |
4674 | private: |
4675 | BasicBlock *getSuccessor(unsigned Idx) const { |
4676 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")(static_cast <bool> (Idx < getNumSuccessors() && "Successor # out of range for catchret!") ? void (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchret!\"" , "llvm/include/llvm/IR/Instructions.h", 4676, __extension__ __PRETTY_FUNCTION__ )); |
4677 | return getSuccessor(); |
4678 | } |
4679 | |
4680 | void setSuccessor(unsigned Idx, BasicBlock *B) { |
4681 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!")(static_cast <bool> (Idx < getNumSuccessors() && "Successor # out of range for catchret!") ? void (0) : __assert_fail ("Idx < getNumSuccessors() && \"Successor # out of range for catchret!\"" , "llvm/include/llvm/IR/Instructions.h", 4681, __extension__ __PRETTY_FUNCTION__ )); |
4682 | setSuccessor(B); |
4683 | } |
4684 | }; |
4685 | |
4686 | template <> |
4687 | struct OperandTraits<CatchReturnInst> |
4688 | : public FixedNumOperandTraits<CatchReturnInst, 2> {}; |
4689 | |
4690 | 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 { (static_cast <bool > (i_nocapture < OperandTraits<CatchReturnInst>:: operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 4690, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CatchReturnInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 4690, __extension__ __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 ); } |
4691 | |
4692 | //===----------------------------------------------------------------------===// |
4693 | // CleanupReturnInst Class |
4694 | //===----------------------------------------------------------------------===// |
4695 | |
4696 | class CleanupReturnInst : public Instruction { |
4697 | using UnwindDestField = BoolBitfieldElementT<0>; |
4698 | |
4699 | private: |
4700 | CleanupReturnInst(const CleanupReturnInst &RI); |
4701 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, |
4702 | Instruction *InsertBefore = nullptr); |
4703 | CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values, |
4704 | BasicBlock *InsertAtEnd); |
4705 | |
4706 | void init(Value *CleanupPad, BasicBlock *UnwindBB); |
4707 | |
4708 | protected: |
4709 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4710 | friend class Instruction; |
4711 | |
4712 | CleanupReturnInst *cloneImpl() const; |
4713 | |
4714 | public: |
4715 | static CleanupReturnInst *Create(Value *CleanupPad, |
4716 | BasicBlock *UnwindBB = nullptr, |
4717 | Instruction *InsertBefore = nullptr) { |
4718 | assert(CleanupPad)(static_cast <bool> (CleanupPad) ? void (0) : __assert_fail ("CleanupPad", "llvm/include/llvm/IR/Instructions.h", 4718, __extension__ __PRETTY_FUNCTION__)); |
4719 | unsigned Values = 1; |
4720 | if (UnwindBB) |
4721 | ++Values; |
4722 | return new (Values) |
4723 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore); |
4724 | } |
4725 | |
4726 | static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB, |
4727 | BasicBlock *InsertAtEnd) { |
4728 | assert(CleanupPad)(static_cast <bool> (CleanupPad) ? void (0) : __assert_fail ("CleanupPad", "llvm/include/llvm/IR/Instructions.h", 4728, __extension__ __PRETTY_FUNCTION__)); |
4729 | unsigned Values = 1; |
4730 | if (UnwindBB) |
4731 | ++Values; |
4732 | return new (Values) |
4733 | CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd); |
4734 | } |
4735 | |
4736 | /// Provide fast operand accessors |
4737 | 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; |
4738 | |
4739 | bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); } |
4740 | bool unwindsToCaller() const { return !hasUnwindDest(); } |
4741 | |
4742 | /// Convenience accessor. |
4743 | CleanupPadInst *getCleanupPad() const { |
4744 | return cast<CleanupPadInst>(Op<0>()); |
4745 | } |
4746 | void setCleanupPad(CleanupPadInst *CleanupPad) { |
4747 | assert(CleanupPad)(static_cast <bool> (CleanupPad) ? void (0) : __assert_fail ("CleanupPad", "llvm/include/llvm/IR/Instructions.h", 4747, __extension__ __PRETTY_FUNCTION__)); |
4748 | Op<0>() = CleanupPad; |
4749 | } |
4750 | |
4751 | unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; } |
4752 | |
4753 | BasicBlock *getUnwindDest() const { |
4754 | return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr; |
4755 | } |
4756 | void setUnwindDest(BasicBlock *NewDest) { |
4757 | assert(NewDest)(static_cast <bool> (NewDest) ? void (0) : __assert_fail ("NewDest", "llvm/include/llvm/IR/Instructions.h", 4757, __extension__ __PRETTY_FUNCTION__)); |
4758 | assert(hasUnwindDest())(static_cast <bool> (hasUnwindDest()) ? void (0) : __assert_fail ("hasUnwindDest()", "llvm/include/llvm/IR/Instructions.h", 4758 , __extension__ __PRETTY_FUNCTION__)); |
4759 | Op<1>() = NewDest; |
4760 | } |
4761 | |
4762 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4763 | static bool classof(const Instruction *I) { |
4764 | return (I->getOpcode() == Instruction::CleanupRet); |
4765 | } |
4766 | static bool classof(const Value *V) { |
4767 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4768 | } |
4769 | |
4770 | private: |
4771 | BasicBlock *getSuccessor(unsigned Idx) const { |
4772 | assert(Idx == 0)(static_cast <bool> (Idx == 0) ? void (0) : __assert_fail ("Idx == 0", "llvm/include/llvm/IR/Instructions.h", 4772, __extension__ __PRETTY_FUNCTION__)); |
4773 | return getUnwindDest(); |
4774 | } |
4775 | |
4776 | void setSuccessor(unsigned Idx, BasicBlock *B) { |
4777 | assert(Idx == 0)(static_cast <bool> (Idx == 0) ? void (0) : __assert_fail ("Idx == 0", "llvm/include/llvm/IR/Instructions.h", 4777, __extension__ __PRETTY_FUNCTION__)); |
4778 | setUnwindDest(B); |
4779 | } |
4780 | |
4781 | // Shadow Instruction::setInstructionSubclassData with a private forwarding |
4782 | // method so that subclasses cannot accidentally use it. |
4783 | template <typename Bitfield> |
4784 | void setSubclassData(typename Bitfield::Type Value) { |
4785 | Instruction::setSubclassData<Bitfield>(Value); |
4786 | } |
4787 | }; |
4788 | |
4789 | template <> |
4790 | struct OperandTraits<CleanupReturnInst> |
4791 | : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {}; |
4792 | |
4793 | 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 { (static_cast <bool> (i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && "getOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && \"getOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 4793, __extension__ __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) { (static_cast <bool> (i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && "setOperand() out of range!") ? void (0) : __assert_fail ("i_nocapture < OperandTraits<CleanupReturnInst>::operands(this) && \"setOperand() out of range!\"" , "llvm/include/llvm/IR/Instructions.h", 4793, __extension__ __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 ); } |
4794 | |
4795 | //===----------------------------------------------------------------------===// |
4796 | // UnreachableInst Class |
4797 | //===----------------------------------------------------------------------===// |
4798 | |
4799 | //===--------------------------------------------------------------------------- |
4800 | /// This function has undefined behavior. In particular, the |
4801 | /// presence of this instruction indicates some higher level knowledge that the |
4802 | /// end of the block cannot be reached. |
4803 | /// |
4804 | class UnreachableInst : public Instruction { |
4805 | protected: |
4806 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4807 | friend class Instruction; |
4808 | |
4809 | UnreachableInst *cloneImpl() const; |
4810 | |
4811 | public: |
4812 | explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr); |
4813 | explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd); |
4814 | |
4815 | // allocate space for exactly zero operands |
4816 | void *operator new(size_t S) { return User::operator new(S, 0); } |
4817 | void operator delete(void *Ptr) { User::operator delete(Ptr); } |
4818 | |
4819 | unsigned getNumSuccessors() const { return 0; } |
4820 | |
4821 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
4822 | static bool classof(const Instruction *I) { |
4823 | return I->getOpcode() == Instruction::Unreachable; |
4824 | } |
4825 | static bool classof(const Value *V) { |
4826 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4827 | } |
4828 | |
4829 | private: |
4830 | BasicBlock *getSuccessor(unsigned idx) const { |
4831 | llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!" , "llvm/include/llvm/IR/Instructions.h", 4831); |
4832 | } |
4833 | |
4834 | void setSuccessor(unsigned idx, BasicBlock *B) { |
4835 | llvm_unreachable("UnreachableInst has no successors!")::llvm::llvm_unreachable_internal("UnreachableInst has no successors!" , "llvm/include/llvm/IR/Instructions.h", 4835); |
4836 | } |
4837 | }; |
4838 | |
4839 | //===----------------------------------------------------------------------===// |
4840 | // TruncInst Class |
4841 | //===----------------------------------------------------------------------===// |
4842 | |
4843 | /// This class represents a truncation of integer types. |
4844 | class TruncInst : public CastInst { |
4845 | protected: |
4846 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4847 | friend class Instruction; |
4848 | |
4849 | /// Clone an identical TruncInst |
4850 | TruncInst *cloneImpl() const; |
4851 | |
4852 | public: |
4853 | /// Constructor with insert-before-instruction semantics |
4854 | TruncInst( |
4855 | Value *S, ///< The value to be truncated |
4856 | Type *Ty, ///< The (smaller) type to truncate to |
4857 | const Twine &NameStr = "", ///< A name for the new instruction |
4858 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4859 | ); |
4860 | |
4861 | /// Constructor with insert-at-end-of-block semantics |
4862 | TruncInst( |
4863 | Value *S, ///< The value to be truncated |
4864 | Type *Ty, ///< The (smaller) type to truncate to |
4865 | const Twine &NameStr, ///< A name for the new instruction |
4866 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4867 | ); |
4868 | |
4869 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4870 | static bool classof(const Instruction *I) { |
4871 | return I->getOpcode() == Trunc; |
4872 | } |
4873 | static bool classof(const Value *V) { |
4874 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4875 | } |
4876 | }; |
4877 | |
4878 | //===----------------------------------------------------------------------===// |
4879 | // ZExtInst Class |
4880 | //===----------------------------------------------------------------------===// |
4881 | |
4882 | /// This class represents zero extension of integer types. |
4883 | class ZExtInst : public CastInst { |
4884 | protected: |
4885 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4886 | friend class Instruction; |
4887 | |
4888 | /// Clone an identical ZExtInst |
4889 | ZExtInst *cloneImpl() const; |
4890 | |
4891 | public: |
4892 | /// Constructor with insert-before-instruction semantics |
4893 | ZExtInst( |
4894 | Value *S, ///< The value to be zero extended |
4895 | Type *Ty, ///< The type to zero extend to |
4896 | const Twine &NameStr = "", ///< A name for the new instruction |
4897 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4898 | ); |
4899 | |
4900 | /// Constructor with insert-at-end semantics. |
4901 | ZExtInst( |
4902 | Value *S, ///< The value to be zero extended |
4903 | Type *Ty, ///< The type to zero extend to |
4904 | const Twine &NameStr, ///< A name for the new instruction |
4905 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4906 | ); |
4907 | |
4908 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4909 | static bool classof(const Instruction *I) { |
4910 | return I->getOpcode() == ZExt; |
4911 | } |
4912 | static bool classof(const Value *V) { |
4913 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4914 | } |
4915 | }; |
4916 | |
4917 | //===----------------------------------------------------------------------===// |
4918 | // SExtInst Class |
4919 | //===----------------------------------------------------------------------===// |
4920 | |
4921 | /// This class represents a sign extension of integer types. |
4922 | class SExtInst : public CastInst { |
4923 | protected: |
4924 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4925 | friend class Instruction; |
4926 | |
4927 | /// Clone an identical SExtInst |
4928 | SExtInst *cloneImpl() const; |
4929 | |
4930 | public: |
4931 | /// Constructor with insert-before-instruction semantics |
4932 | SExtInst( |
4933 | Value *S, ///< The value to be sign extended |
4934 | Type *Ty, ///< The type to sign extend to |
4935 | const Twine &NameStr = "", ///< A name for the new instruction |
4936 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4937 | ); |
4938 | |
4939 | /// Constructor with insert-at-end-of-block semantics |
4940 | SExtInst( |
4941 | Value *S, ///< The value to be sign extended |
4942 | Type *Ty, ///< The type to sign extend to |
4943 | const Twine &NameStr, ///< A name for the new instruction |
4944 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4945 | ); |
4946 | |
4947 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4948 | static bool classof(const Instruction *I) { |
4949 | return I->getOpcode() == SExt; |
4950 | } |
4951 | static bool classof(const Value *V) { |
4952 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4953 | } |
4954 | }; |
4955 | |
4956 | //===----------------------------------------------------------------------===// |
4957 | // FPTruncInst Class |
4958 | //===----------------------------------------------------------------------===// |
4959 | |
4960 | /// This class represents a truncation of floating point types. |
4961 | class FPTruncInst : public CastInst { |
4962 | protected: |
4963 | // Note: Instruction needs to be a friend here to call cloneImpl. |
4964 | friend class Instruction; |
4965 | |
4966 | /// Clone an identical FPTruncInst |
4967 | FPTruncInst *cloneImpl() const; |
4968 | |
4969 | public: |
4970 | /// Constructor with insert-before-instruction semantics |
4971 | FPTruncInst( |
4972 | Value *S, ///< The value to be truncated |
4973 | Type *Ty, ///< The type to truncate to |
4974 | const Twine &NameStr = "", ///< A name for the new instruction |
4975 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
4976 | ); |
4977 | |
4978 | /// Constructor with insert-before-instruction semantics |
4979 | FPTruncInst( |
4980 | Value *S, ///< The value to be truncated |
4981 | Type *Ty, ///< The type to truncate to |
4982 | const Twine &NameStr, ///< A name for the new instruction |
4983 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
4984 | ); |
4985 | |
4986 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
4987 | static bool classof(const Instruction *I) { |
4988 | return I->getOpcode() == FPTrunc; |
4989 | } |
4990 | static bool classof(const Value *V) { |
4991 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
4992 | } |
4993 | }; |
4994 | |
4995 | //===----------------------------------------------------------------------===// |
4996 | // FPExtInst Class |
4997 | //===----------------------------------------------------------------------===// |
4998 | |
4999 | /// This class represents an extension of floating point types. |
5000 | class FPExtInst : public CastInst { |
5001 | protected: |
5002 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5003 | friend class Instruction; |
5004 | |
5005 | /// Clone an identical FPExtInst |
5006 | FPExtInst *cloneImpl() const; |
5007 | |
5008 | public: |
5009 | /// Constructor with insert-before-instruction semantics |
5010 | FPExtInst( |
5011 | Value *S, ///< The value to be extended |
5012 | Type *Ty, ///< The type to extend to |
5013 | const Twine &NameStr = "", ///< A name for the new instruction |
5014 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5015 | ); |
5016 | |
5017 | /// Constructor with insert-at-end-of-block semantics |
5018 | FPExtInst( |
5019 | Value *S, ///< The value to be extended |
5020 | Type *Ty, ///< The type to extend to |
5021 | const Twine &NameStr, ///< A name for the new instruction |
5022 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5023 | ); |
5024 | |
5025 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
5026 | static bool classof(const Instruction *I) { |
5027 | return I->getOpcode() == FPExt; |
5028 | } |
5029 | static bool classof(const Value *V) { |
5030 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5031 | } |
5032 | }; |
5033 | |
5034 | //===----------------------------------------------------------------------===// |
5035 | // UIToFPInst Class |
5036 | //===----------------------------------------------------------------------===// |
5037 | |
5038 | /// This class represents a cast unsigned integer to floating point. |
5039 | class UIToFPInst : public CastInst { |
5040 | protected: |
5041 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5042 | friend class Instruction; |
5043 | |
5044 | /// Clone an identical UIToFPInst |
5045 | UIToFPInst *cloneImpl() const; |
5046 | |
5047 | public: |
5048 | /// Constructor with insert-before-instruction semantics |
5049 | UIToFPInst( |
5050 | Value *S, ///< The value to be converted |
5051 | Type *Ty, ///< The type to convert to |
5052 | const Twine &NameStr = "", ///< A name for the new instruction |
5053 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5054 | ); |
5055 | |
5056 | /// Constructor with insert-at-end-of-block semantics |
5057 | UIToFPInst( |
5058 | Value *S, ///< The value to be converted |
5059 | Type *Ty, ///< The type to convert to |
5060 | const Twine &NameStr, ///< A name for the new instruction |
5061 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5062 | ); |
5063 | |
5064 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
5065 | static bool classof(const Instruction *I) { |
5066 | return I->getOpcode() == UIToFP; |
5067 | } |
5068 | static bool classof(const Value *V) { |
5069 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5070 | } |
5071 | }; |
5072 | |
5073 | //===----------------------------------------------------------------------===// |
5074 | // SIToFPInst Class |
5075 | //===----------------------------------------------------------------------===// |
5076 | |
5077 | /// This class represents a cast from signed integer to floating point. |
5078 | class SIToFPInst : public CastInst { |
5079 | protected: |
5080 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5081 | friend class Instruction; |
5082 | |
5083 | /// Clone an identical SIToFPInst |
5084 | SIToFPInst *cloneImpl() const; |
5085 | |
5086 | public: |
5087 | /// Constructor with insert-before-instruction semantics |
5088 | SIToFPInst( |
5089 | Value *S, ///< The value to be converted |
5090 | Type *Ty, ///< The type to convert to |
5091 | const Twine &NameStr = "", ///< A name for the new instruction |
5092 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5093 | ); |
5094 | |
5095 | /// Constructor with insert-at-end-of-block semantics |
5096 | SIToFPInst( |
5097 | Value *S, ///< The value to be converted |
5098 | Type *Ty, ///< The type to convert to |
5099 | const Twine &NameStr, ///< A name for the new instruction |
5100 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5101 | ); |
5102 | |
5103 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
5104 | static bool classof(const Instruction *I) { |
5105 | return I->getOpcode() == SIToFP; |
5106 | } |
5107 | static bool classof(const Value *V) { |
5108 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5109 | } |
5110 | }; |
5111 | |
5112 | //===----------------------------------------------------------------------===// |
5113 | // FPToUIInst Class |
5114 | //===----------------------------------------------------------------------===// |
5115 | |
5116 | /// This class represents a cast from floating point to unsigned integer |
5117 | class FPToUIInst : public CastInst { |
5118 | protected: |
5119 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5120 | friend class Instruction; |
5121 | |
5122 | /// Clone an identical FPToUIInst |
5123 | FPToUIInst *cloneImpl() const; |
5124 | |
5125 | public: |
5126 | /// Constructor with insert-before-instruction semantics |
5127 | FPToUIInst( |
5128 | Value *S, ///< The value to be converted |
5129 | Type *Ty, ///< The type to convert to |
5130 | const Twine &NameStr = "", ///< A name for the new instruction |
5131 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5132 | ); |
5133 | |
5134 | /// Constructor with insert-at-end-of-block semantics |
5135 | FPToUIInst( |
5136 | Value *S, ///< The value to be converted |
5137 | Type *Ty, ///< The type to convert to |
5138 | const Twine &NameStr, ///< A name for the new instruction |
5139 | BasicBlock *InsertAtEnd ///< Where to insert the new instruction |
5140 | ); |
5141 | |
5142 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
5143 | static bool classof(const Instruction *I) { |
5144 | return I->getOpcode() == FPToUI; |
5145 | } |
5146 | static bool classof(const Value *V) { |
5147 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5148 | } |
5149 | }; |
5150 | |
5151 | //===----------------------------------------------------------------------===// |
5152 | // FPToSIInst Class |
5153 | //===----------------------------------------------------------------------===// |
5154 | |
5155 | /// This class represents a cast from floating point to signed integer. |
5156 | class FPToSIInst : public CastInst { |
5157 | protected: |
5158 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5159 | friend class Instruction; |
5160 | |
5161 | /// Clone an identical FPToSIInst |
5162 | FPToSIInst *cloneImpl() const; |
5163 | |
5164 | public: |
5165 | /// Constructor with insert-before-instruction semantics |
5166 | FPToSIInst( |
5167 | Value *S, ///< The value to be converted |
5168 | Type *Ty, ///< The type to convert to |
5169 | const Twine &NameStr = "", ///< A name for the new instruction |
5170 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5171 | ); |
5172 | |
5173 | /// Constructor with insert-at-end-of-block semantics |
5174 | FPToSIInst( |
5175 | Value *S, ///< The value to be converted |
5176 | Type *Ty, ///< The type to convert to |
5177 | const Twine &NameStr, ///< A name for the new instruction |
5178 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5179 | ); |
5180 | |
5181 | /// Methods for support type inquiry through isa, cast, and dyn_cast: |
5182 | static bool classof(const Instruction *I) { |
5183 | return I->getOpcode() == FPToSI; |
5184 | } |
5185 | static bool classof(const Value *V) { |
5186 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5187 | } |
5188 | }; |
5189 | |
5190 | //===----------------------------------------------------------------------===// |
5191 | // IntToPtrInst Class |
5192 | //===----------------------------------------------------------------------===// |
5193 | |
5194 | /// This class represents a cast from an integer to a pointer. |
5195 | class IntToPtrInst : public CastInst { |
5196 | public: |
5197 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5198 | friend class Instruction; |
5199 | |
5200 | /// Constructor with insert-before-instruction semantics |
5201 | IntToPtrInst( |
5202 | Value *S, ///< The value to be converted |
5203 | Type *Ty, ///< The type to convert to |
5204 | const Twine &NameStr = "", ///< A name for the new instruction |
5205 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5206 | ); |
5207 | |
5208 | /// Constructor with insert-at-end-of-block semantics |
5209 | IntToPtrInst( |
5210 | Value *S, ///< The value to be converted |
5211 | Type *Ty, ///< The type to convert to |
5212 | const Twine &NameStr, ///< A name for the new instruction |
5213 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5214 | ); |
5215 | |
5216 | /// Clone an identical IntToPtrInst. |
5217 | IntToPtrInst *cloneImpl() const; |
5218 | |
5219 | /// Returns the address space of this instruction's pointer type. |
5220 | unsigned getAddressSpace() const { |
5221 | return getType()->getPointerAddressSpace(); |
5222 | } |
5223 | |
5224 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5225 | static bool classof(const Instruction *I) { |
5226 | return I->getOpcode() == IntToPtr; |
5227 | } |
5228 | static bool classof(const Value *V) { |
5229 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5230 | } |
5231 | }; |
5232 | |
5233 | //===----------------------------------------------------------------------===// |
5234 | // PtrToIntInst Class |
5235 | //===----------------------------------------------------------------------===// |
5236 | |
5237 | /// This class represents a cast from a pointer to an integer. |
5238 | class PtrToIntInst : public CastInst { |
5239 | protected: |
5240 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5241 | friend class Instruction; |
5242 | |
5243 | /// Clone an identical PtrToIntInst. |
5244 | PtrToIntInst *cloneImpl() const; |
5245 | |
5246 | public: |
5247 | /// Constructor with insert-before-instruction semantics |
5248 | PtrToIntInst( |
5249 | Value *S, ///< The value to be converted |
5250 | Type *Ty, ///< The type to convert to |
5251 | const Twine &NameStr = "", ///< A name for the new instruction |
5252 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5253 | ); |
5254 | |
5255 | /// Constructor with insert-at-end-of-block semantics |
5256 | PtrToIntInst( |
5257 | Value *S, ///< The value to be converted |
5258 | Type *Ty, ///< The type to convert to |
5259 | const Twine &NameStr, ///< A name for the new instruction |
5260 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5261 | ); |
5262 | |
5263 | /// Gets the pointer operand. |
5264 | Value *getPointerOperand() { return getOperand(0); } |
5265 | /// Gets the pointer operand. |
5266 | const Value *getPointerOperand() const { return getOperand(0); } |
5267 | /// Gets the operand index of the pointer operand. |
5268 | static unsigned getPointerOperandIndex() { return 0U; } |
5269 | |
5270 | /// Returns the address space of the pointer operand. |
5271 | unsigned getPointerAddressSpace() const { |
5272 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
5273 | } |
5274 | |
5275 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5276 | static bool classof(const Instruction *I) { |
5277 | return I->getOpcode() == PtrToInt; |
5278 | } |
5279 | static bool classof(const Value *V) { |
5280 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5281 | } |
5282 | }; |
5283 | |
5284 | //===----------------------------------------------------------------------===// |
5285 | // BitCastInst Class |
5286 | //===----------------------------------------------------------------------===// |
5287 | |
5288 | /// This class represents a no-op cast from one type to another. |
5289 | class BitCastInst : public CastInst { |
5290 | protected: |
5291 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5292 | friend class Instruction; |
5293 | |
5294 | /// Clone an identical BitCastInst. |
5295 | BitCastInst *cloneImpl() const; |
5296 | |
5297 | public: |
5298 | /// Constructor with insert-before-instruction semantics |
5299 | BitCastInst( |
5300 | Value *S, ///< The value to be casted |
5301 | Type *Ty, ///< The type to casted to |
5302 | const Twine &NameStr = "", ///< A name for the new instruction |
5303 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5304 | ); |
5305 | |
5306 | /// Constructor with insert-at-end-of-block semantics |
5307 | BitCastInst( |
5308 | Value *S, ///< The value to be casted |
5309 | Type *Ty, ///< The type to casted to |
5310 | const Twine &NameStr, ///< A name for the new instruction |
5311 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5312 | ); |
5313 | |
5314 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5315 | static bool classof(const Instruction *I) { |
5316 | return I->getOpcode() == BitCast; |
5317 | } |
5318 | static bool classof(const Value *V) { |
5319 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5320 | } |
5321 | }; |
5322 | |
5323 | //===----------------------------------------------------------------------===// |
5324 | // AddrSpaceCastInst Class |
5325 | //===----------------------------------------------------------------------===// |
5326 | |
5327 | /// This class represents a conversion between pointers from one address space |
5328 | /// to another. |
5329 | class AddrSpaceCastInst : public CastInst { |
5330 | protected: |
5331 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5332 | friend class Instruction; |
5333 | |
5334 | /// Clone an identical AddrSpaceCastInst. |
5335 | AddrSpaceCastInst *cloneImpl() const; |
5336 | |
5337 | public: |
5338 | /// Constructor with insert-before-instruction semantics |
5339 | AddrSpaceCastInst( |
5340 | Value *S, ///< The value to be casted |
5341 | Type *Ty, ///< The type to casted to |
5342 | const Twine &NameStr = "", ///< A name for the new instruction |
5343 | Instruction *InsertBefore = nullptr ///< Where to insert the new instruction |
5344 | ); |
5345 | |
5346 | /// Constructor with insert-at-end-of-block semantics |
5347 | AddrSpaceCastInst( |
5348 | Value *S, ///< The value to be casted |
5349 | Type *Ty, ///< The type to casted to |
5350 | const Twine &NameStr, ///< A name for the new instruction |
5351 | BasicBlock *InsertAtEnd ///< The block to insert the instruction into |
5352 | ); |
5353 | |
5354 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5355 | static bool classof(const Instruction *I) { |
5356 | return I->getOpcode() == AddrSpaceCast; |
5357 | } |
5358 | static bool classof(const Value *V) { |
5359 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5360 | } |
5361 | |
5362 | /// Gets the pointer operand. |
5363 | Value *getPointerOperand() { |
5364 | return getOperand(0); |
5365 | } |
5366 | |
5367 | /// Gets the pointer operand. |
5368 | const Value *getPointerOperand() const { |
5369 | return getOperand(0); |
5370 | } |
5371 | |
5372 | /// Gets the operand index of the pointer operand. |
5373 | static unsigned getPointerOperandIndex() { |
5374 | return 0U; |
5375 | } |
5376 | |
5377 | /// Returns the address space of the pointer operand. |
5378 | unsigned getSrcAddressSpace() const { |
5379 | return getPointerOperand()->getType()->getPointerAddressSpace(); |
5380 | } |
5381 | |
5382 | /// Returns the address space of the result. |
5383 | unsigned getDestAddressSpace() const { |
5384 | return getType()->getPointerAddressSpace(); |
5385 | } |
5386 | }; |
5387 | |
5388 | //===----------------------------------------------------------------------===// |
5389 | // Helper functions |
5390 | //===----------------------------------------------------------------------===// |
5391 | |
5392 | /// A helper function that returns the pointer operand of a load or store |
5393 | /// instruction. Returns nullptr if not load or store. |
5394 | inline const Value *getLoadStorePointerOperand(const Value *V) { |
5395 | if (auto *Load = dyn_cast<LoadInst>(V)) |
5396 | return Load->getPointerOperand(); |
5397 | if (auto *Store = dyn_cast<StoreInst>(V)) |
5398 | return Store->getPointerOperand(); |
5399 | return nullptr; |
5400 | } |
5401 | inline Value *getLoadStorePointerOperand(Value *V) { |
5402 | return const_cast<Value *>( |
5403 | getLoadStorePointerOperand(static_cast<const Value *>(V))); |
5404 | } |
5405 | |
5406 | /// A helper function that returns the pointer operand of a load, store |
5407 | /// or GEP instruction. Returns nullptr if not load, store, or GEP. |
5408 | inline const Value *getPointerOperand(const Value *V) { |
5409 | if (auto *Ptr = getLoadStorePointerOperand(V)) |
5410 | return Ptr; |
5411 | if (auto *Gep = dyn_cast<GetElementPtrInst>(V)) |
5412 | return Gep->getPointerOperand(); |
5413 | return nullptr; |
5414 | } |
5415 | inline Value *getPointerOperand(Value *V) { |
5416 | return const_cast<Value *>(getPointerOperand(static_cast<const Value *>(V))); |
5417 | } |
5418 | |
5419 | /// A helper function that returns the alignment of load or store instruction. |
5420 | inline Align getLoadStoreAlignment(Value *I) { |
5421 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(static_cast <bool> ((isa<LoadInst>(I) || isa< StoreInst>(I)) && "Expected Load or Store instruction" ) ? void (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "llvm/include/llvm/IR/Instructions.h", 5422, __extension__ __PRETTY_FUNCTION__ )) |
5422 | "Expected Load or Store instruction")(static_cast <bool> ((isa<LoadInst>(I) || isa< StoreInst>(I)) && "Expected Load or Store instruction" ) ? void (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "llvm/include/llvm/IR/Instructions.h", 5422, __extension__ __PRETTY_FUNCTION__ )); |
5423 | if (auto *LI = dyn_cast<LoadInst>(I)) |
5424 | return LI->getAlign(); |
5425 | return cast<StoreInst>(I)->getAlign(); |
5426 | } |
5427 | |
5428 | /// A helper function that returns the address space of the pointer operand of |
5429 | /// load or store instruction. |
5430 | inline unsigned getLoadStoreAddressSpace(Value *I) { |
5431 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(static_cast <bool> ((isa<LoadInst>(I) || isa< StoreInst>(I)) && "Expected Load or Store instruction" ) ? void (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "llvm/include/llvm/IR/Instructions.h", 5432, __extension__ __PRETTY_FUNCTION__ )) |
5432 | "Expected Load or Store instruction")(static_cast <bool> ((isa<LoadInst>(I) || isa< StoreInst>(I)) && "Expected Load or Store instruction" ) ? void (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "llvm/include/llvm/IR/Instructions.h", 5432, __extension__ __PRETTY_FUNCTION__ )); |
5433 | if (auto *LI = dyn_cast<LoadInst>(I)) |
5434 | return LI->getPointerAddressSpace(); |
5435 | return cast<StoreInst>(I)->getPointerAddressSpace(); |
5436 | } |
5437 | |
5438 | /// A helper function that returns the type of a load or store instruction. |
5439 | inline Type *getLoadStoreType(Value *I) { |
5440 | assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(static_cast <bool> ((isa<LoadInst>(I) || isa< StoreInst>(I)) && "Expected Load or Store instruction" ) ? void (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "llvm/include/llvm/IR/Instructions.h", 5441, __extension__ __PRETTY_FUNCTION__ )) |
5441 | "Expected Load or Store instruction")(static_cast <bool> ((isa<LoadInst>(I) || isa< StoreInst>(I)) && "Expected Load or Store instruction" ) ? void (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\"" , "llvm/include/llvm/IR/Instructions.h", 5441, __extension__ __PRETTY_FUNCTION__ )); |
5442 | if (auto *LI = dyn_cast<LoadInst>(I)) |
5443 | return LI->getType(); |
5444 | return cast<StoreInst>(I)->getValueOperand()->getType(); |
5445 | } |
5446 | |
5447 | /// A helper function that returns an atomic operation's sync scope; returns |
5448 | /// std::nullopt if it is not an atomic operation. |
5449 | inline std::optional<SyncScope::ID> getAtomicSyncScopeID(const Instruction *I) { |
5450 | if (!I->isAtomic()) |
5451 | return std::nullopt; |
5452 | if (auto *AI = dyn_cast<LoadInst>(I)) |
5453 | return AI->getSyncScopeID(); |
5454 | if (auto *AI = dyn_cast<StoreInst>(I)) |
5455 | return AI->getSyncScopeID(); |
5456 | if (auto *AI = dyn_cast<FenceInst>(I)) |
5457 | return AI->getSyncScopeID(); |
5458 | if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I)) |
5459 | return AI->getSyncScopeID(); |
5460 | if (auto *AI = dyn_cast<AtomicRMWInst>(I)) |
5461 | return AI->getSyncScopeID(); |
5462 | llvm_unreachable("unhandled atomic operation")::llvm::llvm_unreachable_internal("unhandled atomic operation" , "llvm/include/llvm/IR/Instructions.h", 5462); |
5463 | } |
5464 | |
5465 | //===----------------------------------------------------------------------===// |
5466 | // FreezeInst Class |
5467 | //===----------------------------------------------------------------------===// |
5468 | |
5469 | /// This class represents a freeze function that returns random concrete |
5470 | /// value if an operand is either a poison value or an undef value |
5471 | class FreezeInst : public UnaryInstruction { |
5472 | protected: |
5473 | // Note: Instruction needs to be a friend here to call cloneImpl. |
5474 | friend class Instruction; |
5475 | |
5476 | /// Clone an identical FreezeInst |
5477 | FreezeInst *cloneImpl() const; |
5478 | |
5479 | public: |
5480 | explicit FreezeInst(Value *S, |
5481 | const Twine &NameStr = "", |
5482 | Instruction *InsertBefore = nullptr); |
5483 | FreezeInst(Value *S, const Twine &NameStr, BasicBlock *InsertAtEnd); |
5484 | |
5485 | // Methods for support type inquiry through isa, cast, and dyn_cast: |
5486 | static inline bool classof(const Instruction *I) { |
5487 | return I->getOpcode() == Freeze; |
5488 | } |
5489 | static inline bool classof(const Value *V) { |
5490 | return isa<Instruction>(V) && classof(cast<Instruction>(V)); |
5491 | } |
5492 | }; |
5493 | |
5494 | } // end namespace llvm |
5495 | |
5496 | #endif // LLVM_IR_INSTRUCTIONS_H |