[llvm-branch-commits] [llvm-branch] r99285 - in /llvm/branches/Apple/Morbo: include/llvm/Transforms/Utils/BuildLibCalls.h lib/Transforms/Scalar/SimplifyLibCalls.cpp lib/Transforms/Utils/BuildLibCalls.cpp test/Transforms/SimplifyLibCalls/StrCpy.ll

Evan Cheng evan.cheng at apple.com
Tue Mar 23 08:54:05 PDT 2010


Author: evancheng
Date: Tue Mar 23 10:54:04 2010
New Revision: 99285

URL: http://llvm.org/viewvc/llvm-project?rev=99285&view=rev
Log:
Merge 99282, 99283, 99284.

Modified:
    llvm/branches/Apple/Morbo/include/llvm/Transforms/Utils/BuildLibCalls.h
    llvm/branches/Apple/Morbo/lib/Transforms/Scalar/SimplifyLibCalls.cpp
    llvm/branches/Apple/Morbo/lib/Transforms/Utils/BuildLibCalls.cpp
    llvm/branches/Apple/Morbo/test/Transforms/SimplifyLibCalls/StrCpy.ll

Modified: llvm/branches/Apple/Morbo/include/llvm/Transforms/Utils/BuildLibCalls.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Morbo/include/llvm/Transforms/Utils/BuildLibCalls.h?rev=99285&r1=99284&r2=99285&view=diff
==============================================================================
--- llvm/branches/Apple/Morbo/include/llvm/Transforms/Utils/BuildLibCalls.h (original)
+++ llvm/branches/Apple/Morbo/include/llvm/Transforms/Utils/BuildLibCalls.h Tue Mar 23 10:54:04 2010
@@ -49,6 +49,12 @@
   Value *EmitMemCpy(Value *Dst, Value *Src, Value *Len,
                     unsigned Align, IRBuilder<> &B, const TargetData *TD);
 
