Skip to content

Commit 1e6ca44

Browse files
committed
add and use isBitwiseLogicOp() helper function; NFCI
llvm-svn: 287712
1 parent 838ff4d commit 1e6ca44

File tree

4 files changed

+19
-33
lines changed

4 files changed

+19
-33
lines changed

llvm/include/llvm/IR/Instruction.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ class Instruction : public User,
134134
return getOpcode() == AShr;
135135
}
136136

137+
/// Return true if this is and/or/xor.
138+
inline bool isBitwiseLogicOp() const {
139+
return getOpcode() == And || getOpcode() == Or || getOpcode() == Xor;
140+
}
141+
137142
/// Determine if the OpCode is one of the CastInst instructions.
138143
static inline bool isCast(unsigned OpCode) {
139144
return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) {
9898
IntegerType *ITy = dyn_cast<IntegerType>(I.getType());
9999

100100
// Can't do vectors.
101-
if (I.getType()->isVectorTy()) return nullptr;
101+
if (I.getType()->isVectorTy())
102+
return nullptr;
102103

103104
// Can only do bitwise ops.
104-
unsigned Op = I.getOpcode();
105-
if (Op != Instruction::And && Op != Instruction::Or &&
106-
Op != Instruction::Xor)
105+
if (!I.isBitwiseLogicOp())
107106
return nullptr;
108107

109108
Value *OldLHS = I.getOperand(0);
@@ -132,14 +131,7 @@ Value *InstCombiner::SimplifyBSwap(BinaryOperator &I) {
132131
Value *NewRHS = IsBswapRHS ? IntrRHS->getOperand(0) :
133132
Builder->getInt(ConstRHS->getValue().byteSwap());
134133

135-
Value *BinOp = nullptr;
136-
if (Op == Instruction::And)
137-
BinOp = Builder->CreateAnd(NewLHS, NewRHS);
138-
else if (Op == Instruction::Or)
139-
BinOp = Builder->CreateOr(NewLHS, NewRHS);
140-
else //if (Op == Instruction::Xor)
141-
BinOp = Builder->CreateXor(NewLHS, NewRHS);
142-
134+
Value *BinOp = Builder->CreateBinOp(I.getOpcode(), NewLHS, NewRHS);
143135
Function *F = Intrinsic::getDeclaration(I.getModule(), Intrinsic::bswap, ITy);
144136
return Builder->CreateCall(F, BinOp);
145137
}
@@ -1172,9 +1164,7 @@ static Instruction *foldLogicCastConstant(BinaryOperator &Logic, CastInst *Cast,
11721164
/// Fold {and,or,xor} (cast X), Y.
11731165
Instruction *InstCombiner::foldCastedBitwiseLogic(BinaryOperator &I) {
11741166
auto LogicOpc = I.getOpcode();
1175-
assert((LogicOpc == Instruction::And || LogicOpc == Instruction::Or ||
1176-
LogicOpc == Instruction::Xor) &&
1177-
"Unexpected opcode for bitwise logic folding");
1167+
assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic folding");
11781168

11791169
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
11801170
CastInst *Cast0 = dyn_cast<CastInst>(Op0);

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,7 @@ static bool canEvaluateZExtd(Value *V, Type *Ty, unsigned &BitsToClear,
747747

748748
// If the operation is an AND/OR/XOR and the bits to clear are zero in the
749749
// other side, BitsToClear is ok.
750-
if (Tmp == 0 &&
751-
(Opc == Instruction::And || Opc == Instruction::Or ||
752-
Opc == Instruction::Xor)) {
750+
if (Tmp == 0 && I->isBitwiseLogicOp()) {
753751
// We use MaskedValueIsZero here for generality, but the case we care
754752
// about the most is constant RHS.
755753
unsigned VSize = V->getType()->getScalarSizeInBits();
@@ -1781,17 +1779,11 @@ static Instruction *canonicalizeBitCastExtElt(BitCastInst &BitCast,
17811779
/// Change the type of a bitwise logic operation if we can eliminate a bitcast.
17821780
static Instruction *foldBitCastBitwiseLogic(BitCastInst &BitCast,
17831781
InstCombiner::BuilderTy &Builder) {
1784-
BinaryOperator *BO;
1785-
if (!match(BitCast.getOperand(0), m_OneUse(m_BinOp(BO))))
1786-
return nullptr;
1787-
1788-
auto Opcode = BO->getOpcode();
1789-
if (Opcode != Instruction::And && Opcode != Instruction::Or &&
1790-
Opcode != Instruction::Xor)
1791-
return nullptr;
1792-
17931782
Type *DestTy = BitCast.getType();
1794-
if (!DestTy->getScalarType()->isIntegerTy())
1783+
BinaryOperator *BO;
1784+
if (!DestTy->getScalarType()->isIntegerTy() ||
1785+
!match(BitCast.getOperand(0), m_OneUse(m_BinOp(BO))) ||
1786+
!BO->isBitwiseLogicOp())
17951787
return nullptr;
17961788

17971789
// FIXME: This transform is restricted to vector types to avoid backend
@@ -1805,14 +1797,14 @@ static Instruction *foldBitCastBitwiseLogic(BitCastInst &BitCast,
18051797
X->getType() == DestTy && !isa<Constant>(X)) {
18061798
// bitcast(logic(bitcast(X), Y)) --> logic'(X, bitcast(Y))
18071799
Value *CastedOp1 = Builder.CreateBitCast(BO->getOperand(1), DestTy);
1808-
return BinaryOperator::Create(Opcode, X, CastedOp1);
1800+
return BinaryOperator::Create(BO->getOpcode(), X, CastedOp1);
18091801
}
18101802

18111803
if (match(BO->getOperand(1), m_OneUse(m_BitCast(m_Value(X)))) &&
18121804
X->getType() == DestTy && !isa<Constant>(X)) {
18131805
// bitcast(logic(Y, bitcast(X))) --> logic'(bitcast(Y), X)
18141806
Value *CastedOp0 = Builder.CreateBitCast(BO->getOperand(0), DestTy);
1815-
return BinaryOperator::Create(Opcode, CastedOp0, X);
1807+
return BinaryOperator::Create(BO->getOpcode(), CastedOp0, X);
18161808
}
18171809

18181810
return nullptr;

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,10 @@ static bool simplifyAssocCastAssoc(BinaryOperator *BinOp1) {
177177
return false;
178178

179179
// TODO: Enhance logic for other BinOps and remove this check.
180-
auto AssocOpcode = BinOp1->getOpcode();
181-
if (AssocOpcode != Instruction::Xor && AssocOpcode != Instruction::And &&
182-
AssocOpcode != Instruction::Or)
180+
if (!BinOp1->isBitwiseLogicOp())
183181
return false;
184182

183+
auto AssocOpcode = BinOp1->getOpcode();
185184
auto *BinOp2 = dyn_cast<BinaryOperator>(Cast->getOperand(0));
186185
if (!BinOp2 || !BinOp2->hasOneUse() || BinOp2->getOpcode() != AssocOpcode)
187186
return false;

0 commit comments

Comments
 (0)