[PATCH] D15315: [X86ISelLowering] Replace uint64_t with APInt to prevent overflow

Chen Li via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 7 15:50:00 PST 2015


chenli created this revision.
chenli added a reviewer: RKSimon.
chenli added a subscriber: llvm-commits.

This is a prerequisite patch for http://reviews.llvm.org/D14603

http://reviews.llvm.org/D15315

Files:
  lib/Target/X86/X86ISelLowering.cpp

Index: lib/Target/X86/X86ISelLowering.cpp
===================================================================
--- lib/Target/X86/X86ISelLowering.cpp
+++ lib/Target/X86/X86ISelLowering.cpp
@@ -24707,44 +24707,50 @@
   ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
   if (!C)
     return SDValue();
-  uint64_t MulAmt = C->getZExtValue();
-  if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
+  APInt MulAmt = C->getAPIntValue();
+  if (MulAmt.isPowerOf2() || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
     return SDValue();
 
-  uint64_t MulAmt1 = 0;
-  uint64_t MulAmt2 = 0;
-  if ((MulAmt % 9) == 0) {
-    MulAmt1 = 9;
-    MulAmt2 = MulAmt / 9;
-  } else if ((MulAmt % 5) == 0) {
-    MulAmt1 = 5;
-    MulAmt2 = MulAmt / 5;
-  } else if ((MulAmt % 3) == 0) {
-    MulAmt1 = 3;
-    MulAmt2 = MulAmt / 3;
-  }
-  if (MulAmt2 &&
-      (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
+  unsigned BitWidth = MulAmt.getBitWidth();
+  APInt KnownZero = APInt(BitWidth, 0);
+  APInt KnownThree = APInt(BitWidth, 3);
+  APInt KnownFive = APInt(BitWidth, 5);
+  APInt KnownNine = APInt(BitWidth, 9);
+
+  APInt MulAmt1 = KnownZero;
+  APInt MulAmt2 = KnownZero;
+  if (MulAmt.urem(KnownNine) == 0) {
+    MulAmt1 = KnownNine;
+    MulAmt2 = MulAmt.udiv(KnownNine);
+  } else if (MulAmt.urem(KnownFive) == 0) {
+    MulAmt1 = KnownFive;
+    MulAmt2 = MulAmt.udiv(KnownFive);
+  } else if (MulAmt.urem(KnownThree) == 0) {
+    MulAmt1 = KnownThree;
+    MulAmt2 = MulAmt.udiv(KnownThree);
+  }
+  if (MulAmt2 != 0 &&
+      (MulAmt2.isPowerOf2() || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
     SDLoc DL(N);
 
-    if (isPowerOf2_64(MulAmt2) &&
+    if (MulAmt2.isPowerOf2() &&
         !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
       // If second multiplifer is pow2, issue it first. We want the multiply by
       // 3, 5, or 9 to be folded into the addressing mode unless the lone use
       // is an add.
       std::swap(MulAmt1, MulAmt2);
 
     SDValue NewMul;
-    if (isPowerOf2_64(MulAmt1))
+    if (MulAmt1.isPowerOf2())
       NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
-                           DAG.getConstant(Log2_64(MulAmt1), DL, MVT::i8));
+                           DAG.getConstant(MulAmt1.logBase2(), DL, MVT::i8));
     else
       NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
                            DAG.getConstant(MulAmt1, DL, VT));
 
-    if (isPowerOf2_64(MulAmt2))
+    if (MulAmt2.isPowerOf2())
       NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
-                           DAG.getConstant(Log2_64(MulAmt2), DL, MVT::i8));
+                           DAG.getConstant(MulAmt2.logBase2(), DL, MVT::i8));
     else
       NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
                            DAG.getConstant(MulAmt2, DL, VT));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15315.42119.patch
Type: text/x-patch
Size: 2897 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151207/4f32c247/attachment.bin>


More information about the llvm-commits mailing list