forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IR] Add getelementptr nusw and nuw flags (llvm#90824)
This implements the `nusw` and `nuw` flags for `getelementptr` as proposed at https://discourse.llvm.org/t/rfc-add-nusw-and-nuw-flags-for-getelementptr/78672. The three possible flags are encapsulated in the new `GEPNoWrapFlags` class. Currently this class has a ctor from bool, interpreted as the InBounds flag. This ctor should be removed in the future, as code gets migrated to handle all flags. There are a few places annotated with `TODO(gep_nowrap)`, where I've had to touch code but opted to not infer or precisely preserve the new flags, so as to keep this as NFC as possible and make sure any changes of that kind get test coverage when they are made.
- Loading branch information
Showing
28 changed files
with
523 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,7 @@ enum Kind { | |
kw_fast, | ||
kw_nuw, | ||
kw_nsw, | ||
kw_nusw, | ||
kw_exact, | ||
kw_disjoint, | ||
kw_inbounds, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//===-- llvm/GEPNoWrapFlags.h - NoWrap flags for GEPs -----------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file defines the nowrap flags for getelementptr operators. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_IR_GEPNOWRAPFLAGS_H | ||
#define LLVM_IR_GEPNOWRAPFLAGS_H | ||
|
||
namespace llvm { | ||
|
||
/// Represents flags for the getelementptr instruction/expression. | ||
/// The following flags are supported: | ||
/// * inbounds (implies nusw) | ||
/// * nusw (no unsigned signed wrap) | ||
/// * nuw (no unsigned wrap) | ||
/// See LangRef for a description of their semantics. | ||
class GEPNoWrapFlags { | ||
enum : unsigned { | ||
InBoundsFlag = (1 << 0), | ||
NUSWFlag = (1 << 1), | ||
NUWFlag = (1 << 2), | ||
}; | ||
|
||
unsigned Flags; | ||
GEPNoWrapFlags(unsigned Flags) : Flags(Flags) { | ||
assert((!isInBounds() || hasNoUnsignedSignedWrap()) && | ||
"inbounds implies nusw"); | ||
} | ||
|
||
public: | ||
GEPNoWrapFlags() : Flags(0) {} | ||
// For historical reasons, interpret plain boolean as InBounds. | ||
// TODO: Migrate users to pass explicit GEPNoWrapFlags and remove this ctor. | ||
GEPNoWrapFlags(bool IsInBounds) | ||
: Flags(IsInBounds ? (InBoundsFlag | NUSWFlag) : 0) {} | ||
|
||
static GEPNoWrapFlags none() { return GEPNoWrapFlags(); } | ||
static GEPNoWrapFlags inBounds() { | ||
return GEPNoWrapFlags(InBoundsFlag | NUSWFlag); | ||
} | ||
static GEPNoWrapFlags noUnsignedSignedWrap() { | ||
return GEPNoWrapFlags(NUSWFlag); | ||
} | ||
static GEPNoWrapFlags noUnsignedWrap() { return GEPNoWrapFlags(NUWFlag); } | ||
|
||
static GEPNoWrapFlags fromRaw(unsigned Flags) { | ||
return GEPNoWrapFlags(Flags); | ||
} | ||
unsigned getRaw() const { return Flags; } | ||
|
||
bool isInBounds() const { return Flags & InBoundsFlag; } | ||
bool hasNoUnsignedSignedWrap() const { return Flags & NUSWFlag; } | ||
bool hasNoUnsignedWrap() const { return Flags & NUWFlag; } | ||
|
||
GEPNoWrapFlags withoutInBounds() const { | ||
return GEPNoWrapFlags(Flags & ~InBoundsFlag); | ||
} | ||
GEPNoWrapFlags withoutNoUnsignedSignedWrap() const { | ||
return GEPNoWrapFlags(Flags & ~(InBoundsFlag | NUSWFlag)); | ||
} | ||
GEPNoWrapFlags withoutNoUnsignedWrap() const { | ||
return GEPNoWrapFlags(Flags & ~NUWFlag); | ||
} | ||
|
||
bool operator==(GEPNoWrapFlags Other) const { return Flags == Other.Flags; } | ||
bool operator!=(GEPNoWrapFlags Other) const { return !(*this == Other); } | ||
|
||
GEPNoWrapFlags operator&(GEPNoWrapFlags Other) const { | ||
return GEPNoWrapFlags(Flags & Other.Flags); | ||
} | ||
GEPNoWrapFlags operator|(GEPNoWrapFlags Other) const { | ||
return GEPNoWrapFlags(Flags | Other.Flags); | ||
} | ||
GEPNoWrapFlags &operator&=(GEPNoWrapFlags Other) { | ||
Flags &= Other.Flags; | ||
return *this; | ||
} | ||
GEPNoWrapFlags &operator|=(GEPNoWrapFlags Other) { | ||
Flags |= Other.Flags; | ||
return *this; | ||
} | ||
}; | ||
|
||
} // end namespace llvm | ||
|
||
#endif // LLVM_IR_GEPNOWRAPFLAGS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.