@@ -565,7 +565,7 @@ class Constant : public sandboxir::User {
565
565
};
566
566
567
567
class ConstantInt : public Constant {
568
- ConstantInt (llvm::ConstantInt *C, sandboxir:: Context &Ctx)
568
+ ConstantInt (llvm::ConstantInt *C, Context &Ctx)
569
569
: Constant(ClassID::ConstantInt, C, Ctx) {}
570
570
friend class Context ; // For constructor.
571
571
@@ -574,11 +574,164 @@ class ConstantInt : public Constant {
574
574
}
575
575
576
576
public:
577
+ static ConstantInt *getTrue (Context &Ctx);
578
+ static ConstantInt *getFalse (Context &Ctx);
579
+ static ConstantInt *getBool (Context &Ctx, bool V);
580
+ static Constant *getTrue (Type *Ty);
581
+ static Constant *getFalse (Type *Ty);
582
+ static Constant *getBool (Type *Ty, bool V);
583
+
577
584
// / If Ty is a vector type, return a Constant with a splat of the given
578
585
// / value. Otherwise return a ConstantInt for the given value.
579
586
static ConstantInt *get (Type *Ty, uint64_t V, bool IsSigned = false );
580
587
581
- // TODO: Implement missing functions.
588
+ // / Return a ConstantInt with the specified integer value for the specified
589
+ // / type. If the type is wider than 64 bits, the value will be zero-extended
590
+ // / to fit the type, unless IsSigned is true, in which case the value will
591
+ // / be interpreted as a 64-bit signed integer and sign-extended to fit
592
+ // / the type.
593
+ // / Get a ConstantInt for a specific value.
594
+ static ConstantInt *get (IntegerType *Ty, uint64_t V, bool IsSigned = false );
595
+
596
+ // / Return a ConstantInt with the specified value for the specified type. The
597
+ // / value V will be canonicalized to a an unsigned APInt. Accessing it with
598
+ // / either getSExtValue() or getZExtValue() will yield a correctly sized and
599
+ // / signed value for the type Ty.
600
+ // / Get a ConstantInt for a specific signed value.
601
+ static ConstantInt *getSigned (IntegerType *Ty, int64_t V);
602
+ static Constant *getSigned (Type *Ty, int64_t V);
603
+
604
+ // / Return a ConstantInt with the specified value and an implied Type. The
605
+ // / type is the integer type that corresponds to the bit width of the value.
606
+ static ConstantInt *get (Context &Ctx, const APInt &V);
607
+
608
+ // / Return a ConstantInt constructed from the string strStart with the given
609
+ // / radix.
610
+ static ConstantInt *get (IntegerType *Ty, StringRef Str, uint8_t Radix);
611
+
612
+ // / If Ty is a vector type, return a Constant with a splat of the given
613
+ // / value. Otherwise return a ConstantInt for the given value.
614
+ static Constant *get (Type *Ty, const APInt &V);
615
+
616
+ // / Return the constant as an APInt value reference. This allows clients to
617
+ // / obtain a full-precision copy of the value.
618
+ // / Return the constant's value.
619
+ inline const APInt &getValue () const {
620
+ return cast<llvm::ConstantInt>(Val)->getValue ();
621
+ }
622
+
623
+ // / getBitWidth - Return the scalar bitwidth of this constant.
624
+ unsigned getBitWidth () const {
625
+ return cast<llvm::ConstantInt>(Val)->getBitWidth ();
626
+ }
627
+ // / Return the constant as a 64-bit unsigned integer value after it
628
+ // / has been zero extended as appropriate for the type of this constant. Note
629
+ // / that this method can assert if the value does not fit in 64 bits.
630
+ // / Return the zero extended value.
631
+ inline uint64_t getZExtValue () const {
632
+ return cast<llvm::ConstantInt>(Val)->getZExtValue ();
633
+ }
634
+
635
+ // / Return the constant as a 64-bit integer value after it has been sign
636
+ // / extended as appropriate for the type of this constant. Note that
637
+ // / this method can assert if the value does not fit in 64 bits.
638
+ // / Return the sign extended value.
639
+ inline int64_t getSExtValue () const {
640
+ return cast<llvm::ConstantInt>(Val)->getSExtValue ();
641
+ }
642
+
643
+ // / Return the constant as an llvm::MaybeAlign.
644
+ // / Note that this method can assert if the value does not fit in 64 bits or
645
+ // / is not a power of two.
646
+ inline MaybeAlign getMaybeAlignValue () const {
647
+ return cast<llvm::ConstantInt>(Val)->getMaybeAlignValue ();
648
+ }
649
+
650
+ // / Return the constant as an llvm::Align, interpreting `0` as `Align(1)`.
651
+ // / Note that this method can assert if the value does not fit in 64 bits or
652
+ // / is not a power of two.
653
+ inline Align getAlignValue () const {
654
+ return cast<llvm::ConstantInt>(Val)->getAlignValue ();
655
+ }
656
+
657
+ // / A helper method that can be used to determine if the constant contained
658
+ // / within is equal to a constant. This only works for very small values,
659
+ // / because this is all that can be represented with all types.
660
+ // / Determine if this constant's value is same as an unsigned char.
661
+ bool equalsInt (uint64_t V) const {
662
+ return cast<llvm::ConstantInt>(Val)->equalsInt (V);
663
+ }
664
+
665
+ // / Variant of the getType() method to always return an IntegerType, which
666
+ // / reduces the amount of casting needed in parts of the compiler.
667
+ IntegerType *getIntegerType () const ;
668
+
669
+ // / This static method returns true if the type Ty is big enough to
670
+ // / represent the value V. This can be used to avoid having the get method
671
+ // / assert when V is larger than Ty can represent. Note that there are two
672
+ // / versions of this method, one for unsigned and one for signed integers.
673
+ // / Although ConstantInt canonicalizes everything to an unsigned integer,
674
+ // / the signed version avoids callers having to convert a signed quantity
675
+ // / to the appropriate unsigned type before calling the method.
676
+ // / @returns true if V is a valid value for type Ty
677
+ // / Determine if the value is in range for the given type.
678
+ static bool isValueValidForType (Type *Ty, uint64_t V);
679
+ static bool isValueValidForType (Type *Ty, int64_t V);
680
+
681
+ bool isNegative () const { return cast<llvm::ConstantInt>(Val)->isNegative (); }
682
+
683
+ // / This is just a convenience method to make client code smaller for a
684
+ // / common code. It also correctly performs the comparison without the
685
+ // / potential for an assertion from getZExtValue().
686
+ bool isZero () const { return cast<llvm::ConstantInt>(Val)->isZero (); }
687
+
688
+ // / This is just a convenience method to make client code smaller for a
689
+ // / common case. It also correctly performs the comparison without the
690
+ // / potential for an assertion from getZExtValue().
691
+ // / Determine if the value is one.
692
+ bool isOne () const { return cast<llvm::ConstantInt>(Val)->isOne (); }
693
+
694
+ // / This function will return true iff every bit in this constant is set
695
+ // / to true.
696
+ // / @returns true iff this constant's bits are all set to true.
697
+ // / Determine if the value is all ones.
698
+ bool isMinusOne () const { return cast<llvm::ConstantInt>(Val)->isMinusOne (); }
699
+
700
+ // / This function will return true iff this constant represents the largest
701
+ // / value that may be represented by the constant's type.
702
+ // / @returns true iff this is the largest value that may be represented
703
+ // / by this type.
704
+ // / Determine if the value is maximal.
705
+ bool isMaxValue (bool IsSigned) const {
706
+ return cast<llvm::ConstantInt>(Val)->isMaxValue (IsSigned);
707
+ }
708
+
709
+ // / This function will return true iff this constant represents the smallest
710
+ // / value that may be represented by this constant's type.
711
+ // / @returns true if this is the smallest value that may be represented by
712
+ // / this type.
713
+ // / Determine if the value is minimal.
714
+ bool isMinValue (bool IsSigned) const {
715
+ return cast<llvm::ConstantInt>(Val)->isMinValue (IsSigned);
716
+ }
717
+
718
+ // / This function will return true iff this constant represents a value with
719
+ // / active bits bigger than 64 bits or a value greater than the given uint64_t
720
+ // / value.
721
+ // / @returns true iff this constant is greater or equal to the given number.
722
+ // / Determine if the value is greater or equal to the given number.
723
+ bool uge (uint64_t Num) const {
724
+ return cast<llvm::ConstantInt>(Val)->uge (Num);
725
+ }
726
+
727
+ // / getLimitedValue - If the value is smaller than the specified limit,
728
+ // / return it, otherwise return the limit value. This causes the value
729
+ // / to saturate to the limit.
730
+ // / @returns the min of the value of the constant and the specified value
731
+ // / Get the constant's value with a saturation limit
732
+ uint64_t getLimitedValue (uint64_t Limit = ~0ULL ) const {
733
+ return cast<llvm::ConstantInt>(Val)->getLimitedValue (Limit);
734
+ }
582
735
583
736
// / For isa/dyn_cast.
584
737
static bool classof (const sandboxir::Value *From) {
@@ -3198,6 +3351,7 @@ class Context {
3198
3351
LLVMContext &LLVMCtx;
3199
3352
friend class Type ; // For LLVMCtx.
3200
3353
friend class PointerType ; // For LLVMCtx.
3354
+ friend class IntegerType ; // For LLVMCtx.
3201
3355
Tracker IRTracker;
3202
3356
3203
3357
// / Maps LLVM Value to the corresponding sandboxir::Value. Owns all
0 commit comments