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/InstructionCombining.h"
19 #include "llvm/ConstantHandling.h"
20 #include "llvm/iMemory.h"
21 #include "llvm/iOther.h"
22 #include "llvm/iOperators.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/InstIterator.h"
25 #include "llvm/Support/InstVisitor.h"
26 #include "../TransformInternals.h"
30 class InstCombiner : public FunctionPass,
31 public InstVisitor<InstCombiner, Instruction*> {
32 // Worklist of all of the instructions that need to be simplified.
33 std::vector<Instruction*> WorkList;
35 void AddUsesToWorkList(Instruction *I) {
36 // The instruction was simplified, add all users of the instruction to
37 // the work lists because they might get more simplified now...
39 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
41 WorkList.push_back(cast<Instruction>(*UI));
45 const char *getPassName() const { return "Instruction Combining"; }
47 virtual bool runOnFunction(Function *F);
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 // Visitation implementation - Implement instruction combining for different
54 // instruction types. The semantics are as follows:
56 // null - No change was made
57 // I - Change was made, I is still valid
58 // otherwise - Change was made, replace I with returned instruction
61 Instruction *visitAdd(BinaryOperator *I);
62 Instruction *visitSub(BinaryOperator *I);
63 Instruction *visitMul(BinaryOperator *I);
64 Instruction *visitDiv(BinaryOperator *I);
65 Instruction *visitRem(BinaryOperator *I);
66 Instruction *visitAnd(BinaryOperator *I);
67 Instruction *visitOr (BinaryOperator *I);
68 Instruction *visitXor(BinaryOperator *I);
69 Instruction *visitSetCondInst(BinaryOperator *I);
70 Instruction *visitShiftInst(Instruction *I);
71 Instruction *visitCastInst(CastInst *CI);
72 Instruction *visitGetElementPtrInst(GetElementPtrInst *GEP);
73 Instruction *visitMemAccessInst(MemAccessInst *MAI);
75 // visitInstruction - Specify what to return for unhandled instructions...
76 Instruction *visitInstruction(Instruction *I) { return 0; }
82 // Make sure that this instruction has a constant on the right hand side if it
83 // has any constant arguments. If not, fix it an return true.
85 static bool SimplifyBinOp(BinaryOperator *I) {
86 if (isa<Constant>(I->getOperand(0)) && !isa<Constant>(I->getOperand(1)))
87 return !I->swapOperands();
91 Instruction *InstCombiner::visitAdd(BinaryOperator *I) {
92 if (I->use_empty()) return 0; // Don't fix dead add instructions...
93 bool Changed = SimplifyBinOp(I);
94 Value *Op1 = I->getOperand(0);
96 // Simplify add instructions with a constant RHS...
97 if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) {
98 // Eliminate 'add int %X, 0'
99 if (I->getType()->isIntegral() && Op2->isNullValue()) {
100 AddUsesToWorkList(I); // Add all modified instrs to worklist
101 I->replaceAllUsesWith(Op1);
105 if (BinaryOperator *IOp1 = dyn_cast<BinaryOperator>(Op1)) {
106 Changed |= SimplifyBinOp(IOp1);
108 if (IOp1->getOpcode() == Instruction::Add &&
109 isa<Constant>(IOp1->getOperand(1))) {
111 // %Y = add int %X, 1
112 // %Z = add int %Y, 1
114 // %Z = add int %X, 2
116 if (Constant *Val = *Op2 + *cast<Constant>(IOp1->getOperand(1))) {
117 I->setOperand(0, IOp1->getOperand(0));
118 I->setOperand(1, Val);
125 return Changed ? I : 0;
128 Instruction *InstCombiner::visitSub(BinaryOperator *I) {
129 if (I->use_empty()) return 0; // Don't fix dead add instructions...
130 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
132 if (Op0 == Op1) { // sub X, X -> 0
133 AddUsesToWorkList(I); // Add all modified instrs to worklist
134 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
138 // If this is a subtract instruction with a constant RHS, convert it to an add
139 // instruction of a negative constant
141 if (Constant *Op2 = dyn_cast<Constant>(Op1))
142 if (Constant *RHS = *Constant::getNullValue(I->getType()) - *Op2) // 0 - RHS
143 return BinaryOperator::create(Instruction::Add, Op0, RHS, I->getName());
148 Instruction *InstCombiner::visitMul(BinaryOperator *I) {
149 if (I->use_empty()) return 0; // Don't fix dead instructions...
150 bool Changed = SimplifyBinOp(I);
151 Value *Op1 = I->getOperand(0);
153 // Simplify add instructions with a constant RHS...
154 if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(1))) {
155 if (I->getType()->isIntegral() && cast<ConstantInt>(Op2)->equalsInt(1)){
156 // Eliminate 'mul int %X, 1'
157 AddUsesToWorkList(I); // Add all modified instrs to worklist
158 I->replaceAllUsesWith(Op1);
161 } else if (I->getType()->isIntegral() &&
162 cast<ConstantInt>(Op2)->equalsInt(2)) {
163 // Convert 'mul int %X, 2' to 'add int %X, %X'
164 return BinaryOperator::create(Instruction::Add, Op1, Op1, I->getName());
166 } else if (Op2->isNullValue()) {
167 // Eliminate 'mul int %X, 0'
168 AddUsesToWorkList(I); // Add all modified instrs to worklist
169 I->replaceAllUsesWith(Op2); // Set this value to zero directly
174 return Changed ? I : 0;
178 Instruction *InstCombiner::visitDiv(BinaryOperator *I) {
179 if (I->use_empty()) return 0; // Don't fix dead instructions...
182 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1)))
183 if (RHS->equalsInt(1)) {
184 AddUsesToWorkList(I); // Add all modified instrs to worklist
185 I->replaceAllUsesWith(I->getOperand(0));
192 Instruction *InstCombiner::visitRem(BinaryOperator *I) {
193 if (I->use_empty()) return 0; // Don't fix dead instructions...
196 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1)))
197 if (RHS->equalsInt(1)) {
198 AddUsesToWorkList(I); // Add all modified instrs to worklist
199 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
205 static Constant *getMaxValue(const Type *Ty) {
206 assert(Ty == Type::BoolTy || Ty->isIntegral());
207 if (Ty == Type::BoolTy)
208 return ConstantBool::True;
210 // Calculate -1 casted to the right type...
211 unsigned TypeBits = Ty->getPrimitiveSize()*8;
212 uint64_t Val = (uint64_t)-1LL; // All ones
213 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
216 return ConstantSInt::get(Ty, (int64_t)Val);
217 else if (Ty->isUnsigned())
218 return ConstantUInt::get(Ty, Val);
224 Instruction *InstCombiner::visitAnd(BinaryOperator *I) {
225 if (I->use_empty()) return 0; // Don't fix dead instructions...
226 bool Changed = SimplifyBinOp(I);
227 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
229 // and X, X = X and X, 0 == 0
230 if (Op0 == Op1 || Op1 == Constant::getNullValue(I->getType())) {
231 AddUsesToWorkList(I); // Add all modified instrs to worklist
232 I->replaceAllUsesWith(Op1);
237 if (Constant *RHS = dyn_cast<Constant>(Op1))
238 if (RHS == getMaxValue(I->getType())) {
239 AddUsesToWorkList(I); // Add all modified instrs to worklist
240 I->replaceAllUsesWith(Op0);
244 return Changed ? I : 0;
249 Instruction *InstCombiner::visitOr(BinaryOperator *I) {
250 if (I->use_empty()) return 0; // Don't fix dead instructions...
251 bool Changed = SimplifyBinOp(I);
252 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
254 // or X, X = X or X, 0 == X
255 if (Op0 == Op1 || Op1 == Constant::getNullValue(I->getType())) {
256 AddUsesToWorkList(I); // Add all modified instrs to worklist
257 I->replaceAllUsesWith(Op0);
262 if (Constant *RHS = dyn_cast<Constant>(Op1))
263 if (RHS == getMaxValue(I->getType())) {
264 AddUsesToWorkList(I); // Add all modified instrs to worklist
265 I->replaceAllUsesWith(Op1);
269 return Changed ? I : 0;
274 Instruction *InstCombiner::visitXor(BinaryOperator *I) {
275 if (I->use_empty()) return 0; // Don't fix dead instructions...
276 bool Changed = SimplifyBinOp(I);
277 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
281 AddUsesToWorkList(I); // Add all modified instrs to worklist
282 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
287 if (Op1 == Constant::getNullValue(I->getType())) {
288 AddUsesToWorkList(I); // Add all modified instrs to worklist
289 I->replaceAllUsesWith(Op0);
293 return Changed ? I : 0;
296 Instruction *InstCombiner::visitSetCondInst(BinaryOperator *I) {
297 if (I->use_empty()) return 0; // Don't fix dead instructions...
298 bool Changed = SimplifyBinOp(I);
301 if (I->getOperand(0) == I->getOperand(1)) {
302 bool NewVal = I->getOpcode() == Instruction::SetEQ ||
303 I->getOpcode() == Instruction::SetGE ||
304 I->getOpcode() == Instruction::SetLE;
305 AddUsesToWorkList(I); // Add all modified instrs to worklist
306 I->replaceAllUsesWith(ConstantBool::get(NewVal));
310 return Changed ? I : 0;
315 Instruction *InstCombiner::visitShiftInst(Instruction *I) {
316 if (I->use_empty()) return 0; // Don't fix dead instructions...
317 assert(I->getOperand(1)->getType() == Type::UByteTy);
318 Value *Op0 = I->getOperand(0), *Op1 = I->getOperand(1);
320 // shl X, 0 == X and shr X, 0 == X
321 // shl 0, X == 0 and shr 0, X == 0
322 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
323 Op0 == Constant::getNullValue(Op0->getType())) {
324 AddUsesToWorkList(I); // Add all modified instrs to worklist
325 I->replaceAllUsesWith(Op0);
329 // shl int X, 32 = 0 and shr sbyte Y, 9 = 0, ... just don't eliminate shr of
332 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
333 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
334 if (CUI->getValue() >= TypeBits &&
335 !(Op0->getType()->isSigned() && I->getOpcode() == Instruction::Shr)) {
336 AddUsesToWorkList(I); // Add all modified instrs to worklist
337 I->replaceAllUsesWith(Constant::getNullValue(Op0->getType()));
345 // isEliminableCastOfCast - Return true if it is valid to eliminate the CI
348 static inline bool isEliminableCastOfCast(const CastInst *CI,
349 const CastInst *CSrc) {
350 assert(CI->getOperand(0) == CSrc);
351 const Type *SrcTy = CSrc->getOperand(0)->getType();
352 const Type *MidTy = CSrc->getType();
353 const Type *DstTy = CI->getType();
355 // It is legal to eliminate the instruction if casting A->B->A
356 if (SrcTy == DstTy) return true;
358 // Allow free casting and conversion of sizes as long as the sign doesn't
360 if (SrcTy->isSigned() == MidTy->isSigned() &&
361 MidTy->isSigned() == DstTy->isSigned())
364 // Otherwise, we cannot succeed. Specifically we do not want to allow things
365 // like: short -> ushort -> uint, because this can create wrong results if
366 // the input short is negative!
372 // CastInst simplification
374 Instruction *InstCombiner::visitCastInst(CastInst *CI) {
375 // If the user is casting a value to the same type, eliminate this cast
377 if (CI->getType() == CI->getOperand(0)->getType() && !CI->use_empty()) {
378 AddUsesToWorkList(CI); // Add all modified instrs to worklist
379 CI->replaceAllUsesWith(CI->getOperand(0));
384 // If casting the result of another cast instruction, try to eliminate this
387 if (CastInst *CSrc = dyn_cast<CastInst>(CI->getOperand(0)))
388 if (isEliminableCastOfCast(CI, CSrc)) {
389 // This instruction now refers directly to the cast's src operand. This
390 // has a good chance of making CSrc dead.
391 CI->setOperand(0, CSrc->getOperand(0));
400 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst *GEP) {
401 // Is it getelementptr %P, uint 0
402 // If so, elminate the noop.
403 if (GEP->getNumOperands() == 2 && !GEP->use_empty() &&
404 GEP->getOperand(1) == Constant::getNullValue(Type::UIntTy)) {
405 AddUsesToWorkList(GEP); // Add all modified instrs to worklist
406 GEP->replaceAllUsesWith(GEP->getOperand(0));
410 return visitMemAccessInst(GEP);
414 // Combine Indices - If the source pointer to this mem access instruction is a
415 // getelementptr instruction, combine the indices of the GEP into this
418 Instruction *InstCombiner::visitMemAccessInst(MemAccessInst *MAI) {
419 GetElementPtrInst *Src =
420 dyn_cast<GetElementPtrInst>(MAI->getPointerOperand());
423 std::vector<Value *> Indices;
425 // Only special case we have to watch out for is pointer arithmetic on the
427 unsigned FirstIdx = MAI->getFirstIndexOperandNumber();
428 if (FirstIdx == MAI->getNumOperands() ||
429 (FirstIdx == MAI->getNumOperands()-1 &&
430 MAI->getOperand(FirstIdx) == ConstantUInt::get(Type::UIntTy, 0))) {
431 // Replace the index list on this MAI with the index on the getelementptr
432 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
433 } else if (*MAI->idx_begin() == ConstantUInt::get(Type::UIntTy, 0)) {
434 // Otherwise we can do the fold if the first index of the GEP is a zero
435 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
436 Indices.insert(Indices.end(), MAI->idx_begin()+1, MAI->idx_end());
439 if (Indices.empty()) return 0; // Can't do the fold?
441 switch (MAI->getOpcode()) {
442 case Instruction::GetElementPtr:
443 return new GetElementPtrInst(Src->getOperand(0), Indices, MAI->getName());
444 case Instruction::Load:
445 return new LoadInst(Src->getOperand(0), Indices, MAI->getName());
446 case Instruction::Store:
447 return new StoreInst(MAI->getOperand(0), Src->getOperand(0), Indices);
449 assert(0 && "Unknown memaccessinst!");
457 bool InstCombiner::runOnFunction(Function *F) {
458 bool Changed = false;
460 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
462 while (!WorkList.empty()) {
463 Instruction *I = WorkList.back(); // Get an instruction from the worklist
466 // Now that we have an instruction, try combining it to simplify it...
467 Instruction *Result = visit(I);
469 // Should we replace the old instruction with a new one?
471 ReplaceInstWithInst(I, Result);
473 WorkList.push_back(Result);
474 AddUsesToWorkList(Result);
482 Pass *createInstructionCombiningPass() {
483 return new InstCombiner();