1 //===- InstructionCombining.cpp - Combine multiple instructions -------------=//
3 // InstructionCombining - Combine instructions to form fewer, simple
4 // instructions. This pass does not modify the CFG, and has a tendancy to
5 // make instructions dead, so a subsequent DIE pass is useful. This pass is
6 // where algebraic simplification happens.
8 // This pass combines things like:
14 // This is a simple worklist driven algorithm.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
20 #include "llvm/Transforms/Utils/Local.h"
21 #include "llvm/ConstantHandling.h"
22 #include "llvm/iMemory.h"
23 #include "llvm/iOther.h"
24 #include "llvm/iPHINode.h"
25 #include "llvm/iOperators.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/InstIterator.h"
28 #include "llvm/Support/InstVisitor.h"
29 #include "Support/StatisticReporter.h"
32 static Statistic<> NumCombined("instcombine\t- Number of insts combined");
35 class InstCombiner : public FunctionPass,
36 public InstVisitor<InstCombiner, Instruction*> {
37 // Worklist of all of the instructions that need to be simplified.
38 std::vector<Instruction*> WorkList;
40 void AddUsesToWorkList(Instruction &I) {
41 // The instruction was simplified, add all users of the instruction to
42 // the work lists because they might get more simplified now...
44 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
46 WorkList.push_back(cast<Instruction>(*UI));
50 virtual bool runOnFunction(Function &F);
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 // Visitation implementation - Implement instruction combining for different
57 // instruction types. The semantics are as follows:
59 // null - No change was made
60 // I - Change was made, I is still valid
61 // otherwise - Change was made, replace I with returned instruction
63 Instruction *visitNot(UnaryOperator &I);
64 Instruction *visitAdd(BinaryOperator &I);
65 Instruction *visitSub(BinaryOperator &I);
66 Instruction *visitMul(BinaryOperator &I);
67 Instruction *visitDiv(BinaryOperator &I);
68 Instruction *visitRem(BinaryOperator &I);
69 Instruction *visitAnd(BinaryOperator &I);
70 Instruction *visitOr (BinaryOperator &I);
71 Instruction *visitXor(BinaryOperator &I);
72 Instruction *visitSetCondInst(BinaryOperator &I);
73 Instruction *visitShiftInst(Instruction &I);
74 Instruction *visitCastInst(CastInst &CI);
75 Instruction *visitPHINode(PHINode &PN);
76 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
78 // visitInstruction - Specify what to return for unhandled instructions...
79 Instruction *visitInstruction(Instruction &I) { return 0; }
82 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
86 Instruction *InstCombiner::visitNot(UnaryOperator &I) {
88 if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(0)))
89 if (Op->getOpcode() == Instruction::Not) {
90 AddUsesToWorkList(I); // Add all modified instrs to worklist
91 I.replaceAllUsesWith(Op->getOperand(0));
98 // Make sure that this instruction has a constant on the right hand side if it
99 // has any constant arguments. If not, fix it an return true.
101 static bool SimplifyBinOp(BinaryOperator &I) {
102 if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1)))
103 return !I.swapOperands();
107 // dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
108 // instruction if the LHS is a constant zero (which is the 'negate' form).
110 static inline Value *dyn_castNegInst(Value *V) {
111 Instruction *I = dyn_cast<Instruction>(V);
112 if (!I || I->getOpcode() != Instruction::Sub) return 0;
114 if (I->getOperand(0) == Constant::getNullValue(I->getType()))
115 return I->getOperand(1);
119 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
120 bool Changed = SimplifyBinOp(I);
121 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
123 // Eliminate 'add int %X, 0'
124 if (RHS == Constant::getNullValue(I.getType())) {
125 AddUsesToWorkList(I); // Add all modified instrs to worklist
126 I.replaceAllUsesWith(LHS);
131 if (Value *V = dyn_castNegInst(LHS))
132 return BinaryOperator::create(Instruction::Sub, RHS, V);
135 if (Value *V = dyn_castNegInst(RHS))
136 return BinaryOperator::create(Instruction::Sub, LHS, V);
138 // Simplify add instructions with a constant RHS...
139 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
140 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
141 if (ILHS->getOpcode() == Instruction::Add &&
142 isa<Constant>(ILHS->getOperand(1))) {
144 // %Y = add int %X, 1
145 // %Z = add int %Y, 1
147 // %Z = add int %X, 2
149 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
150 I.setOperand(0, ILHS->getOperand(0));
151 I.setOperand(1, Val);
158 return Changed ? &I : 0;
161 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
162 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
164 if (Op0 == Op1) { // sub X, X -> 0
165 AddUsesToWorkList(I); // Add all modified instrs to worklist
166 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
170 // If this is a subtract instruction with a constant RHS, convert it to an add
171 // instruction of a negative constant
173 if (Constant *Op2 = dyn_cast<Constant>(Op1))
174 if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS
175 return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName());
177 // If this is a 'C = x-B', check to see if 'B = -A', so that C = x+A...
178 if (Value *V = dyn_castNegInst(Op1))
179 return BinaryOperator::create(Instruction::Add, Op0, V);
181 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
182 // not used by anyone else...
184 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
185 if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) {
186 // Swap the two operands of the subexpr...
187 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
188 Op1I->setOperand(0, IIOp1);
189 Op1I->setOperand(1, IIOp0);
191 // Create the new top level add instruction...
192 return BinaryOperator::create(Instruction::Add, Op0, Op1);
197 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
198 bool Changed = SimplifyBinOp(I);
199 Value *Op1 = I.getOperand(0);
201 // Simplify add instructions with a constant RHS...
202 if (Constant *Op2 = dyn_cast<Constant>(I.getOperand(1))) {
203 if (I.getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){
204 // Eliminate 'mul int %X, 1'
205 AddUsesToWorkList(I); // Add all modified instrs to worklist
206 I.replaceAllUsesWith(Op1);
209 } else if (I.getType()->isIntegral() &&
210 cast<ConstantInt>(Op2)->equalsInt(2)) {
211 // Convert 'mul int %X, 2' to 'add int %X, %X'
212 return BinaryOperator::create(Instruction::Add, Op1, Op1, I.getName());
214 } else if (Op2->isNullValue()) {
215 // Eliminate 'mul int %X, 0'
216 AddUsesToWorkList(I); // Add all modified instrs to worklist
217 I.replaceAllUsesWith(Op2); // Set this value to zero directly
222 return Changed ? &I : 0;
226 Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
228 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
229 if (RHS->equalsInt(1)) {
230 AddUsesToWorkList(I); // Add all modified instrs to worklist
231 I.replaceAllUsesWith(I.getOperand(0));
238 Instruction *InstCombiner::visitRem(BinaryOperator &I) {
240 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
241 if (RHS->equalsInt(1)) {
242 AddUsesToWorkList(I); // Add all modified instrs to worklist
243 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
249 static Constant *getMaxValue(const Type *Ty) {
250 assert(Ty == Type::BoolTy || Ty->isIntegral());
251 if (Ty == Type::BoolTy)
252 return ConstantBool::True;
255 return ConstantSInt::get(Ty, -1);
256 else if (Ty->isUnsigned()) {
257 // Calculate -1 casted to the right type...
258 unsigned TypeBits = Ty->getPrimitiveSize()*8;
259 uint64_t Val = (uint64_t)-1LL; // All ones
260 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
261 return ConstantUInt::get(Ty, Val);
267 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
268 bool Changed = SimplifyBinOp(I);
269 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
271 // and X, X = X and X, 0 == 0
272 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) {
273 AddUsesToWorkList(I); // Add all modified instrs to worklist
274 I.replaceAllUsesWith(Op1);
279 if (Constant *RHS = dyn_cast<Constant>(Op1))
280 if (RHS == getMaxValue(I.getType())) {
281 AddUsesToWorkList(I); // Add all modified instrs to worklist
282 I.replaceAllUsesWith(Op0);
286 return Changed ? &I : 0;
291 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
292 bool Changed = SimplifyBinOp(I);
293 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
295 // or X, X = X or X, 0 == X
296 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType())) {
297 AddUsesToWorkList(I); // Add all modified instrs to worklist
298 I.replaceAllUsesWith(Op0);
303 if (Constant *RHS = dyn_cast<Constant>(Op1))
304 if (RHS == getMaxValue(I.getType())) {
305 AddUsesToWorkList(I); // Add all modified instrs to worklist
306 I.replaceAllUsesWith(Op1);
310 return Changed ? &I : 0;
315 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
316 bool Changed = SimplifyBinOp(I);
317 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
321 AddUsesToWorkList(I); // Add all modified instrs to worklist
322 I.replaceAllUsesWith(Constant::getNullValue(I.getType()));
327 if (Op1 == Constant::getNullValue(I.getType())) {
328 AddUsesToWorkList(I); // Add all modified instrs to worklist
329 I.replaceAllUsesWith(Op0);
333 return Changed ? &I : 0;
336 // isTrueWhenEqual - Return true if the specified setcondinst instruction is
337 // true when both operands are equal...
339 static bool isTrueWhenEqual(Instruction &I) {
340 return I.getOpcode() == Instruction::SetEQ ||
341 I.getOpcode() == Instruction::SetGE ||
342 I.getOpcode() == Instruction::SetLE;
345 Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
346 bool Changed = SimplifyBinOp(I);
349 if (I.getOperand(0) == I.getOperand(1)) {
350 AddUsesToWorkList(I); // Add all modified instrs to worklist
351 I.replaceAllUsesWith(ConstantBool::get(isTrueWhenEqual(I)));
355 // setcc <global*>, 0 - Global value addresses are never null!
356 if (isa<GlobalValue>(I.getOperand(0)) &&
357 isa<ConstantPointerNull>(I.getOperand(1))) {
358 AddUsesToWorkList(I); // Add all modified instrs to worklist
359 I.replaceAllUsesWith(ConstantBool::get(!isTrueWhenEqual(I)));
363 return Changed ? &I : 0;
368 Instruction *InstCombiner::visitShiftInst(Instruction &I) {
369 assert(I.getOperand(1)->getType() == Type::UByteTy);
370 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
372 // shl X, 0 == X and shr X, 0 == X
373 // shl 0, X == 0 and shr 0, X == 0
374 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
375 Op0 == Constant::getNullValue(Op0->getType())) {
376 AddUsesToWorkList(I); // Add all modified instrs to worklist
377 I.replaceAllUsesWith(Op0);
381 // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of
384 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
385 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
386 if (CUI->getValue() >= TypeBits &&
387 !(Op0->getType()->isSigned() && I.getOpcode() == Instruction::Shr)) {
388 AddUsesToWorkList(I); // Add all modified instrs to worklist
389 I.replaceAllUsesWith(Constant::getNullValue(Op0->getType()));
397 // isEliminableCastOfCast - Return true if it is valid to eliminate the CI
400 static inline bool isEliminableCastOfCast(const CastInst &CI,
401 const CastInst *CSrc) {
402 assert(CI.getOperand(0) == CSrc);
403 const Type *SrcTy = CSrc->getOperand(0)->getType();
404 const Type *MidTy = CSrc->getType();
405 const Type *DstTy = CI.getType();
407 // It is legal to eliminate the instruction if casting A->B->A if the sizes
408 // are identical and the bits don't get reinterpreted (for example
410 if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy))
413 // Allow free casting and conversion of sizes as long as the sign doesn't
415 if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral() &&
416 SrcTy->isSigned() == MidTy->isSigned() &&
417 MidTy->isSigned() == DstTy->isSigned()) {
418 // Only accept cases where we are either monotonically increasing the type
419 // size, or monotonically decreasing it.
421 unsigned SrcSize = SrcTy->getPrimitiveSize();
422 unsigned MidSize = MidTy->getPrimitiveSize();
423 unsigned DstSize = DstTy->getPrimitiveSize();
424 if (SrcSize < MidSize && MidSize < DstSize)
427 if (SrcSize > MidSize && MidSize > DstSize)
431 // Otherwise, we cannot succeed. Specifically we do not want to allow things
432 // like: short -> ushort -> uint, because this can create wrong results if
433 // the input short is negative!
439 // CastInst simplification
441 Instruction *InstCombiner::visitCastInst(CastInst &CI) {
442 // If the user is casting a value to the same type, eliminate this cast
444 if (CI.getType() == CI.getOperand(0)->getType()) {
445 AddUsesToWorkList(CI); // Add all modified instrs to worklist
446 CI.replaceAllUsesWith(CI.getOperand(0));
450 // If casting the result of another cast instruction, try to eliminate this
453 if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
454 if (isEliminableCastOfCast(CI, CSrc)) {
455 // This instruction now refers directly to the cast's src operand. This
456 // has a good chance of making CSrc dead.
457 CI.setOperand(0, CSrc->getOperand(0));
461 // If this is an A->B->A cast, and we are dealing with integral types, try
462 // to convert this into a logical 'and' instruction.
464 if (CSrc->getOperand(0)->getType() == CI.getType() &&
465 CI.getType()->isIntegral() && CSrc->getType()->isIntegral() &&
466 CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
467 CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
468 assert(CSrc->getType() != Type::ULongTy &&
469 "Cannot have type bigger than ulong!");
470 unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
471 Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
472 return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
481 // PHINode simplification
483 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
484 // If the PHI node only has one incoming value, eliminate the PHI node...
485 if (PN.getNumIncomingValues() == 1) {
486 AddUsesToWorkList(PN); // Add all modified instrs to worklist
487 PN.replaceAllUsesWith(PN.getIncomingValue(0));
495 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
496 // Is it 'getelementptr %P, uint 0' or 'getelementptr %P'
497 // If so, eliminate the noop.
498 if ((GEP.getNumOperands() == 2 &&
499 GEP.getOperand(1) == Constant::getNullValue(Type::UIntTy)) ||
500 GEP.getNumOperands() == 1) {
501 AddUsesToWorkList(GEP); // Add all modified instrs to worklist
502 GEP.replaceAllUsesWith(GEP.getOperand(0));
506 // Combine Indices - If the source pointer to this getelementptr instruction
507 // is a getelementptr instruction, combine the indices of the two
508 // getelementptr instructions into a single instruction.
510 if (GetElementPtrInst *Src =
511 dyn_cast<GetElementPtrInst>(GEP.getPointerOperand())) {
512 std::vector<Value *> Indices;
514 // Can we combine the two pointer arithmetics offsets?
515 if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
516 isa<Constant>(GEP.getOperand(1))) {
517 // Replace the index list on this GEP with the index on the getelementptr
518 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
519 Indices[0] = *cast<Constant>(Src->getOperand(1)) +
520 *cast<Constant>(GEP.getOperand(1));
521 assert(Indices[0] != 0 && "Constant folding of uint's failed!?");
523 } else if (*GEP.idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) {
524 // Otherwise we can do the fold if the first index of the GEP is a zero
525 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
526 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
529 if (!Indices.empty())
530 return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
537 bool InstCombiner::runOnFunction(Function &F) {
538 bool Changed = false;
540 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
542 while (!WorkList.empty()) {
543 Instruction *I = WorkList.back(); // Get an instruction from the worklist
546 // Now that we have an instruction, try combining it to simplify it...
547 if (Instruction *Result = visit(*I)) {
549 // Should we replace the old instruction with a new one?
551 // Instructions can end up on the worklist more than once. Make sure
552 // we do not process an instruction that has been deleted.
553 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
556 ReplaceInstWithInst(I, Result);
558 BasicBlock::iterator II = I;
560 // If the instruction was modified, it's possible that it is now dead.
562 if (dceInstruction(II)) {
563 // Instructions may end up in the worklist more than once. Erase them
565 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
572 WorkList.push_back(Result);
573 AddUsesToWorkList(*Result);
582 Pass *createInstructionCombiningPass() {
583 return new InstCombiner();