[llvm] r335242 - [InstCombine] make div/rem vector constant utility function; NFCI

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 21 07:59:35 PDT 2018


Author: spatel
Date: Thu Jun 21 07:59:35 2018
New Revision: 335242

URL: http://llvm.org/viewvc/llvm-project?rev=335242&view=rev
Log:
[InstCombine] make div/rem vector constant utility function; NFCI

This was originally in D48401 and will be used there.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
    llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h?rev=335242&r1=335241&r2=335242&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h Thu Jun 21 07:59:35 2018
@@ -212,6 +212,23 @@ IntrinsicIDToOverflowCheckFlavor(unsigne
   }
 }
 
+/// Integer division/remainder require special handling to avoid undefined
+/// behavior. If a constant vector has undef elements, replace those undefs with
+/// '1' because that's always safe to execute.
+static inline Constant *getSafeVectorConstantForIntDivRem(Constant *In) {
+  assert(In->getType()->isVectorTy() && "Not expecting scalars here");
+  assert(In->getType()->getVectorElementType()->isIntegerTy() &&
+         "Not expecting FP opcodes/operands/constants here");
+
+  unsigned NumElts = In->getType()->getVectorNumElements();
+  SmallVector<Constant *, 16> Out(NumElts);
+  for (unsigned i = 0; i != NumElts; ++i) {
+    Constant *C = In->getAggregateElement(i);
+    Out[i] = isa<UndefValue>(C) ? ConstantInt::get(C->getType(), 1) : C;
+  }
+  return ConstantVector::get(Out);
+}
+
 /// The core instruction combiner logic.
 ///
 /// This class provides both the logic to recursively visit instructions and

Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=335242&r1=335241&r2=335242&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Thu Jun 21 07:59:35 2018
@@ -1417,23 +1417,17 @@ Instruction *InstCombiner::foldShuffledB
       }
     }
     if (MayChange) {
+      Constant *NewC = ConstantVector::get(NewVecC);
       // With integer div/rem instructions, it is not safe to use a vector with
       // undef elements because the entire instruction can be folded to undef.
-      // So replace undef elements with '1' because that can never induce
-      // undefined behavior. All other binop opcodes are always safe to
-      // speculate, and therefore, it is fine to include undef elements for
-      // unused lanes (and using undefs may help optimization).
-      if (Inst.isIntDivRem()) {
-        assert(C->getType()->getScalarType()->isIntegerTy() &&
-               "Not expecting FP opcodes/operands/constants here");
-        for (unsigned i = 0; i < VWidth; ++i)
-          if (isa<UndefValue>(NewVecC[i]))
-            NewVecC[i] = ConstantInt::get(NewVecC[i]->getType(), 1);
-      }
-
+      // All other binop opcodes are always safe to speculate, and therefore, it
+      // is fine to include undef elements for unused lanes (and using undefs
+      // may help optimization).
+      if (Inst.isIntDivRem())
+        NewC = getSafeVectorConstantForIntDivRem(NewC);
+      
       // Op(shuffle(V1, Mask), C) -> shuffle(Op(V1, NewC), Mask)
       // Op(C, shuffle(V1, Mask)) -> shuffle(Op(NewC, V1), Mask)
-      Constant *NewC = ConstantVector::get(NewVecC);
       Value *NewLHS = isa<Constant>(LHS) ? NewC : V1;
       Value *NewRHS = isa<Constant>(LHS) ? V1 : NewC;
       return createBinOpShuffle(NewLHS, NewRHS, Mask);




More information about the llvm-commits mailing list