OSDN Git Service

Add objectsize intrinsic and hook it up through codegen. Doesn't
authorEric Christopher <echristo@apple.com>
Tue, 27 Oct 2009 00:52:25 +0000 (00:52 +0000)
committerEric Christopher <echristo@apple.com>
Tue, 27 Oct 2009 00:52:25 +0000 (00:52 +0000)
do anything than return "I don't know" at the moment.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85189 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Intrinsics.td
lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
lib/Transforms/Scalar/SimplifyLibCalls.cpp

index e6d8007..c0cf00e 100644 (file)
@@ -259,6 +259,11 @@ def int_longjmp    : Intrinsic<[llvm_void_ty], [llvm_ptr_ty, llvm_i32_ty]>;
 def int_sigsetjmp  : Intrinsic<[llvm_i32_ty] , [llvm_ptr_ty, llvm_i32_ty]>;
 def int_siglongjmp : Intrinsic<[llvm_void_ty], [llvm_ptr_ty, llvm_i32_ty]>;
 
+// Internal interface for object size checking
+def int_objectsize : Intrinsic<[llvm_anyint_ty], [llvm_ptr_ty, llvm_i32_ty],
+                               [IntrReadArgMem]>,
+                               GCCBuiltin<"__builtin_object_size">;
+
 //===-------------------- Bit Manipulation Intrinsics ---------------------===//
 //
 
index f3a0622..b220d55 100644 (file)
@@ -4204,6 +4204,18 @@ SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
     DAG.setRoot(Result);
     return 0;
   }
+  case Intrinsic::objectsize: {
+    // If we don't know by now, we're never going to know.
+    ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
+
+    assert(CI && "Non-constant type in __builtin_object_size?");
+
+    if (CI->getZExtValue() < 2)
+      setValue(&I, DAG.getConstant(-1, MVT::i32));
+    else
+      setValue(&I, DAG.getConstant(0, MVT::i32));
+    return 0;
+  }
   case Intrinsic::var_annotation:
     // Discard annotate attributes
     return 0;
index e186601..575c93b 100644 (file)
@@ -509,6 +509,27 @@ static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
 }
 
 //===----------------------------------------------------------------------===//
+// Miscellaneous LibCall/Intrinsic Optimizations
+//===----------------------------------------------------------------------===//
+
+namespace {
+struct SizeOpt : public LibCallOptimization {
+  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
+    // TODO: We can do more with this, but delaying to here should be no change
+    // in behavior.
+    ConstantInt *Const = dyn_cast<ConstantInt>(CI->getOperand(2));
+    
+    if (!Const) return 0;
+    
+    if (Const->getZExtValue() < 2)
+      return Constant::getAllOnesValue(Const->getType());
+    else
+      return ConstantInt::get(Const->getType(), 0);
+  }
+};
+}
+
+//===----------------------------------------------------------------------===//
 // String and Memory LibCall Optimizations
 //===----------------------------------------------------------------------===//
 
@@ -1548,6 +1569,7 @@ namespace {
     // Formatting and IO Optimizations
     SPrintFOpt SPrintF; PrintFOpt PrintF;
     FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
+    SizeOpt ObjectSize;
 
     bool Modified;  // This is only used by doInitialization.
   public:
@@ -1653,6 +1675,9 @@ void SimplifyLibCalls::InitOptimizations() {
   Optimizations["fwrite"] = &FWrite;
   Optimizations["fputs"] = &FPuts;
   Optimizations["fprintf"] = &FPrintF;
+  
+  // Miscellaneous
+  Optimizations["llvm.objectsize"] = &ObjectSize;
 }