llvm核心类位于 include/llvm/IR中,用以表示机器无关且表现力极强的LLVM IR。

llvm::Value则是这其中的重中之重,它用来表示一个具有类型的值。它是类图如下:

llvm::Argument,llvm::BasicBlock,llvm::Constant,llvm::Instruction这些很重要的类都是它的子类。

llvm::Value有一个llvm::Type*成员和一个use list。后者可以跟踪有哪些其他Value使用了自己,我们可以使用下面的迭代器对它进行访问:

  • unsigned use_size() 返回有多少Value使用它
  • bool use_empty() 是否没有Value使用它
  • use_iterator use_begin() 返回use list的迭代器头
  • use_iterator use_end() 返回尾
  • User *use_back() 返回use list的最后一个元素
  1. int main() {
  2. Value* val1 = ConstantFP::get(theContext, APFloat(3.2));
  3. if (val1->use_empty()) {
  4. std::cout << "no one use it\n";
  5. }
  6. system("pause");
  7. return 0;
  8. }

前者顾名思义表示一个类型。可以通过Value::getType()获取到这个llvm::Type*,它有一些is*()成员函数可以判断是下面哪种类型:

  1. enum TypeID {
  2. // PrimitiveTypes - make sure LastPrimitiveTyID stays up to date.
  3. VoidTyID = 0, ///< 0: type with no size
  4. HalfTyID, ///< 1: 16-bit floating point type
  5. FloatTyID, ///< 2: 32-bit floating point type
  6. DoubleTyID, ///< 3: 64-bit floating point type
  7. X86_FP80TyID, ///< 4: 80-bit floating point type (X87)
  8. FP128TyID, ///< 5: 128-bit floating point type (112-bit mantissa)
  9. PPC_FP128TyID, ///< 6: 128-bit floating point type (two 64-bits, PowerPC)
  10. LabelTyID, ///< 7: Labels
  11. MetadataTyID, ///< 8: Metadata
  12. X86_MMXTyID, ///< 9: MMX vectors (64 bits, X86 specific)
  13. TokenTyID, ///< 10: Tokens
  14. // Derived types... see DerivedTypes.h file.
  15. // Make sure FirstDerivedTyID stays up to date!
  16. IntegerTyID, ///< 11: Arbitrary bit width integers
  17. FunctionTyID, ///< 12: Functions
  18. StructTyID, ///< 13: Structures
  19. ArrayTyID, ///< 14: Arrays
  20. PointerTyID, ///< 15: Pointers
  21. VectorTyID ///< 16: SIMD 'packed' format, or other vector type
  22. };

比如这样:

  1. int main() {
  2. Value* val1 = ConstantFP::get(theContext, APFloat(3.2));
  3. Type* t = val1->getType();
  4. if (t->isDoubleTy()) {
  5. std::cout << "val1 is typed as double(" << t->getTypeID() <<")\n";
  6. }
  7. system("pause");
  8. return 0;
  9. }

除此之外llvm::Type还有很多成员函数,详细请参见http://llvm.org/doxygen/classllvm_1_1Type.html

多说一句,我们还可以对 llvm::Value 进行命名

  1. bool hasName() const
  2. std::string getName() const
  3. void setName(const std::string &Name)

llvm::Constant表示一个各种常量的基类,基于它派生出了ConstantInt 整型常量,ConstantFP 浮点型常量,ConstantArray 数组常量,ConstantStruct 结构体常量:

  1. int main() {
  2. // 构造一个32位,无符号的整型值,值为1024
  3. APInt ci = APInt(32, 1024);
  4. ConstantInt* intVal = ConstantInt::get(theContext, ci);
  5. std::cout << "bit width:" << intVal->getBitWidth()
  6. << "\nvalue:" << intVal->getValue().toString(16, false);
  7. system("pause");
  8. return 0;
  9. }

版权声明:本文为racaljk原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/racaljk/p/9688336.html