Add ConstantExpr::getSizeOf(Type*).
authorAlkis Evlogimenos <alkis@evlogimenos.com>
Sun, 24 Oct 2004 01:41:10 +0000 (01:41 +0000)
committerAlkis Evlogimenos <alkis@evlogimenos.com>
Sun, 24 Oct 2004 01:41:10 +0000 (01:41 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17196 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Constants.h
lib/VMCore/Constants.cpp

index 6a59cfa732bf9c6906f07f0098e629832484cf72..502974dc779744b5580c1ae2545de47d1f7307b9 100644 (file)
@@ -557,6 +557,10 @@ public:
     return getSelectTy(V1->getType(), C, V1, V2);
   }
 
+  /// getSizeOf constant expr - computes the size of a type in a
+  /// target independent way
+  ///
+  static Constant *getSizeOf(const Type *Ty);
 
   /// ConstantExpr::get - Return a binary or shift operator constant expression,
   /// folding if possible.
index 77aa23135e5bb6fc2af199099d8ace61f35927e3..3d2e658e634f3f9be7bc92d7c4ee4b9b1a034b89 100644 (file)
@@ -1244,6 +1244,15 @@ Constant *ConstantExpr::getZeroExtend(Constant *C, const Type *Ty) {
   return ConstantExpr::getCast(C, Ty);
 }
 
+Constant *ConstantExpr::getSizeOf(const Type *Ty) {
+  // sizeof is implemented as: (unsigned) gep (Ty)null, 1
+  return getCast(
+    getGetElementPtr(
+      getNullValue(Ty),
+      std::vector<Constant*>(1, ConstantInt::get(Type::UByteTy, 1))),
+    Type::UIntTy);
+}
+
 Constant *ConstantExpr::getTy(const Type *ReqTy, unsigned Opcode,
                               Constant *C1, Constant *C2) {
   if (Opcode == Instruction::Shl || Opcode == Instruction::Shr)