SystemZ: Rephrase this allOnes calculation to avoid UB
authorJustin Bogner <mail@justinbogner.com>
Wed, 24 Jun 2015 05:59:19 +0000 (05:59 +0000)
committerJustin Bogner <mail@justinbogner.com>
Wed, 24 Jun 2015 05:59:19 +0000 (05:59 +0000)
This allOnes function hits undefined behaviour if Count is greater
than 64, but we can avoid that and simplify the calculation by just
saturating if such a value is passed in.

This comes up under ubsan becauseRxSBGOperands is sometimes created
with values that are 128 bits wide. Somebody more familiar with this
code should probably look into whether that's expected, as a 64 bit
mask may or may not be appropriate for such types.

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

lib/Target/SystemZ/SystemZISelDAGToDAG.cpp

index 0eb3d6593fe66ad18a23c14949decf630ce10b9d..26c9c9b9f67d1d0978130f276ba884cf976ae8a4 100644 (file)
@@ -96,7 +96,9 @@ struct SystemZAddressingMode {
 
 // Return a mask with Count low bits set.
 static uint64_t allOnes(unsigned int Count) {
-  return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
+  if (Count > 63)
+    return UINT64_MAX;
+  return (uint64_t(1) << Count) - 1;
 }
 
 // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation