@@ -834,11 +834,9 @@ struct ImportIRInstId : public IdBase<ImportIRInstId> {
834
834
static constexpr llvm::StringLiteral Label = " import_ir_inst" ;
835
835
using ValueType = ImportIRInst;
836
836
837
- // ImportIRInstId is restricted so that it can fit into LocId.
838
- static constexpr int BitsWithNodeId = 30 ;
839
-
840
- // The maximum ID, non-inclusive.
841
- static constexpr int Max = (1 << BitsWithNodeId) - Parse::NodeId::Max - 2 ;
837
+ // The maximum ID, non-inclusive. This is constrained to fit inside LocId.
838
+ static constexpr int Max =
839
+ -(std::numeric_limits<int32_t >::min() + 2 * Parse::NodeId::Max + 1 );
842
840
843
841
constexpr explicit ImportIRInstId (int32_t index) : IdBase(index) {
844
842
CARBON_DCHECK (index < Max, " Index out of range: {0}" , index);
@@ -852,16 +850,15 @@ struct ImportIRInstId : public IdBase<ImportIRInstId> {
852
850
//
853
851
// The structure is:
854
852
// - None: The standard NoneIndex for all Id types, -1.
855
- // - InstId: positive values including zero; a full 31 bits.
853
+ // - InstId: Positive values including zero; a full 31 bits.
856
854
// - [0, 1 << 31)
857
- // - NodeId: negative values starting after None; the 24 bit NodeId range.
855
+ // - NodeId: Negative values starting after None; the 24 bit NodeId range.
858
856
// - [-2, -2 - (1 << 24))
859
- // - ImportIRInstId: remaining negative values; after NodeId, fills out negative
860
- // values to 30 bits.
861
- // - [-2 - (1 << 24), -(1 << 30))
862
- //
863
- // In addition, one bit is used for flags: `DesugaredBit`.
864
- // Note that this can only be used with negative, non-`InstId` values.
857
+ // - Desugared NodeId: Another 24 bit NodeId range.
858
+ // - [-2 - (1 << 24), -2 - (1 << 25))
859
+ // - ImportIRInstId: Remaining negative values; after NodeId, fills out negative
860
+ // values.
861
+ // - [-2 - (1 << 25), -(1 << 31)]
865
862
//
866
863
// For desugaring, use `InstStore::GetLocIdForDesugaring()`.
867
864
struct LocId : public IdBase <LocId> {
@@ -897,10 +894,9 @@ struct LocId : public IdBase<LocId> {
897
894
auto AsDesugared () const -> LocId {
898
895
// This should only be called for NodeId or ImportIRInstId (i.e. canonical
899
896
// locations), but we only set the flag for NodeId.
900
- CARBON_CHECK (kind () != Kind::InstId,
901
- " Use InstStore::GetLocIdForDesugaring" );
902
- if (kind () == Kind::NodeId) {
903
- return LocId (index & ~DesugaredBit);
897
+ CARBON_CHECK (kind () != Kind::InstId, " Use InstStore::GetDesugaredLocId" );
898
+ if (index <= FirstNodeId && index > FirstDesugaredNodeId) {
899
+ return LocId (index - Parse::NodeId::Max);
904
900
}
905
901
return *this ;
906
902
}
@@ -913,7 +909,7 @@ struct LocId : public IdBase<LocId> {
913
909
if (index >= 0 ) {
914
910
return Kind::InstId;
915
911
}
916
- if (index_without_flags () <= FirstImportIRInstId) {
912
+ if (index <= FirstImportIRInstId) {
917
913
return Kind::ImportIRInstId;
918
914
}
919
915
return Kind::NodeId;
@@ -922,7 +918,7 @@ struct LocId : public IdBase<LocId> {
922
918
// Returns true if the location corresponds to desugared instructions.
923
919
// Requires a non-`InstId` location.
924
920
auto is_desugared () const -> bool {
925
- return ( kind () == Kind::NodeId) && ( index & DesugaredBit) == 0 ;
921
+ return index <= FirstDesugaredNodeId && index > FirstImportIRInstId ;
926
922
}
927
923
928
924
// Returns the equivalent `ImportIRInstId` when `kind()` matches or is `None`.
@@ -934,7 +930,7 @@ struct LocId : public IdBase<LocId> {
934
930
return ImportIRInstId::None;
935
931
}
936
932
CARBON_CHECK (kind () == Kind::ImportIRInstId, " {0}" , index);
937
- return ImportIRInstId (FirstImportIRInstId - index_without_flags () );
933
+ return ImportIRInstId (FirstImportIRInstId - index );
938
934
}
939
935
940
936
// Returns the equivalent `InstId` when `kind()` matches or is `None`.
@@ -949,25 +945,22 @@ struct LocId : public IdBase<LocId> {
949
945
return Parse::NodeId::None;
950
946
}
951
947
CARBON_CHECK (kind () == Kind::NodeId, " {0}" , index);
952
- return Parse::NodeId (FirstNodeId - index_without_flags ());
948
+ if (index <= FirstDesugaredNodeId) {
949
+ return Parse::NodeId (FirstDesugaredNodeId - index);
950
+ } else {
951
+ return Parse::NodeId (FirstNodeId - index);
952
+ }
953
953
}
954
954
955
955
auto Print (llvm::raw_ostream& out) const -> void;
956
956
957
957
private:
958
- // Whether a location corresponds to desugared instructions. This only applies
959
- // for `NodeId`.
960
- static constexpr int32_t DesugaredBit = 1 << 30 ;
961
-
962
958
// The value of the 0 index for each of `NodeId` and `ImportIRInstId`.
963
959
static constexpr int32_t FirstNodeId = NoneIndex - 1 ;
964
- static constexpr int32_t FirstImportIRInstId =
960
+ static constexpr int32_t FirstDesugaredNodeId =
965
961
FirstNodeId - Parse::NodeId::Max;
966
-
967
- auto index_without_flags () const -> int32_t {
968
- CARBON_DCHECK (index < NoneIndex, " Only for NodeId and ImportIRInstId" );
969
- return index | DesugaredBit;
970
- }
962
+ static constexpr int32_t FirstImportIRInstId =
963
+ FirstDesugaredNodeId - Parse::NodeId::Max;
971
964
};
972
965
973
966
// Polymorphic id for fields in `Any[...]` typed instruction category. Used for
0 commit comments