[llvm-commits] [llvm] r52138 - in /llvm/trunk: lib/Transforms/Scalar/SimplifyLibCalls.cpp test/Transforms/SimplifyLibCalls/abs.ll

Chris Lattner sabre at nondot.org
Mon Jun 9 01:26:52 PDT 2008


Author: lattner
Date: Mon Jun  9 03:26:51 2008
New Revision: 52138

URL: http://llvm.org/viewvc/llvm-project?rev=52138&view=rev
Log:
lower calls to abs to inline code, PR2337

Added:
    llvm/trunk/test/Transforms/SimplifyLibCalls/abs.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp?rev=52138&r1=52137&r2=52138&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SimplifyLibCalls.cpp Mon Jun  9 03:26:51 2008
@@ -984,6 +984,27 @@
     return B.CreateZExt(Op, CI->getType());
   }
 };
+  
+//===---------------------------------------===//
+// 'abs', 'labs', 'llabs' Optimizations
+
+struct VISIBILITY_HIDDEN AbsOpt : public LibCallOptimization {
+  virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder &B) {
+    const FunctionType *FT = Callee->getFunctionType();
+    // We require integer(integer) where the types agree.
+    if (FT->getNumParams() != 1 || !isa<IntegerType>(FT->getReturnType()) ||
+        FT->getParamType(0) != FT->getReturnType())
+      return 0;
+    
+    // abs(x) -> x >s -1 ? x : -x
+    Value *Op = CI->getOperand(1);
+    Value *Pos = B.CreateICmpSGT(Op,ConstantInt::getAllOnesValue(Op->getType()),
+                                 "ispos");
+    Value *Neg = B.CreateNeg(Op, "neg");
+    return B.CreateSelect(Pos, Op, Neg);
+  }
+};
+  
 
 //===---------------------------------------===//
 // 'toascii' Optimizations
@@ -1258,7 +1279,8 @@
     // Math Library Optimizations
     PowOpt Pow; Exp2Opt Exp2; UnaryDoubleFPOpt UnaryDoubleFP;
     // Integer Optimizations
-    FFSOpt FFS; IsDigitOpt IsDigit; IsAsciiOpt IsAscii; ToAsciiOpt ToAscii;
+    FFSOpt FFS; AbsOpt Abs; IsDigitOpt IsDigit; IsAsciiOpt IsAscii;
+    ToAsciiOpt ToAscii;
     // Formatting and IO Optimizations
     SPrintFOpt SPrintF; PrintFOpt PrintF;
     FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
@@ -1328,6 +1350,9 @@
   Optimizations["ffs"] = &FFS;
   Optimizations["ffsl"] = &FFS;
   Optimizations["ffsll"] = &FFS;
+  Optimizations["abs"] = &Abs;
+  Optimizations["labs"] = &Abs;
+  Optimizations["llabs"] = &Abs;
   Optimizations["isdigit"] = &IsDigit;
   Optimizations["isascii"] = &IsAscii;
   Optimizations["toascii"] = &ToAscii;

Added: llvm/trunk/test/Transforms/SimplifyLibCalls/abs.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyLibCalls/abs.ll?rev=52138&view=auto

==============================================================================
--- llvm/trunk/test/Transforms/SimplifyLibCalls/abs.ll (added)
+++ llvm/trunk/test/Transforms/SimplifyLibCalls/abs.ll Mon Jun  9 03:26:51 2008
@@ -0,0 +1,11 @@
+; RUN: llvm-as < %s | opt -simplify-libcalls | llvm-dis | grep {select i1 %ispos}
+; PR2337
+
+define i32 @test(i32 %x) {
+entry:
+	%call = call i32 @abs( i32 %x )		; <i32> [#uses=1]
+	ret i32 %call
+}
+
+declare i32 @abs(i32)
+





More information about the llvm-commits mailing list