@@ -834,11 +834,9 @@ struct ImportIRInstId : public IdBase<ImportIRInstId> {
834834 static constexpr llvm::StringLiteral Label = " import_ir_inst" ;
835835 using ValueType = ImportIRInst;
836836
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 );
842840
843841 constexpr explicit ImportIRInstId (int32_t index) : IdBase(index) {
844842 CARBON_DCHECK (index < Max, " Index out of range: {0}" , index);
@@ -852,16 +850,15 @@ struct ImportIRInstId : public IdBase<ImportIRInstId> {
852850//
853851// The structure is:
854852// - 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.
856854// - [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.
858856// - [-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)]
865862//
866863// For desugaring, use `InstStore::GetLocIdForDesugaring()`.
867864struct LocId : public IdBase <LocId> {
@@ -897,10 +894,9 @@ struct LocId : public IdBase<LocId> {
897894 auto AsDesugared () const -> LocId {
898895 // This should only be called for NodeId or ImportIRInstId (i.e. canonical
899896 // 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);
904900 }
905901 return *this ;
906902 }
@@ -913,7 +909,7 @@ struct LocId : public IdBase<LocId> {
913909 if (index >= 0 ) {
914910 return Kind::InstId;
915911 }
916- if (index_without_flags () <= FirstImportIRInstId) {
912+ if (index <= FirstImportIRInstId) {
917913 return Kind::ImportIRInstId;
918914 }
919915 return Kind::NodeId;
@@ -922,7 +918,7 @@ struct LocId : public IdBase<LocId> {
922918 // Returns true if the location corresponds to desugared instructions.
923919 // Requires a non-`InstId` location.
924920 auto is_desugared () const -> bool {
925- return ( kind () == Kind::NodeId) && ( index & DesugaredBit) == 0 ;
921+ return index <= FirstDesugaredNodeId && index > FirstImportIRInstId ;
926922 }
927923
928924 // Returns the equivalent `ImportIRInstId` when `kind()` matches or is `None`.
@@ -934,7 +930,7 @@ struct LocId : public IdBase<LocId> {
934930 return ImportIRInstId::None;
935931 }
936932 CARBON_CHECK (kind () == Kind::ImportIRInstId, " {0}" , index);
937- return ImportIRInstId (FirstImportIRInstId - index_without_flags () );
933+ return ImportIRInstId (FirstImportIRInstId - index );
938934 }
939935
940936 // Returns the equivalent `InstId` when `kind()` matches or is `None`.
@@ -949,25 +945,22 @@ struct LocId : public IdBase<LocId> {
949945 return Parse::NodeId::None;
950946 }
951947 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+ }
953953 }
954954
955955 auto Print (llvm::raw_ostream& out) const -> void;
956956
957957 private:
958- // Whether a location corresponds to desugared instructions. This only applies
959- // for `NodeId`.
960- static constexpr int32_t DesugaredBit = 1 << 30 ;
961-
962958 // The value of the 0 index for each of `NodeId` and `ImportIRInstId`.
963959 static constexpr int32_t FirstNodeId = NoneIndex - 1 ;
964- static constexpr int32_t FirstImportIRInstId =
960+ static constexpr int32_t FirstDesugaredNodeId =
965961 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;
971964};
972965
973966// Polymorphic id for fields in `Any[...]` typed instruction category. Used for
0 commit comments