blob: 0c46a7b2233ede95b729a4155cc6d28ccfd2930a [file] [log] [blame]
Chris Lattnere6794492002-08-12 21:17:25 +00001//===- InstructionCombining.cpp - Combine multiple instructions -----------===//
Chris Lattnerca081252001-12-14 16:52:21 +00002//
3// InstructionCombining - Combine instructions to form fewer, simple
Chris Lattner99f48c62002-09-02 04:59:56 +00004// instructions. This pass does not modify the CFG This pass is where algebraic
5// simplification happens.
Chris Lattnerca081252001-12-14 16:52:21 +00006//
7// This pass combines things like:
8// %Y = add int 1, %X
9// %Z = add int 1, %Y
10// into:
11// %Z = add int 2, %X
12//
13// This is a simple worklist driven algorithm.
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner9b55e5a2002-05-07 18:12:18 +000018#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattnerae7a0d32002-08-02 19:29:35 +000019#include "llvm/Transforms/Utils/Local.h"
Chris Lattner65b529f2002-04-08 20:18:09 +000020#include "llvm/ConstantHandling.h"
Chris Lattnerca081252001-12-14 16:52:21 +000021#include "llvm/iMemory.h"
Chris Lattner3a60d042002-04-15 19:45:29 +000022#include "llvm/iOther.h"
Chris Lattnerbbbdd852002-05-06 18:06:38 +000023#include "llvm/iPHINode.h"
Chris Lattner260ab202002-04-18 17:39:14 +000024#include "llvm/iOperators.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000025#include "llvm/Pass.h"
Chris Lattner1085bdf2002-11-04 16:18:53 +000026#include "llvm/DerivedTypes.h"
Chris Lattner60a65912002-02-12 21:07:25 +000027#include "llvm/Support/InstIterator.h"
Chris Lattner260ab202002-04-18 17:39:14 +000028#include "llvm/Support/InstVisitor.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000029#include "Support/Statistic.h"
Chris Lattner053c0932002-05-14 15:24:07 +000030#include <algorithm>
Chris Lattnerca081252001-12-14 16:52:21 +000031
Chris Lattner260ab202002-04-18 17:39:14 +000032namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000033 Statistic<> NumCombined ("instcombine", "Number of insts combined");
34 Statistic<> NumConstProp("instcombine", "Number of constant folds");
35 Statistic<> NumDeadInst ("instcombine", "Number of dead inst eliminated");
36
Chris Lattnerc8e66542002-04-27 06:56:12 +000037 class InstCombiner : public FunctionPass,
Chris Lattner260ab202002-04-18 17:39:14 +000038 public InstVisitor<InstCombiner, Instruction*> {
39 // Worklist of all of the instructions that need to be simplified.
40 std::vector<Instruction*> WorkList;
41
Chris Lattner113f4f42002-06-25 16:13:24 +000042 void AddUsesToWorkList(Instruction &I) {
Chris Lattner260ab202002-04-18 17:39:14 +000043 // The instruction was simplified, add all users of the instruction to
44 // the work lists because they might get more simplified now...
45 //
Chris Lattner113f4f42002-06-25 16:13:24 +000046 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner260ab202002-04-18 17:39:14 +000047 UI != UE; ++UI)
48 WorkList.push_back(cast<Instruction>(*UI));
49 }
50
Chris Lattner99f48c62002-09-02 04:59:56 +000051 // removeFromWorkList - remove all instances of I from the worklist.
52 void removeFromWorkList(Instruction *I);
Chris Lattner260ab202002-04-18 17:39:14 +000053 public:
Chris Lattner113f4f42002-06-25 16:13:24 +000054 virtual bool runOnFunction(Function &F);
Chris Lattner260ab202002-04-18 17:39:14 +000055
Chris Lattnerf12cc842002-04-28 21:27:06 +000056 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000057 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000058 }
59
Chris Lattner260ab202002-04-18 17:39:14 +000060 // Visitation implementation - Implement instruction combining for different
61 // instruction types. The semantics are as follows:
62 // Return Value:
63 // null - No change was made
Chris Lattnere6794492002-08-12 21:17:25 +000064 // I - Change was made, I is still valid, I may be dead though
Chris Lattner260ab202002-04-18 17:39:14 +000065 // otherwise - Change was made, replace I with returned instruction
66 //
Chris Lattner113f4f42002-06-25 16:13:24 +000067 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);
Chris Lattner1085bdf2002-11-04 16:18:53 +000080 Instruction *visitAllocationInst(AllocationInst &AI);
Chris Lattner260ab202002-04-18 17:39:14 +000081
82 // visitInstruction - Specify what to return for unhandled instructions...
Chris Lattner113f4f42002-06-25 16:13:24 +000083 Instruction *visitInstruction(Instruction &I) { return 0; }
Chris Lattner6d14f2a2002-08-09 23:47:40 +000084
85 // InsertNewInstBefore - insert an instruction New before instruction Old
86 // in the program. Add the new instruction to the worklist.
87 //
88 void InsertNewInstBefore(Instruction *New, Instruction &Old) {
Chris Lattner65217ff2002-08-23 18:32:43 +000089 assert(New && New->getParent() == 0 &&
90 "New instruction already inserted into a basic block!");
Chris Lattner6d14f2a2002-08-09 23:47:40 +000091 BasicBlock *BB = Old.getParent();
92 BB->getInstList().insert(&Old, New); // Insert inst
93 WorkList.push_back(New); // Add to worklist
94 }
95
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
100 // modified.
101 //
102 Instruction *ReplaceInstUsesWith(Instruction &I, Value *V) {
103 AddUsesToWorkList(I); // Add all modified instrs to worklist
104 I.replaceAllUsesWith(V);
105 return &I;
106 }
Chris Lattner260ab202002-04-18 17:39:14 +0000107 };
Chris Lattnerb28b6802002-07-23 18:06:35 +0000108
Chris Lattnerc8b70922002-07-26 21:12:46 +0000109 RegisterOpt<InstCombiner> X("instcombine", "Combine redundant instructions");
Chris Lattner260ab202002-04-18 17:39:14 +0000110}
111
112
Chris Lattner260ab202002-04-18 17:39:14 +0000113// 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.
115//
Chris Lattner113f4f42002-06-25 16:13:24 +0000116static bool SimplifyBinOp(BinaryOperator &I) {
117 if (isa<Constant>(I.getOperand(0)) && !isa<Constant>(I.getOperand(1)))
118 return !I.swapOperands();
Chris Lattner260ab202002-04-18 17:39:14 +0000119 return false;
120}
Chris Lattnerca081252001-12-14 16:52:21 +0000121
Chris Lattner9fa53de2002-05-06 16:49:18 +0000122// 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).
124//
125static inline Value *dyn_castNegInst(Value *V) {
Chris Lattner3082c5a2003-02-18 19:28:33 +0000126 return BinaryOperator::isNeg(V) ?
127 BinaryOperator::getNegArgument(cast<BinaryOperator>(V)) : 0;
Chris Lattner9fa53de2002-05-06 16:49:18 +0000128}
129
Chris Lattner31ae8632002-08-14 17:51:49 +0000130static inline Value *dyn_castNotInst(Value *V) {
Chris Lattner3082c5a2003-02-18 19:28:33 +0000131 return BinaryOperator::isNot(V) ?
132 BinaryOperator::getNotArgument(cast<BinaryOperator>(V)) : 0;
133}
Chris Lattner31ae8632002-08-14 17:51:49 +0000134
Chris Lattner3082c5a2003-02-18 19:28:33 +0000135
136// Log2 - Calculate the log base 2 for the specified value if it is exactly a
137// power of 2.
138static unsigned Log2(uint64_t Val) {
139 assert(Val > 1 && "Values 0 and 1 should be handled elsewhere!");
140 unsigned Count = 0;
141 while (Val != 1) {
142 if (Val & 1) return 0; // Multiple bits set?
143 Val >>= 1;
144 ++Count;
145 }
146 return Count;
Chris Lattner31ae8632002-08-14 17:51:49 +0000147}
148
Chris Lattner113f4f42002-06-25 16:13:24 +0000149Instruction *InstCombiner::visitAdd(BinaryOperator &I) {
Chris Lattner260ab202002-04-18 17:39:14 +0000150 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000151 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000152
153 // Eliminate 'add int %X, 0'
Chris Lattnere6794492002-08-12 21:17:25 +0000154 if (RHS == Constant::getNullValue(I.getType()))
155 return ReplaceInstUsesWith(I, LHS);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000156
Chris Lattner147e9752002-05-08 22:46:53 +0000157 // -A + B --> B - A
Chris Lattner9fa53de2002-05-06 16:49:18 +0000158 if (Value *V = dyn_castNegInst(LHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000159 return BinaryOperator::create(Instruction::Sub, RHS, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000160
161 // A + -B --> A - B
162 if (Value *V = dyn_castNegInst(RHS))
Chris Lattner147e9752002-05-08 22:46:53 +0000163 return BinaryOperator::create(Instruction::Sub, LHS, V);
Chris Lattner260ab202002-04-18 17:39:14 +0000164
165 // Simplify add instructions with a constant RHS...
Chris Lattner9fa53de2002-05-06 16:49:18 +0000166 if (Constant *Op2 = dyn_cast<Constant>(RHS)) {
167 if (BinaryOperator *ILHS = dyn_cast<BinaryOperator>(LHS)) {
168 if (ILHS->getOpcode() == Instruction::Add &&
169 isa<Constant>(ILHS->getOperand(1))) {
Chris Lattner260ab202002-04-18 17:39:14 +0000170 // Fold:
171 // %Y = add int %X, 1
172 // %Z = add int %Y, 1
173 // into:
174 // %Z = add int %X, 2
175 //
Chris Lattner9fa53de2002-05-06 16:49:18 +0000176 if (Constant *Val = *Op2 + *cast<Constant>(ILHS->getOperand(1))) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000177 I.setOperand(0, ILHS->getOperand(0));
178 I.setOperand(1, Val);
179 return &I;
Chris Lattner04805fa2002-02-26 21:46:54 +0000180 }
Chris Lattnerca081252001-12-14 16:52:21 +0000181 }
182 }
Chris Lattnerca081252001-12-14 16:52:21 +0000183 }
184
Chris Lattner113f4f42002-06-25 16:13:24 +0000185 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000186}
187
Chris Lattner113f4f42002-06-25 16:13:24 +0000188Instruction *InstCombiner::visitSub(BinaryOperator &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000189 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000190
Chris Lattnere6794492002-08-12 21:17:25 +0000191 if (Op0 == Op1) // sub X, X -> 0
192 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner260ab202002-04-18 17:39:14 +0000193
194 // If this is a subtract instruction with a constant RHS, convert it to an add
195 // instruction of a negative constant
196 //
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000197 if (Constant *Op2 = dyn_cast<Constant>(Op1))
Chris Lattner113f4f42002-06-25 16:13:24 +0000198 if (Constant *RHS = *Constant::getNullValue(I.getType()) - *Op2) // 0 - RHS
199 return BinaryOperator::create(Instruction::Add, Op0, RHS, I.getName());
Chris Lattner260ab202002-04-18 17:39:14 +0000200
Chris Lattnere6794492002-08-12 21:17:25 +0000201 // If this is a 'B = x-(-A)', change to B = x+A...
Chris Lattner147e9752002-05-08 22:46:53 +0000202 if (Value *V = dyn_castNegInst(Op1))
203 return BinaryOperator::create(Instruction::Add, Op0, V);
Chris Lattner9fa53de2002-05-06 16:49:18 +0000204
Chris Lattner3082c5a2003-02-18 19:28:33 +0000205 // Replace (-1 - A) with (~A)...
206 if (ConstantInt *C = dyn_cast<ConstantInt>(Op0))
207 if (C->isAllOnesValue())
208 return BinaryOperator::createNot(Op1);
Chris Lattnerad3c4952002-05-09 01:29:19 +0000209
Chris Lattner3082c5a2003-02-18 19:28:33 +0000210 if (BinaryOperator *Op1I = dyn_cast<BinaryOperator>(Op1))
211 if (Op1I->use_size() == 1) {
212 // Replace (x - (y - z)) with (x + (z - y)) if the (y - z) subexpression
213 // is not used by anyone else...
214 //
215 if (Op1I->getOpcode() == Instruction::Sub) {
216 // Swap the two operands of the subexpr...
217 Value *IIOp0 = Op1I->getOperand(0), *IIOp1 = Op1I->getOperand(1);
218 Op1I->setOperand(0, IIOp1);
219 Op1I->setOperand(1, IIOp0);
220
221 // Create the new top level add instruction...
222 return BinaryOperator::create(Instruction::Add, Op0, Op1);
223 }
224
225 // Replace (A - (A & B)) with (A & ~B) if this is the only use of (A&B)...
226 //
227 if (Op1I->getOpcode() == Instruction::And &&
228 (Op1I->getOperand(0) == Op0 || Op1I->getOperand(1) == Op0)) {
229 Value *OtherOp = Op1I->getOperand(Op1I->getOperand(0) == Op0);
230
231 Instruction *NewNot = BinaryOperator::createNot(OtherOp, "B.not", &I);
232 return BinaryOperator::create(Instruction::And, Op0, NewNot);
233 }
Chris Lattnerad3c4952002-05-09 01:29:19 +0000234 }
Chris Lattner3082c5a2003-02-18 19:28:33 +0000235
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000236 return 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000237}
238
Chris Lattner113f4f42002-06-25 16:13:24 +0000239Instruction *InstCombiner::visitMul(BinaryOperator &I) {
Chris Lattner260ab202002-04-18 17:39:14 +0000240 bool Changed = SimplifyBinOp(I);
Chris Lattner3082c5a2003-02-18 19:28:33 +0000241 Value *Op0 = I.getOperand(0);
Chris Lattner260ab202002-04-18 17:39:14 +0000242
Chris Lattnere6794492002-08-12 21:17:25 +0000243 // Simplify mul instructions with a constant RHS...
Chris Lattner3082c5a2003-02-18 19:28:33 +0000244 if (Constant *Op1 = dyn_cast<Constant>(I.getOperand(1))) {
245 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
246 const Type *Ty = CI->getType();
247 uint64_t Val = Ty->isSigned() ?
248 (uint64_t)cast<ConstantSInt>(CI)->getValue() :
249 cast<ConstantUInt>(CI)->getValue();
250 switch (Val) {
251 case 0:
252 return ReplaceInstUsesWith(I, Op1); // Eliminate 'mul double %X, 0'
253 case 1:
254 return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul int %X, 1'
255 case 2: // Convert 'mul int %X, 2' to 'add int %X, %X'
256 return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
257 }
Chris Lattner31ba1292002-04-29 22:24:47 +0000258
Chris Lattner3082c5a2003-02-18 19:28:33 +0000259 if (uint64_t C = Log2(Val)) // Replace X*(2^C) with X << C
260 return new ShiftInst(Instruction::Shl, Op0,
261 ConstantUInt::get(Type::UByteTy, C));
262 } else {
263 ConstantFP *Op1F = cast<ConstantFP>(Op1);
264 if (Op1F->isNullValue())
265 return ReplaceInstUsesWith(I, Op1);
Chris Lattner31ba1292002-04-29 22:24:47 +0000266
Chris Lattner3082c5a2003-02-18 19:28:33 +0000267 // "In IEEE floating point, x*1 is not equivalent to x for nans. However,
268 // ANSI says we can drop signals, so we can do this anyway." (from GCC)
269 if (Op1F->getValue() == 1.0)
270 return ReplaceInstUsesWith(I, Op0); // Eliminate 'mul double %X, 1.0'
271 }
Chris Lattner260ab202002-04-18 17:39:14 +0000272 }
273
Chris Lattner113f4f42002-06-25 16:13:24 +0000274 return Changed ? &I : 0;
Chris Lattner260ab202002-04-18 17:39:14 +0000275}
276
Chris Lattner113f4f42002-06-25 16:13:24 +0000277Instruction *InstCombiner::visitDiv(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000278 // div X, 1 == X
Chris Lattner3082c5a2003-02-18 19:28:33 +0000279 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
Chris Lattnere6794492002-08-12 21:17:25 +0000280 if (RHS->equalsInt(1))
281 return ReplaceInstUsesWith(I, I.getOperand(0));
Chris Lattner3082c5a2003-02-18 19:28:33 +0000282
283 // Check to see if this is an unsigned division with an exact power of 2,
284 // if so, convert to a right shift.
285 if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
286 if (uint64_t Val = C->getValue()) // Don't break X / 0
287 if (uint64_t C = Log2(Val))
288 return new ShiftInst(Instruction::Shr, I.getOperand(0),
289 ConstantUInt::get(Type::UByteTy, C));
290 }
291
292 // 0 / X == 0, we don't need to preserve faults!
293 if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
294 if (LHS->equalsInt(0))
295 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
296
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000297 return 0;
298}
299
300
Chris Lattner113f4f42002-06-25 16:13:24 +0000301Instruction *InstCombiner::visitRem(BinaryOperator &I) {
Chris Lattner3082c5a2003-02-18 19:28:33 +0000302 if (ConstantInt *RHS = dyn_cast<ConstantInt>(I.getOperand(1))) {
303 if (RHS->equalsInt(1)) // X % 1 == 0
304 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
305
306 // Check to see if this is an unsigned remainder with an exact power of 2,
307 // if so, convert to a bitwise and.
308 if (ConstantUInt *C = dyn_cast<ConstantUInt>(RHS))
309 if (uint64_t Val = C->getValue()) // Don't break X % 0 (divide by zero)
310 if (Log2(Val))
311 return BinaryOperator::create(Instruction::And, I.getOperand(0),
312 ConstantUInt::get(I.getType(), Val-1));
313 }
314
315 // 0 % X == 0, we don't need to preserve faults!
316 if (ConstantInt *LHS = dyn_cast<ConstantInt>(I.getOperand(0)))
317 if (LHS->equalsInt(0))
Chris Lattnere6794492002-08-12 21:17:25 +0000318 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
319
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000320 return 0;
321}
322
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000323// isMaxValueMinusOne - return true if this is Max-1
Chris Lattnere6794492002-08-12 21:17:25 +0000324static bool isMaxValueMinusOne(const ConstantInt *C) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000325 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C)) {
326 // Calculate -1 casted to the right type...
327 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
328 uint64_t Val = ~0ULL; // All ones
329 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
330 return CU->getValue() == Val-1;
331 }
332
333 const ConstantSInt *CS = cast<ConstantSInt>(C);
334
335 // Calculate 0111111111..11111
336 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
337 int64_t Val = INT64_MAX; // All ones
338 Val >>= 64-TypeBits; // Shift out unwanted 1 bits...
339 return CS->getValue() == Val-1;
340}
341
342// isMinValuePlusOne - return true if this is Min+1
Chris Lattnere6794492002-08-12 21:17:25 +0000343static bool isMinValuePlusOne(const ConstantInt *C) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000344 if (const ConstantUInt *CU = dyn_cast<ConstantUInt>(C))
345 return CU->getValue() == 1;
346
347 const ConstantSInt *CS = cast<ConstantSInt>(C);
348
349 // Calculate 1111111111000000000000
350 unsigned TypeBits = C->getType()->getPrimitiveSize()*8;
351 int64_t Val = -1; // All ones
352 Val <<= TypeBits-1; // Shift over to the right spot
353 return CS->getValue() == Val+1;
354}
355
356
Chris Lattner113f4f42002-06-25 16:13:24 +0000357Instruction *InstCombiner::visitAnd(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000358 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000359 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000360
361 // and X, X = X and X, 0 == 0
Chris Lattnere6794492002-08-12 21:17:25 +0000362 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
363 return ReplaceInstUsesWith(I, Op1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000364
365 // and X, -1 == X
Chris Lattner83282632002-08-13 17:50:24 +0000366 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
Chris Lattnere6794492002-08-12 21:17:25 +0000367 if (RHS->isAllOnesValue())
368 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000369
Chris Lattner3082c5a2003-02-18 19:28:33 +0000370 Value *Op0NotVal = dyn_castNotInst(Op0);
371 Value *Op1NotVal = dyn_castNotInst(Op1);
372
373 // (~A & ~B) == (~(A | B)) - Demorgan's Law
374 if (Op0->use_size() == 1 && Op1->use_size() == 1 && Op0NotVal && Op1NotVal) {
375 Instruction *Or = BinaryOperator::create(Instruction::Or, Op0NotVal,
376 Op1NotVal,I.getName()+".demorgan",
377 &I);
378 return BinaryOperator::createNot(Or);
379 }
380
381 if (Op0NotVal == Op1 || Op1NotVal == Op0) // A & ~A == ~A & A == 0
382 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattner65217ff2002-08-23 18:32:43 +0000383
Chris Lattner113f4f42002-06-25 16:13:24 +0000384 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000385}
386
387
388
Chris Lattner113f4f42002-06-25 16:13:24 +0000389Instruction *InstCombiner::visitOr(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000390 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000391 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000392
393 // or X, X = X or X, 0 == X
Chris Lattnere6794492002-08-12 21:17:25 +0000394 if (Op0 == Op1 || Op1 == Constant::getNullValue(I.getType()))
395 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000396
397 // or X, -1 == -1
Chris Lattner83282632002-08-13 17:50:24 +0000398 if (ConstantIntegral *RHS = dyn_cast<ConstantIntegral>(Op1))
Chris Lattnere6794492002-08-12 21:17:25 +0000399 if (RHS->isAllOnesValue())
400 return ReplaceInstUsesWith(I, Op1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000401
Chris Lattner3082c5a2003-02-18 19:28:33 +0000402 if (Value *X = dyn_castNotInst(Op0)) // ~A | A == -1
403 if (X == Op1)
404 return ReplaceInstUsesWith(I,
405 ConstantIntegral::getAllOnesValue(I.getType()));
406
407 if (Value *X = dyn_castNotInst(Op1)) // A | ~A == -1
408 if (X == Op0)
409 return ReplaceInstUsesWith(I,
410 ConstantIntegral::getAllOnesValue(I.getType()));
411
Chris Lattner113f4f42002-06-25 16:13:24 +0000412 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000413}
414
415
416
Chris Lattner113f4f42002-06-25 16:13:24 +0000417Instruction *InstCombiner::visitXor(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000418 bool Changed = SimplifyBinOp(I);
Chris Lattner113f4f42002-06-25 16:13:24 +0000419 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000420
421 // xor X, X = 0
Chris Lattnere6794492002-08-12 21:17:25 +0000422 if (Op0 == Op1)
423 return ReplaceInstUsesWith(I, Constant::getNullValue(I.getType()));
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000424
Chris Lattner83282632002-08-13 17:50:24 +0000425 if (ConstantIntegral *Op1C = dyn_cast<ConstantIntegral>(Op1)) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000426 // xor X, 0 == X
Chris Lattnere6794492002-08-12 21:17:25 +0000427 if (Op1C->isNullValue())
428 return ReplaceInstUsesWith(I, Op0);
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000429
Chris Lattnerb8d6e402002-08-20 18:24:26 +0000430 // Is this a "NOT" instruction?
431 if (Op1C->isAllOnesValue()) {
432 // xor (xor X, -1), -1 = not (not X) = X
Chris Lattner31ae8632002-08-14 17:51:49 +0000433 if (Value *X = dyn_castNotInst(Op0))
434 return ReplaceInstUsesWith(I, X);
Chris Lattnerb8d6e402002-08-20 18:24:26 +0000435
436 // xor (setcc A, B), true = not (setcc A, B) = setncc A, B
437 if (SetCondInst *SCI = dyn_cast<SetCondInst>(Op0))
438 if (SCI->use_size() == 1)
439 return new SetCondInst(SCI->getInverseCondition(),
440 SCI->getOperand(0), SCI->getOperand(1));
441 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000442 }
443
Chris Lattner3082c5a2003-02-18 19:28:33 +0000444 if (Value *X = dyn_castNotInst(Op0)) // ~A ^ A == -1
445 if (X == Op1)
446 return ReplaceInstUsesWith(I,
447 ConstantIntegral::getAllOnesValue(I.getType()));
448
449 if (Value *X = dyn_castNotInst(Op1)) // A ^ ~A == -1
450 if (X == Op0)
451 return ReplaceInstUsesWith(I,
452 ConstantIntegral::getAllOnesValue(I.getType()));
453
Chris Lattner113f4f42002-06-25 16:13:24 +0000454 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000455}
456
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000457// AddOne, SubOne - Add or subtract a constant one from an integer constant...
458static Constant *AddOne(ConstantInt *C) {
459 Constant *Result = *C + *ConstantInt::get(C->getType(), 1);
460 assert(Result && "Constant folding integer addition failed!");
461 return Result;
462}
463static Constant *SubOne(ConstantInt *C) {
464 Constant *Result = *C - *ConstantInt::get(C->getType(), 1);
465 assert(Result && "Constant folding integer addition failed!");
466 return Result;
467}
468
Chris Lattner1fc23f32002-05-09 20:11:54 +0000469// isTrueWhenEqual - Return true if the specified setcondinst instruction is
470// true when both operands are equal...
471//
Chris Lattner113f4f42002-06-25 16:13:24 +0000472static bool isTrueWhenEqual(Instruction &I) {
473 return I.getOpcode() == Instruction::SetEQ ||
474 I.getOpcode() == Instruction::SetGE ||
475 I.getOpcode() == Instruction::SetLE;
Chris Lattner1fc23f32002-05-09 20:11:54 +0000476}
477
Chris Lattner113f4f42002-06-25 16:13:24 +0000478Instruction *InstCombiner::visitSetCondInst(BinaryOperator &I) {
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000479 bool Changed = SimplifyBinOp(I);
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000480 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
481 const Type *Ty = Op0->getType();
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000482
483 // setcc X, X
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000484 if (Op0 == Op1)
485 return ReplaceInstUsesWith(I, ConstantBool::get(isTrueWhenEqual(I)));
Chris Lattner1fc23f32002-05-09 20:11:54 +0000486
487 // setcc <global*>, 0 - Global value addresses are never null!
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000488 if (isa<GlobalValue>(Op0) && isa<ConstantPointerNull>(Op1))
489 return ReplaceInstUsesWith(I, ConstantBool::get(!isTrueWhenEqual(I)));
490
491 // setcc's with boolean values can always be turned into bitwise operations
492 if (Ty == Type::BoolTy) {
493 // If this is <, >, or !=, we can change this into a simple xor instruction
494 if (!isTrueWhenEqual(I))
495 return BinaryOperator::create(Instruction::Xor, Op0, Op1, I.getName());
496
497 // Otherwise we need to make a temporary intermediate instruction and insert
498 // it into the instruction stream. This is what we are after:
499 //
500 // seteq bool %A, %B -> ~(A^B)
501 // setle bool %A, %B -> ~A | B
502 // setge bool %A, %B -> A | ~B
503 //
504 if (I.getOpcode() == Instruction::SetEQ) { // seteq case
505 Instruction *Xor = BinaryOperator::create(Instruction::Xor, Op0, Op1,
506 I.getName()+"tmp");
507 InsertNewInstBefore(Xor, I);
Chris Lattner31ae8632002-08-14 17:51:49 +0000508 return BinaryOperator::createNot(Xor, I.getName());
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000509 }
510
511 // Handle the setXe cases...
512 assert(I.getOpcode() == Instruction::SetGE ||
513 I.getOpcode() == Instruction::SetLE);
514
515 if (I.getOpcode() == Instruction::SetGE)
516 std::swap(Op0, Op1); // Change setge -> setle
517
518 // Now we just have the SetLE case.
Chris Lattner31ae8632002-08-14 17:51:49 +0000519 Instruction *Not = BinaryOperator::createNot(Op0, I.getName()+"tmp");
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000520 InsertNewInstBefore(Not, I);
521 return BinaryOperator::create(Instruction::Or, Not, Op1, I.getName());
522 }
523
524 // Check to see if we are doing one of many comparisons against constant
525 // integers at the end of their ranges...
526 //
527 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
528 // Check to see if we are comparing against the minimum or maximum value...
Chris Lattnere6794492002-08-12 21:17:25 +0000529 if (CI->isMinValue()) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000530 if (I.getOpcode() == Instruction::SetLT) // A < MIN -> FALSE
531 return ReplaceInstUsesWith(I, ConstantBool::False);
532 if (I.getOpcode() == Instruction::SetGE) // A >= MIN -> TRUE
533 return ReplaceInstUsesWith(I, ConstantBool::True);
534 if (I.getOpcode() == Instruction::SetLE) // A <= MIN -> A == MIN
535 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
536 if (I.getOpcode() == Instruction::SetGT) // A > MIN -> A != MIN
537 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
538
Chris Lattnere6794492002-08-12 21:17:25 +0000539 } else if (CI->isMaxValue()) {
Chris Lattner6d14f2a2002-08-09 23:47:40 +0000540 if (I.getOpcode() == Instruction::SetGT) // A > MAX -> FALSE
541 return ReplaceInstUsesWith(I, ConstantBool::False);
542 if (I.getOpcode() == Instruction::SetLE) // A <= MAX -> TRUE
543 return ReplaceInstUsesWith(I, ConstantBool::True);
544 if (I.getOpcode() == Instruction::SetGE) // A >= MAX -> A == MAX
545 return BinaryOperator::create(Instruction::SetEQ, Op0,Op1, I.getName());
546 if (I.getOpcode() == Instruction::SetLT) // A < MAX -> A != MAX
547 return BinaryOperator::create(Instruction::SetNE, Op0,Op1, I.getName());
548
549 // Comparing against a value really close to min or max?
550 } else if (isMinValuePlusOne(CI)) {
551 if (I.getOpcode() == Instruction::SetLT) // A < MIN+1 -> A == MIN
552 return BinaryOperator::create(Instruction::SetEQ, Op0,
553 SubOne(CI), I.getName());
554 if (I.getOpcode() == Instruction::SetGE) // A >= MIN-1 -> A != MIN
555 return BinaryOperator::create(Instruction::SetNE, Op0,
556 SubOne(CI), I.getName());
557
558 } else if (isMaxValueMinusOne(CI)) {
559 if (I.getOpcode() == Instruction::SetGT) // A > MAX-1 -> A == MAX
560 return BinaryOperator::create(Instruction::SetEQ, Op0,
561 AddOne(CI), I.getName());
562 if (I.getOpcode() == Instruction::SetLE) // A <= MAX-1 -> A != MAX
563 return BinaryOperator::create(Instruction::SetNE, Op0,
564 AddOne(CI), I.getName());
565 }
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000566 }
567
Chris Lattner113f4f42002-06-25 16:13:24 +0000568 return Changed ? &I : 0;
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000569}
570
571
572
Chris Lattner113f4f42002-06-25 16:13:24 +0000573Instruction *InstCombiner::visitShiftInst(Instruction &I) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000574 assert(I.getOperand(1)->getType() == Type::UByteTy);
575 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000576
577 // shl X, 0 == X and shr X, 0 == X
578 // shl 0, X == 0 and shr 0, X == 0
579 if (Op1 == Constant::getNullValue(Type::UByteTy) ||
Chris Lattnere6794492002-08-12 21:17:25 +0000580 Op0 == Constant::getNullValue(Op0->getType()))
581 return ReplaceInstUsesWith(I, Op0);
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000582
Chris Lattnere6794492002-08-12 21:17:25 +0000583 // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr of
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000584 // a signed value.
585 //
586 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(Op1)) {
Chris Lattner55f3d942002-09-10 23:04:09 +0000587 if (I.getOpcode() == Instruction::Shr) {
588 unsigned TypeBits = Op0->getType()->getPrimitiveSize()*8;
589 if (CUI->getValue() >= TypeBits && !(Op0->getType()->isSigned()))
590 return ReplaceInstUsesWith(I, Constant::getNullValue(Op0->getType()));
591 }
592
593 // Check to see if we are shifting left by 1. If so, turn it into an add
594 // instruction.
595 if (I.getOpcode() == Instruction::Shl && CUI->equalsInt(1))
Chris Lattner3082c5a2003-02-18 19:28:33 +0000596 // Convert 'shl int %X, 1' to 'add int %X, %X'
Chris Lattner55f3d942002-09-10 23:04:09 +0000597 return BinaryOperator::create(Instruction::Add, Op0, Op0, I.getName());
598
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000599 }
Chris Lattner2e0fb392002-10-08 16:16:40 +0000600
601 // shr int -1, X = -1 (for any arithmetic shift rights of ~0)
602 if (ConstantSInt *CSI = dyn_cast<ConstantSInt>(Op0))
603 if (I.getOpcode() == Instruction::Shr && CSI->isAllOnesValue())
604 return ReplaceInstUsesWith(I, CSI);
605
Chris Lattnerf4cdbf32002-05-06 16:14:14 +0000606 return 0;
607}
608
609
Chris Lattner48a44f72002-05-02 17:06:02 +0000610// isEliminableCastOfCast - Return true if it is valid to eliminate the CI
611// instruction.
612//
Chris Lattner113f4f42002-06-25 16:13:24 +0000613static inline bool isEliminableCastOfCast(const CastInst &CI,
Chris Lattner48a44f72002-05-02 17:06:02 +0000614 const CastInst *CSrc) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000615 assert(CI.getOperand(0) == CSrc);
Chris Lattner48a44f72002-05-02 17:06:02 +0000616 const Type *SrcTy = CSrc->getOperand(0)->getType();
617 const Type *MidTy = CSrc->getType();
Chris Lattner113f4f42002-06-25 16:13:24 +0000618 const Type *DstTy = CI.getType();
Chris Lattner48a44f72002-05-02 17:06:02 +0000619
Chris Lattner650b6da2002-08-02 20:00:25 +0000620 // It is legal to eliminate the instruction if casting A->B->A if the sizes
621 // are identical and the bits don't get reinterpreted (for example
Chris Lattner0bb75912002-08-14 23:21:10 +0000622 // int->float->int would not be allowed)
Chris Lattner650b6da2002-08-02 20:00:25 +0000623 if (SrcTy == DstTy && SrcTy->isLosslesslyConvertableTo(MidTy))
624 return true;
Chris Lattner48a44f72002-05-02 17:06:02 +0000625
626 // Allow free casting and conversion of sizes as long as the sign doesn't
627 // change...
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000628 if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
Chris Lattner650b6da2002-08-02 20:00:25 +0000629 unsigned SrcSize = SrcTy->getPrimitiveSize();
630 unsigned MidSize = MidTy->getPrimitiveSize();
631 unsigned DstSize = DstTy->getPrimitiveSize();
Chris Lattner650b6da2002-08-02 20:00:25 +0000632
Chris Lattner3732aca2002-08-15 16:15:25 +0000633 // Cases where we are monotonically decreasing the size of the type are
634 // always ok, regardless of what sign changes are going on.
635 //
Chris Lattner0bb75912002-08-14 23:21:10 +0000636 if (SrcSize >= MidSize && MidSize >= DstSize)
Chris Lattner650b6da2002-08-02 20:00:25 +0000637 return true;
Chris Lattner3732aca2002-08-15 16:15:25 +0000638
Chris Lattner555518c2002-09-23 23:39:43 +0000639 // Cases where the source and destination type are the same, but the middle
640 // type is bigger are noops.
641 //
642 if (SrcSize == DstSize && MidSize > SrcSize)
643 return true;
644
Chris Lattner3732aca2002-08-15 16:15:25 +0000645 // If we are monotonically growing, things are more complex.
646 //
647 if (SrcSize <= MidSize && MidSize <= DstSize) {
648 // We have eight combinations of signedness to worry about. Here's the
649 // table:
650 static const int SignTable[8] = {
651 // CODE, SrcSigned, MidSigned, DstSigned, Comment
652 1, // U U U Always ok
653 1, // U U S Always ok
654 3, // U S U Ok iff SrcSize != MidSize
655 3, // U S S Ok iff SrcSize != MidSize
656 0, // S U U Never ok
657 2, // S U S Ok iff MidSize == DstSize
658 1, // S S U Always ok
659 1, // S S S Always ok
660 };
661
662 // Choose an action based on the current entry of the signtable that this
663 // cast of cast refers to...
664 unsigned Row = SrcTy->isSigned()*4+MidTy->isSigned()*2+DstTy->isSigned();
665 switch (SignTable[Row]) {
666 case 0: return false; // Never ok
667 case 1: return true; // Always ok
668 case 2: return MidSize == DstSize; // Ok iff MidSize == DstSize
669 case 3: // Ok iff SrcSize != MidSize
670 return SrcSize != MidSize || SrcTy == Type::BoolTy;
671 default: assert(0 && "Bad entry in sign table!");
672 }
Chris Lattner3732aca2002-08-15 16:15:25 +0000673 }
Chris Lattner650b6da2002-08-02 20:00:25 +0000674 }
Chris Lattner48a44f72002-05-02 17:06:02 +0000675
676 // Otherwise, we cannot succeed. Specifically we do not want to allow things
677 // like: short -> ushort -> uint, because this can create wrong results if
678 // the input short is negative!
679 //
680 return false;
681}
682
683
684// CastInst simplification
Chris Lattner260ab202002-04-18 17:39:14 +0000685//
Chris Lattner113f4f42002-06-25 16:13:24 +0000686Instruction *InstCombiner::visitCastInst(CastInst &CI) {
Chris Lattner48a44f72002-05-02 17:06:02 +0000687 // If the user is casting a value to the same type, eliminate this cast
688 // instruction...
Chris Lattnere6794492002-08-12 21:17:25 +0000689 if (CI.getType() == CI.getOperand(0)->getType())
690 return ReplaceInstUsesWith(CI, CI.getOperand(0));
Chris Lattner48a44f72002-05-02 17:06:02 +0000691
Chris Lattner48a44f72002-05-02 17:06:02 +0000692 // If casting the result of another cast instruction, try to eliminate this
693 // one!
694 //
Chris Lattner650b6da2002-08-02 20:00:25 +0000695 if (CastInst *CSrc = dyn_cast<CastInst>(CI.getOperand(0))) {
Chris Lattner48a44f72002-05-02 17:06:02 +0000696 if (isEliminableCastOfCast(CI, CSrc)) {
697 // This instruction now refers directly to the cast's src operand. This
698 // has a good chance of making CSrc dead.
Chris Lattner113f4f42002-06-25 16:13:24 +0000699 CI.setOperand(0, CSrc->getOperand(0));
700 return &CI;
Chris Lattner48a44f72002-05-02 17:06:02 +0000701 }
702
Chris Lattner650b6da2002-08-02 20:00:25 +0000703 // If this is an A->B->A cast, and we are dealing with integral types, try
704 // to convert this into a logical 'and' instruction.
705 //
706 if (CSrc->getOperand(0)->getType() == CI.getType() &&
Chris Lattnerb0b412e2002-09-03 01:08:28 +0000707 CI.getType()->isInteger() && CSrc->getType()->isInteger() &&
Chris Lattner650b6da2002-08-02 20:00:25 +0000708 CI.getType()->isUnsigned() && CSrc->getType()->isUnsigned() &&
709 CSrc->getType()->getPrimitiveSize() < CI.getType()->getPrimitiveSize()){
710 assert(CSrc->getType() != Type::ULongTy &&
711 "Cannot have type bigger than ulong!");
712 unsigned AndValue = (1U << CSrc->getType()->getPrimitiveSize()*8)-1;
713 Constant *AndOp = ConstantUInt::get(CI.getType(), AndValue);
714 return BinaryOperator::create(Instruction::And, CSrc->getOperand(0),
715 AndOp);
716 }
717 }
718
Chris Lattner260ab202002-04-18 17:39:14 +0000719 return 0;
Chris Lattnerca081252001-12-14 16:52:21 +0000720}
721
Chris Lattner48a44f72002-05-02 17:06:02 +0000722
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000723// PHINode simplification
724//
Chris Lattner113f4f42002-06-25 16:13:24 +0000725Instruction *InstCombiner::visitPHINode(PHINode &PN) {
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000726 // If the PHI node only has one incoming value, eliminate the PHI node...
Chris Lattnere6794492002-08-12 21:17:25 +0000727 if (PN.getNumIncomingValues() == 1)
728 return ReplaceInstUsesWith(PN, PN.getIncomingValue(0));
Chris Lattner9cd1e662002-08-20 15:35:35 +0000729
730 // Otherwise if all of the incoming values are the same for the PHI, replace
731 // the PHI node with the incoming value.
732 //
Chris Lattnerf6c0efa2002-08-22 20:22:01 +0000733 Value *InVal = 0;
734 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
735 if (PN.getIncomingValue(i) != &PN) // Not the PHI node itself...
736 if (InVal && PN.getIncomingValue(i) != InVal)
737 return 0; // Not the same, bail out.
738 else
739 InVal = PN.getIncomingValue(i);
740
741 // The only case that could cause InVal to be null is if we have a PHI node
742 // that only has entries for itself. In this case, there is no entry into the
743 // loop, so kill the PHI.
744 //
745 if (InVal == 0) InVal = Constant::getNullValue(PN.getType());
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000746
Chris Lattner9cd1e662002-08-20 15:35:35 +0000747 // All of the incoming values are the same, replace the PHI node now.
748 return ReplaceInstUsesWith(PN, InVal);
Chris Lattnerbbbdd852002-05-06 18:06:38 +0000749}
750
Chris Lattner48a44f72002-05-02 17:06:02 +0000751
Chris Lattner113f4f42002-06-25 16:13:24 +0000752Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000753 // Is it 'getelementptr %P, uint 0' or 'getelementptr %P'
Chris Lattner113f4f42002-06-25 16:13:24 +0000754 // If so, eliminate the noop.
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000755 if ((GEP.getNumOperands() == 2 &&
Chris Lattner136dab72002-09-11 01:21:33 +0000756 GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
Chris Lattnere6794492002-08-12 21:17:25 +0000757 GEP.getNumOperands() == 1)
758 return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
Chris Lattner48a44f72002-05-02 17:06:02 +0000759
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000760 // Combine Indices - If the source pointer to this getelementptr instruction
761 // is a getelementptr instruction, combine the indices of the two
762 // getelementptr instructions into a single instruction.
763 //
Chris Lattnerc59af1d2002-08-17 22:21:59 +0000764 if (GetElementPtrInst *Src = dyn_cast<GetElementPtrInst>(GEP.getOperand(0))) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000765 std::vector<Value *> Indices;
Chris Lattnerca081252001-12-14 16:52:21 +0000766
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000767 // Can we combine the two pointer arithmetics offsets?
768 if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
769 isa<Constant>(GEP.getOperand(1))) {
770 // Replace the index list on this GEP with the index on the getelementptr
771 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
772 Indices[0] = *cast<Constant>(Src->getOperand(1)) +
773 *cast<Constant>(GEP.getOperand(1));
774 assert(Indices[0] != 0 && "Constant folding of uint's failed!?");
775
Chris Lattner5d606a02002-11-04 16:43:32 +0000776 } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) &&
Chris Lattnera8339e32002-09-17 21:05:42 +0000777 Src->getNumOperands() != 1) {
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000778 // Otherwise we can do the fold if the first index of the GEP is a zero
779 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end());
780 Indices.insert(Indices.end(), GEP.idx_begin()+1, GEP.idx_end());
Chris Lattner5d606a02002-11-04 16:43:32 +0000781 } else if (Src->getOperand(Src->getNumOperands()-1) ==
782 Constant::getNullValue(Type::LongTy)) {
783 // If the src gep ends with a constant array index, merge this get into
784 // it, even if we have a non-zero array index.
785 Indices.insert(Indices.end(), Src->idx_begin(), Src->idx_end()-1);
786 Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000787 }
788
789 if (!Indices.empty())
790 return new GetElementPtrInst(Src->getOperand(0), Indices, GEP.getName());
Chris Lattnerc59af1d2002-08-17 22:21:59 +0000791
792 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(GEP.getOperand(0))) {
793 // GEP of global variable. If all of the indices for this GEP are
794 // constants, we can promote this to a constexpr instead of an instruction.
795
796 // Scan for nonconstants...
797 std::vector<Constant*> Indices;
798 User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end();
799 for (; I != E && isa<Constant>(*I); ++I)
800 Indices.push_back(cast<Constant>(*I));
801
802 if (I == E) { // If they are all constants...
803 ConstantExpr *CE =
804 ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices);
805
806 // Replace all uses of the GEP with the new constexpr...
807 return ReplaceInstUsesWith(GEP, CE);
808 }
Chris Lattnerca081252001-12-14 16:52:21 +0000809 }
810
Chris Lattnerca081252001-12-14 16:52:21 +0000811 return 0;
812}
813
Chris Lattner1085bdf2002-11-04 16:18:53 +0000814Instruction *InstCombiner::visitAllocationInst(AllocationInst &AI) {
815 // Convert: malloc Ty, C - where C is a constant != 1 into: malloc [C x Ty], 1
816 if (AI.isArrayAllocation()) // Check C != 1
817 if (const ConstantUInt *C = dyn_cast<ConstantUInt>(AI.getArraySize())) {
818 const Type *NewTy = ArrayType::get(AI.getAllocatedType(), C->getValue());
Chris Lattnera2620ac2002-11-09 00:49:43 +0000819 AllocationInst *New = 0;
Chris Lattner1085bdf2002-11-04 16:18:53 +0000820
821 // Create and insert the replacement instruction...
822 if (isa<MallocInst>(AI))
823 New = new MallocInst(NewTy, 0, AI.getName(), &AI);
Chris Lattnera2620ac2002-11-09 00:49:43 +0000824 else {
825 assert(isa<AllocaInst>(AI) && "Unknown type of allocation inst!");
Chris Lattner1085bdf2002-11-04 16:18:53 +0000826 New = new AllocaInst(NewTy, 0, AI.getName(), &AI);
Chris Lattnera2620ac2002-11-09 00:49:43 +0000827 }
Chris Lattner1085bdf2002-11-04 16:18:53 +0000828
829 // Scan to the end of the allocation instructions, to skip over a block of
830 // allocas if possible...
831 //
832 BasicBlock::iterator It = New;
833 while (isa<AllocationInst>(*It)) ++It;
834
835 // Now that I is pointing to the first non-allocation-inst in the block,
836 // insert our getelementptr instruction...
837 //
838 std::vector<Value*> Idx(2, Constant::getNullValue(Type::LongTy));
839 Value *V = new GetElementPtrInst(New, Idx, New->getName()+".sub", It);
840
841 // Now make everything use the getelementptr instead of the original
842 // allocation.
843 ReplaceInstUsesWith(AI, V);
844 return &AI;
845 }
846 return 0;
847}
848
849
Chris Lattnerca081252001-12-14 16:52:21 +0000850
Chris Lattner99f48c62002-09-02 04:59:56 +0000851void InstCombiner::removeFromWorkList(Instruction *I) {
852 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), I),
853 WorkList.end());
854}
855
Chris Lattner113f4f42002-06-25 16:13:24 +0000856bool InstCombiner::runOnFunction(Function &F) {
Chris Lattner260ab202002-04-18 17:39:14 +0000857 bool Changed = false;
Chris Lattnerca081252001-12-14 16:52:21 +0000858
Chris Lattner260ab202002-04-18 17:39:14 +0000859 WorkList.insert(WorkList.end(), inst_begin(F), inst_end(F));
Chris Lattnerca081252001-12-14 16:52:21 +0000860
861 while (!WorkList.empty()) {
862 Instruction *I = WorkList.back(); // Get an instruction from the worklist
863 WorkList.pop_back();
864
Misha Brukman632df282002-10-29 23:06:16 +0000865 // Check to see if we can DCE or ConstantPropagate the instruction...
Chris Lattner99f48c62002-09-02 04:59:56 +0000866 // Check to see if we can DIE the instruction...
867 if (isInstructionTriviallyDead(I)) {
868 // Add operands to the worklist...
869 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
870 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
871 WorkList.push_back(Op);
872
873 ++NumDeadInst;
874 BasicBlock::iterator BBI = I;
875 if (dceInstruction(BBI)) {
876 removeFromWorkList(I);
877 continue;
878 }
879 }
880
Misha Brukman632df282002-10-29 23:06:16 +0000881 // Instruction isn't dead, see if we can constant propagate it...
Chris Lattner99f48c62002-09-02 04:59:56 +0000882 if (Constant *C = ConstantFoldInstruction(I)) {
883 // Add operands to the worklist...
884 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
885 if (Instruction *Op = dyn_cast<Instruction>(I->getOperand(i)))
886 WorkList.push_back(Op);
Chris Lattnerc6509f42002-12-05 22:41:53 +0000887 ReplaceInstUsesWith(*I, C);
888
Chris Lattner99f48c62002-09-02 04:59:56 +0000889 ++NumConstProp;
890 BasicBlock::iterator BBI = I;
891 if (dceInstruction(BBI)) {
892 removeFromWorkList(I);
893 continue;
894 }
895 }
896
Chris Lattnerca081252001-12-14 16:52:21 +0000897 // Now that we have an instruction, try combining it to simplify it...
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000898 if (Instruction *Result = visit(*I)) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000899 ++NumCombined;
Chris Lattner260ab202002-04-18 17:39:14 +0000900 // Should we replace the old instruction with a new one?
Chris Lattner053c0932002-05-14 15:24:07 +0000901 if (Result != I) {
902 // Instructions can end up on the worklist more than once. Make sure
903 // we do not process an instruction that has been deleted.
Chris Lattner99f48c62002-09-02 04:59:56 +0000904 removeFromWorkList(I);
Chris Lattner260ab202002-04-18 17:39:14 +0000905 ReplaceInstWithInst(I, Result);
Chris Lattner113f4f42002-06-25 16:13:24 +0000906 } else {
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000907 BasicBlock::iterator II = I;
908
909 // If the instruction was modified, it's possible that it is now dead.
910 // if so, remove it.
911 if (dceInstruction(II)) {
912 // Instructions may end up in the worklist more than once. Erase them
913 // all.
Chris Lattner99f48c62002-09-02 04:59:56 +0000914 removeFromWorkList(I);
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000915 Result = 0;
916 }
Chris Lattner053c0932002-05-14 15:24:07 +0000917 }
Chris Lattner260ab202002-04-18 17:39:14 +0000918
Chris Lattnerae7a0d32002-08-02 19:29:35 +0000919 if (Result) {
920 WorkList.push_back(Result);
921 AddUsesToWorkList(*Result);
922 }
Chris Lattner260ab202002-04-18 17:39:14 +0000923 Changed = true;
Chris Lattnerca081252001-12-14 16:52:21 +0000924 }
925 }
926
Chris Lattner260ab202002-04-18 17:39:14 +0000927 return Changed;
Chris Lattner04805fa2002-02-26 21:46:54 +0000928}
929
930Pass *createInstructionCombiningPass() {
Chris Lattner260ab202002-04-18 17:39:14 +0000931 return new InstCombiner();
Chris Lattner04805fa2002-02-26 21:46:54 +0000932}