blob: a8288bf67a97fb04418733c9b41912f5a8feb389 [file] [log] [blame]
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001//===- InstructionSimplify.cpp - Fold instruction operands ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements routines for folding instructions into simpler forms
11// that do not require creating new instructions. For example, this does
12// constant folding, and can handle identities like (X&0)->0.
13//
14//===----------------------------------------------------------------------===//
15
16#include "llvm/Analysis/InstructionSimplify.h"
17#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner40d8c282009-11-10 22:26:15 +000018#include "llvm/Support/ValueHandle.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000019#include "llvm/Instructions.h"
Chris Lattnerd06094f2009-11-10 00:55:12 +000020#include "llvm/Support/PatternMatch.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000021using namespace llvm;
Chris Lattnerd06094f2009-11-10 00:55:12 +000022using namespace llvm::PatternMatch;
Chris Lattner9f3c25a2009-11-09 22:57:59 +000023
Duncan Sandsbc68d712010-11-10 20:53:24 +000024#define MaxRecursionDepth 3
Duncan Sandsa74a58c2010-11-10 18:23:01 +000025
26static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *,
27 unsigned);
28static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *,
29 unsigned);
30
Duncan Sandsb2cbdc32010-11-10 13:00:08 +000031/// ThreadBinOpOverSelect - In the case of a binary operation with a select
32/// instruction as an operand, try to simplify the binop by seeing whether
33/// evaluating it on both branches of the select results in the same value.
34/// Returns the common value if so, otherwise returns null.
35static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sandsa74a58c2010-11-10 18:23:01 +000036 const TargetData *TD, unsigned MaxRecurse) {
Duncan Sandsb2cbdc32010-11-10 13:00:08 +000037 SelectInst *SI;
38 if (isa<SelectInst>(LHS)) {
39 SI = cast<SelectInst>(LHS);
40 } else {
41 assert(isa<SelectInst>(RHS) && "No select instruction operand!");
42 SI = cast<SelectInst>(RHS);
43 }
44
45 // Evaluate the BinOp on the true and false branches of the select.
46 Value *TV;
47 Value *FV;
48 if (SI == LHS) {
Duncan Sandsa74a58c2010-11-10 18:23:01 +000049 TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, MaxRecurse);
50 FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, MaxRecurse);
Duncan Sandsb2cbdc32010-11-10 13:00:08 +000051 } else {
Duncan Sandsa74a58c2010-11-10 18:23:01 +000052 TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, MaxRecurse);
53 FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, MaxRecurse);
Duncan Sandsb2cbdc32010-11-10 13:00:08 +000054 }
55
56 // If they simplified to the same value, then return the common value.
57 // If they both failed to simplify then return null.
58 if (TV == FV)
59 return TV;
60
61 // If one branch simplified to undef, return the other one.
62 if (TV && isa<UndefValue>(TV))
63 return FV;
64 if (FV && isa<UndefValue>(FV))
65 return TV;
66
67 // If applying the operation did not change the true and false select values,
68 // then the result of the binop is the select itself.
69 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
70 return SI;
71
72 // If one branch simplified and the other did not, and the simplified
73 // value is equal to the unsimplified one, return the simplified value.
74 // For example, select (cond, X, X & Z) & Z -> X & Z.
75 if ((FV && !TV) || (TV && !FV)) {
76 // Check that the simplified value has the form "X op Y" where "op" is the
77 // same as the original operation.
78 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
79 if (Simplified && Simplified->getOpcode() == Opcode) {
80 // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
81 // We already know that "op" is the same as for the simplified value. See
82 // if the operands match too. If so, return the simplified value.
83 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
84 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
85 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
86 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
87 Simplified->getOperand(1) == UnsimplifiedRHS)
88 return Simplified;
89 if (Simplified->isCommutative() &&
90 Simplified->getOperand(1) == UnsimplifiedLHS &&
91 Simplified->getOperand(0) == UnsimplifiedRHS)
92 return Simplified;
93 }
94 }
95
96 return 0;
97}
98
99/// ThreadCmpOverSelect - In the case of a comparison with a select instruction,
100/// try to simplify the comparison by seeing whether both branches of the select
101/// result in the same value. Returns the common value if so, otherwise returns
102/// null.
103static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000104 Value *RHS, const TargetData *TD,
105 unsigned MaxRecurse) {
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000106 // Make sure the select is on the LHS.
107 if (!isa<SelectInst>(LHS)) {
108 std::swap(LHS, RHS);
109 Pred = CmpInst::getSwappedPredicate(Pred);
110 }
111 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
112 SelectInst *SI = cast<SelectInst>(LHS);
113
114 // Now that we have "cmp select(cond, TV, FV), RHS", analyse it.
115 // Does "cmp TV, RHS" simplify?
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000116 if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD,
117 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000118 // It does! Does "cmp FV, RHS" simplify?
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000119 if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD,
120 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000121 // It does! If they simplified to the same value, then use it as the
122 // result of the original comparison.
123 if (TCmp == FCmp)
124 return TCmp;
125 return 0;
126}
127
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000128/// ThreadBinOpOverPHI - In the case of a binary operation with an operand that
129/// is a PHI instruction, try to simplify the binop by seeing whether evaluating
130/// it on the incoming phi values yields the same result for every value. If so
131/// returns the common value, otherwise returns null.
132static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS,
133 const TargetData *TD, unsigned MaxRecurse) {
134 PHINode *PI;
135 if (isa<PHINode>(LHS)) {
136 PI = cast<PHINode>(LHS);
137 } else {
138 assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
139 PI = cast<PHINode>(RHS);
140 }
141
142 // Evaluate the BinOp on the incoming phi values.
143 Value *CommonValue = 0;
144 for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
Duncan Sands55200892010-11-15 17:52:45 +0000145 Value *Incoming = PI->getIncomingValue(i);
146 // If the incoming value is the phi node itself, it can be safely skipped.
147 if (Incoming == PI) continue;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000148 Value *V = PI == LHS ?
Duncan Sands55200892010-11-15 17:52:45 +0000149 SimplifyBinOp(Opcode, Incoming, RHS, TD, MaxRecurse) :
150 SimplifyBinOp(Opcode, LHS, Incoming, TD, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000151 // If the operation failed to simplify, or simplified to a different value
152 // to previously, then give up.
153 if (!V || (CommonValue && V != CommonValue))
154 return 0;
155 CommonValue = V;
156 }
157
158 return CommonValue;
159}
160
161/// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try
162/// try to simplify the comparison by seeing whether comparing with all of the
163/// incoming phi values yields the same result every time. If so returns the
164/// common result, otherwise returns null.
165static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
166 const TargetData *TD, unsigned MaxRecurse) {
167 // Make sure the phi is on the LHS.
168 if (!isa<PHINode>(LHS)) {
169 std::swap(LHS, RHS);
170 Pred = CmpInst::getSwappedPredicate(Pred);
171 }
172 assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
173 PHINode *PI = cast<PHINode>(LHS);
174
175 // Evaluate the BinOp on the incoming phi values.
176 Value *CommonValue = 0;
177 for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
Duncan Sands55200892010-11-15 17:52:45 +0000178 Value *Incoming = PI->getIncomingValue(i);
179 // If the incoming value is the phi node itself, it can be safely skipped.
180 if (Incoming == PI) continue;
181 Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000182 // If the operation failed to simplify, or simplified to a different value
183 // to previously, then give up.
184 if (!V || (CommonValue && V != CommonValue))
185 return 0;
186 CommonValue = V;
187 }
188
189 return CommonValue;
190}
191
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000192/// SimplifyAddInst - Given operands for an Add, see if we can
193/// fold the result. If not, this returns null.
194Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
195 const TargetData *TD) {
196 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
197 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
198 Constant *Ops[] = { CLHS, CRHS };
199 return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(),
200 Ops, 2, TD);
201 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000202
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000203 // Canonicalize the constant to the RHS.
204 std::swap(Op0, Op1);
205 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000206
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000207 if (Constant *Op1C = dyn_cast<Constant>(Op1)) {
208 // X + undef -> undef
209 if (isa<UndefValue>(Op1C))
210 return Op1C;
Duncan Sands12a86f52010-11-14 11:23:23 +0000211
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000212 // X + 0 --> X
213 if (Op1C->isNullValue())
214 return Op0;
215 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000216
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000217 // FIXME: Could pull several more out of instcombine.
218 return 0;
219}
220
Chris Lattnerd06094f2009-11-10 00:55:12 +0000221/// SimplifyAndInst - Given operands for an And, see if we can
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000222/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000223static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
224 unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +0000225 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
226 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
227 Constant *Ops[] = { CLHS, CRHS };
228 return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
229 Ops, 2, TD);
230 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000231
Chris Lattnerd06094f2009-11-10 00:55:12 +0000232 // Canonicalize the constant to the RHS.
233 std::swap(Op0, Op1);
234 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000235
Chris Lattnerd06094f2009-11-10 00:55:12 +0000236 // X & undef -> 0
237 if (isa<UndefValue>(Op1))
238 return Constant::getNullValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000239
Chris Lattnerd06094f2009-11-10 00:55:12 +0000240 // X & X = X
241 if (Op0 == Op1)
242 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000243
Chris Lattnerd06094f2009-11-10 00:55:12 +0000244 // X & <0,0> = <0,0>
245 if (isa<ConstantAggregateZero>(Op1))
246 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000247
Chris Lattnerd06094f2009-11-10 00:55:12 +0000248 // X & <-1,-1> = X
249 if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1))
250 if (CP->isAllOnesValue())
251 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000252
Chris Lattnerd06094f2009-11-10 00:55:12 +0000253 if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) {
254 // X & 0 = 0
255 if (Op1CI->isZero())
256 return Op1CI;
257 // X & -1 = X
258 if (Op1CI->isAllOnesValue())
259 return Op0;
260 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000261
Chris Lattnerd06094f2009-11-10 00:55:12 +0000262 // A & ~A = ~A & A = 0
263 Value *A, *B;
Chris Lattner70ce6d02009-11-10 02:04:54 +0000264 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
265 (match(Op1, m_Not(m_Value(A))) && A == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000266 return Constant::getNullValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000267
Chris Lattnerd06094f2009-11-10 00:55:12 +0000268 // (A | ?) & A = A
269 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
270 (A == Op1 || B == Op1))
271 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000272
Chris Lattnerd06094f2009-11-10 00:55:12 +0000273 // A & (A | ?) = A
274 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
275 (A == Op0 || B == Op0))
276 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000277
Benjamin Kramer6844c8e2010-09-10 22:39:55 +0000278 // (A & B) & A -> A & B
279 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
280 (A == Op1 || B == Op1))
281 return Op0;
282
283 // A & (A & B) -> A & B
284 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
285 (A == Op0 || B == Op0))
286 return Op1;
287
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000288 // If the operation is with the result of a select instruction, check whether
289 // operating on either branch of the select always yields the same value.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000290 if (MaxRecurse && (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)))
291 if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD,
292 MaxRecurse-1))
293 return V;
294
295 // If the operation is with the result of a phi instruction, check whether
296 // operating on all incoming values of the phi always yields the same value.
297 if (MaxRecurse && (isa<PHINode>(Op0) || isa<PHINode>(Op1)))
298 if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD,
299 MaxRecurse-1))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000300 return V;
301
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000302 return 0;
303}
304
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000305Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD) {
306 return ::SimplifyAndInst(Op0, Op1, TD, MaxRecursionDepth);
307}
308
Chris Lattnerd06094f2009-11-10 00:55:12 +0000309/// SimplifyOrInst - Given operands for an Or, see if we can
310/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000311static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
312 unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +0000313 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
314 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
315 Constant *Ops[] = { CLHS, CRHS };
316 return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
317 Ops, 2, TD);
318 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000319
Chris Lattnerd06094f2009-11-10 00:55:12 +0000320 // Canonicalize the constant to the RHS.
321 std::swap(Op0, Op1);
322 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000323
Chris Lattnerd06094f2009-11-10 00:55:12 +0000324 // X | undef -> -1
325 if (isa<UndefValue>(Op1))
326 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000327
Chris Lattnerd06094f2009-11-10 00:55:12 +0000328 // X | X = X
329 if (Op0 == Op1)
330 return Op0;
331
332 // X | <0,0> = X
333 if (isa<ConstantAggregateZero>(Op1))
334 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000335
Chris Lattnerd06094f2009-11-10 00:55:12 +0000336 // X | <-1,-1> = <-1,-1>
337 if (ConstantVector *CP = dyn_cast<ConstantVector>(Op1))
Duncan Sands12a86f52010-11-14 11:23:23 +0000338 if (CP->isAllOnesValue())
Chris Lattnerd06094f2009-11-10 00:55:12 +0000339 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000340
Chris Lattnerd06094f2009-11-10 00:55:12 +0000341 if (ConstantInt *Op1CI = dyn_cast<ConstantInt>(Op1)) {
342 // X | 0 = X
343 if (Op1CI->isZero())
344 return Op0;
345 // X | -1 = -1
346 if (Op1CI->isAllOnesValue())
347 return Op1CI;
348 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000349
Chris Lattnerd06094f2009-11-10 00:55:12 +0000350 // A | ~A = ~A | A = -1
351 Value *A, *B;
Chris Lattner70ce6d02009-11-10 02:04:54 +0000352 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
353 (match(Op1, m_Not(m_Value(A))) && A == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000354 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000355
Chris Lattnerd06094f2009-11-10 00:55:12 +0000356 // (A & ?) | A = A
357 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
358 (A == Op1 || B == Op1))
359 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000360
Chris Lattnerd06094f2009-11-10 00:55:12 +0000361 // A | (A & ?) = A
362 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
363 (A == Op0 || B == Op0))
364 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000365
Benjamin Kramer6844c8e2010-09-10 22:39:55 +0000366 // (A | B) | A -> A | B
367 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
368 (A == Op1 || B == Op1))
369 return Op0;
370
371 // A | (A | B) -> A | B
372 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
373 (A == Op0 || B == Op0))
374 return Op1;
375
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000376 // If the operation is with the result of a select instruction, check whether
377 // operating on either branch of the select always yields the same value.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000378 if (MaxRecurse && (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)))
379 if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD,
380 MaxRecurse-1))
381 return V;
382
383 // If the operation is with the result of a phi instruction, check whether
384 // operating on all incoming values of the phi always yields the same value.
385 if (MaxRecurse && (isa<PHINode>(Op0) || isa<PHINode>(Op1)))
386 if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD,
387 MaxRecurse-1))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000388 return V;
389
Chris Lattnerd06094f2009-11-10 00:55:12 +0000390 return 0;
391}
392
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000393Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD) {
394 return ::SimplifyOrInst(Op0, Op1, TD, MaxRecursionDepth);
395}
Chris Lattnerd06094f2009-11-10 00:55:12 +0000396
Chris Lattner210c5d42009-11-09 23:55:12 +0000397static const Type *GetCompareTy(Value *Op) {
398 return CmpInst::makeCmpResultType(Op->getType());
399}
400
Chris Lattner9dbb4292009-11-09 23:28:39 +0000401/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
402/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000403static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
404 const TargetData *TD, unsigned MaxRecurse) {
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000405 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
Chris Lattner9dbb4292009-11-09 23:28:39 +0000406 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
Duncan Sands12a86f52010-11-14 11:23:23 +0000407
Chris Lattnerd06094f2009-11-10 00:55:12 +0000408 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner8f73dea2009-11-09 23:06:58 +0000409 if (Constant *CRHS = dyn_cast<Constant>(RHS))
410 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattnerd06094f2009-11-10 00:55:12 +0000411
412 // If we have a constant, make sure it is on the RHS.
413 std::swap(LHS, RHS);
414 Pred = CmpInst::getSwappedPredicate(Pred);
415 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000416
Chris Lattner210c5d42009-11-09 23:55:12 +0000417 // ITy - This is the return type of the compare we're considering.
418 const Type *ITy = GetCompareTy(LHS);
Duncan Sands12a86f52010-11-14 11:23:23 +0000419
Chris Lattner210c5d42009-11-09 23:55:12 +0000420 // icmp X, X -> true/false
Chris Lattnerc8e14b32010-03-03 19:46:03 +0000421 // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false
422 // because X could be 0.
423 if (LHS == RHS || isa<UndefValue>(RHS))
Chris Lattner210c5d42009-11-09 23:55:12 +0000424 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
Duncan Sands12a86f52010-11-14 11:23:23 +0000425
Chris Lattner210c5d42009-11-09 23:55:12 +0000426 // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
427 // addresses never equal each other! We already know that Op0 != Op1.
Duncan Sands12a86f52010-11-14 11:23:23 +0000428 if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
Chris Lattner210c5d42009-11-09 23:55:12 +0000429 isa<ConstantPointerNull>(LHS)) &&
Duncan Sands12a86f52010-11-14 11:23:23 +0000430 (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
Chris Lattner210c5d42009-11-09 23:55:12 +0000431 isa<ConstantPointerNull>(RHS)))
432 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
Duncan Sands12a86f52010-11-14 11:23:23 +0000433
Chris Lattner210c5d42009-11-09 23:55:12 +0000434 // See if we are doing a comparison with a constant.
435 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
436 // If we have an icmp le or icmp ge instruction, turn it into the
437 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
438 // them being folded in the code below.
439 switch (Pred) {
440 default: break;
441 case ICmpInst::ICMP_ULE:
442 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
443 return ConstantInt::getTrue(CI->getContext());
444 break;
445 case ICmpInst::ICMP_SLE:
446 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
447 return ConstantInt::getTrue(CI->getContext());
448 break;
449 case ICmpInst::ICMP_UGE:
450 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
451 return ConstantInt::getTrue(CI->getContext());
452 break;
453 case ICmpInst::ICMP_SGE:
454 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
455 return ConstantInt::getTrue(CI->getContext());
456 break;
457 }
Chris Lattner210c5d42009-11-09 23:55:12 +0000458 }
Duncan Sands1ac7c992010-11-07 16:12:23 +0000459
460 // If the comparison is with the result of a select instruction, check whether
461 // comparing with either branch of the select always yields the same value.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000462 if (MaxRecurse && (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)))
463 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, MaxRecurse-1))
464 return V;
465
466 // If the comparison is with the result of a phi instruction, check whether
467 // doing the compare with each incoming phi value yields a common result.
468 if (MaxRecurse && (isa<PHINode>(LHS) || isa<PHINode>(RHS)))
469 if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, MaxRecurse-1))
Duncan Sands3bbb0cc2010-11-09 17:25:51 +0000470 return V;
Duncan Sands1ac7c992010-11-07 16:12:23 +0000471
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000472 return 0;
473}
474
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000475Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
476 const TargetData *TD) {
477 return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, MaxRecursionDepth);
478}
479
Chris Lattner9dbb4292009-11-09 23:28:39 +0000480/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
481/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000482static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
483 const TargetData *TD, unsigned MaxRecurse) {
Chris Lattner9dbb4292009-11-09 23:28:39 +0000484 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
485 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
486
Chris Lattnerd06094f2009-11-10 00:55:12 +0000487 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner9dbb4292009-11-09 23:28:39 +0000488 if (Constant *CRHS = dyn_cast<Constant>(RHS))
489 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Duncan Sands12a86f52010-11-14 11:23:23 +0000490
Chris Lattnerd06094f2009-11-10 00:55:12 +0000491 // If we have a constant, make sure it is on the RHS.
492 std::swap(LHS, RHS);
493 Pred = CmpInst::getSwappedPredicate(Pred);
494 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000495
Chris Lattner210c5d42009-11-09 23:55:12 +0000496 // Fold trivial predicates.
497 if (Pred == FCmpInst::FCMP_FALSE)
498 return ConstantInt::get(GetCompareTy(LHS), 0);
499 if (Pred == FCmpInst::FCMP_TRUE)
500 return ConstantInt::get(GetCompareTy(LHS), 1);
501
Chris Lattner210c5d42009-11-09 23:55:12 +0000502 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
503 return UndefValue::get(GetCompareTy(LHS));
504
505 // fcmp x,x -> true/false. Not all compares are foldable.
506 if (LHS == RHS) {
507 if (CmpInst::isTrueWhenEqual(Pred))
508 return ConstantInt::get(GetCompareTy(LHS), 1);
509 if (CmpInst::isFalseWhenEqual(Pred))
510 return ConstantInt::get(GetCompareTy(LHS), 0);
511 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000512
Chris Lattner210c5d42009-11-09 23:55:12 +0000513 // Handle fcmp with constant RHS
514 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
515 // If the constant is a nan, see if we can fold the comparison based on it.
516 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
517 if (CFP->getValueAPF().isNaN()) {
518 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
519 return ConstantInt::getFalse(CFP->getContext());
520 assert(FCmpInst::isUnordered(Pred) &&
521 "Comparison must be either ordered or unordered!");
522 // True if unordered.
523 return ConstantInt::getTrue(CFP->getContext());
524 }
Dan Gohman6b617a72010-02-22 04:06:03 +0000525 // Check whether the constant is an infinity.
526 if (CFP->getValueAPF().isInfinity()) {
527 if (CFP->getValueAPF().isNegative()) {
528 switch (Pred) {
529 case FCmpInst::FCMP_OLT:
530 // No value is ordered and less than negative infinity.
531 return ConstantInt::getFalse(CFP->getContext());
532 case FCmpInst::FCMP_UGE:
533 // All values are unordered with or at least negative infinity.
534 return ConstantInt::getTrue(CFP->getContext());
535 default:
536 break;
537 }
538 } else {
539 switch (Pred) {
540 case FCmpInst::FCMP_OGT:
541 // No value is ordered and greater than infinity.
542 return ConstantInt::getFalse(CFP->getContext());
543 case FCmpInst::FCMP_ULE:
544 // All values are unordered with and at most infinity.
545 return ConstantInt::getTrue(CFP->getContext());
546 default:
547 break;
548 }
549 }
550 }
Chris Lattner210c5d42009-11-09 23:55:12 +0000551 }
552 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000553
Duncan Sands92826de2010-11-07 16:46:25 +0000554 // If the comparison is with the result of a select instruction, check whether
555 // comparing with either branch of the select always yields the same value.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000556 if (MaxRecurse && (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)))
557 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, MaxRecurse-1))
558 return V;
559
560 // If the comparison is with the result of a phi instruction, check whether
561 // doing the compare with each incoming phi value yields a common result.
562 if (MaxRecurse && (isa<PHINode>(LHS) || isa<PHINode>(RHS)))
563 if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, MaxRecurse-1))
Duncan Sands3bbb0cc2010-11-09 17:25:51 +0000564 return V;
Duncan Sands92826de2010-11-07 16:46:25 +0000565
Chris Lattner9dbb4292009-11-09 23:28:39 +0000566 return 0;
567}
568
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000569Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
570 const TargetData *TD) {
571 return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, MaxRecursionDepth);
572}
573
Chris Lattner04754262010-04-20 05:32:14 +0000574/// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
575/// the result. If not, this returns null.
576Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
577 const TargetData *TD) {
578 // select true, X, Y -> X
579 // select false, X, Y -> Y
580 if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
581 return CB->getZExtValue() ? TrueVal : FalseVal;
Duncan Sands12a86f52010-11-14 11:23:23 +0000582
Chris Lattner04754262010-04-20 05:32:14 +0000583 // select C, X, X -> X
584 if (TrueVal == FalseVal)
585 return TrueVal;
Duncan Sands12a86f52010-11-14 11:23:23 +0000586
Chris Lattner04754262010-04-20 05:32:14 +0000587 if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
588 return FalseVal;
589 if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
590 return TrueVal;
591 if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
592 if (isa<Constant>(TrueVal))
593 return TrueVal;
594 return FalseVal;
595 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000596
Chris Lattner04754262010-04-20 05:32:14 +0000597 return 0;
598}
599
600
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000601/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
602/// fold the result. If not, this returns null.
603Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
604 const TargetData *TD) {
605 // getelementptr P -> P.
606 if (NumOps == 1)
607 return Ops[0];
608
609 // TODO.
610 //if (isa<UndefValue>(Ops[0]))
611 // return UndefValue::get(GEP.getType());
612
613 // getelementptr P, 0 -> P.
614 if (NumOps == 2)
615 if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
616 if (C->isZero())
617 return Ops[0];
Duncan Sands12a86f52010-11-14 11:23:23 +0000618
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000619 // Check to see if this is constant foldable.
620 for (unsigned i = 0; i != NumOps; ++i)
621 if (!isa<Constant>(Ops[i]))
622 return 0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000623
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000624 return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
625 (Constant *const*)Ops+1, NumOps-1);
626}
627
628
Chris Lattnerd06094f2009-11-10 00:55:12 +0000629//=== Helper functions for higher up the class hierarchy.
Chris Lattner9dbb4292009-11-09 23:28:39 +0000630
Chris Lattnerd06094f2009-11-10 00:55:12 +0000631/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
632/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000633static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
634 const TargetData *TD, unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +0000635 switch (Opcode) {
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000636 case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, MaxRecurse);
637 case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD, MaxRecurse);
Chris Lattnerd06094f2009-11-10 00:55:12 +0000638 default:
639 if (Constant *CLHS = dyn_cast<Constant>(LHS))
640 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
641 Constant *COps[] = {CLHS, CRHS};
642 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
643 }
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000644
645 // If the operation is with the result of a select instruction, check whether
646 // operating on either branch of the select always yields the same value.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000647 if (MaxRecurse && (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)))
648 if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, MaxRecurse-1))
649 return V;
650
651 // If the operation is with the result of a phi instruction, check whether
652 // operating on all incoming values of the phi always yields the same value.
653 if (MaxRecurse && (isa<PHINode>(LHS) || isa<PHINode>(RHS)))
654 if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, MaxRecurse-1))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000655 return V;
656
Chris Lattnerd06094f2009-11-10 00:55:12 +0000657 return 0;
658 }
659}
Chris Lattner9dbb4292009-11-09 23:28:39 +0000660
Duncan Sands12a86f52010-11-14 11:23:23 +0000661Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000662 const TargetData *TD) {
663 return ::SimplifyBinOp(Opcode, LHS, RHS, TD, MaxRecursionDepth);
Chris Lattner9dbb4292009-11-09 23:28:39 +0000664}
665
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000666/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
667/// fold the result.
668static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
669 const TargetData *TD, unsigned MaxRecurse) {
670 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
671 return SimplifyICmpInst(Predicate, LHS, RHS, TD, MaxRecurse);
672 return SimplifyFCmpInst(Predicate, LHS, RHS, TD, MaxRecurse);
673}
674
675Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
676 const TargetData *TD) {
677 return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, MaxRecursionDepth);
678}
Chris Lattnere3453782009-11-10 01:08:51 +0000679
680/// SimplifyInstruction - See if we can compute a simplified version of this
681/// instruction. If not, this returns null.
Duncan Sandseff05812010-11-14 18:36:10 +0000682Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD,
683 const DominatorTree *DT) {
Chris Lattnere3453782009-11-10 01:08:51 +0000684 switch (I->getOpcode()) {
685 default:
686 return ConstantFoldInstruction(I, TD);
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000687 case Instruction::Add:
Owen Anderson4e282de2010-09-16 20:51:41 +0000688 return SimplifyAddInst(I->getOperand(0), I->getOperand(1),
689 cast<BinaryOperator>(I)->hasNoSignedWrap(),
690 cast<BinaryOperator>(I)->hasNoUnsignedWrap(), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000691 case Instruction::And:
Owen Anderson4e282de2010-09-16 20:51:41 +0000692 return SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000693 case Instruction::Or:
Owen Anderson4e282de2010-09-16 20:51:41 +0000694 return SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000695 case Instruction::ICmp:
Owen Anderson4e282de2010-09-16 20:51:41 +0000696 return SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
697 I->getOperand(0), I->getOperand(1), TD);
Chris Lattnere3453782009-11-10 01:08:51 +0000698 case Instruction::FCmp:
Owen Anderson4e282de2010-09-16 20:51:41 +0000699 return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
700 I->getOperand(0), I->getOperand(1), TD);
Chris Lattner04754262010-04-20 05:32:14 +0000701 case Instruction::Select:
Owen Anderson4e282de2010-09-16 20:51:41 +0000702 return SimplifySelectInst(I->getOperand(0), I->getOperand(1),
Chris Lattner04754262010-04-20 05:32:14 +0000703 I->getOperand(2), TD);
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000704 case Instruction::GetElementPtr: {
705 SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
Owen Anderson4e282de2010-09-16 20:51:41 +0000706 return SimplifyGEPInst(&Ops[0], Ops.size(), TD);
Chris Lattnerc514c1f2009-11-27 00:29:05 +0000707 }
Duncan Sandscd6636c2010-11-14 13:30:18 +0000708 case Instruction::PHI:
Duncan Sandseff05812010-11-14 18:36:10 +0000709 return cast<PHINode>(I)->hasConstantValue(DT);
Chris Lattnere3453782009-11-10 01:08:51 +0000710 }
711}
712
Chris Lattner40d8c282009-11-10 22:26:15 +0000713/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
714/// delete the From instruction. In addition to a basic RAUW, this does a
715/// recursive simplification of the newly formed instructions. This catches
716/// things where one simplification exposes other opportunities. This only
717/// simplifies and deletes scalar operations, it does not change the CFG.
718///
719void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
Duncan Sandseff05812010-11-14 18:36:10 +0000720 const TargetData *TD,
721 const DominatorTree *DT) {
Chris Lattner40d8c282009-11-10 22:26:15 +0000722 assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
Duncan Sands12a86f52010-11-14 11:23:23 +0000723
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000724 // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
725 // we can know if it gets deleted out from under us or replaced in a
726 // recursive simplification.
Chris Lattner40d8c282009-11-10 22:26:15 +0000727 WeakVH FromHandle(From);
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000728 WeakVH ToHandle(To);
Duncan Sands12a86f52010-11-14 11:23:23 +0000729
Chris Lattner40d8c282009-11-10 22:26:15 +0000730 while (!From->use_empty()) {
731 // Update the instruction to use the new value.
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000732 Use &TheUse = From->use_begin().getUse();
733 Instruction *User = cast<Instruction>(TheUse.getUser());
734 TheUse = To;
735
736 // Check to see if the instruction can be folded due to the operand
737 // replacement. For example changing (or X, Y) into (or X, -1) can replace
738 // the 'or' with -1.
739 Value *SimplifiedVal;
740 {
741 // Sanity check to make sure 'User' doesn't dangle across
742 // SimplifyInstruction.
743 AssertingVH<> UserHandle(User);
Duncan Sands12a86f52010-11-14 11:23:23 +0000744
Duncan Sandseff05812010-11-14 18:36:10 +0000745 SimplifiedVal = SimplifyInstruction(User, TD, DT);
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000746 if (SimplifiedVal == 0) continue;
Chris Lattner40d8c282009-11-10 22:26:15 +0000747 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000748
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000749 // Recursively simplify this user to the new value.
Duncan Sandseff05812010-11-14 18:36:10 +0000750 ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, DT);
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000751 From = dyn_cast_or_null<Instruction>((Value*)FromHandle);
752 To = ToHandle;
Duncan Sands12a86f52010-11-14 11:23:23 +0000753
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000754 assert(ToHandle && "To value deleted by recursive simplification?");
Duncan Sands12a86f52010-11-14 11:23:23 +0000755
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000756 // If the recursive simplification ended up revisiting and deleting
757 // 'From' then we're done.
758 if (From == 0)
759 return;
Chris Lattner40d8c282009-11-10 22:26:15 +0000760 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000761
Chris Lattnerd2bfe542010-07-15 06:36:08 +0000762 // If 'From' has value handles referring to it, do a real RAUW to update them.
763 From->replaceAllUsesWith(To);
Duncan Sands12a86f52010-11-14 11:23:23 +0000764
Chris Lattner40d8c282009-11-10 22:26:15 +0000765 From->eraseFromParent();
766}