1 //===- InstructionCombining.cpp - Combine multiple instructions -----------===//
3 // InstructionCombining - Combine instructions to form fewer, simple
4 // instructions. This pass does not modify the CFG This pass is where algebraic
5 // simplification happens.
7 // This pass combines things like:
13 // This is a simple worklist driven algorithm.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
19 #include "llvm/Transforms/Utils/Local.h"
20 #include "llvm/ConstantHandling.h"
21 #include "llvm/iMemory.h"
22 #include "llvm/iOther.h"
23 #include "llvm/iPHINode.h"
24 #include "llvm/iOperators.h"
25 #include "llvm/Pass.h"
26 #include "llvm/DerivedTypes.h"
27 #include "llvm/Support/InstIterator.h"
28 #include "llvm/Support/InstVisitor.h"
29 #include "Support/Statistic.h"
33 Statistic<> NumCombined ("instcombine", "Number of insts combined");
34 Statistic<> NumConstProp("instcombine", "Number of constant folds");
35 Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
37 class InstCombiner : public FunctionPass,
38 public InstVisitor<InstCombiner, Instruction*> {
39 // Worklist of all of the instructions that need to be simplified.
40 std::vector<Instruction*> WorkList;
42 void AddUsesToWorkList(Instruction &I) {
43 // The instruction was simplified, add all users of the instruction to
44 // the work lists because they might get more simplified now...
46 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
48 WorkList.push_back(cast<Instruction>(*UI));
51 // removeFromWorkList - remove all instances of I from the worklist.
52 void removeFromWorkList(Instruction *I);
54 virtual bool runOnFunction(Function &F);
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 // Visitation implementation - Implement instruction combining for different
61 // instruction types. The semantics are as follows:
63 // null - No change was made
64 // I - Change was made, I is still valid, I may be dead though
65 // otherwise - Change was made, replace I with returned instruction
67 Instruction *visitAdd(BinaryOperator &I);
68 Instruction *visitSub(BinaryOperator &I);
69 Instruction *visitMul(BinaryOperator &I);
70 Instruction *visitDiv(BinaryOperator &I);
71 Instruction *visitRem(BinaryOperator &I);
72 Instruction *visitAnd(BinaryOperator &I);
73 Instruction *visitOr (BinaryOperator &I);
74 Instruction *visitXor(BinaryOperator &I);
75 Instruction *visitSetCondInst(BinaryOperator &I);
76 Instruction *visitShiftInst(Instruction &I);
77 Instruction *visitCastInst(CastInst &CI);
78 Instruction *visitPHINode(PHINode &PN);
79 Instruction *visitGetElementPtrInst(GetElementPtrInst &GEP);
80 Instruction *visitAllocationInst(AllocationInst &AI);
82 // visitInstruction - Specify what to return for unhandled instructions...
83 Instruction *visitInstruction(Instruction &I) { return 0; }
85 // InsertNewInstBefore - insert an instruction New before instruction Old
86 // in the program. Add the new instruction to the worklist.
88 void InsertNewInstBefore(Instruction *New, Instruction &Old) {
89 assert(New && New->getParent() == 0 &&
90 "New instruction already inserted into a basic block!");
91 BasicBlock *BB = Old.getParent();
92 BB->getInstList().insert(&Old, New); // Insert inst
93 WorkList.push_back(New); // Add to worklist
96 // ReplaceInstUsesWith - This method is to be used when an instruction is
97 // found to be dead, replacable with another preexisting expression. Here
98 // we add all uses of I to the worklist, replace all uses of I with the new
99 // value, then return I, so that the inst combiner will know that I was
102 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
103 AddUsesToWorkList(I); // Add all modified instrs to worklist
104 I.replaceAllUsesWith(V);
109 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
113 // Make sure that this instruction has a constant on the right hand side if it
114 // has any constant arguments. If not, fix it an return true.
116 static bool SimplifyBinOp(BinaryOperator &I) {
117 if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1)))
118 return !I.swapOperands();
122 // dyn_castNegInst - Given a 'sub' instruction, return the RHS of the
123 // instruction if the LHS is a constant zero (which is the 'negate' form).
125 static inline Value *dyn_castNegInst(Value *V) {
126 Instruction *I = dyn_cast<Instruction>(V);
127 if (!I || I->getOpcode() != Instruction::Sub) return 0;
129 if (I->getOperand(0) == Constant::getNullValue(I->getType()))
130 return I->getOperand(1);
134 static inline Value *dyn_castNotInst(Value *V) {
135 Instruction *I = dyn_cast<Instruction>(V);
136 if (!I || I->getOpcode() != Instruction::Xor) return 0;
138 if (ConstantIntegral *CI = dyn_cast<ConstantIntegral>(I->getOperand(1)))
139 if (CI->isAllOnesValue())
140 return I->getOperand(0);
144 Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
145 bool Changed = SimplifyBinOp(I);
146 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
148 // Eliminate 'add int %X, 0'
149 if (RHS == Constant::getNullValue(I.getType()))
150 return ReplaceInstUsesWith(I, LHS);
153 if (Value *V = dyn_castNegInst(LHS))
154 return BinaryOperator::create(Instruction::Sub, RHS, V);
157 if (Value *V = dyn_castNegInst(RHS))
158 return BinaryOperator::create(Instruction::Sub, LHS, V);
160 // Simplify add instructions with a constant RHS...
161 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
162 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
163 if (ILHS->getOpcode() == Instruction::Add &&
164 isa<Constant>(ILHS->getOperand(1))) {
166 // %Y = add int %X, 1
167 // %Z = add int %Y, 1
169 // %Z = add int %X, 2
171 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
172 I.setOperand(0, ILHS->getOperand(0));
173 I.setOperand(1, Val);
180 return Changed ? &I : 0;
183 Instruction *InstCombiner::visitSub(BinaryOperator &I) {
184 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
186 if (Op0 == Op1) // sub X, X -> 0
187 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
189 // If this is a subtract instruction with a constant RHS, convert it to an add
190 // instruction of a negative constant
192 if (Constant *Op2 = dyn_cast<Constant>(Op1))
193 if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS
194 return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName());
196 // If this is a 'B = x-(-A)', change to B = x+A...
197 if (Value *V = dyn_castNegInst(Op1))
198 return BinaryOperator::create(Instruction::Add, Op0, V);
200 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression is
201 // not used by anyone else...
203 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
204 if (Op1I->use_size() == 1 && Op1I->getOpcode() == Instruction::Sub) {
205 // Swap the two operands of the subexpr...
206 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
207 Op1I->setOperand(0, IIOp1);
208 Op1I->setOperand(1, IIOp0);
210 // Create the new top level add instruction...
211 return BinaryOperator::create(Instruction::Add, Op0, Op1);
216 Instruction *InstCombiner::visitMul(BinaryOperator &I) {
217 bool Changed = SimplifyBinOp(I);
218 Value *Op1 = I.getOperand(0);
220 // Simplify mul instructions with a constant RHS...
221 if (Constant *Op2 = dyn_cast<Constant>(I.getOperand(1))) {
222 if (I.getType()->isInteger() && cast<ConstantInt>(Op2)->equalsInt(1))
223 return ReplaceInstUsesWith(I, Op1); // Eliminate 'mul int %X, 1'
225 if (I.getType()->isInteger() && cast<ConstantInt>(Op2)->equalsInt(2))
226 // Convert 'mul int %X, 2' to 'add int %X, %X'
227 return BinaryOperator::create(Instruction::Add, Op1, Op1, I.getName());
229 if (Op2->isNullValue())
230 return ReplaceInstUsesWith(I, Op2); // Eliminate 'mul int %X, 0'
233 return Changed ? &I : 0;
237 Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
239 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
240 if (RHS->equalsInt(1))
241 return ReplaceInstUsesWith(I, I.getOperand(0));
246 Instruction *InstCombiner::visitRem(BinaryOperator &I) {
248 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1)))
249 if (RHS->equalsInt(1))
250 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
255 // isMaxValueMinusOne - return true if this is Max-1
256 static bool isMaxValueMinusOne(const ConstantInt *C) {
257 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
258 // Calculate -1 casted to the right type...
259 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
260 uint64_t Val = ~0ULL; // All ones
261 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
262 return CU->getValue() == Val-1;
265 const ConstantSInt *CS = cast<ConstantSInt>(C);
267 // Calculate 0111111111..11111
268 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
269 int64_t Val = INT64_MAX; // All ones
270 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
271 return CS->getValue() == Val-1;
274 // isMinValuePlusOne - return true if this is Min+1
275 static bool isMinValuePlusOne(const ConstantInt *C) {
276 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
277 return CU->getValue() == 1;
279 const ConstantSInt *CS = cast<ConstantSInt>(C);
281 // Calculate 1111111111000000000000
282 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
283 int64_t Val = -1; // All ones
284 Val <<= TypeBits-1; // Shift over to the right spot
285 return CS->getValue() == Val+1;
289 Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
290 bool Changed = SimplifyBinOp(I);
291 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
293 // and X, X = X and X, 0 == 0
294 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
295 return ReplaceInstUsesWith(I, Op1);
298 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
299 if (RHS->isAllOnesValue())
300 return ReplaceInstUsesWith(I, Op0);
302 // and (not A), (not B) == not (or A, B)
303 if (Op0->use_size() == 1 && Op1->use_size() == 1)
304 if (Value *A = dyn_castNotInst(Op0))
305 if (Value *B = dyn_castNotInst(Op1)) {
306 Instruction *Or = BinaryOperator::create(Instruction::Or, A, B,
307 I.getName()+".demorgan");
308 InsertNewInstBefore(Or, I);
309 return BinaryOperator::createNot(Or, I.getName());
312 return Changed ? &I : 0;
317 Instruction *InstCombiner::visitOr(BinaryOperator &I) {
318 bool Changed = SimplifyBinOp(I);
319 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
321 // or X, X = X or X, 0 == X
322 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
323 return ReplaceInstUsesWith(I, Op0);
326 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
327 if (RHS->isAllOnesValue())
328 return ReplaceInstUsesWith(I, Op1);
330 return Changed ? &I : 0;
335 Instruction *InstCombiner::visitXor(BinaryOperator &I) {
336 bool Changed = SimplifyBinOp(I);
337 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
341 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
343 if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) {
345 if (Op1C->isNullValue())
346 return ReplaceInstUsesWith(I, Op0);
348 // Is this a "NOT" instruction?
349 if (Op1C->isAllOnesValue()) {
350 // xor (xor X, -1), -1 = not (not X) = X
351 if (Value *X = dyn_castNotInst(Op0))
352 return ReplaceInstUsesWith(I, X);
354 // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
355 if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0))
356 if (SCI->use_size() == 1)
357 return new SetCondInst(SCI->getInverseCondition(),
358 SCI->getOperand(0), SCI->getOperand(1));
362 return Changed ? &I : 0;
365 // AddOne, SubOne - Add or subtract a constant one from an integer constant...
366 static Constant *AddOne(ConstantInt *C) {
367 Constant *Result = *C + *ConstantInt::get(C->getType(), 1);
368 assert(Result && "Constant folding integer addition failed!");
371 static Constant *SubOne(ConstantInt *C) {
372 Constant *Result = *C - *ConstantInt::get(C->getType(), 1);
373 assert(Result && "Constant folding integer addition failed!");
377 // isTrueWhenEqual - Return true if the specified setcondinst instruction is
378 // true when both operands are equal...
380 static bool isTrueWhenEqual(Instruction &I) {
381 return I.getOpcode() == Instruction::SetEQ ||
382 I.getOpcode() == Instruction::SetGE ||
383 I.getOpcode() == Instruction::SetLE;
386 Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
387 bool Changed = SimplifyBinOp(I);
388 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
389 const Type *Ty = Op0->getType();
393 return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
395 // setcc <global*>, 0 - Global value addresses are never null!
396 if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1))
397 return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
399 // setcc's with boolean values can always be turned into bitwise operations
400 if (Ty == Type::BoolTy) {
401 // If this is <, >, or !=, we can change this into a simple xor instruction
402 if (!isTrueWhenEqual(I))
403 return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName());
405 // Otherwise we need to make a temporary intermediate instruction and insert
406 // it into the instruction stream. This is what we are after:
408 // seteq bool %A, %B -> ~(A^B)
409 // setle bool %A, %B -> ~A | B
410 // setge bool %A, %B -> A | ~B
412 if (I.getOpcode() == Instruction::SetEQ) { // seteq case
413 Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1,
415 InsertNewInstBefore(Xor, I);
416 return BinaryOperator::createNot(Xor, I.getName());
419 // Handle the setXe cases...
420 assert(I.getOpcode() == Instruction::SetGE ||
421 I.getOpcode() == Instruction::SetLE);
423 if (I.getOpcode() == Instruction::SetGE)
424 std::swap(Op0, Op1); // Change setge -> setle
426 // Now we just have the SetLE case.
427 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
428 InsertNewInstBefore(Not, I);
429 return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName());
432 // Check to see if we are doing one of many comparisons against constant
433 // integers at the end of their ranges...
435 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
436 // Check to see if we are comparing against the minimum or maximum value...
437 if (CI->isMinValue()) {
438 if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE
439 return ReplaceInstUsesWith(I, ConstantBool::False);
440 if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE
441 return ReplaceInstUsesWith(I, ConstantBool::True);
442 if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN
443 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
444 if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN
445 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
447 } else if (CI->isMaxValue()) {
448 if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE
449 return ReplaceInstUsesWith(I, ConstantBool::False);
450 if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE
451 return ReplaceInstUsesWith(I, ConstantBool::True);
452 if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX
453 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
454 if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX
455 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
457 // Comparing against a value really close to min or max?
458 } else if (isMinValuePlusOne(CI)) {
459 if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN
460 return BinaryOperator::create(Instruction::SetEQ, Op0,
461 SubOne(CI), I.getName());
462 if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN
463 return BinaryOperator::create(Instruction::SetNE, Op0,
464 SubOne(CI), I.getName());
466 } else if (isMaxValueMinusOne(CI)) {
467 if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX
468 return BinaryOperator::create(Instruction::SetEQ, Op0,
469 AddOne(CI), I.getName());
470 if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX
471 return BinaryOperator::create(Instruction::SetNE, Op0,
472 AddOne(CI), I.getName());
476 return Changed ? &I : 0;
481 Instruction *InstCombiner::visitShiftInst(Instruction &I) {
482 assert(I.getOperand(1)->getType() == Type::UByteTy);
483 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
485 // shl X, 0 == X and shr X, 0 == X
486 // shl 0, X == 0 and shr 0, X == 0
487 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
488 Op0 == Constant::getNullValue(Op0->getType()))
489 return ReplaceInstUsesWith(I, Op0);
491 // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr of
494 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
495 if (I.getOpcode() == Instruction::Shr) {
496 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
497 if (CUI->getValue() >= TypeBits && !(Op0->getType()->isSigned()))
498 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
501 // Check to see if we are shifting left by 1. If so, turn it into an add
503 if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1))
504 // Convert 'shl int %X, 2' to 'add int %X, %X'
505 return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
509 // shr int -1, X = -1 (for any arithmetic shift rights of ~0)
510 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
511 if (I.getOpcode() == Instruction::Shr && CSI->isAllOnesValue())
512 return ReplaceInstUsesWith(I, CSI);
518 // isEliminableCastOfCast - Return true if it is valid to eliminate the CI
521 static inline bool isEliminableCastOfCast(const CastInst &CI,
522 const CastInst *CSrc) {
523 assert(CI.getOperand(0) == CSrc);
524 const Type *SrcTy = CSrc->getOperand(0)->getType();
525 const Type *MidTy = CSrc->getType();
526 const Type *DstTy = CI.getType();
528 // It is legal to eliminate the instruction if casting A->B->A if the sizes
529 // are identical and the bits don't get reinterpreted (for example
530 // int->float->int would not be allowed)
531 if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy))
534 // Allow free casting and conversion of sizes as long as the sign doesn't
536 if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
537 unsigned SrcSize = SrcTy->getPrimitiveSize();
538 unsigned MidSize = MidTy->getPrimitiveSize();
539 unsigned DstSize = DstTy->getPrimitiveSize();
541 // Cases where we are monotonically decreasing the size of the type are
542 // always ok, regardless of what sign changes are going on.
544 if (SrcSize >= MidSize && MidSize >= DstSize)
547 // Cases where the source and destination type are the same, but the middle
548 // type is bigger are noops.
550 if (SrcSize == DstSize && MidSize > SrcSize)
553 // If we are monotonically growing, things are more complex.
555 if (SrcSize <= MidSize && MidSize <= DstSize) {
556 // We have eight combinations of signedness to worry about. Here's the
558 static const int SignTable[8] = {
559 // CODE, SrcSigned, MidSigned, DstSigned, Comment
560 1, // U U U Always ok
561 1, // U U S Always ok
562 3, // U S U Ok iff SrcSize != MidSize
563 3, // U S S Ok iff SrcSize != MidSize
565 2, // S U S Ok iff MidSize == DstSize
566 1, // S S U Always ok
567 1, // S S S Always ok
570 // Choose an action based on the current entry of the signtable that this
571 // cast of cast refers to...
572 unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned();
573 switch (SignTable[Row]) {
574 case 0: return false; // Never ok
575 case 1: return true; // Always ok
576 case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize
577 case 3: // Ok iff SrcSize != MidSize
578 return SrcSize != MidSize || SrcTy == Type::BoolTy;
579 default: assert(0 && "Bad entry in sign table!");
584 // Otherwise, we cannot succeed. Specifically we do not want to allow things
585 // like: short -> ushort -> uint, because this can create wrong results if
586 // the input short is negative!
592 // CastInst simplification
594 Instruction *InstCombiner::visitCastInst(CastInst &CI) {
595 // If the user is casting a value to the same type, eliminate this cast
597 if (CI.getType() == CI.getOperand(0)->getType())
598 return ReplaceInstUsesWith(CI, CI.getOperand(0));
600 // If casting the result of another cast instruction, try to eliminate this
603 if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
604 if (isEliminableCastOfCast(CI, CSrc)) {
605 // This instruction now refers directly to the cast's src operand. This
606 // has a good chance of making CSrc dead.
607 CI.setOperand(0, CSrc->getOperand(0));
611 // If this is an A->B->A cast, and we are dealing with integral types, try
612 // to convert this into a logical 'and' instruction.
614 if (CSrc->getOperand(0)->getType() == CI.getType() &&
615 CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
616 CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
617 CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
618 assert(CSrc->getType() != Type::ULongTy &&
619 "Cannot have type bigger than ulong!");
620 unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
621 Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
622 return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
631 // PHINode simplification
633 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
634 // If the PHI node only has one incoming value, eliminate the PHI node...
635 if (PN.getNumIncomingValues() == 1)
636 return ReplaceInstUsesWith(PN, PN.getIncomingValue(0));
638 // Otherwise if all of the incoming values are the same for the PHI, replace
639 // the PHI node with the incoming value.
642 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
643 if (PN.getIncomingValue(i) != &PN) // Not the PHI node itself...
644 if (InVal && PN.getIncomingValue(i) != InVal)
645 return 0; // Not the same, bail out.
647 InVal = PN.getIncomingValue(i);
649 // The only case that could cause InVal to be null is if we have a PHI node
650 // that only has entries for itself. In this case, there is no entry into the
651 // loop, so kill the PHI.
653 if (InVal == 0) InVal = Constant::getNullValue(PN.getType());
655 // All of the incoming values are the same, replace the PHI node now.
656 return ReplaceInstUsesWith(PN, InVal);
660 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
661 // Is it 'getelementptr %P, uint 0' or 'getelementptr %P'
662 // If so, eliminate the noop.
663 if ((GEP.getNumOperands() == 2 &&
664 GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
665 GEP.getNumOperands() == 1)
666 return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
668 // Combine Indices - If the source pointer to this getelementptr instruction
669 // is a getelementptr instruction, combine the indices of the two
670 // getelementptr instructions into a single instruction.
672 if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) {
673 std::vector<Value *> Indices;
675 // Can we combine the two pointer arithmetics offsets?
676 if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
677 isa<Constant>(GEP.getOperand(1))) {
678 // Replace the index list on this GEP with the index on the getelementptr
679 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
680 Indices[0] = *cast<Constant>(Src->getOperand(1)) +
681 *cast<Constant>(GEP.getOperand(1));
682 assert(Indices[0] != 0 && "Constant folding of uint's failed!?");
684 } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) &&
685 Src->getNumOperands() != 1) {
686 // Otherwise we can do the fold if the first index of the GEP is a zero
687 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
688 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
689 } else if (Src->getOperand(Src->getNumOperands()-1) ==
690 Constant::getNullValue(Type::LongTy)) {
691 // If the src gep ends with a constant array index, merge this get into
692 // it, even if we have a non-zero array index.
693 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1);
694 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
697 if (!Indices.empty())
698 return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
700 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) {
701 // GEP of global variable. If all of the indices for this GEP are
702 // constants, we can promote this to a constexpr instead of an instruction.
704 // Scan for nonconstants...
705 std::vector<Constant*> Indices;
706 User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
707 for (; I != E && isa<Constant>(*I); ++I)
708 Indices.push_back(cast<Constant>(*I));
710 if (I == E) { // If they are all constants...
712 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices);
714 // Replace all uses of the GEP with the new constexpr...
715 return ReplaceInstUsesWith(GEP, CE);
722 Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
723 // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
724 if (AI.isArrayAllocation()) // Check C != 1
725 if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
726 const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
727 AllocationInst *New = 0;
729 // Create and insert the replacement instruction...
730 if (isa<MallocInst>(AI))
731 New = new MallocInst(NewTy, 0, AI.getName(), &AI);
733 assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
734 New = new AllocaInst(NewTy, 0, AI.getName(), &AI);
737 // Scan to the end of the allocation instructions, to skip over a block of
738 // allocas if possible...
740 BasicBlock::iterator It = New;
741 while (isa<AllocationInst>(*It)) ++It;
743 // Now that I is pointing to the first non-allocation-inst in the block,
744 // insert our getelementptr instruction...
746 std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy));
747 Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It);
749 // Now make everything use the getelementptr instead of the original
751 ReplaceInstUsesWith(AI, V);
759 void InstCombiner::removeFromWorkList(Instruction *I) {
760 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
764 bool InstCombiner::runOnFunction(Function &F) {
765 bool Changed = false;
767 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
769 while (!WorkList.empty()) {
770 Instruction *I = WorkList.back(); // Get an instruction from the worklist
773 // Check to see if we can DCE or ConstantPropagate the instruction...
774 // Check to see if we can DIE the instruction...
775 if (isInstructionTriviallyDead(I)) {
776 // Add operands to the worklist...
777 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
778 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
779 WorkList.push_back(Op);
782 BasicBlock::iterator BBI = I;
783 if (dceInstruction(BBI)) {
784 removeFromWorkList(I);
789 // Instruction isn't dead, see if we can constant propagate it...
790 if (Constant *C = ConstantFoldInstruction(I)) {
791 // Add operands to the worklist...
792 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
793 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
794 WorkList.push_back(Op);
795 I->replaceAllUsesWith(C);
797 BasicBlock::iterator BBI = I;
798 if (dceInstruction(BBI)) {
799 removeFromWorkList(I);
804 // Now that we have an instruction, try combining it to simplify it...
805 if (Instruction *Result = visit(*I)) {
807 // Should we replace the old instruction with a new one?
809 // Instructions can end up on the worklist more than once. Make sure
810 // we do not process an instruction that has been deleted.
811 removeFromWorkList(I);
812 ReplaceInstWithInst(I, Result);
814 BasicBlock::iterator II = I;
816 // If the instruction was modified, it's possible that it is now dead.
818 if (dceInstruction(II)) {
819 // Instructions may end up in the worklist more than once. Erase them
821 removeFromWorkList(I);
827 WorkList.push_back(Result);
828 AddUsesToWorkList(*Result);
837 Pass *createInstructionCombiningPass() {
838 return new InstCombiner();