Add an int64_t variant of abs, for host environments
authorDale Johannesen <dalej@apple.com>
Wed, 13 May 2009 00:24:22 +0000 (00:24 +0000)
committerDale Johannesen <dalej@apple.com>
Wed, 13 May 2009 00:24:22 +0000 (00:24 +0000)
without one.  Use it where we were using abs on
int64_t objects.
(I strongly suspect the casts to unsigned in the
fragments in LoopStrengthReduce are not doing whatever
the original intent was, but the obvious change to
uint64_t doesn't work.  Maybe later.)

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

include/llvm/Support/MathExtras.h
lib/Target/Alpha/AlphaInstrInfo.td
lib/Transforms/Scalar/LoopStrengthReduce.cpp

index 0fb2760b125abcd29ac42d7c90de7ecb2c7ec29d..fd3ac9157b3200b70a3f7a5b2b78632227426dc0 100644 (file)
@@ -425,6 +425,13 @@ inline uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align) {
   return ((Value + Align - 1) / Align) * Align;
 }
 
+/// abs64 - absolute value of a 64-bit int.  Not all environments support
+/// "abs" on whatever their name for the 64-bit int type is.  The absolute
+/// value of the largest negative number is undefined, as with "abs".
+inline int64_t abs64(int64_t x) {
+  return (x < 0) ? -x : x;
+}
+
 } // End llvm namespace
 
 #endif
index ae9282564d1e0780720a986a10cebd6cbb696d5e..e73bdf9f6e9156b8142bc5498265f52537e4de5a 100644 (file)
@@ -69,7 +69,7 @@ def nearP2X : SDNodeXForm<imm, [{
 }]>;
 def nearP2RemX : SDNodeXForm<imm, [{
   uint64_t x =
-    abs(N->getZExtValue() - getNearPower2((uint64_t)N->getZExtValue()));
+    abs64(N->getZExtValue() - getNearPower2((uint64_t)N->getZExtValue()));
   return getI64Imm(Log2_64(x));
 }]>;
 
@@ -124,7 +124,7 @@ def immRemP2 : PatLeaf<(imm), [{
                          getNearPower2((uint64_t)N->getZExtValue()));
 }]>;
 def immUExt8ME : PatLeaf<(imm), [{ //use this imm for mulqi
-  int64_t d =  abs((int64_t)N->getZExtValue() -
+  int64_t d =  abs64((int64_t)N->getZExtValue() -
                (int64_t)getNearPower2((uint64_t)N->getZExtValue()));
   if (isPowerOf2_64(d)) return false;
   switch (d) {
index 4f6d53179edb872e578a74e0d3ca2cb853007e62..ed12d8683d2926197ae4fe5738ce480d9c846c4d 100644 (file)
@@ -1013,7 +1013,7 @@ SCEVHandle LoopStrengthReduce::CheckForIVReuse(bool HasBaseReg,
         continue;
       int64_t SSInt = cast<SCEVConstant>(SI->first)->getValue()->getSExtValue();
       if (SI->first != Stride &&
-          (unsigned(abs(SInt)) < SSInt || (SInt % SSInt) != 0))
+          (unsigned(abs64(SInt)) < SSInt || (SInt % SSInt) != 0))
         continue;
       int64_t Scale = SInt / SSInt;
       // Check that this stride is valid for all the types used for loads and
@@ -1900,7 +1900,7 @@ ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond,
         continue;
       int64_t SSInt = cast<SCEVConstant>(SI->first)->getValue()->getSExtValue();
       if (SSInt == CmpSSInt ||
-          abs(SSInt) < abs(CmpSSInt) ||
+          abs64(SSInt) < abs64(CmpSSInt) ||
           (SSInt % CmpSSInt) != 0)
         continue;
 
@@ -2336,7 +2336,7 @@ void LoopStrengthReduce::OptimizeLoopTermCond(Loop *L) {
           cast<SCEVConstant>(SI->first)->getValue()->getSExtValue();
         if (SSInt == SInt)
           return; // This can definitely be reused.
-        if (unsigned(abs(SSInt)) < SInt || (SSInt % SInt) != 0)
+        if (unsigned(abs64(SSInt)) < SInt || (SSInt % SInt) != 0)
           continue;
         int64_t Scale = SSInt / SInt;
         bool AllUsesAreAddresses = true;