+  /// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
+  /// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
+  /// are pointers.
+  Value *EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
+                       IRBuilder<> &B, const TargetData *TD);
+
   /// EmitMemMove - Emit a call to the memmove function to the builder.  This
   /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
   Value *EmitMemMove(Value *Dst, Value *Src, Value *Len,

Modified: llvm/branches/Apple/Morbo/lib/Transforms/Scalar/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Morbo/lib/Transforms/Scalar/SimplifyLibCalls.cpp?rev=99285&r1=99284&r2=99285&view=diff
==============================================================================
--- llvm/branches/Apple/Morbo/lib/Transforms/Scalar/SimplifyLibCalls.cpp (original)
+++ llvm/branches/Apple/Morbo/lib/Transforms/Scalar/SimplifyLibCalls.cpp Tue Mar 23 10:54:04 2010
@@ -49,10 +49,13 @@
   Function *Caller;
   const TargetData *TD;
   LLVMContext* Context;
+  bool OptChkCall;  // True if it's optimizing a *_chk libcall.
 public:
-  LibCallOptimization() { }
+  LibCallOptimization() : OptChkCall(false) { }
   virtual ~LibCallOptimization() {}
 
+  void setOptChkCall(bool c) { OptChkCall = c; }
+
   /// CallOptimizer - This pure virtual method is implemented by base classes to
   /// do various optimizations.  If this returns null then no transformation was
   /// performed.  If it returns CI, then it transformed the call and CI is to be
@@ -352,8 +355,10 @@
 struct StrCpyOpt : public LibCallOptimization {
   virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
     // Verify the "strcpy" function prototype.
+    unsigned NumParams = OptChkCall ? 3 : 2;
     const FunctionType *FT = Callee->getFunctionType();
-    if (FT->getNumParams() != 2 || FT->getReturnType() != FT->getParamType(0) ||
+    if (FT->getNumParams() != NumParams ||
+        FT->getReturnType() != FT->getParamType(0) ||
         FT->getParamType(0) != FT->getParamType(1) ||
         FT->getParamType(0) != Type::getInt8PtrTy(*Context))
       return 0;
@@ -371,8 +376,13 @@
 
     // We have enough information to now generate the memcpy call to do the
     // concatenation for us.  Make a memcpy to copy the nul byte with align = 1.
-    EmitMemCpy(Dst, Src,
-               ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
+    if (OptChkCall)
+      EmitMemCpyChk(Dst, Src,
+                    ConstantInt::get(TD->getIntPtrType(*Context), Len),
+                    CI->getOperand(3), B, TD);
+    else
+      EmitMemCpy(Dst, Src,
+                 ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
     return Dst;
   }
 };
@@ -1162,7 +1172,8 @@
     StringMap<LibCallOptimization*> Optimizations;
     // String and Memory LibCall Optimizations
     StrCatOpt StrCat; StrNCatOpt StrNCat; StrChrOpt StrChr; StrCmpOpt StrCmp;
-    StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrNCpyOpt StrNCpy; StrLenOpt StrLen;
+    StrNCmpOpt StrNCmp; StrCpyOpt StrCpy; StrCpyOpt StrCpyChk;
+    StrNCpyOpt StrNCpy; StrLenOpt StrLen;
     StrToOpt StrTo; StrStrOpt StrStr;
     MemCmpOpt MemCmp; MemCpyOpt MemCpy; MemMoveOpt MemMove; MemSetOpt MemSet;
     // Math Library Optimizations
@@ -1228,6 +1239,10 @@
   Optimizations["memmove"] = &MemMove;
   Optimizations["memset"] = &MemSet;
 
+  // _chk variants of String and Memory LibCall Optimizations.
+  StrCpyChk.setOptChkCall(true);
+  Optimizations["__strcpy_chk"] = &StrCpyChk;
+
   // Math Library Optimizations
   Optimizations["powf"] = &Pow;
   Optimizations["pow"] = &Pow;

Modified: llvm/branches/Apple/Morbo/lib/Transforms/Utils/BuildLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Morbo/lib/Transforms/Utils/BuildLibCalls.cpp?rev=99285&r1=99284&r2=99285&view=diff
==============================================================================
--- llvm/branches/Apple/Morbo/lib/Transforms/Utils/BuildLibCalls.cpp (original)
+++ llvm/branches/Apple/Morbo/lib/Transforms/Utils/BuildLibCalls.cpp Tue Mar 23 10:54:04 2010
@@ -108,7 +108,7 @@
 
 
 /// EmitMemCpy - Emit a call to the memcpy function to the builder.  This always
-/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
+/// expects that Len has type 'intptr_t' and Dst/Src are pointers.
 Value *llvm::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
                         unsigned Align, IRBuilder<> &B, const TargetData *TD) {
   Module *M = B.GetInsertBlock()->getParent()->getParent();
@@ -120,6 +120,30 @@
                        ConstantInt::get(B.getInt32Ty(), Align));
 }
 
+/// EmitMemCpyChk - Emit a call to the __memcpy_chk function to the builder.
+/// This expects that the Len and ObjSize have type 'intptr_t' and Dst/Src
+/// are pointers.
+Value *llvm::EmitMemCpyChk(Value *Dst, Value *Src, Value *Len, Value *ObjSize,
+                           IRBuilder<> &B, const TargetData *TD) {
+  Module *M = B.GetInsertBlock()->getParent()->getParent();
+  AttributeWithIndex AWI;
+  AWI = AttributeWithIndex::get(~0u, Attribute::NoUnwind);
+  LLVMContext &Context = B.GetInsertBlock()->getContext();
+  Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
+                                         AttrListPtr::get(&AWI, 1),
+                                         B.getInt8PtrTy(),
+                                         B.getInt8PtrTy(),
+                                         B.getInt8PtrTy(),
+                                         TD->getIntPtrType(Context),
+                                         TD->getIntPtrType(Context), NULL);
+  Dst = CastToCStr(Dst, B);
+  Src = CastToCStr(Src, B);
+  CallInst *CI = B.CreateCall4(MemCpy, Dst, Src, Len, ObjSize);
+  if (const Function *F = dyn_cast<Function>(MemCpy->stripPointerCasts()))
+    CI->setCallingConv(F->getCallingConv());
+  return CI;
+}
+
 /// EmitMemMove - Emit a call to the memmove function to the builder.  This
 /// always expects that the size has type 'intptr_t' and Dst/Src are pointers.
 Value *llvm::EmitMemMove(Value *Dst, Value *Src, Value *Len,

Modified: llvm/branches/Apple/Morbo/test/Transforms/SimplifyLibCalls/StrCpy.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Morbo/test/Transforms/SimplifyLibCalls/StrCpy.ll?rev=99285&r1=99284&r2=99285&view=diff
==============================================================================
--- llvm/branches/Apple/Morbo/test/Transforms/SimplifyLibCalls/StrCpy.ll (original)
+++ llvm/branches/Apple/Morbo/test/Transforms/SimplifyLibCalls/StrCpy.ll Tue Mar 23 10:54:04 2010
@@ -1,30 +1,37 @@
 ; Test that the StrCpyOptimizer works correctly
-; RUN: opt < %s -simplify-libcalls -S | \
-; RUN:   not grep {call.*strcpy}
+; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
 
 ; This transformation requires the pointer size, as it assumes that size_t is
 ; the size of a pointer.
-target datalayout = "-p:64:64:64"
+target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32"
 
- at hello = constant [6 x i8] c"hello\00"		; <[6 x i8]*> [#uses=1]
- at null = constant [1 x i8] zeroinitializer		; <[1 x i8]*> [#uses=1]
- at null_hello = constant [7 x i8] c"\00hello\00"		; <[7 x i8]*> [#uses=1]
+ at hello = constant [6 x i8] c"hello\00"
 
 declare i8* @strcpy(i8*, i8*)
 
-declare i32 @puts(i8*)
+declare i8* @__strcpy_chk(i8*, i8*, i32) nounwind
 
-define i32 @main() {
-	%target = alloca [1024 x i8]		; <[1024 x i8]*> [#uses=1]
-	%arg1 = getelementptr [1024 x i8]* %target, i32 0, i32 0		; <i8*> [#uses=2]
-	store i8 0, i8* %arg1
-	%arg2 = getelementptr [6 x i8]* @hello, i32 0, i32 0		; <i8*> [#uses=1]
-	%rslt1 = call i8* @strcpy( i8* %arg1, i8* %arg2 )		; <i8*> [#uses=1]
-	%arg3 = getelementptr [1 x i8]* @null, i32 0, i32 0		; <i8*> [#uses=1]
-	%rslt2 = call i8* @strcpy( i8* %rslt1, i8* %arg3 )		; <i8*> [#uses=1]
-	%arg4 = getelementptr [7 x i8]* @null_hello, i32 0, i32 0		; <i8*> [#uses=1]
-	%rslt3 = call i8* @strcpy( i8* %rslt2, i8* %arg4 )		; <i8*> [#uses=1]
-	call i32 @puts( i8* %rslt3 )		; <i32>:1 [#uses=0]
-	ret i32 0
+declare i32 @llvm.objectsize.i32(i8*, i1) nounwind readonly
+
+; rdar://6839935
+
+define i32 @t1() {
+; CHECK: @t1
+  %target = alloca [1024 x i8]
+  %arg1 = getelementptr [1024 x i8]* %target, i32 0, i32 0
+  %arg2 = getelementptr [6 x i8]* @hello, i32 0, i32 0
+  %rslt1 = call i8* @strcpy( i8* %arg1, i8* %arg2 )
+; CHECK: @llvm.memcpy.i32
+  ret i32 0
 }
 
+define i32 @t2() {
+; CHECK: @t2
+  %target = alloca [1024 x i8]
+  %arg1 = getelementptr [1024 x i8]* %target, i32 0, i32 0
+  %arg2 = getelementptr [6 x i8]* @hello, i32 0, i32 0
+  %tmp1 = call i32 @llvm.objectsize.i32(i8* %arg1, i1 false)
+  %rslt1 = call i8* @__strcpy_chk(i8* %arg1, i8* %arg2, i32 %tmp1)
+; CHECK: @__memcpy_chk
+  ret i32 0
+}





More information about the llvm-branch-commits mailing list