15
15
// This pass guarantees that the following cannonicalizations are performed on
16
16
// the program:
17
17
// 1. If a binary operator has a constant operand, it is moved to the RHS
18
- // 2. Logical operators with constant operands are always grouped so that
19
- // 'or's are performed first, then ' and's, then ' xor's.
18
+ // 2. Bitwise operators with constant operands are always grouped so that
19
+ // shifts are performed first, then or's, then and's, then xor's.
20
20
// 3. SetCC instructions are converted from <,>,<=,>= to ==,!= if possible
21
21
// 4. All SetCC instructions on boolean values are replaced with logical ops
22
22
// N. This list is incomplete
@@ -879,24 +879,76 @@ Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
879
879
Instruction *InstCombiner::visitShiftInst (ShiftInst &I) {
880
880
assert (I.getOperand (1 )->getType () == Type::UByteTy);
881
881
Value *Op0 = I.getOperand (0 ), *Op1 = I.getOperand (1 );
882
+ bool isLeftShift = I.getOpcode () == Instruction::Shl;
882
883
883
884
// shl X, 0 == X and shr X, 0 == X
884
885
// shl 0, X == 0 and shr 0, X == 0
885
886
if (Op1 == Constant::getNullValue (Type::UByteTy) ||
886
887
Op0 == Constant::getNullValue (Op0->getType ()))
887
888
return ReplaceInstUsesWith (I, Op0);
888
889
890
+ // shr int -1, X = -1 (for any arithmetic shift rights of ~0)
891
+ if (!isLeftShift)
892
+ if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
893
+ if (CSI->isAllOnesValue ())
894
+ return ReplaceInstUsesWith (I, CSI);
895
+
889
896
if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
890
897
// shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr
891
898
// of a signed value.
892
899
//
893
900
unsigned TypeBits = Op0->getType ()->getPrimitiveSize ()*8 ;
894
901
if (CUI->getValue () >= TypeBits &&
895
- (!Op0->getType ()->isSigned () || I. getOpcode () == Instruction::Shl ))
902
+ (!Op0->getType ()->isSigned () || isLeftShift ))
896
903
return ReplaceInstUsesWith (I, Constant::getNullValue (Op0->getType ()));
897
904
905
+ // If the operand is an bitwise operator with a constant RHS, and the
906
+ // shift is the only use, we can pull it out of the shift.
907
+ if (Op0->use_size () == 1 )
908
+ if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0))
909
+ if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand (1 ))) {
910
+ bool isValid = true ; // Valid only for And, Or, Xor
911
+ bool highBitSet = false ; // Transform if high bit of constant set?
912
+
913
+ switch (Op0BO->getOpcode ()) {
914
+ default : isValid = false ; break ; // Do not perform transform!
915
+ case Instruction::Or:
916
+ case Instruction::Xor:
917
+ highBitSet = false ;
918
+ break ;
919
+ case Instruction::And:
920
+ highBitSet = true ;
921
+ break ;
922
+ }
923
+
924
+ // If this is a signed shift right, and the high bit is modified
925
+ // by the logical operation, do not perform the transformation.
926
+ // The highBitSet boolean indicates the value of the high bit of
927
+ // the constant which would cause it to be modified for this
928
+ // operation.
929
+ //
930
+ if (isValid && !isLeftShift && !I.getType ()->isUnsigned ()) {
931
+ uint64_t Val = Op0C->getRawValue ();
932
+ isValid = ((Val & (1 << (TypeBits-1 ))) != 0 ) == highBitSet;
933
+ }
934
+
935
+ if (isValid) {
936
+ Constant *NewRHS =
937
+ ConstantFoldShiftInstruction (I.getOpcode (), Op0C, CUI);
938
+
939
+ Instruction *NewShift =
940
+ new ShiftInst (I.getOpcode (), Op0BO->getOperand (0 ), CUI,
941
+ Op0BO->getName ());
942
+ Op0BO->setName (" " );
943
+ InsertNewInstBefore (NewShift, I);
944
+
945
+ return BinaryOperator::create (Op0BO->getOpcode (), NewShift,
946
+ NewRHS);
947
+ }
948
+ }
949
+
898
950
// If this is a shift of a shift, see if we can fold the two together...
899
- if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0)) {
951
+ if (ShiftInst *Op0SI = dyn_cast<ShiftInst>(Op0))
900
952
if (ConstantUInt *ShiftAmt1C =
901
953
dyn_cast<ConstantUInt>(Op0SI->getOperand (1 ))) {
902
954
unsigned ShiftAmt1 = ShiftAmt1C->getValue ();
@@ -912,13 +964,13 @@ Instruction *InstCombiner::visitShiftInst(ShiftInst &I) {
912
964
// Check for (A << c1) >> c2 or visaversa. If we are dealing with
913
965
// signed types, we can only support the (A >> c1) << c2 configuration,
914
966
// because it can not turn an arbitrary bit of A into a sign bit.
915
- if (I.getType ()->isUnsigned () || I. getOpcode () == Instruction::Shl ) {
967
+ if (I.getType ()->isUnsigned () || isLeftShift ) {
916
968
// Calculate bitmask for what gets shifted off the edge...
917
969
Constant *C = ConstantIntegral::getAllOnesValue (I.getType ());
918
- if (I.getOpcode () == Instruction::Shr)
919
- C = ConstantExpr::getShift (Instruction::Shr, C, ShiftAmt1C);
920
- else
970
+ if (isLeftShift)
921
971
C = ConstantExpr::getShift (Instruction::Shl, C, ShiftAmt1C);
972
+ else
973
+ C = ConstantExpr::getShift (Instruction::Shr, C, ShiftAmt1C);
922
974
923
975
Instruction *Mask =
924
976
BinaryOperator::create (Instruction::And, Op0SI->getOperand (0 ),
@@ -937,21 +989,14 @@ Instruction *InstCombiner::visitShiftInst(ShiftInst &I) {
937
989
}
938
990
}
939
991
}
940
- }
941
992
942
993
// Check to see if we are shifting left by 1. If so, turn it into an add
943
994
// instruction.
944
- if (I. getOpcode () == Instruction::Shl && CUI->equalsInt (1 ))
995
+ if (isLeftShift && CUI->equalsInt (1 ))
945
996
// Convert 'shl int %X, 1' to 'add int %X, %X'
946
997
return BinaryOperator::create (Instruction::Add, Op0, Op0, I.getName ());
947
998
}
948
999
949
- // shr int -1, X = -1 (for any arithmetic shift rights of ~0)
950
- if (I.getOpcode () == Instruction::Shr)
951
- if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
952
- if (CSI->isAllOnesValue ())
953
- return ReplaceInstUsesWith (I, CSI);
954
-
955
1000
return 0 ;
956
1001
}
957
1002
0 commit comments