LLVM 24.0.0git
SeparateConstOffsetFromGEP.cpp
Go to the documentation of this file.
1//===- SeparateConstOffsetFromGEP.cpp -------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Loop unrolling may create many similar GEPs for array accesses.
10// e.g., a 2-level loop
11//
12// float a[32][32]; // global variable
13//
14// for (int i = 0; i < 2; ++i) {
15// for (int j = 0; j < 2; ++j) {
16// ...
17// ... = a[x + i][y + j];
18// ...
19// }
20// }
21//
22// will probably be unrolled to:
23//
24// gep %a, 0, %x, %y; load
25// gep %a, 0, %x, %y + 1; load
26// gep %a, 0, %x + 1, %y; load
27// gep %a, 0, %x + 1, %y + 1; load
28//
29// LLVM's GVN does not use partial redundancy elimination yet, and is thus
30// unable to reuse (gep %a, 0, %x, %y). As a result, this misoptimization incurs
31// significant slowdown in targets with limited addressing modes. For instance,
32// because the PTX target does not support the reg+reg addressing mode, the
33// NVPTX backend emits PTX code that literally computes the pointer address of
34// each GEP, wasting tons of registers. It emits the following PTX for the
35// first load and similar PTX for other loads.
36//
37// mov.u32 %r1, %x;
38// mov.u32 %r2, %y;
39// mul.wide.u32 %rl2, %r1, 128;
40// mov.u64 %rl3, a;
41// add.s64 %rl4, %rl3, %rl2;
42// mul.wide.u32 %rl5, %r2, 4;
43// add.s64 %rl6, %rl4, %rl5;
44// ld.global.f32 %f1, [%rl6];
45//
46// To reduce the register pressure, the optimization implemented in this file
47// merges the common part of a group of GEPs, so we can compute each pointer
48// address by adding a simple offset to the common part, saving many registers.
49//
50// It works by splitting each GEP into a variadic base and a constant offset.
51// The variadic base can be computed once and reused by multiple GEPs, and the
52// constant offsets can be nicely folded into the reg+immediate addressing mode
53// (supported by most targets) without using any extra register.
54//
55// For instance, we transform the four GEPs and four loads in the above example
56// into:
57//
58// base = gep a, 0, x, y
59// load base
60// load base + 1 * sizeof(float)
61// load base + 32 * sizeof(float)
62// load base + 33 * sizeof(float)
63//
64// Given the transformed IR, a backend that supports the reg+immediate
65// addressing mode can easily fold the pointer arithmetics into the loads. For
66// example, the NVPTX backend can easily fold the pointer arithmetics into the
67// ld.global.f32 instructions, and the resultant PTX uses much fewer registers.
68//
69// mov.u32 %r1, %tid.x;
70// mov.u32 %r2, %tid.y;
71// mul.wide.u32 %rl2, %r1, 128;
72// mov.u64 %rl3, a;
73// add.s64 %rl4, %rl3, %rl2;
74// mul.wide.u32 %rl5, %r2, 4;
75// add.s64 %rl6, %rl4, %rl5;
76// ld.global.f32 %f1, [%rl6]; // so far the same as unoptimized PTX
77// ld.global.f32 %f2, [%rl6+4]; // much better
78// ld.global.f32 %f3, [%rl6+128]; // much better
79// ld.global.f32 %f4, [%rl6+132]; // much better
80//
81// Another improvement enabled by the LowerGEP flag is to lower a GEP with
82// multiple indices to multiple GEPs with a single index.
83// Such transformation can have following benefits:
84// (1) It can always extract constants in the indices of structure type.
85// (2) After such Lowering, there are more optimization opportunities such as
86// CSE, LICM and CGP.
87//
88// E.g. The following GEPs have multiple indices:
89// BB1:
90// %p = getelementptr [10 x %struct], ptr %ptr, i64 %i, i64 %j1, i32 3
91// load %p
92// ...
93// BB2:
94// %p2 = getelementptr [10 x %struct], ptr %ptr, i64 %i, i64 %j1, i32 2
95// load %p2
96// ...
97//
98// We can not do CSE to the common part related to index "i64 %i". Lowering
99// GEPs can achieve such goals.
100//
101// This pass will lower a GEP with multiple indices into multiple GEPs with a
102// single index:
103// BB1:
104// %2 = mul i64 %i, length_of_10xstruct ; CSE opportunity
105// %3 = getelementptr i8, ptr %ptr, i64 %2 ; CSE opportunity
106// %4 = mul i64 %j1, length_of_struct
107// %5 = getelementptr i8, ptr %3, i64 %4
108// %p = getelementptr i8, ptr %5, struct_field_3 ; Constant offset
109// load %p
110// ...
111// BB2:
112// %8 = mul i64 %i, length_of_10xstruct ; CSE opportunity
113// %9 = getelementptr i8, ptr %ptr, i64 %8 ; CSE opportunity
114// %10 = mul i64 %j2, length_of_struct
115// %11 = getelementptr i8, ptr %9, i64 %10
116// %p2 = getelementptr i8, ptr %11, struct_field_2 ; Constant offset
117// load %p2
118// ...
119//
120// Lowering GEPs can also benefit other passes such as LICM and CGP.
121// LICM (Loop Invariant Code Motion) can not hoist/sink a GEP of multiple
122// indices if one of the index is variant. If we lower such GEP into invariant
123// parts and variant parts, LICM can hoist/sink those invariant parts.
124// CGP (CodeGen Prepare) tries to sink address calculations that match the
125// target's addressing modes. A GEP with multiple indices may not match and will
126// not be sunk. If we lower such GEP into smaller parts, CGP may sink some of
127// them. So we end up with a better addressing mode.
128//
129//===----------------------------------------------------------------------===//
130
132#include "ScalarOptions.h"
133#include "llvm/ADT/APInt.h"
134#include "llvm/ADT/DenseMap.h"
136#include "llvm/ADT/SmallVector.h"
142#include "llvm/IR/BasicBlock.h"
143#include "llvm/IR/Constant.h"
144#include "llvm/IR/Constants.h"
145#include "llvm/IR/DataLayout.h"
146#include "llvm/IR/DerivedTypes.h"
147#include "llvm/IR/Dominators.h"
148#include "llvm/IR/Function.h"
150#include "llvm/IR/IRBuilder.h"
151#include "llvm/IR/InstrTypes.h"
152#include "llvm/IR/Instruction.h"
153#include "llvm/IR/Instructions.h"
154#include "llvm/IR/Module.h"
155#include "llvm/IR/PassManager.h"
156#include "llvm/IR/PatternMatch.h"
157#include "llvm/IR/Type.h"
158#include "llvm/IR/User.h"
159#include "llvm/IR/Value.h"
161#include "llvm/Pass.h"
162#include "llvm/Support/Casting.h"
168#include <cassert>
169#include <cstdint>
170#include <iterator>
171#include <optional>
172#include <string>
173
174using namespace llvm;
175using namespace llvm::PatternMatch;
176
177namespace {
178
179/// A helper class for separating a constant offset from a GEP index.
180///
181/// In real programs, a GEP index may be more complicated than a simple addition
182/// of something and a constant integer which can be trivially splitted. For
183/// example, to split ((a << 3) | 5) + b, we need to search deeper for the
184/// constant offset, so that we can separate the index to (a << 3) + b and 5.
185///
186/// Therefore, this class looks into the expression that computes a given GEP
187/// index, and tries to find a constant integer that can be hoisted to the
188/// outermost level of the expression as an addition. Not every constant in an
189/// expression can jump out. e.g., we cannot transform (b * (a + 5)) to (b * a +
190/// 5); nor can we transform (3 * (a + 5)) to (3 * a + 5), however in this case,
191/// -instcombine probably already optimized (3 * (a + 5)) to (3 * a + 15).
192class ConstantOffsetExtractor {
193public:
194 /// Extracts a constant offset from the given GEP index. It returns the
195 /// new index representing the remainder (equal to the original index minus
196 /// the constant offset), or nullptr if we cannot extract a constant offset.
197 /// \p Idx The given GEP index use
198 /// \p UserTreeRoot Outputs the rebuilt index for dead instruction cleanup.
199 /// \p PreservesNUW Outputs whether the extraction allows preserving the
200 /// GEP's nuw flag, if it has one.
201 static Value *Extract(const Use &Idx, User *&UserTreeRoot,
202 bool &PreservesNUW);
203
204 /// Looks for a constant offset from the given GEP index without extracting
205 /// it. It returns the numeric value of the extracted constant offset, or
206 /// std::nullopt on failure. The arguments have the same meaning as Extract.
207 static std::optional<APInt> Find(const Use &Idx);
208
209private:
210 struct RewriteNode {
211 User *V;
212 std::optional<unsigned> LHS;
213 std::optional<unsigned> RHS;
214 };
215
216 struct CastState {
218
219 void pushCast(CastInst *Cast) {
220 assert((isa<SExtInst, ZExtInst, TruncInst>(Cast)) && "Unexpected cast");
221 Casts.push_back(Cast);
222 }
223
224 void popCast() { Casts.pop_back(); }
225
226 bool hasSignExtension() const {
227 // The innermost extension determines whether signed overflow matters:
228 // sext(zext(a)) = zext(a).
229 for (CastInst *C : reverse(Casts))
231 return isa<SExtInst>(C);
232 return false;
233 }
234
235 bool hasZeroExtension() const { return any_of(Casts, IsaPred<ZExtInst>); }
236
237 bool hasExtensions() const {
238 return any_of(Casts, IsaPred<SExtInst, ZExtInst>);
239 }
240
241 APInt apply(APInt Offset) const {
242 for (CastInst *Cast : llvm::reverse(Casts)) {
243 unsigned BitWidth = cast<IntegerType>(Cast->getType())->getBitWidth();
244 if (isa<SExtInst>(Cast))
245 Offset = Offset.sext(BitWidth);
246 else if (isa<ZExtInst>(Cast))
247 Offset = Offset.zext(BitWidth);
248 else {
249 assert(isa<TruncInst>(Cast) && "Unexpected cast");
250 Offset = Offset.trunc(BitWidth);
251 }
252 }
253 return Offset;
254 }
255 };
256
257 ConstantOffsetExtractor(BasicBlock::iterator InsertionPt)
258 : IP(InsertionPt), DL(InsertionPt->getDataLayout()), SQ(DL) {}
259
260 /// Searches the expression that computes V for a constant offset C s.t.
261 /// V can be reassociated into the form V' + C. If the searching is
262 /// successful, returns C and appends the rewrite for V to UserTree;
263 /// otherwise, returns std::nullopt and leaves UserTree unchanged.
264 /// \p V The given expression
265 /// \p Idx The original index use of the GEP, or nullptr if its
266 /// sign and bounds information no longer applies
267 /// \p Casts The casts surrounding V in the original expression.
268 std::optional<APInt> find(Value *V, const Use *Idx, CastState &Casts);
269
270 /// Collect offsets from the operands and record their replacements.
271 std::optional<APInt> findInOperands(RewriteNode &Node, CastState &Casts);
272
273 /// After finding the constant offset C from the GEP index I, we build a new
274 /// index I' s.t. I' + C = I. This function builds and returns the new
275 /// index I' according to UserTree produced by function "find".
276 ///
277 /// While rebuilding, distribute the casts recorded in \p Casts to each
278 /// operand that remains in the expression, then reassociate the expression to
279 /// the form I' + C and return I'.
280 Value *rebuildWithoutConstOffset(unsigned NodeIndex, CastState &Casts);
281
282 Value *rebuildWithoutConstOffset() {
283 CastState Casts;
284 return rebuildWithoutConstOffset(UserTree.size() - 1, Casts);
285 }
286
287 /// A helper function to apply a list of sext/zext/trunc casts to value
288 /// V. e.g., if Casts = [sext i32 to i64, zext i16 to i32], this function
289 /// returns "sext i32 (zext i16 V to i32) to i64".
290 Value *applyCasts(Value *V, const CastState &Casts);
291
292 /// A helper function that returns whether we can trace into the operands
293 /// of binary operator BO for a constant offset.
294 ///
295 /// \p Casts The casts surrounding BO.
296 /// \p Idx The original index use of the GEP, or nullptr if its
297 /// sign and bounds information no longer applies
298 bool canTraceInto(const CastState &Casts, BinaryOperator *BO, const Use *Idx);
299
300 /// Analyze a xor expression, and identify the bits in the constant operand
301 /// that are disjoint from the base operand's known set bits. For these
302 /// disjoint bits, a xor is equivalent to an addition, which allows us to
303 /// extract them as constant offsets that can be folded into the immediate
304 /// field of addressing operations. The transformation is the following one:
305 ///
306 /// Base ^ Const becomes (Base ^ NonDisjointBits) + DisjointBits
307 ///
308 /// where DisjointBits = Const & KnownZeros(Base) and
309 /// NonDisjointBits = Const & ~DisjointBits.
310 ///
311 /// Example with ptr having known-zero low bit:
312 /// Original: `xor %ptr, 3` ; 3 = 0b11
313 /// Analysis: DisjointBits = 3 & KnownZeros(%ptr) = 0b11 & 0b01 = 0b01
314 /// Result: `(xor %ptr, 2) + 1` where 1 can be folded into address mode
315 ///
316 /// \param Node The XOR node to analyze.
317 /// \return The disjoint bits (the extractable offset), or std::nullopt if
318 /// none exist. On success, records the remaining constant as a child node.
319 std::optional<APInt> extractDisjointBitsFromXor(RewriteNode &Node);
320
321 /// A postorder rewrite plan. Child indices refer to earlier entries; absent
322 /// children leave the original operands unchanged. Constant leaves hold
323 /// replacement values rather than the constants being extracted.
325
326 static constexpr unsigned MaxVisitedNodes = 64;
327
328 /// Count visits, including repeated visits to shared expressions.
329 unsigned NumVisited = 0;
330
331 /// Insertion position of cloned instructions.
333
334 const DataLayout &DL;
335 const SimplifyQuery SQ;
336};
337
338/// A pass that tries to split every GEP in the function into a variadic
339/// base and a constant offset. It is a FunctionPass because searching for the
340/// constant offset may inspect other basic blocks.
341class SeparateConstOffsetFromGEPLegacyPass : public FunctionPass {
342public:
343 static char ID;
344
345 SeparateConstOffsetFromGEPLegacyPass(bool LowerGEP = false)
346 : FunctionPass(ID), LowerGEP(LowerGEP) {
349 }
350
351 void getAnalysisUsage(AnalysisUsage &AU) const override {
352 AU.addRequired<DominatorTreeWrapperPass>();
353 AU.addRequired<TargetTransformInfoWrapperPass>();
354 AU.addRequired<LoopInfoWrapperPass>();
355 AU.setPreservesCFG();
356 AU.addRequired<TargetLibraryInfoWrapperPass>();
357 }
358
359 bool runOnFunction(Function &F) override;
360
361private:
362 bool LowerGEP;
363};
364
365/// A pass that tries to split every GEP in the function into a variadic
366/// base and a constant offset. It is a FunctionPass because searching for the
367/// constant offset may inspect other basic blocks.
368class SeparateConstOffsetFromGEP {
369public:
370 SeparateConstOffsetFromGEP(
371 DominatorTree *DT, LoopInfo *LI, TargetLibraryInfo *TLI,
372 function_ref<TargetTransformInfo &(Function &)> GetTTI, bool LowerGEP)
373 : DT(DT), LI(LI), TLI(TLI), GetTTI(GetTTI), LowerGEP(LowerGEP) {}
374
375 bool run(Function &F);
376
377private:
378 /// Track the operands of an add or sub.
379 using ExprKey = std::pair<Value *, Value *>;
380
381 /// Create a pair for use as a map key for a commutable operation.
382 static ExprKey createNormalizedCommutablePair(Value *A, Value *B) {
383 if (A < B)
384 return {A, B};
385 return {B, A};
386 }
387
388 /// Tries to split the given GEP into a variadic base and a constant offset,
389 /// and returns true if the splitting succeeds.
390 bool splitGEP(GetElementPtrInst *GEP);
391
392 /// Tries to reorder the given GEP with the GEP that produces the base if
393 /// doing so results in producing a constant offset as the outermost
394 /// index.
395 bool reorderGEP(GetElementPtrInst *GEP, TargetTransformInfo &TTI);
396
397 /// Lower a GEP with multiple indices into multiple GEPs with a single index.
398 /// Function splitGEP already split the original GEP into a variadic part and
399 /// a constant offset (i.e., AccumulativeByteOffset). This function lowers the
400 /// variadic part into a set of GEPs with a single index and applies
401 /// AccumulativeByteOffset to it.
402 /// \p Variadic The variadic part of the original GEP.
403 /// \p AccumulativeByteOffset The constant offset.
404 void lowerToSingleIndexGEPs(GetElementPtrInst *Variadic,
405 const APInt &AccumulativeByteOffset);
406
407 /// Finds the constant offset within each index and accumulates them. If
408 /// LowerGEP is true, it finds in indices of both sequential and structure
409 /// types, otherwise it only finds in sequential indices. The output
410 /// NeedsExtraction indicates whether we successfully find a constant
411 /// offset, and SignedOverflow indicates if calculating or removing offsets
412 /// may have signed overflow.
413 APInt accumulateByteOffset(GetElementPtrInst *GEP, bool &NeedsExtraction,
414 bool &SignedOverflow);
415
416 /// Canonicalize array indices to pointer-size integers. This helps to
417 /// simplify the logic of splitting a GEP. For example, if a + b is a
418 /// pointer-size integer, we have
419 /// gep base, a + b = gep (gep base, a), b
420 /// However, this equality may not hold if the size of a + b is smaller than
421 /// the pointer size, because LLVM conceptually sign-extends GEP indices to
422 /// pointer size before computing the address
423 /// (http://llvm.org/docs/LangRef.html#id181).
424 ///
425 /// This canonicalization is very likely already done in clang and
426 /// instcombine. Therefore, the program will probably remain the same.
427 ///
428 /// Returns true if the module changes.
429 ///
430 /// Verified in @i32_add in split-gep.ll
431 bool canonicalizeArrayIndicesToIndexSize(GetElementPtrInst *GEP);
432
433 /// Optimize sext(a)+sext(b) to sext(a+b) when a+b can't sign overflow.
434 /// SeparateConstOffsetFromGEP distributes a sext to leaves before extracting
435 /// the constant offset. After extraction, it becomes desirable to reunion the
436 /// distributed sexts. For example,
437 ///
438 /// &a[sext(i +nsw (j +nsw 5)]
439 /// => distribute &a[sext(i) +nsw (sext(j) +nsw 5)]
440 /// => constant extraction &a[sext(i) + sext(j)] + 5
441 /// => reunion &a[sext(i +nsw j)] + 5
442 bool reuniteExts(Function &F);
443
444 /// A helper that reunites sexts in an instruction.
445 bool reuniteExts(Instruction *I);
446
447 /// Find the closest dominator of <Dominatee> that is equivalent to <Key>.
448 Instruction *findClosestMatchingDominator(
449 ExprKey Key, Instruction *Dominatee,
450 DenseMap<ExprKey, SmallVector<Instruction *, 2>> &DominatingExprs);
451
452 /// Verify F is free of dead code.
453 void verifyNoDeadCode(Function &F);
454
455 bool hasMoreThanOneUseInLoop(Value *v, Loop *L);
456
457 // Swap the index operand of two GEP.
458 void swapGEPOperand(GetElementPtrInst *First, GetElementPtrInst *Second);
459
460 // Check if it is safe to swap operand of two GEP.
461 bool isLegalToSwapOperand(GetElementPtrInst *First, GetElementPtrInst *Second,
462 Loop *CurLoop);
463
464 const DataLayout *DL = nullptr;
465 DominatorTree *DT = nullptr;
466 LoopInfo *LI;
467 TargetLibraryInfo *TLI;
468 // Retrieved lazily since not always used.
469 function_ref<TargetTransformInfo &(Function &)> GetTTI;
470
471 /// Whether to lower a GEP with multiple indices into arithmetic operations or
472 /// multiple GEPs with a single index.
473 bool LowerGEP;
474
475 DenseMap<ExprKey, SmallVector<Instruction *, 2>> DominatingAdds;
476 DenseMap<ExprKey, SmallVector<Instruction *, 2>> DominatingSubs;
477};
478
479} // end anonymous namespace
480
481char SeparateConstOffsetFromGEPLegacyPass::ID = 0;
482
484 SeparateConstOffsetFromGEPLegacyPass, "separate-const-offset-from-gep",
485 "Split GEPs to a variadic base and a constant offset for better CSE", false,
486 false)
493 SeparateConstOffsetFromGEPLegacyPass, "separate-const-offset-from-gep",
494 "Split GEPs to a variadic base and a constant offset for better CSE", false,
495 false)
496
498 return new SeparateConstOffsetFromGEPLegacyPass(LowerGEP);
499}
500
501// Checks if it is safe to reorder an add/sext result used in a GEP.
502//
503// An inbounds GEP does not guarantee that the index is non-negative.
504// This helper first checks whether value tracking proves that the add cannot
505// have signed overflow. If so, the transform is safe.
506// Second, it checks whether the GEP is inbounds and directly based on a global
507// or an alloca, which are required to prove futher transform validity.
508// If the GEP:
509// - Has a zero offset from the base and Idx is its first index, the index is
510// non-negative (any negative value would produce poison/UB)
511// - Has ObjectSize < (2^(N-1) - C + 1) * stride, where C is a constant from the
512// add, stride is the element size of Idx, and N is bitwidth of the add.
513// This is because with this pattern:
514// %add = add iN %val, C
515// %sext = sext iN %add to i64
516// %gep = getelementptr inbounds TYPE, %sext
517// The worst-case is when %val sign-flips to produce the smallest magnitude
518// negative value, at 2^(N-1)-1. In this case, the add/sext is -(2^(N-1)-C+1),
519// and the sext/add is 2^(N-1)+C-1 (2^N difference). The original add/sext
520// only produces a defined GEP when -(2^(N-1)-C+1) is inbounds. So, if
521// ObjectSize < (2^(N-1) - C + 1) * stride, it is impossible for the
522// worst-case sign-flip to be defined.
523// Note that in this case the GEP is not neccesarily non-negative, but any
524// negative results will still produce the same behavior in the reordered
525// version with a defined GEP.
526// This can also work for negative C, but the threshold is instead
527// (2^(N-1)+C)*stride, since the sign-flip is done in reverse and is instead
528// producing a large positive value that still needs to be inbounds to the
529// object size. If C is negative, we cannot make any useful assumptions based
530// on the offset, since it would need to be extremely large.
531static bool canReorderAddSextToGEP(const Use *Idx, const BinaryOperator *Add,
532 const DataLayout &DL) {
535 return true;
536
537 const auto *GEP = cast<GetElementPtrInst>(Idx->getUser());
538 if (!GEP->isInBounds())
539 return false;
540
541 const Value *Ptr = GEP->getPointerOperand();
542 int64_t Offset = 0;
543 const Value *Base =
544 GetPointerBaseWithConstantOffset(const_cast<Value *>(Ptr), Offset, DL);
545
546 // We need one of the operands to be a constant to be able to trace into the
547 // operator.
548 const ConstantInt *CI = dyn_cast<ConstantInt>(Add->getOperand(0));
549 if (!CI)
550 CI = dyn_cast<ConstantInt>(Add->getOperand(1));
551 if (!CI)
552 return false;
553 // Calculate the threshold
554 APInt Threshold;
555 unsigned N = Add->getType()->getIntegerBitWidth();
556 // Track the use: the same value may index different types in this GEP.
557 auto GTI = std::next(gep_type_begin(GEP), Idx->getOperandNo() - 1);
558 TypeSize ElemSize = GTI.getSequentialElementStride(DL);
559 if (ElemSize.isScalable())
560 return false;
561 uint64_t Stride = ElemSize.getFixedValue();
562 if (!CI->isNegative()) {
563 // (2^(N-1) - C + 1) * stride
564 Threshold = (APInt::getSignedMinValue(N).zext(128) -
565 CI->getValue().zextOrTrunc(128) + 1) *
566 APInt(128, Stride);
567 } else {
568 // (2^(N-1) + C) * stride
569 Threshold = (APInt::getSignedMinValue(N).zext(128) +
570 CI->getValue().sextOrTrunc(128)) *
571 APInt(128, Stride);
572 }
573
574 // Only the first index is relative to Ptr. Earlier indices may move the
575 // pointer within the object, so later indices must use the object-size proof.
576 if (Idx->getOperandNo() == 1 && Base &&
578 // If the offset is zero from an alloca or global, inbounds is sufficient to
579 // prove non-negativity if one add operand is non-negative
580 if (Offset == 0)
581 return true;
582
583 // Check if the Offset < Threshold (positive CI only) otherwise
584 if (Offset < 0)
585 return true;
586 if (APInt(128, (uint64_t)Offset).ult(Threshold))
587 return true;
588 } else {
589 // If we can't determine the offset from the base object, we can still use
590 // the underlying object and type size constraints
592 // Can only prove non-negativity if the base object is known
594 return false;
595 }
596
597 // Check if the ObjectSize < Threshold (for both positive or negative C)
598 uint64_t ObjSize = 0;
599 if (const auto *AI = dyn_cast<AllocaInst>(Base)) {
600 if (auto AllocSize = AI->getAllocationSize(DL))
601 if (!AllocSize->isScalable())
602 ObjSize = AllocSize->getFixedValue();
603 } else if (const auto *GV = dyn_cast<GlobalVariable>(Base)) {
604 TypeSize GVSize = DL.getTypeAllocSize(GV->getValueType());
605 if (!GVSize.isScalable())
606 ObjSize = GVSize.getFixedValue();
607 }
608 if (ObjSize > 0 && APInt(128, ObjSize).ult(Threshold))
609 return true;
610
611 return false;
612}
613
614bool ConstantOffsetExtractor::canTraceInto(const CastState &Casts,
615 BinaryOperator *BO, const Use *Idx) {
616 bool SignExtended = Casts.hasSignExtension();
617 bool ZeroExtended = Casts.hasZeroExtension();
618 // Do not trace into "or" unless it is equivalent to "add nuw nsw".
619 // This is the case if the or's disjoint flag is set.
620 if (BO->getOpcode() == Instruction::Or)
621 return cast<PossiblyDisjointInst>(BO)->isDisjoint();
622
623 // We only consider ADD and SUB here, because a non-zero constant found in
624 // expressions composed of these operations can be easily hoisted as a
625 // constant offset by reassociation.
626 if (BO->getOpcode() != Instruction::Add &&
627 BO->getOpcode() != Instruction::Sub)
628 return false;
629
630 // In addition, tracing into BO requires that its surrounding sext/zext/trunc
631 // (if any) is distributable to both operands.
632 //
633 // sext (add/sub nsw A, B) == add/sub nsw (sext A), (sext B)
634 // zext (add/sub nuw A, B) == add/sub nuw (zext A), (zext B)
635 if ((!SignExtended || BO->hasNoSignedWrap()) &&
636 (!ZeroExtended || BO->hasNoUnsignedWrap()))
637 return true;
638
639 if (BO->getOpcode() == Instruction::Add && !ZeroExtended && Idx) {
640 const auto *GEP = cast<GetElementPtrInst>(Idx->getUser());
641 // For a sext(add nuw), allow tracing through when the enclosing GEP is both
642 // inbounds and nuw.
643 if (SignExtended && BO->hasNoUnsignedWrap() && GEP->isInBounds() &&
644 GEP->hasNoUnsignedWrap())
645 return true;
646
647 // Trace through sext when value tracking or the GEP's bounds prove that
648 // the addition cannot have signed overflow.
649 //
650 // Verified in @sext_add in split-gep.ll.
651 if (canReorderAddSextToGEP(Idx, BO, DL))
652 return true;
653 }
654
655 return false;
656}
657
658std::optional<APInt> ConstantOffsetExtractor::findInOperands(RewriteNode &Node,
659 CastState &Casts) {
660 auto *BO = cast<BinaryOperator>(Node.V);
661 // An intervening binary operator invalidates the GEP's index sign and bounds
662 // information, so do not pass it to either operand.
663 std::optional<APInt> LHS = find(BO->getOperand(0), nullptr, Casts);
664 if (LHS)
665 Node.LHS = UserTree.size() - 1;
666
667 std::optional<APInt> RHS = find(BO->getOperand(1), nullptr, Casts);
668 if (!RHS)
669 return LHS;
670 Node.RHS = UserTree.size() - 1;
671 if (BO->getOpcode() == Instruction::Sub)
672 *RHS = -*RHS;
673 return LHS ? *LHS + *RHS : *RHS;
674}
675
676std::optional<APInt> ConstantOffsetExtractor::find(Value *V, const Use *Idx,
677 CastState &Casts) {
678 // TODO(jingyue): We could trace into integer/pointer casts, such as
679 // inttoptr, ptrtoint, bitcast, and addrspacecast. We choose to handle only
680 // integers because it gives good enough results for our benchmarks.
681
682 // Bound repeated traversal of shared expressions and expansion of the
683 // rewrite tree. We can still extract offsets from the visited portion.
684 if (NumVisited++ >= MaxVisitedNodes)
685 return std::nullopt;
686
687 // We cannot do much with Values that are not a User, such as an Argument.
688 User *U = dyn_cast<User>(V);
689 if (U == nullptr)
690 return std::nullopt;
691
692 RewriteNode Node{U, std::nullopt, std::nullopt};
693 std::optional<APInt> ConstantOffset;
694 if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
695 // Leave literal zero offsets alone.
696 if (CI->isZero())
697 return std::nullopt;
698 // Hooray, we found it!
699 ConstantOffset = Casts.apply(CI->getValue());
700 Node.V = ConstantInt::getNullValue(CI->getType());
701 } else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V)) {
702 // Trace into subexpressions for more hoisting opportunities.
703 if (canTraceInto(Casts, BO, Idx))
704 ConstantOffset = findInOperands(Node, Casts);
705 else if (BO->getOpcode() == Instruction::Xor) {
706 ConstantOffset = extractDisjointBitsFromXor(Node);
707 if (ConstantOffset)
708 *ConstantOffset = Casts.apply(*ConstantOffset);
709 }
710 } else if (isa<SExtInst, ZExtInst>(V) ||
711 (isa<TruncInst>(V) && !Casts.hasExtensions())) {
712 Casts.pushCast(cast<CastInst>(V));
713 ConstantOffset = find(U->getOperand(0), Idx, Casts);
714 Casts.popCast();
715 if (ConstantOffset)
716 Node.LHS = UserTree.size() - 1;
717 }
718
719 if (ConstantOffset)
720 UserTree.push_back(Node);
721 return ConstantOffset;
722}
723
724Value *ConstantOffsetExtractor::applyCasts(Value *V, const CastState &Casts) {
725 Value *Current = V;
726 // Casts is built in the use-def order. Therefore, we apply them to V in the
727 // reversed order.
728 for (CastInst *I : llvm::reverse(Casts.Casts)) {
729 if (Constant *C = dyn_cast<Constant>(Current)) {
730 // Try to constant fold the cast.
731 Current = ConstantFoldCastOperand(I->getOpcode(), C, I->getType(), DL);
732 if (Current)
733 continue;
734 }
735
736 Instruction *Cast = I->clone();
737 Cast->setOperand(0, Current);
738 // In ConstantOffsetExtractor::find we do not analyze nuw/nsw for trunc, so
739 // we assume that it is ok to redistribute trunc over add/sub/or. But for
740 // example (add (trunc nuw A), (trunc nuw B)) would be more poisonous than
741 // (trunc nuw (add A, B))), and similarly, (sub (zext nneg A), (zext nneg
742 // B)) would be more poisonous than (zext nneg (sub nuw A, B)). To make such
743 // redistributions legal, we drop all the poison generating flags from the
744 // cloned cast instructions here.
746 Cast->insertBefore(*IP->getParent(), IP);
747 Current = Cast;
748 }
749 return Current;
750}
751
752Value *ConstantOffsetExtractor::rebuildWithoutConstOffset(unsigned NodeIndex,
753 CastState &Casts) {
754 const RewriteNode &Node = UserTree[NodeIndex];
755 if (isa<ConstantInt>(Node.V))
756 return applyCasts(Node.V, Casts);
757
758 if (auto *Cast = dyn_cast<CastInst>(Node.V)) {
759 Casts.pushCast(Cast);
760 Value *Result = rebuildWithoutConstOffset(*Node.LHS, Casts);
761 Casts.popCast();
762 return Result;
763 }
764
765 auto *BO = cast<BinaryOperator>(Node.V);
766 Value *LHS = Node.LHS ? rebuildWithoutConstOffset(*Node.LHS, Casts)
767 : applyCasts(BO->getOperand(0), Casts);
768 Value *RHS = Node.RHS ? rebuildWithoutConstOffset(*Node.RHS, Casts)
769 : applyCasts(BO->getOperand(1), Casts);
770
771 // Zero is a right identity for all supported operators, and a left identity
772 // for all except subtraction.
773 if (match(RHS, m_Zero()))
774 return LHS;
775 if (match(LHS, m_Zero()) && BO->getOpcode() != Instruction::Sub)
776 return RHS;
777
778 BinaryOperator::BinaryOps NewOp = BO->getOpcode();
779 if (BO->getOpcode() == Instruction::Or) {
780 // Rebuild "or" as "add", because "or" may be invalid for the new
781 // expression.
782 //
783 // For instance, given
784 // a | (b + 5) where a and b + 5 have no common bits,
785 // we can extract 5 as the constant offset.
786 //
787 // However, reusing the "or" in the new index would give us
788 // (a | b) + 5
789 // which does not equal a | (b + 5).
790 //
791 // Replacing the "or" with "add" is fine, because
792 // a | (b + 5) = a + (b + 5) = (a + b) + 5
793 NewOp = Instruction::Add;
794 }
795
796 BinaryOperator *NewBO = BinaryOperator::Create(NewOp, LHS, RHS, "", IP);
797 NewBO->takeName(BO);
798 return NewBO;
799}
800
801std::optional<APInt>
802ConstantOffsetExtractor::extractDisjointBitsFromXor(RewriteNode &Node) {
803 auto *XorInst = cast<BinaryOperator>(Node.V);
804 assert(XorInst && XorInst->getOpcode() == Instruction::Xor &&
805 "Expected XOR instruction");
806
807 Value *BaseOp;
808 ConstantInt *XorConstantOp;
809
810 if (!match(XorInst, m_Xor(m_Value(BaseOp), m_ConstantInt(XorConstantOp))))
811 return std::nullopt;
812
813 const KnownBits BaseKnownBits = computeKnownBits(BaseOp, SQ);
814 const APInt &ConstantValue = XorConstantOp->getValue();
815
816 // Compute the disjoint bits, i.e., those bits of the constant operand that
817 // are known-zero in the base. These disjoint bits will contribute to the
818 // final GEP offset. If there are no disjoint bits, there isn't any offset to
819 // extract from the xor.
820 const APInt DisjointBits = ConstantValue & BaseKnownBits.Zero;
821 if (DisjointBits.isZero())
822 return std::nullopt;
823
824 // Avoid a pessimizing rewrite if the disjoint bits include the sign bit.
825 if (DisjointBits.isSignBitSet())
826 return std::nullopt;
827
828 // Compute the remaining bits, i.e., the non-disjoint ones, which are those
829 // that must be preserved in the xor.
830 const APInt NonDisjointBits = ConstantValue & ~DisjointBits;
831 Node.RHS = UserTree.size();
832 UserTree.push_back({ConstantInt::get(XorInst->getContext(), NonDisjointBits),
833 std::nullopt, std::nullopt});
834
835 return DisjointBits;
836}
837
838/// Check if reassociating through a tree node would invalidate GEP's nuw flag.
839static bool allowsPreservingNUW(const User *U) {
840 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(U)) {
841 // Binary operations need to be effectively add nuw.
842 auto Opcode = BO->getOpcode();
843 if (Opcode == BinaryOperator::Or) {
844 // Ors are only considered here if they are disjoint. The addition that
845 // they represent in this case is NUW.
846 assert(cast<PossiblyDisjointInst>(BO)->isDisjoint());
847 return true;
848 }
849 return Opcode == BinaryOperator::Add && BO->hasNoUnsignedWrap();
850 }
851 // UserTree can only contain ConstantInt, CastInst, or BinaryOperator.
852 // Among the possible CastInsts, only trunc without nuw is a problem: If it
853 // is distributed through an add nuw, wrapping may occur:
854 // "add nuw trunc(a), trunc(b)" is more poisonous than "trunc(add nuw a, b)"
855 if (const TruncInst *TI = dyn_cast<TruncInst>(U))
856 return TI->hasNoUnsignedWrap();
857 assert((isa<CastInst>(U) || isa<ConstantInt>(U)) && "Unexpected User.");
858 return true;
859}
860
862 if (auto *I = dyn_cast<Instruction>(Idx.get()))
863 if (auto IP = I->getInsertionPointAfterDef())
864 return *IP;
865 return cast<GetElementPtrInst>(Idx.getUser())->getIterator();
866}
867
868Value *ConstantOffsetExtractor::Extract(const Use &Idx, User *&UserTreeRoot,
869 bool &PreservesNUW) {
870 ConstantOffsetExtractor Extractor(getIndexInsertionPoint(Idx));
871 // Find a constant offset first.
872 CastState Casts;
873 if (!Extractor.find(Idx, &Idx, Casts)) {
874 UserTreeRoot = nullptr;
875 PreservesNUW = true;
876 return nullptr;
877 }
878
879 PreservesNUW = all_of(Extractor.UserTree, [](const RewriteNode &Node) {
880 return allowsPreservingNUW(Node.V);
881 });
882
883 // Separates the constant offset from the GEP index.
884 Value *IdxWithoutConstOffset = Extractor.rebuildWithoutConstOffset();
885 UserTreeRoot = dyn_cast<User>(IdxWithoutConstOffset);
886 return IdxWithoutConstOffset;
887}
888
889std::optional<APInt> ConstantOffsetExtractor::Find(const Use &Idx) {
890 auto *GEP = cast<GetElementPtrInst>(Idx.getUser());
891 CastState Casts;
892 return ConstantOffsetExtractor(GEP->getIterator()).find(Idx, &Idx, Casts);
893}
894
895bool SeparateConstOffsetFromGEP::canonicalizeArrayIndicesToIndexSize(
896 GetElementPtrInst *GEP) {
897 bool Changed = false;
898 Type *PtrIdxTy = DL->getIndexType(GEP->getType());
900 for (User::op_iterator I = GEP->op_begin() + 1, E = GEP->op_end();
901 I != E; ++I, ++GTI) {
902 // Skip struct member indices which must be i32.
903 if (GTI.isSequential()) {
904 if ((*I)->getType() != PtrIdxTy) {
905 *I = CastInst::CreateIntegerCast(*I, PtrIdxTy, true, "idxprom",
907 Changed = true;
908 }
909 }
910 }
911 return Changed;
912}
913
914APInt SeparateConstOffsetFromGEP::accumulateByteOffset(GetElementPtrInst *GEP,
915 bool &NeedsExtraction,
916 bool &SignedOverflow) {
917 NeedsExtraction = false;
918 SignedOverflow = false;
919 unsigned IdxWidth = DL->getIndexTypeSizeInBits(GEP->getType());
920 APInt AccumulativeByteOffset(IdxWidth, 0);
922 for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
923 if (GTI.isSequential()) {
924 // Constant offsets of scalable types are not really constant.
925 if (GTI.getIndexedType()->isScalableTy())
926 continue;
927
928 // Tries to extract a constant offset from this GEP index.
929 if (std::optional<APInt> ConstantOffset =
930 ConstantOffsetExtractor::Find(GEP->getOperandUse(I))) {
931 NeedsExtraction = true;
932 // A GEP may have multiple indices. We accumulate the extracted
933 // constant offset to a byte offset, and later offset the remainder of
934 // the original GEP with this byte offset.
935 const uint64_t Stride = GTI.getSequentialElementStride(*DL);
936 bool Overflow;
937 auto ByteOffset = ConstantOffset->sextOrTrunc(IdxWidth).smul_ov(
938 APInt(IdxWidth, Stride, /*IsSigned=*/true, /*ImplicitTrunc=*/true),
939 Overflow);
940 SignedOverflow |= Overflow;
941 // Removing an offset must not introduce signed overflow. For strides
942 // greater than one, the non-wrapping multiplication of both the
943 // original index and extracted offset already rules out this wrap.
944 if (Stride == 1) {
945 auto *Offset = ConstantInt::get(GEP->getContext(), *ConstantOffset);
946 SignedOverflow |=
947 computeOverflowForSignedSub(GEP->getOperand(I), Offset, *DL) !=
948 OverflowResult::NeverOverflows;
949 }
950 AccumulativeByteOffset =
951 AccumulativeByteOffset.sadd_ov(ByteOffset, Overflow);
952 SignedOverflow |= Overflow;
953 }
954 } else if (LowerGEP) {
955 StructType *StTy = GTI.getStructType();
956 uint64_t Field = cast<ConstantInt>(GEP->getOperand(I))->getZExtValue();
957 // Skip field 0 as the offset is always 0.
958 if (Field != 0) {
959 NeedsExtraction = true;
960 AccumulativeByteOffset +=
961 APInt(IdxWidth, DL->getStructLayout(StTy)->getElementOffset(Field),
962 /*IsSigned=*/true, /*ImplicitTrunc=*/true);
963 }
964 }
965 }
966 return AccumulativeByteOffset;
967}
968
969void SeparateConstOffsetFromGEP::lowerToSingleIndexGEPs(
970 GetElementPtrInst *Variadic, const APInt &AccumulativeByteOffset) {
971 IRBuilder<> Builder(Variadic);
972 Type *PtrIndexTy = DL->getIndexType(Variadic->getType());
973
974 Value *ResultPtr = Variadic->getOperand(0);
975 Loop *L = LI->getLoopFor(Variadic->getParent());
976 // Check if the base is not loop invariant or used more than once.
977 bool isSwapCandidate =
978 L && L->isLoopInvariant(ResultPtr) &&
979 !hasMoreThanOneUseInLoop(ResultPtr, L);
980 Value *FirstResult = nullptr;
981
982 gep_type_iterator GTI = gep_type_begin(*Variadic);
983 // Create an ugly GEP for each sequential index. We don't create GEPs for
984 // structure indices, as they are accumulated in the constant offset index.
985 for (unsigned I = 1, E = Variadic->getNumOperands(); I != E; ++I, ++GTI) {
986 if (GTI.isSequential()) {
987 Value *Idx = Variadic->getOperand(I);
988 // Skip zero indices.
989 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx))
990 if (CI->isZero())
991 continue;
992
993 APInt ElementSize = APInt(PtrIndexTy->getIntegerBitWidth(),
995 // Scale the index by element size.
996 if (ElementSize != 1) {
997 if (ElementSize.isPowerOf2()) {
998 Idx = Builder.CreateShl(
999 Idx, ConstantInt::get(PtrIndexTy, ElementSize.logBase2()));
1000 } else {
1001 Idx =
1002 Builder.CreateMul(Idx, ConstantInt::get(PtrIndexTy, ElementSize));
1003 }
1004 }
1005 // Create an ugly GEP with a single index for each index.
1006 ResultPtr = Builder.CreatePtrAdd(ResultPtr, Idx, "uglygep");
1007 if (FirstResult == nullptr)
1008 FirstResult = ResultPtr;
1009 }
1010 }
1011
1012 // Create a GEP with the constant offset index.
1013 if (AccumulativeByteOffset != 0) {
1014 Value *Offset = ConstantInt::get(PtrIndexTy, AccumulativeByteOffset);
1015 ResultPtr = Builder.CreatePtrAdd(ResultPtr, Offset, "uglygep");
1016 } else
1017 isSwapCandidate = false;
1018
1019 // If we created a GEP with constant index, and the base is loop invariant,
1020 // then we swap the first one with it, so LICM can move constant GEP out
1021 // later.
1022 auto *FirstGEP = dyn_cast_or_null<GetElementPtrInst>(FirstResult);
1023 auto *SecondGEP = dyn_cast<GetElementPtrInst>(ResultPtr);
1024 if (isSwapCandidate && isLegalToSwapOperand(FirstGEP, SecondGEP, L))
1025 swapGEPOperand(FirstGEP, SecondGEP);
1026
1027 Variadic->replaceAllUsesWith(ResultPtr);
1028 Variadic->eraseFromParent();
1029}
1030
1031bool SeparateConstOffsetFromGEP::reorderGEP(GetElementPtrInst *GEP,
1032 TargetTransformInfo &TTI) {
1033 auto PtrGEP = dyn_cast<GetElementPtrInst>(GEP->getPointerOperand());
1034 if (!PtrGEP)
1035 return false;
1036
1037 bool NestedNeedsExtraction, OffsetOverflow;
1038 APInt NestedByteOffset =
1039 accumulateByteOffset(PtrGEP, NestedNeedsExtraction, OffsetOverflow);
1040 if (!NestedNeedsExtraction)
1041 return false;
1042
1043 unsigned AddrSpace = PtrGEP->getPointerAddressSpace();
1044 if (!TTI.isLegalAddressingMode(GEP->getResultElementType(),
1045 /*BaseGV=*/nullptr,
1046 NestedByteOffset.getSExtValue(),
1047 /*HasBaseReg=*/true, /*Scale=*/0, AddrSpace))
1048 return false;
1049
1050 bool GEPInBounds = GEP->isInBounds();
1051 bool PtrGEPInBounds = PtrGEP->isInBounds();
1052 bool IsChainInBounds = GEPInBounds && PtrGEPInBounds;
1053 if (IsChainInBounds) {
1054 auto IsKnownNonNegative = [this](Value *V) {
1055 return isKnownNonNegative(V, *DL);
1056 };
1057 IsChainInBounds &= all_of(GEP->indices(), IsKnownNonNegative);
1058 if (IsChainInBounds)
1059 IsChainInBounds &= all_of(PtrGEP->indices(), IsKnownNonNegative);
1060 }
1061
1062 IRBuilder<> Builder(GEP);
1063 // For trivial GEP chains, we can swap the indices.
1064 Value *NewSrc = Builder.CreateGEP(
1065 GEP->getSourceElementType(), PtrGEP->getPointerOperand(),
1066 SmallVector<Value *, 4>(GEP->indices()), "", IsChainInBounds);
1067 Value *NewGEP = Builder.CreateGEP(PtrGEP->getSourceElementType(), NewSrc,
1068 SmallVector<Value *, 4>(PtrGEP->indices()),
1069 "", IsChainInBounds);
1070 GEP->replaceAllUsesWith(NewGEP);
1072 return true;
1073}
1074
1075bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
1076 // Skip vector GEPs.
1077 if (GEP->getType()->isVectorTy())
1078 return false;
1079
1080 // If the base of this GEP is a ptradd of a constant, lets pass the constant
1081 // along. This ensures that when we have a chain of GEPs the constant
1082 // offset from each is accumulated.
1083 Value *NewBase;
1084 const APInt *BaseOffset;
1085 bool ExtractBase = match(GEP->getPointerOperand(),
1086 m_PtrAdd(m_Value(NewBase), m_APInt(BaseOffset)));
1087
1088 unsigned IdxWidth = DL->getIndexTypeSizeInBits(GEP->getType());
1089 APInt BaseByteOffset =
1090 ExtractBase ? BaseOffset->sextOrTrunc(IdxWidth) : APInt(IdxWidth, 0);
1091
1092 // The backend can already nicely handle the case where all indices are
1093 // constant.
1094 if (GEP->hasAllConstantIndices() && !ExtractBase)
1095 return false;
1096
1097 bool Changed = canonicalizeArrayIndicesToIndexSize(GEP);
1098
1099 bool NeedsExtraction, OffsetOverflow;
1100 APInt NonBaseByteOffset =
1101 accumulateByteOffset(GEP, NeedsExtraction, OffsetOverflow);
1102 bool AddOverflow;
1103 APInt AccumulativeByteOffset =
1104 BaseByteOffset.sadd_ov(NonBaseByteOffset, AddOverflow);
1105 OffsetOverflow |= AddOverflow;
1106
1107 TargetTransformInfo &TTI = GetTTI(*GEP->getFunction());
1108
1109 if (!NeedsExtraction && !ExtractBase) {
1110 Changed |= reorderGEP(GEP, TTI);
1111 return Changed;
1112 }
1113
1114 // If LowerGEP is disabled, before really splitting the GEP, check whether the
1115 // backend supports the addressing mode we are about to produce. If no, this
1116 // splitting probably won't be beneficial.
1117 // If LowerGEP is enabled, even the extracted constant offset can not match
1118 // the addressing mode, we can still do optimizations to other lowered parts
1119 // of variable indices. Therefore, we don't check for addressing modes in that
1120 // case.
1121 if (!LowerGEP) {
1122 unsigned AddrSpace = GEP->getPointerAddressSpace();
1124 GEP->getResultElementType(),
1125 /*BaseGV=*/nullptr, AccumulativeByteOffset.getSExtValue(),
1126 /*HasBaseReg=*/true, /*Scale=*/0, AddrSpace)) {
1127 // If the addressing mode was not legal and the base byte offset was not
1128 // 0, it could be a case where the total offset became too large for
1129 // the addressing mode. Try again without extracting the base offset.
1130 if (!ExtractBase)
1131 return Changed;
1132 ExtractBase = false;
1133 BaseByteOffset = APInt(IdxWidth, 0);
1134 AccumulativeByteOffset = NonBaseByteOffset;
1136 GEP->getResultElementType(),
1137 /*BaseGV=*/nullptr, AccumulativeByteOffset.getSExtValue(),
1138 /*HasBaseReg=*/true, /*Scale=*/0, AddrSpace))
1139 return Changed;
1140 // We can proceed with just extracting the other (non-base) offsets.
1141 NeedsExtraction = true;
1142 }
1143 }
1144
1145 // Track information for preserving GEP flags.
1146 bool AllOffsetsNonNegative =
1147 AccumulativeByteOffset.isNonNegative() && !OffsetOverflow;
1148 bool AllNUWPreserved = GEP->hasNoUnsignedWrap();
1149 bool NewGEPInBounds = GEP->isInBounds();
1150 bool NewGEPNUSW = GEP->hasNoUnsignedSignedWrap();
1151
1152 // Remove the constant offset in each sequential index. The resultant GEP
1153 // computes the variadic base.
1154 // Notice that we don't remove struct field indices here. If LowerGEP is
1155 // disabled, a structure index is not accumulated and we still use the old
1156 // one. If LowerGEP is enabled, a structure index is accumulated in the
1157 // constant offset. LowerToSingleIndexGEPs will later handle the constant
1158 // offset and won't need a new structure index.
1160 for (unsigned I = 1, E = GEP->getNumOperands(); I != E; ++I, ++GTI) {
1161 if (GTI.isSequential()) {
1162 // Constant offsets of scalable types are not really constant.
1163 if (GTI.getIndexedType()->isScalableTy())
1164 continue;
1165
1166 // Splits this GEP index into a variadic part and a constant offset, and
1167 // uses the variadic part as the new index.
1168 Value *Idx = GEP->getOperand(I);
1169 User *UserTreeRoot;
1170 bool PreservesNUW;
1171 Value *NewIdx = ConstantOffsetExtractor::Extract(
1172 GEP->getOperandUse(I), UserTreeRoot, PreservesNUW);
1173 if (NewIdx != nullptr) {
1174 // Switches to the index with the constant offset removed.
1175 GEP->setOperand(I, NewIdx);
1176 // After switching to the new index, we can garbage-collect the
1177 // rewritten tree and the old index if they are not used.
1178 if (auto *I = dyn_cast_or_null<Instruction>(UserTreeRoot))
1180 if (auto *I = dyn_cast<Instruction>(Idx))
1182 Idx = NewIdx;
1183 AllNUWPreserved &= PreservesNUW;
1184 }
1185 AllOffsetsNonNegative =
1186 AllOffsetsNonNegative && isKnownNonNegative(Idx, *DL);
1187 }
1188 }
1189 if (ExtractBase) {
1190 GEPOperator *Base = cast<GEPOperator>(GEP->getPointerOperand());
1191 AllNUWPreserved &= Base->hasNoUnsignedWrap();
1192 NewGEPInBounds &= Base->isInBounds();
1193 NewGEPNUSW &= Base->hasNoUnsignedSignedWrap();
1194 AllOffsetsNonNegative &= BaseByteOffset.isNonNegative();
1195
1196 GEP->setOperand(0, NewBase);
1198 }
1199
1200 // Clear the inbounds attribute because the new index may be off-bound.
1201 // e.g.,
1202 //
1203 // b = add i64 a, 5
1204 // addr = gep inbounds float, float* p, i64 b
1205 //
1206 // is transformed to:
1207 //
1208 // addr2 = gep float, float* p, i64 a ; inbounds removed
1209 // addr = gep float, float* addr2, i64 5 ; inbounds removed
1210 //
1211 // If a is -4, although the old index b is in bounds, the new index a is
1212 // off-bound. http://llvm.org/docs/LangRef.html#id181 says "if the
1213 // inbounds keyword is not present, the offsets are added to the base
1214 // address with silently-wrapping two's complement arithmetic".
1215 // Therefore, the final code will be a semantically equivalent.
1216 GEPNoWrapFlags NewGEPFlags = GEPNoWrapFlags::none();
1217
1218 // If removing and scaling offsets cannot have signed overflow, and all
1219 // remaining indices and accumulated offsets are nonnegative, they can be
1220 // added in any order. An initially inbounds/nusw GEP then guarantees that the
1221 // intermediate results stay in bounds and do not overflow in a nusw sense.
1222 bool CanPreserveInBoundsNUSW = AllOffsetsNonNegative;
1223
1224 // If the initial GEP was NUW and all operations that we reassociate were NUW
1225 // additions, the resulting GEPs are also NUW.
1226 if (AllNUWPreserved) {
1227 NewGEPFlags |= GEPNoWrapFlags::noUnsignedWrap();
1228 // If the initial GEP additionally had NUSW (or inbounds, which implies
1229 // NUSW), we know that the indices in the initial GEP must all have their
1230 // signbit not set. For indices that are the result of NUW adds, the
1231 // add-operands therefore also don't have their signbit set. Therefore, all
1232 // indices of the resulting GEPs are non-negative -> we can preserve
1233 // the inbounds/nusw flag.
1234 CanPreserveInBoundsNUSW |= NewGEPNUSW;
1235 }
1236
1237 if (CanPreserveInBoundsNUSW) {
1238 if (NewGEPInBounds)
1239 NewGEPFlags |= GEPNoWrapFlags::inBounds();
1240 else if (NewGEPNUSW)
1241 NewGEPFlags |= GEPNoWrapFlags::noUnsignedSignedWrap();
1242 }
1243
1244 GEP->setNoWrapFlags(NewGEPFlags);
1245
1246 // Lowers a GEP to GEPs with a single index.
1247 if (LowerGEP) {
1248 lowerToSingleIndexGEPs(GEP, AccumulativeByteOffset);
1249 return true;
1250 }
1251
1252 // No need to create another GEP if the accumulative byte offset is 0.
1253 if (AccumulativeByteOffset == 0)
1254 return true;
1255
1256 // Offsets the base with the accumulative byte offset.
1257 //
1258 // %gep ; the base
1259 // ... %gep ...
1260 //
1261 // => add the offset
1262 //
1263 // %gep2 ; clone of %gep
1264 // %new.gep = gep i8, %gep2, %offset
1265 // %gep ; will be removed
1266 // ... %gep ...
1267 //
1268 // => replace all uses of %gep with %new.gep and remove %gep
1269 //
1270 // %gep2 ; clone of %gep
1271 // %new.gep = gep i8, %gep2, %offset
1272 // ... %new.gep ...
1273 Instruction *NewGEP = GEP->clone();
1274 NewGEP->insertBefore(GEP->getIterator());
1275
1276 Type *PtrIdxTy = DL->getIndexType(GEP->getType());
1277 IRBuilder<> Builder(GEP);
1278 NewGEP = cast<Instruction>(Builder.CreatePtrAdd(
1279 NewGEP, ConstantInt::get(PtrIdxTy, AccumulativeByteOffset),
1280 GEP->getName(), NewGEPFlags));
1281 NewGEP->copyMetadata(*GEP);
1282
1283 GEP->replaceAllUsesWith(NewGEP);
1284 GEP->eraseFromParent();
1285
1286 return true;
1287}
1288
1289bool SeparateConstOffsetFromGEPLegacyPass::runOnFunction(Function &F) {
1290 if (skipFunction(F))
1291 return false;
1292 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1293 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1294 auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
1295 auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
1296 return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
1297 };
1298 SeparateConstOffsetFromGEP Impl(DT, LI, TLI, GetTTI, LowerGEP);
1299 return Impl.run(F);
1300}
1301
1302bool SeparateConstOffsetFromGEP::run(Function &F) {
1303 const ScalarOptions &Opts =
1304 F.getContext().getLibraryOptions().get<ScalarOptions>();
1305 if (Opts.disable_separate_const_offset_from_gep)
1306 return false;
1307
1308 DL = &F.getDataLayout();
1309 bool Changed = false;
1310
1311 ReversePostOrderTraversal<Function *> RPOT(&F);
1312 for (BasicBlock *B : RPOT) {
1313 if (!DT->isReachableFromEntry(B))
1314 continue;
1315
1316 for (Instruction &I : llvm::make_early_inc_range(*B))
1317 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I))
1318 Changed |= splitGEP(GEP);
1319 // No need to split GEP ConstantExprs because all its indices are constant
1320 // already.
1321 }
1322
1323 Changed |= reuniteExts(F);
1324
1325 if (Opts.reassociate_geps_verify_no_dead_code)
1326 verifyNoDeadCode(F);
1327
1328 return Changed;
1329}
1330
1331Instruction *SeparateConstOffsetFromGEP::findClosestMatchingDominator(
1332 ExprKey Key, Instruction *Dominatee,
1333 DenseMap<ExprKey, SmallVector<Instruction *, 2>> &DominatingExprs) {
1334 auto Pos = DominatingExprs.find(Key);
1335 if (Pos == DominatingExprs.end())
1336 return nullptr;
1337
1338 auto &Candidates = Pos->second;
1339 // Because we process the basic blocks in pre-order of the dominator tree, a
1340 // candidate that doesn't dominate the current instruction won't dominate any
1341 // future instruction either. Therefore, we pop it out of the stack. This
1342 // optimization makes the algorithm O(n).
1343 while (!Candidates.empty()) {
1344 Instruction *Candidate = Candidates.back();
1345 if (DT->dominates(Candidate, Dominatee))
1346 return Candidate;
1347 Candidates.pop_back();
1348 }
1349 return nullptr;
1350}
1351
1352bool SeparateConstOffsetFromGEP::reuniteExts(Instruction *I) {
1353 if (!I->getType()->isIntOrIntVectorTy())
1354 return false;
1355
1356 // Dom: LHS+RHS
1357 // I: sext(LHS)+sext(RHS)
1358 // If Dom can't sign overflow and Dom dominates I, optimize I to sext(Dom).
1359 // TODO: handle zext
1360 Value *LHS = nullptr, *RHS = nullptr;
1361 if (match(I, m_Add(m_SExt(m_Value(LHS)), m_SExt(m_Value(RHS))))) {
1362 if (LHS->getType() == RHS->getType()) {
1363 ExprKey Key = createNormalizedCommutablePair(LHS, RHS);
1364 if (auto *Dom = findClosestMatchingDominator(Key, I, DominatingAdds)) {
1365 Instruction *NewSExt =
1366 new SExtInst(Dom, I->getType(), "", I->getIterator());
1367 NewSExt->takeName(I);
1368 I->replaceAllUsesWith(NewSExt);
1369 NewSExt->setDebugLoc(I->getDebugLoc());
1371 return true;
1372 }
1373 }
1374 } else if (match(I, m_Sub(m_SExt(m_Value(LHS)), m_SExt(m_Value(RHS))))) {
1375 if (LHS->getType() == RHS->getType()) {
1376 if (auto *Dom =
1377 findClosestMatchingDominator({LHS, RHS}, I, DominatingSubs)) {
1378 Instruction *NewSExt =
1379 new SExtInst(Dom, I->getType(), "", I->getIterator());
1380 NewSExt->takeName(I);
1381 I->replaceAllUsesWith(NewSExt);
1382 NewSExt->setDebugLoc(I->getDebugLoc());
1384 return true;
1385 }
1386 }
1387 }
1388
1389 // Add I to DominatingExprs if it's an add/sub that can't sign overflow.
1390 if (match(I, m_NSWAdd(m_Value(LHS), m_Value(RHS)))) {
1392 ExprKey Key = createNormalizedCommutablePair(LHS, RHS);
1393 DominatingAdds[Key].push_back(I);
1394 }
1395 } else if (match(I, m_NSWSub(m_Value(LHS), m_Value(RHS)))) {
1397 DominatingSubs[{LHS, RHS}].push_back(I);
1398 }
1399 return false;
1400}
1401
1402bool SeparateConstOffsetFromGEP::reuniteExts(Function &F) {
1403 bool Changed = false;
1404 DominatingAdds.clear();
1405 DominatingSubs.clear();
1406 for (const auto Node : depth_first(DT)) {
1407 BasicBlock *BB = Node->getBlock();
1408 for (Instruction &I : llvm::make_early_inc_range(*BB))
1409 Changed |= reuniteExts(&I);
1410 }
1411 return Changed;
1412}
1413
1414void SeparateConstOffsetFromGEP::verifyNoDeadCode(Function &F) {
1415 for (BasicBlock &B : F) {
1416 for (Instruction &I : B) {
1418 std::string ErrMessage;
1419 raw_string_ostream RSO(ErrMessage);
1420 RSO << "Dead instruction detected!\n" << I << "\n";
1421 llvm_unreachable(RSO.str().c_str());
1422 }
1423 }
1424 }
1425}
1426
1427bool SeparateConstOffsetFromGEP::isLegalToSwapOperand(
1428 GetElementPtrInst *FirstGEP, GetElementPtrInst *SecondGEP, Loop *CurLoop) {
1429 if (!FirstGEP || !FirstGEP->hasOneUse())
1430 return false;
1431
1432 if (!SecondGEP || FirstGEP->getParent() != SecondGEP->getParent())
1433 return false;
1434
1435 if (FirstGEP == SecondGEP)
1436 return false;
1437
1438 unsigned FirstNum = FirstGEP->getNumOperands();
1439 unsigned SecondNum = SecondGEP->getNumOperands();
1440 // Give up if the number of operands are not 2.
1441 if (FirstNum != SecondNum || FirstNum != 2)
1442 return false;
1443
1444 Value *FirstBase = FirstGEP->getOperand(0);
1445 Value *SecondBase = SecondGEP->getOperand(0);
1446 Value *FirstOffset = FirstGEP->getOperand(1);
1447 // Give up if the index of the first GEP is loop invariant.
1448 if (CurLoop->isLoopInvariant(FirstOffset))
1449 return false;
1450
1451 // Give up if base doesn't have same type.
1452 if (FirstBase->getType() != SecondBase->getType())
1453 return false;
1454
1455 Instruction *FirstOffsetDef = dyn_cast<Instruction>(FirstOffset);
1456
1457 // Check if the second operand of first GEP has constant coefficient.
1458 // For an example, for the following code, we won't gain anything by
1459 // hoisting the second GEP out because the second GEP can be folded away.
1460 // %scevgep.sum.ur159 = add i64 %idxprom48.ur, 256
1461 // %67 = shl i64 %scevgep.sum.ur159, 2
1462 // %uglygep160 = getelementptr i8* %65, i64 %67
1463 // %uglygep161 = getelementptr i8* %uglygep160, i64 -1024
1464
1465 // Skip constant shift instruction which may be generated by Splitting GEPs.
1466 if (FirstOffsetDef && FirstOffsetDef->isShift() &&
1467 isa<ConstantInt>(FirstOffsetDef->getOperand(1)))
1468 FirstOffsetDef = dyn_cast<Instruction>(FirstOffsetDef->getOperand(0));
1469
1470 // Give up if FirstOffsetDef is an Add or Sub with constant.
1471 // Because it may not profitable at all due to constant folding.
1472 if (FirstOffsetDef)
1473 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FirstOffsetDef)) {
1474 unsigned opc = BO->getOpcode();
1475 if ((opc == Instruction::Add || opc == Instruction::Sub) &&
1476 (isa<ConstantInt>(BO->getOperand(0)) ||
1478 return false;
1479 }
1480 return true;
1481}
1482
1483bool SeparateConstOffsetFromGEP::hasMoreThanOneUseInLoop(Value *V, Loop *L) {
1484 // TODO: Could look at uses of globals, but we need to make sure we are
1485 // looking at the correct function.
1486 if (isa<Constant>(V))
1487 return false;
1488
1489 int UsesInLoop = 0;
1490 for (User *U : V->users()) {
1491 if (Instruction *User = dyn_cast<Instruction>(U))
1492 if (L->contains(User))
1493 if (++UsesInLoop > 1)
1494 return true;
1495 }
1496 return false;
1497}
1498
1499void SeparateConstOffsetFromGEP::swapGEPOperand(GetElementPtrInst *First,
1500 GetElementPtrInst *Second) {
1501 Value *Offset1 = First->getOperand(1);
1502 Value *Offset2 = Second->getOperand(1);
1503 First->setOperand(1, Offset2);
1504 Second->setOperand(1, Offset1);
1505
1506 // After changing (p+o)+c to (p+c)+o, the inner GEP may not be inbounds
1507 // anymore.
1508 const DataLayout &DAL = First->getDataLayout();
1509 unsigned IdxBits = DAL.getIndexSizeInBits(
1510 cast<PointerType>(First->getType())->getAddressSpace());
1511
1512 auto ClearNoWrapFlags = [&] {
1513 // TODO(gep_nowrap): Make flag preservation more precise.
1514 First->setNoWrapFlags(GEPNoWrapFlags::none());
1516 };
1517
1518 APInt FirstOffset(IdxBits, 0);
1519 if (!First->accumulateConstantOffset(DAL, FirstOffset)) {
1520 ClearNoWrapFlags();
1521 return;
1522 }
1523
1524 APInt BaseOffset(IdxBits, 0);
1525 Value *NewBase =
1527 DAL, BaseOffset);
1528
1529 bool Overflow = false;
1530 APInt TotalOffset = BaseOffset.uadd_ov(FirstOffset, Overflow);
1531 uint64_t ObjectSize;
1532 if (Overflow || !getObjectSize(NewBase, ObjectSize, DAL, TLI) ||
1533 TotalOffset.ugt(ObjectSize)) {
1534 ClearNoWrapFlags();
1535 return;
1536 }
1537
1538 First->setIsInBounds(true);
1539}
1540
1542 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
1543 static_cast<PassInfoMixin<SeparateConstOffsetFromGEPPass> *>(this)
1544 ->printPipeline(OS, MapClassName2PassName);
1545 OS << '<';
1546 if (LowerGEP)
1547 OS << "lower-gep";
1548 OS << '>';
1549}
1550
1553 auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
1554 auto *LI = &AM.getResult<LoopAnalysis>(F);
1555 auto *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
1556 auto GetTTI = [&AM](Function &F) -> TargetTransformInfo & {
1557 return AM.getResult<TargetIRAnalysis>(F);
1558 };
1559 SeparateConstOffsetFromGEP Impl(DT, LI, TLI, GetTTI, LowerGEP);
1560 if (!Impl.run(F))
1561 return PreservedAnalyses::all();
1564 return PA;
1565}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static bool runOnFunction(Function &F, bool PostInlining)
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
static const T * Find(StringRef S, ArrayRef< T > A)
Find KV in array using binary search.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
OptimizedStructLayoutField Field
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
static bool allowsPreservingNUW(const User *U)
Check if reassociating through a tree node would invalidate GEP's nuw flag.
static bool canReorderAddSextToGEP(const Use *Idx, const BinaryOperator *Add, const DataLayout &DL)
static BasicBlock::iterator getIndexInsertionPoint(const Use &Idx)
This file defines the SmallVector class.
This pass exposes codegen information to IR-level passes.
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1057
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition APInt.cpp:1078
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1186
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:376
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1966
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1973
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:215
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1086
unsigned logBase2() const
Definition APInt.h:1781
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:330
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:436
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
Definition APInt.h:337
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1582
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:278
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
BinaryOps getOpcode() const
Definition InstrTypes.h:409
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
static LLVM_ABI CastInst * CreateIntegerCast(Value *S, Type *Ty, bool isSigned, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Create a ZExt, BitCast, or Trunc for int -> int casts.
This is the shared class of boolean and integer constants.
Definition Constants.h:87
bool isNegative() const
Definition Constants.h:214
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
unsigned getIndexSizeInBits(unsigned AS) const
The size in bits of indices used for address calculation in getelementptr and for addresses in the gi...
Definition DataLayout.h:509
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:277
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
static GEPNoWrapFlags inBounds()
static GEPNoWrapFlags noUnsignedWrap()
static GEPNoWrapFlags noUnsignedSignedWrap()
static GEPNoWrapFlags none()
LLVM_ABI void setNoWrapFlags(GEPNoWrapFlags NW)
Set nowrap flags for GEP instruction.
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
bool isShift() const
LLVM_ABI void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:594
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:619
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition LoopInfo.cpp:67
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale, unsigned AddrSpace=0, Instruction *I=nullptr, int64_t ScalableOffset=0) const
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
This class represents a truncation of integer types.
LLVM_ABI unsigned getIntegerBitWidth() const
LLVM_ABI bool isScalableTy() const
Return true if this is a type whose size is a known multiple of vscale.
Definition Type.cpp:61
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
Use * op_iterator
Definition User.h:254
void setOperand(unsigned i, Value *Val)
Definition User.h:212
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:729
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:441
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:396
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
PtrAdd_match< PointerOpTy, OffsetOpTy > m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
Matches GEP with i8 source element type.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
auto m_Value()
Match an arbitrary value and ignore it.
is_zero< true > m_Zero()
Match any null constant or a vector with all elements equal to 0.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
void apply(Opt *O, const Mod &M, const Mods &... Ms)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
@ User
could "use" a pointer
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
@ NeverOverflows
Never overflows.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1781
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition Local.cpp:520
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
constexpr std::enable_if_t< std::is_signed_v< T >, std::pair< T, bool > > AddOverflow(T X, T Y)
Add two signed integers, computing the two's complement truncated result, returning a pair {result,...
Definition MathExtras.h:698
LLVM_ABI void initializeSeparateConstOffsetFromGEPLegacyPassPass(PassRegistry &)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:406
LLVM_ABI bool getObjectSize(const Value *Ptr, uint64_t &Size, const DataLayout &DL, const TargetLibraryInfo *TLI, ObjectSizeOpts Opts={})
Compute the size of the object pointed by Ptr.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
IRBuilder(LLVMContext &, FolderTy, InserterTy) -> IRBuilder< FolderTy, InserterTy >
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI FunctionPass * createSeparateConstOffsetFromGEPPass(bool LowerGEP=false)
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
TargetTransformInfo TTI
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
@ Add
Sum of integers.
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
gep_type_iterator gep_type_begin(const User *GEP)
iterator_range< df_iterator< T > > depth_first(const T &G)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
#define N